Array of list
This is a small program to illustrate the use of Why3's Peano numbers (see module mach.peano.Peano).
Authors: Jean-Christophe Filliâtre
Topics: Array Data Structure / List Data Structure
Tools: Why3
see also the index (by topic, by tool, by reference, by year)
Turning a list into an array.
This is a small program to illustrate the use of Why3's Peano numbers (see module mach.peano.Peano).
It turns a list into an array. The point is that we use machine integers (for the list length and for array indices) but we avoid having to prove the absence of arithmetic overflow.
Author: Jean-Christophe Filliâtre (CNRS)
use int.Int use option.Option use list.List use seq.Seq use mach.peano.Peano use mach.peano.Int63 use mach.int.Int63 use mach.array.Array63 as A use list.NthLength let rec length (l: list 'a) : Peano.t variant { l } ensures { result = length l } = match l with | Nil -> Peano.zero | Cons _ l -> Peano.succ (length l) end
The length of a list computed with Peano numbers.
let partial array_of_list (l: list 'a) : (a: A.array 'a) requires { l <> Nil } ensures { A.length a = length l } ensures { forall i. 0 <= i < length l -> Some a[i] = nth i l } = let n = to_int63 (length l) in match l with Nil -> absurd | Cons x ll -> let a = A.make n x in let rec fill (i: int63) (ll: list 'a) : unit requires { i >= 1 && n - i = length ll } requires { forall j. 0 <= j < i -> Some a[j] = nth j l } requires { forall j. i <= j < n -> nth (j-i) ll = nth j l } variant { n - i } ensures { forall j. 0 <= j < n -> Some a[j] = nth j l } = match ll with Nil -> () | Cons x ll -> A.(a[i] <- x); fill (i+1) ll end in fill 1 ll; return a end
To turn a list into an array, we first compute the length of the list with the function above, then we build an array of that size, and finally we fill it with the elements from the list.
Note: Array.make requires a value of type 'a, so the code below
requires a non-empty list. (In OCaml, we would return the empty
array [||] when the list is empty, but there is no such empty
array in Why3's library.)
download ZIP archive
Why3 Proof Results for Project "array_of_list"
Theory "array_of_list.Top": fully verified
| Obligations | CVC4 1.8 | Z3 4.8.10 | ||
| VC for length | --- | 0.08 | ||
| VC for array_of_list | --- | --- | ||
| split_vc | ||||
| unreachable point | --- | 0.02 | ||
| array creation size | --- | 0.04 | ||
| postcondition | --- | 0.03 | ||
| index in array bounds | --- | 0.04 | ||
| integer overflow | --- | 0.04 | ||
| variant decrease | --- | 0.04 | ||
| precondition | --- | 0.03 | ||
| precondition | 0.14 | --- | ||
| precondition | --- | --- | ||
| split_vc | ||||
| precondition | --- | 0.08 | ||
| postcondition | --- | 0.05 | ||
| precondition | --- | 0.05 | ||
| precondition | --- | 0.05 | ||
| precondition | --- | 0.04 | ||
| postcondition | --- | 0.04 | ||
| postcondition | --- | 0.05 | ||