Debian Codegraph @

test-suite/success/Fixpoint.v

raw
1 (* Playing with (co-)fixpoints with local definitions *)
2
3 Inductive listn : nat -> Set :=
4 niln : listn 0
5 | consn : forall n:nat, nat -> listn n -> listn (S n).
6
7 Fixpoint f (n:nat) (m:=pred n) (l:listn m) (p:=S n) {struct l} : nat :=
8 match n with O => p | _ =>
9 match l with niln => p | consn q _ l => f (S q) l end
10 end.
11
12 Eval compute in (f 2 (consn 0 0 niln)).
13
14 CoInductive Stream : nat -> Set :=
15 Consn : forall n, nat -> Stream n -> Stream (S n).
16
17 CoFixpoint g (n:nat) (m:=pred n) (l:Stream m) (p:=S n) : Stream p :=
18 match n return (let m:=pred n in forall l:Stream m, let p:=S n in Stream p)
19 with
20 | O => fun l:Stream 0 => Consn O 0 l
21 | S n' =>
22 fun l:Stream n' =>
23 let l' :=
24 match l in Stream q return Stream (pred q) with Consn _ _ l => l end
25 in
26 let a := match l with Consn _ a l => a end in
27 Consn (S n') (S a) (g n' l')
28 end l.
29
30 Eval compute in (fun l => match g 2 (Consn 0 6 l) with Consn _ a _ => a end).
31
32 (* Check inference of simple types in presence of non ambiguous
33 dependencies (needs revision 10125) *)
34
35 Section folding.
36
37 Inductive vector (A:Type) : nat -> Type :=
38 | Vnil : vector A 0
39 | Vcons : forall (a:A) (n:nat), vector A n -> vector A (S n).
40
41 Variables (B C : Set) (g : B -> C -> C) (c : C).
42
43 Fixpoint foldrn n bs :=
44 match bs with
45 | Vnil _ => c
46 | Vcons _ b _ tl => g b (foldrn _ tl)
47 end.
48
49 End folding.
50
51 (* Check definition by tactics *)
52
53 Inductive even : nat -> Type :=
54 | even_O : even 0
55 | even_S : forall n, odd n -> even (S n)
56 with odd : nat -> Type :=
57 odd_S : forall n, even n -> odd (S n).
58
59 Fixpoint even_div2 n (H:even n) : nat :=
60 match H with
61 | even_O => 0
62 | even_S n H => S (odd_div2 n H)
63 end
64 with odd_div2 n H : nat.
65 destruct H.
66 apply even_div2 with n.
67 assumption.
68 Qed.
69
70 Fixpoint even_div2' n (H:even n) : nat with odd_div2' n (H:odd n) : nat.
71 destruct H.
72 exact 0.
73 apply odd_div2' with n.
74 assumption.
75 destruct H.
76 apply even_div2' with n.
77 assumption.
78 Qed.
79
80 CoInductive Stream1 (A B:Type) := Cons1 : A -> Stream2 A B -> Stream1 A B
81 with Stream2 (A B:Type) := Cons2 : B -> Stream1 A B -> Stream2 A B.
82
83 CoFixpoint ex1 (n:nat) (b:bool) : Stream1 nat bool
84 with ex2 (n:nat) (b:bool) : Stream2 nat bool.
85 apply Cons1.
86 exact n.
87 apply (ex2 n b).
88 apply Cons2.
89 exact b.
90 apply (ex1 (S n) (negb b)).
91 Defined.
92
93 Section visibility.
94
95 Let Fixpoint imm (n:nat) : True := I.
96
97 Let Fixpoint by_proof (n:nat) : True.
98 Proof. exact I. Defined.
99
100 Let Fixpoint foo (n:nat) : bool with bar (n:nat) : bool.
101 Proof.
102 - destruct n as [|n].
103 + exact true.
104 + exact (bar n).
105 - destruct n as [|n].
106 + exact false.
107 + exact (foo n).
108 Qed.
109
110 Let Fixpoint bla (n:nat) : Type with bli (n:nat) : bool.
111 Admitted.
112
113 End visibility.
114
115 Fail Check imm.
116 Fail Check by_proof.
117 Check bla. Check bli.
118
119 Module Import mod_local.
120 Fixpoint imm_importable (n:nat) : True := I.
121
122 Local Fixpoint imm_local (n:nat) : True := I.
123
124 Fixpoint by_proof_importable (n:nat) : True.
125 Proof. exact I. Defined.
126
127 Local Fixpoint by_proof_local (n:nat) : True.
128 Proof. exact I. Defined.
129 End mod_local.
130
131 Check imm_importable.
132 Fail Check imm_local.
133 Check mod_local.imm_local.
134 Check by_proof_importable.
135 Fail Check by_proof_local.
136 Check mod_local.by_proof_local.
137
138 (* Miscellaneous tests *)
139
140 Module IotaRedex.
141
142 Fixpoint minus (n m:nat) {struct n} : nat :=
143 match (n, m) with
144 | (O , _) => O
145 | (S _ , O) => n
146 | (S n', S m') => minus n' m'
147 end.
148
149 End IotaRedex.
150
151 Module ReturningInductive.
152
153 Fail Fixpoint geneq s (x: list nat) {struct s} : Prop :=
154 match x with
155 | cons a t => geneq (S a) t /\ geneq (S a) t
156 | _ => False
157 end.
158
159 End ReturningInductive.
160
161 Module NestingAndUnfolding.
162
163 Fail Fixpoint f (x:nat) := id (fix g x : nat := f x) 0.
164
165 Fixpoint f x :=
166 match x with
167 | 0 => 0
168 | S n => id (fix g x := f x) n
169 end.
170
171 End NestingAndUnfolding.
172
173 Module NestingAndConstructedUnfolding.
174
175 Definition fold_left {A B : Type} (f : A -> B -> A) :=
176 fix fold_left (l : list B) (a0 : A) {struct l} : A :=
177 match l with
178 | nil => a0
179 | cons b t => fold_left t (f a0 b)
180 end.
181
182 Record t A : Type :=
183 mk {
184 elt: A
185 }.
186
187 Arguments elt {A} t.
188
189 Inductive LForm : Type :=
190 | LIMPL : t LForm -> list (t LForm) -> LForm.
191
192 Fixpoint hcons (m : unit) (f : LForm) :=
193 match f with
194 | LIMPL f l => fold_left (fun m f => hcons m f.(elt) ) (cons f l) m
195 end.
196 End NestingAndConstructedUnfolding.
197
198 Module CofixRedex.
199
200 CoInductive Stream := {hd : nat; tl : Stream}.
201 Definition zeros := cofix zeros := {|hd := 0; tl := zeros|}.
202
203 Fixpoint f n :=
204 match n with
205 | 0 => 0
206 | S n =>
207 match zeros with
208 | {|hd:=_|} => fun f => f n
209 end f
210 end.
211
212 End CofixRedex.
213
214 Module CofixRedexPrimProj.
215
216 Set Primitive Projections.
217 CoInductive Stream A := {hd : A; tl : Stream A}.
218 Arguments hd {A} s.
219
220 Fixpoint f n :=
221 match n with
222 | 0 => 0
223 | S n => (cofix cst := {|hd := (fun f => f n); tl := cst|}).(hd) f
224 end.
225
226 End CofixRedexPrimProj.
227
228 Module ArgumentsAcrossMatch.
229
230 (* large subterm passed across match *)
231 Fail Fixpoint f n p {struct n} :=
232 match n with
233 | 0 => fun _ => 0
234 | S q => fun r => f q (f r 0)
235 end n.
236
237 (* strict subterm passed across match *)
238 Fixpoint f n p {struct n} :=
239 match n with
240 | 0 => 0
241 | S q =>
242 match q with
243 | 0 => fun _ => 0
244 | S q' => fun r => f q (f r 0)
245 end q
246 end.
247
248 End ArgumentsAcrossMatch.
249
250 Module LetToExpand.
251
252 Fixpoint h n :=
253 let f n := (fun x : h n -> True => True) (fun y : h n => I) in
254 match n with
255 | 0 => True
256 | S n => f n
257 end.
258
259 End LetToExpand.
260
261 Module RecursiveCallInsideCoFix.
262
263 CoInductive I := { field : I }.
264
265 Fail Fixpoint f (n:nat) :=
266 (cofix g n := {| field := f n |}) 0.
267
268 End RecursiveCallInsideCoFix.
269
270 Module NestedRedexes.
271
272 Fixpoint f n :=
273 match n with
274 | 0 => 0
275 | S n => id (fun x => id (fun _ => id (f x)) 0) n
276 end.
277
278 End NestedRedexes.
279
280 Module NestedRedexesWithCofix.
281
282 CoInductive I := { field : nat -> nat }.
283
284 Fail Fixpoint f n :=
285 ((cofix g h := {| field := h |}) f).(field) n.
286
287 Fixpoint f n :=
288 match n with
289 | 0 => 0
290 | S p => ((cofix g h := {| field := h |}) f).(field) p
291 end.
292
293 End NestedRedexesWithCofix.
294
295 Module NestedApplicationsWithVariables.
296
297 Section S.
298 Variable h : (nat -> nat) -> nat.
299 Fixpoint f n :=
300 match n with
301 | 0 => 0
302 | S p => (fun _ => 0) (h f)
303 end.
304 End S.
305
306 End NestedApplicationsWithVariables.
307
308 Module NestedApplicationsWithParameters.
309
310 Parameter h : (nat -> nat) -> nat.
311 Fixpoint f n :=
312 match n with
313 | 0 => 0
314 | S p => (fun _ => 0) (h f)
315 end.
316
317 End NestedApplicationsWithParameters.
318
319 Module NestedApplicationsWithLocalVariables.
320
321 Fixpoint f (h:(nat->nat)->nat) n :=
322 match n with
323 | 0 => 0
324 | S p => (fun _ => 0) (h (f h))
325 end.
326
327 End NestedApplicationsWithLocalVariables.
328
329 Module NestedApplicationsWithProjections.
330
331 Set Primitive Projections.
332 Record R := { field : (nat -> nat) -> nat }.
333
334 Fixpoint f x n :=
335 match n with
336 | 0 => 0
337 | S p => (fun _ => 0) (x.(field) (f x))
338 end.
339
340 End NestedApplicationsWithProjections.
341
342 Module NestedRedexesWithFix.
343
344 Fixpoint f n :=
345 match n with
346 | 0 => 0
347 | S p => (fun _ => 0) ((fix h k (q:nat) {struct q} := k) f)
348 end.
349
350 (* inner fix fully applied with a match subterm *)
351 Fixpoint f' n :=
352 match n with
353 | 0 => 0
354 | S p => (fun _ => 0) ((fix h k (q:nat) {struct q} := k) f' p)
355 end.
356
357 (* inner fix fully applied with an arbitrary term *)
358 Fixpoint f'' o n :=
359 match n with
360 | 0 => 0
361 | S p => (fun _ => 0) ((fix h k (q:nat) {struct q} := k o) f'' o)
362 end.
363
364 End NestedRedexesWithFix.
365
366 Module NestedRedexesWithMatch.
367
368 Fixpoint f o n :=
369 match n with
370 | 0 => 0
371 | S p => (fun _ => 0) (match o with tt => f o end)
372 end.
373
374 Fixpoint f' o n :=
375 match n with
376 | 0 => 0
377 | S p => (fun _ => 0) ((match o with tt => fun x => x o end) f')
378 end.
379
380 End NestedRedexesWithMatch.
381
382 Module ErasableInertSubterm.
383
384 Fixpoint P (n:nat) :=
385 (fun _ => True) (forall a : (forall p, P p), True).
386
387 End ErasableInertSubterm.
388
389 Module WithLetInLift.
390
391 Fixpoint f (n : nat) : nat :=
392 match n with
393 | 0 => 0
394 | S n => (let x := 0 in fun n => f n) n
395 end.
396
397 End WithLetInLift.
398
399 Module WithLateCaseReduction.
400
401 Definition B := true.
402 Fixpoint f (n : nat) :=
403 match n with
404 | 0 => 0
405 | S n => (if B as b return if b then nat -> nat else unit then fun n => f n else tt) n
406 end.
407
408 End WithLateCaseReduction.
409
410 Module NtnInteractiveFixpoint.
411
412 Reserved Notation "# n" (at level 2, right associativity).
413 Fixpoint f (n:nat) : nat where "# n" := (f n).
414 exact (match n with 0 => 0 | S n => # n end).
415 Defined.
416 Check eq_refl : # 0 = f 0.
417
418 End NtnInteractiveFixpoint.
419
420 Module NoArgumentFixpoint.
421
422 Fail Fixpoint f : nat. (* was an anomaly at some time *)
423
424 End NoArgumentFixpoint.
425
426 Module FixpointRelevance.
427
428 (* Check that the recursive reference to a fixpoint name has correct
429 relevance, in different execution paths *)
430
431 Inductive STrue : SProp := SI.
432 Inductive seq (a:STrue) : STrue -> SProp := srefl : seq a a.
433 Fixpoint g1 (n:nat) : STrue :=
434 match n with
435 | 0 => SI
436 | S n => let x := srefl (g1 n) : seq (g1 n) (g2 n) in g2 n
437 end
438 with g2 (n:nat) : STrue :=
439 match n with
440 | 0 => SI
441 | S n => let x := srefl (g1 n) : seq (g1 n) (g2 n) in g1 n
442 end.
443 Fixpoint h1 (n:nat) : STrue with h2 (n:nat) : STrue.
444 exact
445 (match n with
446 | 0 => SI
447 | S n => let x := srefl (h1 n) : seq (h1 n) (h2 n) in h2 n
448 end).
449 exact
450 (match n with
451 | 0 => SI
452 | S n => let x := srefl (h1 n) : seq (h1 n) (h2 n) in h1 n
453 end).
454 Defined.
455 Theorem k1 (n:nat) : STrue with k2 (n:nat) : STrue.
456 exact
457 (match n with
458 | 0 => SI
459 | S n => let x := srefl (k1 n) : seq (k1 n) (k2 n) in k2 n
460 end).
461 exact
462 (match n with
463 | 0 => SI
464 | S n => let x := srefl (k1 n) : seq (k1 n) (k2 n) in k1 n
465 end).
466 Defined.
467
468 End FixpointRelevance.
469
470 Module ClearFixBody.
471
472 CoInductive Stream : Set := Cons : nat -> Stream -> Stream.
473
474 Section S.
475 #[clearbody] Let CoFixpoint f : Stream := Cons 1 f.
476 #[clearbody] Let Fixpoint g n := match n with 0 => 0 | S n => g n end.
477 Goal True.
478 Fail Check eq_refl : f = cofix f := Cons 1 f.
479 Fail Check eq_refl : g = fix g n := match n with 0 => 0 | S n => g n end.
480 Abort.
481 End S.
482
483 End ClearFixBody.
484
485 Module TheoremWithUnivs.
486
487 Fail Fixpoint f@{u} (n:nat) : nat with g@{v} (n:nat) : nat.
488 Fail Theorem f@{u} (n:nat) : nat with g@{v} (n:nat) : nat.
489 Fail CoFixpoint f@{u} (n:nat) : Stream 0 with g@{v} (n:nat) : Stream 0.
490 Succeed Fixpoint f@{u} (n:nat) : nat with g@{u} (n:nat) : nat.
491 Succeed Theorem f@{u} (n:nat) : nat with g@{u} (n:nat) : nat.
492 Succeed CoFixpoint f@{u} (n:nat) : Stream 0 with g@{u} (n:nat) : Stream 0.
493 Succeed Fixpoint f@{u} (n:nat) : nat with g (n:nat) : nat. (* Accepted *)
494 Succeed Theorem f@{u} (n:nat) : nat with g (n:nat) : nat. (* Accepted *)
495 Succeed CoFixpoint f@{u} (n:nat) : Stream 0 with g (n:nat) : Stream 0. (* Accepted *)
496
497 End TheoremWithUnivs.
498
499 Module DependMutualFix.
500
501 Inductive tree (A : Type) := Node : A -> list (tree A) -> tree A.
502
503 Definition lmap' {A B} (f : A -> B) : list A -> list B :=
504 fix F l :=
505 match l with
506 | nil => nil
507 | cons x l => cons (f x) (G l)
508 end
509 with G l :=
510 match l with
511 | nil => nil
512 | cons x l => cons (f x) (F l)
513 end for F.
514
515 (* Not yet able to accept this *)
516 Fail Fixpoint map {A B} (f : A -> B) (t : tree A) {struct t} : tree B :=
517 match t with
518 | Node _ x l => Node _ (f x) (lmap' (map f) l)
519 end.
520
521 End DependMutualFix.
522
523 Module Wish16040.
524
525 Inductive tree (A : Type) := Node : A -> list (tree A) -> tree A.
526
527 Fixpoint lmap {A B} (f : A -> B) (l : list A) : list B :=
528 match l with
529 | nil => nil
530 | cons x l => cons (f x) (lmap f l)
531 end.
532
533 Fixpoint map {A B} (f : A -> B) (t : tree A) {struct t} : tree B :=
534 match t with
535 | Node _ x l => Node _ (f x) (lmap (map f) l)
536 end.
537
538 (* Check that we don't find too much uniform parameters *)
539
540 Fixpoint lmap' {A} (f g : A -> A) (l : list A) : list A :=
541 match l with
542 | nil => nil
543 | cons x l => cons (f x) (lmap' g f l)
544 end.
545
546 (* Not supposed to be detected guarded, as only A is uniform in lmap' *)
547 Fail Fixpoint map' {A} (f : A -> A) (t : tree A) {struct t} : tree A :=
548 match t with
549 | Node _ x l => Node _ (f x) (lmap' (map' f) (map' f) l)
550 end.
551
552 (* Uniform arguments after a non-uniform one *)
553
554 Fixpoint lmap'' {A} n (f : A -> A) (l : list A) : list A :=
555 match l with
556 | nil => nil
557 | cons x l => cons (f x) (lmap'' (S n) f l)
558 end.
559
560 (* The current guard supports extrusion of uniform arguments only in prefix position *)
561 Fail Fixpoint map'' {A} (f : A -> A) (t : tree A) {struct t} : tree A :=
562 match t with
563 | Node _ x l => Node _ (f x) (lmap'' 0 (map'' f) l)
564 end.
565
566 End Wish16040.
567
568 Module TheoremWith.
569
570 CoInductive Stream : Set := Cons : nat -> Stream -> Stream.
571
572 (* Support for mutually recursive theorems in non-mutual types *)
573 Theorem a : Stream with b : Stream.
574 Proof.
575 apply (Cons 0), b.
576 apply (Cons 0), a.
577 Defined.
578
579 Theorem c (n:nat) : Stream with d (n:nat) : Stream. (* corecursive *)
580 Proof.
581 apply (Cons n), (d n).
582 apply (Cons n), (c n).
583 Defined.
584
585 Theorem c' (n:nat) : Stream with d' (n:nat) : Stream. (* recursive *)
586 Proof.
587 destruct n as [|n']. apply a. apply (d' n').
588 destruct n as [|n']. apply a. apply (c' n').
589 Defined.
590
591 End TheoremWith.
592
593 Module HighlyNested.
594
595 Inductive T A := E : A * list A * list (list A) -> T A.
596 Inductive U := H : T (T U) -> U.
597
598 Definition map {A B : Type} (f : A -> B) :=
599 fix map (l : list A) : list B :=
600 match l with
601 | nil => nil
602 | cons a t => cons (f a) (map t)
603 end.
604
605 Definition mapT {A B} (f:A -> B) t :=
606 match t with E _ (a, l, ll) => E _ (f a, map f l, map (map f) ll) end.
607
608 Fixpoint mapU (f:U->U) u :=
609 match u with
610 | H t => H (mapT (mapT (mapU f)) t)
611 end.
612
613 End HighlyNested.
614
615 Module TestIntersection.
616
617 (* This example used to stress rtree.inter (3 nested types) *)
618
619 Inductive Pmap_ne (A : Type) :=
620 | PNode010 : A -> Pmap_ne A
621 | PNode110 : Pmap_ne A -> A -> Pmap_ne A.
622 Arguments PNode010 {A} _ : assert.
623 Arguments PNode110 {A} _ _ : assert.
624
625 Variant Pmap (A : Type) := PEmpty : Pmap A | PNodes : Pmap_ne A -> Pmap A.
626 Arguments PEmpty {A}.
627 Arguments PNodes {A} _.
628
629 Definition Pmap_ne_case {A B} (t : Pmap_ne A) (f : Pmap A -> option A -> Pmap A -> B) : B :=
630 match t with
631 | PNode010 x => f PEmpty (Some x) PEmpty
632 | PNode110 l x => f (PNodes l) (Some x) PEmpty
633 end.
634 Definition Pmap_fold_aux {A B} (go : B -> Pmap_ne A -> B) (y : B) (mt : Pmap A) : B :=
635 match mt with PEmpty => y | PNodes t => go y t end.
636 Definition Pmap_ne_fold {A B} (f : A -> B -> B) : B -> Pmap_ne A -> B :=
637 fix go y t :=
638 Pmap_ne_case t (fun ml mx mr => Pmap_fold_aux go
639 (Pmap_fold_aux go match mx with None => y | Some x => f x y end ml) mr).
640 Definition Pmap_fold {A} {B} (f : A -> B -> B) := Pmap_fold_aux (Pmap_ne_fold f).
641
642 Inductive test := Test : Pmap test -> test.
643 Fixpoint test_size (t : test) : nat :=
644 let 'Test ts := t in S (Pmap_fold (fun t' => plus (test_size t')) 0%nat ts).
645
646 End TestIntersection.