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
|
use crate::error::TypeError;
use crate::semantics::{Hir, HirKind, NzEnv, TyEnv, Value, ValueKind, VarEnv};
use crate::syntax::{Builtin, Const, Span};
use crate::{NormalizedExpr, ToExprOptions};
/// An expression representing a type
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Type {
val: Value,
}
/// A hir expression plus its inferred type.
#[derive(Debug, Clone)]
pub(crate) struct TyExpr {
hir: Hir,
ty: Type,
}
impl Type {
pub fn from_const(c: Const) -> Self {
Value::from_const(c).into()
}
pub fn from_builtin(b: Builtin) -> Self {
Value::from_builtin(b).into()
}
pub fn to_value(&self) -> Value {
self.val.clone()
}
pub fn as_value(&self) -> &Value {
&self.val
}
pub fn into_value(self) -> Value {
self.val
}
pub fn as_const(&self) -> Option<Const> {
self.val.as_const()
}
pub fn kind(&self) -> &ValueKind {
self.val.kind()
}
pub fn to_hir(&self, venv: VarEnv) -> Hir {
self.val.to_hir(venv)
}
pub fn to_expr_tyenv(&self, tyenv: &TyEnv) -> NormalizedExpr {
self.val.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv)
}
}
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
}
/// Get the kind (the type of the type) of this value
// TODO: avoid recomputing so much
pub fn get_kind(&self, env: &TyEnv) -> Result<Option<Const>, TypeError> {
Ok(self
.ty()
.to_hir(env.as_varenv())
.typecheck(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())
}
/// Evaluate to a Type.
pub fn eval_to_type(&self, env: impl Into<NzEnv>) -> Type {
self.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
}
}
impl From<Value> for Type {
fn from(x: Value) -> Type {
Type { val: x }
}
}
|