From e68e34f361a29e0661201ac3c93a711a9884833f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 18 Apr 2019 13:15:42 +0200 Subject: Reduce duplication between mapping functions --- dhall_core/src/core.rs | 90 +++++++++++++++++------------------------------ dhall_core/src/visitor.rs | 1 - 2 files changed, 33 insertions(+), 58 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index d0e908b..7f9fd14 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -273,9 +273,21 @@ impl ExprF { }) } - pub fn map_ref<'a, SE2, L2, N2, E2, F1, F3, F4, F5>( + 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, map_note: F3, map_embed: F4, mut map_label: F5, @@ -284,33 +296,23 @@ impl ExprF { L: Ord, L2: Ord, F1: FnMut(&'a SE) -> SE2, + F2: FnMut(&'a L, &'a SE) -> SE2, 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)), |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, - >( + pub fn map_ref<'a, SE2, L2, N2, E2, F1, F3, F4, F5>( &'a self, mut map_subexpr: F1, - mut map_under_binder: F2, map_note: F3, map_embed: F4, mut map_label: F5, @@ -319,14 +321,12 @@ impl ExprF { L: Ord, L2: Ord, F1: FnMut(&'a SE) -> SE2, - F2: FnMut(&'a L, &'a SE) -> SE2, F3: FnOnce(&'a N) -> N2, F4: FnOnce(&'a E) -> E2, F5: FnMut(&'a L) -> L2, { - trivial_result(self.traverse_ref_with_special_handling_of_binders( + trivial_result(self.traverse_ref( |x| Ok(map_subexpr(x)), - |l, x| Ok(map_under_binder(l, x)), |x| Ok(map_note(x)), |x| Ok(map_embed(x)), |x| Ok(map_label(x)), @@ -362,51 +362,24 @@ impl ExprF { } impl Expr { - pub fn map_shallow( + pub fn traverse_embed( &self, - map_expr: F1, - map_note: F2, - map_embed: F3, - map_label: F4, - ) -> Expr + map_embed: F, + ) -> Result, Err> where - E: Clone, - N2: Clone, N: Clone, - F1: Fn(&Self) -> Expr, - F2: Fn(&N) -> N2, - F3: Fn(&E) -> E2, - F4: Fn(&Label) -> Label, + F: FnMut(&E) -> Result, { - self.map_ref( - |x| rc(map_expr(x.as_ref())), - map_note, - map_embed, - map_label, - ) + self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) } - pub fn map_embed(&self, map_embed: &F) -> Expr + pub fn map_embed(&self, mut map_embed: F) -> Expr where E: Clone, N: Clone, - F: Fn(&E) -> E2, + F: FnMut(&E) -> E2, { - let recurse = - |e: &Expr| -> Expr { e.map_embed(map_embed) }; - self.map_shallow(recurse, N::clone, map_embed, Label::clone) - } - - pub fn traverse_embed( - &self, - map_embed: F, - ) -> Result, Err> - where - N: Clone, - E2: Clone, - F: FnMut(&E) -> Result, - { - self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) + trivial_result(self.traverse_embed(|x| Ok(map_embed(x)))) } pub fn map_label(&self, map_label: &F) -> Self @@ -415,8 +388,10 @@ impl Expr { N: Clone, F: Fn(&Label) -> Label, { - let recurse = |e: &Self| -> Self { e.map_label(map_label) }; - self.map_shallow(recurse, N::clone, E::clone, map_label) + let recurse = |e: &SubExpr| -> SubExpr { + rc(e.as_ref().map_label(map_label)) + }; + self.map_ref(recurse, N::clone, E::clone, map_label) } pub fn roll(&self) -> SubExpr @@ -447,11 +422,12 @@ impl Expr { impl Expr { // Deprecated, use embed_absurd instead - pub fn absurd_rec(&self) -> Expr { + pub fn absurd_rec(&self) -> Expr { self.embed_absurd() } - pub fn embed_absurd(&self) -> Expr { + pub fn embed_absurd(&self) -> Expr { self.visit(&mut visitor::EmbedAbsurdVisitor) + // self.visit(&mut visitor::SquashEmbedVisitor(|e| match *e {})) } } diff --git a/dhall_core/src/visitor.rs b/dhall_core/src/visitor.rs index c6b7321..053932d 100644 --- a/dhall_core/src/visitor.rs +++ b/dhall_core/src/visitor.rs @@ -433,7 +433,6 @@ impl<'a, 'b, N, E, E2, Err, F1> > for &'b mut TraverseEmbedVisitor where N: Clone + 'a, - E2: Clone, F1: FnMut(&E) -> Result, { type Error = Err; -- cgit v1.2.3