Created
November 3, 2017 16:11
-
-
Save mandubian/ee591f26aa5bcfc46518bf065225e56f to your computer and use it in GitHub Desktop.
Yoneda Isomorphism between type A and the continuation/handler pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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