The exercises come with the chapter's download, 13-authority.tar.gz. Each starts from exercises/N/start: the day-to-day program with a new function and its tests added. una check . reports a problem there, so una test . fails until the exercise is done.
13.7.1 Exercise 1: a clearance that keeps its dry run
At the end of a season, the store halves the price of some products. A product of 1000 cents or less is cheap already, and keeps its price. The start package adds clearance, whose header allows set_price only where not dry_run and item.cents > 1000 holds, and three tests:
exercises/1/start/src/store/sale.unauna
--- Halves the price of an item at the end of a season, and gives back its new price. An item of--- 1000 cents or less is cheap already, and keeps its price. A dry run changes no price. Exercise 1:--- make the body keep its header.pubclearance(item: Item, dry_run: Bool) -> Intuses@prices: PriceList[set_price wherenot dry_run and item.cents > 1000]
=
let half = item.cents / 2@prices.set_price(item.sku, cents: half)
half
test "a dry run of a clearance gives the new price"given@prices = ignored()
doclearance(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: true)
expectresult == 2250
test "a clearance halves the price"given@prices = ignored()
doclearance(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: false)
expectresult == 2250
test "a cheap item keeps its price"given@prices = ignored()
doclearance(Item(sku: "SOAP-03", name: "Hand soap", cents: 800), dry_run: false)
expectresult == 800
una check . reports:
Diagnostic
errorU5001 at src/store/sale.una:91:3: `@prices.set_price` may run where `not dry_run and item.cents > 1000` is not proven
counterexample: dry_run = true, half = 500, item = "Item#0", item.cents = 1001, item.sku = "Text#0"
Change the body of clearance so that it keeps its header, and the three tests pass.
Hint: the diagnostic's first repair guards the operation, but the result must change too: a cheap item gives back its own price. Let one if choose the price, and a second guard the operation inside the branch that halves it. The verifier proves the condition from both.
Check your answer with una test ..
Answer (check, run, test; exercises/1/answer).
Keep a cheap item's price, and guard the operation in the branch that halves it:
src/store/sale.unadiff
--- a/src/store/sale.una+++ b/src/store/sale.una@@ -84,4 +84,3 @@
--- Halves the price of an item at the end of a season, and gives back its new price. An item of
---- 1000 cents or less is cheap already, and keeps its price. A dry run changes no price. Exercise 1:---- make the body keep its header.+--- 1000 cents or less is cheap already, and keeps its price. A dry run changes no price.
pub clearance(item: Item, dry_run: Bool) -> Int
@@ -89,5 +88,9 @@
=
- let half = item.cents / 2- @prices.set_price(item.sku, cents: half)- half+ if item.cents <= 1000+ item.cents+ else+ let half = item.cents / 2+ if not dry_run+ @prices.set_price(item.sku, cents: half)+ half
Inside the else, item.cents > 1000 holds, and inside the guard not dry_run holds, so both parts of the condition are proven where set_price runs.
13.7.2 Exercise 2: honor a lock
The start package adds end_sale, which puts an item's old price back and gives back that price, and a lock for it in una.toml:
exercises/2/start/una.tomltoml
[package]
name = "store"
version = "0.1.0"
edition = "0.3"
[run]
entry = "store.app"
grants = ["console"]
# What the sale may do with the price list, and when.
[contracts]
"store.sale.apply_sale" = ["@prices: PriceList[set_price where not dry_run]"]
"store.sale.end_sale" = ["@prices: PriceList[set_price where not dry_run]"]
"store.sale.run_sale" = ["@prices: PriceList[set_price where not dry_run]", "@console: Console[print]"]
# Code from the maker of the tag printer: the most the store may ever grant it.
[boundaries]
"vendor.tags" = ["Console[print]"]
exercises/2/start/src/store/sale.unauna
--- Puts back the price an item had before the sale, and gives back that price. A dry run changes--- no price. Exercise 2: honor the lock in `una.toml`.pubend_sale(item: Item, dry_run: Bool) -> Intuses@prices: PriceList[set_price]
=
@prices.set_price(item.sku, cents: item.cents)
item.cents
test "a dry run of the end of a sale gives the old price"given@prices = ignored()
doend_sale(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: true)
expectresult == 4500
test "the end of a sale gives the old price back"given@prices = ignored()
doend_sale(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: false)
expectresult == 4500
una check . reports that the header lets set_price run when dry_run is true:
Diagnostic
errorU3023 at src/store/sale.una:87:27: the header of `end_sale` lets `@prices.set_price` run where `not dry_run` does not hold, which `una.toml` locks
counterexample: dry_run = true
Change end_sale so that a dry run sets no price, and its header keeps the lock. Do not change una.toml.
Hint: copy the condition from the lock into the header. The verifier then asks for the guard that makes the header true:
Diagnostic
[U5001]Error:`@prices.set_price` may run where `not dry_run` is not proven│ Help 1: repair 1: guard the operation with `if not dry_run`
Check your answer with una test .. Both tests must pass, which needs una check . to report nothing: the verifier then proves that a dry run sets no price.
Answer (check, run, test; exercises/2/answer).
Write the locked condition in the header, and guard the operation to match it:
src/store/sale.unadiff
--- a/src/store/sale.una+++ b/src/store/sale.una@@ -84,7 +84,8 @@
--- Puts back the price an item had before the sale, and gives back that price. A dry run changes
---- no price. Exercise 2: honor the lock in `una.toml`.+--- no price.
pub end_sale(item: Item, dry_run: Bool) -> Int
- uses @prices: PriceList[set_price]+ uses @prices: PriceList[set_price where not dry_run]
=
- @prices.set_price(item.sku, cents: item.cents)+ if not dry_run+ @prices.set_price(item.sku, cents: item.cents)
item.cents
The header now keeps the lock, and the body keeps the header: two separate proofs, as in How a lock is checked.
13.7.3 Exercise 3: end the sale for every item
The start package adds put_back, which sets one item back to its old price, and end_all, which calls it for every item and counts them. end_all promises that a dry run sets no price, but its loop calls put_back on every turn:
exercises/3/start/src/store/sale.unauna
--- Sets one item back to its price before the sale.pubput_back(item: Item) -> Unituses@prices: PriceList[set_price]
=
@prices.set_price(item.sku, cents: item.cents)
--- Ends the sale for every item, and gives back how many prices it puts back. A dry run only--- counts them. Exercise 3: make the dry run set no price.pubend_all(items: List[Item], dry_run: Bool) -> Intuses@prices: PriceList[set_price wherenot dry_run]
=
letedit count = 0for item in items
put_back(item)
count = count + 1
count
test "a dry run counts the prices"given@prices = ignored()
doend_all([Item(sku: "RUG-07", name: "Wool rug", cents: 4500), Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990)], dry_run: true)
expectresult == 2
test "the end of the sale counts every price it puts back"given@prices = ignored()
doend_all([Item(sku: "RUG-07", name: "Wool rug", cents: 4500), Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990)], dry_run: false)
expectresult == 2
una check . reports:
Diagnostic
errorU5001 at src/store/sale.una:97:5: `@prices.set_price` may run where `not dry_run` is not proven
counterexample: count = 0, dry_run = true, items = "List#0", items.len = 1
Change end_all so that a dry run still counts every item, sets no price, and both tests pass. Do not change put_back.
Hint: the diagnostic is at the call of put_back, not at an operation: its header has no condition, so it may set a price on every call. put_back has no dry_run parameter, so a condition in its header could not name one. Guard the call instead, and keep the count outside the guard.
Check your answer with una test ..
Answer (check, run, test; exercises/3/answer).
Guard the call, and count on every turn:
src/store/sale.unadiff
--- a/src/store/sale.una+++ b/src/store/sale.una@@ -90,3 +90,3 @@
--- Ends the sale for every item, and gives back how many prices it puts back. A dry run only
---- counts them. Exercise 3: make the dry run set no price.+--- counts them.
pub end_all(items: List[Item], dry_run: Bool) -> Int
@@ -96,3 +96,4 @@
for item in items
- put_back(item)+ if not dry_run+ put_back(item)
count = count + 1
A call of a function that may perform the operation is checked as the operation is. Inside the guard, dry_run is false, so the condition of end_all holds at the call.