Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Recognition context and the auxiliary languages Lu(C,K,l)L_u(C,K,l)Lu​(C,K,l)

Definition
MSKleene_AuxLang

by Cosme · Sep 8, 2026 · Mathlib 0df444a (Lean v4.33.1)

formal-languagesmany-sorted-algebratree-automatauniversal-algebra

The recognition context KleeneCtx and the auxiliary languages Lu(C,K,l)L_u(C,K,l)Lu​(C,K,l) of the proof of Proposition 4.10; the object of the Main Claim (Claim 4.13).

A KleeneCtx packages the data produced at the start of that proof: a state-count function n:S→Nn : S \to \mathbb{N}n:S→N so the finite recognizing algebra NNN has carrier s↦Fin(ns)s \mapsto \mathrm{Fin}(n_s)s↦Fin(ns​) with operations Nop; the recognizing map fgen on generators, extending to f♯:TΣ(X)→Nf^{\sharp} : \mathbf{T}_\Sigma(X) \to Nf♯:TΣ​(X)→N; and, derived from these, Z=X∪NZ = X \cup NZ=X∪N (extVars), the map h:Z→Nh : Z \to Nh:Z→N that is f♯f^{\sharp}f♯ on XXX and the identity on NNN, and h♯:TΣ(Z)→Nh^{\sharp} : \mathbf{T}_\Sigma(Z) \to Nh♯:TΣ​(Z)→N.

For u∈Su \in Su∈S, a set C⊆NC \subseteq NC⊆N of leaf-admissible states, a sortwise budget K≤NK \le NK≤N (where KtK_tKt​ is read as the set {0,…,Kt−1}\{0,\dots,K_t-1\}{0,…,Kt​−1} of state values still admissible at internal subterms), and a target state l∈Nul \in N_ul∈Nu​, auxLang u C K l is the set of terms P∈TΣ(Z)uP \in \mathrm{T}_\Sigma(Z)_uP∈TΣ​(Z)u​ such that (a) P∈TΣ(X∪C)P \in \mathrm{T}_\Sigma(X \cup C)P∈TΣ​(X∪C); (b) every proper non-minimal subterm QQQ of PPP with variables in X∪CX \cup CX∪C has h♯(Q)<Kh^{\sharp}(Q) < Kh♯(Q)<K at its sort; (c) h♯(P)=lh^{\sharp}(P) = lh♯(P)=l.

Term.varsIn is the predicate 'every variable of the term satisfies a given condition'.

Definition code
/-
The recognition context and the auxiliary languages `L_u(C,K,l)` of the proof
of Proposition 4.10 (`PRectoReg`); the object of the Main Claim (Claim 4.13).

A `KleeneCtx` packages the data produced at the start of that proof:
  * `n : S → ℕ`, so the finite recognizing algebra `N` has carrier
    `s ↦ Fin (n s)` and operations `Nop`;
  * `fgen`, the recognizing map on generators, extending to `f^♯ : T_Σ(X) → N`;
  * from these, `Z = X ∪ N` (`extVars`), the map `h : Z → N` with
    `h(x) = f^♯(x)` on `X` and `h = id` on `N`, and `h^♯ : T_Σ(Z) → N`.

For `u ∈ S`, `C ⊆ N` (states allowed at leaves), a sortwise budget
`K ≤ N` (`K t` = the set `{0,…,K t−1}` of state values still admissible at
internal subterms), and `l ∈ N_u`, `auxLang u C K l` is the set of terms
`P ∈ T_Σ(Z)_u` with: (a) `P ∈ T_Σ(X ∪ C)`; (b) every proper non-minimal
subterm `Q` of `P` (with variables in `X ∪ C`) has `h^♯(Q) < K`; (c) `h^♯(P) = l`.

The whole file works at sort universe `0`, matching the mission's theorem
statements (`S : Type`), because the recognizing algebra has carrier `Fin (n s)`.
-/
import Definitions.Def_MSKleene_Subterm
import Definitions.Def_MSKleene_RegAlgebra
import Definitions.Def_MSKleene_Regular

