~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to test-suite/success/extraction.v

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(************************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
 
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
 
 
9
Require Import Arith.
 
10
Require Import List.
 
11
 
 
12
(**** A few tests for the extraction mechanism ****) 
 
13
 
 
14
(* Ideally, we should monitor the extracted output 
 
15
   for changes, but this is painful. For the moment, 
 
16
   we just check for failures of this script. *)
 
17
 
 
18
(*** STANDARD EXAMPLES *)
 
19
 
 
20
(** Functions. *)
 
21
 
 
22
Definition idnat (x:nat) := x.
 
23
Extraction idnat.
 
24
(* let idnat x = x *)
 
25
 
 
26
Definition id (X:Type) (x:X) := x.      
 
27
Extraction id. (* let id x = x *)
 
28
Definition id' := id Set nat.
 
29
Extraction id'. (* type id' = nat *)
 
30
 
 
31
Definition test2 (f:nat -> nat) (x:nat) := f x.
 
32
Extraction test2.
 
33
(* let test2 f x = f x *)
 
34
 
 
35
Definition test3 (f:nat -> Set -> nat) (x:nat) := f x nat.
 
36
Extraction test3.
 
37
(* let test3 f x = f x __ *)
 
38
 
 
39
Definition test4 (f:(nat -> nat) -> nat) (x:nat) (g:nat -> nat) := f g.
 
40
Extraction test4.
 
41
(* let test4 f x g = f g *)
 
42
 
 
43
Definition test5 := (1, 0).
 
44
Extraction test5.
 
45
(* let test5 = Pair ((S O), O) *)
 
46
 
 
47
Definition cf (x:nat) (_:x <= 0) := S x.
 
48
Extraction NoInline cf.
 
49
Definition test6 := cf 0 (le_n 0).
 
50
Extraction test6.  
 
51
(* let test6 = cf O *)
 
52
 
 
53
Definition test7 := (fun (X:Set) (x:X) => x) nat.
 
54
Extraction test7.
 
55
(* let test7 x = x *)
 
56
 
 
57
Definition d (X:Type) := X.
 
58
Extraction d. (* type 'x d = 'x *)
 
59
Definition d2 := d Set.
 
60
Extraction d2. (* type d2 = __ d *)
 
61
Definition d3 (x:d Set) := 0.
 
62
Extraction d3. (* let d3 _ = O *)
 
63
Definition d4 := d nat. 
 
64
Extraction d4. (* type d4 = nat d *)
 
65
Definition d5 := (fun x:d Type => 0) Type.  
 
66
Extraction d5.  (* let d5 = O *)
 
67
Definition d6 (x:d Type) := x.
 
68
Extraction d6.  (* type 'x d6 = 'x *)
 
69
 
 
70
Definition test8 := (fun (X:Type) (x:X) => x) Set nat.
 
71
Extraction test8. (* type test8 = nat *)
 
72
 
 
73
Definition test9 := let t := nat in id Set t.
 
74
Extraction test9.  (* type test9 = nat *)
 
75
 
 
76
Definition test10 := (fun (X:Type) (x:X) => 0) Type Type.
 
77
Extraction test10. (* let test10 = O *)
 
78
 
 
79
Definition test11 := let n := 0 in let p := S n in S p.
 
80
Extraction test11. (* let test11 = S (S O) *)
 
81
 
 
82
Definition test12 := forall x:forall X:Type, X -> X, x Type Type.
 
83
Extraction test12. 
 
84
(* type test12 = (__ -> __ -> __) -> __ *)
 
85
 
 
86
 
 
87
Definition test13 := match @left True True I with
 
88
                     | left x => 1
 
89
                     | right x => 0
 
90
                     end.
 
91
Extraction test13. (* let test13 = S O *)
 
92
 
 
93
 
 
94
(** example with more arguments that given by the type *)
 
95
 
 
96
Definition test19 :=
 
97
  nat_rec (fun n:nat => nat -> nat) (fun n:nat => 0)
 
