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 ++++++++-- dhall/src/semantics/nze/nir.rs | 34 ++++++++++++++++++++++++++++++++++ dhall/src/simple.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 dhall/src/simple.rs (limited to 'dhall/src') 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 { diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 32ef590..9e3644c 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -11,6 +11,7 @@ use crate::syntax::{ Span, }; use crate::{NormalizedExpr, ToExprOptions}; +use crate::{SValKind, SimpleValue}; /// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation /// automatically. Uses a Rc to share computation. @@ -142,6 +143,39 @@ impl Nir { pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> NormalizedExpr { self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } + pub(crate) fn to_simple_value(&self) -> Option { + Some(SimpleValue::new(match self.kind() { + NirKind::Lit(lit) => SValKind::Lit(lit.clone()), + NirKind::TextLit(x) => SValKind::Text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), + NirKind::NEOptionalLit(x) => { + SValKind::Optional(Some(x.to_simple_value()?)) + } + NirKind::EmptyListLit(_) => SValKind::List(vec![]), + NirKind::NEListLit(xs) => SValKind::List( + xs.iter() + .map(|v| v.to_simple_value()) + .collect::>()?, + ), + NirKind::RecordLit(kvs) => SValKind::Record( + kvs.iter() + .map(|(k, v)| Some((k.into(), v.to_simple_value()?))) + .collect::>()?, + ), + NirKind::UnionLit(field, x, _) => { + SValKind::Union(field.into(), Some(x.to_simple_value()?)) + } + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + SValKind::Union(field.into(), None) + } + _ => return None, + })) + } pub(crate) fn normalize(&self) { self.0.normalize() diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs new file mode 100644 index 0000000..6457291 --- /dev/null +++ b/dhall/src/simple.rs @@ -0,0 +1,29 @@ +use std::collections::BTreeMap; + +use crate::syntax::LitKind; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SimpleValue { + kind: Box, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SValKind { + Lit(LitKind), + Optional(Option), + List(Vec), + Record(BTreeMap), + Union(String, Option), + Text(String), +} + +impl SimpleValue { + pub(crate) fn new(kind: SValKind) -> Self { + SimpleValue { + kind: Box::new(kind), + } + } + pub fn kind(&self) -> &SValKind { + self.kind.as_ref() + } +} -- 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 ++++- dhall/src/semantics/nze/nir.rs | 48 +++++++++++++++++++++++++++++++++++++- dhall/src/simple.rs | 52 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 3 deletions(-) (limited to 'dhall/src') 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() diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 9e3644c..4615b39 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -11,7 +11,7 @@ use crate::syntax::{ Span, }; use crate::{NormalizedExpr, ToExprOptions}; -use crate::{SValKind, SimpleValue}; +use crate::{STyKind, SValKind, SimpleType, SimpleValue}; /// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation /// automatically. Uses a Rc to share computation. @@ -176,6 +176,52 @@ impl Nir { _ => return None, })) } + pub(crate) fn to_simple_type(&self) -> Option { + Some(SimpleType::new(match self.kind() { + NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. }) + if args.is_empty() => + { + match b { + Builtin::Bool => STyKind::Bool, + Builtin::Natural => STyKind::Natural, + Builtin::Integer => STyKind::Integer, + Builtin::Double => STyKind::Double, + Builtin::Text => STyKind::Text, + _ => return None, + } + } + NirKind::AppliedBuiltin(BuiltinClosure { + b: Builtin::Optional, + args, + .. + }) if args.len() == 1 => { + STyKind::Optional(args[0].to_simple_type()?) + } + NirKind::AppliedBuiltin(BuiltinClosure { + b: Builtin::List, + args, + .. + }) if args.len() == 1 => STyKind::List(args[0].to_simple_type()?), + NirKind::RecordType(kts) => STyKind::Record( + kts.iter() + .map(|(k, v)| Some((k.into(), v.to_simple_type()?))) + .collect::>()?, + ), + NirKind::UnionType(kts) => STyKind::Union( + kts.iter() + .map(|(k, v)| { + Some(( + k.into(), + v.as_ref() + .map(|v| Ok(v.to_simple_type()?)) + .transpose()?, + )) + }) + .collect::>()?, + ), + _ => return None, + })) + } pub(crate) fn normalize(&self) { self.0.normalize() diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 6457291..9d3846e 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; -use crate::syntax::LitKind; +use crate::syntax::{Builtin, LitKind}; +use crate::Normalized; #[derive(Debug, Clone, PartialEq, Eq)] pub struct SimpleValue { @@ -17,6 +18,24 @@ pub enum SValKind { Text(String), } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SimpleType { + kind: Box, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum STyKind { + Bool, + Natural, + Integer, + Double, + Text, + Optional(SimpleType), + List(SimpleType), + Record(BTreeMap), + Union(BTreeMap>), +} + impl SimpleValue { pub(crate) fn new(kind: SValKind) -> Self { SimpleValue { @@ -27,3 +46,34 @@ impl SimpleValue { self.kind.as_ref() } } + +impl SimpleType { + pub fn new(kind: STyKind) -> Self { + SimpleType { + kind: Box::new(kind), + } + } + pub fn kind(&self) -> &STyKind { + self.kind.as_ref() + } + pub fn into_normalized(self) -> Normalized { + match *self.kind { + STyKind::Bool => Normalized::make_builtin_type(Builtin::Bool), + STyKind::Natural => Normalized::make_builtin_type(Builtin::Natural), + STyKind::Integer => Normalized::make_builtin_type(Builtin::Integer), + STyKind::Double => Normalized::make_builtin_type(Builtin::Double), + STyKind::Text => Normalized::make_builtin_type(Builtin::Text), + STyKind::Optional(t) => { + Normalized::make_optional_type(t.into_normalized()) + } + STyKind::List(t) => Normalized::make_list_type(t.into_normalized()), + STyKind::Record(kts) => Normalized::make_record_type( + kts.into_iter().map(|(k, t)| (k, t.into_normalized())), + ), + STyKind::Union(kts) => Normalized::make_union_type( + kts.into_iter() + .map(|(k, t)| (k, t.map(|t| t.into_normalized()))), + ), + } + } +} -- 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 +++++++++++++++++++++++++++++++++--- dhall/src/semantics/tck/typecheck.rs | 4 +-- 2 files changed, 62 insertions(+), 6 deletions(-) (limited to 'dhall/src') 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) + } +} diff --git a/dhall/src/semantics/tck/typecheck.rs b/dhall/src/semantics/tck/typecheck.rs index 173b76d..9fa33f2 100644 --- a/dhall/src/semantics/tck/typecheck.rs +++ b/dhall/src/semantics/tck/typecheck.rs @@ -808,8 +808,8 @@ pub(crate) fn typecheck<'hir>(hir: &'hir Hir) -> Result, TypeError> { /// Like `typecheck`, but additionally checks that the expression's type matches the provided type. pub(crate) fn typecheck_with<'hir>( hir: &'hir Hir, - ty: Hir, + ty: &Hir, ) -> Result, TypeError> { - let ty = typecheck(&ty)?.eval_to_type(&TyEnv::new())?; + let ty = typecheck(ty)?.eval_to_type(&TyEnv::new())?; type_with(&TyEnv::new(), hir, Some(ty)) } -- 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 ++++++++++++++---------------------------- dhall/src/simple.rs | 7 +++++-- dhall/src/tests.rs | 2 +- 3 files changed, 20 insertions(+), 31 deletions(-) (limited to 'dhall/src') 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( diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 9d3846e..014053e 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use crate::syntax::{Builtin, LitKind}; -use crate::Normalized; +use crate::{Normalized, Value}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct SimpleValue { @@ -56,7 +56,10 @@ impl SimpleType { pub fn kind(&self) -> &STyKind { self.kind.as_ref() } - pub fn into_normalized(self) -> Normalized { + pub fn to_value(&self) -> Value { + self.clone().into_normalized().to_value() + } + fn into_normalized(self) -> Normalized { match *self.kind { STyKind::Bool => Normalized::make_builtin_type(Builtin::Bool), STyKind::Natural => Normalized::make_builtin_type(Builtin::Natural), diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index a5a278c..659f6f7 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -302,7 +302,7 @@ fn run_test(test: Test) -> Result<()> { expected.compare(expr)?; } ImportFailure(expr, expected) => { - let err = expr.parse()?.resolve().unwrap_err(); + let err = expr.resolve().unwrap_err(); expected.compare_ui(err)?; } TypeInferenceSuccess(expr, expected) => { -- 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 +++++++---------------------------------------------- dhall/src/simple.rs | 50 ++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 84 deletions(-) (limited to 'dhall/src') 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() diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 014053e..7e7e459 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,7 +1,8 @@ use std::collections::BTreeMap; -use crate::syntax::{Builtin, LitKind}; -use crate::{Normalized, Value}; +use crate::semantics::{Hir, HirKind}; +use crate::syntax::{Builtin, ExprKind, LitKind, Span}; +use crate::Value; #[derive(Debug, Clone, PartialEq, Eq)] pub struct SimpleValue { @@ -57,26 +58,39 @@ impl SimpleType { self.kind.as_ref() } pub fn to_value(&self) -> Value { - self.clone().into_normalized().to_value() + Value { + hir: self.to_hir(), + as_simple_val: None, + as_simple_ty: Some(self.clone()), + } } - fn into_normalized(self) -> Normalized { - match *self.kind { - STyKind::Bool => Normalized::make_builtin_type(Builtin::Bool), - STyKind::Natural => Normalized::make_builtin_type(Builtin::Natural), - STyKind::Integer => Normalized::make_builtin_type(Builtin::Integer), - STyKind::Double => Normalized::make_builtin_type(Builtin::Double), - STyKind::Text => Normalized::make_builtin_type(Builtin::Text), - STyKind::Optional(t) => { - Normalized::make_optional_type(t.into_normalized()) + pub(crate) fn to_hir(&self) -> Hir { + let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); + hir(match self.kind() { + STyKind::Bool => ExprKind::Builtin(Builtin::Bool), + STyKind::Natural => ExprKind::Builtin(Builtin::Natural), + STyKind::Integer => ExprKind::Builtin(Builtin::Integer), + STyKind::Double => ExprKind::Builtin(Builtin::Double), + STyKind::Text => ExprKind::Builtin(Builtin::Text), + STyKind::Optional(t) => ExprKind::App( + hir(ExprKind::Builtin(Builtin::Optional)), + t.to_hir(), + ), + STyKind::List(t) => { + ExprKind::App(hir(ExprKind::Builtin(Builtin::List)), t.to_hir()) } - STyKind::List(t) => Normalized::make_list_type(t.into_normalized()), - STyKind::Record(kts) => Normalized::make_record_type( - kts.into_iter().map(|(k, t)| (k, t.into_normalized())), + STyKind::Record(kts) => ExprKind::RecordType( + kts.into_iter() + .map(|(k, t)| (k.as_str().into(), t.to_hir())) + .collect(), ), - STyKind::Union(kts) => Normalized::make_union_type( + STyKind::Union(kts) => ExprKind::UnionType( kts.into_iter() - .map(|(k, t)| (k, t.map(|t| t.into_normalized()))), + .map(|(k, t)| { + (k.as_str().into(), t.as_ref().map(|t| t.to_hir())) + }) + .collect(), ), - } + }) } } -- cgit v1.2.3 From 2f5c45fd2f712f7befe6f7c92b62dc76d5f77538 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 16:50:49 +0000 Subject: Rename LitKind to NumKind --- dhall/src/semantics/builtins.rs | 54 ++++++++++++++-------------- dhall/src/semantics/nze/nir.rs | 10 +++--- dhall/src/semantics/nze/normalize.rs | 70 ++++++++++++++++++------------------ dhall/src/semantics/tck/typecheck.rs | 10 +++--- dhall/src/simple.rs | 4 +-- dhall/src/syntax/ast/expr.rs | 6 ++-- dhall/src/syntax/ast/visitor.rs | 2 +- dhall/src/syntax/binary/decode.rs | 16 ++++----- dhall/src/syntax/binary/encode.rs | 10 +++--- dhall/src/syntax/text/parser.rs | 14 ++++---- dhall/src/syntax/text/printer.rs | 6 ++-- 11 files changed, 101 insertions(+), 101 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index 803630b..8d17aed 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -5,7 +5,7 @@ use crate::syntax::map::DupTreeMap; use crate::syntax::Const::Type; use crate::syntax::{ BinOp, Builtin, Const, Expr, ExprKind, InterpolatedText, - InterpolatedTextContents, Label, LitKind, NaiveDouble, Span, UnspannedExpr, + InterpolatedTextContents, Label, NaiveDouble, NumKind, Span, UnspannedExpr, V, }; use crate::Normalized; @@ -241,7 +241,7 @@ macro_rules! make_closure { rc(ExprKind::BinOp( BinOp::NaturalPlus, make_closure!($($v)*), - rc(ExprKind::Lit(LitKind::Natural(1))) + rc(ExprKind::Num(NumKind::Natural(1))) )) }; ([ $($head:tt)* ] # $($tail:tt)*) => {{ @@ -257,8 +257,8 @@ macro_rules! make_closure { #[allow(clippy::cognitive_complexity)] fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { - use LitKind::{Bool, Double, Integer, Natural}; use NirKind::*; + use NumKind::{Bool, Double, Integer, Natural}; // Small helper enum enum Ret { @@ -277,36 +277,36 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { Ret::NirKind(EmptyOptionalLit(t.clone())) } (Builtin::NaturalIsZero, [n]) => match &*n.kind() { - Lit(Natural(n)) => Ret::NirKind(Lit(Bool(*n == 0))), + Num(Natural(n)) => Ret::NirKind(Num(Bool(*n == 0))), _ => Ret::DoneAsIs, }, (Builtin::NaturalEven, [n]) => match &*n.kind() { - Lit(Natural(n)) => Ret::NirKind(Lit(Bool(*n % 2 == 0))), + Num(Natural(n)) => Ret::NirKind(Num(Bool(*n % 2 == 0))), _ => Ret::DoneAsIs, }, (Builtin::NaturalOdd, [n]) => match &*n.kind() { - Lit(Natural(n)) => Ret::NirKind(Lit(Bool(*n % 2 != 0))), + Num(Natural(n)) => Ret::NirKind(Num(Bool(*n % 2 != 0))), _ => Ret::DoneAsIs, }, (Builtin::NaturalToInteger, [n]) => match &*n.kind() { - Lit(Natural(n)) => Ret::NirKind(Lit(Integer(*n as isize))), + Num(Natural(n)) => Ret::NirKind(Num(Integer(*n as isize))), _ => Ret::DoneAsIs, }, (Builtin::NaturalShow, [n]) => match &*n.kind() { - Lit(Natural(n)) => Ret::Nir(Nir::from_text(n)), + Num(Natural(n)) => Ret::Nir(Nir::from_text(n)), _ => Ret::DoneAsIs, }, (Builtin::NaturalSubtract, [a, b]) => match (&*a.kind(), &*b.kind()) { - (Lit(Natural(a)), Lit(Natural(b))) => { - Ret::NirKind(Lit(Natural(if b > a { b - a } else { 0 }))) + (Num(Natural(a)), Num(Natural(b))) => { + Ret::NirKind(Num(Natural(if b > a { b - a } else { 0 }))) } - (Lit(Natural(0)), _) => Ret::Nir(b.clone()), - (_, Lit(Natural(0))) => Ret::NirKind(Lit(Natural(0))), - _ if a == b => Ret::NirKind(Lit(Natural(0))), + (Num(Natural(0)), _) => Ret::Nir(b.clone()), + (_, Num(Natural(0))) => Ret::NirKind(Num(Natural(0))), + _ if a == b => Ret::NirKind(Num(Natural(0))), _ => Ret::DoneAsIs, }, (Builtin::IntegerShow, [n]) => match &*n.kind() { - Lit(Integer(n)) => { + Num(Integer(n)) => { let s = if *n < 0 { n.to_string() } else { @@ -317,23 +317,23 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { _ => Ret::DoneAsIs, }, (Builtin::IntegerToDouble, [n]) => match &*n.kind() { - Lit(Integer(n)) => { - Ret::NirKind(Lit(Double(NaiveDouble::from(*n as f64)))) + Num(Integer(n)) => { + Ret::NirKind(Num(Double(NaiveDouble::from(*n as f64)))) } _ => Ret::DoneAsIs, }, (Builtin::IntegerNegate, [n]) => match &*n.kind() { - Lit(Integer(n)) => Ret::NirKind(Lit(Integer(-n))), + Num(Integer(n)) => Ret::NirKind(Num(Integer(-n))), _ => Ret::DoneAsIs, }, (Builtin::IntegerClamp, [n]) => match &*n.kind() { - Lit(Integer(n)) => { - Ret::NirKind(Lit(Natural((*n).try_into().unwrap_or(0)))) + Num(Integer(n)) => { + Ret::NirKind(Num(Natural((*n).try_into().unwrap_or(0)))) } _ => Ret::DoneAsIs, }, (Builtin::DoubleShow, [n]) => match &*n.kind() { - Lit(Double(n)) => Ret::Nir(Nir::from_text(n)), + Num(Double(n)) => Ret::Nir(Nir::from_text(n)), _ => Ret::DoneAsIs, }, (Builtin::TextShow, [v]) => match &*v.kind() { @@ -351,8 +351,8 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { _ => Ret::DoneAsIs, }, (Builtin::ListLength, [_, l]) => match &*l.kind() { - EmptyListLit(_) => Ret::NirKind(Lit(Natural(0))), - NEListLit(xs) => Ret::NirKind(Lit(Natural(xs.len()))), + EmptyListLit(_) => Ret::NirKind(Num(Natural(0))), + NEListLit(xs) => Ret::NirKind(Num(Natural(xs.len()))), _ => Ret::DoneAsIs, }, (Builtin::ListHead, [_, l]) => match &*l.kind() { @@ -398,7 +398,7 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { let mut kvs = HashMap::new(); kvs.insert( "index".into(), - Nir::from_kind(Lit(Natural(i))), + Nir::from_kind(Num(Natural(i))), ); kvs.insert("value".into(), e.clone()); Nir::from_kind(RecordLit(kvs)) @@ -466,14 +466,14 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { λ(x : Natural) -> 1 + var(x) ))) - .app(Lit(Natural(0)).into_nir()), + .app(Num(Natural(0)).into_nir()), ), (Builtin::NaturalFold, [n, t, succ, zero]) => match &*n.kind() { - Lit(Natural(0)) => Ret::Nir(zero.clone()), - Lit(Natural(n)) => { + Num(Natural(0)) => Ret::Nir(zero.clone()), + Num(Natural(n)) => { let fold = Nir::from_builtin(Builtin::NaturalFold) - .app(Lit(Natural(n - 1)).into_nir()) + .app(Num(Natural(n - 1)).into_nir()) .app(t.clone()) .app(succ.clone()) .app(zero.clone()); diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 4615b39..6f482a8 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -7,7 +7,7 @@ use crate::semantics::{ BuiltinClosure, Hir, HirKind, NzEnv, NzVar, TyEnv, Type, Universe, VarEnv, }; use crate::syntax::{ - BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, Label, LitKind, + BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, Label, NumKind, Span, }; use crate::{NormalizedExpr, ToExprOptions}; @@ -75,7 +75,7 @@ pub(crate) enum NirKind { Var(NzVar), Const(Const), - Lit(LitKind), + Num(NumKind), EmptyOptionalLit(Nir), NEOptionalLit(Nir), // EmptyListLit(t) means `[] : List t`, not `[] : t` @@ -145,7 +145,7 @@ impl Nir { } pub(crate) fn to_simple_value(&self) -> Option { Some(SimpleValue::new(match self.kind() { - NirKind::Lit(lit) => SValKind::Lit(lit.clone()), + NirKind::Num(lit) => SValKind::Num(lit.clone()), NirKind::TextLit(x) => SValKind::Text( x.as_text() .expect("Normal form should ensure the text is a string"), @@ -268,7 +268,7 @@ impl Nir { closure.to_hir(venv), ), NirKind::Const(c) => ExprKind::Const(*c), - NirKind::Lit(l) => ExprKind::Lit(l.clone()), + NirKind::Num(l) => ExprKind::Num(l.clone()), NirKind::EmptyOptionalLit(n) => ExprKind::App( Nir::from_builtin(Builtin::OptionalNone).to_hir(venv), n.to_hir(venv), @@ -366,7 +366,7 @@ impl NirKind { pub(crate) fn normalize(&self) { match self { - NirKind::Var(..) | NirKind::Const(_) | NirKind::Lit(_) => {} + NirKind::Var(..) | NirKind::Const(_) | NirKind::Num(_) => {} NirKind::EmptyOptionalLit(tth) | NirKind::EmptyListLit(tth) => { tth.normalize(); diff --git a/dhall/src/semantics/nze/normalize.rs b/dhall/src/semantics/nze/normalize.rs index 79d55e8..c5e66a1 100644 --- a/dhall/src/semantics/nze/normalize.rs +++ b/dhall/src/semantics/nze/normalize.rs @@ -6,7 +6,7 @@ use crate::semantics::{ Binder, BuiltinClosure, Closure, Hir, HirKind, Nir, NirKind, TextLit, }; use crate::syntax::{ - BinOp, Builtin, ExprKind, InterpolatedTextContents, LitKind, + BinOp, Builtin, ExprKind, InterpolatedTextContents, NumKind, }; pub(crate) fn apply_any(f: Nir, a: Nir) -> NirKind { @@ -99,40 +99,40 @@ fn apply_binop<'a>(o: BinOp, x: &'a Nir, y: &'a Nir) -> Option> { NaturalTimes, RecursiveRecordMerge, RecursiveRecordTypeMerge, RightBiasedRecordMerge, TextAppend, }; - use LitKind::{Bool, Natural}; - use NirKind::{EmptyListLit, Lit, NEListLit, RecordLit, RecordType}; + use NirKind::{EmptyListLit, NEListLit, Num, RecordLit, RecordType}; + use NumKind::{Bool, Natural}; Some(match (o, x.kind(), y.kind()) { - (BoolAnd, Lit(Bool(true)), _) => Ret::NirRef(y), - (BoolAnd, _, Lit(Bool(true))) => Ret::NirRef(x), - (BoolAnd, Lit(Bool(false)), _) => Ret::NirKind(Lit(Bool(false))), - (BoolAnd, _, Lit(Bool(false))) => Ret::NirKind(Lit(Bool(false))), + (BoolAnd, Num(Bool(true)), _) => Ret::NirRef(y), + (BoolAnd, _, Num(Bool(true))) => Ret::NirRef(x), + (BoolAnd, Num(Bool(false)), _) => Ret::NirKind(Num(Bool(false))), + (BoolAnd, _, Num(Bool(false))) => Ret::NirKind(Num(Bool(false))), (BoolAnd, _, _) if x == y => Ret::NirRef(x), - (BoolOr, Lit(Bool(true)), _) => Ret::NirKind(Lit(Bool(true))), - (BoolOr, _, Lit(Bool(true))) => Ret::NirKind(Lit(Bool(true))), - (BoolOr, Lit(Bool(false)), _) => Ret::NirRef(y), - (BoolOr, _, Lit(Bool(false))) => Ret::NirRef(x), + (BoolOr, Num(Bool(true)), _) => Ret::NirKind(Num(Bool(true))), + (BoolOr, _, Num(Bool(true))) => Ret::NirKind(Num(Bool(true))), + (BoolOr, Num(Bool(false)), _) => Ret::NirRef(y), + (BoolOr, _, Num(Bool(false))) => Ret::NirRef(x), (BoolOr, _, _) if x == y => Ret::NirRef(x), - (BoolEQ, Lit(Bool(true)), _) => Ret::NirRef(y), - (BoolEQ, _, Lit(Bool(true))) => Ret::NirRef(x), - (BoolEQ, Lit(Bool(x)), Lit(Bool(y))) => Ret::NirKind(Lit(Bool(x == y))), - (BoolEQ, _, _) if x == y => Ret::NirKind(Lit(Bool(true))), - (BoolNE, Lit(Bool(false)), _) => Ret::NirRef(y), - (BoolNE, _, Lit(Bool(false))) => Ret::NirRef(x), - (BoolNE, Lit(Bool(x)), Lit(Bool(y))) => Ret::NirKind(Lit(Bool(x != y))), - (BoolNE, _, _) if x == y => Ret::NirKind(Lit(Bool(false))), + (BoolEQ, Num(Bool(true)), _) => Ret::NirRef(y), + (BoolEQ, _, Num(Bool(true))) => Ret::NirRef(x), + (BoolEQ, Num(Bool(x)), Num(Bool(y))) => Ret::NirKind(Num(Bool(x == y))), + (BoolEQ, _, _) if x == y => Ret::NirKind(Num(Bool(true))), + (BoolNE, Num(Bool(false)), _) => Ret::NirRef(y), + (BoolNE, _, Num(Bool(false))) => Ret::NirRef(x), + (BoolNE, Num(Bool(x)), Num(Bool(y))) => Ret::NirKind(Num(Bool(x != y))), + (BoolNE, _, _) if x == y => Ret::NirKind(Num(Bool(false))), - (NaturalPlus, Lit(Natural(0)), _) => Ret::NirRef(y), - (NaturalPlus, _, Lit(Natural(0))) => Ret::NirRef(x), - (NaturalPlus, Lit(Natural(x)), Lit(Natural(y))) => { - Ret::NirKind(Lit(Natural(x + y))) + (NaturalPlus, Num(Natural(0)), _) => Ret::NirRef(y), + (NaturalPlus, _, Num(Natural(0))) => Ret::NirRef(x), + (NaturalPlus, Num(Natural(x)), Num(Natural(y))) => { + Ret::NirKind(Num(Natural(x + y))) } - (NaturalTimes, Lit(Natural(0)), _) => Ret::NirKind(Lit(Natural(0))), - (NaturalTimes, _, Lit(Natural(0))) => Ret::NirKind(Lit(Natural(0))), - (NaturalTimes, Lit(Natural(1)), _) => Ret::NirRef(y), - (NaturalTimes, _, Lit(Natural(1))) => Ret::NirRef(x), - (NaturalTimes, Lit(Natural(x)), Lit(Natural(y))) => { - Ret::NirKind(Lit(Natural(x * y))) + (NaturalTimes, Num(Natural(0)), _) => Ret::NirKind(Num(Natural(0))), + (NaturalTimes, _, Num(Natural(0))) => Ret::NirKind(Num(Natural(0))), + (NaturalTimes, Num(Natural(1)), _) => Ret::NirRef(y), + (NaturalTimes, _, Num(Natural(1))) => Ret::NirRef(x), + (NaturalTimes, Num(Natural(x)), Num(Natural(y))) => { + Ret::NirKind(Num(Natural(x * y))) } (ListAppend, EmptyListLit(_), _) => Ret::NirRef(y), @@ -212,12 +212,12 @@ fn apply_binop<'a>(o: BinOp, x: &'a Nir, y: &'a Nir) -> Option> { #[allow(clippy::cognitive_complexity)] pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { - use LitKind::Bool; use NirKind::{ - EmptyListLit, EmptyOptionalLit, Lit, NEListLit, NEOptionalLit, + EmptyListLit, EmptyOptionalLit, NEListLit, NEOptionalLit, Num, PartialExpr, RecordLit, RecordType, UnionConstructor, UnionLit, UnionType, }; + use NumKind::Bool; let ret = match expr { ExprKind::Import(..) | ExprKind::Completion(..) => { @@ -235,7 +235,7 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { ExprKind::Builtin(b) => Ret::Nir(Nir::from_builtin_env(b, env)), ExprKind::Assert(_) => Ret::Expr(expr), ExprKind::App(v, a) => Ret::Nir(v.app(a)), - ExprKind::Lit(l) => Ret::NirKind(Lit(l)), + ExprKind::Num(l) => Ret::NirKind(Num(l)), ExprKind::SomeLit(e) => Ret::NirKind(NEOptionalLit(e)), ExprKind::EmptyListLit(t) => { let arg = match t.kind() { @@ -271,12 +271,12 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { } ExprKind::BoolIf(ref b, ref e1, ref e2) => { match b.kind() { - Lit(Bool(true)) => Ret::NirRef(e1), - Lit(Bool(false)) => Ret::NirRef(e2), + Num(Bool(true)) => Ret::NirRef(e1), + Num(Bool(false)) => Ret::NirRef(e2), _ => { match (e1.kind(), e2.kind()) { // Simplify `if b then True else False` - (Lit(Bool(true)), Lit(Bool(false))) => Ret::NirRef(b), + (Num(Bool(true)), Num(Bool(false))) => Ret::NirRef(b), _ if e1 == e2 => Ret::NirRef(e1), _ => Ret::Expr(expr), } diff --git a/dhall/src/semantics/tck/typecheck.rs b/dhall/src/semantics/tck/typecheck.rs index 9fa33f2..6951d62 100644 --- a/dhall/src/semantics/tck/typecheck.rs +++ b/dhall/src/semantics/tck/typecheck.rs @@ -9,7 +9,7 @@ use crate::semantics::{ NirKind, Tir, TyEnv, Type, }; use crate::syntax::{ - BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, LitKind, Span, + BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, NumKind, Span, }; fn check_rectymerge( @@ -96,14 +96,14 @@ fn type_one_layer( let t_hir = type_of_builtin(*b); typecheck(&t_hir)?.eval_to_type(env)? } - ExprKind::Lit(LitKind::Bool(_)) => Type::from_builtin(Builtin::Bool), - ExprKind::Lit(LitKind::Natural(_)) => { + ExprKind::Num(NumKind::Bool(_)) => Type::from_builtin(Builtin::Bool), + ExprKind::Num(NumKind::Natural(_)) => { Type::from_builtin(Builtin::Natural) } - ExprKind::Lit(LitKind::Integer(_)) => { + ExprKind::Num(NumKind::Integer(_)) => { Type::from_builtin(Builtin::Integer) } - ExprKind::Lit(LitKind::Double(_)) => { + ExprKind::Num(NumKind::Double(_)) => { Type::from_builtin(Builtin::Double) } ExprKind::TextLit(interpolated) => { diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 7e7e459..9c4d6ba 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use crate::semantics::{Hir, HirKind}; -use crate::syntax::{Builtin, ExprKind, LitKind, Span}; +use crate::syntax::{Builtin, ExprKind, NumKind, Span}; use crate::Value; #[derive(Debug, Clone, PartialEq, Eq)] @@ -11,7 +11,7 @@ pub struct SimpleValue { #[derive(Debug, Clone, PartialEq, Eq)] pub enum SValKind { - Lit(LitKind), + Num(NumKind), Optional(Option), List(Vec), Record(BTreeMap), diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index b53e6cb..2c1860e 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -112,9 +112,9 @@ pub struct Expr { pub type UnspannedExpr = ExprKind; -/// Simple literals +/// Numeric literals #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum LitKind { +pub enum NumKind { /// `True` Bool(bool), /// `1` @@ -132,7 +132,7 @@ pub enum LitKind { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ExprKind { Const(Const), - Lit(LitKind), + Num(NumKind), /// `x` /// `x@n` Var(V), diff --git a/dhall/src/syntax/ast/visitor.rs b/dhall/src/syntax/ast/visitor.rs index c361bc1..0743af4 100644 --- a/dhall/src/syntax/ast/visitor.rs +++ b/dhall/src/syntax/ast/visitor.rs @@ -91,7 +91,7 @@ where Annot(x, t) => Annot(expr!(x)?, expr!(t)?), Const(k) => Const(*k), Builtin(v) => Builtin(*v), - Lit(l) => Lit(l.clone()), + Num(n) => Num(n.clone()), TextLit(t) => TextLit(t.traverse_ref(|e| expr!(e))?), BinOp(o, x, y) => BinOp(*o, expr!(x)?, expr!(y)?), BoolIf(b, t, f) => BoolIf(expr!(b)?, expr!(t)?, expr!(f)?), diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index 2ecd7e0..05d5921 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -6,7 +6,7 @@ use crate::error::DecodeError; use crate::syntax; use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, - Integer, InterpolatedText, Label, LitKind, Natural, Scheme, Span, + Integer, InterpolatedText, Label, Natural, NumKind, Scheme, Span, UnspannedExpr, URL, V, }; use crate::DecodedExpr; @@ -31,8 +31,8 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { String(s) => match Builtin::parse(s) { Some(b) => ExprKind::Builtin(b), None => match s.as_str() { - "True" => Lit(LitKind::Bool(true)), - "False" => Lit(LitKind::Bool(false)), + "True" => Num(NumKind::Bool(true)), + "False" => Num(NumKind::Bool(false)), "Type" => Const(Const::Type), "Kind" => Const(Const::Kind), "Sort" => Const(Const::Sort), @@ -44,8 +44,8 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { }, }, U64(n) => Var(V(Label::from("_"), *n as usize)), - F64(x) => Lit(LitKind::Double((*x).into())), - Bool(b) => Lit(LitKind::Bool(*b)), + F64(x) => Num(NumKind::Double((*x).into())), + Bool(b) => Num(NumKind::Bool(*b)), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { if l.as_str() == "_" { @@ -224,9 +224,9 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { let z = cbor_value_to_dhall(&z)?; BoolIf(x, y, z) } - [U64(15), U64(x)] => Lit(LitKind::Natural(*x as Natural)), - [U64(16), U64(x)] => Lit(LitKind::Integer(*x as Integer)), - [U64(16), I64(x)] => Lit(LitKind::Integer(*x as Integer)), + [U64(15), U64(x)] => Num(NumKind::Natural(*x as Natural)), + [U64(16), U64(x)] => Num(NumKind::Integer(*x as Integer)), + [U64(16), I64(x)] => Num(NumKind::Integer(*x as Integer)), [U64(18), String(first), rest @ ..] => { TextLit(InterpolatedText::from(( first.clone(), diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 9e6948e..5974f47 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -48,7 +48,7 @@ where use std::iter::once; use syntax::Builtin; use syntax::ExprKind::*; - use syntax::LitKind::*; + use syntax::NumKind::*; use self::Serialize::{RecordDupMap, RecordMap, UnionMap}; fn expr(x: &Expr) -> self::Serialize<'_> { @@ -63,10 +63,10 @@ where match e.as_ref() { Const(c) => ser.serialize_str(&c.to_string()), Builtin(b) => ser.serialize_str(&b.to_string()), - Lit(Bool(b)) => ser.serialize_bool(*b), - Lit(Natural(n)) => ser_seq!(ser; tag(15), U64(*n as u64)), - Lit(Integer(n)) => ser_seq!(ser; tag(16), I64(*n as i64)), - Lit(Double(n)) => { + Num(Bool(b)) => ser.serialize_bool(*b), + Num(Natural(n)) => ser_seq!(ser; tag(15), U64(*n as u64)), + Num(Integer(n)) => ser_seq!(ser; tag(16), I64(*n as i64)), + Num(Double(n)) => { let n: f64 = (*n).into(); ser.serialize_f64(n) } diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index 03211c7..6f5949f 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -9,7 +9,7 @@ use pest_consume::{match_nodes, Parser}; use crate::syntax::map::{DupTreeMap, DupTreeSet}; use crate::syntax::ExprKind::*; -use crate::syntax::LitKind::*; +use crate::syntax::NumKind::*; use crate::syntax::{ Double, Expr, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, Integer, InterpolatedText, InterpolatedTextContents, Label, NaiveDouble, @@ -135,7 +135,7 @@ fn insert_recordlit_entry(map: &mut BTreeMap, l: Label, e: Expr) { entry.insert(e); } Entry::Occupied(mut entry) => { - let dummy = Expr::new(Lit(Bool(false)), Span::Artificial); + let dummy = Expr::new(Num(Bool(false)), Span::Artificial); let other = entry.insert(dummy); entry.insert(Expr::new( BinOp(RecursiveRecordMerge, other, e), @@ -390,8 +390,8 @@ impl DhallParser { let e = match crate::syntax::Builtin::parse(s) { Some(b) => Builtin(b), None => match s { - "True" => Lit(Bool(true)), - "False" => Lit(Bool(false)), + "True" => Num(Bool(true)), + "False" => Num(Bool(false)), "Type" => Const(crate::syntax::Const::Type), "Kind" => Const(crate::syntax::Const::Kind), "Sort" => Const(crate::syntax::Const::Sort), @@ -924,9 +924,9 @@ impl DhallParser { #[alias(expression, shortcut = true)] fn primitive_expression(input: ParseInput) -> ParseResult { Ok(match_nodes!(input.children(); - [double_literal(n)] => spanned(input, Lit(Double(n))), - [natural_literal(n)] => spanned(input, Lit(Natural(n))), - [integer_literal(n)] => spanned(input, Lit(Integer(n))), + [double_literal(n)] => spanned(input, Num(Double(n))), + [natural_literal(n)] => spanned(input, Num(Natural(n))), + [integer_literal(n)] => spanned(input, Num(Integer(n))), [double_quote_literal(s)] => spanned(input, TextLit(s)), [single_quote_literal(s)] => spanned(input, TextLit(s)), [record_type_or_literal(e)] => spanned(input, e), diff --git a/dhall/src/syntax/text/printer.rs b/dhall/src/syntax/text/printer.rs index 2b7bc2e..378f408 100644 --- a/dhall/src/syntax/text/printer.rs +++ b/dhall/src/syntax/text/printer.rs @@ -201,7 +201,7 @@ impl Display for ExprKind { Var(a) => a.fmt(f)?, Const(k) => k.fmt(f)?, Builtin(v) => v.fmt(f)?, - Lit(a) => a.fmt(f)?, + Num(a) => a.fmt(f)?, TextLit(a) => a.fmt(f)?, RecordType(a) if a.is_empty() => f.write_str("{}")?, RecordType(a) => fmt_list("{ ", ", ", " }", a, f, |(k, t), f| { @@ -240,9 +240,9 @@ impl Display for Expr { } } -impl Display for LitKind { +impl Display for NumKind { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use LitKind::*; + use NumKind::*; match self { Bool(true) => f.write_str("True")?, Bool(false) => f.write_str("False")?, -- 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 +++++++++------------- dhall/src/semantics/builtins.rs | 3 +-- dhall/src/semantics/nze/nir.rs | 10 +++++----- dhall/src/semantics/resolve/hir.rs | 14 +++++--------- dhall/src/semantics/resolve/resolve.rs | 4 ++-- dhall/src/semantics/tck/tir.rs | 7 +++---- dhall/src/syntax/binary/decode.rs | 2 +- dhall/src/tests.rs | 15 ++++++--------- 8 files changed, 32 insertions(+), 45 deletions(-) (limited to 'dhall/src') 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() } diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index 8d17aed..eb50612 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -8,7 +8,6 @@ use crate::syntax::{ InterpolatedTextContents, Label, NaiveDouble, NumKind, Span, UnspannedExpr, V, }; -use crate::Normalized; use std::collections::HashMap; use std::convert::TryInto; @@ -340,7 +339,7 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { TextLit(tlit) => { if let Some(s) = tlit.as_text() { // Printing InterpolatedText takes care of all the escaping - let txt: InterpolatedText = + let txt: InterpolatedText = std::iter::once(InterpolatedTextContents::Text(s)) .collect(); Ret::Nir(Nir::from_text(txt)) diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 6f482a8..7365463 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -7,10 +7,10 @@ use crate::semantics::{ BuiltinClosure, Hir, HirKind, NzEnv, NzVar, TyEnv, Type, Universe, VarEnv, }; use crate::syntax::{ - BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, Label, NumKind, - Span, + BinOp, Builtin, Const, Expr, ExprKind, InterpolatedTextContents, Label, + NumKind, Span, }; -use crate::{NormalizedExpr, ToExprOptions}; +use crate::ToExprOptions; use crate::{STyKind, SValKind, SimpleType, SimpleValue}; /// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation @@ -137,10 +137,10 @@ impl Nir { Type::new(self.clone(), u.into()) } /// Converts a value back to the corresponding AST expression. - pub(crate) fn to_expr(&self, opts: ToExprOptions) -> NormalizedExpr { + pub(crate) fn to_expr(&self, opts: ToExprOptions) -> Expr { self.to_hir_noenv().to_expr(opts) } - pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> NormalizedExpr { + pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } pub(crate) fn to_simple_value(&self) -> Option { diff --git a/dhall/src/semantics/resolve/hir.rs b/dhall/src/semantics/resolve/hir.rs index fa2989f..22abaaf 100644 --- a/dhall/src/semantics/resolve/hir.rs +++ b/dhall/src/semantics/resolve/hir.rs @@ -1,7 +1,7 @@ use crate::error::TypeError; use crate::semantics::{type_with, NameEnv, Nir, NzEnv, Tir, TyEnv, Type}; use crate::syntax::{Expr, ExprKind, Span, V}; -use crate::{NormalizedExpr, ToExprOptions}; +use crate::ToExprOptions; /// Stores an alpha-normalized variable. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -51,15 +51,15 @@ impl Hir { } /// Converts a closed Hir expr back to the corresponding AST expression. - pub fn to_expr(&self, opts: ToExprOptions) -> NormalizedExpr { + pub fn to_expr(&self, opts: ToExprOptions) -> Expr { hir_to_expr(self, opts, &mut NameEnv::new()) } /// Converts a closed Hir expr back to the corresponding AST expression. - pub fn to_expr_noopts(&self) -> NormalizedExpr { + pub fn to_expr_noopts(&self) -> Expr { let opts = ToExprOptions { alpha: false }; self.to_expr(opts) } - pub fn to_expr_tyenv(&self, env: &TyEnv) -> NormalizedExpr { + pub fn to_expr_tyenv(&self, env: &TyEnv) -> Expr { let opts = ToExprOptions { alpha: false }; let mut env = env.as_nameenv().clone(); hir_to_expr(self, opts, &mut env) @@ -93,11 +93,7 @@ impl Hir { } } -fn hir_to_expr( - hir: &Hir, - opts: ToExprOptions, - env: &mut NameEnv, -) -> NormalizedExpr { +fn hir_to_expr(hir: &Hir, opts: ToExprOptions, env: &mut NameEnv) -> Expr { let kind = match hir.kind() { HirKind::Var(v) if opts.alpha => ExprKind::Var(V("_".into(), v.idx())), HirKind::Var(v) => ExprKind::Var(env.label_var(*v)), diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index f3fda4b..eb2f04f 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -13,7 +13,7 @@ use crate::syntax::{ BinOp, Builtin, Expr, ExprKind, FilePath, FilePrefix, ImportMode, ImportTarget, Span, UnspannedExpr, URL, }; -use crate::{Parsed, ParsedExpr, Resolved}; +use crate::{Parsed, Resolved}; // TODO: evaluate import headers pub(crate) type Import = syntax::Import<()>; @@ -333,7 +333,7 @@ pub(crate) fn resolve(parsed: Parsed) -> Result { resolve_with_env(&mut ImportEnv::new(), parsed) } -pub(crate) fn skip_resolve(expr: &ParsedExpr) -> Result { +pub(crate) fn skip_resolve(expr: &Expr) -> Result { traverse_resolve_expr(&mut NameEnv::new(), expr, &mut |import| { Err(ImportError::UnexpectedImport(import).into()) }) diff --git a/dhall/src/semantics/tck/tir.rs b/dhall/src/semantics/tck/tir.rs index aeb7bf9..1865b8e 100644 --- a/dhall/src/semantics/tck/tir.rs +++ b/dhall/src/semantics/tck/tir.rs @@ -1,7 +1,6 @@ use crate::error::{ErrorBuilder, TypeError}; use crate::semantics::{mkerr, Hir, Nir, NirKind, NzEnv, TyEnv, VarEnv}; -use crate::syntax::{Builtin, Const, Span}; -use crate::NormalizedExpr; +use crate::syntax::{Builtin, Const, Expr, Span}; /// The type of a type. 0 is `Type`, 1 is `Kind`, etc... #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] @@ -101,7 +100,7 @@ impl Type { pub fn to_hir(&self, venv: VarEnv) -> Hir { self.val.to_hir(venv) } - pub fn to_expr_tyenv(&self, tyenv: &TyEnv) -> NormalizedExpr { + pub fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { self.val.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } } @@ -124,7 +123,7 @@ impl<'hir> Tir<'hir> { pub fn as_hir(&self) -> &Hir { &self.hir } - pub fn to_expr_tyenv(&self, env: &TyEnv) -> NormalizedExpr { + pub fn to_expr_tyenv(&self, env: &TyEnv) -> Expr { self.as_hir().to_expr_tyenv(env) } diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index 05d5921..0fd358e 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -9,7 +9,7 @@ use crate::syntax::{ Integer, InterpolatedText, Label, Natural, NumKind, Scheme, Span, UnspannedExpr, URL, V, }; -use crate::DecodedExpr; +type DecodedExpr = Expr; pub(crate) fn decode(data: &[u8]) -> Result { match serde_cbor::de::from_slice(data) { diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 659f6f7..2cd354f 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -10,8 +10,8 @@ use std::io::{Read, Write}; use std::path::PathBuf; use crate::error::{ErrorKind, Result}; -use crate::syntax::binary; -use crate::{Normalized, NormalizedExpr, Parsed, Resolved, Typed}; +use crate::syntax::{binary, Expr}; +use crate::{Normalized, Parsed, Resolved, Typed}; macro_rules! assert_eq_display { ($left:expr, $right:expr) => {{ @@ -111,7 +111,7 @@ impl TestFile { env::var("UPDATE_TEST_FILES") == Ok("1".to_string()) } /// Write the provided expression to the pointed file. - fn write_expr(&self, expr: impl Into) -> Result<()> { + fn write_expr(&self, expr: impl Into) -> Result<()> { let expr = expr.into(); let path = self.path(); create_dir_all(path.parent().unwrap())?; @@ -142,7 +142,7 @@ impl TestFile { } /// Check that the provided expression matches the file contents. - pub fn compare(&self, expr: impl Into) -> Result<()> { + pub fn compare(&self, expr: impl Into) -> Result<()> { let expr = expr.into(); if !self.path().is_file() { return self.write_expr(expr); @@ -159,7 +159,7 @@ impl TestFile { Ok(()) } /// Check that the provided expression matches the file contents. - pub fn compare_debug(&self, expr: impl Into) -> Result<()> { + pub fn compare_debug(&self, expr: impl Into) -> Result<()> { let expr = expr.into(); if !self.path().is_file() { return self.write_expr(expr); @@ -176,10 +176,7 @@ impl TestFile { Ok(()) } /// Check that the provided expression matches the file contents. - pub fn compare_binary( - &self, - expr: impl Into, - ) -> Result<()> { + pub fn compare_binary(&self, expr: impl Into) -> Result<()> { let expr = expr.into(); match self { TestFile::Binary(_) => {} -- cgit v1.2.3 From b3a05d930dd8f2bbc705145219025af96e5dec86 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 20:16:08 +0000 Subject: Use dhall::Value more in serde --- dhall/src/simple.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dhall/src') diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 9c4d6ba..32c9c79 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -12,11 +12,11 @@ pub struct SimpleValue { #[derive(Debug, Clone, PartialEq, Eq)] pub enum SValKind { Num(NumKind), + Text(String), Optional(Option), List(Vec), Record(BTreeMap), Union(String, Option), - Text(String), } #[derive(Debug, Clone, PartialEq, Eq)] -- 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 +- dhall/src/semantics/nze/nir.rs | 81 ----------------------------------------- dhall/src/simple.rs | 83 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 84 deletions(-) (limited to 'dhall/src') 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), } } diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 7365463..1148e31 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -11,7 +11,6 @@ use crate::syntax::{ NumKind, Span, }; use crate::ToExprOptions; -use crate::{STyKind, SValKind, SimpleType, SimpleValue}; /// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation /// automatically. Uses a Rc to share computation. @@ -143,86 +142,6 @@ impl Nir { pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } - pub(crate) fn to_simple_value(&self) -> Option { - Some(SimpleValue::new(match self.kind() { - NirKind::Num(lit) => SValKind::Num(lit.clone()), - NirKind::TextLit(x) => SValKind::Text( - x.as_text() - .expect("Normal form should ensure the text is a string"), - ), - NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), - NirKind::NEOptionalLit(x) => { - SValKind::Optional(Some(x.to_simple_value()?)) - } - NirKind::EmptyListLit(_) => SValKind::List(vec![]), - NirKind::NEListLit(xs) => SValKind::List( - xs.iter() - .map(|v| v.to_simple_value()) - .collect::>()?, - ), - NirKind::RecordLit(kvs) => SValKind::Record( - kvs.iter() - .map(|(k, v)| Some((k.into(), v.to_simple_value()?))) - .collect::>()?, - ), - NirKind::UnionLit(field, x, _) => { - SValKind::Union(field.into(), Some(x.to_simple_value()?)) - } - NirKind::UnionConstructor(field, ty) - if ty.get(field).map(|f| f.is_some()) == Some(false) => - { - SValKind::Union(field.into(), None) - } - _ => return None, - })) - } - pub(crate) fn to_simple_type(&self) -> Option { - Some(SimpleType::new(match self.kind() { - NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. }) - if args.is_empty() => - { - match b { - Builtin::Bool => STyKind::Bool, - Builtin::Natural => STyKind::Natural, - Builtin::Integer => STyKind::Integer, - Builtin::Double => STyKind::Double, - Builtin::Text => STyKind::Text, - _ => return None, - } - } - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::Optional, - args, - .. - }) if args.len() == 1 => { - STyKind::Optional(args[0].to_simple_type()?) - } - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - }) if args.len() == 1 => STyKind::List(args[0].to_simple_type()?), - NirKind::RecordType(kts) => STyKind::Record( - kts.iter() - .map(|(k, v)| Some((k.into(), v.to_simple_type()?))) - .collect::>()?, - ), - NirKind::UnionType(kts) => STyKind::Union( - kts.iter() - .map(|(k, v)| { - Some(( - k.into(), - v.as_ref() - .map(|v| Ok(v.to_simple_type()?)) - .transpose()?, - )) - }) - .collect::>()?, - ), - _ => return None, - })) - } - pub(crate) fn normalize(&self) { self.0.normalize() } diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 32c9c79..2e5d3c4 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use crate::semantics::{Hir, HirKind}; +use crate::semantics::{BuiltinClosure, Hir, HirKind, Nir, NirKind}; use crate::syntax::{Builtin, ExprKind, NumKind, Span}; use crate::Value; @@ -43,6 +43,40 @@ impl SimpleValue { kind: Box::new(kind), } } + pub(crate) fn from_nir(nir: &Nir) -> Option { + Some(SimpleValue::new(match nir.kind() { + NirKind::Num(lit) => SValKind::Num(lit.clone()), + NirKind::TextLit(x) => SValKind::Text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), + NirKind::NEOptionalLit(x) => { + SValKind::Optional(Some(Self::from_nir(x)?)) + } + NirKind::EmptyListLit(_) => SValKind::List(vec![]), + NirKind::NEListLit(xs) => SValKind::List( + xs.iter() + .map(|v| Self::from_nir(v)) + .collect::>()?, + ), + NirKind::RecordLit(kvs) => SValKind::Record( + kvs.iter() + .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .collect::>()?, + ), + NirKind::UnionLit(field, x, _) => { + SValKind::Union(field.into(), Some(Self::from_nir(x)?)) + } + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + SValKind::Union(field.into(), None) + } + _ => return None, + })) + } + pub fn kind(&self) -> &SValKind { self.kind.as_ref() } @@ -54,6 +88,53 @@ impl SimpleType { kind: Box::new(kind), } } + pub(crate) fn from_nir(nir: &Nir) -> Option { + Some(SimpleType::new(match nir.kind() { + NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. }) + if args.is_empty() => + { + match b { + Builtin::Bool => STyKind::Bool, + Builtin::Natural => STyKind::Natural, + Builtin::Integer => STyKind::Integer, + Builtin::Double => STyKind::Double, + Builtin::Text => STyKind::Text, + _ => return None, + } + } + NirKind::AppliedBuiltin(BuiltinClosure { + b: Builtin::Optional, + args, + .. + }) if args.len() == 1 => { + STyKind::Optional(Self::from_nir(&args[0])?) + } + NirKind::AppliedBuiltin(BuiltinClosure { + b: Builtin::List, + args, + .. + }) if args.len() == 1 => STyKind::List(Self::from_nir(&args[0])?), + NirKind::RecordType(kts) => STyKind::Record( + kts.iter() + .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .collect::>()?, + ), + NirKind::UnionType(kts) => STyKind::Union( + kts.iter() + .map(|(k, v)| { + Some(( + k.into(), + v.as_ref() + .map(|v| Ok(Self::from_nir(v)?)) + .transpose()?, + )) + }) + .collect::>()?, + ), + _ => return None, + })) + } + pub fn kind(&self) -> &STyKind { self.kind.as_ref() } -- cgit v1.2.3 From ef3734a3e9381b2e91552089774126f58f560bc3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 21:21:32 +0000 Subject: Improve handling of builtin types in Nir --- dhall/src/semantics/builtins.rs | 31 +++++++++++++++++-------------- dhall/src/semantics/nze/nir.rs | 35 +++++++++++++++++++++++++++-------- dhall/src/semantics/nze/normalize.rs | 22 +++++----------------- dhall/src/semantics/tck/typecheck.rs | 28 ++++++---------------------- dhall/src/simple.rs | 36 +++++++++++------------------------- 5 files changed, 66 insertions(+), 86 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index eb50612..7fbb933 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -14,22 +14,17 @@ use std::convert::TryInto; /// A partially applied builtin. /// Invariant: the evaluation of the given args must not be able to progress further #[derive(Debug, Clone)] -pub(crate) struct BuiltinClosure { - pub env: NzEnv, - pub b: Builtin, +pub(crate) struct BuiltinClosure { + env: NzEnv, + b: Builtin, /// Arguments applied to the closure so far. - pub args: Vec, + args: Vec, } -impl BuiltinClosure { - pub fn new(b: Builtin, env: NzEnv) -> Self { - BuiltinClosure { - env, - b, - args: Vec::new(), - } +impl BuiltinClosure { + pub fn new(b: Builtin, env: NzEnv) -> NirKind { + apply_builtin(b, Vec::new(), env) } - pub fn apply(&self, a: Nir) -> NirKind { use std::iter::once; let args = self.args.iter().cloned().chain(once(a)).collect(); @@ -272,6 +267,14 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { }; let ret = match (b, args.as_slice()) { + (Builtin::Bool, []) + | (Builtin::Natural, []) + | (Builtin::Integer, []) + | (Builtin::Double, []) + | (Builtin::Text, []) => Ret::NirKind(BuiltinType(b)), + (Builtin::Optional, [t]) => Ret::NirKind(OptionalType(t.clone())), + (Builtin::List, [t]) => Ret::NirKind(ListType(t.clone())), + (Builtin::OptionalNone, [t]) => { Ret::NirKind(EmptyOptionalLit(t.clone())) } @@ -489,9 +492,9 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { } } -impl std::cmp::PartialEq for BuiltinClosure { +impl std::cmp::PartialEq for BuiltinClosure { fn eq(&self, other: &Self) -> bool { self.b == other.b && self.args == other.args } } -impl std::cmp::Eq for BuiltinClosure {} +impl std::cmp::Eq for BuiltinClosure {} diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 1148e31..e6482fc 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -70,13 +70,17 @@ pub(crate) enum NirKind { annot: Nir, closure: Closure, }, - AppliedBuiltin(BuiltinClosure), + AppliedBuiltin(BuiltinClosure), Var(NzVar), Const(Const), + // Must be a number type, Bool or Text + BuiltinType(Builtin), Num(NumKind), + OptionalType(Nir), EmptyOptionalLit(Nir), NEOptionalLit(Nir), + ListType(Nir), // EmptyListLit(t) means `[] : List t`, not `[] : t` EmptyListLit(Nir), NEListLit(Vec), @@ -187,12 +191,21 @@ impl Nir { closure.to_hir(venv), ), NirKind::Const(c) => ExprKind::Const(*c), + NirKind::BuiltinType(b) => ExprKind::Builtin(*b), NirKind::Num(l) => ExprKind::Num(l.clone()), + NirKind::OptionalType(t) => ExprKind::App( + Nir::from_builtin(Builtin::Optional).to_hir(venv), + t.to_hir(venv), + ), NirKind::EmptyOptionalLit(n) => ExprKind::App( Nir::from_builtin(Builtin::OptionalNone).to_hir(venv), n.to_hir(venv), ), NirKind::NEOptionalLit(n) => ExprKind::SomeLit(n.to_hir(venv)), + NirKind::ListType(t) => ExprKind::App( + Nir::from_builtin(Builtin::List).to_hir(venv), + t.to_hir(venv), + ), NirKind::EmptyListLit(n) => ExprKind::EmptyListLit(Hir::new( HirKind::Expr(ExprKind::App( Nir::from_builtin(Builtin::List).to_hir(venv), @@ -285,14 +298,20 @@ impl NirKind { pub(crate) fn normalize(&self) { match self { - NirKind::Var(..) | NirKind::Const(_) | NirKind::Num(_) => {} - - NirKind::EmptyOptionalLit(tth) | NirKind::EmptyListLit(tth) => { - tth.normalize(); + NirKind::Var(..) + | NirKind::Const(_) + | NirKind::Num(_) + | NirKind::BuiltinType(..) => {} + + NirKind::EmptyOptionalLit(v) + | NirKind::EmptyListLit(v) + | NirKind::OptionalType(v) + | NirKind::ListType(v) => { + v.normalize(); } - NirKind::NEOptionalLit(th) => { - th.normalize(); + NirKind::NEOptionalLit(v) => { + v.normalize(); } NirKind::LamClosure { annot, closure, .. } | NirKind::PiClosure { annot, closure, .. } => { @@ -341,7 +360,7 @@ impl NirKind { NirKind::from_builtin_env(b, NzEnv::new()) } pub(crate) fn from_builtin_env(b: Builtin, env: NzEnv) -> NirKind { - NirKind::AppliedBuiltin(BuiltinClosure::new(b, env)) + BuiltinClosure::new(b, env) } } diff --git a/dhall/src/semantics/nze/normalize.rs b/dhall/src/semantics/nze/normalize.rs index c5e66a1..46d8fb5 100644 --- a/dhall/src/semantics/nze/normalize.rs +++ b/dhall/src/semantics/nze/normalize.rs @@ -2,12 +2,8 @@ use itertools::Itertools; use std::collections::HashMap; use crate::semantics::NzEnv; -use crate::semantics::{ - Binder, BuiltinClosure, Closure, Hir, HirKind, Nir, NirKind, TextLit, -}; -use crate::syntax::{ - BinOp, Builtin, ExprKind, InterpolatedTextContents, NumKind, -}; +use crate::semantics::{Binder, Closure, Hir, HirKind, Nir, NirKind, TextLit}; +use crate::syntax::{BinOp, ExprKind, InterpolatedTextContents, NumKind}; pub(crate) fn apply_any(f: Nir, a: Nir) -> NirKind { match f.kind() { @@ -239,11 +235,7 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { ExprKind::SomeLit(e) => Ret::NirKind(NEOptionalLit(e)), ExprKind::EmptyListLit(t) => { let arg = match t.kind() { - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - }) if args.len() == 1 => args[0].clone(), + NirKind::ListType(t) => t.clone(), _ => panic!("internal type error"), }; Ret::NirKind(NirKind::EmptyListLit(arg)) @@ -442,12 +434,8 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { ExprKind::ToMap(ref v, ref annot) => match v.kind() { RecordLit(kvs) if kvs.is_empty() => { match annot.as_ref().map(|v| v.kind()) { - Some(NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - })) if args.len() == 1 => { - Ret::NirKind(EmptyListLit(args[0].clone())) + Some(NirKind::ListType(t)) => { + Ret::NirKind(EmptyListLit(t.clone())) } _ => Ret::Expr(expr), } diff --git a/dhall/src/semantics/tck/typecheck.rs b/dhall/src/semantics/tck/typecheck.rs index 6951d62..880ab22 100644 --- a/dhall/src/semantics/tck/typecheck.rs +++ b/dhall/src/semantics/tck/typecheck.rs @@ -5,8 +5,8 @@ use std::collections::HashMap; use crate::error::{ErrorBuilder, TypeError, TypeMessage}; use crate::semantics::merge_maps; use crate::semantics::{ - type_of_builtin, Binder, BuiltinClosure, Closure, Hir, HirKind, Nir, - NirKind, Tir, TyEnv, Type, + type_of_builtin, Binder, Closure, Hir, HirKind, Nir, NirKind, Tir, TyEnv, + Type, }; use crate::syntax::{ BinOp, Builtin, Const, ExprKind, InterpolatedTextContents, NumKind, Span, @@ -121,11 +121,7 @@ fn type_one_layer( ExprKind::EmptyListLit(t) => { let t = t.eval_to_type(env)?; match t.kind() { - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - }) if args.len() == 1 => {} + NirKind::ListType(..) => {} _ => return span_err("InvalidListType"), }; t @@ -376,10 +372,7 @@ fn type_one_layer( } ExprKind::BinOp(BinOp::ListAppend, l, r) => { match l.ty().kind() { - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - .. - }) => {} + NirKind::ListType(..) => {} _ => return span_err("BinOpTypeMismatch"), } @@ -435,12 +428,7 @@ fn type_one_layer( let union_type = union.ty(); let variants = match union_type.kind() { NirKind::UnionType(kts) => Cow::Borrowed(kts), - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::Optional, - args, - .. - }) if args.len() == 1 => { - let ty = &args[0]; + NirKind::OptionalType(ty) => { let mut kts = HashMap::new(); kts.insert("None".into(), None); kts.insert("Some".into(), Some(ty.clone())); @@ -595,11 +583,7 @@ fn type_one_layer( let err_msg = "The type of `toMap x` must be of the form \ `List { mapKey : Text, mapValue : T }`"; let arg = match annot_val.kind() { - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - }) if args.len() == 1 => &args[0], + NirKind::ListType(t) => t, _ => return span_err(err_msg), }; let kts = match arg.kind() { diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 2e5d3c4..45fa656 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use crate::semantics::{BuiltinClosure, Hir, HirKind, Nir, NirKind}; +use crate::semantics::{Hir, HirKind, Nir, NirKind}; use crate::syntax::{Builtin, ExprKind, NumKind, Span}; use crate::Value; @@ -90,30 +90,16 @@ impl SimpleType { } pub(crate) fn from_nir(nir: &Nir) -> Option { Some(SimpleType::new(match nir.kind() { - NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. }) - if args.is_empty() => - { - match b { - Builtin::Bool => STyKind::Bool, - Builtin::Natural => STyKind::Natural, - Builtin::Integer => STyKind::Integer, - Builtin::Double => STyKind::Double, - Builtin::Text => STyKind::Text, - _ => return None, - } - } - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::Optional, - args, - .. - }) if args.len() == 1 => { - STyKind::Optional(Self::from_nir(&args[0])?) - } - NirKind::AppliedBuiltin(BuiltinClosure { - b: Builtin::List, - args, - .. - }) if args.len() == 1 => STyKind::List(Self::from_nir(&args[0])?), + NirKind::BuiltinType(b) => match b { + Builtin::Bool => STyKind::Bool, + Builtin::Natural => STyKind::Natural, + Builtin::Integer => STyKind::Integer, + Builtin::Double => STyKind::Double, + Builtin::Text => STyKind::Text, + _ => unreachable!(), + }, + NirKind::OptionalType(t) => STyKind::Optional(Self::from_nir(t)?), + NirKind::ListType(t) => STyKind::List(Self::from_nir(t)?), NirKind::RecordType(kts) => STyKind::Record( kts.iter() .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) -- 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 +- dhall/src/semantics/builtins.rs | 7 ---- dhall/src/semantics/nze/nir.rs | 76 -------------------------------------- dhall/src/semantics/resolve/hir.rs | 6 --- 4 files changed, 3 insertions(+), 90 deletions(-) (limited to 'dhall/src') 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. diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index 7fbb933..ef375b8 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -30,13 +30,6 @@ impl BuiltinClosure { let args = self.args.iter().cloned().chain(once(a)).collect(); apply_builtin(self.b, args, self.env.clone()) } - /// This doesn't break the invariant because we already checked that the appropriate arguments - /// did not normalize to something that allows evaluation to proceed. - pub fn normalize(&self) { - for x in self.args.iter() { - x.normalize(); - } - } pub fn to_hirkind(&self, venv: VarEnv) -> HirKind { HirKind::Expr(self.args.iter().fold( ExprKind::Builtin(self.b), diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index e6482fc..5fccbf1 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -146,9 +146,6 @@ impl Nir { pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } - pub(crate) fn normalize(&self) { - self.0.normalize() - } pub(crate) fn app(&self, v: Nir) -> Nir { Nir::from_kind(apply_any(self.clone(), v)) @@ -286,9 +283,6 @@ impl NirInternal { fn kind(&self) -> &NirKind { &self.kind } - fn normalize(&self) { - self.kind().normalize(); - } } impl NirKind { @@ -296,66 +290,6 @@ impl NirKind { Nir::from_kind(self) } - pub(crate) fn normalize(&self) { - match self { - NirKind::Var(..) - | NirKind::Const(_) - | NirKind::Num(_) - | NirKind::BuiltinType(..) => {} - - NirKind::EmptyOptionalLit(v) - | NirKind::EmptyListLit(v) - | NirKind::OptionalType(v) - | NirKind::ListType(v) => { - v.normalize(); - } - - NirKind::NEOptionalLit(v) => { - v.normalize(); - } - NirKind::LamClosure { annot, closure, .. } - | NirKind::PiClosure { annot, closure, .. } => { - annot.normalize(); - closure.normalize(); - } - NirKind::AppliedBuiltin(closure) => closure.normalize(), - NirKind::NEListLit(elts) => { - for x in elts.iter() { - x.normalize(); - } - } - NirKind::RecordLit(kvs) => { - for x in kvs.values() { - x.normalize(); - } - } - NirKind::RecordType(kvs) => { - for x in kvs.values() { - x.normalize(); - } - } - NirKind::UnionType(kts) | NirKind::UnionConstructor(_, kts) => { - for x in kts.values().flatten() { - x.normalize(); - } - } - NirKind::UnionLit(_, v, kts) => { - v.normalize(); - for x in kts.values().flatten() { - x.normalize(); - } - } - NirKind::TextLit(tlit) => tlit.normalize(), - NirKind::Equivalence(x, y) => { - x.normalize(); - y.normalize(); - } - NirKind::PartialExpr(e) => { - e.map_ref(Nir::normalize); - } - } - } - pub(crate) fn from_builtin(b: Builtin) -> NirKind { NirKind::from_builtin_env(b, NzEnv::new()) } @@ -408,9 +342,6 @@ impl Closure { } } - // TODO: somehow normalize the body. Might require to pass an env. - pub fn normalize(&self) {} - /// Convert this closure to a Hir expression pub fn to_hir(&self, venv: VarEnv) -> Hir { self.apply_var(NzVar::new(venv.size())) @@ -474,13 +405,6 @@ impl TextLit { pub fn iter(&self) -> impl Iterator> { self.0.iter() } - /// Normalize the contained values. This does not break the invariant because we have already - /// ensured that no contained values normalize to a TextLit. - pub fn normalize(&self) { - for x in self.0.iter() { - x.map_ref(Nir::normalize); - } - } } impl lazy::Eval for Thunk { diff --git a/dhall/src/semantics/resolve/hir.rs b/dhall/src/semantics/resolve/hir.rs index 22abaaf..3934bbb 100644 --- a/dhall/src/semantics/resolve/hir.rs +++ b/dhall/src/semantics/resolve/hir.rs @@ -85,12 +85,6 @@ impl Hir { pub fn eval_closed_expr(&self) -> Nir { self.eval(NzEnv::new()) } - /// Eval a closed Hir fully and recursively; - pub fn rec_eval_closed_expr(&self) -> Nir { - let val = self.eval_closed_expr(); - val.normalize(); - val - } } fn hir_to_expr(hir: &Hir, opts: ToExprOptions, env: &mut NameEnv) -> Expr { -- cgit v1.2.3 From 7901220c7cc18b740ba2b1d7f786ebc1d97167db Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 22:02:43 +0000 Subject: Start overengineering SimpleVal/Ty --- dhall/src/simple.rs | 151 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 120 insertions(+), 31 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 45fa656..6f44183 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -37,6 +37,125 @@ pub enum STyKind { Union(BTreeMap>), } +pub trait SimpleValueFolder: Sized { + fn num(x: NumKind) -> Self; + fn text(x: String) -> Self; + fn optional(x: Option) -> Self; + fn list(x: impl Iterator) -> Self; + fn record(values: impl Iterator) -> Self; + fn union(variant: String, content: Option) -> Self; +} + +pub trait SimpleTypeFolder: Sized { + fn bool() -> Self; + fn natural() -> Self; + fn integer() -> Self; + fn double() -> Self; + fn text() -> Self; + fn optional(x: Self) -> Self; + fn list(x: Self) -> Self; + fn record(types: impl Iterator) -> Self; + fn union(types: impl Iterator)>) -> Self; +} + +pub(crate) fn fold_simple_value(nir: &Nir) -> Option +where + T: SimpleValueFolder, +{ + // TODO: check the type first and remove error handling + Some(match nir.kind() { + NirKind::Num(lit) => T::num(lit.clone()), + NirKind::TextLit(x) => T::text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => T::optional(None), + NirKind::NEOptionalLit(x) => T::optional(Some(fold_simple_value(x)?)), + NirKind::EmptyListLit(_) => T::list(std::iter::empty()), + NirKind::NEListLit(xs) => T::list( + xs.iter() + .map(|v| fold_simple_value(v)) + .collect::>>()? + .into_iter(), + ), + NirKind::RecordLit(kvs) => T::record( + kvs.iter() + .map(|(k, v)| Some((k.into(), fold_simple_value(v)?))) + .collect::>>()? + .into_iter(), + ), + NirKind::UnionLit(field, x, _) => { + T::union(field.into(), Some(fold_simple_value(x)?)) + } + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + T::union(field.into(), None) + } + _ => return None, + }) +} + +pub(crate) fn fold_simple_type(nir: &Nir) -> Option +where + T: SimpleTypeFolder, +{ + // TODO: avoid unnecessary allocations + Some(match nir.kind() { + NirKind::BuiltinType(b) => match b { + Builtin::Bool => T::bool(), + Builtin::Natural => T::natural(), + Builtin::Integer => T::integer(), + Builtin::Double => T::double(), + Builtin::Text => T::text(), + _ => unreachable!(), + }, + NirKind::OptionalType(t) => T::optional(fold_simple_type(t)?), + NirKind::ListType(t) => T::list(fold_simple_type(t)?), + NirKind::RecordType(kts) => T::record( + kts.iter() + .map(|(k, v)| Some((k.into(), fold_simple_type(v)?))) + .collect::>>()? + .into_iter(), + ), + NirKind::UnionType(kts) => T::union( + kts.iter() + .map(|(k, v)| { + Some(( + k.into(), + v.as_ref() + .map(|v| Ok(fold_simple_type(v)?)) + .transpose()?, + )) + }) + .collect::>>()? + .into_iter(), + ), + _ => return None, + }) +} + +impl SimpleValueFolder for SimpleValue { + fn num(x: NumKind) -> Self { + SimpleValue::new(SValKind::Num(x)) + } + fn text(x: String) -> Self { + SimpleValue::new(SValKind::Text(x)) + } + fn optional(x: Option) -> Self { + SimpleValue::new(SValKind::Optional(x)) + } + fn list(xs: impl Iterator) -> Self { + SimpleValue::new(SValKind::List(xs.collect())) + } + fn record(values: impl Iterator) -> Self { + SimpleValue::new(SValKind::Record(values.collect())) + } + fn union(variant: String, content: Option) -> Self { + SimpleValue::new(SValKind::Union(variant, content)) + } +} + impl SimpleValue { pub(crate) fn new(kind: SValKind) -> Self { SimpleValue { @@ -44,37 +163,7 @@ impl SimpleValue { } } pub(crate) fn from_nir(nir: &Nir) -> Option { - Some(SimpleValue::new(match nir.kind() { - NirKind::Num(lit) => SValKind::Num(lit.clone()), - NirKind::TextLit(x) => SValKind::Text( - x.as_text() - .expect("Normal form should ensure the text is a string"), - ), - NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), - NirKind::NEOptionalLit(x) => { - SValKind::Optional(Some(Self::from_nir(x)?)) - } - NirKind::EmptyListLit(_) => SValKind::List(vec![]), - NirKind::NEListLit(xs) => SValKind::List( - xs.iter() - .map(|v| Self::from_nir(v)) - .collect::>()?, - ), - NirKind::RecordLit(kvs) => SValKind::Record( - kvs.iter() - .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) - .collect::>()?, - ), - NirKind::UnionLit(field, x, _) => { - SValKind::Union(field.into(), Some(Self::from_nir(x)?)) - } - NirKind::UnionConstructor(field, ty) - if ty.get(field).map(|f| f.is_some()) == Some(false) => - { - SValKind::Union(field.into(), None) - } - _ => return None, - })) + fold_simple_value::(nir) } pub fn kind(&self) -> &SValKind { -- cgit v1.2.3 From 844dc3defdd7735742ad41eb6d1da8fe2e7f340b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 22:03:14 +0000 Subject: Revert "Start overengineering SimpleVal/Ty" This reverts commit ee9ee1e0ae26bd7fe718a1f8cbe56370ccf969a7. --- dhall/src/simple.rs | 151 +++++++++++----------------------------------------- 1 file changed, 31 insertions(+), 120 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 6f44183..45fa656 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -37,125 +37,6 @@ pub enum STyKind { Union(BTreeMap>), } -pub trait SimpleValueFolder: Sized { - fn num(x: NumKind) -> Self; - fn text(x: String) -> Self; - fn optional(x: Option) -> Self; - fn list(x: impl Iterator) -> Self; - fn record(values: impl Iterator) -> Self; - fn union(variant: String, content: Option) -> Self; -} - -pub trait SimpleTypeFolder: Sized { - fn bool() -> Self; - fn natural() -> Self; - fn integer() -> Self; - fn double() -> Self; - fn text() -> Self; - fn optional(x: Self) -> Self; - fn list(x: Self) -> Self; - fn record(types: impl Iterator) -> Self; - fn union(types: impl Iterator)>) -> Self; -} - -pub(crate) fn fold_simple_value(nir: &Nir) -> Option -where - T: SimpleValueFolder, -{ - // TODO: check the type first and remove error handling - Some(match nir.kind() { - NirKind::Num(lit) => T::num(lit.clone()), - NirKind::TextLit(x) => T::text( - x.as_text() - .expect("Normal form should ensure the text is a string"), - ), - NirKind::EmptyOptionalLit(_) => T::optional(None), - NirKind::NEOptionalLit(x) => T::optional(Some(fold_simple_value(x)?)), - NirKind::EmptyListLit(_) => T::list(std::iter::empty()), - NirKind::NEListLit(xs) => T::list( - xs.iter() - .map(|v| fold_simple_value(v)) - .collect::>>()? - .into_iter(), - ), - NirKind::RecordLit(kvs) => T::record( - kvs.iter() - .map(|(k, v)| Some((k.into(), fold_simple_value(v)?))) - .collect::>>()? - .into_iter(), - ), - NirKind::UnionLit(field, x, _) => { - T::union(field.into(), Some(fold_simple_value(x)?)) - } - NirKind::UnionConstructor(field, ty) - if ty.get(field).map(|f| f.is_some()) == Some(false) => - { - T::union(field.into(), None) - } - _ => return None, - }) -} - -pub(crate) fn fold_simple_type(nir: &Nir) -> Option -where - T: SimpleTypeFolder, -{ - // TODO: avoid unnecessary allocations - Some(match nir.kind() { - NirKind::BuiltinType(b) => match b { - Builtin::Bool => T::bool(), - Builtin::Natural => T::natural(), - Builtin::Integer => T::integer(), - Builtin::Double => T::double(), - Builtin::Text => T::text(), - _ => unreachable!(), - }, - NirKind::OptionalType(t) => T::optional(fold_simple_type(t)?), - NirKind::ListType(t) => T::list(fold_simple_type(t)?), - NirKind::RecordType(kts) => T::record( - kts.iter() - .map(|(k, v)| Some((k.into(), fold_simple_type(v)?))) - .collect::>>()? - .into_iter(), - ), - NirKind::UnionType(kts) => T::union( - kts.iter() - .map(|(k, v)| { - Some(( - k.into(), - v.as_ref() - .map(|v| Ok(fold_simple_type(v)?)) - .transpose()?, - )) - }) - .collect::>>()? - .into_iter(), - ), - _ => return None, - }) -} - -impl SimpleValueFolder for SimpleValue { - fn num(x: NumKind) -> Self { - SimpleValue::new(SValKind::Num(x)) - } - fn text(x: String) -> Self { - SimpleValue::new(SValKind::Text(x)) - } - fn optional(x: Option) -> Self { - SimpleValue::new(SValKind::Optional(x)) - } - fn list(xs: impl Iterator) -> Self { - SimpleValue::new(SValKind::List(xs.collect())) - } - fn record(values: impl Iterator) -> Self { - SimpleValue::new(SValKind::Record(values.collect())) - } - fn union(variant: String, content: Option) -> Self { - SimpleValue::new(SValKind::Union(variant, content)) - } -} - impl SimpleValue { pub(crate) fn new(kind: SValKind) -> Self { SimpleValue { @@ -163,7 +44,37 @@ impl SimpleValue { } } pub(crate) fn from_nir(nir: &Nir) -> Option { - fold_simple_value::(nir) + Some(SimpleValue::new(match nir.kind() { + NirKind::Num(lit) => SValKind::Num(lit.clone()), + NirKind::TextLit(x) => SValKind::Text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), + NirKind::NEOptionalLit(x) => { + SValKind::Optional(Some(Self::from_nir(x)?)) + } + NirKind::EmptyListLit(_) => SValKind::List(vec![]), + NirKind::NEListLit(xs) => SValKind::List( + xs.iter() + .map(|v| Self::from_nir(v)) + .collect::>()?, + ), + NirKind::RecordLit(kvs) => SValKind::Record( + kvs.iter() + .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .collect::>()?, + ), + NirKind::UnionLit(field, x, _) => { + SValKind::Union(field.into(), Some(Self::from_nir(x)?)) + } + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + SValKind::Union(field.into(), None) + } + _ => return None, + })) } pub fn kind(&self) -> &SValKind { -- 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/error/builder.rs | 2 +- dhall/src/error/mod.rs | 14 ++++++------ dhall/src/lib.rs | 30 ++++++++++++------------ dhall/src/semantics/builtins.rs | 6 ++--- dhall/src/semantics/mod.rs | 8 +++---- dhall/src/semantics/nze/env.rs | 6 ++--- dhall/src/semantics/nze/mod.rs | 8 +++---- dhall/src/semantics/nze/nir.rs | 42 +++++++++++++++++----------------- dhall/src/semantics/nze/normalize.rs | 10 ++++---- dhall/src/semantics/nze/var.rs | 4 ++-- dhall/src/semantics/parse.rs | 10 ++++---- dhall/src/semantics/resolve/env.rs | 8 +++---- dhall/src/semantics/resolve/hir.rs | 8 +++---- dhall/src/semantics/resolve/mod.rs | 6 ++--- dhall/src/semantics/resolve/resolve.rs | 10 ++++---- dhall/src/semantics/tck/env.rs | 4 ++-- dhall/src/semantics/tck/mod.rs | 6 ++--- dhall/src/semantics/tck/tir.rs | 6 ++--- dhall/src/semantics/tck/typecheck.rs | 10 ++++---- dhall/src/simple.rs | 8 +++---- dhall/src/syntax/ast/expr.rs | 10 ++++---- dhall/src/syntax/ast/mod.rs | 2 +- dhall/src/syntax/ast/span.rs | 12 +++++----- dhall/src/syntax/ast/visitor.rs | 2 +- dhall/src/syntax/binary/decode.rs | 2 +- dhall/src/syntax/binary/encode.rs | 2 +- dhall/src/syntax/binary/mod.rs | 4 ++-- 27 files changed, 120 insertions(+), 120 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/error/builder.rs b/dhall/src/error/builder.rs index c0bacb5..3ee65fb 100644 --- a/dhall/src/error/builder.rs +++ b/dhall/src/error/builder.rs @@ -6,7 +6,7 @@ use annotate_snippets::{ use crate::syntax::{ParsedSpan, Span}; #[derive(Debug, Clone, Default)] -pub(crate) struct ErrorBuilder { +pub struct ErrorBuilder { title: FreeAnnotation, annotations: Vec, footer: Vec, diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index e28b98b..ef4d41f 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -4,7 +4,7 @@ use crate::semantics::resolve::{ImportLocation, ImportStack}; use crate::syntax::{Import, ParseError}; mod builder; -pub(crate) use builder::*; +pub use builder::*; pub type Result = std::result::Result; @@ -15,7 +15,7 @@ pub struct Error { #[derive(Debug)] #[non_exhaustive] -pub(crate) enum ErrorKind { +pub enum ErrorKind { IO(IOError), Parse(ParseError), Decode(DecodeError), @@ -25,7 +25,7 @@ pub(crate) enum ErrorKind { } #[derive(Debug)] -pub(crate) enum ImportError { +pub enum ImportError { Missing, MissingEnvVar, SanityCheck, @@ -53,21 +53,21 @@ pub struct TypeError { /// The specific type error #[derive(Debug)] -pub(crate) enum TypeMessage { +pub enum TypeMessage { Custom(String), } impl Error { - pub(crate) fn new(kind: ErrorKind) -> Self { + pub fn new(kind: ErrorKind) -> Self { Error { kind } } - pub(crate) fn kind(&self) -> &ErrorKind { + pub fn kind(&self) -> &ErrorKind { &self.kind } } impl TypeError { - pub(crate) fn new(message: TypeMessage) -> Self { + pub fn new(message: TypeMessage) -> Self { TypeError { message } } } 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()) } } diff --git a/dhall/src/semantics/builtins.rs b/dhall/src/semantics/builtins.rs index ef375b8..fa22bd0 100644 --- a/dhall/src/semantics/builtins.rs +++ b/dhall/src/semantics/builtins.rs @@ -14,7 +14,7 @@ use std::convert::TryInto; /// A partially applied builtin. /// Invariant: the evaluation of the given args must not be able to progress further #[derive(Debug, Clone)] -pub(crate) struct BuiltinClosure { +pub struct BuiltinClosure { env: NzEnv, b: Builtin, /// Arguments applied to the closure so far. @@ -43,7 +43,7 @@ impl BuiltinClosure { } } -pub(crate) fn rc(x: UnspannedExpr) -> Expr { +pub fn rc(x: UnspannedExpr) -> Expr { Expr::new(x, Span::Artificial) } @@ -103,7 +103,7 @@ macro_rules! make_type { }; } -pub(crate) fn type_of_builtin(b: Builtin) -> Hir { +pub fn type_of_builtin(b: Builtin) -> Hir { use Builtin::*; let expr = match b { Bool | Natural | Integer | Double | Text => make_type!(Type), diff --git a/dhall/src/semantics/mod.rs b/dhall/src/semantics/mod.rs index 87033c9..468d8b1 100644 --- a/dhall/src/semantics/mod.rs +++ b/dhall/src/semantics/mod.rs @@ -3,7 +3,7 @@ pub mod nze; pub mod parse; pub mod resolve; pub mod tck; -pub(crate) use self::builtins::*; -pub(crate) use self::nze::*; -pub(crate) use self::resolve::*; -pub(crate) use self::tck::*; +pub use self::builtins::*; +pub use self::nze::*; +pub use self::resolve::*; +pub use self::tck::*; diff --git a/dhall/src/semantics/nze/env.rs b/dhall/src/semantics/nze/env.rs index ef2bee6..ec99dbe 100644 --- a/dhall/src/semantics/nze/env.rs +++ b/dhall/src/semantics/nze/env.rs @@ -1,7 +1,7 @@ use crate::semantics::{AlphaVar, Nir, NirKind}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum NzVar { +pub enum NzVar { /// Reverse-debruijn index: counts number of binders from the bottom of the stack. Bound(usize), /// Fake fresh variable generated for expression equality checking. @@ -17,11 +17,11 @@ enum EnvItem { } #[derive(Debug, Clone)] -pub(crate) struct ValEnv { +pub struct ValEnv { items: Vec>, } -pub(crate) type NzEnv = ValEnv<()>; +pub type NzEnv = ValEnv<()>; impl NzVar { pub fn new(idx: usize) -> Self { diff --git a/dhall/src/semantics/nze/mod.rs b/dhall/src/semantics/nze/mod.rs index 2648339..23022e0 100644 --- a/dhall/src/semantics/nze/mod.rs +++ b/dhall/src/semantics/nze/mod.rs @@ -3,7 +3,7 @@ pub mod lazy; pub mod nir; pub mod normalize; pub mod var; -pub(crate) use env::*; -pub(crate) use nir::*; -pub(crate) use normalize::*; -pub(crate) use var::*; +pub use env::*; +pub use nir::*; +pub use normalize::*; +pub use var::*; diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs index 5fccbf1..e0d227e 100644 --- a/dhall/src/semantics/nze/nir.rs +++ b/dhall/src/semantics/nze/nir.rs @@ -19,7 +19,7 @@ use crate::ToExprOptions; /// normalize as needed. /// Stands for "Normalized intermediate representation" #[derive(Clone)] -pub(crate) struct Nir(Rc); +pub struct Nir(Rc); #[derive(Debug)] struct NirInternal { @@ -28,7 +28,7 @@ struct NirInternal { /// An unevaluated subexpression #[derive(Debug, Clone)] -pub(crate) enum Thunk { +pub enum Thunk { /// A completely unnormalized expression. Thunk { env: NzEnv, body: Hir }, /// A partially normalized expression that may need to go through `normalize_one_layer`. @@ -37,7 +37,7 @@ pub(crate) enum Thunk { /// An unevaluated subexpression that takes an argument. #[derive(Debug, Clone)] -pub(crate) enum Closure { +pub enum Closure { /// Normal closure Closure { env: NzEnv, body: Hir }, /// Closure that ignores the argument passed @@ -48,7 +48,7 @@ pub(crate) enum Closure { // Invariant: this must not contain interpolations that are themselves TextLits, and contiguous // text values must be merged. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct TextLit(Vec>); +pub struct TextLit(Vec>); /// This represents a value in Weak Head Normal Form (WHNF). This means that the value is /// normalized up to the first constructor, but subexpressions may not be fully normalized. @@ -58,7 +58,7 @@ pub(crate) struct TextLit(Vec>); /// In particular, this means that once we get a `NirKind`, it can be considered immutable, and /// we only need to recursively normalize its sub-`Nir`s to get to the NF. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum NirKind { +pub enum NirKind { /// Closures LamClosure { binder: Binder, @@ -97,34 +97,34 @@ pub(crate) enum NirKind { impl Nir { /// Construct a Nir from a completely unnormalized expression. - pub(crate) fn new_thunk(env: NzEnv, hir: Hir) -> Nir { + pub fn new_thunk(env: NzEnv, hir: Hir) -> Nir { NirInternal::from_thunk(Thunk::new(env, hir)).into_nir() } /// Construct a Nir from a partially normalized expression that's not in WHNF. - pub(crate) fn from_partial_expr(e: ExprKind) -> Nir { + pub fn from_partial_expr(e: ExprKind) -> Nir { // TODO: env let env = NzEnv::new(); NirInternal::from_thunk(Thunk::from_partial_expr(env, e)).into_nir() } /// Make a Nir from a NirKind - pub(crate) fn from_kind(v: NirKind) -> Nir { + pub fn from_kind(v: NirKind) -> Nir { NirInternal::from_whnf(v).into_nir() } - pub(crate) fn from_const(c: Const) -> Self { + pub fn from_const(c: Const) -> Self { let v = NirKind::Const(c); NirInternal::from_whnf(v).into_nir() } - pub(crate) fn from_builtin(b: Builtin) -> Self { + pub fn from_builtin(b: Builtin) -> Self { Self::from_builtin_env(b, &NzEnv::new()) } - pub(crate) fn from_builtin_env(b: Builtin, env: &NzEnv) -> Self { + pub fn from_builtin_env(b: Builtin, env: &NzEnv) -> Self { Nir::from_kind(NirKind::from_builtin_env(b, env.clone())) } - pub(crate) fn from_text(txt: impl ToString) -> Self { + pub fn from_text(txt: impl ToString) -> Self { Nir::from_kind(NirKind::TextLit(TextLit::from_text(txt.to_string()))) } - pub(crate) fn as_const(&self) -> Option { + pub fn as_const(&self) -> Option { match &*self.kind() { NirKind::Const(c) => Some(*c), _ => None, @@ -132,22 +132,22 @@ impl Nir { } /// This is what you want if you want to pattern-match on the value. - pub(crate) fn kind(&self) -> &NirKind { + pub fn kind(&self) -> &NirKind { self.0.kind() } - pub(crate) fn to_type(&self, u: impl Into) -> Type { + pub fn to_type(&self, u: impl Into) -> Type { Type::new(self.clone(), u.into()) } /// Converts a value back to the corresponding AST expression. - pub(crate) fn to_expr(&self, opts: ToExprOptions) -> Expr { + pub fn to_expr(&self, opts: ToExprOptions) -> Expr { self.to_hir_noenv().to_expr(opts) } - pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { + pub fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr { self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv) } - pub(crate) fn app(&self, v: Nir) -> Nir { + pub fn app(&self, v: Nir) -> Nir { Nir::from_kind(apply_any(self.clone(), v)) } @@ -286,14 +286,14 @@ impl NirInternal { } impl NirKind { - pub(crate) fn into_nir(self) -> Nir { + pub fn into_nir(self) -> Nir { Nir::from_kind(self) } - pub(crate) fn from_builtin(b: Builtin) -> NirKind { + pub fn from_builtin(b: Builtin) -> NirKind { NirKind::from_builtin_env(b, NzEnv::new()) } - pub(crate) fn from_builtin_env(b: Builtin, env: NzEnv) -> NirKind { + pub fn from_builtin_env(b: Builtin, env: NzEnv) -> NirKind { BuiltinClosure::new(b, env) } } diff --git a/dhall/src/semantics/nze/normalize.rs b/dhall/src/semantics/nze/normalize.rs index 46d8fb5..570e106 100644 --- a/dhall/src/semantics/nze/normalize.rs +++ b/dhall/src/semantics/nze/normalize.rs @@ -5,7 +5,7 @@ use crate::semantics::NzEnv; use crate::semantics::{Binder, Closure, Hir, HirKind, Nir, NirKind, TextLit}; use crate::syntax::{BinOp, ExprKind, InterpolatedTextContents, NumKind}; -pub(crate) fn apply_any(f: Nir, a: Nir) -> NirKind { +pub fn apply_any(f: Nir, a: Nir) -> NirKind { match f.kind() { NirKind::LamClosure { closure, .. } => closure.apply(a).kind().clone(), NirKind::AppliedBuiltin(closure) => closure.apply(a), @@ -16,7 +16,7 @@ pub(crate) fn apply_any(f: Nir, a: Nir) -> NirKind { } } -pub(crate) fn squash_textlit( +pub fn squash_textlit( elts: impl Iterator>, ) -> Vec> { use std::mem::replace; @@ -54,7 +54,7 @@ pub(crate) fn squash_textlit( ret } -pub(crate) fn merge_maps( +pub fn merge_maps( map1: &HashMap, map2: &HashMap, mut f: F, @@ -207,7 +207,7 @@ fn apply_binop<'a>(o: BinOp, x: &'a Nir, y: &'a Nir) -> Option> { } #[allow(clippy::cognitive_complexity)] -pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { +pub fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { use NirKind::{ EmptyListLit, EmptyOptionalLit, NEListLit, NEOptionalLit, Num, PartialExpr, RecordLit, RecordType, UnionConstructor, UnionLit, @@ -464,7 +464,7 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { } /// Normalize Hir into WHNF -pub(crate) fn normalize_hir_whnf(env: &NzEnv, hir: &Hir) -> NirKind { +pub fn normalize_hir_whnf(env: &NzEnv, hir: &Hir) -> NirKind { match hir.kind() { HirKind::Var(var) => env.lookup_val(*var), HirKind::Import(hir, _) => normalize_hir_whnf(env, hir), diff --git a/dhall/src/semantics/nze/var.rs b/dhall/src/semantics/nze/var.rs index 413c759..302dbb7 100644 --- a/dhall/src/semantics/nze/var.rs +++ b/dhall/src/semantics/nze/var.rs @@ -8,10 +8,10 @@ pub struct Binder { } impl Binder { - pub(crate) fn new(name: Label) -> Self { + pub fn new(name: Label) -> Self { Binder { name } } - pub(crate) fn to_label(&self) -> Label { + pub fn to_label(&self) -> Label { self.clone().into() } } diff --git a/dhall/src/semantics/parse.rs b/dhall/src/semantics/parse.rs index 45860d0..2326471 100644 --- a/dhall/src/semantics/parse.rs +++ b/dhall/src/semantics/parse.rs @@ -9,33 +9,33 @@ use crate::syntax::binary; use crate::syntax::parse_expr; use crate::Parsed; -pub(crate) fn parse_file(f: &Path) -> Result { +pub fn parse_file(f: &Path) -> Result { let text = std::fs::read_to_string(f)?; let expr = parse_expr(&text)?; let root = ImportLocation::Local(f.to_owned()); Ok(Parsed(expr, root)) } -pub(crate) fn parse_remote(url: Url) -> Result { +pub fn parse_remote(url: Url) -> Result { let body = reqwest::blocking::get(url.clone()).unwrap().text().unwrap(); let expr = parse_expr(&body)?; let root = ImportLocation::Remote(url); Ok(Parsed(expr, root)) } -pub(crate) fn parse_str(s: &str) -> Result { +pub fn parse_str(s: &str) -> Result { let expr = parse_expr(s)?; let root = ImportLocation::Missing; Ok(Parsed(expr, root)) } -pub(crate) fn parse_binary(data: &[u8]) -> Result { +pub fn parse_binary(data: &[u8]) -> Result { let expr = binary::decode(data)?; let root = ImportLocation::Missing; Ok(Parsed(expr, root)) } -pub(crate) fn parse_binary_file(f: &Path) -> Result { +pub fn parse_binary_file(f: &Path) -> Result { let mut buffer = Vec::new(); File::open(f)?.read_to_end(&mut buffer)?; let expr = binary::decode(&buffer)?; diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index fe8c178..d5bb15b 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -6,16 +6,16 @@ use crate::syntax::{Label, V}; /// Environment for resolving names. #[derive(Debug, Clone)] -pub(crate) struct NameEnv { +pub struct NameEnv { names: Vec