Giter Site home page Giter Site logo

truth_tables's Introduction

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.

truth_tables's People

Contributors

salt-die avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

abir0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.