summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/tck/tyexpr.rs
blob: ac15ac54cb83e210703a9a97be075e627f4e5eaf (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
use crate::semantics::{Hir, HirKind, NzEnv, TyEnv, Value};
use crate::syntax::{Const, Span};
use crate::{NormalizedExpr, ToExprOptions};

pub(crate) type Type = Value;

// A hir expression plus its inferred type.
#[derive(Debug, Clone)]
pub(crate) struct TyExpr {
    hir: Hir,
    ty: Type,
}

impl TyExpr {
    pub fn new(kind: HirKind, ty: Type, span: Span) -> Self {
        TyExpr {
            hir: Hir::new(kind, span),
            ty,
        }
    }

    pub fn span(&self) -> Span {
        self.as_hir().span()
    }
    pub fn ty(&self) -> &Type {
        &self.ty
    }
    pub fn get_type_tyexpr(&self, env: &TyEnv) -> TyExpr {
        self.ty()
            .to_hir(env.as_varenv())
            .typecheck(env)
            .expect("Internal type error")
    }
    /// Get the kind (the type of the type) of this value
    // TODO: avoid recomputing so much
    pub fn get_kind(&self, env: &TyEnv) -> Option<Const> {
        self.get_type_tyexpr(env).ty().as_const()
    }

    pub fn to_hir(&self) -> Hir {
        self.as_hir().clone()
    }
    pub fn as_hir(&self) -> &Hir {
        &self.hir
    }
    /// Converts a value back to the corresponding AST expression.
    pub fn to_expr(&self, opts: ToExprOptions) -> NormalizedExpr {
        self.as_hir().to_expr(opts)
    }

    /// Eval the TyExpr. It will actually get evaluated only as needed on demand.
    pub fn eval(&self, env: impl Into<NzEnv>) -> Value {
        self.as_hir().eval(&env.into())
    }
    /// Eval a closed TyExpr (i.e. without free variables). It will actually get evaluated only as
    /// needed on demand.
    pub fn eval_closed_expr(&self) -> Value {
        self.eval(NzEnv::new())
    }
    /// Eval a closed TyExpr fully and recursively;
    pub fn rec_eval_closed_expr(&self) -> Value {
        let val = self.eval_closed_expr();
        val.normalize();
        val
    }
}