lisette_semantics/checker/
type_env.rs1use ecow::EcoString;
16use syntax::types::{Bound, Type, TypeVarId};
17
18#[derive(Debug, Clone)]
19pub enum VarState {
20 Unbound { hint: Option<EcoString> },
21 Bound(Type),
22}
23
24pub struct TypeEnv {
25 entries: Vec<VarState>,
26 undo_log: Option<Vec<(TypeVarId, VarState)>>,
29}
30
31impl Default for TypeEnv {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl TypeEnv {
38 pub fn new() -> Self {
39 Self {
40 entries: Vec::new(),
41 undo_log: None,
42 }
43 }
44
45 pub fn fresh(&mut self, hint: Option<EcoString>) -> TypeVarId {
47 let id = TypeVarId(self.entries.len() as u32);
48 self.entries.push(VarState::Unbound { hint });
49 id
50 }
51
52 fn slot(id: TypeVarId) -> usize {
53 debug_assert!(
54 !id.is_reserved(),
55 "TypeEnv should not be queried for reserved ids"
56 );
57 id.0 as usize
58 }
59
60 pub fn state(&self, id: TypeVarId) -> &VarState {
61 &self.entries[Self::slot(id)]
62 }
63
64 pub fn is_unbound(&self, id: TypeVarId) -> bool {
65 if id.is_reserved() {
66 return true;
67 }
68 matches!(self.entries[Self::slot(id)], VarState::Unbound { .. })
69 }
70
71 pub fn bind(&mut self, id: TypeVarId, ty: Type) {
74 if id.is_reserved() {
75 return;
76 }
77 let slot = Self::slot(id);
78 let old = std::mem::replace(&mut self.entries[slot], VarState::Bound(ty));
79 if let Some(log) = &mut self.undo_log {
80 log.push((id, old));
81 }
82 }
83
84 pub fn shallow_resolve(&self, ty: &Type) -> Type {
87 let mut current = ty.clone();
88 loop {
89 match ¤t {
90 Type::Var { id, .. } if !id.is_reserved() => match &self.entries[Self::slot(*id)] {
91 VarState::Unbound { .. } => return current,
92 VarState::Bound(bound) => current = bound.clone(),
93 },
94 _ => return current,
95 }
96 }
97 }
98
99 pub fn resolve(&self, ty: &Type) -> Type {
104 match ty {
105 Type::Var { id, .. } if !id.is_reserved() => match &self.entries[Self::slot(*id)] {
106 VarState::Unbound { .. } => ty.clone(),
107 VarState::Bound(bound) => self.resolve(bound),
108 },
109 Type::Nominal {
110 id,
111 params,
112 underlying_ty,
113 } => Type::Nominal {
114 id: id.clone(),
115 params: params.iter().map(|p| self.resolve(p)).collect(),
116 underlying_ty: underlying_ty.as_ref().map(|u| Box::new(self.resolve(u))),
117 },
118 Type::Compound { kind, args } => Type::Compound {
119 kind: *kind,
120 args: args.iter().map(|a| self.resolve(a)).collect(),
121 },
122 Type::Function(f) => Type::function(
123 f.params.iter().map(|p| self.resolve(p)).collect(),
124 f.param_mutability.clone(),
125 f.bounds
126 .iter()
127 .map(|b| Bound {
128 param_name: b.param_name.clone(),
129 generic: self.resolve(&b.generic),
130 ty: self.resolve(&b.ty),
131 })
132 .collect(),
133 Box::new(self.resolve(&f.return_type)),
134 ),
135 Type::Forall { vars, body } => Type::Forall {
136 vars: vars.clone(),
137 body: Box::new(self.resolve(body)),
138 },
139 Type::Tuple(elements) => {
140 Type::Tuple(elements.iter().map(|e| self.resolve(e)).collect())
141 }
142 _ => ty.clone(),
143 }
144 }
145
146 pub fn occurs(&self, id: TypeVarId, ty: &Type) -> bool {
149 match ty {
150 Type::Var { id: other, .. } => {
151 if *other == id {
152 return true;
153 }
154 if other.is_reserved() {
155 return false;
156 }
157 match &self.entries[Self::slot(*other)] {
158 VarState::Unbound { .. } => false,
159 VarState::Bound(bound) => self.occurs(id, bound),
160 }
161 }
162 Type::Nominal { params, .. } => params.iter().any(|p| self.occurs(id, p)),
163 Type::Compound { args, .. } => args.iter().any(|a| self.occurs(id, a)),
164 Type::Function(f) => {
165 f.params.iter().any(|p| self.occurs(id, p)) || self.occurs(id, &f.return_type)
166 }
167 Type::Forall { body, .. } => self.occurs(id, body),
168 Type::Tuple(elements) => elements.iter().any(|e| self.occurs(id, e)),
169 _ => false,
170 }
171 }
172
173 pub fn begin_speculation(&mut self) -> Speculation {
176 let prev = self.undo_log.take();
177 self.undo_log = Some(Vec::new());
178 Speculation { prev }
179 }
180
181 pub fn end_speculation(&mut self, spec: Speculation, is_err: bool) {
186 let log = self.undo_log.take().expect("speculation log must exist");
187 self.undo_log = spec.prev;
188 if is_err {
189 for (id, original) in log.into_iter().rev() {
190 self.entries[Self::slot(id)] = original;
191 }
192 } else if let Some(parent_log) = &mut self.undo_log {
193 parent_log.extend(log);
194 }
195 }
196}
197
198#[must_use]
201pub struct Speculation {
202 prev: Option<Vec<(TypeVarId, VarState)>>,
203}
204
205pub trait EnvResolve {
208 fn resolve_in(&self, env: &TypeEnv) -> Type;
209 fn shallow_resolve_in(&self, env: &TypeEnv) -> Type;
210}
211
212impl EnvResolve for Type {
213 fn resolve_in(&self, env: &TypeEnv) -> Type {
214 env.resolve(self)
215 }
216 fn shallow_resolve_in(&self, env: &TypeEnv) -> Type {
217 env.shallow_resolve(self)
218 }
219}