44
|
1 |
package Main
|
|
2 |
|
|
3 |
abstract class Term
|
|
4 |
case class Var(name: String) extends Term
|
|
5 |
case class Fun(arg: String, body: Term) extends Term
|
|
6 |
case class Appli(f: Term, v: Term) extends Term
|
|
7 |
|
|
8 |
object TermTest extends App {
|
|
9 |
def printTerm(term: Term) {
|
|
10 |
term match {
|
|
11 |
case Var(n) =>
|
|
12 |
print("" + n)
|
|
13 |
case Fun(x, b) =>
|
|
14 |
print("^" + x + ".")
|
|
15 |
printTerm(b)
|
|
16 |
case Appli(f, v) =>
|
|
17 |
Console.print("(")
|
|
18 |
printTerm(f)
|
|
19 |
print(" ")
|
|
20 |
printTerm(v)
|
|
21 |
print(")")
|
|
22 |
}
|
|
23 |
}
|
|
24 |
def isIdentityFun(term: Term): Boolean = term match {
|
|
25 |
case Fun(x, Var(y)) if x == y => true
|
|
26 |
case _ => false
|
|
27 |
}
|
|
28 |
val id = Fun("x", Var("x"))
|
|
29 |
println("id: " + id)
|
|
30 |
val t = Fun("x", Fun("y", Appli(Var("x"), Var("y"))))
|
|
31 |
println("t: " + t)
|
|
32 |
printTerm(t)
|
|
33 |
println
|
|
34 |
println(isIdentityFun(id))
|
|
35 |
println(isIdentityFun(t))
|
|
36 |
}
|