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-05-04 16:40:59 +02:00
|
|
|
|
pub fn formula<D>(input: &str, declarations: &D)
|
|
|
|
|
-> Result<crate::ClosedFormula, crate::parse::Error>
|
|
|
|
|
where
|
|
|
|
|
D: crate::FindOrCreateFunctionDeclaration + crate::FindOrCreatePredicateDeclaration,
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let variable_declaration_stack = crate::VariableDeclarationStackLayer::free();
|
|
|
|
|
|
|
|
|
|
let formula_str = FormulaStr::new(input, declarations, &variable_declaration_stack);
|
|
|
|
|
let formula = formula_str.parse(0)?;
|
|
|
|
|
|
|
|
|
|
let free_variable_declarations = match variable_declaration_stack
|
|
|
|
|
{
|
|
|
|
|
crate::VariableDeclarationStackLayer::Free(free_variable_declarations) =>
|
|
|
|
|
std::rc::Rc::new(free_variable_declarations.into_inner()),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(crate::ClosedFormula
|
|
|
|
|
{
|
|
|
|
|
formula,
|
|
|
|
|
free_variable_declarations,
|
|
|
|
|
})
|
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
|
|
|
|
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-05-04 16:40:59 +02:00
|
|
|
|
struct FormulaStr<'i, 'd, 'p, 'v, D>
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-04-22 20:01:29 +02:00
|
|
|
|
input: &'i str,
|
2020-05-04 16:40:59 +02:00
|
|
|
|
declarations: &'d D,
|
|
|
|
|
variable_declaration_stack: &'v crate::VariableDeclarationStackLayer<'p>,
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
impl<'i, 'd, 'p, 'v, D> FormulaStr<'i, 'd, 'p, 'v, D>
|
|
|
|
|
where
|
|
|
|
|
D: crate::FindOrCreateFunctionDeclaration + crate::FindOrCreatePredicateDeclaration,
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
pub fn new(input: &'i str, declarations: &'d D,
|
|
|
|
|
variable_declaration_stack: &'v crate::VariableDeclarationStackLayer<'p>) -> Self
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
|
|
|
|
Self
|
|
|
|
|
{
|
|
|
|
|
input,
|
2020-05-04 16:40:59 +02:00
|
|
|
|
declarations,
|
|
|
|
|
variable_declaration_stack,
|
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 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 05:21:58 +02:00
|
|
|
|
Token::Identifier("and") => Some(LogicalConnective::And),
|
|
|
|
|
Token::Identifier("or") => Some(LogicalConnective::Or),
|
|
|
|
|
Token::Symbol(Symbol::ArrowLeft) => Some(LogicalConnective::ImpliesRightToLeft),
|
|
|
|
|
Token::Symbol(Symbol::ArrowLeftAndRight) => Some(LogicalConnective::IfAndOnlyIf),
|
|
|
|
|
Token::Symbol(Symbol::ArrowRight) => Some(LogicalConnective::ImpliesLeftToRight),
|
2020-04-28 02:02:20 +02:00
|
|
|
|
_ => 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
|
|
|
|
|
{
|
2020-04-28 05:21:58 +02:00
|
|
|
|
Token::Identifier("and") => logical_connective == LogicalConnective::And,
|
|
|
|
|
Token::Identifier("or") => logical_connective == LogicalConnective::Or,
|
|
|
|
|
Token::Symbol(Symbol::ArrowLeft) =>
|
|
|
|
|
logical_connective == LogicalConnective::ImpliesRightToLeft,
|
|
|
|
|
Token::Symbol(Symbol::ArrowLeftAndRight) =>
|
|
|
|
|
logical_connective == LogicalConnective::IfAndOnlyIf,
|
|
|
|
|
Token::Symbol(Symbol::ArrowRight) =>
|
|
|
|
|
logical_connective == LogicalConnective::ImpliesLeftToRight,
|
2020-04-27 19:30:25 +02:00
|
|
|
|
_ => 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 mut top_level_logical_connective = None;
|
|
|
|
|
|
2020-04-28 02:36:02 +02:00
|
|
|
|
for logical_connective in self.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-28 02:39:58 +02:00
|
|
|
|
let implication_directions_are_mixed =
|
|
|
|
|
logical_connective == LogicalConnective::ImpliesLeftToRight
|
|
|
|
|
&& top_level_logical_connective == LogicalConnective::ImpliesRightToLeft
|
|
|
|
|
|| logical_connective == LogicalConnective::ImpliesRightToLeft
|
|
|
|
|
&& top_level_logical_connective == LogicalConnective::ImpliesLeftToRight;
|
|
|
|
|
|
|
|
|
|
if implication_directions_are_mixed
|
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)))),
|
2020-04-28 03:18:05 +02:00
|
|
|
|
None => return Err(crate::parse::Error::new_empty_expression(
|
2020-04-27 19:30:25 +02:00
|
|
|
|
crate::parse::error::Location::new(0, Some(0)))),
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse logical infix connectives
|
|
|
|
|
if let Some(top_level_logical_connective) = self.top_level_logical_connective()?
|
|
|
|
|
{
|
2020-05-03 18:05:45 +02:00
|
|
|
|
println!("{} parsing “{:?}” logical connective", indentation,
|
|
|
|
|
top_level_logical_connective);
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
// 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-05-04 16:40:59 +02:00
|
|
|
|
.map(|argument| FormulaStr::new(argument?, self.declarations, self.variable_declaration_stack).parse(level + 1))
|
2020-04-27 19:30:25 +02:00
|
|
|
|
.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 =>
|
2020-05-04 16:40:59 +02:00
|
|
|
|
return self.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-28 02:36:53 +02:00
|
|
|
|
LogicalConnective::ImpliesRightToLeft =>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-28 02:36:53 +02:00
|
|
|
|
let mut argument_iterator =
|
|
|
|
|
self.split_at_logical_connective(top_level_logical_connective);
|
|
|
|
|
let first_argument = argument_iterator.next().ok_or_else(||
|
|
|
|
|
crate::parse::Error::new_expected_logical_connective_argument(
|
|
|
|
|
"right-to-left implication".to_string(),
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))))?;
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let first_argument = FormulaStr::new(first_argument?, self.declarations, self.variable_declaration_stack).parse(level + 1)?;
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
2020-04-28 02:36:53 +02:00
|
|
|
|
return argument_iterator.try_fold(first_argument,
|
|
|
|
|
|accumulator, argument|
|
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let argument = FormulaStr::new(argument?, self.declarations, self.variable_declaration_stack).parse(level + 1)?;
|
2020-04-28 02:36:53 +02:00
|
|
|
|
|
|
|
|
|
Ok(crate::Formula::implies(crate::ImplicationDirection::RightToLeft,
|
|
|
|
|
Box::new(accumulator), Box::new(argument)))
|
|
|
|
|
});
|
|
|
|
|
},
|
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)
|
|
|
|
|
{
|
2020-04-28 05:21:58 +02:00
|
|
|
|
match identifier
|
2020-04-28 03:18:05 +02:00
|
|
|
|
{
|
2020-04-28 05:21:58 +02:00
|
|
|
|
"not" =>
|
|
|
|
|
{
|
|
|
|
|
let input = input.trim_start();
|
|
|
|
|
println!("{} parsing “not” formula body: {}", indentation, input);
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let argument = FormulaStr::new(input, self.declarations, self.variable_declaration_stack).parse(level + 1)?;
|
2020-04-28 05:21:58 +02:00
|
|
|
|
|
|
|
|
|
return Ok(crate::Formula::not(Box::new(argument)));
|
|
|
|
|
},
|
|
|
|
|
"true" =>
|
|
|
|
|
{
|
|
|
|
|
if !input.trim().is_empty()
|
|
|
|
|
{
|
|
|
|
|
return Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))))
|
|
|
|
|
}
|
2020-04-28 03:18:05 +02:00
|
|
|
|
|
2020-04-28 05:21:58 +02:00
|
|
|
|
return Ok(crate::Formula::true_());
|
|
|
|
|
},
|
|
|
|
|
"false" =>
|
|
|
|
|
{
|
|
|
|
|
if !input.trim().is_empty()
|
|
|
|
|
{
|
|
|
|
|
return Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))))
|
|
|
|
|
}
|
2020-04-28 03:18:05 +02:00
|
|
|
|
|
2020-04-28 05:21:58 +02:00
|
|
|
|
return Ok(crate::Formula::false_());
|
|
|
|
|
},
|
|
|
|
|
_ => (),
|
2020-04-28 03:18:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 19:30:25 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
return self.quantified_formula(input, quantifier, level + 1);
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-28 03:18:05 +02:00
|
|
|
|
assert!(comparison_operator_split.next().is_none());
|
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)
|
|
|
|
|
{
|
2020-04-28 03:18:05 +02:00
|
|
|
|
println!("{} parsing predicate {}", indentation, predicate_name);
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
|
|
|
|
let input = input.trim_start();
|
|
|
|
|
|
|
|
|
|
// Parse arguments if there are any
|
2020-04-28 03:18:05 +02:00
|
|
|
|
let (arguments, input) = match parenthesized_expression(input)?
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 03:18:05 +02:00
|
|
|
|
Some((parenthesized_expression, input)) =>
|
2020-04-22 20:01:29 +02:00
|
|
|
|
{
|
2020-04-28 03:18:05 +02:00
|
|
|
|
let functor = |token: &_| *token == Token::Symbol(Symbol::Comma);
|
|
|
|
|
let arguments = Tokens::new_filter(parenthesized_expression, functor).split()
|
|
|
|
|
.map(|argument| TermStr::new(argument?).parse(level + 1))
|
|
|
|
|
.collect::<Result<_, _>>()?;
|
|
|
|
|
|
|
|
|
|
(arguments, input)
|
2020-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-04-28 03:18:05 +02:00
|
|
|
|
None => (vec![], input),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !input.trim().is_empty()
|
|
|
|
|
{
|
|
|
|
|
return Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))))
|
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
2020-04-28 03:18:05 +02:00
|
|
|
|
// TODO: implement look-up
|
|
|
|
|
let declaration =
|
|
|
|
|
crate::PredicateDeclaration::new(predicate_name.to_string(), arguments.len());
|
|
|
|
|
let declaration = std::rc::Rc::new(declaration);
|
|
|
|
|
|
|
|
|
|
return Ok(crate::Formula::predicate(declaration, arguments));
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parenthesized formulas
|
2020-04-28 05:21:58 +02:00
|
|
|
|
if let Some((parenthesized_expression, input)) = parenthesized_expression(input)?
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 05:21:58 +02:00
|
|
|
|
if !input.trim().is_empty()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-04-28 05:21:58 +02:00
|
|
|
|
return Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))));
|
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
return FormulaStr::new(parenthesized_expression, self.declarations, self.variable_declaration_stack).parse(level + 1);
|
2020-04-28 03:18:05 +02:00
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
2020-04-28 03:18:05 +02:00
|
|
|
|
Err(crate::parse::Error::new_unexpected_token(
|
|
|
|
|
crate::parse::error::Location::new(0, Some(0))))
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
// TODO: refactor
|
|
|
|
|
fn implication_left_to_right_inner<T>(&self, mut argument_iterator: T, level: usize)
|
|
|
|
|
-> Result<Option<crate::Formula>, crate::parse::Error>
|
|
|
|
|
where
|
|
|
|
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
match argument_iterator.next()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
Some(argument) =>
|
|
|
|
|
{
|
|
|
|
|
// TODO: improve error handling if antecedent cannot be parsed
|
|
|
|
|
let argument = FormulaStr::new(argument?, self.declarations, self.variable_declaration_stack).parse(level)?;
|
|
|
|
|
match self.implication_left_to_right_inner(argument_iterator, level)?
|
|
|
|
|
{
|
|
|
|
|
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-04-22 20:01:29 +02:00
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
fn implication_left_to_right<T>(&self, mut argument_iterator: T, level: usize)
|
|
|
|
|
-> Result<crate::Formula, crate::parse::Error>
|
|
|
|
|
where
|
|
|
|
|
T: std::iter::Iterator<Item = Result<&'i str, crate::parse::Error>>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
match argument_iterator.next()
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
Some(argument) =>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
// TODO: improve error handling if antecedent cannot be parsed
|
|
|
|
|
let argument = FormulaStr::new(argument?, self.declarations, self.variable_declaration_stack).parse(level)?;
|
|
|
|
|
match self.implication_left_to_right_inner(argument_iterator, level)?
|
|
|
|
|
{
|
|
|
|
|
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-27 19:30:25 +02:00
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
// TODO: refactor without input argument
|
|
|
|
|
fn quantified_formula(&self, input: &str, quantifier: Quantifier, level: usize)
|
|
|
|
|
-> Result<crate::Formula, crate::parse::Error>
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let (parameters, input) = match variable_declarations(input)?
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
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 variable_declaration_stack = crate::VariableDeclarationStackLayer::bound(
|
|
|
|
|
self.variable_declaration_stack, std::rc::Rc::clone(¶meters));
|
|
|
|
|
|
|
|
|
|
let formula_str =
|
|
|
|
|
FormulaStr::new(input.trim(), self.declarations, &variable_declaration_stack);
|
|
|
|
|
let formula = Box::new(formula_str.parse(level)?);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
|
pub(crate) enum Quantifier
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
Existential,
|
|
|
|
|
Universal,
|
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
impl std::fmt::Debug for Quantifier
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
|
2020-04-27 19:30:25 +02:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
match &self
|
|
|
|
|
{
|
|
|
|
|
Self::Existential => write!(formatter, "exists"),
|
|
|
|
|
Self::Universal => write!(formatter, "forall"),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-27 19:30:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:01:29 +02:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests
|
|
|
|
|
{
|
|
|
|
|
use super::*;
|
2020-02-25 15:36:34 +01:00
|
|
|
|
|
|
|
|
|
#[test]
|
2020-05-03 18:05:45 +02:00
|
|
|
|
fn tokenize_formula_logical_connectives()
|
2020-02-25 15:36:34 +01:00
|
|
|
|
{
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let declarations = crate::Declarations::new();
|
|
|
|
|
let variable_declaration_stack = crate::VariableDeclarationStackLayer::free();
|
|
|
|
|
|
|
|
|
|
let formula_str = |input| FormulaStr::new(input, &declarations,
|
|
|
|
|
&variable_declaration_stack);
|
|
|
|
|
|
|
|
|
|
let f = formula_str("((forall X exists Y (p(X) -> q(Y)) and false) or p) -> false");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert_eq!(f.top_level_logical_connective().unwrap(),
|
|
|
|
|
Some(LogicalConnective::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.logical_connectives();
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().1, LogicalConnective::ImpliesLeftToRight);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let f = formula_str("forall X exists Y (p(X) -> q(Y)) and false or p -> false");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert_eq!(f.top_level_logical_connective().unwrap(),
|
|
|
|
|
Some(LogicalConnective::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.logical_connectives();
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().1, LogicalConnective::And);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().1, LogicalConnective::Or);
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap().1, LogicalConnective::ImpliesLeftToRight);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let f = formula_str(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert_eq!(f.top_level_logical_connective().unwrap(),
|
|
|
|
|
Some(LogicalConnective::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.split_at_logical_connective(LogicalConnective::ImpliesLeftToRight);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
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());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let f = formula_str(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert_eq!(f.top_level_logical_connective().unwrap(),
|
|
|
|
|
Some(LogicalConnective::ImpliesLeftToRight));
|
|
|
|
|
let mut i = f.split_at_logical_connective(LogicalConnective::And);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
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());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let f = formula_str(" p and forall X exists Y (p(X) -> q(Y)) and false or p or false ");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert_eq!(f.top_level_logical_connective().unwrap(), Some(LogicalConnective::Or));
|
|
|
|
|
let mut i = f.split_at_logical_connective(LogicalConnective::Or);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
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());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
let f = formula_str(" (p and q) ");
|
2020-05-03 18:05:45 +02:00
|
|
|
|
assert!(f.top_level_logical_connective().unwrap().is_none());
|
|
|
|
|
let mut i = f.split_at_logical_connective(LogicalConnective::And);
|
2020-04-22 20:01:29 +02:00
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), "(p and q)");
|
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
assert!(formula_str(" a -> b -> c ").parse(0).is_ok());
|
|
|
|
|
assert!(formula_str(" a -> b <- c ").parse(0).is_err());
|
2020-04-22 20:01:29 +02:00
|
|
|
|
|
2020-05-04 16:40:59 +02:00
|
|
|
|
assert!(formula_str(" p -> forall X exists Y (p(X) -> q(Y)) and false or p -> false ")
|
2020-04-22 20:01:29 +02:00
|
|
|
|
.parse(0).is_ok());
|
2020-02-25 15:36:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|