Debian Codegraph @

test-suite/output/Cases.v

raw
1 (* Cases with let-in in constructors types *)
2
3 Unset Printing Allow Match Default Clause.
4
5 Inductive t : Set :=
6 k : let x := t in x -> x.
7
8 Print t_rect.
9
10 Record TT : Type := CTT { f1 := 0 : nat; f2: nat; f3 : f1=f1 }.
11
12 Eval cbv in fun d:TT => match d return 0 = 0 with CTT a _ b => b end.
13 Eval lazy in fun d:TT => match d return 0 = 0 with CTT a _ b => b end.
14
15 (* Do not contract nested patterns with dependent return type *)
16 (* see bug #1699 *)
17
18 Require Import TestSuite.arith.
19
20 Definition proj (x y:nat) (P:nat -> Type) (def:P x) (prf:P y) : P y :=
21 match eq_nat_dec x y return P y with
22 | left eqprf =>
23 match eqprf in (_ = z) return (P z) with
24 | refl_equal => def
25 end
26 | _ => prf
27 end.
28
29 Print proj.
30
31 (* Use notations even below aliases *)
32
33 Require Import TestSuite.list.
34
35 Fixpoint foo (A:Type) (l:list A) : option A :=
36 match l with
37 | nil => None
38 | cons x0 nil => Some x0
39 | cons x0 (cons x1 xs as l0) => foo A l0
40 end.
41
42 Print foo.
43
44 (* Accept and use notation with binded parameters *)
45
46 #[universes(template)]
47 Inductive I (A: Type) : Type := C : A -> I A.
48 Notation "x <: T" := (C T x) (at level 38).
49
50 Definition uncast A (x : I A) :=
51 match x with
52 | x <: _ => x
53 end.
54
55 Print uncast.
56
57 (* Do not duplicate the matched term *)
58
59 Axiom A : nat -> bool.
60
61 Definition foo' :=
62 match A 0 with
63 | true => true
64 | x => x
65 end.
66
67 Print foo'.
68
69 (* Was bug #3293 (eta-expansion at "match" printing time was failing because
70 of let-in's interpreted as being part of the expansion) *)
71
72 Axiom b : bool.
73 Axiom P : bool -> Prop.
74 Inductive B : Prop := AC : P b -> B.
75 Definition f : B -> True.
76
77 Proof.
78 intros [x].
79 destruct b as [|] ; exact Logic.I.
80 Defined.
81
82 Print f.
83
84 (* Was enhancement request #5142 (error message reported on the most
85 general return clause heuristic) *)
86
87 Inductive gadt : Type -> Type :=
88 | gadtNat : nat -> gadt nat
89 | gadtTy : forall T, T -> gadt T.
90
91 Fail Definition gadt_id T (x: gadt T) : gadt T :=
92 match x with
93 | gadtNat n => gadtNat n
94 end.
95
96 (* A variant of #5142 (see Satrajit Roy's example on coq-club (Oct 17, 2016)) *)
97
98 Inductive type:Set:=Nat.
99 Inductive tbinop:type->type->type->Set:= TPlus : tbinop Nat Nat Nat.
100 Inductive texp:type->Set:=
101 |TNConst:nat->texp Nat
102 |TBinop:forall t1 t2 t, tbinop t1 t2 t->texp t1->texp t2->texp t.
103 Definition typeDenote(t:type):Set:= match t with Nat => nat end.
104
105 (* We expect a failure on TBinop *)
106 Fail Fixpoint texpDenote t (e:texp t):typeDenote t:=
107 match e with
108 | TNConst n => n
109 | TBinop t1 t2 _ b e1 e2 => O
110 end.
111
112 (* Test notations with local definitions in constructors *)
113
114 Inductive J := D : forall n m, let p := n+m in nat -> J.
115 Notation "{{ n , m , q }}" := (D n m q).
116
117 Check fun x : J => let '{{n, m, _}} := x in n + m.
118 Check fun x : J => let '{{n, m, p}} := x in n + m + p.
119
120 (* Cannot use the notation because of the dependency in p *)
121
122 Check fun x => let '(D n m p q) := x in n+m+p+q.
123
124 (* This used to succeed, being interpreted as "let '{{n, m, p}} := ..." *)
125
126 Fail Check fun x : J => let '{{n, m, _}} p := x in n + m + p.
127
128 (* Test use of idents bound to ltac names in a "match" *)
129
130 Lemma lem1 : forall k, k=k :>nat * nat.
131 let x := fresh "aa" in
132 let y := fresh "bb" in
133 let z := fresh "cc" in
134 let k := fresh "dd" in
135 refine (fun k : nat * nat => match k as x return x = x with (y,z) => eq_refl end).
136 Qed.
137 Print lem1.
138
139 Lemma lem2 : forall k, k=k :> bool.
140 let x := fresh "aa" in
141 let y := fresh "bb" in
142 let z := fresh "cc" in
143 let k := fresh "dd" in
144 refine (fun k => if k as x return x = x then eq_refl else eq_refl).
145 Qed.
146 Print lem2.
147
148 Lemma lem3 : forall k, k=k :>nat * nat.
149 let x := fresh "aa" in
150 let y := fresh "bb" in
151 let z := fresh "cc" in
152 let k := fresh "dd" in
153 refine (fun k : nat * nat => let (y,z) as x return x = x := k in eq_refl).
154 Qed.
155 Print lem3.
156
157 Lemma lem4 x : x+0=0.
158 match goal with |- ?y = _ => pose (match y with 0 => 0 | S n => 0 end) end.
159 match goal with |- ?y = _ => pose (match y as y with 0 => 0 | S n => 0 end) end.
160 match goal with |- ?y = _ => pose (match y as y return y=y with 0 => eq_refl | S n => eq_refl end) end.
161 match goal with |- ?y = _ => pose (match y return y=y with 0 => eq_refl | S n => eq_refl end) end.
162 match goal with |- ?y + _ = _ => pose (match y with 0 => 0 | S n => 0 end) end.
163 match goal with |- ?y + _ = _ => pose (match y as y with 0 => 0 | S n => 0 end) end.
164 match goal with |- ?y + _ = _ => pose (match y as y return y=y with 0 => eq_refl | S n => eq_refl end) end.
165 match goal with |- ?y + _ = _ => pose (match y return y=y with 0 => eq_refl | S n => eq_refl end) end.
166 Show.
167 Abort.
168
169 Lemma lem5 (p:nat) : eq_refl p = eq_refl p.
170 let y := fresh "n" in (* Checking that y is hidden *)
171 let z := fresh "e" in (* Checking that z is hidden *)
172 match goal with
173 |- ?y = _ => pose (match y as y in _ = z return y=y /\ z=z with eq_refl => conj eq_refl eq_refl end)
174 end.
175 let y := fresh "n" in
176 let z := fresh "e" in
177 match goal with
178 |- ?y = _ => pose (match y in _ = z return y=y /\ z=z with eq_refl => conj eq_refl eq_refl end)
179 end.
180 let y := fresh "n" in
181 let z := fresh "e" in
182 match goal with
183 |- eq_refl ?y = _ => pose (match eq_refl y in _ = z return y=y /\ z=z with eq_refl => conj eq_refl eq_refl end)
184 end.
185 let p := fresh "p" in
186 let z := fresh "e" in
187 match goal with
188 |- eq_refl ?p = _ => pose (match eq_refl p in _ = z return p=p /\ z=z with eq_refl => conj eq_refl eq_refl end)
189 end.
190 Show.
191 Abort.
192
193 Set Printing Allow Match Default Clause.
194
195 (***************************************************)
196 (* Testing strategy for factorizing cases branches *)
197
198 (* Factorization + default clause *)
199 Check fun x => match x with Eq => 1 | _ => 0 end.
200
201 (* No factorization *)
202 Unset Printing Factorizable Match Patterns.
203 Check fun x => match x with Eq => 1 | _ => 0 end.
204 Set Printing Factorizable Match Patterns.
205
206 (* Factorization but no default clause *)
207 Unset Printing Allow Match Default Clause.
208 Check fun x => match x with Eq => 1 | _ => 0 end.
209 Set Printing Allow Match Default Clause.
210
211 (* No factorization in printing all mode *)
212 Set Printing All.
213 Check fun x => match x with Eq => 1 | _ => 0 end.
214 Unset Printing All.
215
216 (* Several clauses *)
217 Inductive K := a1|a2|a3|a4|a5|a6.
218 Check fun x => match x with a3 | a4 => 3 | _ => 2 end.
219 Check fun x => match x with a3 => 3 | a2 | a1 => 4 | _ => 2 end.
220 Check fun x => match x with a4 => 3 | a2 | a1 => 4 | _ => 2 end.
221 Check fun x => match x with a3 | a4 | a1 => 3 | _ => 2 end.
222
223 (* Test redundant clause within a disjunctive pattern *)
224 Fail Check fun n m => match n, m with 0, 0 | _, S _ | S 0, _ | S (S _ | _), _ => false end.
225
226 Module Bug11231.
227
228 (* Missing dependency in computing if a clause is a default clause *)
229
230 Inductive Tree: Set :=
231 | Node : Tree
232 | App : Tree -> Tree -> Tree
233 .
234
235 Definition stray N :=
236 match N with
237 | App (App Node (App (App Node Node) Node)) _ => Node
238 | App (App Node strayvariable) _ => strayvariable
239 | _ => Node
240 end.
241
242 Print stray.
243
244 End Bug11231.
245
246 Module Wish12762.
247
248 Inductive foo := a | b | c.
249
250 Definition bar (f : foo) :=
251 match f with
252 | a => 0
253 | B => 1
254 end.
255
256 End Wish12762.
257
258 Module ConstructorArgumentsNumber.
259
260 Arguments cons {A} _ _.
261
262 Inductive J' A {B} (C:=(A*B)%type) (c:C) := D' : forall n {m}, let p := n+m in m=m -> J' A c.
263
264 Unset Asymmetric Patterns.
265
266 Fail Check fun x => match x with (y,z) w => y+z+w end.
267 Fail Check fun x => match x with cons y z w => 0 | nil => 0 end.
268 Fail Check fun x => match x with cons y => 0 | nil => 0 end.
269
270 (* Missing a let-in to be in let-in mode *)
271 Fail Check fun x => match x with D' _ _ n p e => 0 end.
272 Check fun x : J' bool (true,true) => match x with D' _ _ n e => existT (fun x => eq x x) _ e end.
273 Check fun x : J' bool (true,true) => match x with D' _ _ _ n p e => n+p end.
274
275 Set Asymmetric Patterns.
276
277 Fail Check fun x => match x with (y,z) w => y+z+w end.
278 Fail Check fun x => match x with cons y z w => 0 | nil => 0 end.
279 Fail Check fun x => match x with cons y => 0 | nil => 0 end.
280
281 Fail Check fun x => match x with D' n _ => 0 end.
282 Fail Check fun x => match x with D' n m p e _ => 0 end.
283 Check fun x : J' bool (true,true) => match x with D' n m e => existT (fun x => eq x x) m e end.
284 Check fun x : J' bool (true,true) => match x with D' n m p e => (n,p) end.
285
286 End ConstructorArgumentsNumber.
287
288 Module Bug14207.
289
290 Inductive type {base_type : Type} := base (t : base_type) | arrow (s d : type).
291 Global Arguments type : clear implicits.
292 Fixpoint interp {base_type} (base_interp : base_type -> Type) (t : type base_type) : Type
293 := match t with
294 | base t => base_interp t
295 | arrow s d => @interp _ base_interp s -> @interp _ base_interp d
296 end.
297 Axiom admit : forall {T}, T.
298 Section with_base.
299 Context {base_type : Type}
300 {base_interp : base_type -> Type}.
301 Local Abbreviation type := (@type base_type).
302
303 Fixpoint default {t} : interp base_interp t
304 := match t with
305 | base x => admit
306 | arrow s d => fun _ => @default d
307 end.
308 End with_base.
309
310 Definition c :=
311 match 0, 0 with
312 | S (S x), y => 0
313 | x, S (S y) => 1
314 | x, y => 2
315 end.
316
317 End Bug14207.
318
319 Module Bug17071.
320
321 Notation "x :||: l" := (true x l) (at level 51).
322
323 Fail Check
324 match true with
325 | x :||: l => 0
326 end.
327
328 End Bug17071.