Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Last active March 13, 2018 11:28
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchiusano/10678834 to your computer and use it in GitHub Desktop.
Save pchiusano/10678834 to your computer and use it in GitHub Desktop.
Finally tagless encoding of GADTs in Scala
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
def run[F[+_]](F: ConsoleAlg[F]): F[A]
}
object Console {
val readLine: Console[Option[String]] = new Console[Option[String]] {
def run[F[+_]](F: ConsoleAlg[F]): F[Option[String]] =
F.readLine
}
def printLine(line: String): Console[Unit] = new Console[Unit] {
def run[F[+_]](F: ConsoleAlg[F]): F[Unit] = F.printLine(line)
}
}
// more generally
trait Term[Alg[_[_]], +A] {
def apply[F[_]](A: Alg[F]): F[A]
}
object Term {
def readLine: Term[ConsoleAlg, Option[String]] = new Term[ConsoleAlg, Option[String]] {
def apply[F[_]](A: ConsoleAlg[F]): F[Option[String]] = A.readLine
}
// etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment