All Projects → chicolucio → truth-table-generator

chicolucio / truth-table-generator

Licence: Apache-2.0 license
truth-table-generator is a tool that allows to generate a truth table

Programming Languages

Jupyter Notebook
11667 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to truth-table-generator

Javascript.anomaly
Examples of not obvious behaviors for javascript beginner programmers
Stars: ✭ 124 (+163.83%)
Mutual labels:  logic
Grakn
TypeDB: a strongly-typed database
Stars: ✭ 2,947 (+6170.21%)
Mutual labels:  logic
awesome-rust-formalized-reasoning
An exhaustive list of all Rust resources regarding automated or semi-automated formalization efforts in any area, constructive mathematics, formal algorithms, and program verification.
Stars: ✭ 185 (+293.62%)
Mutual labels:  logic
Mesecons
Mod for minetest that adds digital circuitry [=minecraft redstone]
Stars: ✭ 165 (+251.06%)
Mutual labels:  logic
Imove
Move your mouse, generate code from flow chart
Stars: ✭ 3,282 (+6882.98%)
Mutual labels:  logic
ciao
Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
Stars: ✭ 190 (+304.26%)
Mutual labels:  logic
Nalu
Basic pytorch implementation of NAC/NALU from Neural Arithmetic Logic Units paper by trask et.al
Stars: ✭ 110 (+134.04%)
Mutual labels:  logic
LPL-solutions
Solutions for the book "Language Proof and Logic".
Stars: ✭ 51 (+8.51%)
Mutual labels:  logic
Limbo
A Reasoning System for a First-Order Logic of Limited Belief, written in C++
Stars: ✭ 233 (+395.74%)
Mutual labels:  logic
illogical
A micro conditional javascript engine used to parse the raw logical and comparison expressions, evaluate the expression in the given data context, and provide access to a text form of the given expressions.
Stars: ✭ 16 (-65.96%)
Mutual labels:  logic
Acl2
ACL2 System and Books as Maintained by the Community
Stars: ✭ 200 (+325.53%)
Mutual labels:  logic
Latte
LaTTe : a Laboratory for Type Theory experiments (in clojure)
Stars: ✭ 210 (+346.81%)
Mutual labels:  logic
bloc
A predictable state management library that helps implement the BLoC design pattern
Stars: ✭ 12 (-74.47%)
Mutual labels:  logic
Dls Schematics
Schematics for DLS - The Digital Logic Simulator game http://makingartstudios.itch.io/dls
Stars: ✭ 124 (+163.83%)
Mutual labels:  logic
ioBroker.linkeddevices
Create linked objects (datapoints) of your devices with a self-defined structure. This makes it possible to create a structure in ioBroker, where all objects are centralized, e.g. to be used in the vis or scripts.
Stars: ✭ 17 (-63.83%)
Mutual labels:  logic
Lbox
Stars: ✭ 116 (+146.81%)
Mutual labels:  logic
CSCv2
Version 2 of my Crazy Small CPU
Stars: ✭ 53 (+12.77%)
Mutual labels:  logic
Leo-III
An Automated Theorem Prover for Classical Higher-Order Logic with Henkin Semantics
Stars: ✭ 29 (-38.3%)
Mutual labels:  logic
binary-decision-diagram
A library to create, minimize and optimize binary decision diagrams
Stars: ✭ 20 (-57.45%)
Mutual labels:  truth-table
awesome-philosophy
A curated list of awesome philosophy
Stars: ✭ 119 (+153.19%)
Mutual labels:  logic

truth-table-generator

truth-table-generator is a tool that allows to generate a truth table. It is a fork of truths by tr3buchet.

Multiple outputs

Build Status codecov

It merges some of the pull requests in the original and other external helpers. The following are some of the changes and enhancements from the original:

  • tabulate instead of obsolete prettytable as main tool to represent tabular data in ASCII tables (PrettyTable version is still available).
    • so there are many table formats available as such LaTeX, Org Tables, HTML and all others cited on tabulate docs
  • the table is now a Pandas DataFrame so you can make the output more visually appealing with Pandas Styling. See examples below.
  • new function valuation that eval a proposition as a tautology, contradiction or contingency.
  • new command line interface (CLI) for printing a truth table from terminal.