98
    (fun (n:nat) (f:nat -> nat) => f) 0 0.
 
99
Extraction test19.
 
100
(* let test19 =
 
101
  let rec f = function
 
102
    | O -> (fun n0 -> O)
 
103
    | S n0 -> f n0
 
104
  in f O O
 
105
*)
 
106
 
 
107
 
 
108
(** casts *)
 
109
 
 
110
Definition test20 := True:Type.
 
111
Extraction test20.
 
112
(* type test20 = __ *)
 
113
 
 
114
 
 
115
(** Simple inductive type and recursor. *)
 
116
 
 
117
Extraction nat.
 
118
(* 
 
119
type nat = 
 
120
  | O 
 
121
  | S of nat 
 
122
*)
 
123
 
 
124
Extraction sumbool_rect.
 
125
(* 
 
126
let sumbool_rect f f0 = function
 
127
  |  Left -> f __
 
128
  | Right -> f0 __
 
129
*)
 
130
 
 
131
(** Less simple inductive type. *)
 
132
 
 
133
Inductive c (x:nat) : nat -> Set :=
 
134
  | refl : c x x
 
135
  | trans : forall y z:nat, c x y -> y <= z -> c x z.
 
136
Extraction c.
 
137
(* 
 
138
type c =
 
139
  | Refl
 
140
  | Trans of nat * nat * c
 
141
*)
 
142
 
 
143
Definition Ensemble (U:Type) := U -> Prop.
 
144
Definition Empty_set (U:Type) (x:U) := False.
 
145
Definition Add (U:Type) (A:Ensemble U) (x y:U) := A y \/ x = y.
 
146
 
 
147
Inductive Finite (U:Type) : Ensemble U -> Type :=
 
148
  | Empty_is_finite : Finite U (Empty_set U)
 
149
  | Union_is_finite :
 
150
      forall A:Ensemble U,
 
151
        Finite U A -> forall x:U, ~ A x -> Finite U (Add U A x).
 
152
Extraction Finite.
 
153
(* 
 
154
type 'u finite =
 
155
  | Empty_is_finite
 
156
  | Union_is_finite of 'u finite * 'u
 
157
*)
 
158
 
 
159
 
 
160
(** Mutual Inductive *)
 
161
 
 
162
Inductive tree : Set :=
 
163
    Node : nat -> forest -> tree
 
164
with forest : Set :=
 
165
  | Leaf : nat -> forest
 
166
  | Cons : tree -> forest -> forest.
 
167
 
 
168
Extraction tree.
 
169
(* 
 
170
type tree =
 
171
  | Node of nat * forest
 
172
and forest =
 
173
  | Leaf of nat
 
174
  | Cons of tree * forest
 
175
*)
 
176
 
 
177
Fixpoint tree_size (t:tree) : nat :=
 
178
  match t with
 
179
  | Node a f => S (forest_size f)
 
180
  end
 
181
 
 
182
 with forest_size (f:forest) : nat :=
 
183
  match f with
 
184
  | Leaf b => 1
 
185
  | Cons t f' => tree_size t + forest_size f'
 
186
  end.
 
187
 
 
188
Extraction tree_size.
 
189
(* 
 
190
let rec tree_size = function
 
191
  | Node (a, f) -> S (forest_size f)
 
192
and forest_size = function
 
193
  | Leaf b -> S O
 
194
  | Cons (t, f') -> plus (tree_size t) (forest_size f')
 
195
*)
 
196
 
 
197
 
 
198
(** Eta-expansions of inductive constructor *)
 
199
 
 
200
Inductive titi : Set :=
 
201
    tata : nat -> nat -> nat -> nat -> titi.
 
202
Definition test14 := tata 0.
 
203
Extraction test14.
 
204
(* let test14 x x0 x1 = Tata (O, x, x0, x1) *)
 
205
Definition test15 := tata 0 1.
 
206
Extraction test15. 
 
207
(* let test15 x x0 = Tata (O, (S O), x, x0) *)
 
