gdritter repos rust-examples / 1d06911
Added a few helper functions for constructing terms Getty Ritter 10 years ago
1 changed file(s) with 30 addition(s) and 14 deletion(s). Collapse all Expand all
1 #![feature(macro_rules)]
2
13 #[deriving(Eq,PartialEq,Clone,Show)]
24 enum Term {
35 Num(int),
57 Lam(String, Box<Term>),
68 App(Box<Term>, Box<Term>),
79 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)
830 }
931
1032 #[deriving(Eq,PartialEq,Clone,Show)]
5981
6082 fn main() {
6183 // (λ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 box Num(8));
84 let s1 = app(app(lam("x", lam("y", var("x"))),
85 num(5)),
86 num(6));
6787 // 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)));
7793 let e = Empty;
7894 println!("s1: {:}", lcalc_eval(&*s1, &e));
7995 println!("s2: {:}", lcalc_eval(&*s2, &e));