|
| 1 | +import DumbContracts.Lang |
| 2 | +import DumbContracts.Semantics |
| 3 | +import DumbContracts.Stdlib |
| 4 | + |
| 5 | +namespace DumbContracts.Examples |
| 6 | + |
| 7 | +open DumbContracts.Lang |
| 8 | +open DumbContracts.Semantics |
| 9 | +open DumbContracts |
| 10 | +open DumbContracts.Std |
| 11 | + |
| 12 | +/-- Update a slot to the min of its current value and a new input. -/ |
| 13 | + |
| 14 | +def updateMinFun : Fun := |
| 15 | + { name := "updateMin" |
| 16 | + args := ["slot", "value"] |
| 17 | + body := |
| 18 | + letSload "current" (v "slot") |
| 19 | + (sstoreMin (v "slot") (v "current") (v "value")) |
| 20 | + ret := none } |
| 21 | + |
| 22 | +def updateMinSpec (slot value : Nat) : Spec Store := |
| 23 | + { requires := fun _ => True |
| 24 | + ensures := fun s s' => s' = updateStore s slot (if s slot < value then s slot else value) } |
| 25 | + |
| 26 | +theorem updateMin_meets_spec (s : Store) (slot value : Nat) : |
| 27 | + (updateMinSpec slot value).requires s -> |
| 28 | + (match execFun updateMinFun [slot, value] s [] with |
| 29 | + | ExecResult.ok _ s' => (updateMinSpec slot value).ensures s s' |
| 30 | + | _ => False) := by |
| 31 | + intro _hreq |
| 32 | + by_cases h : s slot < value |
| 33 | + · simp [updateMinFun, updateMinSpec, letSload, sstoreMin, v, execFun, execStmt, evalExpr, |
| 34 | + bindArgs, emptyEnv, updateEnv, updateStore, h] |
| 35 | + · simp [updateMinFun, updateMinSpec, letSload, sstoreMin, v, execFun, execStmt, evalExpr, |
| 36 | + bindArgs, emptyEnv, updateEnv, updateStore, h] |
| 37 | + |
| 38 | +end DumbContracts.Examples |
0 commit comments