From fd992e35ab1f61db59ad3a17a4e1795d1f216432 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 15:02:12 +0200 Subject: Transform more mapping functions to use the Visitor trait --- dhall/src/normalize.rs | 2 +- dhall_core/src/core.rs | 94 +++++++--------------------- dhall_core/src/visitor.rs | 152 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 74 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 49812c9..debf277 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -12,7 +12,7 @@ impl<'a> Typed<'a> { #[allow(dead_code)] pub fn skip_normalize(self) -> Normalized<'a> { Normalized( - self.0.unroll().squash_embed(&|e| e.0.clone()), + self.0.unroll().squash_embed(|e| e.0.clone()), self.1, self.2, ) diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index fe53bb2..2c23ed1 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use std::rc::Rc; -use crate::visitor::{TraverseRefSimpleVisitor, TraverseRefVisitor}; +use crate::visitor; use crate::*; pub type Integer = isize; @@ -236,23 +236,14 @@ impl Expr { pub fn traverse_embed( &self, - map_embed: &F, + map_embed: F, ) -> Result, Err> where S: Clone, B: Clone, - F: Fn(&A) -> Result, + F: FnMut(&A) -> Result, { - let recurse = |e: &SubExpr| -> Result, Err> { - Ok(e.as_ref().traverse_embed(map_embed)?.roll()) - }; - self.traverse_ref( - |e| recurse(e), - |_, e| recurse(e), - |x| Ok(S::clone(x)), - map_embed, - |x| Ok(Label::clone(x)), - ) + self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) } pub fn map_label(&self, map_label: &F) -> Self @@ -277,20 +268,11 @@ impl Expr { impl Expr { pub fn squash_embed( &self, - f: &impl Fn(&E) -> SubExpr, + f: impl FnMut(&E) -> SubExpr, ) -> SubExpr { - match self.as_ref() { - ExprF::Embed(e) => f(e), - _ => self - .map_ref( - |e| e.as_ref().squash_embed(f), - |_, e| e.as_ref().squash_embed(f), - N::clone, - |_| unreachable!(), - Label::clone, - ) - .roll(), - } + rc(trivial_result( + self.visit(&mut visitor::SquashEmbedVisitor(f)), + )) } } @@ -359,7 +341,7 @@ impl ExprF { F4: FnOnce(&'a E) -> Result, F5: FnMut(&'a L) -> Result, { - self.visit(TraverseRefVisitor { + self.visit(visitor::TraverseRefVisitor { visit_subexpr, visit_under_binder, visit_note, @@ -404,7 +386,7 @@ impl ExprF { E: Clone, F1: FnMut(&'a SE) -> Result, { - self.visit(TraverseRefSimpleVisitor { visit_subexpr }) + self.visit(visitor::TraverseRefSimpleVisitor { visit_subexpr }) } pub fn map_ref_simple<'a, SE2, F1>( @@ -517,65 +499,31 @@ impl SubExpr { impl SubExpr { pub fn note_absurd(&self) -> SubExpr { - rc(self.as_ref().map_ref( - |e| e.note_absurd(), - |_, e| e.note_absurd(), - |_| unreachable!(), - E::clone, - Label::clone, - )) + rc(self.as_ref().note_absurd()) } } impl Expr { - pub fn note_absurd(&self) -> Expr { - self.roll().note_absurd().unroll() + pub fn note_absurd(&self) -> Expr { + trivial_result(self.visit(&mut visitor::NoteAbsurdVisitor)) } } -impl SubExpr { +impl SubExpr { pub fn unnote(&self) -> SubExpr { - match self.as_ref() { - ExprF::Note(_, e) => e.unnote(), - e => rc(e.map_ref( - |e| e.unnote(), - |_, e| e.unnote(), - |_| unreachable!(), - E::clone, - Label::clone, - )), - } + rc(trivial_result( + self.as_ref().visit(&mut visitor::UnNoteVisitor), + )) } } impl Expr { - // This is all very sad and I hope this can be avoided sometime + // Deprecated, use embed_absurd instead pub fn absurd_rec(&self) -> Expr { - self.map_ref( - |e| e.absurd(), - |_, e| e.absurd(), - |_| unreachable!(), - |_| unreachable!(), - Label::clone, - ) + self.embed_absurd() } -} - -impl ExprF -where - SE: Clone, - L: Clone + Ord, - N: Clone, -{ - // When we know there is no Embed - pub fn absurd(&self) -> ExprF { - self.map_ref( - |e| e.clone(), - |_, e| e.clone(), - N::clone, - |_| unreachable!(), - L::clone, - ) + pub fn embed_absurd(&self) -> Expr { + trivial_result(self.visit(&mut visitor::EmbedAbsurdVisitor)) } } diff --git a/dhall_core/src/visitor.rs b/dhall_core/src/visitor.rs index 1b50e46..3a94c7b 100644 --- a/dhall_core/src/visitor.rs +++ b/dhall_core/src/visitor.rs @@ -224,3 +224,155 @@ where Ok(L::clone(label)) } } + +pub struct TraverseEmbedVisitor(pub F1); + +impl<'a, 'b, N, E, E2, Err, F1> + ExprVisitor<'a, SubExpr, SubExpr, Label, Label, N, N, E, E2> + for &'b mut TraverseEmbedVisitor +where + N: Clone + 'a, + E2: Clone, + F1: FnMut(&E) -> Result, +{ + type Error = Err; + + fn visit_subexpr( + &mut self, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + Ok(rc(subexpr.as_ref().visit(&mut **self)?)) + } + fn visit_note(self, note: &'a N) -> Result { + Ok(N::clone(note)) + } + fn visit_embed(self, embed: &'a E) -> Result { + (self.0)(embed) + } + fn visit_label(&mut self, label: &'a Label) -> Result { + Ok(Label::clone(label)) + } +} + +pub struct SquashEmbedVisitor(pub F1); + +impl<'a, 'b, N, E, E2, F1> + ExprVisitor<'a, SubExpr, SubExpr, Label, Label, N, N, E, E2> + for &'b mut SquashEmbedVisitor +where + N: Clone + 'a, + E2: Clone, + F1: FnMut(&E) -> SubExpr, +{ + type Error = X; + + fn visit_subexpr( + &mut self, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + Ok(rc(subexpr.as_ref().visit(&mut **self)?)) + } + fn visit_note(self, note: &'a N) -> Result { + Ok(N::clone(note)) + } + fn visit_embed(self, _: &'a E) -> Result { + unreachable!() + } + fn visit_embed_squash( + self, + embed: &'a E, + ) -> Result, Self::Error> { + Ok((self.0)(embed).unroll()) + } + fn visit_label(&mut self, label: &'a Label) -> Result { + Ok(Label::clone(label)) + } +} + +pub struct UnNoteVisitor; + +impl<'a, 'b, N, E> + ExprVisitor<'a, SubExpr, SubExpr, Label, Label, N, X, E, E> + for &'b mut UnNoteVisitor +where + E: Clone + 'a, +{ + type Error = X; + + fn visit_subexpr( + &mut self, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + Ok(rc(subexpr.as_ref().visit(&mut **self)?)) + } + fn visit_note(self, _: &'a N) -> Result { + unreachable!() + } + fn visit_note_squash( + self, + _: &'a N, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + subexpr.as_ref().visit(self) + } + fn visit_embed(self, embed: &'a E) -> Result { + Ok(E::clone(embed)) + } + fn visit_label(&mut self, label: &'a Label) -> Result { + Ok(Label::clone(label)) + } +} + +pub struct NoteAbsurdVisitor; + +impl<'a, 'b, N, E> + ExprVisitor<'a, SubExpr, SubExpr, Label, Label, X, N, E, E> + for &'b mut NoteAbsurdVisitor +where + E: Clone + 'a, +{ + type Error = X; + + fn visit_subexpr( + &mut self, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + Ok(rc(subexpr.as_ref().visit(&mut **self)?)) + } + fn visit_note(self, note: &'a X) -> Result { + match *note {} + } + fn visit_embed(self, embed: &'a E) -> Result { + Ok(E::clone(embed)) + } + fn visit_label(&mut self, label: &'a Label) -> Result { + Ok(Label::clone(label)) + } +} + +pub struct EmbedAbsurdVisitor; + +impl<'a, 'b, N, E> + ExprVisitor<'a, SubExpr, SubExpr, Label, Label, N, N, X, E> + for &'b mut EmbedAbsurdVisitor +where + N: Clone + 'a, +{ + type Error = X; + + fn visit_subexpr( + &mut self, + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { + Ok(rc(subexpr.as_ref().visit(&mut **self)?)) + } + fn visit_note(self, note: &'a N) -> Result { + Ok(N::clone(note)) + } + fn visit_embed(self, embed: &'a X) -> Result { + match *embed {} + } + fn visit_label(&mut self, label: &'a Label) -> Result { + Ok(Label::clone(label)) + } +} -- cgit v1.2.3