Debian Codegraph @

apps/tc/tests-stdlib/stdppInj.v

raw
1 (* Test inspired from https://gitlab.mpi-sws.org/iris/stdpp/-/blob/8c98553ad0ca2029b30cf18b58e321ec3a79172b/stdpp/base.v *)
2
3 From Corelib Require Export Morphisms RelationClasses ListDef Setoid.
4 From Stdlib Require Export List Peano Utf8 Permutation.
5 From Stdlib Require Export Program.Basics Program.Syntax.
6
7 Export ListNotations.
8
9
10 From elpi.apps Require Import tc.
11 Elpi TC Solver Override TC.Solver All.
12 Elpi TC.AddAllClasses.
13 Elpi TC.AddAllInstances.
14 Notation length := Datatypes.length.
15 Global Generalizable All Variables.
16 Global Unset Transparent Obligations.
17
18 Definition tc_opaque {A} (x : A) : A := x.
19 (* Typeclasses Opaque tc_opaque. *)
20
21 Global Arguments tc_opaque {_} _ /.
22 Declare Scope stdpp_scope.
23 Delimit Scope stdpp_scope with stdpp.
24 Global Open Scope stdpp_scope.
25 Notation "(=)" := eq (only parsing) : stdpp_scope.
26 Notation "( x =.)" := (eq x) (only parsing) : stdpp_scope.
27 Notation "(.= x )" := (λ y, eq y x) (only parsing) : stdpp_scope.
28 Notation "(≠)" := (λ x y, x ≠ y) (only parsing) : stdpp_scope.
29 Notation "( x ≠.)" := (λ y, x ≠ y) (only parsing) : stdpp_scope.
30 Notation "(.≠ x )" := (λ y, y ≠ x) (only parsing) : stdpp_scope.
31 Infix "=@{ A }" := (@eq A)
32 (at level 70, only parsing, no associativity) : stdpp_scope.
33 Notation "(=@{ A } )" := (@eq A) (only parsing) : stdpp_scope.
34 Notation "(≠@{ A } )" := (λ X Y, ¬X =@{A} Y) (only parsing) : stdpp_scope.
35 Notation "X ≠@{ A } Y":= (¬X =@{ A } Y)
36 (at level 70, only parsing, no associativity) : stdpp_scope.
37
38 Global Hint Extern 0 (_ = _) => reflexivity : core.
39 Global Hint Extern 100 (_ ≠ _) => discriminate : core.
40
41 Global Instance: ∀ A, PreOrder (=@{A}).
42 Proof. split; repeat intro; congruence. Qed.
43 Class Equiv A := equiv: relation A.
44 Global Instance equiv_rewrite_relation `{Equiv A} :
45 RewriteRelation (@equiv A _) | 150 := {}.
46
47 Infix "≡" := equiv (at level 70, no associativity) : stdpp_scope.
48 Infix "≡@{ A }" := (@equiv A _)
49 (at level 70, only parsing, no associativity) : stdpp_scope.
50
51 Notation "(≡)" := equiv (only parsing) : stdpp_scope.
52 Notation "( X ≡.)" := (equiv X) (only parsing) : stdpp_scope.
53 Notation "(.≡ X )" := (λ Y, Y ≡ X) (only parsing) : stdpp_scope.
54 Notation "(≢)" := (λ X Y, ¬X ≡ Y) (only parsing) : stdpp_scope.
55 Notation "X ≢ Y":= (¬X ≡ Y) (at level 70, no associativity) : stdpp_scope.
56 Notation "( X ≢.)" := (λ Y, X ≢ Y) (only parsing) : stdpp_scope.
57 Notation "(.≢ X )" := (λ Y, Y ≢ X) (only parsing) : stdpp_scope.
58
59 Notation "(≡@{ A } )" := (@equiv A _) (only parsing) : stdpp_scope.
60 Notation "(≢@{ A } )" := (λ X Y, ¬X ≡@{A} Y) (only parsing) : stdpp_scope.
61 Notation "X ≢@{ A } Y":= (¬X ≡@{ A } Y)
62 (at level 70, only parsing, no associativity) : stdpp_scope.
63 Class LeibnizEquiv A `{Equiv A} :=
64 leibniz_equiv (x y : A) : x ≡ y → x = y.
65 Global Hint Mode LeibnizEquiv ! - : typeclass_instances.
66
67 Global Instance: Params (@equiv) 2 := {}.
68 Global Instance equiv_default_relation `{Equiv A} :
69 DefaultRelation (≡@{A}) | 3 := {}.
70 Global Hint Extern 0 (_ ≡ _) => reflexivity : core.
71 Global Hint Extern 0 (_ ≡ _) => symmetry; assumption : core.
72
73
74 Class Inj {A B} (R : relation A) (S : relation B) (f : A → B) : Prop :=
75 inj x y : S (f x) (f y) → R x y.
76
77 Class Inj2 {A B C} (R1 : relation A) (R2 : relation B)
78 (S : relation C) (f : A → B → C) : Prop :=
79 inj2 x1 x2 y1 y2 : S (f x1 x2) (f y1 y2) → R1 x1 y1 ∧ R2 x2 y2.
80
81 Global Arguments irreflexivity {_} _ {_} _ _ : assert.
82 Global Arguments inj {_ _ _ _} _ {_} _ _ _ : assert.
83 Global Arguments inj2 {_ _ _ _ _ _} _ {_} _ _ _ _ _: assert.
84
85 Global Instance inj2_inj_1 `{Inj2 A B C R1 R2 R3 f} y : Inj R1 R3 (λ x, f x y) | 100.
86 Proof. repeat intro; edestruct (inj2 f); eauto. Qed.
87 Global Instance inj2_inj_2 `{Inj2 A B C R1 R2 R3 f} x : Inj R2 R3 (f x) | 100.
88 Proof. repeat intro; edestruct (inj2 f); eauto. Qed.
89
90 Notation "(∧)" := and (only parsing) : stdpp_scope.
91 Notation "( A ∧.)" := (and A) (only parsing) : stdpp_scope.
92 Notation "(.∧ B )" := (λ A, A ∧ B) (only parsing) : stdpp_scope.
93
94 Notation "(∨)" := or (only parsing) : stdpp_scope.
95 Notation "( A ∨.)" := (or A) (only parsing) : stdpp_scope.
96 Notation "(.∨ B )" := (λ A, A ∨ B) (only parsing) : stdpp_scope.
97
98 Notation "(↔)" := iff (only parsing) : stdpp_scope.
99 Notation "( A ↔.)" := (iff A) (only parsing) : stdpp_scope.
100 Notation "(.↔ B )" := (λ A, A ↔ B) (only parsing) : stdpp_scope.
101
102 Global Hint Extern 0 (_ ↔ _) => reflexivity : core.
103 Global Hint Extern 0 (_ ↔ _) => symmetry; assumption : core.
104
105 Notation "(→)" := (λ A B, A → B) (only parsing) : stdpp_scope.
106 Notation "( A →.)" := (λ B, A → B) (only parsing) : stdpp_scope.
107 Notation "(.→ B )" := (λ A, A → B) (only parsing) : stdpp_scope.
108
109 Notation "t $ r" := (t r)
110 (at level 65, right associativity, only parsing) : stdpp_scope.
111 Notation "($)" := (λ f x, f x) (only parsing) : stdpp_scope.
112 Notation "(.$ x )" := (λ f, f x) (only parsing) : stdpp_scope.
113
114 Infix "∘" := compose : stdpp_scope.
115 Notation "(∘)" := compose (only parsing) : stdpp_scope.
116 Notation "( f ∘.)" := (compose f) (only parsing) : stdpp_scope.
117 Notation "(.∘ f )" := (λ g, compose g f) (only parsing) : stdpp_scope.
118 (** Ensure that [simpl] unfolds [id], [compose], and [flip] when fully
119 applied. *)
120 Global Arguments id _ _ / : assert.
121 Global Arguments compose _ _ _ _ _ _ / : assert.
122 Global Arguments flip _ _ _ _ _ _ / : assert.
123 Global Arguments const _ _ _ _ / : assert.
124
125 Definition fun_map {A A' B B'} (f: A' → A) (g: B → B') (h : A → B) : A' → B' :=
126 g ∘ h ∘ f.
127
128 Global Instance id_inj {A} : Inj (=) (=) (@id A).
129 Proof. intros ??; auto. Qed.
130 Global Instance compose_inj {A B C} R1 R2 R3 (f : A → B) (g : B → C) :
131 Inj R1 R2 f → Inj R2 R3 g → Inj R1 R3 (g ∘ f).
132 Proof. red; intuition. Qed.
133
134 (** ** Products *)
135 Notation "( x ,.)" := (pair x) (only parsing) : stdpp_scope.
136 Notation "(., y )" := (λ x, (x,y)) (only parsing) : stdpp_scope.
137
138 Notation "p .1" := (fst p) (at level 1, left associativity, format "p .1").
139 Notation "p .2" := (snd p) (at level 1, left associativity, format "p .2").
140
141 Definition prod_map {A A' B B'} (f: A → A') (g: B → B') (p : A * B) : A' * B' :=
142 (f (p.1), g (p.2)).
143 Global Arguments prod_map {_ _ _ _} _ _ !_ / : assert.
144
145 Global Instance pair_inj {A B} : Inj2 (=) (=) (=) (@pair A B).
146 Proof. injection 1; auto. Qed.
147 Global Instance prod_map_inj {A A' B B'} (f : A → A') (g : B → B') :
148 Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (prod_map f g).
149 Proof.
150 intros ?? [??] [??] ?; simpl in *; f_equal;
151 [apply (inj f)|apply (inj g)]; congruence.
152 Qed.
153
154 Definition prod_relation {A B} (R1 : relation A) (R2 : relation B) :
155 relation (A * B) := λ x y, R1 (x.1) (y.1) ∧ R2 (x.2) (y.2).
156
157 Section prod_relation.
158 Context `{RA : relation A, RB : relation B}.
159 Global Instance pair_inj' : Inj2 RA RB (prod_relation RA RB) pair.
160 Proof. inversion_clear 1; eauto. Qed.
161 End prod_relation.
162
163 Global Instance prod_equiv `{Equiv A,Equiv B} : Equiv (A * B) :=
164 prod_relation (≡) (≡).
165
166 Section prod_setoid.
167 Context `{Equiv A, Equiv B}.
168
169 Elpi Accumulate TC.Solver lp:{{
170 shorten tc-elpi_apps_tc_tests_stdlib.stdppInj.{tc-Inj2}.
171 % shorten tc-stdppInj.{tc-Inj2}.
172 tc-Inj2 A B C RA RB RC F S :-
173 RC = app [global {coq.locate "equiv"} | _],
174 Res = {{prod_relation _ _}},
175 coq.unify-eq RC Res ok,
176 tc-Inj2 A B C RA RB Res F S.
177 }}.
178
179
180 Global Instance pair_equiv_inj : Inj2 (≡) (≡) (≡@{A*B}) pair := _.
181 End prod_setoid.
182
183 (* Typeclasses Opaque prod_equiv. *)
184
185 (** ** Sums *)
186 Definition sum_map {A A' B B'} (f: A → A') (g: B → B') (xy : A + B) : A' + B' :=
187 match xy with inl x => inl (f x) | inr y => inr (g y) end.
188 Global Arguments sum_map {_ _ _ _} _ _ !_ / : assert.
189
190 Global Instance inl_inj {A B} : Inj (=) (=) (@inl A B).
191 Proof. injection 1; auto. Qed.
192 Global Instance inr_inj {A B} : Inj (=) (=) (@inr A B).
193 Proof. injection 1; auto. Qed.
194
195 Global Instance sum_map_inj {A A' B B'} (f : A → A') (g : B → B') :
196 Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (sum_map f g).
197 Proof. intros ?? [?|?] [?|?] [=]; f_equal; apply (inj _); auto. Qed.
198
199 Inductive sum_relation {A B}
200 (RA : relation A) (RB : relation B) : relation (A + B) :=
201 | inl_related x1 x2 : RA x1 x2 → sum_relation RA RB (inl x1) (inl x2)
202 | inr_related y1 y2 : RB y1 y2 → sum_relation RA RB (inr y1) (inr y2).
203
204 Section sum_relation.
205 Context `{RA : relation A, RB : relation B}.
206 Global Instance inl_inj' : Inj RA (sum_relation RA RB) inl.
207 Proof. inversion_clear 1; auto. Qed.
208 Global Instance inr_inj' : Inj RB (sum_relation RA RB) inr.
209 Proof. inversion_clear 1; auto. Qed.
210 End sum_relation.
211
212 Global Instance sum_equiv `{Equiv A, Equiv B} : Equiv (A + B) := sum_relation (≡) (≡).
213
214 Elpi Accumulate TC.Solver lp:{{
215 shorten tc-elpi_apps_tc_tests_stdlib.stdppInj.{tc-Inj}.
216 % shorten tc-stdppInj.{tc-Inj}.
217 tc-Inj A B RA {{@equiv (sum _ _) (@sum_equiv _ _ _ _)}} S C :-
218 tc-Inj A B RA {{sum_relation _ _}} S C.
219 }}.
220
221
222 Global Instance inl_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inl A B) := _.
223 Global Instance inr_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inr A B) := _.
224
225 Notation "` x" := (proj1_sig x) (at level 10, format "` x") : stdpp_scope.
226
227 Elpi Accumulate TC.Solver lp:{{
228 shorten tc-elpi_apps_tc_tests_stdlib.stdppInj.{tc-Inj}.
229 tc-Inj A B RA RB F X :-
230 F = fun _ _ _,
231 G = {{@compose _ _ _ _ _}},
232 coq.unify-eq G F ok,
233 tc-Inj A B RA RB G X.
234 }}.
235
236
237 Definition f := Nat.add 0.
238 Global Instance h: Inj eq eq f.
239 unfold f. simpl. easy.
240 Qed.
241
242 Elpi Accumulate TC.Solver lp:{{
243 shorten tc-elpi_apps_tc_tests_stdlib.stdppInj.{tc-Inj}.
244 :after "lastHook"
245 tc-Inj A B RA RB F S :-
246 F = (fun _ _ _), !,
247 G = {{ compose _ _ }},
248 coq.unify-eq G F ok,
249 tc-Inj A B RA RB G S.
250 }}.
251 Set Warnings "+elpi".
252
253
254 Goal Inj eq eq (compose (@id nat) id).
255 apply _.
256 Qed.
257
258 Goal Inj eq eq (compose (compose (@id nat) id) id).
259 apply _.
260 Qed.
261
262 Goal Inj eq eq (fun (x:nat) => id (id x)).
263 apply _.
264 Qed.
265
266 Goal Inj eq eq (fun (x: nat) => (compose id id) (id x)).
267 apply (compose_inj eq eq); apply _.
268 Qed.