From 84796fd247eb1a13fcd092a7cd7ec2d587b261bd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 8 Mar 2020 17:11:34 +0000 Subject: Add SimpleValue type to facilitate deserialization --- dhall/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 24e4377..0be6db3 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -9,6 +9,7 @@ mod tests; pub mod error; pub mod semantics; +pub mod simple; pub mod syntax; use std::fmt::Display; @@ -29,6 +30,7 @@ pub type ParsedExpr = Expr; pub type DecodedExpr = Expr; pub type ResolvedExpr = Expr; pub type NormalizedExpr = Expr; +pub use crate::simple::{SValKind, SimpleValue}; #[derive(Debug, Clone)] pub struct Parsed(ParsedExpr, ImportLocation); @@ -53,7 +55,7 @@ pub struct Typed { pub struct Normalized(Nir); /// Controls conversion from `Nir` to `Expr` -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] pub(crate) struct ToExprOptions { /// Whether to convert all variables to `_` pub(crate) alpha: bool, @@ -138,7 +140,11 @@ impl Normalized { /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> NormalizedExpr { - self.0.to_expr(ToExprOptions { alpha: false }) + self.0.to_expr(ToExprOptions::default()) + } + /// Converts a value into a SimpleValue. + pub fn to_simple_value(&self) -> Result { + self.0.to_simple_value().ok_or_else(|| self.to_expr()) } /// Converts a value back to the corresponding Hir expression. pub(crate) fn to_hir(&self) -> Hir { -- cgit v1.2.3 From 94850b720b0171444694452027f1baf947b3c18f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 8 Mar 2020 21:47:26 +0000 Subject: Add SimpleType to mirror SimpleValue --- dhall/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 0be6db3..25196ba 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -30,7 +30,7 @@ pub type ParsedExpr = Expr; pub type DecodedExpr = Expr; pub type ResolvedExpr = Expr; pub type NormalizedExpr = Expr; -pub use crate::simple::{SValKind, SimpleValue}; +pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue}; #[derive(Debug, Clone)] pub struct Parsed(ParsedExpr, ImportLocation); @@ -146,6 +146,10 @@ impl Normalized { pub fn to_simple_value(&self) -> Result { self.0.to_simple_value().ok_or_else(|| self.to_expr()) } + /// Converts a value into a SimpleType. + pub fn to_simple_type(&self) -> Result { + self.0.to_simple_type().ok_or_else(|| self.to_expr()) + } /// Converts a value back to the corresponding Hir expression. pub(crate) fn to_hir(&self) -> Hir { self.0.to_hir_noenv() -- cgit v1.2.3 From 60425d58151fef142b066d523dc4d5e832070b9c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 14:30:15 +0000 Subject: Add new Value type in API --- dhall/src/lib.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 25196ba..60b7a0e 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -49,11 +49,18 @@ pub struct Typed { } /// A normalized expression. -/// -/// Invariant: the contained expression must be in normal form, #[derive(Debug, Clone)] pub struct Normalized(Nir); +/// A Dhall value. +#[derive(Debug, Clone)] +pub struct Value { + /// Invariant: in normal form + pub(crate) hir: Hir, + simple_val: Option, + simple_ty: Option, +} + /// Controls conversion from `Nir` to `Expr` #[derive(Copy, Clone, Default)] pub(crate) struct ToExprOptions { @@ -99,8 +106,8 @@ impl Resolved { pub fn typecheck(&self) -> Result { Ok(Typed::from_tir(typecheck(&self.0)?)) } - pub fn typecheck_with(self, ty: &Normalized) -> Result { - Ok(Typed::from_tir(typecheck_with(&self.0, ty.to_hir())?)) + pub(crate) fn typecheck_with(self, ty: &Hir) -> Result { + Ok(Typed::from_tir(typecheck_with(&self.0, ty)?)) } /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> ResolvedExpr { @@ -137,6 +144,13 @@ impl Normalized { pub fn encode(&self) -> Result, EncodeError> { binary::encode(&self.to_expr()) } + pub fn to_value(&self) -> Value { + Value { + hir: self.to_hir(), + simple_val: self.0.to_simple_value(), + simple_ty: self.0.to_simple_type(), + } + } /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> NormalizedExpr { @@ -200,6 +214,36 @@ impl Normalized { } } +impl Value { + /// Parse a string into a Value, and optionally ensure that the value matches the provided type + /// annotation. + pub fn from_str_with_annot( + s: &str, + ty: Option<&Self>, + ) -> Result { + let resolved = Parsed::parse_str(s)?.resolve()?; + let typed = match ty { + None => resolved.typecheck()?, + Some(ty) => resolved.typecheck_with(&ty.hir)?, + }; + Ok(typed.normalize().to_value()) + } + + /// Converts a Value into a SimpleValue. + pub fn to_simple_value(&self) -> Option { + self.simple_val.clone() + } + /// Converts a Value into a SimpleType. + pub fn to_simple_type(&self) -> Option { + self.simple_ty.clone() + } + + /// Converts a value back to the corresponding AST expression. + pub(crate) fn to_expr(&self) -> NormalizedExpr { + self.hir.to_expr(ToExprOptions::default()) + } +} + macro_rules! derive_traits_for_wrapper_struct { ($ty:ident) => { impl std::cmp::PartialEq for $ty { @@ -274,3 +318,15 @@ impl Display for Normalized { self.to_expr().fmt(f) } } + +impl Eq for Value {} +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + self.hir == other.hir + } +} +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.to_expr().fmt(f) + } +} -- cgit v1.2.3 From 6fdae6f8026fe9357de63c5557676f4def3eaf92 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 16:16:56 +0000 Subject: Remove Normalized and friends from public API --- dhall/src/lib.rs | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 60b7a0e..7300d88 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -26,31 +26,31 @@ use crate::semantics::{ use crate::syntax::binary; use crate::syntax::{Builtin, Expr}; -pub type ParsedExpr = Expr; -pub type DecodedExpr = Expr; -pub type ResolvedExpr = Expr; -pub type NormalizedExpr = Expr; +pub(crate) type ParsedExpr = Expr; +pub(crate) type DecodedExpr = Expr; +pub(crate) type ResolvedExpr = Expr; +pub(crate) type NormalizedExpr = Expr; pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue}; #[derive(Debug, Clone)] -pub struct Parsed(ParsedExpr, ImportLocation); +pub(crate) struct Parsed(ParsedExpr, ImportLocation); /// An expression where all imports have been resolved /// /// Invariant: there must be no `Import` nodes or `ImportAlt` operations left. #[derive(Debug, Clone)] -pub struct Resolved(Hir); +pub(crate) struct Resolved(Hir); /// A typed expression #[derive(Debug, Clone)] -pub struct Typed { +pub(crate) struct Typed { hir: Hir, ty: Type, } /// A normalized expression. #[derive(Debug, Clone)] -pub struct Normalized(Nir); +pub(crate) struct Normalized(Nir); /// A Dhall value. #[derive(Debug, Clone)] @@ -81,6 +81,7 @@ impl Parsed { pub fn parse_binary_file(f: &Path) -> Result { parse::parse_binary_file(f) } + #[allow(dead_code)] pub fn parse_binary(data: &[u8]) -> Result { parse::parse_binary(data) } @@ -88,13 +89,6 @@ impl Parsed { pub fn resolve(self) -> Result { resolve::resolve(self) } - pub fn skip_resolve(self) -> Result { - Ok(Resolved(resolve::skip_resolve(&self.0)?)) - } - - pub fn encode(&self) -> Result, EncodeError> { - binary::encode(&self.0) - } /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> ParsedExpr { @@ -156,14 +150,6 @@ impl Normalized { pub fn to_expr(&self) -> NormalizedExpr { self.0.to_expr(ToExprOptions::default()) } - /// Converts a value into a SimpleValue. - pub fn to_simple_value(&self) -> Result { - self.0.to_simple_value().ok_or_else(|| self.to_expr()) - } - /// Converts a value into a SimpleType. - pub fn to_simple_type(&self) -> Result { - self.0.to_simple_type().ok_or_else(|| self.to_expr()) - } /// Converts a value back to the corresponding Hir expression. pub(crate) fn to_hir(&self) -> Hir { self.0.to_hir_noenv() @@ -186,25 +172,25 @@ impl Normalized { Normalized(th) } - pub fn make_builtin_type(b: Builtin) -> Self { + pub(crate) fn make_builtin_type(b: Builtin) -> Self { Normalized::from_nir(Nir::from_builtin(b)) } - pub fn make_optional_type(t: Normalized) -> Self { + pub(crate) fn make_optional_type(t: Normalized) -> Self { Normalized::from_nir( Nir::from_builtin(Builtin::Optional).app(t.to_nir()), ) } - pub fn make_list_type(t: Normalized) -> Self { + pub(crate) fn make_list_type(t: Normalized) -> Self { Normalized::from_nir(Nir::from_builtin(Builtin::List).app(t.to_nir())) } - pub fn make_record_type( + pub(crate) fn make_record_type( kts: impl Iterator, ) -> Self { Normalized::from_kind(NirKind::RecordType( kts.map(|(k, t)| (k.into(), t.into_nir())).collect(), )) } - pub fn make_union_type( + pub(crate) fn make_union_type( kts: impl Iterator)>, ) -> Self { Normalized::from_kind(NirKind::UnionType( -- cgit v1.2.3 From f175ea7860cc1dd614b17912140ab7f5400c6bff Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 16:39:45 +0000 Subject: Limit dependency on Normalized --- dhall/src/lib.rs | 76 ++++++++------------------------------------------------ 1 file changed, 10 insertions(+), 66 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 7300d88..0e2ec82 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -16,15 +16,12 @@ use std::fmt::Display; use std::path::Path; use url::Url; -use crate::error::{EncodeError, Error, TypeError}; +use crate::error::{Error, TypeError}; use crate::semantics::parse; use crate::semantics::resolve; use crate::semantics::resolve::ImportLocation; -use crate::semantics::{ - typecheck, typecheck_with, Hir, Nir, NirKind, Tir, Type, -}; -use crate::syntax::binary; -use crate::syntax::{Builtin, Expr}; +use crate::semantics::{typecheck, typecheck_with, Hir, Nir, Tir, Type}; +use crate::syntax::Expr; pub(crate) type ParsedExpr = Expr; pub(crate) type DecodedExpr = Expr; @@ -57,8 +54,9 @@ pub(crate) struct Normalized(Nir); pub struct Value { /// Invariant: in normal form pub(crate) hir: Hir, - simple_val: Option, - simple_ty: Option, + /// Cached conversions because they are annoying to construct from Hir. + pub(crate) as_simple_val: Option, + pub(crate) as_simple_ty: Option, } /// Controls conversion from `Nir` to `Expr` @@ -135,14 +133,11 @@ impl Typed { } impl Normalized { - pub fn encode(&self) -> Result, EncodeError> { - binary::encode(&self.to_expr()) - } pub fn to_value(&self) -> Value { Value { hir: self.to_hir(), - simple_val: self.0.to_simple_value(), - simple_ty: self.0.to_simple_type(), + as_simple_val: self.0.to_simple_value(), + as_simple_ty: self.0.to_simple_type(), } } @@ -158,46 +153,6 @@ impl Normalized { pub(crate) fn to_expr_alpha(&self) -> NormalizedExpr { self.0.to_expr(ToExprOptions { alpha: true }) } - pub(crate) fn to_nir(&self) -> Nir { - self.0.clone() - } - pub(crate) fn into_nir(self) -> Nir { - self.0 - } - - pub(crate) fn from_kind(v: NirKind) -> Self { - Normalized(Nir::from_kind(v)) - } - pub(crate) fn from_nir(th: Nir) -> Self { - Normalized(th) - } - - pub(crate) fn make_builtin_type(b: Builtin) -> Self { - Normalized::from_nir(Nir::from_builtin(b)) - } - pub(crate) fn make_optional_type(t: Normalized) -> Self { - Normalized::from_nir( - Nir::from_builtin(Builtin::Optional).app(t.to_nir()), - ) - } - pub(crate) fn make_list_type(t: Normalized) -> Self { - Normalized::from_nir(Nir::from_builtin(Builtin::List).app(t.to_nir())) - } - pub(crate) fn make_record_type( - kts: impl Iterator, - ) -> Self { - Normalized::from_kind(NirKind::RecordType( - kts.map(|(k, t)| (k.into(), t.into_nir())).collect(), - )) - } - pub(crate) fn make_union_type( - kts: impl Iterator)>, - ) -> Self { - Normalized::from_kind(NirKind::UnionType( - kts.map(|(k, t)| (k.into(), t.map(|t| t.into_nir()))) - .collect(), - )) - } } impl Value { @@ -217,11 +172,11 @@ impl Value { /// Converts a Value into a SimpleValue. pub fn to_simple_value(&self) -> Option { - self.simple_val.clone() + self.as_simple_val.clone() } /// Converts a Value into a SimpleType. pub fn to_simple_type(&self) -> Option { - self.simple_ty.clone() + self.as_simple_ty.clone() } /// Converts a value back to the corresponding AST expression. @@ -253,17 +208,6 @@ macro_rules! derive_traits_for_wrapper_struct { derive_traits_for_wrapper_struct!(Parsed); -impl std::hash::Hash for Normalized { - fn hash(&self, state: &mut H) - where - H: std::hash::Hasher, - { - if let Ok(vec) = self.encode() { - vec.hash(state) - } - } -} - impl From for NormalizedExpr { fn from(other: Parsed) -> Self { other.to_expr() -- cgit v1.2.3 From 35db1b4b54dba8b0cafe661329e0099dfabd8073 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 17:04:47 +0000 Subject: Remove top-level Expr aliases --- dhall/src/lib.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 0e2ec82..799d86f 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -23,14 +23,10 @@ use crate::semantics::resolve::ImportLocation; use crate::semantics::{typecheck, typecheck_with, Hir, Nir, Tir, Type}; use crate::syntax::Expr; -pub(crate) type ParsedExpr = Expr; -pub(crate) type DecodedExpr = Expr; -pub(crate) type ResolvedExpr = Expr; -pub(crate) type NormalizedExpr = Expr; pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue}; #[derive(Debug, Clone)] -pub(crate) struct Parsed(ParsedExpr, ImportLocation); +pub(crate) struct Parsed(Expr, ImportLocation); /// An expression where all imports have been resolved /// @@ -89,7 +85,7 @@ impl Parsed { } /// Converts a value back to the corresponding AST expression. - pub fn to_expr(&self) -> ParsedExpr { + pub fn to_expr(&self) -> Expr { self.0.clone() } } @@ -102,7 +98,7 @@ impl Resolved { Ok(Typed::from_tir(typecheck_with(&self.0, ty)?)) } /// Converts a value back to the corresponding AST expression. - pub fn to_expr(&self) -> ResolvedExpr { + pub fn to_expr(&self) -> Expr { self.0.to_expr_noopts() } } @@ -120,7 +116,7 @@ impl Typed { } /// Converts a value back to the corresponding AST expression. - fn to_expr(&self) -> ResolvedExpr { + fn to_expr(&self) -> Expr { self.hir.to_expr(ToExprOptions { alpha: false }) } @@ -142,7 +138,7 @@ impl Normalized { } /// Converts a value back to the corresponding AST expression. - pub fn to_expr(&self) -> NormalizedExpr { + pub fn to_expr(&self) -> Expr { self.0.to_expr(ToExprOptions::default()) } /// Converts a value back to the corresponding Hir expression. @@ -150,7 +146,7 @@ impl Normalized { self.0.to_hir_noenv() } /// Converts a value back to the corresponding AST expression, alpha-normalizing in the process. - pub(crate) fn to_expr_alpha(&self) -> NormalizedExpr { + pub(crate) fn to_expr_alpha(&self) -> Expr { self.0.to_expr(ToExprOptions { alpha: true }) } } @@ -180,7 +176,7 @@ impl Value { } /// Converts a value back to the corresponding AST expression. - pub(crate) fn to_expr(&self) -> NormalizedExpr { + pub(crate) fn to_expr(&self) -> Expr { self.hir.to_expr(ToExprOptions::default()) } } @@ -208,12 +204,12 @@ macro_rules! derive_traits_for_wrapper_struct { derive_traits_for_wrapper_struct!(Parsed); -impl From for NormalizedExpr { +impl From for Expr { fn from(other: Parsed) -> Self { other.to_expr() } } -impl From for NormalizedExpr { +impl From for Expr { fn from(other: Normalized) -> Self { other.to_expr() } -- cgit v1.2.3 From 6a2a407f27b8068769f208383119ac855f629e96 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 20:35:42 +0000 Subject: Move conversion to SimpleVal/Ty to simple module --- dhall/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 799d86f..07df2b2 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -132,8 +132,8 @@ impl Normalized { pub fn to_value(&self) -> Value { Value { hir: self.to_hir(), - as_simple_val: self.0.to_simple_value(), - as_simple_ty: self.0.to_simple_type(), + as_simple_val: SimpleValue::from_nir(&self.0), + as_simple_ty: SimpleType::from_nir(&self.0), } } -- cgit v1.2.3 From f4a1a6b8d4de5f83649b2ddd90d65f6e6d76e5e1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 21:29:08 +0000 Subject: Nir::normalize isn't useful It pretends to normalize but actually can't normalize under lambdas. The correct way to normalize a Nir is to convert it to Hir. --- dhall/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 07df2b2..caaf260 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -42,6 +42,8 @@ pub(crate) struct Typed { } /// A normalized expression. +/// +/// This is actually a lie, because the expression will only get normalized on demand. #[derive(Debug, Clone)] pub(crate) struct Normalized(Nir); @@ -112,7 +114,7 @@ impl Typed { } /// Reduce an expression to its normal form, performing beta reduction pub fn normalize(&self) -> Normalized { - Normalized(self.hir.rec_eval_closed_expr()) + Normalized(self.hir.eval_closed_expr()) } /// Converts a value back to the corresponding AST expression. -- cgit v1.2.3 From 7848c8e8d3147ebe428290558aa6c0efcbf59ee5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 18 Mar 2020 18:58:51 +0000 Subject: Brutally make all of dhall pub --- dhall/src/lib.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index caaf260..34a9bac 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -26,17 +26,17 @@ use crate::syntax::Expr; pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue}; #[derive(Debug, Clone)] -pub(crate) struct Parsed(Expr, ImportLocation); +pub struct Parsed(Expr, ImportLocation); /// An expression where all imports have been resolved /// /// Invariant: there must be no `Import` nodes or `ImportAlt` operations left. #[derive(Debug, Clone)] -pub(crate) struct Resolved(Hir); +pub struct Resolved(Hir); /// A typed expression #[derive(Debug, Clone)] -pub(crate) struct Typed { +pub struct Typed { hir: Hir, ty: Type, } @@ -45,23 +45,23 @@ pub(crate) struct Typed { /// /// This is actually a lie, because the expression will only get normalized on demand. #[derive(Debug, Clone)] -pub(crate) struct Normalized(Nir); +pub struct Normalized(Nir); /// A Dhall value. #[derive(Debug, Clone)] pub struct Value { /// Invariant: in normal form - pub(crate) hir: Hir, + pub hir: Hir, /// Cached conversions because they are annoying to construct from Hir. - pub(crate) as_simple_val: Option, - pub(crate) as_simple_ty: Option, + pub as_simple_val: Option, + pub as_simple_ty: Option, } /// Controls conversion from `Nir` to `Expr` #[derive(Copy, Clone, Default)] -pub(crate) struct ToExprOptions { +pub struct ToExprOptions { /// Whether to convert all variables to `_` - pub(crate) alpha: bool, + pub alpha: bool, } impl Parsed { @@ -96,7 +96,7 @@ impl Resolved { pub fn typecheck(&self) -> Result { Ok(Typed::from_tir(typecheck(&self.0)?)) } - pub(crate) fn typecheck_with(self, ty: &Hir) -> Result { + pub fn typecheck_with(self, ty: &Hir) -> Result { Ok(Typed::from_tir(typecheck_with(&self.0, ty)?)) } /// Converts a value back to the corresponding AST expression. @@ -122,10 +122,10 @@ impl Typed { self.hir.to_expr(ToExprOptions { alpha: false }) } - pub(crate) fn ty(&self) -> &Type { + pub fn ty(&self) -> &Type { &self.ty } - pub(crate) fn get_type(&self) -> Result { + pub fn get_type(&self) -> Result { Ok(Normalized(self.ty.clone().into_nir())) } } @@ -144,11 +144,11 @@ impl Normalized { self.0.to_expr(ToExprOptions::default()) } /// Converts a value back to the corresponding Hir expression. - pub(crate) fn to_hir(&self) -> Hir { + pub fn to_hir(&self) -> Hir { self.0.to_hir_noenv() } /// Converts a value back to the corresponding AST expression, alpha-normalizing in the process. - pub(crate) fn to_expr_alpha(&self) -> Expr { + pub fn to_expr_alpha(&self) -> Expr { self.0.to_expr(ToExprOptions { alpha: true }) } } @@ -178,7 +178,7 @@ impl Value { } /// Converts a value back to the corresponding AST expression. - pub(crate) fn to_expr(&self) -> Expr { + pub fn to_expr(&self) -> Expr { self.hir.to_expr(ToExprOptions::default()) } } -- cgit v1.2.3 From fa89e9cb319b353332c9e835944e7f86a6604c42 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 18 Mar 2020 21:37:14 +0000 Subject: Move Value, SimpleValue and SimpleType to serde --- dhall/src/lib.rs | 66 +++----------------------------------------------------- 1 file changed, 3 insertions(+), 63 deletions(-) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 34a9bac..0f4b623 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -9,7 +9,6 @@ mod tests; pub mod error; pub mod semantics; -pub mod simple; pub mod syntax; use std::fmt::Display; @@ -23,8 +22,6 @@ use crate::semantics::resolve::ImportLocation; use crate::semantics::{typecheck, typecheck_with, Hir, Nir, Tir, Type}; use crate::syntax::Expr; -pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue}; - #[derive(Debug, Clone)] pub struct Parsed(Expr, ImportLocation); @@ -47,16 +44,6 @@ pub struct Typed { #[derive(Debug, Clone)] pub struct Normalized(Nir); -/// A Dhall value. -#[derive(Debug, Clone)] -pub struct Value { - /// Invariant: in normal form - pub hir: Hir, - /// Cached conversions because they are annoying to construct from Hir. - pub as_simple_val: Option, - pub as_simple_ty: Option, -} - /// Controls conversion from `Nir` to `Expr` #[derive(Copy, Clone, Default)] pub struct ToExprOptions { @@ -131,14 +118,6 @@ impl Typed { } impl Normalized { - pub fn to_value(&self) -> Value { - Value { - hir: self.to_hir(), - as_simple_val: SimpleValue::from_nir(&self.0), - as_simple_ty: SimpleType::from_nir(&self.0), - } - } - /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> Expr { self.0.to_expr(ToExprOptions::default()) @@ -147,42 +126,15 @@ impl Normalized { pub fn to_hir(&self) -> Hir { self.0.to_hir_noenv() } + pub fn as_nir(&self) -> &Nir { + &self.0 + } /// Converts a value back to the corresponding AST expression, alpha-normalizing in the process. pub fn to_expr_alpha(&self) -> Expr { self.0.to_expr(ToExprOptions { alpha: true }) } } -impl Value { - /// Parse a string into a Value, and optionally ensure that the value matches the provided type - /// annotation. - pub fn from_str_with_annot( - s: &str, - ty: Option<&Self>, - ) -> Result { - let resolved = Parsed::parse_str(s)?.resolve()?; - let typed = match ty { - None => resolved.typecheck()?, - Some(ty) => resolved.typecheck_with(&ty.hir)?, - }; - Ok(typed.normalize().to_value()) - } - - /// Converts a Value into a SimpleValue. - pub fn to_simple_value(&self) -> Option { - self.as_simple_val.clone() - } - /// Converts a Value into a SimpleType. - pub fn to_simple_type(&self) -> Option { - self.as_simple_ty.clone() - } - - /// Converts a value back to the corresponding AST expression. - pub fn to_expr(&self) -> Expr { - self.hir.to_expr(ToExprOptions::default()) - } -} - macro_rules! derive_traits_for_wrapper_struct { ($ty:ident) => { impl std::cmp::PartialEq for $ty { @@ -246,15 +198,3 @@ impl Display for Normalized { self.to_expr().fmt(f) } } - -impl Eq for Value {} -impl PartialEq for Value { - fn eq(&self, other: &Self) -> bool { - self.hir == other.hir - } -} -impl Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - self.to_expr().fmt(f) - } -} -- cgit v1.2.3 From 85e2e8ee5e83dadd05b6974ba6c951350cb97a61 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 21 Mar 2020 21:30:30 +0000 Subject: Introduce option builder --- dhall/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 0f4b623..adca2de 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -72,6 +72,9 @@ impl Parsed { pub fn resolve(self) -> Result { resolve::resolve(self) } + pub fn skip_resolve(self) -> Result { + resolve::skip_resolve(self) + } /// Converts a value back to the corresponding AST expression. pub fn to_expr(&self) -> Expr { -- cgit v1.2.3 From 820214615547101f8f2b5de209b5189968bddfee Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 5 Apr 2020 17:37:15 +0100 Subject: Fix clippy warnings --- dhall/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index adca2de..392c344 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -1,7 +1,10 @@ #![doc(html_root_url = "https://docs.rs/dhall/0.4.0")] #![allow( + clippy::implicit_hasher, clippy::module_inception, clippy::needless_lifetimes, + clippy::new_ret_no_self, + clippy::new_without_default, clippy::useless_format )] -- cgit v1.2.3