Debian Codegraph @

theories/Corelib/ssr/ssrbool.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 (** #<style> .doc { font-family: monospace; white-space: pre; } </style># **)
14
15 Require Import ssreflect ssrfun.
16
17 (**
18 A theory of boolean predicates and operators. A large part of this file is
19 concerned with boolean reflection.
20 Definitions and notations:
21 is_true b == the coercion of b : bool to Prop (:= b = true).
22 This is just input and displayed as `b''.
23 reflect P b == the reflection inductive predicate, asserting
24 that the logical proposition P : Prop holds iff
25 the formula b : bool is equal to true. Lemmas
26 asserting reflect P b are often referred to as
27 "views".
28 iffP, appP, sameP, rwP :: lemmas for direct manipulation of reflection
29 views: iffP is used to prove reflection from
30 logical equivalence, appP to compose views, and
31 sameP and rwP to perform boolean and setoid
32 rewriting.
33 elimT :: coercion reflect >-> Funclass, which allows the
34 direct application of `reflect' views to
35 boolean assertions.
36 decidable P <-> P is effectively decidable (:= {P} + {~ P}).
37 contra, contraL, ... :: contraposition lemmas.
38 altP my_viewP :: natural alternative for reflection; given
39 lemma myviewP: reflect my_Prop my_formula,
40 have #[#myP | not_myP#]# := altP my_viewP.
41 generates two subgoals, in which my_formula has
42 been replaced by true and false, resp., with
43 new assumptions myP : my_Prop and
44 not_myP: ~~ my_formula.
45 Caveat: my_formula must be an APPLICATION, not
46 a variable, constant, let-in, etc. (due to the
47 poor behaviour of dependent index matching).
48 boolP my_formula :: boolean disjunction, equivalent to
49 altP (idP my_formula) but circumventing the
50 dependent index capture issue; destructing
51 boolP my_formula generates two subgoals with
52 assumptions my_formula and ~~ my_formula. As
53 with altP, my_formula must be an application.
54 \unless C, P <-> we can assume property P when a something that
55 holds under condition C (such as C itself).
56 := forall G : Prop, (C -> G) -> (P -> G) -> G.
57 This is just C \/ P or rather its impredicative
58 encoding, whose usage better fits the above
59 description: given a lemma UCP whose conclusion
60 is \unless C, P we can assume P by writing:
61 wlog hP: / P by apply/UCP; (prove C -> goal).
62 or even apply: UCP id _ => hP if the goal is C.
63 classically P <-> we can assume P when proving is_true b.
64 := forall b : bool, (P -> b) -> b.
65 This is equivalent to ~ (~ P) when P : Prop.
66 implies P Q == wrapper variant type that coerces to P -> Q and
67 can be used as a P -> Q view unambiguously.
68 Useful to avoid spurious insertion of <-> views
69 when Q is a conjunction of foralls, as in Lemma
70 all_and2 below; conversely, avoids confusion in
71 apply views for impredicative properties, such
72 as \unless C, P. Also supports contrapositives.
73 a && b == the boolean conjunction of a and b.
74 a || b == the boolean disjunction of a and b.
75 a ==> b == the boolean implication of b by a.
76 ~~ a == the boolean negation of a.
77 a (+) b == the boolean exclusive or (or sum) of a and b.
78 #[# /\ P1 , P2 & P3 #]# == multiway logical conjunction, up to 5 terms.
79 #[# \/ P1 , P2 | P3 #]# == multiway logical disjunction, up to 4 terms.
80 #[#&& a, b, c & d#]# == iterated, right associative boolean conjunction
81 with arbitrary arity.
82 #[#|| a, b, c | d#]# == iterated, right associative boolean disjunction
83 with arbitrary arity.
84 #[#==> a, b, c => d#]# == iterated, right associative boolean implication
85 with arbitrary arity.
86 and3P, ... == specific reflection lemmas for iterated
87 connectives.
88 andTb, orbAC, ... == systematic names for boolean connective
89 properties (see suffix conventions below).
90 prop_congr == a tactic to move a boolean equality from
91 its coerced form in Prop to the equality
92 in bool.
93 bool_congr == resolution tactic for blindly weeding out
94 like terms from boolean equalities (can fail).
95 This file provides a theory of boolean predicates and relations:
96 pred T == the type of bool predicates (:= T -> bool).
97 simpl_pred T == the type of simplifying bool predicates, based on
98 the simpl_fun type from ssrfun.v.
99 mem_pred T == a specialized form of simpl_pred for "collective"
100 predicates (see below).
101 rel T == the type of bool relations.
102 := T -> pred T or T -> T -> bool.
103 simpl_rel T == type of simplifying relations.
104 := T -> simpl_pred T
105 predType == the generic predicate interface, supported for
106 for lists and sets.
107 pred_sort == the predType >-> Type projection; pred_sort is
108 itself a Coercion target class. Declaring a
109 coercion to pred_sort is an alternative way of
110 equipping a type with a predType structure, which
111 interoperates better with coercion subtyping.
112 This is used, e.g., for finite sets, so that finite
113 groups inherit the membership operation by
114 coercing to sets.
115 {pred T} == a type convertible to pred T, but whose head
116 constant is pred_sort. This type should be used
117 for parameters that can be used as collective
118 predicates (see below), as this will allow passing
119 in directly collections that implement predType
120 by coercion as described above, e.g., finite sets.
121 := pred_sort (predPredType T)
122 If P is a predicate the proposition "x satisfies P" can be written
123 applicatively as (P x), or using an explicit connective as (x \in P); in
124 the latter case we say that P is a "collective" predicate. We use A, B
125 rather than P, Q for collective predicates:
126 x \in A == x satisfies the (collective) predicate A.
127 x \notin A == x doesn't satisfy the (collective) predicate A.
128 The pred T type can be used as a generic predicate type for either kind,
129 but the two kinds of predicates should not be confused. When a "generic"
130 pred T value of one type needs to be passed as the other the following
131 conversions should be used explicitly:
132 SimplPred P == a (simplifying) applicative equivalent of P.
133 mem A == an applicative equivalent of collective predicate A:
134 mem A x simplifies to x \in A, as mem A has in
135 fact type mem_pred T.
136 --> In user notation collective predicates _only_ occur as arguments to mem:
137 A only appears as (mem A). This is hidden by notation, e.g.,
138 x \in A := in_mem x (mem A) here, enum A := enum_mem (mem A) in fintype.
139 This makes it possible to unify the various ways in which A can be
140 interpreted as a predicate, for both pattern matching and display.
141 Alternatively one can use the syntax for explicit simplifying predicates
142 and relations (in the following x is bound in E):
143 #[#pred x | E#]# == simplifying (see ssrfun) predicate x => E.
144 #[#pred x : T | E#]# == predicate x => E, with a cast on the argument.
145 #[#pred : T | P#]# == constant predicate P on type T.
146 #[#pred x | E1 & E2#]# == #[#pred x | E1 && E2#]#; an x : T cast is allowed.
147 #[#pred x in A#]# == #[#pred x | x in A#]#.
148 #[#pred x in A | E#]# == #[#pred x | x in A & E#]#.
149 #[#pred x in A | E1 & E2#]# == #[#pred x in A | E1 && E2#]#.
150 #[#predU A & B#]# == union of two collective predicates A and B.
151 #[#predI A & B#]# == intersection of collective predicates A and B.
152 #[#predD A & B#]# == difference of collective predicates A and B.
153 #[#predC A#]# == complement of the collective predicate A.
154 #[#preim f of A#]# == preimage under f of the collective predicate A.
155 predU P Q, ..., preim f P == union, etc of applicative predicates.
156 pred_oapp A == the predicate A lifted to the option type
157 := #[#pred x | oapp (mem A) false x#]#.
158 pred0 == the empty predicate.
159 predT == the total (always true) predicate.
160 if T : predArgType, then T coerces to predT.
161 {: T} == T cast to predArgType (e.g., {: bool * nat}).
162 In the following, x and y are bound in E:
163 #[#rel x y | E#]# == simplifying relation x, y => E.
164 #[#rel x y : T | E#]# == simplifying relation with arguments cast.
165 #[#rel x y in A & B | E#]# == #[#rel x y | #[#&& x \in A, y \in B & E#]# #]#.
166 #[#rel x y in A & B#]# == #[#rel x y | (x \in A) && (y \in B) #]#.
167 #[#rel x y in A | E#]# == #[#rel x y in A & A | E#]#.
168 #[#rel x y in A#]# == #[#rel x y in A & A#]#.
169 relU R S == union of relations R and S.
170 relpre f R == preimage of relation R under f.
171 xpredU, ..., xrelpre == lambda terms implementing predU, ..., etc.
172 Explicit values of type pred T (i.e., lamdba terms) should always be used
173 applicatively, while values of collection types implementing the predType
174 interface, such as sequences or sets should always be used as collective
175 predicates. Defined constants and functions of type pred T or simpl_pred T
176 as well as the explicit simpl_pred T values described below, can generally
177 be used either way. Note however that x \in A will not auto-simplify when
178 A is an explicit simpl_pred T value; the generic simplification rule inE
179 must be used (when A : pred T, the unfold_in rule can be used). Constants
180 of type pred T with an explicit simpl_pred value do not auto-simplify when
181 used applicatively, but can still be expanded with inE. This behavior can
182 be controlled as follows:
183 Let A : collective_pred T := #[#pred x | ... #]#.
184 The collective_pred T type is just an alias for pred T, but this cast
185 stops rewrite inE from expanding the definition of A, thus treating A
186 into an abstract collection (unfold_in or in_collective can be used to
187 expand manually).
188 Let A : applicative_pred T := #[#pred x | ... #]#.
189 This cast causes inE to turn x \in A into the applicative A x form;
190 A will then have to be unfolded explicitly with the /A rule. This will
191 also apply to any definition that reduces to A (e.g., Let B := A).
192 Canonical A_app_pred := ApplicativePred A.
193 This declaration, given after definition of A, similarly causes inE to
194 turn x \in A into A x, but in addition allows the app_predE rule to
195 turn A x back into x \in A; it can be used for any definition of type
196 pred T, which makes it especially useful for ambivalent predicates
197 as the relational transitive closure connect, that are used in both
198 applicative and collective styles.
199 Purely for aesthetics, we provide a subtype of collective predicates:
200 qualifier q T == a pred T pretty-printing wrapper. An A : qualifier q T
201 coerces to pred_sort and thus behaves as a collective
202 predicate, but x \in A and x \notin A are displayed as:
203 x \is A and x \isn't A when q = 0,
204 x \is a A and x \isn't a A when q = 1,
205 x \is an A and x \isn't an A when q = 2, respectively.
206 #[#qualify x | P#]# := Qualifier 0 (fun x => P), constructor for the above.
207 #[#qualify x : T | P#]#, #[#qualify a x | P#]#, #[#qualify an X | P#]#, etc.
208 variants of the above with type constraints and different
209 values of q.
210 We provide an internal interface to support attaching properties (such as
211 being multiplicative) to predicates:
212 pred_key p == phantom type that will serve as a support for properties
213 to be attached to p : {pred _}; instances should be
214 created with Fact/Qed so as to be opaque.
215 KeyedPred k_p == an instance of the interface structure that attaches
216 (k_p : pred_key P) to P; the structure projection is a
217 coercion to pred_sort.
218 KeyedQualifier k_q == an instance of the interface structure that attaches
219 (k_q : pred_key q) to (q : qualifier n T).
220 DefaultPredKey p == a default value for pred_key p; the vernacular command
221 Import DefaultKeying attaches this key to all predicates
222 that are not explicitly keyed.
223 Keys can be used to attach properties to predicates, qualifiers and
224 generic nouns in a way that allows them to be used transparently. The key
225 projection of a predicate property structure such as unsignedPred should
226 be a pred_key, not a pred, and corresponding lemmas will have the form
227 Lemma rpredN R S (oppS : @opprPred R S) (kS : keyed_pred oppS) :
228 {mono -%%R: x / x \in kS}.
229 Because x \in kS will be displayed as x \in S (or x \is S, etc), the
230 canonical instance of opprPred will not normally be exposed (it will also
231 be erased by /= simplification). In addition each predicate structure
232 should have a DefaultPredKey Canonical instance that simply issues the
233 property as a proof obligation (which can be caught by the Prop-irrelevant
234 feature of the ssreflect plugin).
235 Some properties of predicates and relations:
236 A =i B <-> A and B are extensionally equivalent.
237 {subset A <= B} <-> A is a (collective) subpredicate of B.
238 subpred P Q <-> P is an (applicative) subpredicate or Q.
239 subrel R S <-> R is a subrelation of S.
240 In the following R is in rel T:
241 reflexive R <-> R is reflexive.
242 irreflexive R <-> R is irreflexive.
243 symmetric R <-> R (in rel T) is symmetric (equation).
244 pre_symmetric R <-> R is symmetric (implication).
245 antisymmetric R <-> R is antisymmetric.
246 total R <-> R is total.
247 transitive R <-> R is transitive.
248 left_transitive R <-> R is a congruence on its left hand side.
249 right_transitive R <-> R is a congruence on its right hand side.
250 equivalence_rel R <-> R is an equivalence relation.
251 Localization of (Prop) predicates; if P1 is convertible to forall x, Qx,
252 P2 to forall x y, Qxy and P3 to forall x y z, Qxyz :
253 {for y, P1} <-> Qx{y / x}.
254 {in A, P1} <-> forall x, x \in A -> Qx.
255 {in A1 & A2, P2} <-> forall x y, x \in A1 -> y \in A2 -> Qxy.
256 {in A &, P2} <-> forall x y, x \in A -> y \in A -> Qxy.
257 {in A1 & A2 & A3, Q3} <-> forall x y z,
258 x \in A1 -> y \in A2 -> z \in A3 -> Qxyz.
259 {in A1 & A2 &, Q3} := {in A1 & A2 & A2, Q3}.
260 {in A1 && A3, Q3} := {in A1 & A1 & A3, Q3}.
261 {in A &&, Q3} := {in A & A & A, Q3}.
262 {in A, bijective f} <-> f has a right inverse in A.
263 {on C, P1} <-> forall x, (f x) \in C -> Qx
264 when P1 is also convertible to Pf f, e.g.,
265 {on C, involutive f}.
266 {on C &, P2} == forall x y, f x \in C -> f y \in C -> Qxy
267 when P2 is also convertible to Pf f, e.g.,
268 {on C &, injective f}.
269 {on C, P1' & g} == forall x, (f x) \in cd -> Qx
270 when P1' is convertible to Pf f
271 and P1' g is convertible to forall x, Qx, e.g.,
272 {on C, cancel f & g}.
273 {on C, bijective f} == f has a right inverse on C.
274 This file extends the lemma name suffix conventions of ssrfun as follows:
275 A -- associativity, as in andbA : associative andb.
276 AC -- right commutativity.
277 ACA -- self-interchange (inner commutativity), e.g.,
278 orbACA : (a || b) || (c || d) = (a || c) || (b || d).
279 b -- a boolean argument, as in andbb : idempotent_op andb.
280 C -- commutativity, as in andbC : commutative andb,
281 or predicate complement, as in predC.
282 CA -- left commutativity.
283 D -- predicate difference, as in predD.
284 E -- elimination, as in negbFE : ~~ b = false -> b.
285 F or f -- boolean false, as in andbF : b && false = false.
286 I -- left/right injectivity, as in addbI : right_injective addb,
287 or predicate intersection, as in predI.
288 l -- a left-hand operation, as andb_orl : left_distributive andb orb.
289 N or n -- boolean negation, as in andbN : a && (~~ a) = false.
290 P -- a characteristic property, often a reflection lemma, as in
291 andP : reflect (a /\ b) (a && b).
292 r -- a right-hand operation, as orb_andr : right_distributive orb andb.
293 T or t -- boolean truth, as in andbT: right_id true andb.
294 U -- predicate union, as in predU.
295 W -- weakening, as in in1W : (forall x, P) -> {in D, forall x, P}. **)
296
297
298 Set Implicit Arguments.
299 Unset Strict Implicit.
300 Unset Printing Implicit Defensive.
301
302 Abbreviation reflect := Datatypes.reflect.
303 Abbreviation ReflectT := Datatypes.ReflectT.
304 Abbreviation ReflectF := Datatypes.ReflectF.
305
306 Reserved Notation "~~ b" (at level 35, right associativity).
307 Reserved Notation "b ==> c" (at level 55, right associativity).
308 Reserved Notation "b1 (+) b2" (at level 50, left associativity).
309
310 Reserved Notation "x \in A" (at level 70, no associativity,
311 format "'[hv' x '/ ' \in A ']'").
312 Reserved Notation "x \notin A" (at level 70, no associativity,
313 format "'[hv' x '/ ' \notin A ']'").
314 Reserved Notation "x \is A" (at level 70, no associativity,
315 format "'[hv' x '/ ' \is A ']'").
316 Reserved Notation "x \isn't A" (at level 70, no associativity,
317 format "'[hv' x '/ ' \isn't A ']'").
318 Reserved Notation "x \is 'a' A" (at level 70, no associativity,
319 format "'[hv' x '/ ' \is 'a' A ']'").
320 Reserved Notation "x \isn't 'a' A" (at level 70, no associativity,
321 format "'[hv' x '/ ' \isn't 'a' A ']'").
322 Reserved Notation "x \is 'an' A" (at level 70, no associativity,
323 format "'[hv' x '/ ' \is 'an' A ']'").
324 Reserved Notation "x \isn't 'an' A" (at level 70, no associativity,
325 format "'[hv' x '/ ' \isn't 'an' A ']'").
326 Reserved Notation "p1 =i p2" (at level 70, no associativity,
327 format "'[hv' p1 '/ ' =i p2 ']'").
328 Reserved Notation "{ 'subset' A <= B }" (at level 0, A, B at level 69,
329 format "'[hv' { 'subset' A '/ ' <= B } ']'").
330
331 Reserved Notation "{ : T }" (at level 0, format "{ : T }").
332 Reserved Notation "{ 'pred' T }" (at level 0, format "{ 'pred' T }").
333 Reserved Notation "[ 'predType' 'of' T ]" (at level 0,
334 format "[ 'predType' 'of' T ]").
335
336 Reserved Notation "[ 'pred' : T | E ]" (at level 0,
337 format "'[hv' [ 'pred' : T | '/ ' E ] ']'").
338 Reserved Notation "[ 'pred' x | E ]" (at level 0, x name,
339 format "'[hv' [ 'pred' x | '/ ' E ] ']'").
340 Reserved Notation "[ 'pred' x : T | E ]" (at level 0, x name,
341 format "'[hv' [ 'pred' x : T | '/ ' E ] ']'").
342 Reserved Notation "[ 'pred' x | E1 & E2 ]" (at level 0, x name,
343 format "'[hv' [ 'pred' x | '/ ' E1 & '/ ' E2 ] ']'").
344 Reserved Notation "[ 'pred' x : T | E1 & E2 ]" (at level 0, x name,
345 format "'[hv' [ 'pred' x : T | '/ ' E1 & E2 ] ']'").
346 Reserved Notation "[ 'pred' x 'in' A ]" (at level 0, x name,
347 format "'[hv' [ 'pred' x 'in' A ] ']'").
348 Reserved Notation "[ 'pred' x 'in' A | E ]" (at level 0, x name,
349 format "'[hv' [ 'pred' x 'in' A | '/ ' E ] ']'").
350 Reserved Notation "[ 'pred' x 'in' A | E1 & E2 ]" (at level 0, x name,
351 format "'[hv' [ 'pred' x 'in' A | '/ ' E1 & '/ ' E2 ] ']'").
352
353 Reserved Notation "[ 'qualify' x | P ]" (at level 0, x at level 99,
354 format "'[hv' [ 'qualify' x | '/ ' P ] ']'").
355 Reserved Notation "[ 'qualify' x : T | P ]" (at level 0, x at level 99,
356 format "'[hv' [ 'qualify' x : T | '/ ' P ] ']'").
357 Reserved Notation "[ 'qualify' 'a' x | P ]" (at level 0, x at level 99,
358 format "'[hv' [ 'qualify' 'a' x | '/ ' P ] ']'").
359 Reserved Notation "[ 'qualify' 'a' x : T | P ]" (at level 0, x at level 99,
360 format "'[hv' [ 'qualify' 'a' x : T | '/ ' P ] ']'").
361 Reserved Notation "[ 'qualify' 'an' x | P ]" (at level 0, x at level 99,
362 format "'[hv' [ 'qualify' 'an' x | '/ ' P ] ']'").
363 Reserved Notation "[ 'qualify' 'an' x : T | P ]" (at level 0, x at level 99,
364 format "'[hv' [ 'qualify' 'an' x : T | '/ ' P ] ']'").
365
366 Reserved Notation "[ 'rel' x y | E ]" (at level 0, x name, y name,
367 format "'[hv' [ 'rel' x y | '/ ' E ] ']'").
368 Reserved Notation "[ 'rel' x y : T | E ]" (at level 0, x name, y name,
369 format "'[hv' [ 'rel' x y : T | '/ ' E ] ']'").
370 Reserved Notation "[ 'rel' x y 'in' A & B | E ]" (at level 0, x name, y name,
371 format "'[hv' [ 'rel' x y 'in' A & B | '/ ' E ] ']'").
372 Reserved Notation "[ 'rel' x y 'in' A & B ]" (at level 0, x name, y name,
373 format "'[hv' [ 'rel' x y 'in' A & B ] ']'").
374 Reserved Notation "[ 'rel' x y 'in' A | E ]" (at level 0, x name, y name,
375 format "'[hv' [ 'rel' x y 'in' A | '/ ' E ] ']'").
376 Reserved Notation "[ 'rel' x y 'in' A ]" (at level 0, x name, y name,
377 format "'[hv' [ 'rel' x y 'in' A ] ']'").
378
379 Reserved Notation "[ 'mem' A ]" (at level 0, format "[ 'mem' A ]").
380 Reserved Notation "[ 'predI' A & B ]" (at level 0,
381 format "[ 'predI' A & B ]").
382 Reserved Notation "[ 'predU' A & B ]" (at level 0,
383 format "[ 'predU' A & B ]").
384 Reserved Notation "[ 'predD' A & B ]" (at level 0,
385 format "[ 'predD' A & B ]").
386 Reserved Notation "[ 'predC' A ]" (at level 0,
387 format "[ 'predC' A ]").
388 Reserved Notation "[ 'preim' f 'of' A ]" (at level 0,
389 format "[ 'preim' f 'of' A ]").
390
391 Reserved Notation "\unless C , P" (at level 200, C at level 100,
392 format "'[hv' \unless C , '/ ' P ']'").
393
394 Reserved Notation "{ 'for' x , P }" (at level 0,
395 format "'[hv' { 'for' x , '/ ' P } ']'").
396 Reserved Notation "{ 'in' d , P }" (at level 0,
397 format "'[hv' { 'in' d , '/ ' P } ']'").
398 Reserved Notation "{ 'in' d1 & d2 , P }" (at level 0,
399 format "'[hv' { 'in' d1 & d2 , '/ ' P } ']'").
400 Reserved Notation "{ 'in' d & , P }" (at level 0,
401 format "'[hv' { 'in' d & , '/ ' P } ']'").
402 Reserved Notation "{ 'in' d1 & d2 & d3 , P }" (at level 0,
403 format "'[hv' { 'in' d1 & d2 & d3 , '/ ' P } ']'").
404 Reserved Notation "{ 'in' d1 & & d3 , P }" (at level 0,
405 format "'[hv' { 'in' d1 & & d3 , '/ ' P } ']'").
406 Reserved Notation "{ 'in' d1 & d2 & , P }" (at level 0,
407 format "'[hv' { 'in' d1 & d2 & , '/ ' P } ']'").
408 Reserved Notation "{ 'in' d & & , P }" (at level 0,
409 format "'[hv' { 'in' d & & , '/ ' P } ']'").
410 Reserved Notation "{ 'on' cd , P }" (at level 0,
411 format "'[hv' { 'on' cd , '/ ' P } ']'").
412 Reserved Notation "{ 'on' cd & , P }" (at level 0,
413 format "'[hv' { 'on' cd & , '/ ' P } ']'").
414 Reserved Notation "{ 'on' cd , P & g }" (at level 0, g at level 8,
415 format "'[hv' { 'on' cd , '/ ' P & g } ']'").
416 Reserved Notation "{ 'in' d , 'bijective' f }" (at level 0, f at level 8,
417 format "'[hv' { 'in' d , '/ ' 'bijective' f } ']'").
418 Reserved Notation "{ 'on' cd , 'bijective' f }" (at level 0, f at level 8,
419 format "'[hv' { 'on' cd , '/ ' 'bijective' f } ']'").
420
421
422 (**
423 We introduce a number of n-ary "list-style" notations that share a common
424 format, namely
425 #[#op arg1, arg2, ... last_separator last_arg#]#
426 This usually denotes a right-associative applications of op, e.g.,
427 #[#&& a, b, c & d#]# denotes a && (b && (c && d))
428 The last_separator must be a non-operator token. Here we use &, | or =>;
429 our default is &, but we try to match the intended meaning of op. The
430 separator is a workaround for limitations of the parsing engine; the same
431 limitations mean the separator cannot be omitted even when last_arg can.
432 The Abbreviation declarations are complicated by the separate treatment for
433 some fixed arities (binary for bool operators, and all arities for Prop
434 operators).
435 We also use the square brackets in comprehension-style notations
436 #[#type var separator expr#]#
437 where "type" is the type of the comprehension (e.g., pred) and "separator"
438 is | or => . It is important that in other notations a leading square
439 bracket #[# is always followed by an operator symbol or a fixed identifier. **)
440
441 Reserved Notation "[ /\ P1 & P2 ]" (at level 0).
442 Reserved Notation "[ /\ P1 , P2 & P3 ]" (at level 0, format
443 "'[hv' [ /\ '[' P1 , '/' P2 ']' '/ ' & P3 ] ']'").
444 Reserved Notation "[ /\ P1 , P2 , P3 & P4 ]" (at level 0, format
445 "'[hv' [ /\ '[' P1 , '/' P2 , '/' P3 ']' '/ ' & P4 ] ']'").
446 Reserved Notation "[ /\ P1 , P2 , P3 , P4 & P5 ]" (at level 0, format
447 "'[hv' [ /\ '[' P1 , '/' P2 , '/' P3 , '/' P4 ']' '/ ' & P5 ] ']'").
448
449 Reserved Notation "[ \/ P1 | P2 ]" (at level 0).
450 Reserved Notation "[ \/ P1 , P2 | P3 ]" (at level 0, format
451 "'[hv' [ \/ '[' P1 , '/' P2 ']' '/ ' | P3 ] ']'").
452 Reserved Notation "[ \/ P1 , P2 , P3 | P4 ]" (at level 0, format
453 "'[hv' [ \/ '[' P1 , '/' P2 , '/' P3 ']' '/ ' | P4 ] ']'").
454
455 Reserved Notation "[ && b1 & c ]" (at level 0).
456 Reserved Notation "[ && b1 , b2 , .. , bn & c ]" (at level 0, format
457 "'[hv' [ && '[' b1 , '/' b2 , '/' .. , '/' bn ']' '/ ' & c ] ']'").
458
459 Reserved Notation "[ || b1 | c ]" (at level 0).
460 Reserved Notation "[ || b1 , b2 , .. , bn | c ]" (at level 0, format
461 "'[hv' [ || '[' b1 , '/' b2 , '/' .. , '/' bn ']' '/ ' | c ] ']'").
462
463 Reserved Notation "[ ==> b1 => c ]" (at level 0).
464 Reserved Notation "[ ==> b1 , b2 , .. , bn => c ]" (at level 0, format
465 "'[hv' [ ==> '[' b1 , '/' b2 , '/' .. , '/' bn ']' '/' => c ] ']'").
466
467 (** Shorter delimiter **)
468 Delimit Scope bool_scope with B.
469 Open Scope bool_scope.
470
471 (** An alternative to xorb that behaves somewhat better wrt simplification. **)
472 Definition addb b := if b then negb else id.
473
474 (** Abbreviation for && and || is declared in Init.Datatypes. **)
475 Notation "~~ b" := (negb b) : bool_scope.
476 Notation "b ==> c" := (implb b c) : bool_scope.
477 Notation "b1 (+) b2" := (addb b1 b2) : bool_scope.
478
479 (** Constant is_true b := b = true is defined in Init.Datatypes. **)
480 Coercion is_true : bool >-> Sortclass. (* Prop *)
481
482 Lemma prop_congr : forall b b' : bool, b = b' -> b = b' :> Prop.
483 Proof. by move=> b b' ->. Qed.
484
485 Ltac prop_congr := apply: prop_congr.
486
487 (** Lemmas for trivial. **)
488 Lemma is_true_true : true. Proof. by []. Qed.
489 Lemma not_false_is_true : ~ false. Proof. by []. Qed.
490 Lemma is_true_locked_true : locked true. Proof. by unlock. Qed.
491 #[global]
492 Hint Resolve is_true_true not_false_is_true is_true_locked_true : core.
493
494 (** Shorter names. **)
495 Definition isT := is_true_true.
496 Definition notF := not_false_is_true.
497
498 (** Negation lemmas. **)
499
500 (**
501 We generally take NEGATION as the standard form of a false condition:
502 negative boolean hypotheses should be of the form ~~ b, rather than ~ b or
503 b = false, as much as possible. **)
504
505 Lemma negbT b : b = false -> ~~ b. Proof. by case: b. Qed.
506 Lemma negbTE b : ~~ b -> b = false. Proof. by case: b. Qed.
507 Lemma negbF b : (b : bool) -> ~~ b = false. Proof. by case: b. Qed.
508 Lemma negbFE b : ~~ b = false -> b. Proof. by case: b. Qed.
509 Lemma negbK : involutive negb. Proof. by case. Qed.
510 Lemma negbNE b : ~~ ~~ b -> b. Proof. by case: b. Qed.
511
512 Lemma negb_inj : injective negb. Proof. exact: can_inj negbK. Qed.
513 Lemma negbLR b c : b = ~~ c -> ~~ b = c. Proof. exact: canLR negbK. Qed.
514 Lemma negbRL b c : ~~ b = c -> b = ~~ c. Proof. exact: canRL negbK. Qed.
515
516 Lemma contra (c b : bool) : (c -> b) -> ~~ b -> ~~ c.
517 Proof. by case: b => //; case: c. Qed.
518 Definition contraNN := contra.
519
520 Lemma contraL (c b : bool) : (c -> ~~ b) -> b -> ~~ c.
521 Proof. by case: b => //; case: c. Qed.
522 Definition contraTN := contraL.
523
524 Lemma contraR (c b : bool) : (~~ c -> b) -> ~~ b -> c.
525 Proof. by case: b => //; case: c. Qed.
526 Definition contraNT := contraR.
527
528 Lemma contraLR (c b : bool) : (~~ c -> ~~ b) -> b -> c.
529 Proof. by case: b => //; case: c. Qed.
530 Definition contraTT := contraLR.
531
532 Lemma contraT b : (~~ b -> false) -> b. Proof. by case: b => // ->. Qed.
533
534 Lemma wlog_neg b : (~~ b -> b) -> b. Proof. by case: b => // ->. Qed.
535
536 Lemma contraFT (c b : bool) : (~~ c -> b) -> b = false -> c.
537 Proof. by move/contraR=> notb_c /negbT. Qed.
538
539 Lemma contraFN (c b : bool) : (c -> b) -> b = false -> ~~ c.
540 Proof. by move/contra=> notb_notc /negbT. Qed.
541
542 Lemma contraTF (c b : bool) : (c -> ~~ b) -> b -> c = false.
543 Proof. by move/contraL=> b_notc /b_notc/negbTE. Qed.
544
545 Lemma contraNF (c b : bool) : (c -> b) -> ~~ b -> c = false.
546 Proof. by move/contra=> notb_notc /notb_notc/negbTE. Qed.
547
548 Lemma contraFF (c b : bool) : (c -> b) -> b = false -> c = false.
549 Proof. by move/contraFN=> bF_notc /bF_notc/negbTE. Qed.
550
551 (* additional contra lemmas involving [P,Q : Prop] *)
552 Lemma contra_not (P Q : Prop) : (Q -> P) -> (~ P -> ~ Q). Proof. by auto. Qed.
553
554 Lemma contraPnot (P Q : Prop) : (Q -> ~ P) -> (P -> ~ Q). Proof. by auto. Qed.
555
556 Lemma contraTnot (b : bool) (P : Prop) : (P -> ~~ b) -> (b -> ~ P).
557 Proof. by case: b; auto. Qed.
558
559 Lemma contraNnot (P : Prop) (b : bool) : (P -> b) -> (~~ b -> ~ P).
560 Proof. rewrite -{1}[b]negbK; exact: contraTnot. Qed.
561
562 Lemma contraPT (P : Prop) (b : bool) : (~~ b -> ~ P) -> P -> b.
563 Proof. by case: b => //= /(_ isT) nP /nP. Qed.
564
565 Lemma contra_notT (P : Prop) (b : bool) : (~~ b -> P) -> ~ P -> b.
566 Proof. by case: b => //= /(_ isT) HP /(_ HP). Qed.
567
568 Lemma contra_notN (P : Prop) (b : bool) : (b -> P) -> ~ P -> ~~ b.
569 Proof. rewrite -{1}[b]negbK; exact: contra_notT. Qed.
570
571 Lemma contraPN (P : Prop) (b : bool) : (b -> ~ P) -> (P -> ~~ b).
572 Proof. by case: b => //=; move/(_ isT) => HP /HP. Qed.
573
574 Lemma contraFnot (P : Prop) (b : bool) : (P -> b) -> b = false -> ~ P.
575 Proof. by case: b => //; auto. Qed.
576
577 Lemma contraPF (P : Prop) (b : bool) : (b -> ~ P) -> P -> b = false.
578 Proof. by case: b => // /(_ isT). Qed.
579
580 Lemma contra_notF (P : Prop) (b : bool) : (b -> P) -> ~ P -> b = false.
581 Proof. by case: b => // /(_ isT). Qed.
582
583 (**
584 Coercion of sum-style datatypes into bool, which makes it possible
585 to use ssr's boolean if rather than Rocq's "generic" if. **)
586
587 Coercion isSome T (u : option T) := if u is Some _ then true else false.
588
589 Coercion is_inl A B (u : A + B) := if u is inl _ then true else false.
590
591 Coercion is_left A B (u : {A} + {B}) := if u is left _ then true else false.
592
593 Coercion is_inleft A B (u : A + {B}) := if u is inleft _ then true else false.
594
595 Prenex Implicits isSome is_inl is_left is_inleft.
596
597 Definition decidable P := {P} + {~ P}.
598
599 (**
600 Lemmas for ifs with large conditions, which allow reasoning about the
601 condition without repeating it inside the proof (the latter IS
602 preferable when the condition is short).
603 Usage :
604 if the goal contains (if cond then ...) = ...
605 case: ifP => Hcond.
606 generates two subgoal, with the assumption Hcond : cond = true/false
607 Rewrite if_same eliminates redundant ifs
608 Rewrite (fun_if f) moves a function f inside an if
609 Rewrite if_arg moves an argument inside a function-valued if **)
610
611 Section BoolIf.
612
613 Variables (A B : Type) (x : A) (f : A -> B) (b : bool) (vT vF : A).
614
615 Variant if_spec (not_b : Prop) : bool -> A -> Set :=
616 | IfSpecTrue of b : if_spec not_b true vT
617 | IfSpecFalse of not_b : if_spec not_b false vF.
618
619 Lemma ifP : if_spec (b = false) b (if b then vT else vF).
620 Proof. by case def_b: b; constructor. Qed.
621
622 Lemma ifPn : if_spec (~~ b) b (if b then vT else vF).
623 Proof. by case def_b: b; constructor; rewrite ?def_b. Qed.
624
625 Lemma ifT : b -> (if b then vT else vF) = vT. Proof. by move->. Qed.
626 Lemma ifF : b = false -> (if b then vT else vF) = vF. Proof. by move->. Qed.
627 Lemma ifN : ~~ b -> (if b then vT else vF) = vF. Proof. by move/negbTE->. Qed.
628
629 Lemma if_same : (if b then vT else vT) = vT.
630 Proof. by case b. Qed.
631
632 Lemma if_neg : (if ~~ b then vT else vF) = if b then vF else vT.
633 Proof. by case b. Qed.
634
635 Lemma fun_if : f (if b then vT else vF) = if b then f vT else f vF.
636 Proof. by case b. Qed.
637
638 Lemma if_arg (fT fF : A -> B) :
639 (if b then fT else fF) x = if b then fT x else fF x.
640 Proof. by case b. Qed.
641
642 (** Turning a boolean "if" form into an application. **)
643 Definition if_expr := if b then vT else vF.
644 Lemma ifE : (if b then vT else vF) = if_expr. Proof. by []. Qed.
645
646 End BoolIf.
647
648 (** Core (internal) reflection lemmas, used for the three kinds of views. **)
649
650 Section ReflectCore.
651
652 Variables (P Q : Prop) (b c : bool).
653
654 Hypothesis Hb : reflect P b.
655
656 Lemma introNTF : (if c then ~ P else P) -> ~~ b = c.
657 Proof. by case c; case Hb. Qed.
658
659 Lemma introTF : (if c then P else ~ P) -> b = c.
660 Proof. by case c; case Hb. Qed.
661
662 Lemma elimNTF : ~~ b = c -> if c then ~ P else P.
663 Proof. by move <-; case Hb. Qed.
664
665 Lemma elimTF : b = c -> if c then P else ~ P.
666 Proof. by move <-; case Hb. Qed.
667
668 Lemma equivPif : (Q -> P) -> (P -> Q) -> if b then Q else ~ Q.
669 Proof. by case Hb; auto. Qed.
670
671 Lemma xorPif : Q \/ P -> ~ (Q /\ P) -> if b then ~ Q else Q.
672 Proof. by case Hb => [? _ H ? | ? H _]; case: H. Qed.
673
674 End ReflectCore.
675
676 (** Internal negated reflection lemmas **)
677 Section ReflectNegCore.
678
679 Variables (P Q : Prop) (b c : bool).
680 Hypothesis Hb : reflect P (~~ b).
681
682 Lemma introTFn : (if c then ~ P else P) -> b = c.
683 Proof. by move/(introNTF Hb) <-; case b. Qed.
684
685 Lemma elimTFn : b = c -> if c then ~ P else P.
686 Proof. by move <-; apply: (elimNTF Hb); case b. Qed.
687
688 Lemma equivPifn : (Q -> P) -> (P -> Q) -> if b then ~ Q else Q.
689 Proof. by rewrite -if_neg; apply: equivPif. Qed.
690
691 Lemma xorPifn : Q \/ P -> ~ (Q /\ P) -> if b then Q else ~ Q.
692 Proof. by rewrite -if_neg; apply: xorPif. Qed.
693
694 End ReflectNegCore.
695
696 (** User-oriented reflection lemmas **)
697 Section Reflect.
698
699 Variables (P Q : Prop) (b b' c : bool).
700 Hypotheses (Pb : reflect P b) (Pb' : reflect P (~~ b')).
701
702 Lemma introT : P -> b. Proof. exact: introTF true _. Qed.
703 Lemma introF : ~ P -> b = false. Proof. exact: introTF false _. Qed.
704 Lemma introN : ~ P -> ~~ b. Proof. exact: introNTF true _. Qed.
705 Lemma introNf : P -> ~~ b = false. Proof. exact: introNTF false _. Qed.
706 Lemma introTn : ~ P -> b'. Proof. exact: introTFn true _. Qed.
707 Lemma introFn : P -> b' = false. Proof. exact: introTFn false _. Qed.
708
709 Lemma elimT : b -> P. Proof. exact: elimTF true _. Qed.
710 Lemma elimF : b = false -> ~ P. Proof. exact: elimTF false _. Qed.
711 Lemma elimN : ~~ b -> ~P. Proof. exact: elimNTF true _. Qed.
712 Lemma elimNf : ~~ b = false -> P. Proof. exact: elimNTF false _. Qed.
713 Lemma elimTn : b' -> ~ P. Proof. exact: elimTFn true _. Qed.
714 Lemma elimFn : b' = false -> P. Proof. exact: elimTFn false _. Qed.
715
716 Lemma introP : (b -> Q) -> (~~ b -> ~ Q) -> reflect Q b.
717 Proof. by case b; constructor; auto. Qed.
718
719 Lemma iffP : (P -> Q) -> (Q -> P) -> reflect Q b.
720 Proof. by case: Pb; constructor; auto. Qed.
721
722 Lemma equivP : (P <-> Q) -> reflect Q b.
723 Proof. by case; apply: iffP. Qed.
724
725 Lemma sumboolP (decQ : decidable Q) : reflect Q decQ.
726 Proof. by case: decQ; constructor. Qed.
727
728 Lemma appP : reflect Q b -> P -> Q.
729 Proof. by move=> Qb; move/introT; case: Qb. Qed.
730
731 Lemma sameP : reflect P c -> b = c.
732 Proof. by case; [apply: introT | apply: introF]. Qed.
733
734 Lemma decPcases : if b then P else ~ P. Proof. by case Pb. Qed.
735
736 Definition decP : decidable P. by case: b decPcases; [left | right]. Defined.
737
738 Lemma rwP : P <-> b. Proof. by split; [apply: introT | apply: elimT]. Qed.
739
740 Lemma rwP2 : reflect Q b -> (P <-> Q).
741 Proof. by move=> Qb; split=> ?; [apply: appP | apply: elimT; case: Qb]. Qed.
742
743 (** Predicate family to reflect excluded middle in bool. **)
744 Variant alt_spec : bool -> Type :=
745 | AltTrue of P : alt_spec true
746 | AltFalse of ~~ b : alt_spec false.
747
748 Lemma altP : alt_spec b.
749 Proof. by case def_b: b / Pb; constructor; rewrite ?def_b. Qed.
750
751 Lemma eqbLR (b1 b2 : bool) : b1 = b2 -> b1 -> b2.
752 Proof. by move->. Qed.
753
754 Lemma eqbRL (b1 b2 : bool) : b1 = b2 -> b2 -> b1.
755 Proof. by move->. Qed.
756
757 End Reflect.
758
759 Hint View for move/ elimTF|3 elimNTF|3 elimTFn|3 introT|2 introTn|2 introN|2.
760
761 Hint View for apply/ introTF|3 introNTF|3 introTFn|3 elimT|2 elimTn|2 elimN|2.
762
763 Hint View for apply// equivPif|3 xorPif|3 equivPifn|3 xorPifn|3.
764
765 (** Allow the direct application of a reflection lemma to a boolean assertion. **)
766 Coercion elimT : reflect >-> Funclass.
767
768 #[universes(template)]
769 Variant implies P Q := Implies of P -> Q.
770 Lemma impliesP P Q : implies P Q -> P -> Q. Proof. by case. Qed.
771 Lemma impliesPn (P Q : Prop) : implies P Q -> ~ Q -> ~ P.
772 Proof. by case=> iP ? /iP. Qed.
773 Coercion impliesP : implies >-> Funclass.
774 Hint View for move/ impliesPn|2 impliesP|2.
775 Hint View for apply/ impliesPn|2 impliesP|2.
776
777 (** Impredicative or, which can emulate a classical not-implies. **)
778 Definition unless condition property : Prop :=
779 forall goal : Prop, (condition -> goal) -> (property -> goal) -> goal.
780
781 Notation "\unless C , P" := (unless C P) : type_scope.
782
783 Lemma unlessL C P : implies C (\unless C, P).
784 Proof. by split=> hC G /(_ hC). Qed.
785
786 Lemma unlessR C P : implies P (\unless C, P).
787 Proof. by split=> hP G _ /(_ hP). Qed.
788
789 Lemma unless_sym C P : implies (\unless C, P) (\unless P, C).
790 Proof. by split; apply; [apply/unlessR | apply/unlessL]. Qed.
791
792 Lemma unlessP (C P : Prop) : (\unless C, P) <-> C \/ P.
793 Proof. by split=> [|[/unlessL | /unlessR]]; apply; [left | right]. Qed.
794
795 Lemma bind_unless C P {Q} : implies (\unless C, P) (\unless (\unless C, Q), P).
796 Proof. by split; apply=> [hC|hP]; [apply/unlessL/unlessL | apply/unlessR]. Qed.
797
798 Lemma unless_contra b C : implies (~~ b -> C) (\unless C, b).
799 Proof. by split; case: b => [_ | hC]; [apply/unlessR | apply/unlessL/hC]. Qed.
800
801 (**
802 Classical reasoning becomes directly accessible for any bool subgoal.
803 Note that we cannot use "unless" here for lack of universe polymorphism. **)
804 Definition classically P : Prop := forall b : bool, (P -> b) -> b.
805
806 Lemma classicP (P : Prop) : classically P <-> ~ ~ P.
807 Proof.
808 split=> [cP nP | nnP [] // nP]; last by case nnP; move/nP.
809 by have: P -> false; [move/nP | move/cP].
810 Qed.
811
812 Lemma classicW P : P -> classically P. Proof. by move=> hP _ ->. Qed.
813
814 Lemma classic_bind P Q : (P -> classically Q) -> classically P -> classically Q.
815 Proof. by move=> iPQ cP b /iPQ-/cP. Qed.
816
817 Lemma classic_EM P : classically (decidable P).
818 Proof.
819 by case=> // undecP; apply/undecP; right=> notP; apply/notF/undecP; left.
820 Qed.
821
822 Lemma classic_pick T P : classically ({x : T | P x} + (forall x, ~ P x)).
823 Proof.
824 case=> // undecP; apply/undecP; right=> x Px.
825 by apply/notF/undecP; left; exists x.
826 Qed.
827
828 Lemma classic_imply P Q : (P -> classically Q) -> classically (P -> Q).
829 Proof.
830 move=> iPQ []// notPQ; apply/notPQ=> /iPQ-cQ.
831 by case: notF; apply: cQ => hQ; apply: notPQ.
832 Qed.
833
834 Lemma classic_sigW T (P : T -> Prop) :
835 classically (exists x, P x) <-> classically ({x | P x}).
836 Proof. by split; apply: classic_bind => -[x Px]; apply/classicW; exists x. Qed.
837
838 Lemma classic_ex T (P : T -> Prop) :
839 ~ (forall x, ~ P x) -> classically (exists x, P x).
840 Proof.
841 move=> NfNP; apply/classicP => exPF; apply: NfNP => x Px.
842 by apply: exPF; exists x.
843 Qed.
844
845 (**
846 List notations for wider connectives; the Prop connectives have a fixed
847 width so as to avoid iterated destruction (we go up to width 5 for /\, and
848 width 4 for or). The bool connectives have arbitrary widths, but denote
849 expressions that associate to the RIGHT. This is consistent with the right
850 associativity of list expressions and thus more convenient in most proofs. **)
851
852 Inductive and3 (P1 P2 P3 : Prop) : Prop := And3 of P1 & P2 & P3.
853
854 Inductive and4 (P1 P2 P3 P4 : Prop) : Prop := And4 of P1 & P2 & P3 & P4.
855
856 Inductive and5 (P1 P2 P3 P4 P5 : Prop) : Prop :=
857 And5 of P1 & P2 & P3 & P4 & P5.
858
859 Inductive or3 (P1 P2 P3 : Prop) : Prop := Or31 of P1 | Or32 of P2 | Or33 of P3.
860
861 Inductive or4 (P1 P2 P3 P4 : Prop) : Prop :=
862 Or41 of P1 | Or42 of P2 | Or43 of P3 | Or44 of P4.
863
864 Notation "[ /\ P1 & P2 ]" := (and P1 P2) (only parsing) : type_scope.
865 Notation "[ /\ P1 , P2 & P3 ]" := (and3 P1 P2 P3) : type_scope.
866 Notation "[ /\ P1 , P2 , P3 & P4 ]" := (and4 P1 P2 P3 P4) : type_scope.
867 Notation "[ /\ P1 , P2 , P3 , P4 & P5 ]" := (and5 P1 P2 P3 P4 P5) : type_scope.
868
869 Notation "[ \/ P1 | P2 ]" := (or P1 P2) (only parsing) : type_scope.
870 Notation "[ \/ P1 , P2 | P3 ]" := (or3 P1 P2 P3) : type_scope.
871 Notation "[ \/ P1 , P2 , P3 | P4 ]" := (or4 P1 P2 P3 P4) : type_scope.
872
873 Notation "[ && b1 & c ]" := (b1 && c) (only parsing) : bool_scope.
874 Notation "[ && b1 , b2 , .. , bn & c ]" := (b1 && (b2 && .. (bn && c) .. ))
875 : bool_scope.
876
877 Notation "[ || b1 | c ]" := (b1 || c) (only parsing) : bool_scope.
878 Notation "[ || b1 , b2 , .. , bn | c ]" := (b1 || (b2 || .. (bn || c) .. ))
879 : bool_scope.
880
881 Notation "[ ==> b1 , b2 , .. , bn => c ]" :=
882 (b1 ==> (b2 ==> .. (bn ==> c) .. )) : bool_scope.
883 Notation "[ ==> b1 => c ]" := (b1 ==> c) (only parsing) : bool_scope.
884
885 Section AllAnd.
886
887 Variables (T : Type) (P1 P2 P3 P4 P5 : T -> Prop).
888 Local Abbreviation a P := (forall x, P x).
889
890 Lemma all_and2 : implies (forall x, [/\ P1 x & P2 x]) [/\ a P1 & a P2].
891 Proof. by split=> haveP; split=> x; case: (haveP x). Qed.
892
893 Lemma all_and3 : implies (forall x, [/\ P1 x, P2 x & P3 x])
894 [/\ a P1, a P2 & a P3].
895 Proof. by split=> haveP; split=> x; case: (haveP x). Qed.
896
897 Lemma all_and4 : implies (forall x, [/\ P1 x, P2 x, P3 x & P4 x])
898 [/\ a P1, a P2, a P3 & a P4].
899 Proof. by split=> haveP; split=> x; case: (haveP x). Qed.
900
901 Lemma all_and5 : implies (forall x, [/\ P1 x, P2 x, P3 x, P4 x & P5 x])
902 [/\ a P1, a P2, a P3, a P4 & a P5].
903 Proof. by split=> haveP; split=> x; case: (haveP x). Qed.
904
905 End AllAnd.
906
907 Arguments all_and2 {T P1 P2}.
908 Arguments all_and3 {T P1 P2 P3}.
909 Arguments all_and4 {T P1 P2 P3 P4}.
910 Arguments all_and5 {T P1 P2 P3 P4 P5}.
911
912 Lemma pair_andP P Q : P /\ Q <-> P * Q. Proof. by split; case. Qed.
913
914 Section ReflectConnectives.
915
916 Variable b1 b2 b3 b4 b5 : bool.
917
918 Lemma idP : reflect b1 b1.
919 Proof. by case b1; constructor. Qed.
920
921 Lemma boolP : alt_spec b1 b1 b1.
922 Proof. exact: (altP idP). Qed.
923
924 (* Left-to-right reflection of ~~b1 to (b1 = false), no-op otherwise. *)
925 Lemma idPn : reflect (~~ b1) (~~ b1).
926 Proof. by case b1; constructor. Qed.
927
928 Lemma negP : reflect (~ b1) (~~ b1).
929 Proof. by case b1; constructor; auto. Qed.
930
931 Lemma negPn : reflect b1 (~~ ~~ b1).
932 Proof. by case b1; constructor. Qed.
933
934 (* Right-to-left reflection, no-op otherwise.
935 C.f., https://github.com/math-comp/math-comp/issues/284
936 To change `b1 = false` into `~~ b1`, use `apply/negbTE` or `apply/idPn` (goal)
937 or `move/negbT` or `move/idPn` (hypothesis). *)
938 Lemma negPf : reflect (b1 = false) (~~ b1).
939 Proof. by case b1; constructor. Qed.
940
941 Lemma andP : reflect (b1 /\ b2) (b1 && b2).
942 Proof. by case b1; case b2; constructor=> //; case. Qed.
943
944 Lemma and3P : reflect [/\ b1, b2 & b3] [&& b1, b2 & b3].
945 Proof. by case b1; case b2; case b3; constructor; try by case. Qed.
946
947 Lemma and4P : reflect [/\ b1, b2, b3 & b4] [&& b1, b2, b3 & b4].
948 Proof. by case b1; case b2; case b3; case b4; constructor; try by case. Qed.
949
950 Lemma and5P : reflect [/\ b1, b2, b3, b4 & b5] [&& b1, b2, b3, b4 & b5].
951 Proof.
952 by case b1; case b2; case b3; case b4; case b5; constructor; try by case.
953 Qed.
954
955 Lemma orP : reflect (b1 \/ b2) (b1 || b2).
956 Proof. by case b1; case b2; constructor; auto; case. Qed.
957
958 Lemma or3P : reflect [\/ b1, b2 | b3] [|| b1, b2 | b3].
959 Proof.
960 case b1; first by constructor; constructor 1.
961 case b2; first by constructor; constructor 2.
962 case b3; first by constructor; constructor 3.
963 by constructor; case.
964 Qed.
965
966 Lemma or4P : reflect [\/ b1, b2, b3 | b4] [|| b1, b2, b3 | b4].
967 Proof.
968 case b1; first by constructor; constructor 1.
969 case b2; first by constructor; constructor 2.
970 case b3; first by constructor; constructor 3.
971 case b4; first by constructor; constructor 4.
972 by constructor; case.
973 Qed.
974
975 Lemma nandP : reflect (~~ b1 \/ ~~ b2) (~~ (b1 && b2)).
976 Proof. by case b1; case b2; constructor; auto; case; auto. Qed.
977
978 Lemma norP : reflect (~~ b1 /\ ~~ b2) (~~ (b1 || b2)).
979 Proof. by case b1; case b2; constructor; auto; case; auto. Qed.
980
981 Lemma implyP : reflect (b1 -> b2) (b1 ==> b2).
982 Proof. by case b1; case b2; constructor; auto. Qed.
983
984 End ReflectConnectives.
985
986 Arguments idP {b1}.
987 Arguments idPn {b1}.
988 Arguments negP {b1}.
989 Arguments negPn {b1}.
990 Arguments negPf {b1}.
991 Arguments andP {b1 b2}.
992 Arguments and3P {b1 b2 b3}.
993 Arguments and4P {b1 b2 b3 b4}.
994 Arguments and5P {b1 b2 b3 b4 b5}.
995 Arguments orP {b1 b2}.
996 Arguments or3P {b1 b2 b3}.
997 Arguments or4P {b1 b2 b3 b4}.
998 Arguments nandP {b1 b2}.
999 Arguments norP {b1 b2}.
1000 Arguments implyP {b1 b2}.
1001 Prenex Implicits idP idPn negP negPn negPf.
1002 Prenex Implicits andP and3P and4P and5P orP or3P or4P nandP norP implyP.
1003
1004 Section ReflectCombinators.
1005
1006 Variables (P Q : Prop) (p q : bool).
1007
1008 Hypothesis rP : reflect P p.
1009 Hypothesis rQ : reflect Q q.
1010
1011 Lemma negPP : reflect (~ P) (~~ p).
1012 Proof. by apply:(iffP negP); apply: contra_not => /rP. Qed.
1013
1014 Lemma andPP : reflect (P /\ Q) (p && q).
1015 Proof. by apply: (iffP andP) => -[/rP ? /rQ ?]. Qed.
1016
1017 Lemma orPP : reflect (P \/ Q) (p || q).
1018 Proof. by apply: (iffP orP) => -[/rP ?|/rQ ?]; tauto. Qed.
1019
1020 Lemma implyPP : reflect (P -> Q) (p ==> q).
1021 Proof. by apply: (iffP implyP) => pq /rP /pq /rQ. Qed.
1022
1023 End ReflectCombinators.
1024 Arguments negPP {P p}.
1025 Arguments andPP {P Q p q}.
1026 Arguments orPP {P Q p q}.
1027 Arguments implyPP {P Q p q}.
1028 Prenex Implicits negPP andPP orPP implyPP.
1029
1030 (** Shorter, more systematic names for the boolean connectives laws. **)
1031
1032 Lemma andTb : left_id true andb. Proof. by []. Qed.
1033 Lemma andFb : left_zero false andb. Proof. by []. Qed.
1034 Lemma andbT : right_id true andb. Proof. by case. Qed.
1035 Lemma andbF : right_zero false andb. Proof. by case. Qed.
1036 Lemma andbb : idempotent_op andb. Proof. by case. Qed.
1037 Lemma andbC : commutative andb. Proof. by do 2!case. Qed.
1038 Lemma andbA : associative andb. Proof. by do 3!case. Qed.
1039 Lemma andbCA : left_commutative andb. Proof. by do 3!case. Qed.
1040 Lemma andbAC : right_commutative andb. Proof. by do 3!case. Qed.
1041 Lemma andbACA : interchange andb andb. Proof. by do 4!case. Qed.
1042
1043 Lemma orTb : forall b, true || b. Proof. by []. Qed.
1044 Lemma orFb : left_id false orb. Proof. by []. Qed.
1045 Lemma orbT : forall b, b || true. Proof. by case. Qed.
1046 Lemma orbF : right_id false orb. Proof. by case. Qed.
1047 Lemma orbb : idempotent_op orb. Proof. by case. Qed.
1048 Lemma orbC : commutative orb. Proof. by do 2!case. Qed.
1049 Lemma orbA : associative orb. Proof. by do 3!case. Qed.
1050 Lemma orbCA : left_commutative orb. Proof. by do 3!case. Qed.
1051 Lemma orbAC : right_commutative orb. Proof. by do 3!case. Qed.
1052 Lemma orbACA : interchange orb orb. Proof. by do 4!case. Qed.
1053
1054 Lemma andbN b : b && ~~ b = false. Proof. by case: b. Qed.
1055 Lemma andNb b : ~~ b && b = false. Proof. by case: b. Qed.
1056 Lemma orbN b : b || ~~ b = true. Proof. by case: b. Qed.
1057 Lemma orNb b : ~~ b || b = true. Proof. by case: b. Qed.
1058
1059 Lemma andb_orl : left_distributive andb orb. Proof. by do 3!case. Qed.
1060 Lemma andb_orr : right_distributive andb orb. Proof. by do 3!case. Qed.
1061 Lemma orb_andl : left_distributive orb andb. Proof. by do 3!case. Qed.
1062 Lemma orb_andr : right_distributive orb andb. Proof. by do 3!case. Qed.
1063
1064 Lemma andb_idl (a b : bool) : (b -> a) -> a && b = b.
1065 Proof. by case: a; case: b => // ->. Qed.
1066 Lemma andb_idr (a b : bool) : (a -> b) -> a && b = a.
1067 Proof. by case: a; case: b => // ->. Qed.
1068 Lemma andb_id2l (a b c : bool) : (a -> b = c) -> a && b = a && c.
1069 Proof. by case: a; case: b; case: c => // ->. Qed.
1070 Lemma andb_id2r (a b c : bool) : (b -> a = c) -> a && b = c && b.
1071 Proof. by case: a; case: b; case: c => // ->. Qed.
1072
1073 Lemma orb_idl (a b : bool) : (a -> b) -> a || b = b.
1074 Proof. by case: a; case: b => // ->. Qed.
1075 Lemma orb_idr (a b : bool) : (b -> a) -> a || b = a.
1076 Proof. by case: a; case: b => // ->. Qed.
1077 Lemma orb_id2l (a b c : bool) : (~~ a -> b = c) -> a || b = a || c.
1078 Proof. by case: a; case: b; case: c => // ->. Qed.
1079 Lemma orb_id2r (a b c : bool) : (~~ b -> a = c) -> a || b = c || b.
1080 Proof. by case: a; case: b; case: c => // ->. Qed.
1081
1082 Lemma negb_and (a b : bool) : ~~ (a && b) = ~~ a || ~~ b.
1083 Proof. by case: a; case: b. Qed.
1084
1085 Lemma negb_or (a b : bool) : ~~ (a || b) = ~~ a && ~~ b.
1086 Proof. by case: a; case: b. Qed.
1087
1088 (** Pseudo-cancellation -- i.e, absorption **)
1089
1090 Lemma andbK a b : a && b || a = a. Proof. by case: a; case: b. Qed.
1091 Lemma andKb a b : a || b && a = a. Proof. by case: a; case: b. Qed.
1092 Lemma orbK a b : (a || b) && a = a. Proof. by case: a; case: b. Qed.
1093 Lemma orKb a b : a && (b || a) = a. Proof. by case: a; case: b. Qed.
1094
1095 (** Imply **)
1096
1097 Lemma implybT b : b ==> true. Proof. by case: b. Qed.
1098 Lemma implybF b : (b ==> false) = ~~ b. Proof. by case: b. Qed.
1099 Lemma implyFb b : false ==> b. Proof. by []. Qed.
1100 Lemma implyTb b : (true ==> b) = b. Proof. by []. Qed.
1101 Lemma implybb b : b ==> b. Proof. by case: b. Qed.
1102
1103 Lemma negb_imply a b : ~~ (a ==> b) = a && ~~ b.
1104 Proof. by case: a; case: b. Qed.
1105
1106 Lemma implybE a b : (a ==> b) = ~~ a || b.
1107 Proof. by case: a; case: b. Qed.
1108
1109 Lemma implyNb a b : (~~ a ==> b) = a || b.
1110 Proof. by case: a; case: b. Qed.
1111
1112 Lemma implybN a b : (a ==> ~~ b) = (b ==> ~~ a).
1113 Proof. by case: a; case: b. Qed.
1114
1115 Lemma implybNN a b : (~~ a ==> ~~ b) = b ==> a.
1116 Proof. by case: a; case: b. Qed.
1117
1118 Lemma implyb_idl (a b : bool) : (~~ a -> b) -> (a ==> b) = b.
1119 Proof. by case: a; case: b => // ->. Qed.
1120 Lemma implyb_idr (a b : bool) : (b -> ~~ a) -> (a ==> b) = ~~ a.
1121 Proof. by case: a; case: b => // ->. Qed.
1122 Lemma implyb_id2l (a b c : bool) : (a -> b = c) -> (a ==> b) = (a ==> c).
1123 Proof. by case: a; case: b; case: c => // ->. Qed.
1124
1125 (** Addition (xor) **)
1126
1127 Lemma addFb : left_id false addb. Proof. by []. Qed.
1128 Lemma addbF : right_id false addb. Proof. by case. Qed.
1129 Lemma addbb : self_inverse false addb. Proof. by case. Qed.
1130 Lemma addbC : commutative addb. Proof. by do 2!case. Qed.
1131 Lemma addbA : associative addb. Proof. by do 3!case. Qed.
1132 Lemma addbCA : left_commutative addb. Proof. by do 3!case. Qed.
1133 Lemma addbAC : right_commutative addb. Proof. by do 3!case. Qed.
1134 Lemma addbACA : interchange addb addb. Proof. by do 4!case. Qed.
1135 Lemma andb_addl : left_distributive andb addb. Proof. by do 3!case. Qed.
1136 Lemma andb_addr : right_distributive andb addb. Proof. by do 3!case. Qed.
1137 Lemma addKb : left_loop id addb. Proof. by do 2!case. Qed.
1138 Lemma addbK : right_loop id addb. Proof. by do 2!case. Qed.
1139 Lemma addIb : left_injective addb. Proof. by do 3!case. Qed.
1140 Lemma addbI : right_injective addb. Proof. by do 3!case. Qed.
1141
1142 Lemma addTb b : true (+) b = ~~ b. Proof. by []. Qed.
1143 Lemma addbT b : b (+) true = ~~ b. Proof. by case: b. Qed.
1144
1145 Lemma addbN a b : a (+) ~~ b = ~~ (a (+) b).
1146 Proof. by case: a; case: b. Qed.
1147 Lemma addNb a b : ~~ a (+) b = ~~ (a (+) b).
1148 Proof. by case: a; case: b. Qed.
1149
1150 Lemma addbP a b : reflect (~~ a = b) (a (+) b).
1151 Proof. by case: a; case: b; constructor. Qed.
1152 Arguments addbP {a b}.
1153
1154 (**
1155 Resolution tactic for blindly weeding out common terms from boolean
1156 equalities. When faced with a goal of the form (andb/orb/addb b1 b2) = b3
1157 they will try to locate b1 in b3 and remove it. This can fail! **)
1158
1159 Ltac bool_congr :=
1160 match goal with
1161 | |- (?X1 && ?X2 = ?X3) => first
1162 [ symmetry; rewrite -1?(andbC X1) -?(andbCA X1); congr 1 (andb X1); symmetry
1163 | case: (X1); [ rewrite ?andTb ?andbT // | by rewrite ?andbF /= ] ]
1164 | |- (?X1 || ?X2 = ?X3) => first
1165 [ symmetry; rewrite -1?(orbC X1) -?(orbCA X1); congr 1 (orb X1); symmetry
1166 | case: (X1); [ by rewrite ?orbT //= | rewrite ?orFb ?orbF ] ]
1167 | |- (?X1 (+) ?X2 = ?X3) =>
1168 symmetry; rewrite -1?(addbC X1) -?(addbCA X1); congr 1 (addb X1); symmetry
1169 | |- (~~ ?X1 = ?X2) => congr 1 negb
1170 end.
1171
1172
1173 (**
1174 Predicates, i.e., packaged functions to bool.
1175 - pred T, the basic type for predicates over a type T, is simply an alias
1176 for T -> bool.
1177 We actually distinguish two kinds of predicates, which we call applicative
1178 and collective, based on the syntax used to test them at some x in T:
1179 - For an applicative predicate P, one uses prefix syntax:
1180 P x
1181 Also, most operations on applicative predicates use prefix syntax as
1182 well (e.g., predI P Q).
1183 - For a collective predicate A, one uses infix syntax:
1184 x \in A
1185 and all operations on collective predicates use infix syntax as well
1186 (e.g., #[#predI A & B#]#).
1187 There are only two kinds of applicative predicates:
1188 - pred T, the alias for T -> bool mentioned above
1189 - simpl_pred T, an alias for simpl_fun T bool with a coercion to pred T
1190 that auto-simplifies on application (see ssrfun).
1191 On the other hand, the set of collective predicate types is open-ended via
1192 - predType T, a Structure that can be used to put Canonical collective
1193 predicate interpretation on other types, such as lists, tuples,
1194 finite sets, etc.
1195 Indeed, we define such interpretations for applicative predicate types,
1196 which can therefore also be used with the infix syntax, e.g.,
1197 x \in predI P Q
1198 Moreover these infix forms are convertible to their prefix counterpart
1199 (e.g., predI P Q x which in turn simplifies to P x && Q x). The converse
1200 is not true, however; collective predicate types cannot, in general, be
1201 used applicatively, because of restrictions on implicit coercions.
1202 However, we do define an explicit generic coercion
1203 - mem : forall (pT : predType), pT -> mem_pred T
1204 where mem_pred T is a variant of simpl_pred T that preserves the infix
1205 syntax, i.e., mem A x auto-simplifies to x \in A.
1206 Indeed, the infix "collective" operators are notation for a prefix
1207 operator with arguments of type mem_pred T or pred T, applied to coerced
1208 collective predicates, e.g.,
1209 Notation "x \in A" := (in_mem x (mem A)).
1210 This prevents the variability in the predicate type from interfering with
1211 the application of generic lemmas. Moreover this also makes it much easier
1212 to define generic lemmas, because the simplest type -- pred T -- can be
1213 used as the type of generic collective predicates, provided one takes care
1214 not to use it applicatively; this avoids the burden of having to declare a
1215 different predicate type for each predicate parameter of each section or
1216 lemma.
1217 In detail, we ensure that the head normal form of mem A is always of the
1218 eta-long MemPred (fun x => pA x) form, where pA is the pred interpretation of
1219 A following its predType pT, i.e., the _expansion_ of topred A. For a pred T
1220 evar ?P, (mem ?P) converts MemPred (fun x => ?P x), whose argument is a Miller
1221 pattern and therefore always unify: unifying (mem A) with (mem ?P) always
1222 yields ?P = pA, because the rigid constant MemPred aligns the unification.
1223 Furthermore, we ensure pA is always either A or toP .... A where toP ... is
1224 the expansion of @topred T pT, and toP is declared as a Coercion, so pA will
1225 _display_ as A in either case, and the instances of @mem T (predPredType T) pA
1226 appearing in the premises or right-hand side of a generic lemma parameterized
1227 by ?P will be indistinguishable from @mem T pT A.
1228 Users should take care not to inadvertently "strip" (mem A) down to the
1229 coerced A, since this will expose the internal toP coercion: Rocq could then
1230 display terms A x that cannot be typed as such. The topredE lemma can be used
1231 to restore the x \in A syntax in this case. While -topredE can conversely be
1232 used to change x \in P into P x for an applicative P, it is safer to use the
1233 inE, unfold_in or and memE lemmas instead, as they do not run the risk of
1234 exposing internal coercions. As a consequence it is better to explicitly
1235 cast a generic applicative predicate to simpl_pred using the SimplPred
1236 constructor when it is used as a collective predicate (see, e.g.,
1237 Lemma eq_big in bigop).
1238 We also sometimes "instantiate" the predType structure by defining a
1239 coercion to the sort of the predPredType structure, conveniently denoted
1240 {pred T}. This works better for types such as {set T} that have subtypes that
1241 coerce to them, since the same coercion will be inserted by the application
1242 of mem, or of any lemma that expects a generic collective predicates with
1243 type {pred T} := pred_sort (predPredType T) = pred T; thus {pred T} should be
1244 the preferred type for generic collective predicate parameters.
1245 This device also lets us turn any Type aT : predArgType into the total
1246 predicate over that type, i.e., fun _: aT => true. This allows us to write,
1247 e.g., ##|'I_n| for the cardinal of the (finite) type of integers less than n.
1248 **)
1249
1250 (** Boolean predicates. *)
1251
1252 Definition pred T := T -> bool.
1253 Identity Coercion fun_of_pred : pred >-> Funclass.
1254
1255 Definition subpred T (p1 p2 : pred T) := forall x : T, p1 x -> p2 x.
1256
1257 (* Abbreviation for some manifest predicates. *)
1258
1259 Abbreviation xpred0 := (fun=> false).
1260 Abbreviation xpredT := (fun=> true).
1261 Abbreviation xpredI := (fun (p1 p2 : pred _) x => p1 x && p2 x).
1262 Abbreviation xpredU := (fun (p1 p2 : pred _) x => p1 x || p2 x).
1263 Abbreviation xpredC := (fun (p : pred _) x => ~~ p x).
1264 Abbreviation xpredD := (fun (p1 p2 : pred _) x => ~~ p2 x && p1 x).
1265 Abbreviation xpreim := (fun f (p : pred _) x => p (f x)).
1266
1267 (** The packed class interface for pred-like types. **)
1268
1269 Structure predType T :=
1270 PredType {pred_sort :> Type; topred : pred_sort -> pred T}.
1271
1272 Definition clone_pred T U :=
1273 fun pT & @pred_sort T pT -> U =>
1274 fun toP (pT' := @PredType T U toP) & phant_id pT' pT => pT'.
1275 Notation "[ 'predType' 'of' T ]" := (@clone_pred _ T _ id _ id) : form_scope.
1276
1277 Canonical predPredType T := PredType (@id (pred T)).
1278 Set Warnings "-redundant-canonical-projection".
1279 Canonical boolfunPredType T := PredType (@id (T -> bool)).
1280 Set Warnings "redundant-canonical-projection".
1281
1282 (** The type of abstract collective predicates.
1283 While {pred T} is convertible to pred T, it presents the pred_sort coercion
1284 class, which crucially does _not_ coerce to Funclass. Term whose type P coerces
1285 to {pred T} cannot be applied to arguments, but they _can_ be used as if P
1286 had a canonical predType instance, as the coercion will be inserted if the
1287 unification P =~= pred_sort ?pT fails, changing the problem into the trivial
1288 {pred T} =~= pred_sort ?pT (solution ?pT := predPredType P).
1289 Additional benefits of this approach are that any type coercing to P will
1290 also inherit this behaviour, and that the coercion will be apparent in the
1291 elaborated expression. The latter may be important if the coercion is also
1292 a canonical structure projector - see mathcomp/fingroup/fingroup.v. The
1293 main drawback of implementing predType by coercion in this way is that the
1294 type of the value must be known when the unification constraint is imposed:
1295 if we only register the constraint and then later discover later that the
1296 expression had type P it will be too late to insert a coercion, whereas a
1297 canonical instance of predType for P would have solved the deferred constraint.
1298 Finally, definitions, lemmas and sections should use type {pred T} for
1299 their generic collective type parameters, as this will make it possible to
1300 apply such definitions and lemmas directly to values of types that implement
1301 predType by coercion to {pred T} (values of types that implement predType
1302 without coercing to {pred T} will have to be coerced explicitly using topred).
1303 **)
1304 Notation "{ 'pred' T }" := (pred_sort (predPredType T)) : type_scope.
1305
1306 (** The type of self-simplifying collective predicates. **)
1307 Definition simpl_pred T := simpl_fun T bool.
1308 Definition SimplPred {T} (p : pred T) : simpl_pred T := SimplFun p.
1309
1310 (** Some simpl_pred constructors. **)
1311
1312 Definition pred0 {T} := @SimplPred T xpred0.
1313 Definition predT {T} := @SimplPred T xpredT.
1314 Definition predI {T} (p1 p2 : pred T) := SimplPred (xpredI p1 p2).
1315 Definition predU {T} (p1 p2 : pred T) := SimplPred (xpredU p1 p2).
1316 Definition predC {T} (p : pred T) := SimplPred (xpredC p).
1317 Definition predD {T} (p1 p2 : pred T) := SimplPred (xpredD p1 p2).
1318 Definition preim {aT rT} (f : aT -> rT) (d : pred rT) := SimplPred (xpreim f d).
1319
1320 Notation "[ 'pred' : T | E ]" := (SimplPred (fun _ : T => E%B)) :
1321 function_scope.
1322 Notation "[ 'pred' x | E ]" := (SimplPred (fun x => E%B)) : function_scope.
1323 Notation "[ 'pred' x | E1 & E2 ]" := [pred x | E1 && E2 ] : function_scope.
1324 Notation "[ 'pred' x : T | E ]" :=
1325 (SimplPred (fun x : T => E%B)) (only parsing) : function_scope.
1326 Notation "[ 'pred' x : T | E1 & E2 ]" :=
1327 [pred x : T | E1 && E2 ] (only parsing) : function_scope.
1328
1329 (** Coercions for simpl_pred.
1330 As simpl_pred T values are used both applicatively and collectively we
1331 need simpl_pred to coerce to both pred T _and_ {pred T}. However it is
1332 undesirable to have two distinct constants for what are essentially identical
1333 coercion functions, as this confuses the SSReflect keyed matching algorithm.
1334 While the Rocq Coercion declarations appear to disallow such Coercion aliasing,
1335 it is possible to work around this limitation with a combination of modules
1336 and functors, which we do below.
1337 In addition we also give a predType instance for simpl_pred, which will
1338 be preferred to the {pred T} coercion to solve simpl_pred T =~= pred_sort ?pT
1339 constraints; note however that the pred_of_simpl coercion _will_ be used
1340 when a simpl_pred T is passed as a {pred T}, since the simplPredType T
1341 structure for simpl_pred T is _not_ convertible to predPredType T. **)
1342
1343 Module PredOfSimpl.
1344 Definition coerce T (sp : simpl_pred T) : pred T := fun_of_simpl sp.
1345 End PredOfSimpl.
1346 Abbreviation pred_of_simpl := PredOfSimpl.coerce.
1347 Coercion pred_of_simpl : simpl_pred >-> pred.
1348 Canonical simplPredType T := PredType (@pred_of_simpl T).
1349
1350 Module Type PredSortOfSimplSignature.
1351 Parameter coerce : forall T, simpl_pred T -> {pred T}.
1352 End PredSortOfSimplSignature.
1353 Module DeclarePredSortOfSimpl (PredSortOfSimpl : PredSortOfSimplSignature).
1354 Coercion PredSortOfSimpl.coerce : simpl_pred >-> pred_sort.
1355 End DeclarePredSortOfSimpl.
1356 Module Export PredSortOfSimplCoercion := DeclarePredSortOfSimpl PredOfSimpl.
1357
1358 (** Type to pred coercion.
1359 This lets us use types of sort predArgType as a synonym for their universal
1360 predicate. We define this predicate as a simpl_pred T rather than a pred T or
1361 a {pred T} so that /= and inE reduce (T x) and x \in T to true, respectively.
1362 Unfortunately, this can't be used for existing types like bool whose sort
1363 is already fixed (at least, not without redefining bool, true, false and
1364 all bool operations and lemmas); we provide syntax to recast a given type
1365 in predArgType as a workaround. **)
1366 Definition predArgType := Type.
1367 Bind Scope type_scope with predArgType.
1368 Identity Coercion sort_of_predArgType : predArgType >-> Sortclass.
1369 Coercion pred_of_argType (T : predArgType) : simpl_pred T := predT.
1370 Notation "{ : T }" := (T%type : predArgType) : type_scope.
1371
1372 (** Boolean relations.
1373 Simplifying relations follow the coding pattern of 2-argument simplifying
1374 functions: the simplifying type constructor is applied to the _last_
1375 argument. This design choice will let the in_simpl component of inE expand
1376 membership in simpl_rel as well. We provide an explicit coercion to rel T
1377 to avoid eta-expansion during coercion; this coercion self-simplifies so it
1378 should be invisible.
1379 **)
1380
1381 Definition rel T := T -> pred T.
1382 Identity Coercion fun_of_rel : rel >-> Funclass.
1383
1384 Definition subrel T (r1 r2 : rel T) := forall x y : T, r1 x y -> r2 x y.
1385
1386 Definition simpl_rel T := T -> simpl_pred T.
1387
1388 Coercion rel_of_simpl T (sr : simpl_rel T) : rel T := fun x : T => sr x.
1389 Arguments rel_of_simpl {T} sr x /.
1390
1391 Abbreviation xrelU := (fun (r1 r2 : rel _) x y => r1 x y || r2 x y).
1392 Abbreviation xrelpre := (fun f (r : rel _) x y => r (f x) (f y)).
1393
1394 Definition SimplRel {T} (r : rel T) : simpl_rel T := fun x => SimplPred (r x).
1395 Definition relU {T} (r1 r2 : rel T) := SimplRel (xrelU r1 r2).
1396 Definition relpre {aT rT} (f : aT -> rT) (r : rel rT) := SimplRel (xrelpre f r).
1397
1398 Notation "[ 'rel' x y | E ]" := (SimplRel (fun x y => E%B))
1399 (only parsing) : function_scope.
1400 Notation "[ 'rel' x y : T | E ]" :=
1401 (SimplRel (fun x y : T => E%B)) (only parsing) : function_scope.
1402
1403 Lemma subrelUl T (r1 r2 : rel T) : subrel r1 (relU r1 r2).
1404 Proof. by move=> x y r1xy; apply/orP; left. Qed.
1405
1406 Lemma subrelUr T (r1 r2 : rel T) : subrel r2 (relU r1 r2).
1407 Proof. by move=> x y r2xy; apply/orP; right. Qed.
1408
1409 (** Variant of simpl_pred specialised to the membership operator. **)
1410
1411 Variant mem_pred T := Mem of pred T.
1412
1413 (**
1414 We mainly declare pred_of_mem as a coercion so that it is not displayed.
1415 Similarly to pred_of_simpl, it will usually not be inserted by type
1416 inference, as all mem_pred mp =~= pred_sort ?pT unification problems will
1417 be solve by the memPredType instance below; pred_of_mem will however
1418 be used if a mem_pred T is used as a {pred T}, which is desirable as it
1419 will avoid a redundant mem in a collective, e.g., passing (mem A) to a lemma
1420 exception a generic collective predicate p : {pred T} and premise x \in P
1421 will display a subgoal x \in A rather than x \in mem A.
1422 Conversely, pred_of_mem will _not_ if it is used id (mem A) is used
1423 applicatively or as a pred T; there the simpl_of_mem coercion defined below
1424 will be used, resulting in a subgoal that displays as mem A x by simplifies
1425 to x \in A.
1426 **)
1427 Coercion pred_of_mem {T} mp : {pred T} := let: Mem p := mp in [eta p].
1428 Canonical memPredType T := PredType (@pred_of_mem T).
1429
1430 Definition in_mem {T} (x : T) mp := pred_of_mem mp x.
1431 Definition eq_mem {T} mp1 mp2 := forall x : T, in_mem x mp1 = in_mem x mp2.
1432 Definition sub_mem {T} mp1 mp2 := forall x : T, in_mem x mp1 -> in_mem x mp2.
1433
1434 Arguments in_mem {T} x mp : simpl never.
1435 Global Typeclasses Opaque eq_mem sub_mem.
1436
1437 (** The [simpl_of_mem; pred_of_simpl] path provides a new mem_pred >-> pred
1438 coercion, but does _not_ override the pred_of_mem : mem_pred >-> pred_sort
1439 explicit coercion declaration above.
1440 **)
1441 Coercion simpl_of_mem {T} mp := SimplPred (fun x : T => in_mem x mp).
1442
1443 Lemma sub_refl T (mp : mem_pred T) : sub_mem mp mp. Proof. by []. Qed.
1444 Arguments sub_refl {T mp} [x] mp_x.
1445
1446 (**
1447 It is essential to interlock the production of the Mem constructor inside
1448 the branch of the predType match, to ensure that unifying mem A with
1449 Mem [eta ?p] sets ?p := toP A (or ?p := P if toP = id and A = [eta P]),
1450 rather than topred pT A, had we put mem A := Mem (topred A).
1451 **)
1452 Definition mem T (pT : predType T) : pT -> mem_pred T :=
1453 let: PredType toP := pT in fun A => Mem [eta toP A].
1454 Arguments mem {T pT} A : rename, simpl never.
1455
1456 Notation "x \in A" := (in_mem x (mem A)) (only parsing) : bool_scope.
1457 Notation "x \in A" := (in_mem x (mem A)) (only printing) : bool_scope.
1458 Notation "x \notin A" := (~~ (x \in A)) : bool_scope.
1459 Notation "A =i B" := (eq_mem (mem A) (mem B)) : type_scope.
1460 Notation "{ 'subset' A <= B }" := (sub_mem (mem A) (mem B)) : type_scope.
1461
1462 Notation "[ 'in' A ]" := (in_mem^~ (mem A))
1463 (at level 0, format "[ 'in' A ]") : function_scope.
1464
1465 Notation "[ 'mem' A ]" :=
1466 (pred_of_simpl (simpl_of_mem (mem A))) (only parsing) : function_scope.
1467
1468 Notation "[ 'predI' A & B ]" := (predI [in A] [in B]) : function_scope.
1469 Notation "[ 'predU' A & B ]" := (predU [in A] [in B]) : function_scope.
1470 Notation "[ 'predD' A & B ]" := (predD [in A] [in B]) : function_scope.
1471 Notation "[ 'predC' A ]" := (predC [in A]) : function_scope.
1472 Notation "[ 'preim' f 'of' A ]" := (preim f [in A]) : function_scope.
1473
1474 Notation "[ 'pred' x 'in' A ]" := [pred x | x \in A] : function_scope.
1475 Notation "[ 'pred' x 'in' A | E ]" := [pred x | x \in A & E] : function_scope.
1476 Notation "[ 'pred' x 'in' A | E1 & E2 ]" :=
1477 [pred x | x \in A & E1 && E2 ] : function_scope.
1478
1479 Notation "[ 'rel' x y 'in' A & B | E ]" :=
1480 [rel x y | (x \in A) && (y \in B) && E] : function_scope.
1481 Notation "[ 'rel' x y 'in' A & B ]" :=
1482 [rel x y | (x \in A) && (y \in B)] : function_scope.
1483 Notation "[ 'rel' x y 'in' A | E ]" := [rel x y in A & A | E] : function_scope.
1484 Notation "[ 'rel' x y 'in' A ]" := [rel x y in A & A] : function_scope.
1485
1486 (** Aliases of pred T that let us tag instances of simpl_pred as applicative
1487 or collective, via bespoke coercions. This tagging will give control over
1488 the simplification behaviour of inE and other rewriting lemmas below.
1489 For this control to work it is crucial that collective_of_simpl _not_
1490 be convertible to either applicative_of_simpl or pred_of_simpl. Indeed
1491 they differ here by a commutative conversion (of the match and lambda).
1492 **)
1493 Definition applicative_pred T := pred T.
1494 Definition collective_pred T := pred T.
1495 Coercion applicative_pred_of_simpl T (sp : simpl_pred T) : applicative_pred T :=
1496 fun_of_simpl sp.
1497 Coercion collective_pred_of_simpl T (sp : simpl_pred T) : collective_pred T :=
1498 let: SimplFun p := sp in p.
1499
1500 (** Explicit simplification rules for predicate application and membership. **)
1501 Section PredicateSimplification.
1502
1503 Variables T : Type.
1504
1505 Implicit Types (p : pred T) (pT : predType T) (sp : simpl_pred T).
1506 Implicit Types (mp : mem_pred T).
1507
1508 (**
1509 The following four bespoke structures provide fine-grained control over
1510 matching the various predicate forms. While all four follow a common pattern
1511 of using a canonical projection to match a particular form of predicate
1512 (in pred T, simpl_pred, mem_pred and mem_pred, respectively), and display
1513 the matched predicate in the structure type, each is in fact used for a
1514 different, specific purpose:
1515 - registered_applicative_pred: this user-facing structure is used to
1516 declare values of type pred T meant to be used applicatively. The
1517 structure parameter merely displays this same value, and is used to avoid
1518 undesirable, visible occurrence of the structure in the right hand side
1519 of rewrite rules such as app_predE.
1520 There is a canonical instance of registered_applicative_pred for values
1521 of the applicative_of_simpl coercion, which handles the
1522 Definition Apred : applicative_pred T := [pred x | ...] idiom.
1523 This instance is mainly intended for the in_applicative component of inE,
1524 in conjunction with manifest_mem_pred and applicative_mem_pred.
1525 - manifest_simpl_pred: the only instance of this structure matches manifest
1526 simpl_pred values of the form SimplPred p, displaying p in the structure
1527 type. This structure is used in in_simpl to detect and selectively expand
1528 collective predicates of this form. An explicit SimplPred p pattern would
1529 _NOT_ work for this purpose, as then the left-hand side of in_simpl would
1530 reduce to in_mem ?x (Mem [eta ?p]) and would thus match _any_ instance
1531 of \in, not just those arising from a manifest simpl_pred.
1532 - manifest_mem_pred: similar to manifest_simpl_pred, the one instance of this
1533 structure matches manifest mem_pred values of the form Mem [eta ?p]. The
1534 purpose is different however: to match and display in ?p the actual
1535 predicate appearing in an ... \in ... expression matched by the left hand
1536 side of the in_applicative component of inE; then
1537 - applicative_mem_pred is a telescope refinement of manifest_mem_pred p with
1538 a default constructor that checks that the predicate p is the value of a
1539 registered_applicative_pred; any unfolding occurring during this check
1540 does _not_ affect the value of p passed to in_applicative, since that
1541 has been fixed earlier by the manifest_mem_pred match. In particular the
1542 definition of a predicate using the applicative_pred_of_simpl idiom above
1543 will not be expanded - this very case is the reason in_applicative uses
1544 a mem_pred telescope in its left hand side. The more straightforward
1545 ?x \in applicative_pred_value ?ap (equivalent to in_mem ?x (Mem ?ap))
1546 with ?ap : registered_applicative_pred ?p would set ?p := [pred x | ...]
1547 rather than ?p := Apred in the example above.
1548 Also note that the in_applicative component of inE must be come before the
1549 in_simpl one, as the latter also matches terms of the form x \in Apred.
1550 Finally, no component of inE matches x \in Acoll, when
1551 Definition Acoll : collective_pred T := [pred x | ...].
1552 as the collective_pred_of_simpl is _not_ convertible to pred_of_simpl. **)
1553
1554 Structure registered_applicative_pred p := RegisteredApplicativePred {
1555 applicative_pred_value :> pred T;
1556 _ : applicative_pred_value = p
1557 }.
1558 Definition ApplicativePred p := RegisteredApplicativePred (erefl p).
1559 Canonical applicative_pred_applicative sp :=
1560 ApplicativePred (applicative_pred_of_simpl sp).
1561
1562 Structure manifest_simpl_pred p := ManifestSimplPred {
1563 simpl_pred_value :> simpl_pred T;
1564 _ : simpl_pred_value = SimplPred p
1565 }.
1566 Canonical expose_simpl_pred p := ManifestSimplPred (erefl (SimplPred p)).
1567
1568 Structure manifest_mem_pred p := ManifestMemPred {
1569 mem_pred_value :> mem_pred T;
1570 _ : mem_pred_value = Mem [eta p]
1571 }.
1572 Canonical expose_mem_pred p := ManifestMemPred (erefl (Mem [eta p])).
1573
1574 Structure applicative_mem_pred p :=
1575 ApplicativeMemPred {applicative_mem_pred_value :> manifest_mem_pred p}.
1576 Canonical check_applicative_mem_pred p (ap : registered_applicative_pred p) :=
1577 [eta @ApplicativeMemPred ap].
1578
1579 Lemma mem_topred pT (pp : pT) : mem (topred pp) = mem pp.
1580 Proof. by case: pT pp. Qed.
1581
1582 Lemma topredE pT x (pp : pT) : topred pp x = (x \in pp).
1583 Proof. by rewrite -mem_topred. Qed.
1584
1585 Lemma app_predE x p (ap : registered_applicative_pred p) : ap x = (x \in p).
1586 Proof. by case: ap => _ /= ->. Qed.
1587
1588 Lemma in_applicative x p (amp : applicative_mem_pred p) : in_mem x amp = p x.
1589 Proof. by case: amp => -[_ /= ->]. Qed.
1590
1591 Lemma in_collective x p (msp : manifest_simpl_pred p) :
1592 (x \in collective_pred_of_simpl msp) = p x.
1593 Proof. by case: msp => _ /= ->. Qed.
1594
1595 Lemma in_simpl x p (msp : manifest_simpl_pred p) :
1596 in_mem x (Mem [eta pred_of_simpl msp]) = p x.
1597 Proof. by case: msp => _ /= ->. Qed.
1598
1599 (**
1600 Because of the explicit eta expansion in the left-hand side, this lemma
1601 should only be used in the left-to-right direction.
1602 **)
1603 Lemma unfold_in x p : (x \in ([eta p] : pred T)) = p x.
1604 Proof. by []. Qed.
1605
1606 Lemma simpl_predE p : SimplPred p =1 p.
1607 Proof. by []. Qed.
1608
1609 Definition inE := (in_applicative, in_simpl, simpl_predE). (* to be extended *)
1610
1611 Lemma mem_simpl sp : mem sp = sp :> pred T.
1612 Proof. by []. Qed.
1613
1614 Definition memE := mem_simpl. (* could be extended *)
1615
1616 Lemma mem_mem mp :
1617 (mem mp = mp) * (mem (mp : simpl_pred T) = mp) * (mem (mp : pred T) = mp).
1618 Proof. by case: mp. Qed.
1619
1620 End PredicateSimplification.
1621
1622 (** Qualifiers and keyed predicates. **)
1623
1624 Variant qualifier (q : nat) T := Qualifier of {pred T}.
1625
1626 Coercion has_quality n T (q : qualifier n T) : {pred T} :=
1627 fun x => let: Qualifier _ p := q in p x.
1628 Arguments has_quality n {T}.
1629
1630 Lemma qualifE n T p x : (x \in @Qualifier n T p) = p x. Proof. by []. Qed.
1631
1632 Notation "x \is A" := (x \in has_quality 0 A) (only parsing) : bool_scope.
1633 Notation "x \is A" := (x \in has_quality 0 A) (only printing) : bool_scope.
1634 Notation "x \is 'a' A" := (x \in has_quality 1 A) (only parsing) : bool_scope.
1635 Notation "x \is 'a' A" := (x \in has_quality 1 A) (only printing) : bool_scope.
1636 Notation "x \is 'an' A" := (x \in has_quality 2 A) (only parsing) : bool_scope.
1637 Notation "x \is 'an' A" := (x \in has_quality 2 A) (only printing) : bool_scope.
1638 Notation "x \isn't A" := (x \notin has_quality 0 A) : bool_scope.
1639 Notation "x \isn't 'a' A" := (x \notin has_quality 1 A) : bool_scope.
1640 Notation "x \isn't 'an' A" := (x \notin has_quality 2 A) : bool_scope.
1641 Notation "[ 'qualify' x | P ]" := (Qualifier 0 (fun x => P%B)) : form_scope.
1642 Notation "[ 'qualify' x : T | P ]" :=
1643 (Qualifier 0 (fun x : T => P%B)) (only parsing) : form_scope.
1644 Notation "[ 'qualify' 'a' x | P ]" := (Qualifier 1 (fun x => P%B)) : form_scope.
1645 Notation "[ 'qualify' 'a' x : T | P ]" :=
1646 (Qualifier 1 (fun x : T => P%B)) (only parsing) : form_scope.
1647 Notation "[ 'qualify' 'an' x | P ]" :=
1648 (Qualifier 2 (fun x => P%B)) : form_scope.
1649 Notation "[ 'qualify' 'an' x : T | P ]" :=
1650 (Qualifier 2 (fun x : T => P%B)) (only parsing) : form_scope.
1651
1652 (** Keyed predicates: support for property-bearing predicate interfaces. **)
1653
1654 Section KeyPred.
1655
1656 Variable T : Type.
1657 Variant pred_key (p : {pred T}) : Prop := DefaultPredKey.
1658
1659 Variable p : {pred T}.
1660 Structure keyed_pred (k : pred_key p) :=
1661 PackKeyedPred {unkey_pred :> {pred T}; _ : unkey_pred =i p}.
1662
1663 Variable k : pred_key p.
1664 Definition KeyedPred := @PackKeyedPred k p (frefl _).
1665
1666 Variable k_p : keyed_pred k.
1667 Lemma keyed_predE : k_p =i p. Proof. by case: k_p. Qed.
1668
1669 (**
1670 Instances that strip the mem cast; the first one has "pred_of_mem" as its
1671 projection head value, while the second has "pred_of_simpl". The latter
1672 has the side benefit of preempting accidental misdeclarations.
1673 Note: pred_of_mem is the registered mem >-> pred_sort coercion, while
1674 [simpl_of_mem; pred_of_simpl] is the mem >-> pred >=> Funclass coercion. We
1675 must write down the coercions explicitly as the Canonical head constant
1676 computation does not strip casts. **)
1677 Canonical keyed_mem :=
1678 @PackKeyedPred k (pred_of_mem (mem k_p)) keyed_predE.
1679 Canonical keyed_mem_simpl :=
1680 @PackKeyedPred k (pred_of_simpl (mem k_p)) keyed_predE.
1681
1682 End KeyPred.
1683
1684 Local Abbreviation in_unkey x S := (x \in @unkey_pred _ S _ _) (only parsing).
1685 Notation "x \in S" := (in_unkey x S) (only printing) : bool_scope.
1686
1687 Section KeyedQualifier.
1688
1689 Variables (T : Type) (n : nat) (q : qualifier n T).
1690
1691 Structure keyed_qualifier (k : pred_key q) :=
1692 PackKeyedQualifier {unkey_qualifier; _ : unkey_qualifier = q}.
1693 Definition KeyedQualifier k := PackKeyedQualifier k (erefl q).
1694 Variables (k : pred_key q) (k_q : keyed_qualifier k).
1695 Fact keyed_qualifier_suproof : unkey_qualifier k_q =i q.
1696 Proof. by case: k_q => /= _ ->. Qed.
1697 Canonical keyed_qualifier_keyed := PackKeyedPred k keyed_qualifier_suproof.
1698
1699 End KeyedQualifier.
1700
1701 Notation "x \is A" :=
1702 (in_unkey x (has_quality 0 A)) (only printing) : bool_scope.
1703 Notation "x \is 'a' A" :=
1704 (in_unkey x (has_quality 1 A)) (only printing) : bool_scope.
1705 Notation "x \is 'an' A" :=
1706 (in_unkey x (has_quality 2 A)) (only printing) : bool_scope.
1707
1708 Module DefaultKeying.
1709
1710 Canonical default_keyed_pred T p := KeyedPred (@DefaultPredKey T p).
1711 Canonical default_keyed_qualifier T n (q : qualifier n T) :=
1712 KeyedQualifier (DefaultPredKey q).
1713
1714 End DefaultKeying.
1715
1716 (** Skolemizing with conditions. **)
1717
1718 Lemma all_tag_cond_dep I T (C : pred I) U :
1719 (forall x, T x) -> (forall x, C x -> {y : T x & U x y}) ->
1720 {f : forall x, T x & forall x, C x -> U x (f x)}.
1721 Proof.
1722 move=> f0 fP; apply: all_tag (fun x y => C x -> U x y) _ => x.
1723 by case Cx: (C x); [case/fP: Cx => y; exists y | exists (f0 x)].
1724 Qed.
1725
1726 Lemma all_tag_cond I T (C : pred I) U :
1727 T -> (forall x, C x -> {y : T & U x y}) ->
1728 {f : I -> T & forall x, C x -> U x (f x)}.
1729 Proof. by move=> y0; apply: all_tag_cond_dep. Qed.
1730
1731 Lemma all_sig_cond_dep I T (C : pred I) P :
1732 (forall x, T x) -> (forall x, C x -> {y : T x | P x y}) ->
1733 {f : forall x, T x | forall x, C x -> P x (f x)}.
1734 Proof. by move=> f0 /(all_tag_cond_dep f0)[f]; exists f. Qed.
1735
1736 Lemma all_sig_cond I T (C : pred I) P :
1737 T -> (forall x, C x -> {y : T | P x y}) ->
1738 {f : I -> T | forall x, C x -> P x (f x)}.
1739 Proof. by move=> y0; apply: all_sig_cond_dep. Qed.
1740
1741 Lemma all_sig2_cond {I T} (C : pred I) P Q :
1742 T -> (forall x, C x -> {y : T | P x y & Q x y}) ->
1743 {f : I -> T | forall x, C x -> P x (f x) & forall x, C x -> Q x (f x)}.
1744 Proof.
1745 by move=> /all_sig_cond/[apply]-[f Pf]; exists f => i Di; have [] := Pf i Di.
1746 Qed.
1747
1748 Section RelationProperties.
1749
1750 (**
1751 Caveat: reflexive should not be used to state lemmas, as auto and trivial
1752 will not expand the constant. **)
1753
1754 Variable T : Type.
1755
1756 Variable R : rel T.
1757
1758 Definition total := forall x y, R x y || R y x.
1759 Definition transitive := forall y x z, R x y -> R y z -> R x z.
1760
1761 Definition symmetric := forall x y, R x y = R y x.
1762 Definition antisymmetric := forall x y, R x y && R y x -> x = y.
1763 Definition pre_symmetric := forall x y, R x y -> R y x.
1764
1765 Lemma symmetric_from_pre : pre_symmetric -> symmetric.
1766 Proof. by move=> symR x y; apply/idP/idP; apply: symR. Qed.
1767
1768 Definition reflexive := forall x, R x x.
1769 Definition irreflexive := forall x, R x x = false.
1770
1771 Definition left_transitive := forall x y, R x y -> R x =1 R y.
1772 Definition right_transitive := forall x y, R x y -> R^~ x =1 R^~ y.
1773
1774 Section PER.
1775
1776 Hypotheses (symR : symmetric) (trR : transitive).
1777
1778 Lemma sym_left_transitive : left_transitive.
1779 Proof. by move=> x y Rxy z; apply/idP/idP; apply: trR; rewrite // symR. Qed.
1780
1781 Lemma sym_right_transitive : right_transitive.
1782 Proof. by move=> x y /sym_left_transitive Rxy z; rewrite !(symR z) Rxy. Qed.
1783
1784 End PER.
1785
1786 (**
1787 We define the equivalence property with prenex quantification so that it
1788 can be localized using the {in ..., ..} form defined below. **)
1789
1790 Definition equivalence_rel := forall x y z, R z z * (R x y -> R x z = R y z).
1791
1792 Lemma equivalence_relP : equivalence_rel <-> reflexive /\ left_transitive.
1793 Proof.
1794 split=> [eqiR | [Rxx trR] x y z]; last by split=> [|/trR->].
1795 by split=> [x | x y Rxy z]; [rewrite (eqiR x x x) | rewrite (eqiR x y z)].
1796 Qed.
1797
1798 End RelationProperties.
1799
1800 Lemma rev_trans T (R : rel T) : transitive R -> transitive (fun x y => R y x).
1801 Proof. by move=> trR x y z Ryx Rzy; apply: trR Rzy Ryx. Qed.
1802
1803 (** Property localization **)
1804
1805 Local Notation "{ 'all1' P }" := (forall x, P x : Prop) (at level 0).
1806 Local Notation "{ 'all2' P }" := (forall x y, P x y : Prop) (at level 0).
1807 Local Notation "{ 'all3' P }" := (forall x y z, P x y z: Prop) (at level 0).
1808 Local Abbreviation ph := (phantom _).
1809
1810 Section LocalProperties.
1811
1812 Variables T1 T2 T3 : Type.
1813
1814 Variables (d1 : mem_pred T1) (d2 : mem_pred T2) (d3 : mem_pred T3).
1815 Local Abbreviation ph := (phantom Prop).
1816
1817 Definition prop_for (x : T1) P & ph {all1 P} := P x.
1818
1819 Lemma forE x P phP : @prop_for x P phP = P x. Proof. by []. Qed.
1820
1821 Definition prop_in1 P & ph {all1 P} :=
1822 forall x, in_mem x d1 -> P x.
1823
1824 Definition prop_in11 P & ph {all2 P} :=
1825 forall x y, in_mem x d1 -> in_mem y d2 -> P x y.
1826
1827 Definition prop_in2 P & ph {all2 P} :=
1828 forall x y, in_mem x d1 -> in_mem y d1 -> P x y.
1829
1830 Definition prop_in111 P & ph {all3 P} :=
1831 forall x y z, in_mem x d1 -> in_mem y d2 -> in_mem z d3 -> P x y z.
1832
1833 Definition prop_in12 P & ph {all3 P} :=
1834 forall x y z, in_mem x d1 -> in_mem y d2 -> in_mem z d2 -> P x y z.
1835
1836 Definition prop_in21 P & ph {all3 P} :=
1837 forall x y z, in_mem x d1 -> in_mem y d1 -> in_mem z d2 -> P x y z.
1838
1839 Definition prop_in3 P & ph {all3 P} :=
1840 forall x y z, in_mem x d1 -> in_mem y d1 -> in_mem z d1 -> P x y z.
1841
1842 Variable f : T1 -> T2.
1843
1844 Definition prop_on1 Pf P & phantom T3 (Pf f) & ph {all1 P} :=
1845 forall x, in_mem (f x) d2 -> P x.
1846
1847 Definition prop_on2 Pf P & phantom T3 (Pf f) & ph {all2 P} :=
1848 forall x y, in_mem (f x) d2 -> in_mem (f y) d2 -> P x y.
1849
1850 End LocalProperties.
1851
1852 Definition inPhantom := Phantom Prop.
1853 Definition onPhantom {T} P (x : T) := Phantom Prop (P x).
1854
1855 Definition bijective_in aT rT (d : mem_pred aT) (f : aT -> rT) :=
1856 exists2 g, prop_in1 d (inPhantom (cancel f g))
1857 & prop_on1 d (Phantom _ (cancel g)) (onPhantom (cancel g) f).
1858
1859 Definition bijective_on aT rT (cd : mem_pred rT) (f : aT -> rT) :=
1860 exists2 g, prop_on1 cd (Phantom _ (cancel f)) (onPhantom (cancel f) g)
1861 & prop_in1 cd (inPhantom (cancel g f)).
1862
1863 Notation "{ 'for' x , P }" := (prop_for x (inPhantom P)) : type_scope.
1864 Notation "{ 'in' d , P }" := (prop_in1 (mem d) (inPhantom P)) : type_scope.
1865 Notation "{ 'in' d1 & d2 , P }" :=
1866 (prop_in11 (mem d1) (mem d2) (inPhantom P)) : type_scope.
1867 Notation "{ 'in' d & , P }" := (prop_in2 (mem d) (inPhantom P)) : type_scope.
1868 Notation "{ 'in' d1 & d2 & d3 , P }" :=
1869 (prop_in111 (mem d1) (mem d2) (mem d3) (inPhantom P)) : type_scope.
1870 Notation "{ 'in' d1 & & d3 , P }" :=
1871 (prop_in21 (mem d1) (mem d3) (inPhantom P)) : type_scope.
1872 Notation "{ 'in' d1 & d2 & , P }" :=
1873 (prop_in12 (mem d1) (mem d2) (inPhantom P)) : type_scope.
1874 Notation "{ 'in' d & & , P }" := (prop_in3 (mem d) (inPhantom P)) : type_scope.
1875 Notation "{ 'on' cd , P }" :=
1876 (prop_on1 (mem cd) (inPhantom P) (inPhantom P)) : type_scope.
1877
1878 Notation "{ 'on' cd & , P }" :=
1879 (prop_on2 (mem cd) (inPhantom P) (inPhantom P)) : type_scope.
1880
1881 Local Arguments onPhantom : clear scopes.
1882 Notation "{ 'on' cd , P & g }" :=
1883 (prop_on1 (mem cd) (Phantom (_ -> Prop) P) (onPhantom P g)) : type_scope.
1884 Notation "{ 'in' d , 'bijective' f }" := (bijective_in (mem d) f) : type_scope.
1885 Notation "{ 'on' cd , 'bijective' f }" :=
1886 (bijective_on (mem cd) f) : type_scope.
1887
1888 (**
1889 Weakening and monotonicity lemmas for localized predicates.
1890 Note that using these lemmas in backward reasoning will force expansion of
1891 the predicate definition, as Rocq needs to expose the quantifier to apply
1892 these lemmas. We define a few specialized variants to avoid this for some
1893 of the ssrfun predicates. **)
1894
1895 Section LocalGlobal.
1896
1897 Variables T1 T2 T3 : predArgType.
1898 Variables (D1 : {pred T1}) (D2 : {pred T2}) (D3 : {pred T3}).
1899 Variables (d1 d1' : mem_pred T1) (d2 d2' : mem_pred T2) (d3 d3' : mem_pred T3).
1900 Variables (f f' : T1 -> T2) (g : T2 -> T1) (h : T3).
1901 Variables (P1 : T1 -> Prop) (P2 : T1 -> T2 -> Prop).
1902 Variable P3 : T1 -> T2 -> T3 -> Prop.
1903 Variable Q1 : (T1 -> T2) -> T1 -> Prop.
1904 Variable Q1l : (T1 -> T2) -> T3 -> T1 -> Prop.
1905 Variable Q2 : (T1 -> T2) -> T1 -> T1 -> Prop.
1906
1907 Hypothesis sub1 : sub_mem d1 d1'.
1908 Hypothesis sub2 : sub_mem d2 d2'.
1909 Hypothesis sub3 : sub_mem d3 d3'.
1910
1911 Lemma in1W : {all1 P1} -> {in D1, {all1 P1}}.
1912 Proof. by move=> ? ?. Qed.
1913 Lemma in2W : {all2 P2} -> {in D1 & D2, {all2 P2}}.
1914 Proof. by move=> ? ?. Qed.
1915 Lemma in3W : {all3 P3} -> {in D1 & D2 & D3, {all3 P3}}.
1916 Proof. by move=> ? ?. Qed.
1917
1918 Lemma in1T : {in T1, {all1 P1}} -> {all1 P1}.
1919 Proof. by move=> ? ?; auto. Qed.
1920 Lemma in2T : {in T1 & T2, {all2 P2}} -> {all2 P2}.
1921 Proof. by move=> ? ?; auto. Qed.
1922 Lemma in3T : {in T1 & T2 & T3, {all3 P3}} -> {all3 P3}.
1923 Proof. by move=> ? ?; auto. Qed.
1924
1925 Lemma sub_in1 (Ph : ph {all1 P1}) : prop_in1 d1' Ph -> prop_in1 d1 Ph.
1926 Proof. by move=> allP x /sub1; apply: allP. Qed.
1927
1928 Lemma sub_in11 (Ph : ph {all2 P2}) : prop_in11 d1' d2' Ph -> prop_in11 d1 d2 Ph.
1929 Proof. by move=> allP x1 x2 /sub1 d1x1 /sub2; apply: allP. Qed.
1930
1931 Lemma sub_in111 (Ph : ph {all3 P3}) :
1932 prop_in111 d1' d2' d3' Ph -> prop_in111 d1 d2 d3 Ph.
1933 Proof. by move=> allP x1 x2 x3 /sub1 d1x1 /sub2 d2x2 /sub3; apply: allP. Qed.
1934
1935 Let allQ1 f'' := {all1 Q1 f''}.
1936 Let allQ1l f'' h' := {all1 Q1l f'' h'}.
1937 Let allQ2 f'' := {all2 Q2 f''}.
1938
1939 Lemma on1W : allQ1 f -> {on D2, allQ1 f}. Proof. by move=> ? ?. Qed.
1940
1941 Lemma on1lW : allQ1l f h -> {on D2, allQ1l f & h}. Proof. by move=> ? ?. Qed.
1942
1943 Lemma on2W : allQ2 f -> {on D2 &, allQ2 f}. Proof. by move=> ? ?. Qed.
1944
1945 Lemma on1T : {on T2, allQ1 f} -> allQ1 f. Proof. by move=> ? ?; auto. Qed.
1946
1947 Lemma on1lT : {on T2, allQ1l f & h} -> allQ1l f h.
1948 Proof. by move=> ? ?; auto. Qed.
1949
1950 Lemma on2T : {on T2 &, allQ2 f} -> allQ2 f.
1951 Proof. by move=> ? ?; auto. Qed.
1952
1953 Lemma subon1 (Phf : ph (allQ1 f)) (Ph : ph (allQ1 f)) :
1954 prop_on1 d2' Phf Ph -> prop_on1 d2 Phf Ph.
1955 Proof. by move=> allQ x /sub2; apply: allQ. Qed.
1956
1957 Lemma subon1l (Phf : ph (allQ1l f)) (Ph : ph (allQ1l f h)) :
1958 prop_on1 d2' Phf Ph -> prop_on1 d2 Phf Ph.
1959 Proof. by move=> allQ x /sub2; apply: allQ. Qed.
1960
1961 Lemma subon2 (Phf : ph (allQ2 f)) (Ph : ph (allQ2 f)) :
1962 prop_on2 d2' Phf Ph -> prop_on2 d2 Phf Ph.
1963 Proof. by move=> allQ x y /sub2=> d2fx /sub2; apply: allQ. Qed.
1964
1965 Lemma can_in_inj : {in D1, cancel f g} -> {in D1 &, injective f}.
1966 Proof. by move=> fK x y /fK{2}<- /fK{2}<- ->. Qed.
1967
1968 Lemma canLR_in x y : {in D1, cancel f g} -> y \in D1 -> x = f y -> g x = y.
1969 Proof. by move=> fK D1y ->; rewrite fK. Qed.
1970
1971 Lemma canRL_in x y : {in D1, cancel f g} -> x \in D1 -> f x = y -> x = g y.
1972 Proof. by move=> fK D1x <-; rewrite fK. Qed.
1973
1974 Lemma on_can_inj : {on D2, cancel f & g} -> {on D2 &, injective f}.
1975 Proof. by move=> fK x y /fK{2}<- /fK{2}<- ->. Qed.
1976
1977 Lemma canLR_on x y : {on D2, cancel f & g} -> f y \in D2 -> x = f y -> g x = y.
1978 Proof. by move=> fK D2fy ->; rewrite fK. Qed.
1979
1980 Lemma canRL_on x y : {on D2, cancel f & g} -> f x \in D2 -> f x = y -> x = g y.
1981 Proof. by move=> fK D2fx <-; rewrite fK. Qed.
1982
1983 Lemma inW_bij : bijective f -> {in D1, bijective f}.
1984 Proof. by case=> g' fK g'K; exists g' => * ? *; auto. Qed.
1985
1986 Lemma onW_bij : bijective f -> {on D2, bijective f}.
1987 Proof. by case=> g' fK g'K; exists g' => * ? *; auto. Qed.
1988
1989 Lemma inT_bij : {in T1, bijective f} -> bijective f.
1990 Proof. by case=> g' fK g'K; exists g' => * ? *; auto. Qed.
1991
1992 Lemma onT_bij : {on T2, bijective f} -> bijective f.
1993 Proof. by case=> g' fK g'K; exists g' => * ? *; auto. Qed.
1994
1995 Lemma sub_in_bij (D1' : pred T1) :
1996 {subset D1 <= D1'} -> {in D1', bijective f} -> {in D1, bijective f}.
1997 Proof.
1998 by move=> subD [g' fK g'K]; exists g' => x; move/subD; [apply: fK | apply: g'K].
1999 Qed.
2000
2001 Lemma subon_bij (D2' : pred T2) :
2002 {subset D2 <= D2'} -> {on D2', bijective f} -> {on D2, bijective f}.
2003 Proof.
2004 by move=> subD [g' fK g'K]; exists g' => x; move/subD; [apply: fK | apply: g'K].
2005 Qed.
2006
2007 Lemma in_on1P : {in D1, {on D2, allQ1 f}} <->
2008 {in [pred x in D1 | f x \in D2], allQ1 f}.
2009 Proof.
2010 split => allf x; have := allf x; rewrite inE => Q1f; first by case/andP.
2011 by move=> ? ?; apply: Q1f; apply/andP.
2012 Qed.
2013
2014 Lemma in_on1lP : {in D1, {on D2, allQ1l f & h}} <->
2015 {in [pred x in D1 | f x \in D2], allQ1l f h}.
2016 Proof.
2017 split => allf x; have := allf x; rewrite inE => Q1f; first by case/andP.
2018 by move=> ? ?; apply: Q1f; apply/andP.
2019 Qed.
2020
2021 Lemma in_on2P : {in D1 &, {on D2 &, allQ2 f}} <->
2022 {in [pred x in D1 | f x \in D2] &, allQ2 f}.
2023 Proof.
2024 split => allf x y; have := allf x y; rewrite !inE => Q2f.
2025 by move=> /andP[? ?] /andP[? ?]; apply: Q2f.
2026 by move=> ? ? ? ?; apply: Q2f; apply/andP.
2027 Qed.
2028
2029 Lemma on1W_in : {in D1, allQ1 f} -> {in D1, {on D2, allQ1 f}}.
2030 Proof. by move=> D1f ? /D1f. Qed.
2031
2032 Lemma on1lW_in : {in D1, allQ1l f h} -> {in D1, {on D2, allQ1l f & h}}.
2033 Proof. by move=> D1f ? /D1f. Qed.
2034
2035 Lemma on2W_in : {in D1 &, allQ2 f} -> {in D1 &, {on D2 &, allQ2 f}}.
2036 Proof. by move=> D1f ? ? ? ? ? ?; apply: D1f. Qed.
2037
2038 Lemma in_on1W : allQ1 f -> {in D1, {on D2, allQ1 f}}.
2039 Proof. by move=> allf ? ? ?; apply: allf. Qed.
2040
2041 Lemma in_on1lW : allQ1l f h -> {in D1, {on D2, allQ1l f & h}}.
2042 Proof. by move=> allf ? ? ?; apply: allf. Qed.
2043
2044 Lemma in_on2W : allQ2 f -> {in D1 &, {on D2 &, allQ2 f}}.
2045 Proof. by move=> allf ? ? ? ? ? ?; apply: allf. Qed.
2046
2047 Lemma on1S : (forall x, f x \in D2) -> {on D2, allQ1 f} -> allQ1 f.
2048 Proof. by move=> ? fD1 ?; apply: fD1. Qed.
2049
2050 Lemma on1lS : (forall x, f x \in D2) -> {on D2, allQ1l f & h} -> allQ1l f h.
2051 Proof. by move=> ? fD1 ?; apply: fD1. Qed.
2052
2053 Lemma on2S : (forall x, f x \in D2) -> {on D2 &, allQ2 f} -> allQ2 f.
2054 Proof. by move=> ? fD1 ? ?; apply: fD1. Qed.
2055
2056 Lemma on1S_in : {homo f : x / x \in D1 >-> x \in D2} ->
2057 {in D1, {on D2, allQ1 f}} -> {in D1, allQ1 f}.
2058 Proof. by move=> fD fD1 ? ?; apply/fD1/fD. Qed.
2059
2060 Lemma on1lS_in : {homo f : x / x \in D1 >-> x \in D2} ->
2061 {in D1, {on D2, allQ1l f & h}} -> {in D1, allQ1l f h}.
2062 Proof. by move=> fD fD1 ? ?; apply/fD1/fD. Qed.
2063
2064 Lemma on2S_in : {homo f : x / x \in D1 >-> x \in D2} ->
2065 {in D1 &, {on D2 &, allQ2 f}} -> {in D1 &, allQ2 f}.
2066 Proof. by move=> fD fD1 ? ? ? ?; apply: fD1 => //; apply: fD. Qed.
2067
2068 Lemma in_on1S : (forall x, f x \in D2) -> {in T1, {on D2, allQ1 f}} -> allQ1 f.
2069 Proof. by move=> fD2 fD1 ?; apply: fD1. Qed.
2070
2071 Lemma in_on1lS : (forall x, f x \in D2) ->
2072 {in T1, {on D2, allQ1l f & h}} -> allQ1l f h.
2073 Proof. by move=> fD2 fD1 ?; apply: fD1. Qed.
2074
2075 Lemma in_on2S : (forall x, f x \in D2) ->
2076 {in T1 &, {on D2 &, allQ2 f}} -> allQ2 f.
2077 Proof. by move=> fD2 fD1 ? ?; apply: fD1. Qed.
2078
2079 End LocalGlobal.
2080 Arguments in_on1P {T1 T2 D1 D2 f Q1}.
2081 Arguments in_on1lP {T1 T2 T3 D1 D2 f h Q1l}.
2082 Arguments in_on2P {T1 T2 D1 D2 f Q2}.
2083 Arguments on1W_in {T1 T2 D1} D2 {f Q1}.
2084 Arguments on1lW_in {T1 T2 T3 D1} D2 {f h Q1l}.
2085 Arguments on2W_in {T1 T2 D1} D2 {f Q2}.
2086 Arguments in_on1W {T1 T2} D1 D2 {f Q1}.
2087 Arguments in_on1lW {T1 T2 T3} D1 D2 {f h Q1l}.
2088 Arguments in_on2W {T1 T2} D1 D2 {f Q2}.
2089 Arguments on1S {T1 T2} D2 {f Q1}.
2090 Arguments on1lS {T1 T2 T3} D2 {f h Q1l}.
2091 Arguments on2S {T1 T2} D2 {f Q2}.
2092 Arguments on1S_in {T1 T2 D1} D2 {f Q1}.
2093 Arguments on1lS_in {T1 T2 T3 D1} D2 {f h Q1l}.
2094 Arguments on2S_in {T1 T2 D1} D2 {f Q2}.
2095 Arguments in_on1S {T1 T2} D2 {f Q1}.
2096 Arguments in_on1lS {T1 T2 T3} D2 {f h Q1l}.
2097 Arguments in_on2S {T1 T2} D2 {f Q2}.
2098
2099 Lemma can_in_pcan [rT aT : Type] (A : {pred aT}) [f : aT -> rT] [g : rT -> aT] :
2100 {in A, cancel f g} -> {in A, pcancel f (fun y : rT => Some (g y))}.
2101 Proof. by move=> fK x Ax; rewrite fK. Qed.
2102
2103 Lemma pcan_in_inj [rT aT : Type] [A : {pred aT}]
2104 [f : aT -> rT] [g : rT -> option aT] :
2105 {in A, pcancel f g} -> {in A &, injective f}.
2106 Proof. by move=> fK x y Ax Ay /(congr1 g); rewrite !fK// => -[]. Qed.
2107
2108 Lemma in_inj_comp A B C (f : B -> A) (h : C -> B) (P : pred B) (Q : pred C) :
2109 {in P &, injective f} -> {in Q &, injective h} -> {homo h : x / Q x >-> P x} ->
2110 {in Q &, injective (f \o h)}.
2111 Proof.
2112 by move=> Pf Qh QP x y xQ yQ xy; apply Qh => //; apply Pf => //; apply QP.
2113 Qed.
2114
2115 Lemma can_in_comp [A B C : Type] (D : {pred B}) (D' : {pred C})
2116 [f : B -> A] [h : C -> B] [f' : A -> B] [h' : B -> C] :
2117 {homo h : x / x \in D' >-> x \in D} ->
2118 {in D, cancel f f'} -> {in D', cancel h h'} ->
2119 {in D', cancel (f \o h) (h' \o f')}.
2120 Proof. by move=> hD fK hK c cD /=; rewrite fK ?hK ?hD. Qed.
2121
2122 Lemma pcan_in_comp [A B C : Type] (D : {pred B}) (D' : {pred C})
2123 [f : B -> A] [h : C -> B] [f' : A -> option B] [h' : B -> option C] :
2124 {homo h : x / x \in D' >-> x \in D} ->
2125 {in D, pcancel f f'} -> {in D', pcancel h h'} ->
2126 {in D', pcancel (f \o h) (obind h' \o f')}.
2127 Proof. by move=> hD fK hK c cD /=; rewrite fK/= ?hK ?hD. Qed.
2128
2129 Definition pred_oapp T (D : {pred T}) : pred (option T) :=
2130 [pred x | oapp (mem D) false x].
2131
2132 Lemma ocan_in_comp [A B C : Type] (D : {pred B}) (D' : {pred C})
2133 [f : B -> option A] [h : C -> option B] [f' : A -> B] [h' : B -> C] :
2134 {homo h : x / x \in D' >-> x \in pred_oapp D} ->
2135 {in D, ocancel f f'} -> {in D', ocancel h h'} ->
2136 {in D', ocancel (obind f \o h) (h' \o f')}.
2137 Proof.
2138 move=> hD fK hK c cD /=; rewrite -[RHS]hK/=; case hcE : (h c) => [b|]//=.
2139 have bD : b \in D by have := hD _ cD; rewrite hcE inE.
2140 by rewrite -[b in RHS]fK; case: (f b) => //=; have /hK := cD; rewrite hcE.
2141 Qed.
2142
2143 Section in_sig.
2144
2145 Variables T1 T2 T3 : Type.
2146 Variables (D1 : {pred T1}) (D2 : {pred T2}) (D3 : {pred T3}).
2147 Variable P1 : T1 -> Prop.
2148 Variable P2 : T1 -> T2 -> Prop.
2149 Variable P3 : T1 -> T2 -> T3 -> Prop.
2150
2151 Lemma in1_sig : {in D1, {all1 P1}} -> forall x : sig D1, P1 (sval x).
2152 Proof. by move=> DP [x Dx]; have := DP _ Dx. Qed.
2153
2154 Lemma in2_sig : {in D1 & D2, {all2 P2}} ->
2155 forall (x : sig D1) (y : sig D2), P2 (sval x) (sval y).
2156 Proof. by move=> DP [x Dx] [y Dy]; have := DP _ _ Dx Dy. Qed.
2157
2158 Lemma in3_sig : {in D1 & D2 & D3, {all3 P3}} ->
2159 forall (x : sig D1) (y : sig D2) (z : sig D3), P3 (sval x) (sval y) (sval z).
2160 Proof. by move=> DP [x Dx] [y Dy] [z Dz]; have := DP _ _ _ Dx Dy Dz. Qed.
2161
2162 End in_sig.
2163 Arguments in1_sig {T1 D1 P1}.
2164 Arguments in2_sig {T1 T2 D1 D2 P2}.
2165 Arguments in3_sig {T1 T2 T3 D1 D2 D3 P3}.
2166
2167 Lemma sub_in2 T d d' (P : T -> T -> Prop) :
2168 sub_mem d d' -> forall Ph : ph {all2 P}, prop_in2 d' Ph -> prop_in2 d Ph.
2169 Proof. by move=> /= sub_dd'; apply: sub_in11. Qed.
2170
2171 Lemma sub_in3 T d d' (P : T -> T -> T -> Prop) :
2172 sub_mem d d' -> forall Ph : ph {all3 P}, prop_in3 d' Ph -> prop_in3 d Ph.
2173 Proof. by move=> /= sub_dd'; apply: sub_in111. Qed.
2174
2175 Lemma sub_in12 T1 T d1 d1' d d' (P : T1 -> T -> T -> Prop) :
2176 sub_mem d1 d1' -> sub_mem d d' ->
2177 forall Ph : ph {all3 P}, prop_in12 d1' d' Ph -> prop_in12 d1 d Ph.
2178 Proof. by move=> /= sub1 sub; apply: sub_in111. Qed.
2179
2180 Lemma sub_in21 T T3 d d' d3 d3' (P : T -> T -> T3 -> Prop) :
2181 sub_mem d d' -> sub_mem d3 d3' ->
2182 forall Ph : ph {all3 P}, prop_in21 d' d3' Ph -> prop_in21 d d3 Ph.
2183 Proof. by move=> /= sub sub3; apply: sub_in111. Qed.
2184
2185 Lemma equivalence_relP_in T (R : rel T) (A : pred T) :
2186 {in A & &, equivalence_rel R}
2187 <-> {in A, reflexive R} /\ {in A &, forall x y, R x y -> {in A, R x =1 R y}}.
2188 Proof.
2189 split=> [eqiR | [Rxx trR] x y z *]; last by split=> [|/trR-> //]; apply: Rxx.
2190 by split=> [x Ax|x y Ax Ay Rxy z Az]; [rewrite (eqiR x x) | rewrite (eqiR x y)].
2191 Qed.
2192
2193 Section MonoHomoMorphismTheory.
2194
2195 Variables (aT rT sT : Type) (f : aT -> rT) (g : rT -> aT).
2196 Variables (aP : pred aT) (rP : pred rT) (aR : rel aT) (rR : rel rT).
2197
2198 Lemma monoW : {mono f : x / aP x >-> rP x} -> {homo f : x / aP x >-> rP x}.
2199 Proof. by move=> hf x ax; rewrite hf. Qed.
2200
2201 Lemma mono2W :
2202 {mono f : x y / aR x y >-> rR x y} -> {homo f : x y / aR x y >-> rR x y}.
2203 Proof. by move=> hf x y axy; rewrite hf. Qed.
2204
2205 Hypothesis fgK : cancel g f.
2206
2207 Lemma homoRL :
2208 {homo f : x y / aR x y >-> rR x y} -> forall x y, aR (g x) y -> rR x (f y).
2209 Proof. by move=> Hf x y /Hf; rewrite fgK. Qed.
2210
2211 Lemma homoLR :
2212 {homo f : x y / aR x y >-> rR x y} -> forall x y, aR x (g y) -> rR (f x) y.
2213 Proof. by move=> Hf x y /Hf; rewrite fgK. Qed.
2214
2215 Lemma homo_mono :
2216 {homo f : x y / aR x y >-> rR x y} -> {homo g : x y / rR x y >-> aR x y} ->
2217 {mono g : x y / rR x y >-> aR x y}.
2218 Proof.
2219 move=> mf mg x y; case: (boolP (rR _ _))=> [/mg //|].
2220 by apply: contraNF=> /mf; rewrite !fgK.
2221 Qed.
2222
2223 Lemma monoLR :
2224 {mono f : x y / aR x y >-> rR x y} -> forall x y, rR (f x) y = aR x (g y).
2225 Proof. by move=> mf x y; rewrite -{1}[y]fgK mf. Qed.
2226
2227 Lemma monoRL :
2228 {mono f : x y / aR x y >-> rR x y} -> forall x y, rR x (f y) = aR (g x) y.
2229 Proof. by move=> mf x y; rewrite -{1}[x]fgK mf. Qed.
2230
2231 Lemma can_mono :
2232 {mono f : x y / aR x y >-> rR x y} -> {mono g : x y / rR x y >-> aR x y}.
2233 Proof. by move=> mf x y /=; rewrite -mf !fgK. Qed.
2234
2235 End MonoHomoMorphismTheory.
2236
2237 Section MonoHomoMorphismTheory_in.
2238
2239 Variables (aT rT : predArgType) (f : aT -> rT) (g : rT -> aT).
2240 Variables (aD : {pred aT}) (rD : {pred rT}).
2241 Variable (aP : pred aT) (rP : pred rT) (aR : rel aT) (rR : rel rT).
2242
2243 Lemma mono1W_in :
2244 {in aD, {mono f : x / aP x >-> rP x}} ->
2245 {in aD, {homo f : x / aP x >-> rP x}}.
2246 Proof. by move=> hf x hx ax; rewrite hf. Qed.
2247 #[deprecated(since="Coq 8.16", note="Use mono1W_in instead.")]
2248 Abbreviation mono2W_in := mono1W_in.
2249
2250 Lemma monoW_in :
2251 {in aD &, {mono f : x y / aR x y >-> rR x y}} ->
2252 {in aD &, {homo f : x y / aR x y >-> rR x y}}.
2253 Proof. by move=> hf x y hx hy axy; rewrite hf. Qed.
2254
2255 Hypothesis fgK : {in rD, {on aD, cancel g & f}}.
2256 Hypothesis mem_g : {homo g : x / x \in rD >-> x \in aD}.
2257
2258 Lemma homoRL_in :
2259 {in aD &, {homo f : x y / aR x y >-> rR x y}} ->
2260 {in rD & aD, forall x y, aR (g x) y -> rR x (f y)}.
2261 Proof. by move=> Hf x y hx hy /Hf; rewrite fgK ?mem_g// ?inE; apply. Qed.
2262
2263 Lemma homoLR_in :
2264 {in aD &, {homo f : x y / aR x y >-> rR x y}} ->
2265 {in aD & rD, forall x y, aR x (g y) -> rR (f x) y}.
2266 Proof. by move=> Hf x y hx hy /Hf; rewrite fgK ?mem_g// ?inE; apply. Qed.
2267
2268 Lemma homo_mono_in :
2269 {in aD &, {homo f : x y / aR x y >-> rR x y}} ->
2270 {in rD &, {homo g : x y / rR x y >-> aR x y}} ->
2271 {in rD &, {mono g : x y / rR x y >-> aR x y}}.
2272 Proof.
2273 move=> mf mg x y hx hy; case: (boolP (rR _ _))=> [/mg //|]; first exact.
2274 by apply: contraNF=> /mf; rewrite !fgK ?mem_g//; apply.
2275 Qed.
2276
2277 Lemma monoLR_in :
2278 {in aD &, {mono f : x y / aR x y >-> rR x y}} ->
2279 {in aD & rD, forall x y, rR (f x) y = aR x (g y)}.
2280 Proof. by move=> mf x y hx hy; rewrite -{1}[y]fgK ?mem_g// mf ?mem_g. Qed.
2281
2282 Lemma monoRL_in :
2283 {in aD &, {mono f : x y / aR x y >-> rR x y}} ->
2284 {in rD & aD, forall x y, rR x (f y) = aR (g x) y}.
2285 Proof. by move=> mf x y hx hy; rewrite -{1}[x]fgK ?mem_g// mf ?mem_g. Qed.
2286
2287 Lemma can_mono_in :
2288 {in aD &, {mono f : x y / aR x y >-> rR x y}} ->
2289 {in rD &, {mono g : x y / rR x y >-> aR x y}}.
2290 Proof. by move=> mf x y hx hy; rewrite -mf ?mem_g// !fgK ?mem_g. Qed.
2291
2292 End MonoHomoMorphismTheory_in.
2293 Arguments homoRL_in {aT rT f g aD rD aR rR}.
2294 Arguments homoLR_in {aT rT f g aD rD aR rR}.
2295 Arguments homo_mono_in {aT rT f g aD rD aR rR}.
2296 Arguments monoLR_in {aT rT f g aD rD aR rR}.
2297 Arguments monoRL_in {aT rT f g aD rD aR rR}.
2298 Arguments can_mono_in {aT rT f g aD rD aR rR}.
2299
2300 Section HomoMonoMorphismFlip.
2301 Variables (aT rT : Type) (aR : rel aT) (rR : rel rT) (f : aT -> rT).
2302 Variable (aD aD' : {pred aT}).
2303
2304 Lemma homo_sym : {homo f : x y / aR x y >-> rR x y} ->
2305 {homo f : y x / aR x y >-> rR x y}.
2306 Proof. by move=> fR y x; apply: fR. Qed.
2307
2308 Lemma mono_sym : {mono f : x y / aR x y >-> rR x y} ->
2309 {mono f : y x / aR x y >-> rR x y}.
2310 Proof. by move=> fR y x; apply: fR. Qed.
2311
2312 Lemma homo_sym_in : {in aD &, {homo f : x y / aR x y >-> rR x y}} ->
2313 {in aD &, {homo f : y x / aR x y >-> rR x y}}.
2314 Proof. by move=> fR y x yD xD; apply: fR. Qed.
2315
2316 Lemma mono_sym_in : {in aD &, {mono f : x y / aR x y >-> rR x y}} ->
2317 {in aD &, {mono f : y x / aR x y >-> rR x y}}.
2318 Proof. by move=> fR y x yD xD; apply: fR. Qed.
2319
2320 Lemma homo_sym_in11 : {in aD & aD', {homo f : x y / aR x y >-> rR x y}} ->
2321 {in aD' & aD, {homo f : y x / aR x y >-> rR x y}}.
2322 Proof. by move=> fR y x yD xD; apply: fR. Qed.
2323
2324 Lemma mono_sym_in11 : {in aD & aD', {mono f : x y / aR x y >-> rR x y}} ->
2325 {in aD' & aD, {mono f : y x / aR x y >-> rR x y}}.
2326 Proof. by move=> fR y x yD xD; apply: fR. Qed.
2327
2328 End HomoMonoMorphismFlip.
2329 Arguments homo_sym {aT rT} [aR rR f].
2330 Arguments mono_sym {aT rT} [aR rR f].
2331 Arguments homo_sym_in {aT rT} [aR rR f aD].
2332 Arguments mono_sym_in {aT rT} [aR rR f aD].
2333 Arguments homo_sym_in11 {aT rT} [aR rR f aD aD'].
2334 Arguments mono_sym_in11 {aT rT} [aR rR f aD aD'].
2335
2336 Section CancelOn.
2337
2338 Variables (aT rT : predArgType) (aD : {pred aT}) (rD : {pred rT}).
2339 Variables (f : aT -> rT) (g : rT -> aT).
2340
2341 Lemma onW_can : cancel g f -> {on aD, cancel g & f}.
2342 Proof. by move=> fgK x xaD; apply: fgK. Qed.
2343
2344 Lemma onW_can_in : {in rD, cancel g f} -> {in rD, {on aD, cancel g & f}}.
2345 Proof. by move=> fgK x xrD xaD; apply: fgK. Qed.
2346
2347 Lemma in_onW_can : cancel g f -> {in rD, {on aD, cancel g & f}}.
2348 Proof. by move=> fgK x xrD xaD; apply: fgK. Qed.
2349
2350 Lemma onS_can : (forall x, g x \in aD) -> {on aD, cancel g & f} -> cancel g f.
2351 Proof. by move=> mem_g fgK x; apply: fgK. Qed.
2352
2353 Lemma onS_can_in : {homo g : x / x \in rD >-> x \in aD} ->
2354 {in rD, {on aD, cancel g & f}} -> {in rD, cancel g f}.
2355 Proof. by move=> mem_g fgK x x_rD; apply/fgK/mem_g. Qed.
2356
2357 Lemma in_onS_can : (forall x, g x \in aD) ->
2358 {in rT, {on aD, cancel g & f}} -> cancel g f.
2359 Proof. by move=> mem_g fgK x; apply/fgK. Qed.
2360
2361 End CancelOn.
2362 Arguments onW_can {aT rT} aD {f g}.
2363 Arguments onW_can_in {aT rT} aD {rD f g}.
2364 Arguments in_onW_can {aT rT} aD rD {f g}.
2365 Arguments onS_can {aT rT} aD {f g}.
2366 Arguments onS_can_in {aT rT} aD {rD f g}.
2367 Arguments in_onS_can {aT rT} aD {f g}.
2368
2369 Section inj_can_sym_in_on.
2370 Variables (aT rT : predArgType) (aD : {pred aT}) (rD : {pred rT}).
2371 Variables (f : aT -> rT) (g : rT -> aT).
2372
2373 Lemma inj_can_sym_in_on :
2374 {homo f : x / x \in aD >-> x \in rD} -> {in aD, {on rD, cancel f & g}} ->
2375 {in rD &, {on aD &, injective g}} -> {in rD, {on aD, cancel g & f}}.
2376 Proof. by move=> fD fK gI x x_rD gx_aD; apply: gI; rewrite ?inE ?fK ?fD. Qed.
2377
2378 Lemma inj_can_sym_on : {in aD, cancel f g} ->
2379 {on aD &, injective g} -> {on aD, cancel g & f}.
2380 Proof. by move=> fK gI x gx_aD; apply: gI; rewrite ?inE ?fK. Qed.
2381
2382 Lemma inj_can_sym_in : {homo f \o g : x / x \in rD} -> {on rD, cancel f & g} ->
2383 {in rD &, injective g} -> {in rD, cancel g f}.
2384 Proof. by move=> fgD fK gI x x_rD; apply: gI; rewrite ?fK ?fgD. Qed.
2385
2386 End inj_can_sym_in_on.
2387 Arguments inj_can_sym_in_on {aT rT aD rD f g}.
2388 Arguments inj_can_sym_on {aT rT aD f g}.
2389 Arguments inj_can_sym_in {aT rT rD f g}.