"and", "or", bitwise or shortcircuit, what we get searching docs

When I think about it, it’s maybe technically NOT breaking (if you’re clever with parsing, EDIT: apparently that’s still a problem), I was just going by my proposal labeled like such, but as already stated you can e.g. do:

julia> or = 1  # yes, a horrible variable name, you would want to disallow this, but you COULD not do that.
1

julia> var1 or var2  # already a syntax error, so not breaking at least this
ERROR: syntax: extra token "or" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

If you allow the former, then having either (or both!) var1 or var2 be the “or” variable would be very confusing:

julia> if or or var2

https://github.com/JuliaLang/julia/issues/19933

2 Likes

I meant comparing to this:

>>> 1 == 1 and 2 == 2
True

I think in this case is common language and logic, not another programming language.

1 Like

While I agree that and and or are more readable, && and || are used in C, C++, Java and many other C-like languages. So it probably is missing from the docs because whoever wrote it thought this was common knowledge. I made a PR to improve it.

Funny, to me google returns the Control Flow page first and Mathematical Operations and Elementary Functions second.

2 Likes

Anyway, I am not saying that it is very important to have and and or as && and ||, although that would be cute.

What I am saying is that the docs and the help pages lead to confusion, and there is no mention anywhere that && and || is what we should most commonly use for conditionals where we logically would use and and or.

2 Likes

You are forgetting about whitespace sensitive contexts like in array expressions or macros. So for example [a or b] would parse as a single element vector instead of 3 hcatted ones like it does currently. That’s why I’m sceptical about adding further word operators, since they make it more difficult to tell what’s actually going on.

6 Likes

But note that Python’s and is && in Julia, so you already have that.

There is nothing “logical” in calling that operator and as opposed to &&. Various languages have different conventions, you cannot derive this from first principles. At least not spelling them out is consistent (cf 1 + 1 vs 1 plus 1).

We would, if we were using another programming language that uses these operators. Again, you appeal to “logic”, but these are (arbitrary) syntactic decisions.

In any case, this is unlikely to change now (unless, of course, we all go Zen).

2 Likes

Off-topic: Have you taught other languages (which?) [to beginners]? Did it go better or worse? Why did you choose Julia, is it about the language, or mostly about some package[s]?

I’m thinking Julia could be good for beginners, after you get over some small issues like this, shared by many other languages. I assume chemistry students are in many cases (not all) new to programming (if not, this likely wouldn’t be an issue for you).

1 Like

I insist, I am not suggesting that we should add those keywords, if that is complicated. I just think that would have been nice.

But I am not comparing programming languages here. I am just saying that if a new user searches the docs it might start using & where && is the correct thing to use. And that is because in common language “and” is “and”, and what we get from the docs is:

In the expression a && b, the subexpressionbis only evaluated ifaevaluates totrue .

or

x & y bitwise and

the english word and is in the second definition, not in the first. Related to this:

This in particular is not a big problem, if we warn the students soon enough. Other small things (which other languages share), are slightly more confusing, for example the difference between single name assignment and copying.

Yes, these students are VERY beginners, they commonly never had any contact with any other programming language. I have used Fortran and Python before. I want to teach them how to solve the dynamics of chemical kinetics and some particle simulations, and I do not use almost any package (except Plots). I usually teach them how to implement the loops explicitly for solving coupled differential equations, and only at the end of the course I show them how to feed packages with efficient implementations of the same concepts.

It has been a nice experience using Julia for that.

3 Likes

One issue has been package installation. I have to teach them in a student computer laboratory, and the students have limited storage space. Installing Julia and a few packages takes GBs of disk, and that is not possible. Installing Julia in a shared environment has been an issue.

1 Like

I am not sure why you think that:

  1. searching the docs with ?and will turn up nothing, as that is not a keyword or operator in Julia,

  2. the docstrings of & and | point out that these are bitwise operators in the first sentence, and even have examples to this effect.

Sure, that’s why the docs use the words “bitwise” and “short-circuiting boolean” before each “and” in the docstrings.

I am genuinely puzzled how confusion could arise.

1 Like

Well, to be truth, I still do not understand what bitwise is and where ever we would use that. And I have programmed for 20 years now, and developed some popular packages in my domain. I am teaching computer programming to people who don’t know in advance what an ASCII file is and start coding in Word. “Short-circuit boolean” also does not mean anything to most people, particularly for non-fluent-english speakers.

4 Likes

I would also add that searching for “and” in the docs doesn’t give any link to &, &&, control flow, or bitwise operators at all. But there were many threads criticising julia docs search already :slight_smile:

Personally I always have to resort to google search in julia docs. The content is great and very useful, the search - not so much.

7 Likes

That’s possible! Given enough disk space… :slight_smile: Anyway, you could look into Juliabox (at was a free solution, then paid, still is?), I see the website(s) are gone, but he software still exists, and could be installed at your institution. It’s at least a hosted environment, I guess the storage for packages was (to some degree only?) shared.

For a Julia cluster/HPC, I doubt each node (pre)compiles and stores its own packages. I assume the precompilation needs to only be done once, so maybe some shared disk for all students it possible (even allowing non-shared packages?), just to save space. [I believe HPC nodes do some redundant work when they load precompiled packages, as there compilation step is not fully finished, I however think that part is only in memory.]

