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





charguer's avatar
xfor  
charguer committed
11

charguer's avatar
xgc  
charguer committed
12 13


charguer's avatar
recfun  
charguer committed
14

charguer's avatar
xpat  
charguer committed
15 16 17 18 19
(********************************************************************)
(********************************************************************)
(********************************************************************)


charguer's avatar
charguer committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

(********************************************************************)
(* ** 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
charguer committed
47 48
(********************************************************************)
(* ** Top-level values *)
charguer's avatar
ok  
charguer committed
49

charguer's avatar
charguer committed
50 51 52 53 54 55 56 57
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
58
  xcf_show top_val_int as M. skip.
charguer's avatar
charguer committed
59 60
  xcf. skip.
Qed.
charguer's avatar
ok  
charguer committed
61

charguer's avatar
charguer committed
62 63 64 65 66
Lemma top_val_int_list_spec : 
  top_val_int_list = @nil int.
Proof using.
  xcf. auto.
Qed.
charguer's avatar
ok  
charguer committed
67

charguer's avatar
charguer committed
68 69 70
Lemma top_val_poly_list_spec : forall A,
  top_val_poly_list = @nil A.
Proof using. xcf*. Qed.
charguer's avatar
init  
charguer committed
71

charguer's avatar
charguer committed
72 73 74
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
75

charguer's avatar
init  
charguer committed
76

charguer's avatar
xpat  
charguer committed
77

charguer's avatar
charguer committed
78
(********************************************************************)
charguer's avatar
xpat  
charguer committed
79
(* ** Return *)
charguer's avatar
init  
charguer committed
80

charguer's avatar
xpat  
charguer committed
81 82
Lemma ret_unit_spec : 
  app ret_unit [tt] \[] \[= tt]. (* (fun (_:unit) => \[]).*) (* same as (# \[]). *)
charguer's avatar
charguer committed
83
Proof using.
charguer's avatar
xwhile  
charguer committed
84
  xcf. dup 5. (* TODO : accolade *)
charguer's avatar
xpat  
charguer committed
85 86 87 88 89 90
  xret. xsimpl. auto.
  (* demos *)
  xrets. auto.
  xrets*.
  xret_no_gc. xsimpl. auto.
  xret_no_clean. xsimpl*. 
charguer's avatar
charguer committed
91
Qed.
charguer's avatar
ok  
charguer committed
92

charguer's avatar
xpat  
charguer committed
93 94 95
Lemma ret_int_spec : 
  app ret_int [tt] \[] \[= 3].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok  
charguer committed
96

charguer's avatar
xpat  
charguer committed
97 98
Lemma ret_int_pair_spec :
  app ret_int_pair [tt] \[] \[= (3,4)].
charguer's avatar
xwhile  
charguer committed
99
Proof using. xcf_go*. Qed.
charguer's avatar
ok  
charguer committed
100

charguer's avatar
xpat  
charguer committed
101 102
Lemma ret_poly_spec : forall A,
  app ret_poly [tt] \[] \[= @nil A].
charguer's avatar
xwhile  
charguer committed
103
Proof using. xcf. xgo*. Qed.
charguer's avatar
ok  
charguer committed
104 105


charguer's avatar
xlet  
charguer committed
106 107 108 109 110 111 112 113
(********************************************************************)
(* ** 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
114

charguer's avatar
xlet  
charguer committed
115 116 117 118 119 120 121 122 123 124 125 126 127
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
128
  { xseq. xapp. xapp. xsimpl. auto. }
charguer's avatar
demo1  
charguer committed
129 130
  { xapp. intro_subst. xapp. }
  { xapps. xapps. }
charguer's avatar
xlet  
charguer committed
131 132 133 134
  { xapps. xapps~. }
Qed.


charguer's avatar
ok  
charguer committed
135

charguer's avatar
charguer committed
136
(********************************************************************)
charguer's avatar
xpat  
charguer committed
137
(* ** Let-value *)
charguer's avatar
init  
charguer committed
138

charguer's avatar
xpat  
charguer committed
139 140
Lemma let_val_int_spec : 
  app let_val_int [tt] \[] \[= 3].
charguer's avatar
charguer committed
141
Proof using.
charguer's avatar
xpat  
charguer committed
142 143 144 145 146 147 148 149 150
  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
151 152
Qed.

charguer's avatar
xpat  
charguer committed
153 154 155 156 157 158
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
159
Proof using.
charguer's avatar
xpat  
charguer committed
160
  xcf. dup 3.
charguer's avatar
demo1  
charguer committed
161 162 163
  { xval. xret. xsimpl. auto. }
  { xval as r. xrets~. } 
  { xvals. xrets~. }
charguer's avatar
charguer committed
164
Qed.
charguer's avatar
init  
charguer committed
165 166


charguer's avatar
xlet  
charguer committed
167 168 169 170 171 172
(********************************************************************)
(* ** Let-function *)

Lemma let_fun_const_spec : 
  app let_fun_const [tt] \[] \[= 3].
Proof using.
charguer's avatar
PRE  
charguer committed
173 174
  xcf. dup 10.
  { xfun. apply Sf. xtag_goal. xrets~. }
charguer's avatar
xlet  
charguer committed
175
  { xfun as g. apply Sg. skip. }
charguer's avatar
PRE  
charguer committed
176
  { xfun as g. xapp. xret. skip. }
charguer's avatar
xlet  
charguer committed
177 178
  { xfun as g G. apply G. skip. }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]).
charguer's avatar
PRE  
charguer committed
179
    { xapp. skip. } 
charguer's avatar
xlet  
charguer committed
180 181 182 183 184
    { 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
185 186
    { xapp. skip. } 
    { xapp. } }
charguer's avatar
xlet  
charguer committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  { 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
206 207 208 209 210
    { 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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
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
235 236 237 238 239 240 241 242 243 244
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
245

charguer's avatar
PRE  
charguer committed
246 247 248 249 250 251 252
Lemma let_fun_in_let_spec' :
  app let_fun_in_let [tt] 
  PRE \[] 
  RET g ST \[ forall A (x:A), app g [x] \[] \[= x] ].
Proof using.
Abort.

charguer's avatar
xwhile  
charguer committed
253 254 255 256 257 258 259 260 261
Lemma let_fun_in_let_spec'' :
  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
262 263 264 265 266 267 268 269

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

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

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

charguer's avatar
demo  
charguer committed
336

charguer's avatar
charguer committed
337 338 339 340 341 342 343 344 345 346 347 348
(********************************************************************)
(* ** 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
349 350 351
(********************************************************************)
(* ** Infix functions *)
 
charguer's avatar
charguer committed
352 353
Lemma infix_plus_plus_plus_spec : forall x y,
  app infix_plus_plus_plus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
354
Proof using.
charguer's avatar
xwhile  
charguer committed
355
  xcf_go~.
charguer's avatar
charguer committed
356 357
Qed.

charguer's avatar
charguer committed
358
Hint Extern 1 (RegisterSpec infix_plus_plus_plus__) => Provide infix_plus_plus_plus_spec.
charguer's avatar
charguer committed
359 360 361 362 363 364 365 366 367

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
368 369
Lemma infix_minus_minus_minus_spec : forall x y,
  app infix_minus_minus_minus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
370 371 372
Proof using.
  intros. xcf_show as S. rewrite S. xapps~.
Qed.
charguer's avatar
charguer committed
373 374


charguer's avatar
charguer committed
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
demo2  
charguer committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
(********************************************************************)
(* ** 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
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 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
(********************************************************************)
(* ** 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
560 561
  xcf. xlet. xassert. { xrets. } xrets. 
  xextracts. xrets~.
charguer's avatar
demo3  
charguer committed
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
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.
  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
charguer committed
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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
(********************************************************************)
(* ** While loops *)

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. }
    { xextracts. xif. 
      { xseq. xapps. xapps. simpl. xapplys IH. hnf. skip. skip. skip. } (* TODO math. *)
      { xrets. math. skip. } } (* TODO math. *) 
    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. }
    { intros S LS b k FS. xextract. intros. xlet. 
      { xapps. xrets. }
      { xextracts. xif. 
        { xseq. xapps. xapps. simpl. xapplys FS.
            hnf. skip. skip. eauto. skip. eauto. eauto.  } (* TODO math. *)
        { xret. xsimpl. math. math. } } }
    { intros. xapps. xsimpl. skip. (*  math. *) } }
  { xwhile_inv_basic (fun b k => \[k >= 0] \* \[b = isTrue (k > 0)]
                         \* n ~~> k \* c ~~> (3-k)) (downto 0).
    { xsimpl*. math. }
    { intros b k. xextract ;=> Hk Hb. xapps. xrets. xauto*. math. }
    { intros k. xextract ;=> Hk Hb. xapps. xapps. xsimpl. skip. eauto. skip. hnf. skip. } 
    { => k Hk Hb. xapp. xsimpl. skip. (*  math.*) } }
  { (* 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*. }
    { intros. xextracts. xrets~. } 
    { intros. xextract. auto_false. }
    { xsimpl~. }
  }
Qed.



charguer's avatar
charguer committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
(********************************************************************)
(* ** 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
745 746 747 748 749 750 751 752 753
(********************************************************************)
(* ** Lazy binary operators *)

Lemma lazyop_val_spec : 
  app lazyop_val [tt] \[] \[= 1].
Proof using.
  xcf. xif. xrets~.
Qed.

charguer's avatar
xfor  
charguer committed
754 755 756 757
(*
Ltac xauto_tilde ::= xauto_tilde_default ltac:(fun _ => auto_tilde).
*)

charguer's avatar
demo3  
charguer committed
758 759 760 761 762
Lemma lazyop_term_spec : 
  app lazyop_term [tt] \[] \[= 1].
Proof using.
  xcf. xfun (fun f => forall (x:int), 
    app f [x] \[] \[= isTrue (x = 0)]). 
charguer's avatar
xfor  
charguer committed
763
  { xrets*. }
charguer's avatar
demo3  
charguer committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
  xapps.
  xlet. 
  { dup 3. 
    { 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? *)
  }
  xextracts. xif. xrets~.
Qed.

Lemma lazyop_mixex_spec : 
  app lazyop_mixed [tt] \[] \[= 1].
Proof using.
  xcf. xfun (fun f => forall (x:int), 
    app f [x] \[] \[= isTrue (x = 0)]). 
charguer's avatar
xfor  
charguer committed
782
  { xrets*. }
charguer's avatar
demo3  
charguer committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  xlet \[= true].
  { xif. xapps. xlet \[= true].
    { xif. xapps. xlet \[= true]. 
      { xif. xrets~. }
      { intro_subst. xrets~. } }
    { intro_subst. xrets~. } }
  { intro_subst. xif. xrets~. }
Qed.



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

Lemma order_app_spec : 
  app order_app [tt] \[] \[= 2]. 
Proof using.
charguer's avatar
xwhile  
charguer committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  dup 2. 
    {
    xcf. xapps. xfun. xfun. xfun.
    xapps. { xapps. xrets~. } xextracts.
    xapps. { xassert. xapps. xrets~. xapps. xrets~. } xextracts.
    xapps. { xassert. xapps. xrets~. xfun. 
      xrets~ (fun f => \[AppCurried f [a b] := (Ret (a + b)%I)] \* r ~~> 2). eauto. }
      xextract ;=> Hf.  
    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
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
Qed.

Lemma order_constr_spec : 
  app order_constr [tt] \[] \[= 1::1::nil]. 
Proof using.
  xcf_go*.
Qed.
  (* Details:
  xcf. xapps. xfun. xfun.
  xapps. { xapps. xrets~. } xextracts.
  xapps. { xassert. xapps. xrets~. xrets~. } xextracts.
  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
835
  app order_tuple [tt] \[] \[= (1,1)]. 
charguer's avatar
demo3  
charguer committed
836 837 838 839 840 841 842 843 844 845 846 847 848
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
849 850 851 852 853
(********************************************************************)
(* ** Recursive function *)

Require Import LibInt.

charguer's avatar
PRE  
charguer committed
854 855 856
Lemma rec_partial_half_spec : forall k n,
  n = 2 * k ->
  app rec_partial_half [n] \[] \[= k].
charguer's avatar
recfun  
charguer committed
857
Proof using.
charguer's avatar
PRE  
charguer committed
858 859 860
  dup 2.
  { => k. induction_wf IH: (downto 0) k. xcf.
    xif.
charguer's avatar
xfor  
charguer committed
861
    { xrets. math. }
charguer's avatar
PRE  
charguer committed
862 863 864 865 866 867 868 869 870 871 872 873
    { xif.
      { xfail. math. }
      { xapps (k-1). 
        { unfolds. skip.
          (* TODO Anomaly: Z.sub is not an evaluable constant. 
          => maybe because I made it opaque? *)
        }
        { skip. }
        { xrets. skip. } } } }
  { xind_skip as IH. xcf. x.
    { xgo~. }
    { x. { x. math. } { xapps (k-1). skip. x. x. skip. } } }
charguer's avatar
recfun  
charguer committed
874 875 876 877 878 879 880 881 882 883
Qed.


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, x <= m -> x >= 0 -> app rec_mutual_f [x] \[] \[= x])
  /\ (forall x, x+1 <= m -> x >= -1 -> app rec_mutual_g [x] \[] \[= x+1])). 
charguer's avatar
xfor  
charguer committed
884
  { split; intros x P; specializes G (x+1); destruct G as [G1 G2]; xapp; try math. }
charguer's avatar
recfun  
charguer committed
885 886 887 888 889 890 891 892 893
  => m. induction_wf IH: (downto 0) m. split; intros x Lx Px.
  { xcf. xif. xrets~. xapp (x-1).
    unfolds. skip. (* TODO *) skip. skip.  
    intro_subst. xrets. skip. }
  { xcf. xapp x. unfolds. skip. (* TODO *) skip. skip. }
Qed.



charguer's avatar
xgc  
charguer committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
(********************************************************************)
(* ** Reference and garbage collection *)

Lemma ref_gc_spec : 
  app ref_gc [tt] \[] \[= 3].
Proof using.
  xcf.
  xapp.
  xapp.
  xapp.
  xapp.
  dup 4.
  { xgc (r3 ~~> 1). skip. }
  { xgc r3. skip. }
  { 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
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
(********************************************************************)
(* ** 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
934 935 936 937 938 939 940 941
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
942 943 944
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
945
     (# (r ~> `{ nb' := n+1; items' := L })). 
charguer's avatar
array  
charguer committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
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 ].

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. xunfold Sitems. xextract ;=> n E. 
  xapps. xapps. xapps. xapp. xsimpl. rew_list; math.
Qed.

charguer's avatar
xwhile  
charguer committed
972 973 974 975 976
(* TODO: enéoncé spec dérivée pour
App' r`.nb'
en terme de Sitems  

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

charguer's avatar
xwhile  
charguer committed
978 979 980
(** Demo of [xopen] and [xclose] *)

Lemma Sitems_open : forall r A (L:list A),
charguer's avatar
array  
charguer committed
981 982 983 984
  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
985
Lemma Sitems_close : forall r A (L:list A) (n:int),
charguer's avatar
array  
charguer committed
986 987 988 989 990
  n = LibListZ.length L ->
  r ~> `{ nb' := n; items' := L } ==> 
  r ~> Sitems L.
Proof using. intros. xunfolds~ Sitems. Qed.

charguer's avatar
xwhile  
charguer committed
991 992 993 994 995 996
Implicit Arguments Sitems_close [].
(* TODO comment
r ~> Sitems _ 
xopen r   
xchange (Sitems_open r).
*)
charguer's avatar
array  
charguer committed
997

charguer's avatar
charguer committed
998
Hint Extern 1 (RegisterOpen (Sitems _)) => 
charguer's avatar
xwhile  
charguer committed
999
  Provide Sitems_open.
charguer's avatar
charguer committed
1000
Hint Extern 1 (RegisterClose (record_repr `{ nb' := _; items' := _ }')) =>  
charguer's avatar
xwhile  
charguer committed
1001
  Provide Sitems_close.
charguer's avatar
array  
charguer committed
1002 1003 1004 1005 1006

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
xwhile  
charguer committed
1007 1008 1009
  { xopen r. xextract ;=> n E. skip. } 
  { xopenx r ;=> n E. xapps. xapps. xapps. xapp.
    xclose r. rew_list; math. xsimpl~. }
charguer's avatar
array  
charguer committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
Qed.


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

Require Import Array_ml Array_proof.

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.
  xapp. math. => L EL. 
  asserts LL: (length L = 3). subst. rewrite length_make; math. 
  xapps. { apply index_bounds_impl; math. }
  xapp~.
  xapps~.
  xapps~.
  xapps~.
  xsimpl. subst. rew_arr~. 
Qed.

End Array.

charguer's avatar
demo3  
charguer committed
1040

charguer's avatar
charguer committed
1041

charguer's avatar
init  
charguer committed
1042

charguer's avatar
charguer committed
1043
(********************************************************************)
charguer's avatar
xpat  
charguer committed
1044 1045
(********************************************************************)
(********************************************************************)
charguer's avatar
init  
charguer committed
1046

charguer's avatar
xpat  
charguer committed
1047
(*
charguer's avatar
init  
charguer committed
1048 1049


charguer's avatar
charguer committed
1050 1051
(********************************************************************)
(* ** Partial applications *)
charguer's avatar
init  
charguer committed
1052

charguer's avatar
charguer committed
1053 1054 1055 1056 1057 1058
Lemma app_partial_2_1 () =
   let f x y = (x,y) in
   f 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
1059

charguer's avatar
charguer committed
1060 1061 1062 1063 1064 1065
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
1066

charguer's avatar
charguer committed
1067 1068 1069 1070 1071 1072
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
1073

charguer's avatar
charguer committed
1074 1075 1076 1077 1078 1079 1080
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
1081

charguer's avatar
charguer committed
1082 1083 1084 1085 1086 1087 1088 1089 1090
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
1091

charguer's avatar
charguer committed
1092 1093 1094 1095 1096
Lemma app_partial_builtin () =
  let f = (+) 1 in
  f 2
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
1097 1098 1099
Qed.


charguer's avatar
cp  
charguer committed
1100 1101 1102 1103 1104 1105 1106
let app_partial_builtin_and () =
  let f = (&&) true in
  f false




charguer's avatar
charguer committed
1107 1108 1109 1110 1111 1112 1113 1114
(********************************************************************)
(* ** Over applications *)

Lemma app_over_id () =
   let f x = x in
   f f 3
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
1115 1116 1117 1118
Qed.



charguer's avatar
charguer committed
1119

charguer's avatar
charguer committed
1120

charguer's avatar
demo  
charguer committed
1121
*)