# On the arbitrariness of truth(iness)

**URL:** <https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381>\
**Category:** Internals & Design\
**Created:** [May 20, 2022, 3:32pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381 "2022-05-20T15:32:55Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 21, 2022, 5:51am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/25 "2022-05-21T05:51:58Z")

</div>

Think the first example was the one referenced, i.e.

```julia
julia> all([])
true

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 21, 2022, 5:53am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/26 "2022-05-21T05:53:32Z")

</div>

If they were modular integers like `Int64`, they would wrap with addition, no?

```julia
julia> typemax(Int) + 1
-9223372036854775808

julia> typemax(Bool) + true
2

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 21, 2022, 5:57am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/27 "2022-05-21T05:57:29Z")

</div>

that’s just [Vacuous truth - Wikipedia](https://en.wikipedia.org/wiki/Vacuous_truth) which is very common. The problem with Python is `[]` can be interpreted as Boolean, which Julia is very much not able to

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 21, 2022, 6:01am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/28 "2022-05-21T06:01:43Z")

</div>

Yeah, but I was ju saying the quote on “julia does the same thing” was mentioned only for the first example, and not the one you brought up.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 21, 2022, 6:07am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/29 "2022-05-21T06:07:59Z")

</div>

that’s exactly what I don’t understand because the point of @ bkamins is the whole thing, not just `all([])` being true or not. I don’t see how “Julia is the same” when it literally is NOT

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 21, 2022, 9:48am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/30 "2022-05-21T09:48:37Z")

</div>

I also don’t think the `all([])` example is at all confusing/surprising. That has always been exactly what I wanted it to do; I have always wanted `all` to mean that there are no exceptions (so `all([]) == true`), and `any` to mean that there is an exception (so `any([]) == false`). Any other behavior I’d find both surprising and annoying, as I’d then be forced to add length checks on all my uses.

The other Python cases I find more surprising.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 21, 2022, 10:22am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/31 "2022-05-21T10:22:32Z")

</div>

> [@StefanKarpinski](#):
>
> But when you convert an integer type to a smaller integer type, you typically truncate and keep the trailing bigts. By that logic shouldn’t the last bit of an integer dictate its value when converted to boolean? I.e we would consider all odd numbers true and all even numbers false? That makes at least as much sense to me as zero versus non-zero.

I know (and sadly had to use) a language which does that: [IDL](http://www.idlcoyote.com/code_tips/bitwiselogical.html).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 21, 2022, 10:32am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/32 "2022-05-21T10:32:26Z")

</div>

Does IDL also let you use integers in conditionals?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 21, 2022, 10:38am UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/33 "2022-05-21T10:38:14Z")

</div>

Yes, see the article linked. And why limiting to integers? They support floating point numbers too, both real and complex, but the rule for floating point numbers is different: zero is false, all the rest is true (complex numbers ignore the imaginary part, so there are plenty of falsy zeros). You get the best of both worlds! 😀 And appreciate this example from the article

```julia
IDL> print, 2 or 0.
         2.00000

```

The literal `2` is somehow automatically converted to floating point, so it becomes true. You can’t even guess what the code is doing by reading it. IDL is a such a nonsense language that it all makes sense.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 21, 2022, 12:10pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/34 "2022-05-21T12:10:42Z")

</div>

> [@Elrod](#):
>
> I also don’t think the `all([])` example is at all confusing/surprising.

Yes, that is why it is the first example in the list. This example is the one that most people get right even without having to run the code. For the next two examples - most people get them wrong in Python if they are asked to guess the results and often even struggle to explain the reason for output produced once they see it.

The point is: such expressions should be easy to reason about without having to run the code.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 21, 2022, 3:28pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/35 "2022-05-21T15:28:07Z")

</div>

They look a lot more attractive if you use the dignified term “generalized booleans”.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 21, 2022, 3:31pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/36 "2022-05-21T15:31:24Z")

</div>

This is great. I love examples of language designs that maybe made some sense in a microcosm but then become totally bizarre when you step back and look at the whole.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [May 21, 2022, 4:06pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/37 "2022-05-21T16:06:24Z")

</div>

Sad that you need the emoticon to make sure we all get that it’s sarcasm 😉

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 21, 2022, 5:00pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/38 "2022-05-21T17:00:56Z")

</div>

Hm, interesting I must have misremembered that. That’s unfortunate. In that case, I am a bit more sympathetic to your POV here, though I’d lean instead towards making `true + true == false`.

As far as I understand, `+` should be `xor` and `*` should be `&`

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 21, 2022, 5:18pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/39 "2022-05-21T17:18:14Z")

</div>

☹

```julia
julia> true * false
false

```

In Base:

```julia
+(x::Bool, y::Bool) = Int(x) + Int(y)
-(x::Bool, y::Bool) = Int(x) - Int(y)
*(x::Bool, y::Bool) = x & y

```

It’s not obvious why. But, I’m sure it wasn’t an arbitrary choice.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 21, 2022, 5:21pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/40 "2022-05-21T17:21:24Z")

</div>

It’s stated as

```julia
## do arithmetic as Int ##

+(x::Bool) = Int(x)
-(x::Bool) = -Int(x)

+(x::Bool, y::Bool) = Int(x) + Int(y)
-(x::Bool, y::Bool) = Int(x) - Int(y)
*(x::Bool, y::Bool) = x & y
^(x::Bool, y::Bool) = x | !y
^(x::Integer, y::Bool) = ifelse(y, x, one(x))

```

in bool.jl

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 21, 2022, 5:22pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/41 "2022-05-21T17:22:45Z")

</div>

Just like `true & false == false`, right? `true == 1` and `false == 0` so this is all consistent.

Same with `true + true == 2` since `2 mod 2 == 0 == false`. I just would have wanted addition on Bools to wrap around instead of widening.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 21, 2022, 5:28pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/42 "2022-05-21T17:28:10Z")

</div>

> Same with `true + true == 2` since `2 mod 2 == 0 == false`.

But, exactly because it widens this doesn’t make sense. Sure `2 mod 2 == 0 == false`, but the return type is `Int` which doesn’t follow mod 2 artithmetic. I mean, is there a more “practical” reason for widening ?

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [May 21, 2022, 6:15pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/43 "2022-05-21T18:15:19Z")

</div>

I think to allow something like this

```julia
sum(rand(Bool, 1000)) # to count true's

```

Although a little unfortunate, because there is `count`. Maybe there are more cases of use.

(Edit: easier example.)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 22, 2022, 9:18pm UTC](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381/44 "2022-05-22T21:18:08Z")

</div>

`Bool`s are often used as the “lowest type” form of 1/0 in order to prevent accidental type promotion, could this be a reason to define arithmetic the way it’s done? For example,

```julia
julia> I(2)
2×2 Diagonal{Bool, Vector{Bool}}:
 1 ⋅
 ⋅ 1

```

produces a Bool matrix to make sure that, e.g., `I(2)*B` does not promote the type to a wider type than the type of `B`.

[Previous page](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381.md?page=1)

[Next page](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381.md?page=3)
