anthem-rs/src/output/tptp.rs

449 lines
12 KiB
Rust
Raw Normal View History

use foliage::flavor::{FunctionDeclaration as _, PredicateDeclaration as _,
VariableDeclaration as _};
2020-02-03 02:57:45 +01:00
pub(crate) struct DomainDisplay
2020-02-02 20:27:30 +01:00
{
2020-02-04 16:45:06 +01:00
domain: crate::Domain,
2020-02-02 20:27:30 +01:00
}
2020-02-04 16:45:06 +01:00
pub(crate) fn display_domain(domain: crate::Domain) -> DomainDisplay
2020-02-02 20:27:30 +01:00
{
2020-02-03 02:57:45 +01:00
DomainDisplay
{
domain,
}
2020-02-02 20:27:30 +01:00
}
2020-02-03 02:57:45 +01:00
impl std::fmt::Debug for DomainDisplay
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-03 02:57:45 +01:00
{
let domain_name = match self.domain
{
2020-02-04 16:45:06 +01:00
crate::Domain::Integer => "$int",
crate::Domain::Program => "object",
2020-02-03 02:57:45 +01:00
};
2020-05-11 03:12:50 +02:00
write!(formatter, "{}", domain_name)
2020-02-03 02:57:45 +01:00
}
}
impl std::fmt::Display for DomainDisplay
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-03 02:57:45 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?}", &self)
2020-02-03 02:57:45 +01:00
}
}
pub(crate) struct FunctionDeclarationDisplay<'a>(&'a crate::FunctionDeclaration);
2020-02-05 02:14:47 +01:00
pub(crate) fn display_function_declaration<'a>(function_declaration: &'a crate::FunctionDeclaration)
-> FunctionDeclarationDisplay<'a>
2020-02-05 02:14:47 +01:00
{
FunctionDeclarationDisplay(function_declaration)
2020-02-05 02:14:47 +01:00
}
impl<'a> std::fmt::Debug for FunctionDeclarationDisplay<'a>
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-05 02:14:47 +01:00
{
self.0.display_name(formatter)?;
write!(formatter, ":")?;
2020-02-05 02:14:47 +01:00
let domain_identifier = match *self.0.domain.borrow()
2020-02-05 02:14:47 +01:00
{
crate::Domain::Integer => "$int",
crate::Domain::Program => "object",
};
let mut separator = "";
if self.0.arity() > 0
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, " (")?;
2020-02-05 02:14:47 +01:00
for _ in 0..self.0.arity()
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}object", separator)?;
2020-02-05 02:14:47 +01:00
separator = " * ";
}
2020-05-11 03:12:50 +02:00
write!(formatter, ") >")?;
2020-02-05 02:14:47 +01:00
}
2020-05-11 03:12:50 +02:00
write!(formatter, " {}", domain_identifier)
2020-02-05 02:14:47 +01:00
}
}
impl<'a> std::fmt::Display for FunctionDeclarationDisplay<'a>
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?}", &self)
2020-02-05 02:14:47 +01:00
}
}
pub(crate) struct PredicateDeclarationDisplay<'a>(&'a crate::PredicateDeclaration);
2020-02-05 02:14:47 +01:00
pub(crate) fn display_predicate_declaration<'a>(
predicate_declaration: &'a crate::PredicateDeclaration)
2020-02-05 02:14:47 +01:00
-> PredicateDeclarationDisplay<'a>
{
PredicateDeclarationDisplay(predicate_declaration)
}
impl<'a> std::fmt::Debug for PredicateDeclarationDisplay<'a>
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-05 02:14:47 +01:00
{
self.0.display_name(formatter)?;
write!(formatter, ":")?;
2020-02-05 02:14:47 +01:00
let mut separator = "";
if self.0.arity() > 0
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, " (")?;
2020-02-05 02:14:47 +01:00
for _ in 0..self.0.arity()
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}object", separator)?;
2020-02-05 02:14:47 +01:00
separator = " * ";
}
2020-05-11 03:12:50 +02:00
write!(formatter, ") >")?;
2020-02-05 02:14:47 +01:00
}
2020-05-11 03:12:50 +02:00
write!(formatter, " $o")
2020-02-05 02:14:47 +01:00
}
}
impl<'a> std::fmt::Display for PredicateDeclarationDisplay<'a>
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-05 02:14:47 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?}", &self)
2020-02-05 02:14:47 +01:00
}
}
pub(crate) struct TermDisplay<'a>(&'a crate::Term);
2020-02-04 23:35:42 +01:00
pub(crate) fn display_term<'a>(term: &'a crate::Term) -> TermDisplay<'a>
2020-02-04 23:35:42 +01:00
{
TermDisplay(term)
2020-02-04 23:35:42 +01:00
}
impl<'a> std::fmt::Debug for TermDisplay<'a>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:18:11 +02:00
use foliage::*;
match &self.0
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:18:11 +02:00
Term::Boolean(true) => write!(formatter, "$true"),
Term::Boolean(false) => write!(formatter, "$false"),
Term::SpecialInteger(SpecialInteger::Infimum) => write!(formatter, "c__infimum__"),
Term::SpecialInteger(SpecialInteger::Supremum) => write!(formatter, "c__supremum__"),
Term::Integer(value) => match value.is_negative()
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
true => write!(formatter, "$uminus({})", -value),
false => write!(formatter, "{}", value),
2020-02-02 20:27:30 +01:00
},
2020-05-11 03:18:11 +02:00
Term::String(_) => panic!("strings not supported in TPTP"),
Term::Variable(variable) => variable.declaration.display_name(formatter),
2020-05-11 03:18:11 +02:00
Term::Function(function) =>
2020-02-02 20:27:30 +01:00
{
function.declaration.display_name(formatter)?;
2020-02-02 20:27:30 +01:00
assert!(function.declaration.arity() == function.arguments.len(),
2020-02-02 20:27:30 +01:00
"function has a different number of arguments than declared (expected {}, got {})",
function.declaration.arity(), function.arguments.len());
2020-02-02 20:27:30 +01:00
if function.arguments.len() > 0
{
function.declaration.display_name(formatter)?;
write!(formatter, "(")?;
2020-02-02 20:27:30 +01:00
let mut separator = "";
for argument in &function.arguments
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}{:?}", separator, display_term(&argument))?;
2020-02-02 20:27:30 +01:00
separator = ", ";
}
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
2020-02-02 20:27:30 +01:00
}
Ok(())
},
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Add, left, right}) =>
2020-05-11 03:12:50 +02:00
write!(formatter, "$sum({:?}, {:?})", display_term(left), display_term(right)),
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Subtract, left, right})
=>
2020-05-11 03:12:50 +02:00
write!(formatter, "$difference({:?}, {:?})", display_term(left),
display_term(right)),
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Multiply, left, right})
=>
2020-05-11 03:12:50 +02:00
write!(formatter, "$product({:?}, {:?})", display_term(left), display_term(right)),
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Divide, ..}) =>
2020-05-11 03:12:50 +02:00
panic!("division not supported with TPTP output"),
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Modulo, ..}) =>
2020-05-11 03:12:50 +02:00
panic!("modulo not supported with TPTP output"),
2020-05-11 03:18:11 +02:00
Term::BinaryOperation(BinaryOperation{operator: BinaryOperator::Exponentiate, ..}) =>
2020-05-11 03:12:50 +02:00
panic!("exponentiation not supported with TPTP output"),
2020-05-11 03:18:11 +02:00
Term::UnaryOperation(UnaryOperation{operator: UnaryOperator::Negative, argument}) =>
2020-05-11 03:12:50 +02:00
write!(formatter, "$uminus({:?})", display_term(argument)),
2020-05-11 03:18:11 +02:00
Term::UnaryOperation(UnaryOperation{operator: UnaryOperator::AbsoluteValue, ..}) =>
2020-05-11 03:12:50 +02:00
panic!("absolute value not supported with TPTP output"),
2020-02-02 20:27:30 +01:00
}
}
}
impl<'a> std::fmt::Display for TermDisplay<'a>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?}", self)
2020-02-02 20:27:30 +01:00
}
}
pub(crate) struct FormulaDisplay<'a>(&'a crate::Formula);
2020-02-04 23:35:42 +01:00
pub(crate) fn display_formula<'a>(formula: &'a crate::Formula) -> FormulaDisplay<'a>
2020-02-04 23:35:42 +01:00
{
FormulaDisplay(formula)
2020-02-04 23:35:42 +01:00
}
impl<'a> std::fmt::Debug for FormulaDisplay<'a>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-02 20:27:30 +01:00
{
2020-02-04 23:33:59 +01:00
let mut display_compare = |left, right, notation, integer_operator_name,
auxiliary_predicate_name| -> std::fmt::Result
{
let mut notation = notation;
let mut operation_identifier = integer_operator_name;
let is_left_term_arithmetic = crate::is_term_arithmetic(left)
2020-02-04 23:33:59 +01:00
.expect("could not determine whether term is arithmetic");
let is_right_term_arithmetic = crate::is_term_arithmetic(right)
2020-02-04 23:33:59 +01:00
.expect("could not determine whether term is arithmetic");
2020-05-07 02:53:48 +02:00
match (!is_left_term_arithmetic || !is_right_term_arithmetic, auxiliary_predicate_name)
2020-02-04 23:33:59 +01:00
{
(true, Some(auxiliary_predicate_name)) =>
{
notation = crate::OperatorNotation::Prefix;
operation_identifier = auxiliary_predicate_name;
},
_ => (),
}
if notation == crate::OperatorNotation::Prefix
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}(", operation_identifier)?;
2020-02-04 23:33:59 +01:00
}
match is_left_term_arithmetic && !is_right_term_arithmetic
{
2020-05-11 03:12:50 +02:00
true => write!(formatter, "f__integer__({})", display_term(left))?,
false => write!(formatter, "{}", display_term(left))?,
2020-02-04 23:33:59 +01:00
}
match notation
{
2020-05-11 03:12:50 +02:00
crate::OperatorNotation::Prefix => write!(formatter, ", ")?,
crate::OperatorNotation::Infix => write!(formatter, " {} ", operation_identifier)?,
2020-02-04 23:33:59 +01:00
}
match is_right_term_arithmetic && !is_left_term_arithmetic
{
2020-05-11 03:12:50 +02:00
true => write!(formatter, "f__integer__({})", display_term(right))?,
false => write!(formatter, "{}", display_term(right))?,
2020-02-04 23:33:59 +01:00
}
if notation == crate::OperatorNotation::Prefix
{
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
2020-02-04 23:33:59 +01:00
}
2020-02-05 02:14:29 +01:00
Ok(())
2020-02-04 23:33:59 +01:00
};
2020-05-11 03:21:51 +02:00
use foliage::*;
match &self.0
2020-02-02 20:27:30 +01:00
{
2020-05-19 12:49:57 +02:00
// TODO: handle cases with 0 parameters properly
Formula::Exists(quantified_formula)
| Formula::ForAll(quantified_formula) =>
2020-02-02 20:27:30 +01:00
{
let operator_symbol = match &self.0
2020-02-02 20:27:30 +01:00
{
Formula::Exists(_) => "?",
Formula::ForAll(_) => "!",
_ => unreachable!(),
};
2020-02-03 02:57:45 +01:00
write!(formatter, "{}[", operator_symbol)?;
2020-02-02 20:27:30 +01:00
let mut separator = "";
for parameter in quantified_formula.parameters.iter()
2020-02-02 20:27:30 +01:00
{
let domain = match parameter.domain()
{
Ok(domain) => domain,
Err(_) => unreachable!(
"all variable domains should have been checked at this point"),
};
2020-02-03 02:57:45 +01:00
write!(formatter, "{}", separator)?;
parameter.display_name(formatter)?;
write!(formatter, ": {}", display_domain(domain))?;
2020-02-02 20:27:30 +01:00
separator = ", "
}
write!(formatter, "]: ({:?})", display_formula(&quantified_formula.argument))?;
2020-02-02 20:27:30 +01:00
},
2020-05-11 03:21:51 +02:00
Formula::Not(argument) => write!(formatter, "~{:?}", display_formula(argument))?,
2020-05-19 12:49:57 +02:00
// TODO: handle cases with < 2 arguments properly
2020-05-11 03:21:51 +02:00
Formula::And(arguments) =>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "(")?;
2020-02-02 20:27:30 +01:00
let mut separator = "";
assert!(!arguments.is_empty());
for argument in arguments
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}{:?}", separator, display_formula(argument))?;
2020-02-02 20:27:30 +01:00
separator = " & "
}
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
2020-02-02 20:27:30 +01:00
},
2020-05-19 12:49:57 +02:00
// TODO: handle cases with < 2 arguments properly
2020-05-11 03:21:51 +02:00
Formula::Or(arguments) =>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "(")?;
2020-02-02 20:27:30 +01:00
let mut separator = "";
assert!(!arguments.is_empty());
for argument in arguments
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}{:?}", separator, display_formula(argument))?;
2020-02-02 20:27:30 +01:00
separator = " | "
}
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
2020-02-02 20:27:30 +01:00
},
2020-05-11 03:21:51 +02:00
Formula::Implies(Implies{antecedent, implication, ..}) =>
write!(formatter, "({:?} => {:?})", display_formula(antecedent),
2020-02-03 02:57:45 +01:00
display_formula(implication))?,
2020-05-19 12:49:57 +02:00
// TODO: handle cases with < 2 arguments properly
2020-05-11 03:21:51 +02:00
Formula::IfAndOnlyIf(arguments) => match arguments.len()
{
2020-05-11 03:12:50 +02:00
0 => write!(formatter, "$true")?,
_ =>
{
let parentheses_required = arguments.len() > 2;
let mut argument_iterator = arguments.iter().peekable();
2020-05-11 03:23:25 +02:00
let mut separator = "";
while let Some(argument) = argument_iterator.next()
{
if let Some(next_argument) = argument_iterator.peek()
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}", separator)?;
if parentheses_required
{
2020-05-11 03:12:50 +02:00
write!(formatter, "(")?;
}
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?} <=> {:?}", display_formula(argument),
display_formula(next_argument))?;
if parentheses_required
{
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
}
separator = " & ";
}
}
},
},
2020-05-11 03:21:51 +02:00
Formula::Compare(Compare{operator: ComparisonOperator::Less, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Prefix, "$less",
2020-02-04 23:33:59 +01:00
Some("p__less__"))?,
2020-05-11 03:21:51 +02:00
Formula::Compare(Compare{operator: ComparisonOperator::LessOrEqual, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Prefix, "$lesseq",
2020-02-04 23:33:59 +01:00
Some("p__less_equal__"))?,
2020-05-11 03:21:51 +02:00
Formula::Compare(Compare{operator: ComparisonOperator::Greater, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Prefix, "$greater",
2020-02-04 23:33:59 +01:00
Some("p__greater__"))?,
2020-05-11 03:21:51 +02:00
Formula::Compare(Compare{operator: ComparisonOperator::GreaterOrEqual, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Prefix, "$greatereq",
2020-02-04 23:33:59 +01:00
Some("p__greater_equal__"))?,
2020-05-11 03:21:51 +02:00
Formula::Compare(Compare{operator: ComparisonOperator::Equal, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Infix, "=", None)?,
Formula::Compare(Compare{operator: ComparisonOperator::NotEqual, left, right}) =>
display_compare(left, right, crate::OperatorNotation::Infix, "!=", None)?,
Formula::Boolean(true) => write!(formatter, "$true")?,
Formula::Boolean(false) => write!(formatter, "$false")?,
Formula::Predicate(predicate) =>
2020-02-02 20:27:30 +01:00
{
predicate.declaration.display_name(formatter)?;
2020-02-02 20:27:30 +01:00
if !predicate.arguments.is_empty()
{
2020-05-11 03:12:50 +02:00
write!(formatter, "(")?;
2020-02-02 20:27:30 +01:00
let mut separator = "";
for argument in &predicate.arguments
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{}", separator)?;
2020-05-06 21:38:48 +02:00
let is_argument_arithmetic = crate::is_term_arithmetic(argument)
.expect("could not determine whether term is arithmetic");
2020-05-06 21:38:48 +02:00
match is_argument_arithmetic
{
2020-05-11 03:12:50 +02:00
true => write!(formatter, "f__integer__({})", display_term(argument))?,
false => write!(formatter, "{}", display_term(argument))?,
2020-05-06 21:38:48 +02:00
}
2020-02-02 20:27:30 +01:00
separator = ", "
}
2020-05-11 03:12:50 +02:00
write!(formatter, ")")?;
2020-02-02 20:27:30 +01:00
}
},
}
Ok(())
}
}
impl<'a> std::fmt::Display for FormulaDisplay<'a>
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
2020-02-02 20:27:30 +01:00
{
2020-05-11 03:12:50 +02:00
write!(formatter, "{:?}", self)
2020-02-02 20:27:30 +01:00
}
}