1 (* Fibonacci function with memoisation *)
11 type table = map int (option int)
13 predicate inv (t : table) =
14 forall x y : int. t[x] = Some y -> y = fib x
18 val add (x:int) (y:int) : unit
20 ensures { !table = (old !table)[x <- Some y] }
24 val find (x:int) : int
25 ensures { !table[x] = Some result }
26 raises { Not_found -> !table[x] = None }
29 requires { 0 <= n /\ inv !table }
30 ensures { result = fib n /\ inv !table }
35 memo_fibo (n-1) + memo_fibo (n-2)
38 requires { 0 <= n /\ inv !table }
39 ensures { result = fib n /\ inv !table }
42 with Not_found -> let fn = fibo n in add n fn; fn end