Demo_proof.v 36.4 KB
Newer Older
charguer's avatar
charguer committed
1
Set Implicit Arguments.
charguer's avatar
xwhile  
charguer committed
2 3 4 5
(* LibInt LibWf *)
Require Import CFLib.
Require Import Demo_ml.
Require Import Stdlib.
charguer's avatar
charguer committed
6
 Open Scope tag_scope.
charguer's avatar
charguer committed
7 8 9



charguer's avatar
polylet  
charguer committed
10

charguer's avatar
charguer committed
11
(********************************************************************)
charguer's avatar
charguer committed
12
(* ** Lazy binary operators *)
charguer's avatar
charguer committed
13

charguer's avatar
charguer committed
14 15 16 17 18
Lemma lazyop_val_spec : 
  app lazyop_val [tt] \[] \[= 1].
Proof using.
  xcf. xif. xrets~.
Qed.
charguer's avatar
charguer committed
19

charguer's avatar
charguer committed
20 21
(*
Ltac xauto_tilde ::= xauto_tilde_default ltac:(fun _ => auto_tilde).
charguer's avatar
charguer committed
22 23
*)

charguer's avatar
charguer committed
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
Lemma lazyop_term_spec : 
  app lazyop_term [tt] \[] \[= 1].
Proof using.
  xcf. xfun (fun f => forall (x:int), 
    app f [x] \[] \[= isTrue (x = 0)]). 
  { xrets*. }
  xapps.
  xlet. 
  { dup 3. 
    (* { xif_no_simpl \[= true]. xret. skip. skip. }  *)
      (* TODO: 
          xor \[= true].
            with xif_base and 2 continuations
       *)
    { xif_no_simpl \[= true]. 
      { xclean. false. }
      { xapps. xrets~. } }
    { xif. xapps. xrets~. }
    { xgo*. subst. xclean. auto. }
      (* todo: maybe extend [xauto_common] with [logics]? or would it be too slow? *)
  }
  xpulls. xif. xrets~.
Qed.

Notation "'And_' v1 `&&` F2" :=
  (!If (fun H Q => (v1 = true -> F2 H Q) /\ (v1 = false -> (Ret false) H Q)))
  (at level 69, v1 at level 0) : charac.

Lemma lazyop_mixex_spec : 
  app lazyop_mixed [tt] \[] \[= 1].
Proof using.
  xcf.
  xfun (fun f => forall (x:int), 
    app f [x] \[] \[= isTrue (x = 0)]). 
  { xrets*. }
  xlet \[= true].
  { xif. xapps. xlet \[= true].
    { xif. xapps. xlet \[= true]. 
      { xif. xrets~. }
      { intro_subst. xrets~. } }
    { intro_subst. xrets~. } }
  { intro_subst. xif. xrets~. }
Qed.

charguer's avatar
polylet  
charguer committed
68 69 70 71




charguer's avatar
charguer committed
72 73 74 75 76
(********************************************************************)
(********************************************************************)
(********************************************************************)


charguer's avatar
charguer committed
77 78 79 80 81 82 83 84 85 86 87 88 89
(* TODO



let compare_int () =
  (1 <> 0 && 1 <= 2) || (0 = 1 && 1 >= 2 && 1 < 2 && 2 > 1)

let compare_min () =
  (min 0 1)


(********************************************************************)
(* ** List operators *)
charguer's avatar
charguer committed
90

charguer's avatar
charguer committed
91 92 93
let list_ops () =
  let x = [1] in
  List.length (List.rev (List.concat (List.append [x] [x; x])))
charguer's avatar
charguer committed
94

charguer's avatar
charguer committed
95
*)
charguer's avatar
charguer committed
96 97 98 99



(*
charguer's avatar
charguer committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
let match_term_when () =
   let f x = x + 1 in
   match f 3 with 
   | 0 -> 1
   | n when n > 0 -> 2
   | _ -> 3

   (* captures (Some x, _) or (_, Some x) with x > 0 *)
let match_or_clauses p =
   match p with
   | (None, None) -> false
   | ((Some x, _) | (_, Some x)) when x > 0 -> true
   | (Some x, _) | (_, Some x) -> false


*)
charguer's avatar
polylet  
charguer committed
116 117 118 119 120 121 122 123 124


(********************************************************************)
(********************************************************************)
(********************************************************************)




charguer's avatar
charguer committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
(********************************************************************)
(* ** Encoding of names *)

Lemma renaming_types : True.
Proof using.
  pose renaming_t'_.
  pose renaming_t2_. pose C'. 
  pose renaming_t3_. 
  pose renaming_t4_.
  auto.
Qed. 

Lemma renaming_demo_spec : 
  app renaming_demo [tt] \[] \[= tt].
Proof using.
  xcf.
  xval.
  xval.
  xval.
  xval.
  xval.
  xrets.
  auto. 
Qed.
charguer's avatar
polylet  
charguer committed
149 150


charguer's avatar
charguer committed
151 152 153
(********************************************************************)
(* ** Polymorphic let bindings and value restriction *)

charguer's avatar
polylet  
charguer committed
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 218 219 220 221 222 223 224 225 226 227 228 229 230
Lemma let_poly_p0_spec : 
  app let_poly_p0 [tt] \[] \[= tt].
Proof using.
  xcf. xlet_poly_keep (= true). xapp_skip. intro_subst. xrets~.
Qed.

Lemma let_poly_p1_spec : 
  app let_poly_p1 [tt] \[] \[= tt].
Proof using.
  xcf. xfun. xlet_poly_keep (fun B (r:option B) => r = None).
  { xapps. xrets. }
  { intros Hr. xrets~. }
Qed.

Lemma let_poly_p2_spec : 
  app let_poly_p2 [tt] \[] \[= tt].
Proof using.
  xcf. xfun. xlet.
  { xlet_poly_keep (fun B (r:option B) => r = None).
    { xapps. xrets. }
    { intros Hr. xrets~. } }
  { xrets~. } 
Qed.

Lemma let_poly_p3_spec : 
  app let_poly_p3 [tt] \[] \[= tt].
Proof using.
  xcf. 
  xlet_poly_keep (= true). { xapp_skip. } intro_subst.
  xapp_skip.
  xlet_poly_keep (= false). { xapp_skip. } intro_subst.
  xapp_skip.
  xrets~. 
Qed.

Lemma let_poly_f0_spec : forall A, 
  app let_poly_f0 [tt] \[] \[= @nil A].
Proof using.
  xcf. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_f1_spec : forall A, 
  app let_poly_f1 [tt] \[] \[= @nil A].
Proof using.
  xcf. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_f2_spec : forall A, 
  app let_poly_f2 [tt] \[] \[= @nil A].
Proof using.
  xcf. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_f3_spec :
  app let_poly_f3 [tt] \[] \[= @nil int].
Proof using.
  xcf. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_f4_spec :
  app let_poly_f4 [tt] \[] \[= @nil int].
Proof using.
  xcf. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_g1_spec :
  app let_poly_g1 [tt] \[] \[= 5::nil].
Proof using.
  xcf. xapps. xapps. xapps. xsimpl~.
Qed.

Lemma let_poly_g2_spec :
  app let_poly_g2 [tt] \[] \[= 4::nil].
Proof using.
  xcf. xapps. xapps. xapps. xsimpl~.
Qed.

charguer's avatar
charguer committed
231 232
Lemma let_poly_h0_spec : forall A,
  app let_poly_h0 [tt] \[] (fun (r:loc) => r ~~> (@nil A)).