208
 
 
209
Inductive eta : Type :=
 
210
    eta_c : nat -> Prop -> nat -> Prop -> eta.
 
211
Extraction eta_c.
 
212
(* 
 
213
type eta =
 
214
  | Eta_c of nat * nat
 
215
*)
 
216
Definition test16 := eta_c 0.
 
217
Extraction test16.
 
218
(* let test16 x = Eta_c (O, x) *)
 
219
Definition test17 := eta_c 0 True.
 
220
Extraction test17.
 
221
(* let test17 x = Eta_c (O, x) *)
 
222
Definition test18 := eta_c 0 True 0.
 
223
Extraction test18. 
 
224
(* let test18 _ = Eta_c (O, O) *)
 
225
 
 
226
 
 
227
(** Example of singleton inductive type *)
 
228
 
 
229
Inductive bidon (A:Prop) (B:Type) : Type :=
 
230
    tb : forall (x:A) (y:B), bidon A B. 
 
231
Definition fbidon (A B:Type) (f:A -> B -> bidon True nat) 
 
232
  (x:A) (y:B) := f x y.
 
233
Extraction bidon.
 
234
(* type 'b bidon = 'b *)
 
235
Extraction tb.
 
236
(* tb : singleton inductive constructor *)
 
237
Extraction fbidon.
 
238
(* let fbidon f x y =
 
239
  f x y
 
240
*)
 
241
 
 
242
Definition fbidon2 := fbidon True nat (tb True nat).
 
243
Extraction fbidon2. (* let fbidon2 y = y *)
 
244
Extraction NoInline fbidon.
 
245
Extraction fbidon2.
 
246
(* let fbidon2 y = fbidon (fun _ x -> x) __ y *)
 
247
 
 
248
(* NB: first argument of fbidon2 has type [True], so it disappears. *)
 
249
 
 
250
(** mutual inductive on many sorts *)
 
251
 
 
252
Inductive test_0 : Prop :=
 
253
    ctest0 : test_0
 
254
with test_1 : Set :=
 
255
    ctest1 : test_0 -> test_1.  
 
256
Extraction test_0.
 
257
(* test0 : logical inductive *)
 
258
Extraction test_1. 
 
259
(* 
 
260
type test1 =
 
261
  | Ctest1
 
262
*)
 
263
 
 
264
(** logical singleton *)
 
265
 
 
266
Extraction eq.
 
267
(* eq : logical inductive *)
 
268
Extraction eq_rect.
 
269
(* let eq_rect x f y =
 
270
  f
 
271
*)
 
272
 
 
273
(** No more propagation of type parameters. Obj.t instead. *)
 
274
 
 
275
Inductive tp1 : Type :=
 
276
    T : forall (C:Set) (c:C), tp2 -> tp1
 
277
with tp2 : Type :=
 
278
    T' : tp1 -> tp2.
 
279
Extraction tp1.
 
280
(* 
 
281
type tp1 =
 
282
  | T of __ * tp2
 
283
and tp2 =
 
284
  | T' of tp1
 
285
*) 
 
286
 
 
287
Inductive tp1bis : Type :=
 
288
    Tbis : tp2bis -> tp1bis
 
289
with tp2bis : Type :=
 
290
    T'bis : forall (C:Set) (c:C), tp1bis -> tp2bis.
 
291
Extraction tp1bis.
 
292
(* 
 
293
type tp1bis =
 
294
  | Tbis of tp2bis
 
295
and tp2bis =
 
296
  | T'bis of __ * tp1bis
 
297
*)
 
298
 
 
299
 
 
300
(** Strange inductive type. *)
 
301
 
 
302
Inductive Truc : Set -> Type :=
 
303
  | chose : forall A:Set, Truc A
 
304
  | machin : forall A:Set, A -> Truc bool -> Truc A.
 
305
Extraction Truc.
 
306
(*
 
307
type 'x truc =
 
308
  | Chose
 
309
  | Machin of 'x * bool truc
 
310
*)
 
