From 6037cb224c5e61828ba41cb3d34438ad03a71403 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 9 Mar 2019 15:46:30 +0100 Subject: Remove the pervasive Label type parameter Closes #1 --- dhall/src/typecheck.rs | 136 ++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 80 deletions(-) (limited to 'dhall/src/typecheck.rs') diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 0dbb2f9..28982fc 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -29,8 +29,8 @@ fn rule(a: core::Const, b: core::Const) -> Result { } } -fn match_vars(vl: &V, vr: &V, ctx: &[(L, L)]) -> bool { - let xxs: Option<(&(L, L), &[(L, L)])> = ctx.split_first(); +fn match_vars(vl: &V, vr: &V, ctx: &[(Label, Label)]) -> bool { + let xxs: Option<(&(Label, Label), &[(Label, Label)])> = ctx.split_first(); match (vl, vr, xxs) { (V(xL, nL), V(xR, nR), None) => xL == xR && nL == nR, (V(xL, 0), V(xR, 0), Some(((xL2, xR2), _))) @@ -46,15 +46,15 @@ fn match_vars(vl: &V, vr: &V, ctx: &[(L, L)]) -> bool { } } -fn prop_equal(eL0: &Expr, eR0: &Expr) -> bool +fn prop_equal(eL0: &Expr, eR0: &Expr) -> bool where S: Clone + ::std::fmt::Debug, T: Clone + ::std::fmt::Debug, { fn go( ctx: &mut Vec<(Label, Label)>, - el: &Expr, - er: &Expr, + el: &Expr, + er: &Expr, ) -> bool where S: Clone + ::std::fmt::Debug, @@ -144,16 +144,16 @@ where } fn op2_type( - ctx: &Context>, - e: &Expr, + ctx: &Context>, + e: &Expr, t: core::Builtin, ef: EF, - l: &Expr, - r: &Expr, -) -> Result, TypeError> + l: &Expr, + r: &Expr, +) -> Result, TypeError> where S: Clone + ::std::fmt::Debug, - EF: FnOnce(Expr, Expr) -> TypeMessage, + EF: FnOnce(Expr, Expr) -> TypeMessage, { let tl = normalize(&type_with(ctx, l)?); match tl { @@ -177,9 +177,9 @@ where /// is not necessary for just type-checking. If you actually care about the /// returned type then you may want to `normalize` it afterwards. pub fn type_with( - ctx: &Context>, - e: &Expr, -) -> Result, TypeError> + ctx: &Context>, + e: &Expr, +) -> Result, TypeError> where S: Clone + ::std::fmt::Debug, { @@ -416,7 +416,7 @@ where } ListLit(ref t, ref xs) => { let mut iter = xs.iter().enumerate(); - let t: Box> = match t { + let t: Box> = match t { Some(t) => t.clone(), None => { let (_, first_x) = iter.next().unwrap(); @@ -486,7 +486,7 @@ where pi("_", app(List, "a"), app(Optional, "a")), )), Builtin(ListIndexed) => { - let mut m: BTreeMap> = BTreeMap::new(); + let mut m: BTreeMap> = BTreeMap::new(); m.insert("index".into(), Builtin(Natural)); m.insert("value".into(), Var(V("a".into(), 0))); Ok(pi( @@ -502,7 +502,7 @@ where )), OptionalLit(ref t, ref xs) => { let mut iter = xs.iter(); - let t: Box> = match t { + let t: Box> = match t { Some(t) => t.clone(), None => { let first_x = iter.next().unwrap(); @@ -707,8 +707,8 @@ where /// expression must be closed (i.e. no free variables), otherwise type-checking /// will fail. pub fn type_of( - e: &Expr, -) -> Result, TypeError> { + e: &Expr, +) -> Result, TypeError> { let ctx = Context::new(); type_with(&ctx, e) //.map(|e| e.into_owned()) } @@ -717,83 +717,59 @@ pub fn type_of( #[derive(Debug)] pub enum TypeMessage { UnboundVariable, - InvalidInputType(Expr), - InvalidOutputType(Expr), - NotAFunction(Expr, Expr), - TypeMismatch( - Expr, - Expr, - Expr, - Expr, - ), - AnnotMismatch(Expr, Expr, Expr), + InvalidInputType(Expr), + InvalidOutputType(Expr), + NotAFunction(Expr, Expr), + TypeMismatch(Expr, Expr, Expr, Expr), + AnnotMismatch(Expr, Expr, Expr), Untyped, - InvalidListElement( - usize, - Expr, - Expr, - Expr, - ), - InvalidListType(Expr), - InvalidOptionalElement( - Expr, - Expr, - Expr, - ), + InvalidListElement(usize, Expr, Expr, Expr), + InvalidListType(Expr), + InvalidOptionalElement(Expr, Expr, Expr), InvalidOptionalLiteral(usize), - InvalidOptionalType(Expr), - InvalidPredicate(Expr, Expr), - IfBranchMismatch( - Expr, - Expr, - Expr, - Expr, - ), - IfBranchMustBeTerm( - bool, - Expr, - Expr, - Expr, - ), - InvalidField(Label, Expr), - InvalidFieldType(Label, Expr), - InvalidAlternative(Label, Expr), - InvalidAlternativeType(Label, Expr), + InvalidOptionalType(Expr), + InvalidPredicate(Expr, Expr), + IfBranchMismatch(Expr, Expr, Expr, Expr), + IfBranchMustBeTerm(bool, Expr, Expr, Expr), + InvalidField(Label, Expr), + InvalidFieldType(Label, Expr), + InvalidAlternative(Label, Expr), + InvalidAlternativeType(Label, Expr), DuplicateAlternative(Label), - MustCombineARecord(Expr, Expr), + MustCombineARecord(Expr, Expr), FieldCollision(Label), - MustMergeARecord(Expr, Expr), - MustMergeUnion(Expr, Expr), + MustMergeARecord(Expr, Expr), + MustMergeUnion(Expr, Expr), UnusedHandler(HashSet