summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/nze/normalize.rs
blob: 570e106b81b189a0c8e617b1d7ef5144bbec0a1d (plain)
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
use itertools::Itertools;
use std::collections::HashMap;

use crate::semantics::NzEnv;
use crate::semantics::{Binder, Closure, Hir, HirKind, Nir, NirKind, TextLit};
use crate::syntax::{BinOp, ExprKind, InterpolatedTextContents, NumKind};

pub fn apply_any(f: Nir, a: Nir) -> NirKind {
    match f.kind() {
        NirKind::LamClosure { closure, .. } => closure.apply(a).kind().clone(),
        NirKind::AppliedBuiltin(closure) => closure.apply(a),
        NirKind::UnionConstructor(l, kts) => {
            NirKind::UnionLit(l.clone(), a, kts.clone())
        }
        _ => NirKind::PartialExpr(ExprKind::App(f, a)),
    }
}

pub fn squash_textlit(
    elts: impl Iterator<Item = InterpolatedTextContents<Nir>>,
) -> Vec<InterpolatedTextContents<Nir>> {
    use std::mem::replace;
    use InterpolatedTextContents::{Expr, Text};

    fn inner(
        elts: impl Iterator<Item = InterpolatedTextContents<Nir>>,
        crnt_str: &mut String,
        ret: &mut Vec<InterpolatedTextContents<Nir>>,
    ) {
        for contents in elts {
            match contents {
                Text(s) => crnt_str.push_str(&s),
                Expr(e) => match e.kind() {
                    NirKind::TextLit(elts2) => {
                        inner(elts2.iter().cloned(), crnt_str, ret)
                    }
                    _ => {
                        if !crnt_str.is_empty() {
                            ret.push(Text(replace(crnt_str, String::new())))
                        }
                        ret.push(Expr(e.clone()))
                    }
                },
            }
        }
    }

    let mut crnt_str = String::new();
    let mut ret = Vec::new();
    inner(elts, &mut crnt_str, &mut ret);
    if !crnt_str.is_empty() {
        ret.push(Text(replace(&mut crnt_str, String::new())))
    }
    ret
}

pub fn merge_maps<K, V, F>(
    map1: &HashMap<K, V>,
    map2: &HashMap<K, V>,
    mut f: F,
) -> HashMap<K, V>
where
    F: FnMut(&K, &V, &V) -> V,
    K: std::hash::Hash + Eq + Clone,
    V: Clone,
{
    let mut kvs = HashMap::new();
    for (x, v2) in map2 {
        let newv = if let Some(v1) = map1.get(x) {
            // Collision: the key is present in both maps
            f(x, v1, v2)
        } else {
            v2.clone()
        };
        kvs.insert(x.clone(), newv);
    }
    for (x, v1) in map1 {
        // Insert only if key not already present
        kvs.entry(x.clone()).or_insert_with(|| v1.clone());
    }
    kvs
}

// Small helper enum to avoid repetition
enum Ret<'a> {
    NirKind(NirKind),
    Nir(Nir),
    NirRef(&'a Nir),
    Expr(ExprKind<Nir>),
}

