Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

The many-sorted Kleene theorem

Proved
MSKleene.kleene_theorem

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

formal-languageskleene-theoremmany-sorted-algebrauniversal-algebra

The many-sorted Kleene theorem (Theorem 4.22).

Let SSS be a finite set of sorts, Σ\SigmaΣ a finite SSS-sorted signature, and XXX a finite SSS-sorted set of variables. Then for every sort s∈Ss \in Ss∈S,

Recs(TΣ(X))  =  Regs(TΣ(X)),\mathrm{Rec}_s(\mathbf{T}_\Sigma(X)) \;=\; \mathrm{Reg}_s(\mathbf{T}_\Sigma(X)),Recs​(TΣ​(X))=Regs​(TΣ​(X)),

that is, a language of sort sss in the free many-sorted algebra TΣ(X)\mathbf{T}_\Sigma(X)TΣ​(X) is sss-recognizable — the preimage of a subset of a finite Σ\SigmaΣ-algebra under a homomorphism — if and only if it is sss-regular — denoted by a regular expression built from the operations of Σ\SigmaΣ together with empty language, union, sortwise substitution, and sortwise iteration.

The inclusion Regs⊆Recs\mathrm{Reg}_s \subseteq \mathrm{Rec}_sRegs​⊆Recs​ follows from the closure properties of recognizability (Corollary 4.8); the converse Recs⊆Regs\mathrm{Rec}_s \subseteq \mathrm{Reg}_sRecs​⊆Regs​ (Proposition 4.10) is proved by a constructive state-elimination argument carrying a sortwise budget (Main Claim 4.13).

Preamble
import Definitions.Def_MSKleene_Recognizable
import Definitions.Def_MSKleene_Regular
Formal statement
namespace MSKleene

theorem kleene_theorem {S : Type} [Finite S] (sig : Signature S) (X : SSet S)
    (hsig : SigFinite sig) (hX : SFinite X) (s : S) :
    RecS (freeAlgebra sig X) s = RegS sig X s := by
  sorry

end MSKleene
Source
Gong, Ruiz Mora, Sanmartín Vich, Cosme Llópez, "A Kleene theorem for free many-sorted algebras", 2026, https://arxiv.org/abs/1808.08217 (predecessor CVCL20)
Read-back

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

Ambient data and hypotheses. Fix a type SSS of sorts carrying a Finite instance, so SSS has finitely many elements; it is moreover nonempty, since a sort s:Ss : Ss:S is supplied as the last argument. A signature over SSS is a family

sig:List S→S→Type,\mathrm{sig} : \mathrm{List}\,S \to S \to \mathrm{Type},sig:ListS→S→Type,

so that for an argument–sort list w=[w1,…,wk]w = [w_1,\dots,w_k]w=[w1​,…,wk​] and a result sort rrr, the type sig w r\mathrm{sig}\,w\,rsigwr is the collection of operation symbols of that profile. A sorted variable family is X:S→TypeX : S \to \mathrm{Type}X:S→Type, where X rX\,rXr is the type of variables of sort rrr. Two finiteness hypotheses are assumed:

  • hsig:SigFinite sig\mathtt{hsig} : \mathrm{SigFinite}\,\mathrm{sig}hsig:SigFinitesig, which unfolds to: the type ∑w:List S ∑r:S sig w r\sum_{w : \mathrm{List}\,S}\ \sum_{r : S}\ \mathrm{sig}\,w\,r∑w:ListS​ ∑r:S​ sigwr of all triples (argument–sort list, result sort, operation symbol) is a finite type. Since List S\mathrm{List}\,SListS is infinite, this forces sig w r\mathrm{sig}\,w\,rsigwr to be empty for all but finitely many pairs (w,r)(w,r)(w,r).
  • hX:SFinite X\mathtt{hX} : \mathrm{SFinite}\,XhX:SFiniteX, which unfolds to: the type ∑r:SX r\sum_{r : S} X\,r∑r:S​Xr of all (sort, variable-of-that-sort) pairs is a finite type.

