2011-12-13 18:25:51 -06:00
|
|
|
// -*- rust -*-
|
|
|
|
|
2012-03-10 20:01:01 -06:00
|
|
|
#[doc = "Boolean logic"];
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
export not, and, or, xor, implies;
|
|
|
|
export eq, ne, is_true, is_false;
|
|
|
|
export from_str, to_str, all_values, to_bit;
|
|
|
|
|
2012-03-10 20:01:01 -06:00
|
|
|
#[doc = "Negation / inverse"]
|
|
|
|
pure fn not(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Conjunction"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn and(a: bool, b: bool) -> bool { a && b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Disjunction"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn or(a: bool, b: bool) -> bool { a || b }
|
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
Exclusive or
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-10 20:01:01 -06:00
|
|
|
Identical to `or(and(a, not(b)), and(not(a), b))`
|
|
|
|
"]
|
|
|
|
pure fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Implication in the logic, i.e. from `a` follows `b`"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn implies(a: bool, b: bool) -> bool { !a || b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
|
|
|
true if truth values `a` and `b` are indistinguishable in the logic
|
|
|
|
"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn eq(a: bool, b: bool) -> bool { a == b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "true if truth values `a` and `b` are distinguishable in the logic"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn ne(a: bool, b: bool) -> bool { a != b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "true if `v` represents truth in the logic"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn is_true(v: bool) -> bool { v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "true if `v` represents falsehood in the logic"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn is_false(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Parse logic value from `s`"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn from_str(s: str) -> option<bool> {
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check s {
|
2012-02-22 06:18:15 -06:00
|
|
|
"true" { some(true) }
|
|
|
|
"false" { some(false) }
|
|
|
|
_ { none }
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Convert `v` into a string"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn to_str(v: bool) -> str { if v { "true" } else { "false" } }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
|
|
|
Iterates over all truth values by passing them to `blk` in an unspecified
|
|
|
|
order
|
|
|
|
"]
|
2012-03-10 20:01:01 -06:00
|
|
|
fn all_values(blk: fn(v: bool)) {
|
2011-12-13 18:25:51 -06:00
|
|
|
blk(true);
|
|
|
|
blk(false);
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "converts truth value to an 8 bit byte"]
|
2012-03-10 20:01:01 -06:00
|
|
|
pure fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test_bool_from_str() {
|
2012-06-30 18:19:07 -05:00
|
|
|
do all_values |v| {
|
2012-02-22 06:18:15 -06:00
|
|
|
assert some(v) == from_str(bool::to_str(v))
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bool_to_str() {
|
|
|
|
assert to_str(false) == "false";
|
|
|
|
assert to_str(true) == "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bool_to_bit() {
|
2012-06-30 18:19:07 -05:00
|
|
|
do all_values |v| {
|
2012-01-17 19:28:21 -06:00
|
|
|
assert to_bit(v) == if is_true(v) { 1u8 } else { 0u8 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|