plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
cafcef3
— Peter John
2026-11-06T18:38:47+05:30
Docs: simpler language design notes
docs/decisions/2026-11-simpler-language-notes.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Simpler language: design notes
|
|
2
|
+
|
|
3
|
+
Why the language is shaped this way, and which ideas were turned down. Written 2026-11-06, during the build on branch `simpler-language`. The plan is in [the review plan](../plans/2026-11-06-simpler-language.review.md).
|
|
4
|
+
|
|
5
|
+
## Every function is a tree of expressions
|
|
6
|
+
|
|
7
|
+
With no `return`, `while`, `break` or `continue`, a function's body is values nested inside values. Control flows downward and out the bottom.
|
|
8
|
+
|
|
9
|
+
With `return` and `while`, control can jump around:
|
|
10
|
+
|
|
11
|
+
```plum
|
|
12
|
+
fun firstBig(xs: Slice[Item]) -> Option[Item] =
|
|
13
|
+
i := 0
|
|
14
|
+
while i < xs.size
|
|
15
|
+
x := xs.get(i)
|
|
16
|
+
if x.big
|
|
17
|
+
return Some(x) # jumps straight out of the function
|
|
18
|
+
i = i + 1 # jumps back to the top of the loop
|
|
19
|
+
None
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Paths go backward and exit from the middle, and `i` changes on each pass. The real shape is a graph with cycles.
|
|
23
|
+
|
|
24
|
+
Without them, the same function is a tree:
|
|
25
|
+
|
|
26
|
+
```plum
|
|
27
|
+
fun firstBig(xs: List[Item]) -> Option[Item] =
|
|
28
|
+
match xs
|
|
29
|
+
Nil => None
|
|
30
|
+
Cons(head:, tail:) =>
|
|
31
|
+
if head.big
|
|
32
|
+
Some(head)
|
|
33
|
+
else
|
|
34
|
+
firstBig(tail)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
match xs
|
|
39
|
+
├── Nil → None
|
|
40
|
+
└── Cons → if head.big
|
|
41
|
+
├── yes → Some(head)
|
|
42
|
+
└── no → firstBig(tail)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Each run takes one path from the top to one leaf. Repetition is a call to a function, and a name keeps one value for its whole scope.
|
|
46
|
+
|
|
47
|
+
This is what makes the compiler simpler. An optimiser can walk each branch on its own, with no control-flow analysis. It's why `?` can be a `match`, and why fusion, which refuses early returns, can run everywhere.
|
|
48
|
+
|
|
49
|
+
**It isn't perfectly a tree.** Blocks still run in order, as in `gfx.clear(io, ..)` then `self.ship.draw(io)`, though a straight line is just as easy to walk. `mut` fields can still be assigned, so `Slice` and similar types need more care.
|
|
50
|
+
|
|
51
|
+
## `?` with no `return`
|
|
52
|
+
|
|
53
|
+
The compiler rewrites the rest of the block into a `match` arm:
|
|
54
|
+
|
|
55
|
+
```plum
|
|
56
|
+
x := parse(a)?
|
|
57
|
+
Ok(x + 1)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
means:
|
|
61
|
+
|
|
62
|
+
```plum
|
|
63
|
+
match parse(a)
|
|
64
|
+
Err(error) => Err(error)
|
|
65
|
+
Ok(x) => Ok(x + 1)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The user never writes a return, and the function stays one expression.
|
|
69
|
+
|
|
70
|
+
## `.=` and paths in spreads
|
|
71
|
+
|
|
72
|
+
`self.items .= push(x)` means `self.items = self.items.push(x)`. The path is evaluated once. It works only on `mut` fields, which are the only things `=` can still assign to.
|
|
73
|
+
|
|
74
|
+
`State(..s, ship.x: 1.0)` means `State(..s, ship: Ship(..s.ship, x: 1.0))`. It's the matching form for immutable state.
|
|
75
|
+
|
|
76
|
+
## Keep `self`
|
|
77
|
+
|
|
78
|
+
`self` was never about mutation. It's the name of the value a method was called on, and immutable code needs that name to build the next value: `Ship(..self, x: 1.0)`.
|
|
79
|
+
|
|
80
|
+
Three alternatives were considered:
|
|
81
|
+
|
|
82
|
+
| Alternative | Verdict |
|
|
83
|
+
|---|---|
|
|
84
|
+
| Drop `self` from the parameter list only: `fun update(dt: Float)` | Possible, a matter of taste. It saves one word per method, but static functions must move to module level, and `self` appears undeclared. No effect on speed |
|
|
85
|
+
| Implicit fields: `x + vx * dt` | No. You can't tell a field from a local or a constant, and the spread still needs `self` |
|
|
86
|
+
| No methods, as in Gleam: `fun update(s: Ship, dt: Float)` | No. It loses chaining and duck-typed games, and methods stay indented under their type by decision |
|
|
87
|
+
|
|
88
|
+
## No `mut self` marker, for now
|
|
89
|
+
|
|
90
|
+
`fun push(mut self, x: T)` would mark mutating methods, with the checker enforcing it. It would need to work on every parameter to be complete.
|
|
91
|
+
|
|
92
|
+
It was turned down because the gain is small:
|
|
93
|
+
- It doesn't make programs faster or add any capability.
|
|
94
|
+
- It gives only some of what Rust's `&mut` does. Rust's exclusive access takes a borrow checker, and without one, two names can still point at the same `Slice`.
|
|
95
|
+
- `mut` is mostly in `std`, so most game code would never use it.
|
|
96
|
+
- The bugs it could prevent mostly came from overrides and name matching, and removing inheritance and proving by type deal with most of those.
|
|
97
|
+
|
|
98
|
+
Add it only if the read-only analysis keeps causing bugs or blocking optimisations.
|
|
99
|
+
|
|
100
|
+
## If we ever remove the garbage collector
|
|
101
|
+
|
|
102
|
+
Nothing needs to change now. Plum relies on wasm-gc, and the imperative versus functional comparison will show whether the collector is a real problem. If it is, this is the order of preference.
|
|
103
|
+
|
|
104
|
+
**Reference counting would fit well.** It's how Koka works (its method is called Perceus), and FBIP comes from Koka.
|
|
105
|
+
- It removes the silent copy risk. The program checks at run time: if the count is 1, reuse the value, otherwise copy it.
|
|
106
|
+
- Immutable values can't form cycles. Only `mut` fields could, which is a reason to keep them rare.
|
|
107
|
+
- It needs no new syntax, because the compiler inserts the counting.
|
|
108
|
+
- The tree shape above makes it much simpler to decide where counts are dropped.
|
|
109
|
+
|
|
110
|
+
**Rust-style borrows wouldn't fit.** They need lifetimes and a borrow checker that users must think about. Koka's borrowing is different: the compiler works it out as an optimisation, and users never see it.
|
|
111
|
+
|
|
112
|
+
**The real cost is leaving wasm-gc,** not the language change:
|
|
113
|
+
- A new runtime: an allocator, plus memory layouts for structs, enums, closures, strings and arrays.
|
|
114
|
+
- A large part of `wasm-codegen` and the host interface rewritten.
|
|
115
|
+
- Bugs could become memory corruption instead of type errors.
|
|
116
|
+
- Larger `.wasm` files.
|
|
117
|
+
- It might be slower overall. Counting costs time on every shared value, so it could win only on avoiding pauses.
|
|
118
|
+
|
|
119
|
+
If the comparison shows pauses or allocation costs that hurt, the next step is a small prototype on its own branch.
|