-
Notifications
You must be signed in to change notification settings - Fork 0
/
Words.v
1564 lines (1357 loc) · 47.5 KB
/
Words.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import MoreFun MoreList DeltaList GenFib GenG.
Import ListNotations.
(** * Morphic words
Some theory of infinite words generated by substitutions.
Hofstadter functions [f q] can be described by such infinite words.
*)
(** letters : here some natural numbers.
Most of the time, we'll only use 0..q for some parameter q. *)
Notation letter := nat (only parsing).
(** Finite word : list of letters *)
Definition word := list letter.
(** Sequence a.k.a Infinite word : function from nat to letter *)
Definition sequence := nat -> letter.
Definition PrefixSeq (u:word) (f:sequence) := u = take (length u) f.
(** Substitution : function form letter to words *)
Definition subst := letter -> word.
Definition apply : subst -> word -> word := @flat_map _ _.
Fixpoint napply (s:subst) n w :=
match n with
| 0 => w
| S n => napply s n (apply s w)
end.
Lemma napply_is_iter s n w : napply s n w = (apply s ^^n) w.
Proof.
revert w.
induction n; auto. intros. now rewrite iter_S, <- IHn.
Qed.
Lemma napply_1 s u : napply s 1 u = apply s u.
Proof.
reflexivity.
Qed.
Lemma apply_app s u v : apply s (u++v) = apply s u ++ apply s v.
Proof.
apply flat_map_app.
Qed.
Lemma apply_alt s w : apply s w = concat (map s w).
Proof.
apply flat_map_concat_map.
Qed.
Lemma napply_nil s n : napply s n [] = [].
Proof.
induction n; simpl; auto.
Qed.
Lemma napply_app s n u v : napply s n (u++v) = napply s n u ++ napply s n v.
Proof.
revert u v. induction n; intros; simpl; auto.
now rewrite apply_app, IHn.
Qed.
Lemma napply_cons s n a u : napply s n (a::u) = napply s n [a] ++ napply s n u.
Proof.
apply (napply_app s n [a] u).
Qed.
Lemma napply_alt s n u : napply s (S n) u = apply s (napply s n u).
Proof.
revert u.
induction n; intros; simpl; auto.
now rewrite <- IHn.
Qed.
Lemma napply_add s n m u : napply s (n+m) u = napply s n (napply s m u).
Proof.
induction n.
- simpl; auto.
- now rewrite Nat.add_succ_l, !napply_alt, IHn.
Qed.
Lemma napply_flat_map s n w :
napply s n w = flat_map (fun a => napply s n [a]) w.
Proof.
induction w; simpl; auto using napply_nil.
rewrite napply_cons. now f_equal.
Qed.
Lemma Prefix_apply s u v :
Prefix u v -> Prefix (apply s u) (apply s v).
Proof.
intros (w, <-). rewrite apply_app. now eexists.
Qed.
Lemma Prefix_napply s n u v :
Prefix u v -> Prefix (napply s n u) (napply s n v).
Proof.
intros (w, <-). rewrite napply_app. now eexists.
Qed.
Definition NoErase (s:subst) := forall a, s a <> [].
Definition Prolong (s:subst) a := exists u, u<>[] /\ s a = a::u.
Lemma noerase_nonnil_apply s u : NoErase s -> u<>[] -> apply s u <> [].
Proof.
intros NE. destruct u as [|a u]. easy. intros _. simpl.
specialize (NE a). destruct (s a) as [|b v]; easy.
Qed.
Lemma noerase_nonnil_napply s u n : NoErase s -> u<>[] -> napply s n u <> [].
Proof.
intros NE. revert u. induction n; simpl; intros; auto.
apply IHn. apply noerase_nonnil_apply; auto.
Qed.
Lemma noerase_prolong_napply_len s a :
NoErase s -> Prolong s a -> forall n, n < length (napply s n [a]).
Proof.
intros NE (u & Hu & PR).
induction n. simpl; auto.
simpl. rewrite app_nil_r, PR, napply_cons, app_length.
assert (length (napply s n u) <> 0).
{ rewrite length_zero_iff_nil. now apply noerase_nonnil_napply. }
lia.
Qed.
Lemma napply_prefix_S s a n :
Prolong s a -> Prefix (napply s n [a]) (napply s (S n) [a]).
Proof.
intros (u & Hu & PR).
simpl. rewrite app_nil_r, PR. rewrite (napply_cons _ _ _ u).
now exists (napply s n u).
Qed.
Lemma napply_prefix_mono s a n m : Prolong s a -> n <= m ->
Prefix (napply s n [a]) (napply s m [a]).
Proof.
intros PR.
induction 1.
- exists []. apply app_nil_r.
- destruct IHle as (w & E).
destruct (napply_prefix_S s a m) as (w' & E'); auto.
exists (w++w'). now rewrite app_assoc, E, E'.
Qed.
Lemma apply_grow s w : NoErase s -> length w <= length (apply s w).
Proof.
intros NE.
induction w; simpl; auto.
rewrite app_length. specialize (NE a). destruct (s a); simpl; lia || easy.
Qed.
Lemma napply_mono s n m w :
NoErase s -> n <= m -> length (napply s n w) <= length (napply s m w).
Proof.
induction 2; auto.
rewrite IHle. clear IHle H0. rewrite napply_alt. now apply apply_grow.
Qed.
(** Any nonerasing prolongeable substitution leads to a unique infinite
sequence *)
Definition SubstSeq (s:subst) (f:sequence) a :=
forall n, PrefixSeq (napply s n [a]) f.
Definition subst2seq s a :=
fun n => nth n (napply s n [a]) a.
Lemma subst2seq_indep s a b n : NoErase s -> Prolong s a ->
forall m, n <= m -> subst2seq s a n = nth n (napply s m [a]) b.
Proof.
intros. rewrite nth_indep with (d':=a).
2:{ apply Nat.le_lt_trans with m; trivial.
apply noerase_prolong_napply_len; trivial. }
apply Prefix_nth. apply napply_prefix_mono; auto.
apply noerase_prolong_napply_len; auto.
Qed.
Lemma substseq_exists s a :
NoErase s -> Prolong s a -> SubstSeq s (subst2seq s a) a.
Proof.
intros NE PR n.
red. symmetry. apply take_carac; auto.
intros m b Hm.
unfold subst2seq.
rewrite (nth_indep _ b a); auto.
destruct (Nat.le_gt_cases n m).
- apply Prefix_nth; auto.
apply napply_prefix_mono; auto.
- symmetry.
apply Prefix_nth; auto.
apply napply_prefix_mono; auto with arith.
apply noerase_prolong_napply_len; auto.
Qed.
Lemma substseq_unique s a f f' :
NoErase s -> Prolong s a -> SubstSeq s f a -> SubstSeq s f' a ->
(forall n, f n = f' n).
Proof.
intros NE PR Hf Hf' n.
specialize (Hf (S n)).
specialize (Hf' (S n)). unfold PrefixSeq in *.
set (u := napply s (S n) [a]) in *.
rewrite <- (take_nth f (length u) n a).
2:{ transitivity (S n); auto. apply noerase_prolong_napply_len; auto. }
rewrite <- (take_nth f' (length u) n a); auto.
2:{ transitivity (S n); auto. apply noerase_prolong_napply_len; auto. }
now rewrite <- Hf, <- Hf'.
Qed.
(** Specific susbstitution for Hofstadter functions.
Works on letters 0..q *)
Definition qsubst q (n:letter) := if n =? q then [q; 0] else [S n].
(** After the substitution of letters, now the substitution of words *)
Definition qsubstw q : word -> word := apply (qsubst q).
(** Iterated substitution of word *)
Definition qword q n := napply (qsubst q) n [q].
(** The corresponding infinite word (limit of the qword sequence) *)
Definition qseq q := subst2seq (qsubst q) q.
(* Compute take 20 (qseq 2). *)
(* [2; 0; 1; 2; 2; 0; 2; 0; 1; 2; 0; 1; 2; 2; 0; 1; 2; 2; 0; 2] *)
Lemma qsubst_noerase q : NoErase (qsubst q).
Proof.
red. intros c. unfold qsubst. now case Nat.eqb_spec.
Qed.
Lemma qsubst_prolong q : Prolong (qsubst q) q.
Proof.
red. exists [0]. split. easy.
unfold qsubst. now rewrite Nat.eqb_refl.
Qed.
Lemma qseq_SubstSeq q : SubstSeq (qsubst q) (qseq q) q.
Proof.
apply substseq_exists. apply qsubst_noerase. apply qsubst_prolong.
Qed.
Lemma qsubstw_app q u v : qsubstw q (u++v) = qsubstw q u ++ qsubstw q v.
Proof.
apply apply_app.
Qed.
Lemma qword_S q n : qword q (S n) = qsubstw q (qword q n).
Proof.
apply napply_alt.
Qed.
Lemma qseq_q_0 q : qseq q 0 = q.
Proof.
reflexivity.
Qed.
Lemma qseq_q_1 q : qseq q 1 = 0.
Proof.
unfold qseq, subst2seq, qsubst. simpl. now rewrite Nat.eqb_refl.
Qed.
Lemma qsubst_q q : qsubst q q = [q;0].
Proof.
unfold qsubst. now rewrite Nat.eqb_refl.
Qed.
Lemma qsubst_qm1 q : q<>0 -> qsubst q (q-1) = [q].
Proof.
intros Q. unfold qsubst. case Nat.eqb_spec; intros. lia. f_equal. lia.
Qed.
(** qword letters are always in 0..q *)
Lemma qsubst_letters q w :
Forall (fun a => a <= q) w ->
Forall (fun a => a <= q) (qsubstw q w).
Proof.
induction w; simpl; auto.
intros H; inversion_clear H. apply Forall_app. split; auto.
unfold qsubst.
case Nat.eqb_spec; intro; repeat (constructor; try lia).
Qed.
Lemma napply_qsubst_letters q n w :
Forall (fun a => a <= q) w ->
Forall (fun a => a <= q) (napply (qsubst q) n w).
Proof.
induction n.
- simpl. trivial.
- rewrite napply_alt. intros. now apply qsubst_letters, IHn.
Qed.
Lemma qword_letters q n : Forall (fun a => a <= q) (qword q n).
Proof.
apply napply_qsubst_letters. now constructor.
Qed.
Lemma qseq_letters q n : qseq q n <= q.
Proof.
unfold qseq, subst2seq.
set (l := napply _ _ _).
assert (Forall (fun a => a <= q) l).
{ apply napply_qsubst_letters. now constructor. }
clearbody l. revert n. induction H; simpl; now destruct n.
Qed.
(** Initial values *)
Lemma qword_0 q : qword q 0 = [q].
Proof.
reflexivity.
Qed.
Lemma qword_1 q : qword q 1 = [q;0].
Proof.
cbn. unfold qsubst. now rewrite Nat.eqb_refl.
Qed.
Lemma qsubst_low0 q n : n <= q -> napply (qsubst q) n [0] = [n].
Proof.
induction n. auto.
intros LE.
rewrite napply_alt. rewrite IHn; try lia. simpl.
unfold qsubst. case Nat.eqb_spec. lia. intros _. apply app_nil_r.
Qed.
Lemma qword_low q n : n <= S q -> qword q n = q :: List.seq 0 n.
Proof.
induction n.
- now rewrite qword_0.
- intros LE.
rewrite seq_S.
cbn. unfold qsubst at 2. rewrite Nat.eqb_refl. simpl.
rewrite napply_cons.
rewrite qsubst_low0; auto; try lia.
change (qword q n ++ [n] = q :: List.seq 0 n ++ [n]).
rewrite IHn; try lia. auto.
Qed.
(** Alt equation : *)
Lemma qword_eqn q n : q<=n -> qword q (S n) = qword q n ++ qword q (n-q).
Proof.
induction n.
- intros. replace q with 0 by lia. simpl; auto.
- intros LE. apply Nat.lt_eq_cases in LE. destruct LE as [LT|EQ].
+ replace (S n -q) with (S (n-q)) by lia.
remember (S n) as m eqn:E.
cbn.
rewrite app_nil_r.
unfold qsubst at 2. rewrite Nat.eqb_refl.
rewrite napply_cons. f_equal.
replace m with (m-q+q) by lia.
rewrite napply_add. rewrite qsubst_low0 by lia. simpl.
replace (m-q) with (S (n-q)) by lia.
simpl. now rewrite app_nil_r.
+ rewrite <- EQ. replace (q-q) with 0 by lia.
rewrite qword_0. cbn.
unfold qsubst at 2. rewrite Nat.eqb_refl, app_nil_r.
rewrite napply_cons. f_equal. apply qsubst_low0. lia.
Qed.
Lemma qword_len q n : length (qword q n) = A q n.
Proof.
induction n as [[|n] IH] using lt_wf_ind.
- now rewrite qword_0.
- case (Nat.le_gt_cases q n) as [LE|GT].
+ rewrite qword_eqn; auto. rewrite app_length, !IH; try lia.
simpl; auto.
+ rewrite qword_low by auto. simpl.
rewrite seq_length. rewrite !A_base; lia.
Qed.
Lemma qword_nz q n : qword q n <> [].
Proof.
unfold word. rewrite <- length_zero_iff_nil, qword_len.
generalize (A_nz q n); lia.
Qed.
Lemma qseq_alt q n m a : n < A q m -> qseq q n = nth n (qword q m) a.
Proof.
intros LE.
rewrite (qseq_SubstSeq q m).
change (napply _ _ _) with (qword q m).
rewrite qword_len, take_nth; auto.
Qed.
(** The qword are indeed prefixes of qseq *)
Lemma qseq_take_A q n : take (A q n) (qseq q) = qword q n.
Proof.
apply take_carac.
- apply qword_len.
- intros. symmetry. now apply qseq_alt.
Qed.
(** Link between [qseq] and Zeckendorf decomposition :
0 iff rank 0,
1 iff rank 1,
...
q iff rank >= q (or no rank, ie n=0)
Hence 0 in [qseq] whenever the [f q] function is flat.
*)
Definition omin (oa:option nat) (b:nat) :=
match oa with
| None => b
| Some a => Nat.min a b
end.
Definition bounded_rank q n := omin (rank q n) q.
Lemma qseq_bounded_rank q n : qseq q n = bounded_rank q n.
Proof.
induction n as [n IH] using lt_wf_ind.
assert (E := decomp_sum q n).
assert (D := decomp_delta q n).
set (l := decomp q n) in *.
destruct (rev l) as [|a rl] eqn:E'; apply rev_switch in E'.
- rewrite E' in *. simpl in E. rewrite <- E. easy.
- assert (A q a <= n < A q (S a)).
{ rewrite <- E, E', sumA_rev.
split.
+ simpl; lia.
+ apply decomp_max. apply Delta_rev. now rewrite <- E'. }
rewrite (qseq_alt q n (S a) 0) by lia.
destruct (Nat.lt_ge_cases a q) as [LT|LE].
+ rewrite qword_low by lia.
destruct n as [|n]; try easy.
change (nth _ _ _) with (nth n (List.seq 0 (S a)) 0).
rewrite !A_base in H; try lia.
replace n with a by lia.
rewrite seq_nth by lia. simpl.
unfold bounded_rank, rank. rewrite decomp_low; simpl; lia.
+ rewrite qword_eqn by auto.
rewrite app_nth2; rewrite qword_len; try lia.
rewrite <- qseq_alt by (simpl in H; lia).
rewrite IH by (generalize (@A_nz q a); lia).
unfold bounded_rank, rank. fold l; rewrite E'; simpl rev.
replace (decomp _ _) with (rev rl).
2:{ symmetry; apply decomp_carac.
- rewrite E' in D. simpl in D. now apply Delta_app_iff in D.
- revert E. rewrite E'. simpl. rewrite sumA_app. simpl. lia. }
destruct (rev rl); simpl; lia.
Qed.
(** Projecting qseq to 0 and 1 (i.e. all letters except 0 become 1,
i.e. applying (min 1) to qseq) gives the sequence of (f q) increments. *)
Lemma min_1_qseq_is_delta_f q n :
q<>0 -> Nat.min 1 (qseq q n) = f q (S n) - f q n.
Proof.
intros Hq.
rewrite qseq_bounded_rank.
destruct (flat_rank_0 q n) as (_,H).
destruct (step_rank_nz q n) as (_,H').
unfold bounded_rank. destruct (rank q n) as [[|r]|]; unfold omin.
- simpl. rewrite H; trivial. lia.
- rewrite H'; try easy. lia.
- rewrite H'; try easy. lia.
Qed.
(** More generally, projecting letters p..q to 1 and letters 0..(p-1) to 0
gives the sequence of (fs q p) increments. *)
Lemma qseq_above_p_is_delta_fs q p n :
q<>0 -> p<=q ->
(if p <=? qseq q n then 1 else 0) = fs q p (S n) - fs q p n.
Proof.
intros Hq Hp.
rewrite qseq_bounded_rank.
destruct (@fs_flat_iff' q p n) as (_,H).
destruct (@fs_nonflat_iff' q p n) as (_,H').
unfold bounded_rank. destruct (rank q n) as [r|]; unfold omin.
- destruct (Nat.lt_decidable r p) as [LT|NLT]; case Nat.leb_spec; lia.
- simpl in *. case Nat.leb_spec; lia.
Qed.
(** Another description of [qseq q n] :
sum for p=1..q of [fs q p (n+1) - fs q p n] *)
Definition cumul_diff_fs q n :=
cumul (fun p => fs q (S p) (S n) - fs q (S p) n) q.
Lemma qseq_is_cumul_diff_fs q n : qseq q n = cumul_diff_fs q n.
Proof.
rewrite qseq_bounded_rank. unfold bounded_rank.
destruct (rank q n) as [r|] eqn:E; simpl.
- rewrite <- (cumul_ltb q r). unfold cumul_diff_fs.
apply cumul_ext.
intros m Hm.
case Nat.ltb_spec; intros L.
+ destruct (@fs_nonflat_iff' q (S m) n) as (_,->); try lia.
rewrite E; simpl; lia.
+ destruct (@fs_flat_iff' q (S m) n) as (_,->); try lia.
rewrite E; simpl; lia.
- rewrite rank_none in E. subst. unfold cumul_diff_fs.
erewrite cumul_ext, (cumul_cst 1); try lia.
intros x _. rewrite fs_q_0, fs_q_1. lia.
Qed.
(** Hence the sum of the n first letters of [qseq q] is
the sum for p=1..q of [fs q p n]. *)
Lemma cumul_qseq_is_cumul_fs q n :
cumul (qseq q) n = cumul (fun p => fs q (S p) n) q.
Proof.
induction n; cbn -[fs].
- erewrite cumul_ext; auto using cumul_0, fs_q_0.
- rewrite IHn, qseq_is_cumul_diff_fs. unfold cumul_diff_fs.
rewrite <- cumul_add. apply cumul_ext.
intros m Hm. generalize (fs_mono_S q (S m) n). lia.
Qed.
(** Counting letter 0 in [qseq q] leads bacq to the [f q] fonction. *)
Lemma f_count_0 q n : q<>0 -> count (qseq q) 0 n + f q n = n.
Proof.
induction n.
- easy.
- simpl. intros Hq.
rewrite qseq_bounded_rank.
unfold bounded_rank.
destruct (f_step q n) as [E|E].
+ rewrite E. rewrite flat_rank_0 in E. rewrite E. simpl. lia.
+ rewrite E. rewrite step_rank_nz in E.
destruct (rank q n) as [[|r]|]; simpl; try easy;
destruct q; simpl; auto; try lia.
Qed.
(** Similarly, counting all letters above [p] leads to [fs q p],
the p-iterate of [f q]. *)
Lemma fs_count_above q p n :
p <= q -> fs q p n = count_above (qseq q) p n.
Proof.
intros Hp.
induction n.
- simpl. apply fs_q_0.
- simpl. rewrite qseq_bounded_rank.
unfold bounded_rank.
destruct (fs_step q p n) as [E|E].
+ rewrite E. rewrite fs_flat_iff' in E by lia.
destruct (rank q n); simpl; try easy.
red in E. case Nat.leb_spec; lia.
+ rewrite E. rewrite fs_nonflat_iff' in E.
destruct (rank q n); simpl in *; case Nat.leb_spec; lia.
Qed.
(** Particular case : p=q *)
Lemma count_above_qseq_q q n :
count_above (qseq q) q n = count (qseq q) q n.
Proof.
induction n; simpl; auto.
rewrite qseq_bounded_rank. unfold bounded_rank.
destruct (rank q n); simpl in *; case Nat.leb_spec; case Nat.eqb_spec; lia.
Qed.
Lemma fs_count_q q n : fs q q n = count (qseq q) q n.
Proof.
rewrite fs_count_above by lia.
apply count_above_qseq_q.
Qed.
(** Prefixes of qseq *)
Notation qprefix q n := (take n (qseq q)).
Lemma qprefix_length q n : length (qprefix q n) = n.
Proof.
apply take_length.
Qed.
Lemma qprefix_carac q u :
PrefixSeq u (qseq q) <-> u = qprefix q (length u).
Proof.
reflexivity.
Qed.
Lemma qprefix_ok q n : PrefixSeq (qprefix q n) (qseq q).
Proof.
red. now rewrite qprefix_length.
Qed.
Lemma qprefix_alt q n : qprefix q n = firstn n (qword q (invA_up q n)).
Proof.
rewrite <- qseq_take_A. symmetry. apply firstn_take. apply invA_up_spec.
Qed.
Lemma qprefix_letters q n : Forall (fun a => a <= q) (qprefix q n).
Proof.
induction n.
- constructor.
- rewrite take_S. apply Forall_app. split; trivial.
repeat constructor. apply qseq_letters.
Qed.
Lemma qprefix_A_qword q p : qprefix q (A q p) = qword q p.
Proof.
apply qseq_take_A.
Qed.
Lemma qprefix_prefix_qword q n p :
n <= A q p -> Prefix (qprefix q n) (qword q p).
Proof.
intros H. rewrite <- qprefix_A_qword. now apply Prefix_take.
Qed.
Lemma qprefix_firstn_qword q n p :
n <= A q p -> qprefix q n = firstn n (qword q p).
Proof.
intros H. rewrite <- (take_length n (qseq q)) at 2.
apply Prefix_equiv. now apply qprefix_prefix_qword.
Qed.
Lemma qword_prefix q p p' : p <= p' -> Prefix (qword q p) (qword q p').
Proof.
apply napply_prefix_mono, qsubst_prolong.
Qed.
(** Full decomposition of any prefix of qword, then qseq (used in Lim) *)
Definition qwords q (l:list nat) : word := flat_map (qword q) l.
Lemma qwords_singl q n : qwords q [n] = qword q n.
Proof.
unfold qwords. simpl. now apply app_nil_r.
Qed.
Lemma qwords_app q l l' : qwords q (l++l') = qwords q l ++ qwords q l'.
Proof.
apply flat_map_app.
Qed.
Lemma qwords_cons q a l : qwords q (a::l) = qword q a ++ qwords q l.
Proof.
change (a::l) with ([a]++l). now rewrite qwords_app, qwords_singl.
Qed.
Lemma qsubst_qwords q l :
qsubstw q (qwords q l) = qwords q (map S l).
Proof.
induction l; simpl; auto. now rewrite qsubstw_app, IHl, qword_S.
Qed.
Lemma decomp_prefix_qword q w n l :
Prefix w (qword q n) -> l = rev (decomp q (length w)) -> w = qwords q l.
Proof.
revert w l. induction n as [n IH] using lt_wf_ind.
intros w l P.
destruct (Nat.le_gt_cases n (S q)).
- clear IH. rewrite qword_low in * by trivial.
destruct (Prefix_cons_inv _ _ _ P) as [->|(w' & E' & P')].
+ simpl. now intros ->.
+ assert (LE := Prefix_len _ _ P'). rewrite seq_length in LE.
apply Prefix_seq in P'.
rewrite E'. simpl length. set (p := length w') in *.
rewrite decomp_low by lia.
replace (S _ -1) with p by lia. intros ->. simpl.
rewrite app_nil_r, qword_low, <-P' by lia. trivial.
- assert (P' := P). destruct P' as ([|a u] & E).
+ rewrite app_nil_r in E. rewrite E. rewrite qword_len.
replace (decomp q (A q n)) with [n].
2:{ symmetry; apply decomp_carac; try constructor; simpl; auto. }
intros ->. simpl. now rewrite app_nil_r.
+ assert (LT : length w < A q n).
{ rewrite <- qword_len, <- E, app_length. simpl. lia. }
clear E.
destruct n; [lia|].
rewrite qword_eqn in P by lia.
apply Prefix_app in P. destruct P as [P|(w' & -> & P)].
* apply (IH n); auto.
* rewrite app_length, qword_len in *.
rewrite Nat.add_comm, decomp_plus_A by (simpl in LT; lia).
rewrite rev_app_distr. intros ->. rewrite qwords_app.
simpl. f_equal. now rewrite app_nil_r.
apply (IH (n-q)); auto. lia.
Qed.
Lemma decomp_prefix_qseq q n :
take n (qseq q) = qwords q (rev (decomp q n)).
Proof.
assert (H := invA_spec q n). set (m := invA q n) in *.
assert (H' : n <= A q (S m)) by lia. clear H. clearbody m.
apply (decomp_prefix_qword _ _ (S m)).
- apply Prefix_nth_nat. intros p a. rewrite take_length. intros LT.
rewrite take_nth by trivial.
apply qseq_alt; lia.
- now rewrite take_length.
Qed.
Lemma renorm_qwords q l :
Delta q l -> qwords q (rev (renorm q l)) = qwords q (rev l).
Proof.
unfold renorm.
generalize (Nat.le_refl (length l)). generalize (length l) at 2 3.
intro n. revert l.
induction n as [[|n] IH] using lt_wf_ind; intros l Hl D.
- now destruct l.
- destruct l as [|i l]; simpl in *; trivial.
apply Delta_inv in D.
destruct (renorm_loop q l n) as [|a l'] eqn:E.
+ simpl. rewrite qwords_app.
rewrite <- (IH n); trivial; try lia. now rewrite E.
+ case Nat.eqb_spec; intros.
* rewrite IH; try lia.
2:{ change (length (S a :: l')) with (length (a::l')).
rewrite <- E. rewrite renorm_loop_length; lia. }
2:{ apply Delta_S_cons. rewrite <- E.
apply renorm_loop_delta; trivial. lia. }
subst a. simpl. rewrite !qwords_app, !qwords_singl.
rewrite qword_eqn by lia.
replace (i+q-q) with i by lia. rewrite app_assoc. f_equal.
rewrite <- (qwords_singl q (i+q)), <- qwords_app.
change (_++_) with (rev (i+q::l')). rewrite <- E.
apply IH; trivial; try lia.
* rewrite <- E. simpl. rewrite !qwords_app. f_equal.
apply IH; trivial; lia.
Qed.
Lemma prefix_qseq_laxdecomp q n l :
DeltaRev q l -> sumA q l = n -> take n (qseq q) = qwords q l.
Proof.
rewrite <- Delta_rev. rewrite <- (rev_involutive l) at 2 3.
rewrite sumA_rev. intros D E.
rewrite <- renorm_qwords by trivial.
rewrite decomp_prefix_qseq. f_equal. f_equal.
apply decomp_carac. now apply renorm_delta. now rewrite renorm_sum.
Qed.
Lemma count_qseq_decomp q n a :
count (qseq q) a n =
list_sum (map (fun m => nbocc a (qword q m)) (decomp q n)).
Proof.
rewrite count_nbocc, decomp_prefix_qseq. unfold qwords.
rewrite flat_map_concat_map, nbocc_concat.
now rewrite map_map, map_rev, list_sum_rev.
Qed.
(** Occurrences of letters when applying qsubst *)
Lemma nbocc_qsubst q : q<>0 ->
forall w,
nbocc 0 (qsubstw q w) = nbocc q w /\
nbocc q (qsubstw q w) = nbocc (q-1) w + nbocc q w /\
forall p, p<q-1 -> nbocc (S p) (qsubstw q w) = nbocc p w.
Proof.
induction w; simpl.
- repeat split; lia.
- simpl. rewrite !nbocc_app.
destruct IHw as (IHw0 & IHwq & IHw).
rewrite IHw0, IHwq. repeat split.
+ unfold qsubst. case Nat.eqb_spec; simpl; try lia.
case Nat.eqb_spec; simpl; try lia.
+ unfold qsubst. case Nat.eqb_spec; try lia.
* intros ->. cbn -[Nat.eqb]. rewrite Nat.eqb_refl.
do 2 (case Nat.eqb_spec; try lia).
* intros N. case Nat.eqb_spec; try lia.
{ intros ->. replace (S (q-1)) with q by lia. simpl.
rewrite Nat.eqb_refl. lia. }
{ intros N'. cbn -[Nat.eqb]. case Nat.eqb_spec; try lia. }
+ intros p Hp. rewrite nbocc_app. rewrite (IHw p Hp).
unfold qsubst. case Nat.eqb_spec; try lia.
* intros ->. cbn -[Nat.eqb].
do 3 (case Nat.eqb_spec; try lia).
* intros N. simpl. lia.
Qed.
(* In [napply] of [qsubst], the initial letter doesn't matter much :
low letters become [q] after some rounds of [qsubst], while
unexpected letters stay large. *)
Lemma napply_qsubst_shift q p n : p+n <= q \/ q < p ->
napply (qsubst q) n [p] = [p+n].
Proof.
revert p.
induction n; simpl; intros.
- f_equal. lia.
- rewrite app_nil_r. unfold qsubst at 2.
case Nat.eqb_spec; try lia. intros. rewrite IHn. f_equal; lia. lia.
Qed.
Lemma napply_qsubst_is_qword q p n : p <= q -> q <= n+p ->
napply (qsubst q) n [p] = qword q (n+p-q).
Proof.
intros. replace n with ((n+p-q)+(q-p)) by lia.
rewrite napply_add, napply_qsubst_shift by lia.
unfold qword; f_equal. lia. f_equal; lia.
Qed.
(** We hence have an easy bound on lengths of n-iterates of qsubst on
single letters. *)
Definition NapplySizeBound s n N :=
forall a:letter, length (napply s n [a]) <= N.
Lemma Bound_A q n : NapplySizeBound (qsubst q) n (A q n).
Proof.
unfold NapplySizeBound. intros.
destruct (Nat.le_gt_cases a q).
- destruct (Nat.le_gt_cases q (n+a)).
+ rewrite napply_qsubst_is_qword by lia. rewrite qword_len.
apply A_mono. lia.
+ rewrite napply_qsubst_shift by lia. simpl. apply A_nz.
- rewrite napply_qsubst_shift by lia. simpl. apply A_nz.
Qed.
(** Counting occurrences of [q] and [0] in [qword] *)
Lemma nbocc_q_qword q n : nbocc q (qword q n) = A q (n-q).
Proof.
induction n as [n IH] using lt_wf_ind.
destruct (Nat.le_gt_cases n q).
- rewrite qword_low by lia. simpl.
rewrite Nat.eqb_refl, nbocc_notin.
2:{ rewrite in_seq; lia. }
now replace (n-q) with 0 by lia.
- destruct n; try lia.
rewrite qword_eqn, nbocc_app by lia.
rewrite IH by lia.
rewrite IH by lia.
now replace (S n - q) with (S (n-q)) by lia.
Qed.
Lemma nbocc_0_qword q n : q<>0 -> nbocc 0 (qword q (S n)) = A q (n-q).
Proof.
intros Hq.
induction n as [n IH] using lt_wf_ind.
destruct (Nat.le_gt_cases (S n) q).
- rewrite qword_low by lia. simpl.
rewrite nbocc_notin by (rewrite in_seq; lia).
case Nat.eqb_spec; try lia.
now replace (n-q) with 0 by lia.
- destruct n; try lia.
rewrite qword_eqn, nbocc_app by lia.
rewrite IH by lia.
destruct (Nat.le_gt_cases (S n) q).
+ replace (S n - q) with 0 by lia. replace (n-q) with 0 by lia. simpl.
case Nat.eqb_spec; try lia.
+ replace (S n - q) with (S (n-q)) by lia.
rewrite IH by lia. now simpl.
Qed.
(* Special case q=2
0 -> 1
1 -> 2
2 -> 20
Occurrence matrix :
001
100
011
*)
Lemma nbocc_qsubst2 w :
nbocc 0 (qsubstw 2 w) = nbocc 2 w /\
nbocc 1 (qsubstw 2 w) = nbocc 0 w /\
nbocc 2 (qsubstw 2 w) = nbocc 1 w + nbocc 2 w.
Proof.
assert (H:2<>0) by lia.
destruct (nbocc_qsubst 2 H w) as (H0 & H1 & Hp). repeat split; trivial.
apply Hp; lia.
Qed.
Definition tripleocc w := (nbocc 0 w, nbocc 1 w, nbocc 2 w).
Definition occurmatrix '(x,y,z) : nat*nat*nat := (z,x,y+z).
Lemma nbocc_qsubst2_bis w :
tripleocc (qsubstw 2 w) = occurmatrix (tripleocc w).
Proof.
unfold tripleocc.
now destruct (nbocc_qsubst2 w) as (-> & -> & ->).
Qed.
Lemma len_nbocc_012 w :
Forall (fun a => a <= 2) w ->
length w = nbocc 0 w + nbocc 1 w + nbocc 2 w.
Proof.
intros. rewrite nbocc_total_le with (k:=2); simpl; auto; lia.
Qed.
Lemma len_qsubst q w :
length (qsubstw q w) = length w + nbocc q w.
Proof.
induction w; simpl; auto.
rewrite app_length, IHw.
unfold qsubst at 1.
case Nat.eqb_spec; simpl; lia.
Qed.
(* Special case q=3
0 -> 1
1 -> 2
2 -> 3
3 -> 30
Occurrence matrix :
0001
1000
0100
0011
*)
Lemma nbocc_qsubst3 w :
nbocc 0 (qsubstw 3 w) = nbocc 3 w /\
nbocc 1 (qsubstw 3 w) = nbocc 0 w /\
nbocc 2 (qsubstw 3 w) = nbocc 1 w /\
nbocc 3 (qsubstw 3 w) = nbocc 2 w + nbocc 3 w.
Proof.
assert (H:3<>0) by lia.
destruct (nbocc_qsubst 3 H w) as (H0 & H1 & Hp). repeat split; trivial.
apply Hp; lia.
apply Hp; lia.
Qed.
Definition fourocc w := (nbocc 0 w, nbocc 1 w, nbocc 2 w, nbocc 3 w).
Definition occurmatrix4 '(x,y,z,t) : nat*nat*nat*nat := (t,x,y,z+t).
Lemma nbocc_qsubst3_bis w :
fourocc (qsubstw 3 w) = occurmatrix4 (fourocc w).
Proof.
unfold fourocc.
now destruct (nbocc_qsubst3 w) as (-> & -> & -> & ->).
Qed.
Lemma len_nbocc_0123 w :
Forall (fun a => a <= 3) w ->
length w = nbocc 0 w + nbocc 1 w + nbocc 2 w + nbocc 3 w.
Proof.
intros. rewrite nbocc_total_le with (k:=3); simpl; auto; lia.
Qed.
(* From a Prefix of napply of a word to a prefix of napply of a letter *)
Lemma napply_prefix s n u v :
NoErase s -> v<>[] ->
Prefix u (napply s n v) ->
exists w t a,
Prefix (w++[a]) v /\ u = napply s n w ++ t /\ Prefix t (napply s n [a]).
Proof.
intros NE. revert u.
induction v; try easy.
- intros u _. rewrite napply_cons. intros Pr.
apply Prefix_app in Pr. destruct Pr as [Pr|(u' & E & Pr)].
+ exists [], u, a. rewrite napply_nil. simpl. split; auto. now exists v.
+ destruct (list_eq_dec Nat.eq_dec v []) as [->|NE'].
* rewrite napply_nil in Pr. apply Prefix_nil in Pr. subst u'.
rewrite app_nil_r in E.
exists [], u, a. rewrite napply_nil.
repeat split; subst; auto using Prefix_id.
* destruct (IHv u' NE' Pr) as (w & t & b & Hv & E' & Ht).
exists (a::w), t, b. repeat split; auto.
{ simpl. now apply Prefix_cons. }
{ now rewrite napply_cons, <- app_assoc, <- E', <- E. }
Qed.
(** Saari's Lemma 4 : decomposition of a prefix of s^n(a),
leaving alone a final part whose size is below a certain threshold *)
Lemma Saari_lemma4 s a n w G M1 MG : G<>0 -> n<>0 ->
NapplySizeBound s 1 M1 -> NapplySizeBound s G MG ->
NoErase s ->
Prefix w (napply s n [a]) ->
exists l : list (nat * word), exists z,
w = flat_map (fun '(ni,ui) => napply s ni ui) l ++ z
/\ Forall (fun '(ni,ui) => length ui <= M1 /\ G <= ni < n) l
/\ DeltaRev 1 (map fst l)
/\ length z <= MG.
Proof.
intros HG. revert a w.
induction n as [n IH] using lt_wf_ind.
intros a w Hn B1 BG NE Pr.
destruct (Nat.le_gt_cases n G).
- exists []. exists w. simpl. repeat split; auto. constructor.
apply Prefix_len in Pr.
etransitivity; [apply Pr|]. red in BG.
rewrite <- (BG a). now apply napply_mono.