16 Memory
Page 1 of 10
A store's day ends with a count. Every site replays the sales and deliveries its tills recorded, rejects the movements that do not add up, and reports what is left on the shelves. That work changes a value step by step: the count of each SKU. This chapter shows what a value is in Una, and why two names never share one; how a function changes its caller's value, in plain sight at the call, and leaves it alone when it fails; how a value is given up for good; and what stops a run that grows too deep or too large.
What you'll build
An end-of-day stock count for a store with two sites. Each site's ledger takes its movements one by one, a movement that does not add up leaves the ledger as it was, and both sites are closed at the same time, as in chapter 15:
north: 2 applied, 1 rejected
LAMP-01 4 -> 3
rejected: 2 of DESK-02 sold, 1 counted
LAMP-01 3 -> 9
closing {"DESK-02": 1, "LAMP-01": 9}
south: 1 applied, 1 rejected
rejected: no product has the SKU CHAIR-09
LAMP-01 2 -> 0
closing {"LAMP-01": 0}You will learn to:
- tell a copy from a shared reference, and know that no other binding sees a change you make;
- pass a value in and out of a function with an
editparameter, and rely on it being left unchanged when the call fails; - state what such a function did, with
old(name)in itsensuresclause; - give a value up with
take, so that no code can use it afterwards; - recognize U6003, the stop at the call depth or memory limit, and avoid it with loops.
Download this chapter's packages: each step is a package under steps/, and the chapter's program is day-to-day/; run una check . and una run . in one.
Why it works this way
Most languages let two parts of a program hold the same object. A function that receives a list can change it under its caller, and a cache can change a record that a report is still reading. Reading a function then means asking who else holds what it holds, and a change made in one place can show up in another, far from the line that made it. Some languages add rules to keep this in check.
Una takes another route. Three rules do it:
- Values are values. There are no references. A
letbinding is a value, and copying it into another binding gives that binding its own value. - Change is local and visible. A binding declared
let editcan be rebound, and a parameter declarededitpasses a value in and out. The call saysedittoo, so a reader sees at the call which bindings it may change. A call that fails changes none of them. - A value given up stays given up. A parameter declared
takeuses its argument up, and the checker refuses every later use of the caller's binding.
The price is that two parts of a program never share a changing value: one of them returns a new value, or takes it with edit. In return, a value cannot change behind your back, which is also why the tasks of chapter 15 share nothing that changes. These rules fix what a program computes. How una lays out memory is its own choice and never changes the result; only how much memory a run may use is limited (section 16.4).
In Una, the only bindings a call can change are the ones it names with
edit, and only if the call returns. A failed call leaves them as they were.