r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
93
u/crouchingarmadillo May 03 '26
I feel like a lot of these problems with undefined behavior and the like could be fixed by just removing integer promotion and implicit coercions.
Like yeah you introduce an ergonomic problem. But we’re working with a low level language so, we have ergonomic concerns already. Better to be safe and explicit.
4
u/Athas Futhark May 04 '26
Good post! I also made a similar one a few years ago to argue for signed sizes, although since my language has no implicit conversions, the arguments were not quite as strong.
23
u/protestor May 04 '26
What's unacceptable is a language in the 21th century deciding that uint -> int conversion (via int + uint -> int) should be implicit.
It can't be implicit. If the unsigned int doesn't fit in the int, you will throw away data.
I am okay if lossless conversions are implicit, like, u16 -> u32. But conversions that can lose data should never be implicit.
21
u/ExplodingStrawHat May 03 '26
Relevant reads also include:
3
u/RRumpleTeazzer May 03 '26
sizes should be signed for calculation -- agree.
but also modulus should handle signs better. modulus should be strickly periodic, and not change behaviour across zero.
e.g. -8 mod 10 should be +2, and not (idk what the standard implementation gives). The reasoning should be that (x + n) mod n = x mod n.
7
u/Unlikely-Bed-1133 blombly dev May 03 '26
NIce article!
I have a somewhat hot take that the actual issue is non-erroring underflows; using the representation of signed ints to describe an inherently unsigned number gives me the impression that something has gone wrong, even if I fully understand it (and prefer it in the languages not designed for it).
3
u/fridofrido May 03 '26
So I think there are two basic "solutions":
in both cases, no implicit coercions.
7
u/yorickpeterse Inko May 03 '26
Signed by default makes a lot of sense, and it's the same choice I made for Inko though it's even more strict there as unsigned integers aren't first-class types (as in, you can't define methods for them) and exist for the sole purpose of the C FFI.
One downside is that there are certain APIs that could be expressed more cleanly if you had a non-negative integer, as it would remove the need for runtime checks. One approach I've been thinking about to deal with that is adding support for integer ranges similar to Ada, but it's something I'm not entirely sure about just yet (most notably it will complicate the type system, which I'm trying to avoid).
2
u/tobega May 04 '26
I think this ultimately comes down to a confusion between semantic types and representation types.
Semantically, application programmers want to use "unsigned" to mean that the value should not be negative.
Representationally, compiler programmers want to use "unsigned" as a definition of how to read the bits in the byte.
2
u/flatfinger May 06 '26
A good general-purpose language should have separate categories of types for integer numbers versus algebraic rings with a power of two modulus. Unsigned number types should promote to a larger signed number type (the largest signed number type won't have an unsigned equivalent). Computations should be default be processed as though performed using the largest number type, but there should be convenient ways of inviting compilers to use smaller types. There should be ways of checking whether an integer overflow definitely occurred in a section of code, or definitely has not occurred in a manner that would produce observably numerically incorrect results; compilers should be free to trap or not, at their leisure, in cases where the results of an intermediate computation would have exceeded the guaranteed-representable range, but a compiler is able to produce a numerically correct final result anyhow, or where an overflow would have occurred in a computation whose upper bits (or the entire result) would end up being ignored.
Further, if an overflow would render the computations performed by a loop useless, but continuing the loop for awhile before exiting would be harmless beyond the wasted effort, a mid-loop check with loose semantics may allow a compiler to unroll a loop in ways that might result in an overflow being detected on the "wrong" iteration.
2
u/X4RC05 May 03 '26
Very convincing article tbh
1
u/newstorkcity May 04 '26
I have been considering recently having there be only one integer type,
intwhich can represent any integer (bigint), and all of the other integer types are just refinement types on that.u8is just bound to [0,255], i16, is [-32,768, 32,767], etc. which get optimized to efficient sizes based on these bounds. This gets you conversions for free (since they are all the same type), but you can longer do some things like specialized a function for different integer types, and wrapping arithmetic would need to be done explicitly (sinceint[0,255] + int[0,255]is anint[0,510], you would need to specify that you want mod 256). I'm curious if anyone has tried this experiment and what they thought about the ergonomics and expressivity of this approach.1
u/bshapiro001 May 06 '26
u must use a bit 2 signify signed so if unsigned takes up the whole word u will raise an error wen u convert unsigned 2 signed.
1
u/bjarneh May 04 '26
What bothered me most with that language, was the fact that
@privatefunctions are usable locally within a package, while@localfunctions are private to the file they belong :-)Shouldn't this be the other way around C3 people?
0
u/LegendaryMauricius May 07 '26
They found issues with previous defaults that lead to serious bugs. So they made new defaults, just to fix those specific cases.
Hahahahahahaahhahh
0
u/johnwcowan May 04 '26
Unsigned types were introduced to squeeze an extra bit of range out of 8-bit and 16-bit integers. Except for backward compatibility, they are wholly obsolete in our present 64-bit world and should never be used.