charguer's avatar
polylet  
charguer committed
233
Proof using.
charguer's avatar
charguer committed
234
  xcf. xapps. xret~.
charguer's avatar
polylet  
charguer committed
235 236
Qed.

charguer's avatar
charguer committed
237 238 239 240 241 242 243 244
Lemma let_poly_h1_spec : forall A,
  app let_poly_h1 [tt] \[] (fun (f:func) =>
    \[ app f [tt] \[] (fun (r:loc) => r ~~> (@nil A)) ]).
Proof using.
  xcf. xlet (fun g => \[ app g [tt] \[] (fun (r:loc) => r ~~> (@nil A)) ]). 
  { xfun. xrets. xapps. xapps. }
  intros Hg. xrets. xapps.
Qed.
charguer's avatar
xgc  
charguer committed
245

charguer's avatar
charguer committed
246 247 248 249 250 251
Lemma let_poly_h2_spec : forall A,
  app let_poly_h2 [tt] \[] (fun (f:func) =>
    \[ app f [tt] \[] (fun (r:loc) => r ~~> (@nil A)) ]).
Proof using.
  xcf. xfun. xrets. xapps. xapps.
Qed.
charguer's avatar
recfun  
charguer committed
252

charguer's avatar
charguer committed
253 254 255 256 257
Lemma let_poly_h3_spec : forall A,
  app let_poly_h3 [tt] \[] (fun (r:loc) => r ~~> (@nil A)).
Proof using.
  xcf. xfun. xapps. xapps.
Qed.
charguer's avatar
xpat  
charguer committed
258

charguer's avatar
charguer committed
259 260 261 262 263
Lemma let_poly_k1_spec : forall A,
  app let_poly_k1 [tt] \[] \[= @nil A].
Proof using.
  xcf. xrets~.
Qed.
charguer's avatar
xpat  
charguer committed
264

charguer's avatar
charguer committed
265 266 267 268 269
Lemma let_poly_k2_spec : forall A,
  app let_poly_k2 [tt] \[] (fun (r:loc) => r ~~> (@nil A)).
Proof using.
  xcf. xapps.
Qed.
charguer's avatar
charguer committed
270

charguer's avatar
charguer committed
271 272 273 274 275 276
Lemma let_poly_r1_spec :  
  app let_poly_r1 [tt] \[] \[= tt].
Proof using.
  xcf. xapps. xrets~.
  Unshelve. solve_type.
Qed.
charguer's avatar
charguer committed
277

charguer's avatar
charguer committed
278 279
Lemma let_poly_r2_spec : forall A,
  app let_poly_r2 [tt] \[] \[= @nil A].
charguer's avatar
charguer committed
280
Proof using.
charguer's avatar
charguer committed
281 282 283 284 285
  xcf. xapps. dup 2.
  { xval. xrets~. }
  { xvals. xrets~. }
  Unshelve. solve_type.
Qed.
charguer's avatar
charguer committed
286

charguer's avatar
charguer committed
287 288 289

Lemma let_poly_r3_spec : forall A,
  app let_poly_r3 [tt] \[] \[= @nil A].
charguer's avatar
charguer committed
290
Proof using.
charguer's avatar
charguer committed
291 292 293
  xcf. xlet_poly_keep (fun A (r:list A) => r = nil).
  { xapps. xrets~. }
  intros Hr. xrets. auto.
charguer's avatar
charguer committed
294 295 296
Qed.


charguer's avatar
charguer committed
297

charguer's avatar
charguer committed
298 299
(********************************************************************)
(* ** Top-level values *)
charguer's avatar
ok  
charguer committed
300

charguer's avatar
charguer committed
301 302 303 304 305 306 307 308
Lemma top_val_int_spec :
  top_val_int = 5.
Proof using.
  dup 5.
  xcf. auto.
  (* demos: *)
  xcf_show. skip.
  xcf_show top_val_int. skip. 
charguer's avatar
PRE  
charguer committed
309
  xcf_show top_val_int as M. skip.
charguer's avatar
charguer committed
310 311
  xcf. skip.
Qed.
charguer's avatar
ok  
charguer committed
312

charguer's avatar
charguer committed
313 314 315 316 317
Lemma top_val_int_list_spec : 
  top_val_int_list = @nil int.
Proof using.
  xcf. auto.
Qed.
charguer's avatar
ok  
charguer committed
318

charguer's avatar
charguer committed
319 320 321
Lemma top_val_poly_list_spec : forall A,
  top_val_poly_list = @nil A.
Proof using. xcf*. Qed.
charguer's avatar
init  
charguer committed
322

charguer's avatar
charguer committed
323 324 325
Lemma top_val_poly_list_pair_spec : forall A B,
  top_val_poly_list_pair = (@nil A, @nil B).
Proof using. xcf*. Qed.
charguer's avatar
ok  
charguer committed
326

charguer's avatar
init  
charguer committed
327

charguer's avatar
xpat  
charguer committed
328

charguer's avatar
charguer committed
329
(********************************************************************)
charguer's avatar
xpat  
charguer committed
330
(* ** Return *)
charguer's avatar
init  
charguer committed
331

