Support right-to-left implications
As right-to-left implications are common in answer set programming, this adds support for using implications in both directions.
This commit is contained in:
parent
5bbb09eef8
commit
855fd9abcf
19
src/ast.rs
19
src/ast.rs
@ -29,6 +29,15 @@ pub enum UnaryOperator
|
|||||||
Negative,
|
Negative,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImplicationDirection
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
|
pub enum ImplicationDirection
|
||||||
|
{
|
||||||
|
LeftToRight,
|
||||||
|
RightToLeft,
|
||||||
|
}
|
||||||
|
|
||||||
// Primitives
|
// Primitives
|
||||||
|
|
||||||
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
|
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
@ -293,16 +302,19 @@ impl IfAndOnlyIf
|
|||||||
|
|
||||||
pub struct Implies
|
pub struct Implies
|
||||||
{
|
{
|
||||||
|
pub direction: ImplicationDirection,
|
||||||
pub antecedent: Box<Formula>,
|
pub antecedent: Box<Formula>,
|
||||||
pub implication: Box<Formula>,
|
pub implication: Box<Formula>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Implies
|
impl Implies
|
||||||
{
|
{
|
||||||
pub fn new(antecedent: Box<Formula>, implication: Box<Formula>) -> Self
|
pub fn new(direction: ImplicationDirection, antecedent: Box<Formula>, implication: Box<Formula>)
|
||||||
|
-> Self
|
||||||
{
|
{
|
||||||
Self
|
Self
|
||||||
{
|
{
|
||||||
|
direction,
|
||||||
antecedent,
|
antecedent,
|
||||||
implication,
|
implication,
|
||||||
}
|
}
|
||||||
@ -524,9 +536,10 @@ impl Formula
|
|||||||
Self::IfAndOnlyIf(IfAndOnlyIf::new(left, right))
|
Self::IfAndOnlyIf(IfAndOnlyIf::new(left, right))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn implies(antecedent: Box<Formula>, consequent: Box<Formula>) -> Self
|
pub fn implies(direction: ImplicationDirection, antecedent: Box<Formula>,
|
||||||
|
consequent: Box<Formula>) -> Self
|
||||||
{
|
{
|
||||||
Self::Implies(Implies::new(antecedent, consequent))
|
Self::Implies(Implies::new(direction, antecedent, consequent))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn less(left: Box<Term>, right: Box<Term>) -> Self
|
pub fn less(left: Box<Term>, right: Box<Term>) -> Self
|
||||||
|
@ -28,6 +28,18 @@ impl Precedence for crate::Formula
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for crate::ImplicationDirection
|
||||||
|
{
|
||||||
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||||
|
{
|
||||||
|
match &self
|
||||||
|
{
|
||||||
|
Self::LeftToRight => write!(format, "left to right"),
|
||||||
|
Self::RightToLeft => write!(format, "right to left"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for crate::PredicateDeclaration
|
impl std::fmt::Debug for crate::PredicateDeclaration
|
||||||
{
|
{
|
||||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||||
@ -140,9 +152,14 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
|||||||
separator = " or "
|
separator = " or "
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
crate::Formula::Implies(crate::Implies{antecedent, implication})
|
crate::Formula::Implies(crate::Implies{
|
||||||
|
direction: crate::ImplicationDirection::LeftToRight, antecedent, implication})
|
||||||
=> write!(format, "{:?} -> {:?}", display_formula(antecedent, precedence),
|
=> write!(format, "{:?} -> {:?}", display_formula(antecedent, precedence),
|
||||||
display_formula(implication, precedence))?,
|
display_formula(implication, precedence))?,
|
||||||
|
crate::Formula::Implies(crate::Implies{
|
||||||
|
direction: crate::ImplicationDirection::RightToLeft, antecedent, implication})
|
||||||
|
=> write!(format, "{:?} <- {:?}", display_formula(implication, precedence),
|
||||||
|
display_formula(antecedent, precedence))?,
|
||||||
crate::Formula::IfAndOnlyIf(crate::IfAndOnlyIf{left, right})
|
crate::Formula::IfAndOnlyIf(crate::IfAndOnlyIf{left, right})
|
||||||
=> write!(format, "{:?} <-> {:?}", display_formula(left, precedence),
|
=> write!(format, "{:?} <-> {:?}", display_formula(left, precedence),
|
||||||
display_formula(right, precedence))?,
|
display_formula(right, precedence))?,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user