311
 
 
312
 
 
313
(** Dependant type over Type *)
 
314
 
 
315
Definition test24 := sigT (fun a:Set => option a).
 
316
Extraction test24.
 
317
(* type test24 = (__, __ option) sigT *)
 
318
 
 
319
 
 
320
(** Coq term non strongly-normalizable after extraction *)
 
321
 
 
322
Require Import Gt.
 
323
Definition loop (Ax:Acc gt 0) :=
 
324
  (fix F (a:nat) (b:Acc gt a) {struct b} : nat :=
 
325
     F (S a) (Acc_inv b (S a) (gt_Sn_n a))) 0 Ax.
 
326
Extraction loop.
 
327
(* let loop _ =
 
328
  let rec f a =
 
329
    f (S a)
 
330
  in f O
 
331
*)
 
332
 
 
333
(*** EXAMPLES NEEDING OBJ.MAGIC *)
 
334
 
 
335
(** False conversion of type: *)
 
336
 
 
337
Lemma oups : forall H:nat = list nat, nat -> nat.
 
338
intros.
 
339
generalize H0; intros.
 
340
rewrite H in H1.
 
341
case H1.
 
342
exact H0.
 
343
intros.
 
344
exact n.
 
345
Qed.
 
346
Extraction oups.
 
347
(* 
 
348
let oups h0 = 
 
349
  match Obj.magic h0 with
 
350
    | Nil -> h0
 
351
    | Cons0 (n, l) -> n
 
352
*)
 
353
 
 
354
 
 
355
(** hybrids *)
 
356
 
 
357
Definition horibilis (b:bool) :=
 
358
  if b as b return (if b then Type else nat) then Set else 0.
 
359
Extraction horibilis.
 
360
(* 
 
361
let horibilis = function
 
362
  | True -> Obj.magic __
 
363
  | False -> Obj.magic O
 
364
*)
 
365
 
 
366
Definition PropSet (b:bool) := if b then Prop else Set.
 
367
Extraction PropSet. (* type propSet = __ *)
 
368
 
 
369
Definition natbool (b:bool) := if b then nat else bool.
 
370
Extraction natbool. (* type natbool = __ *)
 
371
 
 
372
Definition zerotrue (b:bool) := if b as x return natbool x then 0 else true.
 
373
Extraction zerotrue. 
 
374
(* 
 
375
let zerotrue = function
 
376
  | True -> Obj.magic O
 
377
  | False -> Obj.magic True
 
378
*)
 
379
 
 
380
Definition natProp (b:bool) := if b return Type then nat else Prop.
 
381
 
 
382
Definition natTrue (b:bool) := if b return Type then nat else True.
 
383
 
 
384
Definition zeroTrue (b:bool) := if b as x return natProp x then 0 else True.
 
385
Extraction zeroTrue.
 
386
(* 
 
387
let zeroTrue = function
 
388
  | True -> Obj.magic O
 
389
  | False -> Obj.magic __
 
390
*)
 
391
 
 
392
Definition natTrue2 (b:bool) := if b return Type then nat else True.
 
393
 
 
394
Definition zeroprop (b:bool) := if b as x return natTrue x then 0 else I.
 
395
Extraction zeroprop.
 
396
(* 
 
397
let zeroprop = function
 
398
  | True -> Obj.magic O
 
399
  | False -> Obj.magic __
 
400
*)
 
401
 
 
402
(** polymorphic f applied several times *)
 
403
 
 
404
Definition test21 := (id nat 0, id bool true).
 
405
Extraction test21.
 
406
(* let test21 = Pair ((id O), (id True)) *)
 
407
 
 
408
(** ok *)
 
409
 
 
410
Definition test22 :=
 
411
  (fun f:forall X:Type, X -> X => (f nat 0, f bool true))
 
412
    (fun (X:Type) (x:X) => x).
 
413
Extraction test22. 
 
