koda_ruskey.mlw 19 KB
 Jean-Christophe Filliatre committed Apr 12, 2016 1 2 3 4 5 6 7 `````` (** Koda-Ruskey's algorithm Authors: Mário Pereira (Université Paris Sud) Jean-Christophe Filliâtre (CNRS) *) `````` Mario Pereira committed Apr 04, 2016 8 9 ``````module KodaRuskey_Spec `````` Andrei Paskevich committed Jun 15, 2018 10 11 12 13 `````` use map.Map use list.List use list.Append use int.Int `````` Mario Pereira committed Apr 04, 2016 14 15 16 `````` type color = White | Black `````` MARCHE Claude committed Jul 25, 2016 17 18 19 20 21 22 23 `````` let eq_color (c1 c2:color) : bool ensures { result <-> c1 = c2 } = match c1,c2 with | White,White | Black,Black -> True | _ -> False end `````` Mario Pereira committed Apr 04, 2016 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 `````` type forest = | E | N int forest forest type coloring = map int color function size_forest (f: forest) : int = match f with | E -> 0 | N _ f1 f2 -> 1 + size_forest f1 + size_forest f2 end lemma size_forest_nonneg : forall f. size_forest f >= 0 predicate mem_forest (n: int) (f: forest) = match f with | E -> false | N i f1 f2 -> i = n || mem_forest n f1 || mem_forest n f2 end predicate between_range_forest (i j: int) (f: forest) = forall n. mem_forest n f -> i <= n < j predicate disjoint (f1 f2: forest) = forall x. mem_forest x f1 -> mem_forest x f2 -> false predicate no_repeated_forest (f: forest) = match f with | E -> true | N i f1 f2 -> no_repeated_forest f1 && no_repeated_forest f2 && not (mem_forest i f1) && not (mem_forest i f2) && disjoint f1 f2 end predicate valid_nums_forest (f: forest) (n: int) = between_range_forest 0 n f && no_repeated_forest f predicate white_forest (f: forest) (c: coloring) = match f with | E -> true | N i f1 f2 -> c[i] = White && white_forest f1 c && white_forest f2 c end predicate valid_coloring (f: forest) (c: coloring) = match f with | E -> true | N i f1 f2 -> valid_coloring f2 c && match c[i] with | White -> white_forest f1 c | Black -> valid_coloring f1 c end end function count_forest (f: forest) : int = match f with | E -> 1 | N _ f1 f2 -> (1 + count_forest f1) * count_forest f2 end lemma count_forest_nonneg: forall f. count_forest f >= 1 predicate eq_coloring (n: int) (c1 c2: coloring) = forall i. 0 <= i < n -> c1[i] = c2[i] end module Lemmas `````` Andrei Paskevich committed Jun 15, 2018 93 94 95 96 97 `````` use map.Map use list.List use list.Append use int.Int use KodaRuskey_Spec `````` Mario Pereira committed Apr 04, 2016 98 99 100 101 102 103 104 105 `````` type stack = list forest predicate mem_stack (n: int) (st: stack) = match st with | Nil -> false | Cons f tl -> mem_forest n f || mem_stack n tl end `````` Guillaume Melquiond committed Jan 12, 2018 106 `````` lemma mem_app: forall n st1 [@induction] st2. `````` Mario Pereira committed Apr 04, 2016 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 134 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 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 `````` mem_stack n (st1 ++ st2) -> mem_stack n st1 || mem_stack n st2 function size_stack (st: stack) : int = match st with | Nil -> 0 | Cons f st -> size_forest f + size_stack st end lemma size_stack_nonneg : forall st. size_stack st >= 0 lemma white_forest_equiv: forall f c. white_forest f c <-> (forall i. mem_forest i f -> c[i] = White) predicate even_forest (f: forest) = match f with | E -> false | N _ f1 f2 -> not (even_forest f1) || even_forest f2 end predicate final_forest (f: forest) (c: coloring) = match f with | E -> true | N i f1 f2 -> c[i] = Black && final_forest f1 c && if not (even_forest f1) then white_forest f2 c else final_forest f2 c end predicate any_forest (f: forest) (c: coloring) = white_forest f c || final_forest f c lemma any_forest_frame: forall f c1 c2. (forall i. mem_forest i f -> c1[i] = c2[i]) -> (final_forest f c1 -> final_forest f c2) && (white_forest f c1 -> white_forest f c2) predicate unchanged (st: stack) (c1 c2: coloring) = forall i. mem_stack i st -> c1[i] = c2[i] predicate inverse (st: stack) (c1 c2: coloring) = match st with | Nil -> true | Cons f st' -> (white_forest f c1 && final_forest f c2 || final_forest f c1 && white_forest f c2) && if even_forest f then unchanged st' c1 c2 else inverse st' c1 c2 end predicate any_stack (st: stack) (c: coloring) = match st with | Nil -> true | Cons f st -> (white_forest f c || final_forest f c) && any_stack st c end lemma any_stack_frame: forall st c1 c2. unchanged st c1 c2 -> any_stack st c1 -> any_stack st c2 lemma inverse_frame: forall st c1 c2 c3. inverse st c1 c2 -> unchanged st c2 c3 -> inverse st c1 c3 lemma inverse_frame2: forall st c1 c2 c3. unchanged st c1 c2 -> inverse st c2 c3 -> inverse st c1 c3 let lemma inverse_any (st: stack) (c1 c2: coloring) requires { any_stack st c1 } requires { inverse st c1 c2 } ensures { any_stack st c2 } = () lemma inverse_final: forall f st c1 c2. final_forest f c1 -> inverse (Cons f st) c1 c2 -> white_forest f c2 lemma inverse_white: forall f st c1 c2. white_forest f c1 -> inverse (Cons f st) c1 c2 -> final_forest f c2 let lemma white_final_exclusive (f: forest) (c: coloring) requires { f <> E } ensures { white_forest f c -> final_forest f c -> false } = match f with E -> () | N _ _ _ -> () end lemma final_unique: forall f c1 c2. final_forest f c1 -> final_forest f c2 -> forall i. mem_forest i f -> c1[i] = c2[i] let rec lemma inverse_inverse (st: stack) (c1 c2 c3: coloring) requires { inverse st c1 c2 } requires { inverse st c2 c3 } ensures { unchanged st c1 c3 } variant { st } = match st with | Nil -> () | Cons E st' -> inverse_inverse st' c1 c2 c3 `````` Andrei Paskevich committed Jun 10, 2017 218 `````` | Cons f st' -> if even_forest f then () else inverse_inverse st' c1 c2 c3 `````` Mario Pereira committed Apr 04, 2016 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 250 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 `````` end inductive sub stack forest coloring = | Sub_reflex: forall f, c. sub (Cons f Nil) f c | Sub_brother: forall st i f1 f2 c. sub st f2 c -> sub st (N i f1 f2) c | Sub_append: forall st i f1 f2 c. sub st f1 c -> c[i] = Black -> sub (st ++ Cons f2 Nil) (N i f1 f2) c lemma sub_not_nil: forall st f c. sub st f c -> st <> Nil lemma sub_empty: forall st f0 c. st <> Nil -> sub (Cons E st) f0 c -> sub st f0 c lemma sub_mem: forall n st f c. mem_stack n st -> sub st f c -> mem_forest n f lemma sub_weaken1: forall st i f1 f2 f0 c. sub (Cons (N i f1 f2) st) f0 c -> sub (Cons f2 st) f0 c lemma sub_weaken2: forall st i f1 f2 f0 c. sub (Cons (N i f1 f2) st) f0 c -> c[i] = Black -> sub (Cons f1 (Cons f2 st)) f0 c lemma not_mem_st: forall i f st c. not (mem_forest i f) -> sub st f c -> not (mem_stack i st) lemma sub_frame: forall st f0 c c'. no_repeated_forest f0 -> (forall i. not (mem_stack i st) -> mem_forest i f0 -> c'[i] = c[i]) -> sub st f0 c -> sub st f0 c' predicate disjoint_stack (f: forest) (st: stack) = forall i. mem_forest i f -> mem_stack i st -> false lemma sub_no_rep: forall f st' f0 c. sub (Cons f st') f0 c -> no_repeated_forest f0 -> no_repeated_forest f lemma sub_no_rep2: forall f st' f0 c. sub (Cons f st') f0 c -> no_repeated_forest f0 -> disjoint_stack f st' lemma white_valid: forall f c. white_forest f c -> valid_coloring f c lemma final_valid: forall f c. final_forest f c -> valid_coloring f c lemma valid_coloring_frame: forall f c1 c2. valid_coloring f c1 -> (forall i. mem_forest i f -> c2[i] = c1[i]) -> valid_coloring f c2 lemma valid_coloring_set: forall f i c. valid_coloring f c -> not (mem_forest i f) -> valid_coloring f c[i <- Black] lemma head_and_tail: forall f1 f2: 'a, st1 st2: list 'a. Cons f1 st1 = st2 ++ Cons f2 Nil -> st2 <> Nil -> exists st. st1 = st ++ Cons f2 Nil /\ st2 = Cons f1 st lemma sub_valid_coloring_f1: forall i f1 f2 c i1. no_repeated_forest (N i f1 f2) -> valid_coloring (N i f1 f2) c -> c[i] = Black -> mem_forest i1 f1 -> valid_coloring f1 c[i1 <- Black] -> valid_coloring (N i f1 f2) c[i1 <- Black] lemma sub_valid_coloring: forall f0 i f1 f2 st c1. no_repeated_forest f0 -> valid_coloring f0 c1 -> sub (Cons (N i f1 f2) st) f0 c1 -> valid_coloring f0 c1[i <- Black] lemma sub_Cons_N: forall f st i f1 f2 c. sub (Cons f st) (N i f1 f2) c -> f = N i f1 f2 || (exists st'. sub (Cons f st') f1 c) || sub (Cons f st) f2 c lemma white_white: forall f c i. white_forest f c -> white_forest f c[i <- White] let rec lemma sub_valid_coloring_white (f0: forest) (i: int) (f1 f2: forest) (c1: coloring) requires { no_repeated_forest f0 } requires { valid_coloring f0 c1 } requires { white_forest f1 c1 } ensures { forall st. sub (Cons (N i f1 f2) st) f0 c1 -> valid_coloring f0 c1[i <- White] } variant { f0 } = match f0 with | E -> () | N _ f10 f20 -> sub_valid_coloring_white f10 i f1 f2 c1; sub_valid_coloring_white f20 i f1 f2 c1 end function count_stack (st: stack) : int = match st with | Nil -> 1 | Cons f st' -> count_forest f * count_stack st' end lemma count_stack_nonneg: forall st. count_stack st >= 1 use seq.Seq as S type visited = S.seq coloring predicate stored_solutions (f0: forest) (bits: coloring) (st: stack) (v1 v2: visited) = let n = size_forest f0 in let start = S.length v1 in let stop = S.length v2 in stop - start = count_stack st && (forall j. 0 <= j < start -> eq_coloring n (S.get v2 j) (S.get v1 j)) && forall j. start <= j < stop -> valid_coloring f0 (S.get v2 j) && (forall i. 0 <= i < n -> not (mem_stack i st) -> (S.get v2 j)[i] = bits[i]) && forall k. start <= k < stop -> j <> k -> not (eq_coloring n (S.get v2 j) (S.get v2 k)) let lemma stored_trans1 (f0: forest) (bits1 bits2: coloring) (i: int) (f1 f2: forest) (st: stack) (v1 v2 v3: visited) requires { valid_nums_forest f0 (size_forest f0) } requires { 0 <= i < size_forest f0 } requires { forall j. 0 <= j < size_forest f0 -> not (mem_stack j (Cons (N i f1 f2) st)) -> bits2[j] = bits1[j] } requires { forall j. S.length v1 <= j < S.length v2 -> (S.get v2 j)[i] = White } requires { forall j. S.length v2 <= j < S.length v3 -> (S.get v3 j)[i] = Black } requires { stored_solutions f0 bits1 (Cons f2 st) v1 v2 } requires { stored_solutions f0 bits2 (Cons f1 (Cons f2 st)) v2 v3 } ensures { stored_solutions f0 bits2 (Cons (N i f1 f2) st) v1 v3 } = () let lemma stored_trans2 (f0: forest) (bits1 bits2: coloring) (i: int) (f1 f2: forest) (st: stack) (v1 v2 v3: visited) requires { valid_nums_forest f0 (size_forest f0) } requires { 0 <= i < size_forest f0 } requires { forall j. 0 <= j < size_forest f0 -> not (mem_stack j (Cons (N i f1 f2) st)) -> bits2[j] = bits1[j] } requires { forall j. S.length v1 <= j < S.length v2 -> (S.get v2 j)[i] = Black } requires { forall j. S.length v2 <= j < S.length v3 -> (S.get v3 j)[i] = White } requires { stored_solutions f0 bits1 (Cons f1 (Cons f2 st)) v1 v2 } requires { stored_solutions f0 bits2 (Cons f2 st) v2 v3 } ensures { stored_solutions f0 bits2 (Cons (N i f1 f2) st) v1 v3 } = () end module KodaRuskey `````` Andrei Paskevich committed Jun 15, 2018 406 407 408 409 `````` use seq.Seq as S use list.List use KodaRuskey_Spec use Lemmas `````` Mario Pereira committed Apr 04, 2016 410 `````` use map.Map as M `````` Andrei Paskevich committed Jun 15, 2018 411 412 413 `````` use array.Array use int.Int use ref.Ref `````` Mario Pereira committed Apr 04, 2016 414 `````` `````` Mário Pereira committed Feb 19, 2017 415 `````` val ghost map_of_array (a: array 'a) : M.map int 'a `````` Mario Pereira committed Apr 04, 2016 416 417 418 419 `````` ensures { result = a.elts } val ghost visited: ref visited `````` Jean-Christophe Filliatre committed May 04, 2016 420 `````` let rec enum (bits: array color) (ghost f0: forest) (st: list forest) : unit `````` Mario Pereira committed Apr 04, 2016 421 422 423 424 425 426 427 428 `````` requires { size_forest f0 = length bits } requires { valid_nums_forest f0 (length bits) } requires { sub st f0 bits.elts } requires { st <> Nil } requires { any_stack st bits.elts } requires { valid_coloring f0 bits.elts } variant { size_stack st, st } ensures { forall i. `````` Jean-Christophe Filliatre committed May 04, 2016 429 `````` not (mem_stack i st) -> bits[i] = (old bits)[i] } `````` Mario Pereira committed Apr 04, 2016 430 431 432 433 434 435 436 437 `````` ensures { inverse st (old bits).elts bits.elts } ensures { valid_coloring f0 bits.elts } ensures { stored_solutions f0 bits.elts st (old !visited) !visited } = match st with | Nil -> absurd | Cons E st' -> match st' with `````` Jean-Christophe Filliatre committed Apr 12, 2016 438 439 `````` | Nil -> (* that's where we visit the next coloring *) `````` Mario Pereira committed Apr 04, 2016 440 441 442 443 `````` assert { valid_coloring f0 bits.elts }; ghost visited := S.snoc !visited (map_of_array bits); () | _ -> `````` Jean-Christophe Filliatre committed May 04, 2016 444 `````` enum bits f0 st' `````` Mario Pereira committed Apr 04, 2016 445 446 447 448 449 `````` end | Cons (N i f1 f2 as f) st' -> assert { disjoint_stack f1 st' }; assert { not (mem_stack i st') }; let ghost visited1 = !visited in `````` MARCHE Claude committed Jul 25, 2016 450 `````` if eq_color bits[i] White then begin `````` Guillaume Melquiond committed Jun 16, 2016 451 452 `````` label A in enum bits f0 (Cons f2 st'); `````` Mario Pereira committed Apr 04, 2016 453 454 455 `````` assert { sub st f0 bits.elts }; let ghost bits1 = map_of_array bits in let ghost visited2 = !visited in `````` Guillaume Melquiond committed Jun 16, 2016 456 457 `````` label B in bits[i] <- Black; `````` Mario Pereira committed Apr 04, 2016 458 459 `````` assert { sub st f0 bits.elts }; assert { white_forest f1 bits.elts }; `````` Guillaume Melquiond committed Jun 16, 2016 460 461 462 463 `````` assert { unchanged (Cons f2 st') (bits at B).elts bits.elts}; assert { inverse (Cons f2 st') (bits at A).elts bits.elts }; label C in enum bits f0 (Cons f1 (Cons f2 st')); `````` Mario Pereira committed Apr 04, 2016 464 465 466 `````` assert { bits[i] = Black }; assert { final_forest f1 bits.elts }; assert { if not (even_forest f1) `````` Guillaume Melquiond committed Jun 16, 2016 467 `````` then inverse (Cons f2 st') (bits at C).elts bits.elts && `````` Mario Pereira committed Apr 04, 2016 468 `````` white_forest f2 bits.elts `````` Guillaume Melquiond committed Jun 16, 2016 469 `````` else unchanged (Cons f2 st') (bits at C).elts bits.elts && `````` Mario Pereira committed Apr 04, 2016 470 471 472 473 474 475 `````` final_forest f2 bits.elts }; ghost stored_trans1 f0 bits1 (map_of_array bits) i f1 f2 st' visited1 visited2 !visited end else begin assert { if not (even_forest f1) then white_forest f2 bits.elts else final_forest f2 bits.elts }; `````` Guillaume Melquiond committed Jun 16, 2016 476 477 `````` label A in enum bits f0 (Cons f1 (Cons f2 st')); `````` Mario Pereira committed Apr 04, 2016 478 479 480 `````` assert { sub st f0 bits.elts }; let ghost bits1 = map_of_array bits in let ghost visited2 = !visited in `````` Guillaume Melquiond committed Jun 16, 2016 481 482 `````` label B in bits[i] <- White; `````` Jean-Christophe Filliatre committed Apr 12, 2016 483 `````` assert { unchanged (Cons f1 (Cons f2 st')) `````` Guillaume Melquiond committed Jun 16, 2016 484 `````` (bits at B).elts bits.elts }; `````` Jean-Christophe Filliatre committed Apr 12, 2016 485 `````` assert { inverse (Cons f1 (Cons f2 st')) `````` Guillaume Melquiond committed Jun 16, 2016 486 `````` (bits at A).elts bits.elts }; `````` Mario Pereira committed Apr 04, 2016 487 488 `````` assert { sub st f0 bits.elts }; assert { if even_forest f1 || even_forest f2 `````` Guillaume Melquiond committed Jun 16, 2016 489 490 491 `````` then unchanged st' (bits at A).elts bits.elts else inverse st' (bits at A).elts bits.elts }; enum bits f0 (Cons f2 st'); `````` Mario Pereira committed Apr 04, 2016 492 493 494 495 496 497 498 `````` assert { bits[i] = White }; assert { white_forest f bits.elts }; ghost stored_trans2 f0 bits1 (map_of_array bits) i f1 f2 st' visited1 visited2 !visited end end `````` Jean-Christophe Filliatre committed May 04, 2016 499 `````` let main (bits: array color) (f0: forest) `````` Mario Pereira committed Apr 04, 2016 500 501 502 503 504 505 `````` requires { white_forest f0 bits.elts } requires { size_forest f0 = length bits } requires { valid_nums_forest f0 (length bits) } ensures { S.length !visited = count_forest f0 } ensures { let n = S.length !visited in forall j. 0 <= j < n -> `````` Jean-Christophe Filliatre committed May 04, 2016 506 507 508 509 `````` valid_coloring f0 (S.get !visited j) && forall k. 0 <= k < n -> j <> k -> not (eq_coloring (length bits) (S.get !visited j) (S.get !visited k)) } `````` Mario Pereira committed Apr 04, 2016 510 `````` = visited := S.empty; `````` Jean-Christophe Filliatre committed May 04, 2016 511 `````` enum bits f0 (Cons f0 Nil) `````` Mario Pereira committed Apr 04, 2016 512 `````` `````` Jean-Christophe Filliatre committed Apr 12, 2016 513 514 515 516 517 518 519 520 ``````end (** Independently, let's prove that count_forest is indeed the number of colorings. *) (* wip module CountCorrect `````` Andrei Paskevich committed Jun 15, 2018 521 `````` use seq.Seq as S `````` Jean-Christophe Filliatre committed Apr 12, 2016 522 `````` use map.Map as M `````` Andrei Paskevich committed Jun 15, 2018 523 524 525 526 527 528 `````` use map.Const use list.List use int.Int use KodaRuskey_Spec use Lemmas use ref.Ref `````` Jean-Christophe Filliatre committed Apr 12, 2016 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 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 629 630 631 632 633 634 635 636 637 638 639 `````` predicate id_forest (f: forest) (c1 c2: coloring) = forall j. mem_forest j f -> M.get c1 j = M.get c2 j (* valid coloring, all white outside of f *) predicate solution (f: forest) (c: coloring) = valid_coloring f c && forall j. not (mem_forest j f) -> M.get c j = White lemma solution_eq: forall n f c1 c2. valid_nums_forest f n -> solution f c1 -> eq_coloring n c1 c2 -> solution f c2 predicate is_product (i: int) (f1 f2: forest) (c1 c2 r: coloring) = solution (N i f1 f2) r && M.get r i = Black && id_forest f1 r c1 && id_forest f2 r c2 let product (n: int) (i: int) (f1 f2: forest) (c1 c2: coloring) : coloring requires { valid_nums_forest (N i f1 f2) n } requires { solution f1 c1 } requires { solution f2 c2 } ensures { is_product i f1 f2 c1 c2 result } = let rec copy (acc: coloring) (f: forest) variant { f } ensures { forall i. M.get result i = if mem_forest i f then M.get c2 i else M.get acc i } = match f with | E -> acc | N i2 left2 right2 -> M.set (copy (copy acc left2) right2) i2 (M.get c2 i2) end in let c = copy c1 f2 in M.set c i Black lemma solution_product: forall n i f1 f2 c1 c2 c. valid_nums_forest (N i f1 f2) n -> solution f1 c1 -> solution f2 c2 -> is_product i f1 f2 c1 c2 c -> solution (N i f1 f2) c predicate solutions (n: int) (f: forest) (s: seq coloring) = (forall j. 0 <= j < length s -> solution f s[j]) && (* colorings are disjoint *) (forall j. 0 <= j < length s -> forall k. 0 <= k < length s -> j <> k -> not (eq_coloring n s[j] s[k])) let product_all (n: int) (i: int) (f1 f2: forest) (s1 s2: seq coloring) : seq coloring requires { valid_nums_forest (N i f1 f2) n } requires { solutions n f1 s1 } requires { solutions n f2 s2 } ensures { solutions n (N i f1 f2) result } ensures { forall j. 0 <= j < length s1 -> forall k. 0 <= k < length s2 -> is_product i f1 f2 s1[j] s2[k] result[j * length s2 + k] } ensures { length result = length s1 * length s2 } = let s = ref empty in for j = 0 to length s1 - 1 do invariant { length !s = j * length s2 } invariant { solutions n (N i f1 f2) !s } invariant { forall j' k'. 0 <= j' < j -> 0 <= k' < length s2 -> let c = !s[j' * length s2 + k'] in is_product i f1 f2 s1[j'] s2[k'] c } for k = 0 to length s2 - 1 do invariant { length !s = j * length s2 + k } invariant { solutions n (N i f1 f2) !s } invariant { forall j' k'. 0 <= j' < j && 0 <= k' < length s2 || j' = j && 0 <= k' < k -> let c = !s[j' * length s2 + k'] in is_product i f1 f2 s1[j'] s2[k'] c } let p = product n i f1 f2 s1[j] s2[k] in assert { forall l. 0 <= l < length !s -> not (eq_coloring n p !s[l]) }; s := snoc !s p done done; !s lemma solution_white_or_black: forall n i f1 f2 c. valid_nums_forest (N i f1 f2) n -> solution (N i f1 f2) c -> match M.get c i with | White -> solution f2 c | Black -> exists c1 c2. is_product i f1 f2 c1 c2 c && solution f1 c1 && solution f2 c2 end let rec enum (n: int) (f: forest) : seq coloring requires { valid_nums_forest f n } ensures { length result = count_forest f } ensures { solutions n f result } ensures { forall c. solution f c <-> exists j. 0 <= j < length result && eq_coloring n c result[j] } variant { f } = match f with | E -> cons (const White) empty | N i f1 f2 -> let s1 = enum n f1 in let s2 = enum n f2 in s2 ++ product_all n i f1 f2 s1 s2 end end *)``````