From 162d2a2a6d241d525346e295b0cf2ad43387bed5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 23 Apr 2019 12:38:48 +0200 Subject: Pass TypedImproved directly to type_last_layer --- dhall/src/typecheck.rs | 95 +++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 40 deletions(-) (limited to 'dhall') diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 37c95c5..6fb7cac 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -176,6 +176,18 @@ impl TypedImproved { TypedImproved::Expr(e) => Ok(e), } } + fn normalize_to_type(self) -> Result, TypeError> { + Ok(self.into_typed()?.normalize_to_type()) + } + fn get_type(&self) -> Result>, TypeError> { + Ok(Cow::Owned(self.clone().get_type_move()?)) + } + fn get_type_move(self) -> Result, TypeError> { + Ok(self.into_typed()?.get_type_move()?) + } + fn normalize(self) -> Result, TypeError> { + Ok(self.into_typed()?.normalize()) + } } #[derive(Debug, Clone)] @@ -453,7 +465,7 @@ fn mktype( ctx: &TypecheckContext, e: SubExpr>, ) -> Result, TypeError> { - Ok(type_with(ctx, e)?.into_typed()?.normalize_to_type()) + Ok(type_with(ctx, e)?.normalize_to_type()?) } fn into_simple_type<'a>(e: SubExpr) -> Type<'a> { @@ -549,7 +561,7 @@ fn type_with( ctx, // Typecheck recursively all subexpressions e.as_ref().traverse_ref_simple(|e| { - Ok(type_with(ctx, e.clone())?.into_typed()?) + Ok(type_with(ctx, e.clone())?) })?, e.clone(), ), @@ -574,7 +586,7 @@ fn type_with( /// layer. fn type_last_layer( ctx: &TypecheckContext, - e: ExprF, Label, X, Normalized<'static>>, + e: ExprF>, original_e: SubExpr>, ) -> Result { use dhall_core::BinOp::*; @@ -609,12 +621,12 @@ fn type_last_layer( Ok(RetExpr(Let( x.clone(), None, - a.normalize().embed(), + a.normalize()?.embed(), tb.embed_absurd(), ))) } Annot(x, t) => { - let t = t.normalize_to_type(); + let t = t.normalize_to_type()?; ensure_equal!( &t, x.get_type()?, @@ -648,7 +660,7 @@ fn type_last_layer( Ok(RetType(y.get_type_move()?)) } EmptyListLit(t) => { - let t = t.normalize_to_type(); + let t = t.normalize_to_type()?; ensure_simple_type!( t, mkerr(InvalidListType(t.into_normalized()?)), @@ -679,15 +691,15 @@ fn type_last_layer( Ok(RetExpr(dhall::expr!(List t))) } OldOptionalLit(None, t) => { - let t = t.normalize().embed(); + let t = t.normalize()?.embed(); let e = dhall::subexpr!(None t); Ok(RetType( type_with(ctx, e)?.into_typed()?.get_type()?.into_owned(), )) } OldOptionalLit(Some(x), t) => { - let t = t.normalize().embed(); - let x = x.normalize().embed(); + let t = t.normalize()?.embed(); + let x = x.normalize()?.embed(); let e = dhall::subexpr!(Some x : Optional t); Ok(RetType( type_with(ctx, e)?.into_typed()?.get_type()?.into_owned(), @@ -759,7 +771,10 @@ fn type_last_layer( let mut kts: std::collections::BTreeMap<_, _> = kvs .into_iter() .map(|(x, v)| { - let t = v.map(|x| x.normalize().embed()); + let t = match v { + Some(x) => Some(x.normalize()?.embed()), + None => None, + }; Ok((x, t)) }) .collect::>()?; @@ -773,7 +788,7 @@ fn type_last_layer( None => Err(mkerr(MissingRecordField(x, r))), }, _ => { - let r = r.normalize_to_type(); + let r = r.normalize_to_type()?; match r.as_normalized()?.as_expr().as_ref() { UnionType(kts) => match kts.get(&x) { // Constructor has type T -> < x: T, ... > @@ -860,21 +875,21 @@ pub(crate) enum TypeMessage<'a> { UnboundVariable(V