Skip to content

Instantly share code, notes, and snippets.

@quelgar
Created March 10, 2017 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quelgar/6ce991d312eae83eeb0c6e84316af4be to your computer and use it in GitHub Desktop.
Save quelgar/6ce991d312eae83eeb0c6e84316af4be to your computer and use it in GitHub Desktop.
Basic GADT in Scala
// Scala version of code from https://en.wikipedia.org/wiki/Generalized_algebraic_data_type
sealed abstract class Expr[A]
final case class EBool(a: Boolean) extends Expr[Boolean]
final case class EInt(a: Int) extends Expr[Int]
final case class EEqual(a: Expr[Int], b: Expr[Int]) extends Expr[Boolean]
def eval[A](e: Expr[A]): A = e match {
case EBool(a) => a
case EInt(a) => a
case EEqual(a, b) => eval(a) == eval(b)
}
val expr1 = EEqual(EInt(2), EInt(3))
val ret = eval(expr1)
val ret2 = eval(EEqual(EInt(4), EInt(4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment