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
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:
exercises/1/start/src/billing.unauna
--- What shipping costs for an invoice of `total` cents: 4.95, and nothing from 50.00.--- Exercise 1: make the body keep the contract.pubshipping(total: Cents) -> Centsensuresresult <= 495ensures total >= 5000impliesresult == 0
=
495
Diagnostic
[U5001]Error: postcondition of `shipping` is refuted│ Note 2: counterexample: result = 495, total = 5000
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:
exercises/1/start/src/billing.unauna
example"an invoice from 50.00 ships free"doshipping(5000)
expectresult == 0example"a smaller invoice pays 4.95"doshipping(4999)
expectresult == 495
Answer (check, run, test; exercises/1/answer).
Give the body a branch:
src/billing.unadiff
--- 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
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:
exercises/2/start/src/billing.unauna
example"a fixed price below the subtotal takes off the difference"dodiscount(18880, coupon: FixedPrice(cents: 15000))
expectresult == 3880example"a fixed price above the subtotal takes off nothing"dodiscount(1000, coupon: FixedPrice(cents: 1500))
expectresult == 0
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 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:
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: coupon.amount = -1 is outside Cents because that field belongs to no value here.
Check your answer with una test ..
Answer (check, run, test; exercises/2/answer).
Add the variant, and an arm with a guard:
src/billing.unadiff
--- 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
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:
exercises/3/start/src/billing.unauna
--- 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`.pubrefund_again(paid: Cents, already: Cents, asked: Cents) -> Centsrequires already <= paid
ensures already + result <= paid
derivedfromBILLING_2
=
if asked > paid then0elserefund(paid, asked: asked)
Diagnostic
[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: 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:
exercises/3/start/src/billing.unauna
example"a second refund cannot pass what is left"dorefund_again(16992, already: 10000, asked: 8000)
expectresult == 0example"a second refund within what is left is paid back"dorefund_again(16992, already: 10000, asked: 6992)
expectresult == 6992
Answer (check, run, test; exercises/3/answer).
Refuse what would take the refunds above paid:
src/billing.unadiff
--- 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:
Output
billing.refund_again
requires already <= paid
ensures already + result <= paid
proven ob-f4387ed6cf13 refinement result >= 0
proven ob-0b04b97219e5 postcondition already + result <= paid