Skip to content

Instantly share code, notes, and snippets.

@purefn
Created April 2, 2012 16:27
Show Gist options
  • Save purefn/2284779 to your computer and use it in GitHub Desktop.
Save purefn/2284779 to your computer and use it in GitHub Desktop.
Encoding Product and Coproduct in Scala
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