From 250525a05e1af17bb46ceb72879f471e54fb5091 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 2 Sep 2019 23:07:12 +0200 Subject: No need to change the type of Embed when resolving anymore --- dhall/src/phase/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'dhall') diff --git a/dhall/src/phase/mod.rs b/dhall/src/phase/mod.rs index 2c5505c..337ce3d 100644 --- a/dhall/src/phase/mod.rs +++ b/dhall/src/phase/mod.rs @@ -16,8 +16,8 @@ pub(crate) mod parse; pub(crate) mod resolve; pub(crate) mod typecheck; -pub type ParsedExpr = Expr; -pub type DecodedExpr = Expr; +pub type ParsedExpr = Expr; +pub type DecodedExpr = Expr; pub type ResolvedExpr = Expr; pub type NormalizedExpr = Expr; -- cgit v1.2.3 From 79595b1ae1a12fa2414d4b6c3c4bb2f7a0a9c094 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:24:07 +0200 Subject: Resolve imports by mutating Expr instead of cloning it --- dhall/src/phase/resolve.rs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'dhall') diff --git a/dhall/src/phase/resolve.rs b/dhall/src/phase/resolve.rs index a58f5e4..32e90c8 100644 --- a/dhall/src/phase/resolve.rs +++ b/dhall/src/phase/resolve.rs @@ -58,18 +58,16 @@ fn load_import( } fn do_resolve_expr( - Parsed(expr, root): Parsed, + parsed: Parsed, import_cache: &mut ImportCache, import_stack: &ImportStack, ) -> Result { - let resolve = |import: &Import| -> Result { - if import_stack.contains(import) { - return Err(ImportError::ImportCycle( - import_stack.clone(), - import.clone(), - )); + let Parsed(mut expr, root) = parsed; + let mut resolve = |import: Import| -> Result { + if import_stack.contains(&import) { + return Err(ImportError::ImportCycle(import_stack.clone(), import)); } - match import_cache.get(import) { + match import_cache.get(&import) { Some(expr) => Ok(expr.clone()), None => { // Copy the import stack and push the current import @@ -77,16 +75,20 @@ fn do_resolve_expr( import_stack.push(import.clone()); // Resolve the import recursively - let expr = - resolve_import(import, &root, import_cache, &import_stack)?; + let expr = resolve_import( + &import, + &root, + import_cache, + &import_stack, + )?; // Add the import to the cache - import_cache.insert(import.clone(), expr.clone()); + import_cache.insert(import, expr.clone()); Ok(expr) } } }; - let expr = expr.traverse_resolve(resolve)?; + expr.traverse_resolve_mut(&mut resolve)?; Ok(Resolved(expr)) } @@ -95,12 +97,13 @@ pub(crate) fn resolve(e: Parsed) -> Result { } pub(crate) fn skip_resolve_expr( - Parsed(expr, _root): Parsed, + parsed: Parsed, ) -> Result { - let resolve = |import: &Import| -> Result { - Err(ImportError::UnexpectedImport(import.clone())) + let mut expr = parsed.0; + let mut resolve = |import: Import| -> Result { + Err(ImportError::UnexpectedImport(import)) }; - let expr = expr.traverse_resolve(resolve)?; + expr.traverse_resolve_mut(&mut resolve)?; Ok(Resolved(expr)) } @@ -131,9 +134,9 @@ mod spec_tests { // import_success!(success_alternativeEnvNatural, "alternativeEnvNatural"); // import_success!(success_alternativeEnvSimple, "alternativeEnvSimple"); // import_success!(success_alternativeHashMismatch, "alternativeHashMismatch"); - // import_success!(success_alternativeNatural, "alternativeNatural"); - // import_success!(success_alternativeParseError, "alternativeParseError"); - // import_success!(success_alternativeTypeError, "alternativeTypeError"); + import_success!(success_alternativeNatural, "alternativeNatural"); + import_success!(success_alternativeParseError, "alternativeParseError"); + import_success!(success_alternativeTypeError, "alternativeTypeError"); // import_success!(success_asLocation, "asLocation"); // import_success!(success_asText, "asText"); // import_success!(success_customHeaders, "customHeaders"); -- cgit v1.2.3 From 31a03bf9140a2fdf5eb52d0998d2d41efaf0d610 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:43:45 +0200 Subject: Use map_mut in ValueF::normalize_mut --- dhall/src/core/value.rs | 11 ----------- dhall/src/core/valuef.rs | 7 ++----- 2 files changed, 2 insertions(+), 16 deletions(-) (limited to 'dhall') diff --git a/dhall/src/core/value.rs b/dhall/src/core/value.rs index 3cccb1d..b4b6b08 100644 --- a/dhall/src/core/value.rs +++ b/dhall/src/core/value.rs @@ -212,17 +212,6 @@ impl Value { WHNF | NF => {} } } - pub(crate) fn normalize_nf(&self) { - let borrow = self.as_internal(); - match borrow.form { - Unevaled | WHNF => { - drop(borrow); - self.as_internal_mut().normalize_nf(); - } - // Already in NF - NF => {} - } - } pub(crate) fn app(&self, v: Value) -> Value { let body_t = match &*self.get_type_not_sort().as_whnf() { diff --git a/dhall/src/core/valuef.rs b/dhall/src/core/valuef.rs index 7ecec86..7a2b51c 100644 --- a/dhall/src/core/valuef.rs +++ b/dhall/src/core/valuef.rs @@ -189,7 +189,7 @@ impl ValueF { for x in elts.iter_mut() { use InterpolatedTextContents::{Expr, Text}; match x { - Expr(n) => n.normalize_mut(), + Expr(v) => v.normalize_mut(), Text(_) => {} } } @@ -199,10 +199,7 @@ impl ValueF { y.normalize_mut(); } ValueF::PartialExpr(e) => { - // TODO: need map_mut - e.map_ref(|v| { - v.normalize_nf(); - }); + e.map_mut(Value::normalize_mut); } } } -- cgit v1.2.3 From f1c3d1d7487fbb18b228a1082fc1c966f34b6dc3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:44:02 +0200 Subject: Add mapping functions to InterpolatedTextContents --- dhall/src/core/valuef.rs | 22 ++++++---------------- dhall/src/core/var.rs | 12 ++---------- 2 files changed, 8 insertions(+), 26 deletions(-) (limited to 'dhall') diff --git a/dhall/src/core/valuef.rs b/dhall/src/core/valuef.rs index 7a2b51c..4e457e6 100644 --- a/dhall/src/core/valuef.rs +++ b/dhall/src/core/valuef.rs @@ -110,17 +110,11 @@ impl ValueF { ValueF::UnionConstructor(l.clone(), kts.clone()).to_expr(opts), v.to_expr(opts), )), - ValueF::TextLit(elts) => { - use InterpolatedTextContents::{Expr, Text}; - rc(ExprF::TextLit( - elts.iter() - .map(|contents| match contents { - Expr(e) => Expr(e.to_expr(opts)), - Text(s) => Text(s.clone()), - }) - .collect(), - )) - } + ValueF::TextLit(elts) => rc(ExprF::TextLit( + elts.iter() + .map(|contents| contents.map_ref(|e| e.to_expr(opts))) + .collect(), + )), ValueF::Equivalence(x, y) => rc(ExprF::BinOp( dhall_syntax::BinOp::Equivalence, x.to_expr(opts), @@ -187,11 +181,7 @@ impl ValueF { } ValueF::TextLit(elts) => { for x in elts.iter_mut() { - use InterpolatedTextContents::{Expr, Text}; - match x { - Expr(v) => v.normalize_mut(), - Text(_) => {} - } + x.map_mut(Value::normalize_mut); } } ValueF::Equivalence(x, y) => { diff --git a/dhall/src/core/var.rs b/dhall/src/core/var.rs index ce4d137..3795f10 100644 --- a/dhall/src/core/var.rs +++ b/dhall/src/core/var.rs @@ -224,11 +224,7 @@ where impl Shift for dhall_syntax::InterpolatedTextContents { fn shift(&self, delta: isize, var: &AlphaVar) -> Option { - use dhall_syntax::InterpolatedTextContents::{Expr, Text}; - Some(match self { - Expr(x) => Expr(x.shift(delta, var)?), - Text(s) => Text(s.clone()), - }) + Some(self.traverse_ref(|x| Ok(x.shift(delta, var)?))?) } } @@ -283,11 +279,7 @@ impl> Subst for Vec { impl> Subst for dhall_syntax::InterpolatedTextContents { fn subst_shift(&self, var: &AlphaVar, val: &S) -> Self { - use dhall_syntax::InterpolatedTextContents::{Expr, Text}; - match self { - Expr(x) => Expr(x.subst_shift(var, val)), - Text(s) => Text(s.clone()), - } + self.map_ref(|x| x.subst_shift(var, val)) } } -- cgit v1.2.3