From bb1f698c23a83f60020a72bb5be1f9a386c60d44 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 18 Apr 2019 11:58:21 +0200 Subject: Start cleaning up the mess of mapping functions --- dhall/src/normalize.rs | 1 - dhall_core/src/core.rs | 111 +++++++++++++++++++++++++++++++++++-------- dhall_core/src/visitor.rs | 42 +++++++++++++++- dhall_generator/src/quote.rs | 4 +- 4 files changed, 132 insertions(+), 26 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index e0ed96c..9110624 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -333,7 +333,6 @@ fn normalize_ref(expr: &Expr>) -> Expr { DoneAsIs => match expr.map_ref_simple(ExprF::roll) { e => e.map_ref( SubExpr::clone, - |_, e| e.clone(), X::clone, |_| unreachable!(), Label::clone, diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index bfe769f..24b4401 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -232,7 +232,6 @@ impl Expr { { self.map_ref( |x| rc(map_expr(x.as_ref())), - |_, x| rc(map_expr(x.as_ref())), map_note, map_embed, map_label, @@ -290,7 +289,19 @@ impl Expr { } impl ExprF { - pub fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F2, F3, F4, F5>( + pub fn traverse_ref_with_special_handling_of_binders< + 'a, + SE2, + L2, + N2, + E2, + Err, + F1, + F2, + F3, + F4, + F5, + >( &'a self, visit_subexpr: F1, visit_under_binder: F2, @@ -307,7 +318,7 @@ impl ExprF { F4: FnOnce(&'a E) -> Result, F5: FnMut(&'a L) -> Result, { - self.visit(visitor::TraverseRefVisitor { + self.visit(visitor::TraverseRefWithBindersVisitor { visit_subexpr, visit_under_binder, visit_note, @@ -316,12 +327,69 @@ impl ExprF { }) } - pub fn map_ref<'a, SE2, L2, N2, E2, F1, F2, F3, F4, F5>( + pub fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F3, F4, F5>( + &'a self, + visit_subexpr: F1, + visit_note: F3, + visit_embed: F4, + visit_label: F5, + ) -> Result, Err> + where + L: Ord, + L2: Ord, + F1: FnMut(&'a SE) -> Result, + F3: FnOnce(&'a N) -> Result, + F4: FnOnce(&'a E) -> Result, + F5: FnMut(&'a L) -> Result, + { + self.visit(visitor::TraverseRefVisitor { + visit_subexpr, + visit_note, + visit_embed, + visit_label, + }) + } + + pub fn map_ref<'a, SE2, L2, N2, E2, F1, F3, F4, F5>( + &'a self, + mut map_subexpr: F1, + map_note: F3, + map_embed: F4, + mut map_label: F5, + ) -> ExprF + where + L: Ord, + L2: Ord, + F1: FnMut(&'a SE) -> SE2, + F3: FnOnce(&'a N) -> N2, + F4: FnOnce(&'a E) -> E2, + F5: FnMut(&'a L) -> L2, + { + trivial_result(self.traverse_ref( + |x| Ok(map_subexpr(x)), + |x| Ok(map_note(x)), + |x| Ok(map_embed(x)), + |x| Ok(map_label(x)), + )) + } + + pub fn map_ref_with_special_handling_of_binders< + 'a, + SE2, + L2, + N2, + E2, + F1, + F2, + F3, + F4, + F5, + >( &'a self, mut map_subexpr: F1, mut map_under_binder: F2, - mut map_note: F3, - mut map_embed: F4, + map_note: F3, + map_embed: F4, mut map_label: F5, ) -> ExprF where @@ -329,11 +397,11 @@ impl ExprF { L2: Ord, F1: FnMut(&'a SE) -> SE2, F2: FnMut(&'a L, &'a SE) -> SE2, - F3: FnMut(&'a N) -> N2, - F4: FnMut(&'a E) -> E2, + F3: FnOnce(&'a N) -> N2, + F4: FnOnce(&'a E) -> E2, F5: FnMut(&'a L) -> L2, { - trivial_result(self.traverse_ref( + trivial_result(self.traverse_ref_with_special_handling_of_binders( |x| Ok(map_subexpr(x)), |l, x| Ok(map_under_binder(l, x)), |x| Ok(map_note(x)), @@ -375,7 +443,11 @@ impl SubExpr { self.0.as_ref() } - fn map_ref<'a, F1, F2>(&'a self, map_expr: F1, map_under_binder: F2) -> Self + pub fn map_subexprs_with_special_handling_of_binders<'a, F1, F2>( + &'a self, + map_expr: F1, + map_under_binder: F2, + ) -> Self where F1: FnMut(&'a Self) -> Self, F2: FnMut(&'a Label, &'a Self) -> Self, @@ -383,9 +455,13 @@ impl SubExpr { match self.as_ref() { ExprF::Embed(_) => SubExpr::clone(self), // Recursive call - ExprF::Note(_, e) => e.map_ref(map_expr, map_under_binder), + ExprF::Note(_, e) => e + .map_subexprs_with_special_handling_of_binders( + map_expr, + map_under_binder, + ), // Call ExprF::map_ref - e => rc(e.map_ref( + e => rc(e.map_ref_with_special_handling_of_binders( map_expr, map_under_binder, |_| unreachable!(), @@ -395,13 +471,6 @@ impl SubExpr { } } - pub fn map_ref_simple(&self, map_expr: F1) -> Self - where - F1: Fn(&Self) -> Self, - { - self.map_ref(&map_expr, |_, e| map_expr(e)) - } - pub fn unroll(&self) -> Expr where N: Clone, @@ -513,7 +582,7 @@ pub fn shift( use crate::ExprF::*; match in_expr.as_ref() { Var(v) => rc(Var(shift_var(delta, var, v))), - _ => in_expr.map_ref( + _ => in_expr.map_subexprs_with_special_handling_of_binders( |e| shift(delta, var, e), |l: &Label, e| { let vl = V(l.clone(), 0); @@ -541,7 +610,7 @@ pub fn subst_shift( match in_expr.as_ref() { Var(v) if v == var => SubExpr::clone(value), Var(v) => rc(Var(shift_var(-1, var, v))), - _ => in_expr.map_ref( + _ => in_expr.map_subexprs_with_special_handling_of_binders( |e| subst_shift(var, &value, e), |l: &Label, e| { let vl = V(l.clone(), 0); diff --git a/dhall_core/src/visitor.rs b/dhall_core/src/visitor.rs index c123275..c6b7321 100644 --- a/dhall_core/src/visitor.rs +++ b/dhall_core/src/visitor.rs @@ -302,7 +302,7 @@ where } } -pub struct TraverseRefVisitor { +pub struct TraverseRefWithBindersVisitor { pub visit_subexpr: F1, pub visit_under_binder: F2, pub visit_note: F3, @@ -312,7 +312,7 @@ pub struct TraverseRefVisitor { impl<'a, SE, L, N, E, SE2, L2, N2, E2, Err, F1, F2, F3, F4, F5> ExprFFallibleVisitor<'a, SE, SE2, L, L2, N, N2, E, E2> - for TraverseRefVisitor + for TraverseRefWithBindersVisitor where SE: 'a, L: 'a, @@ -349,6 +349,44 @@ where } } +pub struct TraverseRefVisitor { + pub visit_subexpr: F1, + pub visit_note: F2, + pub visit_embed: F3, + pub visit_label: F4, +} + +impl<'a, SE, L, N, E, SE2, L2, N2, E2, Err, F1, F2, F3, F4> + ExprFFallibleVisitor<'a, SE, SE2, L, L2, N, N2, E, E2> + for TraverseRefVisitor +where + SE: 'a, + L: 'a, + N: 'a, + E: 'a, + L: Ord, + L2: Ord, + F1: FnMut(&'a SE) -> Result, + F2: FnOnce(&'a N) -> Result, + F3: FnOnce(&'a E) -> Result, + F4: FnMut(&'a L) -> Result, +{ + type Error = Err; + + fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result { + (self.visit_subexpr)(subexpr) + } + fn visit_note(self, note: &'a N) -> Result { + (self.visit_note)(note) + } + fn visit_embed(self, embed: &'a E) -> Result { + (self.visit_embed)(embed) + } + fn visit_label(&mut self, label: &'a L) -> Result { + (self.visit_label)(label) + } +} + pub struct TraverseRefSimpleVisitor { pub visit_subexpr: F1, } diff --git a/dhall_generator/src/quote.rs b/dhall_generator/src/quote.rs index c335666..7128511 100644 --- a/dhall_generator/src/quote.rs +++ b/dhall_generator/src/quote.rs @@ -103,7 +103,7 @@ fn quote_subexpr( ctx: &Context, ) -> TokenStream { use dhall_core::ExprF::*; - match expr.as_ref().map_ref( + match expr.as_ref().map_ref_with_special_handling_of_binders( |e| quote_subexpr(e, ctx), |l, e| quote_subexpr(e, &ctx.insert(l.clone(), ())), |_| unreachable!(), @@ -138,7 +138,7 @@ fn quote_subexpr( // to be of type SubExpr<_, _>. fn quote_expr(expr: &Expr, ctx: &Context) -> TokenStream { use dhall_core::ExprF::*; - match expr.map_ref( + match expr.map_ref_with_special_handling_of_binders( |e| quote_subexpr(e, ctx), |l, e| quote_subexpr(e, &ctx.insert(l.clone(), ())), |_| unreachable!(), -- cgit v1.2.3 From 546fc00e893ba214b7731c95cfaf493644b50447 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 18 Apr 2019 12:01:21 +0200 Subject: Consistent type variable naming --- dhall_core/src/core.rs | 67 +++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 24b4401..a233d67 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -213,21 +213,21 @@ impl ExprF { } } -impl Expr { - pub fn map_shallow( +impl Expr { + pub fn map_shallow( &self, map_expr: F1, map_note: F2, map_embed: F3, map_label: F4, - ) -> Expr + ) -> Expr where - A: Clone, - T: Clone, - S: Clone, - F1: Fn(&Self) -> Expr, - F2: Fn(&S) -> T, - F3: Fn(&A) -> B, + E: Clone, + N2: Clone, + N: Clone, + F1: Fn(&Self) -> Expr, + F2: Fn(&N) -> N2, + F3: Fn(&E) -> E2, F4: Fn(&Label) -> Label, { self.map_ref( @@ -238,42 +238,43 @@ impl Expr { ) } - pub fn map_embed(&self, map_embed: &F) -> Expr + pub fn map_embed(&self, map_embed: &F) -> Expr where - A: Clone, - S: Clone, - F: Fn(&A) -> B, + E: Clone, + N: Clone, + F: Fn(&E) -> E2, { - let recurse = |e: &Expr| -> Expr { e.map_embed(map_embed) }; - self.map_shallow(recurse, S::clone, map_embed, Label::clone) + let recurse = + |e: &Expr| -> Expr { e.map_embed(map_embed) }; + self.map_shallow(recurse, N::clone, map_embed, Label::clone) } - pub fn traverse_embed( + pub fn traverse_embed( &self, map_embed: F, - ) -> Result, Err> + ) -> Result, Err> where - S: Clone, - B: Clone, - F: FnMut(&A) -> Result, + N: Clone, + E2: Clone, + F: FnMut(&E) -> Result, { self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) } pub fn map_label(&self, map_label: &F) -> Self where - A: Clone, - S: Clone, + E: Clone, + N: Clone, F: Fn(&Label) -> Label, { let recurse = |e: &Self| -> Self { e.map_label(map_label) }; - self.map_shallow(recurse, S::clone, A::clone, map_label) + self.map_shallow(recurse, N::clone, E::clone, map_label) } - pub fn roll(&self) -> SubExpr + pub fn roll(&self) -> SubExpr where - S: Clone, - A: Clone, + N: Clone, + E: Clone, { rc(ExprF::clone(self)) } @@ -574,11 +575,11 @@ fn shift_var(delta: isize, var: &V