2 个不稳定版本

0.2.0 2023 年 8 月 30 日
0.1.0 2023 年 8 月 29 日

#1434Rust 模式

MIT 许可证

23KB
247

命题逻辑

crates.io CI

用于生成任何单个复合命题的真值表的 Rust 库。

仅有一个依赖项:cli_table

使用方法

truth_table! 宏

此宏仅创建并返回真值表。

use propositional_logic::prelude::*;
use cli_table::{print_stdout, TableStruct};

let compound_proposition = |p, q, r| -> bool { iff(q, (p && !q) || (!p && q)) && r };

// giving it a function (any function with boolean input and output works, technically)
let table: TableStruct = truth_table!(|p, q, r| => compound_proposition);

// giving it an inline proposition (similar to a closure)
let table: TableStruct = truth_table!(|p, q, r| => inline_compound_proposition -> {
   iff(q, (p && !q) || (!p && q)) && r
});

// giving it both (you can give it as many as you want)
let table: TableStruct = truth_table!(|p, q, r| => {
   compound_proposition,
   inline_compound_proposition -> {
      iff(q, (p && !q) || (!p && q)) && r
   }
});

assert!(print_stdout(table).is_ok());

输出

+-------+-------+-------+----------------------+-----------------------------+
| p     | q     | r     | compound_proposition | inline_compound_proposition |
+-------+-------+-------+----------------------+-----------------------------+
| true  | true  | true  |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+
| true  | true  | false |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+
| true  | false | true  |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+
| true  | false | false |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+
| false | true  | true  |                 true |                        true |
+-------+-------+-------+----------------------+-----------------------------+
| false | true  | false |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+
| false | false | true  |                 true |                        true |
+-------+-------+-------+----------------------+-----------------------------+
| false | false | false |                false |                       false |
+-------+-------+-------+----------------------+-----------------------------+

print_truth_table! 宏

use propositional_logic::prelude::*;

let compound_proposition = |p, q| -> bool { iff(q, p) };

// giving it a function (any function with boolean input and output works, technically)
print_truth_table!(|p, q| => compound_proposition);
println!();
// giving it an inline proposition (similar to a closure)
print_truth_table!(|p| => inline_compound_proposition -> {
   not(p)
});
println!();
// giving it both (you can give it as many as you want, but every item must have a comma at the end (even the last one))
print_truth_table!(|p,q| => {
   compound_proposition,
   inline_compound_proposition -> {
      not(p)
   },
});

输出

+-------+-------+----------------------+
| p     | q     | compound_proposition |
+-------+-------+----------------------+
| true  | true  |                 true |
+-------+-------+----------------------+
| true  | false |                false |
+-------+-------+----------------------+
| false | true  |                false |
+-------+-------+----------------------+
| false | false |                 true |
+-------+-------+----------------------+

+-------+-----------------------------+
| p     | inline_compound_proposition |
+-------+-----------------------------+
| true  |                       false |
+-------+-----------------------------+
| false |                        true |
+-------+-----------------------------+

+-------+-------+----------------------+-----------------------------+
| p     | q     | compound_proposition | inline_compound_proposition |
+-------+-------+----------------------+-----------------------------+
| true  | true  |                 true |                       false |
+-------+-------+----------------------+-----------------------------+
| true  | false |                false |                       false |
+-------+-------+----------------------+-----------------------------+
| false | true  |                false |                        true |
+-------+-------+----------------------+-----------------------------+
| false | false |                 true |                        true |
+-------+-------+----------------------+-----------------------------+

依赖项

~1.7–9.5MB
~64K SLoC