{- | **Finitely generated discrete fields** (Mines–Richman–Ruitenburg, *A Course in Constructive Algebra*, §VI.5).
* {intEval}: evaluation `ℤ[X_0,…,X_{n-1}] → k` at an array `a`.
* {IsFinitelyGeneratedField}: every element of `k` is a quotient `p(a) / q(a)` of values of integer polynomials
at some finite array `a`, i.e. `k` is the fraction image ({FracImage}) of {intEval}.
* {generatedSubField-fg}: the subfield generated by finitely many elements of a discrete field is finitely generated.
* {finite-or-large} (**Theorem VI.5.4**): a finitely generated discrete field is either finite or has more than `N`
elements, for every `N`.
Proof of VI.5.4, following the book. Either `0, 1, …, N` are distinct in `k`, or `k` has characteristic `c <= N`
({finite-or-large.natCoef-inj}). In the second case ({finite-or-large.finiteChar}) the prime field is the image of
`ℤ`, i.e. `{0, 1, …, c-1}`. Let `S̄` be the (finite) set of values at `a` of polynomials with coefficients in the
prime field and degree `<= N` in each variable: these are the sums {finite-or-large.F} of `natCoef (u e) · a^e` over
the exponent vectors `e : Fin n -> Fin (N+1)`. Either `S̄` has more than `N` elements, or every `a_i /= 0` has
multiplicative order `<= N`, since `1, a_i, …, a_i^N ∈ S̄` ({finite-or-large.reduce}). Then every power of `a_i`
equals one with exponent `<= N`, so `S̄` contains the values of all integer polynomials
({finite-or-large.V-all}), and every element of `k` is a quotient of two elements of the finite set `S̄`.
-}
\import Algebra.Domain
\import Algebra.Field
\import Algebra.Field.SubField
\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Pointed
\import Algebra.Ring
\import Algebra.Ring.MPoly
\import Algebra.Ring.MPoly.Rename
\import Algebra.Ring.RingHom
\import Algebra.Ring.SubRing
\import Algebra.Semiring
\import Arith.Int
\import Arith.Nat
\import Data.Fin
\import Data.Or
\import Equiv
\import Function
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.Biordered
\import Order.LinearOrder
\import Order.PartialOrder
\import Order.StrictOrder
\import Paths
\import Paths.Meta
\import Set
\import Set.Fin
\import Set.Fin.Instances
\import Set.Fin.Pi
\import Set.Fin.Pigeonhole
\open Monoid
\open CMonoid
\open AbMonoid
\open Semiring
-- | Evaluation of integer polynomials in `a.len` variables at `a`.
\func intEval {k : CRing} (a : Array k) : RingHom (MPoly (Fin a.len) IntRing) k
=> mPolyMapEvalRingHom intMap a
\where {
\lemma on-const {k : CRing} (a : Array k) (r : Int) : intEval a (mConst r) = intMap r
=> simplify
\lemma on-var {k : CRing} (a : Array k) (i : Fin a.len) : intEval a (mVar i) = a i
=> mPolyMapEval-var intMap a i
}
{- | A discrete field `k` is *finitely generated* if there are finitely many elements `a` of `k` such that every
element of `k` is a quotient `p(a) / q(a)` with `p, q ∈ ℤ[X]`. -}
\type IsFinitelyGeneratedField (k : DiscreteField) : \Prop
=> ∃ (a : Array k) ∀ (x : k) (FracImage (intEval a) x)
-- | The subfield of a discrete field generated by finitely many elements is finitely generated.
\lemma generatedSubField-fg {k : DiscreteField} (a : Array k) : IsFinitelyGeneratedField (IDiscreteField (FracImage (intEval a)))
=> \let | S => FracImage (intEval a)
| a' : Array (IDiscreteField S) a.len => \lam i => SubField.elem S (transport S (intEval.on-var a i) (FracImage.contains_image (mVar i)))
| ev (p : MPoly (Fin a.len) IntRing) : (intEval a' p).1 = intEval a p
=> mPolyRingHom-unique (SubField.embed S RingHom.∘ intEval a') (intEval a)
(\lam r => pmap __.1 (intEval.on-const a' r) *> (SubField.embed S).func-intMap r *> inv (intEval.on-const a r))
(\lam i => pmap __.1 (intEval.on-var a' i) *> inv (intEval.on-var a i)) p
\in inP (a', \lam x => \case x.2 \with {
| inP (p, q, q/=0, x=pq) => inP (p, q, \lam z => q/=0 (inv (ev q) *> pmap __.1 z), ext (x=pq *> inv (pmap2 (\lam u v => u * finv v) (ev p) (ev q))))
})
{- | **MRR Theorem VI.5.4.** A finitely generated discrete field `k` is either finite or has more than `N` distinct
elements. -}
\lemma finite-or-large {k : DiscreteField} (fg : IsFinitelyGeneratedField k) (N : Nat)
: ∃ (l : Array k) ∀ (x : k) ∃ (i : Fin l.len) (l i = x) || ∃ (l : Array k (suc N)) (IsInj l)
=> \case fg \with {
| inP (a, fa) => \case FinSet.searchFin (\lam (j : Fin N) => natCoef (suc j) = {k} 0) (\lam j => decideEq _ _) \with {
| inr none => byRight (inP (\lam i => natCoef i, natCoef-inj none))
| inl (j0, c0, _) => finiteChar a fa (fin_< j0) c0
}
}
\where \private {
-- | If no `natCoef m` with `0 < m <= N` vanishes, then `0, 1, …, N` are distinct.
\lemma natCoef-inj {k : DiscreteField} {N : Nat} (none : \Pi (j : Fin N) -> Not (natCoef (suc j) = {k} 0))
: IsInj (\lam (i : Fin (suc N)) => natCoef {k} i)
=> \lam {i} {j} p => \case LinearOrder.trichotomy (i : Nat) j \with {
| less i<j => absurd (aux none i<j (fin_< j) p)
| equals e => nat_fin_= e
| greater j<i => absurd (aux none j<i (fin_< i) (inv p))
}
\where
\lemma aux {k : DiscreteField} {N : Nat} (none : \Pi (j : Fin N) -> Not (natCoef (suc j) = {k} 0)) {i j : Nat}
(i<j : i < j) (j<=N : j < suc N) (p : natCoef i = {k} natCoef j) : Empty
=> \have | d/=0 : j -' i /= 0 => /=-sym (NatSemiring.<_/= (<_-' i<j))
| d0 : natCoef (j -' i) = {k} 0 => AddGroup.cancel-left (natCoef i) $ inv Semiring.natCoef_+ *> pmap natCoef (<=_exists (<=-less i<j)) *> inv p *> inv zro-right
| d<=N : j -' i <= N => -'<=id <=∘ <_suc_<= j<=N
\in none (toFin (pred (j -' i)) (suc_<=_< (transport (<= N) (inv (suc_pred d/=0)) d<=N)))
(pmap (\lam x => natCoef (suc x)) toFin=id *> pmap natCoef (suc_pred d/=0) *> d0)
-- | The case of finite characteristic `c+1 <= N`.
\lemma finiteChar {k : DiscreteField} (a : Array k) (fa : \Pi (x : k) -> FracImage (intEval a) x) {N c : Nat}
(c<N : c < N) (c0 : natCoef (suc c) = {k} 0)
: ∃ (l : Array k) ∀ (x : k) ∃ (i : Fin l.len) (l i = x) || ∃ (l : Array k (suc N)) (IsInj l)
=> \case (FunFin (FinFin (suc N)) (Coefs a.len N c)).finEq \with {
| inP e => \case FinSet.searchFin-equiv e (\lam l => Not (∃ (s t : Fin (suc N)) (s /= t) (Fd a (l s) = Fd a (l t))))
(\lam l => \case pigeonhole-fin.search-pair (\lam s => Fd a (l s)) \with {
| yes r => no \lam f => f r
| no r => yes r
}) \with {
| inl (l, nl) => byRight $ inP (\lam s => Fd a (l s), \lam {s} {t} q => \case decideEq s t \with {
| yes r => r
| no r => absurd $ nl $ inP (s, t, r, q)
})
| inr H => byLeft $ listing a fa \lam p => V-Fd c0 $ V-all c0 (reduce a (zero<=_ <∘r c<N) c0 \lam l => \case pigeonhole-fin.search-pair (\lam s => Fd a (l s)) \with {
| yes r => r
| no r => absurd (H l r)
}) p
}
}
-- | Exponent vectors `e : Fin n -> Fin (N+1)`, i.e. monomials of degree `<= N` in each variable.
\func Exps (n N : Nat) : FinSet => FunFin (FinFin n) (FinFin (suc N))
-- | Coefficient functions on exponent vectors, with values in `Fin (c+1)` (the prime field of characteristic `c+1`).
\func Coefs (n N c : Nat) : FinSet => FunFin (Exps n N) (FinFin (suc c))
-- | The monomial `a^e = ∏_i a_i^(e_i)`.
\func mon {k : CRing} (a : Array k) (e : Fin a.len -> Nat) : k => k.BigProd (monArr a e)
{- | The factors of {mon}. Lemmas about `BigProd (monArr a e)` must be given this array explicitly: otherwise the
unifier solves the array argument of `BigProd` with `a` itself. -}
\func monArr {k : CRing} (a : Array k) (e : Fin a.len -> Nat) : Array k a.len => \new Array k a.len (\lam i => pow (a i) (e i))
\lemma mon-ext {k : CRing} {a : Array k} {e e' : Fin a.len -> Nat} (p : \Pi (i : Fin a.len) -> pow (a i) (e i) = pow (a i) (e' i)) : mon a e = mon a e'
=> BigProd-ext {k} {a.len} {monArr a e} {monArr a e'} p
-- | `a^e = a_i^m` if the exponent vector `e` is `m` at `i` and `0` elsewhere.
\lemma mon-at {k : CRing} {a : Array k} {B : DecSet} {z : B} {f : B -> Nat} (fz : f z = 0) (i : Fin a.len) (m : B)
: mon a (\lam j => f (ifEq j i m z)) = pow (a i) (f m)
=> BigProd-unique {k} {monArr a (\lam j => f (ifEq j i m z))} i (\lam j q => pmap (\lam x => pow (a j) (f x)) (ifEq-no (/=-sym q)) *> pmap (pow (a j)) fz)
*> pmap (\lam x => pow (a i) (f x)) ifEq-yes
-- | `∑_e natCoef (u e) · a^e` over the exponent vectors `e` of degree `<= N` in each variable.
\func F {k : CRing} (a : Array k) {N : Nat} (u : Exps a.len N -> Nat) : k
=> k.FinSum {Exps a.len N} (\lam e => natCoef (u e) * mon a (\lam i => e i))
-- | The finite set `S̄` is the image of {Fd}.
\func Fd {k : CRing} (a : Array k) {N c : Nat} (d : Coefs a.len N c) : k => F a (\lam e => d e)
-- | The values of {F}; in characteristic `c+1`, this is the image of {Fd} ({V-Fd}).
\func V {k : CRing} (a : Array k) (N : Nat) (x : k) : \Prop => ∃ (u : Exps a.len N -> Nat) (x = F a u)
\func ifEq {A : DecSet} {B : \Set} (x y : A) (b b' : B) : B
=> \case decideEq x y \with {
| yes _ => b
| no _ => b'
}
\lemma ifEq-yes {A : DecSet} {B : \Set} {x : A} {b b' : B} : ifEq x x b b' = b
=> unfold ifEq (rewrite (decideEq=_reduce idp) idp)
\lemma ifEq-no {A : DecSet} {B : \Set} {x y : A} {b b' : B} (q : x /= y) : ifEq x y b b' = b'
=> unfold ifEq (rewrite (decideEq/=_reduce q) idp)
\lemma mon-+ {k : CRing} {a : Array k} {e e' : Fin a.len -> Nat} : mon a (\lam i => e i + e' i) = mon a e * mon a e'
=> BigProd-ext {k} {a.len} {monArr a (\lam i => e i + e' i)} {\new Array k a.len (\lam i => pow (a i) (e i) * pow (a i) (e' i))} (\lam i => pow_+)
*> BigProd_* {k} {a.len} {monArr a e} {monArr a e'}
\lemma F-+ {k : CRing} {a : Array k} {N : Nat} {u u' : Exps a.len N -> Nat} : F a u + F a u' = F a (\lam e => u e + u' e)
=> inv FinSum_+ *> FinSum-ext \lam e => inv rdistr *> pmap (* _) (inv (Semiring.natCoef_+ {k} {u e} {u' e}))
\lemma F-zero {k : CRing} {a : Array k} {N : Nat} {u : Exps a.len N -> Nat} (p : \Pi (e : Exps a.len N) -> natCoef (u e) = {k} 0) : F a u = 0
=> FinSum_zro \lam e => pmap (* _) (p e) *> zro_*-left
\lemma natCoef-mod {k : Semiring} {c u : Nat} (c0 : natCoef (suc c) = {k} 0) : natCoef u = {k} natCoef (u Nat.mod suc c)
=> pmap natCoef (inv $ Nat.divModProp u (suc c)) *> Semiring.natCoef_+ *> pmap (+ _) (Semiring.natCoef_* *> pmap (* _) c0 *> zro_*-left) *> zro-left
\lemma V-Fd {k : CRing} {a : Array k} {N c : Nat} (c0 : natCoef (suc c) = {k} 0) {x : k} (vx : V a N x) : ∃ (d : Coefs a.len N c) (Fd a d = x) \elim vx
| inP (u,p) => inP (\lam e => u e Nat.mod suc c, inv $ p *> FinSum-ext \lam e => pmap (* _) (natCoef-mod c0))
\lemma V-0 {k : CRing} {a : Array k} {N : Nat} : V a N 0
=> inP (\lam _ => 0, inv $ F-zero \lam _ => natCoefZero)
\lemma V-+ {k : CRing} {a : Array k} {N : Nat} {x y : k} (vx : V a N x) (vy : V a N y) : V a N (x + y) \elim vx, vy
| inP (u,p), inP (u',q) => inP (_, pmap2 (+) p q *> F-+)
-- | `natCoef w · a^e ∈ S̄` for an exponent vector `e` of degree `<= N` in each variable.
\lemma V-small {k : CRing} {a : Array k} {N : Nat} (w : Nat) (e : Exps a.len N) : V a N (natCoef w * mon a (\lam i => e i))
=> inP (\lam e' => ifEq e' e w 0, inv $ FinSum-unique e (\lam e' q => pmap (\lam x => natCoef x * _) (ifEq-no q) *> pmap (* _) natCoefZero *> zro_*-left)
*> pmap (\lam x => natCoef x * _) ifEq-yes)
-- | Once every power of every `a_i` equals one with exponent `<= N`, `natCoef w · a^e ∈ S̄` for every exponent vector `e`.
\lemma V-term {k : CRing} {a : Array k} {N : Nat} (red : \Pi (i : Fin a.len) (m : Nat) -> ∃ (j : Fin (suc N)) (pow (a i) m = pow (a i) j))
(w : Nat) (e : Fin a.len -> Nat) : V a N (natCoef w * mon a e)
=> \case FinSet.choice {FinFin a.len} (\lam i => red i (e i)) \with {
| inP h => transportInv (\lam x => V a N (natCoef w * x)) (mon-ext \lam i => (h i).2) (V-small w (\lam i => (h i).1))
}
\lemma V-* {k : CRing} {a : Array k} {N : Nat} (red : \Pi (i : Fin a.len) (m : Nat) -> ∃ (j : Fin (suc N)) (pow (a i) m = pow (a i) j))
{x y : k} (vx : V a N x) (vy : V a N y) : V a N (x * y) \elim vx, vy
| inP (u,p), inP (u',q) => transportInv (V a N) (pmap2 (*) p q *> FinSum-distr) $ AbMonoid.FinSum-pred V-0 V-+ \lam (s : \Sigma (Exps a.len N) (Exps a.len N)) =>
transport (V a N) (pmap2 (*) Semiring.natCoef_* mon-+ *> equation.cRing) (V-term red (u s.1 Nat.* u' s.2) (\lam i => s.1 i Nat.+ s.2 i))
\lemma V-neg {k : CRing} {a : Array k} {N c : Nat} (c0 : natCoef (suc c) = {k} 0) {x : k} (vx : V a N x) : V a N (negative x) \elim vx
| inP (u,p) => inP (\lam e => c Nat.* u e,
\have q : x + F a (\lam e => c Nat.* u e) = 0 => pmap (+ _) p *> F-+ *> F-zero \lam e =>
Semiring.natCoef_+ *> pmap (_ +) Semiring.natCoef_* *> equation.cRing *> pmap (* _) (inv (natCoefSuc c) *> c0) *> zro_*-left
\in inv zro-right *> pmap (negative x +) (inv q) *> inv +-assoc *> pmap (+ _) negative-left *> zro-left)
\lemma V-intMap {k : CRing} {a : Array k} {N c : Nat} (c0 : natCoef (suc c) = {k} 0) (r : Int) : V a N (intMap r) \elim r
| pos m => V-natCoef m
| neg m => rewrite (Ring.intCoef_neg m) (V-neg c0 (V-natCoef m))
\where
\lemma V-natCoef {k : CRing} {a : Array k} {N : Nat} (m : Nat) : V a N (natCoef m)
=> transport (V a N) (pmap (_ *) (BigProd_ide {k} {monArr a (\lam _ => 0)} \lam _ => idp) *> ide-right) (V-small m (\lam _ => 0))
-- | `S̄` contains the values of all integer polynomials.
\lemma V-all {k : CRing} {a : Array k} {N c : Nat} (c0 : natCoef (suc c) = {k} 0)
(red : \Pi (i : Fin a.len) (m : Nat) -> ∃ (j : Fin (suc N)) (pow (a i) m = pow (a i) j))
(p : MPoly (Fin a.len) IntRing) : V a N (intEval a p)
=> mPoly-ind (\new SubRing {
| S => MPoly (Fin a.len) IntRing
| contains p => V a N (intEval a p)
| contains_zro => transportInv (V a N) (intEval a).func-zro V-0
| contains_+ vp vq => transportInv (V a N) (intEval a).func-+ (V-+ vp vq)
| contains_ide => transportInv (V a N) (intEval a).func-ide $ transport (V a N) (intMap {k}).func-ide (V-intMap c0 1)
| contains_* vp vq => transportInv (V a N) (intEval a).func-* (V-* red vp vq)
| contains_negative vp => transportInv (V a N) (intEval a).func-negative (V-neg c0 vp)
}) (\lam r => transportInv (V a N) (intEval.on-const a r) (V-intMap c0 r))
(\lam i => transportInv (V a N) (intEval.on-var a i) $ transport (V a N) (pmap (* _) natCoef1 *> ide-left *> mon-at {k} {a} {NatSemiring} {0} {\lam x => x} idp i 1 *> ide-left) (V-term red 1 (\lam j => ifEq j i 1 0))) p
\where {
\lemma natCoef1 {k : Semiring} : natCoef 1 = {k} 1
=> natCoefSuc 0 *> pmap (+ 1) natCoefZero *> zro-left
}
-- | If `x^s = x^t` with `s < t` and `x /= 0`, then `x^(t-s) = 1`.
\lemma period {k : DiscreteField} {x : k} (x/=0 : x /= 0) {s t : Nat} (s<t : s < t) (p : pow x s = pow x t) : pow x (t -' s) = 1
=> Domain.nonZero-cancel-left (pow/=0 x/=0) $ inv pow_+ *> pmap (pow x) (<=_exists (<=-less s<t)) *> inv p *> inv ide-right
\where
\lemma pow/=0 {k : DiscreteField} {x : k} (x/=0 : x /= 0) {s : Nat} : pow x s /= 0 \elim s
| 0 => \lam p => zro/=ide {k} (inv p)
| suc s => Domain.nonZero_* (pow/=0 x/=0) x/=0
-- | If `x^d = 1` with `0 < d <= N`, then every power of `x` equals one with exponent `<= N`.
\lemma bounded-pow {k : CRing} {x : k} {N d : Nat} (d>0 : 0 < d) (d<=N : d <= N) (p : pow x d = 1) (m : Nat)
: ∃ (j : Fin (suc N)) (pow x m = pow x j)
=> inP (toFin (m Nat.mod d) ((mod<right (/=-sym $ NatSemiring.<_/= d>0) <∘l d<=N) <∘ id<suc), pow_mod p *> pmap (pow x) (inv toFin=id))
{- | If `S̄` has at most `N` elements, then every power of every `a_i` equals one with exponent `<= N`: if `a_i /= 0`,
two of the `N+1` elements `1, a_i, …, a_i^N` of `S̄` coincide. -}
\lemma reduce {k : DiscreteField} (a : Array k) {N c : Nat} (N>0 : 0 < N) (c0 : natCoef (suc c) = {k} 0)
(H : \Pi (l : Fin (suc N) -> Coefs a.len N c) -> ∃ (s t : Fin (suc N)) (s /= t) (Fd a (l s) = Fd a (l t)))
(i : Fin a.len) (m : Nat) : ∃ (j : Fin (suc N)) (pow (a i) m = pow (a i) j)
=> \case \elim m, decideEq (a i) 0 \with {
| 0, yes ai=0 => inP (0, idp)
| suc m, yes ai=0 => inP (toFin 1 (NatOrder.suc<suc N>0), rewrite ai=0 $ Semiring.pow_0 suc/=0 *> inv (Semiring.pow_0 {k} {toFin 1 (NatOrder.suc<suc N>0)} \lam q => suc/=0 {0} (inv (toFin=id {1} {suc N} {NatOrder.suc<suc N>0}) *> q)))
| m, no ai/=0 => \case FinSet.choice {FinFin (suc N)} (\lam t => V-Fd c0 (V-small {k} {a} {N} 1 (\lam j => ifEq j i t 0))) \with {
| inP h =>
\have pw (t : Fin (suc N)) : Fd a (h t).1 = pow (a i) t => (h t).2 *> pmap (* _) V-all.natCoef1 *> ide-left
*> mon-at {k} {a} {FinFin (suc N)} {0} {\lam x => x} idp i t
\in \case H (\lam t => (h t).1) \with {
| inP (s, t, s/=t, q) => \case LinearOrder.trichotomy (s : Nat) t \with {
| less s<t => bounded-pow (<_-' s<t) (-'<=id <=∘ <_suc_<= (fin_< t)) (period ai/=0 s<t (inv (pw s) *> q *> pw t)) m
| equals e => absurd (s/=t (nat_fin_= e))
| greater t<s => bounded-pow (<_-' t<s) (-'<=id <=∘ <_suc_<= (fin_< s)) (period ai/=0 t<s (inv (pw t) *> inv q *> pw s)) m
}
}
}
}
-- | If every value of an integer polynomial at `a` lies in `S̄`, then `k` is finite.
\lemma listing {k : DiscreteField} (a : Array k) (fa : \Pi (x : k) -> FracImage (intEval a) x) {N c : Nat}
(vall : \Pi (p : MPoly (Fin a.len) IntRing) -> ∃ (d : Coefs a.len N c) (Fd a d = intEval a p))
: ∃ (l : Array k) ∀ (x : k) ∃ (i : Fin l.len) (l i = x)
=> \case (ProdFin (Coefs a.len N c) (Coefs a.len N c)).finEq \with {
| inP e => inP (\new Array k _ (\lam j => Fd a (e j).1 * finv (Fd a (e j).2)), \lam x => \case fa x \with {
| inP (p, q, _, x=pq) => \case vall p, vall q \with {
| inP (d1, s1), inP (d2, s2) => inP (e.ret (d1, d2), rewrite (e.f_ret (d1, d2)) $ pmap2 (\lam y z => y * finv z) s1 s2 *> inv x=pq)
}
})
}
}