Debian Codegraph @

src/Util/Listable.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import Arith.
3 From Coq Require Import Bool.
4 From Coq Require Import List.
5 Require Import Crypto.Util.ListUtil.
6 Require Import Crypto.Util.Decidable.
7 Require Import Crypto.Util.Bool.Reflect.
8 Import ListNotations.
9
10 (* TODO: move to using [N] for performance instead of [nat] *)
11 Class Listable T := { list_all : list T ; find_index : T -> nat }.
12
13 Arguments find_index {T} {_} _.
14 Arguments list_all T {_}.
15
16 Class FinitelyListable T {l : Listable T}
17 := { find_index_ok : forall t, nth_error (list_all T) (find_index t) = Some t
18 ; enumerate_smallb : fold_right andb true (List.map (fun '(n, t) => Nat.eqb n (find_index t)) (enumerate (list_all T))) = true }.
19
20 Ltac prove_FinitelyListable :=
21 split;
22 [ let t := fresh in
23 intro t; destruct t;
24 vm_compute; try reflexivity
25 | vm_compute; reflexivity ].
26
27 Ltac push_S_refine_num ev :=
28 lazymatch ev with
29 | S ?ev
30 => refine (S _); push_S_refine_num ev
31 | ?ev => let __ := open_constr:(eq_refl : ev = S _) in
32 refine O
33 end.
34
35 Ltac force_num ev :=
36 lazymatch ev with
37 | S ?ev => force_num ev
38 | ?ev => unify ev O
39 end.
40
41 Ltac partially_prove_Listable :=
42 let fin := open_constr:(_) in
43 split;
44 [
45 | repeat (let t := fresh in intro t; case t; clear t);
46 push_S_refine_num fin ];
47 force_num fin.
48
49 Ltac finish_prove_FinitelyListable :=
50 split;
51 [ cbv [list_all find_index];
52 repeat (let t := fresh in intro t; case t; clear t);
53 unshelve
54 (repeat match goal with
55 | [ |- nth_error (_ :: _) O = _ ] => cbn [nth_error]
56 | [ |- nth_error (_ :: ?xs) (S ?n) = ?rhs ] => change (nth_error xs n = rhs)
57 | [ |- nth_error ?ev _ = ?rhs ]
58 => is_evar ev;
59 let __ := open_constr:(eq_refl : ev = cons _ _) in
60 idtac
61 | [ |- Some _ = Some _ ] => reflexivity
62 end);
63 try exact nil
64 | vm_compute; reflexivity ].
65
66 Ltac prove_ListableDerive :=
67 lazymatch goal with
68 | [ |- @FinitelyListable ?T ?H ]
69 => revert H; instantiate (1:=ltac:(partially_prove_Listable)); intro H; (* argh, instantiate .. in (value of H) doesn't work in old versions of Coq, and current Coq doesn't know about (Value of H) *)
70 cbv [H];
71 finish_prove_FinitelyListable
72 end.
73
74 Ltac prove_ListablePackage T :=
75 refine (_ : { l : Listable T | FinitelyListable T });
76 unshelve econstructor;
77 [ partially_prove_Listable
78 | finish_prove_FinitelyListable ].
79
80 Ltac get_ListablePackage T :=
81 constr:(ltac:(prove_ListablePackage T)).
82
83 Section with_listable.
84 Context {T}
85 {Listable_T : Listable T}
86 {FinitelyListable_T : FinitelyListable T}.
87
88 Lemma find_index_iff x n : find_index x = n <-> List.nth_error (list_all T) n = Some x.
89 Proof.
90 split; [ intro; subst; apply find_index_ok | ].
91 generalize enumerate_smallb.
92 cbv [enumerate].
93 set (offset := O).
94 change n with (offset + n)%nat at 2.
95 clearbody offset.
96 rename x into v.
97 intros Hfold Hnth; revert offset n Hfold Hnth.
98 induction (list_all T) as [|x xs IHxs]; intros.
99 { destruct n; cbn in *; congruence. }
100 { cbn in *.
101 rewrite Bool.andb_true_iff, Nat.eqb_eq in Hfold.
102 destruct Hfold as [Hfold1 Hfold2].
103 specialize (IHxs (S offset) (pred n) Hfold2).
104 subst offset; destruct n as [|n]; cbn in *.
105 { inversion Hnth; subst; lia. }
106 { specialize (IHxs Hnth); lia. } }
107 Qed.
108
109 Lemma enumerate_unique n m x y
110 (Hn : List.nth_error (list_all T) n = Some x)
111 (Hm : List.nth_error (list_all T) m = Some y)
112 (Hxy : x = y)
113 : n = m.
114 Proof. rewrite <- !find_index_iff in *; subst; reflexivity. Qed.
115
116 Lemma find_index_inj x y : find_index x = find_index y -> x = y.
117 Proof. rewrite find_index_iff, find_index_ok; congruence. Qed.
118
119 Definition eqb_of_listable : T -> T -> bool := fun x y => Nat.eqb (find_index x) (find_index y).
120 Lemma eqb_of_listable_refl x : eqb_of_listable x x = true.
121 Proof. cbv [eqb_of_listable]. apply Nat.eqb_refl. Qed.
122 Lemma eqb_of_listable_lb x y : x = y -> eqb_of_listable x y = true.
123 Proof. intros; subst; apply eqb_of_listable_refl. Qed.
124 Lemma eqb_of_listable_bl x y : eqb_of_listable x y = true -> x = y.
125 Proof. cbv [eqb_of_listable]; rewrite Nat.eqb_eq. apply find_index_inj. Qed.
126 Lemma eqb_of_listable_eq x y : eqb_of_listable x y = true <-> x = y.
127 Proof. split; auto using eqb_of_listable_lb, eqb_of_listable_bl. Qed.
128 Global Instance eq_dec_of_listable : DecidableRel (@eq T)
129 := dec_rel_of_bool_dec_rel eqb_of_listable eqb_of_listable_bl eqb_of_listable_lb.
130 Global Instance reflect_eqb_of_listable : reflect_rel (@eq T) eqb_of_listable
131 := reflect_of_beq eqb_of_listable_bl eqb_of_listable_lb.
132 End with_listable.