From d454d31ea07b70ff6d3f8d4d1014d37d954241dd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 12:29:48 +0200 Subject: Universe hierarchy is overkill --- dhall/src/expr.rs | 8 +-- dhall/src/typecheck.rs | 132 ++++++++++++++++++++++--------------------------- 2 files changed, 63 insertions(+), 77 deletions(-) diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 75d7690..555db2f 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -8,19 +8,19 @@ pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); pub struct Resolved(pub(crate) SubExpr); #[derive(Debug, Clone)] -pub struct Typed(pub(crate) SubExpr, pub(crate) Type<'static>); +pub struct Typed(pub(crate) SubExpr, pub(crate) Type); #[derive(Debug, Clone)] -pub struct Type<'a>(pub(crate) std::borrow::Cow<'a, TypeInternal>); +pub struct Type(pub(crate) TypeInternal); #[derive(Debug, Clone)] pub(crate) enum TypeInternal { Expr(Box), - Universe(usize), + Untyped, } #[derive(Debug, Clone)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Type<'static>); +pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); impl PartialEq for Parsed { fn eq(&self, other: &Self) -> bool { diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index f380448..9eead93 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -6,7 +6,6 @@ use dhall_core; use dhall_core::context::Context; use dhall_core::*; use dhall_generator as dhall; -use std::borrow::Cow; use self::TypeMessage::*; @@ -26,8 +25,12 @@ impl Typed { self.0 } #[inline(always)] - pub fn get_type<'a>(&'a self) -> Type<'a> { - self.1.reborrow() + pub fn get_type(&self) -> &Type { + &self.1 + } + #[inline(always)] + fn get_type_move(self) -> Type { + self.1 } } impl Normalized { @@ -40,12 +43,12 @@ impl Normalized { self.0 } #[inline(always)] - pub fn get_type<'a>(&'a self) -> Type<'a> { - self.1.reborrow() + pub fn get_type(&self) -> &Type { + &self.1 } #[inline(always)] - fn into_type(self) -> Type<'static> { - crate::expr::Type(Cow::Owned(TypeInternal::Expr(Box::new(self)))) + fn into_type(self) -> Type { + crate::expr::Type(TypeInternal::Expr(Box::new(self))) } // Expose the outermost constructor #[inline(always)] @@ -58,24 +61,13 @@ impl Normalized { Normalized(shift(delta, var, &self.0), self.1.clone()) } } -impl<'a> Type<'a> { - #[inline(always)] - fn into_owned(self) -> Type<'static> { - Type(Cow::Owned(self.0.into_owned())) - } - #[inline(always)] - fn reborrow<'b>(&'b self) -> Type<'b> { - match &self.0 { - Cow::Owned(x) => crate::expr::Type(Cow::Borrowed(x)), - Cow::Borrowed(x) => crate::expr::Type(Cow::Borrowed(x)), - } - } +impl Type { #[inline(always)] - fn as_normalized(&'a self) -> Result<&'a Normalized, TypeError> { + fn as_normalized(&self) -> Result<&Normalized, TypeError> { use TypeInternal::*; - match self.0.as_ref() { + match &self.0 { Expr(e) => Ok(e), - Universe(_) => Err(TypeError::new( + Untyped => Err(TypeError::new( &Context::new(), rc(ExprF::Const(Const::Sort)), TypeMessage::Untyped, @@ -85,9 +77,9 @@ impl<'a> Type<'a> { #[inline(always)] fn into_normalized(self) -> Result> { use TypeInternal::*; - match self.0.into_owned() { + match self.0 { Expr(e) => Ok(*e), - Universe(_) => Err(TypeError::new( + Untyped => Err(TypeError::new( &Context::new(), rc(ExprF::Const(Const::Sort)), TypeMessage::Untyped, @@ -96,29 +88,28 @@ impl<'a> Type<'a> { } // Expose the outermost constructor #[inline(always)] - fn unroll_ref(&'a self) -> Result<&'a Expr, TypeError> { + fn unroll_ref(&self) -> Result<&Expr, TypeError> { Ok(self.as_normalized()?.unroll_ref()) } #[inline(always)] - pub fn get_type<'b>(&'b self) -> Type<'b> { + pub fn get_type(&self) -> &Type { use TypeInternal::*; - match self.0.as_ref() { + match &self.0 { Expr(e) => e.get_type(), - Universe(n) => crate::expr::Type(Cow::Owned(Universe(n + 1))), + Untyped => &UNTYPE, } } #[inline(always)] fn shift(&self, delta: isize, var: &V