Created
April 2, 2012 16:27
-
-
Save purefn/2284779 to your computer and use it in GitHub Desktop.
Encoding Product and Coproduct in Scala
This file contains hidden or 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
type Product[F[_], G[_], A] = (F[A], G[A]) | |
trait Prod[F[_], G[_]] { | |
type and[H[_]] = Prod[F, ({type λ[α] = Prod[G, H, α]})#λ] | |
type apply[A] = (F[A], G[A]) | |
} | |
type product[F[_], G[_]] = Prod[F, G] | |
type Coproduct[F[_], G[_], A] = Either[F[A], G[A]] | |
trait Coprod[F[_], G[_]] { | |
type or[H[_]] = Coprod[F, ({type λ[α] = Corod[G, H, α]})#λ] | |
type apply[A] = Either[F[A], G[A]] | |
} | |
type coproduct[F[_], G[_]] = Coprod[F, G] | |
// the encoding for `Val :+: Add :+: Mult :+: Div` from "Data types a la carte" would be | |
type Arithmetic[A] = coproduct[Val, Add]#or[Mult]#or[Div]#apply[A] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment