Skip to content

eliascarv/TruthTables.jl

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 

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 │
├───────┼───────┼───────┤
│ truetruetrue  │
│ truefalsetrue  │
│ falsetruetrue  │
│ falsefalsefalse │
└───────┴───────┴───────┘

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) │
├───────┼───────┼───────┼───────┼────────┼──────────────┤
│ truetruetruefalsetruetrue         │
│ truetruefalsefalsefalsefalse        │
│ truefalsetruetruetruetrue         │
│ truefalsefalsetruetruetrue         │
│ falsetruetruefalsetruefalse        │
│ falsetruefalsefalsefalsefalse        │
│ falsefalsetruetruetruefalse        │
│ falsefalsefalsetruetruefalse        │
└───────┴───────┴───────┴───────┴────────┴──────────────┘

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 │
├───┼───┼───┼──────────────┤
│ 1111            │
│ 1100            │
│ 1011            │
│ 1000            │
│ 0111            │
│ 0100            │
│ 0010            │
│ 0001            │
└───┴───┴───┴──────────────┘

julia> @truthtable p || q <--> r full=true
TruthTable
┌───┬───┬───┬───────┬──────────────┐
│ p │ q │ r │ p ∨ q │ p ∨ q <--> r │
├───┼───┼───┼───────┼──────────────┤
│ 11111            │
│ 11010            │
│ 10111            │
│ 10010            │
│ 01111            │
│ 01010            │
│ 00100            │
│ 00001            │
└───┴───┴───┴───────┴──────────────┘

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) │
├───────┼───────┼───────┼──────────────┤
│ truetruetruefalse        │
│ truetruefalsefalse        │
│ truefalsetruefalse        │
│ truefalsefalsefalse        │
│ falsetruetruetrue         │
│ falsetruefalsetrue         │
│ falsefalsetruetrue         │
│ falsefalsefalsefalse        │
└───────┴───────┴───────┴──────────────┘

julia> @truthtable ~p & (q | r) full=true
TruthTable
┌───────┬───────┬───────┬───────┬───────┬──────────────┐
│   p   │   q   │   r   │  ¬p   │ q ∨ r │ ¬p ∧ (q ∨ r) │
├───────┼───────┼───────┼───────┼───────┼──────────────┤
│ truetruetruefalsetruefalse        │
│ truetruefalsefalsetruefalse        │
│ truefalsetruefalsetruefalse        │
│ truefalsefalsefalsefalsefalse        │
│ falsetruetruetruetruetrue         │
│ falsetruefalsetruetruetrue         │
│ falsefalsetruetruetruetrue         │
│ falsefalsefalsetruefalsefalse        │
└───────┴───────┴───────┴───────┴───────┴──────────────┘

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

julia> @truthtable (p --> q)  (¬p ∨ q)
TruthTable
┌───┬───┬────────────────────┐
│ p │ q │ (p --> q)  ¬p ∨ q │
├───┼───┼────────────────────┤
│ 111                  │
│ 101                  │
│ 011                  │
│ 001                  │
└───┴───┴────────────────────┘

julia> @truthtable (p --> q)  (¬p ∨ q) full=true
TruthTable
┌───┬───┬─────────┬────┬────────┬────────────────────┐
│ p │ q │ p --> q │ ¬p │ ¬p ∨ q │ (p --> q)  ¬p ∨ q │
├───┼───┼─────────┼────┼────────┼────────────────────┤
│ 111011                  │
│ 100001                  │
│ 011111                  │
│ 001111                  │
└───┴───┴─────────┴────┴────────┴────────────────────┘