BooleanExpression

Enum BooleanExpression 

Source
pub enum BooleanExpression<T: Atomic> {
    ComparisonExpression(Box<IntegerExpression<T>>, ComparisonOp, Box<IntegerExpression<T>>),
    BinaryExpression(Box<BooleanExpression<T>>, BooleanConnective, Box<BooleanExpression<T>>),
    Not(Box<BooleanExpression<T>>),
    True,
    False,
}
Expand description

Boolean constraint over type T

This enum represents a boolean expression over integer expressions and can be used to for example guards in rules of a threshold automaton.

§Example

use taco_threshold_automaton::expressions::*;

// y > 0
let _ = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Atom(Variable::new("y"))),
    ComparisonOp::Gt,
    Box::new(IntegerExpression::Const(0))
);

// x > 0 && y < 10
let _ = BooleanExpression::ComparisonExpression(
          Box::new(IntegerExpression::Atom(Variable::new("x"))),
          ComparisonOp::Gt,
          Box::new(IntegerExpression::Const(0)),
   ) & BooleanExpression::ComparisonExpression(
          Box::new(IntegerExpression::Atom(Variable::new("y"))),
          ComparisonOp::Lt,
          Box::new(IntegerExpression::Const(10)),
    );

Variants§

§

ComparisonExpression(Box<IntegerExpression<T>>, ComparisonOp, Box<IntegerExpression<T>>)

Comparison between two integer expressions

§

BinaryExpression(Box<BooleanExpression<T>>, BooleanConnective, Box<BooleanExpression<T>>)

Boolean expressions combined through boolean connective

§

Not(Box<BooleanExpression<T>>)

Negation of boolean expression

§

True

true

§

False

false

Implementations§

Source§

impl<T: Atomic> BooleanExpression<T>

Source

pub fn check_satisfied( &self, env: &HashMap<T, u32>, param_env: &HashMap<Parameter, u32>, ) -> Result<bool, EvaluationError<T>>

Check whether the constraint is satisfied in the given environment

This function evaluates the boolean expression in the given environment and returns whether the expression is satisfied.

§Example
use crate::taco_threshold_automaton::expressions::*;
use std::collections::HashMap;
use taco_threshold_automaton::expressions::properties::EvaluationError;

let env = HashMap::from([(Location::new("loc"), 1)]);
let param_env = HashMap::from([(Parameter::new("n"), 1)]);

let expr: BooleanExpression<Location> = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Const(5)),
    ComparisonOp::Eq,
    Box::new(IntegerExpression::Const(5)),
);
assert_eq!(expr.check_satisfied(&env, &param_env), Ok(true));

let expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Atom(Location::new("loc"))),
    ComparisonOp::Eq,
    Box::new(IntegerExpression::Const(1)),
);
assert_eq!(expr.check_satisfied(&env, &param_env), Ok(true));

let expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Atom(Location::new("loc"))),
    ComparisonOp::Gt,
    Box::new(IntegerExpression::Const(1)),
);
assert_eq!(expr.check_satisfied(&env, &param_env), Ok(false));

let expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Atom(Location::new("otherloc"))),
    ComparisonOp::Gt,
    Box::new(IntegerExpression::Const(1)),
);
assert_eq!(expr.check_satisfied(&env, &param_env), Err(EvaluationError::AtomicNotFound(Location::new("otherloc"))));
Source

pub fn contains_atom_a(&self, a: &T) -> bool

Recursively check whether the expression contains the atom a

§Example
use crate::taco_threshold_automaton::expressions::*;

let expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Atom(Location::new("loc"))),
    ComparisonOp::Eq,
    Box::new(IntegerExpression::Const(1)),
);
assert_eq!(expr.contains_atom_a(&Location::new("loc")), true);
assert_eq!(expr.contains_atom_a(&Location::new("otherloc")), false);
Source

pub fn substitute_atom_with( &mut self, to_replace: &T, replace_with: &IntegerExpression<T>, )

Substitute to_replace in this integer expression with integer expression replace_with

§Example
use crate::taco_threshold_automaton::expressions::*;

let mut expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Const(1)),
    ComparisonOp::Eq,
    Box::new(IntegerExpression::Atom(Location::new("l"))),
);
expr.substitute_atom_with(&Location::new("l"),  &IntegerExpression::Const(3));

let expected_expr = BooleanExpression::ComparisonExpression(
    Box::new(IntegerExpression::Const(1)),
    ComparisonOp::Eq,
    Box::new(IntegerExpression::Const(3)),
);
assert_eq!(expr, expected_expr);
Source

pub fn try_check_expr_constraints_to_zero(&self, atom: &T) -> bool

Check whether this expression is a constraint forces the atom to be equal to 0

Note: This function is not complete ! It only checks for simple syntactic cases, as otherwise an SMT solver would be required.

The function covers the following cases:

  • atom == 0
  • atom < 1
  • atom <= 0

(and these cases appearing in a conjunction, or if they appear in both sides of a disjunction)

§Example
use crate::taco_threshold_automaton::expressions::*;

let atom = Location::new("loc");
let expr = BooleanExpression::ComparisonExpression(
   Box::new(IntegerExpression::Atom(atom.clone())),
  ComparisonOp::Eq,
  Box::new(IntegerExpression::Const(0)),
);
assert_eq!(expr.try_check_expr_constraints_to_zero(&atom), true);


let expr = BooleanExpression::ComparisonExpression(
  Box::new(IntegerExpression::Atom(atom.clone())),
  ComparisonOp::Geq,
  Box::new(IntegerExpression::Const(0)),
);
assert_eq!(expr.try_check_expr_constraints_to_zero(&atom), false);

