Last active
March 13, 2018 11:28
-
-
Save pchiusano/10678834 to your computer and use it in GitHub Desktop.
Finally tagless encoding of GADTs 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
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