namespace MSKleene

variable {S : Type} {sig : Signature S} {X : SSet S}

-- `Term.varsIn pred P` — every variable occurring in `P` satisfies `pred`.
mutual
def Term.varsIn (pred : (t : S) → X t → Prop) : {s : S} → Term sig X s → Prop
  | _, .var x => pred _ x
  | _, .app _ ts => TermVec.varsAllIn pred ts
def TermVec.varsAllIn (pred : (t : S) → X t → Prop) :
    {w : List S} → TermVec sig X w → Prop
  | _, .nil => True
  | _, .cons t ts => Term.varsIn pred t ∧ TermVec.varsAllIn pred ts
end

/-- The data extracted at the start of the proof of Proposition 4.10: a finite
recognizing algebra `N` with carrier `s ↦ Fin (n s)`, and a recognizing map
`fgen` on the generators. -/
structure KleeneCtx (sig : Signature S) (X : SSet S) where
  /-- state counts: `N_s = Fin (n s)`. -/
  n : S → ℕ
  /-- operations of the finite recognizing algebra `N`. -/
  Nop : {w : List S} → {s : S} → sig w s → Args (fun s => Fin (n s)) w → Fin (n s)
  /-- the recognizing map on generators, `X → N`. -/
  fgen : SMap X (fun s => Fin (n s))

namespace KleeneCtx

variable (ctx : KleeneCtx sig X)

/-- The finite recognizing algebra `N`. -/
def NAlg : Algebra sig := ⟨fun s => Fin (ctx.n s), ctx.Nop⟩

/-- The set of variables `Z = X ∪ N` of the proof. -/
def Z : SSet S := extVars X (fun s => Fin (ctx.n s))

/-- The recognizing homomorphism `f^♯ : T_Σ(X) → N`. -/
def fHom : Hom (freeAlgebra sig X) ctx.NAlg := evalHom ctx.NAlg ctx.fgen

/-- The assignment `h : Z → N`: `h(x) = f^♯(x)` on `X`, `h = id` on `N`. -/
def hAssign : SMap ctx.Z (fun s => Fin (ctx.n s)) :=
  fun s z => Sum.elim (ctx.fgen s) id z

/-- The homomorphism `h^♯ : T_Σ(Z) → N`. -/
def hHom : Hom (freeAlgebra sig ctx.Z) ctx.NAlg := evalHom ctx.NAlg ctx.hAssign

/-- `h^♯` of a sorted term, as a natural number (its state value). -/
def hVal (Q : STerm sig ctx.Z) : ℕ := (ctx.hHom.toFun Q.1 Q.2).val

/-- `P` has variables only in `X ∪ C` (states from `C` allowed at leaves). -/
def inXC (C : (s : S) → Set (Fin (ctx.n s))) (t : S) (z : ctx.Z t) : Prop :=
  Sum.elim (fun _ => True) (fun m => m ∈ C t) z

/-- The auxiliary language `L_u(C,K,l)` (proof of Proposition 4.10). -/
def auxLang (u : S) (C : (s : S) → Set (Fin (ctx.n s))) (K : S → ℕ)
    (l : Fin (ctx.n u)) : Set (Term sig ctx.Z u) :=
  { P |
    Term.varsIn (ctx.inXC C) P ∧
    (∀ Q : STerm sig ctx.Z, SubtermLT Q ⟨u, P⟩ → ¬ Min Q →
      Term.varsIn (ctx.inXC C) Q.2 → ctx.hVal Q < K Q.1) ∧
    ctx.hHom.toFun u P = l }

end KleeneCtx

end MSKleene
Source
Gong, Ruiz Mora, Sanmartín Vich, Cosme Llópez, "A Kleene theorem for free many-sorted algebras", 2026
Read-back

What the Lean code literally says, in plain math · claude-sonnet-5

Term.varsIn and TermVec.varsAllIn (mutual)