414
(* let test22 = 
 
415
  let f = fun x -> x in Pair ((f O), (f True)) *)
 
416
 
 
417
(* still ok via optim beta -> let *)
 
418
 
 
419
Definition test23 (f:forall X:Type, X -> X) := (f nat 0, f bool true).
 
420
Extraction test23.
 
421
(* let test23 f = Pair ((Obj.magic f __ O), (Obj.magic f __ True)) *)
 
422
 
 
423
(* problem: fun f -> (f 0, f true) not legal in ocaml *)
 
424
(* solution: magic ... *)
 
425
 
 
426
 
 
427
(** Dummy constant __ can be applied.... *)
 
428
 
 
429
Definition f (X:Type) (x:nat -> X) (y:X -> bool) : bool := y (x 0).
 
430
Extraction f.
 
431
(* let f x y =
 
432
  y (x O)
 
433
*)
 
434
 
 
435
Definition f_prop := f (0 = 0) (fun _ => refl_equal 0) (fun _ => true).
 
436
Extraction NoInline f.
 
437
Extraction f_prop.
 
438
(* let f_prop =
 
439
  f (Obj.magic __) (fun _ -> True)
 
440
*)
 
441
 
 
442
Definition f_arity := f Set (fun _:nat => nat) (fun _:Set => true).
 
443
Extraction f_arity.
 
444
(* let f_arity =
 
445
  f (Obj.magic __) (fun _ -> True)
 
446
*)
 
447
 
 
448
Definition f_normal :=
 
449
  f nat (fun x => x) (fun x => match x with
 
450
                               | O => true
 
451
                               | _ => false
 
452
                               end).
 
453
Extraction f_normal.
 
454
(* let f_normal =
 
455
  f (fun x -> x) (fun x -> match x with
 
456
                             | O -> True
 
457
                             | S n -> False)
 
458
*)
 
459
 
 
460
 
 
461
(* inductive with magic needed *)
 
462
 
 
463
Inductive Boite : Set :=
 
464
    boite : forall b:bool, (if b then nat else (nat * nat)%type) -> Boite. 
 
465
Extraction Boite. 
 
466
(*
 
467
type boite =
 
468
  | Boite of bool * __
 
469
*)
 
470
 
 
471
 
 
472
Definition boite1 := boite true 0.
 
473
Extraction boite1.
 
474
(* let boite1 = Boite (True, (Obj.magic O)) *)
 
475
 
 
476
Definition boite2 := boite false (0, 0).
 
477
Extraction boite2.
 
478
(* let boite2 = Boite (False, (Obj.magic (Pair (O, O)))) *)
 
479
 
 
480
Definition test_boite (B:Boite) :=
 
481
  match B return nat with
 
482
  | boite true n => n
 
483
  | boite false n => fst n + snd n
 
484
  end.
 
485
Extraction test_boite. 
 
486
(* 
 
487
let test_boite = function
 
488
  | Boite (b0, n) ->
 
489
      (match b0 with
 
490
         | True -> Obj.magic n
 
491
         | False -> plus (fst (Obj.magic n)) (snd (Obj.magic n)))
 
492
*)
 
493
 
 
494
(* singleton inductive with magic needed *)
 
495
 
 
496
Inductive Box : Type :=
 
497
    box : forall A:Set, A -> Box. 
 
498
Extraction Box.
 
499
(* type box = __ *)
 
500
 
 
501
Definition box1 := box nat 0. 
 
502
Extraction box1. (* let box1 = Obj.magic O *)
 
503
 
 
504
(* applied constant, magic needed *)
 
505
 
 
506
Definition idzarb (b:bool) (x:if b then nat else bool) := x.
 
507
Definition zarb := idzarb true 0.
 
508
Extraction NoInline idzarb. 
 
509
Extraction zarb. 
 
510
(* let zarb = Obj.magic idzarb True (Obj.magic O) *)
 
511
 
 
512
(** function of variable arity. *)
 
513
(** Fun n = nat -> nat -> ... -> nat *)  
 