fn apply_binop<'a>(o: BinOp, x: &'a Nir, y: &'a Nir) -> Option<Ret<'a>> {
    use BinOp::{
        BoolAnd, BoolEQ, BoolNE, BoolOr, Equivalence, ListAppend, NaturalPlus,
        NaturalTimes, RecursiveRecordMerge, RecursiveRecordTypeMerge,
        RightBiasedRecordMerge, TextAppend,
    };
    use NirKind::{EmptyListLit, NEListLit, Num, RecordLit, RecordType};
    use NumKind::{Bool, Natural};

    Some(match (o, x.kind(), y.kind()) {
        (BoolAnd, Num(Bool(true)), _) => Ret::NirRef(y),
        (BoolAnd, _, Num(Bool(true))) => Ret::NirRef(x),
        (BoolAnd, Num(Bool(false)), _) => Ret::NirKind(Num(Bool(false))),
        (BoolAnd, _, Num(Bool(false))) => Ret::NirKind(Num(Bool(false))),
        (BoolAnd, _, _) if x == y => Ret::NirRef(x),
        (BoolOr, Num(Bool(true)), _) => Ret::NirKind(Num(Bool(true))),
        (BoolOr, _, Num(Bool(true))) => Ret::NirKind(Num(Bool(true))),
        (BoolOr, Num(Bool(false)), _) => Ret::NirRef(y),
        (BoolOr, _, Num(Bool(false))) => Ret::NirRef(x),
        (BoolOr, _, _) if x == y => Ret::NirRef(x),
        (BoolEQ, Num(Bool(true)), _) => Ret::NirRef(y),
        (BoolEQ, _, Num(Bool(true))) => Ret::NirRef(x),
        (BoolEQ, Num(Bool(x)), Num(Bool(y))) => Ret::NirKind(Num(Bool(x == y))),
        (BoolEQ, _, _) if x == y => Ret::NirKind(Num(Bool(true))),
        (BoolNE, Num(Bool(false)), _) => Ret::NirRef(y),
        (BoolNE, _, Num(Bool(false))) => Ret::NirRef(x),
        (BoolNE, Num(Bool(x)), Num(Bool(y))) => Ret::NirKind(Num(Bool(x != y))),
        (BoolNE, _, _) if x == y => Ret::NirKind(Num(Bool(false))),

        (NaturalPlus, Num(Natural(0)), _) => Ret::NirRef(y),
        (NaturalPlus, _, Num(Natural(0))) => Ret::NirRef(x),
        (NaturalPlus, Num(Natural(x)), Num(Natural(y))) => {
            Ret::NirKind(Num(Natural(x + y)))
        }
        (NaturalTimes, Num(Natural(0)), _) => Ret::NirKind(Num(Natural(0))),
        (NaturalTimes, _, Num(Natural(0))) => Ret::NirKind(Num(Natural(0))),
        (NaturalTimes, Num(Natural(1)), _) => Ret::NirRef(y),
        (NaturalTimes, _, Num(Natural(1))) => Ret::NirRef(x),
        (NaturalTimes, Num(Natural(x)), Num(Natural(y))) => {
            Ret::NirKind(Num(Natural(x * y)))
        }

        (ListAppend, EmptyListLit(_), _) => Ret::NirRef(y),
        (ListAppend, _, EmptyListLit(_)) => Ret::NirRef(x),
        (ListAppend, NEListLit(xs), NEListLit(ys)) => Ret::NirKind(NEListLit(
            xs.iter().chain(ys.iter()).cloned().collect(),
        )),

        (TextAppend, NirKind::TextLit(x), _) if x.is_empty() => Ret::NirRef(y),
        (TextAppend, _, NirKind::TextLit(y)) if y.is_empty() => Ret::NirRef(x),
        (TextAppend, NirKind::TextLit(x), NirKind::TextLit(y)) => {
            Ret::NirKind(NirKind::TextLit(x.concat(y)))
        }
        (TextAppend, NirKind::TextLit(x), _) => Ret::NirKind(NirKind::TextLit(
            x.concat(&TextLit::interpolate(y.clone())),
        )),
        (TextAppend, _, NirKind::TextLit(y)) => Ret::NirKind(NirKind::TextLit(
            TextLit::interpolate(x.clone()).concat(y),
        )),

        (RightBiasedRecordMerge, _, RecordLit(kvs)) if kvs.is_empty() => {
            Ret::NirRef(x)
        }
        (RightBiasedRecordMerge, RecordLit(kvs), _) if kvs.is_empty() => {
            Ret::NirRef(y)
        }
        (RightBiasedRecordMerge, RecordLit(kvs1), RecordLit(kvs2)) => {
            let mut kvs = kvs2.clone();
            for (x, v) in kvs1 {
                // Insert only if key not already present
                kvs.entry(x.clone()).or_insert_with(|| v.clone());
            }
            Ret::NirKind(RecordLit(kvs))
        }
        (RightBiasedRecordMerge, _, _) if x == y => Ret::NirRef(y),

        (RecursiveRecordMerge, _, RecordLit(kvs)) if kvs.is_empty() => {
            Ret::NirRef(x)
        }
        (RecursiveRecordMerge, RecordLit(kvs), _) if kvs.is_empty() => {
            Ret::NirRef(y)
        }
        (RecursiveRecordMerge, RecordLit(kvs1), RecordLit(kvs2)) => {
            let kvs = merge_maps(kvs1, kvs2, |_, v1, v2| {
                Nir::from_partial_expr(ExprKind::BinOp(
                    RecursiveRecordMerge,
                    v1.clone(),
                    v2.clone(),
                ))
            });
            Ret::NirKind(RecordLit(kvs))
        }

        (RecursiveRecordTypeMerge, RecordType(kts_x), RecordType(kts_y)) => {
            let kts = merge_maps(
                kts_x,
                kts_y,
                // If the Label exists for both records, then we hit the recursive case.
                |_, l: &Nir, r: &Nir| {
                    Nir::from_partial_expr(ExprKind::BinOp(
                        RecursiveRecordTypeMerge,
                        l.clone(),
                        r.clone(),
                    ))
                },
            );
            Ret::NirKind(RecordType(kts))
        }

        (Equivalence, _, _) => {
            Ret::NirKind(NirKind::Equivalence(x.clone(), y.clone()))
        }

        _ => return None,
    })
}

