From fd9e37dfbc49e850a784fa3e907bd0a358d91e28 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 16 Apr 2019 16:16:37 +0200 Subject: Preserve notes when normalizing --- dhall/src/normalize.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index fa669d8..076f889 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -221,16 +221,20 @@ enum WhatNext<'a, N, E> { // The following expression is the normal form Done(Expr), DoneRef(&'a Expr), + DoneSub(SubExpr), DoneRefSub(&'a SubExpr), // The current expression is already in normal form DoneAsIs, } -fn normalize_ref(expr: &Expr>) -> Expr { +fn normalize_ref(expr: &Expr>) -> Expr +where + N: fmt::Debug + Clone, +{ use dhall_core::BinOp::*; use dhall_core::ExprF::*; // Recursively normalize all subexpressions - let expr: ExprF, Label, X, Normalized<'static>> = + let expr: ExprF, Label, N, Normalized<'static>> = expr.map_ref_simple(|e| normalize_ref(e.as_ref())); use WhatNext::*; @@ -241,7 +245,6 @@ fn normalize_ref(expr: &Expr>) -> Expr { ContinueSub(subst_shift(vf0, &r.roll(), &b.roll())) } Annot(x, _) => DoneRef(x), - Note(_, e) => DoneRef(e), App(f, args) if args.is_empty() => DoneRef(f), App(App(f, args1), args2) => Continue(App( f.clone(), @@ -303,7 +306,7 @@ fn normalize_ref(expr: &Expr>) -> Expr { .filter_map(|l| kvs.get(l).map(|x| (l.clone(), x.clone()))) .collect(), )), - Embed(e) => DoneRefSub(&e.0), + Embed(e) => DoneSub(e.0.note_absurd()), _ => DoneAsIs, }; @@ -312,12 +315,13 @@ fn normalize_ref(expr: &Expr>) -> Expr { ContinueSub(e) => normalize_ref(e.absurd().as_ref()), Done(e) => e, DoneRef(e) => e.clone(), + DoneSub(e) => e.unroll(), DoneRefSub(e) => e.unroll(), DoneAsIs => match expr.map_ref_simple(ExprF::roll) { e => e.map_ref( SubExpr::clone, |_, e| e.clone(), - X::clone, + N::clone, |_| unreachable!(), Label::clone, ), @@ -334,7 +338,10 @@ fn normalize_ref(expr: &Expr>) -> Expr { /// However, `normalize` will not fail if the expression is ill-typed and will /// leave ill-typed sub-expressions unevaluated. /// -fn normalize(e: SubExpr>) -> SubExpr { +fn normalize(e: SubExpr>) -> SubExpr +where + N: fmt::Debug + Clone, +{ normalize_ref(e.as_ref()).roll() } -- cgit v1.2.3