Debian Codegraph @

theories/Corelib/ssr/ssrfun.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.
16
17
18 (**
19 This file contains the basic definitions and notations for working with
20 functions. The definitions provide for:
21
22 - Pair projections:
23 p.1 == first element of a pair
24 p.2 == second element of a pair
25 These notations also apply to p : P /\ Q, via an and >-> pair coercion.
26
27 - Simplifying functions, beta-reduced by /= and simpl:
28 #[#fun : T => E#]# == constant function from type T that returns E
29 #[#fun x => E#]# == unary function
30 #[#fun x : T => E#]# == unary function with explicit domain type
31 #[#fun x y => E#]# == binary function
32 #[#fun x y : T => E#]# == binary function with common domain type
33 #[#fun (x : T) y => E#]# \
34 #[#fun (x : xT) (y : yT) => E#]# | == binary function with (some) explicit,
35 #[#fun x (y : T) => E#]# / independent domain types for each argument
36
37 - Partial functions using option type:
38 oapp f d ox == if ox is Some x returns f x, d otherwise
39 odflt d ox == if ox is Some x returns x, d otherwise
40 obind f ox == if ox is Some x returns f x, None otherwise
41 omap f ox == if ox is Some x returns Some (f x), None otherwise
42 olift f := Some \o f
43
44 - Singleton types:
45 all_equal_to x0 == x0 is the only value in its type, so any such value
46 can be rewritten to x0.
47
48 - A generic wrapper type:
49 wrapped T == the inductive type with values Wrap x for x : T.
50 unwrap w == the projection of w : wrapped T on T.
51 wrap x == the canonical injection of x : T into wrapped T; it is
52 equivalent to Wrap x, but is declared as a (default)
53 Canonical Structure, which lets the Rocq HO unification
54 automatically expand x into unwrap (wrap x). The delta
55 reduction of wrap x to Wrap can be exploited to
56 introduce controlled nondeterminism in Canonical
57 Structure inference, as in the implementation of
58 the mxdirect predicate in matrix.v.
59
60 - The empty type:
61 void == a notation for the Empty_set type of the standard library.
62 of_void T == the canonical injection void -> T.
63
64 - Sigma types:
65 tag w == the i of w : {i : I & T i}.
66 tagged w == the T i component of w : {i : I & T i}.
67 Tagged T x == the {i : I & T i} with component x : T i.
68 tag2 w == the i of w : {i : I & T i & U i}.
69 tagged2 w == the T i component of w : {i : I & T i & U i}.
70 tagged2' w == the U i component of w : {i : I & T i & U i}.
71 Tagged2 T U x y == the {i : I & T i} with components x : T i and y : U i.
72 sval u == the x of u : {x : T | P x}.
73 s2val u == the x of u : {x : T | P x & Q x}.
74 The properties of sval u, s2val u are given by lemmas svalP, s2valP, and
75 s2valP'. We provide coercions sigT2 >-> sigT and sig2 >-> sig >-> sigT.
76 A suite of lemmas (all_sig, ...) let us skolemize sig, sig2, sigT, sigT2
77 and pair, e.g.,
78 have /all_sig#[#f fP#]# (x : T): {y : U | P y} by ...
79 yields an f : T -> U such that fP : forall x, P (f x).
80 - Identity functions:
81 id == NOTATION for the explicit identity function fun x => x.
82 @id T == notation for the explicit identity at type T.
83 idfun == an expression with a head constant, convertible to id;
84 idfun x simplifies to x.
85 @idfun T == the expression above, specialized to type T.
86 phant_id x y == the function type phantom _ x -> phantom _ y.
87 *** In addition to their casual use in functional programming, identity
88 functions are often used to trigger static unification as part of the
89 construction of dependent Records and Structures. For example, if we need
90 a structure sT over a type T, we take as arguments T, sT, and a "dummy"
91 function T -> sort sT:
92 Definition foo T sT & T -> sort sT := ...
93 We can avoid specifying sT directly by calling foo (@id T), or specify
94 the call completely while still ensuring the consistency of T and sT, by
95 calling @foo T sT idfun. The phant_id type allows us to extend this trick
96 to non-Type canonical projections. It also allows us to sidestep
97 dependent type constraints when building explicit records, e.g., given
98 Record r := R {x; y : T(x)}.
99 if we need to build an r from a given y0 while inferring some x0, such
100 that y0 : T(x0), we pose
101 Definition mk_r .. y .. (x := ...) y' & phant_id y y' := R x y'.
102 Calling @mk_r .. y0 .. id will cause Rocq to use y' := y0, while checking
103 the dependent type constraint y0 : T(x0).
104
105 - Extensional equality for functions and relations (i.e. functions of two
106 arguments):
107 f1 =1 f2 == f1 x is equal to f2 x for all x.
108 f1 =1 f2 :> A == ... and f2 is explicitly typed.
109 f1 =2 f2 == f1 x y is equal to f2 x y for all x y.
110 f1 =2 f2 :> A == ... and f2 is explicitly typed.
111
112 - Composition for total and partial functions:
113 f^~ y == function f with second argument specialised to y,
114 i.e., fun x => f x y
115 CAVEAT: conditional (non-maximal) implicit arguments
116 of f are NOT inserted in this context
117 @^~ x == application at x, i.e., fun f => f x
118 #[#eta f#]# == the explicit eta-expansion of f, i.e., fun x => f x
119 CAVEAT: conditional (non-maximal) implicit arguments
120 of f are NOT inserted in this context.
121 fun=> v := the constant function fun _ => v.
122 f1 \o f2 == composition of f1 and f2.
123 Note: (f1 \o f2) x simplifies to f1 (f2 x).
124 f1 \; f2 == categorical composition of f1 and f2. This expands to
125 to f2 \o f1 and (f1 \; f2) x simplifies to f2 (f1 x).
126 pcomp f1 f2 == composition of partial functions f1 and f2.
127
128
129 - Properties of functions:
130 injective f <-> f is injective.
131 injective2 f <-> f(x,y) is injective.
132 cancel f g <-> g is a left inverse of f / f is a right inverse of g.
133 pcancel f g <-> g is a left inverse of f where g is partial.
134 ocancel f g <-> g is a left inverse of f where f is partial.
135 bijective f <-> f is bijective (has a left and right inverse).
136 involutive f <-> f is involutive.
137
138 - Properties for operations.
139 left_id e op <-> e is a left identity for op (e op x = x).
140 right_id e op <-> e is a right identity for op (x op e = x).
141 left_inverse e inv op <-> inv is a left inverse for op wrt identity e,
142 i.e., (inv x) op x = e.
143 right_inverse e inv op <-> inv is a right inverse for op wrt identity e
144 i.e., x op (i x) = e.
145 self_inverse e op <-> each x is its own op-inverse (x op x = e).
146 idempotent_op op <-> op is idempotent for op (x op x = x).
147 idempotent_fun f <-> f is idempotent (i.e., f \o f =1 f).
148 associative op <-> op is associative, i.e.,
149 x op (y op z) = (x op y) op z.
150 commutative op <-> op is commutative (x op y = y op x).
151 left_commutative op <-> op is left commutative, i.e.,
152 x op (y op z) = y op (x op z).
153 right_commutative op <-> op is right commutative, i.e.,
154 (x op y) op z = (x op z) op y.
155 left_zero z op <-> z is a left zero for op (z op x = z).
156 right_zero z op <-> z is a right zero for op (x op z = z).
157 left_distributive op1 op2 <-> op1 distributes over op2 to the left:
158 (x op2 y) op1 z = (x op1 z) op2 (y op1 z).
159 right_distributive op1 op2 <-> op distributes over add to the right:
160 x op1 (y op2 z) = (x op1 z) op2 (x op1 z).
161 interchange op1 op2 <-> op1 and op2 satisfy an interchange law:
162 (x op2 y) op1 (z op2 t) = (x op1 z) op2 (y op1 t).
163 Note that interchange op op is a commutativity property.
164 left_injective op <-> op is injective in its left argument:
165 x op y = z op y -> x = z.
166 right_injective op <-> op is injective in its right argument:
167 x op y = x op z -> y = z.
168 left_loop inv op <-> op, inv obey the inverse loop left axiom:
169 (inv x) op (x op y) = y for all x, y, i.e.,
170 op (inv x) is always a left inverse of op x
171 rev_left_loop inv op <-> op, inv obey the inverse loop reverse left
172 axiom: x op ((inv x) op y) = y, for all x, y.
173 right_loop inv op <-> op, inv obey the inverse loop right axiom:
174 (x op y) op (inv y) = x for all x, y.
175 rev_right_loop inv op <-> op, inv obey the inverse loop reverse right
176 axiom: (x op (inv y)) op y = x for all x, y.
177 Note that familiar "cancellation" identities like x + y - y = x or
178 x - y + y = x are respectively instances of right_loop and rev_right_loop
179 The corresponding lemmas will use the K and NK/VK suffixes, respectively.
180
181 - Morphisms for functions and relations:
182 {morph f : x / a >-> r} <-> f is a morphism with respect to functions
183 (fun x => a) and (fun x => r); if r == R#[#x#]#,
184 this states that f a = R#[#f x#]# for all x.
185 {morph f : x / a} <-> f is a morphism with respect to the
186 function expression (fun x => a). This is
187 shorthand for {morph f : x / a >-> a}; note
188 that the two instances of a are often
189 interpreted at different types.
190 {morph f : x y / a >-> r} <-> f is a morphism with respect to functions
191 (fun x y => a) and (fun x y => r).
192 {morph f : x y / a} <-> f is a morphism with respect to the
193 function expression (fun x y => a).
194 {homo f : x / a >-> r} <-> f is a homomorphism with respect to the
195 predicates (fun x => a) and (fun x => r);
196 if r == R#[#x#]#, this states that a -> R#[#f x#]#
197 for all x.
198 {homo f : x / a} <-> f is a homomorphism with respect to the
199 predicate expression (fun x => a).
200 {homo f : x y / a >-> r} <-> f is a homomorphism with respect to the
201 relations (fun x y => a) and (fun x y => r).
202 {homo f : x y / a} <-> f is a homomorphism with respect to the
203 relation expression (fun x y => a).
204 {mono f : x / a >-> r} <-> f is monotone with respect to projectors
205 (fun x => a) and (fun x => r); if r == R#[#x#]#,
206 this states that R#[#f x#]# = a for all x.
207 {mono f : x / a} <-> f is monotone with respect to the projector
208 expression (fun x => a).
209 {mono f : x y / a >-> r} <-> f is monotone with respect to relators
210 (fun x y => a) and (fun x y => r).
211 {mono f : x y / a} <-> f is monotone with respect to the relator
212 expression (fun x y => a).
213
214 The file also contains some basic lemmas for the above concepts.
215 Lemmas relative to cancellation laws use some abbreviated suffixes:
216 K - a cancellation rule like esymK : cancel (@esym T x y) (@esym T y x).
217 LR - a lemma moving an operation from the left hand side of a relation to
218 the right hand side, like canLR: cancel g f -> x = g y -> f x = y.
219 RL - a lemma moving an operation from the right to the left, e.g., canRL.
220 Beware that the LR and RL orientations refer to an "apply" (back chaining)
221 usage; when using the same lemmas with "have" or "move" (forward chaining)
222 the directions will be reversed!. **)
223
224
225 Set Implicit Arguments.
226 Unset Strict Implicit.
227 Unset Printing Implicit Defensive.
228
229 (** Parsing / printing declarations. *)
230 Reserved Notation "f ^~ y" (at level 10, y at level 8, no associativity,
231 format "f ^~ y").
232 Reserved Notation "@^~ x" (at level 10, x at level 8, no associativity,
233 format "@^~ x").
234 Reserved Notation "[ 'eta' f ]" (at level 0, format "[ 'eta' f ]").
235 Reserved Notation "'fun' => E" (at level 200, format "'fun' => E").
236
237 Reserved Notation "[ 'fun' : T => E ]" (at level 0,
238 format "'[hv' [ 'fun' : T => '/ ' E ] ']'").
239 Reserved Notation "[ 'fun' x => E ]" (at level 0,
240 x name, format "'[hv' [ 'fun' x => '/ ' E ] ']'").
241 Reserved Notation "[ 'fun' x : T => E ]" (at level 0,
242 x name, format "'[hv' [ 'fun' x : T => '/ ' E ] ']'").
243 Reserved Notation "[ 'fun' x y => E ]" (at level 0,
244 x name, y name, format "'[hv' [ 'fun' x y => '/ ' E ] ']'").
245 Reserved Notation "[ 'fun' x y : T => E ]" (at level 0,
246 x name, y name, format "'[hv' [ 'fun' x y : T => '/ ' E ] ']'").
247 Reserved Notation "[ 'fun' ( x : T ) y => E ]" (at level 0,
248 x name, y name, format "'[hv' [ 'fun' ( x : T ) y => '/ ' E ] ']'").
249 Reserved Notation "[ 'fun' x ( y : T ) => E ]" (at level 0,
250 x name, y name, format "'[hv' [ 'fun' x ( y : T ) => '/ ' E ] ']'").
251 Reserved Notation "[ 'fun' ( x : T ) ( y : U ) => E ]" (at level 0,
252 x name, y name, format "[ 'fun' ( x : T ) ( y : U ) => E ]" ).
253
254 Reserved Notation "f =1 g" (at level 70, no associativity).
255 Reserved Notation "f =1 g :> A" (at level 70, g at next level, A at level 90).
256 Reserved Notation "f =2 g" (at level 70, no associativity).
257 Reserved Notation "f =2 g :> A" (at level 70, g at next level, A at level 90).
258 Reserved Notation "f \o g" (at level 50, format "f \o '/ ' g").
259 Reserved Notation "f \; g" (at level 60, right associativity,
260 format "f \; '/ ' g").
261
262 Reserved Notation "{ 'morph' f : x / a >-> r }" (at level 0, f at level 99,
263 x name, format "{ 'morph' f : x / a >-> r }").
264 Reserved Notation "{ 'morph' f : x / a }" (at level 0, f at level 99,
265 x name, format "{ 'morph' f : x / a }").
266 Reserved Notation "{ 'morph' f : x y / a >-> r }" (at level 0, f at level 99,
267 x name, y name, format "{ 'morph' f : x y / a >-> r }").
268 Reserved Notation "{ 'morph' f : x y / a }" (at level 0, f at level 99,
269 x name, y name, format "{ 'morph' f : x y / a }").
270 Reserved Notation "{ 'homo' f : x / a >-> r }" (at level 0, f at level 99,
271 x name, format "{ 'homo' f : x / a >-> r }").
272 Reserved Notation "{ 'homo' f : x / a }" (at level 0, f at level 99,
273 x name, format "{ 'homo' f : x / a }").
274 Reserved Notation "{ 'homo' f : x y / a >-> r }" (at level 0, f at level 99,
275 x name, y name, format "{ 'homo' f : x y / a >-> r }").
276 Reserved Notation "{ 'homo' f : x y / a }" (at level 0, f at level 99,
277 x name, y name, format "{ 'homo' f : x y / a }").
278 Reserved Notation "{ 'homo' f : x y /~ a }" (at level 0, f at level 99,
279 x name, y name, format "{ 'homo' f : x y /~ a }").
280 Reserved Notation "{ 'mono' f : x / a >-> r }" (at level 0, f at level 99,
281 x name, format "{ 'mono' f : x / a >-> r }").
282 Reserved Notation "{ 'mono' f : x / a }" (at level 0, f at level 99,
283 x name, format "{ 'mono' f : x / a }").
284 Reserved Notation "{ 'mono' f : x y / a >-> r }" (at level 0, f at level 99,
285 x name, y name, format "{ 'mono' f : x y / a >-> r }").
286 Reserved Notation "{ 'mono' f : x y / a }" (at level 0, f at level 99,
287 x name, y name, format "{ 'mono' f : x y / a }").
288 Reserved Notation "{ 'mono' f : x y /~ a }" (at level 0, f at level 99,
289 x name, y name, format "{ 'mono' f : x y /~ a }").
290
291 Reserved Notation "@ 'id' T" (at level 10, T at level 8, format "@ 'id' T").
292 #[warning="-closed-notation-not-level-0"]
293 Reserved Notation "@ 'sval'" (at level 10, format "@ 'sval'").
294
295 (**
296 Syntax for defining auxiliary recursive function.
297 Usage:
298 Section FooDefinition.
299 Variables (g1 : T1) (g2 : T2). (globals)
300 Fixoint foo_auxiliary (a3 : T3) ... :=
301 body, using #[#rec e3, ... #]# for recursive calls
302 where " #[# 'rec' a3 , a4 , ... #]#" := foo_auxiliary.
303 Definition foo x y .. := #[#rec e1, ... #]#.
304 + proofs about foo
305 End FooDefinition. **)
306
307 Reserved Notation "[ 'rec' a ]" (at level 0,
308 format "[ 'rec' a ]").
309 Reserved Notation "[ 'rec' a , b ]" (at level 0,
310 format "[ 'rec' a , b ]").
311 Reserved Notation "[ 'rec' a , b , c ]" (at level 0,
312 format "[ 'rec' a , b , c ]").
313 Reserved Notation "[ 'rec' a , b , c , d ]" (at level 0,
314 format "[ 'rec' a , b , c , d ]").
315 Reserved Notation "[ 'rec' a , b , c , d , e ]" (at level 0,
316 format "[ 'rec' a , b , c , d , e ]").
317 Reserved Notation "[ 'rec' a , b , c , d , e , f ]" (at level 0,
318 format "[ 'rec' a , b , c , d , e , f ]").
319 Reserved Notation "[ 'rec' a , b , c , d , e , f , g ]" (at level 0,
320 format "[ 'rec' a , b , c , d , e , f , g ]").
321 Reserved Notation "[ 'rec' a , b , c , d , e , f , g , h ]" (at level 0,
322 format "[ 'rec' a , b , c , d , e , f , g , h ]").
323 Reserved Notation "[ 'rec' a , b , c , d , e , f , g , h , i ]" (at level 0,
324 format "[ 'rec' a , b , c , d , e , f , g , h , i ]").
325 Reserved Notation "[ 'rec' a , b , c , d , e , f , g , h , i , j ]" (at level 0,
326 format "[ 'rec' a , b , c , d , e , f , g , h , i , j ]").
327
328 Declare Scope pair_scope.
329 Delimit Scope pair_scope with PAIR.
330 Open Scope pair_scope.
331
332 (** Notations for pair/conjunction projections **)
333 Notation "p .1" := (fst p) : pair_scope.
334 Notation "p .2" := (snd p) : pair_scope.
335
336 Coercion pair_of_and P Q (PandQ : P /\ Q) := (proj1 PandQ, proj2 PandQ).
337
338 Definition all_pair I T U (w : forall i : I, T i * U i) :=
339 (fun i => (w i).1, fun i => (w i).2).
340
341 (**
342 Complements on the option type constructor, used below to
343 encode partial functions. **)
344
345 Module Option.
346
347 Definition apply aT rT (f : aT -> rT) x u := if u is Some y then f y else x.
348
349 Definition default T := apply (fun x : T => x).
350
351 Definition bind aT rT (f : aT -> option rT) := apply f None.
352
353 Definition map aT rT (f : aT -> rT) := bind (fun x => Some (f x)).
354
355 Definition lift aT rT (f : aT -> rT) := fun x => Some (f x).
356
357 End Option.
358
359 Abbreviation oapp := Option.apply.
360 Abbreviation odflt := Option.default.
361 Abbreviation obind := Option.bind.
362 Abbreviation omap := Option.map.
363 Abbreviation olift := Option.lift.
364 Abbreviation some := (@Some _) (only parsing).
365
366 (** Shorthand for some basic equality lemmas. **)
367
368 Abbreviation erefl := refl_equal.
369 Abbreviation ecast i T e x := (let: erefl in _ = i := e return T in x).
370 Definition esym := sym_eq.
371 Definition nesym := sym_not_eq.
372 Definition etrans := trans_eq.
373 Definition congr1 := f_equal.
374 Definition congr2 := f_equal2.
375 (** Force at least one implicit when used as a view. **)
376 Prenex Implicits esym nesym.
377
378 (** A predicate for singleton types. **)
379 Definition all_equal_to T (x0 : T) := forall x, unkeyed x = x0.
380
381 Lemma unitE : all_equal_to tt. Proof. by case. Qed.
382
383 (** A generic wrapper type **)
384
385 #[universes(template)]
386 Structure wrapped T := Wrap {unwrap : T}.
387 Canonical wrap T x := @Wrap T x.
388
389 Prenex Implicits unwrap wrap Wrap.
390
391 Delimit Scope function_scope with FUN.
392 Open Scope function_scope.
393
394 (** Notations for argument transpose **)
395 Notation "f ^~ y" := (fun x => f x y) : function_scope.
396 Notation "@^~ x" := (fun f => f x) : function_scope.
397
398 (**
399 Definitions and notation for explicit functions with simplification,
400 i.e., which simpl and /= beta expand (this is complementary to nosimpl). **)
401
402 #[universes(template)]
403 Variant simpl_fun (aT rT : Type) := SimplFun of aT -> rT.
404
405 Section SimplFun.
406
407 Variables aT rT : Type.
408
409 Definition fun_of_simpl (f : simpl_fun aT rT) := fun x => let: SimplFun lam := f in lam x.
410
411 End SimplFun.
412
413 Coercion fun_of_simpl : simpl_fun >-> Funclass.
414
415 Notation "[ 'fun' : T => E ]" := (SimplFun (fun _ : T => E)) : function_scope.
416 Notation "[ 'fun' x => E ]" := (SimplFun (fun x => E)) : function_scope.
417 Notation "[ 'fun' x y => E ]" := (fun x => [fun y => E]) : function_scope.
418 Notation "[ 'fun' x : T => E ]" := (SimplFun (fun x : T => E))
419 (only parsing) : function_scope.
420 Notation "[ 'fun' x y : T => E ]" := (fun x : T => [fun y : T => E])
421 (only parsing) : function_scope.
422 Notation "[ 'fun' ( x : T ) y => E ]" := (fun x : T => [fun y => E])
423 (only parsing) : function_scope.
424 Notation "[ 'fun' x ( y : T ) => E ]" := (fun x => [fun y : T => E])
425 (only parsing) : function_scope.
426 Notation "[ 'fun' ( x : T ) ( y : U ) => E ]" := (fun x : T => [fun y : U => E])
427 (only parsing) : function_scope.
428
429 (** For delta functions in eqtype.v. **)
430 Definition SimplFunDelta aT rT (f : aT -> aT -> rT) := [fun z => f z z].
431
432 (**
433 Extensional equality, for unary and binary functions, including syntactic
434 sugar. **)
435
436 Section ExtensionalEquality.
437
438 Variables A B C : Type.
439
440 Definition eqfun (f g : B -> A) : Prop := forall x, f x = g x.
441
442 Definition eqrel (r s : C -> B -> A) : Prop := forall x y, r x y = s x y.
443
444 Lemma frefl f : eqfun f f. Proof. by []. Qed.
445 Lemma fsym f g : eqfun f g -> eqfun g f. Proof. by move=> eq_fg x. Qed.
446
447 Lemma ftrans f g h : eqfun f g -> eqfun g h -> eqfun f h.
448 Proof. by move=> eq_fg eq_gh x; rewrite eq_fg. Qed.
449
450 Lemma rrefl r : eqrel r r. Proof. by []. Qed.
451
452 End ExtensionalEquality.
453
454 Global Typeclasses Opaque eqfun eqrel.
455
456 #[global]
457 Hint Resolve frefl rrefl : core.
458
459 Notation "f1 =1 f2" := (eqfun f1 f2) : type_scope.
460 Notation "f1 =1 f2 :> A" := (f1 =1 (f2 : A)) : type_scope.
461 Notation "f1 =2 f2" := (eqrel f1 f2) : type_scope.
462 Notation "f1 =2 f2 :> A" := (f1 =2 (f2 : A)) : type_scope.
463
464 Section Composition.
465
466 Variables A B C : Type.
467
468 Definition comp (f : B -> A) (g : C -> B) x := f (g x).
469 Definition catcomp g f := comp f g.
470 Definition pcomp (f : B -> option A) (g : C -> option B) x := obind f (g x).
471
472 Lemma eq_comp f f' g g' : f =1 f' -> g =1 g' -> comp f g =1 comp f' g'.
473 Proof. by move=> eq_ff' eq_gg' x; rewrite /comp eq_gg' eq_ff'. Qed.
474
475 End Composition.
476
477 Arguments comp {A B C} f g x /.
478 Arguments catcomp {A B C} g f x /.
479 Notation "f1 \o f2" := (comp f1 f2) : function_scope.
480 Notation "f1 \; f2" := (catcomp f1 f2) : function_scope.
481
482 Lemma compA {A B C D : Type} (f : B -> A) (g : C -> B) (h : D -> C) :
483 f \o (g \o h) = (f \o g) \o h.
484 Proof. by []. Qed.
485
486 Notation "[ 'eta' f ]" := (fun x => f x) : function_scope.
487
488 Notation "'fun' => E" := (fun _ => E) : function_scope.
489
490 Abbreviation id := (fun x => x).
491
492 Notation "@ 'id' T" := (fun x : T => x) (only parsing) : function_scope.
493
494 Definition idfun T x : T := x.
495 Arguments idfun {T} x /.
496
497 Definition phant_id T1 T2 v1 v2 := phantom T1 v1 -> phantom T2 v2.
498
499 Section OptionTheory.
500
501 Variables (aT rT sT : Type) (f : aT -> rT) (g : rT -> sT).
502
503 Lemma obindEapp (fo : aT -> option rT) : obind fo = oapp fo None.
504 Proof. by []. Qed.
505
506 Lemma omapEbind : omap f = obind (olift f).
507 Proof. by []. Qed.
508
509 Lemma omap_id (x : option rT) : omap id x = x. Proof. by case: x. Qed.
510
511 Lemma eq_omap (h : aT -> rT) : f =1 h -> omap f =1 omap h.
512 Proof. by move=> Ef [?|] //=; rewrite Ef. Qed.
513
514 Lemma omapEapp : omap f = oapp (olift f) None.
515 Proof. by []. Qed.
516
517 Lemma oappEmap (y0 : rT) x : oapp f y0 x = odflt y0 (omap f x).
518 Proof. by case: x. Qed.
519
520 Lemma omap_comp : omap (g \o f) =1 omap g \o omap f.
521 Proof. by case. Qed.
522
523 Lemma oapp_comp x : oapp (g \o f) x =1 (@oapp _ _)^~ x g \o omap f.
524 Proof. by case. Qed.
525
526 Lemma oapp_comp_f (x : rT) : oapp (g \o f) (g x) =1 g \o oapp f x.
527 Proof. by case. Qed.
528
529 Lemma olift_comp : olift (g \o f) = olift g \o f.
530 Proof. by []. Qed.
531
532 End OptionTheory.
533
534 (** The empty type. **)
535
536 Abbreviation void := Empty_set.
537
538 Definition of_void T (x : void) : T := match x with end.
539
540 (** Strong sigma types. **)
541
542 Section Tag.
543
544 Variables (I : Type) (i : I) (T_ U_ : I -> Type).
545
546 Definition tag := projT1.
547 Definition tagged : forall w, T_(tag w) := @projT2 I [eta T_].
548 Definition Tagged x := @existT I [eta T_] i x.
549
550 Definition tag2 (w : @sigT2 I T_ U_) := let: existT2 _ _ i _ _ := w in i.
551 Definition tagged2 w : T_(tag2 w) := let: existT2 _ _ _ x _ := w in x.
552 Definition tagged2' w : U_(tag2 w) := let: existT2 _ _ _ _ y := w in y.
553 Definition Tagged2 x y := @existT2 I [eta T_] [eta U_] i x y.
554
555 End Tag.
556
557 Arguments Tagged [I i].
558 Arguments Tagged2 [I i].
559 Prenex Implicits tag tagged Tagged tag2 tagged2 tagged2' Tagged2.
560
561 Coercion tag_of_tag2 I T_ U_ (w : @sigT2 I T_ U_) :=
562 Tagged (fun i => T_ i * U_ i)%type (tagged2 w, tagged2' w).
563
564 Lemma all_tag I T U :
565 (forall x : I, {y : T x & U x y}) ->
566 {f : forall x, T x & forall x, U x (f x)}.
567 Proof. by move=> fP; exists (fun x => tag (fP x)) => x; case: (fP x). Qed.
568
569 Lemma all_tag2 I T U V :
570 (forall i : I, {y : T i & U i y & V i y}) ->
571 {f : forall i, T i & forall i, U i (f i) & forall i, V i (f i)}.
572 Proof. by case/all_tag=> f /all_pair[]; exists f. Qed.
573
574 (** Refinement types. **)
575
576 (** Prenex Implicits and renaming. **)
577 Abbreviation sval := (@proj1_sig _ _).
578 Notation "@ 'sval'" := (@proj1_sig) (only parsing) : function_scope.
579
580 Section Sig.
581
582 Variables (T : Type) (P Q : T -> Prop).
583
584 Lemma svalP (u : sig P) : P (sval u). Proof. by case: u. Qed.
585
586 Definition s2val (u : sig2 P Q) := let: exist2 _ _ x _ _ := u in x.
587
588 Lemma s2valP u : P (s2val u). Proof. by case: u. Qed.
589
590 Lemma s2valP' u : Q (s2val u). Proof. by case: u. Qed.
591
592 End Sig.
593
594 Prenex Implicits svalP s2val s2valP s2valP'.
595
596 Coercion tag_of_sig I P (u : @sig I P) := Tagged P (svalP u).
597
598 Coercion sig_of_sig2 I P Q (u : @sig2 I P Q) :=
599 exist (fun i => P i /\ Q i) (s2val u) (conj (s2valP u) (s2valP' u)).
600
601 Lemma all_sig I T P :
602 (forall x : I, {y : T x | P x y}) ->
603 {f : forall x, T x | forall x, P x (f x)}.
604 Proof. by case/all_tag=> f; exists f. Qed.
605
606 Lemma all_sig2 I T P Q :
607 (forall x : I, {y : T x | P x y & Q x y}) ->
608 {f : forall x, T x | forall x, P x (f x) & forall x, Q x (f x)}.
609 Proof. by case/all_sig=> f /all_pair[]; exists f. Qed.
610
611 Section Morphism.
612
613 Variables (aT rT sT : Type) (f : aT -> rT).
614
615 (** Morphism property for unary and binary functions **)
616 Definition morphism_1 aF rF := forall x, f (aF x) = rF (f x).
617 Definition morphism_2 aOp rOp := forall x y, f (aOp x y) = rOp (f x) (f y).
618
619 (** Homomorphism property for unary and binary relations **)
620 Definition homomorphism_1 (aP rP : _ -> Prop) := forall x, aP x -> rP (f x).
621 Definition homomorphism_2 (aR rR : _ -> _ -> Prop) :=
622 forall x y, aR x y -> rR (f x) (f y).
623
624 (** Stability property for unary and binary relations **)
625 Definition monomorphism_1 (aP rP : _ -> sT) := forall x, rP (f x) = aP x.
626 Definition monomorphism_2 (aR rR : _ -> _ -> sT) :=
627 forall x y, rR (f x) (f y) = aR x y.
628
629 End Morphism.
630
631 Notation "{ 'morph' f : x / a >-> r }" :=
632 (morphism_1 f (fun x => a) (fun x => r)) : type_scope.
633 Notation "{ 'morph' f : x / a }" :=
634 (morphism_1 f (fun x => a) (fun x => a)) : type_scope.
635 Notation "{ 'morph' f : x y / a >-> r }" :=
636 (morphism_2 f (fun x y => a) (fun x y => r)) : type_scope.
637 Notation "{ 'morph' f : x y / a }" :=
638 (morphism_2 f (fun x y => a) (fun x y => a)) : type_scope.
639 Notation "{ 'homo' f : x / a >-> r }" :=
640 (homomorphism_1 f (fun x => a) (fun x => r)) : type_scope.
641 Notation "{ 'homo' f : x / a }" :=
642 (homomorphism_1 f (fun x => a) (fun x => a)) : type_scope.
643 Notation "{ 'homo' f : x y / a >-> r }" :=
644 (homomorphism_2 f (fun x y => a) (fun x y => r)) : type_scope.
645 Notation "{ 'homo' f : x y / a }" :=
646 (homomorphism_2 f (fun x y => a) (fun x y => a)) : type_scope.
647 Notation "{ 'homo' f : x y /~ a }" :=
648 (homomorphism_2 f (fun y x => a) (fun x y => a)) : type_scope.
649 Notation "{ 'mono' f : x / a >-> r }" :=
650 (monomorphism_1 f (fun x => a) (fun x => r)) : type_scope.
651 Notation "{ 'mono' f : x / a }" :=
652 (monomorphism_1 f (fun x => a) (fun x => a)) : type_scope.
653 Notation "{ 'mono' f : x y / a >-> r }" :=
654 (monomorphism_2 f (fun x y => a) (fun x y => r)) : type_scope.
655 Notation "{ 'mono' f : x y / a }" :=
656 (monomorphism_2 f (fun x y => a) (fun x y => a)) : type_scope.
657 Notation "{ 'mono' f : x y /~ a }" :=
658 (monomorphism_2 f (fun y x => a) (fun x y => a)) : type_scope.
659
660 (**
661 In an intuitionistic setting, we have two degrees of injectivity. The
662 weaker one gives only simplification, and the strong one provides a left
663 inverse (we show in `fintype' that they coincide for finite types).
664 We also define an intermediate version where the left inverse is only a
665 partial function. **)
666
667 Section Injections.
668
669 Variables (rT aT : Type) (f : aT -> rT).
670
671 Definition injective := forall x1 x2, f x1 = f x2 -> x1 = x2.
672
673 Definition cancel g := forall x, g (f x) = x.
674
675 Definition pcancel g := forall x, g (f x) = Some x.
676
677 Definition ocancel (g : aT -> option rT) h := forall x, oapp h x (g x) = x.
678
679 Lemma can_pcan g : cancel g -> pcancel (fun y => Some (g y)).
680 Proof. by move=> fK x; congr (Some _). Qed.
681
682 Lemma pcan_inj g : pcancel g -> injective.
683 Proof. by move=> fK x y /(congr1 g); rewrite !fK => [[]]. Qed.
684
685 Lemma can_inj g : cancel g -> injective.
686 Proof. by move/can_pcan; apply: pcan_inj. Qed.
687
688 Lemma canLR g x y : cancel g -> x = f y -> g x = y.
689 Proof. by move=> fK ->. Qed.
690
691 Lemma canRL g x y : cancel g -> f x = y -> x = g y.
692 Proof. by move=> fK <-. Qed.
693
694 End Injections.
695
696 Definition injective2 (rT aT1 aT2 : Type) (f : aT1 -> aT2 -> rT) :=
697 forall (x1 x2 : aT1) (y1 y2 : aT2), f x1 y1 = f x2 y2 ->
698 (x1 = x2) * (y1 = y2).
699 Arguments injective2 [rT aT1 aT2] f.
700
701 Lemma Some_inj {T : nonPropType} : injective (@Some T).
702 Proof. by move=> x y []. Qed.
703
704 Lemma inj_omap {aT rT : Type} (f : aT -> rT) :
705 injective f -> injective (omap f).
706 Proof. by move=> injf [?|] [?|] //= [/injf->]. Qed.
707
708 Lemma omapK {aT rT : Type} (f : aT -> rT) (g : rT -> aT) :
709 cancel f g -> cancel (omap f) (omap g).
710 Proof. by move=> fK [?|] //=; rewrite fK. Qed.
711
712 Lemma of_voidK T : pcancel (of_void T) [fun _ => None].
713 Proof. by case. Qed.
714
715 (** cancellation lemmas for dependent type casts. **)
716 Lemma esymK T x y : cancel (@esym T x y) (@esym T y x).
717 Proof. by case: y /. Qed.
718
719 Lemma etrans_id T x y (eqxy : x = y :> T) : etrans (erefl x) eqxy = eqxy.
720 Proof. by case: y / eqxy. Qed.
721
722 Section InjectionsTheory.
723
724 Variables (A B C : Type) (f g : B -> A) (h : C -> B).
725
726 Lemma inj_id : injective (@id A).
727 Proof. by []. Qed.
728
729 Lemma inj_can_sym f' : cancel f f' -> injective f' -> cancel f' f.
730 Proof. by move=> fK injf' x; apply: injf'. Qed.
731
732 Lemma inj_comp : injective f -> injective h -> injective (f \o h).
733 Proof. by move=> injf injh x y /injf; apply: injh. Qed.
734
735 Lemma inj_compr : injective (f \o h) -> injective h.
736 Proof. by move=> injfh x y /(congr1 f) /injfh. Qed.
737
738 Lemma can_comp f' h' : cancel f f' -> cancel h h' -> cancel (f \o h) (h' \o f').
739 Proof. by move=> fK hK x; rewrite /= fK hK. Qed.
740
741 Lemma pcan_pcomp f' h' :
742 pcancel f f' -> pcancel h h' -> pcancel (f \o h) (pcomp h' f').
743 Proof. by move=> fK hK x; rewrite /pcomp fK /= hK. Qed.
744
745 Lemma ocan_comp [fo : B -> option A] [ho : C -> option B]
746 [f' : A -> B] [h' : B -> C] :
747 ocancel fo f' -> ocancel ho h' -> ocancel (obind fo \o ho) (h' \o f').
748 Proof.
749 move=> fK hK c /=; rewrite -[RHS]hK/=; case hcE : (ho c) => [b|]//=.
750 by rewrite -[b in RHS]fK; case: (fo b) => //=; have := hK c; rewrite hcE.
751 Qed.
752
753 Lemma eq_inj : injective f -> f =1 g -> injective g.
754 Proof. by move=> injf eqfg x y; rewrite -2!eqfg; apply: injf. Qed.
755
756 Lemma eq_can f' g' : cancel f f' -> f =1 g -> f' =1 g' -> cancel g g'.
757 Proof. by move=> fK eqfg eqfg' x; rewrite -eqfg -eqfg'. Qed.
758
759 Lemma inj_can_eq f' : cancel f f' -> injective f' -> cancel g f' -> f =1 g.
760 Proof. by move=> fK injf' gK x; apply: injf'; rewrite fK. Qed.
761
762 End InjectionsTheory.
763
764 Section Bijections.
765
766 Variables (A B : Type) (f : B -> A).
767
768 Variant bijective : Prop := Bijective g of cancel f g & cancel g f.
769
770 Hypothesis bijf : bijective.
771
772 Lemma bij_inj : injective f.
773 Proof. by case: bijf => g fK _; apply: can_inj fK. Qed.
774
775 Lemma bij_can_sym f' : cancel f' f <-> cancel f f'.
776 Proof.
777 split=> fK; first exact: inj_can_sym fK bij_inj.
778 by case: bijf => h _ hK x; rewrite -[x]hK fK.
779 Qed.
780
781 Lemma bij_can_eq f' f'' : cancel f f' -> cancel f f'' -> f' =1 f''.
782 Proof.
783 by move=> fK fK'; apply: (inj_can_eq _ bij_inj); apply/bij_can_sym.
784 Qed.
785
786 End Bijections.
787
788 Section BijectionsTheory.
789
790 Variables (A B C : Type) (f : B -> A) (h : C -> B).
791
792 Lemma eq_bij : bijective f -> forall g, f =1 g -> bijective g.
793 Proof. by case=> f' fK f'K g eqfg; exists f'; eapply eq_can; eauto. Qed.
794
795 Lemma bij_comp : bijective f -> bijective h -> bijective (f \o h).
796 Proof.
797 by move=> [f' fK f'K] [h' hK h'K]; exists (h' \o f'); apply: can_comp; auto.
798 Qed.
799
800 Lemma bij_can_bij : bijective f -> forall f', cancel f f' -> bijective f'.
801 Proof. by move=> bijf; exists f; first by apply/(bij_can_sym bijf). Qed.
802
803 End BijectionsTheory.
804
805 Section Involutions.
806
807 Variables (A : Type) (f : A -> A).
808
809 Definition involutive := cancel f f.
810
811 Hypothesis Hf : involutive.
812
813 Lemma inv_inj : injective f. Proof. exact: can_inj Hf. Qed.
814 Lemma inv_bij : bijective f. Proof. by exists f. Qed.
815
816 End Involutions.
817
818 Section OperationProperties.
819
820 Variables S T R : Type.
821
822 Section SopTisR.
823 Implicit Type op : S -> T -> R.
824 Definition left_inverse e inv op := forall x, op (inv x) x = e.
825 Definition right_inverse e inv op := forall x, op x (inv x) = e.
826 Definition left_injective op := forall x, injective (op^~ x).
827 Definition right_injective op := forall y, injective (op y).
828 End SopTisR.
829
830
831 Section SopTisS.
832 Implicit Type op : S -> T -> S.
833 Definition right_id e op := forall x, op x e = x.
834 Definition left_zero z op := forall x, op z x = z.
835 Definition right_commutative op := forall x y z, op (op x y) z = op (op x z) y.
836 Definition left_distributive op add :=
837 forall x y z, op (add x y) z = add (op x z) (op y z).
838 Definition right_loop inv op := forall y, cancel (op^~ y) (op^~ (inv y)).
839 Definition rev_right_loop inv op := forall y, cancel (op^~ (inv y)) (op^~ y).
840 End SopTisS.
841
842 Section SopTisT.
843 Implicit Type op : S -> T -> T.
844 Definition left_id e op := forall x, op e x = x.
845 Definition right_zero z op := forall x, op x z = z.
846 Definition left_commutative op := forall x y z, op x (op y z) = op y (op x z).
847 Definition right_distributive op add :=
848 forall x y z, op x (add y z) = add (op x y) (op x z).
849 Definition left_loop inv op := forall x, cancel (op x) (op (inv x)).
850 Definition rev_left_loop inv op := forall x, cancel (op (inv x)) (op x).
851 End SopTisT.
852
853 Section SopSisT.
854 Implicit Type op : S -> S -> T.
855 Definition self_inverse e op := forall x, op x x = e.
856 Definition commutative op := forall x y, op x y = op y x.
857 End SopSisT.
858
859 Section SopSisS.
860 Implicit Type op : S -> S -> S.
861 Definition idempotent_op op := forall x, op x x = x.
862 Definition associative op := forall x y z, op x (op y z) = op (op x y) z.
863 Definition interchange op1 op2 :=
864 forall x y z t, op1 (op2 x y) (op2 z t) = op2 (op1 x z) (op1 y t).
865 End SopSisS.
866
867 End OperationProperties.
868
869 #[deprecated(since="9.1", use=idempotent_op)]
870 Abbreviation idempotent:= idempotent_op (only parsing).
871
872 Definition idempotent_fun (U : Type) (f : U -> U) := f \o f =1 f.
873
874 Lemma inr_inj {A B} : injective (@inr A B). Proof. by move=> ? ? []. Qed.
875
876 Lemma inl_inj {A B} : injective (@inl A B). Proof. by move=> ? ? []. Qed.