---
title: "5.8 Common mistakes"
url: https://una-lang.org/en/learn/contracts/common-mistakes/
release: v0.4.0
edition: "0.2"
---

# 5.8 Common mistakes

Each mistake is a small change to `day-to-day/src/billing.una`, followed by what `una check .` reports. Read each diagnostic the same way: the first line names the obligation and its goal, the marks show the clause that asks and the code that does not deliver, and `Note 2`, when there is one, is the counterexample.

## 5.8.1 A coupon that can be worth more than the order (U5001)

:::mistake
In `discount`, return the coupon's value without the cap:

```diff include="day-to-day..mistakes/1:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -63,3 +63,3 @@
     is PercentOff(rate) -> percent_of(amount, rate)
-    is CentsOff(off) -> if off > amount then amount else off
+    is CentsOff(off) -> off
 
```

```text include="mistakes/1/expected/rendered.txt"
[U5001] Error: postcondition of `discount` is refuted
    ╭─[ src/billing.una:64:25 ]
    │
 58 │   ensures result <= amount
    │           ────────┬───────
    │                   ╰───────── ensures clause
    │
 64 │     is CentsOff(off) -> off
    │                         ─┬─
    │                          ╰─── postcondition of `discount` is refuted
    │
    │ Help 1: repair 1: add a guard or an arm so that the failing path returns a value for which `result <= amount` holds
    │
    │ Help 2: repair 2: adjust the `ensures` clause
    │
    │ Note 1: obligation ob-e0a2abc0bb6a (postcondition) is refuted: result <= amount
    │
    │ Note 2: counterexample: amount = 0, coupon = CentsOff, coupon.amount = 1, coupon.rate = 101, result = 1
    │
    │ Note 3: run `una explain U5001` for an explanation
────╯
```

*Why:* the arm returns the coupon's value whatever the amount, and a coupon can be worth more than a cheap invoice. The marks point at the clause, `ensures result <= amount`, and at the arm's `off`. With `amount = 0` and a `CentsOff` coupon of 1 cent, the result is 1, which breaks `result <= amount`. In a counterexample for a sum kind, `coupon = CentsOff` names the variant, and only that variant's fields mean anything: `coupon.rate` belongs to `PercentOff`, and its value here means nothing.

*Fix:* cap the coupon at the amount, as the day-to-day program does. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 5.8.2 A caller that needs a promise the callee does not make (U5001)

:::mistake
Remove `ensures result <= amount` from `discount`:

```diff include="day-to-day..mistakes/2:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -57,3 +57,2 @@
 pub discount(amount: Cents, coupon: Coupon) -> Cents
-  ensures result <= amount
   derived from BILLING_1
```

```text include="mistakes/2/expected/rendered.txt#L1-L10,L18"
[U5001] Error: refinement `(gross - off) >= 0` is refuted
     ╭─[ src/billing.una:109:22 ]
     │
  19 │ pub kind Cents = Int where it >= 0
     │                            ───┬───
     │                               ╰───── where clause
     │
 109 │   let total: Cents = gross - off
     │                      ─────┬─────
     │                           ╰─────── refinement `(gross - off) >= 0` is refuted
     │ Note 2: counterexample: desk = Line#0, desk.quantity = 1, desk.sku = "DESK-02", desk.unit_cents = 14900, gross = 0, lamp = Line#1, lamp.quantity = 2, lamp.sku = "LAMP-01", lamp.unit_cents = 1990, off = 1, total = -1
```

*Why:* the clause looked unneeded, since `discount` keeps it anyway, but the error is in `main`, not in `discount`. A caller knows a function only by its header, so without the clause `main` cannot prove that `gross - off` is not negative. The counterexample is not a run: `gross` is 0 and `off` is 1, values that the headers of `subtotal` and `discount` now allow. `Line#0` and `Line#1` name the two lines the solver chose; only `gross` and `off` matter.

*Fix:* put the clause back. When a caller needs a fact about a result, the callee's `ensures` is where it goes. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 5.8.3 A comparison the wrong way round (U5001)

:::mistake
In `refund_line`, swap `asked` and `paid` in the guard:

```diff include="day-to-day..mistakes/3:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -93,3 +93,3 @@
 =
-  if asked > paid
+  if paid > asked
     "refund {money(asked)}: refused, only {money(paid)} was paid"
```

