Debian Codegraph @

test-suite/success/polymorphism.v

raw
1 Unset Strict Universe Declaration.
2
3 Module ParseSet.
4
5 Check eq_refl : Type@{0} = Set.
6
7 Check Set : Type@{1}.
8 Fail Check Type@{1} : Type@{2}. (* it parses but +2 is currently not allowed *)
9
10 Polymorphic Definition id A (a:A) := a.
11
12 Check eq_refl : id@{0} = id@{Set}.
13
14 (* currently does not parse *)
15 (* Check id@{1}. *)
16
17 Sort s.
18
19 Check Type@{s; 0} : Type@{1}.
20
21 Polymorphic Definition apply A B (f:A -> B) x := f x.
22 Check eq_refl : apply@{0 0} = apply@{Set Set}.
23 End ParseSet.
24
25 Module withoutpoly.
26
27 Inductive empty :=.
28
29 Inductive emptyt : Type :=.
30 Inductive singleton : Type :=
31 single.
32 Inductive singletoninfo : Type :=
33 singleinfo : unit -> singletoninfo.
34 Inductive singletonset : Set :=
35 singleset.
36
37 Inductive singletonnoninfo : Type :=
38 singlenoninfo : empty -> singletonnoninfo.
39
40 Inductive singletoninfononinfo : Prop :=
41 singleinfononinfo : unit -> singletoninfononinfo.
42
43 Inductive bool : Type :=
44 | true | false.
45
46 Inductive smashedbool : Prop :=
47 | trueP | falseP.
48 End withoutpoly.
49
50 Set Universe Polymorphism.
51
52 Inductive empty :=.
53 Inductive emptyt : Type :=.
54 Inductive singleton : Type :=
55 single.
56 Inductive singletoninfo : Type :=
57 singleinfo : unit -> singletoninfo.
58 Inductive singletonset : Set :=
59 singleset.
60
61 Inductive singletonnoninfo : Type :=
62 singlenoninfo : empty -> singletonnoninfo.
63
64 Inductive singletoninfononinfo : Prop :=
65 singleinfononinfo : unit -> singletoninfononinfo.
66
67 Inductive bool : Type :=
68 | true | false.
69
70 Inductive smashedbool : Prop :=
71 | trueP | falseP.
72
73 Section foo.
74 Let T := Type.
75 Inductive polybool : T :=
76 | trueT | falseT.
77 End foo.
78
79 Inductive list (A: Type) : Type :=
80 | nil : list A
81 | cons : A -> list A -> list A.
82
83 Module ftypSetSet.
84 Inductive ftyp : Type :=
85 | Funit : ftyp
86 | Ffun : list ftyp -> ftyp
87 | Fref : area -> ftyp
88 with area : Type :=
89 | Stored : ftyp -> area
90 .
91 End ftypSetSet.
92
93
94 Module ftypSetProp.
95 Inductive ftyp : Type :=
96 | Funit : ftyp
97 | Ffun : list ftyp -> ftyp
98 | Fref : area -> ftyp
99 with area : Type :=
100 | Stored : (* ftyp -> *)area
101 .
102 End ftypSetProp.
103
104 Module ftypSetSetForced.
105 Inductive ftyp : Type :=
106 | Funit : ftyp
107 | Ffun : list ftyp -> ftyp
108 | Fref : area -> ftyp
109 with area : Set (* Type *) :=
110 | Stored : (* ftyp -> *)area
111 .
112 End ftypSetSetForced.
113
114 Unset Universe Polymorphism.
115
116 Set Printing Universes.
117 Module Easy.
118
119 Polymorphic Inductive prod (A : Type) (B : Type) : Type :=
120 pair : A -> B -> prod A B.
121
122 Check prod nat nat.
123 Print Universes.
124
125
126 Polymorphic Inductive sum (A B:Type) : Type :=
127 | inl : A -> sum A B
128 | inr : B -> sum A B.
129 Print sum.
130 Check (sum nat nat).
131
132 End Easy.
133
134 Section Hierarchy.
135
136 Definition Type3 := Type.
137 Definition Type2 := Type : Type3.
138 Definition Type1 := Type : Type2.
139
140 Definition id1 := ((forall A : Type1, A) : Type2).
141 Definition id2 := ((forall A : Type2, A) : Type3).
142 Definition id1' := ((forall A : Type1, A) : Type3).
143 Fail Definition id1impred := ((forall A : Type1, A) : Type1).
144
145 End Hierarchy.
146
147 Record hypo : Type := mkhypo {
148 hypo_type : Type;
149 hypo_proof : hypo_type
150 }.
151
152 Definition typehypo (A : Type) : hypo := {| hypo_proof := A |}.
153
154 Polymorphic Record dyn : Type :=
155 mkdyn {
156 dyn_type : Type;
157 dyn_proof : dyn_type
158 }.
159
160 Definition monotypedyn (A : Type) : dyn := {| dyn_proof := A |}.
161 Polymorphic Definition typedyn (A : Type) : dyn := {| dyn_proof := A |}.
162
163 Definition atypedyn : dyn := typedyn Type.
164
165 Definition projdyn := dyn_type atypedyn.
166
167 Definition nested := {| dyn_type := dyn; dyn_proof := atypedyn |}.
168
169 Definition nested2 := {| dyn_type := dyn; dyn_proof := nested |}.
170
171 Definition projnested2 := dyn_type nested2.
172
173 Polymorphic Definition nest (d : dyn) := {| dyn_proof := d |}.
174
175 Polymorphic Definition twoprojs (d : dyn) := dyn_proof d = dyn_proof d.
176
177 Module binders.
178
179 Definition mynat@{|} := nat.
180
181 Definition foo@{i j | i < j, i < j} (A : Type@{i}) : Type@{j}.
182 exact A.
183 Defined.
184
185 Polymorphic Lemma hidden_strict_type : Type.
186 Proof.
187 exact Type.
188 Qed.
189 Check hidden_strict_type@{_}.
190 Fail Check hidden_strict_type@{Set}.
191
192 Fail Definition morec@{i j|} (A : Type@{i}) : Type@{j} := A.
193
194 (* By default constraints are extensible *)
195 Polymorphic Definition morec@{i j} (A : Type@{i}) : Type@{j} := A.
196 Check morec@{_ _}.
197
198 (* Handled in proofs as well *)
199 Lemma bar@{i j | } : Type@{i}.
200 exact Type@{j}.
201 Fail Defined.
202 Abort.
203
204 Fail Lemma bar@{u v | } : let x := (fun x => x) : Type@{u} -> Type@{v} in nat.
205 Fail Fixpoint fbar@{u v | } (n:nat) : let x := (fun x => x) : Type@{u} in nat.
206
207 Lemma bar@{i j| i < j} : Type@{j}.
208 Proof.
209 exact Type@{i}.
210 Qed.
211
212 Lemma barext@{i j|+} : Type@{j}.
213 Proof.
214 exact Type@{i}.
215 Qed.
216
217 Monomorphic Universe M.
218 Fail Definition with_mono@{u|} : Type@{M} := Type@{u}.
219 Definition with_mono@{u|u < M} : Type@{M} := Type@{u}.
220
221 End binders.
222
223 #[universes(polymorphic)]
224 Section cats.
225 Local Set Universe Polymorphism.
226 Definition fibration (A : Type) := A -> Type.
227 Definition Hom (A : Type) := A -> A -> Type.
228
229 Record sigma (A : Type) (P : fibration A) :=
230 { proj1 : A; proj2 : P proj1} .
231
232 Class Identity {A} (M : Hom A) :=
233 identity : forall x, M x x.
234
235 Class Inverse {A} (M : Hom A) :=
236 inverse : forall x y:A, M x y -> M y x.
237
238 Class Composition {A} (M : Hom A) :=
239 composition : forall {x y z:A}, M x y -> M y z -> M x z.
240
241 Notation "g ° f" := (composition f g) (at level 50).
242
243 Class Equivalence T (Eq : Hom T):=
244 {
245 Equivalence_Identity :: Identity Eq ;
246 Equivalence_Inverse :: Inverse Eq ;
247 Equivalence_Composition :: Composition Eq
248 }.
249
250 Class EquivalenceType (T : Type) : Type :=
251 {
252 m2: Hom T;
253 equiv_struct :: Equivalence T m2 }.
254
255 Polymorphic Record cat (T : Type) :=
256 { cat_hom : Hom T;
257 cat_equiv : forall x y, EquivalenceType (cat_hom x y) }.
258
259 Definition catType := sigma Type cat.
260
261 Notation "[ T ]" := (proj1 T).
262
263 From Corelib.Program Require Import Basics Tactics Wf.
264
265 Program Definition small_cat : cat Empty_set :=
266 {| cat_hom x y := unit |}.
267 Next Obligation.
268 refine ({|m2:=fun x y => True|}).
269 constructor; red; intros; trivial.
270 Defined.
271
272 Record iso (T U : Set) :=
273 { f : T -> U;
274 g : U -> T }.
275
276 Program Definition Set_cat : cat Set :=
277 {| cat_hom := iso |}.
278 Next Obligation.
279 refine ({|m2:=fun x y => True|}).
280 constructor; red; intros; trivial.
281 Defined.
282
283 Record isoT (T U : Type) :=
284 { isoT_f : T -> U;
285 isoT_g : U -> T }.
286
287 Program Definition Type_cat : cat Type :=
288 {| cat_hom := isoT |}.
289 Next Obligation.
290 refine ({|m2:=fun x y => True|}).
291 constructor; red; intros; trivial.
292 Defined.
293
294 Polymorphic Record cat1 (T : Type) :=
295 { cat1_car : Type;
296 cat1_hom : Hom cat1_car;
297 cat1_hom_cat : forall x y, cat (cat1_hom x y) }.
298 End cats.
299
300 Polymorphic Definition id {A : Type} (a : A) : A := a.
301
302 Definition typeid := (@id Type).
303
304
305 Fail Check (Prop : Set).
306 Fail Check (Set : Set).
307 Check (Set : Type).
308 Check (Prop : Type).
309 Definition setType := ltac:(let t := type of Set in exact t).
310
311 Definition foo (A : Prop) := A.
312
313 Fail Check foo Set.
314 Check fun A => foo A.
315 Fail Check fun A : Type => foo A.
316 Check fun A : Prop => foo A.
317 Fail Definition bar := fun A : Set => foo A.
318
319 Fail Check (let A := Type in foo (id A)).
320
321 Definition fooS (A : Set) := A.
322
323 Check (let A := nat in fooS (id A)).
324 Fail Check (let A := Set in fooS (id A)).
325 Fail Check (let A := Prop in fooS (id A)).
326
327 (* Some tests of sort-polymorphisme *)
328 #[universes(polymorphic)]
329 Section S.
330 Polymorphic Variable A:Type.
331 (*
332 Definition f (B:Type) := (A * B)%type.
333 *)
334 Polymorphic Inductive I (B:Type) : Type := prod : A->B->I B.
335
336 Check I nat.
337
338 End S.
339 (*
340 Check f nat nat : Set.
341 *)
342 Definition foo' := I nat nat.
343 Print Universes. Print foo. Set Printing Universes. Print foo.
344
345 (* Polymorphic axioms: *)
346 Polymorphic Axiom funext : forall (A B : Type) (f g : A -> B),
347 (forall x, f x = g x) -> f = g.
348
349 (* Check @funext. *)
350 (* Check funext. *)
351
352 Polymorphic Definition fun_ext (A B : Type) :=
353 forall (f g : A -> B),
354 (forall x, f x = g x) -> f = g.
355
356 Polymorphic Class Funext A B := extensional : fun_ext A B.
357
358 Section foo2.
359 Context `{forall A B, Funext A B}.
360 Print Universes.
361 End foo2.
362
363 Module eta.
364 Set Universe Polymorphism.
365
366 Set Printing Universes.
367
368 Axiom admit : forall A, A.
369 Record R := {O : Type}.
370
371 Definition RL (x : R@{i}) : ltac:(let u := constr:(Type@{i}:Type@{j}) in exact (R@{j}) ) := {|O := @O x|}.
372 Definition RLRL : forall x : R, RL x = RL (RL x) := fun x => eq_refl.
373 Definition RLRL' : forall x : R, RL x = RL (RL x).
374 intros. apply eq_refl.
375 Qed.
376
377 End eta.
378
379 Module Hurkens'.
380 Require Import TestSuite.hurkens.
381
382 Polymorphic Record box (X : Type) (T := Type) : Type := wrap { unwrap : T }.
383
384 Definition unwrap' := fun (X : Type) (b : box X) => let (unw) := b in unw.
385
386 Fail Definition bad : False := TypeNeqSmallType.paradox (unwrap' Type (wrap _
387 Type)) eq_refl.
388
389 End Hurkens'.
390
391 Module Anonymous.
392 Set Universe Polymorphism.
393
394 Definition defaultid := (fun x => x) : Type -> Type.
395 Definition collapseid := defaultid@{_ _}.
396 Check collapseid@{_}.
397
398 Definition anonid := (fun x => x) : Type -> Type@{_}.
399 Check anonid@{_}.
400
401 Definition defaultalg := (fun x : Type => x) (Type : Type).
402 Definition usedefaultalg := defaultalg@{_ _ _}.
403 Check usedefaultalg@{_ _}.
404
405 Definition anonalg := (fun x : Type@{_} => x) (Type : Type).
406 Check anonalg@{_ _}.
407
408 Definition unrelated@{i j} := nat.
409 Definition useunrelated := unrelated@{_ _}.
410 Check useunrelated@{_ _}.
411
412 Definition inthemiddle@{i j k} :=
413 let _ := defaultid@{i j} in
414 anonalg@{k j}.
415 (* i <= j < k *)
416 Definition collapsethemiddle := inthemiddle@{i _ j}.
417 Check collapsethemiddle@{_ _}.
418
419 End Anonymous.
420
421 Module Restrict.
422 (* Universes which don't appear in the term should be pruned, unless they have names *)
423 Set Universe Polymorphism.
424
425 Ltac exact0 := let x := constr:(Type) in exact 0.
426 Definition dummy_pruned@{} : nat := ltac:(exact0).
427
428 Definition named_not_pruned@{u} : nat := 0.
429 Check named_not_pruned@{_}.
430
431 Definition named_not_pruned_nonstrict : nat := ltac:(let x := constr:(Type@{u}) in exact 0).
432 Check named_not_pruned_nonstrict@{_}.
433
434 Lemma lemma_restrict_poly@{} : nat.
435 Proof. exact0. Defined.
436
437 Unset Universe Polymorphism.
438 Lemma lemma_restrict_mono_qed@{} : nat.
439 Proof. exact0. Qed.
440
441 Lemma lemma_restrict_abstract@{} : nat.
442 Proof. abstract exact0. Qed.
443
444 End Restrict.
445
446 Module F.
447 #[warning="context-outside-section"] Context {A B : Type}.
448 Definition foo : Type := B.
449 End F.
450
451 Set Universe Polymorphism.
452
453 Cumulative Record box (X : Type) (T := Type) : Type := wrap { unwrap : T }.
454
455 Section test_letin_subtyping.
456 Universe i j k i' j' k'.
457 Constraint j < j'.
458
459 Context (W : Type) (X : box@{i j k} W).
460 Definition Y := X : box@{i' j' k'} W.
461
462 Universe i1 j1 k1 i2 j2 k2.
463 Constraint i1 < i2.
464 Constraint k2 < k1.
465 Context (V : Type).
466
467 Definition Z : box@{i1 j1 k1} V := {| unwrap := V |}.
468 Definition Z' : box@{i2 j2 k2} V := {| unwrap := V |}.
469 Lemma ZZ' : @eq (box@{i2 j2 k2} V) Z Z'.
470 Proof.
471 Set Printing All. Set Printing Universes.
472 cbv.
473 reflexivity.
474 Qed.
475
476 End test_letin_subtyping.
477
478 Module ObligationRegression.
479 (** Test for a regression encountered when fixing obligations for
480 stronger restriction of universe context. *)
481 Require Import CMorphisms.
482 Check trans_co_eq_inv_arrow_morphism@{_ _ _ _ _ _ _}.
483 End ObligationRegression.
484
485 Axiom poly@{i} : forall(A : Type@{i}) (a : A), unit.
486
487 Definition nonpoly := @poly True Logic.I.
488 Definition check := nonpoly@{}.
489
490 Module ProgramFixpoint.
491
492 Local Set Universe Polymorphism.
493 Program Fixpoint f@{u} (A:Type@{u}) (n:nat) : Type@{u} :=
494 match n with 0 => A | S n => f (A->A) n end.
495 Check f@{Set}. (* Check that it depends on only one universe *)
496
497 End ProgramFixpoint.
498
499 Module EarlyPolyUniverseDeclarationCheck.
500
501 Local Set Universe Polymorphism.
502
503 Fail Definition f@{u} n : match n return Type@{v} with 0 => Type@{u} | _ => Type@{u} end.
504
505 Definition f@{u v} n : match n return Type@{v} with 0 => Type@{u} | _ => Type@{u} end.
506 exact (match n with 0 => nat | _ => nat end).
507 Defined.
508
509 Program Fixpoint f'@{u} (A:Type@{u}) (n:nat) : Type@{u} :=
510 match n with 0 => _ | S n => f' (A->A) n end.
511 Next Obligation. exact nat. Defined.
512
513 Fail Program Fixpoint f''@{u} (A:Type@{u}) (n:nat) {measure n} : Type@{u} :=
514 match n with 0 => _ | S n => f'' (Type->A) n end.
515
516 Local Set Universe Polymorphism.
517 Program Fixpoint f''@{u} (A:Type@{u}) (n:nat) {measure n} : Type@{u} :=
518 match n with 0 => _ | S n => f'' (A->A) n end.
519 Next Obligation. Show. exact nat. Defined.
520 Next Obligation. Show. Admitted.
521
522 End EarlyPolyUniverseDeclarationCheck.
523
524 Module EarlyMonoUniverseDeclarationCheck.
525
526 Local Unset Universe Polymorphism.
527
528 Fail Definition f@{u} n : match n return Type@{v} with 0 => Type@{u} | _ => Type@{u} end.
529
530 Definition f@{u v} n : match n return Type@{v} with 0 => Type@{u} | _ => Type@{u} end.
531 exact (match n with 0 => nat | _ => nat end).
532 Defined.
533
534 Fail Program Fixpoint f'@{u} (A:Type@{u}) (n:nat) : Type@{u} := (* By convention, we require extensibility for Program *)
535 match n with 0 => _ | S n => f' (A->A) n end.
536
537 Program Fixpoint f'@{u +} (A:Type@{u}) (n:nat) : Type@{u} :=
538 match n with 0 => _ | S n => f' (A->A) n end.
539 Next Obligation. exact nat. Defined.
540
541 Fail Program Fixpoint f''@{u} (A:Type@{u}) (n:nat) {measure n} : Type@{u} :=
542 match n with 0 => _ | S n => f'' (Type->A) n end.
543
544 Fail Program Fixpoint f''@{u} (A:Type@{u}) (n:nat) {measure n} : Type@{u} := (* By convention, we require extensibility for Program *)
545 match n with 0 => _ | S n => f'' (A->A) n end.
546
547 Program Fixpoint f''@{u +} (A:Type@{u}) (n:nat) {measure n} : Type@{u} :=
548 match n with 0 => _ | S n => f'' (A->A) n end.
549 Next Obligation. Show. exact nat. Defined.
550 Next Obligation. Show. Admitted.
551
552 End EarlyMonoUniverseDeclarationCheck.