349 lines
10 KiB
Rust
349 lines
10 KiB
Rust
struct TermDisplay<'term>
|
|
{
|
|
parent_precedence: i32,
|
|
term: &'term crate::Term,
|
|
}
|
|
|
|
struct FormulaDisplay<'formula>
|
|
{
|
|
parent_precedence: i32,
|
|
formula: &'formula crate::Formula,
|
|
}
|
|
|
|
fn display_term<'term>(term: &'term crate::Term, parent_precedence: i32) -> TermDisplay<'term>
|
|
{
|
|
TermDisplay
|
|
{
|
|
parent_precedence,
|
|
term,
|
|
}
|
|
}
|
|
|
|
fn display_formula<'formula>(formula: &'formula crate::Formula, parent_precedence: i32)
|
|
-> FormulaDisplay<'formula>
|
|
{
|
|
FormulaDisplay
|
|
{
|
|
parent_precedence,
|
|
formula,
|
|
}
|
|
}
|
|
|
|
trait Precedence
|
|
{
|
|
fn precedence(&self) -> i32;
|
|
}
|
|
|
|
impl Precedence for crate::Term
|
|
{
|
|
fn precedence(&self) -> i32
|
|
{
|
|
match &self
|
|
{
|
|
Self::Boolean(_)
|
|
| Self::Function(_)
|
|
| Self::SpecialInteger(_)
|
|
| Self::Integer(_)
|
|
| Self::Symbolic(_)
|
|
| Self::String(_)
|
|
| Self::Variable(_)
|
|
=> 0,
|
|
Self::UnaryOperation(crate::UnaryOperation{operator: crate::UnaryOperator::Negation, ..})
|
|
=> 1,
|
|
Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Exponentiation, ..})
|
|
=> 2,
|
|
Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Multiplication, ..})
|
|
| Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Division, ..})
|
|
| Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Modulo, ..})
|
|
=> 3,
|
|
Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Addition, ..})
|
|
| Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Subtraction, ..})
|
|
=> 4,
|
|
Self::UnaryOperation(crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, ..})
|
|
=> 5,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Precedence for crate::Formula
|
|
{
|
|
fn precedence(&self) -> i32
|
|
{
|
|
match &self
|
|
{
|
|
Self::Predicate(_)
|
|
| Self::Boolean(_)
|
|
| Self::Comparison(_)
|
|
=> 0,
|
|
Self::Exists(_)
|
|
| Self::ForAll(_)
|
|
=> 1,
|
|
Self::Not(_)
|
|
=> 2,
|
|
Self::And(_)
|
|
=> 3,
|
|
Self::Or(_)
|
|
=> 4,
|
|
Self::Implies(_)
|
|
=> 5,
|
|
Self::Biconditional(_)
|
|
=> 6,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::VariableDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}", &self.name)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::VariableDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", &self)
|
|
}
|
|
}
|
|
|
|
impl<'term> std::fmt::Debug for TermDisplay<'term>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
let precedence = self.term.precedence();
|
|
let requires_parentheses = precedence > self.parent_precedence;
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, "(")?;
|
|
}
|
|
|
|
match &self.term
|
|
{
|
|
crate::Term::Boolean(true) => write!(format, "true"),
|
|
crate::Term::Boolean(false) => write!(format, "false"),
|
|
crate::Term::SpecialInteger(crate::SpecialInteger::Infimum) => write!(format, "#inf"),
|
|
crate::Term::SpecialInteger(crate::SpecialInteger::Supremum) => write!(format, "#sup"),
|
|
crate::Term::Integer(value) => write!(format, "{}", value),
|
|
crate::Term::Symbolic(value) => write!(format, "{}", value),
|
|
crate::Term::String(value) => write!(format, "\"{}\"", value),
|
|
crate::Term::Variable(variable) => write!(format, "{:?}", variable.declaration),
|
|
crate::Term::Function(function) =>
|
|
{
|
|
write!(format, "{}", function.declaration.name)?;
|
|
|
|
assert!(function.declaration.arity == function.arguments.len(),
|
|
"function has a different number of arguments than declared (expected {}, got {})",
|
|
function.declaration.arity, function.arguments.len());
|
|
|
|
if function.arguments.len() > 0
|
|
{
|
|
write!(format, "{}(", function.declaration.name)?;
|
|
|
|
let mut separator = "";
|
|
|
|
for argument in &function.arguments
|
|
{
|
|
write!(format, "{}{:?}", separator, display_term(&argument, 1000))?;
|
|
|
|
separator = ", ";
|
|
}
|
|
|
|
write!(format, ")")?;
|
|
}
|
|
|
|
Ok(())
|
|
},
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Addition, left, right})
|
|
=> write!(format, "{:?} + {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Subtraction, left, right})
|
|
=> write!(format, "{:?} - {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Multiplication, left, right})
|
|
=> write!(format, "{:?} * {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Division, left, right})
|
|
=> write!(format, "{:?} / {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Modulo, left, right})
|
|
=> write!(format, "{:?} % {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Exponentiation, left, right})
|
|
=> write!(format, "{:?} ** {:?}", display_term(left, precedence), display_term(right, precedence)),
|
|
crate::Term::UnaryOperation(crate::UnaryOperation{operator: crate::UnaryOperator::Negation, argument})
|
|
=> write!(format, "-{:?}", display_term(argument, precedence)),
|
|
crate::Term::UnaryOperation(crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, argument})
|
|
=> write!(format, "|{:?}|", display_term(argument, precedence)),
|
|
}?;
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, ")")?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'term> std::fmt::Display for TermDisplay<'term>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", self)
|
|
}
|
|
}
|
|
|
|
impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
let precedence = self.formula.precedence();
|
|
let requires_parentheses = precedence > self.parent_precedence;
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, "(")?;
|
|
}
|
|
|
|
match &self.formula
|
|
{
|
|
crate::Formula::Exists(exists) =>
|
|
{
|
|
write!(format, "exists")?;
|
|
|
|
let mut separator = " ";
|
|
|
|
for parameter in &exists.parameters
|
|
{
|
|
write!(format, "{}{:?}", separator, parameter)?;
|
|
|
|
separator = ", "
|
|
}
|
|
|
|
write!(format, " {:?}", display_formula(&exists.argument, precedence))?;
|
|
},
|
|
crate::Formula::ForAll(for_all) =>
|
|
{
|
|
write!(format, "forall")?;
|
|
|
|
let mut separator = " ";
|
|
|
|
for parameter in &for_all.parameters
|
|
{
|
|
write!(format, "{}{:?}", separator, parameter)?;
|
|
|
|
separator = ", "
|
|
}
|
|
|
|
write!(format, " {:?}", display_formula(&for_all.argument, precedence))?;
|
|
},
|
|
crate::Formula::Not(argument) => write!(format, "not {:?}", display_formula(argument, precedence))?,
|
|
crate::Formula::And(arguments) =>
|
|
{
|
|
let mut separator = "";
|
|
|
|
for argument in arguments
|
|
{
|
|
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
|
|
|
separator = " and "
|
|
}
|
|
},
|
|
crate::Formula::Or(arguments) =>
|
|
{
|
|
let mut separator = "";
|
|
|
|
for argument in arguments
|
|
{
|
|
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
|
|
|
separator = " or "
|
|
}
|
|
},
|
|
crate::Formula::Implies(crate::Implies{antecedent, implication})
|
|
=> write!(format, "{:?} -> {:?}", display_formula(antecedent, precedence), display_formula(implication, precedence))?,
|
|
crate::Formula::Biconditional(crate::Biconditional{left, right})
|
|
=> write!(format, "{:?} <-> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::Less, left, right})
|
|
=> write!(format, "{:?} < {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::LessOrEqual, left, right})
|
|
=> write!(format, "{:?} <= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::Greater, left, right})
|
|
=> write!(format, "{:?} > {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::GreaterOrEqual, left, right})
|
|
=> write!(format, "{:?} >= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::Equal, left, right})
|
|
=> write!(format, "{:?} = {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Comparison(crate::Comparison{operator: crate::ComparisonOperator::NotEqual, left, right})
|
|
=> write!(format, "{:?} != {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
|
crate::Formula::Boolean(true) => write!(format, "#true")?,
|
|
crate::Formula::Boolean(false) => write!(format, "#false")?,
|
|
crate::Formula::Predicate(predicate) =>
|
|
{
|
|
write!(format, "{}", predicate.declaration.name)?;
|
|
|
|
if !predicate.arguments.is_empty()
|
|
{
|
|
write!(format, "(")?;
|
|
|
|
let mut separator = "";
|
|
|
|
for argument in &predicate.arguments
|
|
{
|
|
write!(format, "{}{:?}", separator, display_term(argument, 1000))?;
|
|
|
|
separator = ", "
|
|
}
|
|
|
|
write!(format, ")")?;
|
|
}
|
|
},
|
|
}
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, ")")?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'formula> std::fmt::Display for FormulaDisplay<'formula>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", self)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::Formula
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", display_formula(&self, 1000))
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::Formula
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}", display_formula(&self, 1000))
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::Term
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", display_term(&self, 1000))
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::Term
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}", display_term(&self, 1000))
|
|
}
|
|
}
|