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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
mod convert;
mod display;
mod print;
use std::collections::{BTreeMap, BTreeSet};
use symbol::Symbol;
pub use ast::print::PrintStyle;
#[derive(Clone, Debug, PartialEq)]
pub struct Decl<Aux> {
pub name: Symbol,
pub args: Vec<Pattern<Aux>>,
pub body: Expr<Aux>,
pub aux: Aux,
}
impl<Aux> Decl<Aux> {
pub fn aux_ref(&self) -> &Aux {
&self.aux
}
pub fn freevars(&self) -> BTreeSet<Symbol> {
let mut vars = self.body.freevars();
for arg in &self.args {
for var in arg.freevars() {
vars.remove(&var);
}
}
vars.remove(&self.name);
vars
}
pub fn map_aux<Aux2, F: Copy + Fn(Aux) -> Aux2>(self, f: F) -> Decl<Aux2> {
Decl {
name: self.name,
args: self.args.into_iter().map(|arg| arg.map_aux(f)).collect(),
body: self.body.map_aux(f),
aux: f(self.aux),
}
}
}
impl<Aux: Clone> Decl<Aux> {
pub fn aux(&self) -> Aux {
self.aux_ref().clone()
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Pattern<Aux> {
Binding(Symbol, Aux),
Cons(Box<Pattern<Aux>>, Box<Pattern<Aux>>, Aux),
Literal(Literal, Aux),
}
impl<Aux> Pattern<Aux> {
pub fn aux_ref(&self) -> &Aux {
match *self {
Pattern::Binding(_, ref aux)
| Pattern::Cons(_, _, ref aux)
| Pattern::Literal(_, ref aux) => aux,
}
}
pub fn freevars(&self) -> BTreeSet<Symbol> {
match *self {
Pattern::Binding(var, _) => {
let mut set = BTreeSet::new();
set.insert(var);
set
}
Pattern::Cons(ref l, ref r, _) => {
l.freevars().into_iter().chain(r.freevars()).collect()
}
Pattern::Literal(_, _) => BTreeSet::new(),
}
}
pub fn map_aux<Aux2, F: Copy + Fn(Aux) -> Aux2>(self, f: F) -> Pattern<Aux2> {
match self {
Pattern::Binding(var, aux) => Pattern::Binding(var, f(aux)),
Pattern::Cons(l, r, aux) => {
Pattern::Cons(Box::new(l.map_aux(f)), Box::new(r.map_aux(f)), f(aux))
}
Pattern::Literal(lit, aux) => Pattern::Literal(lit, f(aux)),
}
}
}
impl<Aux: Clone> Pattern<Aux> {
pub fn matches(&self, expr: &Expr<Aux>) -> Option<BTreeMap<Symbol, Expr<Aux>>> {
match (self, expr) {
(&Pattern::Binding(var, _), e) => {
let mut map = BTreeMap::new();
map.insert(var, e.clone());
Some(map)
}
(&Pattern::Cons(ref pl, ref pr, _), &Expr::Op(Op::Cons, ref el, ref er, _)) => {
let mut lm = pl.matches(el)?;
lm.extend(pr.matches(er)?);
Some(lm)
}
(&Pattern::Literal(l1, _), &Expr::Literal(l2, _)) if l1 == l2 => Some(BTreeMap::new()),
_ => None,
}
}
}
impl<Aux: Clone> Pattern<Aux> {
pub fn aux(&self) -> Aux {
self.aux_ref().clone()
}
}
#[derive(Clone, Debug, DisplayAttr, PartialEq)]
pub enum Expr<Aux> {
#[display(fmt = "If({}, {}, {})", _0, _1, _2)]
If(Box<Expr<Aux>>, Box<Expr<Aux>>, Box<Expr<Aux>>, Aux),
#[display(fmt = "{}", _0)]
Literal(Literal, Aux),
#[display(fmt = "{}({}, {})", _0, _1, _2)]
Op(Op, Box<Expr<Aux>>, Box<Expr<Aux>>, Aux),
#[display(fmt = "{}", _0)]
Variable(Symbol, Aux),
}
impl<Aux> Expr<Aux> {
pub fn aux_ref(&self) -> &Aux {
match *self {
Expr::If(_, _, _, ref aux)
| Expr::Literal(_, ref aux)
| Expr::Op(_, _, _, ref aux)
| Expr::Variable(_, ref aux) => aux,
}
}
pub fn free_count(&self) -> BTreeMap<Symbol, usize> {
match *self {
Expr::If(ref c, ref t, ref e, _) => {
let mut map = c.free_count();
for (var, count) in t.free_count().into_iter().chain(e.free_count()) {
*map.entry(var).or_insert(0) += count;
}
map
}
Expr::Literal(_, _) => BTreeMap::new(),
Expr::Op(_, ref l, ref r, _) => {
let mut map = l.free_count();
for (var, count) in r.free_count() {
*map.entry(var).or_insert(0) += count;
}
map
}
Expr::Variable(name, _) => {
let mut map = BTreeMap::new();
map.insert(name, 1);
map
}
}
}
pub fn freevars(&self) -> BTreeSet<Symbol> {
match *self {
Expr::If(ref c, ref t, ref e, _) => c.freevars()
.into_iter()
.chain(t.freevars())
.chain(e.freevars())
.collect(),
Expr::Literal(_, _) => BTreeSet::new(),
Expr::Op(_, ref l, ref r, _) => l.freevars().into_iter().chain(r.freevars()).collect(),
Expr::Variable(var, _) => {
let mut set = BTreeSet::new();
set.insert(var);
set
}
}
}
pub fn map_aux<Aux2, F: Copy + Fn(Aux) -> Aux2>(self, f: F) -> Expr<Aux2> {
match self {
Expr::If(c, t, e, aux) => Expr::If(
Box::new(c.map_aux(f)),
Box::new(t.map_aux(f)),
Box::new(e.map_aux(f)),
f(aux),
),
Expr::Literal(lit, aux) => Expr::Literal(lit, f(aux)),
Expr::Op(op, l, r, aux) => {
Expr::Op(op, Box::new(l.map_aux(f)), Box::new(r.map_aux(f)), f(aux))
}
Expr::Variable(var, aux) => Expr::Variable(var, f(aux)),
}
}
}
impl<Aux: Clone> Expr<Aux> {
pub fn aux(&self) -> Aux {
self.aux_ref().clone()
}
}
#[derive(Clone, Copy, Debug, DisplayAttr, PartialEq)]
#[allow(missing_docs)]
pub enum Op {
#[display(fmt = "Add")]
Add,
#[display(fmt = "App")]
App,
#[display(fmt = "Cons")]
Cons,
#[display(fmt = "Div")]
Div,
#[display(fmt = "Mod")]
Mod,
#[display(fmt = "Mul")]
Mul,
#[display(fmt = "Sub")]
Sub,
}
#[derive(Clone, Copy, Debug, DisplayAttr, PartialEq)]
pub enum Literal {
#[display(fmt = "false")]
False,
#[display(fmt = "{}", _0)]
Int(usize),
#[display(fmt = "[]")]
Nil,
#[display(fmt = "true")]
True,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Type {
Bool,
Forall(Box<Type>),
Func(Box<Type>, Box<Type>),
Int,
List(Box<Type>),
Var(usize),
}
impl Type {
pub fn argn(&self) -> usize {
match *self {
Type::Bool | Type::Int | Type::List(_) | Type::Var(_) => 0,
Type::Forall(ref ty) => ty.argn(),
Type::Func(_, ref r) => 1 + r.argn(),
}
}
}