Finally a specific sort s:Ss : Ss:S is fixed.

Terms and the free algebra. Term sig X\mathrm{Term}\,\mathrm{sig}\,XTermsigX is the SSS-indexed inductive family of well-sorted terms (defined mutually with tuples of terms): a term of sort rrr is either var(x)\mathrm{var}(x)var(x) for a variable x:X rx : X\,rx:Xr, or app(σ; t1,…,tk)\mathrm{app}(\sigma;\,t_1,\dots,t_k)app(σ;t1​,…,tk​) where σ:sig w r\sigma : \mathrm{sig}\,w\,rσ:sigwr with w=[w1,…,wk]w = [w_1,\dots,w_k]w=[w1​,…,wk​] and each ti:Term sig X wit_i : \mathrm{Term}\,\mathrm{sig}\,X\,w_iti​:TermsigXwi​. Write Term sig X r\mathrm{Term}\,\mathrm{sig}\,X\,rTermsigXr for the type of sort-rrr terms. The free (term) algebra F:=freeAlgebra sig X\mathcal F := \mathrm{freeAlgebra}\,\mathrm{sig}\,XF:=freeAlgebrasigX is the sig\mathrm{sig}sig-algebra whose carrier at sort rrr is Term sig X r\mathrm{Term}\,\mathrm{sig}\,X\,rTermsigXr and which interprets every symbol σ\sigmaσ by term formation: σF(t1,…,tk)=app(σ; t1,…,tk)\sigma^{\mathcal F}(t_1,\dots,t_k) = \mathrm{app}(\sigma;\,t_1,\dots,t_k)σF(t1​,…,tk​)=app(σ;t1​,…,tk​).

The objects being compared. Both sides of the asserted equation are subsets of the powerset P(Term sig X s)\mathcal P\big(\mathrm{Term}\,\mathrm{sig}\,X\,s\big)P(TermsigXs), i.e. sets of languages of sort-sss terms. The theorem asserts that, for this fixed sss, the two sets of languages coincide.

Left side — RecS(F,s)\mathrm{RecS}(\mathcal F, s)RecS(F,s). This is

{ L⊆Term sig X s ∣ sRecognizable(F,s,L) },\big\{\,L \subseteq \mathrm{Term}\,\mathrm{sig}\,X\,s \ \big|\ \mathrm{sRecognizable}(\mathcal F, s, L)\,\big\},{L⊆TermsigXs ​ sRecognizable(F,s,L)},

and sRecognizable(F,s,L)\mathrm{sRecognizable}(\mathcal F, s, L)sRecognizable(F,s,L) holds iff there exist:

  • a sig\mathrm{sig}sig-algebra B\mathcal BB: a carrier B∙:S→Type\mathcal B_\bullet : S \to \mathrm{Type}B∙​:S→Type together with, for every w,rw, rw,r and every σ:sig w r\sigma : \mathrm{sig}\,w\,rσ:sigwr, an operation σB:Bw1×⋯×Bwk→Br\sigma^{\mathcal B} : \mathcal B_{w_1}\times\cdots\times\mathcal B_{w_k} \to \mathcal B_rσB:Bw1​​×⋯×Bwk​​→Br​;
  • a proof that B\mathcal BB is finite, meaning ∑r:SBr\sum_{r : S}\mathcal B_r∑r:S​Br​ is a finite type;
  • a homomorphism f:F→Bf : \mathcal F \to \mathcal Bf:F→B, i.e. a family of maps fr:Term sig X r→Brf_r : \mathrm{Term}\,\mathrm{sig}\,X\,r \to \mathcal B_rfr​:TermsigXr→Br​ satisfying, for every σ:sig w r\sigma : \mathrm{sig}\,w\,rσ:sigwr and all arguments,
fr(app(σ; t1,…,tk))=σB(fw1(t1),…,fwk(tk));f_r\big(\mathrm{app}(\sigma;\,t_1,\dots,t_k)\big) = \sigma^{\mathcal B}\big(f_{w_1}(t_1),\dots,f_{w_k}(t_k)\big);fr​(app(σ;t1​,…,tk​))=σB(fw1​​(t1​),…,fwk​​(tk​));
  • a subset M⊆BsM \subseteq \mathcal B_sM⊆Bs​;

