Debian Codegraph @

test-suite/success/coercions.v

raw
1 Module InitialTest.
2
3 (* Interaction between coercions and casts *)
4 (* Example provided by Eduardo Gimenez *)
5
6 Parameter Z S : Set.
7
8 Parameter f : S -> Z.
9 Coercion f : S >-> Z.
10
11 Parameter g : Z -> Z.
12
13 Check (fun s => g (s:S)).
14
15
16 (* Check uniform inheritance condition *)
17
18 Parameter h : nat -> nat -> Prop.
19 Parameter i : forall n m : nat, h n m -> nat.
20 Coercion i : h >-> nat.
21
22 (* Check coercion to funclass when the source occurs in the target *)
23
24 Parameter C : nat -> nat -> nat.
25 Coercion C : nat >-> Funclass.
26
27 (* Remark: in the following example, it cannot be decided whether C is
28 from nat to Funclass or from A to nat. An explicit Coercion command is
29 expected
30
31 Parameter A : nat -> Prop.
32 Parameter C:> forall n:nat, A n -> nat.
33 *)
34
35 (* Check coercion between products based on eta-expansion *)
36 (* (there was a de Bruijn bug until rev 9254) *)
37
38 Section P.
39
40 Variable E : Set.
41 Variables C D : E -> Prop.
42 Variable G :> forall x, C x -> D x.
43
44 Check fun (H : forall y:E, y = y -> C y) => (H : forall y:E, y = y -> D y).
45
46 End P.
47
48 (* Check that class arguments are computed the same when looking for a
49 coercion and when applying it (class_args_of) (failed until rev 9255) *)
50
51 Section Q.
52
53 Variable bool : Set.
54 Variables C D : bool -> Prop.
55 Variable G :> forall x, C x -> D x.
56 Variable f : nat -> bool.
57
58 Definition For_all (P : nat -> Prop) := forall x, P x.
59
60 Check fun (H : For_all (fun x => C (f x))) => H : forall x, D (f x).
61 Check fun (H : For_all (fun x => C (f x))) x => H x : D (f x).
62 Check fun (H : For_all (fun x => C (f x))) => H : For_all (fun x => D (f x)).
63
64 End Q.
65
66 (* Combining class lookup and path lookup so that if a lookup fails, another
67 descent in the class can be found (see wish #1934) *)
68
69 Record Setoid : Type :=
70 { car :> Type }.
71
72 Record Morphism (X Y:Setoid) : Type :=
73 {evalMorphism :> X -> Y}.
74
75 Definition extSetoid (X Y:Setoid) : Setoid.
76 constructor.
77 exact (Morphism X Y).
78 Defined.
79
80 Definition ClaimA := forall (X Y:Setoid) (f: extSetoid X Y) x, f x= f x.
81
82 Coercion irrelevant := (fun _ => I) : True -> car (Build_Setoid True).
83
84 Definition ClaimB := forall (X Y:Setoid) (f: extSetoid X Y) (x:X), f x= f x.
85
86 (* Check that coercions are made visible only when modules are imported *)
87
88 Module A.
89 Module B. Coercion b2n (b:bool) := if b then 0 else 1. End B.
90 Fail Check S true.
91 End A.
92 Import A.
93 Fail Check S true.
94
95 (* Tests after the inheritance condition constraint is relaxed *)
96
97 Inductive list (A : Type) : Type :=
98 nil : list A | cons : A -> list A -> list A.
99 Inductive vect (A : Type) : nat -> Type :=
100 vnil : vect A 0 | vcons : forall n, A -> vect A n -> vect A (1+n).
101 Fixpoint size A (l : list A) : nat := match l with nil _ => 0 | cons _ _ tl => 1+size _ tl end.
102
103 Section test_non_unif_but_complete.
104 Fixpoint l2v A (l : list A) : vect A (size A l) :=
105 match l as l return vect A (size A l) with
106 | nil _ => vnil A
107 | cons _ x xs => vcons A (size A xs) x (l2v A xs)
108 end.
109
110 Local Coercion l2v : list >-> vect.
111 Check (fun l : list nat => (l : vect _ _)).
112
113 End test_non_unif_but_complete.
114
115 Section what_we_could_do.
116 Variables T1 T2 : Type.
117 Variable c12 : T1 -> T2.
118
119 Class coercion (A B : Type) : Type := cast : A -> B.
120 Instance atom : coercion T1 T2 := c12.
121 Instance pair A B C D (c1 : coercion A B) (c2 : coercion C D) : coercion (A * C) (B * D) :=
122 fun x => (c1 (fst x), c2 (snd x)).
123
124 Fixpoint l2v2 {A B} {c : coercion A B} (l : list A) : (vect B (size A l)) :=
125 match l as l return vect B (size A l) with
126 | nil _ => vnil B
127 | cons _ x xs => vcons _ _ (c x) (l2v2 xs) end.
128
129 Local Coercion l2v2 : list >-> vect.
130
131 Check (fun l : list (T1 * T1) => (l : vect _ _)).
132 Check (fun l : list (T1 * T1) => (l2v2 l : vect _ _)).
133 End what_we_could_do.
134
135 End InitialTest.
136
137 (** Unit test for Prop as source class *)
138
139 Module TestPropAsSourceCoercion.
140
141 Parameter heap : Prop.
142
143 Parameter heap_empty : heap.
144
145 Definition hprop := heap -> Prop.
146
147 Coercion hpure (P:Prop) : hprop := fun h => h = heap_empty /\ P.
148
149 Parameter heap_single : nat -> nat -> hprop.
150
151 Parameter hstar : hprop -> hprop -> hprop.
152
153 Notation "H1 \* H2" := (hstar H1 H2) (at level 69, left associativity).
154
155 Definition test := heap_single 4 5 \* (5 <> 4) \* heap_single 2 4 \* (True).
156
157 (* Print test. -- reveals [hpure] coercions *)
158
159 End TestPropAsSourceCoercion.
160
161
162 (** Unit test for Type as source class *)
163
164 Module TestTypeAsSourceCoercion.
165
166 Definition relation A := A -> A -> Prop.
167
168 Record setoid := { A : Type ; R : relation A }.
169
170 Definition default_setoid (T : Type) : setoid
171 := {| A := T ; R := eq |}.
172
173 Coercion default_setoid : Sortclass >-> setoid.
174
175 Definition foo := Type : setoid.
176
177 Inductive type := U | Nat.
178 Inductive term : type -> Type :=
179 | ty (_ : Type) : term U
180 | nv (_ : nat) : term Nat.
181
182 Coercion ty : Sortclass >-> term.
183
184 Definition ty1 := Type : term _.
185 Definition ty2 := Prop : term _.
186 Definition ty3 := Set : term _.
187 Definition ty4 := (Type : Type) : term _.
188
189 End TestTypeAsSourceCoercion.
190
191 Module NonUniformInheritance.
192
193 Parameters (C : nat -> bool-> Type) (D : nat -> Type).
194 Parameter c : C O true.
195 Parameter T : D O -> nat.
196
197 Section Test0.
198
199 Parameter f0 : forall (b : bool) (n : nat), C n b -> D n.
200 Local Coercion f0 : C >-> D.
201 Check T c.
202
203 End Test0.
204
205 Section Test1.
206
207 Parameter f1 : forall (n : nat), C n true -> D n.
208 Local Coercion f1 : C >-> D.
209 Check T c.
210
211 End Test1.
212
213 Section Test2.
214
215 Parameter f2 : forall (b : bool) (n : nat) (_ : unit), C n b -> D n.
216 Local Coercion f2 : C >-> D.
217 Check T c.
218
219 End Test2.
220
221 Section Test3.
222
223 Class TC := tc : unit.
224
225 Instance i : TC := tt.
226
227 Parameter f3 : forall (b : bool) (n : nat) (_ : TC), C n b -> D n.
228 Local Coercion f3 : C >-> D.
229 Check T c.
230
231 End Test3.
232
233 End NonUniformInheritance.
234
235 Module PhantType.
236
237 Variant phant (p : Type) : Prop := Phant : phant p.
238
239 Section SetType.
240
241 Variable T : Type.
242
243 Variant set_type : Type := FinSet : T -> set_type.
244 Definition set_of (_ : phant T) := set_type.
245 Identity Coercion type_of_set_of : set_of >-> set_type.
246
247 End SetType.
248
249 Definition sort (gT : Type) := set_of _ (Phant gT).
250 Identity Coercion GroupSet_of_sort : sort >-> set_of.
251
252 Structure group_type (gT : Type) : Type := Group {
253 gval : sort gT;
254 }.
255 Coercion gval : group_type >-> sort.
256
257 Section GroupProp.
258
259 Variable G : group_type unit.
260
261 Check G : @set_type unit.
262
263 Lemma group1 : let y := G : @set_type unit in True.
264 Abort.
265
266 End GroupProp.
267
268 End PhantType.