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

**URL:** https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415
**Category:** General Usage
**Tags:** question
**Created:** [July 18, 2022, 4:51pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415 "2022-07-18T16:51:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [July 18, 2022, 4:51pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/1 "2022-07-18T16:51:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [July 19, 2022, 8:07am UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/2 "2022-07-19T08:07:45Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [July 19, 2022, 12:41pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/3 "2022-07-19T12:41:49Z")

</div>

Do you think it is possible to create table like below:  
 ![Capture d’écran_2022-07-19_19-26-00](https://global.discourse-cdn.com/julialang/original/3X/f/3/f334499a4ae311c0265a44486511025ffc8f4f67.png)

With dataframe package or other suggestion?

I read this  
[https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation](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?

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 19, 2022, 2:07pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/4 "2022-07-19T14:07:58Z")

</div>

something like that, would it be okay?

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

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 19, 2022, 3:47pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/5 "2022-07-19T15:47:21Z")

</div>

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

```julia
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
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 │
└───────┴───────┴───────┴──────────────────────┘

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [July 19, 2022, 4:32pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/6 "2022-07-19T16:32:27Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

### Author: ![guilhermebodin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guilhermebodin/32/6982_2.png) [@guilhermebodin](https://discourse.julialang.org/u/guilhermebodin)
#### Post date: [July 19, 2022, 4:39pm UTC](https://discourse.julialang.org/t/package-or-method-to-determine-truth-table-or-is-a-statement-is-a-tautology/84415/7 "2022-07-19T16:39:20Z")

</div>

You could also try to use [GitHub - eliascarv/TruthTables.jl: Create truth tables in Julia!](https://github.com/eliascarv/TruthTables.jl) directly and try to query the last column
