diff options
author | Nadrieril | 2019-04-18 14:39:56 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-18 14:39:56 +0200 |
commit | 9474d4939db6f844285182fc15aad74f6aa18e21 (patch) | |
tree | 7f14c06afccd10e32c3003555bce809e713d2bfe | |
parent | 69267c7cc108a2f5db35c52a71afaa5be7be7355 (diff) |
Remove more duplication
-rw-r--r-- | dhall/src/expr.rs | 2 | ||||
-rw-r--r-- | dhall/src/imports.rs | 8 | ||||
-rw-r--r-- | dhall/src/normalize.rs | 18 | ||||
-rw-r--r-- | dhall/src/traits/dynamic_type.rs | 2 | ||||
-rw-r--r-- | dhall/src/typecheck.rs | 6 | ||||
-rw-r--r-- | dhall_core/src/core.rs | 66 | ||||
-rw-r--r-- | dhall_core/src/visitor.rs | 30 | ||||
-rw-r--r-- | dhall_generator/src/quote.rs | 8 |
8 files changed, 55 insertions, 85 deletions
diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 7c62a33..5885359 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -98,7 +98,7 @@ impl<'a> From<SubExpr<X, X>> for SimpleType<'a> { #[doc(hidden)] impl<'a> From<Normalized<'a>> for Typed<'a> { fn from(x: Normalized<'a>) -> Typed<'a> { - Typed(x.0.absurd(), x.1, x.2) + Typed(x.0.embed_absurd(), x.1, x.2) } } diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index b6dd80b..e367725 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -98,8 +98,8 @@ fn do_resolve_expr<'a>( } } }; - let expr = expr.as_ref().traverse_embed(resolve)?; - Ok(Resolved(rc(expr))) + let expr = expr.traverse_embed(resolve)?; + Ok(Resolved(expr)) } fn skip_resolve_expr( @@ -109,8 +109,8 @@ fn skip_resolve_expr( |import: &Import| -> Result<Normalized<'static>, ImportError> { Err(ImportError::UnexpectedImport(import.clone())) }; - let expr = expr.as_ref().traverse_embed(&resolve)?; - Ok(Resolved(rc(expr))) + let expr = expr.traverse_embed(resolve)?; + Ok(Resolved(expr)) } impl<'a> Parsed<'a> { diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 9110624..a54e48e 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -325,19 +325,17 @@ fn normalize_ref(expr: &Expr<X, Normalized<'static>>) -> Expr<X, X> { }; match what_next { - Continue(e) => normalize_ref(&e.absurd_rec()), - ContinueSub(e) => normalize_ref(e.absurd().as_ref()), + Continue(e) => normalize_ref(&e.embed_absurd()), + ContinueSub(e) => normalize_ref(e.embed_absurd().as_ref()), Done(e) => e, DoneRef(e) => e.clone(), DoneRefSub(e) => e.unroll(), - DoneAsIs => match expr.map_ref_simple(ExprF::roll) { - e => e.map_ref( - SubExpr::clone, - X::clone, - |_| unreachable!(), - Label::clone, - ), - }, + DoneAsIs => expr.map_ref_simple(ExprF::roll).map_ref( + SubExpr::clone, + X::clone, + |_| unreachable!(), + Label::clone, + ), } } diff --git a/dhall/src/traits/dynamic_type.rs b/dhall/src/traits/dynamic_type.rs index d2a5414..d4faf45 100644 --- a/dhall/src/traits/dynamic_type.rs +++ b/dhall/src/traits/dynamic_type.rs @@ -35,7 +35,7 @@ impl<'a> DynamicType for Normalized<'a> { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( &Context::new(), - self.0.absurd(), + self.0.embed_absurd(), TypeMessage::Untyped, )), } diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index de3899c..5aaeb08 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -496,7 +496,7 @@ fn type_last_layer( PhantomData ))) ); - let tx = mktype(ctx, tx.absurd())?; + let tx = mktype(ctx, tx.embed_absurd())?; ensure_equal!(&tx, a.get_type()?, { let a = a.clone(); mkerr(TypeMismatch( @@ -511,7 +511,7 @@ fn type_last_layer( x.clone(), None, a.clone().normalize().embed(), - tb.absurd(), + tb.embed_absurd(), )), )?; } @@ -675,7 +675,7 @@ fn type_last_layer( } Field(r, x) => match r.get_type()?.unroll_ref()?.as_ref() { RecordType(kts) => match kts.get(&x) { - Some(t) => Ok(RetExpr(t.unroll().absurd_rec())), + Some(t) => Ok(RetExpr(t.unroll().embed_absurd())), None => Err(mkerr(MissingRecordField(x, r))), }, _ => { diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 158abe1..65762f2 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -205,14 +205,14 @@ pub enum ExprF<SubExpr, Label, Note, Embed> { } impl<SE, L, N, E> ExprF<SE, L, N, E> { - pub fn visit<'a, V, Return>(&'a self, v: V) -> Return + pub(crate) fn visit<'a, V, Return>(&'a self, v: V) -> Return where V: visitor::GenericVisitor<&'a ExprF<SE, L, N, E>, Return>, { v.visit(self) } - pub fn traverse_ref_with_special_handling_of_binders< + fn traverse_ref_with_special_handling_of_binders< 'a, SE2, L2, @@ -250,7 +250,7 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { }) } - pub fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F3, F4, F5>( + fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F3, F4, F5>( &'a self, visit_subexpr: F1, visit_note: F3, @@ -343,7 +343,12 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { E: Clone, F1: FnMut(&'a SE) -> Result<SE2, Err>, { - self.visit(visitor::TraverseRefSimpleVisitor { visit_subexpr }) + self.traverse_ref( + visit_subexpr, + |x| Ok(N::clone(x)), + |x| Ok(E::clone(x)), + |x| Ok(L::clone(x)), + ) } pub fn map_ref_simple<'a, SE2, F1>( @@ -351,49 +356,35 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { map_subexpr: F1, ) -> ExprF<SE2, L, N, E> where - L: Ord, - L: Clone, + L: Ord + Clone, N: Clone, E: Clone, F1: Fn(&'a SE) -> SE2, { - trivial_result(self.traverse_ref_simple(|x| Ok(map_subexpr(x)))) + self.map_ref(map_subexpr, N::clone, E::clone, L::clone) } } impl<N, E> Expr<N, E> { - pub fn traverse_embed<E2, Err, F>( + fn traverse_embed<E2, Err, F>( &self, - map_embed: F, + visit_embed: F, ) -> Result<Expr<N, E2>, Err> where N: Clone, F: FnMut(&E) -> Result<E2, Err>, { - self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) + self.visit(&mut visitor::TraverseEmbedVisitor(visit_embed)) } - pub fn map_embed<E2, F>(&self, mut map_embed: F) -> Expr<N, E2> + fn map_embed<E2, F>(&self, mut map_embed: F) -> Expr<N, E2> where - E: Clone, N: Clone, F: FnMut(&E) -> E2, { trivial_result(self.traverse_embed(|x| Ok(map_embed(x)))) } - pub fn map_label<F>(&self, map_label: &F) -> Self - where - E: Clone, - N: Clone, - F: Fn(&Label) -> 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> where N: Clone, @@ -420,10 +411,6 @@ impl<E: Clone> Expr<X, E> { } impl<N: Clone> Expr<N, X> { - // Deprecated, use embed_absurd instead - pub fn absurd_rec<E>(&self) -> Expr<N, E> { - self.embed_absurd() - } pub fn embed_absurd<E>(&self) -> Expr<N, E> { self.visit(&mut visitor::EmbedAbsurdVisitor) } @@ -434,6 +421,25 @@ impl<N, E> SubExpr<N, E> { self.0.as_ref() } + pub fn traverse_embed<E2, Err, F>( + &self, + visit_embed: F, + ) -> Result<SubExpr<N, E2>, Err> + where + N: Clone, + F: FnMut(&E) -> Result<E2, Err>, + { + Ok(rc(self.as_ref().traverse_embed(visit_embed)?)) + } + + pub fn map_embed<E2, F>(&self, map_embed: F) -> SubExpr<N, E2> + where + N: Clone, + F: FnMut(&E) -> E2, + { + rc(self.as_ref().map_embed(map_embed)) + } + pub fn map_subexprs_with_special_handling_of_binders<'a, F1, F2>( &'a self, map_expr: F1, @@ -480,10 +486,6 @@ impl<N, E> SubExpr<N, E> { } impl<N: Clone> SubExpr<N, X> { - // Deprecated, use embed_absurd instead - pub fn absurd<T>(&self) -> SubExpr<N, T> { - self.embed_absurd() - } pub fn embed_absurd<T>(&self) -> SubExpr<N, T> { rc(self.as_ref().embed_absurd()) } diff --git a/dhall_core/src/visitor.rs b/dhall_core/src/visitor.rs index c2b0d06..16ad418 100644 --- a/dhall_core/src/visitor.rs +++ b/dhall_core/src/visitor.rs @@ -472,36 +472,6 @@ where } } -pub struct TraverseRefSimpleVisitor<F1> { - pub visit_subexpr: F1, -} - -impl<'a, SE, L, N, E, SE2, Err, F1> - ExprFFallibleVisitor<'a, SE, SE2, L, L, N, N, E, E> - for TraverseRefSimpleVisitor<F1> -where - SE: 'a, - L: Ord + Clone + 'a, - N: Clone + 'a, - E: Clone + 'a, - F1: FnMut(&'a SE) -> Result<SE2, Err>, -{ - type Error = Err; - - fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result<SE2, Self::Error> { - (self.visit_subexpr)(subexpr) - } - fn visit_note(self, note: &'a N) -> Result<N, Self::Error> { - Ok(N::clone(note)) - } - fn visit_embed(self, embed: &'a E) -> Result<E, Self::Error> { - Ok(E::clone(embed)) - } - fn visit_label(&mut self, label: &'a L) -> Result<L, Self::Error> { - Ok(L::clone(label)) - } -} - pub struct TraverseEmbedVisitor<F1>(pub F1); impl<'a, 'b, N, E, E2, Err, F1> diff --git a/dhall_generator/src/quote.rs b/dhall_generator/src/quote.rs index d680288..400c12c 100644 --- a/dhall_generator/src/quote.rs +++ b/dhall_generator/src/quote.rs @@ -10,8 +10,8 @@ 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 output = quote_expr(&expr, &Context::new()); + let expr = expr.map_embed(no_import); + let output = quote_expr(&expr.unroll(), &Context::new()); output.into() } @@ -20,8 +20,8 @@ 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 output = quote_subexpr(&dhall_core::rc(expr), &Context::new()); + let expr = expr.map_embed(no_import); + let output = quote_subexpr(&expr, &Context::new()); output.into() } |