Reputation: 363
I would like to use the Rust crate 'boolean_expression' to implement a simple digital logic circuit. For example, suppose we have a simple circuit consisting of two 2-input logic gates, a total of three binary input signals, A, B, and C, and one binary output signal, E.
Let a first gate be a 2-input AND gate having input signals A and B and an output signal D. Let a second gate be a 2-input OR gate having inputs C and D and output E.
Circuit input signals: A, B, C
Circuit output signal: E
Logic gate equations:
D = A && B
E = C || D
where '&&' is the Boolean 'AND' and '||' is the Boolean 'OR'
Let us further suppose that we wish to implement a model of this circuit using reduced ordered binary decision diagrams (BDDs). In one common (Python, for example) implementation of a BDD package I'd define A, B, and C as Boolean variables with some sort of command such as 'let A = BDD.var' or something of that sort. I'd write the gate equations shown above as Boolean expressions. I'd apply some fixed Boolean values (True, False, possibly Don't_Care) to inputs A, B, C, or I'd simply ask "What combinations of the inputs A, B, and C define the output E?"
Now, my question is, using the teachings of the Rust crate 'boolean_expression', how do I do that? How do I define input A as a Boolean variable? How do I define my two gates? How do I examine the output signal E to determine what combinations of the inputs A, B, and C define the output E? In short, what do I type--specifically--to use the features of the Rust crate 'boolean_expression'? If you can show me what to type--all of it--I can take it from there. Thank you.
And, of course, the actual circuit I plan to implement is far more complex than this simple example.
Upvotes: 1
Views: 411
Reputation: 363
Here is a way to use the boolean_expression function bdd.from_expr().
let f = bdd.to_expr(e); // create a usable expression
let g = bdd.from_expr(&f); // include the '&' borrow symbol
println!("f is {:?}", f); // print the expression
where e is a bdd, f is an expression that is printable, and g is a bdd.
Upvotes: 0
Reputation: 363
The author of Rust crate "boolean_expression" has provided the following answer as a 'main.rs' file.
// Add this line to dependencies: boolean_expression = "0.3.9"
// for example, add the line under dependencies in Cargo.toml
use boolean_expression::*;
fn main() {
let mut bdd = BDD::new();
// Build the circuit. The arguments passed to 'bdd.terminal()'
// can be any type, as long as they are all the same type (the
// 'BDD' type is polymorphic on its labels); here, we use strings.
let a = bdd.terminal("A");
let b = bdd.terminal("B");
let c = bdd.terminal("C");
let d = bdd.and(a, b);
let e = bdd.or(c, d);
// Can 'e' be true at all? It should be.
assert!(bdd.sat(e));
// Print the expression equivalent of 'e'.
println!("e = {:?}", bdd.to_expr(e));
// Determine a satisfying assignment of [a, b, c] to let `e` be TRUE.
// `sat` is an Option<HashMap<T, bool>>:
// - if `None`, then there is no satisfying assignment.
// (We already checked for this case above.)
// - if `Some(hashmap)`, then the hashmap gives a mapping from
// values of type T (here, the strings we used above) to
// boolean values.
let sat = bdd.sat_one(e);
match sat {
None => panic!("Shouldn't happen!"),
Some(vars) => {
for (var, value) in &vars {
println!("Satisfying assignment: var {} = {}", var, value);
}
}
}
}
Upvotes: 2