Crates.io | truth-values |
lib.rs | truth-values |
version | 0.1.0 |
source | src |
created_at | 2024-04-16 03:56:21.283401 |
updated_at | 2024-04-16 03:56:21.283401 |
description | Generate all possible combinations of N bools |
homepage | |
repository | https://github.com/vallentin/truth-values |
max_upload_size | |
id | 1209917 |
size | 56,849 |
Tiny, zero-dependency, zero-allocation*, no_std
library for generating all possible
combinations of n
bool
s. Useful for testing boolean functions,
verifying logical equivalence, and generating truth tables.
*Optional alloc
feature for Vec
related functions.
each()
and each_const()
Consider implementing an interpreter or optimizer, and now you want to assert logical equivalence between expressions, e.g. asserting De Morgan's laws:
Using const generic variant, i.e. where N
is const:
each_const(|[a, b]| {
assert_eq!(!(a || b), !a && !b);
assert_eq!(!(a && b), !a || !b);
});
// The closure is called for each combination of 2 `bool`s, i.e.:
// [false, false]
// [true, false]
// [false, true]
// [true, true]
Using non-const generic variant, i.e. where n
can be dynamic:
each(2, |bools| match bools {
&[a, b] => {
assert_eq!(!(a || b), !a && !b);
assert_eq!(!(a && b), !a || !b);
}
_ => unreachable!(),
});
// The closure is called for each combination of 2 `bool`s, i.e.:
// &[false, false]
// &[true, false]
// &[false, true]
// &[true, true]
gen()
and gen_const()
Alternatively, use gen()
functions to obtain
an Iterator
for generating all combinations. This could be used
to e.g. map each combination into an Expr
for an AST, to easily
generate all Expr
combinations to verify their evaluation.
Using const generic variant, i.e. where N
is const:
#[derive(PartialEq, Debug)]
enum Expr {
Bool(bool),
And(Box<Self>, Box<Self>),
// ...
}
impl Expr {
fn and(lhs: Expr, rhs: Expr) -> Expr {
Expr::And(Box::new(lhs), Box::new(rhs))
}
}
let exprs = truth_values::gen_const()
.map(|[a, b]| {
Expr::and(Expr::Bool(a), Expr::Bool(b))
})
.collect::<Vec<_>>();
assert_eq!(
exprs,
[
Expr::and(Expr::Bool(false), Expr::Bool(false)),
Expr::and(Expr::Bool(true), Expr::Bool(false)),
Expr::and(Expr::Bool(false), Expr::Bool(true)),
Expr::and(Expr::Bool(true), Expr::Bool(true)),
]
);
Using non-const generic variant, i.e. where n
can be dynamic:
let exprs = truth_values::gen_slice(2, |bools| {
match bools {
&[a, b] => {
Expr::and(Expr::Bool(a), Expr::Bool(b))
}
_ => unreachable!(),
}
})
.collect::<Vec<_>>();
assert_eq!(
exprs,
[
Expr::and(Expr::Bool(false), Expr::Bool(false)),
Expr::and(Expr::Bool(true), Expr::Bool(false)),
Expr::and(Expr::Bool(false), Expr::Bool(true)),
Expr::and(Expr::Bool(true), Expr::Bool(true)),
]
);
bool
s
|
|
|
|
|
|
|
|
The gen()
functions return an Iterator
, which
additionally specializes size_hint()
, count()
, nth()
, last()
.
The Iterator
also implements:
DoubleEndedIterator
implementing next_back()
and nth_back()
ExactSizeIterator
implementing len()
FusedIterator
The amount of combinations is exponential!
The number of combinations for N
boolean variables is 2N
.
In short, 10 variables produce 1024 combinations, but 20 variables
produce over 1 million combinations.
Just alone exhausting the generator for 30 variables, i.e. over 1 billion combinations, takes a handful of seconds on my machine.
Ideally, if used to test expressions, then there will likely only be a handful of variables. However, if user input is accepted, then it might be worth guarding and limiting the number of variables.
So even though up to MAX
(63
) variables for 64-bit platforms
is supported, it is probably undesirable to even attempt to process
that many variables.