Skip to content

Instantly share code, notes, and snippets.

@mandubian
Created November 3, 2017 16:11
Show Gist options
  • Save mandubian/ee591f26aa5bcfc46518bf065225e56f to your computer and use it in GitHub Desktop.
Save mandubian/ee591f26aa5bcfc46518bf065225e56f to your computer and use it in GitHub Desktop.
Yoneda Isomorphism between type A and the continuation/handler pattern
// Yoneda lemma
// - Yoneda Embedding is just another case of the lemma
// - implies that:
// forall r . (a -> r) -> r ≅ a
// Any type can be replaced (& back) by a continuation accepting a handler
trait Cont[A] {
// handler which accepts a A and performs the rest of computation (like returningn a status R)
def apply[R](handler: A => R): R
}
object Cont {
def fromA[A](a: A) = new Cont[A] {
def apply[R](handler: A => R): R = handler(a)
}
def toA[A](c: Cont[A]): A = c(identity)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment