Popularity
74 Stars
Updated Last
2 Years Ago
Started In
May 2022

TruthTables.jl

Build Status Coverage

TruthTables.jl is an educational package that generates truth tables from Julia expressions using the @truthtable macro.

Installation

To install TruthTables.jl, use the Julia's package manager, Pkg.jl:

julia>] add TruthTables

Usage

To create a truth table use the @truthtable macro passing a proposition (logical expression) as an argument:

julia> using TruthTables

julia> @truthtable p || q
TruthTable
┌───────┬───────┬───────┐
│   p   │   q   │ p ∨ q │
├───────┼───────┼───────┤
│ true  │ true  │ true  │
│ true  │ false │ true  │
│ false │ true  │ true  │
│ false │ false │ false │
└───────┴───────┴───────┘

The @truthtable macro has an optional keyword argument: full. If full is true, the truth table will be created in expanded form.

julia> @truthtable p && (!q || r) full=true
TruthTable
┌───────┬───────┬───────┬───────┬────────┬──────────────┐
│   p   │   q   │   r   │  ¬q   │ ¬q ∨ r │ p ∧ (¬q ∨ r) │
├───────┼───────┼───────┼───────┼────────┼──────────────┤
│ true  │ true  │ true  │ false │ true   │ true         │
│ true  │ true  │ false │ false │ false  │ false        │
│ true  │ false │ true  │ true  │ true   │ true         │
│ true  │ false │ false │ true  │ true   │ true         │
│ false │ true  │ true  │ false │ true   │ false        │
│ false │ true  │ false │ false │ false  │ false        │
│ false │ false │ true  │ true  │ true   │ false        │
│ false │ false │ false │ true  │ true   │ false        │
└───────┴───────┴───────┴───────┴────────┴──────────────┘

It is possible to change the way TruthTables are displayed using TruthTables.showmode!(mode) function. The mode argument can be one of these symbols: :bool (default), :bit or :letter. Boolean values (true and false) will be displayed without formatting in :bool mode, as 1 and 0 in :bit mode and as T and F in :letter mode.

julia> TruthTables.showmode!(:bit)
:bit

julia> @truthtable p || q <--> r
TruthTable
┌───┬───┬───┬──────────────┐
│ p │ q │ r │ p ∨ q <--> r │
├───┼───┼───┼──────────────┤
│ 1 │ 1 │ 1 │ 1            │
│ 1 │ 1 │ 0 │ 0            │
│ 1 │ 0 │ 1 │ 1            │
│ 1 │ 0 │ 0 │ 0            │
│ 0 │ 1 │ 1 │ 1            │
│ 0 │ 1 │ 0 │ 0            │
│ 0 │ 0 │ 1 │ 0            │
│ 0 │ 0 │ 0 │ 1            │
└───┴───┴───┴──────────────┘

julia> @truthtable p || q <--> r full=true
TruthTable
┌───┬───┬───┬───────┬──────────────┐
│ p │ q │ r │ p ∨ q │ p ∨ q <--> r │
├───┼───┼───┼───────┼──────────────┤
│ 1 │ 1 │ 1 │ 1     │ 1            │
│ 1 │ 1 │ 0 │ 1     │ 0            │
│ 1 │ 0 │ 1 │ 1     │ 1            │
│ 1 │ 0 │ 0 │ 1     │ 0            │
│ 0 │ 1 │ 1 │ 1     │ 1            │
│ 0 │ 1 │ 0 │ 1     │ 0            │
│ 0 │ 0 │ 1 │ 0     │ 0            │
│ 0 │ 0 │ 0 │ 0     │ 1            │
└───┴───┴───┴───────┴──────────────┘

julia> TruthTables.showmode!(:letter)
:letter

julia> @truthtable !(p || q) <--> (!p && !q)
TruthTable
┌───┬───┬───────────────────────┐
│ p │ q │ ¬(p ∨ q) <--> ¬p ∧ ¬q │
├───┼───┼───────────────────────┤
│ T │ T │ T                     │
│ T │ F │ T                     │
│ F │ T │ T                     │
│ F │ F │ T                     │
└───┴───┴───────────────────────┘

