patience.mlw 27.4 KB
 MARCHE Claude committed Feb 26, 2016 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 `````` (** {1 The Patience Solitaire Game} Problem 1 from the {h Verified Software Competition 2014} Patience Solitaire is played by taking cards one-by-one from a deck of cards and arranging them face up in a sequence of stacks arranged from left to right as follows. The very first card from the deck is kept face up to form a singleton stack. Each subsequent card is placed on the leftmost stack where its card value is no greater than the topmost card on that stack. If there is no such stack, then a new stack is started to right of the other stacks. We can do this with positive numbers instead of cards. If the input sequence is 9, 7, 10, 9, 5, 4, and 10, then the stacks develop as {h
Guillaume Melquiond committed Jun 25, 2018  19     20     21     22     23     24     25                                                                                                                                                                                                                                                                                                                                                 ``````<[[9]]> <[[7, 9]]> <[[7, 9]], [[10]]> <[[7, 9]], [[9, 10]]> <[[5, 7, 9]], [[9, 10]]> <[[4, 5, 7, 9]], [[9, 10]]> <[[4, 5, 7, 9]], [[9, 10]], [[10]]> ``````
MARCHE Claude committed Feb 26, 2016        26     27     28     29     30     31     32     33     34     35     36     37     38     39     40     41     42                                                                                                                                                                                                                                                                           ``````{h
} Verify the claim is that the number of stacks at the end of the game is the length of the longest (strictly) increasing subsequence in the input sequence. *) (** {2 Preliminary: pigeon-hole lemma} *) module PigeonHole (** The Why standard library provides a lemma `````` Guillaume Melquiond committed Jun 14, 2018 43 44 `````` `map.MapInjection.injective_surjective` stating that a map from `(0..n-1)` to `(0..n-1)` that is an injection is also a `````` MARCHE Claude committed Feb 26, 2016 45 46 47 48 49 50 51 52 `````` surjection. This is more or less equivalent to the pigeon-hole lemma. However, we need such a lemma more generally on functions instead of maps. Thus we restate the pigeon-hole lemma here. Proof is left as an exercise. *) `````` Andrei Paskevich committed Jun 15, 2018 53 `````` use int.Int `````` MARCHE Claude committed Feb 26, 2016 54 55 56 `````` predicate range (f: int -> int) (n: int) (m:int) = forall i: int. 0 <= i < n -> 0 <= f i < m `````` Guillaume Melquiond committed Jun 14, 2018 57 58 `````` (** `range f n m` is true when `f` maps the domain `(0..n-1)` into `(0..m-1)` *) `````` MARCHE Claude committed Feb 26, 2016 59 60 61 `````` predicate injective (f: int -> int) (n: int) (m:int) = forall i j: int. 0 <= i < j < n -> f i <> f j `````` Guillaume Melquiond committed Jun 14, 2018 62 63 `````` (** `injective f n m` is true when `f` is an injection from `(0..n-1)` to `(0..m-1)` *) `````` MARCHE Claude committed Feb 26, 2016 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 `````` (* lemma pigeon_hole2: forall n m:int, f: int -> int. range f n m /\ n > m >= 0 -> not (injective f n m) *) exception Found function shift (f: int -> int) (i:int) : int -> int = `````` Guillaume Melquiond committed Mar 16, 2016 81 `````` fun k -> if k < i then f k else f (k+1) `````` MARCHE Claude committed Feb 26, 2016 82 83 84 85 86 87 `````` let rec lemma pigeon_hole (n m:int) (f: int -> int) requires { range f n m } requires { n > m >= 0 } variant { m } ensures { not (injective f n m) } `````` Andrei Paskevich committed Jun 06, 2017 88 `````` = `````` MARCHE Claude committed Feb 26, 2016 89 90 `````` for i = 0 to n-1 do invariant { forall k. 0 <= k < i -> f k <> m-1 } `````` Andrei Paskevich committed Jun 06, 2017 91 `````` if f i = m-1 then begin `````` MARCHE Claude committed Feb 26, 2016 92 93 94 `````` (* we have found index i such that f i = m-1 *) for j = i+1 to n-1 do invariant { forall k. i < k < j -> f k <> m-1 } `````` Andrei Paskevich committed Jun 06, 2017 95 96 `````` (* we know that f i = f j = m-1 hence we are done *) if f j = m-1 then return `````` MARCHE Claude committed Feb 26, 2016 97 98 `````` done; (* we know that for all k <> i, f k <> m-1 *) `````` Andrei Paskevich committed Jun 10, 2017 99 `````` let g = shift f i in `````` MARCHE Claude committed Feb 26, 2016 100 101 `````` assert { range g (n-1) (m-1) }; pigeon_hole (n-1) (m-1) g; `````` Andrei Paskevich committed Jun 06, 2017 102 103 `````` return end `````` MARCHE Claude committed Feb 26, 2016 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 `````` done; (* we know that for all k, f k <> m-1 *) assert { range f n (m-1) }; pigeon_hole n (m-1) f end (** {2 Patience idiomatic code} *) module PatienceCode `````` Andrei Paskevich committed Jun 15, 2018 119 120 121 `````` use int.Int use list.List use list.RevAppend `````` MARCHE Claude committed Feb 26, 2016 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 `````` (** this code was the one written initially, without any specification, except for termination, ans unreachability of the 'absurd' branch'. It can be tested, see below. *) type card = int (** stacks are well-formed if they are non-empty *) predicate wf_stacks (stacks: list (list card)) = match stacks with | Nil -> true | Cons Nil _ -> false | Cons (Cons _ _) rem -> wf_stacks rem end (** concatenation of well-formed stacks is well-formed *) let rec lemma wf_rev_append_stacks (s1 s2: list (list int)) requires { wf_stacks s1 } requires { wf_stacks s2 } variant { s1 } ensures { wf_stacks (rev_append s1 s2) } = match s1 with | Nil -> () | Cons Nil _ -> absurd | Cons s rem -> wf_rev_append_stacks rem (Cons s s2) end `````` Guillaume Melquiond committed Jun 14, 2018 151 152 153 `````` (** `push_card c stacks acc` pushes card `c` on stacks `stacks`, assuming `acc` is an accumulator (in reverse order) of stacks where `c` could not be pushed. `````` MARCHE Claude committed Feb 26, 2016 154 155 156 157 158 159 160 161 162 163 `````` *) let rec push_card (c:card) (stacks : list (list card)) (acc : list (list card)) : list (list card) requires { wf_stacks stacks } requires { wf_stacks acc } variant { stacks } ensures { wf_stacks result } = match stacks with | Nil -> `````` Guillaume Melquiond committed Jun 14, 2018 164 `````` (* we put card `c` in a new stack *) `````` MARCHE Claude committed Feb 26, 2016 165 166 167 `````` rev_append (Cons (Cons c Nil) acc) Nil | Cons stack remaining_stacks -> match stack with `````` Guillaume Melquiond committed Jun 14, 2018 168 `````` | Nil -> absurd (* because `wf_stacks stacks` *) `````` MARCHE Claude committed Feb 26, 2016 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 `````` | Cons c' _ -> if c <= c' then (* card is placed on the leftmost stack where its card value is no greater than the topmost card on that stack *) rev_append (Cons (Cons c stack) acc) remaining_stacks else (* try next stack *) push_card c remaining_stacks (Cons stack acc) end end let rec play_cards (input: list card) (stacks: list (list card)) : list (list card) requires { wf_stacks stacks } variant { input } ensures { wf_stacks result } = match input with | Nil -> stacks | Cons c rem -> let stacks' = push_card c stacks Nil in play_cards rem stacks' end let play_game (input: list card) : list (list card) = play_cards input Nil `````` Guillaume Melquiond committed Jun 14, 2018 199 `````` (** test, can be run using `why3 patience.mlw --exec PatienceCode.test` *) `````` MARCHE Claude committed Feb 26, 2016 200 201 202 203 204 205 206 207 208 209 210 211 212 `````` let test () = (** the list given in the problem description 9, 7, 10, 9, 5, 4, and 10 *) play_game (Cons 9 (Cons 7 (Cons 10 (Cons 9 (Cons 5 (Cons 4 (Cons 10 Nil))))))) end (** {2 Abstract version of Patience game} *) module PatienceAbstract `````` Andrei Paskevich committed Jun 15, 2018 213 `````` use int.Int `````` MARCHE Claude committed Feb 26, 2016 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 `````` (** To specify the expected property of the Patience game, we first provide an abstract version, working on a abstract state that includes a lot of information regarding the positions of the cards in the stack and so on. This abstract state should then be including in the real code as a ghost state, with a gluing invariant that matches the ghost state and the concrete stacks of cards. *) type card = int (** {3 Abstract state} *) `````` Andrei Paskevich committed Jun 15, 2018 231 232 `````` use map.Map use map.Const `````` MARCHE Claude committed Feb 26, 2016 233 234 `````` type state = { `````` MARCHE Claude committed Mar 24, 2016 235 236 237 `````` ghost mutable num_stacks : int; (** number of stacks built so far *) ghost mutable num_elts : int; `````` MARCHE Claude committed Feb 26, 2016 238 `````` (** number of cards already seen *) `````` MARCHE Claude committed Mar 24, 2016 239 `````` ghost mutable values : map int card; `````` MARCHE Claude committed Feb 26, 2016 240 `````` (** cards values seen, indexed in the order they have been seen, `````` Guillaume Melquiond committed Jun 14, 2018 241 `````` from `0` to `num_elts-1` *) `````` MARCHE Claude committed Mar 24, 2016 242 `````` ghost mutable stack_sizes : map int int; `````` Guillaume Melquiond committed Jun 14, 2018 243 `````` (** sizes of these stacks, numbered from `0` to `num_stacks - 1` *) `````` MARCHE Claude committed Mar 24, 2016 244 `````` ghost mutable stacks : map int (map int int); `````` MARCHE Claude committed Feb 26, 2016 245 `````` (** indexes of the cards in respective stacks *) `````` MARCHE Claude committed Mar 24, 2016 246 `````` ghost mutable positions : map int (int,int); `````` MARCHE Claude committed Feb 26, 2016 247 248 `````` (** table that given a card index, provides its position, i.e. in which stack it is and at which height *) `````` MARCHE Claude committed Mar 24, 2016 249 `````` ghost mutable preds : map int int; `````` Guillaume Melquiond committed Jun 14, 2018 250 `````` (** predecessors of cards, i.e. for each card index `i`, `preds[i]` `````` MARCHE Claude committed Feb 26, 2016 251 `````` provides an index of a card in the stack on the immediate left, `````` Guillaume Melquiond committed Jun 14, 2018 252 `````` whose value is smaller. Defaults to `-1` if the card is on the `````` MARCHE Claude committed Feb 26, 2016 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 `````` leftmost stack. *) } (** {3 Invariants on the abstract state} *) predicate inv (s:state) = 0 <= s.num_stacks <= s.num_elts (** the number of stacks is less or equal the number of cards *) /\ (s.num_elts > 0 -> s.num_stacks > 0) (** when there is at least one card, there is at least one stack *) /\ (forall i. 0 <= i < s.num_stacks -> s.stack_sizes[i] >= 1 (** stacks are non-empty *) /\ forall j. 0 <= j < s.stack_sizes[i] -> 0 <= s.stacks[i][j] < s.num_elts) (** contents of stacks are valid card indexes *) /\ (forall i. 0 <= i < s.num_elts -> `````` Andrei Paskevich committed Jun 11, 2017 270 `````` let is,ip = s.positions[i] in `````` MARCHE Claude committed Feb 26, 2016 271 272 273 274 275 `````` 0 <= is < s.num_stacks && let st = s.stacks[is] in 0 <= ip < s.stack_sizes[is] && st[ip] = i) (** the position table of cards is correct, i.e. when `````` Guillaume Melquiond committed Jun 14, 2018 276 277 `````` `(is,ip) = s.positions[i]` then card `i` indeed occurs in stack `is` at height `ip` *) `````` MARCHE Claude committed Feb 26, 2016 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 `````` /\ (forall is. 0 <= is < s.num_stacks -> forall ip. 0 <= ip < s.stack_sizes[is] -> let idx = s.stacks[is][ip] in (is,ip) = s.positions[idx]) (** positions is the proper inverse of stacks *) /\ (forall i. 0 <= i < s.num_stacks -> let stack_i = s.stacks[i] in forall j,k. 0 <= j < k < s.stack_sizes[i] -> stack_i[j] < stack_i[k]) (** in a given stack, indexes are increasing from bottom to top *) /\ (forall i. 0 <= i < s.num_stacks -> let stack_i = s.stacks[i] in forall j,k. 0 <= j <= k < s.stack_sizes[i] -> s.values[stack_i[j]] >= s.values[stack_i[k]]) (** in a given stack, card values are decreasing from bottom to top *) /\ (forall i. 0 <= i < s.num_elts -> let pred = s.preds[i] in -1 <= pred < s.num_elts && `````` Guillaume Melquiond committed Jun 14, 2018 296 `````` (** the predecessor is a valid index or `-1` *) `````` MARCHE Claude committed Feb 26, 2016 297 298 `````` pred < i /\ (** predecessor is always a smaller index *) `````` Andrei Paskevich committed Jun 11, 2017 299 `````` let is,_ip = s.positions[i] in `````` MARCHE Claude committed Feb 26, 2016 300 `````` if pred < 0 then is = 0 `````` Guillaume Melquiond committed Jun 14, 2018 301 `````` (** if predecessor is `-1` then `i` is in leftmost stack *) `````` MARCHE Claude committed Feb 26, 2016 302 303 `````` else s.values[pred] < s.values[i] /\ `````` Guillaume Melquiond committed Jun 14, 2018 304 `````` (** if predecessor is not `-1`, it denotes a card with smaller value... *) `````` MARCHE Claude committed Feb 26, 2016 305 306 `````` is > 0 && (** ...the card is not on the leftmost stack... *) `````` Andrei Paskevich committed Jun 11, 2017 307 `````` let ps,_pp = s.positions[pred] in `````` MARCHE Claude committed Feb 26, 2016 308 309 310 311 312 313 314 `````` ps = is - 1) (** ...and predecessor is in the stack on the immediate left *) (** {2 Programs} *) `````` Andrei Paskevich committed Jun 15, 2018 315 `````` use ref.Ref `````` MARCHE Claude committed Feb 26, 2016 316 317 `````` exception Return int `````` Guillaume Melquiond committed Jun 14, 2018 318 `````` (** `play_card c i s` pushes the card `c` on state `s` *) `````` MARCHE Claude committed Mar 24, 2016 319 `````` let ghost play_card (c:card) (s:state) : unit `````` MARCHE Claude committed Feb 26, 2016 320 321 322 323 324 325 `````` requires { inv s } writes { s } ensures { inv s } ensures { s.num_elts = (old s).num_elts + 1 } ensures { s.values = (old s).values[(old s).num_elts <- c] } = `````` MARCHE Claude committed Mar 24, 2016 326 `````` let ghost pred = ref (-1) in `````` Andrei Paskevich committed Jun 06, 2017 327 `````` try `````` MARCHE Claude committed Feb 26, 2016 328 329 330 331 332 333 334 335 `````` for i = 0 to s.num_stacks - 1 do invariant { if i=0 then !pred = -1 else let stack_im1 = s.stacks[i-1] in let stack_im1_size = s.stack_sizes[i-1] in let top_stack_im1 = stack_im1[stack_im1_size - 1] in !pred = top_stack_im1 /\ c > s.values[!pred] /\ 0 <= !pred < s.num_elts /\ `````` Andrei Paskevich committed Jun 11, 2017 336 `````` let ps,_pp = s.positions[!pred] in `````` MARCHE Claude committed Feb 26, 2016 337 338 339 340 341 `````` ps = i - 1 } let stack_i = s.stacks[i] in let stack_i_size = s.stack_sizes[i] in let top_stack_i = stack_i[stack_i_size - 1] in `````` Andrei Paskevich committed Jun 06, 2017 342 343 `````` if c <= s.values[top_stack_i] then raise (Return i); assert { 0 <= top_stack_i < s.num_elts }; `````` Andrei Paskevich committed Jun 11, 2017 344 `````` assert { let is,ip = s.positions[top_stack_i] in `````` Andrei Paskevich committed Jun 06, 2017 345 346 347 348 349 350 `````` 0 <= is < s.num_stacks && 0 <= ip < s.stack_sizes[is] && s.stacks[is][ip] = top_stack_i && is = i /\ ip = stack_i_size - 1 }; pred := top_stack_i `````` MARCHE Claude committed Feb 26, 2016 351 352 353 354 355 356 357 358 359 360 361 `````` done; (* we add a new stack *) let idx = s.num_elts in let i = s.num_stacks in let stack_i = s.stacks[i] in let new_stack_i = stack_i[0 <- idx] in s.num_elts <- idx + 1; s.values <- s.values[idx <- c]; s.num_stacks <- s.num_stacks + 1; s.stack_sizes <- s.stack_sizes[i <- 1]; s.stacks <- s.stacks[i <- new_stack_i]; `````` Andrei Paskevich committed Jun 11, 2017 362 `````` s.positions <- s.positions[idx <- i,0]; `````` MARCHE Claude committed Feb 26, 2016 363 364 365 366 367 368 369 370 371 372 373 374 `````` s.preds <- s.preds[idx <- !pred] with Return i -> let stack_i = s.stacks[i] in let stack_i_size = s.stack_sizes[i] in (* we put c on top of stack i *) let idx = s.num_elts in let new_stack_i = stack_i[stack_i_size <- idx] in s.num_elts <- idx + 1; s.values <- s.values[idx <- c]; (* s.num_stacks unchanged *) s.stack_sizes <- s.stack_sizes[i <- stack_i_size + 1]; s.stacks <- s.stacks[i <- new_stack_i]; `````` Andrei Paskevich committed Jun 11, 2017 375 `````` s.positions <- s.positions[idx <- i,stack_i_size]; `````` MARCHE Claude committed Feb 26, 2016 376 377 378 379 380 381 382 `````` s.preds <- s.preds[idx <- !pred]; end `````` Andrei Paskevich committed Jun 15, 2018 383 384 385 `````` use list.List use list.Length use list.NthNoOpt `````` MARCHE Claude committed Feb 26, 2016 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 `````` let rec play_cards (input: list int) (s: state) : unit requires { inv s } variant { input } writes { s } ensures { inv s } ensures { s.num_elts = (old s).num_elts + length input } ensures { forall i. 0 <= i < (old s).num_elts -> s.values[i] = (old s).values[i] } ensures { forall i. (old s).num_elts <= i < s.num_elts -> s.values[i] = nth (i - (old s).num_elts) input } = match input with | Nil -> () | Cons c rem -> play_card c s; play_cards rem s end type seq 'a = { seqlen: int; seqval: map int 'a } predicate increasing_subsequence (s:seq int) (l:list int) = 0 <= s.seqlen <= length l && (* subsequence *) ((forall i. 0 <= i < s.seqlen -> 0 <= s.seqval[i] < length l) /\ (forall i,j. 0 <= i < j < s.seqlen -> s.seqval[i] < s.seqval[j])) (* increasing *) && (forall i,j. 0 <= i < j < s.seqlen -> nth s.seqval[i] l < nth s.seqval[j] l) `````` Andrei Paskevich committed Jun 15, 2018 427 `````` use PigeonHole `````` MARCHE Claude committed Feb 26, 2016 428 429 430 431 432 433 434 435 `````` `````` MARCHE Claude committed Mar 24, 2016 436 `````` let ghost play_game (input: list int) : state `````` MARCHE Claude committed Feb 26, 2016 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 `````` ensures { exists s: seq int. s.seqlen = result.num_stacks /\ increasing_subsequence s input } ensures { forall s: seq int. increasing_subsequence s input -> s.seqlen <= result.num_stacks } = let s = { num_elts = 0; values = Const.const (-1) ; num_stacks = 0; stack_sizes = Const.const 0; stacks = Const.const (Const.const (-1)); positions = Const.const (-1,-1); preds = Const.const (-1); } in play_cards input s; (** This is ghost code to build an increasing subsequence of maximal length *) let ns = s.num_stacks in if ns = 0 then begin assert { input = Nil }; let seq = { seqlen = 0 ; seqval = Const.const (-1) } in assert { increasing_subsequence seq input }; s end else let last_stack = s.stacks[ns-1] in let idx = ref (last_stack[s.stack_sizes[ns-1]-1]) in let seq = ref (Const.const (-1)) in for i = ns-1 downto 0 do invariant { -1 <= !idx < s.num_elts } invariant { i >= 0 -> !idx >= 0 && `````` Andrei Paskevich committed Jun 11, 2017 474 `````` let is,_ = s.positions[!idx] in is = i } `````` MARCHE Claude committed Feb 26, 2016 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 `````` invariant { i+1 < ns -> !idx < !seq[i+1] } invariant { 0 <= i < ns-1 -> s.values[!idx] < s.values[!seq[i+1]] } invariant { forall j. i < j < ns -> 0 <= !seq[j] < s.num_elts } invariant { forall j,k. i < j < k < ns -> !seq[j] < !seq[k] } invariant { forall j,k. i < j < k < ns -> s.values[!seq[j]] < s.values[!seq[k]] } seq := !seq[i <- !idx]; idx := s.preds[!idx]; done; let sigma = { seqlen = ns ; seqval = !seq } in assert { forall i. 0 <= i < length input -> nth i input = s.values[i] }; assert { increasing_subsequence sigma input }; (** These are assertions to prove that no increasing subsequence of length larger than the number of stacks may exists *) assert { (* non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > s.num_stacks -> `````` Guillaume Melquiond committed Mar 16, 2016 497 `````` let f = fun i -> `````` MARCHE Claude committed Feb 26, 2016 498 `````` let si = sigma.seqval[i] in `````` Andrei Paskevich committed Jun 11, 2017 499 `````` let stack_i,_ = s.positions[si] in `````` MARCHE Claude committed Feb 26, 2016 500 501 502 503 504 505 506 507 508 509 510 511 `````` stack_i in range f sigma.seqlen s.num_stacks && not (injective f sigma.seqlen s.num_stacks) }; assert { (* non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > s.num_stacks -> exists i,j. 0 <= i < j < sigma.seqlen && let si = sigma.seqval[i] in let sj = sigma.seqval[j] in `````` Andrei Paskevich committed Jun 11, 2017 512 513 `````` let stack_i,_pi = s.positions[si] in let stack_j,_pj = s.positions[sj] in `````` MARCHE Claude committed Feb 26, 2016 514 515 516 517 518 519 520 521 522 `````` stack_i = stack_j }; assert { (* contradiction from non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > s.num_stacks -> forall i,j. 0 <= i < j < sigma.seqlen -> let si = sigma.seqval[i] in let sj = sigma.seqval[j] in `````` Andrei Paskevich committed Jun 11, 2017 523 524 `````` let stack_i,pi = s.positions[si] in let stack_j,pj = s.positions[sj] in `````` MARCHE Claude committed Feb 26, 2016 525 526 527 528 529 `````` stack_i = stack_j -> si < sj && pi < pj && s.values[si] < s.values[sj] }; s `````` MARCHE Claude committed Mar 24, 2016 530 `````` let ghost test () = `````` MARCHE Claude committed Feb 26, 2016 531 532 533 534 535 536 537 538 539 540 541 `````` (* the list given in the problem description 9, 7, 10, 9, 5, 4, and 10 *) play_game (Cons 9 (Cons 7 (Cons 10 (Cons 9 (Cons 5 (Cons 4 (Cons 10 Nil))))))) end (** {2 Gluing abstract version with the original idiomatic code} *) module PatienceFull `````` Andrei Paskevich committed Jun 15, 2018 542 543 `````` use int.Int use PatienceAbstract `````` MARCHE Claude committed Feb 26, 2016 544 545 546 547 `````` (** glue between the ghost state and the stacks of cards *) `````` Andrei Paskevich committed Jun 15, 2018 548 549 550 551 `````` use list.List use list.Length use list.NthNoOpt use map.Map `````` MARCHE Claude committed Feb 26, 2016 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 `````` predicate glue_stack (s:state) (i:int) (st:list card) = length st = s.stack_sizes[i] /\ let stack_i = s.stacks[i] in forall j. 0 <= i < length st -> nth j st = s.values[stack_i[j]] predicate glue (s:state) (st:list (list card)) = length st = s.num_stacks /\ forall i. 0 <= i < length st -> glue_stack s i (nth i st) (** {3 playing a card} *) `````` Andrei Paskevich committed Jun 15, 2018 569 570 `````` use list.RevAppend use ref.Ref `````` MARCHE Claude committed Feb 26, 2016 571 572 573 `````` exception Return `````` Guillaume Melquiond committed Jun 14, 2018 574 ``````(*** FIXME: not proved `````` MARCHE Claude committed Feb 26, 2016 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 `````` let play_card (c:card) (old_stacks : list (list card)) (ghost state:state) : list (list card) requires { inv state } requires { glue state old_stacks } writes { state } ensures { inv state } ensures { state.num_elts = (old state).num_elts + 1 } ensures { state.values = (old state).values[(old state).num_elts <- c] } ensures { glue state result } = let acc = ref Nil in let rem_stacks = ref old_stacks in let ghost pred = ref (-1) in let ghost i = ref 0 in try while !rem_stacks <> Nil do invariant { 0 <= !i <= state.num_stacks } invariant { if !i = 0 then !pred = -1 else let stack_im1 = state.stacks[!i-1] in let stack_im1_size = state.stack_sizes[!i-1] in let top_stack_im1 = stack_im1[stack_im1_size - 1] in !pred = top_stack_im1 /\ c > state.values[!pred] /\ 0 <= !pred < state.num_elts /\ `````` Andrei Paskevich committed Jun 11, 2017 599 `````` let ps,_pp = state.positions[!pred] in `````` MARCHE Claude committed Feb 26, 2016 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 `````` ps = !i - 1 } invariant { old_stacks = rev_append !acc !rem_stacks } invariant { forall j. 0 <= j < !i -> glue_stack state j (nth (!i - j) !acc) } invariant { forall j. !i <= j < state.num_stacks -> glue_stack state j (nth (j - !i) !rem_stacks) } variant { !rem_stacks } match !rem_stacks with | Nil -> absurd | Cons stack remaining_stacks -> rem_stacks := remaining_stacks; match stack with | Nil -> assert { glue_stack state !i stack }; absurd | Cons c' _ -> if c <= c' then begin acc := Cons (Cons c stack) !acc; raise Return; end; let ghost stack_i = state.stacks[!i] in let ghost stack_i_size = state.stack_sizes[!i] in let ghost top_stack_i = stack_i[stack_i_size - 1] in assert { 0 <= top_stack_i < state.num_elts }; `````` Andrei Paskevich committed Jun 11, 2017 629 `````` assert { let is,ip = state.positions[top_stack_i] in `````` MARCHE Claude committed Feb 26, 2016 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 `````` 0 <= is < state.num_stacks && 0 <= ip < state.stack_sizes[is] && state.stacks[is][ip] = top_stack_i && is = !i /\ ip = stack_i_size - 1 }; i := !i + 1; acc := Cons stack !acc; pred := top_stack_i end end done; (* we add a new stack *) let ghost idx = state.num_elts in let ghost i = state.num_stacks in let ghost stack_i = state.stacks[i] in let ghost new_stack_i = stack_i[0 <- idx] in state.num_elts <- idx + 1; state.values <- state.values[idx <- c]; state.num_stacks <- state.num_stacks + 1; state.stack_sizes <- state.stack_sizes[i <- 1]; state.stacks <- state.stacks[i <- new_stack_i]; `````` Andrei Paskevich committed Jun 11, 2017 651 `````` state.positions <- state.positions[idx <- i,0]; `````` MARCHE Claude committed Feb 26, 2016 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 `````` state.preds <- state.preds[idx <- !pred]; (* we put card [c] in a new stack *) rev_append (Cons (Cons c Nil) !acc) Nil with Return -> let ghost stack_i = state.stacks[!i] in let ghost stack_i_size = state.stack_sizes[!i] in let ghost top_stack_i = stack_i[stack_i_size - 1] in assert { c <= state.values[top_stack_i] }; (* we put c on top of stack i *) let ghost idx = state.num_elts in let ghost new_stack_i = stack_i[stack_i_size <- idx] in state.num_elts <- idx + 1; state.values <- state.values[idx <- c]; (* state.num_stacks unchanged *) state.stack_sizes <- state.stack_sizes[!i <- stack_i_size + 1]; state.stacks <- state.stacks[!i <- new_stack_i]; `````` Andrei Paskevich committed Jun 11, 2017 668 `````` state.positions <- state.positions[idx <- !i,stack_i_size]; `````` MARCHE Claude committed Feb 26, 2016 669 670 671 672 673 674 675 676 677 678 `````` state.preds <- state.preds[idx <- !pred]; (* card is placed on the leftmost stack where its card value is no greater than the topmost card on that stack *) rev_append !acc !rem_stacks end *) `````` Guillaume Melquiond committed Jun 14, 2018 679 ``````(*** a version closer to the original code `````` MARCHE Claude committed Feb 26, 2016 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 `````` let play_card (c:card) (old_stacks : list (list card)) (ghost state:state) : list (list card) requires { inv state } requires { glue state old_stacks } writes { state } ensures { inv state } ensures { state.num_elts = (old state).num_elts + 1 } ensures { state.values = (old state).values[(old state).num_elts <- c] } ensures { glue state result } = let i = ref 0 in let pred = ref (-1) in let rec push_card (c:card) (st : list (list card)) (acc : list (list card)) : list (list card) requires { old_stacks = rev_append acc st } variant { st } = match st with | Nil -> (* we put card [c] in a new stack *) rev_append (Cons (Cons c Nil) acc) Nil | Cons stack remaining_stacks -> match stack with | Nil -> assert { glue_stack state !i stack }; absurd | Cons c' _ -> if c <= c' then (* card is placed on the leftmost stack where its card value is no greater than the topmost card on that stack *) rev_append (Cons (Cons c stack) acc) remaining_stacks else (* try next stack *) push_card c remaining_stacks (Cons stack acc) end end in let new_stacks = push_card c old_stacks Nil in let idx = state.num_elts in state.num_elts <- idx + 1; state.values <- state.values[idx <- c]; new_stacks *) `````` Guillaume Melquiond committed Jun 14, 2018 724 ``````(*** {3 playing cards} *) `````` MARCHE Claude committed Feb 26, 2016 725 726 727 `````` `````` Guillaume Melquiond committed Jun 14, 2018 728 ``````(*** `````` MARCHE Claude committed Feb 26, 2016 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 `````` let rec play_cards (input: list card) (stacks: list (list card)) (ghost state:state) : list (list card) requires { inv state } requires { glue state stacks } variant { input } (* writes { state } *) ensures { inv state } ensures { state.num_elts = (old state).num_elts + length input } ensures { forall i. 0 <= i < (old state).num_elts -> state.values[i] = (old state).values[i] } ensures { forall i. (old state).num_elts <= i < state.num_elts -> state.values[i] = nth (i - (old state).num_elts) input } ensures { glue state result } = match input with | Nil -> stacks | Cons c rem -> let stacks' = play_card c stacks state in play_cards rem stacks' state end *) `````` Guillaume Melquiond committed Jun 14, 2018 759 ``````(*** {3 playing a whole game} *) `````` MARCHE Claude committed Feb 26, 2016 760 `````` `````` Guillaume Melquiond committed Jun 14, 2018 761 ``````(*** `````` MARCHE Claude committed Feb 26, 2016 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 `````` type seq 'a = { seqlen: int; seqval: map int 'a } (** a sequence is defined by a length and a mapping *) (** definition of an increasing sub-sequence of a list of card *) predicate increasing_subsequence (sigma:seq int) (l:list card) = 0 <= sigma.seqlen <= length l (** the length of [sigma] is at most the number of cards *) && (forall i. 0 <= i < sigma.seqlen -> 0 <= sigma.seqval[i] < length l) (** [sigma] maps indexes to valid indexes in the card list *) && (forall i,j. 0 <= i < j < sigma.seqlen -> sigma.seqval[i] < sigma.seqval[j]) (** [sigma] is an increasing sequence of indexes *) && (forall i,j. 0 <= i < j < sigma.seqlen -> nth sigma.seqval[i] l < nth sigma.seqval[j] l) (** the card values denoted by [sigma] are increasing *) `````` Andrei Paskevich committed Jun 15, 2018 778 `````` use PigeonHole `````` MARCHE Claude committed Feb 26, 2016 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 `````` let play_game (input: list card) : list (list card) requires { length input > 0 } ensures { exists sigma: seq int. sigma.seqlen = length result /\ increasing_subsequence sigma input } ensures { forall sigma: seq int. increasing_subsequence sigma input -> sigma.seqlen <= length result } = let ghost state = { num_elts = 0; values = Const.const (-1) ; num_stacks = 0; stack_sizes = Const.const 0; stacks = Const.const (Const.const (-1)); positions = Const.const (-1,-1); preds = Const.const (-1); } in let final_stacks = play_cards input Nil state in assert { forall i. 0 <= i < length input -> nth i input = state.values[i] }; (** This is ghost code to build an increasing subsequence of maximal length *) let ghost ns = state.num_stacks in let ghost _sigma = if ns = 0 then begin assert { input = Nil }; absurd (* TODO: if input is empty, we may be able to prove that: let sigma = { seqlen = 0 ; seqval = Const.const (-1) } in assert { increasing_subsequence sigma input }; sigma *) end else let ghost last_stack = state.stacks[ns-1] in let ghost idx = ref (last_stack[state.stack_sizes[ns-1]-1]) in let ghost seq = ref (Const.const (-1)) in for i = ns-1 downto 0 do invariant { -1 <= !idx < state.num_elts } invariant { i >= 0 -> !idx >= 0 && `````` Andrei Paskevich committed Jun 11, 2017 827 `````` let is,_ = state.positions[!idx] in is = i } `````` MARCHE Claude committed Feb 26, 2016 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 `````` invariant { i+1 < ns -> !idx < !seq[i+1] } invariant { 0 <= i < ns-1 -> state.values[!idx] < state.values[!seq[i+1]] } invariant { forall j. i < j < ns -> 0 <= !seq[j] < state.num_elts } invariant { forall j,k. i < j < k < ns -> !seq[j] < !seq[k] } invariant { forall j,k. i < j < k < ns -> state.values[!seq[j]] < state.values[!seq[k]] } seq := !seq[i <- !idx]; idx := state.preds[!idx]; done; let ghost sigma = { seqlen = ns ; seqval = !seq } in assert { increasing_subsequence sigma input }; (** These are assertions to prove that no increasing subsequence of length larger than the number of stacks may exists *) assert { (* non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > state.num_stacks -> `````` Guillaume Melquiond committed Mar 16, 2016 849 `````` let f = fun i -> `````` MARCHE Claude committed Feb 26, 2016 850 `````` let si = sigma.seqval[i] in `````` Andrei Paskevich committed Jun 11, 2017 851 `````` let stack_i,_ = state.positions[si] in `````` MARCHE Claude committed Feb 26, 2016 852 853 854 855 856 857 858 859 860 861 862 `````` stack_i in range f sigma.seqlen state.num_stacks && not (injective f sigma.seqlen state.num_stacks) }; assert { (* non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > state.num_stacks -> exists i,j. 0 <= i < j < sigma.seqlen && let si = sigma.seqval[i] in let sj = sigma.seqval[j] in `````` Andrei Paskevich committed Jun 11, 2017 863 864 `````` let stack_i,_pi = state.positions[si] in let stack_j,_pj = state.positions[sj] in `````` MARCHE Claude committed Feb 26, 2016 865 866 867 868 869 870 871 872 873 `````` stack_i = stack_j }; assert { (* contradiction from non-injectivity *) forall sigma: seq int. increasing_subsequence sigma input /\ sigma.seqlen > state.num_stacks -> forall i,j. 0 <= i < j < sigma.seqlen -> let si = sigma.seqval[i] in let sj = sigma.seqval[j] in `````` Andrei Paskevich committed Jun 11, 2017 874 875 `````` let stack_i,pi = state.positions[si] in let stack_j,pj = state.positions[sj] in `````` MARCHE Claude committed Feb 26, 2016 876 877 878 879 880 881 882 883 884 885 `````` stack_i = stack_j -> si < sj && pi < pj && state.values[si] < state.values[sj] }; sigma in final_stacks *) end``````