These two mutually recursive predicates are parameterized by a fixed sort type SSS, a signature sigsigsig over SSS, an SSS-sorted family of variable sets XXX (so XsX_sXs​ is the set of variables of sort sss), and a predicate pred\mathrm{pred}pred that assigns to every sort t∈St \in St∈S and every variable x∈Xtx \in X_tx∈Xt​ a proposition pred t x\mathrm{pred}\,t\,xpredtx. Given an implicit sort sss and a term PPP of sort sss built over sigsigsig with variables drawn from XXX, the proposition varsIn pred P\mathrm{varsIn}\,\mathrm{pred}\,PvarsInpredP is defined by structural recursion: if PPP is a variable leaf var x\mathrm{var}\,xvarx (with x∈Xsx \in X_sx∈Xs​), then varsIn pred P\mathrm{varsIn}\,\mathrm{pred}\,PvarsInpredP is pred s x\mathrm{pred}\,s\,xpredsx; if PPP is an operator application app σ ts\mathrm{app}\,\sigma\,tsappσts (an operation symbol σ\sigmaσ applied to an argument vector tststs), then varsIn pred P\mathrm{varsIn}\,\mathrm{pred}\,PvarsInpredP is varsAllIn pred ts\mathrm{varsAllIn}\,\mathrm{pred}\,tsvarsAllInpredts. The companion varsAllIn pred\mathrm{varsAllIn}\,\mathrm{pred}varsAllInpred acts on a term-vector tststs of type www (a list of sorts): it is True\mathrm{True}True when tststs is the empty vector, and varsIn pred t∧varsAllIn pred ts′\mathrm{varsIn}\,\mathrm{pred}\,t \wedge \mathrm{varsAllIn}\,\mathrm{pred}\,ts'varsInpredt∧varsAllInpredts′ when tststs is a cons cell cons t ts′\mathrm{cons}\,t\,ts'constts′. Consequently varsIn pred P\mathrm{varsIn}\,\mathrm{pred}\,PvarsInpredP holds exactly when every variable occurrence appearing anywhere in the tree of PPP satisfies pred\mathrm{pred}pred at its sort; for a term with no variable leaves (all leaves being nullary operator applications) the predicate is vacuously True\mathrm{True}True.

KleeneCtx

KleeneCtx sig X\mathrm{KleeneCtx}\,sig\,XKleeneCtxsigX is a structure (record), parameterized by a signature sigsigsig over the sort type SSS and an SSS-sorted variable family XXX, that bundles three fields. The first field nnn is a function n:S→Nn : S \to \mathbb{N}n:S→N assigning a natural number to each sort. The second field Nop\mathrm{Nop}Nop is, for every list of sorts www, every sort sss, and every operation symbol σ∈sig w s\sigma \in sig\,w\,sσ∈sigws, a function that takes an argument tuple whose iii-th component lies in Fin(n si)\mathrm{Fin}(n\,s_i)Fin(nsi​) (the type Fin(k)={0,1,…,k−1}\mathrm{Fin}(k) = \{0,1,\dots,k-1\}Fin(k)={0,1,…,k−1}, empty when k=0k = 0k=0) — one component per sort listed in www — and returns an element of Fin(n s)\mathrm{Fin}(n\,s)Fin(ns). The third field fgen\mathrm{fgen}fgen is an SSS-indexed map that assigns to each sort sss and each variable x∈Xsx \in X_sx∈Xs​ an element of Fin(n s)\mathrm{Fin}(n\,s)Fin(ns). Nothing in the structure forbids n s=0n\,s = 0ns=0 for some sorts. Throughout the remaining declarations, ctxctxctx denotes an arbitrary term of this structure type.

KleeneCtx.NAlg

For a context ctxctxctx, NAlg\mathrm{NAlg}NAlg is the sigsigsig-algebra whose carrier at each sort sss is the finite type Fin(ctx.n s)\mathrm{Fin}(ctx.n\,s)Fin(ctx.ns) and whose interpretation of operation symbols is the field ctx.Nopctx.\mathrm{Nop}ctx.Nop. In other words it packages s↦Fin(ctx.n s)s \mapsto \mathrm{Fin}(ctx.n\,s)s↦Fin(ctx.ns) together with ctx.Nopctx.\mathrm{Nop}ctx.Nop into an Algebra sig\mathrm{Algebra}\,sigAlgebrasig.