https://github.com/JuliaLang/www.julialang.org/commit/11778b41e2f41359e83fbe52768281c940e1ea90#diff-5e113ce6bf37296640a6ae47c9bd4c94

2 Likes

It is documented in detail, with examples:

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

Generally, programming (like all fields or specialties) is full of terminology that is not self-explanatory, regardless of one’s native language. Expecting everything to be self-explanatory is a somewhat unusual approach to learning.

2 Likes

Yes, “Short-circuit” is a CompSci term (or actually taken from electrical engineering), just something you need to know happens, it’s simple. But it means kind of that e.g. “&&” is not “and” as in English in general (and even if people think it might be better, wouldn’t be to people in e.g. Brazil). There are other issues with “or” in English meaning “||” or xor (either, or), so best to get used to that, and I kind of support not having “and” and “or” for it.

Bitwise however means you do for each corresponding bit; and in the context of & and | if there’s only one bit (true or false; as in a condition in an if statement) you get the meaning of “and” and “or” what you would really want it to mean (w/o short-circuiting).

Bitwise & and | (on many bits), is useful, I guess mostly outside your domain (e.g. in crypto, and for graphics when it was black-and-white)? E.g. if you have many flags in one integer. It’s a rather low-level concept, was/is used to conserve memory for flags. I wouldn’t worry to much about it.

It’s a bit dangerous in C and C++ for control flow, but shouldn’t be in Julia, except for the precedense. It’s rarely used as very low-level optimization.

2 Likes

I can relate to that.
The and keyword is available in other languages and I very much like it (readability).

In the past I had to ‘try and error’ a few times when performing element-wise AND.
A few of the following lines were (and still are) not entirely intuitive to me.

1<2 & 0<1 #false as discussed above

1<2 && 0<1 #true
(1<2) + (0<1) #2
+((1<2),(0<1)) #2

&&(1<2,0<1) # -> error (which I would not have expected)
#I guess this does not work because && is not a function?

myand(x,y) = x && y
myand(1<2,0<1) #works as expected

#elementwise 
.+(([1,2] .< [2,3]),([0,0] .< [1,1])) # [2,2]
+(([1,2] .< [2,3]),([0,0] .< [1,1])) # [2,2]
myand.(([1,2] .< [2,3]),([0,0] .< [1,1])) # works as expected

([1,2] .< [2,3]) &&. ([0,0] .< [1,1]) #this fails, which may be unexpected to some users too
([1,2] .< [2,3]) .& ([0,0] .< [1,1]) #produces the desired result (as we are working with BitVectors)
1 Like

“Bitwise” means it operates on a per-bit basis. Similar to saying “component-wise” to say something is per-component.

(truncated the leading 0s to Int16 for readability)

julia> bitstring(1000)
"0000001111101000"

julia> bitstring(90)
"0000000001011010"

julia> bitstring(1000 & 90)
"0000000001001000"

julia> 1000 & 90
72

julia> bitstring(1000 | 90)
"0000001111111010"

julia> 1000 | 90
1018

For &, only where both 1000 and 90 have a 1, the result is 1. For |, wherever at least one of them is a 1, the result is 1.

To add another note here: &, |, xor, and bitshift (<</>>, <<</>>>) are the most fundamental computer operations. All other operations (additions, multiplication, etc.) are built in hardware (and software) using circuits (and code) that perform sequences of these operations. These days you can easily get away with never learning this (I personally didn’t for years) but I think it’s nice to know…


Short-circuit has to do with cutting something off in the middle, or otherwise terminating it before the “end”. Short-circuiting comes from electrical circuits, where if two points in the circuit are connect that shouldn’t be, this creates a short-er circuit than was intended (and often also ruins the device).

julia> false && g()
false

julia> g()
ERROR: UndefVarError: g not defined
Stacktrace:
 [1] top-level scope at REPL[65]:1

g() is not defined, but in the first expression, we never even got to g(), so we didn’t run in to the error. Encountering false in a short circuit operation cuts off the evaluation of the expression prematurely. This is why it can be also be used as an if-statement. For example

condition && return _something_

is common.

5 Likes

Yes, && is not a function, it is a control flow construct, normal functions can’t have short-circuiting behavior. That’s also why it doesn’t make sense in broadcasting, because broadcasting can only work with functions. Your myand is no different here and doesn’t short-circuit:

julia> myand(false, (println("Boo!"); true))
Boo!
false
3 Likes

The constructive improvements that seem plausible are adding help entries for ?and and ?or, which can just reference && and ||. Adding cross-references between the logical operators and control flow sections with some explanation of the differences. PRs for either improvements would be welcomed.

15 Likes

Thank you Stefan for the comment. That was what I meant. I would be wiling to help with that, but it is evident that my understanding of the implications of any phrase I might suggest will likely lead to rejection of the PR (clearly I quite often get misunderstood, probably because my limited capacity to express myself in English, like the use of the “normal” word above).

If it was from my point of view I would add a paragraph stating that && means what usually (?), or normally (?), or most frequently (?) one expects (?) to mean and in a conditional evaluation. People searching for bitwise comparisons certainly know what all that is about, it is for the other people that the docs might cause confusion.

@Henrique_Becker made a pull request naming that && is what and is in Python, but I am also hesitant in explaining things by comparison between languages.

2 Likes