Skip to content

Instantly share code, notes, and snippets.

@mandubian
Last active November 13, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/1852cd98f20f55c34d5f5b330b56ef3a to your computer and use it in GitHub Desktop.
Save mandubian/1852cd98f20f55c34d5f5b330b56ef3a to your computer and use it in GitHub Desktop.
// a very basic Scala function performing a simple operation on primitive type Int
def plus = (x:Int) => (y:Int) => x + y
// First, we need to reify our CCC world
val K = implicitly[CartesianClosedCat[Function1]]
// now we import CCC language into our context
import K._
// pseudo-code
// we can always uncurry curried function
uncurry((x:Int) => (y:Int) => x + y) ≌ (x:Int, y:Int) => x + y
// remind projections
(x:Int, y:Int) => x ≌ exl[Int, Int]
(x:Int, y:Int) => y ≌ exr[Int, Int]
// now use product
{ (x:Int, y:Int) => x } ⨂ { (x:Int, y:Int) => y } ≌ (x:Int, y:Int) => (x, y)
// using projections it can be rewritten
exl[Int, Int] ⨂ exr[Int, Int] ≌ (x:Int, y:Int) => (x, y)
// now compose with + : (Int, Int) => Int
( + ) ○ { (x:Int, y:Int) => (x, y) } ≌ x + y
// or
plus ≌ ( + ) ○ (exl[Int, Int] ⨂ exr[Int, Int])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment