2020-01-24 18:43:42 +01:00
|
|
|
// Operators
|
|
|
|
|
|
|
|
pub enum BinaryOperator
|
|
|
|
{
|
2020-02-02 02:08:39 +01:00
|
|
|
Add,
|
|
|
|
Subtract,
|
|
|
|
Multiply,
|
|
|
|
Divide,
|
2020-01-24 18:43:42 +01:00
|
|
|
Modulo,
|
2020-02-02 02:08:39 +01:00
|
|
|
Exponentiate,
|
2020-01-24 18:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum ComparisonOperator
|
|
|
|
{
|
|
|
|
Greater,
|
|
|
|
Less,
|
|
|
|
LessOrEqual,
|
|
|
|
GreaterOrEqual,
|
|
|
|
NotEqual,
|
|
|
|
Equal,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum UnaryOperator
|
|
|
|
{
|
|
|
|
AbsoluteValue,
|
2020-02-02 02:08:39 +01:00
|
|
|
Negative,
|
2020-01-24 18:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Primitives
|
|
|
|
|
2020-02-01 17:40:15 +01:00
|
|
|
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2020-01-24 18:43:42 +01:00
|
|
|
pub struct FunctionDeclaration
|
|
|
|
{
|
|
|
|
pub name: String,
|
|
|
|
pub arity: usize,
|
|
|
|
}
|
|
|
|
|
2020-01-31 13:53:21 +01:00
|
|
|
pub type FunctionDeclarations = std::collections::HashSet<std::rc::Rc<FunctionDeclaration>>;
|
|
|
|
|
2020-02-01 17:40:15 +01:00
|
|
|
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2019-11-01 22:00:17 +01:00
|
|
|
pub struct PredicateDeclaration
|
|
|
|
{
|
|
|
|
pub name: String,
|
|
|
|
pub arity: usize,
|
|
|
|
}
|
|
|
|
|
2020-01-31 13:53:21 +01:00
|
|
|
pub type PredicateDeclarations = std::collections::HashSet<std::rc::Rc<PredicateDeclaration>>;
|
|
|
|
|
2020-02-01 17:40:15 +01:00
|
|
|
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2020-01-24 18:43:42 +01:00
|
|
|
pub struct VariableDeclaration
|
2019-11-01 22:00:17 +01:00
|
|
|
{
|
2020-01-24 18:43:42 +01:00
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:12:05 +01:00
|
|
|
pub type VariableDeclarations = Vec<std::rc::Rc<VariableDeclaration>>;
|
2020-01-24 18:56:03 +01:00
|
|
|
|
|
|
|
pub struct VariableDeclarationStack
|
|
|
|
{
|
|
|
|
pub free_variable_declarations: VariableDeclarations,
|
|
|
|
bound_variable_declaration_stack: Vec<std::rc::Rc<VariableDeclarations>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VariableDeclarationStack
|
|
|
|
{
|
2020-01-31 13:53:42 +01:00
|
|
|
pub fn new() -> Self
|
|
|
|
{
|
|
|
|
Self
|
|
|
|
{
|
|
|
|
free_variable_declarations: VariableDeclarations::new(),
|
|
|
|
bound_variable_declaration_stack: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:56:03 +01:00
|
|
|
pub fn find(&self, variable_name: &str) -> Option<std::rc::Rc<VariableDeclaration>>
|
|
|
|
{
|
|
|
|
for variable_declarations in self.bound_variable_declaration_stack.iter().rev()
|
|
|
|
{
|
|
|
|
if let Some(variable_declaration) = variable_declarations.iter().find(|x| x.name == variable_name)
|
|
|
|
{
|
|
|
|
return Some(std::rc::Rc::clone(&variable_declaration));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(variable_declaration) = self.free_variable_declarations.iter().find(|x| x.name == variable_name)
|
|
|
|
{
|
|
|
|
return Some(std::rc::Rc::clone(&variable_declaration));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_or_create(&mut self, variable_name: &str) -> std::rc::Rc<VariableDeclaration>
|
|
|
|
{
|
|
|
|
if let Some(variable_declaration) = self.find(variable_name)
|
|
|
|
{
|
|
|
|
return variable_declaration;
|
|
|
|
}
|
|
|
|
|
|
|
|
let variable_declaration = VariableDeclaration
|
|
|
|
{
|
|
|
|
name: variable_name.to_owned(),
|
|
|
|
};
|
|
|
|
let variable_declaration = std::rc::Rc::new(variable_declaration);
|
|
|
|
|
2020-02-02 02:12:05 +01:00
|
|
|
self.free_variable_declarations.push(std::rc::Rc::clone(&variable_declaration));
|
2020-01-24 18:56:03 +01:00
|
|
|
|
|
|
|
variable_declaration
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, bound_variable_declarations: std::rc::Rc<VariableDeclarations>)
|
|
|
|
{
|
|
|
|
self.bound_variable_declaration_stack.push(bound_variable_declarations);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop(&mut self)
|
|
|
|
{
|
|
|
|
// TODO: return error instead
|
|
|
|
self.bound_variable_declaration_stack.pop().expect("bound variable is empty, cannot pop last element");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:42 +01:00
|
|
|
// Terms
|
|
|
|
|
|
|
|
pub struct BinaryOperation
|
|
|
|
{
|
|
|
|
pub operator: BinaryOperator,
|
|
|
|
pub left: Box<Term>,
|
|
|
|
pub right: Box<Term>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function
|
|
|
|
{
|
|
|
|
pub declaration: std::rc::Rc<FunctionDeclaration>,
|
|
|
|
pub arguments: Vec<Box<Term>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SpecialInteger
|
|
|
|
{
|
|
|
|
Infimum,
|
|
|
|
Supremum,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnaryOperation
|
|
|
|
{
|
|
|
|
pub operator: UnaryOperator,
|
|
|
|
pub argument: Box<Term>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variable
|
|
|
|
{
|
|
|
|
pub declaration: std::rc::Rc<VariableDeclaration>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formulas
|
|
|
|
|
2020-02-02 02:08:39 +01:00
|
|
|
pub struct Compare
|
2020-01-24 18:43:42 +01:00
|
|
|
{
|
|
|
|
pub operator: ComparisonOperator,
|
|
|
|
pub left: Box<Term>,
|
|
|
|
pub right: Box<Term>,
|
2019-11-01 22:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Exists
|
|
|
|
{
|
2020-02-02 02:12:05 +01:00
|
|
|
pub parameters: VariableDeclarations,
|
2019-11-01 22:00:17 +01:00
|
|
|
pub argument: Box<Formula>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ForAll
|
|
|
|
{
|
2020-02-02 02:12:05 +01:00
|
|
|
pub parameters: VariableDeclarations,
|
2019-11-01 22:00:17 +01:00
|
|
|
pub argument: Box<Formula>,
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:08:39 +01:00
|
|
|
pub struct IfAndOnlyIf
|
|
|
|
{
|
|
|
|
pub left: Box<Formula>,
|
|
|
|
pub right: Box<Formula>,
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:42 +01:00
|
|
|
pub struct Implies
|
2019-11-01 22:00:17 +01:00
|
|
|
{
|
2020-01-24 18:43:42 +01:00
|
|
|
pub antecedent: Box<Formula>,
|
|
|
|
pub implication: Box<Formula>,
|
2019-11-01 22:00:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:42 +01:00
|
|
|
pub struct Predicate
|
2019-11-01 22:00:17 +01:00
|
|
|
{
|
2020-01-24 18:43:42 +01:00
|
|
|
pub declaration: std::rc::Rc<PredicateDeclaration>,
|
|
|
|
pub arguments: Vec<Box<Term>>,
|
2019-11-01 22:00:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:42 +01:00
|
|
|
// Variants
|
2019-11-01 22:00:17 +01:00
|
|
|
|
|
|
|
pub enum Term
|
|
|
|
{
|
2020-01-24 18:43:42 +01:00
|
|
|
BinaryOperation(BinaryOperation),
|
|
|
|
Boolean(bool),
|
|
|
|
Function(Function),
|
|
|
|
Integer(i32),
|
|
|
|
SpecialInteger(SpecialInteger),
|
2019-11-01 22:00:17 +01:00
|
|
|
String(String),
|
2020-01-24 18:43:42 +01:00
|
|
|
Symbolic(String),
|
|
|
|
UnaryOperation(UnaryOperation),
|
|
|
|
Variable(Variable),
|
2019-11-01 22:00:17 +01:00
|
|
|
}
|
2020-01-24 18:43:42 +01:00
|
|
|
|
|
|
|
pub type Terms = Vec<Box<Term>>;
|
|
|
|
|
|
|
|
pub enum Formula
|
|
|
|
{
|
|
|
|
And(Formulas),
|
|
|
|
Boolean(bool),
|
2020-02-02 02:08:39 +01:00
|
|
|
Compare(Compare),
|
2020-01-24 18:43:42 +01:00
|
|
|
Exists(Exists),
|
|
|
|
ForAll(ForAll),
|
2020-02-02 02:08:39 +01:00
|
|
|
IfAndOnlyIf(IfAndOnlyIf),
|
2020-01-24 18:43:42 +01:00
|
|
|
Implies(Implies),
|
|
|
|
Not(Box<Formula>),
|
|
|
|
Or(Formulas),
|
|
|
|
Predicate(Predicate),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Formulas = Vec<Box<Formula>>;
|