Installation

pip install truth-table-generator

Usage

Importing and syntax

First, let's import the package. ttg stands for truth-table-generator.

import ttg

A truth table has one column for each input variable (for example, p and q), and one final column showing all of the possible results of the logical operation that the table represents. If the input has only one list of strings, each string is considered an input variable:

print(ttg.Truths(['p', 'q', 'r']))
+-----+-----+-----+
|  p  |  q  |  r  |
|-----+-----+-----|
|  1  |  1  |  1  |
|  1  |  1  |  0  |
|  1  |  0  |  1  |
|  1  |  0  |  0  |
|  0  |  1  |  1  |
|  0  |  1  |  0  |
|  0  |  0  |  1  |
|  0  |  0  |  0  |
+-----+-----+-----+

A second list of strings can be passed with propositional expressions created with logical operators.

print(ttg.Truths(['p', 'q', 'r'], ['p and q and r', 'p or q or r', '(p or (~q)) => r']))
+-----+-----+-----+-----------------+---------------+--------------------+
|  p  |  q  |  r  |  p and q and r  |  p or q or r  |  (p or (~q)) => r  |
|-----+-----+-----+-----------------+---------------+--------------------|
|  1  |  1  |  1  |        1        |       1       |         1          |
|  1  |  1  |  0  |        0        |       1       |         0          |
|  1  |  0  |  1  |        0        |       1       |         1          |
|  1  |  0  |  0  |        0        |       1       |         0          |
|  0  |  1  |  1  |        0        |       1       |         1          |
|  0  |  1  |  0  |        0        |       1       |         1          |
|  0  |  0  |  1  |        0        |       1       |         1          |
|  0  |  0  |  0  |        0        |       0       |         0          |
+-----+-----+-----+-----------------+---------------+--------------------+

Operators and their representations:

  • negation: 'not', '-', '~'
  • logical disjunction: 'or'
  • logical nor: 'nor'
  • exclusive disjunction: 'xor', '!='
  • logical conjunction: 'and'
  • logical NAND: 'nand'
  • material implication: '=>', 'implies'
  • logical biconditional: '='

Note: Use parentheses! Especially with the negation operator. Use tables above and below as reference. Although precedence rules are used, sometimes precedence between conjunction and disjunction is unspecified requiring to provide it explicitly in given formula with parentheses.

Showing words (True / False)

If you prefer the words True and False instead of numbers 0 and 1, there is a third parameter, boolean type, ints that can be set to False:

print(ttg.Truths(['p', 'q'], ['p and q', 'p or q', '(p or (~q)) => (~p)'], ints=False))
+-------+-------+-----------+----------+-----------------------+
|   p   |   q   |  p and q  |  p or q  |  (p or (~q)) => (~p)  |
|-------+-------+-----------+----------+-----------------------|
| True  | True  |   True    |   True   |         False         |
| True  | False |   False   |   True   |         False         |
| False | True  |   False   |   True   |         True          |
| False | False |   False   |  False   |         True          |
+-------+-------+-----------+----------+-----------------------+

Reverse output option

If you prefer to have propositions in ascending order (0/False before 1/True), there is the ascending parameter that can be set to True. Let's change the above example to ascending order:

print(ttg.Truths(['p', 'q'], ['p and q', 'p or q', '(p or (~q)) => (~p)'], ints=False, ascending=True))
+-------+-------+-----------+----------+-----------------------+
|   p   |   q   |  p and q  |  p or q  |  (p or (~q)) => (~p)  |
|-------+-------+-----------+----------+-----------------------|
| False | False |   False   |  False   |         True          |
| False | True  |   False   |   True   |         True          |
| True  | False |   False   |   True   |         False         |
| True  | True  |   True    |   True   |         False         |
+-------+-------+-----------+----------+-----------------------+

Formatting options with PrettyTable and Tabulate

For more formatting options, let's create a truth table variable:

table = ttg.Truths(['p', 'q'], ['p => q', 'p = q'])

