13 Limits on authority
Page 1 of 9
Chapter 12 showed how a header says what a function may do: print, look a customer up, send a mail. Three questions remain open in a working program. May the function do it on every call, or only in some, such as never in a dry run? Who notices when an edit widens a header? And how much may code that the store did not write, copied into src/, ever receive? This chapter answers each one with a rule that una check enforces: a condition on an operation, a lock on a header in una.toml, and a ceiling for a module. You end with a price sale that rehearses before it changes any price.
What you'll build
A price sale for a store. It first runs as a dry run, which prints what it would change and changes nothing, then runs for real: it sets each new price and prints a shelf tag through a tag printer's module that the store copied in. The program prints:
Dry run: 20 percent off
LAMP-01 would go from 1990 to 1592
DESK-02 would go from 24900 to 19920
RUG-07 would go from 4500 to 3600
The dry run plans 6278 cents off
Sale: 20 percent off
set LAMP-01 to 1592
tag: [Desk lamp | 1592 cents]
set DESK-02 to 19920
tag: [Oak desk | 19920 cents]
set RUG-07 to 3600
tag: [Wool rug | 3600 cents]
The sale took 6278 cents offYou will learn to:
- write a condition on an operation,
set_price where not dry_run, and read the proof the verifier makes of it; - choose edition
0.3, a preview, inuna.toml; - give a module from outside the store a ceiling, so that every call into it shows what it receives, and grant it less than you hold,
with @x = @y[ops]; - lock the
usesof a public function inuna.toml, so that an edit of the code alone cannot widen it; - test a dry run's price with a stand-in price list.
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
A header that lists PriceList[set_price] says that the function may set prices. It does not say when. In most programs, a rule such as "a dry run changes nothing" lives in an if somewhere in the body, or in a function three calls away. Nothing checks that every path respects it, and a rehearsal that forgets one branch changes real prices.
A second risk is the edit itself. The verifier proves that a body keeps its header, but an edit can change both together: the header loses a line, the body loses its guard, and the package still checks. In a change that touches many files, the missing line is easy to overlook.
A third risk is code the store did not write. A label printer's module, copied into the package, declares its own uses. A new version of it can ask for more, the clock today and the files tomorrow, and the store's code passes it what it asks for by name.
Una gives each risk a rule that the checker enforces. A condition puts "never in a dry run" into the header, where the verifier proves it on every path. A lock in una.toml keeps a copy of a header, so that widening it needs a change to the manifest, which a reviewer sees. A boundary gives a module a ceiling, and every call into it must grant each capability by hand, within that ceiling.
In edition
0.3, a header can say when a function may act,una.tomlcan lock what a header allows, and a module from outside can receive no more than its ceiling.