#[allow(clippy::cognitive_complexity)]
pub fn normalize_one_layer(expr: ExprKind<Nir>, env: &NzEnv) -> NirKind {
    use NirKind::{
        EmptyListLit, EmptyOptionalLit, NEListLit, NEOptionalLit, Num,
        PartialExpr, RecordLit, RecordType, UnionConstructor, UnionLit,
        UnionType,
    };
    use NumKind::Bool;

    let ret = match expr {
        ExprKind::Import(..) | ExprKind::Completion(..) => {
            unreachable!("This case should have been handled in resolution")
        }
        ExprKind::Var(..)
        | ExprKind::Lam(..)
        | ExprKind::Pi(..)
        | ExprKind::Let(..) => unreachable!(
            "This case should have been handled in normalize_hir_whnf"
        ),

        ExprKind::Annot(x, _) => Ret::Nir(x),
        ExprKind::Const(c) => Ret::Nir(Nir::from_const(c)),
        ExprKind::Builtin(b) => Ret::Nir(Nir::from_builtin_env(b, env)),
        ExprKind::Assert(_) => Ret::Expr(expr),
        ExprKind::App(v, a) => Ret::Nir(v.app(a)),
        ExprKind::Num(l) => Ret::NirKind(Num(l)),
        ExprKind::SomeLit(e) => Ret::NirKind(NEOptionalLit(e)),
        ExprKind::EmptyListLit(t) => {
            let arg = match t.kind() {
                NirKind::ListType(t) => t.clone(),
                _ => panic!("internal type error"),
            };
            Ret::NirKind(NirKind::EmptyListLit(arg))
        }
        ExprKind::NEListLit(elts) => {
            Ret::NirKind(NEListLit(elts.into_iter().collect()))
        }
        ExprKind::RecordLit(kvs) => {
            Ret::NirKind(RecordLit(kvs.into_iter().collect()))
        }
        ExprKind::RecordType(kvs) => {
            Ret::NirKind(RecordType(kvs.into_iter().collect()))
        }
        ExprKind::UnionType(kvs) => {
            Ret::NirKind(UnionType(kvs.into_iter().collect()))
        }
        ExprKind::TextLit(elts) => {
            let tlit = TextLit::new(elts.into_iter());
            // Simplify bare interpolation
            if let Some(v) = tlit.as_single_expr() {
                Ret::Nir(v.clone())
            } else {
                Ret::NirKind(NirKind::TextLit(tlit))
            }
        }
        ExprKind::BoolIf(ref b, ref e1, ref e2) => {
            match b.kind() {
                Num(Bool(true)) => Ret::NirRef(e1),
                Num(Bool(false)) => Ret::NirRef(e2),
                _ => {
                    match (e1.kind(), e2.kind()) {
                        // Simplify `if b then True else False`
                        (Num(Bool(true)), Num(Bool(false))) => Ret::NirRef(b),
                        _ if e1 == e2 => Ret::NirRef(e1),
                        _ => Ret::Expr(expr),
                    }
                }
            }
        }
        ExprKind::BinOp(o, ref x, ref y) => match apply_binop(o, x, y) {
            Some(ret) => ret,
            None => Ret::Expr(expr),
        },

        ExprKind::Field(ref v, ref field) => match v.kind() {
            RecordLit(kvs) => match kvs.get(field) {
                Some(r) => Ret::Nir(r.clone()),
                None => Ret::Expr(expr),
            },
            UnionType(kts) => {
                Ret::NirKind(UnionConstructor(field.clone(), kts.clone()))
            }
            PartialExpr(ExprKind::Projection(x, _)) => {
                return normalize_one_layer(
                    ExprKind::Field(x.clone(), field.clone()),
                    env,
                )
            }
            PartialExpr(ExprKind::BinOp(
                BinOp::RightBiasedRecordMerge,
                x,
                y,
            )) => match (x.kind(), y.kind()) {
                (_, RecordLit(kvs)) => match kvs.get(field) {
                    Some(r) => Ret::Nir(r.clone()),
                    None => {
                        return normalize_one_layer(
                            ExprKind::Field(x.clone(), field.clone()),
                            env,
                        )
                    }
                },
                (RecordLit(kvs), _) => match kvs.get(field) {
                    Some(r) => Ret::Expr(ExprKind::Field(
                        Nir::from_kind(PartialExpr(ExprKind::BinOp(
                            BinOp::RightBiasedRecordMerge,
                            Nir::from_kind(RecordLit(
                                Some((field.clone(), r.clone()))
                                    .into_iter()
                                    .collect(),
                            )),
                            y.clone(),
                        ))),
                        field.clone(),
                    )),
                    None => {
                        return normalize_one_layer(
                            ExprKind::Field(y.clone(), field.clone()),
                            env,
                        )
                    }
                },
                _ => Ret::Expr(expr),
            },
            PartialExpr(ExprKind::BinOp(BinOp::RecursiveRecordMerge, x, y)) => {
                match (x.kind(), y.kind()) {
                    (RecordLit(kvs), _) => match kvs.get(field) {
                        Some(r) => Ret::Expr(ExprKind::Field(
                            Nir::from_kind(PartialExpr(ExprKind::BinOp(
                                BinOp::RecursiveRecordMerge,
                                Nir::from_kind(RecordLit(
                                    Some((field.clone(), r.clone()))
                                        .into_iter()
                                        .collect(),
                                )),
                                y.clone(),
                            ))),
                            field.clone(),
                        )),
                        None => {
                            return normalize_one_layer(
                                ExprKind::Field(y.clone(), field.clone()),
                                env,
                            )
                        }
                    },
                    (_, RecordLit(kvs)) => match kvs.get(field) {
                        Some(r) => Ret::Expr(ExprKind::Field(
                            Nir::from_kind(PartialExpr(ExprKind::BinOp(
                                BinOp::RecursiveRecordMerge,
                                x.clone(),
                                Nir::from_kind(RecordLit(
                                    Some((field.clone(), r.clone()))
                                        .into_iter()
                                        .collect(),
                                )),
                            ))),
                            field.clone(),
                        )),
                        None => {
                            return normalize_one_layer(
                                ExprKind::Field(x.clone(), field.clone()),
                                env,
                            )
                        }
                    },
                    _ => Ret::Expr(expr),
                }
            }
            _ => Ret::Expr(expr),
        },
        ExprKind::Projection(_, ref ls) if ls.is_empty() => {
            Ret::NirKind(RecordLit(HashMap::new()))
        }
        ExprKind::Projection(ref v, ref ls) => match v.kind() {
            RecordLit(kvs) => Ret::NirKind(RecordLit(
                ls.iter()
                    .filter_map(|l| kvs.get(l).map(|x| (l.clone(), x.clone())))
                    .collect(),
            )),
            PartialExpr(ExprKind::Projection(v2, _)) => {
                return normalize_one_layer(
                    ExprKind::Projection(v2.clone(), ls.clone()),
                    env,
                )
            }
            _ => Ret::Expr(expr),
        },
        ExprKind::ProjectionByExpr(ref v, ref t) => match t.kind() {
            RecordType(kts) => {
                return normalize_one_layer(
                    ExprKind::Projection(
                        v.clone(),
                        kts.keys().cloned().collect(),
                    ),
                    env,
                )
            }
            _ => Ret::Expr(expr),
        },

        ExprKind::Merge(ref handlers, ref variant, _) => {
            match handlers.kind() {
                RecordLit(kvs) => match variant.kind() {
                    UnionConstructor(l, _) => match kvs.get(l) {
                        Some(h) => Ret::Nir(h.clone()),
                        None => Ret::Expr(expr),
                    },
                    UnionLit(l, v, _) => match kvs.get(l) {
                        Some(h) => Ret::Nir(h.app(v.clone())),
                        None => Ret::Expr(expr),
                    },
                    EmptyOptionalLit(_) => match kvs.get(&"None".into()) {
                        Some(h) => Ret::Nir(h.clone()),
                        None => Ret::Expr(expr),
                    },
                    NEOptionalLit(v) => match kvs.get(&"Some".into()) {
                        Some(h) => Ret::Nir(h.app(v.clone())),
                        None => Ret::Expr(expr),
                    },
                    _ => Ret::Expr(expr),
                },
                _ => Ret::Expr(expr),
            }
        }
        ExprKind::ToMap(ref v, ref annot) => match v.kind() {
            RecordLit(kvs) if kvs.is_empty() => {
                match annot.as_ref().map(|v| v.kind()) {
                    Some(NirKind::ListType(t)) => {
                        Ret::NirKind(EmptyListLit(t.clone()))
                    }
                    _ => Ret::Expr(expr),
                }
            }
            RecordLit(kvs) => Ret::NirKind(NEListLit(
                kvs.iter()
                    .sorted_by_key(|(k, _)| *k)
                    .map(|(k, v)| {
                        let mut rec = HashMap::new();
                        rec.insert("mapKey".into(), Nir::from_text(k));
                        rec.insert("mapValue".into(), v.clone());
                        Nir::from_kind(NirKind::RecordLit(rec))
                    })
                    .collect(),
            )),
            _ => Ret::Expr(expr),
        },
    };

    match ret {
        Ret::NirKind(v) => v,
        Ret::Nir(v) => v.kind().clone(),
        Ret::NirRef(v) => v.kind().clone(),
        Ret::Expr(expr) => NirKind::PartialExpr(expr),
    }
}

/// Normalize Hir into WHNF
pub fn normalize_hir_whnf(env: &NzEnv, hir: &Hir) -> NirKind {
    match hir.kind() {
        HirKind::Var(var) => env.lookup_val(*var),
        HirKind::Import(hir, _) => normalize_hir_whnf(env, hir),
        HirKind::Expr(ExprKind::Lam(binder, annot, body)) => {
            let annot = annot.eval(env);
            NirKind::LamClosure {
                binder: Binder::new(binder.clone()),
                annot,
                closure: Closure::new(env, body.clone()),
            }
        }
        HirKind::Expr(ExprKind::Pi(binder, annot, body)) => {
            let annot = annot.eval(env);
            NirKind::PiClosure {
                binder: Binder::new(binder.clone()),
                annot,
                closure: Closure::new(env, body.clone()),
            }
        }
        HirKind::Expr(ExprKind::Let(_, _, val, body)) => {
            let val = val.eval(env);
            body.eval(env.insert_value(val, ())).kind().clone()
        }
        HirKind::Expr(e) => {
            let e = e.map_ref(|hir| hir.eval(env));
            normalize_one_layer(e, env)
        }
    }
}