charguer's avatar
xpat  
charguer committed
332 333
Lemma ret_unit_spec : 
  app ret_unit [tt] \[] \[= tt]. (* (fun (_:unit) => \[]).*) (* same as (# \[]). *)
charguer's avatar
charguer committed
334
Proof using.
charguer's avatar
charguer committed
335 336 337 338 339 340 341 342 343 344 345
  xcf. dup 8. 
  { xret. xsimpl. auto. }
  { xrets. auto. }
  { xrets*. }
  { xret_no_gc. xsimpl. auto. }
  { xret_no_clean. xsimpl*. } (* differs only on nontrivial goals *)
  { xret_no_pull. xsimpl*. } (* differs only on a let binding *)
  { try xret (fun r => \[r = tt /\ True]).
    xpost. xret (fun r => \[r = tt /\ True]). xsimpl. auto. xsimpl. auto. }
  { try xrets (fun r => \[r = tt /\ True]).
    xpost. xrets (fun r => \[r = tt /\ True]). auto. xsimpl. auto. }
charguer's avatar
charguer committed
346
Qed.
charguer's avatar
ok  
charguer committed
347

charguer's avatar
xpat  
charguer committed
348 349 350
Lemma ret_int_spec : 
  app ret_int [tt] \[] \[= 3].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok  
charguer committed
351

charguer's avatar
xpat  
charguer committed
352 353
Lemma ret_int_pair_spec :
  app ret_int_pair [tt] \[] \[= (3,4)].
charguer's avatar
xwhile  
charguer committed
354
Proof using. xcf_go*. Qed.
charguer's avatar
ok  
charguer committed
355

charguer's avatar
xpat  
charguer committed
356 357
Lemma ret_poly_spec : forall A,
  app ret_poly [tt] \[] \[= @nil A].
charguer's avatar
xwhile  
charguer committed
358
Proof using. xcf. xgo*. Qed.
charguer's avatar
ok  
charguer committed
359 360


charguer's avatar
xlet  
charguer committed
361 362 363 364 365 366 367 368
(********************************************************************)
(* ** Sequence *)

Axiom ret_unit_spec' : forall A (x:A),
  app ret_unit [x] \[] \[= tt]. (* (fun (_:unit) => \[]).*) (* same as (# \[]). *)

Hint Extern 1 (RegisterSpec ret_unit) => Provide ret_unit_spec'.

charguer's avatar
PRE  
charguer committed
369

charguer's avatar
xlet  
charguer committed
370 371 372 373 374 375 376 377 378 379 380 381 382
Lemma seq_ret_unit_spec :
  app seq_ret_unit [tt] \[] \[= tt].
Proof using.
  xcf.
  (* xlet. -- make sure we get a good error here *)
  xseq.
  xapp1.
  xapp2.
  dup 3. 
  { xapp3_no_apply. apply S. }
  { xapp3_no_simpl. }
  { xapp3. }
  dup 4.
charguer's avatar
PRE  
charguer committed
383
  { xseq. xapp. xapp. xsimpl. auto. }
charguer's avatar
demo1  
charguer committed
384 385
  { xapp. intro_subst. xapp. }
  { xapps. xapps. }
charguer's avatar
xlet  
charguer committed
386 387 388 389
  { xapps. xapps~. }
Qed.


charguer's avatar
ok  
charguer committed
390

charguer's avatar
charguer committed
391
(********************************************************************)
charguer's avatar
xpat  
charguer committed
392
(* ** Let-value *)
charguer's avatar
init  
charguer committed
393

charguer's avatar
xpat  
charguer committed
394 395
Lemma let_val_int_spec : 
  app let_val_int [tt] \[] \[= 3].
charguer's avatar
charguer committed
396
Proof using.
charguer's avatar
xpat  
charguer committed
397 398 399 400 401 402 403 404 405
  xcf. dup 7.
  xval. xrets~.
  (* demos *)
  xval as r. xrets~.
  xval as r Er. xrets~.
  xvals. xrets~.
  xval_st (= 3). auto. xrets~.
  xval_st (= 3) as r. auto. xrets~.
  xval_st (= 3) as r Er. auto. xrets~.
charguer's avatar
init  
charguer committed
406 407
Qed.

charguer's avatar
xpat  
charguer committed
408 409 410 411 412 413
Lemma let_val_pair_int_spec :
  app let_val_pair_int [tt] \[] \[= (3,4)].
Proof using. xcf. xvals. xrets*. Qed.

Lemma let_val_poly_spec :
  app let_val_poly [tt] \[] \[= 3].
charguer's avatar
charguer committed
414
Proof using.
charguer's avatar
xpat  
charguer committed
415
  xcf. dup 3.
charguer's avatar
demo1  
charguer committed
416 417 418
  { xval. xret. xsimpl. auto. }
  { xval as r. xrets~. } 
  { xvals. xrets~. }
charguer's avatar
charguer committed
419
Qed.
charguer's avatar
init  
charguer committed
420 421


charguer's avatar
xlet  
charguer committed
422 423 424 425 426 427
(********************************************************************)
(* ** Let-function *)

Lemma let_fun_const_spec : 
  app let_fun_const [tt] \[] \[= 3].
Proof using.
charguer's avatar
PRE  
charguer committed
428
  xcf. dup 10.
charguer's avatar
charguer committed
429
  { xfun. apply Sf. xtag_pre_post. xrets~. }
charguer's avatar
xlet  
charguer committed
430
  { xfun as g. apply Sg. skip. }
charguer's avatar
PRE  
charguer committed
431
  { xfun as g. xapp. xret. skip. }
charguer's avatar
xlet  
charguer committed
432 433
  { xfun as g G. apply G. skip. }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]).
charguer's avatar
PRE  
charguer committed
434
    { xapp. skip. } 
charguer's avatar
xlet  
charguer committed
435 436 437 438 439
    { apply Sf. } }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]) as h.
    { apply Sh. skip. } 
    { apply Sh. } }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]) as h H.
charguer's avatar
PRE  
charguer committed
440 441
    { xapp. skip. } 
    { xapp. } }
charguer's avatar
xlet  
charguer committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  { xfun (fun g => app g [tt] \[] \[=3]).
    { xrets~. } 
    { apply Sf. } }
  { xfun (fun g => app g [tt] \[] \[=3]) as h.
    { skip. } 
    { skip. } }
  { xfun (fun g => app g [tt] \[] \[=3]) as h H.
    { skip. } 
    { skip. } }
Qed.

Lemma let_fun_poly_id_spec :
  app let_fun_poly_id [tt] \[] \[= 3].
Proof using.
  xcf. xfun. dup 2.
  { xapp. xret. xsimpl~. }
  { xapp1.
    xapp2.
    dup 5. 
charguer's avatar
demo1  
charguer committed
461 462 463 464 465
    { apply Spec. xrets. auto. }
    { xapp3_no_apply. Focus 2. apply S. xrets. auto. }
    { xapp3_no_simpl. xrets~. }
    { xapp3. xrets~. }
    { xapp. xret. xsimpl~. } }
charguer's avatar
xlet  
charguer committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
Qed.

Lemma let_fun_poly_pair_homogeneous_spec : 
  app let_fun_poly_pair_homogeneous [tt] \[] \[= (3,3)].
Proof using.
  xcf. 
  xfun.
  xapp. 
  xret.
  xsimpl~.
Qed.

Lemma let_fun_on_the_fly_spec :
  app let_fun_on_the_fly [tt] \[] \[= 4].
Proof using.
  xcf.
  xfun.
  xfun.
  xapp. 
  xapp.
  xret.
  xsimpl~.
Qed.

charguer's avatar
demo1  
charguer committed
490 491 492 493 494
Lemma let_fun_in_let_spec :
  app let_fun_in_let [tt] \[] 
    (fun g => \[ forall A (x:A), app g [x] \[] \[= x] ]).
Proof using.
  xcf. xlet (fun g => \[ forall A (x:A), app g [x] \[] \[= x] ]).
charguer's avatar
charguer committed
495
    (* TODO: use [xpush] *)
charguer's avatar
demo1  
charguer committed
496 497 498 499
  { xassert. { xret. }
    xfun. xrets. =>>. xapp. xrets~. }
  { =>> M. xrets~. }
Qed. 
charguer's avatar
cp  
charguer committed
500

charguer's avatar
PRE  
charguer committed
501
Lemma let_fun_in_let_spec' :
charguer's avatar
xwhile  
charguer committed
502 503 504 505 506 507 508 509
  app let_fun_in_let [tt] 
  PRE \[] 
  POST (fun g => \[ forall A (x:A), app g [x] \[] \[= x] ]).
Proof using.
  xcf.
Abort.


charguer's avatar
xlet  
charguer committed
510 511 512 513 514 515 516 517

(********************************************************************)
(* ** Let-term *)

Lemma let_term_nested_id_calls_spec :
  app let_term_nested_id_calls [tt] \[] \[= 2].
Proof using.
  xcf.
charguer's avatar
demo1  
charguer committed
518
  xfun (fun f => forall (x:int), app f [x] \[] \[= x]). { xrets~. }
charguer's avatar
xlet  
charguer committed
519 520 521 522 523 524 525 526 527 528
  xapps. 
  xapps.
  xapps.
  xrets~.
Qed.

Lemma let_term_nested_pairs_calls_spec :
  app let_term_nested_pairs_calls [tt] \[] \[= ((1,2),(3,(4,5))) ].
Proof using.
  xcf. 
charguer's avatar
demo1  
charguer committed
529
  xfun (fun f => forall A B (x:A) (y:B), app f [x y] \[] \[= (x,y)]). { xrets~. }
charguer's avatar
xlet  
charguer committed
530 531 532 533 534 535 536
  xapps.
  xapps.
  xapps.
  xapps.
  xrets~.
Qed.

