1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::ops::{Add, Mul};
use crate::{Expression, Abstraction, Application, Variable};
impl From<u64> for Expression {
fn from(n: u64) -> Self {
let succ = λ!{n.λ!{f.λ!{x.γ!(f, γ!(γ!(n, f), x))}}};
let mut e = λ!{f.λ!{x.x}};
for _ in 0..n {
e = app!({&succ}, {&e}).normalize(false);
}
e
}
}
impl From<Expression> for u64 {
fn from(e: Expression) -> u64 {
match e.normalize(true) {
Expression::Var(id) => {
if id == variable!(f) { 1 } else { 0 }
},
Expression::Abs(Abstraction(Variable(_id), box body)) => {
u64::from(body)
},
Expression::App(Application(box e1, box e2)) => {
u64::from(e1) + u64::from(e2)
},
}
}
}
impl Add for Expression {
type Output = Self;
fn add(self, other: Self) -> Self {
let add = λ!{m.λ!{n.λ!{f.λ!{x.γ!(γ!(m,f),γ!(γ!(n,f),x))}}}};
γ!(γ!({add},{self}),{other}).normalize(false)
}
}
impl Mul for Expression {
type Output = Self;
fn mul(self, other: Self) -> Self {
let mul = λ!{m.λ!{n.λ!{f.λ!{x.γ!(γ!(m,γ!(n,f)),x)}}}};
γ!(γ!({mul},{self}),{other}).normalize(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn u64() {
assert_eq!(0u64, Expression::from(0).into());
assert_eq!(5u64, Expression::from(5).into());
}
#[test]
fn add() {
assert_eq!(Expression::from(5), Expression::from(2) +
Expression::from(3));
}
#[test]
fn multiply() {
assert_eq!(Expression::from(6), Expression::from(2) * Expression::from(3));
}
}