(Joint Center)Library MathComp.ssralg

(* (c) Copyright Microsoft Corporation and Inria.                       
 You may distribute this file under the terms of the CeCILL-B license *)

Require Import ssreflect ssrfun ssrbool eqtype ssrnat div seq choice fintype.
Require Import finfun bigop prime binomial.

The algebraic part of the Algebraic Hierarchy, as described in ``Packaging mathematical structures'', TPHOLs09, by Francois Garillot, Georges Gonthier, Assia Mahboubi, Laurence Rideau
This file defines for each Structure (Zmodule, Ring, etc ...) its type, its packers and its canonical properties :

(Joint Center)Zmodule (additive abelian groups):

zmodType == interface type for Zmodule structure. ZmodMixin addA addC add0x addNx == builds the mixin for a Zmodule from the algebraic properties of its operations. ZmodType V m == packs the mixin m to build a Zmodule of type zmodType. The carrier type V must have a choiceType canonical structure. [zmodType of V for S] == V-clone of the zmodType structure S: a copy of S where the sort carrier has been replaced by V, and which is therefore a zmodType structure on V. The sort carrier for S must be convertible to V. [zmodType of V] == clone of a canonical zmodType structure on V. Similar to the above, except S is inferred, but possibly with a syntactically different carrier. 0 == the zero (additive identity) of a Zmodule. x + y == the sum of x and y (in a Zmodule).
  • x == the opposite (additive inverse) of x.
x - y == the difference of x and y; this is only notation for x + (- y). x *+ n == n times x, with n in nat (non-negative), i.e., x + (x + .. (x + x)..) (n terms); x *+ 1 is thus convertible to x, and x *+ 2 to x + x. x *- n == notation for - (x *+ n), the opposite of x *+ n. \sum_<range> e == iterated sum for a Zmodule (cf bigop.v). e`_i == nth 0 e i, when e : seq M and M has a zmodType structure. support f == 0.-support f, i.e., [pred x | f x != 0]. oppr_closed S <-> collective predicate S is closed under opposite. addr_closed S <-> collective predicate S is closed under finite sums (0 and x + y in S, for x, y in S). zmod_closed S <-> collective predicate S is closed under zmodType operations (0 and x - y in S, for x, y in S). This property coerces to oppr_pred and addr_pred. OpprPred oppS == packs oppS : oppr_closed S into an opprPred S interface structure associating this property to the canonical pred_key S, i.e. the k for which S has a Canonical keyed_pred k structure (see file ssrbool.v). AddrPred addS == packs addS : addr_closed S into an addrPred S interface structure associating this property to the canonical pred_key S (see above). ZmodPred oppS == packs oppS : oppr_closed S into an zmodPred S interface structure associating the zmod_closed property to the canonical pred_key S (see above), which must already be an addrPred. [zmodMixin of M by <: ] == zmodType mixin for a subType whose base type is a zmodType and whose predicate's canonical pred_key is a zmodPred. > Coq can be made to behave as if all predicates had canonical zmodPred keys by executing Import DefaultKeying GRing.DefaultPred. The required oppr_closed and addr_closed assumptions will be either abstracted, resolved or issued as separate proof obligations by the ssreflect plugin abstraction and Prop-irrelevance functions.

(Joint Center)Ring (non-commutative rings):

ringType == interface type for a Ring structure. RingMixin mulA mul1x mulx1 mulDx mulxD == builds the mixin for a Ring from the algebraic properties of its multiplicative operators; the carrier type must have a zmodType structure. RingType R m == packs the ring mixin m into a ringType. R^c == the converse Ring for R: R^c is convertible to R but when R has a canonical ringType structure R^c has the converse one: if x y : R^c, then x * y = (y : R) * (x : R). [ringType of R for S] == R-clone of the ringType structure S. [ringType of R] == clone of a canonical ringType structure on R. 1 == the multiplicative identity element of a Ring. n%:R == the ring image of an n in nat; this is just notation for 1 *+ n, so 1%:R is convertible to 1 and 2%:R to 1 + 1. x * y == the ring product of x and y. \prod_<range> e == iterated product for a ring (cf bigop.v). x ^+ n == x to the nth power with n in nat (non-negative), i.e., x * (x * .. (x * x)..) (n factors); x ^+ 1 is thus convertible to x, and x ^+ 2 to x * x. GRing.sign R b := (-1) ^+ b in R : ringType, with b : bool. This is a parsing-only helper notation, to be used for defining more specific instances. GRing.comm x y <-> x and y commute, i.e., x * y = y * x. GRing.lreg x <-> x if left-regular, i.e., *%R x is injective. GRing.rreg x <-> x if right-regular, i.e., *%R x is injective. [char R] == the characteristic of R, defined as the set of prime numbers p such that p%:R = 0 in R. The set [char p] has a most one element, and is implemented as a pred_nat collective predicate (see prime.v); thus the statement p \in [char R] can be read as `R has characteristic p', while [char R] =i pred0 means `R has characteristic 0' when R is a field. Frobenius_aut chRp == the Frobenius automorphism mapping x in R to x ^+ p, where chRp : p \in [char R] is a proof that R has (non-zero) characteristic p. mulr_closed S <-> collective predicate S is closed under finite products (1 and x * y in S for x, y in S). smulr_closed S <-> collective predicate S is closed under products and opposite (-1 and x * y in S for x, y in S). semiring_closed S <-> collective predicate S is closed under semiring operations (0, 1, x + y and x * y in S). subring_closed S <-> collective predicate S is closed under ring operations (1, x - y and x * y in S). MulrPred mulS == packs mulS : mulr_closed S into a mulrPred S, SmulrPred mulS smulrPred S, semiringPred S, or subringPred S SemiringPred mulS interface structure, corresponding to the above SubRingPred mulS properties, respectively, provided S already has the supplementary zmodType closure properties. The properties above coerce to subproperties so, e.g., ringS : subring_closed S can be used for the proof obligations of all prerequisites. [ringMixin of R by <: ] == ringType mixin for a subType whose base type is a ringType and whose predicate's canonical key is a SubringPred. > As for zmodType predicates, Import DefaultKeying GRing.DefaultPred turns unresolved GRing.Pred unification constraints into proof obligations for basic closure assumptions.

(Joint Center)ComRing (commutative Rings):

comRingType == interface type for commutative ring structure. ComRingType R mulC == packs mulC into a comRingType; the carrier type R must have a ringType canonical structure. ComRingMixin mulA mulC mul1x mulDx == builds the mixin for a Ring (i.e., a *non commutative* ring), using the commutativity to reduce the number of proof obligagtions. [comRingType of R for S] == R-clone of the comRingType structure S. [comRingType of R] == clone of a canonical comRingType structure on R. [comRingMixin of R by <: ] == comutativity mixin axiom for R when it is a subType of a commutative ring.

(Joint Center)UnitRing (Rings whose units have computable inverses):

unitRingType == interface type for the UnitRing structure. UnitRingMixin mulVr mulrV unitP inv0id == builds the mixin for a UnitRing from the properties of the inverse operation and the boolean test for being a unit (invertible). The inverse of a non-unit x is constrained to be x itself (property inv0id). The carrier type must have a ringType canonical structure. UnitRingType R m == packs the unit ring mixin m into a unitRingType. WARNING: while it is possible to omit R for most of the XxxType functions, R MUST be explicitly given when UnitRingType is used with a mixin produced by ComUnitRingMixin, otherwise the resulting structure will have the WRONG sort key and will NOT BE USED during type inference. [unitRingType of R for S] == R-clone of the unitRingType structure S. [unitRingType of R] == clones a canonical unitRingType structure on R. x \is a GRing.unit <=> x is a unit (i.e., has an inverse). x^-1 == the ring inverse of x, if x is a unit, else x. x / y == x divided by y (notation for x * y^-1). x ^- n := notation for (x ^+ n)^-1, the inverse of x ^+ n. invr_closed S <-> collective predicate S is closed under inverse. divr_closed S <-> collective predicate S is closed under division (1 and x / y in S). sdivr_closed S <-> collective predicate S is closed under division and opposite (-1 and x / y in S, for x, y in S). divring_closed S <-> collective predicate S is closed under unitRing operations (1, x - y and x / y in S). DivrPred invS == packs invS : mulr_closed S into a divrPred S, SdivrPred invS sdivrPred S or divringPred S interface structure, DivringPred invS corresponding to the above properties, resp., provided S already has the supplementary ringType closure properties. The properties above coerce to subproperties, as explained above. [unitRingMixin of R by <: ] == unitRingType mixin for a subType whose base type is a unitRingType and whose predicate's canonical key is a divringPred and whose ring structure is compatible with the base type's.

(Joint Center)ComUnitRing (commutative rings with computable inverses):