KleeneCtx.Z

ZZZ is the SSS-sorted family defined by Zs=Xs⊕Fin(ctx.n s)Z_s = X_s \oplus \mathrm{Fin}(ctx.n\,s)Zs​=Xs​⊕Fin(ctx.ns), the disjoint-sum (tagged union) of the original variable set at sort sss with the finite type Fin(ctx.n s)\mathrm{Fin}(ctx.n\,s)Fin(ctx.ns). This uses extVars X E\mathrm{extVars}\,X\,EextVarsXE, whose value at sort sss is Xs⊕EsX_s \oplus E_sXs​⊕Es​. Thus an element of ZsZ_sZs​ is either a left-tagged original variable inl x\mathrm{inl}\,xinlx with x∈Xsx \in X_sx∈Xs​, or a right-tagged numeral inr m\mathrm{inr}\,minrm with m∈Fin(ctx.n s)m \in \mathrm{Fin}(ctx.n\,s)m∈Fin(ctx.ns).

KleeneCtx.fHom

fHom\mathrm{fHom}fHom is the sigsigsig-algebra homomorphism from the free sigsigsig-algebra on the variable family XXX (whose carrier at sort sss is the set of terms Term sig X s\mathrm{Term}\,sig\,X\,sTermsigXs) to the algebra NAlg\mathrm{NAlg}NAlg, obtained as the evaluation homomorphism induced by the assignment ctx.fgenctx.\mathrm{fgen}ctx.fgen. Its underlying map sends a term ttt of sort sss to Term.eval NAlg ctx.fgen t\mathrm{Term.eval}\,\mathrm{NAlg}\,ctx.\mathrm{fgen}\,tTerm.evalNAlgctx.fgent: each variable leaf var x\mathrm{var}\,xvarx is sent to ctx.fgen s x∈Fin(ctx.n s)ctx.\mathrm{fgen}\,s\,x \in \mathrm{Fin}(ctx.n\,s)ctx.fgensx∈Fin(ctx.ns), and each application app σ ts\mathrm{app}\,\sigma\,tsappσts is sent to ctx.Nop σctx.\mathrm{Nop}\,\sigmactx.Nopσ applied to the recursively evaluated arguments. It also carries the proof that this map commutes with all operations.

KleeneCtx.hAssign

hAssign\mathrm{hAssign}hAssign is the SSS-indexed map from ZZZ to the family s↦Fin(ctx.n s)s \mapsto \mathrm{Fin}(ctx.n\,s)s↦Fin(ctx.ns) defined case-wise on the sum: for a sort sss and an element z∈Zs=Xs⊕Fin(ctx.n s)z \in Z_s = X_s \oplus \mathrm{Fin}(ctx.n\,s)z∈Zs​=Xs​⊕Fin(ctx.ns), if z=inl xz = \mathrm{inl}\,xz=inlx it returns ctx.fgen s xctx.\mathrm{fgen}\,s\,xctx.fgensx, and if z=inr mz = \mathrm{inr}\,mz=inrm it returns mmm itself (the identity on the numeral component).

KleeneCtx.hHom

hHom\mathrm{hHom}hHom is the sigsigsig-algebra homomorphism from the free sigsigsig-algebra on the family ZZZ (carrier at sort sss equal to the terms Term sig Z s\mathrm{Term}\,sig\,Z\,sTermsigZs) to NAlg\mathrm{NAlg}NAlg, obtained as the evaluation homomorphism induced by hAssign\mathrm{hAssign}hAssign. Its underlying map sends a term QQQ of sort sss over the variable family ZZZ to its value in Fin(ctx.n s)\mathrm{Fin}(ctx.n\,s)Fin(ctx.ns), where a left-tagged leaf inl x\mathrm{inl}\,xinlx is evaluated via ctx.fgenctx.\mathrm{fgen}ctx.fgen, a right-tagged leaf inr m\mathrm{inr}\,minrm is evaluated to mmm, and applications are evaluated via ctx.Nopctx.\mathrm{Nop}ctx.Nop.