such that

fs−1(M)=L,i.e.L={ t:Term sig X s ∣ fs(t)∈M }.f_s^{-1}(M) = L, \qquad\text{i.e.}\qquad L = \{\,t : \mathrm{Term}\,\mathrm{sig}\,X\,s \ \mid\ f_s(t) \in M\,\}.fs−1​(M)=L,i.e.L={t:TermsigXs ∣ fs​(t)∈M}.

The condition on fff constrains only its behaviour on app\mathrm{app}app-nodes; its values on variable terms var(x)\mathrm{var}(x)var(x) are otherwise unconstrained.

Right side — RegS(sig,X,s)\mathrm{RegS}(\mathrm{sig}, X, s)RegS(sig,X,s). This is

{ L⊆Term sig X s ∣ sRegular(sig,X,s,L) },\big\{\,L \subseteq \mathrm{Term}\,\mathrm{sig}\,X\,s \ \big|\ \mathrm{sRegular}(\mathrm{sig}, X, s, L)\,\big\},{L⊆TermsigXs ​ sRegular(sig,X,s,L)},

and sRegular(sig,X,s,L)\mathrm{sRegular}(\mathrm{sig}, X, s, L)sRegular(sig,X,s,L) holds iff there exist:

  • a sorted family of extra variables E:S→TypeE : S \to \mathrm{Type}E:S→Type; put Z:=X⊕EZ := X \oplus EZ:=X⊕E, the sortwise disjoint sum, Z r=X r⊔E rZ\,r = X\,r \sqcup E\,rZr=Xr⊔Er;
  • a proof of SFinite(Z)\mathrm{SFinite}(Z)SFinite(Z), i.e. ∑r:S(X r⊔E r)\sum_{r : S}\big(X\,r \sqcup E\,r\big)∑r:S​(Xr⊔Er) is a finite type;
  • a regular expression R:RegExpr sig Z sR : \mathrm{RegExpr}\,\mathrm{sig}\,Z\,sR:RegExprsigZs, which is by definition an ordinary term R:Term (regSig sig Z) Z sR : \mathrm{Term}\,(\mathrm{regSig}\,\mathrm{sig}\,Z)\,Z\,sR:Term(regSigsigZ)Zs of sort sss over the variable family ZZZ and the regular signature regSig sig Z=RegSym sig Z\mathrm{regSig}\,\mathrm{sig}\,Z = \mathrm{RegSym}\,\mathrm{sig}\,ZregSigsigZ=RegSymsigZ, whose operation symbols of profile (w,r)(w, r)(w,r) are exactly:
    • base(σ)\mathrm{base}(\sigma)base(σ) for each σ:sig w r\sigma : \mathrm{sig}\,w\,rσ:sigwr (same profile (w,r)(w,r)(w,r)),
    • empty(r)\mathrm{empty}(r)empty(r), of profile ([ ], r)([\,],\,r)([],r) — a nullary symbol, one for every sort rrr,
    • iter(r,z)\mathrm{iter}(r, z)iter(r,z), of profile ([r], r)([r],\,r)([r],r), parameterised by a chosen variable z:Z rz : Z\,rz:Zr,
    • plus(r)\mathrm{plus}(r)plus(r), of profile ([r,r], r)([r, r],\,r)([r,r],r),
    • subst(t,r,z)\mathrm{subst}(t, r, z)subst(t,r,z), of profile ([t,r], r)([t, r],\,r)([t,r],r), parameterised by a chosen variable z:Z tz : Z\,tz:Zt;

such that

interpExpr sig Z s (R)  =  { relabel(ι)(t) ∣ t∈L },\mathrm{interpExpr}\,\mathrm{sig}\,Z\,s\,(R) \;=\; \big\{\,\mathrm{relabel}(\iota)(t) \ \big|\ t \in L\,\big\},interpExprsigZs(R)={relabel(ι)(t) ​ t∈L},

