summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadrieril2019-04-18 13:15:42 +0200
committerNadrieril2019-04-18 13:15:42 +0200
commite68e34f361a29e0661201ac3c93a711a9884833f (patch)
tree04be61c8effd19911f371200d58fc3bb46f32bc9
parentcb24b56d83479c0fd95bf4dd09879118f4e3afd6 (diff)
Reduce duplication between mapping functions
-rw-r--r--dhall_core/src/core.rs90
-rw-r--r--dhall_core/src/visitor.rs1
-rw-r--r--dhall_generator/src/quote.rs4
3 files changed, 35 insertions, 60 deletions
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<SE, L, N, E> ExprF<SE, L, N, E> {
})
}
- 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<SE, L, N, E> ExprF<SE, L, N, E> {
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<SE, L, N, E> ExprF<SE, L, N, E> {
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<SE, L, N, E> ExprF<SE, L, N, E> {
}
impl<N, E> Expr<N, E> {
- pub fn map_shallow<N2, E2, F1, F2, F3, F4>(
+ pub fn traverse_embed<E2, Err, F>(
&self,
- map_expr: F1,
- map_note: F2,
- map_embed: F3,
- map_label: F4,
- ) -> Expr<N2, E2>
+ map_embed: F,
+ ) -> Result<Expr<N, E2>, Err>
where
- E: Clone,
- N2: Clone,
N: Clone,
- F1: Fn(&Self) -> Expr<N2, E2>,
- F2: Fn(&N) -> N2,
- F3: Fn(&E) -> E2,
- F4: Fn(&Label) -> Label,
+ F: FnMut(&E) -> Result<E2, Err>,
{
- 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<E2, F>(&self, map_embed: &F) -> Expr<N, E2>
+ pub fn map_embed<E2, F>(&self, mut map_embed: F) -> Expr<N, E2>
where
E: Clone,
N: Clone,
- F: Fn(&E) -> E2,
+ F: FnMut(&E) -> E2,
{
- let recurse =
- |e: &Expr<N, E>| -> Expr<N, E2> { e.map_embed(map_embed) };
- self.map_shallow(recurse, N::clone, map_embed, Label::clone)
- }
-
- pub fn traverse_embed<E2, Err, F>(
- &self,
- map_embed: F,
- ) -> Result<Expr<N, E2>, Err>
- where
- N: Clone,
- E2: Clone,
- F: FnMut(&E) -> Result<E2, Err>,
- {
- self.visit(&mut visitor::TraverseEmbedVisitor(map_embed))
+ trivial_result(self.traverse_embed(|x| Ok(map_embed(x))))
}
pub fn map_label<F>(&self, map_label: &F) -> Self
@@ -415,8 +388,10 @@ impl<N, E> Expr<N, E> {
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<N, E>| -> SubExpr<N, E> {
+ rc(e.as_ref().map_label(map_label))
+ };
+ self.map_ref(recurse, N::clone, E::clone, map_label)
}
pub fn roll(&self) -> SubExpr<N, E>
@@ -447,11 +422,12 @@ impl<E: Clone> Expr<X, E> {
impl<N: Clone> Expr<N, X> {
// Deprecated, use embed_absurd instead
- pub fn absurd_rec<T>(&self) -> Expr<N, T> {
+ pub fn absurd_rec<E>(&self) -> Expr<N, E> {
self.embed_absurd()
}
- pub fn embed_absurd<T>(&self) -> Expr<N, T> {
+ pub fn embed_absurd<E>(&self) -> Expr<N, E> {
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<F1>
where
N: Clone + 'a,
- E2: Clone,
F1: FnMut(&E) -> Result<E2, Err>,
{
type Error = Err;
diff --git a/dhall_generator/src/quote.rs b/dhall_generator/src/quote.rs
index 7128511..d680288 100644
--- a/dhall_generator/src/quote.rs
+++ b/dhall_generator/src/quote.rs
@@ -10,7 +10,7 @@ pub fn expr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let expr: SubExpr<_, Import> = parse_expr(&input_str).unwrap().unnote();
let no_import =
|_: &Import| -> X { panic!("Don't use import in dhall::expr!()") };
- let expr = expr.as_ref().map_embed(&no_import);
+ let expr = expr.as_ref().map_embed(no_import);
let output = quote_expr(&expr, &Context::new());
output.into()
}
@@ -20,7 +20,7 @@ pub fn subexpr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let expr: SubExpr<_, Import> = parse_expr(&input_str).unwrap().unnote();
let no_import =
|_: &Import| -> X { panic!("Don't use import in dhall::subexpr!()") };
- let expr = expr.as_ref().map_embed(&no_import);
+ let expr = expr.as_ref().map_embed(no_import);
let output = quote_subexpr(&dhall_core::rc(expr), &Context::new());
output.into()
}