Support empty n-aries

This commit is contained in:
Patrick Lühne 2020-04-08 12:09:27 +02:00
parent 51cbdb313f
commit 9a8013f1cb
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 57 additions and 45 deletions

View File

@ -486,8 +486,6 @@ impl Formula
{ {
pub fn and(arguments: Formulas) -> Self pub fn and(arguments: Formulas) -> Self
{ {
assert!(!arguments.is_empty());
Self::And(arguments) Self::And(arguments)
} }
@ -537,8 +535,6 @@ impl Formula
pub fn if_and_only_if(arguments: Formulas) -> Self pub fn if_and_only_if(arguments: Formulas) -> Self
{ {
assert!(!arguments.is_empty());
Self::IfAndOnlyIf(arguments) Self::IfAndOnlyIf(arguments)
} }
@ -570,8 +566,6 @@ impl Formula
pub fn or(arguments: Formulas) -> Self pub fn or(arguments: Formulas) -> Self
{ {
assert!(!arguments.is_empty());
Self::Or(arguments) Self::Or(arguments)
} }

View File

@ -177,16 +177,20 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
display_formula(argument, Some(self.formula), ChildPosition::Any))?, display_formula(argument, Some(self.formula), ChildPosition::Any))?,
crate::Formula::And(arguments) => crate::Formula::And(arguments) =>
{ {
let mut separator = ""; if arguments.is_empty()
{
assert!(!arguments.is_empty()); write!(format, "true")?;
}
else
{
let (parent_formula, position) = match arguments.len() let (parent_formula, position) = match arguments.len()
{ {
0 | 1 => (self.parent_formula, self.position), 1 => (self.parent_formula, self.position),
_ => (Some(self.formula), ChildPosition::Any), _ => (Some(self.formula), ChildPosition::Any),
}; };
let mut separator = "";
for argument in arguments for argument in arguments
{ {
write!(format, "{}{:?}", separator, write!(format, "{}{:?}", separator,
@ -194,19 +198,24 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
separator = " and " separator = " and "
} }
}
}, },
crate::Formula::Or(arguments) => crate::Formula::Or(arguments) =>
{ {
let mut separator = ""; if arguments.is_empty()
{
assert!(!arguments.is_empty()); write!(format, "false")?;
}
else
{
let (parent_formula, position) = match arguments.len() let (parent_formula, position) = match arguments.len()
{ {
0 | 1 => (self.parent_formula, self.position), 1 => (self.parent_formula, self.position),
_ => (Some(self.formula), ChildPosition::Any), _ => (Some(self.formula), ChildPosition::Any),
}; };
let mut separator = "";
for argument in arguments for argument in arguments
{ {
write!(format, "{}{:?}", separator, write!(format, "{}{:?}", separator,
@ -214,6 +223,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
separator = " or " separator = " or "
} }
}
}, },
crate::Formula::Implies(crate::Implies{direction, antecedent, implication}) => crate::Formula::Implies(crate::Implies{direction, antecedent, implication}) =>
{ {
@ -248,16 +258,20 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
}, },
crate::Formula::IfAndOnlyIf(arguments) => crate::Formula::IfAndOnlyIf(arguments) =>
{ {
let mut separator = ""; if arguments.is_empty()
{
assert!(!arguments.is_empty()); write!(format, "true")?;
}
else
{
let (parent_formula, position) = match arguments.len() let (parent_formula, position) = match arguments.len()
{ {
0 | 1 => (self.parent_formula, self.position), 1 => (self.parent_formula, self.position),
_ => (Some(self.formula), ChildPosition::Any), _ => (Some(self.formula), ChildPosition::Any),
}; };
let mut separator = "";
for argument in arguments for argument in arguments
{ {
write!(format, "{}{:?}", separator, write!(format, "{}{:?}", separator,
@ -265,6 +279,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
separator = " <-> " separator = " <-> "
} }
}
}, },
crate::Formula::Compare( crate::Formula::Compare(
crate::Compare{operator: crate::ComparisonOperator::Less, left, right}) crate::Compare{operator: crate::ComparisonOperator::Less, left, right})
@ -666,6 +681,7 @@ mod tests
#[test] #[test]
fn format_and() fn format_and()
{ {
assert!(and(vec![]), "true");
assert!(and(vec![p()]), "p"); assert!(and(vec![p()]), "p");
assert!(and(pqr()), "p and q and r"); assert!(and(pqr()), "p and q and r");
} }
@ -673,6 +689,7 @@ mod tests
#[test] #[test]
fn format_or() fn format_or()
{ {
assert!(or(vec![]), "false");
assert!(or(vec![p()]), "p"); assert!(or(vec![p()]), "p");
assert!(or(pqr()), "p or q or r"); assert!(or(pqr()), "p or q or r");
} }
@ -687,6 +704,7 @@ mod tests
#[test] #[test]
fn format_if_and_only_if() fn format_if_and_only_if()
{ {
assert!(if_and_only_if(vec![]), "true");
assert!(if_and_only_if(vec![p()]), "p"); assert!(if_and_only_if(vec![p()]), "p");
assert!(if_and_only_if(vec![p(), q()]), "p <-> q"); assert!(if_and_only_if(vec![p(), q()]), "p <-> q");
assert!(if_and_only_if(pqr()), "p <-> q <-> r"); assert!(if_and_only_if(pqr()), "p <-> q <-> r");