From f5d2151d35942b957230c3081a928af3619d9400 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 31 Mar 2019 18:47:34 +0200 Subject: Make SubExpr a newtype --- dhall/src/typecheck.rs | 103 +++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 55 deletions(-) (limited to 'dhall/src/typecheck.rs') diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index f21721d..e769c7f 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -1,7 +1,6 @@ #![allow(non_snake_case)] use std::collections::HashSet; use std::fmt; -use std::rc::Rc; use crate::normalize::normalize; use dhall_core; @@ -113,7 +112,7 @@ where go::(&mut ctx, eL0, eR0) } -fn type_of_builtin(b: Builtin) -> Rc> { +fn type_of_builtin(b: Builtin) -> SubExpr { use dhall_core::Builtin::*; match b { Bool | Natural | Integer | Double | Text => dhall_expr!(Type), @@ -184,16 +183,15 @@ fn type_of_builtin(b: Builtin) -> Rc> { /// 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: Rc>, -) -> Result>, TypeError> + ctx: &Context>, + e: SubExpr, +) -> Result, TypeError> where S: ::std::fmt::Debug, { use dhall_core::BinOp::*; use dhall_core::Builtin::*; use dhall_core::Const::*; - use dhall_core::Expr; use dhall_core::Expr::*; let mkerr = |msg: TypeMessage<_>| TypeError::new(ctx, e.clone(), msg); let ensure_const = |x: &SubExpr<_, _>, msg: TypeMessage<_>| match x.as_ref() @@ -266,7 +264,7 @@ where return Err(mkerr(NotAFunction(f.clone(), tf))); } }; - let tA = normalize(Rc::clone(tA)); + let tA = normalize(SubExpr::clone(tA)); let tA2 = normalized_type_with(ctx, a.clone())?; if prop_equal(tA.as_ref(), tA2.as_ref()) { let vx0 = &V(x.clone(), 0); @@ -280,9 +278,9 @@ where } Let(f, mt, r, b) => { let r = if let Some(t) = mt { - rc(Annot(Rc::clone(r), Rc::clone(t))) + rc(Annot(SubExpr::clone(r), SubExpr::clone(t))) } else { - Rc::clone(r) + SubExpr::clone(r) }; let tR = type_with(ctx, r)?; @@ -351,7 +349,7 @@ where EmptyListLit(t) => { let s = normalized_type_with(ctx, t.clone())?; ensure_is_type(s, InvalidListType(t.clone()))?; - let t = normalize(Rc::clone(t)); + let t = normalize(SubExpr::clone(t)); return Ok(dhall_expr!(List t)); } NEListLit(xs) => { @@ -377,7 +375,7 @@ where return Ok(dhall_expr!(Optional t)); } NEOptionalLit(x) => { - let t: Rc> = type_with(ctx, x.clone())?; + let t: SubExpr<_, _> = type_with(ctx, x.clone())?; let s = normalized_type_with(ctx, t.clone())?; ensure_is_type(s, InvalidOptionalType(t.clone()))?; let t = normalize(t); @@ -451,9 +449,9 @@ where } pub fn normalized_type_with( - ctx: &Context>>, - e: Rc>, -) -> Result>, TypeError> + ctx: &Context>, + e: SubExpr, +) -> Result, TypeError> where S: ::std::fmt::Debug, { @@ -464,8 +462,8 @@ where /// expression must be closed (i.e. no free variables), otherwise type-checking /// will fail. pub fn type_of( - e: Rc>, -) -> Result>, TypeError> { + e: SubExpr, +) -> Result, TypeError> { let ctx = Context::new(); type_with(&ctx, e) //.map(|e| e.into_owned()) } @@ -474,63 +472,58 @@ pub fn type_of( #[derive(Debug)] pub enum TypeMessage { UnboundVariable, - InvalidInputType(Rc>), - InvalidOutputType(Rc>), - NotAFunction(Rc>, Rc>), - TypeMismatch( - Rc>, - Rc>, - Rc>, - Rc>, - ), - AnnotMismatch(Rc>, Rc>, Rc>), + InvalidInputType(SubExpr), + InvalidOutputType(SubExpr), + NotAFunction(SubExpr, SubExpr), + TypeMismatch(SubExpr, SubExpr, SubExpr, SubExpr), + AnnotMismatch(SubExpr, SubExpr, SubExpr), Untyped, - InvalidListElement(usize, Rc>, Rc>, Rc>), - InvalidListType(Rc>), - InvalidOptionalElement(Rc>, Rc>, Rc>), + InvalidListElement(usize, SubExpr, SubExpr, SubExpr), + InvalidListType(SubExpr), + InvalidOptionalElement(SubExpr, SubExpr, SubExpr), InvalidOptionalLiteral(usize), - InvalidOptionalType(Rc>), - InvalidPredicate(Rc>, Rc>), + InvalidOptionalType(SubExpr), + InvalidPredicate(SubExpr, SubExpr), IfBranchMismatch( - Rc>, - Rc>, - Rc>, - Rc>, + SubExpr, + SubExpr, + SubExpr, + SubExpr, ), - IfBranchMustBeTerm(bool, Rc>, Rc>, Rc>), - InvalidField(Label, Rc>), - InvalidFieldType(Label, Rc>), - InvalidAlternative(Label, Rc>), - InvalidAlternativeType(Label, Rc>), + IfBranchMustBeTerm(bool, SubExpr, SubExpr, SubExpr), + InvalidField(Label, SubExpr), + InvalidFieldType(Label, SubExpr), + InvalidAlternative(Label, SubExpr), + InvalidAlternativeType(Label, SubExpr), DuplicateAlternative(Label), - MustCombineARecord(Rc>, Rc>), + MustCombineARecord(SubExpr, SubExpr), FieldCollision(Label), - MustMergeARecord(Rc>, Rc>), - MustMergeUnion(Rc>, Rc>), + MustMergeARecord(SubExpr, SubExpr), + MustMergeUnion(SubExpr, SubExpr), UnusedHandler(HashSet