From 366bc783e62682c9597e8caba1dac56638d34fa9 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 18:24:49 +0200 Subject: Define some newtypes for Expr Closes #50 --- dhall/src/expr.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 dhall/src/expr.rs (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs new file mode 100644 index 0000000..0d093cb --- /dev/null +++ b/dhall/src/expr.rs @@ -0,0 +1,35 @@ +use crate::typecheck::TypeError; +use dhall_core::*; + +pub struct Parsed(SubExpr); +pub struct Resolved(SubExpr); +pub struct Typed(SubExpr, Type); +pub struct Type(Box); +pub struct Normalized(SubExpr); + +// impl Parsed { +// pub fn resolve(self) -> Result { +// Ok(Resolved(crate::imports::resolve(self.0)?)) +// } +// } +impl Resolved { + pub fn typecheck(self) -> Result> { + let typ = Type(Box::new(Normalized(crate::typecheck::type_of( + self.0.clone(), + )?))); + Ok(Typed(self.0, typ)) + } +} +impl Typed { + pub fn normalize(self) -> Normalized { + Normalized(crate::normalize::normalize(self.0)) + } + pub fn get_type(&self) -> &Type { + &self.1 + } +} +impl Type { + pub fn as_expr(&self) -> &Normalized { + &*self.0 + } +} -- cgit v1.2.3 From 396ec334bac1e8d10a2d2b2d683c93e3b2ff4d8d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:04:04 +0200 Subject: Massage import loading into new API Closes #9 --- dhall/src/expr.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 0d093cb..72633ea 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,17 +1,27 @@ +use crate::imports::ImportError; +use crate::imports::ImportRoot; use crate::typecheck::TypeError; use dhall_core::*; -pub struct Parsed(SubExpr); -pub struct Resolved(SubExpr); -pub struct Typed(SubExpr, Type); -pub struct Type(Box); -pub struct Normalized(SubExpr); +#[derive(Debug, Clone)] +pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); +#[derive(Debug, Clone)] +pub struct Resolved(pub(crate) SubExpr); +#[derive(Debug, Clone)] +pub struct Typed(pub(crate) SubExpr, Type); +#[derive(Debug, Clone)] +pub struct Type(pub(crate) Box); +#[derive(Debug, Clone)] +pub struct Normalized(pub(crate) SubExpr); -// impl Parsed { -// pub fn resolve(self) -> Result { -// Ok(Resolved(crate::imports::resolve(self.0)?)) -// } -// } +impl Parsed { + pub fn resolve(self) -> Result { + crate::imports::resolve_expr(self, true) + } + pub fn resolve_no_imports(self) -> Result { + crate::imports::resolve_expr(self, false) + } +} impl Resolved { pub fn typecheck(self) -> Result> { let typ = Type(Box::new(Normalized(crate::typecheck::type_of( -- cgit v1.2.3 From 412d0fac51b7b51aabcb049e3d6ba52f3dda1529 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:32:25 +0200 Subject: Move binary decoding to new API --- dhall/src/expr.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 72633ea..ae52e4d 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,27 +1,33 @@ -use crate::imports::ImportError; use crate::imports::ImportRoot; use crate::typecheck::TypeError; use dhall_core::*; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); -#[derive(Debug, Clone)] + +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Resolved(pub(crate) SubExpr); + #[derive(Debug, Clone)] pub struct Typed(pub(crate) SubExpr, Type); + #[derive(Debug, Clone)] pub struct Type(pub(crate) Box); + #[derive(Debug, Clone)] pub struct Normalized(pub(crate) SubExpr); -impl Parsed { - pub fn resolve(self) -> Result { - crate::imports::resolve_expr(self, true) +impl PartialEq for Parsed { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 } - pub fn resolve_no_imports(self) -> Result { - crate::imports::resolve_expr(self, false) +} +impl std::fmt::Display for Parsed { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.0.fmt(f) } } + impl Resolved { pub fn typecheck(self) -> Result> { let typ = Type(Box::new(Normalized(crate::typecheck::type_of( -- cgit v1.2.3 From 7983c43210f5fcaa439fa1c6742e72252652e4f4 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:50:57 +0200 Subject: Thread Typed through type_with --- dhall/src/expr.rs | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index ae52e4d..cc7f064 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,5 +1,4 @@ use crate::imports::ImportRoot; -use crate::typecheck::TypeError; use dhall_core::*; #[derive(Debug, Clone, Eq)] @@ -9,10 +8,11 @@ pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); pub struct Resolved(pub(crate) SubExpr); #[derive(Debug, Clone)] -pub struct Typed(pub(crate) SubExpr, Type); +pub struct Typed(pub(crate) SubExpr, pub(crate) Type); -#[derive(Debug, Clone)] -pub struct Type(pub(crate) Box); +// #[derive(Debug, Clone)] +// pub struct Type(pub(crate) Box); +pub type Type = SubExpr; #[derive(Debug, Clone)] pub struct Normalized(pub(crate) SubExpr); @@ -28,24 +28,8 @@ impl std::fmt::Display for Parsed { } } -impl Resolved { - pub fn typecheck(self) -> Result> { - let typ = Type(Box::new(Normalized(crate::typecheck::type_of( - self.0.clone(), - )?))); - Ok(Typed(self.0, typ)) - } -} -impl Typed { - pub fn normalize(self) -> Normalized { - Normalized(crate::normalize::normalize(self.0)) - } - pub fn get_type(&self) -> &Type { - &self.1 - } -} -impl Type { - pub fn as_expr(&self) -> &Normalized { - &*self.0 - } -} +// impl Type { +// pub fn as_expr(&self) -> &Normalized { +// &*self.0 +// } +// } -- cgit v1.2.3 From c4438eb3d52b1a69c9022b12e8de135b8c9991c9 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 21:54:55 +0200 Subject: Start taking Typed seriously --- dhall/src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index cc7f064..831fbc5 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -15,7 +15,7 @@ pub struct Typed(pub(crate) SubExpr, pub(crate) Type); pub type Type = SubExpr; #[derive(Debug, Clone)] -pub struct Normalized(pub(crate) SubExpr); +pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); impl PartialEq for Parsed { fn eq(&self, other: &Self) -> bool { -- cgit v1.2.3 From b0eb080caab28196a09c5b60c5b6556846732563 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 23:07:30 +0200 Subject: Store the whole type hierarchy in a Type --- dhall/src/expr.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 831fbc5..ad35645 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -10,9 +10,14 @@ pub struct Resolved(pub(crate) SubExpr); #[derive(Debug, Clone)] pub struct Typed(pub(crate) SubExpr, pub(crate) Type); -// #[derive(Debug, Clone)] -// pub struct Type(pub(crate) Box); -pub type Type = SubExpr; +#[derive(Debug, Clone)] +pub struct Type(pub(crate) TypeInternal); + +#[derive(Debug, Clone)] +pub(crate) enum TypeInternal { + Expr(Box), + Universe(usize), +} #[derive(Debug, Clone)] pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); @@ -27,9 +32,3 @@ impl std::fmt::Display for Parsed { self.0.fmt(f) } } - -// impl Type { -// pub fn as_expr(&self) -> &Normalized { -// &*self.0 -// } -// } -- cgit v1.2.3 From da9937f623f2698adec50718e1e703958e837c85 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 01:08:02 +0200 Subject: Put a Cow in Type --- dhall/src/expr.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index ad35645..75d7690 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -8,10 +8,10 @@ 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); +pub struct Typed(pub(crate) SubExpr, pub(crate) Type<'static>); #[derive(Debug, Clone)] -pub struct Type(pub(crate) TypeInternal); +pub struct Type<'a>(pub(crate) std::borrow::Cow<'a, TypeInternal>); #[derive(Debug, Clone)] pub(crate) enum TypeInternal { @@ -20,7 +20,7 @@ pub(crate) enum TypeInternal { } #[derive(Debug, Clone)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); +pub struct Normalized(pub(crate) SubExpr, pub(crate) Type<'static>); impl PartialEq for Parsed { fn eq(&self, other: &Self) -> bool { -- cgit v1.2.3 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 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'dhall/src/expr.rs') 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 { -- cgit v1.2.3 From 4bebcd96b6e76b9b8ae7877af91d2ae571e617a9 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 16:45:30 +0200 Subject: Restrict public API Closes #20 --- dhall/src/expr.rs | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 555db2f..7baf628 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,34 +1,46 @@ use crate::imports::ImportRoot; use dhall_core::*; +macro_rules! derive_other_traits { + ($ty:ident) => { + impl std::cmp::PartialEq for $ty { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + impl std::fmt::Display for $ty { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> Result<(), std::fmt::Error> { + self.0.fmt(f) + } + } + }; +} + #[derive(Debug, Clone, Eq)] pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); +derive_other_traits!(Parsed); -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Eq)] pub struct Resolved(pub(crate) SubExpr); +derive_other_traits!(Resolved); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct Typed(pub(crate) SubExpr, pub(crate) Type); +derive_other_traits!(Typed); + +#[derive(Debug, Clone, Eq)] +pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); +derive_other_traits!(Normalized); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Type(pub(crate) TypeInternal); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum TypeInternal { Expr(Box), Untyped, } - -#[derive(Debug, Clone)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); - -impl PartialEq for Parsed { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} -impl std::fmt::Display for Parsed { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - self.0.fmt(f) - } -} -- cgit v1.2.3