KleeneCtx.hVal

hVal\mathrm{hVal}hVal takes a sorted term QQQ over the family ZZZ — that is, an element of Σ s:S, Term sig Z s\Sigma\,s : S,\ \mathrm{Term}\,sig\,Z\,sΣs:S, TermsigZs, a dependent pair consisting of a sort Q1Q_1Q1​ and a term Q2Q_2Q2​ of sort Q1Q_1Q1​ over ZZZ — and returns the natural number (hHom(Q2)).val\big(\mathrm{hHom}(Q_2)\big).\mathrm{val}(hHom(Q2​)).val, i.e. the underlying N\mathbb{N}N of the element hHom(Q2)∈Fin(ctx.n Q1)\mathrm{hHom}(Q_2) \in \mathrm{Fin}(ctx.n\,Q_1)hHom(Q2​)∈Fin(ctx.nQ1​) (its representative in {0,1,…,ctx.n Q1−1}\{0,1,\dots,ctx.n\,Q_1 - 1\}{0,1,…,ctx.nQ1​−1}).

KleeneCtx.inXC

inXC\mathrm{inXC}inXC takes a family CCC assigning to each sort sss a subset Cs⊆Fin(ctx.n s)C_s \subseteq \mathrm{Fin}(ctx.n\,s)Cs​⊆Fin(ctx.ns), a sort ttt, and an element z∈Zt=Xt⊕Fin(ctx.n t)z \in Z_t = X_t \oplus \mathrm{Fin}(ctx.n\,t)z∈Zt​=Xt​⊕Fin(ctx.nt), and returns a proposition by case analysis on the sum: if z=inl xz = \mathrm{inl}\,xz=inlx (an original variable) the proposition is True\mathrm{True}True; if z=inr mz = \mathrm{inr}\,mz=inrm (a numeral) the proposition is m∈Ctm \in C_tm∈Ct​.

KleeneCtx.auxLang

Fix a context ctx:KleeneCtx sig Xctx : \mathrm{KleeneCtx}\,sig\,Xctx:KleeneCtxsigX (with SSS, sigsigsig, XXX implicit). Given a sort u∈Su \in Su∈S, a family CCC with Cs⊆Fin(ctx.n s)C_s \subseteq \mathrm{Fin}(ctx.n\,s)Cs​⊆Fin(ctx.ns) for every sort sss, a function K:S→NK : S \to \mathbb{N}K:S→N, and a target element l∈Fin(ctx.n u)l \in \mathrm{Fin}(ctx.n\,u)l∈Fin(ctx.nu), the term auxLang ctx u C K l\mathrm{auxLang}\,ctx\,u\,C\,K\,lauxLangctxuCKl is a subset of the terms of sort uuu over the signature sigsigsig with variables in the family ZZZ (where Zs=Xs⊕Fin(ctx.n s)Z_s = X_s \oplus \mathrm{Fin}(ctx.n\,s)Zs​=Xs​⊕Fin(ctx.ns)). It consists of exactly those terms PPP of sort uuu over ZZZ satisfying the conjunction of all three of the following conditions.

(1) Variable-membership condition. Term.varsIn (ctx.inXC C) P\mathrm{Term.varsIn}\,(ctx.\mathrm{inXC}\,C)\,PTerm.varsIn(ctx.inXCC)P holds: every variable leaf occurring in PPP satisfies inXC C\mathrm{inXC}\,CinXCC at its sort; equivalently, every numeral leaf inr m\mathrm{inr}\,minrm of some sort ttt appearing in PPP has m∈Ctm \in C_tm∈Ct​, while original-variable leaves inl x\mathrm{inl}\,xinlx impose no constraint.

