r/ProgrammingLanguages • u/Purp1eGh0st • Apr 04 '26
Post-Penultimate Conditional Syntax
https://joel.place/blog/conditionals/In fleshing out conditional control flow syntax for my language, I wanted something expressive (read: pattern-matching), but didn't like how that led so many languages to have a divergence between if-style and match-style conditionals.
After taking some inspiration from Ultimate Conditional Syntax and playing around for a bit, I've landed on a form of exhaustive binding if statements that feels to me very much like it falls out naturally from existing work, and so should not be novel, but I can't easily find elsewhere.
Does anyone know of existing languages that use similar syntax and I can look to for inspiration/battle-testing, or see obvious holes in this construction that would have prevented others from using it? Thanks in advance!
6
u/nerdycatgamer Apr 05 '26
Personally, I don't really understand how "and" and "or" keywords are different from short-circuiting, logical and/or ("&&"/"||") given that "is" is a binary operator which: 1) binds variables in the pattern, and 2) evaluates to true iff the pattern matches the expression.
If you haven't, it may he helpful to look at "The Algebra of Patterns"
5
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Apr 05 '26
Holy frick, you can't post a comment on that blog without turning over your entire Github account rights to the comments app -- you literally have to grant it the rights to "Act on your behalf" on Github. That is seriously f***ed up.
What I was going to post was a link to a related blog from Simon ("Core Lang"): https://soc.me/languages/unified-condition-expressions
5
u/phlummox Apr 05 '26
"Can have runtime errors" seems like an odd definition of "unsound". Pretty much every language is unsound, by that criterion.
3
u/matthieum Apr 06 '26
Perhaps unification is not worth it?
I would like to start by saying that I am a fan of the
isexpression. But that's a different topic, really.When it comes to syntax, I do like a moderate amount of sugar. For example, I appreciate that Rust has 3 loop constructs:
loop,while, andfor.foris just syntactic sugar overwhile, which itself is just syntactic sugar overloop, but as a writer it's incredibly convenient to have higher-level syntax to communicate intent more concisely to other (human) readers.Similarly, I appreciate
ifvsmatch(and let-else-guards) because they are syntactic markers which immediately communicate to the readers what's going on here:2
u/GidraFive Apr 05 '26 edited Apr 05 '26
I experimented a bit with it as well and came up with almost the same idea as in here, but explored a bit further in my language. I'd say this idea feels even more natural and pleasing to me. It has a similar syntax to ucs, but more flexible i think.
I think we can get even further by making patterns first class and allowing matching function applications (whatever that would mean), but it needs some experimenting to figure out nice semantics.
I feel like i need to write down a proper post about that, seeing how much these ideas pop up.
2
u/NullPointer-Except Apr 05 '26
Hi, you might want to look at haskell for pattern matching.
Looking at the paper, haskell case-of already handles almost everything:
Is equivalent to:
Is equivalent to:
Is equivalent to:
I personally would dismiss this point: if you are beginning to write deeply nested if-statements, your intention aligns more with control-flow rather than being a simple destructor (which is what most people want in pattern matching). So might as well be honest and embrace a better control-flow abstraction such as continuations.
Does not have a direct equivalent in haskell (which is true due to Or-pattern binding). However, if we allow for lenses:
Yields the same result.
This example also arise another good point: how often do we really need pattern matching and if-statements on haskell? And the answer is... Surprisingly less than you think. The above code can also be (more realistically) written like:
Which is shorter than what the paper exposes, and also just as clear.
AFAIK, this style is not exclusive to haskell, certain ML implementation such as OCAML make monadic code like the above possible via custom/rebinding operators (allowing for
>>=) and via customlet*binding.So, after having said all of this, can we truly state that "The Ultimate Conditional Syntax" is an improvement over the current state of things?
2
u/TheUnlocked Apr 05 '26
C#'s pattern matching looks basically the same as this: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching
2
u/the_sunsetter_TM Apr 05 '26
Great post! I'm hereby offering a bounty to anyone who wants to scoop you. All they need is a marginally better conditional syntax, then they can name it
* post-post-penultimate conditional syntax
* post-antepenultimate conditional syntax
or something like that :P
1
u/Positive_Total_4414 Apr 05 '26
Well, not exactly like this, but Gleam is known for having only pattern matching for resolving any questions.
1
u/Labbekak Apr 05 '26
Have a look at the "with" construct in Agda and Idris as well for another advanced pattern matching feature.