Demo_proof.v 34.2 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
ok    
charguer committed
6

charguer's avatar
charguer committed
7
(* Open Scope tag_scope.*)
charguer's avatar
xwhile    
charguer committed
8
9


charguer's avatar
charguer committed
10
11
12
13
14
15
16
17
(*

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

let compare_min () =
  (min 0 1)

charguer's avatar
polylet    
charguer committed
18

charguer's avatar
charguer committed
19
(********************************************************************)
charguer's avatar
charguer committed
20
21
22
23
24
25
26
27
28
(* ** List operators *)

let list_ops () =
  let x = [1] in
  List.length (List.rev (List.concat (List.append [x] [x; x])))


*)

charguer's avatar
polylet    
charguer committed
29
30
31
32




charguer's avatar
charguer committed
33
34
35
36
37
38
(********************************************************************)
(********************************************************************)
(********************************************************************)



charguer's avatar
charguer committed
39
40
41
42
43




(*
charguer's avatar
charguer committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
60
61
62
63
64
65
66
67
68


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




charguer's avatar
charguer committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(********************************************************************)
(* ** 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
93
94


charguer's avatar
charguer committed
95
96
97
(********************************************************************)
(* ** Polymorphic let bindings and value restriction *)

charguer's avatar
polylet    
charguer committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
175
176
Lemma let_poly_h0_spec : forall A,
  app let_poly_h0 [tt] \[] (fun (r:loc) => r ~~> (@nil A)).
charguer's avatar
polylet    
charguer committed
177
Proof using.
charguer's avatar
charguer committed
178
  xcf. xapps. xret~.
charguer's avatar
polylet    
charguer committed
179
180
Qed.

charguer's avatar
charguer committed
181
182
183
184
185
186
187
188
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
189

charguer's avatar
charguer committed
190
191
192
193
194
195
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
196

charguer's avatar
charguer committed
197
198
199
200
201
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
202

charguer's avatar
charguer committed
203
204
205
206
207
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
208

charguer's avatar
charguer committed
209
210
211
212
213
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
214

charguer's avatar
charguer committed
215
216
217
218
219
220
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
221

charguer's avatar
charguer committed
222
223
Lemma let_poly_r2_spec : forall A,
  app let_poly_r2 [tt] \[] \[= @nil A].
charguer's avatar
charguer committed
224
Proof using.
charguer's avatar
charguer committed
225
226
227
228
229
  xcf. xapps. dup 2.
  { xval. xrets~. }
  { xvals. xrets~. }
  Unshelve. solve_type.
Qed.
charguer's avatar
charguer committed
230

charguer's avatar
charguer committed
231
232
233

Lemma let_poly_r3_spec : forall A,
  app let_poly_r3 [tt] \[] \[= @nil A].
charguer's avatar
charguer committed
234
Proof using.
charguer's avatar
charguer committed
235
236
237
  xcf. xlet_poly_keep (fun A (r:list A) => r = nil).
  { xapps. xrets~. }
  intros Hr. xrets. auto.
charguer's avatar
charguer committed
238
239
240
Qed.


charguer's avatar
charguer committed
241

charguer's avatar
charguer committed
242
243
(********************************************************************)
(* ** Top-level values *)
charguer's avatar
ok    
charguer committed
244

charguer's avatar
charguer committed
245
246
247
248
249
250
251
252
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
253
  xcf_show top_val_int as M. skip.
charguer's avatar
charguer committed
254
255
  xcf. skip.
Qed.
charguer's avatar
ok    
charguer committed
256

charguer's avatar
charguer committed
257
258
259
260
261
Lemma top_val_int_list_spec : 
  top_val_int_list = @nil int.
Proof using.
  xcf. auto.
Qed.
charguer's avatar
ok    
charguer committed
262

charguer's avatar
charguer committed
263
264
265
Lemma top_val_poly_list_spec : forall A,
  top_val_poly_list = @nil A.
Proof using. xcf*. Qed.
charguer's avatar
init  
charguer committed
266

charguer's avatar
charguer committed
267
268
269
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
270

charguer's avatar
init  
charguer committed
271

charguer's avatar
xpat    
charguer committed
272

charguer's avatar
charguer committed
273
(********************************************************************)
charguer's avatar
xpat    
charguer committed
274
(* ** Return *)
charguer's avatar
init  
charguer committed
275

charguer's avatar
xpat    
charguer committed
276
277
Lemma ret_unit_spec : 
  app ret_unit [tt] \[] \[= tt]. (* (fun (_:unit) => \[]).*) (* same as (# \[]). *)
charguer's avatar
charguer committed
278
Proof using.
charguer's avatar
xwhile    
charguer committed
279
  xcf. dup 5. (* TODO : accolade *)
charguer's avatar
xpat    
charguer committed
280
281
282
283
284
285
  xret. xsimpl. auto.
  (* demos *)
  xrets. auto.
  xrets*.
  xret_no_gc. xsimpl. auto.
  xret_no_clean. xsimpl*. 
charguer's avatar
charguer committed
286
Qed.
charguer's avatar
ok    
charguer committed
287

charguer's avatar
xpat    
charguer committed
288
289
290
Lemma ret_int_spec : 
  app ret_int [tt] \[] \[= 3].
Proof using. xcf. xrets*. Qed.
charguer's avatar
ok    
charguer committed
291

charguer's avatar
xpat    
charguer committed
292
293
Lemma ret_int_pair_spec :
  app ret_int_pair [tt] \[] \[= (3,4)].
charguer's avatar
xwhile    
charguer committed
294
Proof using. xcf_go*. Qed.
charguer's avatar
ok    
charguer committed
295

charguer's avatar
xpat    
charguer committed
296
297
Lemma ret_poly_spec : forall A,
  app ret_poly [tt] \[] \[= @nil A].
charguer's avatar
xwhile    
charguer committed
298
Proof using. xcf. xgo*. Qed.
charguer's avatar
ok    
charguer committed
299
300


charguer's avatar
xlet    
charguer committed
301
302
303
304
305
306
307
308
(********************************************************************)
(* ** 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
309

charguer's avatar
xlet    
charguer committed
310
311
312
313
314
315
316
317
318
319
320
321
322
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
323
  { xseq. xapp. xapp. xsimpl. auto. }
charguer's avatar
demo1    
charguer committed
324
325
  { xapp. intro_subst. xapp. }
  { xapps. xapps. }
charguer's avatar
xlet    
charguer committed
326
327
328
329
  { xapps. xapps~. }
Qed.


charguer's avatar
ok    
charguer committed
330

charguer's avatar
charguer committed
331
(********************************************************************)
charguer's avatar
xpat    
charguer committed
332
(* ** Let-value *)
charguer's avatar
init  
charguer committed
333

charguer's avatar
xpat    
charguer committed
334
335
Lemma let_val_int_spec : 
  app let_val_int [tt] \[] \[= 3].
charguer's avatar
charguer committed
336
Proof using.
charguer's avatar
xpat    
charguer committed
337
338
339
340
341
342
343
344
345
  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
346
347
Qed.

charguer's avatar
xpat    
charguer committed
348
349
350
351
352
353
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
354
Proof using.
charguer's avatar
xpat    
charguer committed
355
  xcf. dup 3.
charguer's avatar
demo1    
charguer committed
356
357
358
  { xval. xret. xsimpl. auto. }
  { xval as r. xrets~. } 
  { xvals. xrets~. }
charguer's avatar
charguer committed
359
Qed.
charguer's avatar
init  
charguer committed
360
361


charguer's avatar
xlet    
charguer committed
362
363
364
365
366
367
(********************************************************************)
(* ** Let-function *)

Lemma let_fun_const_spec : 
  app let_fun_const [tt] \[] \[= 3].
Proof using.
charguer's avatar
PRE    
charguer committed
368
369
  xcf. dup 10.
  { xfun. apply Sf. xtag_goal. xrets~. }
charguer's avatar
xlet    
charguer committed
370
  { xfun as g. apply Sg. skip. }
charguer's avatar
PRE    
charguer committed
371
  { xfun as g. xapp. xret. skip. }
charguer's avatar
xlet    
charguer committed
372
373
  { xfun as g G. apply G. skip. }
  { xfun_no_simpl (fun g => app g [tt] \[] \[=3]).
charguer's avatar
PRE    
charguer committed
374
    { xapp. skip. } 
charguer's avatar
xlet    
charguer committed
375
376
377
378
379
    { 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
380
381
    { xapp. skip. } 
    { xapp. } }
charguer's avatar
xlet    
charguer committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  { 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
401
402
403
404
405
    { 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
430
431
432
433
434
435
436
437
438
439
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
440

charguer's avatar
PRE    
charguer committed
441
442
443
444
445
446
447
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
448
449
450
451
452
453
454
455
456
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
457
458
459
460
461
462
463
464

(********************************************************************)
(* ** 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
465
  xfun (fun f => forall (x:int), app f [x] \[] \[= x]). { xrets~. }
charguer's avatar
xlet    
charguer committed
466
467
468
469
470
471
472
473
474
475
  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
476
  xfun (fun f => forall A B (x:A) (y:B), app f [x y] \[] \[= (x,y)]). { xrets~. }
charguer's avatar
xlet    
charguer committed
477
478
479
480
481
482
483
  xapps.
  xapps.
  xapps.
  xapps.
  xrets~.
Qed.

charguer's avatar
charguer committed
484
(********************************************************************)
charguer's avatar
xpat    
charguer committed
485
(* ** Pattern-matching *)
charguer's avatar
init  
charguer committed
486

charguer's avatar
xpat    
charguer committed
487
488
489
490
491
492
493
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
494
495
  { xmatch_no_cases. dup 6. 
    { xmatch_case.
charguer's avatar
demo1    
charguer committed
496
      { xrets*. } 
charguer's avatar
charguer committed
497
      { xmatch_case. } }
charguer's avatar
xpat    
charguer committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    { 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
513
  { xmatch_no_intros. skip. }
charguer's avatar
xpat    
charguer committed
514
515
516
517
518
519
520
521
  { 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
522
    { xrets*. } 
charguer's avatar
xpat    
charguer committed
523
524
525
    { false. (* note: [xrets] would produce a ununified [hprop]. 
     caused by [tryfalse] in [hextract_cleanup]. TODO: avoid this. *) } }
  { xmatch.
charguer's avatar
demo1    
charguer committed
526
    xrets*. 
charguer's avatar
xpat    
charguer committed
527
528
    (* second case is killed by [xcase_post] *) }
  { xmatch_no_intros. skip. skip. }
charguer's avatar
charguer committed
529
Qed.
charguer's avatar
init  
charguer committed
530

charguer's avatar
demo    
charguer committed
531

charguer's avatar
charguer committed
532
533
534
535
536
537
538
539
540
541
542
543
(********************************************************************)
(* ** 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
544
545
546
(********************************************************************)
(* ** Infix functions *)
 
charguer's avatar
charguer committed
547
548
Lemma infix_plus_plus_plus_spec : forall x y,
  app infix_plus_plus_plus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
549
Proof using.
charguer's avatar
xwhile    
charguer committed
550
  xcf_go~.
charguer's avatar
charguer committed
551
552
Qed.

charguer's avatar
charguer committed
553
Hint Extern 1 (RegisterSpec infix_plus_plus_plus__) => Provide infix_plus_plus_plus_spec.
charguer's avatar
charguer committed
554
555
556
557
558
559
560
561
562

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
563
564
Lemma infix_minus_minus_minus_spec : forall x y,
  app infix_minus_minus_minus__ [x y] \[] \[= x + y].
charguer's avatar
charguer committed
565
566
567
Proof using.
  intros. xcf_show as S. rewrite S. xapps~.
Qed.
charguer's avatar
charguer committed
568
569


charguer's avatar
charguer committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

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

Lemma compare_poly_spec : 
  app compare_poly [tt] \[] \[= tt].
Proof using.
  xcf.
  xlet_poly_keep (= true). { xapps. typeclass. xsimpl. subst r. logics~. } intro_subst.
  xapp. typeclass. intro_subst.
  xlet_poly_keep (= true). { xapps. typeclass. xsimpl. subst r. logics~. } intro_subst.
  xapp. typeclass. intro_subst.
  xrets~.
Qed. 

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
toutbon    
charguer committed
617
    { xapps~. xret. xpulls. xif.
charguer's avatar
charguer committed
618
619
620
621
622
623
624
625
626
      { 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
(********************************************************************)
(* ** 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
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
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
(********************************************************************)
(* ** 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
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
811
(********************************************************************)
(* ** 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
812
  xcf. xlet. xassert. { xrets. } xrets. 
charguer's avatar
toutbon    
charguer committed
813
  xpulls. xrets~.
charguer's avatar
demo3    
charguer committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
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
829
  xcf. xfun. xapp. xret. xpulls.
charguer's avatar
demo3    
charguer committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
  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
848
   { (*xclean.*) xpull ;=>> P. xapp. xpulls. xsimpl. math. }
charguer's avatar
demo3    
charguer committed
849
850
851
Qed.


charguer's avatar
charguer committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
(********************************************************************)
(* ** 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. }
charguer's avatar
toutbon    
charguer committed
866
    { xpulls. xif. 
charguer's avatar
charguer committed
867
868
869
870
871
872
873
      { 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. }
charguer's avatar
toutbon    
charguer committed
874
    { intros S LS b k FS. xpull. intros. xlet. 
charguer's avatar
charguer committed
875
      { xapps. xrets. }
charguer's avatar
toutbon    
charguer committed
876
      { xpulls. xif. 
charguer's avatar
charguer committed
877
878
879
880
881
882
883
        { 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. }
charguer's avatar
toutbon    
charguer committed
884
885
    { intros b k. xpull ;=> Hk Hb. xapps. xrets. xauto*. math. }
    { intros k. xpull ;=> Hk Hb. xapps. xapps. xsimpl. skip. eauto. skip. hnf. skip. } 
charguer's avatar
charguer committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
    { => 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*. }
charguer's avatar
toutbon    
charguer committed
918
919
    { intros. xpulls. xrets~. } 
    { intros. xpull. auto_false. }
charguer's avatar
charguer committed
920
921
922
923
924
925
    { xsimpl~. }
  }
Qed.



charguer's avatar
charguer committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
(********************************************************************)
(* ** 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
997
998
999
1000
1001
1002
1003
1004
1005
(********************************************************************)
(* ** Lazy binary operators *)

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

charguer's avatar
xfor    
charguer committed
1006
1007
1008
1009
(*
Ltac xauto_tilde ::= xauto_tilde_default ltac:(fun _ => auto_tilde).
*)

charguer's avatar
demo3    
charguer committed
1010
1011
1012
1013
1014
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
1015
  { xrets*. }
charguer's avatar
demo3    
charguer committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  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? *)
  }
charguer's avatar
toutbon    
charguer committed
1026
  xpulls. xif. xrets~.
charguer's avatar
demo3    
charguer committed
1027
1028
1029
1030
1031
1032
1033
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
1034
  { xrets*. }
charguer's avatar
demo3    
charguer committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  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
1052
1053
1054
  dup 2. 
    {
    xcf. xapps. xfun. xfun. xfun.
charguer's avatar
toutbon    
charguer committed
1055
1056
    xapps. { xapps. xrets~. } xpulls.
    xapps. { xassert. xapps. xrets~. xapps. xrets~. } xpulls.
charguer's avatar
xwhile    
charguer committed
1057
1058
    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
1059
      xpull ;=> Hf.  
charguer's avatar
xwhile    
charguer committed
1060
1061
1062
1063
1064
1065
1066
    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
1067
1068
1069
1070
1071
1072
1073
1074
1075
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
1076
1077
  xapps. { xapps. xrets~. } xpulls.
  xapps. { xassert. xapps. xrets~. xrets~. } xpulls.
charguer's avatar
demo3    
charguer committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
  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
1087
  app order_tuple [tt] \[] \[= (1,1)]. 
charguer's avatar
demo3    
charguer committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
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
1101
1102
1103
1104
1105
(********************************************************************)
(* ** Recursive function *)

Require Import LibInt.

charguer's avatar
PRE    
charguer committed
1106
1107
1108
Lemma rec_partial_half_spec : forall k n,
  n = 2 * k ->
  app rec_partial_half [n] \[] \[= k].
charguer's avatar
recfun    
charguer committed
1109
Proof using.
charguer's avatar
PRE    
charguer committed
1110
1111
1112
  dup 2.
  { => k. induction_wf IH: (downto 0) k. xcf.
    xif.
charguer's avatar
xfor    
charguer committed
1113
    { xrets. math. }
charguer's avatar
PRE    
charguer committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
    { 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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
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
1136
  { split; intros x P; specializes G (x+1); destruct G as [G1 G2]; xapp; try math. }
charguer's avatar
recfun    
charguer committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
  => 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
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
(********************************************************************)
(* ** 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
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
(********************************************************************)
(* ** 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
1186
1187
1188
1189
1190
1191
1192
1193
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
1194
1195
1196
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
1197
     (# (r ~> `{ nb' := n+1; items' := L })). 
charguer's avatar
array    
charguer committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
(*
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
1237
1238
1239
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
1240
  xcf. xunfold Sitems. xpull ;=> n E. 
charguer's avatar
array    
charguer committed
1241
1242
1243
  xapps. xapps. xapps. xapp. xsimpl. rew_list; math.
Qed.

charguer's avatar
xwhile    
charguer committed
1244
1245
1246
1247
1248
(* TODO: enéoncé spec dérivée pour
App' r`.nb'
en terme de Sitems  

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

charguer's avatar
xwhile    
charguer committed
1250
1251
1252
(** Demo of [xopen] and [xclose] *)

Lemma Sitems_open : forall r A (L:list A),
charguer's avatar
array    
charguer committed
1253
1254
1255
1256
  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
1257
Lemma Sitems_close : forall r A (L:list A) (n:int),
charguer's avatar
array    
charguer committed
1258
1259
1260
1261
1262
  n = LibListZ.length L ->
  r ~> `{ nb' := n; items' := L } ==> 
  r ~> Sitems L.
Proof using. intros. xunfolds~ Sitems. Qed.

charguer's avatar
xwhile    
charguer committed
1263
1264
1265
1266
1267
1268
Implicit Arguments Sitems_close [].
(* TODO comment
r ~> Sitems _ 
xopen r   
xchange (Sitems_open r).
*)
charguer's avatar
array    
charguer committed
1269

charguer's avatar
charguer committed
1270
Hint Extern 1 (RegisterOpen (Sitems _)) => 
charguer's avatar
xwhile    
charguer committed
1271
  Provide Sitems_open.
charguer's avatar
charguer committed
1272
Hint Extern 1 (RegisterClose (record_repr _)) =>  
charguer's avatar
xwhile    
charguer committed
1273
  Provide Sitems_close.
charguer's avatar
array    
charguer committed
1274
1275
1276
1277
1278

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
1279
  { xopen r. xpull ;=> n E. skip. } 
charguer's avatar
xwhile    
charguer committed
1280
1281
  { xopenx r ;=> n E. xapps. xapps. xapps. xapp.
    xclose r. rew_list; math. xsimpl~. }
charguer's avatar
array    
charguer committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
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
1312

charguer's avatar
charguer committed
1313

charguer's avatar
charguer committed
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
(********************************************************************)
(* ** Integer arithmetics *)

(* land *)

Goal Z.land 7 28 = 4.
Proof. reflexivity. Qed.

Goal Z.land (-7) 28 = 24.
Proof. reflexivity. Qed.

Goal Z.land 7 (-28) = 4.
Proof. reflexivity. Qed.

Goal Z.land (-7) (-28) = -32.
Proof. reflexivity. Qed.

(* lor *)

Goal Z.lor 7 28 = 31.
Proof. reflexivity. Qed.

Goal Z.lor (-7) 28 = -3.
Proof. reflexivity. Qed.

Goal Z.lor 7 (-28) = -25.
Proof. reflexivity. Qed.

Goal Z.lor (-7) (-28) = -3.
Proof. reflexivity. Qed.

(* lxor *)

Goal Z.lxor 7 28 = 27.
Proof. reflexivity. Qed.

Goal Z.lxor (-7) 28 = -27.
Proof. reflexivity. Qed.

Goal Z.lxor 7 (-28) = -29.
Proof. reflexivity. Qed.

Goal Z.lxor (-7) (-28) = 29.
Proof. reflexivity. Qed.

(* lnot *)

Goal lnot 44 = -45.
Proof. reflexivity. Qed.

Goal lnot (-44) = 43.
Proof. reflexivity. Qed.

(* shiftl *)

Goal Z.shiftl 7 2 = 28.
Proof. reflexivity. Qed.

Goal Z.shiftl (-7) 2 = -28.
Proof. reflexivity. Qed.

(* shiftr *)
charguer's avatar
init  
charguer committed
1376

charguer's avatar
charguer committed
1377
1378
Goal Z.shiftr 7 2 = 1.
Proof. reflexivity. Qed.
charguer's avatar
init  
charguer committed
1379

charguer's avatar
charguer committed
1380
1381
Goal Z.shiftr 7 2 = 1.
Proof. reflexivity. Qed.
charguer's avatar
init  
charguer committed
1382

charguer's avatar
charguer committed
1383
1384
Goal Z.shiftr (-7) 2 = -2.
Proof. reflexivity. Qed.
charguer's avatar
init  
charguer committed
1385
1386


charguer's avatar
charguer committed
1387

charguer's avatar
init  
charguer committed
1388

charguer's avatar
charguer committed
1389
(********************************************************************)
charguer's avatar
xpat    
charguer committed
1390
1391
(********************************************************************)
(********************************************************************)
charguer's avatar
init  
charguer committed
1392

charguer's avatar
xpat    
charguer committed
1393
(*
charguer's avatar
init  
charguer committed
1394
1395


charguer's avatar
charguer committed
1396
1397
(********************************************************************)
(* ** Partial applications *)
charguer's avatar
init  
charguer committed
1398

charguer's avatar
charguer committed
1399
1400
1401
1402
1403
1404
Lemma app_partial_2_1 () =
   let f x y = (x,y) in
   f 3
Proof using.
  xcf.
Qed.
charguer's avatar
init  
charguer committed
1405

charguer's avatar
charguer committed
1406
1407
1408
1409
1410
1411
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
1412

charguer's avatar
charguer committed
1413
1414
1415
1416
1417
1418
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
1419

charguer's avatar
charguer committed
1420
1421
1422
1423
1424
1425
1426
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
1427

charguer's avatar
charguer committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
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
1437

charguer's avatar
charguer committed
1438
1439
1440
1441
1442
Lemma app_partial_builtin () =
  let f = (+) 1 in
  f 2
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
1443
1444
1445
Qed.


charguer's avatar
cp    
charguer committed
1446
1447
1448
1449
1450
1451
1452
let app_partial_builtin_and () =
  let f = (&&) true in
  f false




charguer's avatar
charguer committed
1453
1454
1455
1456
1457
1458
1459
1460
(********************************************************************)
(* ** Over applications *)

Lemma app_over_id () =
   let f x = x in
   f f 3
Proof using.
  xcf.
charguer's avatar
init  
charguer committed
1461
1462
1463
1464
Qed.



charguer's avatar
charguer committed
1465

charguer's avatar
charguer committed
1466

charguer's avatar
demo    
charguer committed
1467
*)