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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
use std::collections::BTreeMap;
use crate::*;
/// A way too generic Visitor trait.
pub trait GenericVisitor<Input, Return>: Sized {
fn visit(self, input: Input) -> Return;
}
/// A visitor trait that can be used to traverse `ExprF`s. We need this pattern
/// so that Rust lets us have as much mutability as we can.
/// For example, `traverse_embed` 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
/// and TraverseEmbedVisitor.
/// This is very generic. For a more legible trait, see ExprFInFallibleVisitor
pub trait ExprFVeryGenericVisitor<'a, Ret, SE1, L1, E1>: Sized {
type Error;
type SE2;
type L2;
type E2;
fn visit_subexpr(
&mut self,
subexpr: &'a SE1,
) -> Result<Self::SE2, Self::Error>;
fn visit_label(&mut self, label: &'a L1) -> Result<Self::L2, Self::Error>;
fn visit_binder(
self,
label: &'a L1,
subexpr: &'a SE1,
) -> Result<(Self::L2, Self::SE2), Self::Error>;
fn visit_embed_squash(self, embed: &'a E1) -> Result<Ret, Self::Error>;
// Called with the result of the map, in the non-embed case.
// Useful to change the result type, and/or avoid some loss of info
fn visit_resulting_exprf(
result: ExprF<Self::SE2, Self::L2, Self::E2>,
) -> Result<Ret, Self::Error>;
}
impl<'a, T, Ret, SE1, L1, E1>
GenericVisitor<&'a ExprF<SE1, L1, E1>, Result<Ret, T::Error>> for T
where
L1: Ord,
T::L2: Ord,
T: ExprFVeryGenericVisitor<'a, Ret, SE1, L1, E1>,
{
fn visit(self, input: &'a ExprF<SE1, L1, E1>) -> Result<Ret, T::Error> {
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 btmap<'a, V, Ret, SE, L, E>(
x: &'a BTreeMap<L, SE>,
mut v: V,
) -> Result<BTreeMap<V::L2, V::SE2>, V::Error>
where
L: Ord,
V::L2: Ord,
V: ExprFVeryGenericVisitor<'a, Ret, SE, L, E>,
{
x.iter()
.map(|(k, x)| Ok((v.visit_label(k)?, v.visit_subexpr(x)?)))
.collect()
}
fn btoptmap<'a, V, Ret, SE, L, E>(
x: &'a BTreeMap<L, Option<SE>>,
mut v: V,
) -> Result<BTreeMap<V::L2, Option<V::SE2>>, V::Error>
where
L: Ord,
V::L2: Ord,
V: ExprFVeryGenericVisitor<'a, Ret, SE, L, E>,
{
x.iter()
.map(|(k, x)| {
Ok((
v.visit_label(k)?,
match x {
Some(x) => Some(v.visit_subexpr(x)?),
None => None,
},
))
})
.collect()
}
let mut v = self;
use crate::ExprF::*;
T::visit_resulting_exprf(match input {
Var(V(l, n)) => Var(V(v.visit_label(l)?, *n)),
Lam(l, t, e) => {
let t = v.visit_subexpr(t)?;
let (l, e) = v.visit_binder(l, e)?;
Lam(l, t, e)
}
Pi(l, t, e) => {
let t = v.visit_subexpr(t)?;
let (l, e) = v.visit_binder(l, e)?;
Pi(l, t, e)
}
Let(l, t, a, e) => {
let t = opt(t, &mut |e| v.visit_subexpr(e))?;
let a = v.visit_subexpr(a)?;
let (l, e) = v.visit_binder(l, e)?;
Let(l, 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))?),
OldOptionalLit(x, t) => OldOptionalLit(
opt(x, |e| v.visit_subexpr(e))?,
v.visit_subexpr(t)?,
),
SomeLit(e) => SomeLit(v.visit_subexpr(e)?),
RecordType(kts) => RecordType(btmap(kts, v)?),
RecordLit(kvs) => RecordLit(btmap(kvs, v)?),
UnionType(kts) => UnionType(btoptmap(kts, v)?),
UnionLit(k, x, kts) => UnionLit(
v.visit_label(k)?,
v.visit_subexpr(x)?,
btoptmap(kts, v)?,
),
Merge(x, y, t) => Merge(
v.visit_subexpr(x)?,
v.visit_subexpr(y)?,
opt(t, |e| v.visit_subexpr(e))?,
),
Field(e, l) => Field(v.visit_subexpr(e)?, v.visit_label(l)?),
Projection(e, ls) => {
Projection(v.visit_subexpr(e)?, vec(ls, |l| v.visit_label(l))?)
}
Embed(a) => return v.visit_embed_squash(a),
})
}
}
/// Like ExprFVeryGenericVisitor, but sets the return
/// type to ExprF<_>
pub trait ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>: Sized {
type Error;
fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error>;
fn visit_label(&mut self, label: &'a L1) -> Result<L2, Self::Error>;
fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error>;
fn visit_subexpr_under_binder(
mut self,
_label: &'a L1,
subexpr: &'a SE1,
) -> Result<SE2, Self::Error> {
self.visit_subexpr(subexpr)
}
fn visit_binder(
mut self,
label: &'a L1,
subexpr: &'a SE1,
) -> Result<(L2, SE2), Self::Error> {
Ok((
self.visit_label(label)?,
self.visit_subexpr_under_binder(label, subexpr)?,
))
}
fn visit_embed_squash(
self,
embed: &'a E1,
) -> Result<ExprF<SE2, L2, E2>, Self::Error> {
Ok(ExprF::Embed(self.visit_embed(embed)?))
}
}
impl<'a, T, SE1, SE2, L1, L2, E1, E2>
ExprFVeryGenericVisitor<'a, ExprF<SE2, L2, E2>, SE1, L1, E1> for T
where
T: ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>,
{
type Error = T::Error;
type SE2 = SE2;
type L2 = L2;
type E2 = E2;
fn visit_subexpr(
&mut self,
subexpr: &'a SE1,
) -> Result<Self::SE2, Self::Error> {
self.visit_subexpr(subexpr)
}
fn visit_label(&mut self, label: &'a L1) -> Result<Self::L2, Self::Error> {
self.visit_label(label)
}
fn visit_binder(
self,
label: &'a L1,
subexpr: &'a SE1,
) -> Result<(Self::L2, Self::SE2), Self::Error> {
self.visit_binder(label, subexpr)
}
fn visit_embed_squash(
self,
embed: &'a E1,
) -> Result<ExprF<SE2, L2, E2>, Self::Error> {
self.visit_embed_squash(embed)
}
// Called with the result of the map, in the non-embed case.
// Useful to change the result type, and/or avoid some loss of info
fn visit_resulting_exprf(
result: ExprF<Self::SE2, Self::L2, Self::E2>,
) -> Result<ExprF<SE2, L2, E2>, Self::Error> {
Ok(result)
}
}
/// Like ExprFFallibleVisitor, but without the error handling.
pub trait ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>: Sized {
fn visit_subexpr(&mut self, subexpr: &'a SE1) -> SE2;
fn visit_label(&mut self, label: &'a L1) -> L2;
fn visit_embed(self, embed: &'a E1) -> E2;
fn visit_subexpr_under_binder(
mut self,
_label: &'a L1,
subexpr: &'a SE1,
) -> SE2 {
self.visit_subexpr(subexpr)
}
fn visit_binder(mut self, label: &'a L1, subexpr: &'a SE1) -> (L2, SE2) {
(
self.visit_label(label),
self.visit_subexpr_under_binder(label, subexpr),
)
}
fn visit_embed_squash(self, embed: &'a E1) -> ExprF<SE2, L2, E2> {
ExprF::Embed(self.visit_embed(embed))
}
}
struct InfallibleWrapper<T>(T);
impl<'a, T, SE1, SE2, L1, L2, E1, E2>
ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2> for InfallibleWrapper<T>
where
T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>,
{
type Error = X;
fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error> {
Ok(self.0.visit_subexpr(subexpr))
}
fn visit_label(&mut self, label: &'a L1) -> Result<L2, Self::Error> {
Ok(self.0.visit_label(label))
}
fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error> {
Ok(self.0.visit_embed(embed))
}
fn visit_binder(
self,
label: &'a L1,
subexpr: &'a SE1,
) -> Result<(L2, SE2), Self::Error> {
Ok(self.0.visit_binder(label, subexpr))
}
fn visit_embed_squash(
self,
embed: &'a E1,
) -> Result<ExprF<SE2, L2, E2>, Self::Error> {
Ok(self.0.visit_embed_squash(embed))
}
}
impl<'a, T, SE1, SE2, L1, L2, E1, E2>
GenericVisitor<&'a ExprF<SE1, L1, E1>, ExprF<SE2, L2, E2>> for T
where
L1: Ord,
L2: Ord,
T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>,
{
fn visit(self, input: &'a ExprF<SE1, L1, E1>) -> ExprF<SE2, L2, E2> {
trivial_result(InfallibleWrapper(self).visit(input))
}
}
pub struct TraverseRefWithBindersVisitor<F1, F2, F4, F5> {
pub visit_subexpr: F1,
pub visit_under_binder: F2,
pub visit_embed: F4,
pub visit_label: F5,
}
impl<'a, SE, L, E, SE2, L2, E2, Err, F1, F2, F4, F5>
ExprFFallibleVisitor<'a, SE, SE2, L, L2, E, E2>
for TraverseRefWithBindersVisitor<F1, F2, F4, F5>
where
SE: 'a,
L: 'a,
E: 'a,
L: Ord,
L2: Ord,
F1: FnMut(&'a SE) -> Result<SE2, Err>,
F2: FnOnce(&'a L, &'a SE) -> Result<SE2, Err>,
F4: FnOnce(&'a E) -> Result<E2, Err>,
F5: FnMut(&'a L) -> Result<L2, Err>,
{
type Error = Err;
fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result<SE2, Self::Error> {
(self.visit_subexpr)(subexpr)
}
fn visit_subexpr_under_binder(
self,
label: &'a L,
subexpr: &'a SE,
) -> Result<SE2, Self::Error> {
(self.visit_under_binder)(label, subexpr)
}
fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> {
(self.visit_embed)(embed)
}
fn visit_label(&mut self, label: &'a L) -> Result<L2, Self::Error> {
(self.visit_label)(label)
}
}
pub struct TraverseRefVisitor<F1, F3, F4> {
pub visit_subexpr: F1,
pub visit_embed: F3,
pub visit_label: F4,
}
impl<'a, SE, L, E, SE2, L2, E2, Err, F1, F3, F4>
ExprFFallibleVisitor<'a, SE, SE2, L, L2, E, E2>
for TraverseRefVisitor<F1, F3, F4>
where
SE: 'a,
L: 'a,
E: 'a,
L: Ord,
L2: Ord,
F1: FnMut(&'a SE) -> Result<SE2, Err>,
F3: FnOnce(&'a E) -> Result<E2, Err>,
F4: FnMut(&'a L) -> Result<L2, Err>,
{
type Error = Err;
fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result<SE2, Self::Error> {
(self.visit_subexpr)(subexpr)
}
fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> {
(self.visit_embed)(embed)
}
fn visit_label(&mut self, label: &'a L) -> Result<L2, Self::Error> {
(self.visit_label)(label)
}
}
pub struct TraverseEmbedVisitor<F1>(pub F1);
impl<'a, 'b, N, E, E2, Err, F1>
ExprFFallibleVisitor<'a, SubExpr<N, E>, SubExpr<N, E2>, Label, Label, E, E2>
for &'b mut TraverseEmbedVisitor<F1>
where
N: Clone + 'a,
F1: FnMut(&E) -> Result<E2, Err>,
{
type Error = Err;
fn visit_subexpr(
&mut self,
subexpr: &'a SubExpr<N, E>,
) -> Result<SubExpr<N, E2>, Self::Error> {
Ok(subexpr.rewrap(subexpr.as_ref().visit(&mut **self)?))
}
fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> {
(self.0)(embed)
}
fn visit_label(&mut self, label: &'a Label) -> Result<Label, Self::Error> {
Ok(Label::clone(label))
}
}
pub struct SquashEmbedVisitor<F1>(pub F1);
impl<'a, 'b, N, E1, E2, F1>
ExprFVeryGenericVisitor<'a, SubExpr<N, E2>, SubExpr<N, E1>, Label, E1>
for &'b mut SquashEmbedVisitor<F1>
where
N: Clone + 'a,
F1: FnMut(&E1) -> SubExpr<N, E2>,
{
type Error = X;
type SE2 = SubExpr<N, E2>;
type L2 = Label;
type E2 = E2;
fn visit_subexpr(
&mut self,
subexpr: &'a SubExpr<N, E1>,
) -> Result<Self::SE2, Self::Error> {
Ok(subexpr.as_ref().visit(&mut **self)?)
}
fn visit_label(
&mut self,
label: &'a Label,
) -> Result<Self::L2, Self::Error> {
Ok(Label::clone(label))
}
fn visit_binder(
mut self,
label: &'a Label,
subexpr: &'a SubExpr<N, E1>,
) -> Result<(Self::L2, Self::SE2), Self::Error> {
Ok((self.visit_label(label)?, self.visit_subexpr(subexpr)?))
}
fn visit_embed_squash(
self,
embed: &'a E1,
) -> Result<SubExpr<N, E2>, Self::Error> {
Ok((self.0)(embed))
}
// Called with the result of the map, in the non-embed case.
// Useful to change the result type, and/or avoid some loss of info
fn visit_resulting_exprf(
result: ExprF<Self::SE2, Self::L2, Self::E2>,
) -> Result<SubExpr<N, E2>, Self::Error> {
// TODO: don't lose note
Ok(SubExpr::from_expr_no_note(result))
}
}
pub struct UnNoteVisitor;
impl<'a, 'b, N, E>
ExprFInFallibleVisitor<'a, SubExpr<N, E>, SubExpr<X, E>, Label, Label, E, E>
for &'b mut UnNoteVisitor
where
E: Clone + 'a,
{
fn visit_subexpr(&mut self, subexpr: &'a SubExpr<N, E>) -> SubExpr<X, E> {
SubExpr::from_expr_no_note(subexpr.as_ref().visit(&mut **self))
}
fn visit_embed(self, embed: &'a E) -> E {
E::clone(embed)
}
fn visit_label(&mut self, label: &'a Label) -> Label {
Label::clone(label)
}
}
pub struct NoteAbsurdVisitor;
impl<'a, 'b, N, E>
ExprFInFallibleVisitor<'a, SubExpr<X, E>, SubExpr<N, E>, Label, Label, E, E>
for &'b mut NoteAbsurdVisitor
where
E: Clone + 'a,
{
fn visit_subexpr(&mut self, subexpr: &'a SubExpr<X, E>) -> SubExpr<N, E> {
SubExpr::from_expr_no_note(subexpr.as_ref().visit(&mut **self))
}
fn visit_embed(self, embed: &'a E) -> E {
E::clone(embed)
}
fn visit_label(&mut self, label: &'a Label) -> Label {
Label::clone(label)
}
}
pub struct EmbedAbsurdVisitor;
impl<'a, 'b, N, E>
ExprFInFallibleVisitor<'a, SubExpr<N, X>, SubExpr<N, E>, Label, Label, X, E>
for &'b mut EmbedAbsurdVisitor
where
N: Clone + 'a,
{
fn visit_subexpr(&mut self, subexpr: &'a SubExpr<N, X>) -> SubExpr<N, E> {
subexpr.rewrap(subexpr.as_ref().visit(&mut **self))
}
fn visit_embed(self, embed: &'a X) -> E {
match *embed {}
}
fn visit_label(&mut self, label: &'a Label) -> Label {
Label::clone(label)
}
}
|