Package or method to determine truth table or is a statement is a Tautology?

Hi all,

I was wondering is there anything close to or similar function/method that can be used to determine if a combination of 2 values are tautology (a statement that is always true).

It is a basic logic statements in mathematics if p then q (p β†’ q), p or q ( p v q), p and q (p ^ q), p happens if and only if q happens ( p <-> q).

With possible outcomes for p and q are only 2(True or False / 1 or 0).

Otherwise if no package available on this subject, I think it will be a code writing with a lot of if then statements.

I suppose you mean a combination of two propositional variables (not values). In the case of only two variables, you can just check the whole truth table, i.e., check all four possible valuations of your formula:

function is_tautology(formula)
    for (p, q) in Iterators.product([true, false], [true, false])
        formula(p, q) || return false
    end
    return true
end

my_formula(p::Bool, q::Bool) = !p || (p && q)
is_tautology(my_formula)

is_tautology((p, q) -> !p || p)

is_tautology() do p, q
    return !(p && ! p)
end

Do you think it is possible to create table like below:
Capture d’écran_2022-07-19_19-26-00

With dataframe package or other suggestion?

I read this
https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation

How to create a function like yours if I want to have 4 propositional variables?
2^4 rows.
Is there a general formula / function for n propositional variables?

something like that, would it be okay?

all((p,q,r,s)-> !p || (p && q) && (r || (s && !r)), Iterators.product(repeat([[true, false]],4)...))

If you are looking for formatted output, to get something that comes close, you could try DatFrames and PrettyTables

using DataFrames, PrettyTables
df=DataFrame(p=repeat([true, false],outer=2), q=repeat([true, false],inner=2))

df[!,"p β©“ q"].=df.p.&&df.q
df[!,"p β©” q"].=df.p.||df.q
df[!,"p ⊻ q"] .=df.p .⊻ df.q

pretty_table(df, nosubheader=true)

julia> pretty_table(df, nosubheader=true)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚     p β”‚     q β”‚ p β©“ q β”‚ p β©” q β”‚ p ⊻ q β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚  true β”‚  true β”‚  true β”‚  true β”‚ false β”‚
β”‚ false β”‚  true β”‚ false β”‚  true β”‚  true β”‚
β”‚  true β”‚ false β”‚ false β”‚  true β”‚  true β”‚
β”‚ false β”‚ false β”‚ false β”‚ false β”‚ false β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜
julia> df=DataFrame(p=repeat([true, false],outer=4), 
                    q=repeat([true, false],inner=2, outer=2),
                    r=repeat([true, false],inner=4))

df[!,"p β©” q β©” r β©” !(r β©“ q)"].= (df.p.||df.q.||df.r) .|| .!(df.r .&& df.q)

pretty_table(df, nosubheader=true)

julia> pretty_table(df, nosubheader=true)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     p β”‚     q β”‚     r β”‚ p β©” q β©” r β©” !(r β©“ q) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  true β”‚  true β”‚  true β”‚                 true β”‚
β”‚ false β”‚  true β”‚  true β”‚                 true β”‚
β”‚  true β”‚ false β”‚  true β”‚                 true β”‚
β”‚ false β”‚ false β”‚  true β”‚                 true β”‚
β”‚  true β”‚  true β”‚ false β”‚                 true β”‚
β”‚ false β”‚  true β”‚ false β”‚                 true β”‚
β”‚  true β”‚ false β”‚ false β”‚                 true β”‚
β”‚ false β”‚ false β”‚ false β”‚                 true β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
1 Like

Here are my problems:

  1. I am trying to create the negation, but do not have an idea what to write, tried to put ! and ~ not working. Then read your text above and needs to add . before ! and put () as well. Thanks…
  2. How about implication ( β‡’) and biimplication
using DataFrames, PrettyTables
df=DataFrame(p=repeat([true, false],inner=4), q=repeat([true, false],inner=2,outer=2), r=repeat([true, false],outer=4))


df[!,"~p"].=.!(df.p)
df[!,"~q"].=.!(df.q)
df[!,"~r"].=.!(df.r)
df[!,"p β©“ q"].=df.p.&&df.q
df[!,"p β©” q"].=df.p.||df.q
df[!,"p ⊻ q"] .=df.p .⊻ df.q
df[!,"p ⊼ q"] .=df.p .⊼ df.q
df[!,"p ⊽ q"] .=df.p .⊽ df.q

pretty_table(df, nosubheader=true)
  1. My table columns line are not pretty like the package name

  2. There is an idea to do something like backward proofing, can we find all possibles tautologies given we want the results of the boolean all=true ? Let Julia thinks.

You could also try to use GitHub - eliascarv/TruthTables.jl: Create truth tables in Julia! directly and try to query the last column

1 Like