diff options
author | Nadrieril | 2019-05-04 14:58:18 +0200 |
---|---|---|
committer | Nadrieril | 2019-05-04 14:58:18 +0200 |
commit | 1b1c34b90bb4bf3859b05b1da6db2dcb374996bb (patch) | |
tree | a344a360f8735a3f33d05c04568d9449efa7ba4c | |
parent | b6f57069b75febf1d312a98efcd6544c9db2fe59 (diff) |
Move `Note`s into the spine of the AST
Diffstat (limited to '')
-rw-r--r-- | dhall/src/normalize.rs | 12 | ||||
-rw-r--r-- | dhall/src/typecheck.rs | 2 | ||||
-rw-r--r-- | dhall_proc_macros/src/quote.rs | 4 | ||||
-rw-r--r-- | dhall_syntax/src/core.rs | 69 | ||||
-rw-r--r-- | dhall_syntax/src/parser.rs | 2 | ||||
-rw-r--r-- | dhall_syntax/src/printer.rs | 10 | ||||
-rw-r--r-- | dhall_syntax/src/visitor.rs | 234 |
7 files changed, 86 insertions, 247 deletions
diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 4636859..4d87225 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -163,7 +163,7 @@ pub(crate) enum Value { UnionLit(Label, Thunk, BTreeMap<Label, Option<TypeThunk>>), TextLit(Vec<InterpolatedTextContents<Thunk>>), /// Invariant: This must not contain a value captured by one of the variants above. - PartialExpr(ExprF<Thunk, Label, X, X>), + PartialExpr(ExprF<Thunk, Label, X>), } impl Value { @@ -497,7 +497,6 @@ impl Value { |v| v.shift(delta, var), |x, v| v.shift(delta, &var.shift(1, &x.into())), X::clone, - X::clone, Label::clone, )) } @@ -607,7 +606,6 @@ impl Value { ) }, X::clone, - X::clone, Label::clone, )) } @@ -1125,11 +1123,10 @@ fn normalize_whnf(ctx: NormalizationContext, expr: InputSubExpr) -> Value { } // Thunk subexpressions - let expr: ExprF<Thunk, Label, X, X> = + let expr: ExprF<Thunk, Label, X> = expr.as_ref().map_ref_with_special_handling_of_binders( |e| Thunk::new(ctx.clone(), e.clone()), |x, e| Thunk::new(ctx.skip(x), e.clone()), - X::clone, |_| unreachable!(), Label::clone, ); @@ -1137,7 +1134,7 @@ fn normalize_whnf(ctx: NormalizationContext, expr: InputSubExpr) -> Value { normalize_one_layer(expr) } -fn normalize_one_layer(expr: ExprF<Thunk, Label, X, X>) -> Value { +fn normalize_one_layer(expr: ExprF<Thunk, Label, X>) -> Value { use Value::{ BoolLit, EmptyListLit, EmptyOptionalLit, Lam, NEListLit, NEOptionalLit, NaturalLit, Pi, RecordLit, RecordType, TextLit, UnionConstructor, @@ -1149,7 +1146,7 @@ fn normalize_one_layer(expr: ExprF<Thunk, Label, X, X>) -> Value { RetValue(Value), RetThunk(Thunk), RetThunkRef(&'a Thunk), - RetExpr(ExprF<Thunk, Label, X, X>), + RetExpr(ExprF<Thunk, Label, X>), } use Ret::{RetExpr, RetThunk, RetThunkRef, RetValue}; @@ -1157,7 +1154,6 @@ fn normalize_one_layer(expr: ExprF<Thunk, Label, X, X>) -> Value { ExprF::Embed(_) => unreachable!(), ExprF::Var(_) => unreachable!(), ExprF::Annot(x, _) => RetThunk(x), - ExprF::Note(_, e) => RetThunk(e), ExprF::Lam(x, t, e) => RetValue(Lam(x, t, e)), ExprF::Pi(x, t, e) => { RetValue(Pi(x, TypeThunk::from_thunk(t), TypeThunk::from_thunk(e))) diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 74de59b..ce4ca96 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -719,7 +719,7 @@ fn type_with( /// layer. fn type_last_layer( ctx: &TypecheckContext, - e: ExprF<Typed, Label, X, Normalized>, + e: ExprF<Typed, Label, Normalized>, ) -> Result<Ret, TypeError> { use dhall_syntax::BinOp::*; use dhall_syntax::Builtin::*; diff --git a/dhall_proc_macros/src/quote.rs b/dhall_proc_macros/src/quote.rs index c2323fa..e11bbaa 100644 --- a/dhall_proc_macros/src/quote.rs +++ b/dhall_proc_macros/src/quote.rs @@ -27,7 +27,7 @@ pub fn subexpr(input: proc_macro::TokenStream) -> proc_macro::TokenStream { // Returns an expression of type ExprF<T, _, _>, where T is the // type of the subexpressions after interpolation. -pub fn quote_exprf<TS>(expr: ExprF<TS, Label, X, X>) -> TokenStream +pub fn quote_exprf<TS>(expr: ExprF<TS, Label, X>) -> TokenStream where TS: quote::ToTokens + std::fmt::Debug, { @@ -103,7 +103,6 @@ fn quote_subexpr( |e| quote_subexpr(e, ctx), |l, e| quote_subexpr(e, &ctx.insert(l.clone(), ())), |_| unreachable!(), - |_| unreachable!(), Label::clone, ) { Var(V(ref s, n)) => { @@ -138,7 +137,6 @@ fn quote_expr(expr: &Expr<X, X>, ctx: &Context<Label, ()>) -> TokenStream { |e| quote_subexpr(e, ctx), |l, e| quote_subexpr(e, &ctx.insert(l.clone(), ())), |_| unreachable!(), - |_| unreachable!(), Label::clone, ) { Var(V(ref s, n)) => { diff --git a/dhall_syntax/src/core.rs b/dhall_syntax/src/core.rs index 3db07dd..a81f96c 100644 --- a/dhall_syntax/src/core.rs +++ b/dhall_syntax/src/core.rs @@ -141,17 +141,25 @@ pub type ParsedExpr = SubExpr<X, Import>; pub type ResolvedExpr = SubExpr<X, X>; pub type DhallExpr = ResolvedExpr; -#[derive(Debug, PartialEq, Eq)] -pub struct SubExpr<Note, Embed>(pub Rc<Expr<Note, Embed>>); +#[derive(Debug)] +pub struct SubExpr<Note, Embed>(Rc<Expr<Note, Embed>>, Option<Note>); -pub type Expr<Note, Embed> = ExprF<SubExpr<Note, Embed>, Label, Note, Embed>; +impl<Note, Embed: PartialEq> std::cmp::PartialEq for SubExpr<Note, Embed> { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl<Note, Embed: Eq> std::cmp::Eq for SubExpr<Note, Embed> {} + +pub type Expr<Note, Embed> = ExprF<SubExpr<Note, Embed>, Label, Embed>; /// Syntax tree for expressions // Having the recursion out of the enum definition enables writing // much more generic code and improves pattern-matching behind // smart pointers. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum ExprF<SubExpr, Label, Note, Embed> { +pub enum ExprF<SubExpr, Label, Embed> { Const(Const), /// `x` /// `x@n` @@ -208,28 +216,25 @@ pub enum ExprF<SubExpr, Label, Note, Embed> { Field(SubExpr, Label), /// `e.{ x, y, z }` Projection(SubExpr, Vec<Label>), - /// Annotation on the AST. Unused for now but could hold e.g. file location information - Note(Note, SubExpr), /// Embeds an import or the result of resolving the import Embed(Embed), } -impl<SE, L, N, E> ExprF<SE, L, N, E> { +impl<SE, L, E> ExprF<SE, L, E> { pub(crate) fn visit<'a, V, Return>(&'a self, v: V) -> Return where - V: visitor::GenericVisitor<&'a ExprF<SE, L, N, E>, Return>, + V: visitor::GenericVisitor<&'a ExprF<SE, L, E>, Return>, { v.visit(self) } - fn traverse_ref_with_special_handling_of_binders<'a, SE2, L2, N2, E2, Err>( + fn traverse_ref_with_special_handling_of_binders<'a, SE2, L2, E2, Err>( &'a self, visit_subexpr: impl FnMut(&'a SE) -> Result<SE2, Err>, visit_under_binder: impl FnOnce(&'a L, &'a SE) -> Result<SE2, Err>, - visit_note: impl FnOnce(&'a N) -> Result<N2, Err>, visit_embed: impl FnOnce(&'a E) -> Result<E2, Err>, visit_label: impl FnMut(&'a L) -> Result<L2, Err>, - ) -> Result<ExprF<SE2, L2, N2, E2>, Err> + ) -> Result<ExprF<SE2, L2, E2>, Err> where L: Ord, L2: Ord, @@ -237,39 +242,35 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { self.visit(visitor::TraverseRefWithBindersVisitor { visit_subexpr, visit_under_binder, - visit_note, visit_embed, visit_label, }) } - fn traverse_ref<'a, SE2, L2, N2, E2, Err>( + fn traverse_ref<'a, SE2, L2, E2, Err>( &'a self, visit_subexpr: impl FnMut(&'a SE) -> Result<SE2, Err>, - visit_note: impl FnOnce(&'a N) -> Result<N2, Err>, visit_embed: impl FnOnce(&'a E) -> Result<E2, Err>, visit_label: impl FnMut(&'a L) -> Result<L2, Err>, - ) -> Result<ExprF<SE2, L2, N2, E2>, Err> + ) -> Result<ExprF<SE2, L2, E2>, Err> where L: Ord, L2: Ord, { self.visit(visitor::TraverseRefVisitor { visit_subexpr, - visit_note, visit_embed, visit_label, }) } - pub fn map_ref_with_special_handling_of_binders<'a, SE2, L2, N2, E2>( + pub fn map_ref_with_special_handling_of_binders<'a, SE2, L2, E2>( &'a self, mut map_subexpr: impl FnMut(&'a SE) -> SE2, mut map_under_binder: impl FnMut(&'a L, &'a SE) -> SE2, - map_note: impl FnOnce(&'a N) -> N2, map_embed: impl FnOnce(&'a E) -> E2, mut map_label: impl FnMut(&'a L) -> L2, - ) -> ExprF<SE2, L2, N2, E2> + ) -> ExprF<SE2, L2, E2> where L: Ord, L2: Ord, @@ -277,26 +278,23 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { 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<'a, SE2, L2, N2, E2>( + pub fn map_ref<'a, SE2, L2, E2>( &'a self, mut map_subexpr: impl FnMut(&'a SE) -> SE2, - map_note: impl FnOnce(&'a N) -> N2, map_embed: impl FnOnce(&'a E) -> E2, mut map_label: impl FnMut(&'a L) -> L2, - ) -> ExprF<SE2, L2, N2, E2> + ) -> ExprF<SE2, L2, E2> where L: Ord, L2: Ord, { 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)), )) @@ -305,15 +303,13 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { pub fn traverse_ref_simple<'a, SE2, Err>( &'a self, visit_subexpr: impl FnMut(&'a SE) -> Result<SE2, Err>, - ) -> Result<ExprF<SE2, L, N, E>, Err> + ) -> Result<ExprF<SE2, L, E>, Err> where L: Ord + Clone, - N: Clone, E: Clone, { self.traverse_ref( visit_subexpr, - |x| Ok(N::clone(x)), |x| Ok(E::clone(x)), |x| Ok(L::clone(x)), ) @@ -322,13 +318,12 @@ impl<SE, L, N, E> ExprF<SE, L, N, E> { pub fn map_ref_simple<'a, SE2>( &'a self, map_subexpr: impl Fn(&'a SE) -> SE2, - ) -> ExprF<SE2, L, N, E> + ) -> ExprF<SE2, L, E> where L: Ord + Clone, - N: Clone, E: Clone, { - self.map_ref(map_subexpr, N::clone, E::clone, L::clone) + self.map_ref(map_subexpr, E::clone, L::clone) } } @@ -413,19 +408,11 @@ impl<N, E> SubExpr<N, E> { ) -> Self { match self.as_ref() { ExprF::Embed(_) => SubExpr::clone(self), - // Recursive call - // TODO: don't discard the note ! - ExprF::Note(_, e) => e - .map_subexprs_with_special_handling_of_binders( - map_expr, - map_under_binder, - ), - // Call ExprF::map_ref + // This calls ExprF::map_ref e => rc(e.map_ref_with_special_handling_of_binders( map_expr, map_under_binder, |_| unreachable!(), - |_| unreachable!(), Label::clone, )), } @@ -491,13 +478,13 @@ impl<E: Clone> SubExpr<X, E> { impl<N, E> Clone for SubExpr<N, E> { fn clone(&self) -> Self { - SubExpr(Rc::clone(&self.0)) + SubExpr(Rc::clone(&self.0), None) } } // Should probably rename this pub fn rc<N, E>(x: Expr<N, E>) -> SubExpr<N, E> { - SubExpr(Rc::new(x)) + SubExpr(Rc::new(x), None) } /// Add an isize to an usize diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 38e67a8..b5bf09d 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -29,7 +29,7 @@ fn rc(x: ParsedExpr) -> ParsedSubExpr { crate::rc(x) } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone)] pub struct Span { input: Rc<str>, /// # Safety diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 704000a..e3b180b 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -3,7 +3,7 @@ use itertools::Itertools; use std::fmt::{self, Display}; /// Generic instance that delegates to subexpressions -impl<SE: Display + Clone, N, E: Display> Display for ExprF<SE, Label, N, E> { +impl<SE: Display + Clone, E: Display> Display for ExprF<SE, Label, E> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use crate::ExprF::*; match self { @@ -102,7 +102,6 @@ impl<SE: Display + Clone, N, E: Display> Display for ExprF<SE, Label, N, E> { f.write_str(" >")? } Embed(a) => a.fmt(f)?, - Note(_, b) => b.fmt(f)?, } Ok(()) } @@ -143,7 +142,7 @@ impl<S: Clone, A: Display + Clone> Expr<S, A> { fn fmt_phase( &self, f: &mut fmt::Formatter, - mut phase: PrintPhase, + phase: PrintPhase, ) -> Result<(), fmt::Error> { use crate::ExprF::*; use PrintPhase::*; @@ -170,10 +169,6 @@ impl<S: Clone, A: Display + Clone> Expr<S, A> { _ => false, }; - if needs_paren { - phase = Base; - } - // Annotate subexpressions with the appropriate phase, defaulting to Base let phased_self = match self.map_ref_simple(|e| PhasedExpr(e, Base)) { Pi(a, b, c) => { @@ -200,7 +195,6 @@ impl<S: Clone, A: Display + Clone> Expr<S, A> { ExprF::App(f, a) => ExprF::App(f.phase(Import), a.phase(Import)), Field(a, b) => Field(a.phase(Primitive), b), Projection(e, ls) => Projection(e.phase(Primitive), ls), - Note(n, b) => Note(n, b.phase(phase)), e => e, }; diff --git a/dhall_syntax/src/visitor.rs b/dhall_syntax/src/visitor.rs index caaefce..cbaa21e 100644 --- a/dhall_syntax/src/visitor.rs +++ b/dhall_syntax/src/visitor.rs @@ -18,11 +18,10 @@ pub trait GenericVisitor<Input, Return>: Sized { /// or just one, and Rust is ok with either. See for example TraverseRefVisitor /// and TraverseEmbedVisitor. /// This is very generic. For a more legible trait, see ExprFInFallibleVisitor -pub trait ExprFVeryGenericVisitor<'a, Ret, SE1, L1, N1, E1>: Sized { +pub trait ExprFVeryGenericVisitor<'a, Ret, SE1, L1, E1>: Sized { type Error; type SE2; type L2; - type N2; type E2; fn visit_subexpr( @@ -39,27 +38,21 @@ pub trait ExprFVeryGenericVisitor<'a, Ret, SE1, L1, N1, E1>: Sized { fn visit_embed_squash(self, embed: &'a E1) -> Result<Ret, Self::Error>; - fn visit_note_squash( - self, - note: &'a N1, - subexpr: &'a SE1, - ) -> Result<Ret, Self::Error>; - - // Called with the result of the map, in the non-embed/note case. + // Called with the result of the map, in the non-embed case. // Useful to change the result type, and/or avoid some loss of info fn visit_resulting_exprf( - result: ExprF<Self::SE2, Self::L2, Self::N2, Self::E2>, + result: ExprF<Self::SE2, Self::L2, Self::E2>, ) -> Result<Ret, Self::Error>; } -impl<'a, T, Ret, SE1, L1, N1, E1> - GenericVisitor<&'a ExprF<SE1, L1, N1, E1>, Result<Ret, T::Error>> for T +impl<'a, T, Ret, SE1, L1, E1> + GenericVisitor<&'a ExprF<SE1, L1, E1>, Result<Ret, T::Error>> for T where L1: Ord, T::L2: Ord, - T: ExprFVeryGenericVisitor<'a, Ret, SE1, L1, N1, E1>, + T: ExprFVeryGenericVisitor<'a, Ret, SE1, L1, E1>, { - fn visit(self, input: &'a ExprF<SE1, L1, N1, E1>) -> Result<Ret, T::Error> { + fn visit(self, input: &'a ExprF<SE1, L1, E1>) -> Result<Ret, T::Error> { fn vec<'a, T, U, Err, F: FnMut(&'a T) -> Result<U, Err>>( x: &'a [T], f: F, @@ -75,27 +68,27 @@ where None => None, }) } - fn btmap<'a, V, Ret, SE, L, N, E>( + fn btmap<'a, V, Ret, SE, L, E>( x: &'a BTreeMap<L, SE>, mut v: V, ) -> Result<BTreeMap<V::L2, V::SE2>, V::Error> where L: Ord, V::L2: Ord, - V: ExprFVeryGenericVisitor<'a, Ret, SE, L, N, E>, + V: ExprFVeryGenericVisitor<'a, Ret, SE, L, E>, { x.iter() .map(|(k, x)| Ok((v.visit_label(k)?, v.visit_subexpr(x)?))) .collect() } - fn btoptmap<'a, V, Ret, SE, L, N, E>( + fn btoptmap<'a, V, Ret, SE, L, E>( x: &'a BTreeMap<L, Option<SE>>, mut v: V, ) -> Result<BTreeMap<V::L2, Option<V::SE2>>, V::Error> where L: Ord, V::L2: Ord, - V: ExprFVeryGenericVisitor<'a, Ret, SE, L, N, E>, + V: ExprFVeryGenericVisitor<'a, Ret, SE, L, E>, { x.iter() .map(|(k, x)| { @@ -171,7 +164,6 @@ where Projection(e, ls) => { Projection(v.visit_subexpr(e)?, vec(ls, |l| v.visit_label(l))?) } - Note(n, e) => return v.visit_note_squash(n, e), Embed(a) => return v.visit_embed_squash(a), }) } @@ -179,14 +171,11 @@ where /// Like ExprFVeryGenericVisitor, but sets the return /// type to ExprF<_> -pub trait ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>: - Sized -{ +pub trait ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>: Sized { type Error; fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error>; fn visit_label(&mut self, label: &'a L1) -> Result<L2, Self::Error>; - fn visit_note(self, note: &'a N1) -> Result<N2, Self::Error>; fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error>; fn visit_subexpr_under_binder( @@ -211,30 +200,19 @@ pub trait ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>: fn visit_embed_squash( self, embed: &'a E1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { + ) -> Result<ExprF<SE2, L2, E2>, Self::Error> { Ok(ExprF::Embed(self.visit_embed(embed)?)) } - - fn visit_note_squash( - mut self, - note: &'a N1, - subexpr: &'a SE1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { - let subexpr = self.visit_subexpr(subexpr)?; - let note = self.visit_note(note)?; - Ok(ExprF::Note(note, subexpr)) - } } -impl<'a, T, SE1, SE2, L1, L2, N1, N2, E1, E2> - ExprFVeryGenericVisitor<'a, ExprF<SE2, L2, N2, E2>, SE1, L1, N1, E1> for T +impl<'a, T, SE1, SE2, L1, L2, E1, E2> + ExprFVeryGenericVisitor<'a, ExprF<SE2, L2, E2>, SE1, L1, E1> for T where - T: ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>, + T: ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>, { type Error = T::Error; type SE2 = SE2; type L2 = L2; - type N2 = N2; type E2 = E2; fn visit_subexpr( @@ -259,34 +237,23 @@ where fn visit_embed_squash( self, embed: &'a E1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { + ) -> Result<ExprF<SE2, L2, E2>, Self::Error> { self.visit_embed_squash(embed) } - fn visit_note_squash( - self, - note: &'a N1, - subexpr: &'a SE1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { - self.visit_note_squash(note, subexpr) - } - - // Called with the result of the map, in the non-embed/note case. + // Called with the result of the map, in the non-embed case. // Useful to change the result type, and/or avoid some loss of info fn visit_resulting_exprf( - result: ExprF<Self::SE2, Self::L2, Self::N2, Self::E2>, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { + result: ExprF<Self::SE2, Self::L2, Self::E2>, + ) -> Result<ExprF<SE2, L2, E2>, Self::Error> { Ok(result) } } /// Like ExprFFallibleVisitor, but without the error handling. -pub trait ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>: - Sized -{ +pub trait ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>: Sized { fn visit_subexpr(&mut self, subexpr: &'a SE1) -> SE2; fn visit_label(&mut self, label: &'a L1) -> L2; - fn visit_note(self, note: &'a N1) -> N2; fn visit_embed(self, embed: &'a E1) -> E2; fn visit_subexpr_under_binder( @@ -304,28 +271,17 @@ pub trait ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>: ) } - fn visit_embed_squash(self, embed: &'a E1) -> ExprF<SE2, L2, N2, E2> { + fn visit_embed_squash(self, embed: &'a E1) -> ExprF<SE2, L2, E2> { ExprF::Embed(self.visit_embed(embed)) } - - fn visit_note_squash( - mut self, - note: &'a N1, - subexpr: &'a SE1, - ) -> ExprF<SE2, L2, N2, E2> { - let subexpr = self.visit_subexpr(subexpr); - let note = self.visit_note(note); - ExprF::Note(note, subexpr) - } } struct InfallibleWrapper<T>(T); -impl<'a, T, SE1, SE2, L1, L2, N1, N2, E1, E2> - ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2> - for InfallibleWrapper<T> +impl<'a, T, SE1, SE2, L1, L2, E1, E2> + ExprFFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2> for InfallibleWrapper<T> where - T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>, + T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>, { type Error = X; @@ -335,9 +291,6 @@ where fn visit_label(&mut self, label: &'a L1) -> Result<L2, Self::Error> { Ok(self.0.visit_label(label)) } - fn visit_note(self, note: &'a N1) -> Result<N2, Self::Error> { - Ok(self.0.visit_note(note)) - } fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error> { Ok(self.0.visit_embed(embed)) } @@ -353,55 +306,41 @@ where fn visit_embed_squash( self, embed: &'a E1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { + ) -> Result<ExprF<SE2, L2, E2>, Self::Error> { Ok(self.0.visit_embed_squash(embed)) } - - fn visit_note_squash( - self, - note: &'a N1, - subexpr: &'a SE1, - ) -> Result<ExprF<SE2, L2, N2, E2>, Self::Error> { - Ok(self.0.visit_note_squash(note, subexpr)) - } } -impl<'a, T, SE1, SE2, L1, L2, N1, N2, E1, E2> - GenericVisitor<&'a ExprF<SE1, L1, N1, E1>, ExprF<SE2, L2, N2, E2>> for T +impl<'a, T, SE1, SE2, L1, L2, E1, E2> + GenericVisitor<&'a ExprF<SE1, L1, E1>, ExprF<SE2, L2, E2>> for T where L1: Ord, L2: Ord, - T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, N1, N2, E1, E2>, + T: ExprFInFallibleVisitor<'a, SE1, SE2, L1, L2, E1, E2>, { - fn visit( - self, - input: &'a ExprF<SE1, L1, N1, E1>, - ) -> ExprF<SE2, L2, N2, E2> { + fn visit(self, input: &'a ExprF<SE1, L1, E1>) -> ExprF<SE2, L2, E2> { trivial_result(InfallibleWrapper(self).visit(input)) } } -pub struct TraverseRefWithBindersVisitor<F1, F2, F3, F4, F5> { +pub struct TraverseRefWithBindersVisitor<F1, F2, F4, F5> { pub visit_subexpr: F1, pub visit_under_binder: F2, - pub visit_note: F3, pub visit_embed: F4, pub visit_label: F5, } -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 TraverseRefWithBindersVisitor<F1, F2, F3, F4, F5> +impl<'a, SE, L, E, SE2, L2, E2, Err, F1, F2, F4, F5> + ExprFFallibleVisitor<'a, SE, SE2, L, L2, E, E2> + for TraverseRefWithBindersVisitor<F1, F2, F4, F5> where SE: 'a, L: 'a, - N: 'a, E: 'a, L: Ord, L2: Ord, F1: FnMut(&'a SE) -> Result<SE2, Err>, F2: FnOnce(&'a L, &'a SE) -> Result<SE2, Err>, - F3: FnOnce(&'a N) -> Result<N2, Err>, F4: FnOnce(&'a E) -> Result<E2, Err>, F5: FnMut(&'a L) -> Result<L2, Err>, { @@ -417,9 +356,6 @@ where ) -> Result<SE2, Self::Error> { (self.visit_under_binder)(label, subexpr) } - fn visit_note(self, note: &'a N) -> Result<N2, Self::Error> { - (self.visit_note)(note) - } fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> { (self.visit_embed)(embed) } @@ -428,25 +364,22 @@ where } } -pub struct TraverseRefVisitor<F1, F2, F3, F4> { +pub struct TraverseRefVisitor<F1, F3, F4> { 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<F1, F2, F3, F4> +impl<'a, SE, L, E, SE2, L2, E2, Err, F1, F3, F4> + ExprFFallibleVisitor<'a, SE, SE2, L, L2, E, E2> + for TraverseRefVisitor<F1, F3, F4> where SE: 'a, L: 'a, - N: 'a, E: 'a, L: Ord, L2: Ord, F1: FnMut(&'a SE) -> Result<SE2, Err>, - F2: FnOnce(&'a N) -> Result<N2, Err>, F3: FnOnce(&'a E) -> Result<E2, Err>, F4: FnMut(&'a L) -> Result<L2, Err>, { @@ -455,9 +388,6 @@ where fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result<SE2, Self::Error> { (self.visit_subexpr)(subexpr) } - fn visit_note(self, note: &'a N) -> Result<N2, Self::Error> { - (self.visit_note)(note) - } fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> { (self.visit_embed)(embed) } @@ -469,17 +399,8 @@ where pub struct TraverseEmbedVisitor<F1>(pub F1); impl<'a, 'b, N, E, E2, Err, F1> - ExprFFallibleVisitor< - 'a, - SubExpr<N, E>, - SubExpr<N, E2>, - Label, - Label, - N, - N, - E, - E2, - > for &'b mut TraverseEmbedVisitor<F1> + ExprFFallibleVisitor<'a, SubExpr<N, E>, SubExpr<N, E2>, Label, Label, E, E2> + for &'b mut TraverseEmbedVisitor<F1> where N: Clone + 'a, F1: FnMut(&E) -> Result<E2, Err>, @@ -492,9 +413,6 @@ where ) -> Result<SubExpr<N, E2>, Self::Error> { Ok(rc(subexpr.as_ref().visit(&mut **self)?)) } - fn visit_note(self, note: &'a N) -> Result<N, Self::Error> { - Ok(N::clone(note)) - } fn visit_embed(self, embed: &'a E) -> Result<E2, Self::Error> { (self.0)(embed) } @@ -506,7 +424,7 @@ where pub struct SquashEmbedVisitor<F1>(pub F1); impl<'a, 'b, N, E1, E2, F1> - ExprFVeryGenericVisitor<'a, SubExpr<N, E2>, SubExpr<N, E1>, Label, N, E1> + ExprFVeryGenericVisitor<'a, SubExpr<N, E2>, SubExpr<N, E1>, Label, E1> for &'b mut SquashEmbedVisitor<F1> where N: Clone + 'a, @@ -515,7 +433,6 @@ where type Error = X; type SE2 = SubExpr<N, E2>; type L2 = Label; - type N2 = N; type E2 = E2; fn visit_subexpr( @@ -547,20 +464,10 @@ where Ok((self.0)(embed)) } - fn visit_note_squash( - mut self, - note: &'a N, - subexpr: &'a SubExpr<N, E1>, - ) -> Result<SubExpr<N, E2>, Self::Error> { - let subexpr = self.visit_subexpr(subexpr)?; - let note = N::clone(note); - Ok(rc(ExprF::Note(note, subexpr))) - } - - // Called with the result of the map, in the non-embed/note case. + // Called with the result of the map, in the non-embed case. // Useful to change the result type, and/or avoid some loss of info fn visit_resulting_exprf( - result: ExprF<Self::SE2, Self::L2, Self::N2, Self::E2>, + result: ExprF<Self::SE2, Self::L2, Self::E2>, ) -> Result<SubExpr<N, E2>, Self::Error> { Ok(rc(result)) } @@ -569,33 +476,14 @@ where pub struct UnNoteVisitor; impl<'a, 'b, N, E> - ExprFInFallibleVisitor< - 'a, - SubExpr<N, E>, - SubExpr<X, E>, - Label, - Label, - N, - X, - E, - E, - > for &'b mut UnNoteVisitor + ExprFInFallibleVisitor<'a, SubExpr<N, E>, SubExpr<X, E>, Label, Label, E, E> + for &'b mut UnNoteVisitor where E: Clone + 'a, { fn visit_subexpr(&mut self, subexpr: &'a SubExpr<N, E>) -> SubExpr<X, E> { rc(subexpr.as_ref().visit(&mut **self)) } - fn visit_note(self, _: &'a N) -> X { - unreachable!() - } - fn visit_note_squash( - self, - _: &'a N, - subexpr: &'a SubExpr<N, E>, - ) -> Expr<X, E> { - subexpr.as_ref().visit(self) - } fn visit_embed(self, embed: &'a E) -> E { E::clone(embed) } @@ -607,26 +495,14 @@ where pub struct NoteAbsurdVisitor; impl<'a, 'b, N, E> - ExprFInFallibleVisitor< - 'a, - SubExpr<X, E>, - SubExpr<N, E>, - Label, - Label, - X, - N, - E, - E, - > for &'b mut NoteAbsurdVisitor + ExprFInFallibleVisitor<'a, SubExpr<X, E>, SubExpr<N, E>, Label, Label, E, E> + for &'b mut NoteAbsurdVisitor where E: Clone + 'a, { fn visit_subexpr(&mut self, subexpr: &'a SubExpr<X, E>) -> SubExpr<N, E> { rc(subexpr.as_ref().visit(&mut **self)) } - fn visit_note(self, note: &'a X) -> N { - match *note {} - } fn visit_embed(self, embed: &'a E) -> E { E::clone(embed) } @@ -638,26 +514,14 @@ where pub struct EmbedAbsurdVisitor; impl<'a, 'b, N, E> - ExprFInFallibleVisitor< - 'a, - SubExpr<N, X>, - SubExpr<N, E>, - Label, - Label, - N, - N, - X, - E, - > for &'b mut EmbedAbsurdVisitor + ExprFInFallibleVisitor<'a, SubExpr<N, X>, SubExpr<N, E>, Label, Label, X, E> + for &'b mut EmbedAbsurdVisitor where N: Clone + 'a, { fn visit_subexpr(&mut self, subexpr: &'a SubExpr<N, X>) -> SubExpr<N, E> { rc(subexpr.as_ref().visit(&mut **self)) } - fn visit_note(self, note: &'a N) -> N { - N::clone(note) - } fn visit_embed(self, embed: &'a X) -> E { match *embed {} } |