514
 
 
515
Fixpoint Fun (n:nat) : Set :=
 
516
  match n with
 
517
  | O => nat
 
518
  | S n => nat -> Fun n
 
519
  end.
 
520
 
 
521
Fixpoint Const (k n:nat) {struct n} : Fun n :=
 
522
  match n as x return Fun x with
 
523
  | O => k
 
524
  | S n => fun p:nat => Const k n
 
525
  end.
 
526
 
 
527
Fixpoint proj (k n:nat) {struct n} : Fun n :=
 
528
  match n as x return Fun x with
 
529
  | O => 0 (* ou assert false ....*)
 
530
  | S n =>
 
531
      match k with
 
532
      | O => fun x => Const x n
 
533
      | S k => fun x => proj k n
 
534
      end
 
535
  end. 
 
536
 
 
537
Definition test_proj := proj 2 4 0 1 2 3.
 
538
 
 
539
Eval compute in test_proj. 
 
540
 
 
541
Recursive Extraction test_proj. 
 
542
 
 
543
 
 
544
 
 
545
(*** TO SUM UP: ***) 
 
546
 
 
547
(* Was previously producing a "test_extraction.ml" *)
 
548
Recursive Extraction 
 
549
  idnat id id' test2 test3 test4 test5 test6 test7 d d2
 
550
  d3 d4 d5 d6 test8 id id' test9 test10 test11 test12
 
551
  test13 test19 test20 nat sumbool_rect c Finite tree
 
552
  tree_size test14 test15 eta_c test16 test17 test18 bidon
 
553
  tb fbidon fbidon2 fbidon2 test_0 test_1 eq eq_rect tp1
 
554
  tp1bis Truc oups test24 loop horibilis PropSet natbool
 
555
  zerotrue zeroTrue zeroprop test21 test22 test23 f f_prop
 
556
  f_arity f_normal Boite boite1 boite2 test_boite Box box1
 
557
  zarb test_proj.
 
558
 
 
559
Extraction Language Haskell.
 
560
(* Was previously producing a "Test_extraction.hs" *)
 
561
Recursive Extraction
 
562
 idnat id id' test2 test3 test4 test5 test6 test7 d d2
 
563
 d3 d4 d5 d6 test8 id id' test9 test10 test11 test12
 
564
 test13 test19 test20 nat sumbool_rect c Finite tree
 
565
 tree_size test14 test15 eta_c test16 test17 test18 bidon
 
566
 tb fbidon fbidon2 fbidon2 test_0 test_1 eq eq_rect tp1
 
567
 tp1bis Truc oups test24 loop horibilis PropSet natbool
 
568
 zerotrue zeroTrue zeroprop test21 test22 test23 f f_prop
 
569
 f_arity f_normal Boite boite1 boite2 test_boite Box box1
 
570
 zarb test_proj.
 
571
 
 
572
Extraction Language Scheme.
 
573
(* Was previously producing a "test_extraction.scm" *)
 
574
Recursive Extraction
 
575
 idnat id id' test2 test3 test4 test5 test6 test7 d d2
 
576
 d3 d4 d5 d6 test8 id id' test9 test10 test11 test12
 
577
 test13 test19 test20 nat sumbool_rect c Finite tree
 
578
 tree_size test14 test15 eta_c test16 test17 test18 bidon
 
579
 tb fbidon fbidon2 fbidon2 test_0 test_1 eq eq_rect tp1
 
580
 tp1bis Truc oups test24 loop horibilis PropSet natbool
 
581
 zerotrue zeroTrue zeroprop test21 test22 test23 f f_prop
 
582
 f_arity f_normal Boite boite1 boite2 test_boite Box box1
 
583
 zarb test_proj.
 
584
                     
 
585
 
 
586
(*** Finally, a test more focused on everyday's life situations ***)
 
587
 
 
588
Require Import ZArith.
 
589
 
 
590
Recursive Extraction two_or_two_plus_one Zdiv_eucl_exist.