added file docs/examples/gadts.scala
git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@6647 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
parent
edc39356b7
commit
0e931fb2b5
|
@ -0,0 +1,22 @@
|
|||
package examples
|
||||
|
||||
object gadts extends Application {
|
||||
|
||||
abstract class Term[T]
|
||||
case class Lit(x: int) extends Term[int]
|
||||
case class Succ(t: Term[int]) extends Term[int]
|
||||
case class IsZero(t: Term[int]) extends Term[boolean]
|
||||
case class If[T](c: Term[boolean],
|
||||
t1: Term[T],
|
||||
t2: Term[T]) extends Term[T]
|
||||
|
||||
def eval[T](t: Term[T]): T = t match {
|
||||
case Lit(n) => n
|
||||
case Succ(u) => eval(u) + 1
|
||||
case IsZero(u) => eval(u) == 0
|
||||
case If(c, u1, u2) => eval(if (eval(c)) u1 else u2)
|
||||
}
|
||||
Console.println(
|
||||
eval(If(IsZero(Lit(1)), Lit(41), Succ(Lit(41)))))
|
||||
}
|
||||
|
Loading…
Reference in New Issue