The command print(table) renders the standard table as seen on above examples:

+-----+-----+----------+---------+
|  p  |  q  |  p => q  |  p = q  |
|-----+-----+----------+---------|
|  1  |  1  |    1     |    1    |
|  1  |  0  |    0     |    0    |
|  0  |  1  |    1     |    0    |
|  0  |  0  |    1     |    1    |
+-----+-----+----------+---------+

The command print(table.as_prettytable()) renders the table with PrettyTable package as on the original version of this package:

+---+---+--------+-------+
| p | q | p => q | p = q |
+---+---+--------+-------+
| 1 | 1 |   1    |   1   |
| 1 | 0 |   0    |   0   |
| 0 | 1 |   1    |   0   |
| 0 | 0 |   1    |   1   |
+---+---+--------+-------+

As can be seen, the PrettyTable output has less blank spaces. However, the PrettyTable package has much less output options and it is deprecated. So I decided to use the Tabulate package as standard.

The command print(table.as_tabulate()) renders the table with Tabulate package. The first column presents line numbers (that can be disabled with the parameter index=False):

+----+-----+-----+----------+---------+
|    |  p  |  q  |  p => q  |  p = q  |
|----+-----+-----+----------+---------|
| 1  |  1  |  1  |    1     |    1    |
| 2  |  1  |  0  |    0     |    0    |
| 3  |  0  |  1  |    1     |    0    |
| 4  |  0  |  0  |    1     |    1    |
+----+-----+-----+----------+---------+

Using Tabulate, we can use any of the formats available. Let's output a LaTeX table without the line number column:

print(table.as_tabulate(index=False, table_format='latex'))
\begin{tabular}{cccc}
\hline
  p  &  q  &  p =\ensuremath{>} q  &  p = q  \\
\hline
  1  &  1  &    1     &    1    \\
  1  &  0  &    0     &    0    \\
  0  &  1  &    1     &    0    \\
  0  &  0  &    1     &    1    \\
\hline
\end{tabular}

Formatting options with Pandas

With an IPython terminal or a Jupyter Notebook, it is possible to render a Pandas DataFrame with table.as_pandas():

pandas01

And this output can be modified with Pandas Styling

pandas02

More advanced modifications can be done with functions that apply styling changes. See the styles tutorial notebook for examples. See the image below for a fancy example with two lines and two columns highlighted with yellow background and different colors for True and False.

pandas03

The valuation function

Let's see the how to use the valuation function with a new truth table:

table_val = ttg.Truths(['p', 'q'], ['p = q', 'p and (~p)', '(p and q) => p'])
print(table_val)
+-----+-----+---------+--------------+------------------+
|  p  |  q  |  p = q  |  p and (~p)  |  (p and q) => p  |
|-----+-----+---------+--------------+------------------|
|  1  |  1  |    1    |      0       |        1         |
|  1  |  0  |    0    |      0       |        1         |
|  0  |  1  |    0    |      0       |        1         |
|  0  |  0  |    1    |      0       |        1         |
+-----+-----+---------+--------------+------------------+

Without arguments, the valuation function classifies the last column as a tautology, a contradiction or a contingency:

table_val.valuation()
'Tautology'

If a integer is used as argument, the function classifies the correspondent column:

table_val.valuation(3)
'Contingency'
table_val.valuation(4)
'Contradiction'

CLI utility

For those who work in the terminal there is a simple command line interface (CLI) for printing tables. The script name is ttg_cly.py and it accepts the following syntax according to its --help:

usage: ttg_cli.py [-h] [-p PROPOSITIONS] [-i INTS] [-a ASCENDING] variables

positional arguments:
  variables             List of variables e. g. "['p', 'q']"

optional arguments:
  -h, --help            show this help message and exit
  -p PROPOSITIONS, --propositions PROPOSITIONS
                        List of propositions e. g. "['p or q', 'p and q']"
  -i INTS, --ints INTS  True for 0 and 1; False for words
  -a ASCENDING, --ascending ASCENDING
                        True for reverse output (False before True)

As seen, the list of variables is mandatory. Note that the lists must be between ".