where ι:=extIncl X E\iota := \mathrm{extIncl}\,X\,Eι:=extInclXE is the sortwise left injection x↦inl(x):X r→Z rx \mapsto \mathrm{inl}(x) : X\,r \to Z\,rx↦inl(x):Xr→Zr, and relabel(ι)\mathrm{relabel}(\iota)relabel(ι) renames every variable occurring in a term along ι\iotaι while leaving the operation structure unchanged. Both sides of this equality are subsets of Term sig Z s\mathrm{Term}\,\mathrm{sig}\,Z\,sTermsigZs (terms over the extended variable family ZZZ); LLL enters only through its relabelled image.

Interpretation of regular expressions. interpExpr sig Z s (R)\mathrm{interpExpr}\,\mathrm{sig}\,Z\,s\,(R)interpExprsigZs(R) is (interp sig Z)s(R)(\mathrm{interp}\,\mathrm{sig}\,Z)_s(R)(interpsigZ)s​(R), the value at RRR of the evaluation homomorphism from the free algebra over regSig sig Z\mathrm{regSig}\,\mathrm{sig}\,ZregSigsigZ with variables ZZZ into the algebra regPowerAlgebra sig Z\mathrm{regPowerAlgebra}\,\mathrm{sig}\,ZregPowerAlgebrasigZ, whose carrier at sort rrr is P(Term sig Z r)\mathcal P\big(\mathrm{Term}\,\mathrm{sig}\,Z\,r\big)P(TermsigZr) and which sends each variable z:Z rz : Z\,rz:Zr to the singleton language {var(z)}\{\mathrm{var}(z)\}{var(z)}. Unfolding the recursion, interpExpr(R)⊆Term sig Z s\mathrm{interpExpr}(R) \subseteq \mathrm{Term}\,\mathrm{sig}\,Z\,sinterpExpr(R)⊆TermsigZs is:

  • R=var(z)R = \mathrm{var}(z)R=var(z) with z:Z sz : Z\,sz:Zs: value {var(z)}\{\mathrm{var}(z)\}{var(z)};
  • R=base(σ)(R1,…,Rk)R = \mathrm{base}(\sigma)(R_1,\dots,R_k)R=base(σ)(R1​,…,Rk​) with σ:sig w s\sigma : \mathrm{sig}\,w\,sσ:sigws: value { app(σ; u1,…,uk) ∣ ui∈interpExpr(Ri) for each i }\big\{\,\mathrm{app}(\sigma;\,u_1,\dots,u_k) \ \mid\ u_i \in \mathrm{interpExpr}(R_i)\text{ for each }i\,\big\}{app(σ;u1​,…,uk​) ∣ ui​∈interpExpr(Ri​) for each i};
  • R=empty(s)R = \mathrm{empty}(s)R=empty(s): value ∅\varnothing∅;
  • R=plus(s)(R1,R2)R = \mathrm{plus}(s)(R_1, R_2)R=plus(s)(R1​,R2​): value interpExpr(R1)∪interpExpr(R2)\mathrm{interpExpr}(R_1)\cup\mathrm{interpExpr}(R_2)interpExpr(R1​)∪interpExpr(R2​);
  • R=iter(s,z)(R1)R = \mathrm{iter}(s, z)(R_1)R=iter(s,z)(R1​) with z:Z sz : Z\,sz:Zs: value iterate(z, interpExpr(R1))\mathrm{iterate}\big(z,\ \mathrm{interpExpr}(R_1)\big)iterate(z, interpExpr(R1​));
  • R=subst(t,s,z)(R1,R2)R = \mathrm{subst}(t, s, z)(R_1, R_2)R=subst(t,s,z)(R1​,R2​) with z:Z tz : Z\,tz:Zt, R1R_1R1​ of sort ttt, R2R_2R2​ of sort sss: value substP(z, interpExpr(R1), s, interpExpr(R2))\mathrm{substP}\big(z,\ \mathrm{interpExpr}(R_1),\ s,\ \mathrm{interpExpr}(R_2)\big)substP(z, interpExpr(R1​), s, interpExpr(R2​)).

