{- | **Primitive elements** (Mines–Richman–Ruitenburg, §VI.5).

     An element `t` of an extension `f : K -> E` is a *primitive element* if $E = K[t]$ (`IsPrimitiveElement`).
     The subring $K[l]$ of `E` generated by an array `l` is `mPolyImage f l`, an extension of `K` via `adjoinHom f l`;
     an element `t` of $K[l]$ is a primitive element of $K[l]$ if and only if every `l j` lies in $K[t]$
     (`primitive-generators` and `primitive-generators-conv`).
-}

\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Pointed
\import Algebra.Ring
\import Algebra.Ring.Integral
\import Algebra.Ring.MPoly
\import Algebra.Ring.Poly
\import Algebra.Ring.RingHom
\import Algebra.Ring.SubRing
\import Algebra.Semiring
\import Logic
\import Paths
\import Paths.Meta

-- | {t} is a primitive element of the extension {f}, that is, every element of {E} is a polynomial in {t}.
\type IsPrimitiveElement {K E : CRing} (f : RingHom K E) (t : E) : \Prop
  => \Pi (x : E) -> polyImage f t x

-- | $K[l]$ as an extension of {K}.
\func adjoinHom {K E : CRing} (f : RingHom K E) (l : Array E) : RingHom K (ICRing (mPolyImage f l))
  => ICRing.corestrict f (mPolyImage-ext f l)

-- | If {t} lies in $K[l]$ and every element of {l} lies in $K[t]$, then {t} is a primitive element of $K[l]$.
\lemma primitive-generators {K E : CRing} (f : RingHom K E) (l : Array E) {t : E} (tl : mPolyImage f l t)
  (lt : \Pi (j : Fin l.len) -> polyImage f t (l j)) : IsPrimitiveElement (adjoinHom f l) (t, tl)
  => \lam x => \case adjoin-sub f l lt x.2 \with {
    | inP (u, ue) => inP (u, ext (sub-eval f l tl u *> ue))
  }
  \where {
    -- | Evaluation in $K[l]$ is evaluation in {E}.
    \lemma sub-eval {K E : CRing} (f : RingHom K E) (l : Array E) {t : E} (tl : mPolyImage f l t) (u : Poly K)
      : (polyMapEval (adjoinHom f l) u (t, tl)).1 = polyMapEval f u t
      => inv (polyEval_polyMap {ICRing.embed {E} {mPolyImage f l}} {polyMap (adjoinHom f l) u} {(t, tl)})
           *> pmap (polyEval __ t) (polyMap-comp (adjoinHom f l) (ICRing.embed {E} {mPolyImage f l}) *> polyMap-ext (\lam r => idp))

    -- | $K[l] \subseteq K[t]$ if every element of {l} lies in $K[t]$.
    \lemma adjoin-sub {K E : CRing} (f : RingHom K E) (l : Array E) {t : E} (lt : \Pi (j : Fin l.len) -> polyImage f t (l j))
      {x : E} (xl : mPolyImage f l x) : polyImage f t x \elim xl
      | inP (w, we) =>
        \let | g : RingHom K (ICRing (polyImage f t)) => ICRing.corestrict f (polyImage-ext f t)
             | H => mPolyMapEvalRingHom g (\lam j => (l j, lt j))
        \in transport (polyImage f t) (mPolyRingHom-unique (ICRing.embed RingHom.∘ H) (mPolyMapEvalRingHom f l)
              (\lam r => pmap __.1 (const-eval g (\lam j => (l j, lt j)) r) *> inv (const-eval f l r))
              (\lam j => pmap __.1 (mPolyMapEval-var g (\lam j => (l j, lt j)) j) *> inv (mPolyMapEval-var f l j)) w *> we) (H w).2

    -- | Evaluation of a constant multivariate polynomial.
    \lemma const-eval {J : \Set} {R S : CRing} (h : RingHom R S) (v : J -> S) (r : R) : mPolyMapEvalRingHom h v (mConst r) = h r
      => simplify
  }

-- | If {t} is a primitive element of $K[l]$, then every element of {l} lies in $K[t]$.
\lemma primitive-generators-conv {K E : CRing} (f : RingHom K E) (l : Array E) {t : ICRing (mPolyImage f l)}
  (pe : IsPrimitiveElement (adjoinHom f l) t) (j : Fin l.len) : polyImage f t.1 (l j)
  => \case pe (l j, mPolyImage-element f l j) \with {
    | inP (u, ue) => inP (u, inv (primitive-generators.sub-eval f l t.2 u) *> pmap __.1 ue)
  }

-- | `a + c b` lies in $K[a, b]$.
\lemma mPolyImage-lin {K E : CRing} (f : RingHom K E) (a b : E) (c : K) : mPolyImage f (a :: b :: nil) (a + f c * b)
  => \let | H => mPolyMapEvalRingHom f (a :: b :: nil)
          | X0 : MPoly (Fin 2) K => mVar (0 : Fin 2)
          | X1 : MPoly (Fin 2) K => mVar (1 : Fin 2)
          | Cc : MPoly (Fin 2) K => mConst c
     \in inP (X0 + {MPoly (Fin 2) K} Cc * {MPoly (Fin 2) K} X1, H.func-+ {X0} {Cc * {MPoly (Fin 2) K} X1}
                *> pmap2 (+) (mPolyMapEval-var f (a :: b :: nil) (0 : Fin 2)) (H.func-* {Cc} {X1} *> pmap2 (*) (primitive-generators.const-eval f (a :: b :: nil) c) (mPolyMapEval-var f (a :: b :: nil) (1 : Fin 2))))