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

# 5.9 Exercises

The exercises come with the chapter's download, `05-contracts.tar.gz`. Each starts from `exercises/N/start`: the day-to-day program with the exercise's examples, and a stub, a function to finish, when there is one. `una check .` or `una test .` fails there until the exercise is done. Run `una fmt .` before `una check .`: it lays the code out in canonical form, for example a body of one expression on the header's line.

## 5.9.1 Exercise 1: free shipping

::::exercise
Shipping costs 4.95, and nothing from 50.00. The start package has `shipping` with its contract and a body that always charges 4.95. `una check .` refutes it:

```una include="exercises/1/start/src/billing.una#shipping" title="exercises/1/start/src/billing.una"
--- What shipping costs for an invoice of `total` cents: 4.95, and nothing from 50.00.
--- Exercise 1: make the body keep the contract.
pub shipping(total: Cents) -> Cents
  ensures result <= 495
  ensures total >= 5000 implies result == 0
=
  495
```

```text include="exercises/1/start/expected/rendered.txt#L1,L18"
[U5001] Error: postcondition of `shipping` is refuted
    │ Note 2: counterexample: result = 495, total = 5000
```

:::hint
*Hint:* read the counterexample as a test case: here the body has no calls, so it is one. For an invoice of exactly 50.00, the body charges 4.95, which breaks the second clause. The body needs two cases, so write it as `if <condition> then <one price> else <the other>`, with the condition the clause names. A branch on `total > 5000` is refuted with the same counterexample: 5000 itself must ship free.
:::

Check your answer with `una check .`, then `una test .`, which runs these examples:

```una include="exercises/1/start/src/billing.una#L158-L164" title="exercises/1/start/src/billing.una"
example "an invoice from 50.00 ships free"
  do shipping(5000)
  expect result == 0

example "a smaller invoice pays 4.95"
  do shipping(4999)
  expect result == 495
```

:::answer Answer (*check, run, test*; `exercises/1/answer`).
Give the body a branch:

```diff include="exercises/1/start..exercises/1/answer:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -82,3 +82,2 @@
 --- What shipping costs for an invoice of `total` cents: 4.95, and nothing from 50.00.
---- Exercise 1: make the body keep the contract.
 pub shipping(total: Cents) -> Cents
@@ -87,3 +86,3 @@
 =
-  495
+  if total >= 5000 then 0 else 495
 
```
:::
::::

## 5.9.2 Exercise 2: a fixed price

::::exercise
A coupon can set the price of a whole invoice, such as "everything for 150.00". Add a variant `FixedPrice(cents: Cents)` to `Coupon`, and an arm for it in `discount`: it takes off the difference when the invoice costs more than the fixed price, and nothing otherwise. The start package ends with these examples, and `una check .` reports U1001, "cannot find name `FixedPrice`", until the variant exists:

```una include="exercises/2/start/src/billing.una#L150-L156" title="exercises/2/start/src/billing.una"
example "a fixed price below the subtotal takes off the difference"
  do discount(18880, coupon: FixedPrice(cents: 15000))
  expect result == 3880

example "a fixed price above the subtotal takes off nothing"
  do discount(1000, coupon: FixedPrice(cents: 1500))
  expect result == 0
```

