Home / Learn / 18 Tools

Release v0.4.0Edition 0.2

18.8 Common mistakes

Page 9 of 11

Each mistake below is a small change to day-to-day/src/pricetags.una, followed by what una check . reports, how to read it, and which tool repairs it.

18.8.1 A layout of your own (U7001)

Put the body of the handler's operation on its own line:

src/pricetags.unadiff
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -50,3 +50,4 @@
 handler listed(products: Map[Text, Product]) for Catalog
-  op product(sku) = products.get(key: sku)
+  op product(sku) =
+    products.get(key: sku)
 
Diagnostic
[U7001] Error: lines 51 to 52 are not in canonical form
    ╭─[ src/pricetags.una:51:1 ]
    │
 51 │ ╭─▶   op product(sku) =
 52 │ ├─▶     products.get(key: sku)
    │ │
    │ ╰──────────────────────────────── lines 51 to 52 are not in canonical form
    │
    │     Help: repair 1: rewrite in canonical form, as `una fmt` does; edit src/pricetags.una:51:1..53:1 to "  op product(sku) = products.get(key: sku)\n"
    │
    │     Note: run `una explain U7001` for an explanation
────╯
1 of 1 diagnostics have an edit that `una fix` applies (`una fix --dry-run` lists them)

Why: canonical form is part of the language, so every package reads the same way, and a diff shows only real changes. The arrows mark lines 51 to 52 as one span out of form, and the Help line gives the exact text una fmt . puts there: the operation on one line. The last line says una fix can apply it.

Fix: una fmt ., or una fix ., which formats too. See U7001.

18.8.2 A missing label (U2004)

Leave out off: in the first example:

src/pricetags.unadiff
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -67,3 +67,3 @@
 example "a sale takes the percent off the price"
-  do sale_price(Product(sku: "LAMP-01", price: 1990, cost: 1200), off: 30)
+  do sale_price(Product(sku: "LAMP-01", price: 1990, cost: 1200), 30)
   expect result == 1393
Diagnostic
[U2004] Error: argument 2 of `sale_price` needs a label; only the first argument may go without one
    │ Help: repair 1: label it `off`; edit src/pricetags.una:68:67..68:69 to "off: 30"

Why: every argument after the first names its parameter, so a call reads the same without the header at hand. The message names the argument by its position, 2, and the repair names the label it needs, off, with the exact edit: from column 67 to 69 of line 68, the text 30 becomes off: 30.

Fix: una fix . applies the edit. una fix --dry-run . shows it first:

Terminal
$ una fix --dry-run .
fixed src/pricetags.una:68:67 U2004 label it `off`

See U2004.

18.8.3 A promise the code breaks (U5001)

Return product.price where the price falls below cost:

src/pricetags.unadiff
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -30,3 +30,3 @@
   let discounted = product.price - product.price * off / 100
-  if discounted < product.cost then product.cost else discounted
+  if discounted < product.cost then product.price else discounted
 
Diagnostic
[U5001] Error: postcondition of `sale_price` is refuted
    │ Note 2: counterexample: off = 0, product = Product#0, product.cost = 1, product.price = 0, result = 0

Why: a product can cost more than its price, as in the counterexample, and then the function returns a value below cost. Read the counterexample field by field: product = Product#0 is a placeholder for the record, and its fields that matter follow, a cost of 1 and a price of 0. With those values the discounted price, 0, is below cost, so the changed line returns product.price, 0, and result = 0 is not at least the cost of 1.

Fix: return product.cost on that path. una fix has nothing to apply: the repairs have no edit. See U5001.

18.8.4 A requirement that does not exist (U1006)

Mistype the requirement's name:

src/pricetags.unadiff
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -27,3 +27,3 @@
   ensures result >= product.cost
-  derived from PRICING_1
+  derived from PRICING_2
 =
Diagnostic
[U1006] Error: requirement `PRICING_2` is not declared in this package

Why: a derived from must name a requirement of the package, or una trace would follow a link to nothing. U1006 is in the names domain: a name that resolves to nothing, found at tier 0, before any proof.

Fix: correct the name, or declare the requirement. See U1006.

18.8.5 A capability that is not there (U3001)

Call tag in main without installing a catalog:

src/pricetags.unadiff
--- a/src/pricetags.una
+++ b/src/pricetags.una
@@ -63,3 +63,3 @@
   for sku in ["LAMP-01", "DESK-02", "CHAIR-09"]
-    let line = tag(sku, off: 30) with @catalog = listed(sample())
+    let line = tag(sku, off: 30)
     @console.print(line)
Diagnostic
[U3001] Error: `tag` needs `@catalog: Catalog[product]`, which is not in scope here
    │ Help: repair 1 (widens authority: the function then holds this authority, and every caller must hold it too: check that the operation belongs to this function's job): declare `uses @catalog: Catalog[product]` in the header; edit src/pricetags.una:61:32..61:32 to "\n  uses @catalog: Catalog[product]"

The repair is marked (widens authority: …): it would declare the capability in main's header, which gives main a power it does not have, so una fix does not apply it:

Terminal
$ una fix --dry-run .
skipped src/pricetags.una:61:32 U3001 declare `uses @catalog: Catalog[product]` in the header: widens authority

Why: tag needs a catalog, and main has none in scope. Even with the widening, main could not run: main may only do what the host grants it, and the catalog is the package's own capability, which no grant provides.

Fix: install a handler for the call with with @catalog = listed(sample()), as the program does. See U3001.

Next:18.9 Exercises