pairing_heap.mlw 7.12 KB
 Mario Pereira committed Aug 31, 2016 1 2 3 4 5 6 7 8 9 `````` (** Pairing heaps (M. Fredman, R. Sedgewick, D. Sleator, R. Tarjan, 1986). Purely applicative implementation, following Okasaki's implementation in his book "Purely Functional Data Structures" (Section 5.5). Author: Mário Pereira (Université Paris Sud) *) `````` 10 11 ``````module Heap `````` Andrei Paskevich committed Jun 15, 2018 12 `````` use int.Int `````` 13 14 `````` type elt `````` Leon Gondelman committed Feb 11, 2017 15 `````` val predicate le elt elt `````` 16 `````` `````` Andrei Paskevich committed Jun 15, 2018 17 `````` clone relations.TotalPreOrder with `````` Andrei Paskevich committed Jun 14, 2018 18 `````` type t = elt, predicate rel = le, axiom . `````` 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 `````` type heap function size heap : int function occ elt heap : int predicate mem (x: elt) (h: heap) = occ x h > 0 function minimum heap : elt predicate is_minimum (x: elt) (h: heap) = mem x h && forall e. mem e h -> le x e axiom min_def: forall h. 0 < size h -> is_minimum (minimum h) h val empty () : heap ensures { size result = 0 } ensures { forall x. occ x result = 0 } val is_empty (h: heap) : bool ensures { result <-> size h = 0 } val size (h: heap) : int ensures { result = size h } val merge (h1 h2: heap) : heap ensures { forall x. occ x result = occ x h1 + occ x h2 } ensures { size result = size h1 + size h2 } val insert (x: elt) (h: heap) : heap ensures { occ x result = occ x h + 1 } ensures { forall y. y <> x -> occ y h = occ y result } ensures { size result = size h + 1 } val find_min (h: heap) : elt requires { size h > 0 } ensures { result = minimum h } val delete_min (h: heap) : heap requires { size h > 0 } ensures { let x = minimum h in occ x result = occ x h - 1 } ensures { forall y. y <> minimum h -> occ y result = occ y h } ensures { size result = size h - 1 } end module HeapType `````` Andrei Paskevich committed Jun 15, 2018 69 `````` use list.List `````` 70 71 72 73 74 75 76 77 `````` type elt type heap = E | T elt (list heap) end module Size `````` Andrei Paskevich committed Jun 15, 2018 78 79 80 `````` use HeapType use int.Int use list.List `````` 81 82 83 84 85 86 87 88 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 `````` function size (h: heap) : int = match h with | E -> 0 | T _ l -> 1 + size_list l end with size_list (l: list heap) : int = match l with | Nil -> 0 | Cons h r -> size h + size_list r end let rec lemma size_nonneg (h: heap) ensures { size h >= 0 } variant { h } = match h with | E -> () | T _ l -> size_list_nonneg l end with size_list_nonneg (l: list heap) ensures { size_list l >= 0 } variant { l } = match l with | Nil -> () | Cons h r -> size_nonneg h; size_list_nonneg r end let lemma size_empty (h: heap) ensures { size h = 0 <-> h = E } = match h with | E -> () | T _ l -> size_list_nonneg l end end module Occ `````` Andrei Paskevich committed Jun 15, 2018 119 120 121 `````` use HeapType use int.Int use list.List `````` 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 `````` function occ (x: elt) (h: heap) : int = match h with | E -> 0 | T e l -> (if x = e then 1 else 0) + occ_list x l end with occ_list (x: elt) (l: list heap) : int = match l with | Nil -> 0 | Cons h r -> occ x h + occ_list x r end let rec lemma occ_nonneg (x: elt) (h: heap) ensures { occ x h >= 0 } variant { h } = match h with | E -> () | T _ l -> occ_list_nonneg x l end with occ_list_nonneg (x: elt) (l: list heap) ensures { occ_list x l >= 0 } variant { l } = match l with | Nil -> () | Cons h r -> occ_nonneg x h; occ_list_nonneg x r end predicate mem (x: elt) (h: heap) = 0 < occ x h predicate mem_list (x: elt) (l: list heap) = 0 < occ_list x l end module PairingHeap `````` Andrei Paskevich committed Jun 15, 2018 158 `````` use int.Int `````` 159 160 161 `````` use export HeapType use export Size use export Occ `````` Andrei Paskevich committed Jun 15, 2018 162 163 `````` use list.List use list.Length `````` 164 `````` `````` Leon Gondelman committed Feb 11, 2017 165 `````` val predicate le elt elt `````` Andrei Paskevich committed Jun 15, 2018 166 `````` clone relations.TotalPreOrder with `````` Andrei Paskevich committed Jun 14, 2018 167 `````` type t = elt, predicate rel = le, axiom . `````` 168 169 170 171 172 173 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 `````` (* [e] is no greater than the root of [h], if any *) predicate le_root (e: elt) (h: heap) = match h with | E -> true | T x _ -> le e x end lemma le_root_trans: forall x y h. le x y -> le_root y h -> le_root x h (* [e] is no greater than the roots of the trees in [l] *) predicate le_roots (e: elt) (l: list heap) = match l with | Nil -> true | Cons h r -> le_root e h && le_roots e r end lemma le_roots_trans: forall x y l. le x y -> le_roots y l -> le_roots x l predicate no_middle_empty (h: heap) = match h with | E -> true | T _ l -> no_middle_empty_list l end with no_middle_empty_list (l: list heap) = match l with | Nil -> true | Cons h r -> h <> E && no_middle_empty h && no_middle_empty_list r end predicate heap (h: heap) = match h with | E -> true | T x l -> le_roots x l && heaps l end with heaps (l: list heap) = match l with | Nil -> true | Cons h r -> heap h && heaps r end predicate inv (h: heap) = heap h && no_middle_empty h let rec lemma heap_mem (h: heap) requires { heap h } variant { h } ensures { forall x. le_root x h -> forall y. mem y h -> le x y } = match h with | E -> () | T _ l -> heap_mem_list l end with heap_mem_list (l: list heap) requires { heaps l } variant { l } ensures { forall x. le_roots x l -> forall y. mem_list y l -> le x y } = match l with | Nil -> () | Cons h r -> heap_mem h; heap_mem_list r end predicate is_minimum (x: elt) (h: heap) = mem x h && forall e. mem e h -> le x e function minimum heap : elt axiom minimum_def: forall x l. minimum (T x l) = x let lemma root_is_minimum (h: heap) requires { 0 < size h } requires { heap h } ensures { is_minimum (minimum h) h } = match h with | E -> absurd | T x l -> occ_list_nonneg x l end let empty () : heap ensures { inv result } ensures { size result = 0 } ensures { forall e. not (mem e result) } = E let is_empty (h: heap) : bool ensures { result <-> size h = 0 } `````` Leon Gondelman committed Feb 11, 2017 250 `````` = match h with E -> true | _ -> false end `````` 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 `````` let merge (h1 h2: heap) : heap requires { inv h1 && inv h2 } ensures { inv result } ensures { size result = size h1 + size h2 } ensures { forall x. occ x result = occ x h1 + occ x h2 } = match h1, h2 with | E, h | h, E -> h | T x1 l1, T x2 l2 -> if le x1 x2 then T x1 (Cons h2 l1) else T x2 (Cons h1 l2) end let insert (x: elt) (h: heap) : heap requires { inv h } ensures { inv result } ensures { size result = size h + 1 } ensures { occ x result = occ x h + 1 } ensures { forall y. x <> y -> occ y result = occ y h } = merge (T x Nil) h let find_min (h: heap) : elt requires { 0 < size h } requires { inv h } ensures { result = minimum h } = match h with | E -> absurd | T x _ -> x end let rec merge_pairs (l: list heap) : heap requires { heaps l && no_middle_empty_list l } ensures { inv result } ensures { size result = size_list l } ensures { forall x. occ x result = occ_list x l } variant { length l } = match l with | Nil -> E | Cons h Nil -> assert { h <> E }; h | Cons h1 (Cons h2 r) -> merge (merge h1 h2) (merge_pairs r) end let delete_min (h: heap) : heap requires { inv h } requires { 0 < size h } ensures { inv result } ensures { occ (minimum h) result = occ (minimum h) h - 1 } ensures { forall y. y <> minimum h -> occ y result = occ y h } ensures { size result = size h - 1 } = match h with | E -> absurd | T _ l -> merge_pairs l end `````` Andrei Paskevich committed Jun 14, 2018 309 ``end``