IntegerExpression

Enum IntegerExpression 

Source
pub enum IntegerExpression<T: Atomic> {
    Atom(T),
    Const(u32),
    Param(Parameter),
    BinaryExpr(Box<IntegerExpression<T>>, IntegerOp, Box<IntegerExpression<T>>),
    Neg(Box<IntegerExpression<T>>),
}
Expand description

Integer expressions over objects of type T, parameters and integers

This enum represents integer expressions over objects of type T, parameters and integers.

§Example

use taco_threshold_automaton::expressions::*;

// x + 5
let _ = IntegerExpression::Atom(Variable::new("x"))
            + IntegerExpression::Const(5);

// -x
let _ = -IntegerExpression::Atom(Variable::new("x"));

Variants§

§

Atom(T)

Atomic of type T

§

Const(u32)

Integer constant

§

Param(Parameter)

Parameter

§

BinaryExpr(Box<IntegerExpression<T>>, IntegerOp, Box<IntegerExpression<T>>)

Integer expression combining two Integer expressions through an arithmetic operator

§

Neg(Box<IntegerExpression<T>>)

Negated expression

Implementations§

Source§

impl<T: Atomic> IntegerExpression<T>

Source

pub fn is_atomic(&self) -> bool

Check whether the constraint is a parameter or atomic

§Example
use taco_threshold_automaton::expressions::{IntegerExpression, Parameter, Variable};

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"));
assert_eq!(expr.is_atomic(), true);

let expr : IntegerExpression<Variable> = IntegerExpression::Atom(Variable::new("x"));
assert_eq!(expr.is_atomic(), true);

let expr : IntegerExpression<Variable> = IntegerExpression::Const(1);
assert_eq!(expr.is_atomic(), false);
Source

pub fn contains_atom(&self) -> bool

Recursively check if the expression contains some atom

§Example
use taco_threshold_automaton::expressions::{IntegerExpression, Parameter, Variable};

let expr : IntegerExpression<Variable> = IntegerExpression::Atom(Variable::new("x"));
assert_eq!(expr.contains_atom(), true);

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"));
assert_eq!(expr.contains_atom(), false);

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"))
    + IntegerExpression::Atom(Variable::new("x"));
assert_eq!(expr.contains_atom(), true);
Source

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

Recursively check if the expression references the atom a

§Example
use taco_threshold_automaton::expressions::{IntegerExpression, Parameter, Variable};

let expr : IntegerExpression<Variable> = IntegerExpression::Atom(Variable::new("x"));
assert_eq!(expr.contains_atom_a(&Variable::new("x")), true);
assert_eq!(expr.contains_atom_a(&Variable::new("y")), false);

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"));
assert_eq!(expr.contains_atom_a(&Variable::new("x")), false);

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"))
    + IntegerExpression::Atom(Variable::new("x"));
assert_eq!(expr.contains_atom_a(&Variable::new("x")), true);
assert_eq!(expr.contains_atom_a(&Variable::new("y")), false);
Source

pub fn try_to_evaluate_to_const(&self) -> Option<i64>

Try to evaluate the integer expression to a constant

This function succeeds if all sub-expressions are constants, otherwise it returns None.

Note: This function uses integer division (e.g. 5 / 2 = 2), be careful when using for simplification.

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

let expr = IntegerExpression::<Location>::Const(5)
    + IntegerExpression::Const(3);
assert_eq!(expr.try_to_evaluate_to_const(), Some(8));

let expr = IntegerExpression::<Location>::Param(Parameter::new("n"));
assert_eq!(expr.try_to_evaluate_to_const(), None);

let expr = IntegerExpression::<Location>::Const(5)
   / IntegerExpression::Const(2);
assert_eq!(expr.try_to_evaluate_to_const(), Some(2));
Source

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

Try to evaluate the integer expression in a given environment to a constant

This function succeeds if all sub-expressions are constants, parameters or atoms with a value specified in environment env or param_env.

If a parameter or atom is not found in the environment, an error is returned.

Note: This function uses integer division (e.g. 5 / 2 = 2), be careful when using for simplification.

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

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

let expr = IntegerExpression::<Location>::Const(5)
   + IntegerExpression::Const(3);
assert_eq!(expr.try_to_evaluate_to_const_with_env(&env, &param_env), Ok(8));

let expr = IntegerExpression::<Location>::Const(5)
   - IntegerExpression::Atom(Location::new("loc"));
assert_eq!(expr.try_to_evaluate_to_const_with_env(&env, &param_env), Ok(4));

let expr = IntegerExpression::<Location>::Const(5)
   - IntegerExpression::Atom(Location::new("loc"))
   - IntegerExpression::Param(Parameter::new("n"));
assert_eq!(expr.try_to_evaluate_to_const_with_env(&env, &param_env), Ok(3));
Source

pub fn substitute_atom_with(&mut self, to_replace: &T, replace_with: &Self)

Substitute to_replace in this integer expression with integer expression replace_with

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

let mut expr = IntegerExpression::Const(5)
    + IntegerExpression::Atom(Location::new("l"));
expr.substitute_atom_with(&Location::new("l"), &IntegerExpression::Const(3));

let expected_expr = IntegerExpression::Const(5)
    + IntegerExpression::Const(3);
assert_eq!(expr, expected_expr);
Source

pub fn contains_parameter(&self) -> bool

Check whether the expressions contains a IntegerExpression::Param clause

This function checks whether an IntegerExpression contains a parameter.

Important: For expressions of type IntegerExpression<Parameter> one needs to additionally check whether the expression contains an atom using IntegerExpression::contains_atom.

§Example
use taco_threshold_automaton::expressions::{IntegerExpression, Parameter, Variable};

