Home / Learn / 9 Repeating: for

Release v0.4.0Edition 0.2

9.6 Common mistakes

Page 7 of 9

Each mistake is a small change to day-to-day/src/restock.una, followed by what una check . reports.

9.6.1 Assigning a plain let (U2019)

Declare the accumulator of stock_after with let:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -54,3 +54,3 @@
 =
-  let edit stock: Units = product.on_hand
+  let stock: Units = product.on_hand
   for week in 1..weeks
Diagnostic
[U2019] Error: `stock` is not editable; declare it `let edit stock`, or the parameter `edit stock`
    ╭─[ src/restock.una:57:5 ]
    │
 57 │     stock = sell_week(stock, sales: product.weekly_sales)
    │     ──┬──
    │       ╰──── `stock` is not editable; declare it `let edit stock`, or the parameter `edit stock`
    │
    │ Note: run `una explain U2019` for an explanation
────╯

Why: a name declared with plain let holds one value for good, and only a name declared let edit can be assigned. The loop assigns stock twice in its body, so there are two diagnostics, one at each assignment; the first is shown. The marks point at the name on the left of the assignment.

Fix: let edit stock: Units = product.on_hand, as the message says. See U2019.

9.6.2 A second let inside the loop (U1002, U2019)

Make the stock after the sales a new let inside the body:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -56,3 +56,3 @@
   for week in 1..weeks
-    stock = sell_week(stock, sales: product.weekly_sales)
+    let stock = sell_week(stock, sales: product.weekly_sales)
     stock = stock + order_for(product, stock: stock)
Diagnostic
[U1002] Error: `stock` is already bound in this scope; a local cannot rebind another
    ╭─[ src/restock.una:57:9 ]
    │
 55 │   let edit stock: Units = product.on_hand
    │            ──┬──
    │              ╰──── first defined here
    │
 57 │     let stock = sell_week(stock, sales: product.weekly_sales)
    │         ──┬──
    │           ╰──── `stock` is already bound in this scope; a local cannot rebind another
    │
    │ Note: run `una explain U1002` for an explanation
────╯

Why: the body is a block nested in the function's body, where stock is already a name, and Una lets no name shadow another. The diagnostic marks both: where stock was first defined, and the new let. A second diagnostic, U2019, follows at the next line: stock = stock + … now names the new, plain stock, which cannot be assigned.

Fix: assign the name the loop carries, stock = sell_week(…), without let. See U1002.

9.6.3 Walking a text without chars (U2001)

Walk the SKU itself in department:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -32,3 +32,3 @@
   let edit past_dash = false
-  for character in sku.chars()
+  for character in sku
     if character == "-"
Diagnostic
[U2001] Error: expected `List[_]`, found `Text`

Why: a for loop walks a range or a list. A text is neither, so the checker reports the type it expected after in, a list, List[_], where _ stands for any type of element, and the type it found, Text. The marks point at sku, after in.

Fix: walk sku.chars(), the list of the characters of the text. See U2001.

9.6.4 An accumulator without its refined type (U5002)

Leave the type out of ordered, in units_ordered:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -64,3 +64,3 @@
   let edit stock: Units = product.on_hand
-  let edit ordered: Units = 0
+  let edit ordered = 0
   for week in 1..weeks
Diagnostic
[U5002] Error: refinement `result >= 0` could not be proven (the loop forgets every fact about `ordered` but the refinements of its type, so the counterexample sets `ordered` freely; guard the use with an `if`, or move the update into a helper with an `edit` parameter whose `ensures` states what the use needs)

Why: without the annotation, ordered is a plain Int, the type of its first value. The verifier checks the body once for any turn and keeps, about ordered, only the refinements of its type: none. So after the loop, ordered could be negative as far as it knows, and the result of the function, a Units, is not proven. The code is right; the proof is missing.

Fix: let edit ordered: Units = 0. Each assignment then proves that ordered stays a Units, and the fact survives the loop. See U5002.

9.6.5 A subtraction that can go below zero (U5001)

Sell the week's units in stock_after without sell_week:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -56,3 +56,3 @@
   for week in 1..weeks
-    stock = sell_week(stock, sales: product.weekly_sales)
+    stock = stock - product.weekly_sales
     stock = stock + order_for(product, stock: stock)
Diagnostic
[U5001] Error: refinement `(stock - product.weekly_sales) >= 0` is refuted
    ╭─[ src/restock.una:57:13 ]
    │
  7 │ kind Units = Int where it >= 0
    │                        ───┬───
    │                           ╰───── where clause
    │
 57 │     stock = stock - product.weekly_sales
    │             ──────────────┬─────────────
    │                           ╰─────────────── refinement `(stock - product.weekly_sales) >= 0` is refuted
    │ Help 1: repair 1: add a guard (`if` or a `when` arm) that handles the values where `(stock - product.weekly_sales) >= 0` does not hold
    │ Note 2: counterexample: product = Product#0, product.on_hand = 0, product.weekly_sales = 1, stock = 0, stock (assigned) = -1, week = 1, weeks = 1

Why: stock is a Units, so every assignment to it must give a number that is not negative. A shelf that holds fewer units than a week's sales would end the week below zero, and the counterexample is such a shelf: product.on_hand = 0 and product.weekly_sales = 1, for one week. stock (assigned) = -1 is the value the assignment would give. This rule really breaks, so it is U5001, refuted.

Fix: sell with sell_week, whose guard gives 0 when the stock is short. See U5001.

9.6.6 Counting the weeks with while (U0103)

Count the weeks of stock_after by hand, with a while loop:

diff
--- a/src/restock.una
+++ b/src/restock.una
@@ -55,5 +55,7 @@
   let edit stock: Units = product.on_hand
-  for week in 1..weeks
+  let edit week = 1
+  while week <= weeks
     stock = sell_week(stock, sales: product.weekly_sales)
     stock = stock + order_for(product, stock: stock)
+    week = week + 1
   stock
Diagnostic
[U0103] Error: `while` is not a keyword in Una; use `for` or recursion

Why: Una has no while. Its loops walk a range or a list whose length is known before the first turn, so a reader sees where each one ends. The message names the two ways Una repeats work, for and recursion. Here the number of weeks is known before the loop starts, so for fits, and the name week and its assignment are not needed.

Fix: for week in 1..weeks, as in the program. See U0103.

Next:9.7 Exercises