charguer's avatar
charguer committed
537
(********************************************************************)
charguer's avatar
xpat  
charguer committed
538
(* ** Pattern-matching *)
charguer's avatar
init  
charguer committed
539

charguer's avatar
xpat  
charguer committed
540 541 542 543 544 545 546
Lemma match_pair_as_spec : 
  app match_pair_as [tt] \[] \[= (4,(3,4))].
Proof using.
  xcf. dup 8.
  { xmatch. xrets*. }
  { xmatch_subst_alias. xrets*. }
  { xmatch_no_alias. xalias. xalias as L. skip. }
charguer's avatar
charguer committed
547 548
  { xmatch_no_cases. dup 6. 
    { xmatch_case.
charguer's avatar
demo1  
charguer committed
549
      { xrets*. } 
charguer's avatar
charguer committed
550
      { xmatch_case. } }
charguer's avatar
xpat  
charguer committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    { xcase_no_simpl.
      { dup 3.
        { xalias. xalias. xret. xsimpl. xauto*. }
        { xalias as u U. 
          xalias as v. skip. }
        { xalias_subst. xalias_subst. skip. } }
      { xdone. } } 
    { xcase_no_simpl as E. skip. skip. }
    { xcase_no_intros. intros x y E. skip. intros F. skip. }
    { xcase. skip. skip. }
    { xcase as C. skip. skip. 
      (* note: inversion got rid of C *) 
    } }
  { xmatch_no_simpl_no_alias. skip. }
  { xmatch_no_simpl_subst_alias. skip. }
charguer's avatar
charguer committed
566
  { xmatch_no_intros. skip. }
charguer's avatar
xpat  
charguer committed
567 568 569 570 571 572 573 574
  { xmatch_no_simpl. inverts C. skip. } 
Qed.

Lemma match_nested_spec : 
  app match_nested [tt] \[] \[= (2,2)::nil].
Proof using.
  xcf. xval. dup 3.
  { xmatch_no_simpl.  
charguer's avatar
demo1  
charguer committed
575
    { xrets*. } 
charguer's avatar
xpat  
charguer committed
576 577 578
    { false. (* note: [xrets] would produce a ununified [hprop]. 
     caused by [tryfalse] in [hextract_cleanup]. TODO: avoid this. *) } }
  { xmatch.
charguer's avatar
demo1  
charguer committed
579
    xrets*. 
charguer's avatar
xpat  
charguer committed
580 581
    (* second case is killed by [xcase_post] *) }
  { xmatch_no_intros. skip. skip. }
charguer's avatar
charguer committed
582
Qed.
charguer's avatar
init  
charguer committed
583

charguer's avatar
demo  
charguer committed
584

charguer's avatar
charguer committed
585 586 587 588 589 590 591 592 593 594 595 596
(********************************************************************)
(* ** Let-pattern *)

Lemma let_pattern_pair_int_spec : 
  app let_pattern_pair_int [tt] \[] \[= 3].
Proof using. xcf. xmatch. xrets~. Qed.

Lemma let_pattern_pair_int_wildcard_spec :
  app let_pattern_pair_int_wildcard [tt] \[] \[= 3].
Proof using. xcf. xmatch. xrets~. Qed.


charguer's avatar
charguer committed
597 598 599
(********************************************************************)
(* ** Infix functions *)
 
charguer's avatar
charguer committed
600 601
Lemma infix_plus_plus_plus_spec : forall x y,
  app infix_plus_plus_plus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
602
Proof using.
charguer's avatar
xwhile  
charguer committed
603
  xcf_go~.
charguer's avatar
charguer committed
604 605
Qed.

charguer's avatar
charguer committed
606
Hint Extern 1 (RegisterSpec infix_plus_plus_plus__) => Provide infix_plus_plus_plus_spec.
charguer's avatar
charguer committed
607 608 609 610 611 612 613 614 615

Lemma infix_aux_spec : forall x y,
  app infix_aux [x y] \[] \[= x + y].
Proof using.
  xcf. xapps~.
Qed.

Hint Extern 1 (RegisterSpec infix_aux) => Provide infix_aux_spec.

charguer's avatar
charguer committed
616 617
Lemma infix_minus_minus_minus_spec : forall x y,
  app infix_minus_minus_minus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
618 619 620
Proof using.
  intros. xcf_show as S. rewrite S. xapps~.
Qed.
charguer's avatar
charguer committed
621 622


charguer's avatar
charguer committed
623 624 625 626 627 628 629 630

(********************************************************************)
(* ** Comparison operators *)

Lemma compare_poly_spec : 
  app compare_poly [tt] \[] \[= tt].
Proof using.
  xcf.
charguer's avatar
charguer committed
631 632 633 634 635 636 637 638
  xlet_poly_keep (= true).
  { xapps. xpolymorphic_eq. xsimpl. subst r. logics~. }
  intro_subst.
  xapp. xpolymorphic_eq. intro_subst.
  xlet_poly_keep (= true).
  { xapps. xpolymorphic_eq. xsimpl. subst r. logics~. }
  intro_subst.
  xapp. xpolymorphic_eq. intro_subst.
charguer's avatar
charguer committed
639 640 641
  xrets~.
Qed. 

charguer's avatar
charguer committed
642 643 644 645 646 647 648 649 650 651 652 653
Lemma compare_poly_custom_spec : forall (A:Type),  
  forall (x:compare_poly_type_ A) (y : compare_poly_type_ int),
  app compare_poly_custom [x y] \[] \[=tt].
Proof using.
  xcf.
  xapp. xpolymorphic_eq. intro_subst.
  xapp. xpolymorphic_eq. intro_subst.
  xapp. xpolymorphic_eq. intro_subst.
  xapp. xpolymorphic_eq. intro_subst.
  xrets~.
Qed.

charguer's avatar
charguer committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
Lemma compare_physical_loc_func_spec : 
  app compare_physical_loc_func [tt] \[] \[= tt].
Proof using.
  xcf. xapps. xapps. 
  xapp. intro_subst.
  xapp. intro_subst.
  xfun.
  xapp_spec infix_eq_eq_gen_spec. intros.
  xlet (\[=1]).
    xif.
      xapps. xrets~.
      xrets~.
    intro_subst. xrets~.
Qed.

