2011-12-13 18:25:51 -06:00
|
|
|
// -*- rust -*-
|
2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-08-13 19:11:33 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Boolean logic
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-12-27 19:53:04 -06:00
|
|
|
use bool;
|
2012-12-23 16:41:37 -06:00
|
|
|
use cmp;
|
2012-09-04 13:12:17 -05:00
|
|
|
use cmp::Eq;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Negation / inverse
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn not(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Conjunction
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn and(a: bool, b: bool) -> bool { a && b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Disjunction
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn or(a: bool, b: bool) -> bool { a || b }
|
2012-03-10 20:01:01 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Exclusive or
|
|
|
|
*
|
|
|
|
* Identical to `or(and(a, not(b)), and(not(a), b))`
|
|
|
|
*/
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Implication in the logic, i.e. from `a` follows `b`
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn implies(a: bool, b: bool) -> bool { !a || b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// true if truth values `a` and `b` are indistinguishable in the logic
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn eq(a: bool, b: bool) -> bool { a == b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// true if truth values `a` and `b` are distinguishable in the logic
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn ne(a: bool, b: bool) -> bool { a != b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// true if `v` represents truth in the logic
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn is_true(v: bool) -> bool { v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// true if `v` represents falsehood in the logic
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn is_false(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Parse logic value from `s`
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn from_str(s: &str) -> Option<bool> {
|
2012-08-13 19:11:33 -05:00
|
|
|
if s == "true" {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(true)
|
2012-08-13 19:11:33 -05:00
|
|
|
} else if s == "false" {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(false)
|
2012-08-13 19:11:33 -05:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Convert `v` into a string
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn to_str(v: bool) -> ~str { if v { ~"true" } else { ~"false" } }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Iterates over all truth values by passing them to `blk` in an unspecified
|
|
|
|
* order
|
|
|
|
*/
|
2012-09-26 16:54:26 -05:00
|
|
|
pub fn all_values(blk: fn(v: bool)) {
|
2011-12-13 18:25:51 -06:00
|
|
|
blk(true);
|
|
|
|
blk(false);
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// converts truth value to an 8 bit byte
|
2012-09-26 16:54:26 -05:00
|
|
|
pub pure fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-11-30 02:47:45 -06:00
|
|
|
#[cfg(notest)]
|
2012-09-19 20:00:26 -05:00
|
|
|
impl bool : cmp::Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
|
|
|
|
pure fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
2012-09-26 16:54:26 -05:00
|
|
|
pub fn test_bool_from_str() {
|
2012-06-30 18:19:07 -05:00
|
|
|
do all_values |v| {
|
2012-08-20 14:23:37 -05:00
|
|
|
assert Some(v) == from_str(bool::to_str(v))
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 16:54:26 -05:00
|
|
|
pub fn test_bool_to_str() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(false) == ~"false";
|
|
|
|
assert to_str(true) == ~"true";
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 16:54:26 -05:00
|
|
|
pub 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:
|