---
title: "18.9 Exercises"
url: https://una-lang.org/en/learn/tools/exercises/
release: v0.4.0
edition: "0.2"
---

# 18.9 Exercises

Each exercise starts from `exercises/N/start`: the day-to-day program with the exercise's function and example added. `una test .` fails there until the exercise is done. Work in the start package's directory, and use the loop of this chapter: `una fmt .`, `una check .`, read each diagnostic from its first line, then `una test .`.

## 18.9.1 Exercise 1: a clearance price

::::exercise
A clearance takes 5.00 off, never below cost. The start package has a first attempt, which `una check .` rejects, and an example:

```una include="exercises/1/start/src/pricetags.una#clearance_price" title="exercises/1/start/src/pricetags.una"
--- The price of a product on clearance: 5.00 off, never below cost. Exercise 1: make the
--- verifier accept it.
pub clearance_price(product: Product) -> Cents
  ensures result >= product.cost
  derived from PRICING_1
=
  if product.price > product.cost then product.price - 500 else product.cost
```

```una include="exercises/1/start/src/pricetags.una#L92-L94" title="exercises/1/start/src/pricetags.una"
example "a clearance takes 5.00 off, but stops at cost"
  do clearance_price(Product(sku: "LAMP-01", price: 1500, cost: 1200))
  expect result == 1200
```

`una check .` reports two refuted obligations with the same counterexample:

```text include="exercises/1/start/expected/diagnostics.txt"
error U5001 at src/pricetags.una:90:40: refinement `result >= 0` is refuted
  counterexample: product = "Product#0", product.cost = 0, product.price = 1, result = -499
error U5001 at src/pricetags.una:90:40: postcondition of `clearance_price` is refuted
  counterexample: product = "Product#0", product.cost = 0, product.price = 1, result = -499
```

The first is the refinement of the result, a `Cents`; the second is the `ensures` clause. Both fail on the same path.

:::hint
*Hint:* compute the result for the counterexample by hand: a price of 1 and a cost of 0. The `if` takes its first branch, since 1 is above 0, and returns 1 - 500. The test in the `if` compares the wrong value: it should ask whether the price after the 5.00 is still above cost.
:::

Check your answer with `una check .`, then `una test .`.

:::answer Answer (*check, run, test*; `exercises/1/answer`).
Compare the discounted price with the cost, not the price:

```diff include="exercises/1/start..exercises/1/answer:src/pricetags.una" context="1" title="src/pricetags.una"
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -83,4 +83,3 @@
 
---- The price of a product on clearance: 5.00 off, never below cost. Exercise 1: make the
---- verifier accept it.
+--- The price of a product on clearance: 5.00 off, never below cost.
 pub clearance_price(product: Product) -> Cents
@@ -89,3 +88,3 @@
 =
-  if product.price > product.cost then product.price - 500 else product.cost
+  if product.price - 500 > product.cost then product.price - 500 else product.cost
 
```
:::
::::

## 18.9.2 Exercise 2: a basket

::::exercise
The till needs the total of a basket during the sale. Write the body of `basket_total`, which asks the catalog for each SKU and adds up the sale prices; a SKU the catalog does not list adds nothing. The start package has the stub and an example:

```una include="exercises/2/start/src/pricetags.una#basket_total" title="exercises/2/start/src/pricetags.una"
--- The total of a basket during a sale of `off` percent. A SKU the catalog does not list
--- adds nothing. Exercise 2: write the body.
pub basket_total(skus: List[Text], off: Percent) -> Cents
  uses @catalog: Catalog[product]
=
  0
```

```una include="exercises/2/start/src/pricetags.una#L91-L94" title="exercises/2/start/src/pricetags.una"
example "a basket adds the sale prices of the products it lists"
  given @catalog = listed(sample())
  do basket_total(["LAMP-01", "DESK-02", "CHAIR-09"], off: 30)
  expect result == 12393
```

