summaryrefslogtreecommitdiff
path: root/dhall/src/normalize.rs
blob: 50a1625435a6199305e06dd5971a102d1e99e8c0 (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
#![allow(non_snake_case)]
use dhall_core::core::*;
use dhall_generator::dhall_expr;
use std::fmt;

/// Reduce an expression to its weak head normal form, i.e. normalize
/// just enough to get the first constructor of the final expression
/// Is identical to normalize on primitive types, but not on more complex
/// types like functions and records.
/// This allows normalization to be lazy.
pub fn normalize_whnf<S, A>(e: &Expr<S, A>) -> Expr<S, A>
where
    S: Clone + fmt::Debug,
    A: Clone + fmt::Debug,
{
    use dhall_core::BinOp::*;
    use dhall_core::Builtin::*;
    use dhall_core::Expr::*;
    match e {
        Let(f, _, r, b) => {
            let vf0 = &V(f.clone(), 0);
            let r2 = shift::<_, S, _>(1, vf0, r);
            let b2 = subst::<_, S, _>(vf0, &r2, b);
            let b3 = shift::<_, S, _>(-1, vf0, &b2);
            normalize_whnf(&b3)
        }
        Annot(x, _) => normalize_whnf(x),
        Note(_, e) => normalize_whnf(e),
        App(f, a) => match (normalize_whnf(f), a) {
            (Lam(x, _, b), a) => {
                // Beta reduce
                let vx0 = &V(x, 0);
                let a2 = shift::<S, S, A>(1, vx0, a);
                let b2 = subst::<S, S, A>(vx0, &a2, &b);
                let b3 = shift::<S, S, A>(-1, vx0, &b2);
                normalize_whnf(&b3)
            }
            (Builtin(b), a) => match (b, normalize_whnf(a)) {
                (OptionalSome, a) => OptionalLit(None, vec![a]),
                (OptionalNone, a) => OptionalLit(Some(bx(a)), vec![]),
                (NaturalIsZero, NaturalLit(n)) => BoolLit(n == 0),
                (NaturalEven, NaturalLit(n)) => BoolLit(n % 2 == 0),
                (NaturalOdd, NaturalLit(n)) => BoolLit(n % 2 != 0),
                (NaturalToInteger, NaturalLit(n)) => IntegerLit(n as isize),
                (NaturalShow, NaturalLit(n)) => TextLit(n.to_string()),

                (b, App(f, x)) => match (b, normalize_whnf(&f), x) {
                    // fold/build fusion for `Natural`
                    (NaturalBuild, Builtin(NaturalFold), x) => {
                        normalize_whnf(&x)
                    }
                    (NaturalFold, Builtin(NaturalBuild), x) => {
                        normalize_whnf(&x)
                    }
                    (b, f, x) => app(Builtin(b), app(f, *x)),
                },
                (b, a) => app(Builtin(b), a),
            },
            (App(f, x), y) => match (normalize_whnf(&f), x, y) {
                // TODO: use whnf
                (Builtin(b), x, y) => match (b, x, normalize::<S, S, A>(&y)) {
                    (ListLength, _, ListLit(_, ys)) => NaturalLit(ys.len()),
                    (ListHead, _, ListLit(t, ys)) => {
                        OptionalLit(t, ys.into_iter().take(1).collect())
                    }
                    (ListLast, _, ListLit(t, ys)) => OptionalLit(
                        t,
                        ys.into_iter().last().into_iter().collect(),
                    ),
                    (ListReverse, _, ListLit(t, ys)) => {
                        let mut xs = ys;
                        xs.reverse();
                        ListLit(t, xs)
                    }

                    // fold/build fusion for `List`
                    (
                        ListBuild,
                        _,
                        App(box App(box Builtin(ListFold), _), box e2),
                    ) => normalize_whnf(&e2),
                    (
                        ListFold,
                        _,
                        App(box App(box Builtin(ListBuild), _), box e2),
                    ) => normalize_whnf(&e2),

                    // fold/build fusion for `Optional`
                    (
                        OptionalBuild,
                        _,
                        App(box App(box Builtin(OptionalFold), _), box e2),
                    ) => normalize_whnf(&e2),
                    (
                        OptionalFold,
                        _,
                        App(box App(box Builtin(OptionalBuild), _), box e2),
                    ) => normalize_whnf(&e2),

                    (ListBuild, a0, k) => {
                        let k = bx(k);
                        let a1 = bx(shift(1, &V("a".into(), 0), &a0));
                        normalize_whnf(
                            &dhall_expr!(k (List a0) (λ(a : a0) -> λ(as : List a1) -> [ a ] # as) ([] : List a0)),
                        )
                    }
                    (OptionalBuild, a0, g) => {
                        let g = bx(g);
                        normalize_whnf(
                            &dhall_expr!((g (Optional a0)) (λ(x: a0) -> [x] :  Optional a0) ([] :  Optional a0)),
                        )
                    }
                    (b, x, y) => app(App(bx(Builtin(b)), x), y),
                },
                (App(f, x), y, z) => match (normalize_whnf(&f), x, y, z) {
                    (App(f, x), y, z, w) => {
                        match (normalize_whnf(&f), x, y, z, w) {
                            (App(f, x), y, z, w, t) => match (
                                normalize_whnf(&f),
                                x,
                                normalize_whnf(&y),
                                z,
                                w,
                                t,
                            ) {
                                (
                                    Builtin(ListFold),
                                    _,
                                    ListLit(_, xs),
                                    _,
                                    cons,
                                    nil,
                                ) => {
                                    let e2: Expr<_, _> = xs
                                        .into_iter()
                                        .rev()
                                        .fold((**nil).clone(), |acc, x| {
                                            let x = bx(x);
                                            let acc = bx(acc);
                                            dhall_expr!(cons x acc)
                                        });
                                    normalize_whnf(&e2)
                                }
                                (
                                    Builtin(OptionalFold),
                                    _,
                                    OptionalLit(_, xs),
                                    _,
                                    just,
                                    nothing,
                                ) => {
                                    let e2: Expr<_, _> = xs.into_iter().fold(
                                        (**nothing).clone(),
                                        |_, y| {
                                            let y = bx(y);
                                            dhall_expr!(just y)
                                        },
                                    );
                                    normalize_whnf(&e2)
                                }
                                (f, x, y, z, w, t) => app(
                                    App(
                                        bx(App(
                                            bx(App(bx(App(bx(f), x)), bx(y))),
                                            z,
                                        )),
                                        w,
                                    ),
                                    (**t).clone(),
                                ),
                            },
                            (f, x, y, z, w) => app(
                                App(bx(App(bx(App(bx(f), x)), y)), z),
                                (**w).clone(),
                            ),
                        }
                    }
                    (f, x, y, z) => {
                        app(App(bx(App(bx(f), x)), y), (**z).clone())
                    }
                },
                (f, x, y) => app(App(bx(f), x), (**y).clone()),
            },
            (f, a) => app(f, (**a).clone()),
        },
        BoolIf(b, t, f) => match normalize_whnf(b) {
            BoolLit(true) => normalize_whnf(t),
            BoolLit(false) => normalize_whnf(f),
            b2 => BoolIf(bx(b2), t.clone(), f.clone()),
        },
        OptionalLit(t, es) => {
            if !es.is_empty() {
                OptionalLit(None, es.clone())
            } else {
                OptionalLit(t.clone(), es.clone())
            }
        }
        BinOp(o, x, y) => match (o, normalize_whnf(&x), normalize_whnf(&y)) {
            (BoolAnd, BoolLit(x), BoolLit(y)) => BoolLit(x && y),
            (BoolOr, BoolLit(x), BoolLit(y)) => BoolLit(x || y),
            (BoolEQ, BoolLit(x), BoolLit(y)) => BoolLit(x == y),
            (BoolNE, BoolLit(x), BoolLit(y)) => BoolLit(x != y),
            (NaturalPlus, NaturalLit(x), NaturalLit(y)) => NaturalLit(x + y),
            (NaturalTimes, NaturalLit(x), NaturalLit(y)) => NaturalLit(x * y),
            (TextAppend, TextLit(x), TextLit(y)) => TextLit(x + &y),
            (ListAppend, ListLit(t1, xs), ListLit(t2, ys)) => {
                // Drop type annotation if the result is nonempty
                let t = if xs.is_empty() && ys.is_empty() {
                    t1.or(t2)
                } else {
                    None
                };
                let xs = xs.into_iter();
                let ys = ys.into_iter();
                ListLit(t, xs.chain(ys).collect())
            }
            (o, x, y) => BinOp(*o, bx(x), bx(y)),
        },
        Field(e, x) => match (normalize_whnf(&e), x) {
            (RecordLit(kvs), x) => match kvs.get(&x) {
                Some(r) => normalize_whnf(r),
                None => Field(bx(RecordLit(kvs)), x.clone()),
            },
            (e, x) => Field(bx(e), x.clone()),
        },
        e => e.clone(),
    }
}

/// Reduce an expression to its normal form, performing beta reduction
///
/// `normalize` does not type-check the expression.  You may want to type-check
/// expressions before normalizing them since normalization can convert an
/// ill-typed expression into a well-typed expression.
///
/// However, `normalize` will not fail if the expression is ill-typed and will
/// leave ill-typed sub-expressions unevaluated.
///
pub fn normalize<S, T, A>(e: &Expr<S, A>) -> Expr<T, A>
where
    S: Clone + fmt::Debug,
    T: Clone + fmt::Debug,
    A: Clone + fmt::Debug,
{
    normalize_whnf(e).map_shallow(
        normalize,
        |_| unreachable!(),
        |x| x.clone(),
        |x| x.clone(),
    )
}