Home / Learn / 18 Tools

Release v0.4.0Edition 0.2

18.9 Exercises

Page 10 of 11

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

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

exercises/1/start/src/pricetags.unauna
--- 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
exercises/1/start/src/pricetags.unauna
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:

Diagnostic
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: 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 (check, run, test; exercises/1/answer).

Compare the discounted price with the cost, not the price:

src/pricetags.unadiff
--- 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

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:

exercises/2/start/src/pricetags.unauna
--- 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
exercises/2/start/src/pricetags.unauna
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: a for loop over the SKUs (section 10.1), a let edit total: Cents = 0 (section 9.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:

Output
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 (check, run, test; exercises/2/answer).

Replace the body:

src/pricetags.unadiff
--- 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:

Terminal
$ 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

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:

exercises/3/start/src/pricetags.unauna
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:

Diagnostic
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: 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)?

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

Answer (check, run, test; exercises/3/answer).

Add a requires clause, which every caller then proves:

src/pricetags.unadiff
--- 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:

Terminal
$ 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

Next:18.10 Recap and reference