2020-04-27 19:30:25 +02:00
|
|
|
|
use super::terms::*;
|
2020-04-22 20:01:29 +02:00
|
|
|
|
use super::tokens::*;
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
pub fn formula(input: &str) -> Result<crate::Formula, crate::parse::Error>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
|
|
|
|
let formula_str = FormulaStr::new(input);
|
|
|
|
|
formula_str.parse(0)?;
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
// TODO: implement correctly
|
|
|
|
|
Ok(crate::Formula::true_())
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
pub(crate) fn predicate_name(identifier: &str) -> Option<(&str, &str)>
|
|
|
|
|
{
|
|
|
|
|
function_name(identifier)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
2020-04-27 19:30:25 +02:00
|
|
|
|
enum LogicalConnective
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
And,
|
|
|
|
|
IfAndOnlyIf,
|
|
|
|
|
ImpliesLeftToRight,
|
|
|
|
|
ImpliesRightToLeft,
|
|
|
|
|
Or,
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
// TODO: rename to logic infix connective
|
|
|
|
|
impl LogicalConnective
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
fn level(&self) -> usize
|
|
|
|
|
{
|
|
|
|
|
match self
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
Self::And => 1,
|
|
|
|
|
Self::Or => 2,
|
|
|
|
|
Self::ImpliesLeftToRight
|
|
|
|
|
| Self::ImpliesRightToLeft => 3,
|
|
|
|
|
Self::IfAndOnlyIf => 4,
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
impl std::fmt::Debug for LogicalConnective
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
|
|
|
{
|
|
|
|
|
match &self
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
Self::And => write!(formatter, "and"),
|
|
|
|
|
Self::IfAndOnlyIf => write!(formatter, "<->"),
|
|
|
|
|
Self::ImpliesLeftToRight => write!(formatter, "->"),
|
|
|
|
|
Self::ImpliesRightToLeft => write!(formatter, "<-"),
|
|
|
|
|
Self::Or => write!(formatter, "or"),
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
struct FormulaStr<'i>
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
input: &'i str,
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
impl<'i> FormulaStr<'i>
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
pub fn new(input: &'i str) -> Self
|
|
|
|
|
{
|
|
|
|
|
Self
|
|
|
|
|
{
|
|
|
|
|
input,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
fn tokens(&self) -> Tokens<'i, impl FnMut(Token<'i>) -> Option<Token<'i>>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Tokens::new_iter(self.input)
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
fn logical_connectives(&self) -> Tokens<'i, impl FnMut(Token<'i>) -> Option<LogicalConnective>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let functor = |token| match token
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Token::Identifier(ref identifier) => match *identifier
|
|
|
|
|
{
|
|
|
|
|
"and" => Some(LogicalConnective::And),
|
|
|
|
|
"or" => Some(LogicalConnective::Or),
|
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
Token::Symbol(ref symbol) => match symbol
|
|
|
|
|
{
|
|
|
|
|
Symbol::ArrowLeft => Some(LogicalConnective::ImpliesRightToLeft),
|
|
|
|
|
Symbol::ArrowLeftAndRight => Some(LogicalConnective::IfAndOnlyIf),
|
|
|
|
|
Symbol::ArrowRight => Some(LogicalConnective::ImpliesLeftToRight),
|
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Tokens::new_filter_map(self.input, functor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn split_at_logical_connective(&self, logical_connective: LogicalConnective)
|
|
|
|
|
-> TokenSplit<Tokens<'i, impl FnMut(Token<'i>) -> Option<Token<'i>>>>
|
|
|
|
|
{
|
|
|
|
|
let predicate = move |token: &_| match token
|
|
|
|
|
{
|
|
|
|
|
Token::Identifier(ref identifier) => match *identifier
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
"and" => logical_connective == LogicalConnective::And,
|
|
|
|
|
"or" => logical_connective == LogicalConnective::Or,
|
|
|
|
|
_ => false,
|
|
|
|
|
},
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Token::Symbol(ref symbol) => match symbol
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
Symbol::ArrowLeft => logical_connective == LogicalConnective::ImpliesRightToLeft,
|
|
|
|
|
Symbol::ArrowLeftAndRight => logical_connective == LogicalConnective::IfAndOnlyIf,
|
|
|
|
|
Symbol::ArrowRight => logical_connective == LogicalConnective::ImpliesLeftToRight,
|
|
|
|
|
_ => false,
|
|
|
|
|
},
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Tokens::new_filter(self.input, predicate).split()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn top_level_logical_connective(&self)
|
|
|
|
|
-> Result<Option<LogicalConnective>, crate::parse::Error>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
let logical_connective = |token| match token
|
|
|
|
|
{
|
|
|
|
|
Token::Identifier(identifier) => match identifier
|
|
|
|
|
{
|
|
|
|
|
"and" => Some(LogicalConnective::And),
|
|
|
|
|
"or" => Some(LogicalConnective::Or),
|
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
Token::Symbol(symbol) => match symbol
|
|
|
|
|
{
|
|
|
|
|
Symbol::ArrowLeft => Some(LogicalConnective::ImpliesRightToLeft),
|
|
|
|
|
Symbol::ArrowLeftAndRight => Some(LogicalConnective::IfAndOnlyIf),
|
|
|
|
|
Symbol::ArrowRight => Some(LogicalConnective::ImpliesLeftToRight),
|
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2020-04-22 20:01:29 +02:00
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let logical_connectives = Tokens::new_filter_map(self.input, logical_connective);
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
let mut top_level_logical_connective = None;
|
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
for logical_connective in logical_connectives
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let (_, logical_connective) = logical_connective?;
|
2020-04-22 20:01:29 +02:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
top_level_logical_connective = match top_level_logical_connective
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
None => Some(logical_connective),
|
|
|
|
|
Some(top_level_logical_connective) =>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
if (logical_connective == LogicalConnective::ImpliesLeftToRight
|
|
|
|
|
&& top_level_logical_connective == LogicalConnective::ImpliesRightToLeft)
|
|
|
|
|
|| (logical_connective == LogicalConnective::ImpliesRightToLeft
|
|
|
|
|
&& top_level_logical_connective == LogicalConnective::ImpliesLeftToRight)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
|
|
|
|
return Err(crate::parse::Error::new_mixed_implication_directions(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)),
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))));
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
if logical_connective.level() > top_level_logical_connective.level()
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
Some(logical_connective)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
Some(top_level_logical_connective)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
Ok(top_level_logical_connective)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
fn comparison_operators(&self) -> Tokens<'i, impl FnMut(Token<'i>)
|
|
|
|
|
-> Option<crate::ComparisonOperator>>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let functor = |token| match token
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Token::Symbol(symbol) => match symbol
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Symbol::Greater => Some(crate::ComparisonOperator::Greater),
|
|
|
|
|
Symbol::GreaterOrEqual => Some(crate::ComparisonOperator::GreaterOrEqual),
|
|
|
|
|
Symbol::Less => Some(crate::ComparisonOperator::Less),
|
|
|
|
|
Symbol::LessOrEqual => Some(crate::ComparisonOperator::LessOrEqual),
|
|
|
|
|
Symbol::Equal => Some(crate::ComparisonOperator::Equal),
|
|
|
|
|
Symbol::NotEqual => Some(crate::ComparisonOperator::NotEqual),
|
2020-04-27 19:30:25 +02:00
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
Tokens::new_filter_map(self.input, functor)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
pub fn parse(&self, level: usize) -> Result<crate::Formula, crate::parse::Error>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
|
|
|
|
let indentation = " ".repeat(level);
|
|
|
|
|
let input = self.input.trim_start();
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
println!("{}- parsing formula: {}", indentation, input);
|
|
|
|
|
|
|
|
|
|
match input.chars().next()
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
Some(')') => return Err(crate::parse::Error::new_unmatched_parenthesis(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
|
|
|
|
None => return Err(crate::parse::Error::new_empty_input(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse logical infix connectives
|
|
|
|
|
if let Some(top_level_logical_connective) = self.top_level_logical_connective()?
|
|
|
|
|
{
|
|
|
|
|
println!("{} parsing “{:?}” infix formula", indentation, top_level_logical_connective);
|
|
|
|
|
|
|
|
|
|
// Parse arguments of n-ary logical infix connectives
|
|
|
|
|
let arguments_n_ary = ||
|
|
|
|
|
{
|
|
|
|
|
// TODO: improve error handling if the formulas between the operators are invalid
|
2020-04-28 02:02:20 +02:00
|
|
|
|
self.split_at_logical_connective(top_level_logical_connective)
|
2020-04-27 19:30:25 +02:00
|
|
|
|
.map(|subformula| FormulaStr::new(subformula?).parse(level + 1))
|
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match top_level_logical_connective
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
LogicalConnective::And => return Ok(crate::Formula::and(arguments_n_ary()?)),
|
|
|
|
|
LogicalConnective::Or => return Ok(crate::Formula::or(arguments_n_ary()?)),
|
|
|
|
|
LogicalConnective::IfAndOnlyIf =>
|
|
|
|
|
return Ok(crate::Formula::if_and_only_if(arguments_n_ary()?)),
|
|
|
|
|
LogicalConnective::ImpliesLeftToRight =>
|
|
|
|
|
return implication_left_to_right(
|
2020-04-28 02:02:20 +02:00
|
|
|
|
self.split_at_logical_connective(top_level_logical_connective), level + 1),
|
2020-04-27 19:30:25 +02:00
|
|
|
|
/*LogicalConnective::ImpliesRightToLeft => unimplemented!(),*/
|
|
|
|
|
_ =>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
println!("{} TODO: parse implication", indentation);
|
|
|
|
|
|
|
|
|
|
// TODO: implement correctly
|
|
|
|
|
return Ok(crate::Formula::true_());
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 20:01:29 +02:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
// Parse quantified formulas
|
|
|
|
|
if let Some((identifier, input)) = identifier(input)
|
|
|
|
|
{
|
|
|
|
|
let quantifier = match identifier
|
|
|
|
|
{
|
|
|
|
|
"exists" => Some(Quantifier::Existential),
|
|
|
|
|
"forall" => Some(Quantifier::Universal),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(quantifier) = quantifier
|
|
|
|
|
{
|
|
|
|
|
let input = input.trim_start();
|
|
|
|
|
println!("{} parsing “{:?}” formula body: {}", indentation, quantifier, input);
|
|
|
|
|
|
|
|
|
|
return quantified_formula(input, quantifier, level + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let mut comparison_operators = self.comparison_operators();
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
// Parse comparisons
|
|
|
|
|
if let Some(comparison_operator) = comparison_operators.next()
|
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let (_, comparison_operator) = comparison_operator?;
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
// Comparisons with more than one comparison operator aren’t supported
|
|
|
|
|
if let Some(next_comparison_operator) = comparison_operators.next()
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let (_, next_comparison_operator) = next_comparison_operator?;
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
return Err(crate::parse::Error::new_multiple_comparison_operators(
|
|
|
|
|
comparison_operator, next_comparison_operator,
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("{} parsing “{:?}” comparison: {}", indentation, comparison_operator, input);
|
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
let mut comparison_operator_split = self.comparison_operators().split();
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
// There’s exactly one comparison operator in this formula, as we have verified above.
|
|
|
|
|
// Hence, the split is guaranteed to generate exactly these two elements
|
|
|
|
|
let input_left = comparison_operator_split.next().unwrap()?;
|
|
|
|
|
let input_right = comparison_operator_split.next().unwrap()?;
|
2020-04-22 20:01:29 +02:00
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
let argument_left = TermStr::new(input_left).parse(level + 1)?;
|
|
|
|
|
let argument_right = TermStr::new(input_right).parse(level + 1)?;
|
|
|
|
|
|
|
|
|
|
return Ok(crate::Formula::compare(comparison_operator, Box::new(argument_left),
|
|
|
|
|
Box::new(argument_right)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse predicates
|
|
|
|
|
if let Some((predicate_name, input)) = predicate_name(input)
|
|
|
|
|
{
|
|
|
|
|
println!("{} TODO: parse predicate {}", indentation, predicate_name);
|
|
|
|
|
|
|
|
|
|
let input = input.trim_start();
|
|
|
|
|
|
|
|
|
|
// Parse arguments if there are any
|
|
|
|
|
/*let arguments = match parenthesized_expression(input)?
|
|
|
|
|
{
|
|
|
|
|
Some((parenthesized_expression, remaining_input)) =>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-27 19:30:25 +02:00
|
|
|
|
unimplemented!();
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
None => unimplemented!(),
|
|
|
|
|
};*/
|
|
|
|
|
|
|
|
|
|
// TODO: implement correctly
|
|
|
|
|
return Ok(crate::Formula::true_());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parenthesized formulas
|
|
|
|
|
if let Some('(') = input.chars().next()
|
|
|
|
|
{
|
|
|
|
|
match parenthesized_expression(input)?
|
|
|
|
|
{
|
|
|
|
|
Some((parenthesized_expression, remaining_input)) =>
|
|
|
|
|
{
|
|
|
|
|
if !remaining_input.trim().is_empty()
|
|
|
|
|
{
|
|
|
|
|
return Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FormulaStr::new(parenthesized_expression).parse(level);
|
|
|
|
|
},
|
|
|
|
|
None => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
println!("{} can’t break down formula any further: {}", indentation, input);
|
|
|
|
|
|
|
|
|
|
// TODO: implement correctly
|
|
|
|
|
Ok(crate::Formula::true_())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
|
pub(crate) enum Quantifier
|
|
|
|
|
{
|
|
|
|
|
Existential,
|
|
|
|
|
Universal,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Quantifier
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
|
|
|
{
|
|
|
|
|
match &self
|
|
|
|
|
{
|
|
|
|
|
Self::Existential => write!(formatter, "exists"),
|
|
|
|
|
Self::Universal => write!(formatter, "forall"),
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: refactor
|
2020-04-28 02:02:20 +02:00
|
|
|
|
fn implication_left_to_right_inner<'i, T>(mut logical_connective_iterator: T, level: usize)
|
2020-04-27 19:30:25 +02:00
|
|
|
|
-> Result<Option<crate::Formula>, crate::parse::Error>
|
|
|
|
|
where
|
2020-04-28 02:02:20 +02:00
|
|
|
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
match logical_connective_iterator.next()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
Some(argument) =>
|
|
|
|
|
{
|
|
|
|
|
// TODO: improve error handling if antecedent cannot be parsed
|
|
|
|
|
let argument = FormulaStr::new(argument?).parse(level)?;
|
2020-04-28 02:02:20 +02:00
|
|
|
|
match implication_left_to_right_inner(logical_connective_iterator, level)?
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
Some(next_argument) => Ok(Some(crate::Formula::implies(
|
|
|
|
|
crate::ImplicationDirection::LeftToRight, Box::new(argument),
|
|
|
|
|
Box::new(next_argument)))),
|
|
|
|
|
None => Ok(Some(argument)),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-04-28 02:02:20 +02:00
|
|
|
|
fn implication_left_to_right<'i, T>(mut logical_connective_iterator: T, level: usize)
|
2020-04-27 19:30:25 +02:00
|
|
|
|
-> Result<crate::Formula, crate::parse::Error>
|
|
|
|
|
where
|
2020-04-28 02:02:20 +02:00
|
|
|
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 02:02:20 +02:00
|
|
|
|
match logical_connective_iterator.next()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
Some(argument) =>
|
|
|
|
|
{
|
|
|
|
|
// TODO: improve error handling if antecedent cannot be parsed
|
|
|
|
|
let argument = FormulaStr::new(argument?).parse(level)?;
|
2020-04-28 02:02:20 +02:00
|
|
|
|
match implication_left_to_right_inner(logical_connective_iterator, level)?
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
|
|
|
|
Some(next_argument) => Ok(crate::Formula::implies(
|
|
|
|
|
crate::ImplicationDirection::LeftToRight, Box::new(argument),
|
|
|
|
|
Box::new(next_argument))),
|
|
|
|
|
None => Err(crate::parse::Error::new_expected_logical_connective_argument(
|
|
|
|
|
"left-to-right implication".to_string(),
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => Err(crate::parse::Error::new_expected_logical_connective_argument(
|
|
|
|
|
"left-to-right implication".to_string(),
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
fn quantified_formula(input: &str, quantifier: Quantifier, level: usize)
|
|
|
|
|
-> Result<crate::Formula, crate::parse::Error>
|
|
|
|
|
{
|
|
|
|
|
let (parameters, input) = match variable_declarations(input)?
|
|
|
|
|
{
|
|
|
|
|
Some(variable_declarations) => variable_declarations,
|
|
|
|
|
None => return Err(crate::parse::Error::new_expected_variable_declaration(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
|
|
|
|
};
|
|
|
|
|
let parameters = std::rc::Rc::new(parameters);
|
|
|
|
|
|
|
|
|
|
let formula_str = FormulaStr::new(input.trim());
|
|
|
|
|
let formula = Box::new(formula_str.parse(level)?);
|
|
|
|
|
|
|
|
|
|
// TODO: push variable stack layer
|
|
|
|
|
let formula = match quantifier
|
|
|
|
|
{
|
|
|
|
|
Quantifier::Existential => crate::Formula::exists(parameters, formula),
|
|
|
|
|
Quantifier::Universal => crate::Formula::for_all(parameters, formula),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(formula)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests
|
|
|
|
|
{
|
|
|
|
|
use super::*;
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
|
|
|
|
#[test]
|
2020-04-22 20:01:29 +02:00
|
|
|
|
fn tokenize_formula_infix_operators()
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
let f = FormulaStr::new("((forall X exists Y (p(X) -> q(Y)) and false) or p) -> false");
|
|
|
|
|
assert_eq!(f.top_level_infix_operator().unwrap(),
|
|
|
|
|
Some(FormulaInfixOperator::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.iter_infix_operators();
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().2, FormulaInfixOperator::ImpliesLeftToRight);
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
let f = FormulaStr::new("forall X exists Y (p(X) -> q(Y)) and false or p -> false");
|
|
|
|
|
assert_eq!(f.top_level_infix_operator().unwrap(),
|
|
|
|
|
Some(FormulaInfixOperator::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.iter_infix_operators();
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().2, FormulaInfixOperator::And);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().2, FormulaInfixOperator::Or);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().2, FormulaInfixOperator::ImpliesLeftToRight);
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
let f = FormulaStr::new(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ");
|
|
|
|
|
assert_eq!(f.top_level_infix_operator().unwrap(),
|
|
|
|
|
Some(FormulaInfixOperator::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.split_at_infix_operator(FormulaInfixOperator::ImpliesLeftToRight);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "p");
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "forall X exists Y (p(X) -> q(Y)) and false or p");
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "false");
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
let f = FormulaStr::new(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ");
|
|
|
|
|
assert_eq!(f.top_level_infix_operator().unwrap(),
|
|
|
|
|
Some(FormulaInfixOperator::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.split_at_infix_operator(FormulaInfixOperator::And);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "p -> forall X exists Y (p(X) -> q(Y))");
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "false or p -> false");
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
let f = FormulaStr::new(" p and forall X exists Y (p(X) -> q(Y)) and false or p or false ");
|
|
|
|
|
assert_eq!(f.top_level_infix_operator().unwrap(), Some(FormulaInfixOperator::Or));
|
|
|
|
|
let mut i = f.split_at_infix_operator(FormulaInfixOperator::Or);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "p and forall X exists Y (p(X) -> q(Y)) and false");
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "p");
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "false");
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
let f = FormulaStr::new(" (p and q) ");
|
|
|
|
|
assert!(f.top_level_infix_operator().unwrap().is_none());
|
|
|
|
|
let mut i = f.split_at_infix_operator(FormulaInfixOperator::And);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "(p and q)");
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
|
|
|
|
assert!(FormulaStr::new(" a -> b -> c ").parse(0).is_ok());
|
|
|
|
|
assert!(FormulaStr::new(" a -> b <- c ").parse(0).is_err());
|
|
|
|
|
|
|
|
|
|
assert!(!FormulaStr::new(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ")
|
|
|
|
|
.parse(0).is_ok());
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|