Palli
April 11, 2021, 1:47am
1
Compared to e.g. C and C++20 - Wikipedia , how much undefined behavior does Julia have?
signed integers are now defined to be represented using two’s complement (signed integer overflow remains undefined behavior )
Julia is also defined to use two’s complement (since always), as it’s pretty much used in all non-ancient platforms, at least on all Julia’s currently supported platforms. I suspect that in C++ overflows being UB (except for unsigned) is because some platforms do trap, or could in theory. I don’t think any of Julia’s current platforms do, so it seems Julia could define it as NOT undefined behavior.
I see only “undefined” mentioned here in one comment (and I’m not sure how know or official it is as Julia doesn’t have a standard):
opened 06:31PM - 26 Nov 20 UTC
doc
I think that the semantics of `const` is not specified in the documentation clea… rly enough. In both [Scope of Variables » Constants](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Constants) and [Base » Essentials » Keywords » const](https://docs.julialang.org/en/v1/base/base/#const) the word "undefined" does not appear a single time.
What is the behavior of the following code?
```julia
const c = 0
const c = 1
print(c)
```
Is it really [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior)?
I see two major answers.
#### 1. It is UB
Then the program above is allowed to do whatever it wants, including printing `0`, printing `1`, printing `2`, printing `"hello"`, not printing anything, crashing, hanging indefinitely, ...
According to the [docs](https://docs.julialang.org/en/v1/base/base/#const):
> In some cases changing the value of a const variable gives a warning instead of an error. However, this can produce unpredictable behavior or corrupt the state of your program, and so should be avoided. This feature is intended only for convenience during interactive use.
However, if it is UB, then redefining a `const` is of no use whatsoever because there is no guarantee that it will do anything meaningful at all. Even during an interactive session, what follows has completely no sense because the whole behavior is undefined.
If this is the case, I think this point should be made more clear in the docs.
#### 2. It is not UB, just "unspecified value"
Another possible interpretation is that when doing `const c = some_new_value`, from this moment on every reference to the name `c` can resolve to any value that the "constant" `c` has ever had, including `some_new_value`. This mean that the "value" of `c` may be unpredictable, maybe even [unspecified](https://en.wikipedia.org/wiki/Unspecified_behavior), but the behavior of the program as a whole is not undefined in the strict sense.
Within this interpretation the following code is allowed to compute the surprising result `(1, 0)`, but is not allowed to crash, hang, return `42`, etc...
```julia
const c = 0
f() = c
f()
c = 1
g() = (c, f())
g() # currently outputs (1, 0)
```
### Question
My personal interpretation from reading the docs is that the intent leans more towards option 2 (unspecified value), but I'm asking here to be sure (and possibly improve the documentation).
What is the correct interpretation of the meaning of `const`?
What’s the reason for it in general? Is it because LLVM has UB? Or because we mostly use C++ rules? Should those be assumed if otherwise not known?
FYI: In C++ you CAN avoid UB for this above example, so in case we need to enforce in Julia (while I’m not sure if this is possible in C, or even if either applies, we use C more but really LLVM is the only thing here that matters?):
https://www.reddit.com/r/cpp_questions/comments/kgyabw/are_we_sure_that_signed_integer_overflow_leads_to/
A particular implementation may define signed integer overflow to wrap as an extension to the standard. Support for this would be indicated via the std::numeric_limits<T>::is_modulo
template. Otherwise, it is undefined behavior.