---
title: "13.7 Exercises"
url: https://una-lang.org/en/learn/authority/exercises/
release: v0.4.0
edition: "0.2, 0.3"
---

# 13.7 Exercises

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

::::exercise
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:

```una include="exercises/1/start/src/store/sale.una#L84-L107" title="exercises/1/start/src/store/sale.una"
--- 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.
pub clearance(item: Item, dry_run: Bool) -> Int
  uses @prices: PriceList[set_price where not 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()
  do clearance(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: true)
  expect result == 2250

test "a clearance halves the price"
  given @prices = ignored()
  do clearance(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: false)
  expect result == 2250

test "a cheap item keeps its price"
  given @prices = ignored()
  do clearance(Item(sku: "SOAP-03", name: "Hand soap", cents: 800), dry_run: false)
  expect result == 800
```

`una check .` reports:

```text include="exercises/1/start/expected/diagnostics.txt"
error U5001 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
*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 Answer (*check, run, test*; `exercises/1/answer`).
Keep a cheap item's price, and guard the operation in the branch that halves it:

```diff include="exercises/1/start..exercises/1/answer:src/store/sale.una" context="1" title="src/store/sale.una"
--- 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

::::exercise
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`:

```toml include="exercises/2/start/una.toml" title="exercises/2/start/una.toml"
[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]"]
```

```una include="exercises/2/start/src/store/sale.una#L84-L100" title="exercises/2/start/src/store/sale.una"
--- 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`.
pub end_sale(item: Item, dry_run: Bool) -> Int
  uses @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()
  do end_sale(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: true)
  expect result == 4500

test "the end of a sale gives the old price back"
  given @prices = ignored()
  do end_sale(Item(sku: "RUG-07", name: "Wool rug", cents: 4500), dry_run: false)
  expect result == 4500
```

`una check .` reports that the header lets `set_price` run when `dry_run` is true:

```text include="exercises/2/start/expected/diagnostics.txt"
error U3023 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
*Hint:* copy the condition from the lock into the header. The verifier then asks for the guard that makes the header true:

```text include="exercises/2/header-only/expected/rendered.txt#L1,L12"
[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 Answer (*check, run, test*; `exercises/2/answer`).
Write the locked condition in the header, and guard the operation to match it:

```diff include="exercises/2/start..exercises/2/answer:src/store/sale.una" context="1" title="src/store/sale.una"
--- 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](https://una-lang.org/en/learn/authority/locking-a-contract-contracts/#how-a-lock-is-checked).
:::
::::

## 13.7.3 Exercise 3: end the sale for every item

::::exercise
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:

```una include="exercises/3/start/src/store/sale.una#L84-L109" title="exercises/3/start/src/store/sale.una"
--- Sets one item back to its price before the sale.
pub put_back(item: Item) -> Unit
  uses @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.
pub end_all(items: List[Item], dry_run: Bool) -> Int
  uses @prices: PriceList[set_price where not dry_run]
=
  let edit count = 0
  for item in items
    put_back(item)
    count = count + 1
  count

test "a dry run counts the prices"
  given @prices = ignored()
  do end_all([Item(sku: "RUG-07", name: "Wool rug", cents: 4500), Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990)], dry_run: true)
  expect result == 2

test "the end of the sale counts every price it puts back"
  given @prices = ignored()
  do end_all([Item(sku: "RUG-07", name: "Wool rug", cents: 4500), Item(sku: "LAMP-01", name: "Desk lamp", cents: 1990)], dry_run: false)
  expect result == 2
```

`una check .` reports:

```text include="exercises/3/start/expected/diagnostics.txt"
error U5001 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
*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 Answer (*check, run, test*; `exercises/3/answer`).
Guard the call, and count on every turn:

```diff include="exercises/3/start..exercises/3/answer:src/store/sale.una" context="1" title="src/store/sale.una"
--- 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.
:::
::::