The substitution operation. For z:Z tz : Z\,tz:Zt, a language Λ⊆Term sig Z t\Lambda \subseteq \mathrm{Term}\,\mathrm{sig}\,Z\,tΛ⊆TermsigZt, a sort rrr, and a language K⊆Term sig Z rK \subseteq \mathrm{Term}\,\mathrm{sig}\,Z\,rK⊆TermsigZr,

substP(z,Λ,r,K)  =  ⋃P∈K Θz,Λ(P),\mathrm{substP}(z, \Lambda, r, K) \;=\; \bigcup_{P \in K}\ \Theta_{z,\Lambda}(P),substP(z,Λ,r,K)=P∈K⋃​ Θz,Λ​(P),

where Θz,Λ(P)⊆Term sig Z r\Theta_{z,\Lambda}(P) \subseteq \mathrm{Term}\,\mathrm{sig}\,Z\,rΘz,Λ​(P)⊆TermsigZr is the evaluation of the term PPP in the powerset algebra of the term algebra under the variable assignment that sends the parameter variable zzz to Λ\LambdaΛ and every other variable yyy to {var(y)}\{\mathrm{var}(y)\}{var(y)}. Concretely, Θz,Λ(P)\Theta_{z,\Lambda}(P)Θz,Λ​(P) is the set of all terms obtained from PPP by replacing each occurrence of zzz independently by some term of Λ\LambdaΛ and leaving all other variables fixed; thus Θz,Λ(var(z))=Λ\Theta_{z,\Lambda}(\mathrm{var}(z)) = \LambdaΘz,Λ​(var(z))=Λ, Θz,Λ(var(y))={var(y)}\Theta_{z,\Lambda}(\mathrm{var}(y)) = \{\mathrm{var}(y)\}Θz,Λ​(var(y))={var(y)} for y≠zy \ne zy=z, and Θz,Λ\Theta_{z,\Lambda}Θz,Λ​ distributes elementwise through app\mathrm{app}app.

The iteration operation. For z:Z sz : Z\,sz:Zs and Λ⊆Term sig Z s\Lambda \subseteq \mathrm{Term}\,\mathrm{sig}\,Z\,sΛ⊆TermsigZs, define stages over i∈Ni \in \mathbb Ni∈N by

iterStage(z,Λ,0)={var(z)},iterStage(z,Λ,i+1)=iterStage(z,Λ,i) ∪ substP(z, iterStage(z,Λ,i), s, Λ),\mathrm{iterStage}(z,\Lambda,0) = \{\mathrm{var}(z)\},\qquad \mathrm{iterStage}(z,\Lambda,i{+}1) = \mathrm{iterStage}(z,\Lambda,i)\ \cup\ \mathrm{substP}\big(z,\ \mathrm{iterStage}(z,\Lambda,i),\ s,\ \Lambda\big),iterStage(z,Λ,0)={var(z)},iterStage(z,Λ,i+1)=iterStage(z,Λ,i) ∪ substP(z, iterStage(z,Λ,i), s, Λ),

and set iterate(z,Λ)=⋃i∈NiterStage(z,Λ,i)\mathrm{iterate}(z,\Lambda) = \bigcup_{i \in \mathbb N}\mathrm{iterStage}(z,\Lambda,i)iterate(z,Λ)=⋃i∈N​iterStage(z,Λ,i). In words: start from {var(z)}\{\mathrm{var}(z)\}{var(z)}; at each stage, for every term P∈ΛP \in \LambdaP∈Λ, substitute the terms produced so far into the zzz-occurrences of PPP, and accumulate.