comUnitRingType == interface type for ComUnitRing structure. ComUnitRingMixin mulVr unitP inv0id == builds the mixin for a UnitRing (a *non commutative* unit ring, using commutativity to simplify the proof obligations; the carrier type must have a comRingType structure. WARNING: ALWAYS give an explicit type argument to UnitRingType along with a mixin produced by ComUnitRingMixin (see above). [comUnitRingType of R] == a comUnitRingType structure for R created by merging canonical comRingType and unitRingType structures on R.

(Joint Center)IntegralDomain (integral, commutative, ring with partial inverses):

idomainType == interface type for the IntegralDomain structure. IdomainType R mulf_eq0 == packs the integrality property into an idomainType integral domain structure; R must have a comUnitRingType canonical structure. [idomainType of R for S] == R-clone of the idomainType structure S. [idomainType of R] == clone of a canonical idomainType structure on R. [idomainMixin of R by <: ] == mixin axiom for a idomain subType.

(Joint Center)Field (commutative fields):

fieldType == interface type for fields. GRing.Field.axiom inv == the field axiom (x != 0 -> inv x * x = 1). FieldUnitMixin mulVx unitP inv0id == builds a *non commutative unit ring* mixin, using the field axiom to simplify proof obligations. The carrier type must have a comRingType canonical structure. FieldMixin mulVx == builds the field mixin from the field axiom. The carrier type must have a comRingType structure. FieldIdomainMixin m == builds an *idomain* mixin from a field mixin m. FieldType R m == packs the field mixin M into a fieldType. The carrier type R must be an idomainType. [fieldType of F for S] == F-clone of the fieldType structure S. [fieldType of F] == clone of a canonical fieldType structure on F. [fieldMixin of R by <: ] == mixin axiom for a field subType.

(Joint Center)DecidableField (fields with a decidable first order theory):

decFieldType == interface type for DecidableField structure. DecFieldMixin satP == builds the mixin for a DecidableField from the correctness of its satisfiability predicate. The carrier type must have a unitRingType structure. DecFieldType F m == packs the decidable field mixin m into a decFieldType; the carrier type F must have a fieldType structure. [decFieldType of F for S] == F-clone of the decFieldType structure S. [decFieldType of F] == clone of a canonical decFieldType structure on F GRing.term R == the type of formal expressions in a unit ring R with formal variables 'X_k, k : nat, and manifest constants x%:T, x : R. The notation of all the ring operations is redefined for terms, in scope %T. GRing.formula R == the type of first order formulas over R; the %T scope binds the logical connectives /\, \/, ~, ==>, ==, and != to formulae; GRing.True/False and GRing.Bool b denote constant formulae, and quantifiers are written 'forall/'exists 'X_k, f. GRing.Unit x tests for ring units GRing.If p_f t_f e_f emulates if-then-else GRing.Pick p_f t_f e_f emulates fintype.pick foldr GRing.Exists/Forall q_f xs can be used to write iterated quantifiers. GRing.eval e t == the value of term t with valuation e : seq R (e maps 'X_i to e`_i). GRing.same_env e1 e2 <-> environments e1 and e2 are extensionally equal. GRing.qf_form f == f is quantifier-free. GRing.holds e f == the intuitionistic CiC interpretation of the formula f holds with valuation e. GRing.qf_eval e f == the value (in bool) of a quantifier-free f. GRing.sat e f == valuation e satisfies f (only in a decField). GRing.sol n f == a sequence e of size n such that e satisfies f, if one exists, or [:: ] if there is no such e. QEdecFieldMixin wfP okP == a decidable field Mixin built from a quantifier eliminator p and proofs wfP : GRing.wf_QE_proj p and okP : GRing.valid_QE_proj p that p returns well-formed and valid formulae, i.e., p i (u, v) is a quantifier-free formula equivalent to 'exists 'X_i, u1 == 0 /\ ... /\ u_m == 0 /\ v1 != 0 ... /\ v_n != 0

(Joint Center)ClosedField (algebraically closed fields):

closedFieldType == interface type for the ClosedField structure. ClosedFieldType F m == packs the closed field mixin m into a closedFieldType. The carrier F must have a decFieldType structure. [closedFieldType of F on S] == F-clone of a closedFieldType structure S. [closedFieldType of F] == clone of a canonicalclosedFieldType structure on F.

(Joint Center)Lmodule (module with left multiplication by external scalars).

lmodType R == interface type for an Lmodule structure with scalars of type R; R must have a ringType structure. LmodMixin scalA scal1v scalxD scalDv == builds an Lmodule mixin from the algebraic properties of the scaling operation; the module carrier type must have a zmodType structure, and the scalar carrier must have a ringType structure. LmodType R V m == packs the mixin v to build an Lmodule of type lmodType R. The carrier type V must have a zmodType structure. [lmodType R of V for S] == V-clone of an lmodType R structure S. [lmodType R of V] == clone of a canonical lmodType R structure on V. a *: v == v scaled by a, when v is in an Lmodule V and a is in the scalar Ring of V. scaler_closed S <-> collective predicate S is closed under scaling. linear_closed S <-> collective predicate S is closed under linear combinations (a *: u + v in S when u, v in S). submod_closed S <-> collective predicate S is closed under lmodType operations (0 and a *: u + v in S). SubmodPred scaleS == packs scaleS : scaler_closed S in a submodPred S interface structure corresponding to the above property, provided S's key is a zmodPred; submod_closed coerces to all the prerequisites. [lmodMixin of V by <: ] == mixin for a subType of an lmodType, whose predicate's key is a submodPred.

(Joint Center)Lalgebra (left algebra, ring with scaling that associates on the left):

lalgType R == interface type for Lalgebra structures with scalars in R; R must have ringType structure. LalgType R V scalAl == packs scalAl : k (x y) = (k x) y into an Lalgebra of type lalgType R. The carrier type V must have both lmodType R and ringType canonical structures. R^o == the regular algebra of R: R^o is convertible to R, but when R has a ringType structure then R^o extends it to an lalgType structure by letting R act on itself: if x : R and y : R^o then x *: y = x * (y : R). k%:A == the image of the scalar k in an L-algebra; this is simply notation for k *: 1. [lalgType R of V for S] == V-clone the lalgType R structure S. [lalgType R of V] == clone of a canonical lalgType R structure on V. subalg_closed S <-> collective predicate S is closed under lalgType operations (1, a *: u + v and u * v in S). SubalgPred scaleS == packs scaleS : scaler_closed S in a subalgPred S interface structure corresponding to the above property, provided S's key is a subringPred; subalg_closed coerces to all the prerequisites. [lalgMixin of V by <: ] == mixin axiom for a subType of an lalgType.

(Joint Center)Algebra (ring with scaling that associates both left and right):

algType R == type for Algebra structure with scalars in R. R should be a commutative ring. AlgType R A scalAr == packs scalAr : k (x y) = x (k y) into an Algebra Structure of type algType R. The carrier type A must have an lalgType R structure. CommAlgType R A == creates an Algebra structure for an A that has both lalgType R and comRingType structures. [algType R of V for S] == V-clone of an algType R structure on S. [algType R of V] == clone of a canonical algType R structure on V. [algMixin of V by <: ] == mixin axiom for a subType of an algType.

(Joint Center)UnitAlgebra (algebra with computable inverses):

unitAlgType R == interface type for UnitAlgebra structure with scalars in R; R should have a unitRingType structure. [unitAlgType R of V] == a unitAlgType R structure for V created by merging canonical algType and unitRingType on V. divalg_closed S <-> collective predicate S is closed under all unitAlgType operations (1, a *: u + v and u / v are in S fo u, v in S). DivalgPred scaleS == packs scaleS : scaler_closed S in a divalgPred S interface structure corresponding to the above property, provided S's key is a divringPred; divalg_closed coerces to all the prerequisites.
In addition to this structure hierarchy, we also develop a separate, parallel hierarchy for morphisms linking these structures:

(Joint Center)Additive (additive functions):

additive f <-> f of type U -> V is additive, i.e., f maps the Zmodule structure of U to that of V, 0 to 0,
  • to - and + to + (equivalently, binary - to -).
:= {morph f : u v / u + v}. {additive U -> V} == the interface type for a Structure (keyed on a function f : U -> V) that encapsulates the additive property; both U and V must have zmodType canonical structures. Additive add_f == packs add_f : additive f into an additive function structure of type {additive U -> V}. [additive of f as g] == an f-clone of the additive structure on the function g -- f and g must be convertible. [additive of f] == a clone of an existing additive structure on f.

(Joint Center)RMorphism (ring morphisms):

multiplicative f <-> f of type R -> S is multiplicative, i.e., f maps 1 and * in R to 1 and * in S, respectively, R ans S must have canonical ringType structures. rmorphism f <-> f is a ring morphism, i.e., f is both additive and multiplicative. {rmorphism R -> S} == the interface type for ring morphisms, i.e., a Structure that encapsulates the rmorphism property for functions f : R -> S; both R and S must have ringType structures. RMorphism morph_f == packs morph_f : rmorphism f into a Ring morphism structure of type {rmorphism R -> S}. AddRMorphism mul_f == packs mul_f : multiplicative f into an rmorphism structure of type {rmorphism R -> S}; f must already have an {additive R -> S} structure. [rmorphism of f as g] == an f-clone of the rmorphism structure of g. [rmorphism of f] == a clone of an existing additive structure on f.
  • > If R and S are UnitRings the f also maps units to units and inverses of units to inverses; if R is a field then f if a field isomorphism between R and its image.
  • > As rmorphism coerces to both additive and multiplicative, all structures for f can be built from a single proof of rmorphism f.
  • > Additive properties (raddf_suffix, see below) are duplicated and specialised for RMorphism (as rmorph_suffix). This allows more precise rewriting and cleaner chaining: although raddf lemmas will recognize RMorphism functions, the converse will not hold (we cannot add reverse inheritance rules because of incomplete backtracking in the Canonical Projection unification), so one would have to insert a /= every time one switched from additive to multiplicative rules.
  • > The property duplication also means that it is not strictly necessary to declare all Additive instances.

(Joint Center)Linear (linear functions):

scalable f <-> f of type U -> V is scalable, i.e., f morphs scaling on U to scaling on V, a *: _ to a *: _. U and V must both have lmodType R structures, for the same ringType R. scalable_for s f <-> f is scalable for scaling operator s, i.e., f morphs a *: _ to s a _; the range of f only need to be a zmodType. The scaling operator s should be one of *:%R (see scalable, above), *%R or a combination nu \; *%R or nu \; *:%R with nu : {rmorphism _}; otherwise some of the theory (e.g., the linearZ rule) will not apply. linear f <-> f of type U -> V is linear, i.e., f morphs linear combinations a *: u + v in U to similar linear combinations in V; U and V must both have lmodType R structures, for the same ringType R. := forall a, {morph f: u v / a *: u + v}. scalar f <-> f of type U -> R is a scalar function, i.e., f (a *: u + v) = a * f u + f v. linear_for s f <-> f is linear for the scaling operator s, i.e., f (a *: u + v) = s a (f u) + f v. The range of f only needs to be a zmodType, but s MUST be of the form described in in scalable_for paragraph for this predicate to type check. lmorphism f <-> f is both additive and scalable. This is in fact equivalent to linear f, although somewhat less convenient to prove. lmorphism_for s f <-> f is both additive and scalable for s. {linear U -> V} == the interface type for linear functions, i.e., a Structure that encapsulates the linear property for functions f : U -> V; both U and V must have lmodType R structures, for the same R. {scalar U} == the interface type for scalar functions, of type U -> R where U has an lmodType R structure. {linear U -> V | s} == the interface type for functions linear for s. Linear lin_f == packs lin_f : lmorphism_for s f into a linear function structure of type {linear U -> V | s}. As linear_for s f coerces to lmorphism_for s f, Linear can be used with lin_f : linear_for s f (indeed, that is the recommended usage). Note that as linear f, scalar f, {linear U -> V} and {scalar U} are simply notation for corresponding generic "_for" forms, Linear can be used for any of these special cases, transparantly. AddLinear scal_f == packs scal_f : scalable_for s f into a {linear U -> V | s} structure; f must already have an additive structure; as with Linear, AddLinear can be used with lin_f : linear f, etc [linear of f as g] == an f-clone of the linear structure of g. [linear of f] == a clone of an existing linear structure on f. (a *: u)%Rlin == transient forms that simplifiy to a *: u, a * u, (a * u)%Rlin nu a *: u, and nu a * u, respectively, and are (a *:^nu u)%Rlin created by rewriting with the linearZ lemma. The (a *^nu u)%Rlin forms allows the RHS of linearZ to be matched reliably, using the GRing.Scale.law structure.
  • > Similarly to Ring morphisms, additive properties are specialized for linear functions.
  • > Although {scalar U} is convertible to {linear U -> R^o}, it does not actually use R^o, so that rewriting preserves the canonical structure of the range of scalar functions.
  • > The generic linearZ lemma uses a set of bespoke interface structures to ensure that both left-to-right and right-to-left rewriting work even in the presence of scaling functions that simplify non-trivially (e.g., idfun \; *%R). Because most of the canonical instances and projections are coercions the machinery will be mostly invisible (with only the {linear ...} structure and %Rlin notations showing), but users should beware that in (a *: f u)%Rlin, a actually occurs in the f u subterm.
  • > The simpler linear_LR, or more specialized linearZZ and scalarZ rules should be used instead of linearZ if there are complexity issues, as well as for explicit forward and backward application, as the main parameter of linearZ is a proper sub-interface of {linear fUV | s}.

(Joint Center)LRMorphism (linear ring morphisms, i.e., algebra morphisms):

lrmorphism f <-> f of type A -> B is a linear Ring (Algebra) morphism: f is both additive, multiplicative and scalable. A and B must both have lalgType R canonical structures, for the same ringType R. lrmorphism_for s f <-> f a linear Ring morphism for the scaling operator s: f is additive, multiplicative and scalable for s. A must be an lalgType R, but B only needs to have a ringType structure. {lrmorphism A -> B} == the interface type for linear morphisms, i.e., a Structure that encapsulates the lrmorphism property for functions f : A -> B; both A and B must have lalgType R structures, for the same R. {lrmorphism A -> B | s} == the interface type for morphisms linear for s. LRmorphism lrmorph_f == packs lrmorph_f : lrmorphism_for s f into a linear morphism structure of type {lrmorphism A -> B | s}. Like Linear, LRmorphism can be used transparently for lrmorphism f. AddLRmorphism scal_f == packs scal_f : scalable_for s f into a linear morphism structure of type {lrmorphism A -> B | s}; f must already have an {rmorphism A -> B} structure, and AddLRmorphism can be applied to a linear_for s f, linear f, scalar f, etc argument, like AddLinear. [lrmorphism of f] == creates an lrmorphism structure from existing rmorphism and linear structures on f; this is the preferred way of creating lrmorphism structures.
  • > Linear and rmorphism properties do not need to be specialized for as we supply inheritance join instances in both directions.
Finally we supply some helper notation for morphisms: x^f == the image of x under some morphism. This notation is only reserved (not defined) here; it is bound locally in sections where some morphism is used heavily (e.g., the container morphism in the parametricity sections of poly and matrix, or the Frobenius section here). \0 == the constant null function, which has a canonical linear structure, and simplifies on application (see ssrfun.v). f \+ g == the additive composition of f and g, i.e., the function x |-> f x + g x; f \+ g is canonically linear when f and g are, and simplifies on application (see ssrfun.v). f \- g == the function x |-> f x - g x, canonically linear when f and g are, and simplifies on application. k \*: f == the function x |-> k *: f x, which is canonically linear when f is and simplifies on application (this is a shorter alternative to *:%R k \o f). GRing.in_alg A == the ring morphism that injects R into A, where A has an lalgType R structure; GRing.in_alg A k simplifies to k%:A. a \*o f == the function x |-> a * f x, canonically linear linear when f is and its codomain is an algType and which simplifies on application. a \o* f == the function x |-> f x * a, canonically linear linear when f is and its codomain is an lalgType and which simplifies on application. The Lemmas about these structures are contained in both the GRing module and in the submodule GRing.Theory, which can be imported when unqualified access to the theory is needed (GRing.Theory also allows the unqualified use of additive, linear, Linear, etc). The main GRing module should NOT be imported. Notations are defined in scope ring_scope (delimiter %R), except term and formula notations, which are in term_scope (delimiter %T). This library also extends the conventional suffixes described in library ssrbool.v with the following: 0 -- ring 0, as in addr0 : x + 0 = x. 1 -- ring 1, as in mulr1 : x * 1 = x. D -- ring addition, as in linearD : f (u + v) = f u + f v. B -- ring substraction, as in opprB : - (x - y) = y - x. M -- ring multiplication, as in invfM : (x * y)^-1 = x^-1 * y^-1. Mn -- ring by nat multiplication, as in raddfMn : f (x *+ n) = f x *+ n. N -- ring opposite, as in mulNr : (- x) * y = - (x * y). V -- ring inverse, as in mulVr : x^-1 * x = 1. X -- ring exponentiation, as in rmorphX : f (x ^+ n) = f x ^+ n. Z -- (left) module scaling, as in linearZ : f (a *: v) = s *: f v. The operator suffixes D, B, M and X are also used for the corresponding operations on nat, as in natrX : (m ^ n)%:R = m%:R ^+ n. For the binary power operator, a trailing "n" suffix is used to indicate the operator suffix applies to the left-hand ring argument, as in expr1n : 1 ^+ n = 1 vs. expr1 : x ^+ 1 = x.

Set Implicit Arguments.

Reserved Notation "+%R" (at level 0).
Reserved Notation "-%R" (at level 0).
Reserved Notation "*%R" (at level 0, format " *%R").
Reserved Notation "*:%R" (at level 0, format " *:%R").
Reserved Notation "n %:R" (at level 2, left associativity, format "n %:R").
Reserved Notation "k %:A" (at level 2, left associativity, format "k %:A").
Reserved Notation "[ 'char' F ]" (at level 0, format "[ 'char' F ]").

Reserved Notation "x %:T" (at level 2, left associativity, format "x %:T").
Reserved Notation "''X_' i" (at level 8, i at level 2, format "''X_' i").
Patch for recurring Coq parser bug: Coq seg faults when a level 200 notation is used as a pattern.
Reserved Notation "''exists' ''X_' i , f"
  (at level 199, i at level 2, right associativity,
   format "'[hv' ''exists' ''X_' i , '/ ' f ']'").
Reserved Notation "''forall' ''X_' i , f"
  (at level 199, i at level 2, right associativity,
   format "'[hv' ''forall' ''X_' i , '/ ' f ']'").

Reserved Notation "x ^f" (at level 2, left associativity, format "x ^f").

Reserved Notation "\0" (at level 0).
Reserved Notation "f \+ g" (at level 50, left associativity).
Reserved Notation "f \- g" (at level 50, left associativity).
Reserved Notation "a \*o f" (at level 40).
Reserved Notation "a \o* f" (at level 40).
Reserved Notation "a \*: f" (at level 40).

Delimit Scope ring_scope with R.
Delimit Scope term_scope with T.
Local Open Scope ring_scope.

Module Import GRing.

Import Monoid.Theory.

Module Zmodule.

Record mixin_of (V : Type) : Type := Mixin {
  zero : V;
  opp : VV;
  add : VVV;
  _ : associative add;
  _ : commutative add;
  _ : left_id zero add;
  _ : left_inverse zero opp add
}.

Section ClassDef.

Record class_of T := Class { base : Choice.class_of T; mixin : mixin_of T }.
Local Coercion base : class_of >-> Choice.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (T : Type) (cT : type).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack m :=
  fun bT b & phant_id (Choice.class bT) bPack (@Class T b m) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Choice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Notation zmodType := type.
Notation ZmodType T m := (@pack T m _ _ id).
Notation ZmodMixin := Mixin.
Notation "[ 'zmodType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
  (at level 0, format "[ 'zmodType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'zmodType' 'of' T ]" := (@clone T _ _ id)
  (at level 0, format "[ 'zmodType' 'of' T ]") : form_scope.
End Exports.

End Zmodule.
Import Zmodule.Exports.

Definition zero V := Zmodule.zero (Zmodule.class V).
Definition opp V := Zmodule.opp (Zmodule.class V).
Definition add V := Zmodule.add (Zmodule.class V).

Local Notation "0" := (zero _) : ring_scope.
Local Notation "-%R" := (@opp _) : ring_scope.
Local Notation "- x" := (opp x) : ring_scope.
Local Notation "+%R" := (@add _) : ring_scope.
Local Notation "x + y" := (add x y) : ring_scope.
Local Notation "x - y" := (x + - y) : ring_scope.

Definition natmul V x n := nosimpl iterop _ n +%R x (zero V).

Local Notation "x *+ n" := (natmul x n) : ring_scope.
Local Notation "x *- n" := (- (x *+ n)) : ring_scope.

Local Notation "\sum_ ( i <- r | P ) F" := (\big[+%R/0]_(i <- r | P) F).
Local Notation "\sum_ ( m <= i < n ) F" := (\big[+%R/0]_(m i < n) F).
Local Notation "\sum_ ( i < n ) F" := (\big[+%R/0]_(i < n) F).
Local Notation "\sum_ ( i 'in' A ) F" := (\big[+%R/0]_(i in A) F).

Local Notation "s `_ i" := (nth 0 s i) : ring_scope.

Section ZmoduleTheory.

Variable V : zmodType.
Implicit Types x y : V.

Lemma addrA : @associative V +%R.
Lemma addrC : @commutative V V +%R.
Lemma add0r : @left_id V V 0 +%R.
Lemma addNr : @left_inverse V V V 0 -%R +%R.

Lemma addr0 : @right_id V V 0 +%R.
Lemma addrN : @right_inverse V V V 0 -%R +%R.
Definition subrr := addrN.

Canonical add_monoid := Monoid.Law addrA add0r addr0.
Canonical add_comoid := Monoid.ComLaw addrC.

Lemma addrCA : @left_commutative V V +%R.
Lemma addrAC : @right_commutative V V +%R.
Lemma addrACA : @interchange V +%R +%R.

Lemma addKr : @left_loop V V -%R +%R.
Lemma addNKr : @rev_left_loop V V -%R +%R.
Lemma addrK : @right_loop V V -%R +%R.
Lemma addrNK : @rev_right_loop V V -%R +%R.
Definition subrK := addrNK.
Lemma addrI : @right_injective V V V +%R.
Lemma addIr : @left_injective V V V +%R.
Lemma opprK : @involutive V -%R.
Lemma oppr_inj : @injective V V -%R.
Lemma oppr0 : -0 = 0 :> V.
Lemma oppr_eq0 x : (- x == 0) = (x == 0).

Lemma subr0 x : x - 0 = x.
Lemma sub0r x : 0 - x = - x.

Lemma opprD : {morph -%R: x y / x + y : V}.

Lemma opprB x y : - (x - y) = y - x.

Lemma subr_eq x y z : (x - z == y) = (x == y + z).

Lemma subr_eq0 x y : (x - y == 0) = (x == y).

Lemma addr_eq0 x y : (x + y == 0) = (x == - y).

Lemma eqr_opp x y : (- x == - y) = (x == y).

Lemma eqr_oppLR x y : (- x == y) = (x == - y).

Lemma mulr0n x : x *+ 0 = 0.
Lemma mulr1n x : x *+ 1 = x.
Lemma mulr2n x : x *+ 2 = x + x.

Lemma mulrS x n : x *+ n.+1 = x + x *+ n.

Lemma mulrSr x n : x *+ n.+1 = x *+ n + x.

Lemma mulrb x (b : bool) : x *+ b = (if b then x else 0).

Lemma mul0rn n : 0 *+ n = 0 :> V.

Lemma mulNrn x n : (- x) *+ n = x *- n.

Lemma mulrnDl n : {morph (fun xx *+ n) : x y / x + y}.

Lemma mulrnDr x m n : x *+ (m + n) = x *+ m + x *+ n.

Lemma mulrnBl n : {morph (fun xx *+ n) : x y / x - y}.

Lemma mulrnBr x m n : n mx *+ (m - n) = x *+ m - x *+ n.

Lemma mulrnA x m n : x *+ (m × n) = x *+ m *+ n.

Lemma mulrnAC x m n : x *+ m *+ n = x *+ n *+ m.

Lemma sumrN I r P (F : IV) :
  (\sum_(i <- r | P i) - F i = - (\sum_(i <- r | P i) F i)).

Lemma sumrB I r (P : pred I) (F1 F2 : IV) :
  \sum_(i <- r | P i) (F1 i - F2 i)
     = \sum_(i <- r | P i) F1 i - \sum_(i <- r | P i) F2 i.

Lemma sumrMnl I r P (F : IV) n :
  \sum_(i <- r | P i) F i *+ n = (\sum_(i <- r | P i) F i) *+ n.

Lemma sumrMnr x I r P (F : Inat) :
  \sum_(i <- r | P i) x *+ F i = x *+ (\sum_(i <- r | P i) F i).

Lemma sumr_const (I : finType) (A : pred I) (x : V) :
  \sum_(i in A) x = x *+ #|A|.

Lemma telescope_sumr n m (f : natV) : n m
  \sum_(n k < m) (f k.+1 - f k) = f m - f n.

Section ClosedPredicates.

Variable S : predPredType V.

Definition addr_closed := 0 \in S {in S &, u v, u + v \in S}.
Definition oppr_closed := {in S, u, - u \in S}.
Definition subr_2closed := {in S &, u v, u - v \in S}.
Definition zmod_closed := 0 \in S subr_2closed.

Lemma zmod_closedN : zmod_closedoppr_closed.

Lemma zmod_closedD : zmod_closedaddr_closed.

End ClosedPredicates.

End ZmoduleTheory.

Implicit Arguments addrI [[V] x1 x2].
Implicit Arguments addIr [[V] x1 x2].
Implicit Arguments oppr_inj [[V] x1 x2].

Module Ring.

Record mixin_of (R : zmodType) : Type := Mixin {
  one : R;
  mul : RRR;
  _ : associative mul;
  _ : left_id one mul;
  _ : right_id one mul;
  _ : left_distributive mul +%R;
  _ : right_distributive mul +%R;
  _ : one != 0
}.

Definition EtaMixin R one mul mulA mul1x mulx1 mul_addl mul_addr nz1 :=
  let _ := @Mixin R one mul mulA mul1x mulx1 mul_addl mul_addr nz1 in
  @Mixin (Zmodule.Pack (Zmodule.class R) R) _ _
     mulA mul1x mulx1 mul_addl mul_addr nz1.

Section ClassDef.

Record class_of (R : Type) : Type := Class {
  base : Zmodule.class_of R;
  mixin : mixin_of (Zmodule.Pack base R)
}.
Local Coercion base : class_of >-> Zmodule.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (T : Type) (cT : type).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack b0 (m0 : mixin_of (@Zmodule.Pack T b0 T)) :=
  fun bT b & phant_id (Zmodule.class bT) b
  fun m & phant_id m0 mPack (@Class T b m) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Zmodule.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Notation ringType := type.
Notation RingType T m := (@pack T _ m _ _ id _ id).
Notation RingMixin := Mixin.
Notation "[ 'ringType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
  (at level 0, format "[ 'ringType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'ringType' 'of' T ]" := (@clone T _ _ id)
  (at level 0, format "[ 'ringType' 'of' T ]") : form_scope.
End Exports.

End Ring.
Import Ring.Exports.

Definition one (R : ringType) : R := Ring.one (Ring.class R).
Definition mul (R : ringType) : RRR := Ring.mul (Ring.class R).
Definition exp R x n := nosimpl iterop _ n (@mul R) x (one R).
Notation sign R b := (exp (- one R) (nat_of_bool b)) (only parsing).
Definition comm R x y := @mul R x y = mul y x.
Definition lreg R x := injective (@mul R x).
Definition rreg R x := injective ((@mul R)^~ x).

Local Notation "1" := (one _) : ring_scope.
Local Notation "- 1" := (- (1)) : ring_scope.
Local Notation "n %:R" := (1 *+ n) : ring_scope.
Local Notation "*%R" := (@mul _).
Local Notation "x * y" := (mul x y) : ring_scope.
Local Notation "x ^+ n" := (exp x n) : ring_scope.

Local Notation "\prod_ ( i <- r | P ) F" := (\big[*%R/1]_(i <- r | P) F).
Local Notation "\prod_ ( i | P ) F" := (\big[*%R/1]_(i | P) F).
Local Notation "\prod_ ( i 'in' A ) F" := (\big[*%R/1]_(i in A) F).
Local Notation "\prod_ ( m <= i < n ) F" := (\big[*%R/1%R]_(m i < n) F%R).

The ``field'' characteristic; the definition, and many of the theorems, has to apply to rings as well; indeed, we need the Frobenius automorphism results for a non commutative ring in the proof of Gorenstein 2.6.3.
Definition char (R : Ring.type) of phant R : nat_pred :=
  [pred p | prime p & p%:R == 0 :> R].

Local Notation "[ 'char' R ]" := (char (Phant R)) : ring_scope.

Converse ring tag.
Definition converse R : Type := R.
Local Notation "R ^c" := (converse R) (at level 2, format "R ^c") : type_scope.

Section RingTheory.

Variable R : ringType.
Implicit Types x y : R.

Lemma mulrA : @associative R *%R.
Lemma mul1r : @left_id R R 1 *%R.
Lemma mulr1 : @right_id R R 1 *%R.
Lemma mulrDl : @left_distributive R R *%R +%R.
Lemma mulrDr : @right_distributive R R *%R +%R.
Lemma oner_neq0 : 1 != 0 :> R.
Lemma oner_eq0 : (1 == 0 :> R) = false.

Lemma mul0r : @left_zero R R 0 *%R.
Lemma mulr0 : @right_zero R R 0 *%R.
Lemma mulrN x y : x × (- y) = - (x × y).
Lemma mulNr x y : (- x) × y = - (x × y).
Lemma mulrNN x y : (- x) × (- y) = x × y.
Lemma mulN1r x : -1 × x = - x.
Lemma mulrN1 x : x × -1 = - x.

Canonical mul_monoid := Monoid.Law mulrA mul1r mulr1.
Canonical muloid := Monoid.MulLaw mul0r mulr0.
Canonical addoid := Monoid.AddLaw mulrDl mulrDr.

Lemma mulr_suml I r P (F : IR) x :
  (\sum_(i <- r | P i) F i) × x = \sum_(i <- r | P i) F i × x.

Lemma mulr_sumr I r P (F : IR) x :
  x × (\sum_(i <- r | P i) F i) = \sum_(i <- r | P i) x × F i.

Lemma mulrBl x y z : (y - z) × x = y × x - z × x.

Lemma mulrBr x y z : x × (y - z) = x × y - x × z.

Lemma mulrnAl x y n : (x *+ n) × y = (x × y) *+ n.

Lemma mulrnAr x y n : x × (y *+ n) = (x × y) *+ n.

Lemma mulr_natl x n : n%:R × x = x *+ n.

Lemma mulr_natr x n : x × n%:R = x *+ n.

Lemma natrD m n : (m + n)%:R = m%:R + n%:R :> R.

Lemma natrB m n : n m(m - n)%:R = m%:R - n%:R :> R.

Definition natr_sum := big_morph (natmul 1) natrD (mulr0n 1).

Lemma natrM m n : (m × n)%:R = m%:R × n%:R :> R.

Lemma expr0 x : x ^+ 0 = 1.
Lemma expr1 x : x ^+ 1 = x.
Lemma expr2 x : x ^+ 2 = x × x.

Lemma exprS x n : x ^+ n.+1 = x × x ^+ n.

Lemma expr0n n : 0 ^+ n = (n == 0%N)%:R :> R.

Lemma expr1n n : 1 ^+ n = 1 :> R.

Lemma exprD x m n : x ^+ (m + n) = x ^+ m × x ^+ n.

Lemma exprSr x n : x ^+ n.+1 = x ^+ n × x.

Lemma commr_sym x y : comm x ycomm y x.
Lemma commr_refl x : comm x x.

Lemma commr0 x : comm x 0.

Lemma commr1 x : comm x 1.

Lemma commrN x y : comm x ycomm x (- y).

Lemma commrN1 x : comm x (-1).

Lemma commrD x y z : comm x ycomm x zcomm x (y + z).

Lemma commrMn x y n : comm x ycomm x (y *+ n).

Lemma commrM x y z : comm x ycomm x zcomm x (y × z).

Lemma commr_nat x n : comm x n%:R.

Lemma commrX x y n : comm x ycomm x (y ^+ n).

Lemma exprMn_comm x y n : comm x y(x × y) ^+ n = x ^+ n × y ^+ n.

Lemma commr_sign x n : comm x ((-1) ^+ n).

Lemma exprMn_n x m n : (x *+ m) ^+ n = x ^+ n *+ (m ^ n) :> R.

Lemma exprM x m n : x ^+ (m × n) = x ^+ m ^+ n.

Lemma exprAC x m n : (x ^+ m) ^+ n = (x ^+ n) ^+ m.

Lemma expr_mod n x i : x ^+ n = 1 → x ^+ (i %% n) = x ^+ i.

Lemma expr_dvd n x i : x ^+ n = 1 → n %| ix ^+ i = 1.

Lemma natrX n k : (n ^ k)%:R = n%:R ^+ k :> R.

Lemma signr_odd n : (-1) ^+ (odd n) = (-1) ^+ n :> R.

Lemma signr_eq0 n : ((-1) ^+ n == 0 :> R) = false.

Lemma mulr_sign (b : bool) x : (-1) ^+ b × x = (if b then - x else x).

Lemma signr_addb b1 b2 : (-1) ^+ (b1 (+) b2) = (-1) ^+ b1 × (-1) ^+ b2 :> R.

Lemma signrE (b : bool) : (-1) ^+ b = 1 - b.*2%:R :> R.

Lemma signrN b : (-1) ^+ (~~ b) = - (-1) ^+ b :> R.

Lemma mulr_signM (b1 b2 : bool) x1 x2 :
  ((-1) ^+ b1 × x1) × ((-1) ^+ b2 × x2) = (-1) ^+ (b1 (+) b2) × (x1 × x2).

Lemma exprNn x n : (- x) ^+ n = (-1) ^+ n × x ^+ n :> R.

Lemma sqrrN x : (- x) ^+ 2 = x ^+ 2.

Lemma sqrr_sign n : ((-1) ^+ n) ^+ 2 = 1 :> R.

Lemma signrMK n : @involutive R ( *%R ((-1) ^+ n)).

Lemma mulrI_eq0 x y : lreg x(x × y == 0) = (y == 0).

Lemma lreg_neq0 x : lreg xx != 0.

Lemma mulrI0_lreg x : ( y, x × y = 0 → y = 0) → lreg x.

Lemma lregN x : lreg xlreg (- x).

Lemma lreg1 : lreg (1 : R).

Lemma lregM x y : lreg xlreg ylreg (x × y).

Lemma lregX x n : lreg xlreg (x ^+ n).

Lemma lreg_sign n : lreg ((-1) ^+ n : R).

Lemma prodr_const (I : finType) (A : pred I) (x : R) :
  \prod_(i in A) x = x ^+ #|A|.

Lemma prodrXr x I r P (F : Inat) :
  \prod_(i <- r | P i) x ^+ F i = x ^+ (\sum_(i <- r | P i) F i).

Lemma prodrN (I : finType) (A : pred I) (F : IR) :
  \prod_(i in A) - F i = (- 1) ^+ #|A| × \prod_(i in A) F i.

Lemma prodrMn n (I : finType) (A : pred I) (F : IR) :
  \prod_(i in A) (F i *+ n) = \prod_(i in A) F i *+ n ^ #|A|.

Lemma natr_prod I r P (F : Inat) :
  (\prod_(i <- r | P i) F i)%:R = \prod_(i <- r | P i) (F i)%:R :> R.

Lemma exprDn_comm x y n (cxy : comm x y) :
  (x + y) ^+ n = \sum_(i < n.+1) (x ^+ (n - i) × y ^+ i) *+ 'C(n, i).

Lemma exprBn_comm x y n (cxy : comm x y) :
  (x - y) ^+ n =
    \sum_(i < n.+1) ((-1) ^+ i × x ^+ (n - i) × y ^+ i) *+ 'C(n, i).

Lemma subrXX_comm x y n (cxy : comm x y) :
  x ^+ n - y ^+ n = (x - y) × (\sum_(i < n) x ^+ (n.-1 - i) × y ^+ i).

Lemma exprD1n x n : (x + 1) ^+ n = \sum_(i < n.+1) x ^+ i *+ 'C(n, i).

Lemma subrX1 x n : x ^+ n - 1 = (x - 1) × (\sum_(i < n) x ^+ i).

Lemma sqrrD1 x : (x + 1) ^+ 2 = x ^+ 2 + x *+ 2 + 1.

Lemma sqrrB1 x : (x - 1) ^+ 2 = x ^+ 2 - x *+ 2 + 1.

Lemma subr_sqr_1 x : x ^+ 2 - 1 = (x - 1) × (x + 1).

Definition Frobenius_aut p of p \in [char R] := fun xx ^+ p.

Section FrobeniusAutomorphism.

Variable p : nat.
Hypothesis charFp : p \in [char R].

Lemma charf0 : p%:R = 0 :> R.
Lemma charf_prime : prime p.
Hint Resolve charf_prime.

Lemma mulrn_char x : x *+ p = 0.

Lemma natr_mod_char n : (n %% p)%:R = n%:R :> R.

Lemma dvdn_charf n : (p %| n)%N = (n%:R == 0 :> R).

Lemma charf_eq : [char R] =i (p : nat_pred).

Lemma bin_lt_charf_0 k : 0 < k < p'C(p, k)%:R = 0 :> R.

Local Notation "x ^f" := (Frobenius_aut charFp x).

Lemma Frobenius_autE x : x^f = x ^+ p.
Local Notation fE := Frobenius_autE.

Lemma Frobenius_aut0 : 0^f = 0.

Lemma Frobenius_aut1 : 1^f = 1.

Lemma Frobenius_autD_comm x y (cxy : comm x y) : (x + y)^f = x^f + y^f.

Lemma Frobenius_autMn x n : (x *+ n)^f = x^f *+ n.

Lemma Frobenius_aut_nat n : (n%:R)^f = n%:R.

Lemma Frobenius_autM_comm x y : comm x y(x × y)^f = x^f × y^f.

Lemma Frobenius_autX x n : (x ^+ n)^f = x^f ^+ n.

Lemma Frobenius_autN x : (- x)^f = - x^f.

Lemma Frobenius_autB_comm x y : comm x y(x - y)^f = x^f - y^f.

End FrobeniusAutomorphism.

Lemma exprNn_char x n : [char R].-nat n(- x) ^+ n = - (x ^+ n).

Section Char2.

Hypothesis charR2 : 2 \in [char R].

Lemma addrr_char2 x : x + x = 0.

Lemma oppr_char2 x : - x = x.

Lemma subr_char2 x y : x - y = x + y.

Lemma addrK_char2 x : involutive (+%R^~ x).

Lemma addKr_char2 x : involutive (+%R x).

End Char2.

Canonical converse_eqType := [eqType of R^c].
Canonical converse_choiceType := [choiceType of R^c].
Canonical converse_zmodType := [zmodType of R^c].

Definition converse_ringMixin :=
  let mul' x y := y × x in
  let mulrA' x y z := esym (mulrA z y x) in
  let mulrDl' x y z := mulrDr z x y in
  let mulrDr' x y z := mulrDl y z x in
  @Ring.Mixin converse_zmodType
    1 mul' mulrA' mulr1 mul1r mulrDl' mulrDr' oner_neq0.
Canonical converse_ringType := RingType R^c converse_ringMixin.

Section ClosedPredicates.

Variable S : predPredType R.

Definition mulr_2closed := {in S &, u v, u × v \in S}.
Definition mulr_closed := 1 \in S mulr_2closed.
Definition smulr_closed := -1 \in S mulr_2closed.
Definition semiring_closed := addr_closed S mulr_closed.
Definition subring_closed := [/\ 1 \in S, subr_2closed S & mulr_2closed].

Lemma smulr_closedM : smulr_closedmulr_closed.

Lemma smulr_closedN : smulr_closedoppr_closed S.

Lemma semiring_closedD : semiring_closedaddr_closed S.

Lemma semiring_closedM : semiring_closedmulr_closed.

Lemma subring_closedB : subring_closedzmod_closed S.

Lemma subring_closedM : subring_closedsmulr_closed.

Lemma subring_closed_semi : subring_closedsemiring_closed.

End ClosedPredicates.

End RingTheory.

Section RightRegular.

Variable R : ringType.
Implicit Types x y : R.
Let Rc := converse_ringType R.

Lemma mulIr_eq0 x y : rreg x(y × x == 0) = (y == 0).

Lemma mulIr0_rreg x : ( y, y × x = 0 → y = 0) → rreg x.

Lemma rreg_neq0 x : rreg xx != 0.

Lemma rregN x : rreg xrreg (- x).

Lemma rreg1 : rreg (1 : R).

Lemma rregM x y : rreg xrreg yrreg (x × y).

Lemma revrX x n : (x : Rc) ^+ n = (x : R) ^+ n.

Lemma rregX x n : rreg xrreg (x ^+ n).

End RightRegular.

Module Lmodule.

Structure mixin_of (R : ringType) (V : zmodType) : Type := Mixin {
  scale : RVV;
  _ : a b v, scale a (scale b v) = scale (a × b) v;
  _ : left_id 1 scale;
  _ : right_distributive scale +%R;
  _ : v, {morph scale^~ v: a b / a + b}
}.

Section ClassDef.

Variable R : ringType.

Structure class_of V := Class {
  base : Zmodule.class_of V;
  mixin : mixin_of R (Zmodule.Pack base V)
}.
Local Coercion base : class_of >-> Zmodule.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variable (phR : phant R) (T : Type) (cT : type phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack phR T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack b0 (m0 : mixin_of R (@Zmodule.Pack T b0 T)) :=
  fun bT b & phant_id (Zmodule.class bT) b
  fun m & phant_id m0 mPack phR (@Class T b m) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.

End ClassDef.

Module Import Exports.
Coercion base : class_of >-> Zmodule.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Notation lmodType R := (type (Phant R)).
Notation LmodType R T m := (@pack _ (Phant R) T _ m _ _ id _ id).
Notation LmodMixin := Mixin.
Notation "[ 'lmodType' R 'of' T 'for' cT ]" := (@clone _ (Phant R) T cT _ idfun)
  (at level 0, format "[ 'lmodType' R 'of' T 'for' cT ]") : form_scope.
Notation "[ 'lmodType' R 'of' T ]" := (@clone _ (Phant R) T _ _ id)
  (at level 0, format "[ 'lmodType' R 'of' T ]") : form_scope.
End Exports.

End Lmodule.
Import Lmodule.Exports.

Definition scale (R : ringType) (V : lmodType R) :=
  Lmodule.scale (Lmodule.class V).

Local Notation "*:%R" := (@scale _ _).
Local Notation "a *: v" := (scale a v) : ring_scope.

Section LmoduleTheory.

Variables (R : ringType) (V : lmodType R).
Implicit Types (a b c : R) (u v : V).

Local Notation "*:%R" := (@scale R V).

Lemma scalerA a b v : a *: (b *: v) = a × b *: v.

Lemma scale1r : @left_id R V 1 *:%R.

Lemma scalerDr a : {morph *:%R a : u v / u + v}.

Lemma scalerDl v : {morph *:%R^~ v : a b / a + b}.

Lemma scale0r v : 0 *: v = 0.

Lemma scaler0 a : a *: 0 = 0 :> V.

Lemma scaleNr a v : - a *: v = - (a *: v).

Lemma scaleN1r v : (- 1) *: v = - v.

Lemma scalerN a v : a *: (- v) = - (a *: v).

Lemma scalerBl a b v : (a - b) *: v = a *: v - b *: v.

Lemma scalerBr a u v : a *: (u - v) = a *: u - a *: v.

Lemma scaler_nat n v : n%:R *: v = v *+ n.

Lemma scaler_sign (b : bool) v: (-1) ^+ b *: v = (if b then - v else v).

Lemma signrZK n : @involutive V ( *:%R ((-1) ^+ n)).

Lemma scalerMnl a v n : a *: v *+ n = (a *+ n) *: v.

Lemma scalerMnr a v n : a *: v *+ n = a *: (v *+ n).

Lemma scaler_suml v I r (P : pred I) F :
  (\sum_(i <- r | P i) F i) *: v = \sum_(i <- r | P i) F i *: v.

Lemma scaler_sumr a I r (P : pred I) (F : IV) :
  a *: (\sum_(i <- r | P i) F i) = \sum_(i <- r | P i) a *: F i.

Section ClosedPredicates.

Variable S : predPredType V.

Definition scaler_closed := a, {in S, v, a *: v \in S}.
Definition linear_closed := a, {in S &, u v, a *: u + v \in S}.
Definition submod_closed := 0 \in S linear_closed.

Lemma linear_closedB : linear_closedsubr_2closed S.

Lemma submod_closedB : submod_closedzmod_closed S.

Lemma submod_closedZ : submod_closedscaler_closed.

End ClosedPredicates.

End LmoduleTheory.

Module Lalgebra.

Definition axiom (R : ringType) (V : lmodType R) (mul : VVV) :=
   a u v, a *: mul u v = mul (a *: u) v.

Section ClassDef.

Variable R : ringType.

Record class_of (T : Type) : Type := Class {
  base : Ring.class_of T;
  mixin : Lmodule.mixin_of R (Zmodule.Pack base T);
  ext : @axiom R (Lmodule.Pack _ (Lmodule.Class mixin) T) (Ring.mul base)
}.
Definition base2 R m := Lmodule.Class (@mixin R m).
Local Coercion base : class_of >-> Ring.class_of.
Local Coercion base2 : class_of >-> Lmodule.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variable (phR : phant R) (T : Type) (cT : type phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack phR T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack T b0 mul0 (axT : @axiom R (@Lmodule.Pack R _ T b0 T) mul0) :=
  fun bT b & phant_id (Ring.class bT) (b : Ring.class_of T) ⇒
  fun mT m & phant_id (@Lmodule.class R phR mT) (@Lmodule.Class R T b m) ⇒
  fun ax & phant_id axT ax
  Pack (Phant R) (@Class T b m ax) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @Ring.Pack cT xclass xT.
Definition lmodType := @Lmodule.Pack R phR cT xclass xT.
Definition lmod_ringType := @Lmodule.Pack R phR ringType xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Ring.class_of.
Coercion base2 : class_of >-> Lmodule.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Coercion ringType : type >-> Ring.type.
Canonical ringType.
Coercion lmodType : type >-> Lmodule.type.
Canonical lmodType.
Canonical lmod_ringType.
Notation lalgType R := (type (Phant R)).
Notation LalgType R T a := (@pack _ (Phant R) T _ _ a _ _ id _ _ id _ id).
Notation "[ 'lalgType' R 'of' T 'for' cT ]" := (@clone _ (Phant R) T cT _ idfun)
  (at level 0, format "[ 'lalgType' R 'of' T 'for' cT ]")
  : form_scope.
Notation "[ 'lalgType' R 'of' T ]" := (@clone _ (Phant R) T _ _ id)
  (at level 0, format "[ 'lalgType' R 'of' T ]") : form_scope.
End Exports.

End Lalgebra.
Import Lalgebra.Exports.

Scalar injection (see the definition of in_alg A below).
Local Notation "k %:A" := (k *: 1) : ring_scope.

Regular ring algebra tag.
Definition regular R : Type := R.
Local Notation "R ^o" := (regular R) (at level 2, format "R ^o") : type_scope.

Section LalgebraTheory.

Variables (R : ringType) (A : lalgType R).
Implicit Types x y : A.

Lemma scalerAl k (x y : A) : k *: (x × y) = k *: x × y.

Lemma mulr_algl a x : a%:A × x = a *: x.

Canonical regular_eqType := [eqType of R^o].
Canonical regular_choiceType := [choiceType of R^o].
Canonical regular_zmodType := [zmodType of R^o].
Canonical regular_ringType := [ringType of R^o].

Definition regular_lmodMixin :=
  let mkMixin := @Lmodule.Mixin R regular_zmodType (@mul R) in
  mkMixin (@mulrA R) (@mul1r R) (@mulrDr R) (fun v a bmulrDl a b v).

Canonical regular_lmodType := LmodType R R^o regular_lmodMixin.
Canonical regular_lalgType := LalgType R R^o (@mulrA regular_ringType).

Section ClosedPredicates.

Variable S : predPredType A.

Definition subalg_closed := [/\ 1 \in S, linear_closed S & mulr_2closed S].

Lemma subalg_closedZ : subalg_closedsubmod_closed S.

Lemma subalg_closedBM : subalg_closedsubring_closed S.

End ClosedPredicates.

End LalgebraTheory.

Morphism hierarchy.

Module Additive.

Section ClassDef.

Variables U V : zmodType.

Definition axiom (f : UV) := {morph f : x y / x - y}.

Structure map (phUV : phant (UV)) := Pack {apply; _ : axiom apply}.
Local Coercion apply : map >-> Funclass.

Variables (phUV : phant (UV)) (f g : UV) (cF : map phUV).
Definition class := let: Pack _ c as cF' := cF return axiom cF' in c.
Definition clone fA of phant_id g (apply cF) & phant_id fA class :=
  @Pack phUV f fA.

End ClassDef.

Module Exports.
Notation additive f := (axiom f).
Coercion apply : map >-> Funclass.
Notation Additive fA := (Pack (Phant _) fA).
Notation "{ 'additive' fUV }" := (map (Phant fUV))
  (at level 0, format "{ 'additive' fUV }") : ring_scope.
Notation "[ 'additive' 'of' f 'as' g ]" := (@clone _ _ _ f g _ _ idfun id)
  (at level 0, format "[ 'additive' 'of' f 'as' g ]") : form_scope.
Notation "[ 'additive' 'of' f ]" := (@clone _ _ _ f f _ _ id id)
  (at level 0, format "[ 'additive' 'of' f ]") : form_scope.
End Exports.

End Additive.
Include Additive.Exports. (* Allows GRing.additive to resolve conflicts. *)

Lifted additive operations.
Section LiftedZmod.
Variables (U : Type) (V : zmodType).
Definition null_fun_head (phV : phant V) of U : V := let: Phant := phV in 0.
Definition add_fun_head t (f g : UV) x := let: tt := t in f x + g x.
Definition sub_fun_head t (f g : UV) x := let: tt := t in f x - g x.
End LiftedZmod.

Lifted multiplication.
Section LiftedRing.
Variables (R : ringType) (T : Type).
Implicit Type f : TR.
Definition mull_fun_head t a f x := let: tt := t in a × f x.
Definition mulr_fun_head t a f x := let: tt := t in f x × a.
End LiftedRing.

Lifted linear operations.
Section LiftedScale.
Variables (R : ringType) (U : Type) (V : lmodType R) (A : lalgType R).
Definition scale_fun_head t a (f : UV) x := let: tt := t in a *: f x.
Definition in_alg_head (phA : phant A) k : A := let: Phant := phA in k%:A.
End LiftedScale.

Notation null_fun V := (null_fun_head (Phant V)) (only parsing).
The real in_alg notation is declared after GRing.Theory so that at least in Coq 8.2 it gets precedence when GRing.Theory is not imported.
Local Notation in_alg_loc A := (in_alg_head (Phant A)) (only parsing).

Local Notation "\0" := (null_fun _) : ring_scope.
Local Notation "f \+ g" := (add_fun_head tt f g) : ring_scope.
Local Notation "f \- g" := (sub_fun_head tt f g) : ring_scope.
Local Notation "a \*: f" := (scale_fun_head tt a f) : ring_scope.
Local Notation "x \*o f" := (mull_fun_head tt x f) : ring_scope.
Local Notation "x \o* f" := (mulr_fun_head tt x f) : ring_scope.

Section AdditiveTheory.

Section Properties.

Variables (U V : zmodType) (k : unit) (f : {additive UV}).

Lemma raddfB : {morph f : x y / x - y}.

Lemma raddf0 : f 0 = 0.

Lemma raddf_eq0 x : injective f(f x == 0) = (x == 0).

Lemma raddfN : {morph f : x / - x}.

Lemma raddfD : {morph f : x y / x + y}.

Lemma raddfMn n : {morph f : x / x *+ n}.

Lemma raddfMNn n : {morph f : x / x *- n}.

Lemma raddf_sum I r (P : pred I) E :
  f (\sum_(i <- r | P i) E i) = \sum_(i <- r | P i) f (E i).

Lemma can2_additive f' : cancel f f'cancel f' fadditive f'.

Lemma bij_additive :
  bijective fexists2 f' : {additive VU}, cancel f f' & cancel f' f.

Fact locked_is_additive : additive (locked_with k (f : UV)).
Canonical locked_additive := Additive locked_is_additive.

End Properties.

Section RingProperties.

Variables (R S : ringType) (f : {additive RS}).

Lemma raddfMnat n x : f (n%:R × x) = n%:R × f x.

Lemma raddfMsign n x : f ((-1) ^+ n × x) = (-1) ^+ n × f x.

Variables (U : lmodType R) (V : lmodType S) (h : {additive UV}).

Lemma raddfZnat n u : h (n%:R *: u) = n%:R *: h u.

Lemma raddfZsign n u : h ((-1) ^+ n *: u) = (-1) ^+ n *: h u.

End RingProperties.

Section AddFun.

Variables (U V W : zmodType) (f g : {additive VW}) (h : {additive UV}).

Fact idfun_is_additive : additive (@idfun U).
Canonical idfun_additive := Additive idfun_is_additive.

Fact comp_is_additive : additive (f \o h).
Canonical comp_additive := Additive comp_is_additive.

Fact opp_is_additive : additive (-%R : UU).
Canonical opp_additive := Additive opp_is_additive.

Fact null_fun_is_additive : additive (\0 : UV).
Canonical null_fun_additive := Additive null_fun_is_additive.

Fact add_fun_is_additive : additive (f \+ g).
Canonical add_fun_additive := Additive add_fun_is_additive.

Fact sub_fun_is_additive : additive (f \- g).
Canonical sub_fun_additive := Additive sub_fun_is_additive.

End AddFun.

Section MulFun.

Variables (R : ringType) (U : zmodType).
Variables (a : R) (f : {additive UR}).

Fact mull_fun_is_additive : additive (a \*o f).
Canonical mull_fun_additive := Additive mull_fun_is_additive.

Fact mulr_fun_is_additive : additive (a \o× f).
Canonical mulr_fun_additive := Additive mulr_fun_is_additive.

End MulFun.

Section ScaleFun.

Variables (R : ringType) (U : zmodType) (V : lmodType R).
Variables (a : R) (f : {additive UV}).

Canonical scale_additive := Additive (@scalerBr R V a).
Canonical scale_fun_additive := [additive of a \*: f as f \; *:%R a].

End ScaleFun.

End AdditiveTheory.

Module RMorphism.

Section ClassDef.

Variables R S : ringType.

Definition mixin_of (f : RS) :=
  {morph f : x y / x × y}%R × (f 1 = 1) : Prop.

Record class_of f : Prop := Class {base : additive f; mixin : mixin_of f}.
Local Coercion base : class_of >-> additive.

Structure map (phRS : phant (RS)) := Pack {apply; _ : class_of apply}.
Local Coercion apply : map >-> Funclass.
Variables (phRS : phant (RS)) (f g : RS) (cF : map phRS).

Definition class := let: Pack _ c as cF' := cF return class_of cF' in c.

Definition clone fM of phant_id g (apply cF) & phant_id fM class :=
  @Pack phRS f fM.

Definition pack (fM : mixin_of f) :=
  fun (bF : Additive.map phRS) fA & phant_id (Additive.class bF) fA
  Pack phRS (Class fA fM).

Canonical additive := Additive.Pack phRS class.

End ClassDef.

Module Exports.
Notation multiplicative f := (mixin_of f).
Notation rmorphism f := (class_of f).
Coercion base : rmorphism >-> Additive.axiom.
Coercion mixin : rmorphism >-> multiplicative.
Coercion apply : map >-> Funclass.
Notation RMorphism fM := (Pack (Phant _) fM).
Notation AddRMorphism fM := (pack fM id).
Notation "{ 'rmorphism' fRS }" := (map (Phant fRS))
  (at level 0, format "{ 'rmorphism' fRS }") : ring_scope.
Notation "[ 'rmorphism' 'of' f 'as' g ]" := (@clone _ _ _ f g _ _ idfun id)
  (at level 0, format "[ 'rmorphism' 'of' f 'as' g ]") : form_scope.
Notation "[ 'rmorphism' 'of' f ]" := (@clone _ _ _ f f _ _ id id)
  (at level 0, format "[ 'rmorphism' 'of' f ]") : form_scope.
Coercion additive : map >-> Additive.map.
Canonical additive.
End Exports.

End RMorphism.
Include RMorphism.Exports.

Section RmorphismTheory.

Section Properties.

Variables (R S : ringType) (k : unit) (f : {rmorphism RS}).

Lemma rmorph0 : f 0 = 0.
Lemma rmorphN : {morph f : x / - x}.
Lemma rmorphD : {morph f : x y / x + y}.
Lemma rmorphB : {morph f: x y / x - y}.
Lemma rmorphMn n : {morph f : x / x *+ n}.
Lemma rmorphMNn n : {morph f : x / x *- n}.
Lemma rmorph_sum I r (P : pred I) E :
  f (\sum_(i <- r | P i) E i) = \sum_(i <- r | P i) f (E i).
Lemma rmorphMsign n : {morph f : x / (- 1) ^+ n × x}.

Lemma rmorphismP : rmorphism f.
Lemma rmorphismMP : multiplicative f.
Lemma rmorph1 : f 1 = 1.
Lemma rmorphM : {morph f: x y / x × y}.

Lemma rmorph_prod I r (P : pred I) E :
  f (\prod_(i <- r | P i) E i) = \prod_(i <- r | P i) f (E i).

Lemma rmorphX n : {morph f: x / x ^+ n}.

Lemma rmorph_nat n : f n%:R = n%:R.
Lemma rmorphN1 : f (- 1) = (- 1).

Lemma rmorph_sign n : f ((- 1) ^+ n) = (- 1) ^+ n.

Lemma rmorph_char p : p \in [char R]p \in [char S].

Lemma rmorph_eq_nat x n : injective f(f x == n%:R) = (x == n%:R).

Lemma rmorph_eq1 x : injective f(f x == 1) = (x == 1).

Lemma can2_rmorphism f' : cancel f f'cancel f' frmorphism f'.

Lemma bij_rmorphism :
  bijective fexists2 f' : {rmorphism SR}, cancel f f' & cancel f' f.

Fact locked_is_multiplicative : multiplicative (locked_with k (f : RS)).
Canonical locked_rmorphism := AddRMorphism locked_is_multiplicative.

End Properties.

Section Projections.

Variables (R S T : ringType) (f : {rmorphism ST}) (g : {rmorphism RS}).

Fact idfun_is_multiplicative : multiplicative (@idfun R).
Canonical idfun_rmorphism := AddRMorphism idfun_is_multiplicative.

Fact comp_is_multiplicative : multiplicative (f \o g).
Canonical comp_rmorphism := AddRMorphism comp_is_multiplicative.

End Projections.

Section InAlgebra.

Variables (R : ringType) (A : lalgType R).

Fact in_alg_is_rmorphism : rmorphism (in_alg_loc A).
Canonical in_alg_additive := Additive in_alg_is_rmorphism.
Canonical in_alg_rmorphism := RMorphism in_alg_is_rmorphism.

Lemma in_algE a : in_alg_loc A a = a%:A.

End InAlgebra.

End RmorphismTheory.

Module Scale.

Section ScaleLaw.

Structure law (R : ringType) (V : zmodType) (s : RVV) := Law {
  op : RVV;
  _ : op = s;
  _ : op (-1) =1 -%R;
  _ : a, additive (op a)
}.

Definition mul_law R := Law (erefl *%R) (@mulN1r R) (@mulrBr R).
Definition scale_law R U := Law (erefl *:%R) (@scaleN1r R U) (@scalerBr R U).

Variables (R : ringType) (V : zmodType) (s : RVV) (s_law : law s).
Local Notation s_op := (op s_law).

Lemma opE : s_op = s.
Lemma N1op : s_op (-1) =1 -%R.
Fact opB a : additive (s_op a).
Definition op_additive a := Additive (opB a).

Variables (aR : ringType) (nu : {rmorphism aRR}).
Fact comp_opE : nu \; s_op = nu \; s.
Fact compN1op : (nu \; s_op) (-1) =1 -%R.
Definition comp_law : law (nu \; s) := Law comp_opE compN1op (fun aopB _).

End ScaleLaw.

End Scale.

Module Linear.

Section ClassDef.

Variables (R : ringType) (U : lmodType R) (V : zmodType) (s : RVV).
Implicit Type phUV : phant (UV).

Local Coercion Scale.op : Scale.law >-> Funclass.
Definition axiom (f : UV) (s_law : Scale.law s) of s = s_law :=
   a, {morph f : u v / a *: u + v >-> s a u + v}.
Definition mixin_of (f : UV) :=
   a, {morph f : v / a *: v >-> s a v}.

Record class_of f : Prop := Class {base : additive f; mixin : mixin_of f}.
Local Coercion base : class_of >-> additive.

Lemma class_of_axiom f s_law Ds : @axiom f s_law Dsclass_of f.

Structure map (phUV : phant (UV)) := Pack {apply; _ : class_of apply}.
Local Coercion apply : map >-> Funclass.

Variables (phUV : phant (UV)) (f g : UV) (cF : map phUV).
Definition class := let: Pack _ c as cF' := cF return class_of cF' in c.
Definition clone fL of phant_id g (apply cF) & phant_id fL class :=
  @Pack phUV f fL.

Definition pack (fZ : mixin_of f) :=
  fun (bF : Additive.map phUV) fA & phant_id (Additive.class bF) fA
  Pack phUV (Class fA fZ).

Canonical additive := Additive.Pack phUV class.

Support for right-to-left rewriting with the generic linearZ rule.
Notation mapUV := (map (Phant (UV))).
Definition map_class := mapUV.
Definition map_at (a : R) := mapUV.
Structure map_for a s_a := MapFor {map_for_map : mapUV; _ : s a = s_a}.
Definition unify_map_at a (f : map_at a) := MapFor f (erefl (s a)).
Structure wrapped := Wrap {unwrap : mapUV}.
Definition wrap (f : map_class) := Wrap f.

End ClassDef.

Module Exports.
Canonical Scale.mul_law.
Canonical Scale.scale_law.
Canonical Scale.comp_law.
Canonical Scale.op_additive.
Delimit Scope linear_ring_scope with linR.
Notation "a *: u" := (@Scale.op _ _ *:%R _ a u) : linear_ring_scope.
Notation "a * u" := (@Scale.op _ _ *%R _ a u) : linear_ring_scope.
Notation "a *:^ nu u" := (@Scale.op _ _ (nu \; *:%R) _ a u)
  (at level 40, nu at level 1, format "a *:^ nu u") : linear_ring_scope.
Notation "a *^ nu u" := (@Scale.op _ _ (nu \; *%R) _ a u)
  (at level 40, nu at level 1, format "a *^ nu u") : linear_ring_scope.
Notation scalable_for s f := (mixin_of s f).
Notation scalable f := (scalable_for *:%R f).
Notation linear_for s f := (axiom f (erefl s)).
Notation linear f := (linear_for *:%R f).
Notation scalar f := (linear_for *%R f).
Notation lmorphism_for s f := (class_of s f).
Notation lmorphism f := (lmorphism_for *:%R f).
Coercion class_of_axiom : axiom >-> lmorphism_for.
Coercion base : lmorphism_for >-> Additive.axiom.
Coercion mixin : lmorphism_for >-> scalable.
Coercion apply : map >-> Funclass.
Notation Linear fL := (Pack (Phant _) fL).
Notation AddLinear fZ := (pack fZ id).
Notation "{ 'linear' fUV | s }" := (map s (Phant fUV))
  (at level 0, format "{ 'linear' fUV | s }") : ring_scope.
Notation "{ 'linear' fUV }" := {linear fUV | *:%R}
  (at level 0, format "{ 'linear' fUV }") : ring_scope.
Notation "{ 'scalar' U }" := {linear U_ | *%R}
  (at level 0, format "{ 'scalar' U }") : ring_scope.
Notation "[ 'linear' 'of' f 'as' g ]" := (@clone _ _ _ _ _ f g _ _ idfun id)
  (at level 0, format "[ 'linear' 'of' f 'as' g ]") : form_scope.
Notation "[ 'linear' 'of' f ]" := (@clone _ _ _ _ _ f f _ _ id id)
  (at level 0, format "[ 'linear' 'of' f ]") : form_scope.
Coercion additive : map >-> Additive.map.
Canonical additive.
Support for right-to-left rewriting with the generic linearZ rule.
Coercion map_for_map : map_for >-> map.
Coercion unify_map_at : map_at >-> map_for.
Canonical unify_map_at.
Coercion unwrap : wrapped >-> map.
Coercion wrap : map_class >-> wrapped.
Canonical wrap.
End Exports.

End Linear.
Include Linear.Exports.

Section LinearTheory.

Variable R : ringType.

Section GenericProperties.

Variables (U : lmodType R) (V : zmodType) (s : RVV) (k : unit).
Variable f : {linear UV | s}.

Lemma linear0 : f 0 = 0.
Lemma linearN : {morph f : x / - x}.
Lemma linearD : {morph f : x y / x + y}.
Lemma linearB : {morph f : x y / x - y}.
Lemma linearMn n : {morph f : x / x *+ n}.
Lemma linearMNn n : {morph f : x / x *- n}.
Lemma linear_sum I r (P : pred I) E :
  f (\sum_(i <- r | P i) E i) = \sum_(i <- r | P i) f (E i).

Lemma linearZ_LR : scalable_for s f.
Lemma linearP a : {morph f : u v / a *: u + v >-> s a u + v}.

Fact locked_is_scalable : scalable_for s (locked_with k (f : UV)).
Canonical locked_linear := AddLinear locked_is_scalable.

End GenericProperties.

Section BidirectionalLinearZ.

Variables (U : lmodType R) (V : zmodType) (s : RVV).

The general form of the linearZ lemma uses some bespoke interfaces to allow right-to-left rewriting when a composite scaling operation such as conjC \; *%R has been expanded, say in a^* * f u. This redex is matched by using the Scale.law interface to recognize a "head" scaling operation h (here *%R), stow away its "scalar" c, then reconcile h c and s a, once s is known, that is, once the Linear.map structure for f has been found. In general, s and a need not be equal to h and c; indeed they need not have the same type! The unification is performed by the unify_map_at default instance for the Linear.map_for U s a h_c sub-interface of Linear.map; the h_c pattern uses the Scale.law structure to insure it is inferred when rewriting right-to-left. The wrap on the rhs allows rewriting f (a *: b *: u) into a *: b *: f u with rewrite !linearZ /= instead of rewrite linearZ /= linearZ /=. Without it, the first rewrite linearZ would produce (a *: apply (map_for_map (@check_map_at .. a f)) (b *: u)%R)%Rlin and matching the second rewrite LHS would bypass the unify_map_at default instance for b, reuse the one for a, and subsequently fail to match the b *: u argument. The extra wrap / unwrap ensures that this can't happen. In the RL direction, the wrap / unwrap will be inserted on the redex side as needed, without causing unnecessary delta-expansion: using an explicit identity function would have Coq normalize the redex to head normal, then reduce the identity to expose the map_for_map projection, and the expanded Linear.map structure would then be exposed in the result. Most of this machinery will be invisible to a casual user, because all the projections and default instances involved are declared as coercions.

Variables (S : ringType) (h : SVV) (h_law : Scale.law h).

Lemma linearZ c a (h_c := Scale.op h_law c) (f : Linear.map_for U s a h_c) u :
  f (a *: u) = h_c (Linear.wrap f u).

End BidirectionalLinearZ.

Section LmodProperties.

Variables (U V : lmodType R) (f : {linear UV}).

Lemma linearZZ : scalable f.
Lemma linearPZ : linear f.

Lemma can2_linear f' : cancel f f'cancel f' flinear f'.

Lemma bij_linear :
  bijective fexists2 f' : {linear VU}, cancel f f' & cancel f' f.

End LmodProperties.

Section ScalarProperties.

Variable (U : lmodType R) (f : {scalar U}).

Lemma scalarZ : scalable_for *%R f.
Lemma scalarP : scalar f.

End ScalarProperties.

Section LinearLmod.

Variables (W U : lmodType R) (V : zmodType) (s : RVV).
Variables (f : {linear UV | s}) (h : {linear WU}).

Lemma idfun_is_scalable : scalable (@idfun U).
Canonical idfun_linear := AddLinear idfun_is_scalable.

Lemma opp_is_scalable : scalable (-%R : UU).
Canonical opp_linear := AddLinear opp_is_scalable.

Lemma comp_is_scalable : scalable_for s (f \o h).
Canonical comp_linear := AddLinear comp_is_scalable.

Variables (s_law : Scale.law s) (g : {linear UV | Scale.op s_law}).
Let Ds : s =1 Scale.op s_law.

Lemma null_fun_is_scalable : scalable_for (Scale.op s_law) (\0 : UV).
Canonical null_fun_linear := AddLinear null_fun_is_scalable.

Lemma add_fun_is_scalable : scalable_for s (f \+ g).
Canonical add_fun_linear := AddLinear add_fun_is_scalable.

Lemma sub_fun_is_scalable : scalable_for s (f \- g).
Canonical sub_fun_linear := AddLinear sub_fun_is_scalable.

End LinearLmod.

Section LinearLalg.

Variables (A : lalgType R) (U : lmodType R).

Variables (a : A) (f : {linear UA}).

Fact mulr_fun_is_scalable : scalable (a \o× f).
Canonical mulr_fun_linear := AddLinear mulr_fun_is_scalable.

End LinearLalg.

End LinearTheory.

Module LRMorphism.

Section ClassDef.

Variables (R : ringType) (A : lalgType R) (B : ringType) (s : RBB).

Record class_of (f : AB) : Prop :=
  Class {base : rmorphism f; mixin : scalable_for s f}.
Local Coercion base : class_of >-> rmorphism.
Definition base2 f (fLM : class_of f) := Linear.Class fLM (mixin fLM).
Local Coercion base2 : class_of >-> lmorphism.

Structure map (phAB : phant (AB)) := Pack {apply; _ : class_of apply}.
Local Coercion apply : map >-> Funclass.

Variables (phAB : phant (AB)) (f : AB) (cF : map phAB).
Definition class := let: Pack _ c as cF' := cF return class_of cF' in c.

Definition clone :=
  fun (g : RMorphism.map phAB) fM & phant_id (RMorphism.class g) fM
  fun (h : Linear.map s phAB) fZ &
     phant_id (Linear.mixin (Linear.class h)) fZ
  Pack phAB (@Class f fM fZ).

Definition pack (fZ : scalable_for s f) :=
  fun (g : RMorphism.map phAB) fM & phant_id (RMorphism.class g) fM
  Pack phAB (Class fM fZ).

Canonical additive := Additive.Pack phAB class.
Canonical rmorphism := RMorphism.Pack phAB class.
Canonical linear := Linear.Pack phAB class.
Canonical join_rmorphism := @RMorphism.Pack _ _ phAB linear class.
Canonical join_linear := @Linear.Pack R A B s phAB rmorphism class.

End ClassDef.

Module Exports.
Notation lrmorphism_for s f := (class_of s f).
Notation lrmorphism f := (lrmorphism_for *:%R f).
Coercion base : lrmorphism_for >-> RMorphism.class_of.
Coercion base2 : lrmorphism_for >-> lmorphism_for.
Coercion apply : map >-> Funclass.
Notation LRMorphism f_lrM := (Pack (Phant _) (Class f_lrM f_lrM)).
Notation AddLRMorphism fZ := (pack fZ id).
Notation "{ 'lrmorphism' fAB | s }" := (map s (Phant fAB))
  (at level 0, format "{ 'lrmorphism' fAB | s }") : ring_scope.
Notation "{ 'lrmorphism' fAB }" := {lrmorphism fAB | *:%R}
  (at level 0, format "{ 'lrmorphism' fAB }") : ring_scope.
Notation "[ 'lrmorphism' 'of' f ]" := (@clone _ _ _ _ _ f _ _ id _ _ id)
  (at level 0, format "[ 'lrmorphism' 'of' f ]") : form_scope.
Coercion additive : map >-> Additive.map.
Canonical additive.
Coercion rmorphism : map >-> RMorphism.map.
Canonical rmorphism.
Coercion linear : map >-> Linear.map.
Canonical linear.
Canonical join_rmorphism.
Canonical join_linear.
End Exports.

End LRMorphism.
Include LRMorphism.Exports.

Section LRMorphismTheory.

Variables (R : ringType) (A B : lalgType R) (C : ringType) (s : RCC).
Variables (k : unit) (f : {lrmorphism AB}) (g : {lrmorphism BC | s}).

Definition idfun_lrmorphism := [lrmorphism of @idfun A].
Definition comp_lrmorphism := [lrmorphism of g \o f].
Definition locked_lrmorphism := [lrmorphism of locked_with k (f : AB)].

Lemma rmorph_alg a : f a%:A = a%:A.

Lemma lrmorphismP : lrmorphism f.

Lemma can2_lrmorphism f' : cancel f f'cancel f' flrmorphism f'.

Lemma bij_lrmorphism :
  bijective fexists2 f' : {lrmorphism BA}, cancel f f' & cancel f' f.

End LRMorphismTheory.

Module ComRing.

Definition RingMixin R one mul mulA mulC mul1x mul_addl :=
  let mulx1 := Monoid.mulC_id mulC mul1x in
  let mul_addr := Monoid.mulC_dist mulC mul_addl in
  @Ring.EtaMixin R one mul mulA mul1x mulx1 mul_addl mul_addr.

Section ClassDef.

Record class_of R :=
  Class {base : Ring.class_of R; mixin : commutative (Ring.mul base)}.
Local Coercion base : class_of >-> Ring.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variable (T : Type) (cT : type).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack mul0 (m0 : @commutative T T mul0) :=
  fun bT b & phant_id (Ring.class bT) b
  fun m & phant_id m0 mPack (@Class T b m) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @Ring.Pack cT xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Ring.class_of.
Implicit Arguments mixin [R].
Coercion mixin : class_of >-> commutative.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Coercion ringType : type >-> Ring.type.
Canonical ringType.
Notation comRingType := type.
Notation ComRingType T m := (@pack T _ m _ _ id _ id).
Notation ComRingMixin := RingMixin.
Notation "[ 'comRingType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
  (at level 0, format "[ 'comRingType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'comRingType' 'of' T ]" := (@clone T _ _ id)
  (at level 0, format "[ 'comRingType' 'of' T ]") : form_scope.
End Exports.

End ComRing.
Import ComRing.Exports.

Section ComRingTheory.

Variable R : comRingType.
Implicit Types x y : R.

Lemma mulrC : @commutative R R *%R.
Canonical mul_comoid := Monoid.ComLaw mulrC.
Lemma mulrCA : @left_commutative R R *%R.
Lemma mulrAC : @right_commutative R R *%R.
Lemma mulrACA : @interchange R *%R *%R.

Lemma exprMn n : {morph (fun xx ^+ n) : x y / x × y}.

Lemma prodrXl n I r (P : pred I) (F : IR) :
  \prod_(i <- r | P i) F i ^+ n = (\prod_(i <- r | P i) F i) ^+ n.

Lemma prodr_undup_exp_count (I : eqType) r (P : pred I) (F : IR) :
  \prod_(i <- undup r | P i) F i ^+ count_mem i r = \prod_(i <- r | P i) F i.

Lemma exprDn x y n :
  (x + y) ^+ n = \sum_(i < n.+1) (x ^+ (n - i) × y ^+ i) *+ 'C(n, i).

Lemma exprBn x y n :
  (x - y) ^+ n =
     \sum_(i < n.+1) ((-1) ^+ i × x ^+ (n - i) × y ^+ i) *+ 'C(n, i).

Lemma subrXX x y n :
  x ^+ n - y ^+ n = (x - y) × (\sum_(i < n) x ^+ (n.-1 - i) × y ^+ i).

Lemma sqrrD x y : (x + y) ^+ 2 = x ^+ 2 + x × y *+ 2 + y ^+ 2.

Lemma sqrrB x y : (x - y) ^+ 2 = x ^+ 2 - x × y *+ 2 + y ^+ 2.

Lemma subr_sqr x y : x ^+ 2 - y ^+ 2 = (x - y) × (x + y).

Lemma subr_sqrDB x y : (x + y) ^+ 2 - (x - y) ^+ 2 = x × y *+ 4.

Section FrobeniusAutomorphism.

Variables (p : nat) (charRp : p \in [char R]).

Lemma Frobenius_aut_is_rmorphism : rmorphism (Frobenius_aut charRp).

Canonical Frobenius_aut_additive := Additive Frobenius_aut_is_rmorphism.
Canonical Frobenius_aut_rmorphism := RMorphism Frobenius_aut_is_rmorphism.

End FrobeniusAutomorphism.

Lemma exprDn_char x y n : [char R].-nat n(x + y) ^+ n = x ^+ n + y ^+ n.

Lemma rmorph_comm (S : ringType) (f : {rmorphism RS}) x y :
  comm (f x) (f y).

Section ScaleLinear.

Variables (U V : lmodType R) (b : R) (f : {linear UV}).

Lemma scale_is_scalable : scalable ( *:%R b : VV).
Canonical scale_linear := AddLinear scale_is_scalable.

Lemma scale_fun_is_scalable : scalable (b \*: f).
Canonical scale_fun_linear := AddLinear scale_fun_is_scalable.

End ScaleLinear.

End ComRingTheory.

Module Algebra.

Section Mixin.

Variables (R : ringType) (A : lalgType R).

Definition axiom := k (x y : A), k *: (x × y) = x × (k *: y).

Lemma comm_axiom : phant Acommutative (@mul A) → axiom.

End Mixin.

Section ClassDef.

Variable R : ringType.

Record class_of (T : Type) : Type := Class {
  base : Lalgebra.class_of R T;
  mixin : axiom (Lalgebra.Pack _ base T)
}.
Local Coercion base : class_of >-> Lalgebra.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variable (phR : phant R) (T : Type) (cT : type phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack phR T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack b0 (ax0 : @axiom R b0) :=
  fun bT b & phant_id (@Lalgebra.class R phR bT) b
  fun ax & phant_id ax0 axPack phR (@Class T b ax) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @Ring.Pack cT xclass xT.
Definition lmodType := @Lmodule.Pack R phR cT xclass xT.
Definition lalgType := @Lalgebra.Pack R phR cT xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Lalgebra.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Coercion ringType : type >-> Ring.type.
Canonical ringType.
Coercion lmodType : type >-> Lmodule.type.
Canonical lmodType.
Coercion lalgType : type >-> Lalgebra.type.
Canonical lalgType.
Notation algType R := (type (Phant R)).
Notation AlgType R A ax := (@pack _ (Phant R) A _ ax _ _ id _ id).
Notation CommAlgType R A := (AlgType R A (comm_axiom (Phant A) (@mulrC _))).
Notation "[ 'algType' R 'of' T 'for' cT ]" := (@clone _ (Phant R) T cT _ idfun)
  (at level 0, format "[ 'algType' R 'of' T 'for' cT ]")
  : form_scope.
Notation "[ 'algType' R 'of' T ]" := (@clone _ (Phant R) T _ _ id)
  (at level 0, format "[ 'algType' R 'of' T ]") : form_scope.
End Exports.

End Algebra.
Import Algebra.Exports.

Section AlgebraTheory.

Variables (R : comRingType) (A : algType R).
Implicit Types (k : R) (x y : A).

Lemma scalerAr k x y : k *: (x × y) = x × (k *: y).

Lemma scalerCA k x y : k *: x × y = x × (k *: y).

Lemma mulr_algr a x : x × a%:A = a *: x.

Lemma exprZn k x n : (k *: x) ^+ n = k ^+ n *: x ^+ n.

Lemma scaler_prod I r (P : pred I) (F : IR) (G : IA) :
  \prod_(i <- r | P i) (F i *: G i) =
    \prod_(i <- r | P i) F i *: \prod_(i <- r | P i) G i.

Lemma scaler_prodl (I : finType) (S : pred I) (F : IA) k :
  \prod_(i in S) (k *: F i) = k ^+ #|S| *: \prod_(i in S) F i.

Lemma scaler_prodr (I : finType) (S : pred I) (F : IR) x :
  \prod_(i in S) (F i *: x) = \prod_(i in S) F i *: x ^+ #|S|.

Canonical regular_comRingType := [comRingType of R^o].
Canonical regular_algType := CommAlgType R R^o.

Variables (U : lmodType R) (a : A) (f : {linear UA}).

Lemma mull_fun_is_scalable : scalable (a \*o f).
Canonical mull_fun_linear := AddLinear mull_fun_is_scalable.

End AlgebraTheory.

Module UnitRing.

Record mixin_of (R : ringType) : Type := Mixin {
  unit : pred R;
  inv : RR;
  _ : {in unit, left_inverse 1 inv *%R};
  _ : {in unit, right_inverse 1 inv *%R};
  _ : x y, y × x = 1 x × y = 1 → unit x;
  _ : {in [predC unit], inv =1 id}
}.

Definition EtaMixin R unit inv mulVr mulrV unitP inv_out :=
  let _ := @Mixin R unit inv mulVr mulrV unitP inv_out in
  @Mixin (Ring.Pack (Ring.class R) R) unit inv mulVr mulrV unitP inv_out.

Section ClassDef.

Record class_of (R : Type) : Type := Class {
  base : Ring.class_of R;
  mixin : mixin_of (Ring.Pack base R)
}.
Local Coercion base : class_of >-> Ring.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (T : Type) (cT : type).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack T c T.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition pack b0 (m0 : mixin_of (@Ring.Pack T b0 T)) :=
  fun bT b & phant_id (Ring.class bT) b
  fun m & phant_id m0 mPack (@Class T b m) T.

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition zmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @Ring.Pack cT xclass xT.

End ClassDef.

Module Exports.
Coercion base : class_of >-> Ring.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion zmodType : type >-> Zmodule.type.
Canonical zmodType.
Coercion ringType : type >-> Ring.type.
Canonical ringType.
Notation unitRingType := type.
Notation UnitRingType T m := (@pack T _ m _ _ id _ id).
Notation UnitRingMixin := EtaMixin.
Notation "[ 'unitRingType' 'of' T 'for' cT ]" := (@clone T cT _ idfun)
  (at level 0, format "[ 'unitRingType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'unitRingType' 'of' T ]" := (@clone T _ _ id)
  (at level 0, format "[ 'unitRingType' 'of' T ]") : form_scope.
End Exports.

End UnitRing.
Import UnitRing.Exports.

Definition unit {R : unitRingType} :=
  [qualify a u : R | UnitRing.unit (UnitRing.class R) u].
Fact unit_key R : pred_key (@unit R).
Canonical unit_keyed R := KeyedQualifier (@unit_key R).
Definition inv {R : unitRingType} : RR := UnitRing.inv (UnitRing.class R).

Local Notation "x ^-1" := (inv x).
Local Notation "x / y" := (x × y^-1).
Local Notation "x ^- n" := ((x ^+ n)^-1).

Section UnitRingTheory.

Variable R : unitRingType.
Implicit Types x y : R.

Lemma divrr : {in unit, right_inverse 1 (@inv R) *%R}.
Definition mulrV := divrr.

Lemma mulVr : {in unit, left_inverse 1 (@inv R) *%R}.

Lemma invr_out x : x \isn't a unitx^-1 = x.

Lemma unitrP x : reflect ( y, y × x = 1 x × y = 1) (x \is a unit).

Lemma mulKr : {in unit, left_loop (@inv R) *%R}.

Lemma mulVKr : {in unit, rev_left_loop (@inv R) *%R}.

Lemma mulrK : {in unit, right_loop (@inv R) *%R}.

Lemma mulrVK : {in unit, rev_right_loop (@inv R) *%R}.
Definition divrK := mulrVK.

Lemma mulrI : {in @unit R, right_injective *%R}.

Lemma mulIr : {in @unit R, left_injective *%R}.

Due to noncommutativity, fractions are inverted.
Lemma telescope_prodr n m (f : natR) :
    ( k, n < k < mf k \is a unit) → n < m
  \prod_(n k < m) (f k / f k.+1) = f n / f m.

Lemma commrV x y : comm x ycomm x y^-1.

Lemma unitrE x : (x \is a unit) = (x / x == 1).

Lemma invrK : involutive (@inv R).

Lemma invr_inj : injective (@inv R).

Lemma unitrV x : (x^-1 \in unit) = (x \in unit).

Lemma unitr1 : 1 \in @unit R.

Lemma invr1 : 1^-1 = 1 :> R.

Lemma div1r x : 1 / x = x^-1.
Lemma divr1 x : x / 1 = x.

Lemma natr_div m d :
  d %| md%:R \is a @unit R(m %/ d)%:R = m%:R / d%:R :> R.

Lemma unitr0 : (0 \is a @unit R) = false.

Lemma invr0 : 0^-1 = 0 :> R.

Lemma unitrN1 : -1 \is a @unit R.

Lemma invrN1 : (-1)^-1 = -1 :> R.

Lemma invr_sign n : ((-1) ^- n) = (-1) ^+ n :> R.

Lemma unitrMl x y : y \is a unit(x × y \is a unit) = (x \is a unit).

Lemma unitrMr x y : x \is a unit(x × y \is a unit) = (y \is a unit).

Lemma invrM : {in unit &, x y, (x × y)^-1 = y^-1 × x^-1}.

Lemma unitrM_comm x y :
  comm x y(x × y \is a unit) = (x \is a unit) && (y \is a unit).

Lemma unitrX x n : x \is a unitx ^+ n \is a unit.

Lemma unitrX_pos x n : n > 0 → (x ^+ n \in unit) = (x \in unit).

Lemma exprVn x n : x^-1 ^+ n = x ^- n.

Lemma exprB m n x : n mx \is a unitx ^+ (m - n) = x ^+ m / x ^+ n.

Lemma invr_neq0 x : x != 0 → x^-1 != 0.

Lemma invr_eq0 x : (x^-1 == 0) = (x == 0).

Lemma invr_eq1 x : (x^-1 == 1) = (x == 1).

Lemma rev_unitrP (x y : R^c) : y × x = 1 x × y = 1 → x \is a unit.

Definition converse_unitRingMixin :=
  @UnitRing.Mixin _ ((unit : pred_class) : pred R^c) _
     mulrV mulVr rev_unitrP invr_out.
Canonical converse_unitRingType := UnitRingType R^c converse_unitRingMixin.
Canonical regular_unitRingType := [unitRingType of R^o].

Section ClosedPredicates.

Variables S : predPredType R.

Definition invr_closed := {in S, x, x^-1 \in S}.
Definition divr_2closed := {in S &, x y, x / y \in S}.
Definition divr_closed := 1 \in S divr_2closed.
Definition sdivr_closed := -1 \in S divr_2closed.
Definition divring_closed := [/\ 1 \in S, subr_2closed S & divr_2closed].

Lemma divr_closedV : divr_closedinvr_closed.

Lemma divr_closedM : divr_closedmulr_closed S.

Lemma sdivr_closed_div : sdivr_closeddivr_closed.

Lemma sdivr_closedM : sdivr_closedsmulr_closed S.

Lemma divring_closedBM : divring_closedsubring_closed S.

Lemma divring_closed_div : divring_closedsdivr_closed.

End