Skip to content

Print lovely formatted truth tables from any boolean logic expression!

License

Notifications You must be signed in to change notification settings

salt-die/truth_tables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generate truth tables and abstract syntax trees from boolean expressions!

Example usage:

>>> from truth_tables import TruthTable
>>> my_table = TruthTable('p or q', '~p -> q', 'T and ~T')
>>> print(my_table)
┌───┬───┬────────┬─────────┬──────────┐
│ pqp or q~p -> qT and ~T │
├───┼───┼────────┼─────────┼──────────┤
│ FFFFF     │
│ FTTTF     │
│ TFTTF     │
│ TTTTF     │
└───┴───┴────────┴─────────┴──────────┘
>>> print(my_table.ast)
Or
├─Var('p')
╰─Var('q')

Implies
├─Negate
│ ╰─Var('p')
╰─Var('q')

And
├─Const(True)
╰─Negate
  ╰─Const(True)
>>> my_table = TruthTable('~((p xor (q and ~r) or q) and ~(p <-> r))', binary=True)
>>> print(my_table)
┌───┬───┬───┬───────────────────────────────────────────┐
│ pqr~((p xor (q and ~r) or q) and ~(p <-> r)) │
├───┼───┼───┼───────────────────────────────────────────┤
│ 0001                     │
│ 0011                     │
│ 0101                     │
│ 0110                     │
│ 1000                     │
│ 1011                     │
│ 1100                     │
│ 1111                     │
└───┴───┴───┴───────────────────────────────────────────┘

Two TruthTables are equal if they have the same variables and the same truth values (not necessarily the same propositions).

>>> TruthTable('p -> q') == TruthTable('~p or q')
True

The parser will accept symbolic or english names for boolean operators:

┌──────────┬──────────┬─────────┬──────────────┐
│ operator │ symbolic │ english │ alternatives │
├──────────┼──────────┼─────────┼──────────────┤
│   Not    │    ~     │   not   │              │
│   And    │    &     │   and   │              │
│    Or    │    |     │   or    │              │
│ Implies  │    ->    │ implies │     then     │
│   Iff    │   <->    │   iff   │              │
│   Xor    │    ^     │   xor   │              │
└──────────┴──────────┴─────────┴──────────────┘

Precedence is Not, (), And, then everything else.

About

Print lovely formatted truth tables from any boolean logic expression!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages