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
|
use std::iter::FromIterator;
use crate::syntax::*;
/// A visitor trait that can be used to traverse `ExprKind`s. We need this pattern so that Rust lets
/// us have as much mutability as we can.
/// For example, `traverse_ref_with_special_handling_of_binders` cannot be made using only
/// `traverse_ref`, because `traverse_ref` takes a `FnMut` so we would need to pass multiple
/// mutable reverences to this argument to `traverse_ref`. But Rust's ownership system is all about
/// preventing exactly this ! So we have to be more clever. The visitor pattern allows us to have
/// only one mutable thing the whole time: the visitor itself. The visitor can then carry around
/// multiple closures or just one, and Rust is ok with either. See for example TraverseRefVisitor.
pub trait ExprKindVisitor<'a, SE1, SE2, E1, E2>: Sized {
type Error;
fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error>;
fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error>;
fn visit_subexpr_under_binder(
mut self,
_label: &'a Label,
subexpr: &'a SE1,
) -> Result<SE2, Self::Error> {
self.visit_subexpr(subexpr)
}
fn visit(
self,
input: &'a ExprKind<SE1, E1>,
) -> Result<ExprKind<SE2, E2>, Self::Error> {
visit_ref(self, input)
}
}
fn visit_ref<'a, V, SE1, SE2, E1, E2>(
mut v: V,
input: &'a ExprKind<SE1, E1>,
) -> Result<ExprKind<SE2, E2>, V::Error>
where
V: ExprKindVisitor<'a, SE1, SE2, E1, E2>,
{
fn vec<'a, T, U, Err, F: FnMut(&'a T) -> Result<U, Err>>(
x: &'a [T],
f: F,
) -> Result<Vec<U>, Err> {
x.iter().map(f).collect()
}
fn opt<'a, T, U, Err, F: FnOnce(&'a T) -> Result<U, Err>>(
x: &'a Option<T>,
f: F,
) -> Result<Option<U>, Err> {
Ok(match x {
Some(x) => Some(f(x)?),
None => None,
})
}
fn dupmap<'a, V, SE1, SE2, E1, E2, T>(
x: impl IntoIterator<Item = (&'a Label, &'a SE1)>,
mut v: V,
) -> Result<T, V::Error>
where
SE1: 'a,
T: FromIterator<(Label, SE2)>,
V: ExprKindVisitor<'a, SE1, SE2, E1, E2>,
{
x.into_iter()
.map(|(k, x)| Ok((k.clone(), v.visit_subexpr(x)?)))
.collect()
}
fn optdupmap<'a, V, SE1, SE2, E1, E2, T>(
x: impl IntoIterator<Item = (&'a Label, &'a Option<SE1>)>,
mut v: V,
) -> Result<T, V::Error>
where
SE1: 'a,
T: FromIterator<(Label, Option<SE2>)>,
V: ExprKindVisitor<'a, SE1, SE2, E1, E2>,
{
x.into_iter()
.map(|(k, x)| {
Ok((
k.clone(),
match x {
Some(x) => Some(v.visit_subexpr(x)?),
None => None,
},
))
})
.collect()
}
use crate::syntax::ExprKind::*;
Ok(match input {
Var(v) => Var(v.clone()),
Lam(l, t, e) => {
let t = v.visit_subexpr(t)?;
let e = v.visit_subexpr_under_binder(l, e)?;
Lam(l.clone(), t, e)
}
Pi(l, t, e) => {
let t = v.visit_subexpr(t)?;
let e = v.visit_subexpr_under_binder(l, e)?;
Pi(l.clone(), t, e)
}
Let(l, t, a, e) => {
let t = opt(t, &mut |e| v.visit_subexpr(e))?;
let a = v.visit_subexpr(a)?;
let e = v.visit_subexpr_under_binder(l, e)?;
Let(l.clone(), t, a, e)
}
App(f, a) => App(v.visit_subexpr(f)?, v.visit_subexpr(a)?),
Annot(x, t) => Annot(v.visit_subexpr(x)?, v.visit_subexpr(t)?),
Const(k) => Const(*k),
Builtin(v) => Builtin(*v),
BoolLit(b) => BoolLit(*b),
NaturalLit(n) => NaturalLit(*n),
IntegerLit(n) => IntegerLit(*n),
DoubleLit(n) => DoubleLit(*n),
TextLit(t) => TextLit(t.traverse_ref(|e| v.visit_subexpr(e))?),
BinOp(o, x, y) => BinOp(*o, v.visit_subexpr(x)?, v.visit_subexpr(y)?),
BoolIf(b, t, f) => BoolIf(
v.visit_subexpr(b)?,
v.visit_subexpr(t)?,
v.visit_subexpr(f)?,
),
EmptyListLit(t) => EmptyListLit(v.visit_subexpr(t)?),
NEListLit(es) => NEListLit(vec(es, |e| v.visit_subexpr(e))?),
SomeLit(e) => SomeLit(v.visit_subexpr(e)?),
RecordType(kts) => RecordType(dupmap(kts, v)?),
RecordLit(kvs) => RecordLit(dupmap(kvs, v)?),
UnionType(kts) => UnionType(optdupmap(kts, v)?),
Merge(x, y, t) => Merge(
v.visit_subexpr(x)?,
v.visit_subexpr(y)?,
opt(t, |e| v.visit_subexpr(e))?,
),
ToMap(x, t) => {
ToMap(v.visit_subexpr(x)?, opt(t, |e| v.visit_subexpr(e))?)
}
Field(e, l) => Field(v.visit_subexpr(e)?, l.clone()),
Projection(e, ls) => Projection(v.visit_subexpr(e)?, ls.clone()),
ProjectionByExpr(e, x) => {
ProjectionByExpr(v.visit_subexpr(e)?, v.visit_subexpr(x)?)
}
Completion(e, x) => {
Completion(v.visit_subexpr(e)?, v.visit_subexpr(x)?)
}
Assert(e) => Assert(v.visit_subexpr(e)?),
Import(i) => Import(i.traverse_ref(|e| v.visit_subexpr(e))?),
Embed(a) => Embed(v.visit_embed(a)?),
})
}
pub struct TraverseRefMaybeBinderVisitor<F>(pub F);
impl<'a, SE, E, SE2, Err, F> ExprKindVisitor<'a, SE, SE2, E, E>
for TraverseRefMaybeBinderVisitor<F>
where
SE: 'a,
E: 'a + Clone,
F: FnMut(Option<&'a Label>, &'a SE) -> Result<SE2, Err>,
{
type Error = Err;
fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result<SE2, Self::Error> {
(self.0)(None, subexpr)
}
fn visit_subexpr_under_binder(
mut self,
label: &'a Label,
subexpr: &'a SE,
) -> Result<SE2, Self::Error> {
(self.0)(Some(label), subexpr)
}
fn visit_embed(self, embed: &'a E) -> Result<E, Self::Error> {
Ok(embed.clone())
}
}
|