Fixpoint list_update (k:int) (v:int) (l:list (int*int)) :=
  match l with
  | nil => nil
  | (k2,v2)::t2 => 
     let t := (list_update k v t2) in
     let v' := (If k = k2 then v else v2) in
     (k2,v')::t
  end.

Lemma compare_physical_algebraic_spec : 
  app compare_physical_algebraic [tt] \[] \[= (1,9)::(4,2)::(2,5)::nil ].
Proof using.
  xcf. xfun_ind (@list_sub (int*int)) (fun f =>
     forall (l:list (int*int)) (k:int) (v:int), 
     app f [k v l] \[] \[= list_update k v l ]).
  { xmatch. 
    { xrets~. }
charguer's avatar
charguer committed
686
    { xapps~. xrets. xif.
charguer's avatar
charguer committed
687 688 689 690 691 692 693 694 695
      { xrets. case_if. auto. }
      { xapp_spec infix_emark_eq_gen_spec. intros M. xif.
        { xrets. case_if~. }
        { xrets. case_if~. rewrite~ M. } } } }
   { xapps. xsimpl. subst r. simpl. do 3 case_if. auto. }
Qed.  
    


charguer's avatar
charguer committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
(********************************************************************)
(* ** Inlined total functions *)

Lemma inlined_fun_arith_spec :
  app inlined_fun_arith [tt] \[] \[= 3].
Proof using.  
  xcf.
  xval.
  xlet.
  (* note: division by a possibly-null constant is not inlined *) 
  xapp_skip.
  xrets.
  skip.
Qed.

Lemma inlined_fun_other_spec : forall (n:int),
  app inlined_fun_others [n] \[] \[= n+1].
Proof using.
  xcf. xret. xsimpl. simpl. auto.
Qed.


charguer's avatar
demo2  
charguer committed
718 719 720 721 722 723 724 725 726 727 728 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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 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
(********************************************************************)
(* ** Type annotations *)

Lemma annot_let_spec :
  app annot_let [tt] \[] \[= 3].
Proof using.
  xcf_show. 
  xcf. xval. xrets~.
Qed.

Lemma annot_tuple_arg_spec :
  app annot_tuple_arg [tt] \[] \[= (3, @nil int)].
Proof using.
  xcf_show. 
  xcf. xrets~.
Qed.

Lemma annot_pattern_var_spec : forall (x:list int),
  app annot_pattern_var [x] \[] \[= If x = nil then 1 else 0].
Proof using.
  xcf_show.
  xcf. xmatch; xrets; case_if~.
Qed.

Lemma annot_pattern_constr_spec :
  app annot_pattern_constr [tt] \[] \[= 1].
Proof using.
  xcf_show. 
  xcf. xmatch; xrets~.
Qed.


(********************************************************************)
(* ** Polymorphic functions *)

Lemma top_fun_poly_id_spec : forall A (x:A),
  app top_fun_poly_id [x] \[] \[= x].  (* (fun r => \[r = x]). *)
Proof using.
  xcf. xrets~.
Qed.

Lemma top_fun_poly_proj1_spec : forall A B (x:A) (y:B),
  app top_fun_poly_proj1 [(x,y)] \[] \[= x].
Proof using.
  xcf. xmatch. xrets~.
Qed.

Lemma top_fun_poly_proj1' : forall A B (p:A*B),
  app top_fun_poly_proj1 [p] \[] \[= Datatypes.fst p]. 
  (* TODO: maybe it's better if [fst] remains the one from Datatypes
     rather than the one from Pervasives? *)
Proof using.
  xcf. xmatch. xrets~.
Qed.

Lemma top_fun_poly_pair_homogeneous_spec : forall A (x y : A), 
  app top_fun_poly_pair_homogeneous [x y] \[] \[= (x,y)]. 
Proof using.
  xcf. xrets~.
Qed.


(********************************************************************)
(* ** Polymorphic let bindings *)

Lemma let_poly_nil_spec : forall A,
  app let_poly_nil [tt] \[] \[= @nil A].
Proof using.
  xcf. dup 2.
  { xval. xrets. subst x. auto. }
  { xvals. xrets~. }  
Qed.

Lemma let_poly_nil_pair_spec : forall A B,
  app let_poly_nil_pair [tt] \[] \[= (@nil A, @nil B)].
Proof using.
  xcf. xvals. xrets~.
Qed.

Lemma let_poly_nil_pair_homogeneous_spec : forall A,
  app let_poly_nil_pair_homogeneous [tt] \[] \[= (@nil A, @nil A)].
Proof using.
  xcf. xvals. xrets~.
Qed.

Lemma let_poly_nil_pair_heterogeneous_spec : forall A,
  app let_poly_nil_pair_heterogeneous [tt] \[] \[= (@nil A, @nil int)].
Proof using.
  xcf. xvals. xrets~.
Qed.



charguer's avatar
demo3  
charguer committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
(********************************************************************)
(* ** Fatal Exceptions *)

Lemma exn_assert_false_spec : False ->
  app exn_assert_false [tt] \[] \[= tt].
Proof using.
  xcf. xfail. auto.
Qed.

Lemma exn_failwith_spec : False ->
  app exn_failwith [tt] \[] \[= tt].
Proof using.
  xcf. xfail. auto.
Qed.

Lemma exn_raise_spec : False ->
  app exn_raise [tt] \[] \[= tt].
Proof using.
  xcf. xfail. auto.
Qed.


(********************************************************************)
(* ** Assertions *)

Lemma assert_true_spec :
  app assert_true [tt] \[] \[= 3].
Proof using.
  dup 2.
  { xcf. xassert. { xrets~. } xrets~. }
  { xcf_go~. }
Qed.

Lemma assert_pos_spec : forall (x:int),
  x > 0 ->
  app assert_pos [x] \[] \[= 3].
Proof using.
  dup 2.
  { xcf. xassert. { xrets~. } xrets~. }
  { xcf_go~. }
Qed.

Lemma assert_same_spec : forall (x:int),
  app assert_same [x x] \[] \[= 3].
Proof using.
  dup 2.
  { xcf. xassert. { xrets~. } xrets~. }
  { xcf_go~. }
Qed.

Lemma assert_let_spec :
  app assert_let [tt] \[] \[= 3].
Proof using.
  dup 2.
  { xcf. xassert. { xvals. xrets~. } xrets~. }
  { xcf_go~. }
Qed.

Lemma assert_seq_spec : 
  app assert_seq [tt] \[] \[= 1].
Proof using.
  xcf. xapp. xassert.
    xapp. xrets.
  (* assert cannot do visible side effects,
     otherwise the semantics could change with -noassert *) 
Abort.

Lemma assert_in_seq_spec : 
  app assert_in_seq [tt] \[] \[= 4].
Proof using.
charguer's avatar
PRE  
charguer committed
881
  xcf. xlet. xassert. { xrets. } xrets. 
charguer's avatar
toutbon  
charguer committed
882
  xpulls. xrets~.
charguer's avatar
demo3  
charguer committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
Qed.


(********************************************************************)
(* ** Conditionals *)

Lemma if_true_spec : 
  app if_true [tt] \[] \[= 1].
Proof using.
  xcf. xif. xret. xsimpl. auto.
Qed.

Lemma if_term_spec :
  app if_term [tt] \[] \[= 1].
Proof using.
charguer's avatar
toutbon  
charguer committed
898
  xcf. xfun. xapp. xret. xpulls.
charguer's avatar
demo3  
charguer committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
  xif. xrets~.
Qed.

Lemma if_else_if_spec : 
  app if_else_if [tt] \[] \[= 0].
Proof using.
  xcf. xfun (fun f => forall (x:int), app f [x] \[] \[= false]).
    { xrets~. }
  xapps. xif. xapps. xif. xrets~.
Qed.

Lemma if_then_no_else_spec : forall (b:bool),
  app if_then_no_else [b] \[] (fun x => \[ x >= 0]).
Proof using.
  xcf. xapp. 
  xseq. xif (Hexists n, \[n >= 0] \* r ~~> n).
   { xapp. xsimpl. math. }
   { xrets. math. }
charguer's avatar
toutbon  
charguer committed
917
   { (*xclean.*) xpull ;=>> P. xapp. xpulls. xsimpl. math. }
charguer's avatar
demo3  
charguer committed
918 919 920
Qed.


charguer's avatar
charguer committed
921 922 923
(********************************************************************)
(* ** While loops *)

charguer's avatar
charguer committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
(* TODO: fix hack *)

Definition Zsub := Zminus.

Infix "-" := Zsub : Int_scope.

Open Scope Int_scope.

Lemma Zsub_eq : Zsub = Zminus.
Proof using. auto. Qed.

Opaque Zsub.

Hint Rewrite Zsub_eq : rew_maths.

(* end hack *)

(* TODO: move *)
Hint Rewrite downto_def nat_upto_wf upto_def : rew_maths.


charguer's avatar
charguer committed
945 946 947 948 949 950 951 952 953 954 955
Lemma while_decr_spec : 
  app while_decr [tt] \[] \[= 3].
Proof using.
  xcf. xapps. xapps. dup 9.
  { xwhile. intros R LR HR. 
    cuts PR: (forall k, k >= 0 ->
      R (n ~~> k \* c ~~> (3-k)) (# n ~~> 0 \* c ~~> 3)).
    { xapplys PR. math. }
    intros k. induction_wf IH: (downto 0) k; intros Hk.  
    applys (rm HR). xlet. 
    { xapps. xrets. }
charguer's avatar
toutbon  
charguer committed
956
    { xpulls. xif. 
charguer's avatar
charguer committed
957 958
      { xseq. xapps. xapps. simpl. xapplys IH. math. math. math. } 
      { xrets. math. math. } } 
charguer's avatar
charguer committed
959 960 961 962 963
    xapps. xsimpl~. }
  { xwhile as R. skip. skip. }
  { xwhile_inv (fun b k => \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)) (downto 0).  
    { xsimpl*. math. }
charguer's avatar
toutbon  
charguer committed
964
    { intros S LS b k FS. xpull. intros. xlet. 
charguer's avatar
charguer committed
965
      { xapps. xrets. }
charguer's avatar
toutbon  
charguer committed
966
      { xpulls. xif. 
charguer's avatar
charguer committed
967
        { xseq. xapps. xapps. simpl. xapplys FS.
charguer's avatar
charguer committed
968
            hnf. math. math. eauto. math. eauto. eauto. }
charguer's avatar
charguer committed
969
        { xret. xsimpl. math. math. } } }
charguer's avatar
charguer committed
970
    { intros. xapps. xsimpl. math. } }
charguer's avatar
charguer committed
971 972 973
  { xwhile_inv_basic (fun b k => \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)) (downto 0).
    { xsimpl*. math. }
charguer's avatar
toutbon  
charguer committed
974
    { intros b k. xpull ;=> Hk Hb. xapps. xrets. xauto*. math. }
charguer's avatar
charguer committed
975 976
    { intros k. xpull ;=> Hk Hb. xapps. xapps. xsimpl. math. eauto. math. math. } 
    { => k Hk Hb. xapp. xsimpl. math. } }
charguer's avatar
charguer committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
  { (* using a measure [fun k => abs k] *)
    xwhile_inv_basic (fun b k => \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)) (abs).
    skip. skip. skip. skip. }
  { (* defining the measure externally *)
    xwhile_inv_basic_measure (fun b m => Hexists k, 
         \[m = k] \* \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)).
    skip. skip. skip. skip. }
  { (* defining the measure externally, downwards *)
    xwhile_inv_basic_measure (fun b m => Hexists i, 
         \[m = 3-i] \* \[i <= 3] \* \[b = isTrue (i <= 3)]
                    \* n ~~> (3-i) \* c ~~> i).
    skip. skip. skip. skip. }
  { xwhile_inv_skip (fun b => Hexists k, \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)).  
    skip. skip. skip. }
  { xwhile_inv_basic_skip (fun b => Hexists k, \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)). 
    skip. skip. skip. skip. }