:::hint
*Hint:* a `for` loop over the SKUs ([section 10.1](https://una-lang.org/en/learn/collections/lists/#s10-1)), a `let edit total: Cents = 0` ([section 9.2](https://una-lang.org/en/learn/loops/names-that-change-let-edit/#s9-2)), and a `when` on what the catalog gives, with one arm for `Some(product)` and one for `None`. The verifier proves that the total stays a `Cents`, since each sale price is one. When you are done, `una contract pricetags.basket_total` should show the same `uses` as the stub: the loop adds no power.
:::

Check your answer with `una test .`. On the start package it reports:

```text include="exercises/2/start/expected/commands/test.txt#L6-L10"
example pricetags "a basket adds the sale prices of the products it lists" ... FAILED
  src/pricetags.una:94: expect result == 12393
  it is false, with `result` = `0`

5 examples: 4 passed, 1 failed
```

:::answer Answer (*check, run, test*; `exercises/2/answer`).
Replace the body:

```diff include="exercises/2/start..exercises/2/answer:src/pricetags.una" context="1" title="src/pricetags.una"
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -84,3 +84,3 @@
 --- The total of a basket during a sale of `off` percent. A SKU the catalog does not list
---- adds nothing. Exercise 2: write the body.
+--- adds nothing.
 pub basket_total(skus: List[Text], off: Percent) -> Cents
@@ -88,3 +88,9 @@
 =
-  0
+  let edit total: Cents = 0
+  for sku in skus
+    let found = @catalog.product(sku)
+    total = when found
+      is Some(product) -> total + sale_price(product, off: off)
+      is None -> total
+  total
 
```

`una contract pricetags.basket_total` shows that the basket may ask the catalog and nothing else:

```text include="exercises/2/answer/expected/commands/contract.txt"
$ una contract pricetags.basket_total
--- The total of a basket during a sale of `off` percent. A SKU the catalog does not list
--- adds nothing.
pub basket_total(skus: List[Text], off: Percent) -> Cents
  uses @catalog: Catalog[product]
```
:::
::::

## 18.9.3 Exercise 3: two requirements

::::exercise
The store adds a second rule: a member never pays more than the shelf price. `member_price` takes 1.00 off and is derived from both requirements. The start package adds the requirement, the function and two examples:

```una include="exercises/3/start/src/pricetags.una#L84-L105" title="exercises/3/start/src/pricetags.una"
requirement PRICING_2
  text "A member never pays more than the price on the shelf"
  source human
  owner pricing
  status active

--- The price a member pays: 1.00 off, never below cost and never above the shelf price.
--- Exercise 3: make the verifier accept it, without dropping either requirement.
pub member_price(product: Product) -> Cents
  ensures result >= product.cost
  ensures result <= product.price
  derived from PRICING_1, PRICING_2
=
  if product.price - 100 < product.cost then product.cost else product.price - 100

example "a member pays 1.00 less"
  do member_price(Product(sku: "LAMP-01", price: 1990, cost: 1200))
  expect result == 1890

example "a member price stops at cost"
  do member_price(Product(sku: "DESK-02", price: 11050, cost: 11000))
  expect result == 11000
```

`una check .` reports:

```text include="exercises/3/start/expected/diagnostics.txt"
error U5001 at src/pricetags.una:97:46: postcondition of `member_price` is refuted
  counterexample: product = "Product#0", product.cost = 1, product.price = 0, result = 1
```

Make the package check without dropping either `ensures` clause, and keep both examples passing.

:::hint
*Hint:* look at the counterexample: a cost of 1 and a price of 0. No result can be at least the cost and at most the price for that product. The function cannot fix that; its callers can promise never to ask. Which clause states what a caller promises ([chapter 5](https://una-lang.org/en/learn/contracts/preconditions-requires/#s5-1))?
:::

Check your answer with `una check .`, then `una test .` and `una trace PRICING_2`.

:::answer Answer (*check, run, test*; `exercises/3/answer`).
Add a `requires` clause, which every caller then proves:

```diff include="exercises/3/start..exercises/3/answer:src/pricetags.una" context="1" title="src/pricetags.una"
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -90,4 +90,6 @@
 --- The price a member pays: 1.00 off, never below cost and never above the shelf price.
---- Exercise 3: make the verifier accept it, without dropping either requirement.
+--- A product whose cost is above its price cannot meet both requirements, so callers prove
+--- that it is not one.
 pub member_price(product: Product) -> Cents
+  requires product.cost <= product.price
   ensures result >= product.cost
```

`una trace PRICING_2` shows the second requirement reaching `member_price`, with every obligation proven:

```text include="exercises/3/answer/expected/commands/trace.txt"
$ una trace PRICING_2
requirement PRICING_2 (active)
  text "A member never pays more than the price on the shelf"
  source human
  owner pricing
pricetags.member_price
  requires product.cost <= product.price
  ensures result >= product.cost
  ensures result <= product.price
  proven   ob-f6655d5a7e9c refinement    result >= 0
  proven   ob-077e1c646169 postcondition result >= product.cost
  proven   ob-592d56ba992b postcondition result <= product.price
```
:::
::::
