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
|
use crate::ast::*;
use crate::checker_state::*;
use std::iter::zip;
use tracing::instrument;
impl CheckerState {
#[instrument(skip(self), level = "debug", fields(%set))]
pub fn check_set(&self, set: Set) -> Result<Set, CheckerError> {
match set {
Set::BuiltIn(_) => Ok(set.clone()),
Set::Record(fields) => {
let mut ctx = self.clone();
let fields = fields
.into_iter()
.map(|RecordField { name, set }| {
let set = ctx.check_set(set)?;
ctx.add_element(
name.clone(),
Value::Hypothetical(set.clone()),
set.clone(),
)?;
Ok(RecordField { name, set })
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Set::Record(fields))
}
Set::Variant(fields) => {
let fields = fields
.into_iter()
.map(|VariantField { name, set }| {
let set = self.check_set(set)?;
Ok(VariantField {
name: name.clone(),
set,
})
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Set::Variant(fields))
}
Set::ClaimedSet(_) => Err(CheckerError::Unimplemented("instances as sets".to_string())),
Set::Var(v) => {
let deref = self.lookup_set(&v)?;
Ok(deref.clone())
}
}
}
fn _check_literal_set_helper(
&self,
value: ElementValue,
claimed: &Set,
should_be: Set,
) -> Result<(), CheckerError> {
if !self.equal(claimed, &should_be) {
Err(CheckerError::WrongSetForElement {
value: value.clone(),
claimed: claimed.clone(),
real: should_be,
})
} else {
Ok(())
}
}
#[instrument(skip(self), level = "debug", fields(%value, %set))]
pub fn check_element(
&self,
value: ElementValue,
set: &Set,
) -> Result<ElementValue, CheckerError> {
match value {
// This arm ensures that if we ever in the position of obtaining a
// hypothetical from a call to check_element, in the context of
// check_element, we can safely ignore its payload. I'll point this
// out later as (*)
Value::Hypothetical(ref h_set) => {
if !self.equal(set, &h_set) {
Err(CheckerError::WrongSetForElement {
value: value.clone(),
claimed: set.clone(),
real: h_set.clone(),
})
} else {
Ok(value)
}
}
Value::Concrete(ref element @ Element::Literal(ref lit)) => {
let value = value.clone();
// we may infer the type from the element
match lit {
Literal::Int(_) => {
self._check_literal_set_helper(value, set, Set::BuiltIn(BuiltIn::Int))?;
}
Literal::Nat(_) => {
self._check_literal_set_helper(value, set, Set::BuiltIn(BuiltIn::Nat))?;
}
Literal::Str(_) => {
self._check_literal_set_helper(value, set, Set::BuiltIn(BuiltIn::Str))?;
}
Literal::Bool(_) => {
self._check_literal_set_helper(value, set, Set::BuiltIn(BuiltIn::Bool))?;
}
Literal::Float(_) => {
self._check_literal_set_helper(value, set, Set::BuiltIn(BuiltIn::Float))?;
}
}
Ok(element.clone().into())
}
Value::Concrete(Element::Var(ref v)) => {
let lookup = self.lookup_element(&v)?;
// we have previously done the work to discover the type of
// this element, so what we're claiming now must match!
if !self.equal(set, &lookup.container) {
return Err(CheckerError::WrongSetForElement {
value: value.clone(),
claimed: set.clone(),
real: lookup.container.clone(),
});
}
Ok(lookup.value.clone())
}
Value::Concrete(ref concrete @ Element::Record(ref assignations)) => {
let rej = |reason| CheckerError::ElementDoesNotBelong {
element: concrete.clone(),
claimed: set.clone(),
reason,
};
// make sure we are filling a record
let fields = if let Set::Record(fields) = set {
Ok(fields)
} else {
Err(rej("element is a record instance".to_string()))
}?;
let (set_fnames, set_fsets): (Vec<String>, Vec<Set>) = fields
.iter()
.map(|RecordField { name, set }| (name.clone(), set.clone()))
.unzip();
let mut set_fnames_sorted = set_fnames.clone();
set_fnames_sorted.sort();
let (element_fnames, element_felements): (Vec<String>, Vec<&Element>) =
assignations
.iter()
.map(|ElemAssign { name, element }| (name.clone(), element))
.unzip();
let mut element_fnames_sorted = element_fnames.clone();
element_fnames_sorted.sort();
if set_fnames_sorted != element_fnames_sorted {
return Err(rej(format!(
"expected [{}] but found [{}]",
set_fnames.join(", "),
element_fnames.join(", "),
)));
}
// recurse, sets have already been completely expanded
let sub_els = zip(element_felements, set_fsets)
.map(|(e_f, e_s)| self.check_element(e_f.clone().into(), &e_s))
.collect::<Result<Vec<_>, _>>()?;
// rebuild, hypotheticals are contagious
let assignations = zip(element_fnames, sub_els)
.map(|(name, element)| match element {
Value::Concrete(element) => Some(ElemAssign { name, element }),
Value::Hypothetical(_) => None,
})
.collect();
// resign?
Ok(if let Some(assignations) = assignations {
Element::Record(assignations).into()
} else {
Value::Hypothetical(set.clone())
})
}
Value::Concrete(Element::Project {
element: ref inner,
ref field,
}) => {
// globally unique projections mean we know what the sets going
// in and out must be
let Field {
field: field_set,
owner: owner_set,
} = self.lookup_record_field(&field)?;
// enforce the correct typing of the claimed result
if !self.equal(set, field_set) {
return Err(CheckerError::WrongSetForElement {
value,
claimed: set.clone(),
real: field_set.clone(),
});
}
// enforce the correct typing of the element
let inner = self.check_element((*inner.clone()).into(), owner_set)?;
match inner {
Value::Concrete(inner) => {
// Unfortunately we still have to do something nasty here to obtain the data
let Element::Record(assignations) = inner else {
panic!(
"invariant violation: check_element returned non-record for record set"
);
};
let sub_element = assignations
.into_iter()
.find(|a| a.name == *field)
.expect(
"invariant violation: record missing field that was type-checked",
)
.element
.clone();
Ok(sub_element.into())
}
// correct by (*)
Value::Hypothetical(_) => Ok(Value::Hypothetical(set.clone())),
}
}
Value::Concrete(Element::Inject {
element: ref inner,
ref field,
}) => {
// globally unique injections mean that we know what the sets
// going in and out must be, but compared to projections their
// roles are here interchanged
let Field {
field: field_set,
owner: owner_set,
} = self.lookup_variant_field(&field)?;
// enforce the correct typing of the claimed result
if !self.equal(set, owner_set) {
return Err(CheckerError::WrongSetForElement {
value,
claimed: set.clone(),
real: owner_set.clone(),
});
}
// enforce the correct typing of the element
let element = self.check_element((*inner.clone()).into(), field_set)?;
match element {
Value::Concrete(element) => Ok(Element::Inject {
element: Box::new(element),
field: field.clone(),
}
.into()),
// correct by (*)
Value::Hypothetical(_) => Ok(Value::Hypothetical(set.clone())),
}
}
Value::Concrete(
ref element @ Element::Case {
ref arms,
ref scrutinee,
},
) => {
// TODO: do we allow mapping out of bottom?
if arms.is_empty() {
return Err(CheckerError::Unimplemented(
"mapping out of bottom types".to_string(),
));
}
// 1. Syntactic checks
// -------------------
// arms agree on the set to which the scrutinee should belong
let arm_owners = arms
.iter()
.map(|ca| self.lookup_variant_field(&ca.tag).map(|sf| &sf.owner))
.collect::<Result<Vec<_>, _>>()?;
let owner = arm_owners[0]; // safe because of the above decision about bottom
if !arm_owners.iter().all(|o| self.equal(owner, o)) {
return Err(CheckerError::IncosistentCaseScrutineeSet(element.clone()));
}
// all cases are handled
let Set::Variant(fields) = owner else {
panic!(
"invariant violation: looking up the owner of a variant field resulted in a non-variant set",
)
};
let mut required_field_names_sorted: Vec<String> =
fields.iter().map(|vf| vf.name.clone()).collect();
required_field_names_sorted.sort();
let mut covered_field_names_sorted: Vec<String> =
arms.iter().map(|ca| ca.tag.clone()).collect();
covered_field_names_sorted.sort();
if required_field_names_sorted != covered_field_names_sorted {
return Err(CheckerError::IncompleteCaseAnalysis {
found: covered_field_names_sorted,
required: required_field_names_sorted,
});
}
// 2. semantic checks
// ------------------
// scrutinee must be of the same set that all the arms are
// implying, in particular this implies that the following holds
// `inner : self.lookup_variant_field(field).field_set`
let scrutinee = self.check_element((*scrutinee.clone()).into(), owner)?;
// which variant are we, if any
let matching: Option<(String, Element)> = match scrutinee {
Value::Hypothetical(_) => None,
Value::Concrete(Element::Inject {
field,
element: inner,
}) => Some((field, *inner)),
_ => {
panic!(
"invariant violation: we believe element is of a variant set but it's not an injection"
);
}
};
// for each arm, recurse with a concrete value (if we have one)
// otherwise fall back to hypothetical elements; in the former
// case record the end result
let mut computed_output = None;
for arm in arms {
let Field {
field: field_set, ..
} = self.lookup_variant_field(&arm.tag)?;
// TODO: if we were worried about overhead we'd have a separate
// locals stack, though truly if we were worried about overhead
// we'd not have NNN instances of clone elsewhere in the
// codebase and we wouldn't be eagerly evaluating all
// expressions fully.
let mut new_context = self.clone();
let binding_name = arm.bound.clone();
let binding_set = field_set.clone();
if let Some((tag, inner)) = &matching
&& *tag == arm.tag
{
new_context.add_element(binding_name, inner.clone().into(), binding_set)?;
let output = new_context.check_element(arm.body.clone().into(), set)?;
if matches!(computed_output, Some(_)) {
panic!(
"invariant violation: we somehow matched multiple arms in case analysis"
)
}
computed_output = Some(output);
} else {
new_context.add_element(
binding_name,
Value::Hypothetical(field_set.clone()),
binding_set,
)?;
new_context.check_element(arm.body.clone().into(), set)?;
};
}
Ok(computed_output.unwrap_or(Value::Hypothetical(set.clone())))
}
}
}
}
|