Skip to content

Commit

Permalink
Remove commented code and rename term items.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Mar 25, 2024
1 parent 5a3d9b4 commit 5ea2e02
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 182 deletions.
9 changes: 4 additions & 5 deletions src/token/mod.rs
Expand Up @@ -26,7 +26,9 @@ 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::{Separation, SeparatedTerm, Finalize, Breadth, Depth, DepthTerm, Invariant, Size, Text};
pub use crate::token::variance::invariant::{
Breadth, Depth, DepthTerm, Finalize, 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 @@ -1480,10 +1482,7 @@ impl VarianceTerm<Breadth> for Wildcard {
impl VarianceTerm<Depth> for Wildcard {
fn term(&self) -> <Depth as Invariant>::Term {
match self {
Wildcard::Tree { .. } => BoundaryTerm::Conjunctive(SeparatedTerm(
Separation::Closed { is_coalescent: true },
Variance::unbounded(),
)),
Wildcard::Tree { .. } => BoundaryTerm::unbounded(),
_ => Zero::zero(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/token/variance/invariant/mod.rs
Expand Up @@ -10,7 +10,7 @@ use crate::token::variance::TokenVariance;

pub use crate::token::variance::invariant::natural::{Depth, DepthTerm, Size};
pub use crate::token::variance::invariant::separation::{
BoundaryTerm, SeparatedTerm, Separation, Finalize,
BoundaryTerm, Finalize, SeparatedTerm, Termination,
};
pub use crate::token::variance::invariant::text::{IntoNominalText, IntoStructuralText, Text};

Expand Down
19 changes: 8 additions & 11 deletions src/token/variance/invariant/natural.rs
Expand Up @@ -4,12 +4,11 @@ use crate::token::variance::bound::{
Bounded, BoundedVariantRange, Boundedness, Unbounded, VariantRange,
};
use crate::token::variance::invariant::separation::{
BoundaryTerm, Finalize, SeparatedTerm, Separation,
BoundaryTerm, Finalize, SeparatedTerm, Termination,
};
use crate::token::variance::invariant::{BoundedVariance, Invariant, InvariantTerm, One, Zero};
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
use crate::token::variance::{TokenVariance, Variance};
use crate::token::Boundary;

macro_rules! impl_natural_invariant_term {
($name:ident $(,)?) => {
Expand Down Expand Up @@ -169,15 +168,13 @@ impl Finalize for SeparatedTerm<TokenVariance<Depth>> {
type Output = TokenVariance<Depth>;

fn finalize(self) -> Self::Output {
use Boundary::Component;
use Separation::{Closed, First, Last, Open};
use Variance::Invariant;

let SeparatedTerm(separation, term) = self;
match separation {
Open => ops::conjunction(term, Invariant(Depth::ONE)),
First | Last => term,
Closed { .. } => term.map_invariant(|term| Depth(term.0.saturating_sub(1))),
use Termination::{Closed, Open};

let SeparatedTerm(termination, term) = self;
match termination {
Open => ops::conjunction(term, One::one()),
Closed => term.map_invariant(|term| term.map(|term| term.saturating_sub(1))),
_ => term,
}
}
}
Expand Down
192 changes: 45 additions & 147 deletions src/token/variance/invariant/separation.rs
@@ -1,12 +1,12 @@
use itertools::{Itertools, Position};
use itertools::Itertools;
use std::collections::HashSet;
use std::hash::Hash;

use crate::token::variance::bound::Boundedness;
use crate::token::variance::invariant::{BoundedVariance, Invariant, One, Zero};
use crate::token::variance::invariant::{BoundedVariance, One, Zero};
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
use crate::token::variance::{TokenVariance, Variance};
use crate::token::{Boundary, Composition};
use crate::token::Composition;

pub trait Finalize: Sized {
type Output;
Expand All @@ -21,21 +21,12 @@ 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 unbounded() -> Self {
BoundaryTerm::Conjunctive(SeparatedTerm(
Termination::Coalescent,
Variance::unbounded(),
))
}

pub fn as_variance(&self) -> Variance<&T, Option<&Boundedness<T::Bound>>> {
match self {
Expand All @@ -49,8 +40,8 @@ pub type TreeTerm<T> = Composition<T, DisjunctiveTerm<T>>;

impl<T> Conjunction for TreeTerm<T>
where
DisjunctiveTerm<T>: Conjunction<T, Output = DisjunctiveTerm<T>>
+ Conjunction<Output = DisjunctiveTerm<T>>,
DisjunctiveTerm<T>:
Conjunction<T, Output = DisjunctiveTerm<T>> + Conjunction<Output = DisjunctiveTerm<T>>,
T: Conjunction<Output = T>
+ Conjunction<DisjunctiveTerm<T>, Output = DisjunctiveTerm<T>>
+ Eq
Expand Down Expand Up @@ -267,106 +258,44 @@ where
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Separation {
// TODO: While additional information is needed to close `Open` for separators, more general
// boundary information appears to be unnecessary. More importantly, this is insufficient
// to handle the "coalescence" of tree wildcards! This information likely needs to be
// pushed up. One algebra that seems to work is to immediately finalize tree wildcard
// terms in conjunctions (prior to performing the conjunction). The `Separation` is
// 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>),
pub enum Coalescence<T> {
Left(T),
Right(T),
Neither(T),
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Termination {
Open,
First,
Last,
Closed {
is_coalescent: bool,
},
Closed,
Coalescent,
}

impl Separation {
pub fn is_coalescent(&self) -> bool {
match self {
Separation::Closed { ref is_coalescent } => *is_coalescent,
_ => false,
}
}
}

impl Conjunction for Separation {
type Output = Self;
impl Conjunction for Termination {
type Output = Coalescence<Self>;

fn conjunction(self, rhs: Self) -> Self::Output {
use Separation::{Closed, First, Last, Open};
use Coalescence::{Left, Neither, Right};
use Termination::{Closed, Coalescent, First, Last, Open};

match (self, rhs) {
(Closed { is_coalescent: lhs }, Closed { is_coalescent: rhs }) => Closed { is_coalescent: lhs && rhs },
(First, Last | Closed { .. }) | (Closed { .. }, Last) => Closed { is_coalescent: false },
(Last | Open, Closed { .. }) | (Last | Open, Last) => Last,
(Closed { .. }, First | Open) | (First, First | Open) => First,
(Open, First | Open) | (Last, First | Open) => Open,
(Coalescent, Coalescent) => Neither(Coalescent),
(Closed, Closed) | (First, Last | Closed) | (Closed, Last) => Neither(Closed),
(Last | Open, Closed) | (Last | Open, Last) => Neither(Last),
(Closed, First | Open) | (First, First | Open) => Neither(First),
(Open, First | Open) | (Last, First | Open) => Neither(Open),
(Coalescent, Closed | Last) => Right(Closed),
(Closed | First, Coalescent) => Left(Closed),
(Last | Open, Coalescent) => Left(Last),
(Coalescent, First | Open) => Right(First),
}
}
}

//impl Disjunction for Separation {
// type Output = Self;
//
// fn disjunction(self, rhs: Self) -> Self::Output {
// use Separation::{First, Last, Terminal, Unit};
//
// match (self, rhs) {
// (Terminal, _) | (_, Terminal) | (First, Last) | (Last, First) => Terminal,
// (Last, _) | (_, Last) => Last,
// (First, _) | (_, First) => First,
// _ => Unit,
// }
// }
//}

#[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 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 into_inner(self) -> T {
self.1
}

fn finalize_in_place(self) -> Self
where
Self: Finalize<Output = T>,
{
let separation = self.0;
SeparatedTerm(separation, self.finalize())
}
}
pub struct SeparatedTerm<T>(pub Termination, pub T);

impl<T, U> Conjunction<SeparatedTerm<U>> for SeparatedTerm<T>
where
Expand All @@ -377,37 +306,18 @@ where
type Output = SeparatedTerm<T::Output>;

fn conjunction(self, rhs: SeparatedTerm<U>) -> Self::Output {
let (lhs, rhs) = match (self.0.is_coalescent(), rhs.0.is_coalescent()) {
(true, true) => (self, rhs),
(true, _) => (self, rhs.finalize_in_place()),
(_, true) => (self.finalize_in_place(), rhs),
_ => (self, rhs),
use Coalescence::{Left, Neither, Right};

let lhs = self;
let (termination, lhs, rhs) = match ops::conjunction(lhs.0, rhs.0) {
Left(termination) => (termination, lhs.finalize(), rhs.1),
Right(termination) => (termination, lhs.1, rhs.finalize()),
Neither(termination) => (termination, lhs.1, rhs.1),
};
SeparatedTerm(
ops::conjunction(lhs.0, rhs.0),
ops::conjunction(lhs.1, rhs.1),
)
SeparatedTerm(termination, ops::conjunction(lhs, rhs))
}
}

//impl<T, U> Disjunction<Separated<U>> for Separated<T>
//where
// Self: Finalize<Output = Self>,
// Separated<U>: Finalize<Output = Separated<U>>,
// T: Disjunction<U>,
//{
// type Output = Separated<T::Output>;
//
// fn disjunction(self, rhs: Separated<U>) -> Self::Output {
// let lhs = self.finalize();
// let rhs = rhs.finalize();
// Separated(
// ops::disjunction(lhs.0, rhs.0),
// ops::disjunction(lhs.1, rhs.1),
// )
// }
//}

impl<T> Disjunction for SeparatedTerm<T>
where
T: Eq + Hash,
Expand All @@ -419,24 +329,12 @@ where
}
}

//impl<T> Finalize for Separated<TokenVariance<T>>
//where
// T: BoundedVariance + Invariant<Term = Self>,
//{
// type Output = Self;
//
// fn finalize(self) -> Self {
// let separation = self.0;
// Separated(separation, T::finalize(self))
// }
//}

impl<T> One for SeparatedTerm<T>
where
T: One,
{
fn one() -> Self {
SeparatedTerm(Separation::Closed { is_coalescent: false }, T::one())
SeparatedTerm(Termination::Closed, T::one())
}
}

Expand All @@ -456,6 +354,6 @@ where
T: Zero,
{
fn zero() -> Self {
SeparatedTerm(Separation::Open, T::zero())
SeparatedTerm(Termination::Open, T::zero())
}
}

0 comments on commit 5ea2e02

Please sign in to comment.