```text include="mistakes/3/expected/rendered.txt#L1-L10,L18"
[U5001] Error: precondition `asked <= paid` of this call is refuted
    ╭─[ src/billing.una:97:16 ]
    │
 76 │   requires asked <= paid
    │            ──────┬──────
    │                  ╰──────── requires clause
    │
 97 │     let back = refund(paid, asked: asked)
    │                ─────────────┬────────────
    │                             ╰────────────── precondition `asked <= paid` of this call is refuted
    │ Note 2: counterexample: asked = 1, paid = 0
```

*Why:* the two names read alike, and `paid > asked` looks as plausible as `asked > paid`. The marks point at the `requires` clause of `refund` and at the call in the `else` branch. In that branch `paid > asked` is false, which allows `paid = 0` and `asked = 1`: a refund of 1 cent on an invoice that cost nothing. The precondition catches a swap that a quick review misses.

*Fix:* `if asked > paid`. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 5.8.4 A product without a guard (U5002)

:::mistake
Write `line_total` as one expression:

```diff include="day-to-day..mistakes/4:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -39,6 +39,3 @@
 --- are at least 0; the solver cannot see that for itself.
-pub line_total(line: Line) -> Cents
-=
-  let total = line.unit_cents * line.quantity
-  if total < 0 then 0 else total
+pub line_total(line: Line) -> Cents = line.unit_cents * line.quantity
 
```

```text include="mistakes/4/expected/rendered.txt#L1"
[U5002] Error: refinement `result >= 0` could not be proven (the refutation depends on a product, quotient or remainder of two values, which the encoding of `spec/12 §2` leaves uninterpreted)
```

*Why:* one expression looks simpler, and the rule obviously holds. But the solver treats the product of two values as an unknown number, so it cannot prove the result is not negative. The message ends with the reason, in parentheses. The values the solver found do not agree with real multiplication, so there is no counterexample, and the obligation is unknown, not refuted.

*Fix:* bind the product, then guard it, as in [Giving the solver the fact](https://una-lang.org/en/learn/contracts/what-the-verifier-proves/#giving-the-solver-the-fact-bind-and-guard). See [U5002](https://una-lang.org/en/docs/errors/U5002/).
:::

## 5.8.5 A contract outside the refinement language (U2014)

:::mistake
Promise the exact product in `line_total`:

```diff include="day-to-day..mistakes/5:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -40,2 +40,3 @@
 pub line_total(line: Line) -> Cents
+  ensures result == line.unit_cents * line.quantity
 =
```

```text include="mistakes/5/expected/rendered.txt#L1"
[U2014] Error: `*` of two operands that are not literals is outside the refinement language
```

*Why:* the exact result seems the most useful promise to make. But a clause is a question for the solver, written in the language it decides, which multiplies only by a literal. U2014 is reported before any proof is tried, and the mark covers the whole product in the clause.

*Fix:* state what the solver can decide, such as `result >= line.unit_cents`, or leave the exact value to an example. See [U2014](https://una-lang.org/en/docs/errors/U2014/).
:::

## 5.8.6 A rate above 100 percent (U5001)

:::mistake
Give the coupon a rate of 110:

```diff include="day-to-day..mistakes/6:src/billing.una" context="1" title="src/billing.una"
--- a/src/billing.una
+++ b/src/billing.una
@@ -108,3 +108,3 @@
   let gross = subtotal(lamp, second: desk)
-  let off = discount(gross, coupon: PercentOff(rate: 10))
+  let off = discount(gross, coupon: PercentOff(rate: 110))
   let total: Cents = gross - off
```

```text include="mistakes/6/expected/rendered.txt#L1"
[U5001] Error: refinement `110 in 0..100` is refuted
```

*Why:* a rate of 110 is an easy slip of the keyboard. `rate` is a `Percent`, and the marks point at the range of `Percent` and at the literal 110. The goal names no variable, so there is no counterexample: the literal itself is the case.

*Fix:* a rate from 0 to 100. A rate that a person types while the program runs is checked once, where it enters the program, as [section 7.6](https://una-lang.org/en/learn/failure/data-from-outside-checking-it-once/#s7-6) shows. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::
