Skip to content

Commit

Permalink
Use Closed terms for separators.
Browse files Browse the repository at this point in the history
It is unnecessary to map terms via `close_boundary_separation`. This
change removes this function and emits `Closed` terms for `Separator`s
(and similar terms for tree wildcards).
  • Loading branch information
olson-sean-k committed Mar 25, 2024
1 parent 828e7cf commit 572ffbc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 55 deletions.
24 changes: 16 additions & 8 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use crate::token::parse::{parse, ParseError, ROOT_SEPARATOR_EXPRESSION};
pub use crate::token::variance::bound::{
BoundedVariantRange, Boundedness, NaturalRange, VariantRange,
};
pub use crate::token::variance::invariant::{Finalize, Breadth, Depth, DepthTerm, Invariant, Size, Text};
pub use crate::token::variance::invariant::{Separation, SeparatedTerm, Finalize, Breadth, Depth, DepthTerm, Invariant, Size, Text};
pub use crate::token::variance::{TokenVariance, Variance};

// TODO: Tree representations of expressions are intrusive and only differ in their annotations.
Expand Down Expand Up @@ -1162,14 +1162,19 @@ impl<'t, A> From<Vec<Token<'t, A>>> for Concatenation<'t, A> {
impl<'t, A> VarianceFold<Depth> for Concatenation<'t, A> {
fn fold(&self, terms: Vec<DepthTerm>) -> Option<DepthTerm> {
//terms.into_iter().with_position().map(|(position, term)| term.close_boundary_separation(position)).reduce(ops::conjunction)
//terms.into_iter().reduce(ops::conjunction)
eprintln!("==> Concatenation (Conjunction) Terms");
//let sum = terms
// .into_iter()
// .with_position()
// .map(|(position, term)| {
// eprintln!("\t{:?}", term);
// term.close_boundary_separation(position)
// })
// .inspect(|term| eprintln!("\t{:?}\n", term))
// .reduce(ops::conjunction);
let sum = terms
.into_iter()
.with_position()
.map(|(position, term)| {
eprintln!("\t{:?}", term);
term.close_boundary_separation(position)
})
.inspect(|term| eprintln!("\t{:?}\n", term))
.reduce(ops::conjunction);
eprintln!("\tConjuction: {:?}", sum);
Expand Down Expand Up @@ -1407,7 +1412,7 @@ impl VarianceTerm<Breadth> for Separator {

impl VarianceTerm<Depth> for Separator {
fn term(&self) -> <Depth as Invariant>::Term {
BoundaryTerm::separator(One::one())
One::one()
}
}

Expand Down Expand Up @@ -1475,7 +1480,10 @@ impl VarianceTerm<Breadth> for Wildcard {
impl VarianceTerm<Depth> for Wildcard {
fn term(&self) -> <Depth as Invariant>::Term {
match self {
Wildcard::Tree { .. } => BoundaryTerm::component(Variance::unbounded()),
Wildcard::Tree { ref has_root, .. } => BoundaryTerm::Conjunctive(SeparatedTerm(
if *has_root { Separation::Closed } else { Separation::Last },
Variance::unbounded(),
)),
_ => Zero::zero(),
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/token/variance/invariant/natural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ impl Finalize for SeparatedTerm<TokenVariance<Depth>> {

let SeparatedTerm(separation, term) = self;
match separation {
Open(Some(Component)) | First | Last => term,
Open(_) => ops::conjunction(term, Invariant(Depth::ONE)),
//Open(Some(Component)) | First | Last => term,
Open => ops::conjunction(term, Invariant(Depth::ONE)),
First | Last => term,
Closed => term.map_invariant(|term| Depth(term.0.saturating_sub(1))),
}
}
Expand Down
89 changes: 45 additions & 44 deletions src/token/variance/invariant/separation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ where
TokenVariance<T>: Eq + Hash,
T: BoundedVariance,
{
pub fn separator(term: TokenVariance<T>) -> Self {
BoundaryTerm::Conjunctive(SeparatedTerm::separator(term))
}

pub fn component(term: TokenVariance<T>) -> Self {
BoundaryTerm::Conjunctive(SeparatedTerm::component(term))
}

pub fn close_boundary_separation(self, position: Position) -> Self {
match self {
BoundaryTerm::Conjunctive(term) => BoundaryTerm::Conjunctive(term.close_boundary_separation(position)),
// TODO: Is it necessary to close boundary separations in disjunctive terms?
BoundaryTerm::Disjunctive(term) => BoundaryTerm::Disjunctive(term.remap(|term| term.close_boundary_separation(position))),
}
}
//pub fn separator(term: TokenVariance<T>) -> Self {
// BoundaryTerm::Conjunctive(SeparatedTerm::separator(term))
//}

//pub fn component(term: TokenVariance<T>) -> Self {
// BoundaryTerm::Conjunctive(SeparatedTerm::component(term))
//}

//pub fn close_boundary_separation(self, position: Position) -> Self {
// match self {
// BoundaryTerm::Conjunctive(term) => BoundaryTerm::Conjunctive(term.close_boundary_separation(position)),
// // TODO: Is it necessary to close boundary separations in disjunctive terms?
// BoundaryTerm::Disjunctive(term) => BoundaryTerm::Disjunctive(term.remap(|term| term.close_boundary_separation(position))),
// }
//}

pub fn as_variance(&self) -> Variance<&T, Option<&Boundedness<T::Bound>>> {
match self {
Expand Down Expand Up @@ -276,7 +276,8 @@ pub enum Separation {
// otherwise unaffected: tree wildcards are either `Last` or `Closed` and are always
// unbounded. They "just" have an implicit +1 in conjunctions for depth. This extra +1
// ought to be specified per invariant rather than generalized for all `BoundaryTerm`s.
Open(Option<Boundary>),
//Open(Option<Boundary>),
Open,
First,
Last,
Closed,
Expand All @@ -290,9 +291,9 @@ impl Conjunction for Separation {

match (self, rhs) {
(Closed, Closed) | (First, Last | Closed) | (Closed, Last) => Closed,
(Last | Open(_), Closed) | (Last | Open(_), Last) => Last,
(Closed, First | Open(_)) | (First, First | Open(_)) => First,
(Open(_), First | Open(_)) | (Last, First | Open(_)) => Open(None),
(Last | Open, Closed) | (Last | Open, Last) => Last,
(Closed, First | Open) | (First, First | Open) => First,
(Open, First | Open) | (Last, First | Open) => Open,
}
}
}
Expand All @@ -315,32 +316,32 @@ impl Conjunction for Separation {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SeparatedTerm<T>(pub Separation, pub T);

impl<T> SeparatedTerm<T> {
pub fn close_boundary_separation(self, position: Position) -> Self {
use Boundary::{Component, Separator};
use Position::{First, Middle, Last, Only};

let SeparatedTerm(separation, term) = self;
SeparatedTerm(match separation {
separation @ Separation::Open(Some(boundary)) => match (boundary, position) {
(Component, _) | (_, Only) => separation,
(Separator, First) => Separation::First,
(Separator, Middle) => Separation::Open(None),
(Separator, Last) => Separation::Last,
},
separation => separation,
}, term)
}
}
//impl<T> SeparatedTerm<T> {
// pub fn close_boundary_separation(self, position: Position) -> Self {
// use Boundary::{Component, Separator};
// use Position::{First, Middle, Last, Only};
//
// let SeparatedTerm(separation, term) = self;
// SeparatedTerm(match separation {
// separation @ Separation::Open(Some(boundary)) => match (boundary, position) {
// (Component, _) | (_, Only) => separation,
// (Separator, First) => Separation::First,
// (Separator, Middle) => Separation::Open(None),
// (Separator, Last) => Separation::Last,
// },
// separation => separation,
// }, term)
// }
//}

impl<T> SeparatedTerm<T> {
pub fn separator(term: T) -> Self {
SeparatedTerm(Separation::Open(Some(Boundary::Separator)), term)
}
//pub fn separator(term: T) -> Self {
// SeparatedTerm(Separation::Open(Some(Boundary::Separator)), term)
//}

pub fn component(term: T) -> Self {
SeparatedTerm(Separation::Open(Some(Boundary::Component)), term)
}
//pub fn component(term: T) -> Self {
// SeparatedTerm(Separation::Open(Some(Boundary::Component)), term)
//}

pub fn into_inner(self) -> T {
self.1
Expand Down Expand Up @@ -407,7 +408,7 @@ where
T: One,
{
fn one() -> Self {
SeparatedTerm(Separation::Open(None), T::one())
SeparatedTerm(Separation::Closed, T::one())
}
}

Expand All @@ -427,6 +428,6 @@ where
T: Zero,
{
fn zero() -> Self {
SeparatedTerm(Separation::Open(None), T::zero())
SeparatedTerm(Separation::Open, T::zero())
}
}
2 changes: 1 addition & 1 deletion src/token/variance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl<'t, A> Fold<'t, A> for TreeExhaustiveness {
.into_inner();
if !all && any {
return Some(BoundaryTerm::Conjunctive(SeparatedTerm(
Separation::Open(None),
Separation::Open,
TokenVariance::<Depth>::Variant(Bounded(BoundedVariantRange::Upper(unsafe {
NonZeroUsize::new_unchecked(1)
}))),
Expand Down

0 comments on commit 572ffbc

Please sign in to comment.