summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/nze/nir.rs
blob: 8cf06c5c6a9556374eac138928fe40c36d57df8e (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
use std::collections::HashMap;
use std::rc::Rc;

use crate::builtins::{Builtin, BuiltinClosure};
use crate::operations::{BinOp, OpKind};
use crate::semantics::nze::lazy;
use crate::semantics::{
    apply_any, normalize_hir, normalize_one_layer, squash_textlit, Binder, Hir,
    HirKind, NzEnv, NzVar, TyEnv, Type, Universe, VarEnv,
};
use crate::syntax::{
    Const, Expr, ExprKind, InterpolatedTextContents, Label, NumKind, Span,
};
use crate::{Ctxt, ToExprOptions};

/// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation
/// automatically. Uses a Rc<OnceCell> to share computation.
/// If you compare for equality two `Nir`s, then equality will be up to alpha-equivalence
/// (renaming of bound variables) and beta-equivalence (normalization). It will recursively
/// normalize as needed.
/// Stands for "Normalized Intermediate Representation"
#[derive(Clone)]
pub struct Nir<'cx>(Rc<lazy::Lazy<Thunk<'cx>, NirKind<'cx>>>);

/// An unevaluated subexpression
#[derive(Debug, Clone)]
pub enum Thunk<'cx> {
    /// A completely unnormalized expression.
    Thunk { env: NzEnv<'cx>, body: Hir<'cx> },
    /// A partially normalized expression that may need to go through `normalize_one_layer`.
    PartialExpr { expr: ExprKind<Nir<'cx>> },
}

/// An unevaluated subexpression that takes an argument.
#[derive(Debug, Clone)]
pub enum Closure<'cx> {
    /// Normal closure
    Closure { env: NzEnv<'cx>, body: Hir<'cx> },
    /// Closure that ignores the argument passed
    ConstantClosure { body: Nir<'cx> },
}

/// A text literal with interpolations.
// Invariant: this must not contain interpolations that are themselves TextLits, and contiguous
// text values must be merged.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextLit<'cx>(Vec<InterpolatedTextContents<Nir<'cx>>>);

/// This represents a value in Weak Head Normal Form (WHNF). This means that the value is
/// normalized up to the first constructor, but subexpressions may not be fully normalized.
/// When all the Nirs in a NirKind are in WHNF, and recursively so, then the NirKind is in
/// Normal Form (NF). This is because WHNF ensures that we have the first constructor of the NF; so
/// if we have the first constructor of the NF at all levels, we actually have the NF.
/// In particular, this means that once we get a `NirKind`, it can be considered immutable, and
/// we only need to recursively normalize its sub-`Nir`s to get to the NF.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NirKind<'cx> {
    /// Closures
    LamClosure {
        binder: Binder,
        annot: Nir<'cx>,
        closure: Closure<'cx>,
    },
    PiClosure {
        binder: Binder,
        annot: Nir<'cx>,
        closure: Closure<'cx>,
    },
    AppliedBuiltin(BuiltinClosure<'cx>),

    Var(NzVar),
    Const(Const),
    Num(NumKind),
    // Must be a number type, Bool or Text
    BuiltinType(Builtin),
    TextLit(TextLit<'cx>),
    EmptyOptionalLit(Nir<'cx>),
    NEOptionalLit(Nir<'cx>),
    OptionalType(Nir<'cx>),
    // EmptyListLit(t) means `[] : List t`, not `[] : t`
    EmptyListLit(Nir<'cx>),
    NEListLit(Vec<Nir<'cx>>),
    ListType(Nir<'cx>),
    RecordLit(HashMap<Label, Nir<'cx>>),
    RecordType(HashMap<Label, Nir<'cx>>),
    UnionConstructor(Label, HashMap<Label, Option<Nir<'cx>>>),
    UnionLit(Label, Nir<'cx>, HashMap<Label, Option<Nir<'cx>>>),
    UnionType(HashMap<Label, Option<Nir<'cx>>>),
    Equivalence(Nir<'cx>, Nir<'cx>),
    Assert(Nir<'cx>),
    /// Invariant: evaluation must not be able to progress with `normalize_operation`.
    /// This is used when an operation couldn't proceed further, for example because of variables.
    Op(OpKind<Nir<'cx>>),
}

impl<'cx> Nir<'cx> {
    /// Construct a Nir from a completely unnormalized expression.
    pub fn new_thunk(env: NzEnv<'cx>, hir: Hir<'cx>) -> Self {
        Nir(Rc::new(lazy::Lazy::new(Thunk::new(env, hir))))
    }
    /// Construct a Nir from a partially normalized expression that's not in WHNF.
    pub fn from_partial_expr(e: ExprKind<Self>) -> Self {
        Nir(Rc::new(lazy::Lazy::new(Thunk::from_partial_expr(e))))
    }
    /// Make a Nir from a NirKind
    pub fn from_kind(v: NirKind<'cx>) -> Self {
        Nir(Rc::new(lazy::Lazy::new_completed(v)))
    }
    pub fn from_const(c: Const) -> Self {
        Self::from_kind(NirKind::Const(c))
    }
    pub fn from_builtin(cx: Ctxt<'cx>, b: Builtin) -> Self {
        Self::from_builtin_env(b, &NzEnv::new(cx))
    }
    pub fn from_builtin_env(b: Builtin, env: &NzEnv<'cx>) -> Self {
        Nir::from_kind(NirKind::from_builtin_env(b, env.clone()))
    }
    pub fn from_text(txt: impl ToString) -> Self {
        Nir::from_kind(NirKind::TextLit(TextLit::from_text(txt.to_string())))
    }

    pub fn as_const(&self) -> Option<Const> {
        match &*self.kind() {
            NirKind::Const(c) => Some(*c),
            _ => None,
        }
    }

    /// This is what you want if you want to pattern-match on the value.
    pub fn kind(&self) -> &NirKind<'cx> {
        &*self.0
    }

    /// The contents of a `Nir` are immutable and shared. If however we happen to be the sole
    /// owners, we can mutate it directly. Otherwise, this clones the internal value first.
    pub fn kind_mut(&mut self) -> &mut NirKind<'cx> {
        Rc::make_mut(&mut self.0).get_mut()
    }
    /// If we are the sole owner of this Nir, we can avoid a clone.
    pub fn into_kind(self) -> NirKind<'cx> {
        match Rc::try_unwrap(self.0) {
            Ok(lazy) => lazy.into_inner(),
            Err(rc) => (**rc).clone(),
        }
    }

    pub fn to_type(&self, u: impl Into<Universe>) -> Type<'cx> {
        Type::new(self.clone(), u.into())
    }
    /// Converts a value back to the corresponding AST expression.
    pub fn to_expr(&self, cx: Ctxt<'cx>, opts: ToExprOptions) -> Expr {
        self.to_hir_noenv().to_expr(cx, opts)
    }
    pub fn to_expr_tyenv(&self, tyenv: &TyEnv<'cx>) -> Expr {
        self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv)
    }

    pub fn app(&self, v: Self) -> Self {
        Nir::from_kind(self.app_to_kind(v))
    }
    pub fn app_to_kind(&self, v: Self) -> NirKind<'cx> {
        apply_any(self, v)
    }

    pub fn to_hir(&self, venv: VarEnv) -> Hir<'cx> {
        let map_uniontype =
            |kts: &HashMap<Label, Option<Nir<'cx>>>| -> ExprKind<Hir<'cx>> {
                ExprKind::UnionType(
                    kts.iter()
                        .map(|(k, v)| {
                            (k.clone(), v.as_ref().map(|v| v.to_hir(venv)))
                        })
                        .collect(),
                )
            };
        let builtin =
            |b| Hir::new(HirKind::Expr(ExprKind::Builtin(b)), Span::Artificial);

        let hir = match self.kind() {
            NirKind::Var(v) => HirKind::Var(venv.lookup(*v)),
            NirKind::AppliedBuiltin(closure) => closure.to_hirkind(venv),
            self_kind => HirKind::Expr(match self_kind {
                NirKind::Var(..) | NirKind::AppliedBuiltin(..) => {
                    unreachable!()
                }
                NirKind::LamClosure {
                    binder,
                    annot,
                    closure,
                } => ExprKind::Lam(
                    binder.to_label(),
                    annot.to_hir(venv),
                    closure.to_hir(venv),
                ),
                NirKind::PiClosure {
                    binder,
                    annot,
                    closure,
                } => ExprKind::Pi(
                    binder.to_label(),
                    annot.to_hir(venv),
                    closure.to_hir(venv),
                ),
                NirKind::Const(c) => ExprKind::Const(*c),
                NirKind::BuiltinType(b) => ExprKind::Builtin(*b),
                NirKind::Num(l) => ExprKind::Num(l.clone()),
                NirKind::OptionalType(t) => ExprKind::Op(OpKind::App(
                    builtin(Builtin::Optional),
                    t.to_hir(venv),
                )),
                NirKind::EmptyOptionalLit(n) => ExprKind::Op(OpKind::App(
                    builtin(Builtin::OptionalNone),
                    n.to_hir(venv),
                )),
                NirKind::NEOptionalLit(n) => ExprKind::SomeLit(n.to_hir(venv)),
                NirKind::ListType(t) => ExprKind::Op(OpKind::App(
                    builtin(Builtin::List),
                    t.to_hir(venv),
                )),
                NirKind::EmptyListLit(n) => ExprKind::EmptyListLit(Hir::new(
                    HirKind::Expr(ExprKind::Op(OpKind::App(
                        builtin(Builtin::List),
                        n.to_hir(venv),
                    ))),
                    Span::Artificial,
                )),
                NirKind::NEListLit(elts) => ExprKind::NEListLit(
                    elts.iter().map(|v| v.to_hir(venv)).collect(),
                ),
                NirKind::TextLit(elts) => ExprKind::TextLit(
                    elts.iter()
                        .map(|t| t.map_ref(|v| v.to_hir(venv)))
                        .collect(),
                ),
                NirKind::RecordLit(kvs) => ExprKind::RecordLit(
                    kvs.iter()
                        .map(|(k, v)| (k.clone(), v.to_hir(venv)))
                        .collect(),
                ),
                NirKind::RecordType(kts) => ExprKind::RecordType(
                    kts.iter()
                        .map(|(k, v)| (k.clone(), v.to_hir(venv)))
                        .collect(),
                ),
                NirKind::UnionType(kts) => map_uniontype(kts),
                NirKind::UnionConstructor(l, kts) => {
                    ExprKind::Op(OpKind::Field(
                        Hir::new(
                            HirKind::Expr(map_uniontype(kts)),
                            Span::Artificial,
                        ),
                        l.clone(),
                    ))
                }
                NirKind::UnionLit(l, v, kts) => ExprKind::Op(OpKind::App(
                    Hir::new(
                        HirKind::Expr(ExprKind::Op(OpKind::Field(
                            Hir::new(
                                HirKind::Expr(map_uniontype(kts)),
                                Span::Artificial,
                            ),
                            l.clone(),
                        ))),
                        Span::Artificial,
                    ),
                    v.to_hir(venv),
                )),
                NirKind::Equivalence(x, y) => ExprKind::Op(OpKind::BinOp(
                    BinOp::Equivalence,
                    x.to_hir(venv),
                    y.to_hir(venv),
                )),
                NirKind::Assert(x) => ExprKind::Assert(x.to_hir(venv)),
                NirKind::Op(e) => ExprKind::Op(e.map_ref(|v| v.to_hir(venv))),
            }),
        };

        Hir::new(hir, Span::Artificial)
    }
    pub fn to_hir_noenv(&self) -> Hir<'cx> {
        self.to_hir(VarEnv::new())
    }
}

impl<'cx> NirKind<'cx> {
    pub fn into_nir(self) -> Nir<'cx> {
        Nir::from_kind(self)
    }

    pub fn from_builtin(cx: Ctxt<'cx>, b: Builtin) -> Self {
        NirKind::from_builtin_env(b, NzEnv::new(cx))
    }
    pub fn from_builtin_env(b: Builtin, env: NzEnv<'cx>) -> Self {
        BuiltinClosure::new(b, env)
    }
}

impl<'cx> Thunk<'cx> {
    fn new(env: NzEnv<'cx>, body: Hir<'cx>) -> Self {
        Thunk::Thunk { env, body }
    }
    fn from_partial_expr(expr: ExprKind<Nir<'cx>>) -> Self {
        Thunk::PartialExpr { expr }
    }
    fn eval(self) -> NirKind<'cx> {
        match self {
            Thunk::Thunk { env, body, .. } => normalize_hir(&env, &body),
            Thunk::PartialExpr { expr } => normalize_one_layer(expr),
        }
    }
}

impl<'cx> Closure<'cx> {
    pub fn new(env: &NzEnv<'cx>, body: Hir<'cx>) -> Self {
        Closure::Closure {
            env: env.clone(),
            body,
        }
    }
    /// New closure that ignores its argument
    pub fn new_constant(body: Nir<'cx>) -> Self {
        Closure::ConstantClosure { body }
    }

    pub fn apply(&self, val: Nir<'cx>) -> Nir<'cx> {
        match self {
            Closure::Closure { env, body, .. } => {
                body.eval(env.insert_value(val, ()))
            }
            Closure::ConstantClosure { body, .. } => body.clone(),
        }
    }
    fn apply_var(&self, var: NzVar) -> Nir<'cx> {
        match self {
            Closure::Closure { .. } => {
                self.apply(Nir::from_kind(NirKind::Var(var)))
            }
            Closure::ConstantClosure { body, .. } => body.clone(),
        }
    }

    /// Convert this closure to a Hir expression
    pub fn to_hir(&self, venv: VarEnv) -> Hir<'cx> {
        self.apply_var(NzVar::new(venv.size()))
            .to_hir(venv.insert())
    }
    /// If the closure variable is free in the closure, return Err. Otherwise, return the value
    /// with that free variable remove.
    pub fn remove_binder(&self) -> Result<Nir<'cx>, ()> {
        match self {
            Closure::Closure { .. } => {
                let v = NzVar::fresh();
                // TODO: handle case where variable is used in closure
                // TODO: return information about where the variable is used
                Ok(self.apply_var(v))
            }
            Closure::ConstantClosure { body, .. } => Ok(body.clone()),
        }
    }
}

impl<'cx> TextLit<'cx> {
    pub fn new(
        elts: impl Iterator<Item = InterpolatedTextContents<Nir<'cx>>>,
    ) -> Self {
        TextLit(squash_textlit(elts))
    }
    pub fn interpolate(v: Nir<'cx>) -> Self {
        TextLit(vec![InterpolatedTextContents::Expr(v)])
    }
    pub fn from_text(s: String) -> Self {
        TextLit(vec![InterpolatedTextContents::Text(s)])
    }

    pub fn concat(&self, other: &Self) -> Self {
        TextLit::new(self.iter().chain(other.iter()).cloned())
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    /// If the literal consists of only one interpolation and not text, return the interpolated
    /// value.
    pub fn as_single_expr(&self) -> Option<&Nir<'cx>> {
        use InterpolatedTextContents::Expr;
        if let [Expr(v)] = self.0.as_slice() {
            Some(v)
        } else {
            None
        }
    }
    /// If there are no interpolations, return the corresponding text value.
    pub fn as_text(&self) -> Option<String> {
        use InterpolatedTextContents::Text;
        if self.is_empty() {
            Some(String::new())
        } else if let [Text(s)] = self.0.as_slice() {
            Some(s.clone())
        } else {
            None
        }
    }
    pub fn iter(
        &self,
    ) -> impl Iterator<Item = &InterpolatedTextContents<Nir<'cx>>> {
        self.0.iter()
    }
}

impl<'cx> lazy::Eval<NirKind<'cx>> for Thunk<'cx> {
    fn eval(self) -> NirKind<'cx> {
        self.eval()
    }
}

/// Compare two values for equality modulo alpha/beta-equivalence.
impl<'cx> std::cmp::PartialEq for Nir<'cx> {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0) || self.kind() == other.kind()
    }
}
impl<'cx> std::cmp::Eq for Nir<'cx> {}

impl<'cx> std::cmp::PartialEq for Thunk<'cx> {
    fn eq(&self, _other: &Self) -> bool {
        unreachable!(
            "Trying to compare thunks but we should only compare WHNFs"
        )
    }
}
impl<'cx> std::cmp::Eq for Thunk<'cx> {}

impl<'cx> std::cmp::PartialEq for Closure<'cx> {
    fn eq(&self, other: &Self) -> bool {
        let v = NzVar::fresh();
        self.apply_var(v) == other.apply_var(v)
    }
}
impl<'cx> std::cmp::Eq for Closure<'cx> {}

impl<'cx> std::fmt::Debug for Nir<'cx> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let NirKind::Const(c) = self.kind() {
            return write!(fmt, "{:?}", c);
        }
        let mut x = fmt.debug_struct("Nir");
        x.field("kind", self.kind());
        x.finish()
    }
}