-
Notifications
You must be signed in to change notification settings - Fork 143
/
SimplyTypedArithmetic.v
8563 lines (8018 loc) · 388 KB
/
SimplyTypedArithmetic.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
(* Following http://adam.chlipala.net/theses/andreser.pdf chapter 3 *)
Require Import Coq.ZArith.ZArith Coq.micromega.Lia Crypto.Algebra.Nsatz.
Require Import Coq.Strings.String.
Require Import Coq.MSets.MSetPositive.
Require Import Coq.FSets.FMapPositive.
Require Import Coq.derive.Derive.
Require Import Crypto.Util.Tactics.UniquePose Crypto.Util.Decidable.
Require Import Crypto.Util.Tuple Crypto.Util.Prod Crypto.Util.LetIn.
Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.NatUtil.
Require Import QArith.QArith_base QArith.Qround Crypto.Util.QUtil.
Require Import Crypto.Algebra.Ring Crypto.Util.Decidable.Bool2Prop.
Require Import Crypto.Algebra.Ring.
Require Import Crypto.Algebra.SubsetoidRing.
Require Import Crypto.Arithmetic.PrimeFieldTheorems.
Require Import Crypto.Arithmetic.BarrettReduction.Generalized.
Require Import Crypto.Arithmetic.MontgomeryReduction.Definition.
Require Import Crypto.Arithmetic.MontgomeryReduction.Proofs.
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
Require Import Crypto.Util.ZRange.
Require Import Crypto.Util.ZRange.Operations.
Require Import Crypto.Util.Tactics.RunTacticAsConstr.
Require Import Crypto.Util.Tactics.Head.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.Sum.
Require Import Crypto.Util.ZUtil.
Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div Crypto.Util.ZUtil.Hints.Core.
Require Import Crypto.Util.ZUtil.Hints.PullPush.
Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit.
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
Require Import Crypto.Util.Tactics.SpecializeBy.
Require Import Crypto.Util.Tactics.SplitInContext.
Require Import Crypto.Util.Tactics.SubstEvars.
Require Import Crypto.Util.Notations.
Require Import Crypto.Util.ZUtil.Definitions.
Require Import Crypto.Util.ZUtil.CC Crypto.Util.ZUtil.Rshi.
Require Import Crypto.Util.ZUtil.Zselect Crypto.Util.ZUtil.AddModulo.
Require Import Crypto.Util.ZUtil.AddGetCarry Crypto.Util.ZUtil.MulSplit.
Require Import Crypto.Util.ZUtil Crypto.Util.ZUtil.Hints.Core.
Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div.
Require Import Crypto.Util.ZUtil.Hints.PullPush.
Require Import Crypto.Util.ZUtil.EquivModulo.
Import ListNotations. Local Open Scope Z_scope.
Module Associational.
Definition eval (p:list (Z*Z)) : Z :=
fold_right (fun x y => x + y) 0%Z (map (fun t => fst t * snd t) p).
Lemma eval_nil : eval nil = 0.
Proof. trivial. Qed.
Lemma eval_cons p q : eval (p::q) = fst p * snd p + eval q.
Proof. trivial. Qed.
Lemma eval_app p q: eval (p++q) = eval p + eval q.
Proof. induction p; rewrite <-?List.app_comm_cons;
rewrite ?eval_nil, ?eval_cons; nsatz. Qed.
Hint Rewrite eval_nil eval_cons eval_app : push_eval.
Local Ltac push := autorewrite with
push_eval push_map push_partition push_flat_map
push_fold_right push_nth_default cancel_pair.
Lemma eval_map_mul (a x:Z) (p:list (Z*Z))
: eval (List.map (fun t => (a*fst t, x*snd t)) p) = a*x*eval p.
Proof. induction p; push; nsatz. Qed.
Hint Rewrite eval_map_mul : push_eval.
Definition mul (p q:list (Z*Z)) : list (Z*Z) :=
flat_map (fun t =>
map (fun t' =>
(fst t * fst t', snd t * snd t'))
q) p.
Lemma eval_mul p q : eval (mul p q) = eval p * eval q.
Proof. induction p; cbv [mul]; push; nsatz. Qed.
Hint Rewrite eval_mul : push_eval.
Definition negate_snd (p:list (Z*Z)) : list (Z*Z) :=
map (fun cx => (fst cx, -snd cx)) p.
Lemma eval_negate_snd p : eval (negate_snd p) = - eval p.
Proof. induction p; cbv [negate_snd]; push; nsatz. Qed.
Hint Rewrite eval_negate_snd : push_eval.
Example base10_2digit_mul (a0:Z) (a1:Z) (b0:Z) (b1:Z) :
{ab| eval ab = eval [(10,a1);(1,a0)] * eval [(10,b1);(1,b0)]}.
eexists ?[ab].
(* Goal: eval ?ab = eval [(10,a1);(1,a0)] * eval [(10,b1);(1,b0)] *)
rewrite <-eval_mul.
(* Goal: eval ?ab = eval (mul [(10,a1);(1,a0)] [(10,b1);(1,b0)]) *)
cbv -[Z.mul eval]; cbn -[eval].
(* Goal: eval ?ab = eval [(100,(a1*b1));(10,a1*b0);(10,a0*b1);(1,a0*b0)]%RT *)
trivial. Defined.
Definition split (s:Z) (p:list (Z*Z)) : list (Z*Z) * list (Z*Z)
:= let hi_lo := partition (fun t => fst t mod s =? 0) p in
(snd hi_lo, map (fun t => (fst t / s, snd t)) (fst hi_lo)).
Lemma eval_split s p (s_nz:s<>0) :
eval (fst (split s p)) + s * eval (snd (split s p)) = eval p.
Proof. cbv [Let_In split]; induction p;
repeat match goal with
| |- context[?a/?b] =>
unique pose proof (Z_div_exact_full_2 a b ltac:(trivial) ltac:(trivial))
| _ => progress push
| _ => progress break_match
| _ => progress nsatz end. Qed.
Lemma reduction_rule a b s c (modulus_nz:s-c<>0) :
(a + s * b) mod (s - c) = (a + c * b) mod (s - c).
Proof. replace (a + s * b) with ((a + c*b) + b*(s-c)) by nsatz.
rewrite Z.add_mod,Z_mod_mult,Z.add_0_r,Z.mod_mod;trivial. Qed.
Definition reduce (s:Z) (c:list _) (p:list _) : list (Z*Z) :=
let lo_hi := split s p in fst lo_hi ++ mul c (snd lo_hi).
Lemma eval_reduce s c p (s_nz:s<>0) (modulus_nz:s-eval c<>0) :
eval (reduce s c p) mod (s - eval c) = eval p mod (s - eval c).
Proof. cbv [reduce]; push.
rewrite <-reduction_rule, eval_split; trivial. Qed.
Hint Rewrite eval_reduce : push_eval.
Section Carries.
Definition carryterm (w fw:Z) (t:Z * Z) :=
if (Z.eqb (fst t) w)
then dlet_nd t2 := snd t in
dlet_nd d2 := t2 / fw in
dlet_nd m2 := t2 mod fw in
[(w * fw, d2);(w,m2)]
else [t].
Lemma eval_carryterm w fw (t:Z * Z) (fw_nonzero:fw<>0):
eval (carryterm w fw t) = eval [t].
Proof using Type*.
cbv [carryterm Let_In]; break_match; push; [|trivial].
pose proof (Z.div_mod (snd t) fw fw_nonzero).
rewrite Z.eqb_eq in *.
nsatz.
Qed. Hint Rewrite eval_carryterm using auto : push_eval.
Definition carry (w fw:Z) (p:list (Z * Z)):=
flat_map (carryterm w fw) p.
Lemma eval_carry w fw p (fw_nonzero:fw<>0):
eval (carry w fw p) = eval p.
Proof using Type*. cbv [carry]; induction p; push; nsatz. Qed.
Hint Rewrite eval_carry using auto : push_eval.
End Carries.
End Associational.
Module Positional. Section Positional.
Context (weight : nat -> Z)
(weight_0 : weight 0%nat = 1)
(weight_nz : forall i, weight i <> 0).
Definition to_associational (n:nat) (xs:list Z) : list (Z*Z)
:= combine (map weight (List.seq 0 n)) xs.
Definition eval n x := Associational.eval (@to_associational n x).
Lemma eval_to_associational n x :
Associational.eval (@to_associational n x) = eval n x.
Proof. trivial. Qed.
Hint Rewrite @eval_to_associational : push_eval.
Lemma eval_nil n : eval n [] = 0.
Proof. cbv [eval to_associational]. rewrite combine_nil_r. reflexivity. Qed.
Hint Rewrite eval_nil : push_eval.
Lemma eval0 p : eval 0 p = 0.
Proof. cbv [eval to_associational]. reflexivity. Qed.
Hint Rewrite eval0 : push_eval.
Lemma eval_snoc n m x y : n = length x -> m = S n -> eval m (x ++ [y]) = eval n x + weight n * y.
Proof.
cbv [eval to_associational]; intros; subst n m.
rewrite seq_snoc, map_app.
rewrite combine_app_samelength by distr_length.
autorewrite with push_eval. simpl.
autorewrite with push_eval cancel_pair; ring.
Qed.
(* SKIP over this: zeros, add_to_nth *)
Local Ltac push := autorewrite with push_eval push_map distr_length
push_flat_map push_fold_right push_nth_default cancel_pair natsimplify.
Definition zeros n : list Z := List.repeat 0 n.
Lemma length_zeros n : length (zeros n) = n. Proof. cbv [zeros]; distr_length. Qed.
Hint Rewrite length_zeros : distr_length.
Lemma eval_zeros n : eval n (zeros n) = 0.
Proof.
cbv [eval Associational.eval to_associational zeros].
rewrite <- (seq_length n 0) at 2.
generalize dependent (List.seq 0 n); intro xs.
induction xs; simpl; nsatz. Qed.
Definition add_to_nth i x (ls : list Z) : list Z
:= ListUtil.update_nth i (fun y => x + y) ls.
Lemma length_add_to_nth i x ls : length (add_to_nth i x ls) = length ls.
Proof. cbv [add_to_nth]; distr_length. Qed.
Hint Rewrite length_add_to_nth : distr_length.
Lemma eval_add_to_nth (n:nat) (i:nat) (x:Z) (xs:list Z) (H:(i<length xs)%nat)
(Hn : length xs = n) (* N.B. We really only need [i < Nat.min n (length xs)] *) :
eval n (add_to_nth i x xs) = weight i * x + eval n xs.
Proof.
subst n.
cbv [eval to_associational add_to_nth].
rewrite ListUtil.combine_update_nth_r at 1.
rewrite <-(update_nth_id i (List.combine _ _)) at 2.
rewrite <-!(ListUtil.splice_nth_equiv_update_nth_update _ _
(weight 0, 0)) by (push; lia); cbv [ListUtil.splice_nth id].
repeat match goal with
| _ => progress push
| _ => progress break_match
| _ => progress (apply Zminus_eq; ring_simplify)
| _ => rewrite <-ListUtil.map_nth_default_always
end; lia. Qed.
Hint Rewrite @eval_add_to_nth eval_zeros : push_eval.
Definition place (t:Z*Z) (i:nat) : nat * Z :=
nat_rect
(fun _ => (nat * Z)%type)
(O, fst t * snd t)
(fun i' place_i'
=> let i := S i' in
if (fst t mod weight i =? 0)
then (i, let c := fst t / weight i in c * snd t)
else place_i')
i.
Lemma place_in_range (t:Z*Z) (n:nat) : (fst (place t n) < S n)%nat.
Proof. induction n; cbv [place nat_rect] in *; break_match; autorewrite with cancel_pair; try omega. Qed.
Lemma weight_place t i : weight (fst (place t i)) * snd (place t i) = fst t * snd t.
Proof. induction i; cbv [place nat_rect] in *; break_match; push;
repeat match goal with |- context[?a/?b] =>
unique pose proof (Z_div_exact_full_2 a b ltac:(auto) ltac:(auto))
end; nsatz. Qed.
Hint Rewrite weight_place : push_eval.
Definition from_associational n (p:list (Z*Z)) :=
List.fold_right (fun t ls =>
dlet_nd p := place t (pred n) in
add_to_nth (fst p) (snd p) ls ) (zeros n) p.
Lemma eval_from_associational n p (n_nz:n<>O \/ p = nil) :
eval n (from_associational n p) = Associational.eval p.
Proof. destruct n_nz; [ induction p | subst p ];
cbv [from_associational Let_In] in *; push; try
pose proof place_in_range a (pred n); try omega; try nsatz;
apply fold_right_invariant; cbv [zeros add_to_nth];
intros; rewrite ?map_length, ?List.repeat_length, ?seq_length, ?length_update_nth;
try omega. Qed.
Hint Rewrite @eval_from_associational : push_eval.
Lemma length_from_associational n p : length (from_associational n p) = n.
Proof. cbv [from_associational Let_In]. apply fold_right_invariant; intros; distr_length. Qed.
Hint Rewrite length_from_associational : distr_length.
Section mulmod.
Context (s:Z) (s_nz:s <> 0)
(c:list (Z*Z))
(m_nz:s - Associational.eval c <> 0).
Definition mulmod (n:nat) (a b:list Z) : list Z
:= let a_a := to_associational n a in
let b_a := to_associational n b in
let ab_a := Associational.mul a_a b_a in
let abm_a := Associational.reduce s c ab_a in
from_associational n abm_a.
Lemma eval_mulmod n (f g:list Z)
(Hf : length f = n) (Hg : length g = n) :
eval n (mulmod n f g) mod (s - Associational.eval c)
= (eval n f * eval n g) mod (s - Associational.eval c).
Proof. cbv [mulmod]; push; trivial.
destruct f, g; simpl in *; [ right; subst n | left; try omega.. ].
clear; cbv -[Associational.reduce].
induction c as [|?? IHc]; simpl; trivial. Qed.
End mulmod.
Hint Rewrite @eval_mulmod : push_eval.
Definition add (n:nat) (a b:list Z) : list Z
:= let a_a := to_associational n a in
let b_a := to_associational n b in
from_associational n (a_a ++ b_a).
Lemma eval_add n (f g:list Z)
(Hf : length f = n) (Hg : length g = n) :
eval n (add n f g) = (eval n f + eval n g).
Proof. cbv [add]; push; trivial. destruct n; auto. Qed.
Hint Rewrite @eval_add : push_eval.
Lemma length_add n f g
(Hf : length f = n) (Hg : length g = n) :
length (add n f g) = n.
Proof. clear -Hf Hf; cbv [add]; distr_length. Qed.
Hint Rewrite @length_add : distr_length.
Section Carries.
Definition carry n m (index:nat) (p:list Z) : list Z :=
from_associational
m (@Associational.carry (weight index)
(weight (S index) / weight index)
(to_associational n p)).
Lemma length_carry n m index p : length (carry n m index p) = m.
Proof. cbv [carry]; distr_length. Qed.
Lemma eval_carry n m i p: (n <> 0%nat) -> (m <> 0%nat) ->
weight (S i) / weight i <> 0 ->
eval m (carry n m i p) = eval n p.
Proof.
cbv [carry]; intros; push; [|tauto].
rewrite @Associational.eval_carry by eauto.
apply eval_to_associational.
Qed. Hint Rewrite @eval_carry : push_eval.
Definition carry_reduce n (s:Z) (c:list (Z * Z))
(index:nat) (p : list Z) :=
from_associational
n (Associational.reduce
s c (to_associational (S n) (@carry n (S n) index p))).
Lemma eval_carry_reduce n s c index p :
(s <> 0) -> (s - Associational.eval c <> 0) -> (n <> 0%nat) ->
(weight (S index) / weight index <> 0) ->
eval n (carry_reduce n s c index p) mod (s - Associational.eval c)
= eval n p mod (s - Associational.eval c).
Proof. cbv [carry_reduce]; intros; push; auto. Qed.
Hint Rewrite @eval_carry_reduce : push_eval.
Lemma length_carry_reduce n s c index p
: length p = n -> length (carry_reduce n s c index p) = n.
Proof. cbv [carry_reduce]; distr_length. Qed.
Hint Rewrite @length_carry_reduce : distr_length.
(* N.B. It is important to reverse [idxs] here, because fold_right is
written such that the first terms in the list are actually used
last in the computation. For example, running:
`Eval cbv - [Z.add] in (fun a b c d => fold_right Z.add d [a;b;c]).`
will produce [fun a b c d => (a + (b + (c + d)))].*)
Definition chained_carries n s c p (idxs : list nat) :=
fold_right (fun a b => carry_reduce n s c a b) p (rev idxs).
Lemma eval_chained_carries n s c p idxs :
(s <> 0) -> (s - Associational.eval c <> 0) -> (n <> 0%nat) ->
(forall i, In i idxs -> weight (S i) / weight i <> 0) ->
eval n (chained_carries n s c p idxs) mod (s - Associational.eval c)
= eval n p mod (s - Associational.eval c).
Proof using Type*.
cbv [chained_carries]; intros; push.
apply fold_right_invariant; [|intro; rewrite <-in_rev];
destruct n; intros; push; auto.
Qed. Hint Rewrite @eval_chained_carries : push_eval.
Lemma length_chained_carries n s c p idxs
: length p = n -> length (@chained_carries n s c p idxs) = n.
Proof.
intros; cbv [chained_carries]; induction (rev idxs) as [|x xs IHxs];
cbn [fold_right]; distr_length.
Qed. Hint Rewrite @length_chained_carries : distr_length.
(* carries without modular reduction; useful for converting between bases *)
Definition chained_carries_no_reduce n p (idxs : list nat) :=
fold_right (fun a b => carry n n a b) p (rev idxs).
Lemma eval_chained_carries_no_reduce n p idxs:
(forall i, In i idxs -> weight (S i) / weight i <> 0) ->
eval n (chained_carries_no_reduce n p idxs) = eval n p.
Proof.
cbv [chained_carries_no_reduce]; intros.
destruct n; [push;reflexivity|].
apply fold_right_invariant; [|intro; rewrite <-in_rev];
intros; push; auto.
Qed. Hint Rewrite @eval_chained_carries_no_reduce : push_eval.
(* Reverse of [eval]; translate from Z to basesystem by putting
everything in first digit and then carrying. *)
Definition encode n s c (x : Z) : list Z :=
chained_carries n s c (from_associational n [(1,x)]) (seq 0 n).
Lemma eval_encode n s c x :
(s <> 0) -> (s - Associational.eval c <> 0) -> (n <> 0%nat) ->
(forall i, In i (seq 0 n) -> weight (S i) / weight i <> 0) ->
eval n (encode n s c x) mod (s - Associational.eval c)
= x mod (s - Associational.eval c).
Proof using Type*. cbv [encode]; intros; push; auto; f_equal; omega. Qed.
Lemma length_encode n s c x
: length (encode n s c x) = n.
Proof. cbv [encode]; repeat distr_length. Qed.
End Carries.
Hint Rewrite @eval_encode : push_eval.
Hint Rewrite @length_encode : distr_length.
Section sub.
Context (n:nat)
(s:Z) (s_nz:s <> 0)
(c:list (Z * Z))
(m_nz:s - Associational.eval c <> 0)
(coef:Z).
Definition negate_snd (a:list Z) : list Z
:= let A := to_associational n a in
let negA := Associational.negate_snd A in
from_associational n negA.
Definition scmul (x:Z) (a:list Z) : list Z
:= let A := to_associational n a in
let R := Associational.mul A [(1, x)] in
from_associational n R.
Definition balance : list Z
:= scmul coef (encode n s c (s - Associational.eval c)).
Definition sub (a b:list Z) : list Z
:= let ca := add n balance a in
let _b := negate_snd b in
add n ca _b.
Lemma eval_sub a b
: (forall i, In i (seq 0 n) -> weight (S i) / weight i <> 0) ->
(List.length a = n) -> (List.length b = n) ->
eval n (sub a b) mod (s - Associational.eval c)
= (eval n a - eval n b) mod (s - Associational.eval c).
Proof.
destruct (zerop n); subst; try reflexivity.
intros; cbv [sub balance scmul negate_snd]; push; repeat distr_length;
eauto with omega.
push_Zmod; push; pull_Zmod; push_Zmod; pull_Zmod; distr_length; eauto.
Qed.
Hint Rewrite eval_sub : push_eval.
Lemma length_sub a b
: length a = n -> length b = n ->
length (sub a b) = n.
Proof. intros; cbv [sub balance scmul negate_snd]; repeat distr_length. Qed.
Hint Rewrite length_sub : distr_length.
Definition opp (a:list Z) : list Z
:= sub (zeros n) a.
Lemma eval_opp
(a:list Z)
: (length a = n) ->
(forall i, In i (seq 0 n) -> weight (S i) / weight i <> 0) ->
eval n (opp a) mod (s - Associational.eval c)
= (- eval n a) mod (s - Associational.eval c).
Proof. intros; cbv [opp]; push; distr_length; auto. Qed.
Lemma length_opp a
: length a = n -> length (opp a) = n.
Proof. cbv [opp]; intros; repeat distr_length. Qed.
End sub.
Hint Rewrite @eval_opp @eval_sub : push_eval.
Hint Rewrite @length_sub @length_opp : distr_length.
End Positional. End Positional.
Record weight_properties {weight : nat -> Z} :=
{
weight_0 : weight 0%nat = 1;
weight_positive : forall i, 0 < weight i;
weight_multiples : forall i, weight (S i) mod weight i = 0;
weight_divides : forall i : nat, 0 < weight (S i) / weight i;
}.
Hint Resolve weight_0 weight_positive weight_multiples weight_divides.
Section mod_ops.
Import Positional.
Local Coercion Z.of_nat : nat >-> Z.
Local Coercion QArith_base.inject_Z : Z >-> Q.
(* Design constraints:
- inputs must be [Z] (b/c reification does not support Q)
- internal structure must not match on the arguments (b/c reification does not support [positive]) *)
Context (limbwidth_num limbwidth_den : Z)
(limbwidth_good : 0 < limbwidth_den <= limbwidth_num)
(s : Z)
(c : list (Z*Z))
(n : nat)
(len_c : nat)
(idxs : list nat)
(len_idxs : nat)
(m_nz:s - Associational.eval c <> 0) (s_nz:s <> 0)
(Hn_nz : n <> 0%nat)
(Hc : length c = len_c)
(Hidxs : length idxs = len_idxs).
Definition weight (i : nat)
:= 2^(-(-(limbwidth_num * i) / limbwidth_den)).
Local Ltac Q_cbv :=
cbv [Qceiling inject_Z Qle Qfloor Qdiv Qnum Qden Qmult Qinv Qopp].
Local Lemma weight_ZQ_correct i
(limbwidth := (limbwidth_num / limbwidth_den)%Q)
: weight i = 2^Qceiling(limbwidth*i).
Proof.
clear -limbwidth_good.
cbv [limbwidth weight]; Q_cbv.
destruct limbwidth_num, limbwidth_den, i; try reflexivity;
repeat rewrite ?Pos.mul_1_l, ?Pos.mul_1_r, ?Z.mul_0_l, ?Zdiv_0_l, ?Zdiv_0_r, ?Z.mul_1_l, ?Z.mul_1_r, <- ?Z.opp_eq_mul_m1, ?Pos2Z.opp_pos;
try reflexivity; try lia.
Qed.
Local Ltac t_weight_with lem :=
clear -limbwidth_good;
intros; rewrite !weight_ZQ_correct;
apply lem;
try omega; Q_cbv; destruct limbwidth_den; cbn; try lia.
Definition wprops : @weight_properties weight.
Proof.
constructor.
{ cbv [weight Z.of_nat]; autorewrite with zsimplify_fast; reflexivity. }
{ intros; apply Z.gt_lt. t_weight_with (@pow_ceil_mul_nat_pos 2). }
{ t_weight_with (@pow_ceil_mul_nat_multiples 2). }
{ intros; apply Z.gt_lt. t_weight_with (@pow_ceil_mul_nat_divide 2). }
Defined.
Local Hint Immediate (weight_0 wprops).
Local Hint Immediate (weight_positive wprops).
Local Hint Immediate (weight_multiples wprops).
Local Hint Immediate (weight_divides wprops).
Local Hint Resolve Z.positive_is_nonzero Z.lt_gt.
Local Lemma weight_1_gt_1 : weight 1 > 1.
Proof.
clear -limbwidth_good.
cut (1 < weight 1); [ lia | ].
cbv [weight Z.of_nat]; autorewrite with zsimplify_fast.
apply Z.pow_gt_1; [ omega | ].
Z.div_mod_to_quot_rem; nia.
Qed.
Derive carry_mulmod
SuchThat (forall (f g : list Z)
(Hf : length f = n)
(Hg : length g = n),
(eval weight n (carry_mulmod f g)) mod (s - Associational.eval c)
= (eval weight n f * eval weight n g) mod (s - Associational.eval c))
As eval_carry_mulmod.
Proof.
intros.
rewrite <-eval_mulmod with (s:=s) (c:=c) by auto.
etransitivity;
[ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
by auto; reflexivity ].
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst carry_mulmod; reflexivity.
Qed.
Derive carrymod
SuchThat (forall (f : list Z)
(Hf : length f = n),
(eval weight n (carrymod f)) mod (s - Associational.eval c)
= (eval weight n f) mod (s - Associational.eval c))
As eval_carrymod.
Proof.
intros.
etransitivity;
[ | rewrite <- @eval_chained_carries with (s:=s) (c:=c) (idxs:=idxs)
by auto; reflexivity ].
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst carrymod; reflexivity.
Qed.
Derive addmod
SuchThat (forall (f g : list Z)
(Hf : length f = n)
(Hg : length g = n),
(eval weight n (addmod f g)) mod (s - Associational.eval c)
= (eval weight n f + eval weight n g) mod (s - Associational.eval c))
As eval_addmod.
Proof.
intros.
rewrite <-eval_add by auto.
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst addmod; reflexivity.
Qed.
Derive submod
SuchThat (forall (coef:Z)
(f g : list Z)
(Hf : length f = n)
(Hg : length g = n),
(eval weight n (submod coef f g)) mod (s - Associational.eval c)
= (eval weight n f - eval weight n g) mod (s - Associational.eval c))
As eval_submod.
Proof.
intros.
rewrite <-eval_sub with (coef:=coef) by auto.
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst submod; reflexivity.
Qed.
Derive oppmod
SuchThat (forall (coef:Z)
(f: list Z)
(Hf : length f = n),
(eval weight n (oppmod coef f)) mod (s - Associational.eval c)
= (- eval weight n f) mod (s - Associational.eval c))
As eval_oppmod.
Proof.
intros.
rewrite <-eval_opp with (coef:=coef) by auto.
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst oppmod; reflexivity.
Qed.
Derive encodemod
SuchThat (forall (f:Z),
(eval weight n (encodemod f)) mod (s - Associational.eval c)
= f mod (s - Associational.eval c))
As eval_encodemod.
Proof.
intros.
etransitivity.
2:rewrite <-@eval_encode with (weight:=weight) (n:=n) by auto; reflexivity.
eapply f_equal2; [|trivial]. eapply f_equal.
expand_lists ().
subst encodemod; reflexivity.
Qed.
End mod_ops.
Module Saturated.
Hint Resolve weight_positive weight_0 weight_multiples weight_divides.
Hint Resolve Z.positive_is_nonzero Z.lt_gt Nat2Z.is_nonneg.
Section Weight.
Context weight {wprops : @weight_properties weight}.
Lemma weight_multiples_full' j : forall i, weight (i+j) mod weight i = 0.
Proof.
induction j; intros;
repeat match goal with
| _ => rewrite Nat.add_succ_r
| _ => rewrite IHj
| |- context [weight (S ?x) mod weight _] =>
rewrite (Z.div_mod (weight (S x)) (weight x)), weight_multiples by auto
| _ => progress autorewrite with push_Zmod natsimplify zsimplify_fast
| _ => reflexivity
end.
Qed.
Lemma weight_multiples_full j i : (i <= j)%nat -> weight j mod weight i = 0.
Proof.
intros; replace j with (i + (j - i))%nat by omega.
apply weight_multiples_full'.
Qed.
Lemma weight_divides_full j i : (i <= j)%nat -> 0 < weight j / weight i.
Proof. auto using Z.gt_lt, Z.div_positive_gt_0, weight_multiples_full. Qed.
Lemma weight_div_mod j i : (i <= j)%nat -> weight j = weight i * (weight j / weight i).
Proof. intros. apply Z.div_exact; auto using weight_multiples_full. Qed.
End Weight.
Module Associational.
Section Associational.
Definition sat_multerm s (t t' : (Z * Z)) : list (Z * Z) :=
dlet_nd xy := Z.mul_split s (snd t) (snd t') in
[(fst t * fst t', fst xy); (fst t * fst t' * s, snd xy)].
Definition sat_mul s (p q : list (Z * Z)) : list (Z * Z) :=
flat_map (fun t => flat_map (sat_multerm s t) q) p.
Lemma eval_map_sat_multerm s a q (s_nonzero:s<>0):
Associational.eval (flat_map (sat_multerm s a) q) = fst a * snd a * Associational.eval q.
Proof.
cbv [sat_multerm Let_In]; induction q;
repeat match goal with
| _ => progress autorewrite with cancel_pair push_eval to_div_mod in *
| _ => progress simpl flat_map
| _ => rewrite IHq
| _ => rewrite Z.mod_eq by assumption
| _ => ring_simplify; omega
end.
Qed.
Hint Rewrite eval_map_sat_multerm using (omega || assumption) : push_eval.
Lemma eval_sat_mul s p q (s_nonzero:s<>0):
Associational.eval (sat_mul s p q) = Associational.eval p * Associational.eval q.
Proof.
cbv [sat_mul]; induction p; [reflexivity|].
repeat match goal with
| _ => progress (autorewrite with push_flat_map push_eval in * )
| _ => rewrite IHp
| _ => ring_simplify; omega
end.
Qed.
Hint Rewrite eval_sat_mul : push_eval.
End Associational.
End Associational.
Section DivMod.
Lemma mod_step a b c d: 0 < a -> 0 < b ->
c mod a + a * ((c / a + d) mod b) = (a * d + c) mod (a * b).
Proof.
intros; rewrite Z.rem_mul_r by omega. push_Zmod.
autorewrite with zsimplify pull_Zmod. repeat (f_equal; try ring).
Qed.
Lemma div_step a b c d : 0 < a -> 0 < b ->
(c / a + d) / b = (a * d + c) / (a * b).
Proof. intros; Z.div_mod_to_quot_rem; nia. Qed.
Lemma add_mod_div_multiple a b n m:
n > 0 ->
0 <= m / n ->
m mod n = 0 ->
(a / n + b) mod (m / n) = (a + n * b) mod m / n.
Proof.
intros. rewrite <-!Z.div_add' by auto using Z.positive_is_nonzero.
rewrite Z.mod_pull_div, Z.mul_div_eq' by auto using Z.gt_lt.
repeat (f_equal; try omega).
Qed.
Lemma add_mod_l_multiple a b n m:
0 < n / m -> m <> 0 -> n mod m = 0 ->
(a mod n + b) mod m = (a + b) mod m.
Proof.
intros.
rewrite (proj2 (Z.div_exact n m ltac:(auto))) by auto.
rewrite Z.rem_mul_r by auto.
push_Zmod. autorewrite with zsimplify.
pull_Zmod. reflexivity.
Qed.
Definition is_div_mod {T} (evalf : T -> Z) dm y n :=
evalf (fst dm) = y mod n /\ snd dm = y / n.
Lemma is_div_mod_step {T} evalf1 evalf2 dm1 dm2 y1 y2 n1 n2 x :
n1 > 0 ->
0 < n2 / n1 ->
n2 mod n1 = 0 ->
evalf2 (fst dm2) = evalf1 (fst dm1) + n1 * ((snd dm1 + x) mod (n2 / n1)) ->
snd dm2 = (snd dm1 + x) / (n2 / n1) ->
y2 = y1 + n1 * x ->
@is_div_mod T evalf1 dm1 y1 n1 ->
@is_div_mod T evalf2 dm2 y2 n2.
Proof.
intros; subst y2; cbv [is_div_mod] in *.
repeat match goal with
| H: _ /\ _ |- _ => destruct H
| H: ?LHS = _ |- _ => match LHS with context [dm2] => rewrite H end
| H: ?LHS = _ |- _ => match LHS with context [dm1] => rewrite H end
| _ => rewrite mod_step by omega
| _ => rewrite div_step by omega
| _ => rewrite Z.mul_div_eq_full by omega
end.
split; f_equal; omega.
Qed.
Lemma is_div_mod_result_equal {T} evalf dm y1 y2 n :
y1 = y2 ->
@is_div_mod T evalf dm y1 n ->
@is_div_mod T evalf dm y2 n.
Proof. congruence. Qed.
End DivMod.
End Saturated.
Module Columns.
Import Saturated.
Section Columns.
Context weight {wprops : @weight_properties weight}.
Definition eval n (x : list (list Z)) : Z := Positional.eval weight n (map sum x).
Lemma eval_nil n : eval n [] = 0.
Proof. cbv [eval]; simpl. apply Positional.eval_nil. Qed.
Hint Rewrite eval_nil : push_eval.
Lemma eval_snoc n x y : n = length x -> eval (S n) (x ++ [y]) = eval n x + weight n * sum y.
Proof.
cbv [eval]; intros; subst. rewrite map_app. simpl map.
apply Positional.eval_snoc; distr_length.
Qed. Hint Rewrite eval_snoc using (solve [distr_length]) : push_eval.
Hint Rewrite <- Z.div_add' using omega : pull_Zdiv.
Ltac cases :=
match goal with
| |- _ /\ _ => split
| H: _ /\ _ |- _ => destruct H
| H: _ \/ _ |- _ => destruct H
| _ => progress break_match; try discriminate
end.
Section Flatten.
Section flatten_column.
Context (fw : Z). (* maximum size of the result *)
(* Outputs (sum, carry) *)
Definition flatten_column (digit: list Z) : (Z * Z) :=
list_rect (fun _ => (Z * Z)%type) (0,0)
(fun xx tl flatten_column_tl =>
list_rect
(fun _ => (Z * Z)%type) (xx mod fw, xx / fw)
(fun yy tl' _ =>
list_rect
(fun _ => (Z * Z)%type) (dlet_nd x := xx in dlet_nd y := yy in Z.add_get_carry_full fw x y)
(fun _ _ _ =>
dlet_nd x := xx in
dlet_nd rec := flatten_column_tl in (* recursively get the sum and carry *)
dlet_nd sum_carry := Z.add_get_carry_full fw x (fst rec) in (* add the new value to the sum *)
dlet_nd carry' := snd sum_carry + snd rec in (* add the two carries together *)
(fst sum_carry, carry'))
tl')
tl)
digit.
End flatten_column.
Definition flatten_step (digit:list Z) (acc_carry:list Z * Z) : list Z * Z :=
dlet sum_carry := flatten_column (weight (S (length (fst acc_carry))) / weight (length (fst acc_carry))) (snd acc_carry::digit) in
(fst acc_carry ++ fst sum_carry :: nil, snd sum_carry).
Definition flatten (xs : list (list Z)) : list Z * Z :=
fold_right (fun a b => flatten_step a b) (nil,0) (rev xs).
Ltac push_fast :=
repeat match goal with
| _ => progress cbv [Let_In]
| |- context [list_rect _ _ _ ?ls] => rewrite single_list_rect_to_match; destruct ls
| _ => progress (unfold flatten_step in *; fold flatten_step in * )
| _ => rewrite Nat.add_1_r
| _ => rewrite Z.mul_div_eq_full by (auto; omega)
| _ => rewrite weight_multiples
| _ => reflexivity
| _ => solve [repeat (f_equal; try ring)]
| _ => congruence
| _ => progress cases
end.
Ltac push :=
repeat match goal with
| _ => progress push_fast
| _ => progress autorewrite with cancel_pair to_div_mod
| _ => progress autorewrite with push_sum push_fold_right push_nth_default in *
| _ => progress autorewrite with pull_Zmod pull_Zdiv zsimplify_fast
| _ => progress autorewrite with list distr_length push_eval
end.
Lemma flatten_column_mod fw (xs : list Z) :
fst (flatten_column fw xs) = sum xs mod fw.
Proof.
induction xs; simpl flatten_column; cbv [Let_In];
repeat match goal with
| _ => rewrite IHxs
| _ => progress push
end.
Qed. Hint Rewrite flatten_column_mod : to_div_mod.
Lemma flatten_column_div fw (xs : list Z) (fw_nz : fw <> 0) :
snd (flatten_column fw xs) = sum xs / fw.
Proof.
induction xs; simpl flatten_column; cbv [Let_In];
repeat match goal with
| _ => rewrite IHxs
| _ => rewrite Z.mul_div_eq_full by omega
| _ => progress push
end.
Qed. Hint Rewrite flatten_column_div using auto with zarith : to_div_mod.
Hint Rewrite Positional.eval_nil : push_eval.
Hint Resolve Z.gt_lt.
Lemma length_flatten_step digit state :
length (fst (flatten_step digit state)) = S (length (fst state)).
Proof. cbv [flatten_step]; push. Qed.
Hint Rewrite length_flatten_step : distr_length.
Lemma length_flatten inp : length (fst (flatten inp)) = length inp.
Proof. cbv [flatten]. induction inp using rev_ind; push. Qed.
Hint Rewrite length_flatten : distr_length.
Lemma flatten_div_mod n inp :
length inp = n ->
(Positional.eval weight n (fst (flatten inp))
= (eval n inp) mod (weight n))
/\ (snd (flatten inp) = eval n inp / weight n).
Proof.
(* to make the invariant take the right form, we make everything depend on output length, not input length *)
intro. subst n. rewrite <-(length_flatten inp). cbv [flatten].
induction inp using rev_ind; intros; [push|].
repeat match goal with
| _ => rewrite Nat.add_1_r
| _ => progress (fold (flatten inp) in * )
| _ => erewrite Positional.eval_snoc by (distr_length; reflexivity)
| H: _ = _ mod (weight _) |- _ => rewrite H
| H: _ = _ / (weight _) |- _ => rewrite H
| _ => progress rewrite ?mod_step, ?div_step by auto
| _ => progress autorewrite with cancel_pair to_div_mod push_sum list push_fold_right push_eval
| _ => progress (distr_length; push_fast)
end.
Qed.
Lemma flatten_mod {n} inp :
length inp = n ->
(Positional.eval weight n (fst (flatten inp)) = (eval n inp) mod (weight n)).
Proof. apply flatten_div_mod. Qed.
Hint Rewrite @flatten_mod : push_eval.
Lemma flatten_div {n} inp :
length inp = n -> snd (flatten inp) = eval n inp / weight n.
Proof. apply flatten_div_mod. Qed.
Hint Rewrite @flatten_div : push_eval.
Lemma flatten_snoc x inp : flatten (inp ++ [x]) = flatten_step x (flatten inp).
Proof. cbv [flatten]. rewrite rev_unit. reflexivity. Qed.
Lemma flatten_partitions inp:
forall n i, length inp = n -> (i < n)%nat ->
nth_default 0 (fst (flatten inp)) i = ((eval n inp) mod (weight (S i))) / weight i.
Proof.
induction inp using rev_ind; intros; destruct n; distr_length.
rewrite flatten_snoc.
push; distr_length;
[rewrite IHinp with (n:=n) by omega; rewrite weight_div_mod with (j:=n) (i:=S i) by (eauto; omega); push_Zmod; push |].
repeat match goal with
| _ => progress replace (length inp) with n by omega
| _ => progress replace i with n by omega
| _ => progress push
| _ => erewrite flatten_div by eauto
| _ => rewrite <-Z.div_add' by auto
| _ => rewrite Z.mul_div_eq' by auto
| _ => rewrite Z.mod_pull_div by auto using Z.lt_le_incl
| _ => progress autorewrite with push_nth_default natsimplify
end.
Qed.
End Flatten.
Section FromAssociational.
(* nils *)
Definition nils n : list (list Z) := List.repeat nil n.
Lemma length_nils n : length (nils n) = n. Proof. cbv [nils]. distr_length. Qed.
Hint Rewrite length_nils : distr_length.
Lemma eval_nils n : eval n (nils n) = 0.
Proof.
erewrite <-Positional.eval_zeros by eauto.
cbv [eval nils]; rewrite List.map_repeat; reflexivity.
Qed. Hint Rewrite eval_nils : push_eval.
(* cons_to_nth *)
Definition cons_to_nth i x (xs : list (list Z)) : list (list Z) :=
ListUtil.update_nth i (fun y => cons x y) xs.
Lemma length_cons_to_nth i x xs : length (cons_to_nth i x xs) = length xs.
Proof. cbv [cons_to_nth]. distr_length. Qed.
Hint Rewrite length_cons_to_nth : distr_length.
Lemma cons_to_nth_add_to_nth xs : forall i x,
map sum (cons_to_nth i x xs) = Positional.add_to_nth i x (map sum xs).
Proof.
cbv [cons_to_nth]; induction xs as [|? ? IHxs];
intros i x; destruct i; simpl; rewrite ?IHxs; reflexivity.
Qed.
Lemma eval_cons_to_nth n i x xs : (i < length xs)%nat -> length xs = n ->
eval n (cons_to_nth i x xs) = weight i * x + eval n xs.
Proof using Type.
cbv [eval]; intros. rewrite cons_to_nth_add_to_nth.
apply Positional.eval_add_to_nth; distr_length.
Qed. Hint Rewrite eval_cons_to_nth using (solve [distr_length]) : push_eval.
Hint Rewrite Positional.eval_zeros : push_eval.
Hint Rewrite Positional.length_from_associational : distr_length.
Hint Rewrite Positional.eval_add_to_nth using (solve [distr_length]): push_eval.
(* from_associational *)
Definition from_associational n (p:list (Z*Z)) : list (list Z) :=
List.fold_right (fun t ls =>
let p := Positional.place weight t (pred n) in
cons_to_nth (fst p) (snd p) ls ) (nils n) p.
Lemma length_from_associational n p : length (from_associational n p) = n.
Proof. cbv [from_associational]. apply fold_right_invariant; intros; distr_length. Qed.
Hint Rewrite length_from_associational: distr_length.
Lemma eval_from_associational n p (n_nonzero:n<>0%nat\/p=nil):
eval n (from_associational n p) = Associational.eval p.
Proof.
erewrite <-Positional.eval_from_associational by eauto.
induction p; [ autorewrite with push_eval; solve [auto] |].
cbv [from_associational Positional.from_associational]; autorewrite with push_fold_right.
fold (from_associational n p); fold (Positional.from_associational weight n p).
cbv [Let_In].
match goal with |- context [Positional.place _ ?x ?n] =>
pose proof (Positional.place_in_range weight x n) end.
repeat match goal with
| _ => rewrite Nat.succ_pred in * by auto
| _ => rewrite IHp by auto
| _ => progress autorewrite with push_eval
| _ => progress cases
| _ => congruence
end.
Qed.
Lemma from_associational_step n t p :
from_associational n (t :: p) =
cons_to_nth (fst (Positional.place weight t (Nat.pred n)))
(snd (Positional.place weight t (Nat.pred n)))
(from_associational n p).
Proof. reflexivity. Qed.
End FromAssociational.
Section mul.
Definition mul s n m (p q : list Z) : list Z :=
let p_a := Positional.to_associational weight n p in
let q_a := Positional.to_associational weight n q in
let pq_a := Associational.sat_mul s p_a q_a in
fst (flatten (from_associational m pq_a)).
End mul.
End Columns.
End Columns.
Module Rows.
Import Saturated.
Section Rows.
Context weight {wprops : @weight_properties weight}.
Local Notation rows := (list (list Z)) (only parsing).
Local Notation cols := (list (list Z)) (only parsing).
Hint Rewrite Positional.eval_nil Positional.eval0 @Positional.eval_snoc
Positional.eval_to_associational
Columns.eval_nil Columns.eval_snoc using (auto; solve [distr_length]) : push_eval.
Hint Resolve in_eq in_cons.