:::hint
*Hint:* work in three steps. First add `| FixedPrice(cents: Cents)` at the end of `Coupon`, and run `una check .`: it now reports that the `when` of `discount` has no arm for the new variant, as [section 4.3](https://una-lang.org/en/learn/kinds/taking-a-sum-apart-when/#s4-3) says it must. Then add the arm `is FixedPrice(cents) -> …`. The verifier checks it against the header of `discount`: the result must be a `Cents`, so never negative, and at most `amount`.
:::

The first arm most people write is `amount - cents`. `una check .` refutes it:

```text include="exercises/2/unguarded/expected/rendered.txt#L1,L8,L18"
[U5001] Error: refinement `result >= 0` is refuted
 65 │     is FixedPrice(cents) -> amount - cents
    │ Note 2: counterexample: amount = 0, coupon = FixedPrice, coupon.amount = -1, coupon.cents = 3, coupon.rate = 101, result = -3
```

The counterexample is a fixed price of 0.03 on an invoice of 0: the discount would be −3 cents. Only `coupon.cents` matters here, as in [a coupon that can be worth more than the order](https://una-lang.org/en/learn/contracts/common-mistakes/#a-coupon-that-can-be-worth-more-than-the-order-u5001): `coupon.amount = -1` is outside `Cents` because that field belongs to no value here.

Check your answer with `una test .`.

:::answer Answer (*check, run, test*; `exercises/2/answer`).
Add the variant, and an arm with a guard:

```diff include="exercises/2/start..exercises/2/answer:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -30,3 +30,3 @@
 --- A coupon on the whole invoice.
-pub kind Coupon = NoCoupon | PercentOff(rate: Percent) | CentsOff(amount: Cents)
+pub kind Coupon = NoCoupon | PercentOff(rate: Percent) | CentsOff(amount: Cents) | FixedPrice(cents: Cents)
 
@@ -64,2 +64,3 @@
     is CentsOff(off) -> if off > amount then amount else off
+    is FixedPrice(cents) -> if cents < amount then amount - cents else 0
 
```
:::
::::

## 5.9.3 Exercise 3: a second refund

::::exercise
A customer can ask for a refund in parts. Write `refund_again(paid, already, asked)`, which pays back `asked` when the refunds together stay within what was paid, and pays back nothing, 0, otherwise. The start package has the header, with its contract, and a body that forgets the refunds already made. `una check .` refutes it:

```una include="exercises/3/start/src/billing.una#refund_again" title="exercises/3/start/src/billing.una"
--- A further refund, after `already` was paid back: the refunds together never pay back more
--- than the customer paid, and a refund that would go above `paid` pays back nothing.
--- Exercise 3: refuse what would go above `paid`.
pub refund_again(paid: Cents, already: Cents, asked: Cents) -> Cents
  requires already <= paid
  ensures already + result <= paid
  derived from BILLING_2
=
  if asked > paid then 0 else refund(paid, asked: asked)
```

```text include="exercises/3/start/expected/rendered.txt#L1,L18"
[U5001] Error: postcondition of `refund_again` is refuted
    │ Note 2: counterexample: already = 1, asked = 1, paid = 1, result = 1
```

The counterexample is a real run: a customer paid 0.01, already got 0.01 back, and asks for 0.01 again. `asked` is not more than `paid`, so the body pays it back, and the refunds together come to 0.02.

:::hint
*Hint:* the body may not rely on `refund` alone, since `refund` knows nothing of `already`. Write the test yourself: compare `already + asked` with `paid`, return 0 when it is more, and return `asked` otherwise. In the `else` branch, the verifier then knows `already + asked <= paid`, which is the promise.
:::

Check your answer with `una test .`, which runs these examples:

```una include="exercises/3/start/src/billing.una#L160-L166" title="exercises/3/start/src/billing.una"
example "a second refund cannot pass what is left"
  do refund_again(16992, already: 10000, asked: 8000)
  expect result == 0

example "a second refund within what is left is paid back"
  do refund_again(16992, already: 10000, asked: 6992)
  expect result == 6992
```

:::answer Answer (*check, run, test*; `exercises/3/answer`).
Refuse what would take the refunds above `paid`:

```diff include="exercises/3/start..exercises/3/answer:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -83,3 +83,2 @@
 --- than the customer paid, and a refund that would go above `paid` pays back nothing.
---- Exercise 3: refuse what would go above `paid`.
 pub refund_again(paid: Cents, already: Cents, asked: Cents) -> Cents
@@ -89,3 +88,3 @@
 =
-  if asked > paid then 0 else refund(paid, asked: asked)
+  if already + asked > paid then 0 else asked
 
```

The function is `derived from BILLING_2`, so `una trace BILLING_2` now lists it next to `refund`, with its proofs:

```text include="exercises/3/answer/expected/commands/trace.txt#L11-L15"
billing.refund_again
  requires already <= paid
  ensures already + result <= paid
  proven   ob-f4387ed6cf13 refinement    result >= 0
  proven   ob-0b04b97219e5 postcondition already + result <= paid
```
:::
::::
