A Tour of Temporal Typing Code, Contracts & Types · 6/6
Code, Contracts & Types · Page 6 of 6

Exercise: Conforming Vault

Let's put refined types, guards, invariants, and conformance together.

Vault holds a non-negative balance up to capacity = 4. Right now it has two bugs:

  1. deposit has no guard preventing balance from exceeding capacity (violating within_cap).
  2. withdraw returns amount - 1 instead of amount (violating Ensures and return-type Positive).
Exercise: Click Check to inspect the failure, then:
1. Add Requires(lambda s, amount: s.balance + amount <= s.capacity), to deposit.
2. Fix withdraw so it returns amount.
Click Check until you see PASS (5 states).
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE Vault ----
EXTENDS Integers
CONSTANT capacity
VARIABLE balance

ASSUME capacity > 0
Init == balance = 0
Deposit(a)  == a > 0 /\ balance + a <= capacity /\ balance' = balance + a
Withdraw(a) == a > 0 /\ a <= balance /\ balance' = balance - a
Next == \E a \in 1..3 : Deposit(a) \/ Withdraw(a)

WithinCap == balance <= capacity /\ balance >= 0
====