I feel like this change wouldn’t affect Julia in general, but AND and OR operation between Boolean variables are usually denoted as addition and multiplication and result in a Boolean. In its current implementation, sum(Array{Bool,1})
returns an Int64
whereas prod(Array{Bool,1})
returns a Bool
, which is to say products are defined equivalent to AND but addition is not equivalent to OR.
I know modulo 2 addition is the XOR operation and thus this implementation is not useless, but I feel usually sum(Array{Bool,1})
is used to see if any one entry is correct, where I know sum(someArray)>0
does the job. Any opinions?
We have any
and all
, so what would be the point of having sum
do the same thing?
3 Likes
Welcome, Emre! Boolean or
could be the same as addition — but only if it’s done mod 2. We’ve simply found it helpful to widen small integers to avoid overflow as it’s an easy trap, and Julia’s integer addition is generally at least mod 32 or 64.
We also have count
, which does the same thing as sum
for collections of bools.
3 Likes
Yep, these functions are what I was suggesting (and apparently well optimized). Though I am not entirely sure why we should make Bool
s add up to Integer
s (well, except for the XOR remark).
You’re right, but prod
and all
are the same. It’s somewhat confusing to add Bool
s to start with since, you know, Boolean arithmetic doesn’t have addition. So I suggest to equate Boolean addition to OR operation because other operations doesn’t make a lot of sense (without casting).
Julia’s Bool
is a subtype of Integer
, so they do behave like them in some contexts. It’s not a perfect situation, but it works — and I’d argue it’s more helpful in practice than it is hurtful.
There are a number of places where we favor practicality over theoretical purity.
2 Likes
Makes sense, and rightfully so. Since any new behavior one can suggest already exists in the language (and semantic purity is not the goal) I guess my suggestion becomes meaningless. Thanks, Matt and Milan.