/
lc_let.rs
162 lines (144 loc) · 4.89 KB
/
lc_let.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! An example of using the `moniker` library to implement the untyped lambda
//! calculus with nested let bindings
#[macro_use]
extern crate moniker;
use moniker::{Binder, Embed, Nest, Scope, Var};
use std::rc::Rc;
/// Expressions
///
/// ```text
/// e ::= x variables
/// | \x => e anonymous functions
/// | e₁ e₂ function application
/// | let x₁=e₁, ..., xₙ=eₙ in e nested let bindings
/// ````
#[derive(Debug, Clone, BoundTerm)]
pub enum Expr {
/// Variables
Var(Var<String>),
/// Lambda expressions
Lam(Scope<Binder<String>, RcExpr>),
/// Function application
App(RcExpr, RcExpr),
/// Nested let bindings
Let(Scope<Nest<(Binder<String>, Embed<RcExpr>)>, RcExpr>),
}
/// Reference counted expressions
#[derive(Debug, Clone, BoundTerm)]
pub struct RcExpr {
pub inner: Rc<Expr>,
}
impl From<Expr> for RcExpr {
fn from(src: Expr) -> RcExpr {
RcExpr {
inner: Rc::new(src),
}
}
}
impl RcExpr {
// FIXME: auto-derive this somehow!
fn substs<N: PartialEq<Var<String>>>(&self, mappings: &[(N, RcExpr)]) -> RcExpr {
match *self.inner {
Expr::Var(ref var) => match mappings.iter().find(|&(name, _)| name == var) {
Some((_, ref replacement)) => replacement.clone(),
None => self.clone(),
},
Expr::Lam(ref scope) => RcExpr::from(Expr::Lam(Scope {
unsafe_pattern: scope.unsafe_pattern.clone(),
unsafe_body: scope.unsafe_body.substs(mappings),
})),
Expr::App(ref fun, ref arg) => {
RcExpr::from(Expr::App(fun.substs(mappings), arg.substs(mappings)))
},
Expr::Let(ref scope) => RcExpr::from(Expr::Let(Scope {
unsafe_pattern: Nest {
unsafe_patterns: scope
.unsafe_pattern
.unsafe_patterns
.iter()
.map(|&(ref n, Embed(ref value))| {
(n.clone(), Embed(value.substs(mappings)))
})
.collect(),
},
unsafe_body: scope.unsafe_body.substs(mappings),
})),
}
}
}
/// Evaluate an expression into its normal form
pub fn eval(expr: &RcExpr) -> RcExpr {
match *expr.inner {
Expr::Var(_) | Expr::Lam(_) => expr.clone(),
Expr::App(ref fun, ref arg) => match *eval(fun).inner {
Expr::Lam(ref scope) => {
let (binder, body) = scope.clone().unbind();
eval(&body.substs(&[(binder, eval(arg))]))
},
_ => expr.clone(),
},
Expr::Let(ref scope) => {
let (bindings, body) = scope.clone().unbind();
let mut mappings = Vec::with_capacity(bindings.unsafe_patterns.len());
for (binder, Embed(value)) in bindings.unnest() {
let value = eval(&value.substs(&mappings));
mappings.push((binder, value));
}
eval(&body.substs(&mappings))
},
}
}
#[test]
fn test_eval() {
use moniker::FreeVar;
let x = FreeVar::fresh_named("x");
let y = FreeVar::fresh_named("y");
// expr = (\x -> x) y
let expr = RcExpr::from(Expr::App(
RcExpr::from(Expr::Lam(Scope::new(
Binder(x.clone()),
RcExpr::from(Expr::Var(Var::Free(x.clone()))),
))),
RcExpr::from(Expr::Var(Var::Free(y.clone()))),
));
assert_term_eq!(eval(&expr), RcExpr::from(Expr::Var(Var::Free(y.clone()))));
}
#[test]
fn test_eval_let() {
use moniker::FreeVar;
let x = FreeVar::fresh_named("x");
let y = FreeVar::fresh_named("y");
let id = FreeVar::fresh_named("id");
let foo = FreeVar::fresh_named("foo");
let bar = FreeVar::fresh_named("bar");
// expr =
// let id = \x -> x
// foo = y
// bar = id foo
// in bar
let expr = RcExpr::from(Expr::Let(Scope::new(
Nest::new(vec![
(
Binder(id.clone()),
Embed(RcExpr::from(Expr::Lam(Scope::new(
Binder(x.clone()),
RcExpr::from(Expr::Var(Var::Free(x.clone()))),
)))),
),
(
Binder(foo.clone()),
Embed(RcExpr::from(Expr::Var(Var::Free(y.clone())))),
),
(
Binder(bar.clone()),
Embed(RcExpr::from(Expr::App(
RcExpr::from(Expr::Var(Var::Free(id.clone()))),
RcExpr::from(Expr::Var(Var::Free(foo.clone()))),
))),
),
]),
RcExpr::from(Expr::Var(Var::Free(bar.clone()))),
)));
assert_term_eq!(eval(&expr), RcExpr::from(Expr::Var(Var::Free(y.clone()))),);
}
fn main() {}