(2) Strict-subterm value bound. For every sorted term QQQ over ZZZ (an element of Σ s:S, Term sig Z s\Sigma\,s : S,\ \mathrm{Term}\,sig\,Z\,sΣs:S, TermsigZs, with sort Q1Q_1Q1​ and term Q2Q_2Q2​) such that

SubtermLT Q ⟨u,P⟩and¬ Min QandTerm.varsIn (ctx.inXC C) Q2,\mathrm{SubtermLT}\,Q\,\langle u, P\rangle \quad\text{and}\quad \neg\,\mathrm{Min}\,Q \quad\text{and}\quad \mathrm{Term.varsIn}\,(ctx.\mathrm{inXC}\,C)\,Q_2,SubtermLTQ⟨u,P⟩and¬MinQandTerm.varsIn(ctx.inXCC)Q2​,

it must hold that

hVal Q  <  K Q1,\mathrm{hVal}\,Q \;<\; K\,Q_1,hValQ<KQ1​,

that is, the natural number (hHom(Q2)).val\big(\mathrm{hHom}(Q_2)\big).\mathrm{val}(hHom(Q2​)).val is strictly less than KKK evaluated at the sort Q1Q_1Q1​ of QQQ. Here:

  • SubtermLT Q ⟨u,P⟩\mathrm{SubtermLT}\,Q\,\langle u, P\rangleSubtermLTQ⟨u,P⟩ is the transitive closure (one or more steps) of the immediate-subterm relation ImmSub\mathrm{ImmSub}ImmSub applied to the pair QQQ and the sorted term ⟨u,P⟩\langle u, P\rangle⟨u,P⟩; ImmSub a b\mathrm{ImmSub}\,a\,bImmSubab holds when bbb's term component is an application app σ ts\mathrm{app}\,\sigma\,tsappσts for some symbol σ\sigmaσ and argument vector tststs, and aaa's term component occurs as one of the entries of tststs. So QQQ ranges over the proper subterms of PPP (paired with their sorts) reachable by at least one descent step.
  • ¬ Min Q\neg\,\mathrm{Min}\,Q¬MinQ is the negation of the statement "Min Q\mathrm{Min}\,QMinQ", where Min Q\mathrm{Min}\,QMinQ means that no sorted term aaa satisfies ImmSub a Q\mathrm{ImmSub}\,a\,QImmSubaQ; i.e. ¬ Min Q\neg\,\mathrm{Min}\,Q¬MinQ says it is not the case that QQQ has no immediate subterm, so Q2Q_2Q2​ is an application to a non-empty argument vector (it is not a variable leaf and not a nullary application).
  • Term.varsIn (ctx.inXC C) Q2\mathrm{Term.varsIn}\,(ctx.\mathrm{inXC}\,C)\,Q_2Term.varsIn(ctx.inXCC)Q2​ says every variable leaf of Q2Q_2Q2​ satisfies inXC C\mathrm{inXC}\,CinXCC, as in condition (1).

If no QQQ meets all three antecedents (for instance when PPP is itself a variable or a shallow term), condition (2) is vacuously satisfied. If for some qualifying QQQ one has K Q1=0K\,Q_1 = 0KQ1​=0, then hVal Q<0\mathrm{hVal}\,Q < 0hValQ<0 is impossible (values are natural numbers), so any PPP admitting such a QQQ is excluded.

(3) Evaluation target condition. hHom(P)=l\mathrm{hHom}(P) = lhHom(P)=l: evaluating PPP through the homomorphism hHom\mathrm{hHom}hHom into NAlg\mathrm{NAlg}NAlg (numeral leaves interpreted as themselves, original-variable leaves via ctx.fgenctx.\mathrm{fgen}ctx.fgen, operations via ctx.Nopctx.\mathrm{Nop}ctx.Nop) yields exactly the prescribed element lll of Fin(ctx.n u)\mathrm{Fin}(ctx.n\,u)Fin(ctx.nu).

Human review
  • Endorsed by Shuze Chen · Sep 9, 2026

  • Endorsed by Cosme · Sep 9, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me