Demo_proof.v 18.2 KB
Newer Older
charguer's avatar
charguer committed
1
Set Implicit Arguments.
charguer's avatar
charguer committed
2
(* LibInt LibWf *)
charguer's avatar
charguer committed
3
Require Import CFLib.
charguer's avatar
charguer committed
4
Require Import Demo_ml.
charguer's avatar
cp  
charguer committed
5
Require Import Stdlib. 
charguer's avatar
ok  
charguer committed
6

charguer's avatar
charguer committed
7 8 9 10




charguer's avatar
demo1  
charguer committed
11 12 13 14 15 16
Ltac xapply_core H cont1 cont2 ::=
  forwards_nounfold_then H ltac:(fun K =>
    match cfml_postcondition_is_evar tt with
    | true => eapply local_frame; [ xlocal | sapply K | cont1 tt | try xok ]
    | false => eapply local_frame_gc; [ xlocal | sapply K | cont1 tt | cont2 tt ]
    end).
charguer's avatar
charguer committed
17

charguer's avatar
cp  
charguer committed
18

charguer's avatar
demo1  
charguer committed
19 20 21
(** [xassert] applies to a goal of the form [(Assert F) H Q],
    (or, more generally, of the form [(Seq_ (Assert F) ;; F') H Q],
    in which case [xseq] is called first.).
charguer's avatar
cp  
charguer committed
22

charguer's avatar
demo1  
charguer committed
23 24 25
    It produces two subgoals: [F H (fun (b:bool) => \[b = true] \* H)]
    and [H ==> Q tt]. The second one is discharged automatically
    if [Q] is not instantiated---this is the case whenever. *) 
charguer's avatar
cp  
charguer committed
26

charguer's avatar
demo1  
charguer committed
27 28 29 30
Ltac xassert_core tt :=
  xuntag tag_assert;
  apply local_erase;
  split; [ | try xok ].
charguer's avatar
cp  
charguer committed
31

charguer's avatar
demo1  
charguer committed
32 33 34 35 36 37
Ltac xassert_pre cont := 
  xextract_check_not_needed tt;
  match cfml_get_tag tt with
  | tag_assert => cont tt
  | tag_seq => xseq; [ cont tt | instantiate ]
  end. 
charguer's avatar
cp  
charguer committed
38

charguer's avatar
demo1  
charguer committed
39 40
Tactic Notation "xassert" :=  
  xassert_pre ltac:(fun _ => xassert_core tt).
charguer's avatar
cp  
charguer committed
41 42


charguer's avatar
demo1  
charguer committed
43 44
(********************************************************************)
(* ** Type annotations *)
charguer's avatar
charguer committed
45

charguer's avatar
demo1  
charguer committed
46 47
let annot_let () =
   let x : int = 3 in x
charguer's avatar
charguer committed
48

charguer's avatar
demo1  
charguer committed
49 50
let annot_tuple_arg () =
   (3, ([] : int list))
charguer's avatar
charguer committed
51

charguer's avatar
demo1  
charguer committed
52 53
let annot_pattern_var x =
   match (x : int list) with [] -> 1 | _ -> 0
charguer's avatar
charguer committed
54

charguer's avatar
demo1  
charguer committed
55 56
let annot_pattern_constr () =
   match ([] : int list) with [] -> 1 | _ -> 0
charguer's avatar
charguer committed
57 58


charguer's avatar
xpat  
charguer committed
59 60 61 62 63
(********************************************************************)
(********************************************************************)
(********************************************************************)


charguer's avatar
charguer committed
64 65
(********************************************************************)
(* ** Top-level values *)
charguer's avatar
ok  
charguer committed
66

charguer's avatar
charguer committed
67 68 69 70 71 72 73 74 75 76 77
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. 
  xcf. skip.
  xcf top_val_int. skip.
Qed.
charguer's avatar
ok  
charguer committed
78

charguer's avatar
charguer committed
79 80 81 82 83
Lemma top_val_int_list_spec : 
  top_val_int_list = @nil int.
Proof using.
  xcf. auto.
Qed.
charguer's avatar
ok  
charguer committed
84

charguer's avatar
charguer committed
85 86 87
Lemma top_val_poly_list_spec : forall A,
  top_val_poly_list = @nil A.
Proof using. xcf*. Qed.
charguer's avatar
init  
charguer committed
88

charguer's avatar
charguer committed
89 90 91
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
92

charguer's avatar
init  
charguer committed
93

charguer's avatar
xpat  
charguer committed
94

charguer's avatar
charguer committed
95
(********************************************************************)
charguer's avatar
xpat  
charguer committed
96
(* ** Return *)
charguer's avatar
init  
charguer committed
97

charguer's avatar
xpat  
charguer committed
98 99
Lemma ret_unit_spec : 
  app ret_unit [tt] \[] \[= tt]. (* (fun (_:unit) => \[]).*) (* same as (# \[]). *)
charguer's avatar
charguer committed
100
Proof using.
charguer's avatar
xpat  
charguer committed
101 102 103 104 105 106 107
  xcf. dup 5.
  xret. xsimpl. auto.
  (* demos *)
  xrets. auto.
  xrets*.
  xret_no_gc. xsimpl. auto.
  xret_no_clean. xsimpl*. 
charguer's avatar
charguer committed
108
Qed.
charguer's avatar
ok  
charguer committed
109

charguer's avatar
xpat  
charguer committed
110 111 112
Lemma ret_int_spec : 
  app ret_int [tt] \[] \[= 3].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok  
charguer committed
113

charguer's avatar
xpat  
charguer committed
114 115 116
Lemma ret_int_pair_spec :
  app ret_int_pair [tt] \[] \[= (3,4)].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok  
charguer committed
117

charguer's avatar
xpat  
charguer committed
118 119 120
Lemma ret_poly_spec : forall A,
  app ret_poly [tt] \[] \[= @nil A].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok  
charguer committed
121 122


charguer's avatar
xlet  
charguer committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
(********************************************************************)
(* ** 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'.

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.
  { xseq. xapp. xapp. xsimpl~. }
charguer's avatar
demo1  
charguer committed
145 146
  { xapp. intro_subst. xapp. }
  { xapps. xapps. }
charguer's avatar
xlet  
charguer committed
147 148 149 150
  { xapps. xapps~. }
Qed.


charguer's avatar
ok  
charguer committed
151

charguer's avatar
charguer committed
152
(********************************************************************)
charguer's avatar
xpat  
charguer committed
153
(* ** Let-value *)
charguer's avatar
init  
charguer committed
154

charguer's avatar
xpat  
charguer committed
155 156
Lemma let_val_int_spec : 
  app let_val_int [tt] \[] \[= 3].
charguer's avatar
charguer committed
157
Proof using.
charguer's avatar
xpat  
charguer committed
158 159 160 161 162 163 164 165 166
  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
167 168
Qed.

charguer's avatar
xpat  
charguer committed
169 170 171 172 173 174
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
175
Proof using.
charguer's avatar
xpat  
charguer committed
176
  xcf. dup 3.
charguer's avatar
demo1  
charguer committed
177 178 179
  { xval. xret. xsimpl. auto. }
  { xval as r. xrets~. } 
  { xvals. xrets~. }
charguer's avatar
charguer committed
180
Qed.
charguer's avatar
init  
charguer committed
181 182


charguer's avatar
xlet  
charguer committed
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
(********************************************************************)
(* ** Let-function *)

Lemma let_fun_const_spec : 
  app let_fun_const [tt] \[] \[= 3].
Proof using.
  xcf. dup 9.
  { xfun. apply Sf. xrets~. }
  { xfun as g. apply Sg. skip. }
  { xfun as g G. apply G. skip. }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]).
    { apply Sf. skip. } 
    { 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.
    { apply H. skip. } 
    { apply H. } }
  { 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
221 222 223 224 225
    { 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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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
250 251 252 253 254 255 256 257 258 259
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] ]).
    (* TODO: could we get away by typing just [xlet] above? *)
  { xassert. { xret. }
    xfun. xrets. =>>. xapp. xrets~. }
  { =>> M. xrets~. }
Qed. 
charguer's avatar
cp  
charguer committed
260

charguer's avatar
xlet  
charguer committed
261 262 263 264 265 266 267 268

(********************************************************************)
(* ** 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
269
  xfun (fun f => forall (x:int), app f [x] \[] \[= x]). { xrets~. }
charguer's avatar
xlet  
charguer committed
270 271 272 273 274 275 276 277 278 279
  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
280
  xfun (fun f => forall A B (x:A) (y:B), app f [x y] \[] \[= (x,y)]). { xrets~. }
charguer's avatar
xlet  
charguer committed
281 282 283 284 285 286 287
  xapps.
  xapps.
  xapps.
  xapps.
  xrets~.
Qed.

charguer's avatar
charguer committed
288
(********************************************************************)
charguer's avatar
xpat  
charguer committed
289
(* ** Pattern-matching *)
charguer's avatar
init  
charguer committed
290

charguer's avatar
xpat  
charguer committed
291 292 293 294 295 296 297
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
298 299
  { xmatch_no_cases. dup 6. 
    { xmatch_case.
charguer's avatar
demo1  
charguer committed
300
      { xrets*. } 
charguer's avatar
charguer committed
301
      { xmatch_case. } }
charguer's avatar
xpat  
charguer committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    { 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
317
  { xmatch_no_intros. skip. }
charguer's avatar
xpat  
charguer committed
318 319 320 321 322 323 324 325
  { 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
326
    { xrets*. } 
charguer's avatar
xpat  
charguer committed
327 328 329
    { false. (* note: [xrets] would produce a ununified [hprop]. 
     caused by [tryfalse] in [hextract_cleanup]. TODO: avoid this. *) } }
  { xmatch.
charguer's avatar
demo1  
charguer committed
330
    xrets*. 
charguer's avatar
xpat  
charguer committed
331 332
    (* second case is killed by [xcase_post] *) }
  { xmatch_no_intros. skip. skip. }
charguer's avatar
charguer committed
333
Qed.
charguer's avatar
init  
charguer committed
334

charguer's avatar
demo  
charguer committed
335

charguer's avatar
charguer committed
336 337 338 339 340 341 342 343 344 345 346 347
(********************************************************************)
(* ** 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
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
(********************************************************************)
(* ** Infix functions *)
 
Lemma infix_plus_plus_plus__spec : forall x y,
  app infix_plus_plus_plus_ [x y] \[] \[= x + y].
Proof using.
  xcf. xrets~.
Qed.

Hint Extern 1 (RegisterSpec infix_plus_plus_plus_) => Provide infix_plus_plus_plus__spec.

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.

Lemma infix_minus_minus_minus__spec : forall x y,
  app infix_minus_minus_minus_ [x y] \[] \[= x + y].
Proof using.
  intros. xcf_show as S. rewrite S. xapps~.
Qed.
charguer's avatar
charguer committed
372 373


charguer's avatar
charguer committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
(********************************************************************)
(* ** 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
init  
charguer committed
397

charguer's avatar
charguer committed
398
(********************************************************************)
charguer's avatar
xpat  
charguer committed
399 400
(********************************************************************)
(********************************************************************)
charguer's avatar
init  
charguer committed
401

charguer's avatar
xpat  
charguer committed
402
(*
charguer's avatar
init  
charguer committed
403 404


charguer's avatar
charguer committed
405 406
(********************************************************************)
(* ** Partial applications *)
charguer's avatar
init  
charguer committed
407

charguer's avatar
charguer committed
408 409 410 411 412 413
Lemma app_partial_2_1 () =
   let f x y = (x,y) in
   f 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
414

charguer's avatar
charguer committed
415 416 417 418 419 420
Lemma app_partial_3_2 () =
   let f x y z = (x,z) in
   f 2 4
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
421

charguer's avatar
charguer committed
422 423 424 425 426 427
Lemma app_partial_add () =
  let add x y = x + y in
  let g = add 1 in g 2
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
428

charguer's avatar
charguer committed
429 430 431 432 433 434 435
Lemma app_partial_appto () =
  let appto x f = f x in
  let _r = appto 3 ((+) 1) in
  appto 3 (fun x -> x + 1)
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
436

charguer's avatar
charguer committed
437 438 439 440 441 442 443 444 445
Lemma test_partial_app_arities () =
   let func4 a b c d = a + b + c + d in
   let f1 = func4 1 in
   let f2 = func4 1 2 in
   let f3 = func4 1 2 3 in
   f1 2 3 4 + f2 3 4 + f3 4
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
446

charguer's avatar
charguer committed
447 448 449 450 451
Lemma app_partial_builtin () =
  let f = (+) 1 in
  f 2
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
452 453 454
Qed.


charguer's avatar
cp  
charguer committed
455 456 457 458 459 460 461
let app_partial_builtin_and () =
  let f = (&&) true in
  f false




charguer's avatar
charguer committed
462 463 464 465 466 467 468 469
(********************************************************************)
(* ** Over applications *)

Lemma app_over_id () =
   let f x = x in
   f f 3
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
470 471 472 473
Qed.



charguer's avatar
charguer committed
474 475 476 477 478 479 480 481 482 483

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



Lemma top_fun_poly_id : forall A (x:A),
  app top_fun_poly_id [x] \[] \[= x].  (* (fun r => \[r = x]). *)
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
484 485
Qed.

charguer's avatar
charguer committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
Lemma top_fun_poly_proj1 : forall A B (x:A) (y:B),
  app top_fun_poly_proj1 [(x,y)] \[] \[= x]
Proof using.
  xcf.
Qed.

Lemma top_fun_poly_proj1' : forall A B (p:A*B),
  app top_fun_poly_proj1 [p] \[] \[= fst x]. (* (fun r => \[r = fst x]).  *)
Proof using.
  xcf.
Qed.

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


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

Lemma let_poly_nil () = 
  let x = [] in x
Proof using.
  xcf.
Qed.

Lemma let_poly_nil_pair () = 
  let x = ([], []) in x
Proof using.
  xcf.
Qed.

Lemma let_poly_nil_pair_homogeneous () =
  let x : ('a list * 'a list) = ([], []) in x
Proof using.
  xcf.
Qed.

Lemma let_poly_nil_pair_heterogeneous () =
  let x : ('a list * int list) = ([], []) in x
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
531

charguer's avatar
charguer committed
532

charguer's avatar
demo  
charguer committed
533
*)
charguer's avatar
charguer committed
534

charguer's avatar
charguer committed
535 536 537 538 539 540 541

(********************************************************************)
(********************************************************************)
(********************************************************************)
(* TODO: xgc demo *)


charguer's avatar
charguer committed
542 543 544
(********************************************************************)
(********************************************************************)
(********************************************************************)
charguer's avatar
init  
charguer committed
545 546
(*

charguer's avatar
charguer committed
547 548 549

(********************************************************************)
(* ** Fatal Exceptions *)
charguer's avatar
init  
charguer committed
550

charguer's avatar
charguer committed
551 552 553 554 555 556 557 558 559 560
Lemma exn_assert_false () =
   assert false
Proof using.
  xcf.
Qed.

Lemma exn_failwith () =
   failwith "ok"
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
561 562
Qed.

charguer's avatar
charguer committed
563
exception My_exn 
charguer's avatar
init  
charguer committed
564

charguer's avatar
charguer committed
565 566 567 568
Lemma exn_raise () =
   raise My_exn
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
569 570 571
Qed.


charguer's avatar
charguer committed
572 573 574 575 576 577 578 579 580 581 582 583
let assert_let () =
  assert (let x = true in true); 
  3

let assert_seq () =
  let r = ref 0 in
  assert (incr r; true); 
  !r

let assert_in_seq () =
  (assert (true); 3) + 1

charguer's avatar
charguer committed
584 585
(********************************************************************)
(* ** Assertions *)
charguer's avatar
init  
charguer committed
586

charguer's avatar
charguer committed
587 588 589 590 591
Lemma assert_true () =
  assert true; 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
592

charguer's avatar
charguer committed
593 594 595 596 597
Lemma assert_pos x =
  assert (x > 0); 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
598

charguer's avatar
charguer committed
599 600 601 602 603
Lemma assert_same (x:int) (y:int) =
  assert (x = y); 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
604 605 606



charguer's avatar
charguer committed
607 608
(********************************************************************)
(* ** Conditionals *)
charguer's avatar
init  
charguer committed
609 610


charguer's avatar
cp  
charguer committed
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 640 641 642
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.
  xcf. xfun. xapp. xret. xextracts.
  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. }
   { (*xclean.*) xextract ;=>> P. xapp. xextracts. xsimpl. math. }.
Qed.


charguer's avatar
init  
charguer committed
643

charguer's avatar
charguer committed
644 645 646 647 648 649 650 651 652 653 654
(********************************************************************)
(* ** Records *)

type 'a sitems = {
  mutable nb : int;
  mutable items : 'a list; }

Lemma sitems_build n =
  { nb = n; items = [] }
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
655 656
Qed.

charguer's avatar
charguer committed
657 658 659 660 661
Lemma sitems_get_nb r =
  r.nb
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
662

charguer's avatar
charguer committed
663 664 665 666 667
Lemma sitems_incr_nb r =
  r.nb <- r.nb + 1 
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
668

charguer's avatar
charguer committed
669 670 671 672 673
Lemma sitems_length_items r =
  List.length r.items
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
674

charguer's avatar
charguer committed
675 676 677 678 679
Lemma sitems_push x r =
  r.nb <- r.nb + 1;
  r.items <- x :: r.items
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
680 681 682
Qed.


charguer's avatar
charguer committed
683 684
(********************************************************************)
(* ** Arrays *)
charguer's avatar
init  
charguer committed
685

charguer's avatar
charguer committed
686 687 688 689 690 691 692 693
Lemma array_ops () =
  let t = Array.make 3 0 in
  let _x = t.(1) in
  t.(2) <- 4;
  let _y = t.(2) in
  let _z = t.(1) in
  Array.length t
Proof using.
charguer's avatar
init  
charguer committed
694 695 696 697
  xcf.
Qed.


charguer's avatar
charguer committed
698 699
(********************************************************************)
(* ** While loops *)
charguer's avatar
init  
charguer committed
700

charguer's avatar
charguer committed
701 702 703 704 705 706 707 708 709
Lemma while_decr () =
   let n = ref 3 in
   let c = ref 0 in
   while !n > 0 do 
      incr c;
      decr n;
   done;
   !c
Proof using.
charguer's avatar
init  
charguer committed
710 711 712
  xcf.
Qed.

charguer's avatar
charguer committed
713 714 715 716 717
Lemma while_false () =
   while false do () done
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
718 719


charguer's avatar
charguer committed
720 721 722 723 724 725 726 727 728
(********************************************************************)
(* ** For loops *)

Lemma for_incr () =
   let n = ref 0 in
   for i = 1 to 10 do
      incr n;
   done;
   !n
charguer's avatar
init  
charguer committed
729

charguer's avatar
charguer committed
730 731 732 733
(* "for .. down to" not yet supported *)
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
734 735


charguer's avatar
charguer committed
736 737
(********************************************************************)
(* ** Recursive function *)
charguer's avatar
init  
charguer committed
738

charguer's avatar
charguer committed
739 740 741 742 743 744 745
Lemma rec rec_partial_half x =
  if x = 0 then 0
  else if x = 1 then assert false
  else 1 + rec_partial_half(x-2)
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
746 747 748



charguer's avatar
cp  
charguer committed
749 750 751 752
let rec rec_mutual_f x =
  if x <= 0 then x else 1 + rec_mutual_g (x-2)
and rec rec_mutual g x =
  rec_mutual_f (x+1)
charguer's avatar
init  
charguer committed
753 754 755
*)


charguer's avatar
cp  
charguer committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
(********************************************************************)
(* ** Lazy binary operators 

let lazyop_val () =
  if true && (false || true) then 1 else 0

let lazyop_term () =
  let f x = (x = 0) in
  if f 1 || f 0 then 1 else 0

let lazyop_mixed () =
  let f x = (x = 0) in
  if true && (f 1 || (f 0 && true)) then 1 else 0

*)

charguer's avatar
init  
charguer committed
772

charguer's avatar
charguer committed
773
(* TODO: include demo of  xpost (fun r =>\[r = 3]). *)
charguer's avatar
init  
charguer committed
774 775


charguer's avatar
cp  
charguer committed
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 811 812 813 814 815
(********************************************************************)
(* ** Evaluation order 

let order_app () =
  let r = ref 0 in
  let f () = incr r; 1 in
  let g () = assert (!r = 1); 1 in
  g() + f()

let order_constr () =
  let r = ref 0 in
  let f () = incr r; 1 in
  let g () = assert (!r = 1); 1 in
  (g() :: f() :: nil)

let order_array () =
  let r = ref 0 in
  let f () = incr r; 1 in
  let g () = assert (!r = 1); 1 in
  [| g() ; f() |]

let order_list () =
  let r = ref 0 in
  let f () = incr r; 1 in
  let g () = assert (!r = 1); 1 in
  [ g() ; f() ]

let order_tuple () =
  let r = ref 0 in
  let f () = incr r; 1 in
  let g () = assert (!r = 1); 1 in
  (g(), f())

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
init  
charguer committed
816 817


charguer's avatar
charguer committed
818 819 820 821
(*************************************************************************)
(*************************************************************************)
(*************************************************************************)
(** * Polymorphic let demos
charguer's avatar
init  
charguer committed
822 823


charguer's avatar
charguer committed
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
(** Demo top-level polymorphic let. *)

Lemma poly_top_spec : forall A,
  poly_top = @nil A.
Proof using. xcf. Qed.

(** Demo local polymorphic let. *)

Lemma poly_let_1_spec : forall A,
  Spec poly_let_1 () |B>>
    B \[] (fun (x:list A) => \[x = nil]).
Proof using. xcf. xval. subst. xrets. auto. Qed.  

(** Demo [xval_st P] *)

Lemma poly_let_1_spec' : forall A,
  Spec poly_let_1 () |B>>
    B \[] (fun (x:list A) => \[x = nil]).
Proof using. xcf. xval_st (fun a => a = @nil). extens~. xrets. subst~. Qed.

(** Demo [xval_st P as x Hx] *)

Lemma poly_let_1_spec'' : forall A,
  Spec poly_let_1 () |B>>
    B \[] (fun (x:list A) => \[x = nil]).
Proof using. xcf. xval_st (fun a => a = @nil) as p Hp. extens~. xrets. subst~. Qed.

(** Demo for partially-polymorphic values. *)

Lemma poly_let_2_spec : forall A1 A2,
  Spec poly_let_2 () |B>>
    B \[] (fun '(x,y) : list A1 * list A2 => \[x = nil /\ y = nil]).
Proof using. intros. xcf. xvals. xrets. auto. Qed.

Lemma poly_let_2_same_spec : forall A,
  Spec poly_let_2_same () |B>>
    B \[] (fun '(x,y) : list A * list A => \[x = nil /\ y = nil]).
Proof using. intros. xcf. xvals. xrets. auto. Qed.

Lemma poly_let_2_partial_spec : forall A,
  Spec poly_let_2_partial () |B>>
    B \[] (fun '(x,y) : list A * list int => \[x = nil /\ y = nil]).
Proof using. intros. xcf. xval as p Hp. subst p. xrets. auto. Qed.
 *)


charguer's avatar
charguer committed
870 871 872 873 874
(*
let (top_val_pair_int_1,top_val_pair_int_2) = (1,2)

let (top_val_pair_fun_1,top_val_pair_fun_2) = (fun x -> x), (fun x -> x)
*)
charguer's avatar
charguer committed
875

charguer's avatar
charguer committed
876 877 878 879 880




*)