Debian Codegraph @

apps/tc/tests-stdlib/bigTest.v

raw
1 From elpi.apps Require Import tc.
2 Elpi TC Solver Override TC.Solver All.
3
4 (** This file collects type class interfaces, notations, and general theorems
5 that are used throughout the whole development. Most importantly it contains
6 abstract interfaces for ordered structures, sets, and various other data
7 structures. *)
8
9 (* We want to ensure that [le] and [lt] refer to operations on [nat].
10 These two functions being defined both in [Stdlib.Bool] and in [Stdlib.Peano],
11 we must export [Stdlib.Peano] later than any export of [Stdlib.Bool]. *)
12 (* We also want to ensure that notations from [Stdlib.Utf8] take precedence
13 over the ones of [Stdlib.Peano] (see Coq PR#12950), so we import [Utf8] last. *)
14 From Corelib Require Export Morphisms RelationClasses ListDef Setoid.
15 From Stdlib Require Export Bool List Peano Utf8 Permutation.
16 From Stdlib Require Export Program.Basics Program.Syntax.
17
18 Export ListNotations.
19
20 TC.AddAllClasses.
21 TC.AddAllInstances.
22
23 (** This notation is necessary to prevent [length] from being printed
24 as [strings.length] if strings.v is imported and later base.v. See
25 also strings.v and
26 https://gitlab.mpi-sws.org/iris/stdpp/-/merge_requests/144 and
27 https://gitlab.mpi-sws.org/iris/stdpp/-/merge_requests/129. *)
28 Notation length := Datatypes.length.
29
30 (** * Enable implicit generalization. *)
31 (** This option enables implicit generalization in arguments of the form
32 [`{...}] (i.e., anonymous arguments). Unfortunately, it also enables
33 implicit generalization in [Instance]. We think that the fact that both
34 behaviors are coupled together is a [bug in
35 Coq](https://github.com/coq/coq/issues/6030). *)
36 Global Generalizable All Variables.
37
38 (** * Tweak program *)
39 (** 1. Since we only use Program to solve logical side-conditions, they should
40 always be made Opaque, otherwise we end up with performance problems due to
41 Coq blindly unfolding them.
42
43 Note that in most cases we use [Next Obligation. (* ... *) Qed.], for which
44 this option does not matter. However, sometimes we write things like
45 [Solve Obligations with naive_solver (* ... *)], and then the obligations
46 should surely be opaque. *)
47 Global Unset Transparent Obligations.
48
49 (** 2. Do not let Program automatically simplify obligations. The default
50 obligation tactic is [Tactics.program_simpl], which, among other things,
51 introduces all variables and gives them fresh names. As such, it becomes
52 impossible to refer to hypotheses in a robust way. *)
53 Global Obligation Tactic := idtac.
54
55 (** 3. Hide obligations and unsealing lemmas from the results of the [Search]
56 commands. *)
57 Add Search Blacklist "_obligation_".
58 Add Search Blacklist "_unseal".
59
60 (** * Sealing off definitions *)
61 #[projections(primitive=yes)]
62 Record seal {A} (f : A) := { unseal : A; seal_eq : unseal = f }.
63 Global Arguments unseal {_ _} _ : assert.
64 Global Arguments seal_eq {_ _} _ : assert.
65
66 (** * Solving type class instances *)
67 (** The tactic [tc_solve] is used to solve type class goals by invoking type
68 class search. It is similar to [apply _], but it is more robust since it does
69 not affect unrelated goals/evars due to https://github.com/coq/coq/issues/6583.
70
71 The tactic [tc_solve] is particularly useful when building custom tactics that
72 need tight control over when type class search is invoked. In Iris, many of the
73 proof mode tactics make use of [notypeclasses refine] and use [tc_solve] to
74 manually invoke type class search.
75
76 Note that [typeclasses eauto] is multi-success. That means, whenever subsequent
77 tactics fail, it will backtrack to [typeclasses eauto] to try the next type
78 class instance. This is almost always undesired and can lead to poor performance
79 and horrible error messages. Hence, we wrap it in a [once]. *)
80 Ltac tc_solve :=
81 solve [once (typeclasses eauto)].
82
83 (** * Non-backtracking type classes *)
84 (** The type class [TCNoBackTrack P] can be used to establish [P] without ever
85 backtracking on the instance of [P] that has been found. Backtracking may
86 normally happen when [P] contains evars that could be instanciated in different
87 ways depending on which instance is picked, and type class search somewhere else
88 depends on this evar.
89
90 The proper way of handling this would be by setting Coq's option
91 `Typeclasses Unique Instances`. However, this option seems to be broken, see Coq
92 issue #6714.
93
94 See https://gitlab.mpi-sws.org/FP/iris-coq/merge_requests/112 for a rationale
95 of this type class. *)
96 Class TCNoBackTrack (P : Prop) := TCNoBackTrack_intro { tc_no_backtrack : P }.
97 Global Hint Extern 0 (TCNoBackTrack _) =>
98 notypeclasses refine (TCNoBackTrack_intro _ _); tc_solve : typeclass_instances.
99
100 (* A conditional at the type class level. Note that [TCIf P Q R] is not the same
101 as [TCOr (TCAnd P Q) R]: the latter will backtrack to [R] if it fails to
102 establish [Q], i.e. does not have the behavior of a conditional. Furthermore,
103 note that [TCOr (TCAnd P Q) (TCAnd (TCNot P) R)] would not work; we generally
104 would not be able to prove the negation of [P]. *)
105 Inductive TCIf (P Q R : Prop) : Prop :=
106 | TCIf_true : P → Q → TCIf P Q R
107 | TCIf_false : R → TCIf P Q R.
108 Existing Class TCIf.
109
110 Global Hint Extern 0 (TCIf _ _ _) =>
111 first [notypeclasses refine (TCIf_true _ _ _ _ _); [tc_solve|]
112 |notypeclasses refine (TCIf_false _ _ _ _)] : typeclass_instances.
113
114 (** * Typeclass opaque definitions *)
115 (** The constant [tc_opaque] is used to make definitions opaque for just type
116 class search. Note that [simpl] is set up to always unfold [tc_opaque]. *)
117 Definition tc_opaque {A} (x : A) : A := x.
118 Global Typeclasses Opaque tc_opaque.
119 Global Arguments tc_opaque {_} _ /.
120
121 (** Below we define type class versions of the common logical operators. It is
122 important to note that we duplicate the definitions, and do not declare the
123 existing logical operators as type classes. That is, we do not say:
124
125 Existing Class or.
126 Existing Class and.
127
128 If we could define the existing logical operators as classes, there is no way
129 of disambiguating whether a premise of a lemma should be solved by type class
130 resolution or not.
131
132 These classes are useful for two purposes: writing complicated type class
133 premises in a more concise way, and for efficiency. For example, using the [Or]
134 class, instead of defining two instances [P → Q1 → R] and [P → Q2 → R] we could
135 have one instance [P → Or Q1 Q2 → R]. When we declare the instance that way, we
136 avoid the need to derive [P] twice. *)
137 Inductive TCOr (P1 P2 : Prop) : Prop :=
138 | TCOr_l : P1 → TCOr P1 P2
139 | TCOr_r : P2 → TCOr P1 P2.
140 Existing Class TCOr.
141 Global Existing Instance TCOr_l | 9.
142 Global Existing Instance TCOr_r | 10.
143 Global Hint Mode TCOr ! ! : typeclass_instances.
144
145 Inductive TCAnd (P1 P2 : Prop) : Prop := TCAnd_intro : P1 → P2 → TCAnd P1 P2.
146 Existing Class TCAnd.
147 Global Existing Instance TCAnd_intro.
148 Global Hint Mode TCAnd ! ! : typeclass_instances.
149
150 Inductive TCTrue : Prop := TCTrue_intro : TCTrue.
151 Existing Class TCTrue.
152 Global Existing Instance TCTrue_intro.
153
154 (** The class [TCFalse] is not stricly necessary as one could also use
155 [False]. However, users might expect that TCFalse exists and if it
156 does not, it can cause hard to diagnose bugs due to automatic
157 generalization. *)
158 Inductive TCFalse : Prop :=.
159 Existing Class TCFalse.
160
161 (** The class [TCUnless] can be used to check that search for [P]
162 fails. This is useful as a guard for certain instances together with
163 classes like [TCFastDone] (see [tactics.v]) to prevent infinite loops
164 (e.g. when saturating the context). *)
165 Notation TCUnless P := (TCIf P TCFalse TCTrue).
166
167 Inductive TCForall {A} (P : A → Prop) : list A → Prop :=
168 | TCForall_nil : TCForall P []
169 | TCForall_cons x xs : P x → TCForall P xs → TCForall P (x :: xs).
170 Existing Class TCForall.
171 Global Existing Instance TCForall_nil.
172 Global Existing Instance TCForall_cons.
173 Global Hint Mode TCForall ! ! ! : typeclass_instances.
174
175 (** The class [TCForall2 P l k] is commonly used to transform an input list [l]
176 into an output list [k], or the converse. Therefore there are two modes, either
177 [l] input and [k] output, or [k] input and [l] input. *)
178 Inductive TCForall2 {A B} (P : A → B → Prop) : list A → list B → Prop :=
179 | TCForall2_nil : TCForall2 P [] []
180 | TCForall2_cons x y xs ys :
181 P x y → TCForall2 P xs ys → TCForall2 P (x :: xs) (y :: ys).
182 Existing Class TCForall2.
183 Global Existing Instance TCForall2_nil.
184 Global Existing Instance TCForall2_cons.
185 Global Hint Mode TCForall2 ! ! ! ! - : typeclass_instances.
186 Global Hint Mode TCForall2 ! ! ! - ! : typeclass_instances.
187
188 Inductive TCExists {A} (P : A → Prop) : list A → Prop :=
189 | TCExists_cons_hd x l : P x → TCExists P (x :: l)
190 | TCExists_cons_tl x l: TCExists P l → TCExists P (x :: l).
191 Existing Class TCExists.
192 Global Existing Instance TCExists_cons_hd | 10.
193 Global Existing Instance TCExists_cons_tl | 20.
194 Global Hint Mode TCExists ! ! ! : typeclass_instances.
195
196 Inductive TCElemOf {A} (x : A) : list A → Prop :=
197 | TCElemOf_here xs : TCElemOf x (x :: xs)
198 | TCElemOf_further y xs : TCElemOf x xs → TCElemOf x (y :: xs).
199 Existing Class TCElemOf.
200 Global Existing Instance TCElemOf_here.
201 Global Existing Instance TCElemOf_further.
202 Global Hint Mode TCElemOf ! ! ! : typeclass_instances.
203
204 (** The intended use of [TCEq x y] is to use [x] as input and [y] as output, but
205 this is not enforced. We use output mode [-] (instead of [!]) for [x] to ensure
206 that type class search succeed on goals like [TCEq (if ? then e1 else e2) ?y],
207 see https://gitlab.mpi-sws.org/iris/iris/merge_requests/391 for a use case.
208 Mode [-] is harmless, the only instance of [TCEq] is [TCEq_refl] below, so we
209 cannot create loops. *)
210 Inductive TCEq {A} (x : A) : A → Prop := TCEq_refl : TCEq x x.
211 Existing Class TCEq.
212 Global Existing Instance TCEq_refl.
213 Global Hint Mode TCEq ! - - : typeclass_instances.
214
215 Lemma TCEq_eq {A} (x1 x2 : A) : TCEq x1 x2 ↔ x1 = x2.
216 Proof. split; destruct 1; reflexivity. Qed.
217
218 (** The [TCSimpl x y] type class is similar to [TCEq] but performs [simpl]
219 before proving the goal by reflexivity. Similar to [TCEq], the argument [x]
220 is the input and [y] the output. When solving [TCEq x y], the argument [x]
221 should be a concrete term and [y] an evar for the [simpl]ed result. *)
222 Class TCSimpl {A} (x x' : A) := TCSimpl_TCEq : TCEq x x'.
223 Global Hint Extern 0 (TCSimpl _ _) =>
224 (* Since the second argument should be an evar, we can call [simpl] on the
225 whole goal. *)
226 simpl; notypeclasses refine (TCEq_refl _) : typeclass_instances.
227 Global Hint Mode TCSimpl ! - - : typeclass_instances.
228
229 Lemma TCSimpl_eq {A} (x1 x2 : A) : TCSimpl x1 x2 ↔ x1 = x2.
230 Proof. apply TCEq_eq. Qed.
231
232 Inductive TCDiag {A} (C : A → Prop) : A → A → Prop :=
233 | TCDiag_diag x : C x → TCDiag C x x.
234 Existing Class TCDiag.
235 Global Existing Instance TCDiag_diag.
236 Global Hint Mode TCDiag ! ! ! - : typeclass_instances.
237 Global Hint Mode TCDiag ! ! - ! : typeclass_instances.
238
239 (** Given a proposition [P] that is a type class, [tc_to_bool P] will return
240 [true] iff there is an instance of [P]. It is often useful in Ltac programming,
241 where one can do [lazymatch tc_to_bool P with true => .. | false => .. end]. *)
242 Definition tc_to_bool (P : Prop)
243 {p : bool} `{TCIf P (TCEq p true) (TCEq p false)} : bool := p.
244
245 (** Throughout this development we use [stdpp_scope] for all general purpose
246 notations that do not belong to a more specific scope. *)
247 Declare Scope stdpp_scope.
248 Delimit Scope stdpp_scope with stdpp.
249 Global Open Scope stdpp_scope.
250
251 (** Change [True] and [False] into notations in order to enable overloading.
252 We will use this to give [True] and [False] a different interpretation for
253 embedded logics. *)
254 Notation "'True'" := True (format "True") : type_scope.
255 Notation "'False'" := False (format "False") : type_scope.
256
257 (** Change [forall] into a notation in order to enable overloading. *)
258 Notation "'forall' x .. y , P" := (forall x, .. (forall y, P) ..)
259 (at level 200, x binder, y binder, right associativity,
260 only parsing) : type_scope.
261
262
263 (** * Equality *)
264 (** Introduce some Haskell style like notations. *)
265 Notation "(=)" := eq (only parsing) : stdpp_scope.
266 Notation "( x =.)" := (eq x) (only parsing) : stdpp_scope.
267 Notation "(.= x )" := (λ y, eq y x) (only parsing) : stdpp_scope.
268 Notation "(≠)" := (λ x y, x ≠ y) (only parsing) : stdpp_scope.
269 Notation "( x ≠.)" := (λ y, x ≠ y) (only parsing) : stdpp_scope.
270 Notation "(.≠ x )" := (λ y, y ≠ x) (only parsing) : stdpp_scope.
271
272 Infix "=@{ A }" := (@eq A)
273 (at level 70, only parsing, no associativity) : stdpp_scope.
274 Notation "(=@{ A } )" := (@eq A) (only parsing) : stdpp_scope.
275 Notation "(≠@{ A } )" := (λ X Y, ¬X =@{A} Y) (only parsing) : stdpp_scope.
276 Notation "X ≠@{ A } Y":= (¬X =@{ A } Y)
277 (at level 70, only parsing, no associativity) : stdpp_scope.
278
279 Global Hint Extern 0 (_ = _) => reflexivity : core.
280 Global Hint Extern 100 (_ ≠ _) => discriminate : core.
281
282 Global Instance: ∀ A, PreOrder (=@{A}).
283 Proof. split; repeat intro; congruence. Qed.
284
285 (** ** Setoid equality *)
286 (** We define an operational type class for setoid equality, i.e., the
287 "canonical" equivalence for a type. The typeclass is tied to the \equiv
288 symbol. This is based on (Spitters/van der Weegen, 2011). *)
289 Class Equiv A := equiv: relation A.
290 Global Hint Mode Equiv ! : typeclass_instances.
291
292 (** We instruct setoid rewriting to infer [equiv] as a relation on
293 type [A] when needed. This allows setoid_rewrite to solve constraints
294 of shape [Proper (eq ==> ?R) f] using [Proper (eq ==> (equiv (A:=A))) f]
295 when an equivalence relation is available on type [A]. We put this instance
296 at level 150 so it does not take precedence over Coq's stdlib instances,
297 favoring inference of [eq] (all Coq functions are automatically morphisms
298 for [eq]). We have [eq] (at 100) < [≡] (at 150) < [⊑] (at 200). *)
299 Global Instance equiv_rewrite_relation `{Equiv A} :
300 RewriteRelation (@equiv A _) | 150 := {}.
301
302 Infix "≡" := equiv (at level 70, no associativity) : stdpp_scope.
303 Infix "≡@{ A }" := (@equiv A _)
304 (at level 70, only parsing, no associativity) : stdpp_scope.
305
306 Notation "(≡)" := equiv (only parsing) : stdpp_scope.
307 Notation "( X ≡.)" := (equiv X) (only parsing) : stdpp_scope.
308 Notation "(.≡ X )" := (λ Y, Y ≡ X) (only parsing) : stdpp_scope.
309 Notation "(≢)" := (λ X Y, ¬X ≡ Y) (only parsing) : stdpp_scope.
310 Notation "X ≢ Y":= (¬X ≡ Y) (at level 70, no associativity) : stdpp_scope.
311 Notation "( X ≢.)" := (λ Y, X ≢ Y) (only parsing) : stdpp_scope.
312 Notation "(.≢ X )" := (λ Y, Y ≢ X) (only parsing) : stdpp_scope.
313
314 Notation "(≡@{ A } )" := (@equiv A _) (only parsing) : stdpp_scope.
315 Notation "(≢@{ A } )" := (λ X Y, ¬X ≡@{A} Y) (only parsing) : stdpp_scope.
316 Notation "X ≢@{ A } Y":= (¬X ≡@{ A } Y)
317 (at level 70, only parsing, no associativity) : stdpp_scope.
318
319 (** The type class [LeibnizEquiv] collects setoid equalities that coincide
320 with Leibniz equality. We provide the tactic [fold_leibniz] to transform such
321 setoid equalities into Leibniz equalities, and [unfold_leibniz] for the
322 reverse.
323
324 Various std++ tactics assume that this class is only instantiated if [≡]
325 is an equivalence relation. *)
326 Class LeibnizEquiv A `{Equiv A} :=
327 leibniz_equiv (x y : A) : x ≡ y → x = y.
328 Global Hint Mode LeibnizEquiv ! ! : typeclass_instances.
329
330 Lemma leibniz_equiv_iff `{LeibnizEquiv A, !Reflexive (≡@{A})} (x y : A) :
331 x ≡ y ↔ x = y.
332 Proof. split; [apply leibniz_equiv|]. intros ->; reflexivity. Qed.
333
334 Ltac fold_leibniz := repeat
335 match goal with
336 | H : context [ _ ≡@{?A} _ ] |- _ =>
337 setoid_rewrite (leibniz_equiv_iff (A:=A)) in H
338 | |- context [ _ ≡@{?A} _ ] =>
339 setoid_rewrite (leibniz_equiv_iff (A:=A))
340 end.
341 Ltac unfold_leibniz := repeat
342 match goal with
343 | H : context [ _ =@{?A} _ ] |- _ =>
344 setoid_rewrite <-(leibniz_equiv_iff (A:=A)) in H
345 | |- context [ _ =@{?A} _ ] =>
346 setoid_rewrite <-(leibniz_equiv_iff (A:=A))
347 end.
348
349 Definition equivL {A} : Equiv A := (=).
350
351 (** A [Params f n] instance forces the setoid rewriting mechanism not to
352 rewrite in the first [n] arguments of the function [f]. We will declare such
353 instances for all operational type classes in this development. *)
354 Global Instance: Params (@equiv) 2 := {}.
355
356 (** The following instance forces [setoid_replace] to use setoid equality
357 (for types that have an [Equiv] instance) rather than the standard Leibniz
358 equality. *)
359 Global Instance equiv_default_relation `{Equiv A} :
360 DefaultRelation (≡@{A}) | 3 := {}.
361 Global Hint Extern 0 (_ ≡ _) => reflexivity : core.
362 Global Hint Extern 0 (_ ≡ _) => symmetry; assumption : core.
363
364
365 (** * Type classes *)
366 (** ** Decidable propositions *)
367 (** This type class by (Spitters/van der Weegen, 2011) collects decidable
368 propositions. *)
369 Class Decision (P : Prop) := decide : {P} + {¬P}.
370 Global Hint Mode Decision ! : typeclass_instances.
371 Global Arguments decide _ {_} : simpl never, assert.
372
373 (** Although [RelDecision R] is just [∀ x y, Decision (R x y)], we make this
374 an explicit class instead of a notation for two reasons:
375
376 - It allows us to control [Hint Mode] more precisely. In particular, if it were
377 defined as a notation, the above [Hint Mode] for [Decision] would not prevent
378 diverging instance search when looking for [RelDecision (@eq ?A)], which would
379 result in it looking for [Decision (@eq ?A x y)], i.e. an instance where the
380 head position of [Decision] is not en evar.
381 - We use it to avoid inefficient computation due to eager evaluation of
382 propositions by [vm_compute]. This inefficiency arises for example if
383 [(x = y) := (f x = f y)]. Since [decide (x = y)] evaluates to
384 [decide (f x = f y)], this would then lead to evaluation of [f x] and [f y].
385 Using the [RelDecision], the [f] is hidden under a lambda, which prevents
386 unnecessary evaluation. *)
387 Class RelDecision {A B} (R : A → B → Prop) :=
388 decide_rel x y :: Decision (R x y).
389 Global Hint Mode RelDecision ! ! ! : typeclass_instances.
390 Global Arguments decide_rel {_ _} _ {_} _ _ : simpl never, assert.
391 Notation EqDecision A := (RelDecision (=@{A})).
392
393 (** ** Inhabited types *)
394 (** This type class collects types that are inhabited. *)
395 Class Inhabited (A : Type) : Type := populate { inhabitant : A }.
396 Global Hint Mode Inhabited ! : typeclass_instances.
397 Global Arguments populate {_} _ : assert.
398
399 (** ** Proof irrelevant types *)
400 (** This type class collects types that are proof irrelevant. That means, all
401 elements of the type are equal. We use this notion only used for propositions,
402 but by universe polymorphism we can generalize it. *)
403 Class ProofIrrel (A : Type) : Prop := proof_irrel (x y : A) : x = y.
404 Global Hint Mode ProofIrrel ! : typeclass_instances.
405
406 (** ** Common properties *)
407 (** These operational type classes allow us to refer to common mathematical
408 properties in a generic way. For example, for injectivity of [(k ++.)] it
409 allows us to write [inj (k ++.)] instead of [app_inv_head k]. *)
410 Class Inj {A B} (R : relation A) (S : relation B) (f : A → B) : Prop :=
411 inj x y : S (f x) (f y) → R x y.
412 Class Inj2 {A B C} (R1 : relation A) (R2 : relation B)
413 (S : relation C) (f : A → B → C) : Prop :=
414 inj2 x1 x2 y1 y2 : S (f x1 x2) (f y1 y2) → R1 x1 y1 ∧ R2 x2 y2.
415 Class Cancel {A B} (S : relation B) (f : A → B) (g : B → A) : Prop :=
416 cancel x : S (f (g x)) x.
417 Class Surj {A B} (R : relation B) (f : A → B) :=
418 surj y : ∃ x, R (f x) y.
419 Class IdemP {A} (R : relation A) (f : A → A → A) : Prop :=
420 idemp x : R (f x x) x.
421 Class Comm {A B} (R : relation A) (f : B → B → A) : Prop :=
422 comm x y : R (f x y) (f y x).
423 Class LeftId {A} (R : relation A) (i : A) (f : A → A → A) : Prop :=
424 left_id x : R (f i x) x.
425 Class RightId {A} (R : relation A) (i : A) (f : A → A → A) : Prop :=
426 right_id x : R (f x i) x.
427 Class Assoc {A} (R : relation A) (f : A → A → A) : Prop :=
428 assoc x y z : R (f x (f y z)) (f (f x y) z).
429 Class LeftAbsorb {A} (R : relation A) (i : A) (f : A → A → A) : Prop :=
430 left_absorb x : R (f i x) i.
431 Class RightAbsorb {A} (R : relation A) (i : A) (f : A → A → A) : Prop :=
432 right_absorb x : R (f x i) i.
433 Class AntiSymm {A} (R S : relation A) : Prop :=
434 anti_symm x y : S x y → S y x → R x y.
435 Class Total {A} (R : relation A) := total x y : R x y ∨ R y x.
436 Class Trichotomy {A} (R : relation A) :=
437 trichotomy x y : R x y ∨ x = y ∨ R y x.
438 Class TrichotomyT {A} (R : relation A) :=
439 trichotomyT x y : {R x y} + {x = y} + {R y x}.
440
441 Notation Involutive R f := (Cancel R f f).
442 Lemma involutive {A} {R : relation A} (f : A → A) `{Involutive R f} x :
443 R (f (f x)) x.
444 Proof. auto. Qed.
445
446 Global Arguments irreflexivity {_} _ {_} _ _ : assert.
447 Global Arguments inj {_ _ _ _} _ {_} _ _ _ : assert.
448 Global Arguments inj2 {_ _ _ _ _ _} _ {_} _ _ _ _ _: assert.
449 Global Arguments cancel {_ _ _} _ _ {_} _ : assert.
450 Global Arguments surj {_ _ _} _ {_} _ : assert.
451 Global Arguments idemp {_ _} _ {_} _ : assert.
452 Global Arguments comm {_ _ _} _ {_} _ _ : assert.
453 Global Arguments left_id {_ _} _ _ {_} _ : assert.
454 Global Arguments right_id {_ _} _ _ {_} _ : assert.
455 Global Arguments assoc {_ _} _ {_} _ _ _ : assert.
456 Global Arguments left_absorb {_ _} _ _ {_} _ : assert.
457 Global Arguments right_absorb {_ _} _ _ {_} _ : assert.
458 Global Arguments anti_symm {_ _} _ {_} _ _ _ _ : assert.
459 Global Arguments total {_} _ {_} _ _ : assert.
460 Global Arguments trichotomy {_} _ {_} _ _ : assert.
461 Global Arguments trichotomyT {_} _ {_} _ _ : assert.
462
463 Lemma not_symmetry `{R : relation A, !Symmetric R} x y : ¬R x y → ¬R y x.
464 Proof. intuition. Qed.
465 Lemma symmetry_iff `(R : relation A) `{!Symmetric R} x y : R x y ↔ R y x.
466 Proof. intuition. Qed.
467
468 Lemma not_inj `{Inj A B R R' f} x y : ¬R x y → ¬R' (f x) (f y).
469 Proof. intuition. Qed.
470 Lemma not_inj2_1 `{Inj2 A B C R R' R'' f} x1 x2 y1 y2 :
471 ¬R x1 x2 → ¬R'' (f x1 y1) (f x2 y2).
472 Proof. intros HR HR''. destruct (inj2 f x1 y1 x2 y2); auto. Qed.
473 Lemma not_inj2_2 `{Inj2 A B C R R' R'' f} x1 x2 y1 y2 :
474 ¬R' y1 y2 → ¬R'' (f x1 y1) (f x2 y2).
475 Proof. intros HR' HR''. destruct (inj2 f x1 y1 x2 y2); auto. Qed.
476
477 Lemma inj_iff {A B} {R : relation A} {S : relation B} (f : A → B)
478 `{!Inj R S f} `{!Proper (R ==> S) f} x y : S (f x) (f y) ↔ R x y.
479 Proof. firstorder. Qed.
480 Global Instance inj2_inj_1 `{Inj2 A B C R1 R2 R3 f} y : Inj R1 R3 (λ x, f x y).
481 Proof. repeat intro; edestruct (inj2 f); eauto. Qed.
482 Global Instance inj2_inj_2 `{Inj2 A B C R1 R2 R3 f} x : Inj R2 R3 (f x).
483 Proof. repeat intro; edestruct (inj2 f); eauto. Qed.
484 Elpi TC Solver Override TC.Solver Rm ProperProxy.
485
486 Lemma cancel_inj `{Cancel A B R1 f g, !Equivalence R1, !Proper (R2 ==> R1) f} :
487 Inj R1 R2 g.
488 Proof.
489 intros x y E. rewrite <-(cancel f g x), <-(cancel f g y), E. reflexivity.
490 Qed.
491 Lemma cancel_surj `{Cancel A B R1 f g} : Surj R1 f.
492 Proof. intros y. exists (g y). auto. Qed.
493
494 (** The following lemmas are specific versions of the projections of the above
495 type classes for Leibniz equality. These lemmas allow us to enforce Coq not to
496 use the setoid rewriting mechanism. *)
497 Lemma idemp_L {A} f `{!@IdemP A (=) f} x : f x x = x.
498 Proof. auto. Qed.
499 Lemma comm_L {A B} f `{!@Comm A B (=) f} x y : f x y = f y x.
500 Proof. auto. Qed.
501 Lemma left_id_L {A} i f `{!@LeftId A (=) i f} x : f i x = x.
502 Proof. auto. Qed.
503 Lemma right_id_L {A} i f `{!@RightId A (=) i f} x : f x i = x.
504 Proof. auto. Qed.
505 Lemma assoc_L {A} f `{!@Assoc A (=) f} x y z : f x (f y z) = f (f x y) z.
506 Proof. auto. Qed.
507 Lemma left_absorb_L {A} i f `{!@LeftAbsorb A (=) i f} x : f i x = i.
508 Proof. auto. Qed.
509 Lemma right_absorb_L {A} i f `{!@RightAbsorb A (=) i f} x : f x i = i.
510 Proof. auto. Qed.
511
512 (** ** Generic orders *)
513 (** The classes [PreOrder], [PartialOrder], and [TotalOrder] use an arbitrary
514 relation [R] instead of [⊆] to support multiple orders on the same type. *)
515 Definition strict {A} (R : relation A) : relation A := λ X Y, R X Y ∧ ¬R Y X.
516 Global Instance: Params (@strict) 2 := {}.
517
518 Set Warnings "-future-coercion-class-field".
519
520 Class PartialOrder {A} (R : relation A) : Prop := {
521 partial_order_pre ::> PreOrder R;
522 partial_order_anti_symm :> AntiSymm (=) R
523 }.
524 Global Hint Mode PartialOrder ! ! : typeclass_instances.
525
526 Class TotalOrder {A} (R : relation A) : Prop := {
527 total_order_partial :> PartialOrder R;
528 total_order_trichotomy :> Trichotomy (strict R)
529 }.
530 Global Hint Mode TotalOrder ! ! : typeclass_instances.
531
532 (** * Logic *)
533 Global Instance prop_inhabited : Inhabited Prop := populate True.
534
535 Notation "(∧)" := and (only parsing) : stdpp_scope.
536 Notation "( A ∧.)" := (and A) (only parsing) : stdpp_scope.
537 Notation "(.∧ B )" := (λ A, A ∧ B) (only parsing) : stdpp_scope.
538
539 Notation "(∨)" := or (only parsing) : stdpp_scope.
540 Notation "( A ∨.)" := (or A) (only parsing) : stdpp_scope.
541 Notation "(.∨ B )" := (λ A, A ∨ B) (only parsing) : stdpp_scope.
542
543 Notation "(↔)" := iff (only parsing) : stdpp_scope.
544 Notation "( A ↔.)" := (iff A) (only parsing) : stdpp_scope.
545 Notation "(.↔ B )" := (λ A, A ↔ B) (only parsing) : stdpp_scope.
546
547 Global Hint Extern 0 (_ ↔ _) => reflexivity : core.
548 Global Hint Extern 0 (_ ↔ _) => symmetry; assumption : core.
549
550 Lemma or_l P Q : ¬Q → P ∨ Q ↔ P.
551 Proof. tauto. Qed.
552 Lemma or_r P Q : ¬P → P ∨ Q ↔ Q.
553 Proof. tauto. Qed.
554 Lemma and_wlog_l (P Q : Prop) : (Q → P) → Q → (P ∧ Q).
555 Proof. tauto. Qed.
556 Lemma and_wlog_r (P Q : Prop) : P → (P → Q) → (P ∧ Q).
557 Proof. tauto. Qed.
558 Lemma impl_transitive (P Q R : Prop) : (P → Q) → (Q → R) → (P → R).
559 Proof. tauto. Qed.
560 Lemma forall_proper {A} (P Q : A → Prop) :
561 (∀ x, P x ↔ Q x) → (∀ x, P x) ↔ (∀ x, Q x).
562 Proof. firstorder. Qed.
563 Lemma exist_proper {A} (P Q : A → Prop) :
564 (∀ x, P x ↔ Q x) → (∃ x, P x) ↔ (∃ x, Q x).
565 Proof. firstorder. Qed.
566
567 Global Instance eq_comm {A} : Comm (↔) (=@{A}).
568 Proof. red; intuition. Qed.
569 Global Instance flip_eq_comm {A} : Comm (↔) (λ x y, y =@{A} x).
570 Proof. red; intuition. Qed.
571 Global Instance iff_comm : Comm (↔) (↔).
572 Proof. red; intuition. Qed.
573 Global Instance and_comm : Comm (↔) (∧).
574 Proof. red; intuition. Qed.
575 Global Instance and_assoc : Assoc (↔) (∧).
576 Proof. red; intuition. Qed.
577 Global Instance and_idemp : IdemP (↔) (∧).
578 Proof. red; intuition. Qed.
579 Global Instance or_comm : Comm (↔) (∨).
580 Proof. red; intuition. Qed.
581 Global Instance or_assoc : Assoc (↔) (∨).
582 Proof. red; intuition. Qed.
583 Global Instance or_idemp : IdemP (↔) (∨).
584 Proof. red; intuition. Qed.
585 Global Instance True_and : LeftId (↔) True (∧).
586 Proof. red; intuition. Qed.
587 Global Instance and_True : RightId (↔) True (∧).
588 Proof. red; intuition. Qed.
589 Global Instance False_and : LeftAbsorb (↔) False (∧).
590 Proof. red; intuition. Qed.
591 Global Instance and_False : RightAbsorb (↔) False (∧).
592 Proof. red; intuition. Qed.
593 Global Instance False_or : LeftId (↔) False (∨).
594 Proof. red; intuition. Qed.
595 Global Instance or_False : RightId (↔) False (∨).
596 Proof. red; intuition. Qed.
597 Global Instance True_or : LeftAbsorb (↔) True (∨).
598 Proof. red; intuition. Qed.
599 Global Instance or_True : RightAbsorb (↔) True (∨).
600 Proof. red; intuition. Qed.
601 Global Instance True_impl : LeftId (↔) True impl.
602 Proof. unfold impl. red; intuition. Qed.
603 Global Instance impl_True : RightAbsorb (↔) True impl.
604 Proof. unfold impl. red; intuition. Qed.
605
606
607 (** * Common data types *)
608 (** ** Functions *)
609 Notation "(→)" := (λ A B, A → B) (only parsing) : stdpp_scope.
610 Notation "( A →.)" := (λ B, A → B) (only parsing) : stdpp_scope.
611 Notation "(.→ B )" := (λ A, A → B) (only parsing) : stdpp_scope.
612
613 Notation "t $ r" := (t r)
614 (at level 65, right associativity, only parsing) : stdpp_scope.
615 Notation "($)" := (λ f x, f x) (only parsing) : stdpp_scope.
616 Notation "(.$ x )" := (λ f, f x) (only parsing) : stdpp_scope.
617
618 Infix "∘" := compose : stdpp_scope.
619 Notation "(∘)" := compose (only parsing) : stdpp_scope.
620 Notation "( f ∘.)" := (compose f) (only parsing) : stdpp_scope.
621 Notation "(.∘ f )" := (λ g, compose g f) (only parsing) : stdpp_scope.
622
623 Global Instance impl_inhabited {A} `{Inhabited B} : Inhabited (A → B) :=
624 populate (λ _, inhabitant).
625
626 (** Ensure that [simpl] unfolds [id], [compose], and [flip] when fully
627 applied. *)
628 Global Arguments id _ _ / : assert.
629 Global Arguments compose _ _ _ _ _ _ / : assert.
630 Global Arguments flip _ _ _ _ _ _ / : assert.
631 Global Arguments const _ _ _ _ / : assert.
632 Global Typeclasses Transparent id compose flip const.
633
634 Definition fun_map {A A' B B'} (f: A' → A) (g: B → B') (h : A → B) : A' → B' :=
635 g ∘ h ∘ f.
636
637 Global Instance const_proper `{R1 : relation A, R2 : relation B} (x : B) :
638 Reflexive R2 → Proper (R1 ==> R2) (λ _, x).
639 Proof. intros ? y1 y2; reflexivity. Qed.
640
641 Global Instance id_inj {A} : Inj (=) (=) (@id A).
642 Proof. intros ??; auto. Qed.
643 Global Instance compose_inj {A B C} R1 R2 R3 (f : A → B) (g : B → C) :
644 Inj R1 R2 f → Inj R2 R3 g → Inj R1 R3 (g ∘ f).
645 Proof. red; intuition. Qed.
646
647 Global Instance id_surj {A} : Surj (=) (@id A).
648 Proof. intros y; exists y; reflexivity. Qed.
649 Global Instance compose_surj {A B C} R (f : A → B) (g : B → C) :
650 Surj (=) f → Surj R g → Surj R (g ∘ f).
651 Proof.
652 intros ?? x. unfold compose. destruct (surj g x) as [y ?].
653 destruct (surj f y) as [z ?]. exists z. congruence.
654 Qed.
655
656 Global Instance const2_comm {A B} (x : B) : Comm (=) (λ _ _ : A, x).
657 Proof. intros ?; reflexivity. Qed.
658 Global Instance const2_assoc {A} (x : A) : Assoc (=) (λ _ _ : A, x).
659 Proof. intros ???; reflexivity. Qed.
660 Global Instance id1_assoc {A} : Assoc (=) (λ x _ : A, x).
661 Proof. intros ???; reflexivity. Qed.
662 Global Instance id2_assoc {A} : Assoc (=) (λ _ x : A, x).
663 Proof. intros ???; reflexivity. Qed.
664 Global Instance id1_idemp {A} : IdemP (=) (λ x _ : A, x).
665 Proof. intros ?; reflexivity. Qed.
666 Global Instance id2_idemp {A} : IdemP (=) (λ _ x : A, x).
667 Proof. intros ?; reflexivity. Qed.
668
669 (** ** Lists *)
670 Global Instance list_inhabited {A} : Inhabited (list A) := populate [].
671
672 Definition zip_with {A B C} (f : A → B → C) : list A → list B → list C :=
673 fix go l1 l2 :=
674 match l1, l2 with x1 :: l1, x2 :: l2 => f x1 x2 :: go l1 l2 | _ , _ => [] end.
675 Notation zip := (zip_with pair).
676
677 (** ** Booleans *)
678 (** The following coercion allows us to use Booleans as propositions. *)
679 Coercion Is_true : bool >-> Sortclass.
680 Global Hint Unfold Is_true : core.
681 Global Hint Immediate Is_true_eq_left : core.
682 Global Hint Resolve orb_prop_intro andb_prop_intro : core.
683 Notation "(&&)" := andb (only parsing).
684 Notation "(||)" := orb (only parsing).
685 Infix "&&*" := (zip_with (&&)) (at level 40).
686 Infix "||*" := (zip_with (||)) (at level 50).
687
688 Global Instance bool_inhabated : Inhabited bool := populate true.
689
690 Definition bool_le (β1 β2 : bool) : Prop := negb β1 || β2.
691 Infix "=.>" := bool_le (at level 70).
692 Infix "=.>*" := (Forall2 bool_le) (at level 70).
693 Global Instance: PartialOrder bool_le.
694 Proof. repeat split; repeat intros [|]; compute; tauto. Qed.
695
696 Lemma andb_True b1 b2 : b1 && b2 ↔ b1 ∧ b2.
697 Proof. destruct b1, b2; simpl; tauto. Qed.
698 Lemma orb_True b1 b2 : b1 || b2 ↔ b1 ∨ b2.
699 Proof. destruct b1, b2; simpl; tauto. Qed.
700 Lemma negb_True b : negb b ↔ ¬b.
701 Proof. destruct b; simpl; tauto. Qed.
702 Lemma Is_true_true (b : bool) : b ↔ b = true.
703 Proof. now destruct b. Qed.
704 Lemma Is_true_true_1 (b : bool) : b → b = true.
705 Proof. apply Is_true_true. Qed.
706 Lemma Is_true_true_2 (b : bool) : b = true → b.
707 Proof. apply Is_true_true. Qed.
708 Lemma Is_true_false (b : bool) : ¬ b ↔ b = false.
709 Proof. now destruct b; simpl. Qed.
710 Lemma Is_true_false_1 (b : bool) : ¬b → b = false.
711 Proof. apply Is_true_false. Qed.
712 Lemma Is_true_false_2 (b : bool) : b = false → ¬b.
713 Proof. apply Is_true_false. Qed.
714
715 (** ** Unit *)
716 Global Instance unit_equiv : Equiv unit := λ _ _, True.
717 Global Instance unit_equivalence : Equivalence (≡@{unit}).
718 Proof. repeat split. Qed.
719 Global Instance unit_leibniz : LeibnizEquiv unit.
720 Proof. intros [] []; reflexivity. Qed.
721 Global Instance unit_inhabited: Inhabited unit := populate ().
722
723 (** ** Empty *)
724 Global Instance Empty_set_equiv : Equiv Empty_set := λ _ _, True.
725 Global Instance Empty_set_equivalence : Equivalence (≡@{Empty_set}).
726 Proof. repeat split. Qed.
727 Global Instance Empty_set_leibniz : LeibnizEquiv Empty_set.
728 Proof. intros [] []; reflexivity. Qed.
729
730 (** ** Products *)
731 Notation "( x ,.)" := (pair x) (only parsing) : stdpp_scope.
732 Notation "(., y )" := (λ x, (x,y)) (only parsing) : stdpp_scope.
733
734 Notation "p .1" := (fst p) (at level 1, left associativity, format "p .1").
735 Notation "p .2" := (snd p) (at level 1, left associativity, format "p .2").
736
737 Global Instance: Params (@pair) 2 := {}.
738 Global Instance: Params (@fst) 2 := {}.
739 Global Instance: Params (@snd) 2 := {}.
740
741 Global Instance: Params (@curry) 3 := {}.
742 Global Instance: Params (@uncurry) 3 := {}.
743
744 Definition uncurry3 {A B C D} (f : A → B → C → D) (p : A * B * C) : D :=
745 let '(a,b,c) := p in f a b c.
746 Global Instance: Params (@uncurry3) 4 := {}.
747 Definition uncurry4 {A B C D E} (f : A → B → C → D → E) (p : A * B * C * D) : E :=
748 let '(a,b,c,d) := p in f a b c d.
749 Global Instance: Params (@uncurry4) 5 := {}.
750
751 Definition curry3 {A B C D} (f : A * B * C → D) (a : A) (b : B) (c : C) : D :=
752 f (a, b, c).
753 Global Instance: Params (@curry3) 4 := {}.
754 Definition curry4 {A B C D E} (f : A * B * C * D → E)
755 (a : A) (b : B) (c : C) (d : D) : E := f (a, b, c, d).
756 Global Instance: Params (@curry4) 5 := {}.
757
758 Definition prod_map {A A' B B'} (f: A → A') (g: B → B') (p : A * B) : A' * B' :=
759 (f (p.1), g (p.2)).
760 Global Instance: Params (@prod_map) 4 := {}.
761 Global Arguments prod_map {_ _ _ _} _ _ !_ / : assert.
762
763 Definition prod_zip {A A' A'' B B' B''} (f : A → A' → A'') (g : B → B' → B'')
764 (p : A * B) (q : A' * B') : A'' * B'' := (f (p.1) (q.1), g (p.2) (q.2)).
765 Global Instance: Params (@prod_zip) 6 := {}.
766 Global Arguments prod_zip {_ _ _ _ _ _} _ _ !_ !_ / : assert.
767
768 Definition prod_swap {A B} (p : A * B) : B * A := (p.2, p.1).
769 Global Arguments prod_swap {_ _} !_ /.
770 Global Instance: Params (@prod_swap) 2 := {}.
771
772 Global Instance prod_inhabited {A B} (iA : Inhabited A)
773 (iB : Inhabited B) : Inhabited (A * B) :=
774 match iA, iB with populate x, populate y => populate (x,y) end.
775
776 (** Note that we need eta for products for the [uncurry_curry] lemmas to hold
777 in non-applied form ([uncurry (curry f) = f]). *)
778 Lemma curry_uncurry {A B C} (f : A → B → C) : curry (uncurry f) = f.
779 Proof. reflexivity. Qed.
780 Lemma uncurry_curry {A B C} (f : A * B → C) p : uncurry (curry f) p = f p.
781 Proof. destruct p; reflexivity. Qed.
782 Lemma curry3_uncurry3 {A B C D} (f : A → B → C → D) : curry3 (uncurry3 f) = f.
783 Proof. reflexivity. Qed.
784 Lemma uncurry3_curry3 {A B C D} (f : A * B * C → D) p :
785 uncurry3 (curry3 f) p = f p.
786 Proof. destruct p as [[??] ?]; reflexivity. Qed.
787 Lemma curry4_uncurry4 {A B C D E} (f : A → B → C → D → E) :
788 curry4 (uncurry4 f) = f.
789 Proof. reflexivity. Qed.
790 Lemma uncurry4_curry4 {A B C D E} (f : A * B * C * D → E) p :
791 uncurry4 (curry4 f) p = f p.
792 Proof. destruct p as [[[??] ?] ?]; reflexivity. Qed.
793
794 (** [pair_eq] as a name is more consistent with our usual naming. *)
795 Lemma pair_eq {A B} (a1 a2 : A) (b1 b2 : B) :
796 (a1, b1) = (a2, b2) ↔ a1 = a2 ∧ b1 = b2.
797 Proof. apply pair_equal_spec. Qed.
798
799 Global Instance pair_inj {A B} : Inj2 (=) (=) (=) (@pair A B).
800 Proof. injection 1; auto. Qed.
801 Global Instance prod_map_inj {A A' B B'} (f : A → A') (g : B → B') :
802 Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (prod_map f g).
803 Proof.
804 intros ?? [??] [??] ?; simpl in *; f_equal;
805 [apply (inj f)|apply (inj g)]; congruence.
806 Qed.
807
808 Elpi TC Solver Override TC.Solver Rm ProperProxy Proper.
809
810 Global Instance prod_swap_cancel {A B} :
811 Cancel (=) (@prod_swap A B) (@prod_swap B A).
812 Proof. intros [??]; reflexivity. Qed.
813 Global Instance prod_swap_inj {A B} : Inj (=) (=) (@prod_swap A B).
814 Proof. apply cancel_inj. Qed.
815 Global Instance prod_swap_surj {A B} : Surj (=) (@prod_swap A B).
816 Proof. apply cancel_surj. Qed.
817
818 Definition prod_relation {A B} (R1 : relation A) (R2 : relation B) :
819 relation (A * B) := λ x y, R1 (x.1) (y.1) ∧ R2 (x.2) (y.2).
820
821 Section prod_relation.
822 Context `{RA : relation A, RB : relation B}.
823
824 Global Instance prod_relation_refl :
825 Reflexive RA → Reflexive RB → Reflexive (prod_relation RA RB).
826 Proof. firstorder eauto. Qed.
827 Global Instance prod_relation_sym :
828 Symmetric RA → Symmetric RB → Symmetric (prod_relation RA RB).
829 Proof. firstorder eauto. Qed.
830 Global Instance prod_relation_trans :
831 Transitive RA → Transitive RB → Transitive (prod_relation RA RB).
832 Proof. firstorder eauto. Qed.
833 Global Instance prod_relation_equiv :
834 Equivalence RA → Equivalence RB → Equivalence (prod_relation RA RB).
835 Proof. split; apply _. Qed.
836
837 Global Instance pair_proper' : Proper (RA ==> RB ==> prod_relation RA RB) pair.
838 Proof. firstorder eauto. Qed.
839 Global Instance pair_inj' : Inj2 RA RB (prod_relation RA RB) pair.
840 Proof. inversion_clear 1; eauto. Qed.
841 Global Instance fst_proper' : Proper (prod_relation RA RB ==> RA) fst.
842 Proof. firstorder eauto. Qed.
843 Global Instance snd_proper' : Proper (prod_relation RA RB ==> RB) snd.
844 Proof. firstorder eauto. Qed.
845
846 Global Instance prod_swap_proper' :
847 Proper (prod_relation RA RB ==> prod_relation RB RA) prod_swap.
848 Proof. firstorder eauto. Qed.
849
850 Global Instance curry_proper' `{RC : relation C} :
851 Proper ((prod_relation RA RB ==> RC) ==> RA ==> RB ==> RC) curry.
852 Proof. firstorder eauto. Qed.
853 Global Instance uncurry_proper' `{RC : relation C} :
854 Proper ((RA ==> RB ==> RC) ==> prod_relation RA RB ==> RC) uncurry.
855 Proof. intros f1 f2 Hf [x1 y1] [x2 y2] []; apply Hf; assumption. Qed.
856
857 Global Instance curry3_proper' `{RC : relation C, RD : relation D} :
858 Proper ((prod_relation (prod_relation RA RB) RC ==> RD) ==>
859 RA ==> RB ==> RC ==> RD) curry3.
860 Proof. firstorder eauto. Qed.
861 Global Instance uncurry3_proper' `{RC : relation C, RD : relation D} :
862 Proper ((RA ==> RB ==> RC ==> RD) ==>
863 prod_relation (prod_relation RA RB) RC ==> RD) uncurry3.
864 Proof. intros f1 f2 Hf [[??] ?] [[??] ?] [[??] ?]; apply Hf; assumption. Qed.
865
866 Global Instance curry4_proper' `{RC : relation C, RD : relation D, RE : relation E} :
867 Proper ((prod_relation (prod_relation (prod_relation RA RB) RC) RD ==> RE) ==>
868 RA ==> RB ==> RC ==> RD ==> RE) curry4.
869 Proof. firstorder eauto. Qed.
870 Global Instance uncurry4_proper' `{RC : relation C, RD : relation D, RE : relation E} :
871 Proper ((RA ==> RB ==> RC ==> RD ==> RE) ==>
872 prod_relation (prod_relation (prod_relation RA RB) RC) RD ==> RE) uncurry4.
873 Proof.
874 intros f1 f2 Hf [[[??] ?] ?] [[[??] ?] ?] [[[??] ?] ?]; apply Hf; assumption.
875 Qed.
876 End prod_relation.
877
878 Global Instance prod_equiv `{Equiv A,Equiv B} : Equiv (A * B) :=
879 prod_relation (≡) (≡).
880
881 (** Below we make [prod_equiv] type class opaque, so we first lift all
882 instances *)
883 Section prod_setoid.
884 Context `{Equiv A, Equiv B}.
885 Elpi Accumulate TC.Solver lp:{{
886 shorten tc-Corelib.Classes.RelationClasses.{tc-Equivalence}.
887 :after "lastHook"
888 tc-Equivalence A RA R :-
889 RA = {{@equiv _ (@prod_equiv _ _ _ _)}},
890 RA' = {{@prod_relation _ _ _ _}},
891 coq.unify-eq RA RA' ok,
892 % coq.say A RA,
893 tc-Equivalence A RA' R.
894 }}.
895 (* *)
896
897 Global Instance prod_equivalence@{i} (C D: Type@{i}) `{Equiv C, Equiv D}:
898 @Equivalence C (≡@{C}) → @Equivalence D (≡@{D}) → @Equivalence (C * D) (≡@{C * D}) := _.
899
900 Elpi Accumulate TC.Solver lp:{{
901
902 pred remove_equiv_prod_equiv i:term, o:term.
903 remove_equiv_prod_equiv T1 T3 :-
904 T1 = {{@equiv _ (@prod_equiv _ _ _ _)}}, !,
905 T2 = {{@prod_relation lp:F lp:G lp:A lp:B}},
906 coq.unify-eq T1 T2 ok,
907 remove_equiv_prod_equiv A X,
908 remove_equiv_prod_equiv B Y,
909 {{@prod_relation lp:F lp:G lp:X lp:Y}} = T3.
910 remove_equiv_prod_equiv (app L1) (app L2) :- !,
911 std.map L1 remove_equiv_prod_equiv L2.
912 remove_equiv_prod_equiv A A.
913
914 shorten tc-Corelib.Classes.Morphisms.{tc-Proper}.
915
916 :after "lastHook"
917 tc-Proper A B C R :-
918 B = {{ @respectful _ _ _ _ }},
919 remove_equiv_prod_equiv B B1,
920 tc-Proper A B1 C R.
921
922 tc-Proper A {{@respectful lp:K1 lp:K2 lp:B1 (@respectful lp:K3 lp:K4 lp:B2 lp:B3)}} C S :-
923 C1 = {{ @equiv _ _ }},
924 C2 = {{ @equiv _ _ }},
925 C3 = {{ @prod_relation _ _ _ _ }},
926 coq.unify-eq B1 C1 ok,
927 coq.unify-eq B2 C2 ok,
928 coq.unify-eq B3 C3 ok,
929 tc-Proper A {{@respectful lp:K1 lp:K2 lp:C1 (@respectful lp:K3 lp:K4 lp:C2 lp:C3)}} C S.
930
931 }}.
932
933
934 Global Instance pair_proper : Proper ((≡) ==> (≡) ==> (≡@{A*B})) pair := _.
935
936 Elpi Accumulate TC.Solver lp:{{
937 shorten tc-elpi_apps_tc_tests_stdlib.bigTest.{tc-Inj2}.
938 % shorten tc-bigTest.{tc-Inj2}.
939 :after "lastHook"
940 tc-Inj2 A B C RA RB RC F S :-
941 RC = app [global {coq.locate "equiv"} | _],
942 remove_equiv_prod_equiv RC RC',
943 tc-Inj2 A B C RA RB RC' F S.
944 }}.
945
946
947 Global Instance pair_equiv_inj : Inj2 (≡) (≡) (≡@{A*B}) pair := _.
948 Global Instance fst_proper : Proper ((≡@{A*B}) ==> (≡)) fst := _.
949 Global Instance snd_proper : Proper ((≡@{A*B}) ==> (≡)) snd := _.
950
951 Global Instance prod_swap_proper :
952 Proper ((≡@{A*B}) ==> (≡@{B*A})) prod_swap := _.
953
954 Global Instance curry_proper `{Equiv C} :
955 Proper (((≡@{A*B}) ==> (≡@{C})) ==> (≡) ==> (≡) ==> (≡)) curry := _.
956 Global Instance uncurry_proper `{Equiv C} :
957 Proper (((≡) ==> (≡) ==> (≡)) ==> (≡@{A*B}) ==> (≡@{C})) uncurry := _.
958
959 Global Instance curry3_proper `{Equiv C, Equiv D} :
960 Proper (((≡@{A*B*C}) ==> (≡@{D})) ==>
961 (≡) ==> (≡) ==> (≡) ==> (≡)) curry3 := _.
962 Global Instance uncurry3_proper `{Equiv C, Equiv D} :
963 Proper (((≡) ==> (≡) ==> (≡) ==> (≡)) ==>
964 (≡@{A*B*C}) ==> (≡@{D})) uncurry3 := _.
965
966 Global Instance curry4_proper `{Equiv C, Equiv D, Equiv E} :
967 Proper (((≡@{A*B*C*D}) ==> (≡@{E})) ==>
968 (≡) ==> (≡) ==> (≡) ==> (≡) ==> (≡)) curry4 := _.
969 Global Instance uncurry4_proper `{Equiv C, Equiv D, Equiv E} :
970 Proper (((≡) ==> (≡) ==> (≡) ==> (≡) ==> (≡)) ==>
971 (≡@{A*B*C*D}) ==> (≡@{E})) uncurry4 := _.
972
973 Lemma pair_equiv (a1 a2 : A) (b1 b2 : B) :
974 (a1, b1) ≡ (a2, b2) ↔ a1 ≡ a2 ∧ b1 ≡ b2.
975 Proof. reflexivity. Qed.
976 End prod_setoid.
977
978 Global Typeclasses Opaque prod_equiv.
979
980 Global Instance prod_leibniz `{LeibnizEquiv A, LeibnizEquiv B} :
981 LeibnizEquiv (A * B).
982 Proof. intros [??] [??] [??]; f_equal; apply leibniz_equiv; auto. Qed.
983
984 (** ** Sums *)
985 Definition sum_map {A A' B B'} (f: A → A') (g: B → B') (xy : A + B) : A' + B' :=
986 match xy with inl x => inl (f x) | inr y => inr (g y) end.
987 Global Arguments sum_map {_ _ _ _} _ _ !_ / : assert.
988
989 Global Instance sum_inhabited_l {A B} (iA : Inhabited A) : Inhabited (A + B) :=
990 match iA with populate x => populate (inl x) end.
991 Global Instance sum_inhabited_r {A B} (iB : Inhabited B) : Inhabited (A + B) :=
992 match iB with populate y => populate (inr y) end.
993
994 Global Instance inl_inj {A B} : Inj (=) (=) (@inl A B).
995 Proof. injection 1; auto. Qed.
996 Global Instance inr_inj {A B} : Inj (=) (=) (@inr A B).
997 Proof. injection 1; auto. Qed.
998
999 Global Instance sum_map_inj {A A' B B'} (f : A → A') (g : B → B') :
1000 Inj (=) (=) f → Inj (=) (=) g → Inj (=) (=) (sum_map f g).
1001 Proof. intros ?? [?|?] [?|?] [=]; f_equal; apply (inj _); auto. Qed.
1002
1003 Inductive sum_relation {A B}
1004 (RA : relation A) (RB : relation B) : relation (A + B) :=
1005 | inl_related x1 x2 : RA x1 x2 → sum_relation RA RB (inl x1) (inl x2)
1006 | inr_related y1 y2 : RB y1 y2 → sum_relation RA RB (inr y1) (inr y2).
1007
1008 Section sum_relation.
1009 Context `{RA : relation A, RB : relation B}.
1010 Global Instance sum_relation_refl :
1011 Reflexive RA → Reflexive RB → Reflexive (sum_relation RA RB).
1012 Proof. intros ?? [?|?]; constructor; reflexivity. Qed.
1013 Global Instance sum_relation_sym :
1014 Symmetric RA → Symmetric RB → Symmetric (sum_relation RA RB).
1015 Proof. destruct 3; constructor; eauto. Qed.
1016 Global Instance sum_relation_trans :
1017 Transitive RA → Transitive RB → Transitive (sum_relation RA RB).
1018 Proof. destruct 3; inversion_clear 1; constructor; eauto. Qed.
1019 Global Instance sum_relation_equiv :
1020 Equivalence RA → Equivalence RB → Equivalence (sum_relation RA RB).
1021 Proof. split; apply _. Qed.
1022 Global Instance inl_proper' : Proper (RA ==> sum_relation RA RB) inl.
1023 Proof. constructor; auto. Qed.
1024 Global Instance inr_proper' : Proper (RB ==> sum_relation RA RB) inr.
1025 Proof. constructor; auto. Qed.
1026 Global Instance inl_inj' : Inj RA (sum_relation RA RB) inl.
1027 Proof. inversion_clear 1; auto. Qed.
1028 Global Instance inr_inj' : Inj RB (sum_relation RA RB) inr.
1029 Proof. inversion_clear 1; auto. Qed.
1030 End sum_relation.
1031
1032 Global Instance sum_equiv `{Equiv A, Equiv B} : Equiv (A + B) := sum_relation (≡) (≡).
1033
1034 Elpi Accumulate TC.Solver lp:{{
1035 pred remove_equiv_sum_equiv i:term, o:term.
1036 remove_equiv_sum_equiv T1 T3 :-
1037 T1 = {{@equiv _ (@sum_equiv _ _ _ _)}}, !,
1038 T2 = {{@sum_relation lp:F lp:G lp:A lp:B}},
1039 coq.unify-eq T1 T2 ok,
1040 remove_equiv_sum_equiv A X,
1041 remove_equiv_sum_equiv B Y,
1042 {{@sum_relation lp:F lp:G lp:X lp:Y}} = T3.
1043 remove_equiv_sum_equiv (app L1) (app L2) :- !,
1044 std.map L1 remove_equiv_sum_equiv L2.
1045 remove_equiv_sum_equiv A A.
1046
1047 shorten tc-Corelib.Classes.Morphisms.{tc-Proper}.
1048 :after "lastHook"
1049 tc-Proper A B C R :-
1050 B = {{ @respectful _ _ _ _ }},
1051 remove_equiv_sum_equiv B B1,
1052 tc-Proper A B1 C R.
1053 }}.
1054
1055
1056
1057 Global Instance inl_proper `{Equiv A, Equiv B} : Proper ((≡) ==> (≡)) (@inl A B) := _.
1058 Global Instance inr_proper `{Equiv A, Equiv B} : Proper ((≡) ==> (≡)) (@inr A B) := _.
1059
1060
1061 Elpi Accumulate TC.Solver lp:{{
1062 shorten tc-elpi_apps_tc_tests_stdlib.bigTest.{tc-Inj}.
1063 % shorten tc-bigTest.{tc-Inj}.
1064 :after "lastHook"
1065 tc-Inj A B R1 R2 S C :-
1066 R2 = {{@equiv (sum _ _) sum_equiv}},
1067 R2' = {{sum_relation _ _}},
1068 coq.unify-eq R2 R2' ok,
1069 tc-Inj A B R1 R2' S C.
1070 }}.
1071
1072
1073 Global Instance inl_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inl A B) := _.
1074 Global Instance inr_equiv_inj `{Equiv A, Equiv B} : Inj (≡) (≡) (@inr A B) := _.
1075 Global Typeclasses Opaque sum_equiv.
1076
1077 (** ** Option *)
1078 Global Instance option_inhabited {A} : Inhabited (option A) := populate None.
1079
1080 (** ** Sigma types *)
1081 Global Arguments existT {_ _} _ _ : assert.
1082 Global Arguments projT1 {_ _} _ : assert.
1083 Global Arguments projT2 {_ _} _ : assert.
1084
1085 Global Arguments exist {_} _ _ _ : assert.
1086 Global Arguments proj1_sig {_ _} _ : assert.
1087 Global Arguments proj2_sig {_ _} _ : assert.
1088 Notation "x ↾ p" := (exist _ x p) (at level 20) : stdpp_scope.
1089 Notation "` x" := (proj1_sig x) (at level 10, format "` x") : stdpp_scope.
1090
1091 Lemma proj1_sig_inj {A} (P : A → Prop) x (Px : P x) y (Py : P y) :
1092 x↾Px = y↾Py → x = y.
1093 Proof. injection 1; trivial. Qed.
1094
1095 Section sig_map.
1096 Context `{P : A → Prop} `{Q : B → Prop} (f : A → B) (Hf : ∀ x, P x → Q (f x)).
1097 Definition sig_map (x : sig P) : sig Q := f (`x) ↾ Hf _ (proj2_sig x).
1098 Global Instance sig_map_inj:
1099 (∀ x, ProofIrrel (P x)) → Inj (=) (=) f → Inj (=) (=) sig_map.
1100 Proof.
1101 intros ?? [x Hx] [y Hy]. injection 1. intros Hxy.
1102 apply (inj f) in Hxy; subst. rewrite (proof_irrel _ Hy). auto.
1103 Qed.
1104 End sig_map.
1105 Global Arguments sig_map _ _ _ _ _ _ !_ / : assert.
1106
1107 Definition proj1_ex {P : Prop} {Q : P → Prop} (p : ∃ x, Q x) : P :=
1108 let '(ex_intro _ x _) := p in x.
1109 Definition proj2_ex {P : Prop} {Q : P → Prop} (p : ∃ x, Q x) : Q (proj1_ex p) :=
1110 let '(ex_intro _ x H) := p in H.
1111
1112 (** * Operations on sets *)
1113 (** We define operational type classes for the traditional operations and
1114 relations on sets: the empty set [∅], the union [(∪)],
1115 intersection [(∩)], and difference [(∖)], the singleton [{[_]}], the subset
1116 [(⊆)] and element of [(∈)] relation, and disjointess [(##)]. *)
1117 Class Empty A := empty: A.
1118 Global Hint Mode Empty ! : typeclass_instances.
1119 Notation "∅" := empty (format "∅") : stdpp_scope.
1120
1121 Global Instance empty_inhabited `(Empty A) : Inhabited A := populate ∅.
1122
1123 Class Union A := union: A → A → A.
1124 Global Hint Mode Union ! : typeclass_instances.
1125 Global Instance: Params (@union) 2 := {}.
1126 Infix "∪" := union (at level 50, left associativity) : stdpp_scope.
1127 Notation "(∪)" := union (only parsing) : stdpp_scope.
1128 Notation "( x ∪.)" := (union x) (only parsing) : stdpp_scope.
1129 Notation "(.∪ x )" := (λ y, union y x) (only parsing) : stdpp_scope.
1130 Infix "∪*" := (zip_with (∪)) (at level 50, left associativity) : stdpp_scope.
1131 Notation "(∪*)" := (zip_with (∪)) (only parsing) : stdpp_scope.
1132
1133 Definition union_list `{Empty A} `{Union A} : list A → A := fold_right (∪) ∅.
1134 Global Arguments union_list _ _ _ !_ / : assert.
1135 Notation "⋃ l" := (union_list l) (at level 20, format "⋃ l") : stdpp_scope.
1136
1137 Class Intersection A := intersection: A → A → A.
1138 Global Hint Mode Intersection ! : typeclass_instances.
1139 Global Instance: Params (@intersection) 2 := {}.
1140 Infix "∩" := intersection (at level 40) : stdpp_scope.
1141 Notation "(∩)" := intersection (only parsing) : stdpp_scope.
1142 Notation "( x ∩.)" := (intersection x) (only parsing) : stdpp_scope.
1143 Notation "(.∩ x )" := (λ y, intersection y x) (only parsing) : stdpp_scope.
1144
1145 Class Difference A := difference: A → A → A.
1146 Global Hint Mode Difference ! : typeclass_instances.
1147 Global Instance: Params (@difference) 2 := {}.
1148 Infix "∖" := difference (at level 40, left associativity) : stdpp_scope.
1149 Notation "(∖)" := difference (only parsing) : stdpp_scope.
1150 Notation "( x ∖.)" := (difference x) (only parsing) : stdpp_scope.
1151 Notation "(.∖ x )" := (λ y, difference y x) (only parsing) : stdpp_scope.
1152 Infix "∖*" := (zip_with (∖)) (at level 40, left associativity) : stdpp_scope.
1153 Notation "(∖*)" := (zip_with (∖)) (only parsing) : stdpp_scope.
1154
1155 Class Singleton A B := singleton: A → B.
1156 Global Hint Mode Singleton - ! : typeclass_instances.
1157 Global Instance: Params (@singleton) 3 := {}.
1158 Notation "{[ x ]}" := (singleton x) (at level 1) : stdpp_scope.
1159 Notation "{[ x ; y ; .. ; z ]}" :=
1160 (union .. (union (singleton x) (singleton y)) .. (singleton z))
1161 (at level 1) : stdpp_scope.
1162
1163 Class SubsetEq A := subseteq: relation A.
1164 Global Hint Mode SubsetEq ! : typeclass_instances.
1165 Global Instance: Params (@subseteq) 2 := {}.
1166 Infix "⊆" := subseteq (at level 70) : stdpp_scope.
1167 Notation "(⊆)" := subseteq (only parsing) : stdpp_scope.
1168 Notation "( X ⊆.)" := (subseteq X) (only parsing) : stdpp_scope.
1169 Notation "(.⊆ X )" := (λ Y, Y ⊆ X) (only parsing) : stdpp_scope.
1170 Notation "X ⊈ Y" := (¬X ⊆ Y) (at level 70) : stdpp_scope.
1171 Notation "(⊈)" := (λ X Y, X ⊈ Y) (only parsing) : stdpp_scope.
1172 Notation "( X ⊈.)" := (λ Y, X ⊈ Y) (only parsing) : stdpp_scope.
1173 Notation "(.⊈ X )" := (λ Y, Y ⊈ X) (only parsing) : stdpp_scope.
1174
1175 Infix "⊆@{ A }" := (@subseteq A _) (at level 70, only parsing) : stdpp_scope.
1176 Notation "(⊆@{ A } )" := (@subseteq A _) (only parsing) : stdpp_scope.
1177
1178 Infix "⊆*" := (Forall2 (⊆)) (at level 70) : stdpp_scope.
1179 Notation "(⊆*)" := (Forall2 (⊆)) (only parsing) : stdpp_scope.
1180
1181 Global Hint Extern 0 (_ ⊆ _) => reflexivity : core.
1182 Global Hint Extern 0 (_ ⊆* _) => reflexivity : core.
1183
1184 Infix "⊂" := (strict (⊆)) (at level 70) : stdpp_scope.
1185 Notation "(⊂)" := (strict (⊆)) (only parsing) : stdpp_scope.
1186 Notation "( X ⊂.)" := (strict (⊆) X) (only parsing) : stdpp_scope.
1187 Notation "(.⊂ X )" := (λ Y, Y ⊂ X) (only parsing) : stdpp_scope.
1188 Notation "X ⊄ Y" := (¬X ⊂ Y) (at level 70) : stdpp_scope.
1189 Notation "(⊄)" := (λ X Y, X ⊄ Y) (only parsing) : stdpp_scope.
1190 Notation "( X ⊄.)" := (λ Y, X ⊄ Y) (only parsing) : stdpp_scope.
1191 Notation "(.⊄ X )" := (λ Y, Y ⊄ X) (only parsing) : stdpp_scope.
1192
1193 Infix "⊂@{ A }" := (strict (⊆@{A})) (at level 70, only parsing) : stdpp_scope.
1194 Notation "(⊂@{ A } )" := (strict (⊆@{A})) (only parsing) : stdpp_scope.
1195
1196 Notation "X ⊆ Y ⊆ Z" := (X ⊆ Y ∧ Y ⊆ Z) (at level 70, Y at next level) : stdpp_scope.
1197 Notation "X ⊆ Y ⊂ Z" := (X ⊆ Y ∧ Y ⊂ Z) (at level 70, Y at next level) : stdpp_scope.
1198 Notation "X ⊂ Y ⊆ Z" := (X ⊂ Y ∧ Y ⊆ Z) (at level 70, Y at next level) : stdpp_scope.
1199 Notation "X ⊂ Y ⊂ Z" := (X ⊂ Y ∧ Y ⊂ Z) (at level 70, Y at next level) : stdpp_scope.
1200
1201 (** We define type classes for multisets: disjoint union [⊎] and the multiset
1202 singleton [{[+ _ +]}]. Multiset literals [{[+ x1; ..; xn +]}] are defined in
1203 terms of iterated disjoint union [{[+ x1 +]} ⊎ .. ⊎ {[+ xn +]}], and are thus
1204 different from set literals [{[ x1; ..; xn ]}], which use [∪].
1205
1206 Note that in principle we could reuse the set singleton [{[ _ ]}] for multisets,
1207 and define [{[+ x1; ..; xn +]}] as [{[ x1 ]} ⊎ .. ⊎ {[ xn ]}]. However, this
1208 would risk accidentally using [{[ x1; ..; xn ]}] for multisets (leading to
1209 unexpected results) and lead to ambigious pretty printing for [{[+ x +]}]. *)
1210 Class DisjUnion A := disj_union: A → A → A.
1211 Global Hint Mode DisjUnion ! : typeclass_instances.
1212 Global Instance: Params (@disj_union) 2 := {}.
1213 Infix "⊎" := disj_union (at level 50, left associativity) : stdpp_scope.
1214 Notation "(⊎)" := disj_union (only parsing) : stdpp_scope.
1215 Notation "( x ⊎.)" := (disj_union x) (only parsing) : stdpp_scope.
1216 Notation "(.⊎ x )" := (λ y, disj_union y x) (only parsing) : stdpp_scope.
1217
1218 Class SingletonMS A B := singletonMS: A → B.
1219 Global Hint Mode SingletonMS - ! : typeclass_instances.
1220 Global Instance: Params (@singletonMS) 3 := {}.
1221 Notation "{[+ x +]}" := (singletonMS x)
1222 (at level 1, format "{[+ x +]}") : stdpp_scope.
1223 Notation "{[+ x ; y ; .. ; z +]}" :=
1224 (disj_union .. (disj_union (singletonMS x) (singletonMS y)) .. (singletonMS z))
1225 (at level 1, format "{[+ x ; y ; .. ; z +]}") : stdpp_scope.
1226
1227 Definition option_to_set `{Singleton A C, Empty C} (mx : option A) : C :=
1228 match mx with None => ∅ | Some x => {[ x ]} end.
1229 Fixpoint list_to_set `{Singleton A C, Empty C, Union C} (l : list A) : C :=
1230 match l with [] => ∅ | x :: l => {[ x ]} ∪ list_to_set l end.
1231 Fixpoint list_to_set_disj `{SingletonMS A C, Empty C, DisjUnion C} (l : list A) : C :=
1232 match l with [] => ∅ | x :: l => {[+ x +]} ⊎ list_to_set_disj l end.
1233
1234 Class ScalarMul N A := scalar_mul : N → A → A.
1235 Global Hint Mode ScalarMul - ! : typeclass_instances.
1236 (** The [N] arguments is typically [nat] or [Z], so we do not want to rewrite
1237 in that. Hence, the value of [Params] is 3. *)
1238 Global Instance: Params (@scalar_mul) 3 := {}.
1239 (** The notation [*:] and level is taken from ssreflect, see
1240 https://github.com/math-comp/math-comp/blob/master/mathcomp/ssreflect/ssrnotations.v *)
1241 Infix "*:" := scalar_mul (at level 40) : stdpp_scope.
1242 Notation "(*:)" := scalar_mul (only parsing) : stdpp_scope.
1243 Notation "( x *:.)" := (scalar_mul x) (only parsing) : stdpp_scope.
1244 Notation "(.*: x )" := (λ y, scalar_mul y x) (only parsing) : stdpp_scope.
1245
1246 (** The class [Lexico A] is used for the lexicographic order on [A]. This order
1247 is used to create finite maps, finite sets, etc, and is typically different from
1248 the order [(⊆)]. *)
1249 Class Lexico A := lexico: relation A.
1250 Global Hint Mode Lexico ! : typeclass_instances.
1251
1252 Class ElemOf A B := elem_of: A → B → Prop.
1253 Global Hint Mode ElemOf - ! : typeclass_instances.
1254 Global Instance: Params (@elem_of) 3 := {}.
1255 Infix "∈" := elem_of (at level 70) : stdpp_scope.
1256 Notation "(∈)" := elem_of (only parsing) : stdpp_scope.
1257 Notation "( x ∈.)" := (elem_of x) (only parsing) : stdpp_scope.
1258 Notation "(.∈ X )" := (λ x, elem_of x X) (only parsing) : stdpp_scope.
1259 Notation "x ∉ X" := (¬x ∈ X) (at level 80) : stdpp_scope.
1260 Notation "(∉)" := (λ x X, x ∉ X) (only parsing) : stdpp_scope.
1261 Notation "( x ∉.)" := (λ X, x ∉ X) (only parsing) : stdpp_scope.
1262 Notation "(.∉ X )" := (λ x, x ∉ X) (only parsing) : stdpp_scope.
1263
1264 Infix "∈@{ B }" := (@elem_of _ B _) (at level 70, only parsing) : stdpp_scope.
1265 Notation "(∈@{ B } )" := (@elem_of _ B _) (only parsing) : stdpp_scope.
1266
1267 Notation "x ∉@{ B } X" := (¬x ∈@{B} X) (at level 80, only parsing) : stdpp_scope.
1268 Notation "(∉@{ B } )" := (λ x X, x ∉@{B} X) (only parsing) : stdpp_scope.
1269
1270 Class Disjoint A := disjoint : A → A → Prop.
1271 Global Hint Mode Disjoint ! : typeclass_instances.
1272 Global Instance: Params (@disjoint) 2 := {}.
1273 Infix "##" := disjoint (at level 70) : stdpp_scope.
1274 Notation "(##)" := disjoint (only parsing) : stdpp_scope.
1275 Notation "( X ##.)" := (disjoint X) (only parsing) : stdpp_scope.
1276 Notation "(.## X )" := (λ Y, Y ## X) (only parsing) : stdpp_scope.
1277
1278 Infix "##@{ A }" := (@disjoint A _) (at level 70, only parsing) : stdpp_scope.
1279 Notation "(##@{ A } )" := (@disjoint A _) (only parsing) : stdpp_scope.
1280
1281 Infix "##*" := (Forall2 (##)) (at level 70) : stdpp_scope.
1282 Notation "(##*)" := (Forall2 (##)) (only parsing) : stdpp_scope.
1283
1284 Global Hint Extern 0 (_ ## _) => symmetry; eassumption : core.
1285 Global Hint Extern 0 (_ ##* _) => symmetry; eassumption : core.
1286
1287 Class Filter A B := filter: ∀ (P : A → Prop) `{∀ x, Decision (P x)}, B → B.
1288 Global Hint Mode Filter - ! : typeclass_instances.
1289
1290 Class UpClose A B := up_close : A → B.
1291 Global Hint Mode UpClose - ! : typeclass_instances.
1292 Notation "↑ x" := (up_close x) (at level 20, format "↑ x").
1293
1294 (** * Monadic operations *)
1295 (** We define operational type classes for the monadic operations bind, join
1296 and fmap. We use these type classes merely for convenient overloading of
1297 notations and do not formalize any theory on monads (we do not even define a
1298 class with the monad laws). *)
1299 Class MRet (M : Type → Type) := mret: ∀ {A}, A → M A.
1300 Global Arguments mret {_ _ _} _ : assert.
1301 Global Instance: Params (@mret) 3 := {}.
1302 Global Hint Mode MRet ! : typeclass_instances.
1303
1304 Class MBind (M : Type → Type) := mbind : ∀ {A B}, (A → M B) → M A → M B.
1305 Global Arguments mbind {_ _ _ _} _ !_ / : assert.
1306 Global Instance: Params (@mbind) 4 := {}.
1307 Global Hint Mode MBind ! : typeclass_instances.
1308
1309 Class MJoin (M : Type → Type) := mjoin: ∀ {A}, M (M A) → M A.
1310 Global Arguments mjoin {_ _ _} !_ / : assert.
1311 Global Instance: Params (@mjoin) 3 := {}.
1312 Global Hint Mode MJoin ! : typeclass_instances.
1313
1314 Class FMap (M : Type → Type) := fmap : ∀ {A B}, (A → B) → M A → M B.
1315 Global Arguments fmap {_ _ _ _} _ !_ / : assert.
1316 Global Instance: Params (@fmap) 4 := {}.
1317 Global Hint Mode FMap ! : typeclass_instances.
1318
1319 Class OMap (M : Type → Type) := omap: ∀ {A B}, (A → option B) → M A → M B.
1320 Global Arguments omap {_ _ _ _} _ !_ / : assert.
1321 Global Instance: Params (@omap) 4 := {}.
1322 Global Hint Mode OMap ! : typeclass_instances.
1323
1324 Notation "m ≫= f" := (mbind f m) (at level 60, right associativity) : stdpp_scope.
1325 Notation "( m ≫=.)" := (λ f, mbind f m) (only parsing) : stdpp_scope.
1326 Notation "(.≫= f )" := (mbind f) (only parsing) : stdpp_scope.
1327 Notation "(≫=)" := (λ m f, mbind f m) (only parsing) : stdpp_scope.
1328
1329 Notation "x ← y ; z" := (y ≫= (λ x : _, z))
1330 (at level 20, y at level 100, z at level 200, only parsing) : stdpp_scope.
1331
1332 Notation "' x ← y ; z" := (y ≫= (λ x : _, z))
1333 (at level 20, x pattern, y at level 100, z at level 200, only parsing) : stdpp_scope.
1334
1335 Infix "<$>" := fmap (at level 61, left associativity) : stdpp_scope.
1336
1337 Notation "x ;; z" := (x ≫= λ _, z)
1338 (at level 100, z at level 200, only parsing, right associativity): stdpp_scope.
1339
1340 Notation "ps .*1" := (fmap (M:=list) fst ps)
1341 (at level 2, left associativity, format "ps .*1").
1342 Notation "ps .*2" := (fmap (M:=list) snd ps)
1343 (at level 2, left associativity, format "ps .*2").
1344
1345 (** For any monad that has a builtin way to throw an exception/error *)
1346 Class MThrow (E : Type) (M : Type → Type) := mthrow : ∀ {A}, E → M A.
1347 Global Arguments mthrow {_ _ _ _} _ : assert.
1348 Global Instance: Params (@mthrow) 4 := {}.
1349 Global Hint Mode MThrow ! ! : typeclass_instances.
1350
1351 (** We use unit as the error content for monads that can only report an error
1352 without any payload like an option *)
1353 Global Notation MFail := (MThrow ()).
1354 Global Notation mfail := (mthrow ()).
1355
1356 Definition guard_or {E} (e : E) `{MThrow E M, MRet M} P `{Decision P} : M P :=
1357 match decide P with
1358 | left H => mret H
1359 | right _ => mthrow e
1360 end.
1361 Global Notation guard := (guard_or ()).
1362
1363
1364 (** * Operations on maps *)
1365 (** In this section we define operational type classes for the operations
1366 on maps. In the file [fin_maps] we will axiomatize finite maps.
1367 The function look up [m !! k] should yield the element at key [k] in [m]. *)
1368 Class Lookup (K A M : Type) := lookup: K → M → option A.
1369 Global Hint Mode Lookup - - ! : typeclass_instances.
1370 Global Instance: Params (@lookup) 5 := {}.
1371 Notation "m !! i" := (lookup i m) (at level 20) : stdpp_scope.
1372 Notation "(!!)" := lookup (only parsing) : stdpp_scope.
1373 Notation "( m !!.)" := (λ i, m !! i) (only parsing) : stdpp_scope.
1374 Notation "(.!! i )" := (lookup i) (only parsing) : stdpp_scope.
1375 Global Arguments lookup _ _ _ _ !_ !_ / : simpl nomatch, assert.
1376
1377 (** The function [lookup_total] should be the total over-approximation
1378 of the partial [lookup] function. *)
1379 Class LookupTotal (K A M : Type) := lookup_total : K → M → A.
1380 Global Hint Mode LookupTotal - - ! : typeclass_instances.
1381 Global Instance: Params (@lookup_total) 5 := {}.
1382 Notation "m !!! i" := (lookup_total i m) (at level 20) : stdpp_scope.
1383 Notation "(!!!)" := lookup_total (only parsing) : stdpp_scope.
1384 Notation "( m !!!.)" := (λ i, m !!! i) (only parsing) : stdpp_scope.
1385 Notation "(.!!! i )" := (lookup_total i) (only parsing) : stdpp_scope.
1386 Global Arguments lookup_total _ _ _ _ !_ !_ / : simpl nomatch, assert.
1387
1388 (** The singleton map *)
1389 Class SingletonM K A M := singletonM: K → A → M.
1390 Global Hint Mode SingletonM - - ! : typeclass_instances.
1391 Global Instance: Params (@singletonM) 5 := {}.
1392 Notation "{[ k := a ]}" := (singletonM k a) (at level 1) : stdpp_scope.
1393
1394 (** The function insert [<[k:=a]>m] should update the element at key [k] with
1395 value [a] in [m]. *)
1396 Class Insert (K A M : Type) := insert: K → A → M → M.
1397 Global Hint Mode Insert - - ! : typeclass_instances.
1398 Global Instance: Params (@insert) 5 := {}.
1399 Notation "<[ k := a ]>" := (insert k a)
1400 (at level 5, right associativity, format "<[ k := a ]>") : stdpp_scope.
1401 Global Arguments insert _ _ _ _ !_ _ !_ / : simpl nomatch, assert.
1402
1403 (** Notation for more elements (up to 13) *)
1404 (* Defining a generic notation does not seem possible with Coq's
1405 recursive notation system, so we define individual notations
1406 for some cases relevant in practice. *)
1407 (* The "format" makes sure that linebreaks are placed after the separating semicola [;] when printing. *)
1408 (* _ : we are using parantheses in the "de-sugaring" of the notation instead of [$] because Coq 8.12
1409 and earlier have trouble with using the notation for printing otherwise.
1410 Once support for Coq 8.12 is dropped, this can be replaced with [$]. *)
1411 Notation "{[ k1 := a1 ; k2 := a2 ]}" :=
1412 (<[ k1 := a1 ]>{[ k2 := a2 ]})
1413 (at level 1, format
1414 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ']' ']' ]}") : stdpp_scope.
1415 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ]}" :=
1416 (<[ k1 := a1 ]> ( <[ k2 := a2 ]>{[ k3 := a3 ]}))
1417 (at level 1, format
1418 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ']' ']' ]}") : stdpp_scope.
1419 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ]}" :=
1420 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]>{[ k4 := a4 ]})))
1421 (at level 1, format
1422 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ']' ']' ]}") : stdpp_scope.
1423 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ]}" :=
1424 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]>{[ k5 := a5 ]}))))
1425 (at level 1, format
1426 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ']' ']' ]}") : stdpp_scope.
1427 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ]}" :=
1428 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1429 <[ k5 := a5 ]>{[ k6 := a6 ]})))))
1430 (at level 1, format
1431 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ']' ']' ]}") : stdpp_scope.
1432 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ]}" :=
1433 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1434 <[ k5 := a5 ]> ( <[ k6 := a6 ]>{[ k7 := a7 ]}))))))
1435 (at level 1, format
1436 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ']' ']' ]}") : stdpp_scope.
1437 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ]}" :=
1438 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1439 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]>{[ k8 := a8 ]})))))))
1440 (at level 1, format
1441 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ']' ']' ]}") : stdpp_scope.
1442 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ; k9 := a9 ]}" :=
1443 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1444 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]> ( <[ k8 := a8 ]>{[ k9 := a9 ]}))))))))
1445 (at level 1, format
1446 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ; ']' '/' '[' k9 := a9 ']' ']' ]}") : stdpp_scope.
1447 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ; k9 := a9 ; k10 := a10 ]}" :=
1448 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1449 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]> ( <[ k8 := a8 ]> (
1450 <[ k9 := a9 ]>{[ k10 := a10 ]})))))))))
1451 (at level 1, format
1452 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ; ']' '/' '[' k9 := a9 ; ']' '/' '[' k10 := a10 ']' ']' ]}") : stdpp_scope.
1453 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ; k9 := a9 ; k10 := a10 ; k11 := a11 ]}" :=
1454 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1455 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]> ( <[ k8 := a8 ]> (
1456 <[ k9 := a9 ]> ( <[ k10 := a10 ]>{[ k11 := a11 ]}))))))))))
1457 (at level 1, format
1458 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ; ']' '/' '[' k9 := a9 ; ']' '/' '[' k10 := a10 ; ']' '/' '[' k11 := a11 ']' ']' ]}") : stdpp_scope.
1459 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ; k9 := a9 ; k10 := a10 ; k11 := a11 ; k12 := a12 ]}" :=
1460 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1461 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]> ( <[ k8 := a8 ]> (
1462 <[ k9 := a9 ]> ( <[ k10 := a10 ]> ( <[ k11 := a11 ]>{[ k12 := a12 ]})))))))))))
1463 (at level 1, format
1464 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ; ']' '/' '[' k9 := a9 ; ']' '/' '[' k10 := a10 ; ']' '/' '[' k11 := a11 ; ']' '/' '[' k12 := a12 ']' ']' ]}") : stdpp_scope.
1465 Notation "{[ k1 := a1 ; k2 := a2 ; k3 := a3 ; k4 := a4 ; k5 := a5 ; k6 := a6 ; k7 := a7 ; k8 := a8 ; k9 := a9 ; k10 := a10 ; k11 := a11 ; k12 := a12 ; k13 := a13 ]}" :=
1466 (<[ k1 := a1 ]> ( <[ k2 := a2 ]> ( <[ k3 := a3 ]> ( <[ k4 := a4 ]> (
1467 <[ k5 := a5 ]> ( <[ k6 := a6 ]> ( <[ k7 := a7 ]> ( <[ k8 := a8 ]> (
1468 <[ k9 := a9 ]> ( <[ k10 := a10 ]> ( <[ k11 := a11 ]> ( <[ k12 := a12 ]>{[ k13 := a13 ]}))))))))))))
1469 (at level 1, format
1470 "{[ '[hv' '[' k1 := a1 ; ']' '/' '[' k2 := a2 ; ']' '/' '[' k3 := a3 ; ']' '/' '[' k4 := a4 ; ']' '/' '[' k5 := a5 ; ']' '/' '[' k6 := a6 ; ']' '/' '[' k7 := a7 ; ']' '/' '[' k8 := a8 ; ']' '/' '[' k9 := a9 ; ']' '/' '[' k10 := a10 ; ']' '/' '[' k11 := a11 ; ']' '/' '[' k12 := a12 ; ']' '/' '[' k13 := a13 ']' ']' ]}") : stdpp_scope.
1471
1472 (** The function delete [delete k m] should delete the value at key [k] in
1473 [m]. If the key [k] is not a member of [m], the original map should be
1474 returned. *)
1475 Class Delete (K M : Type) := delete: K → M → M.
1476 Global Hint Mode Delete - ! : typeclass_instances.
1477 Global Instance: Params (@delete) 4 := {}.
1478 Global Arguments delete _ _ _ !_ !_ / : simpl nomatch, assert.
1479
1480 (** The function [alter f k m] should update the value at key [k] using the
1481 function [f], which is called with the original value. *)
1482 Class Alter (K A M : Type) := alter: (A → A) → K → M → M.
1483 Global Hint Mode Alter - - ! : typeclass_instances.
1484 Global Instance: Params (@alter) 4 := {}.
1485 Global Arguments alter {_ _ _ _} _ !_ !_ / : simpl nomatch, assert.
1486
1487 (** The function [partial_alter f k m] should update the value at key [k] using the
1488 function [f], which is called with the original value at key [k] or [None]
1489 if [k] is not a member of [m]. The value at [k] should be deleted if [f]
1490 yields [None]. *)
1491 Class PartialAlter (K A M : Type) :=
1492 partial_alter: (option A → option A) → K → M → M.
1493 Global Hint Mode PartialAlter - - ! : typeclass_instances.
1494 Global Instance: Params (@partial_alter) 4 := {}.
1495 Global Arguments partial_alter _ _ _ _ _ !_ !_ / : simpl nomatch, assert.
1496
1497 (** The function [dom m] should yield the domain of [m]. That is a finite
1498 set of type [D] that contains the keys that are a member of [m].
1499 [D] is an output of the typeclass, i.e., there can be only one instance per map
1500 type [M]. *)
1501 Class Dom (M D : Type) := dom: M → D.
1502 Global Hint Mode Dom ! - : typeclass_instances.
1503 Global Instance: Params (@dom) 3 := {}.
1504 Global Arguments dom : clear implicits.
1505 Global Arguments dom {_ _ _} !_ / : simpl nomatch, assert.
1506
1507 (** The function [merge f m1 m2] should merge the maps [m1] and [m2] by
1508 constructing a new map whose value at key [k] is [f (m1 !! k) (m2 !! k)].*)
1509 Class Merge (M : Type → Type) :=
1510 merge: ∀ {A B C}, (option A → option B → option C) → M A → M B → M C.
1511 Global Hint Mode Merge ! : typeclass_instances.
1512 Global Instance: Params (@merge) 4 := {}.
1513 Global Arguments merge _ _ _ _ _ _ !_ !_ / : simpl nomatch, assert.
1514
1515 (** The function [union_with f m1 m2] is supposed to yield the union of [m1]
1516 and [m2] using the function [f] to combine values of members that are in
1517 both [m1] and [m2]. *)
1518 Class UnionWith (A M : Type) :=
1519 union_with: (A → A → option A) → M → M → M.
1520 Global Hint Mode UnionWith - ! : typeclass_instances.
1521 Global Instance: Params (@union_with) 3 := {}.
1522 Global Arguments union_with {_ _ _} _ !_ !_ / : simpl nomatch, assert.
1523
1524 (** Similarly for intersection and difference. *)
1525 Class IntersectionWith (A M : Type) :=
1526 intersection_with: (A → A → option A) → M → M → M.
1527 Global Hint Mode IntersectionWith - ! : typeclass_instances.
1528 Global Instance: Params (@intersection_with) 3 := {}.
1529 Global Arguments intersection_with {_ _ _} _ !_ !_ / : simpl nomatch, assert.
1530
1531 Class DifferenceWith (A M : Type) :=
1532 difference_with: (A → A → option A) → M → M → M.
1533 Global Hint Mode DifferenceWith - ! : typeclass_instances.
1534 Global Instance: Params (@difference_with) 3 := {}.
1535 Global Arguments difference_with {_ _ _} _ !_ !_ / : simpl nomatch, assert.
1536
1537 Definition intersection_with_list `{IntersectionWith A M}
1538 (f : A → A → option A) : M → list M → M := fold_right (intersection_with f).
1539 Global Arguments intersection_with_list _ _ _ _ _ !_ / : assert.
1540
1541 (** * Notations for lattices. *)
1542 (** SqSubsetEq registers the "canonical" partial order for a type, and is used
1543 for the \sqsubseteq symbol. *)
1544 Class SqSubsetEq A := sqsubseteq: relation A.
1545 Global Hint Mode SqSubsetEq ! : typeclass_instances.
1546 Global Instance: Params (@sqsubseteq) 2 := {}.
1547 Infix "⊑" := sqsubseteq (at level 70) : stdpp_scope.
1548 Notation "(⊑)" := sqsubseteq (only parsing) : stdpp_scope.
1549 Notation "( x ⊑.)" := (sqsubseteq x) (only parsing) : stdpp_scope.
1550 Notation "(.⊑ y )" := (λ x, sqsubseteq x y) (only parsing) : stdpp_scope.
1551
1552 Infix "⊑@{ A }" := (@sqsubseteq A _) (at level 70, only parsing) : stdpp_scope.
1553 Notation "(⊑@{ A } )" := (@sqsubseteq A _) (only parsing) : stdpp_scope.
1554
1555 (** [sqsubseteq] does not take precedence over the stdlib's instances (like [eq],
1556 [impl], [iff]) or std++'s [equiv].
1557 We have [eq] (at 100) < [≡] (at 150) < [⊑] (at 200). *)
1558 Global Instance sqsubseteq_rewrite `{SqSubsetEq A} : RewriteRelation (⊑@{A}) | 200 := {}.
1559
1560 Global Hint Extern 0 (_ ⊑ _) => reflexivity : core.
1561
1562 Class Meet A := meet: A → A → A.
1563 Global Hint Mode Meet ! : typeclass_instances.
1564 Global Instance: Params (@meet) 2 := {}.
1565 Infix "⊓" := meet (at level 40) : stdpp_scope.
1566 Notation "(⊓)" := meet (only parsing) : stdpp_scope.
1567 Notation "( x ⊓.)" := (meet x) (only parsing) : stdpp_scope.
1568 Notation "(.⊓ y )" := (λ x, meet x y) (only parsing) : stdpp_scope.
1569
1570 Class Join A := join: A → A → A.
1571 Global Hint Mode Join ! : typeclass_instances.
1572 Global Instance: Params (@join) 2 := {}.
1573 Infix "⊔" := join (at level 50) : stdpp_scope.
1574 Notation "(⊔)" := join (only parsing) : stdpp_scope.
1575 Notation "( x ⊔.)" := (join x) (only parsing) : stdpp_scope.
1576 Notation "(.⊔ y )" := (λ x, join x y) (only parsing) : stdpp_scope.
1577
1578 Class Top A := top : A.
1579 Global Hint Mode Top ! : typeclass_instances.
1580 Notation "⊤" := top (format "⊤") : stdpp_scope.
1581
1582 Class Bottom A := bottom : A.
1583 Global Hint Mode Bottom ! : typeclass_instances.
1584 Notation "⊥" := bottom (format "⊥") : stdpp_scope.
1585
1586
1587 (** * Axiomatization of sets *)
1588 (** The classes [SemiSet A C], [Set_ A C], and [TopSet A C] axiomatize sets of
1589 type [C] with elements of type [A]. The first class, [SemiSet] does not include
1590 intersection and difference. It is useful for the case of lists, where decidable
1591 equality is needed to implement intersection and difference, but not union.
1592
1593 Note that we cannot use the name [Set] since that is a reserved keyword. Hence
1594 we use [Set_]. *)
1595 Class SemiSet A C `{ElemOf A C,
1596 Empty C, Singleton A C, Union C} : Prop := {
1597 not_elem_of_empty (x : A) : x ∉@{C} ∅; (* We prove
1598 [elem_of_empty : x ∈@{C} ∅ ↔ False] in [sets.v], which is more convenient for
1599 rewriting. *)
1600 elem_of_singleton (x y : A) : x ∈@{C} {[ y ]} ↔ x = y;
1601 elem_of_union (X Y : C) (x : A) : x ∈ X ∪ Y ↔ x ∈ X ∨ x ∈ Y
1602 }.
1603 Global Hint Mode SemiSet - ! - - - - : typeclass_instances.
1604
1605 Class Set_ A C `{ElemOf A C, Empty C, Singleton A C,
1606 Union C, Intersection C, Difference C} : Prop := {
1607 set_semi_set :> SemiSet A C;
1608 elem_of_intersection (X Y : C) (x : A) : x ∈ X ∩ Y ↔ x ∈ X ∧ x ∈ Y;
1609 elem_of_difference (X Y : C) (x : A) : x ∈ X ∖ Y ↔ x ∈ X ∧ x ∉ Y
1610 }.
1611 Global Hint Mode Set_ - ! - - - - - - : typeclass_instances.
1612
1613 Class TopSet A C `{ElemOf A C, Empty C, Top C, Singleton A C,
1614 Union C, Intersection C, Difference C} : Prop := {
1615 top_set_set :> Set_ A C;
1616 elem_of_top' (x : A) : x ∈@{C} ⊤; (* We prove [elem_of_top : x ∈@{C} ⊤ ↔ True]
1617 in [sets.v], which is more convenient for rewriting. *)
1618 }.
1619 Global Hint Mode TopSet - ! - - - - - - - : typeclass_instances.
1620
1621 (** We axiomative a finite set as a set whose elements can be
1622 enumerated as a list. These elements, given by the [elements] function, may be
1623 in any order and should not contain duplicates. *)
1624 Class Elements A C := elements: C → list A.
1625 Global Hint Mode Elements - ! : typeclass_instances.
1626 Global Instance: Params (@elements) 3 := {}.
1627
1628 (** We redefine the standard library's [In] and [NoDup] using type classes. *)
1629 Inductive elem_of_list {A} : ElemOf A (list A) :=
1630 | elem_of_list_here (x : A) l : x ∈ x :: l
1631 | elem_of_list_further (x y : A) l : x ∈ l → x ∈ y :: l.
1632 Global Existing Instance elem_of_list.
1633
1634 Lemma elem_of_list_In {A} (l : list A) x : x ∈ l ↔ In x l.
1635 Proof.
1636 split.
1637 - induction 1; simpl; auto.
1638 - induction l; destruct 1; subst; constructor; auto.
1639 Qed.
1640
1641 Inductive NoDup {A} : list A → Prop :=
1642 | NoDup_nil_2 : NoDup []
1643 | NoDup_cons_2 x l : x ∉ l → NoDup l → NoDup (x :: l).
1644
1645 Lemma NoDup_ListNoDup {A} (l : list A) : NoDup l ↔ List.NoDup l.
1646 Proof.
1647 split.
1648 - induction 1; constructor; rewrite <-?elem_of_list_In; auto.
1649 - induction 1; constructor; rewrite ?elem_of_list_In; auto.
1650 Qed.
1651
1652 (** Decidability of equality of the carrier set is admissible, but we add it
1653 anyway so as to avoid cycles in type class search. *)
1654 Class FinSet A C `{ElemOf A C, Empty C, Singleton A C, Union C,
1655 Intersection C, Difference C, Elements A C, EqDecision A} : Prop := {
1656 fin_set_set :> Set_ A C;
1657 elem_of_elements (X : C) x : x ∈ elements X ↔ x ∈ X;
1658 NoDup_elements (X : C) : NoDup (elements X)
1659 }.
1660 Global Hint Mode FinSet - ! - - - - - - - - : typeclass_instances.
1661
1662 Class Size C := size: C → nat.
1663 Global Hint Mode Size ! : typeclass_instances.
1664 Global Arguments size {_ _} !_ / : simpl nomatch, assert.
1665 Global Instance: Params (@size) 2 := {}.
1666
1667 (** The class [MonadSet M] axiomatizes a type constructor [M] that can be
1668 used to construct a set [M A] with elements of type [A]. The advantage
1669 of this class, compared to [Set_], is that it also axiomatizes the
1670 the monadic operations. The disadvantage is that not many inhabitants are
1671 possible: we will only provide as inhabitants [propset] and [listset], which are
1672 represented respectively using Boolean functions and lists with duplicates.
1673
1674 More interesting implementations typically need
1675 decidable equality, or a total order on the elements, which do not fit
1676 in a type constructor of type [Type → Type]. *)
1677 Class MonadSet M `{∀ A, ElemOf A (M A),
1678 ∀ A, Empty (M A), ∀ A, Singleton A (M A), ∀ A, Union (M A),
1679 !MBind M, !MRet M, !FMap M, !MJoin M} : Prop := {
1680 monad_set_semi_set A :> SemiSet A (M A);
1681 elem_of_bind {A B} (f : A → M B) (X : M A) (x : B) :
1682 x ∈ X ≫= f ↔ ∃ y, x ∈ f y ∧ y ∈ X;
1683 elem_of_ret {A} (x y : A) : x ∈@{M A} mret y ↔ x = y;
1684 elem_of_fmap {A B} (f : A → B) (X : M A) (x : B) :
1685 x ∈ f <$> X ↔ ∃ y, x = f y ∧ y ∈ X;
1686 elem_of_join {A} (X : M (M A)) (x : A) :
1687 x ∈ mjoin X ↔ ∃ Y : M A, x ∈ Y ∧ Y ∈ X
1688 }.
1689
1690 (** The [Infinite A] class axiomatizes types [A] with infinitely many elements.
1691 It contains a function [fresh : list A → A] that given a list [xs] gives an
1692 element [fresh xs ∉ xs].
1693
1694 We do not directly make [fresh] a field of the [Infinite] class, but use a
1695 separate operational type class [Fresh] for it. That way we can overload [fresh]
1696 to pick fresh elements from other data structure like sets. See the file
1697 [fin_sets], where we define [fresh : C → A] for any finite set implementation
1698 [FinSet C A].
1699
1700 Note: we require [fresh] to respect permutations, which is needed to define the
1701 aforementioned [fresh] function on finite sets that respects set equality.
1702
1703 Instead of instantiating [Infinite] directly, consider using [max_infinite] or
1704 [inj_infinite] from the [infinite] module. *)
1705 Class Fresh A C := fresh: C → A.
1706 Global Hint Mode Fresh - ! : typeclass_instances.
1707 Global Instance: Params (@fresh) 3 := {}.
1708 Global Arguments fresh : simpl never.
1709
1710 Class Infinite A := {
1711 infinite_fresh :> Fresh A (list A);
1712 infinite_is_fresh (xs : list A) : fresh xs ∉ xs;
1713 infinite_fresh_Permutation :> Proper (@Permutation A ==> (=)) fresh;
1714 }.
1715 Global Hint Mode Infinite ! : typeclass_instances.
1716 Global Arguments infinite_fresh : simpl never.
1717
1718 (** * Miscellaneous *)
1719 Class Half A := half: A → A.
1720 Global Hint Mode Half ! : typeclass_instances.
1721 Notation "½" := half (format "½") : stdpp_scope.
1722 Notation "½*" := (fmap (M:=list) half) : stdpp_scope.