random_access_list.mlw 6.95 KB
Newer Older
1 2

(** Random Access Lists.
3
    (Okasaki, "Purely Functional Data Structures", 10.1.2.)
4 5 6 7

    The code below uses polymorphic recursion (both in the logic
    and in the programs).

8
    Author: Jean-Christophe Filliâtre (CNRS)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *)

module RandomAccessList

  use import int.Int
  use import int.ComputerDivision
  use import list.List
  use import list.Length
  use import list.Nth
  use import option.Option

  type ral 'a =
  | Empty
  | Zero    (ral ('a, 'a))
  | One  'a (ral ('a, 'a))

25
  function flatten (l: list ('a, 'a)) : list 'a
26 27 28 29 30
  = match l with
    | Nil -> Nil
    | Cons (x, y) l1 -> Cons x (Cons y (flatten l1))
    end

31
  let rec lemma length_flatten (l:list ('a, 'a))
32 33 34 35 36 37 38
    ensures { length (flatten l) = 2 * length l }
    variant { l }
  = match l with
    | Cons (_,_) q -> length_flatten q
    | Nil -> ()
    end

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  function elements (l: ral 'a) : list 'a
    = match l with
      | Empty    -> Nil
      | Zero l1  -> flatten (elements l1)
      | One x l1 -> Cons x (flatten (elements l1))
      end

  let rec size (l: ral 'a) : int
    variant { l }
    ensures { result = length (elements l) }
  =
    match l with
    | Empty    -> 0
    | Zero  l1 ->     2 * size l1
    | One _ l1 -> 1 + 2 * size l1
    end

  let rec add (x: 'a) (l: ral 'a) : ral 'a
    variant { l }
    ensures { elements result = Cons x (elements l) }
    = match l with
      | Empty    -> One x Empty
      | Zero l1  -> One x l1
      | One y l1 -> Zero (add (x, y) l1)
      end

  let rec lemma nth_flatten (i: int) (l: list ('a, 'a))
    requires { 0 <= i < length l }
    variant  { l }
    ensures  { match nth i l with
               | None -> false
               | Some (x0, x1) -> Some x0 = nth (2 * i)     (flatten l) /\
                                  Some x1 = nth (2 * i + 1) (flatten l) end }
  = match l with
    | Nil -> ()
    | Cons _ r -> if i > 0 then nth_flatten (i-1) r
    end

77

78 79 80 81 82 83 84 85 86 87 88
  let rec get (i: int) (l: ral 'a) : 'a
    requires { 0 <= i < length (elements l) }
    variant  { i, l }
    ensures  { nth i (elements l) = Some result }
    = match l with
      | Empty    -> absurd
      | One x l1 -> if i = 0 then x else get (i-1) (Zero l1)
      | Zero l1  -> let (x0, x1) = get (div i 2) l1 in
                    if mod i 2 = 0 then x0 else x1
      end

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

   let rec tail (l: ral 'a) : ral 'a
    requires { elements l <> Nil }
    variant { l }
    ensures {let m = elements l in
             match nth 0 m with
             | None -> false
             | Some x -> m = Cons x (elements result)
             end }
    = match l with
      | Empty    -> absurd
      | One _ l1 -> Zero l1
      | Zero  l1 -> let (_, x1) = get 0 l1 in One x1 (tail l1)
      end

  let rec set (y: 'a) (i: int) (l: ral 'a) : ral 'a
    requires { 0 <= i < length (elements l) }
    variant  { i, l}
    ensures  { nth i (elements result) = Some y}
    ensures  { forall j. 0 <= j < length (elements l) ->
               j <> i -> nth j (elements result) = nth j (elements l) }
    ensures  { length (elements result) = length (elements l) }
    ensures  { match result, l with
               | One _ _, One _ _ | Zero _, Zero _ -> true
               | _                                 -> false
               end }
    = match l with
      | Empty    -> absurd
      | One x l1 -> if i = 0 then One y l1 else
                    match set y (i-1) (Zero l1) with
                    | Empty | One _ _ -> absurd
                    | Zero l1         -> One x l1
                    end
      | Zero l1  ->
          let (x0, x1) = get (div i 2) l1 in
          let l1' = set (if mod i 2 = 0 then (y,x1) else (x0,y)) (div i 2) l1 in
            assert { forall j. 0 <= j < length (elements l) -> j <> i ->
               match nth (div j 2) (elements l1) with
               | None -> false
               | Some (x0,_) -> Some x0 = nth (2 * (div j 2)) (elements l)
               end
               && nth j (elements l) = nth j (elements (Zero l1')) };
          Zero l1'
      end

134
end
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

(** A straightforward encapsulation with a list ghost model
    (in anticipation of module refinement) *)

module RAL

  use import int.Int
  use import RandomAccessList as R
  use import list.List
  use import list.Length
  use import option.Option
  use import list.Nth

  type t 'a = { r: ral 'a; ghost l: list 'a }
    invariant { self.l = elements self.r }

  let empty () : t 'a
    ensures { result.l = Nil }
  =
    { r = Empty; l = Nil }

  let size (t: t 'a) : int
    ensures { result = length t.l }
  =
    size t.r

  let cons (x: 'a) (s: t 'a) : t 'a
    ensures { result.l = Cons x s.l }
  =
    { r = add x s.r; l = Cons x s.l }

  let get (i: int) (s: t 'a) : 'a
    requires { 0 <= i < length s.l }
    ensures { Some result = nth i s.l }
  =
    get i s.r

end

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
module RandomAccessListWithSeq

  use import int.Int
  use import int.ComputerDivision
  use import seq.Seq

  type ral 'a =
  | Empty
  | Zero    (ral ('a, 'a))
  | One  'a (ral ('a, 'a))

  function flatten (s: seq ('a, 'a)) : seq 'a
    = create (2 * length s)
             (\ i: int. let (x0, x1) = s[div i 2] in
                        if mod i 2 = 0 then x0 else x1)

  function elements (l: ral 'a) : seq 'a
    = match l with
      | Empty    -> empty
      | Zero l1  -> flatten (elements l1)
      | One x l1 -> cons x (flatten (elements l1))
      end

  let rec size (l: ral 'a) : int
    variant { l }
    ensures { result = length (elements l) }
  =
    match l with
    | Empty    -> 0
    | Zero  l1 ->     2 * size l1
    | One _ l1 -> 1 + 2 * size l1
    end

  let rec add (x: 'a) (l: ral 'a) : ral 'a
    variant { l }
    ensures { elements result == cons x (elements l) }
    = match l with
      | Empty    -> One x Empty
      | Zero l1  -> One x l1
      | One y l1 -> Zero (add (x, y) l1)
      end

216
   let rec get (i: int) (l: ral 'a) : 'a
217 218 219 220 221 222 223 224 225 226
    requires { 0 <= i < length (elements l) }
    variant  { i, l }
    ensures  { (elements l)[i] = result }
    = match l with
      | Empty    -> absurd
      | One x l1 -> if i = 0 then x else get (i-1) (Zero l1)
      | Zero l1  -> let (x0, x1) = get (div i 2) l1 in
                    if mod i 2 = 0 then x0 else x1
      end

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  let rec tail (l: ral 'a) : ral 'a
    requires { not ((elements l) == empty) }
    variant { l }
    ensures { (elements l) == cons (elements l)[0] (elements result) }
    = match l with
      | Empty    -> absurd
      | One _ l1 -> Zero l1
      | Zero  l1 -> let (_, x1) = get 0 l1 in One x1 (tail l1)
      end

  let rec set (y: 'a) (i: int) (l: ral 'a) : ral 'a
    requires { 0 <= i < length (elements l) }
    variant  { i, l}
    ensures  { elements result == set (elements l) i y}
    = match l with
      | Empty    -> absurd
      | One x l1 -> if i = 0 then One y l1 else
                    match set y (i-1) (Zero l1) with
                    | Empty | One _ _ -> absurd
                    | Zero l1 -> One x l1
                    end
      | Zero l1  -> let (x0, x1) = get (div i 2) l1 in
                    Zero
                    (set (if mod i 2 = 0 then (y,x1) else (x0,y)) (div i 2) l1)
      end

253
end