Home / Docs / Guides

Editable values and loops

Release v0.4.0

Values stay immutable and there are no references; what changes is which value a binding holds (spec/17).

una
module demo

pub kind Tally
  count: Int
  total: Int

--- An `edit` parameter goes in and comes back out; `old(t)` is its value on entry.
pub record(edit t: Tally, amount: Int) -> Unit
  ensures t.count == old(t).count + 1
=
  t.count = t.count + 1
  t.total = t.total + amount

pub summarize(amounts: List[Int]) -> Tally
=
  let edit tally = Tally(count: 0, total: 0)
  for amount in amounts
    edit tally.record(amount)
  tally

pub sum_to(n: Nat) -> Int
=
  let edit total = 0
  for i in 1..n
    total = total + i
  total

--- `set` keeps the length, so every index of the range stays valid (ADR 0071).
pub zeroed(xs: List[Int]) -> List[Int]
  ensures result.len == xs.len
=
  let edit ys = xs
  for i in 0..<ys.len
    edit ys.set(index: i, item: 0)
  ys

pub cleared(xs: List[Int], index: Int) -> List[Int]
  requires 0 <= index and index < xs.len
  ensures result.len == xs.len
=
  let edit copy = xs
  edit copy.set(index: index, item: 0)
  copy
una
module tally

--- One vote counted. The key is in the map afterwards: a fact a loop would forget, which
--- each call of the helper keeps.
add_vote(edit counts: Map[Text, Int], vote: Text) -> Unit
  ensures counts.has(key: vote)
=
  let n = counts.get(key: vote)
    if none use 0
  edit counts.set(key: vote, value: n + 1)

--- The votes counted, one call of the helper per vote.
pub tally(votes: List[Text]) -> Map[Text, Int]
=
  let edit counts: Map[Text, Int] = empty_map()
  for vote in votes
    add_vote(edit counts, vote)
  counts

--- A vote counted on top of the others: its key is there, by the helper's contract.
pub with_vote(votes: List[Text], vote: Text) -> Map[Text, Int]
  ensures result.has(key: vote)
=
  let edit counts = tally(votes)
  add_vote(edit counts, vote)
  counts
una
module demo

pub kind Limit = | Full

pub kind Added
  items: List[Int]
  full: Bool

--- Fails when the list is full.
pub bounded(acc: List[Int], item: Int) -> List[Int]
  fails Limit
=
  if acc.len >= 100
    fail Full
  acc.append(item: item)

--- Quadratic: `if error use acc` reads `acc` after the call, so each call copies it.
pub kept(items: List[Int]) -> List[Int]
=
  let edit acc: List[Int] = []
  for item in items
    acc = bounded(acc, item: item)
      if error use acc
  acc

--- Linear: check first, then update.
pub checked_first(items: List[Int]) -> List[Int]
=
  let edit acc: List[Int] = []
  for item in items
    if acc.len < 100
      acc = acc.append(item: item)
  acc

--- Never fails: the new list, and whether it was full.
pub add(acc: List[Int], item: Int) -> Added
=
  if acc.len >= 100
    Added(items: acc, full: true)
  else
    Added(items: acc.append(item: item), full: false)

--- Linear: the update cannot fail, and returns its status with the new value.
pub with_status(items: List[Int]) -> List[Int]
=
  let edit acc: List[Int] = []
  for item in items
    let added = add(acc, item: item)
    acc = added.items
  acc

example "the three keep the first 100 items"
  do kept([1, 2, 3]) == checked_first([1, 2, 3]) and checked_first([1, 2, 3]) == with_status([1, 2, 3])
  expect result == true
una
-- fails with U2019
module demo

pub count_up(n: Int) -> Int
=
  let total = 0
  total = n
  total