Added a few helper functions for constructing terms
Getty Ritter
10 years ago
1 | #![feature(macro_rules)] | |
2 | ||
1 | 3 | #[deriving(Eq,PartialEq,Clone,Show)] |
2 | 4 | enum Term { |
3 | 5 | Num(int), |
5 | 7 | Lam(String, Box<Term>), |
6 | 8 | App(Box<Term>, Box<Term>), |
7 | 9 | Let(String, Box<Term>, Box<Term>), |
10 | } | |
11 | ||
12 | fn num(n: int) -> Box<Term> { | |
13 | box Num(n) | |
14 | } | |
15 | ||
16 | fn var(s: &str) -> Box<Term> { | |
17 | box Var(s.to_string()) | |
18 | } | |
19 | ||
20 | fn lam(x: &str, n: Box<Term>) -> Box<Term> { | |
21 | box Lam(x.to_string(), n) | |
22 | } | |
23 | ||
24 | fn app(x: Box<Term>, y: Box<Term>) -> Box<Term> { | |
25 | box App(x, y) | |
26 | } | |
27 | ||
28 | fn let_(x: &str, y: Box<Term>, z: Box<Term>) -> Box<Term> { | |
29 | box Let(x.to_string(), y, z) | |
8 | 30 | } |
9 | 31 | |
10 | 32 | #[deriving(Eq,PartialEq,Clone,Show)] |
59 | 81 | |
60 | 82 | fn main() { |
61 | 83 | // (λx.λy.x)(5)(6) |
62 | let s1 = box App(box App(box Lam("x".to_string(), | |
63 | box Lam("y".to_string(), | |
64 | box Var("x".to_string()))), | |
65 | box Num(5)), | |
66 |
|
|
84 | let s1 = app(app(lam("x", lam("y", var("x"))), | |
85 | num(5)), | |
86 | num(6)); | |
67 | 87 | // let f = (λx.λy.x)(2) in f 4 |
68 | let s2 = box | |
69 | Let("f".to_string(), | |
70 | box App(box Lam("x".to_string(), | |
71 | box Lam("y".to_string(), | |
72 | box Var("x".to_string()))), | |
73 | box Num(2)), | |
74 | box App(box Var("f".to_string()), | |
75 | box Num(4)) | |
76 |
|
|
88 | let s2 = let_("f", | |
89 | app(lam("x", lam("y", var("x"))), | |
90 | num(2)), | |
91 | app(var("f"), | |
92 | num(4))); | |
77 | 93 | let e = Empty; |
78 | 94 | println!("s1: {:}", lcalc_eval(&*s1, &e)); |
79 | 95 | println!("s2: {:}", lcalc_eval(&*s2, &e)); |