julia> @truthtable !(p || q) <--> (!p && !q) full=true
TruthTable
┌───┬───┬───────┬──────────┬────┬────┬─────────┬───────────────────────┐
│ p │ q │ p ∨ q │ ¬(p ∨ q) │ ¬p │ ¬q │ ¬p ∧ ¬q │ ¬(p ∨ q) <--> ¬p ∧ ¬q │
├───┼───┼───────┼──────────┼────┼────┼─────────┼───────────────────────┤
│ T │ T │ T     │ F        │ F  │ F  │ F       │ T                     │
│ T │ F │ T     │ F        │ F  │ T  │ F       │ T                     │
│ F │ T │ T     │ F        │ T  │ F  │ F       │ T                     │
│ F │ F │ F     │ T        │ T  │ T  │ T       │ T                     │
└───┴───┴───────┴──────────┴────┴────┴─────────┴───────────────────────┘

Some logical operators can be expressed using different symbols. This is the list of available symbols:

Operator Symbols
AND &&, &, ∧ (\wedge<tab>)
OR ||, |, ∨ (\vee<tab>)
NOT !, ~, ¬ (\neg<tab>)
XOR ⊻ (\xor<tab>)
NAND ⊼ (\nand<tab>)
NOR ⊽ (\nor<tab>)
IMPLICATION -->, → (\to<tab> or \rightarrow<tab>), ⇒ ( \Rightarrow<tab>)
EQUIVALENCE <-->, ===, ≡ (\equiv<tab>), ↔ (\leftrightarrow<tab>), ⇔ (\Leftrightarrow<tab>)

Examples:

julia> TruthTables.showmode!() # default show mode
:bool

julia> @truthtable ~p & (q | r)
TruthTable
┌───────┬───────┬───────┬──────────────┐
│   p   │   q   │   r   │ ¬p ∧ (q ∨ r) │
├───────┼───────┼───────┼──────────────┤
│ true  │ true  │ true  │ false        │
│ true  │ true  │ false │ false        │
│ true  │ false │ true  │ false        │
│ true  │ false │ false │ false        │
│ false │ true  │ true  │ true         │
│ false │ true  │ false │ true         │
│ false │ false │ true  │ true         │
│ false │ false │ false │ false        │
└───────┴───────┴───────┴──────────────┘

julia> @truthtable ~p & (q | r) full=true
TruthTable
┌───────┬───────┬───────┬───────┬───────┬──────────────┐
│   p   │   q   │   r   │  ¬p   │ q ∨ r │ ¬p ∧ (q ∨ r) │
├───────┼───────┼───────┼───────┼───────┼──────────────┤
│ true  │ true  │ true  │ false │ true  │ false        │
│ true  │ true  │ false │ false │ true  │ false        │
│ true  │ false │ true  │ false │ true  │ false        │
│ true  │ false │ false │ false │ false │ false        │
│ false │ true  │ true  │ true  │ true  │ true         │
│ false │ true  │ false │ true  │ true  │ true         │
│ false │ false │ true  │ true  │ true  │ true         │
│ false │ false │ false │ true  │ false │ false        │
└───────┴───────┴───────┴───────┴───────┴──────────────┘

julia> TruthTables.showmode!(:bit)
:bit

julia> @truthtable (p --> q) ≡ (¬p ∨ q)
TruthTable
┌───┬───┬────────────────────┐
│ p │ q │ (p --> q) ≡ ¬p ∨ q │
├───┼───┼────────────────────┤
│ 1 │ 1 │ 1                  │
│ 1 │ 0 │ 1                  │
│ 0 │ 1 │ 1                  │
│ 0 │ 0 │ 1                  │
└───┴───┴────────────────────┘

julia> @truthtable (p --> q) ≡ (¬p ∨ q) full=true
TruthTable
┌───┬───┬─────────┬────┬────────┬────────────────────┐
│ p │ q │ p --> q │ ¬p │ ¬p ∨ q │ (p --> q) ≡ ¬p ∨ q │
├───┼───┼─────────┼────┼────────┼────────────────────┤
│ 1 │ 1 │ 1       │ 0  │ 1      │ 1                  │
│ 1 │ 0 │ 0       │ 0  │ 0      │ 1                  │
│ 0 │ 1 │ 1       │ 1  │ 1      │ 1                  │
│ 0 │ 0 │ 1       │ 1  │ 1      │ 1                  │
└───┴───┴─────────┴────┴────────┴────────────────────┘

Used By Packages

No packages found.