$ ttg_cli.py "['p', 'q', 'r']"
+-----+-----+-----+
|  p  |  q  |  r  |
|-----+-----+-----|
|  1  |  1  |  1  |
|  1  |  1  |  0  |
|  1  |  0  |  1  |
|  1  |  0  |  0  |
|  0  |  1  |  1  |
|  0  |  1  |  0  |
|  0  |  0  |  1  |
|  0  |  0  |  0  |
+-----+-----+-----+

The CLI utility also has an option, -i, to show words instead of numbers:

$ ttg_cli.py "['p', 'q', 'r']" -i False
+-------+-------+-------+
|   p   |   q   |   r   |
|-------+-------+-------|
| True  | True  | True  |
| True  | True  | False |
| True  | False | True  |
| True  | False | False |
| False | True  | True  |
| False | True  | False |
| False | False | True  |
| False | False | False |
+-------+-------+-------+

A -p parameter must be before the propositions list:

$ ttg_cli.py "['p', 'q', 'r']" -p "['p or q', 'p and q or r']"
+-----+-----+-----+----------+----------------+
|  p  |  q  |  r  |  p or q  |  p and q or r  |
|-----+-----+-----+----------+----------------|
|  1  |  1  |  1  |    1     |       1        |
|  1  |  1  |  0  |    1     |       1        |
|  1  |  0  |  1  |    1     |       1        |
|  1  |  0  |  0  |    1     |       0        |
|  0  |  1  |  1  |    1     |       1        |
|  0  |  1  |  0  |    1     |       0        |
|  0  |  0  |  1  |    0     |       1        |
|  0  |  0  |  0  |    0     |       0        |
+-----+-----+-----+----------+----------------+

With words instead of numbers:

$ ttg_cli.py "['p', 'q', 'r']" -p "['p or q', 'p and q or r']" -i False
+-------+-------+-------+----------+----------------+
|   p   |   q   |   r   |  p or q  |  p and q or r  |
|-------+-------+-------+----------+----------------|
| True  | True  | True  |   True   |      True      |
| True  | True  | False |   True   |      True      |
| True  | False | True  |   True   |      True      |
| True  | False | False |   True   |     False      |
| False | True  | True  |   True   |      True      |
| False | True  | False |   True   |     False      |
| False | False | True  |  False   |      True      |
| False | False | False |  False   |     False      |
+-------+-------+-------+----------+----------------+

In ascending order (0/False before 1/True):

$ ttg_cli.py "['p', 'q', 'r']" -p "['p or q', 'p and q or r']" -i False -a True
+-------+-------+-------+----------+----------------+
|   p   |   q   |   r   |  p or q  |  p and q or r  |
|-------+-------+-------+----------+----------------|
| False | False | False |  False   |     False      |
| False | False | True  |  False   |      True      |
| False | True  | False |   True   |     False      |
| False | True  | True  |   True   |      True      |
| True  | False | False |   True   |     False      |
| True  | False | True  |   True   |      True      |
| True  | True  | False |   True   |      True      |
| True  | True  | True  |   True   |      True      |
+-------+-------+-------+----------+----------------+

The real look of the table depends on your terminal appearance configuration. The green on black background screenshots from the first picture of this README are from my terminal.

Contributing

All contributions are welcome.

Issues

Feel free to submit issues regarding:

  • recommendations
  • more examples for the tutorial
  • enhancement requests and new useful features
  • code bugs

Pull requests

  • before starting to work on your pull request, please submit an issue first
  • fork the repo
  • clone the project to your own machine
  • commit changes to your own branch
  • push your work back up to your fork
  • submit a pull request so that your changes can be reviewed

License

Apache 2.0, see LICENSE

Citing

If you use truth-table-generator in a scientific publication or in classes, please consider citing as

F. L. S. Bustamante, truth-table-generator - generating truth tables., 2019 - Available at: https://github.com/chicolucio/truth-table-generator

Funding

If you enjoy this project and would like to see many more math and science related programming projects, I would greatly appreciate any assistance. Send me an e-mail to know how to assist. Many more projects are to come and your support will be rewarded with more STEM coding projects :-)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].