Array Languages #28: Bindings, Values and Immutability in X_eTaL
2651 words • 14 min read • Abstract

Episode 8 of my video series on array programming is about bindings: what a name points to, and what can change. Episode 7 went through X_eTaL’s syntax poster and ended with four bindings, shown but not explained: a lambda’s own name, amend giving a new array, a tuple taken apart, and a variable bound again. This episode explains them.
| Resource | Link |
|---|---|
| Video | Bindings, Values and Immutability in X_eTaL: Bound Once, Variables and Tuples: Array Languages #8![]() |
| All the videos | X_eTaL-video’s page |
| Try X_eTaL | In your browser |
| Code | X_eTaL, X_eTaL-video |
| Related posts | Reading and Writing X_eTaL, X_eTaL, an Array Language You Can Read, Small Comforts, X_eTaL’s Ergonomics |
| The series | Array Languages: every post |
| Comments | Discord |
A name and a value
A binding joins a name to a value. The arrow is typed :=. The name is just a way to refer to the value: use the name, and you get the value.
x ← 5
x × x ⍝ 25
Immutably Bound once (i.e. Constants)
A plain name is bound once. Bind it again, and the program is refused:
x ← 5
x ← 6
error[rebind]: x is already bound in this scope: a name is bound once;
write x! for a variable that changes
That happens when the program is verified, before any of it runs. So within its scope, a name means one thing. You can trust it: wherever you see it, it has the value it was given, and you never search the code above for a later change.
A new value from an old one
To change something, you make a new value from the old one. a̲mend puts 100 at position 2. It gives a new array, bound to a new name; the old array is still 1 2 3. It can take several positions at once, too:
v ← 1 2 3
w ← v a̲mend (2, 100)
w ⍝ 1 100 3
v ⍝ 1 2 3, unchanged
v a̲mend (1 3, 0) ⍝ 0 2 0
Scopes, and binding nothing
A function body is a new scope. Inside it, a name may be bound again, as its own. This function binds its own x and returns 20; outside, x is still 5:
x ← 5
ᵘt̲wice ← { y → x ← y × 2◆ x }
ᵘt̲wice 10 ⍝ 20
x ⍝ 5
Inside the function, the same rule holds. Bind a twice there, and the program is refused:
ᵘf̲ ← { x → a ← 1◆ a ← 2◆ a }
error[rebind]: a is already bound in this scope
Sometimes you want an effect, not a value. An underscore on the left binds nothing, and it may repeat as often as you like. These two lines print hi and again:
_ ← p̲rint! "hi"
_ ← p̲rint! "again"
Mutable Values (i.e. Typed Variables) end in !
When you do need a name to change, end it with an exclamation mark. Now it is a variable, and it may be bound again. The mark shows wherever the name appears, so a change is never hidden:
count! ← 0
count! ← count! + 1
count! ⍝ 1
A variable has a type, though you don’t write it: the type is inferred when the name is first bound, and it cannot change when the value changes. This one began as a whole number, so a decimal value is refused:
a! ← 42
a! ← 3.2
error[type-mismatch]: expected Int, found Float
= help: Int and Float do not mix: `f_loat n` makes a Float of an Int,
`f_loor x` or `c_eiling x` an Int of a Float
To hold decimal values, start it as one, with a decimal point: b! ← 42.0, and then b! ← 3.2 is fine.
Hidden state, locals and globals
Here is why variables are discouraged. This function multiplies by a rate, and the rate is a variable. Call it with 10, and you get 20. Change the rate, and the same call gives 30. The answer depends on more than the argument, and an impure function like this is harder to test:
rate! ← 2
ᵘc̲ost ← { ⍵ × rate! }
ᵘc̲ost 10 ⍝ 20
rate! ← 3
ᵘc̲ost 10 ⍝ 30
That function only reads the global, and reading needs no mark. Changing one is where X_eTaL is subtle, and the video only hints at it. The rule has two parts:
- Inside a function, binding a name always makes a local, a variable too, even when a global has the same name. Reading a global is fine; binding it is not how you change it.
- A global that a function changes is marked
ʰ, typedh:, everywhere it appears: where it is first bound at the top, and inside the function. The function’s own prefix doesn’t matter: a program’sᵘfunction and a file-privateʰone both changeʰn!, and neither can change a plainn!.ʰn!andn!are different names, so marking it only inside the function doesn’t work: the program is refused, sinceʰn!was never bound at the top (error[bad-binding]).
Here is the same counter both ways. On the left, the function’s n! ← n! + k reads the global, 0, and binds a new local n! that shadows it. The function returns the local, 5, and the global stays 0. On the right, every n! is ʰn!, so the function changes the global itself:
n! ← 0
ᵘb̲ump ← { k → n! ← n! + k◆ n! }
ᵘb̲ump 5 ⍝ 5, a local
n! ⍝ 0, unchanged
ʰn! ← 0
ᵘb̲ump ← { k → ʰn! ← ʰn! + k◆ ʰn! }
ᵘb̲ump 5 ⍝ 5
ʰn! ⍝ 5, changed
The left one runs without an error, so it is easy to miss. xetal lint catches it, as a correctness warning, and names the fix:
warning[global-assigned-in-function]: `n!` here is a new local made from the global `n!`,
which stays unchanged (a name bound in a function is its own, M6):
to change the global, write `h:n!`
The right one is correct, and lint still warns about it, for a different reason: as a matter of style, it notes every global that changes, since passing the state through a function is usually better (below). Where a global really is the point, say so in the file, and the warning goes away:
warning[global-mutable]: `h:n!` is a global that changes: keep the state in a function,
or `# lint: allow global-mutable` where a global is the point
⍝ lint: allow global-mutable
ʰn! ← 0
So the two warnings mean different things. global-assigned-in-function is a bug: the code doesn’t do what it seems to. global-mutable is a reminder that a changing global is a choice.
A library may hold a variable too. Here a small library, Counter, keeps count! and changes it in a̲dd. Your program can read it, through the library’s prefix; only the library can change it, through its own functions:
"k:" u̲se< "Counter"
ᵏa̲dd 3 ⍝ 3
ᵏa̲dd 4 ⍝ 7
ᵏcount! ⍝ 7
Assign it yourself, ᵏcount! ← 99, and the program is refused:
error[imported-name]: k:count! belongs to the library imported as k:;
read it here, and change it through that library's functions
Tuples and patterns
A tuple holds values of different kinds, side by side. Parentheses and commas build it, and each part keeps its own type and shape: here a list, a number and some text.
(1 2 3, 4.5, "abc") ⍝ (1 2 3, 4.5, abc)
A pattern takes a tuple apart, by position. Each part gets a name of its own, and an underscore skips a part. To change one part, build a new tuple: take the old one apart and put it back together with the new part. Here, 7 becomes 8, and the word comes along:
pt ← (7, "seven")
(num, word) ← pt
word ⍝ seven
(lo, _, hi) ← (1, 2, 3)
lo + hi ⍝ 4
(n, w) ← pt
(n + 1, w) ⍝ (8, seven)
The tuple itself is never changed.
Pass the state through
So instead of variables, pass the state along. Keep it in a tuple, here a balance and a count of years. One step adds five percent and counts a year. Applied three times, with a power, it gives the balance after three years:
ᵘy̲ear ← { (bal, n) → (bal × 1.05, n + 1) }
ᵘy̲ear³ (100.0, 0) ⍝ (115.7625, 3)
No name was changed. The step is a pure function: its answer depends only on its argument. You can test it with any state, in any order, and nothing else in the program can change what it does.
Compared with Rust and APL
Rust is similar. Its names are fixed unless marked mutable, and a variable keeps its type. But Rust lets you bind the same name again in the same scope; the new one hides, or shadows, the old:
let x = 5;
let mut n = 0; n += 1;
let x = x + 1; // allowed: shadows
X_eTaL refuses that, and asks for a new name:
x ← 5
x ← x + 1
error[rebind]: x is already bound in this scope
APL lets a name be assigned again, with a value of a different type: x←5, then x←'one'. X_eTaL only lets a name change when it ends in an exclamation mark. Even then, only its value can change; its type cannot.
In short, and next time
- A plain name is bound once.
- A new value comes from an old one:
a̲mend, or a new tuple. - A variable, marked as mutable, ends in
!, and keeps its type. - State is best passed as a tuple, through pure functions.
The next episode is about functions as values: passed, returned, and joined together. This one ends with a preview. A quote in front of a function passes it instead of applying it: plus passed to reduce, tally passed to a function of our own, and our own function passed to each:
'+ r̲/ 1 2 3 4 ⍝ 10
ᵘb̲oth ← { (f̲, a, b) → (f̲ a, f̲ b) }
ᵘb̲oth ('t̲ally, "hello", "hi") ⍝ (5, 2)
ᵘm̲yFn ← { ⍵ × ⍵ }
'ᵘm̲yFn e̲ach 1 2 3 ⍝ 1 4 9
Before then, try the episode’s exercise in X_eTaL in your browser: write a step function of your own, keep its state in a tuple, and apply it with a power.
Part 28 of the Array Languages series. View all parts
Comments or questions? SW Lab Discord or YouTube @SoftwareWrighter.