Abort.


Lemma while_false_spec : 
  app while_false [tt] \[] \[= tt].
Proof using.
  xcf. dup 2.
  { xwhile_inv_skip (fun (b:bool)  => \[b = false]). skip. skip. skip. }
  { xwhile_inv_basic (fun (b:bool) (_:unit) => \[b = false]) (fun (_ _:unit) => False).
    { intros_all. constructor. auto_false. }
    { xsimpl*. }
charguer's avatar
toutbon  
charguer committed
1008 1009
    { intros. xpulls. xrets~. } 
    { intros. xpull. auto_false. }
charguer's avatar
charguer committed
1010 1011 1012 1013 1014 1015
    { xsimpl~. }
  }
Qed.



charguer's avatar
charguer committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
(********************************************************************)
(* ** For loops *)

Lemma for_to_incr_spec : forall (r:int), r >= 0 ->
  app for_to_incr [r] \[] \[= r].
Proof using.
  xcf. xapps. dup 7. 
  { xfor. intros S LS HS.
    cuts PS: (forall i, (i <= r) -> S i (n ~~> i) (# n ~~> r)).
    { applys PS. math. }
    { intros i. induction_wf IH: (upto r) i. intros Li.
      applys (rm HS). xif.
      { xapps. applys IH. hnf. math. math. }
      { xrets. math. } } 
    xapps. xsimpl~. } 
  { xfor as S. skip. skip. }
  { xfor_inv (fun (i:int) => n ~~> i).
    { math. }
    { xsimpl. }
    { introv L. xapps. }
    xapps. xsimpl. math. } 
  { xseq (# n ~~> r). xfor_inv (fun (i:int) => n ~~> i).
    skip. skip. skip. skip. skip. }
  { xseq (# n ~~> r). xfor_inv_void. skip. skip. skip. }
  { xfor_inv_void. skip. skip. }
  { try xfor_inv_case (fun (i:int) => n ~~> i). 
    (* fails because no post condition *)
    xseq (# n ~~> r).
    { xfor_inv_case (fun (i:int) => n ~~> i).
      { xsimpl. }
      { introv L. xapps. }
      { xsimpl. math. } 
      { math_rewrite (r = 0). xsimpl. } }
    { xapps. xsimpl~. } }
Abort.

Lemma for_downto_spec : forall (r:int), r >= 0 ->
  app for_downto [r] \[] \[= r].
Proof using.
  xcf. xapps. dup 7. 
  { xfor_down. intros S LS HS.
    cuts PS: (forall i, (i >= -1) -> S i (n ~~> (r-1-i)) (# n ~~> r)).
    { xapplys PS. math. math. }
    { intros i. induction_wf IH: (downto (-1)) i. intros Li.
      applys (rm HS). xif.
      { xapps. xapplys IH. hnf. math. math. math. }
      { xrets. math. } } 
    xapps. xsimpl~. } 
  { xfor_down as S. skip. skip. }
  { xfor_down_inv (fun (i:int) => n ~~> (r-1-i)). 
    { math. }
    { xsimpl. math. }
    { introv L. xapps. xsimpl. math. }
    xapps. xsimpl. math. }
  { xseq (# n ~~> r). xfor_down_inv (fun (i:int) => n ~~> (r-1-i)).
    skip. skip. skip. skip. skip. }
  { xseq (# n ~~> r). xfor_down_inv_void. skip. skip. skip. }
  { xfor_down_inv_void. skip. skip. }
  { try xfor_down_inv_case (fun (i:int) => n ~~> (r-1-i)).
    (* fails because no post condition *)
    xseq (# n ~~> r).
    { xfor_down_inv_case (fun (i:int) => n ~~> (r-1-i)).
      { xsimpl. math. }
      { introv L. xapps. xsimpl. math. }
      { xsimpl. math. } 
      { math_rewrite (r = 0). xsimpl. } }
    { xapps. xsimpl~. } }
Abort.



charguer's avatar
demo3  
charguer committed
1087 1088 1089 1090 1091 1092 1093

(********************************************************************)
(* ** Evaluation order *)

Lemma order_app_spec : 
  app order_app [tt] \[] \[= 2]. 
Proof using.
charguer's avatar
xwhile  
charguer committed
1094 1095 1096
  dup 2. 
    {
    xcf. xapps. xfun. xfun. xfun.
charguer's avatar
toutbon  
charguer committed
1097 1098
    xapps. { xapps. xrets~. } xpulls.
    xapps. { xassert. xapps. xrets~. xapps. xrets~. } xpulls.
charguer's avatar
xwhile  
charguer committed
1099 1100
    xapps. { xassert. xapps. xrets~. xfun. 
      xrets~ (fun f => \[AppCurried f [a b] := (Ret (a + b)%I)] \* r ~~> 2). eauto. }
charguer's avatar
toutbon  
charguer committed
1101
      xpull ;=> Hf.  
charguer's avatar
xwhile  
charguer committed
1102 1103 1104 1105 1106 1107 1108
    xapp. xrets~.
     (* TODO: can we make xret guess the post? 
        The idea is to have [(Ret f) H ?Q] where [f:func] and [f] has a spec in hyps
        to instantiate Q with [fun f => H \* \[spec of f]].
        Then, the proof should just be [xgo~]. *)
  }
  { xcf_go*. skip. (* TODO *) }
charguer's avatar
demo3  
charguer committed
1109 1110 1111 1112 1113 1114 1115 1116 1117
Qed.

Lemma order_constr_spec : 
  app order_constr [tt] \[] \[= 1::1::nil]. 
Proof using.
  xcf_go*.
Qed.
  (* Details:
  xcf. xapps. xfun. xfun.
charguer's avatar
toutbon  
charguer committed
1118 1119
  xapps. { xapps. xrets~. } xpulls.
  xapps. { xassert. xapps. xrets~. xrets~. } xpulls.
charguer's avatar
demo3  
charguer committed
1120 1121 1122 1123 1124 1125 1126 1127 1128
  xrets~.
  *)


Lemma order_list_spec : 
  app order_list [tt] \[] \[= 1::1::nil]. 
Proof using. xcf_go*. Qed.
 
Lemma order_tuple_spec : 
charguer's avatar
charguer committed
1129
  app order_tuple [tt] \[] \[= (1,1)]. 
charguer's avatar
demo3  
charguer committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
Proof using. xcf_go*. Qed.

(* TODO:
let order_array () =

let order_record () =
  let r = ref 0 in
  let g () = incr r; [] in
  let f () = assert (!r = 1); 1 in
  { nb = f(); items = g() }
*)


charguer's avatar
recfun  
charguer committed
1143 1144 1145 1146 1147
(********************************************************************)
(* ** Recursive function *)

Require Import LibInt.

charguer's avatar
PRE  
charguer committed
1148
Lemma rec_partial_half_spec : forall k n,
charguer's avatar
charguer committed
1149
  n = 2 * k -> k >= 0 ->
charguer's avatar
PRE  
charguer committed
1150
  app rec_partial_half [n] \[] \[= k].
charguer's avatar
recfun  
charguer committed
1151
Proof using.
charguer's avatar
PRE  
charguer committed
1152 1153
  dup 2.
  { => k. induction_wf IH: (downto 0) k. xcf.
charguer's avatar
charguer committed
1154
    xrets. xif.
charguer's avatar
xfor  
charguer committed
1155
    { xrets. math. }
charguer's avatar
charguer committed
1156
    { xrets. xif.
charguer's avatar
PRE  
charguer committed
1157
      { xfail. math. }
charguer's avatar
charguer committed
1158 1159 1160 1161 1162
      { xapps (k-1). math. math. math.
        xrets. math. } } }
  { xind_skip as IH. xcf. xrets. xif.
    { xgo~. math. }
    { xrets. xif. math. xapps (k-1). math. math. xrets. math. } }
charguer's avatar
recfun  
charguer committed
1163 1164
Qed.

charguer's avatar
charguer committed
1165 1166 1167 1168 1169
Ltac xuntag_goal_core tt ::=
  match goal with  
  | |- @tag tag_goal _ _ _ _ => unfold tag at 1
  | _ => idtac
  end.
charguer's avatar
recfun  
charguer committed
1170

charguer's avatar
charguer committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

Ltac xcf_core tt ::=
  intros;
  xuntag_goal;
  match goal with
  | |- spec ?f ?n ?P => first [ xcf_core_spec f | xcf_fallback f | fail 2 ]
  | |- curried ?n ?f /\ ?P => first [ xcf_core_spec f | xcf_fallback f | fail 2 ]
  | |- app ?f ?xs ?H ?Q => first [ xcf_core_app f | xcf_top_value f | xcf_fallback f | fail 2 ]
  | |- tag tag_apply (app ?f ?xs) ?H ?Q => first [ xuntag tag_apply; xcf_core_app f | xcf_fallback f | fail 2 ]
  | |- ?f = _ => first [ xcf_top_value f | xcf_fallback f | fail 2 ]
  | _ => fail 1 "need to call [xcf_show f as H], where [f] is the name of the definition"
  end.

(* we can do a simple proof if we are ready to duplicate the verification of [g] *)
Lemma rec_mutual_f_and_g_spec_inlining : 
charguer's avatar
recfun  
charguer committed
1186 1187 1188
     (forall (x:int), x >= 0 -> app rec_mutual_f [x] \[] \[= x])
  /\ (forall (x:int), x >= -1 -> app rec_mutual_g [x] \[] \[= x+1]).
Proof using.
charguer's avatar
charguer committed
1189 1190 1191 1192 1193
  logic (forall (A B:Prop), A -> (A -> B) -> A /\ B).
  { intros x. induction_wf IH: (downto 0) x. intros Px.
    xcf. xif. xrets~. xlet.
    xcf. xapp. math. math. xpulls. xrets. math. }
  { intros Sg. introv Px. xcf. xapps. math. }
charguer's avatar
recfun  
charguer committed
1194 1195
Qed.

charguer's avatar
charguer committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
(* the general approach is as follows *)
(* TODO: does not work yet
Lemma rec_mutual_f_and_g_spec : 
     (forall (x:int), x >= 0 -> app rec_mutual_f [x] \[] \[= x])
  /\ (forall (x:int), x >= -1 -> app rec_mutual_g [x] \[] \[= x+1]).
Proof using.
  intros. cuts G: (forall (m:int), 
     (forall x, 0 <= x <= m+1 -> app rec_mutual_f [x] \[] \[= x])
  /\ (forall x, -1 <= x <= m-1 -> app rec_mutual_g [x] \[] \[= x+1])). 
  { split; intros x P; specializes G (x+4); 
      destruct G as [G1 G2]; xapp; try math. }
  => m. induction_wf IH: (downto (-2)) m. 
    specializes IH (m-1). split; intros x (Lx&Px).
  { xcf. xif. xrets~. xapp.
    math. split. math. skip. intro_subst. xrets. math. }
  { xcf. xapp. math. math. }
Qed.
*)
charguer's avatar
recfun  
charguer committed
1214 1215


charguer's avatar
xgc  
charguer committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
(********************************************************************)
(* ** Reference and garbage collection *)

Lemma ref_gc_spec : 
  app ref_gc [tt] \[] \[= 3].
Proof using.
  xcf.
  xapp.
  xapp.
  xapp.
  xapp.
  dup 4.
charguer's avatar
charguer committed
1228 1229
  { xgc (_r3 ~~> 1). skip. }
  { xgc _r3. skip. }
charguer's avatar
xgc  
charguer committed
1230 1231 1232 1233 1234 1235 1236 1237
  { xgc_but r1. skip. }
  { xlet (fun x => \[x = 2] \* r1 ~~> 1). 
    { xapp. xapp. xsimpl~. } (* auto GC on r5 *)
    { intro_subst. xapps. xrets~. } (* auto GC on r1 *) 
  }
Qed.


charguer's avatar
array  
charguer committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
(********************************************************************)
(* ** Records *)

Lemma sitems_build_spec : forall (A:Type) (n:int),
  app sitems_build [n] \[] (fun r => r ~> `{ nb' := n; items' := @nil A }).
Proof using. xcf_go~. Qed.

Lemma sitems_get_nb_spec : forall (A:Type) (r:loc) (n:int),
  app_keep sitems_get_nb [r]  
     (r ~> `{ nb' := n; items' := @nil A })
     \[= n].
Proof using.
  dup 3. 
  { intros A. xcf_show as R. applys (R A). xgo~. }
  { xcf_show as R. unfold sitems_ in R. specializes R unit. xgo~. }
  { xcf_go~. Unshelve. solve_type. }
Qed.  (* TODO: can we do better than a manual unshelve for dealing with unused type vars? *)

charguer's avatar
xwhile  
charguer committed
1256 1257 1258 1259 1260 1261 1262 1263
Lemma sitems_get_nb_spec' : forall (A:Type) (r:sitems_ A) (n:int),
  app_keep sitems_get_nb [r]  
     (r ~> `{ nb' := n; items' := @nil A })
     \[= n].
Proof using.
  { xcf_go~. }
Qed.  (* TODO: can we do better than a manual unshelve for dealing with unused type vars? *)

charguer's avatar
array  
charguer committed
1264 1265 1266
Lemma sitems_incr_nb_spec : forall (A:Type) (L:list A) (r:loc) (n:int),
  app sitems_incr_nb [r]  
     (r ~> `{ nb' := n; items' := L })
charguer's avatar
xwhile  
charguer committed
1267
     (# (r ~> `{ nb' := n+1; items' := L })). 
charguer's avatar
array  
charguer committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
Proof using.
  dup 2.
  { xcf. xapps. xapp. Unshelve. solve_type. } 
  { xcf_go*. Grab Existential Variables. solve_type. }
Qed.

Lemma sitems_length_item_spec : forall (A:Type) (r:loc) (L:list A) (n:int),
  app_keep sitems_length_items [r]  
     (r ~> `{ nb' := n; items' := L })
     \[= LibListZ.length L ].
Proof using.
  dup 2.
  { xcf. xapps. xrets. }
  { xcf_go*. }
Qed.

Definition Sitems A (L:list A) r := 
  Hexists n, r ~> `{ nb' := n; items' := L } \* \[ n = LibListZ.length L ].

charguer's avatar
charguer committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
(*
Section ProjLemma.
Variables (B:Type) (A1 : Type).
Variables (A2 : forall (x1 : A1), Type).
Variables (A3 : forall (x1 : A1) (x2 : A2 x1), Type).

Lemma proj_lemma_2 : forall  (R:forall (x1:A1) (x2:A2 x1) (t:B), hprop),
  (forall x1 x2 t, R x1 x2 t = t ~> R x1 x2).
Proof using. auto. Qed.

End ProjLemma.

Lemma Sitems_open_gen : True.
Proof.
  pose (@proj_lemma_2 Sitems).
Qed.
*)



charguer's avatar
array  
charguer committed
1307 1308 1309
Lemma sitems_push_spec : forall (A:Type) (r:loc) (L:list A) (x:A),
  app sitems_push [x r] (r ~> Sitems L) (# r ~> Sitems (x::L)).
Proof using.
charguer's avatar
toutbon  
charguer committed
1310
  xcf. xunfold Sitems. xpull ;=> n E. 
charguer's avatar
array  
charguer committed
1311 1312 1313
  xapps. xapps. xapps. xapp. xsimpl. rew_list; math.
Qed.

charguer's avatar
xwhile  
charguer committed
1314 1315 1316 1317 1318
(* TODO: enéoncé spec dérivée pour
App' r`.nb'
en terme de Sitems  

xapp_spec .. *)
charguer's avatar
array  
charguer committed
1319

charguer's avatar
xwhile  
charguer committed
1320 1321 1322
(** Demo of [xopen] and [xclose] *)

Lemma Sitems_open : forall r A (L:list A),
charguer's avatar
array  
charguer committed
1323 1324 1325 1326
  r ~> Sitems L ==> 
  Hexists n, r ~> `{ nb' := n; items' := L } \* \[ n = LibListZ.length L ].
Proof using. intros. xunfolds~ Sitems. Qed.

charguer's avatar
xwhile  
charguer committed
1327
Lemma Sitems_close : forall r A (L:list A) (n:int),
charguer's avatar
array  
charguer committed
1328 1329 1330 1331 1332
  n = LibListZ.length L ->
  r ~> `{ nb' := n; items' := L } ==> 
  r ~> Sitems L.
Proof using. intros. xunfolds~ Sitems. Qed.

charguer's avatar
xwhile  
charguer committed
1333 1334 1335 1336 1337 1338
Implicit Arguments Sitems_close [].
(* TODO comment
r ~> Sitems _ 
xopen r   
xchange (Sitems_open r).
*)
charguer's avatar
array  
charguer committed
1339

charguer's avatar
charguer committed
1340
Hint Extern 1 (RegisterOpen (Sitems _)) => 
charguer's avatar
xwhile  
charguer committed
1341
  Provide Sitems_open.
charguer's avatar
charguer committed
1342
Hint Extern 1 (RegisterClose (record_repr _)) =>  
charguer's avatar
xwhile  
charguer committed
1343
  Provide Sitems_close.
charguer's avatar
array  
charguer committed
1344 1345 1346 1347 1348

Lemma sitems_push_spec' : forall (A:Type) (r:loc) (L:list A) (x:A),
  app sitems_push [x r] (r ~> Sitems L) (# r ~> Sitems (x::L)).
Proof using. 
  xcf. dup 2.
charguer's avatar
toutbon  
charguer committed
1349
  { xopen r. xpull ;=> n E. skip. } 
charguer's avatar
xwhile  
charguer committed
1350 1351
  { xopenx r ;=> n E. xapps. xapps. xapps. xapp.
    xclose r. rew_list; math. xsimpl~. }
charguer's avatar
array  
charguer committed
1352 1353 1354 1355 1356 1357
Qed.


(********************************************************************)
(* ** Arrays *)

charguer's avatar
charguer committed
1358
Require Import Array_proof LibListZ.
charguer's avatar
array  
charguer committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369

Section Array.

Hint Extern 1 (@index _ (list _) _ _ _) => apply index_bounds_impl : maths.
Hint Extern 1 (_ < length (?l[?i:=?v])) => rewrite length_update : maths.
Ltac auto_tilde ::= auto with maths.

Lemma array_ops_spec : 
  app array_ops [tt] \[] \[= 3].
Proof using.
  xcf.
charguer's avatar
charguer committed
1370 1371 1372
  xapp. math. => L EL.
  asserts LL: (LibListZ.length L = 3).
  { subst. rewrite LibListZ.length_make; math. } 
charguer's avatar
array  
charguer committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  xapps. { apply index_bounds_impl; math. }
  xapp~.
  xapps~.
  xapps~.
  xapps~.
  xsimpl. subst. rew_arr~. 
Qed.

End Array.

charguer's avatar
demo3  
charguer committed
1383

charguer's avatar
charguer committed
1384

charguer's avatar
charguer committed