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 { 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::, _>>()?; 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::, _>>()?; 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: Element, claimed: &Set, should_be: Set, ) -> Result<(), CheckerError> { if !self.equal(claimed, &should_be) { Err(CheckerError::WrongSetForElement { value: value.clone().into(), claimed: claimed.clone(), real: should_be, }) } else { Ok(()) } } #[instrument(skip(self), level = "debug", fields(%element, %set))] pub fn check_element(&self, element: Element, set: &Set) -> Result { match element { Element::Literal(ref lit) => { let value = element.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()) } 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: element.clone().into(), claimed: set.clone(), real: lookup.container.clone(), }); } // If we found a formal binding, we have no value to report. // This is the ONLY source of Var as a return value for // check_element, so in other branches we condition our logic // for formal bindings on finding Var after recursing. if let ElementValue::Concrete(ref deref) = lookup.value { Ok(deref.clone()) } else { Ok(Element::Var(v.clone())) } } Element::Record(ref assignations) => { let rej = |reason| CheckerError::ElementDoesNotBelong { element: element.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, Vec) = 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, 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::, _>>()?; // rebuild let assignations = zip(element_fnames, sub_els) .map(|(name, element)| ElemAssign { name, element }) .collect(); // resign? Ok(Element::Record(assignations)) } 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: element.clone().into(), claimed: set.clone(), real: field_set.clone(), }); } // enforce the correct typing of the element let inner = self.check_element(*inner.clone(), owner_set)?; // Unfortunately we still have to do something nasty here to // obtain the data match inner { // We're stuck on something that bottoms out in a binding // blocking computation, nothing to be done here Element::Var(_) | Element::Project { .. } | Element::Case { .. } => { Ok(Element::Project { element: Box::new(inner), field: field.clone(), }) } Element::Record(assignations) => { let sub_element = assignations .into_iter() .find(|a| a.name == *field) .expect( "invariant violation: record missing field that was type-checked", ) .element; Ok(sub_element) } _ => panic!( "invariant violation: check_element returned neither a record or stuck computation for record set" ), } } 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: element.clone().into(), claimed: set.clone(), real: owner_set.clone(), }); } // enforce the correct typing of the element let element = self.check_element(*inner.clone(), field_set)?; Ok(Element::Inject { element: Box::new(element), field: field.clone(), }) } 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::, _>>()?; let owner = arm_owners[0]; // safe because of the above decision about bottom if !arm_owners.into_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 = fields.iter().map(|vf| vf.name.clone()).collect(); required_field_names_sorted.sort(); let mut covered_field_names_sorted: Vec = 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(), owner)?; // which variant are we, if any let matching: Option<(String, Element)> = match scrutinee { Element::Inject { ref field, element: ref inner, } => Some((field.clone(), *inner.clone())), // These are all the cases which could become stuck on a // formal binding Element::Var(_) | Element::Project { .. } | Element::Case { .. } => None, Element::Literal(_) | Element::Record(_) => panic!( "invariant violation: scrutinee is a non-variant value at variant set" ), }; // 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; let mut processed_arms = Vec::new(); for arm in arms { let Field { field: field_set, .. } = self.lookup_variant_field(&arm.tag)?; let mut new_context = self.clone(); let binding_name = arm.bound.clone(); let binding_set = field_set.clone(); let body = if let Some((tag, inner)) = &matching && *tag == arm.tag { new_context.add_element( binding_name.clone(), 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.clone()); output } else { new_context.add_element( binding_name.clone(), Value::Hypothetical(field_set.clone()), binding_set, )?; new_context.check_element(arm.body.clone().into(), set)? }; processed_arms.push(CaseArm { tag: arm.tag.clone(), bound: binding_name, body, }); } Ok(computed_output.unwrap_or(Element::Case { scrutinee: Box::new(scrutinee), arms: processed_arms, })) } } } }