The full assertion. For the fixed sort sss, the theorem states equality of the following two subsets of P(Term sig X s)\mathcal P\big(\mathrm{Term}\,\mathrm{sig}\,X\,s\big)P(TermsigXs): a language LLL of sort-sss terms belongs to the left side iff there exist a sig\mathrm{sig}sig-algebra B\mathcal BB with finite total carrier, a homomorphism f:F→Bf : \mathcal F \to \mathcal Bf:F→B, and a subset M⊆BsM \subseteq \mathcal B_sM⊆Bs​ with L=fs−1(M)L = f_s^{-1}(M)L=fs−1​(M); and LLL belongs to the right side iff there exist an extra-variable family EEE making X⊕EX \oplus EX⊕E have finite total variable type, together with a regular expression RRR over regSig sig (X⊕E)\mathrm{regSig}\,\mathrm{sig}\,(X\oplus E)regSigsig(X⊕E) of sort sss whose interpreted language equals the ι\iotaι-relabelled image {relabel(ι)(t):t∈L}\{\mathrm{relabel}(\iota)(t) : t \in L\}{relabel(ι)(t):t∈L}. The equation asserts that membership in the left side holds precisely when membership in the right side does, with no further relationship imposed between the data (E,R)(E, R)(E,R) and the data (B,f,M)(\mathcal B, f, M)(B,f,M).

Degenerate and edge cases.

  • SSS is finite and, because s:Ss : Ss:S is given, nonempty.
  • hX\mathtt{hX}hX allows ∑rX r\sum_r X\,r∑r​Xr to be empty, and hsig\mathtt{hsig}hsig allows sig\mathrm{sig}sig to have no operation symbols at all. If in addition no term of sort sss exists, then Term sig X s\mathrm{Term}\,\mathrm{sig}\,X\,sTermsigXs is empty, P(Term sig X s)={∅}\mathcal P\big(\mathrm{Term}\,\mathrm{sig}\,X\,s\big) = \{\varnothing\}P(TermsigXs)={∅}, and the claim reduces to "∅\varnothing∅ lies on the left side iff it lies on the right side".
  • In sRegular\mathrm{sRegular}sRegular the family EEE is unconstrained except that SFinite(X⊕E)\mathrm{SFinite}(X \oplus E)SFinite(X⊕E) must hold; given hX\mathtt{hX}hX this amounts to ∑rE r\sum_r E\,r∑r​Er being finite. Every E rE\,rEr may be empty, in which case ZZZ is XXX up to the left injection ι\iotaι.
  • The symbols empty\mathrm{empty}empty, plus\mathrm{plus}plus, iter\mathrm{iter}iter, subst\mathrm{subst}subst are present in regSig sig Z\mathrm{regSig}\,\mathrm{sig}\,ZregSigsigZ for every sort (respectively sort pair), independently of sig\mathrm{sig}sig. plus\mathrm{plus}plus only ever combines two languages whose common sort equals the result sort.
  • RRR may be a bare variable var(z)\mathrm{var}(z)var(z) with z:Z sz : Z\,sz:Zs, whose interpretation is the singleton {var(z)}\{\mathrm{var}(z)\}{var(z)}.
  • iterate(z,Λ)\mathrm{iterate}(z,\Lambda)iterate(z,Λ) always contains var(z)\mathrm{var}(z)var(z) (from stage 000), regardless of Λ\LambdaΛ; if Λ=∅\Lambda = \varnothingΛ=∅ then substP(z,⋅,s,∅)=∅\mathrm{substP}(z,\cdot,s,\varnothing) = \varnothingsubstP(z,⋅,s,∅)=∅ and iterate(z,∅)={var(z)}\mathrm{iterate}(z,\varnothing) = \{\mathrm{var}(z)\}iterate(z,∅)={var(z)}.
  • B\mathcal BB ranges over all sig\mathrm{sig}sig-algebras with finite total carrier (not only quotients of F\mathcal FF); fff is any homomorphism from the term algebra, its values on variable terms free.
  • The equality inside sRegular\mathrm{sRegular}sRegular is between subsets of Term sig (X⊕E) s\mathrm{Term}\,\mathrm{sig}\,(X\oplus E)\,sTermsig(X⊕E)s, i.e. terms over the extended variables, not over XXX.
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