Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Predicate node to the ASP and FOL syntax trees #44

Merged
merged 8 commits into from
Dec 8, 2023
35 changes: 22 additions & 13 deletions src/formatting/asp/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use {
syntax_tree::{
asp::{
Atom, AtomicFormula, BinaryOperator, Body, Comparison, Head, Literal,
PrecomputedTerm, Program, Relation, Rule, Sign, Term, UnaryOperator, Variable,
PrecomputedTerm, Predicate, Program, Relation, Rule, Sign, Term, UnaryOperator,
Variable,
},
Node,
},
Expand Down Expand Up @@ -106,9 +107,17 @@ impl Display for Format<'_, Term> {
}
}

impl Display for Format<'_, Predicate> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let symbol = &self.0.symbol;
let arity = &self.0.arity;
write!(f, "{symbol}/{arity}")
}
}

impl Display for Format<'_, Atom> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let predicate = &self.0.predicate;
let predicate = &self.0.predicate_symbol;
let terms = &self.0.terms;

write!(f, "{predicate}")?;
Expand Down Expand Up @@ -342,7 +351,7 @@ mod tests {
fn format_atom() {
assert_eq!(
Format(&Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![],
})
.to_string(),
Expand All @@ -351,7 +360,7 @@ mod tests {

assert_eq!(
Format(&Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![Term::PrecomputedTerm(PrecomputedTerm::Numeral(1))],
})
.to_string(),
Expand All @@ -360,7 +369,7 @@ mod tests {

assert_eq!(
Format(&Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![
Term::PrecomputedTerm(PrecomputedTerm::Numeral(1)),
Term::PrecomputedTerm(PrecomputedTerm::Numeral(2))
Expand All @@ -384,7 +393,7 @@ mod tests {
Format(&Literal {
sign: Sign::Negation,
atom: Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}
})
Expand Down Expand Up @@ -422,7 +431,7 @@ mod tests {
Format(&AtomicFormula::Literal(Literal {
sign: Sign::DoubleNegation,
atom: Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}
}))
Expand All @@ -445,7 +454,7 @@ mod tests {
fn format_head() {
assert_eq!(
Format(&Head::Basic(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.to_string(),
Expand All @@ -454,7 +463,7 @@ mod tests {

assert_eq!(
Format(&Head::Choice(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.to_string(),
Expand All @@ -474,7 +483,7 @@ mod tests {
AtomicFormula::Literal(Literal {
sign: Sign::NoSign,
atom: Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![Term::Variable(Variable("X".into()))]
}
}),
Expand Down Expand Up @@ -502,21 +511,21 @@ mod tests {
rules: vec![
Rule {
head: Head::Basic(Atom {
predicate: "a".into(),
predicate_symbol: "a".into(),
terms: vec![]
}),
body: Body { formulas: vec![] }
},
Rule {
head: Head::Basic(Atom {
predicate: "b".into(),
predicate_symbol: "b".into(),
terms: vec![]
}),
body: Body {
formulas: vec![AtomicFormula::Literal(Literal {
sign: Sign::Negation,
atom: Atom {
predicate: "a".into(),
predicate_symbol: "a".into(),
terms: vec![]
}
})]
Expand Down
70 changes: 39 additions & 31 deletions src/formatting/fol/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use {
syntax_tree::{
fol::{
Atom, AtomicFormula, BasicIntegerTerm, BinaryConnective, BinaryOperator,
Comparison, Formula, GeneralTerm, Guard, IntegerTerm, Quantification, Quantifier,
Relation, Sort, Theory, UnaryConnective, UnaryOperator, Variable,
Comparison, Formula, GeneralTerm, Guard, IntegerTerm, Predicate, Quantification,
Quantifier, Relation, Sort, Theory, UnaryConnective, UnaryOperator, Variable,
},
Node,
},
Expand Down Expand Up @@ -101,9 +101,17 @@ impl Display for Format<'_, GeneralTerm> {
}
}

impl Display for Format<'_, Predicate> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let symbol = &self.0.symbol;
let arity = &self.0.arity;
write!(f, "{symbol}/{arity}")
}
}

impl Display for Format<'_, Atom> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let predicate = &self.0.predicate;
let predicate = &self.0.predicate_symbol;
let terms = &self.0.terms;

write!(f, "{predicate}")?;
Expand Down Expand Up @@ -445,7 +453,7 @@ mod tests {
fn format_atomic_formula() {
assert_eq!(
Format(&AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![
GeneralTerm::Symbol("a".into()),
GeneralTerm::IntegerTerm(IntegerTerm::BasicIntegerTerm(
Expand Down Expand Up @@ -476,7 +484,7 @@ mod tests {
fn format_formula() {
assert_eq!(
Format(&Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
})))
.to_string(),
Expand All @@ -487,7 +495,7 @@ mod tests {
Format(&Formula::UnaryFormula {
connective: UnaryConnective::Negation,
formula: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into()
Expand All @@ -506,7 +514,7 @@ mod tests {
}]
},
formula: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![GeneralTerm::GeneralVariable("X".into())]
}))
.into()
Expand All @@ -521,19 +529,19 @@ mod tests {
lhs: Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into()
}
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into(),
Expand All @@ -546,19 +554,19 @@ mod tests {
Format(&Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into()
Expand All @@ -575,19 +583,19 @@ mod tests {
lhs: Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into()
}
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into(),
Expand All @@ -600,19 +608,19 @@ mod tests {
Format(&Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into()
Expand All @@ -629,19 +637,19 @@ mod tests {
lhs: Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into()
}
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into(),
Expand All @@ -654,19 +662,19 @@ mod tests {
Format(&Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into()
Expand All @@ -683,19 +691,19 @@ mod tests {
lhs: Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into()
}
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into(),
Expand All @@ -708,19 +716,19 @@ mod tests {
Format(&Formula::BinaryFormula {
connective: BinaryConnective::ReverseImplication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "p".into(),
predicate_symbol: "p".into(),
terms: vec![]
}))
.into(),
rhs: Formula::BinaryFormula {
connective: BinaryConnective::Implication,
lhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "q".into(),
predicate_symbol: "q".into(),
terms: vec![]
}))
.into(),
rhs: Formula::AtomicFormula(AtomicFormula::Atom(Atom {
predicate: "r".into(),
predicate_symbol: "r".into(),
terms: vec![]
}))
.into()
Expand Down
Loading
Loading