let expr : IntegerExpression<Variable> = IntegerExpression::Param(Parameter::new("x"));
assert_eq!(expr.contains_parameter(), true);

let expr : IntegerExpression<Variable> = IntegerExpression::Const(1);
assert_eq!(expr.contains_parameter(), false);

// careful when using with `IntegerExpression<Parameter>`
let expr : IntegerExpression<Parameter> = IntegerExpression::Atom(Parameter::new("x"));
assert_eq!(expr.contains_parameter(), false);
Source§

impl<T: Atomic> IntegerExpression<T>

Source

pub fn remove_unary_neg_and_sub(self) -> NonMinusIntegerExpr<T>

Remove unary negations and subtractions from the integer expression such that there are only negated constants.

§Implementation

The function recursively traverses the expression tree and pushes negations inwards until they are in front of constants, atoms, or parameters. In the latter two cases, it adds a -1 * in front of the atom or parameter.

§Example

// 1 - (n + -5)
let expr = IntegerExpression::BinaryExpr(
    Box::new(IntegerExpression::Const(1)),
    IntegerOp::Sub,
    Box::new(IntegerExpression::BinaryExpr(
        Box::new(IntegerExpression::Param(Parameter::new("n"))),
        IntegerOp::Add,
        Box::new(IntegerExpression::Neg(Box::new(IntegerExpression::Const(5)))),
    )),
);

// 1 + (-1 * n + 5)
let expected_expr : NonMinusIntegerExpr<Variable> = NonMinusIntegerExpr::BinaryExpr(
    Box::new(NonMinusIntegerExpr::Const(1)),
    NonMinusIntegerOp::Add,
    Box::new(NonMinusIntegerExpr::BinaryExpr(
        Box::new(
            NonMinusIntegerExpr::BinaryExpr(
                Box::new(NonMinusIntegerExpr::NegConst(1)),
                NonMinusIntegerOp::Mul,
                Box::new(NonMinusIntegerExpr::Param(Parameter::new("n"))),
           )
        ),
        NonMinusIntegerOp::Add,
        Box::new(NonMinusIntegerExpr::Const(5)),
    )),
);
     
let got_expr = expr.remove_unary_neg_and_sub();
assert_eq!(got_expr, expected_expr);
Source

fn negate_expression(self) -> NonMinusIntegerExpr<T>

Compute the negation of expression and removes any inner negations or subtractions

Helper function for remove_unary_neg_and_sub: This function pushes a found negation inwards until they are in front of constants, atoms, or parameters. In the latter two cases, it adds a -1 * in front of the atom or parameter.

Source

fn has_removable_negation_inside(&self, has_neg: bool) -> bool

Check whether further down the expression tree there is a negation that will be removed if we push the negation inwards.

Helper function for negate_expression. To check for a removable negation, for expression ex call with has_neg initially set to false, i.e. ex.has_removable_negation_inside(false).

Trait Implementations§

Source§

impl<T> Add for IntegerExpression<T>
where T: Atomic,

Source§

type Output = IntegerExpression<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: IntegerExpression<T>) -> IntegerExpression<T>

Performs the + operation. Read more
Source§

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

Source§

fn clone(&self) -> IntegerExpression<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 IntegerExpression<T>

Source§

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

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

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

Source§

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

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

impl<T> Div for IntegerExpression<T>
where T: Atomic,

Source§

type Output = IntegerExpression<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: IntegerExpression<T>) -> IntegerExpression<T>

Performs the / operation. Read more
Source§

impl<T: Atomic> From<Fraction> for IntegerExpression<T>

Source§

fn from(value: Fraction) -> Self

Converts to this type from the input type.
Source§

impl From<IntegerExpression<Parameter>> for IntegerExpression<Location>

Source§

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

Converts to this type from the input type.
Source§

impl From<IntegerExpression<Parameter>> for IntegerExpression<Variable>

Source§

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

Converts to this type from the input type.
Source§

impl<T: Atomic> From<IntegerExpression<T>> for NonMinusIntegerExpr<T>

Source§

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

Converts to this type from the input type.
Source§

impl From<Location> for IntegerExpression<Location>

Source§

fn from(value: Location) -> Self

Converts to this type from the input type.
Source§

impl<T: Atomic> From<NonMinusIntegerExpr<T>> for IntegerExpression<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<Parameter> for IntegerExpression<T>
where T: Atomic,

Source§

fn from(value: Parameter) -> Self

Converts to this type from the input type.
Source§

impl From<Variable> for IntegerExpression<Variable>

Source§

fn from(value: Variable) -> Self

Converts to this type from the input type.
Source§

impl<S> From<u32> for IntegerExpression<S>
where S: Atomic,

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Atomic> Hash for IntegerExpression<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> Mul for IntegerExpression<T>
where T: Atomic,

Source§

type Output = IntegerExpression<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: IntegerExpression<T>) -> IntegerExpression<T>

Performs the * operation. Read more
Source§

impl<T> Neg for IntegerExpression<T>
where T: Atomic,

Source§

type Output = IntegerExpression<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> IntegerExpression<T>

Performs the unary - operation. Read more
Source§

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

Source§

fn cmp(&self, other: &IntegerExpression<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 IntegerExpression<T>

Source§

fn eq(&self, other: &IntegerExpression<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 IntegerExpression<T>

Source§

fn partial_cmp(&self, other: &IntegerExpression<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<T> Sub for IntegerExpression<T>
where T: Atomic,

Source§

type Output = IntegerExpression<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: IntegerExpression<T>) -> IntegerExpression<T>

Performs the - operation. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for IntegerExpression<T>
where T: Freeze,

§

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

§

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

§

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

§

impl<T> Unpin for IntegerExpression<T>
where T: Unpin,

§

impl<T> UnwindSafe for IntegerExpression<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.