Skip to content

Commit

Permalink
implement missing predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Vassor committed Jul 8, 2024
1 parent 1778770 commit bc43800
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
18 changes: 16 additions & 2 deletions generate/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@ impl Display for Predicate {
Some(l) => write!(f, "Equal::<Value, char, {}, {}, {}>", l, lhs, rhs),
}
}
_ => unimplemented!()
Predicate::LTn(lhs, rhs, label) => {
match label {
None => write!(f, "LTn::<Value, char, Label, {}, {}>", lhs, rhs),
Some(l) => write!(f, "LTn::<Value, char, {}, {}, {}>", l, lhs, rhs),
}
}
Predicate::GTn(lhs, rhs, label) => {
match label {
None => write!(f, "GTn::<Value, char, Label, {}, {}>", lhs, rhs),
Some(l) => write!(f, "GTn::<Value, char, {}, {}, {}>", l, lhs, rhs),
}
}
_ => {
unimplemented!("{:#?}", self)
}
}
}
}
Expand Down Expand Up @@ -330,7 +344,7 @@ impl Display for TypeFormatter<'_> {
} => {
let (other, param_name, label, effect, next) = (
self.role(role),
self.param_names(label).iter().next().unwrap().clone(),
*self.param_names(label).iter().next().unwrap(),
self.label(label),
self.effect(side_effect),
self.with(next),
Expand Down
78 changes: 78 additions & 0 deletions src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,81 @@ where
}
}
}

/// The `LTn` struct implements an less-than operator.
pub struct LTn<V: Ord, N, L, LHS: IntExpr, RHS: IntExpr> {
_p: PhantomData<(V, N, L, LHS, RHS)>,
}

impl<L, N, V: Ord, LHS: IntExpr, RHS: IntExpr> Default for LTn<V, N, L, LHS, RHS> {
fn default() -> Self {
Self {
_p: PhantomData
}
}
}

impl<L, N, V: Ord, LHS: IntExpr, RHS: IntExpr> Predicate for LTn<V, N, L, LHS, RHS>
where
V: std::cmp::Ord,
LHS: IntExpr<Name = N, Value = V, Error = ()>,
RHS: IntExpr<Name = N, Value = V, Error = ()>
{
type Name = N;
type Value = V;
type Label = L;
type Error = ();

fn check(
&self,
m: &HashMap<Self::Name, Self::Value>,
_l: Option<&Self::Label>,
) -> Result<(), Self::Error> {
let lhs = LHS::default().value(m)?;
let rhs = RHS::default().value(m)?;
if lhs < rhs {
Ok(())
} else {
Err(())
}
}
}

/// The `GTn` struct implements an greater-than operator.
pub struct GTn<V: Ord, N, L, LHS: IntExpr, RHS: IntExpr> {
_p: PhantomData<(V, N, L, LHS, RHS)>,
}

impl<L, N, V: Ord, LHS: IntExpr, RHS: IntExpr> Default for GTn<V, N, L, LHS, RHS> {
fn default() -> Self {
Self {
_p: PhantomData
}
}
}

impl<L, N, V: Ord, LHS: IntExpr, RHS: IntExpr> Predicate for GTn<V, N, L, LHS, RHS>
where
V: std::cmp::Ord,
LHS: IntExpr<Name = N, Value = V, Error = ()>,
RHS: IntExpr<Name = N, Value = V, Error = ()>
{
type Name = N;
type Value = V;
type Label = L;
type Error = ();

fn check(
&self,
m: &HashMap<Self::Name, Self::Value>,
_l: Option<&Self::Label>,
) -> Result<(), Self::Error> {
let lhs = LHS::default().value(m)?;
let rhs = RHS::default().value(m)?;
if lhs > rhs {
Ok(())
} else {
Err(())
}
}
}

0 comments on commit bc43800

Please sign in to comment.