{- | **Primitive elements** (Mines–Richman–Ruitenburg, §VI.5).
* **Corollary VI.5.2** (`primitive-pair`): let `E` be a commutative ring containing a discrete field `k`, and let `a`
be a root of a monic polynomial of degree `n` and `b` a root of a separable monic polynomial of degree `m` over
`k`. If `k` has more than `N(N-1)/2` elements, where `N = n m`, then $\theta = a + c b$ is a primitive element of
$k[a, b]$ for some `c` in `k`. By Lemma VI.4.4, `a` and `b` are represented by commuting `N × N` matrices `A`, `B`
killed by the same polynomials, so `B` is separable; by Theorem VI.5.1 (`primitive-matrices`) `A` and `B` are
polynomials in `A + c B`, and the same polynomials express `a` and `b` in terms of `a + c b`.
* **Corollary VI.5.5** (`primitive-element`): if `k ⊆ E` are discrete fields, `a` is algebraic and `b` is separable
over `k`, then $k[a, b]$ has a primitive element. Replace `k` by the subfield generated by the coefficients of the
polynomials of `a` and `b`, which is finitely generated; by Theorem VI.5.4 (`finite-or-large`) it is either big
enough for Corollary VI.5.2, or finite, in which case Lemma VI.5.3 applies (`finite-primitive`).
-}
\import Algebra.Field
\import Algebra.Field.PrimitiveElement
\import Algebra.Field.FieldOfDefinition
\import Algebra.Field.FinitePrimitive
\import Algebra.Field.FinitelyGenerated
\import Algebra.Field.LargeField
\import Algebra.Field.SeparableElements
\import Algebra.Field.SubField
\import Algebra.Group
\import Algebra.Linear.Matrix
\import Algebra.Linear.Matrix.AlgebraicPair
\import Algebra.Linear.Matrix.Companion
\import Algebra.Linear.Matrix.PolyEval
\import Algebra.Linear.Matrix.PrimitiveElement
\import Algebra.Meta
\import Algebra.Module
\import Algebra.Monoid
\import Algebra.Pointed
\import Algebra.Ring
\import Algebra.Ring.Integral
\import Algebra.Ring.MPoly
\import Algebra.Ring.Poly
\import Algebra.Ring.Poly.Separable
\import Algebra.Ring.RingHom
\import Algebra.Ring.SubRing
\import Algebra.Semiring
\import Arith.Int
\import Combinatorics.Binom
\import Data.Array
\import Function
\import Logic
\import Logic.Meta
\import Paths
\import Set.Fin
{- | **Corollary VI.5.2.** If `a` is a root of a monic polynomial of degree `n`, `b` is a root of a separable monic
polynomial of degree `m`, and `k` has more than `N(N-1)/2` elements for `N = n m`, then `a + c b` is a primitive
element of $k[a, b]$ for some `c` in `k`. -}
\lemma primitive-pair {k : DiscreteField} {E : CRing} (f : RingHom k E) {a b : E} {n m : Nat} {p q : Poly k}
(pd : degree<= p n) (pc : polyCoef p n = 1) (pa : polyMapEval f p a = 0)
(qd : degree<= q m) (qc : polyCoef q m = 1) (qs : IsSeparable q) (qb : polyMapEval f q b = 0)
(big : HasMoreThan k (binom (n Nat.* m) 2))
: ∃ (c : k) (IsPrimitiveElement (adjoinHom f (a :: b :: nil)) (a + f c * b, mPolyImage-lin f a b c))
=> \have | pe : p = monicPoly (\new Array k n (\lam j => polyCoef p j)) => monicPoly-polyCoef pd pc
| qe : q = monicPoly (\new Array k m (\lam j => polyCoef q j)) => monicPoly-polyCoef qd qc
\in \let P => algebraicPair f (\new Array k n (\lam j => polyCoef p j)) (\new Array k m (\lam j => polyCoef q j))
(transport (\lam r => polyMapEval f r a = 0) pe pa) (transport (\lam r => polyMapEval f r b = 0) qe qb)
\in \case primitive-matrices P.comm (inP (_, transport IsSeparable qe qs, P.g-B)) big \with {
| inP (c, u, v, ue, ve) =>
\have rT => rep-+ P.rep-A (rep-*c P.rep-B c)
\in inP (c, primitive-generators f (a :: b :: nil) (mPolyImage-lin f a b c) \lam j => \case j \as j \return polyImage f (a + f c * b) ((a :: b :: nil) j) \with {
| 0 => inP (u, rep-unique P.unitVec P.unit-eval (transport (MatRepresents f P.family __ _) ue (rep-poly rT)) P.rep-A)
| 1 => inP (v, rep-unique P.unitVec P.unit-eval (transport (MatRepresents f P.family __ _) ve (rep-poly rT)) P.rep-B)
})
}
\where {
-- | A matrix represents at most one element, if some combination of the family is `1`.
\lemma rep-unique {k E : CRing} {emb : RingHom k E} {N : Nat} {e : Array E N} (v : Array k N) (ve : E.BigSum (\lam i => emb (v i) * e i) = 1)
{C : Matrix k N N} {x y : E} (rx : MatRepresents emb e C x) (ry : MatRepresents emb e C y) : x = y
=> \have | r0 : MatRepresents emb e (C + -1 *c C) (x + emb -1 * y) => rep-+ rx (rep-*c ry -1)
| c0 : C + -1 *c C = 0 => matrixExt (\lam i j => equation.cRing)
| z : x + emb -1 * y = 0 => inv (fieldOfDefinition.var-eval {k} {E} {emb} {x + emb -1 * y}) *> rep-transfer {k} {E} {emb} {N} {e} v ve {C + -1 *c C} r0 {padd 1 0} (pmap (matPolyEval (padd 1 0)) c0 *> matPolyEval_X)
\in AddGroup.fromZero (pmap (x +) (inv (pmap (`* y) (emb.func-negative *> pmap negative emb.func-ide) *> E.negative_ide-left)) *> z)
}
{- | **Corollary VI.5.5.** Let `k ⊆ E` be discrete fields, `a` algebraic and `b` separable over `k`. Then $k[a, b]$ has
a primitive element. -}
\lemma primitive-element {k E : DiscreteField} (f : RingHom k E) {a b : E} (aa : ∃ (p : Poly k) (p /= 0) (polyMapEval f p a = 0))
(bs : IsSeparableElement f b) : ∃ (t : ICRing (mPolyImage f (a :: b :: nil))) (IsPrimitiveElement (adjoinHom f (a :: b :: nil)) t)
=> \case aa, separableElement-monic bs \with {
| inP (p, p/=0, pa), inP (q, inP (m, qd, qc), qs, qb) => \case field-toMonic p/=0 \with {
| inP (n, pd, pc) =>
\let | p1 => finv (leadCoef p) *c p
| cs : Array k (suc n Nat.+ suc m) => (\new Array k (suc n) (\lam j => polyCoef p1 j)) ++' (\new Array k (suc m) (\lam j => polyCoef q j))
| S => FracImage (intEval cs)
| ki => SubField.embed S
\in \have | memP (j : Fin (suc n)) : S (polyCoef p1 j) => transport S (intEval.on-var cs (fin-inc {suc n} {suc m} j) *> ++'.++'_index-left {k} {\new Array k (suc n) (\lam j => polyCoef p1 j)} {suc m} {\new Array k (suc m) (\lam j => polyCoef q j)} {j})
(FracImage.contains_image {_} {k} {intEval cs} (mVar {Fin cs.len} {IntRing} (fin-inc {suc n} {suc m} j)))
| memQ (j : Fin (suc m)) : S (polyCoef q j) => transport S (intEval.on-var cs (fin-raise {suc m} {suc n} j) *> ++'.++'_index-right {k} {\new Array k (suc n) (\lam j => polyCoef p1 j)} {suc m} {\new Array k (suc m) (\lam j => polyCoef q j)} {j})
(FracImage.contains_image {_} {k} {intEval cs} (mVar {Fin cs.len} {IntRing} (fin-raise {suc m} {suc n} j)))
| P' => descend S p1 pd pc memP
| Q' => descend S q qd qc memQ
| pa' : polyMapEval (f RingHom.∘ ki) P'.1 a = 0 => comp-eval ki f P'.1 a *> pmap (polyMapEval f __ a) P'.2 *> (polyMapEval-toMonic f p/=0).2 pa
| qb' : polyMapEval (f RingHom.∘ ki) Q'.1 b = 0 => comp-eval ki f Q'.1 b *> pmap (polyMapEval f __ b) Q'.2 *> qb
| qs' : IsSeparable Q'.1 => IsSeparable_polyMap-conv ki (transportInv IsSeparable Q'.2 qs)
\in \case finite-or-large (generatedSubField-fg cs) (binom (n Nat.* m) 2) \with {
| byLeft fin => \case finite-primitive (f RingHom.∘ ki) fin P'.3 P'.4 pa' Q'.3 Q'.4 qb' \with {
| inP (t, pe) => inP (_, primitive-lift ki f pe)
}
| byRight big => \case primitive-pair (f RingHom.∘ ki) P'.3 P'.4 pa' Q'.3 Q'.4 qs' qb' big \with {
| inP (c, pe) => inP (_, primitive-lift ki f pe)
}
}
}
}
\where {
-- | A polynomial whose coefficients lie in a subfield comes from a polynomial over the subfield.
\func descend {k : DiscreteField} (S : SubField k) (p : Poly k) {n : Nat} (d : degree<= p n) (c : polyCoef p n = 1) (mem : \Pi (j : Fin (suc n)) -> S (polyCoef p j))
: \Sigma (p' : Poly (IDiscreteField S)) (polyMap (SubField.embed S) p' = p) (degree<= p' n) (polyCoef p' n = 1)
=> \let p' => Poly.fromArray (\new Array (IDiscreteField S) (suc n) (\lam j => SubField.elem S (mem j)))
\in \have | e : polyMap (SubField.embed S) p' = p => polyMap_fromArray {SubField.embed S} {\new Array (IDiscreteField S) (suc n) (\lam j => SubField.elem S (mem j))} *> inv (fromArray_polyCoef (degree<=_degree< d))
| inj : IsInj (SubField.embed S) => RingHom.field-isInj
\in (p', e, degree<_degree<= \lam {j} s => inj (inv polyCoef_polyMap *> pmap (polyCoef __ j) e *> degree<=_degree< d s *> inv (SubField.embed S).func-zro),
inj (inv polyCoef_polyMap *> pmap (polyCoef __ n) e *> c *> inv (SubField.embed S).func-ide))
\lemma comp-eval {k' k E : CRing} (i : RingHom k' k) (f : RingHom k E) (u : Poly k') (t : E)
: polyMapEval (f RingHom.∘ i) u t = polyMapEval f (polyMap i u) t
=> pmap (polyEval __ t) (inv (polyMap-comp i f))
-- | A primitive element over a subfield is a primitive element over the field.
\lemma primitive-lift {k' k E : CRing} (i : RingHom k' k) (f : RingHom k E) {l : Array E} {t : ICRing (mPolyImage (f RingHom.∘ i) l)}
(pe : IsPrimitiveElement (adjoinHom (f RingHom.∘ i) l) t) : IsPrimitiveElement (adjoinHom f l) (t.1, lift-adjoin i f t.2)
=> primitive-generators f l (lift-adjoin i f t.2) \lam j => \case primitive-generators-conv (f RingHom.∘ i) l pe j \with {
| inP (u, ue) => inP (polyMap i u, inv (comp-eval i f u t.1) *> ue)
}
-- | $k'[l] \subseteq k[l]$.
\lemma lift-adjoin {k' k E : CRing} (i : RingHom k' k) (f : RingHom k E) {l : Array E} {x : E} (xl : mPolyImage (f RingHom.∘ i) l x)
: mPolyImage f l x \elim xl
| inP (w, we) => inP (mPoly-map i w, pmap (mPolyEval l) (inv mPoly-map-comp) *> we)
}