Skip to main content

Formula

Enum Formula 

Source
pub enum Formula {
    Top,
    Bottom,
    Atom {
        predicate: Pred,
        terms: Vec<Term>,
    },
    Equals {
        left: Term,
        right: Term,
    },
    Not {
        formula: Box<Formula>,
    },
    And {
        left: Box<Formula>,
        right: Box<Formula>,
    },
    Or {
        left: Box<Formula>,
        right: Box<Formula>,
    },
    Implies {
        left: Box<Formula>,
        right: Box<Formula>,
    },
    Iff {
        left: Box<Formula>,
        right: Box<Formula>,
    },
    Exists {
        variables: Vec<V>,
        formula: Box<Formula>,
    },
    Forall {
        variables: Vec<V>,
        formula: Box<Formula>,
    },
}
Expand description

Is an abstract syntax tree (AST) for first-order formulae.

Variants§

§

Top

Is logical Top (⊤) or Truth

§

Bottom

Is logical Bottom (⟘) or Falsehood

§

Atom

Is an atomic formula, made by applying predicate on a list of terms.

Fields

§predicate: Pred
§terms: Vec<Term>
§

Equals

Represents an equality between two terms.

Note: Equality is a special type of atomic formula.

Fields

§left: Term
§right: Term
§

Not

Makes the negation of the formula that it holds.

Fields

§formula: Box<Formula>
§

And

Makes a conjunction of the left and right formulae that it holds.

Fields

§left: Box<Formula>
§right: Box<Formula>
§

Or

Makes a disjunction of left and right formulae that it holds.

Fields

§left: Box<Formula>
§right: Box<Formula>
§

Implies

Makes an implication between left and right formulae that it holds.

Fields

§left: Box<Formula>
§right: Box<Formula>
§

Iff

Makes a bi-implication between left and right formulae that it holds.

Fields

§left: Box<Formula>
§right: Box<Formula>
§

Exists

Makes an existentially quantified formula with the bound variables and the formula that it holds.

Fields

§variables: Vec<V>
§formula: Box<Formula>
§

Forall

Makes a universally quantified formula with the bound variables and the formula that it holds.

Fields

§variables: Vec<V>
§formula: Box<Formula>

Implementations§

Source§

impl Formula

Source

pub fn and(self, formula: Self) -> Self

Returns a conjunction of the receiver and formula.

Source

pub fn or(self, formula: Self) -> Self

Returns a disjunction of the receiver and formula.

Source

pub fn implies(self, formula: Self) -> Self

Returns an implication between the receiver and formula.

Source

pub fn iff(self, formula: Self) -> Self

Returns a bi-implication between the receiver and formula.

Source

pub fn free_vars(&self) -> Vec<&V>

Returns a list of free variable symbols in the receiver formula.

Note: In the list of free variables, each variable symbol appears only once even if it is present at multiple positions of the receiver formula.

Example:


// `x`, `y` and `z` are variable symbols:
let x = V::from("x");
let y = V::from("y");
let z = V::from("z");

// (P(x) ∧ Q(x, f(g(x), y))) ∨ ('c = g(z))
let formula: Formula = "(P(x) & Q(x, f(g(x), y))) |  'c = g(z)".parse().unwrap();
assert_eq!(vec![&x, &y, &z].iter().sorted(), formula.free_vars().iter().sorted());

// ∀ x. P(x, y)
let formula: Formula = "forall x. P(x, y)".parse().unwrap();
// notice that the bound variable `x` is not in the list of free variables of `formula`
assert_eq!(vec![&y], formula.free_vars());

// ∃ x. P(x, y)
let formula: Formula = "exists x. P(x, y)".parse().unwrap();
assert_eq!(vec![&y], formula.free_vars());
Source§

impl Formula

Source

pub fn pnf(&self) -> Formula

Returns a Prenex Normal Form (PNF) equivalent to the receiver.

Hint: A PNF is a formula with all quantifiers (existential and universal) and bound variables at the front, followed by a quantifier-free part.

Example:


let formula: Formula = "Q(x, y) → ∃ x, y. P(x, y)".parse().unwrap();
assert_eq!("∃ x`, y`. (Q(x, y) → P(x`, y`))", formula.pnf().to_string());
Source

pub fn snf(&self) -> Formula

Returns a Skolem Normal Form (SNF) equivalent to the receiver.