let expr = BooleanExpression::ComparisonExpression(
   Box::new(IntegerExpression::Atom(Location::new("otherloc"))),
   ComparisonOp::Eq,
   Box::new(IntegerExpression::Const(0)),
);
assert_eq!(expr.try_check_expr_constraints_to_zero(&atom), false);

let expr = !BooleanExpression::ComparisonExpression(
  Box::new(IntegerExpression::Atom(atom.clone())),
  ComparisonOp::Gt,
  Box::new(IntegerExpression::Const(1)),
);
assert_eq!(expr.try_check_expr_constraints_to_zero(&atom), false);
Source§

impl<T: Atomic> BooleanExpression<T>

Source

pub fn remove_boolean_negations(&self) -> NonNegatedBooleanExpression<T>

Remove boolean negations from an expression, transforming the expression into a NonNegatedBooleanExpression.

This function removes a boolean negation by pushing it inwards and transforming the expression accordingly.

§Example
// !(x > 0)
let expr = BooleanExpression::Not(Box::new(BooleanExpression::ComparisonExpression(
   Box::new(IntegerExpression::Atom(Variable::new("x"))),
   ComparisonOp::Gt,
   Box::new(IntegerExpression::Const(0)),
)));

// !(x > 0) -> x <= 0
let transformed_expr = expr.remove_boolean_negations();

assert_eq!(
   transformed_expr,
   NonNegatedBooleanExpression::ComparisonExpression(
        Box::new(IntegerExpression::Atom(Variable::new("x"))),
        ComparisonOp::Leq,
        Box::new(IntegerExpression::Const(0)),
   ),
);
§Implementation

The goal of this function is to remove all boolean negations from an expression. This is done by recursively traversing the expression, if a negation i.e. Not is encountered, the negation is pushed inwards using the helper function negate_expression.

The helper function then: - replaces a BooleanExpression::True constant with a BooleanExpression::False and vice versa - calls BooleanExpression::remove_boolean_negations again if another BooleanExpression::Not is encountered (double negation) - inverts the comparison operator if a comparison expression is encountered - inverts the boolean connective if a binary expression is encountered and continues recursively on the left and right side of the binary expression

Source

fn negate_expression(&self) -> NonNegatedBooleanExpression<T>

Computes the negation of an expression and removes any inner negations.

Helper function for BooleanExpression::remove_boolean_negations, called when a negation is encountered to remove it by pushing it inwards.

Trait Implementations§

Source§

impl<T> BitAnd for BooleanExpression<T>
where T: Atomic,

Source§

type Output = BooleanExpression<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: BooleanExpression<T>) -> BooleanExpression<T>

Performs the & operation. Read more
Source§

impl<T> BitOr for BooleanExpression<T>
where T: Atomic,

Source§

type Output = BooleanExpression<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: BooleanExpression<T>) -> BooleanExpression<T>

Performs the | operation. Read more
Source§

impl<T: Clone + Atomic> Clone for BooleanExpression<T>

Source§

fn clone(&self) -> BooleanExpression<T>

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<T: Debug + Atomic> Debug for BooleanExpression<T>

Source§

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

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

impl<T: Atomic> Display for BooleanExpression<T>

Source§

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

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

impl From<BooleanExpression<Parameter>> for BooleanExpression<Location>

Source§

fn from(value: BooleanExpression<Parameter>) -> Self

Converts to this type from the input type.
Source§

impl From<BooleanExpression<Parameter>> for BooleanExpression<Variable>

Source§

fn from(value: BooleanExpression<Parameter>) -> Self

Converts to this type from the input type.
Source§

impl<T: Atomic> From<BooleanExpression<T>> for NonNegatedBooleanExpression<T>

Source§

fn from(val: BooleanExpression<T>) -> Self

Converts to this type from the input type.
Source§

impl<S, T> From<ThresholdConstraintOver<T>> for BooleanExpression<S>

Source§

fn from(val: ThresholdConstraintOver<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Atomic> Hash for BooleanExpression<T>

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<T> Not for BooleanExpression<T>
where T: Atomic,

Source§

type Output = BooleanExpression<T>

The resulting type after applying the ! operator.
Source§

fn not(self) -> BooleanExpression<T>

Performs the unary ! operation. Read more
Source§

impl<T: Ord + Atomic> Ord for BooleanExpression<T>

Source§

fn cmp(&self, other: &BooleanExpression<T>) -> 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<T: PartialEq + Atomic> PartialEq for BooleanExpression<T>

Source§

fn eq(&self, other: &BooleanExpression<T>) -> 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<T: PartialOrd + Atomic> PartialOrd for BooleanExpression<T>

Source§

fn partial_cmp(&self, other: &BooleanExpression<T>) -> 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 TryFrom<&BooleanExpression<Variable>> for LIAVariableConstraint

Source§

type Error = ConstraintRewriteError

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

fn try_from(expr: &BooleanExpression<Variable>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BooleanExpression<Variable>> for LIAVariableConstraint

Source§

type Error = ConstraintRewriteError

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

fn try_from(expr: BooleanExpression<Variable>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Eq + Atomic> Eq for BooleanExpression<T>

Source§

impl<T: Atomic> StructuralPartialEq for BooleanExpression<T>

Auto Trait Implementations§

§

impl<T> Freeze for BooleanExpression<T>

§

impl<T> RefUnwindSafe for BooleanExpression<T>
where T: RefUnwindSafe,

§

impl<T> Send for BooleanExpression<T>
where T: Send,

§

impl<T> Sync for BooleanExpression<T>
where T: Sync,

§

impl<T> Unpin for BooleanExpression<T>

§

impl<T> UnwindSafe for BooleanExpression<T>
where T: UnwindSafe,

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> 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.