Home / Learn / 5 Contracts and the verifier
Release v0.4.0Edition 0.2
5.8 Common mistakes
Page 9 of 11
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)
In discount, return the coupon's value without the cap:
--- 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
[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.
5.8.2 A caller that needs a promise the callee does not make (U5001)
Remove ensures result <= amount from discount:
--- 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
[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.
5.8.3 A comparison the wrong way round (U5001)
In refund_line, swap asked and paid in the guard:
--- 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"
[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.
5.8.4 A product without a guard (U5002)
Write line_total as one expression:
--- 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
[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. See U5002.
5.8.5 A contract outside the refinement language (U2014)
Promise the exact product in line_total:
--- 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
=
[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.
5.8.6 A rate above 100 percent (U5001)
Give the coupon a rate of 110:
--- 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
[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 shows. See U5001.
Next:5.9 Exercises