r/ProgrammingLanguages • u/mocompute • Apr 09 '26
Keyword minimalism and code readability
In my language project I've emphasised simplicity of syntax, but I have some doubts about whether I've pushed it too far.
Like many folks I settled on the left-to-right pattern, name: Type. I started with ML semantics and syntax before migrating to a C-like syntax, but I kept the concept of type constructors, so ML's List a becomes List[a].
In my mind I have consistent rules:
- the colon : always introduces a type
- the square brackets always introduce a type argument
- the round braces () always introduce a callable or apply it.
So the simplest syntax for toplevel forms that are type declarations involves no keyword:
Vec2[T] : { x: T, y: T }
defines a generic type. In C++ this would require the struct or class keyword. A concrete type would just omit the [T].
ADTs introduce the | symbol to separate variants, as is common:
Option[T] : | Some { value: T } | None
I feel like this works, even though the two forms so far look quite similar.
Where it gets a bit messy is a few other things.
Plain enums (for C interop):
Color : { Red, Green, Blue }
This one is odd, because it would feel more natural to use the | separator since it's really just a sum type. But this makes the grammar ambiguous, so we're back to curly braces and commas. The lack of type annotations on the fields becomes the disambiguator.
C union (for interop):
Value : { | the_int: Int | the_float: Float }
Here, the idea is to take the plain struct syntax, and use | as the field separator rather than comma. Plus, to help the parser, there's an extra | at the start. Honestly I think this form is truly odd: it feels like it needs a terminating |} to match the opening {|.
Other toplevel forms are function declarations and definitions:
identity[T](x: T) -> T
and
identity[T](x) { x }
And some other toplevel things not worth mentioning here.
I attempted to write a formal EBNF grammar for this and the rest of the language some months ago, but gave up. Instead, the hand-coded backtracking parser handles everything fine.
I guess the question is whether or not it would make the language easier to read and write if there were top level keywords such as struct and union? But then it seems I'd need a defun for functions, and all the other toplevel things.
The benefit of left-margin keywords is that they make code groups easy to scan with the eye. All the structs together, then all the constants, then all the functions, etc. So I'm a bit on the fence at the moment. I like the minimalism, but I wonder if it places too much burden on the reader?
19
u/RecursiveServitor x15 Apr 10 '26
I've done the opposite and embraced keywords. All declarations start with a keyword that describes what's up. Why not? It doesn't add complexity if it describes a concept that exists anyway. On the contrary, I think. Disambiguating with keywords seems simpler than to do the same with syntax.
3
u/AustinVelonaut Admiran Apr 10 '26
In your
Colorexample, why wouldnot work? You say it is ambiguous -- is that because you somehow separate enums from ADTs?
2
u/alien_ideology Apr 10 '26
It’s confusing to me because your colon doesn’t always introduce a type like you said. Color doesn’t have the type { Red, Green, Blue }
2
u/shponglespore Apr 11 '26
I don't like how you use a colon to separate the fields of a struct from the type name but you don't use one to separate the fields of an enum variant from its name. Using the colon breaks the rule that the thing to the left of the colon is a thing that has a type rather than a thing that is a type.
Also, beware of the trap of making struct initializers use a colon between field names and their values. This is a pain point in Rust grammar. I can't remember the exact details, but it's something like the syntax making it hard to use a colon as a type ascription operator.
5
u/oscarryz Yz Apr 11 '26
Write a decent size program in your target syntax, you can judge yourself how readable it is.
Minimalism for the sake of minimalism is not good, unless of course you want to find out how minimal can you go without reinventing lisp.
I have a design that has everything and barely any keywords (from structs to functions and even concurrency everything uses the same mechanism
{}) It is not very useful and reading it is gets complicated with larger programs, but it is a fun exercise.1
u/Temporary_Pie2733 Apr 14 '26
Why are you using
|as a prefix, rather than an actual separator, i.eOption[T]: | Some {…} | Noneinstead ofOption[T]: Some {…} | None? Is it because you are overloading:for both type annotation and type definition? I’d consider not doing that.1
u/Jaded_Following2089 Apr 22 '26
I believe keywords are actually helpful to the programmers. Similarly to types, they help you document the code, making it more readable and easier to debug. A self-explanatory big keyword is better than a small difficult-to-understand expression.
Also, the keywords help remove ambiguity from the grammar, which is probably why you are having problems with the enum and with writing an EBNF
1
u/cmontella 🤖 mech-lang Apr 10 '26
Mech has no keywords so far, and I think it's just fine. Honestly, going into the future LLMs will be writing most code anyway, so having all these forms shouldn't be such a problem as long as they are internally consistent like you say.
I'll give you my latest example of keyword-free code. I'll leave off the explanation and see if you can understand actually what's going on just by the forms alone:
Here's another example of something else:
Here's one more:
My guess is you probably know what each of these does without knowing the language in particular and without having any keywords to guide you. Maybe the var names help.
One thing you can do to test your syntax is feed it to an LLM and ask it if it knows what the code means. If your syntax is context free then the LLM will likely be able to do it just from syntax cues alone.
2
u/WittyStick Apr 10 '26 edited Apr 10 '26
I dislike having a bunch of different syntactic forms for each kind of
Type.A way to have a more unified syntax without this is to just have first-class symbols, like Lisp/Scheme.
structcould just be an identifier which is looked up in the static environment, and resolves to a combiner (or "callable"). I prefer$struct,$lambdaetc, which is the convention Kernel uses for operatives - combiners which behave like special forms in Lisp/Scheme, but which do not get special treatment by the evaluator. More importantly, they can be defined by the programmer, via$vau- a combiner which constructs new combiners.If we didn't want to go the full way of making the combiners and symbols first-class, we could at least borrow the convention and use
$struct, which would not conflict with a regular identifier namedstruct. "Keywords" would be disjoint tokens from identifiers.Kernel (and Scheme) use a similar convention for literals, with
#as a prefix for certain keyword constants. eg, instead oftrueandfalsebeing reserved words, the tokens#tand#fare used. The language can be extended with new literals and keywords without conflicting with any user code by simply reserving all such literals, and forbidding#, or$, being used in identifiers.2
u/Caldraddigon Apr 11 '26
I'm a complete beginner, but i started to think of it this way:
While it's true that fewer keywords make code look less bloated and makes it quicker to learn in the beginning, you have to think of it this way too:
If the keyword is unique or an abstract idea, the longer it will take to learn the language and can make people find harder to understand the code, despite being 'less bloated' to look at.
So for me, I decided on these three rules:
A. They make it feel more complete and:
B. They make the language more understandable and readable,
then you have to ask yourself, it's really worth the reduced keyword bloat and the easier time learning the language at the beginning, if it's going feel like there's things missing and you need to do work arounds to get the same functionality from those keywords, and make the code be less understandable and readable.
That's the questions I ask myself anyway, on whether keywords should be added or not, and what keywords to decide upon and think up.
6
u/[deleted] Apr 10 '26
[removed] — view removed comment