r/ProgrammingLanguages • u/onlyrealcuzzo • Jul 19 '26
Thoughts on syntax for Zig-style Tenses + Swift like "sane assignment"?
I don't think Zig formally calls its type system a tense based system. But it has `!T` and `?T` and `!?T` which delineate between fallible / optional. I call this a `tense`-based system.
One of my biggest problems with type inference in Systems languages is that it's non-obvious when you have a fallible, optional, or temporal (a stream, future, promise, etc).
Swift solves this explicitly like:
let data = try await fetch()?.process()
async let user = fetchUser()
The compiler forces you to make it explicit what you have. I want to combine this with Zig's tense system where `~` is a third sigil to denote the temporal tense (future, stream promise):
data = fetch(); // Compiler Error: use data:~ = fetch() or data = AWAIT fetch();
foo = fallibleBar(); // Compiler Error: use foo:! = fallibalBar() or foo = TRY fallibleBar();
baz = maybeBlah(); // Compiler Error: use foo:? = maybeBlah() or foo = UNWRAP maybeBlah();
Alternatively I could use keywords before assignment like Swift, but I think it works well with Zig's `tense` system and with safe navigation:
FN falliableFoo() -> !T ...;
FN maybeFoo() -> ?T ...;
x = fallibleFoo()!.name;
y = maybeFoo()?.bar;
I do not allow blocking navigation:
FN futureFoo() -> ~T ...;
z = futureFoo()~.name;
A major reason is because if you do `!.` it changes the return type to `!T`. Where `~.` could change your return from a `~T` to a `T` which would imply you've shifted the blocking, but it doesn't guarantee that. I want blocking to be more clearly called out.
11
u/evincarofautumn Jul 19 '26
I don’t quite see the problem you’re trying to illustrate with “blocking navigation”. I would expect that if
futureFoo()has type~Fooand thenameproperty ofFoois, say,Text, thenfutureFoo()~.namein effect has type~Text. That is, we’ve introduced a data dependency in the same way asawait(or Rust’s.await) but we haven’t left the async context.More concretely, in Haskell, given
data Foo = Foo { name :: Text }, if I launch a task withfutureFoo <- async do { threadDelay 1_000_000; pure Foo { name = "bar" } }, I get an actionfutureFoo :: Async Foo. I can compose that with other functions and actions without actually awaiting its result, e.g.fmap (.name) futureFoo :: Async Textdoesn’t run anything itself, and can be treated as just another task that depends on the first. Only when I callwaitdo I actually block.Your “tenses” are also called modalities or modal types, and typically they’re analysed as monads or comonads. You can spell them as sigils or keywords or ordinary types, that doesn’t matter so much, though you’ll run out of ASCII symbols long before you run out of words.
2
u/MadCervantes Jul 19 '26
I like the idea but the use of sigils here seems like bad ux.
5
u/sebamestre ICPC World Finalist Jul 19 '26
FYI triple backtick code blocks don't work in the default wysiwyg editor, you need to enable markdown mode
2
u/mamcx Jul 19 '26
Oh yeah, also I add "blocking" and "lock".
I think "optional" should be normal type is not that of a problem for system langs, that need to know the contracts of resources instead of "normal" type-heavy system that only care for the abstractions.
Single out optional, you can free one sigil for the others.
BTW:
"Alternatively I could use keywords before assignment"
Check https://without.boats/blog/the-registers-of-rust/ that argue that for this cases, all sugar SHOULD be possible to be write and name in code.
2
u/Clementsparrow Jul 21 '26
Oh yes, it's hard and I would even say: impossible in the general case. There are theorems that cannot be proven, properties that are undecidable, etc. But I'd like to see attempts to bring practical benefits in this domain even if it doesn't solve all the issues, like Rust did for memory safety. In a way, dependent types may be a step in that direction. But improving the type system is not enough, you also need to make it easy to use for the programmer, and have a good support for it in the standard library.
The example you gave is a good illustration: maybe a division by zero can never happen and the compiler can not prove it. But maybe the programmer can, and if it is easy enough and important enough, then it is worth doing it. Maybe that proof is actually a case of a more general theorem that can be added to a library for others to benefit from it? Maybe also the compiler cannot prove the theorem but it can tell you what it needs to know. Like, you divide by the length of a vector so that length must not be zero, so the vector cannot be null, but the vector is actually the cross product of two other vectors so what you actually need to prove (or check at runtime) is that these two vectors are not colinear. And now the programmer needs to decide how and where he will ensure that in a way that the type system can use. That's not easy to do neither, but I'd like to see at least attempts at doing some of it. Actually, compilers are already capable of proving many properties that they use for optimization. It would be nice if some of these features were made available to the programmers.
2
u/Clementsparrow Jul 20 '26
I don't understand the fascination that PL designers have today for things like optional types, result types, error types, etc. Very often it's mostly helping using functions that have multiple exit conditions, by integrating these exit conditions into a single return value.
IMO it is bad coding practice and should be discouraged instead of being facilitated, because it means that a condition has to be tested first in the called function, then again at the call site. It has performance issues (speed and size of the code) and maintenance issues (dependencies between the function, the calls to it, and the definition of a specific return type for that function). And yes, some of these issues can be managed by the compiler, but not always (e.g. the overhead of functions in libraries cannot be removed).
So, for me, it's not a direction one should research when designing a new programming language. The real questions are "how can we avoid as much as possible having multiple exit conditions in functions?" and "when we can't avoid it, what is a nice and convenient syntax to deal with calls to functions that have multiple exit conditions?".