Hint: An SNF is a PNF with only universal quantifiers (see: https://en.wikipedia.org/wiki/Skolem_normal_form).

Example:


let formula: Formula = "∃ y. P(x, y)".parse().unwrap();
assert_eq!("P(x, sk#0(x))", formula.snf().to_string());
Source

pub fn snf_with(&self, generator: &mut SkolemGenerator) -> Formula

Is similar to Formula::snf but uses an existing SkolemGenerator to avoid collision when generating Skolem function names (including Skolem constants).

Example:

use razor_fol::transform::SkolemGenerator;

let mut generator = SkolemGenerator::from("skolem");
let formula: Formula = "∃ y. P(x, y)".parse().unwrap();
assert_eq!("P(x, skolem0(x))", formula.snf_with(&mut generator).to_string());
Source

pub fn nnf(&self) -> Formula

Returns an Negation Normal Form (NNF) equivalent to the receiver.

Hint: An NNF is a formula where negation is only applied to its atomic (including equations) sub-formulae.

Example:


let formula: Formula = "not (P(x) iff Q(y))".parse().unwrap();
assert_eq!("(P(x) ∧ (¬Q(y))) ∨ ((¬P(x)) ∧ Q(y))", formula.nnf().to_string());
Source

pub fn cnf(&self) -> Formula

Returns a Conjunctive Normal Form (CNF) equivalent to the receiver.

Hint: A CNF is a formula that is a conjunction of zero or more clauses. A clause is a disjunction of atomic formulae (including equations).

Note: All free variables are assumed to be universally quantified.

Example:


let formula: Formula = "P(x) <=> Q(y)".parse().unwrap();
assert_eq!("((¬P(x)) ∨ Q(y)) ∧ ((¬Q(y)) ∨ P(x))", formula.cnf().to_string());
Source

pub fn cnf_with(&self, generator: &mut SkolemGenerator) -> Formula

Is similar to Formula::cnf but uses an existing SkolemGenerator to avoid collision when generating Skolem function names (including Skolem constants).

Note: The CNF transformation includes Skolemization.

Example:

use razor_fol::transform::SkolemGenerator;

let mut generator = SkolemGenerator::from("s%");
let formula: Formula = "exists x. ((forall y. P(y) & Q(x, y))  -> R(x))".parse().unwrap();
assert_eq!("((¬P(\'s%1)) ∨ (¬Q(\'s%0, \'s%1))) ∨ R(\'s%0)",
    formula.cnf_with(&mut generator).to_string());
Source

pub fn dnf(&self) -> Formula

Returns a Disjunctive Normal Form (DNF) equivalent to the receiver.

Hint: A DNF is a formula that is a disjunction of zero or more conjuncts. A conjunct is a conjunction of atomic formulae (including equations).

Note: All of the free variables are assumed to be universally quantified.

Example:


let formula: Formula = "P(x) iff Q(y)".parse().unwrap();
assert_eq!("(((¬P(x)) ∧ (¬Q(y))) ∨ ((¬P(x)) ∧ P(x))) ∨ ((Q(y) ∧ (¬Q(y))) ∨ (Q(y) ∧ P(x)))",
    formula.dnf().to_string());
Source

pub fn dnf_with(&self, generator: &mut SkolemGenerator) -> Formula

Is similar to Formula::dnf but uses an existing SkolemGenerator to avoid collision when generating Skolem function names (including Skolem constants).

Note: The DNF transformation includes Skolemization.

Example:

use razor_fol::transform::SkolemGenerator;

let mut generator = SkolemGenerator::from("s%");
let formula: Formula = "!y. (!x. (P(y, x) | Q(x)) -> Q(y))".parse().unwrap();
assert_eq!("((¬P(y, s%0(y))) ∧ (¬Q(s%0(y)))) ∨ Q(y)",
    formula.dnf_with(&mut generator).to_string());
Source

pub fn simplify(&self) -> Formula

Applies a number of syntactic transformations to simplify the receiver formula.

Example:


let formula: Formula = "not (not P())".parse().unwrap();
assert_eq!("P()", formula.simplify().to_string());

let formula: Formula = "forall x. (P() and true) | (Q(x) or false)".parse().unwrap();
assert_eq!("∀ x. (P() ∨ Q(x))", formula.simplify().to_string());
Source

pub fn gnf(&self) -> Vec<Formula>

Returns a list of formulae in Geometric Normal Form (GNF), equivalent to the receiver.

Hint: For mor information about GNF, see Geometric Logic in Computer Science by Steve Vickers.

Example:


let formula: Formula = "P(x) & (Q(x) | R(x))".parse().unwrap();
let gnf_to_string: Vec<String> = formula.gnf().iter().map(|f| f.to_string()).collect();
assert_eq!(vec!["⊤ → P(x)", "⊤ → (Q(x) ∨ R(x))"], gnf_to_string);
Source

pub fn gnf_with(&self, generator: &mut SkolemGenerator) -> Vec<Formula>

Is similar to Formula::gnf but uses an existing SkolemGenerator to avoid collision when generating Skolem function names (including Skolem constants).

Note: The GNF transformation includes Skolemization.

Example:

use razor_fol::transform::SkolemGenerator;

let mut generator = SkolemGenerator::from("s%");
let formula: Formula = "P(y) -> exists x. P(x) & Q(y)".parse().unwrap();
let gnf_to_string: Vec<String> = formula.gnf().iter().map(|f| f.to_string()).collect();
assert_eq!(vec!["P(y) → P(sk#0(y))", "P(y) → Q(y)"], gnf_to_string);

Trait Implementations§

Source§

impl Clone for Formula

Source§

fn clone(&self) -> Formula

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Formula

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Formula

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FromStr for Formula

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Formula

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Formula

Source§

fn cmp(&self, other: &Formula) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Formula

Source§

fn eq(&self, other: &Formula) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Formula

Source§

fn partial_cmp(&self, other: &Formula) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TermBased for Formula

Source§

fn rename_vars(&self, renaming: &impl VariableRenaming) -> Self

Note: Applies a VariableRenaming on the free variables of the formula, keeping the bound variables unchanged.

Source§

fn substitute(&self, substitution: &impl Substitution) -> Self

Note: Applies a Substitution on the free variables of the formula, keeping the bound variables unchanged.

Source§

fn transform(&self, f: &impl Fn(&Term) -> Term) -> Self

Applies a transformation function f on the Terms of the receiver.
Source§

impl Eq for Formula

Source§

impl StructuralPartialEq for Formula

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.