SHARE
TWEET

Untitled

a guest Nov 15th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a very basic Scala function performing a simple operation on primitive type Int
  2. def plus = (x:Int) => (y:Int) => x + y
  3.  
  4. // First, we need to reify our CCC world
  5. val K = implicitly[CartesianClosedCat[Function1]]
  6. // now we import CCC language into our context
  7. import K._
  8.  
  9. // pseudo-code
  10. // we can always uncurry curried function
  11. uncurry((x:Int) => (y:Int) => x + y) ≌ (x:Int, y:Int) => x + y
  12. // remind projections
  13. (x:Int, y:Int) => x ≌ exl[Int, Int]
  14. (x:Int, y:Int) => y ≌ exr[Int, Int]
  15. // now use product
  16. { (x:Int, y:Int) => x } ⨂ { (x:Int, y:Int) => y } ≌ (x:Int, y:Int) => (x, y)
  17. // using projections it can be rewritten
  18. exl[Int, Int] ⨂ exr[Int, Int] ≌ (x:Int, y:Int) => (x, y)
  19. // now compose with + : (Int, Int) => Int
  20. ( + ) ○ { (x:Int, y:Int) => (x, y) } ≌ x + y
  21. // or
  22. plus ≌ ( + ) ○ (exl[Int, Int] ⨂ exr[Int, Int])
RAW Paste Data
Top