### 12 unstable releases (3 breaking)

0.4.2 | Mar 27, 2023 |
---|---|

0.4.1 | Mar 27, 2023 |

0.3.2 | Mar 27, 2023 |

0.2.0 | Jun 8, 2021 |

0.1.4 | May 30, 2021 |

#**638** in Encoding

**28** downloads per month

**MIT/Apache**

40KB

699 lines

`simple_predicates`

-- a library for modelling simple boolean predicates

`simple_predicates`

A predicate is a formula that can be evaluated to true or false as a function of the values of the variables that occur in it. In

, the variables denoted by a user-chosen type satisfying the `simple_predicates`

trait (which requires `Eval`

and `Clone`

.) The `PartialEq`

trait also provides an associated `Eval`

type which can be used to provide contextual data needed to resolve the variables. The `Context`

, `Expr`

, and `Cnf`

types can be used to construct evaluable expressions.`Dnf`

# Installation

Add the following to your

:`Cargo.toml`

`[``dependencies``]`
`simple_predicates ``=` `"`0.2`"`

## Features

Feature | Description |
---|---|

"serde" | Enables serialization and deserialization of data using serde. |

By default, there are no features enabled.

# Example Usage

Lets say we want to write boolean expressions denoting the possibility of a

relating to some `Vec`

values according to the `u32`

method. We start by implementing `Vec`` ::`contains

`Eval`

:`use` `simple_predicates``::`Eval`;`
`//` Wrapper struct needed because `u32` & `Eval` are both foreign.
`#``[``derive``(``Clone``,` PartialEq`)``]`
`struct` `Contains``(`pub `u32``)``;`
`impl` `Eval ``for`` ``Contains` `{`
`type` `Context` `=` `Vec``<``u32``>``;`
`fn` `eval``(``&``self`, `data``:` `&``Self``::`Context`)`` ``->` `bool` `{`
`//` Evaluate the u32 by checking if it is in the `Vec`.
data`.``contains``(``&``self``.``0``)`
`}`
`}`

Now we can check arbitrary containment conditions on a given

like so:`Vec`

`use` `simple_predicates``::`Eval`;`
`use` `simple_predicates``::``Expr``::``*``;`
`let` items`:` `Vec``<``u32``>` `=` `vec!``[``1``,` `2``,` `4``,` `7``,` `9``,` `10``]``;`
`//` `eval` is `true` if input contains 4 but not 5.
`let` expr `=` And`(`
`Box``::`new`(`Var`(`Contains`(``4``)``)``)``,`
`Box``::`new`(`Not`(``Box``::`new`(`Var`(`Contains`(``5``)``)``)``)``)``)``;`
`assert!``(`expr`.``eval``(``&`items`)``)``;`

# Cnf / Dnf

For more complex expressions, the nesting of

and `And`

expressions can get very tedious, so the `Or`

and `Cnf`

types are provided to simplify their handling.`Dnf`

The

type represents the Conjunctive Normal Form of a boolean expression; a set of expressions which are `Cnf`

ed together. The `And`

type represents the Disjunctive Normal Form of a boolean expression; a set of expressions which are `Dnf`

ed together.`Or`

The

and `Cnf`

types can only be used if the variable type implements `Dnf`

and `Eq`

. They have identical APIs, so the examples below are representative of either.`Hash`

# Examples

A

can be constructed from an Expr, using the `Cnf`

trait:`From`

`use` `simple_predicates``::`Eval`;`
`use` `simple_predicates``::``Expr``::``*``;`
`let` items`:` `Vec``<``u32``>` `=` `vec!``[``1``,` `2``,` `4``,` `7``,` `9``,` `10``]``;`
`let` cnf `=` `Cnf``::`from`(`
And`(`
`Box``::`new`(`Var`(`Contains`(``4``)``)``)``,`
`Box``::`new`(`Not`(``Box``::`new`(`Var`(`Contains`(``5``)``)``)``)``)``)``)``;`
`assert!``(`cnf`.``eval``(``&`items`)``)``;`

A

can also be constructed from anything that emits Exprs with the `Cnf`

trait:`IntoIterator`

`use` `simple_predicates``::`Eval`;`
`use` `simple_predicates``::``Expr``::``*``;`
`let` items`:` `Vec``<``u32``>` `=` `vec!``[``1``,` `2``,` `4``,` `7``,` `9``,` `10``]``;`
`let` cnf `=` `Cnf``::`from`(``vec!``[`
Var`(`Contains`(``4``)``)``,`
Not`(``Box``::`new`(`Var`(`Contains`(``5``)``)``)``)``,`
`]``)``;`
`assert!``(`cnf`.``eval``(``&`items`)``)``;`

# License

simple_predicates is licenced with the MIT license or the Apache version 2.0 license, at your option.

#### Dependencies

~165KB