Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

The subterm order

Definition
MSKleene_Subterm

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

formal-languagesmany-sorted-algebratree-automatauniversal-algebra

The subterm order on the free many-sorted algebra (Definitions 2.30, 3.7, 3.8).

ImmSub a b holds when the sorted term a (an element of the coproduct ∐sTΣ(X)s\coprod_s \mathrm{T}_\Sigma(X)_s∐s​TΣ​(X)s​, STerm) is an immediate argument of b in b's unique decomposition. Its transitive closure SubtermLT is the strict proper-subterm order <; its reflexive–transitive closure SubtermLE is ≤; Min b says b has no proper subterm; Subt P is the SSS-sorted set of subterms of P. TermVec.Mem is entry membership in an argument vector.

Proposition 3.6 (a milestone) states that SubtermLT is Artinian and that its minimal elements are exactly the variables and the constant symbols.

Definition code
/-
The subterm order on the free many-sorted algebra (Definitions 2.30, 3.7).

`ImmSub a b` holds when the sorted term `a` is an immediate argument of `b`
(one decomposition step). Its transitive closure `SubtermLT` is the strict
proper-subterm order `<`, its reflexive–transitive closure `SubtermLE` is `≤`,
and `Min` picks out the minimal sorted terms.

Proposition 3.6 (`PArtOrd`) — proved as a milestone — states that `SubtermLT` is
Artinian (well-founded) on the free algebra and that its minimal elements are
exactly the variables and the constant symbols.
-/
import Definitions.Def_MSKleene_Term
import Mathlib.Logic.Relation

namespace MSKleene

universe u

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

/-- A **sorted term**: an element of the coproduct `∐_s T_Σ(X)_s`. -/
def STerm (sig : Signature S) (X : SSet S) : Type u := Σ s : S, Term sig X s

/-- `p` occurs as one of the entries of the term vector `ts`. -/
inductive TermVec.Mem {sig : Signature S} {X : SSet S} :
    {t : S} → {w : List S} → Term sig X t → TermVec sig X w → Prop
  | head {t : S} {w : List S} (p : Term sig X t) (ps : TermVec sig X w) :
      TermVec.Mem p (.cons p ps)
  | tail {t s : S} {w : List S} {p : Term sig X t} (q : Term sig X s)
      {ps : TermVec sig X w} : TermVec.Mem p ps → TermVec.Mem p (.cons q ps)

/-- The immediate-subterm relation `<_{T_Σ(X)}` (Definition 2.30): `a` is an
immediate argument of `b` in `b`'s unique decomposition. -/
def ImmSub (a b : STerm sig X) : Prop :=
  ∃ (w : List S) (σ : sig w b.1) (ts : TermVec sig X w),
    b.2 = Term.app σ ts ∧ TermVec.Mem a.2 ts

/-- The strict proper-subterm order `<` — the transitive closure of `ImmSub`. -/
def SubtermLT (a b : STerm sig X) : Prop := Relation.TransGen ImmSub a b

/-- The subterm order `≤` — the reflexive–transitive closure of `ImmSub`. -/
def SubtermLE (a b : STerm sig X) : Prop := Relation.ReflTransGen ImmSub a b

/-- A sorted term is **minimal** when it has no proper subterm. -/
def Min (b : STerm sig X) : Prop := ∀ a : STerm sig X, ¬ ImmSub a b

/-- `Subt P` — the `S`-sorted set of subterms of `P` (Definition 3.8). -/
def Subt {s : S} (P : Term sig X s) : SSub (Term sig X) :=
  fun t => { Q : Term sig X t | SubtermLE ⟨t, Q⟩ ⟨s, P⟩ }

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

Setup common to every declaration. A type SSS is fixed (the sorts); its elements are drawn from universe uuu. A signature sigsigsig over SSS is a family that assigns to every pair (w,s)(w, s)(w,s), where www is a finite list of sorts and sss is a single sort, a type sig  w  ssig\;w\;ssigws whose elements are operation symbols of input profile www and output sort sss. An object XXX of type SSet S\mathrm{SSet}\,SSSetS is a family assigning to each sort sss a type XsX_sXs​ of variables of sort sss. The type family Term sig X:S→Type\mathrm{Term}\,sig\,X : S \to \mathrm{Type}TermsigX:S→Type and the type family TermVec sig X:List S→Type\mathrm{TermVec}\,sig\,X : \mathrm{List}\,S \to \mathrm{Type}TermVecsigX:ListS→Type are defined by mutual induction: a term of sort sss is either var x\mathrm{var}\,xvarx for some variable x:Xsx : X_sx:Xs​, or app σ ts\mathrm{app}\,\sigma\,tsappσts where σ:sig  w  s\sigma : sig\;w\;sσ:sigws is an operation symbol and tststs is a term-vector over www; a term-vector over a list is either nil\mathrm{nil}nil (over the empty list [][][]) or cons p ps\mathrm{cons}\,p\,psconspps (over s::ws :: ws::w), formed from a term ppp of sort sss and a term-vector pspsps over www. In the file, sigsigsig and XXX are implicit parameters of every declaration below (except where re-declared explicitly).

STerm\mathrm{STerm}STerm. Given an explicit signature sigsigsig over SSS and an explicit variable family X:SSet SX : \mathrm{SSet}\,SX:SSetS, the type STerm sig X\mathrm{STerm}\,sig\,XSTermsigX is defined to be the dependent sum

STerm sig X  :=  ∑s:STerm sig X s,\mathrm{STerm}\,sig\,X \;:=\; \sum_{s : S} \mathrm{Term}\,sig\,X\,s,STermsigX:=s:S∑​TermsigXs,

that is, the type of pairs ⟨s,t⟩\langle s, t\rangle⟨s,t⟩ consisting of a sort s:Ss : Ss:S together with a term ttt of sort sss (over signature sigsigsig with variables in XXX). It is placed in universe uuu. For an element a:STerm sig Xa : \mathrm{STerm}\,sig\,Xa:STermsigX, its first component a1:Sa_1 : Sa1​:S is the sort and its second component a2:Term sig X a1a_2 : \mathrm{Term}\,sig\,X\,a_1a2​:TermsigXa1​ is the term.

TermVec.Mem\mathrm{TermVec.Mem}TermVec.Mem. This defines, for the fixed SSS, sigsigsig, XXX, an inductive proposition-valued relation. Its full argument list is: an implicit sort t:St : St:S, an implicit list of sorts w:List Sw : \mathrm{List}\,Sw:ListS, an (explicit) term ppp of sort ttt, and an (explicit) term-vector pspsps over www; the statement TermVec.Mem p ps\mathrm{TermVec.Mem}\,p\,psTermVec.Mempps asserts that ppp occurs as an entry of the term-vector pspsps. It is generated by exactly two constructors:

  • head: for every sort ttt, every list www, every term ppp of sort ttt, and every term-vector pspsps over www, one has TermVec.Mem p (cons p ps)\mathrm{TermVec.Mem}\,p\,(\mathrm{cons}\,p\,ps)TermVec.Memp(conspps) — the vector obtained by prepending ppp (the same term ppp, so this is the strict/heterogeneous equality forced by the constructor) to pspsps, which is a term-vector over t::wt :: wt::w.
  • tail: for all sorts t,st, st,s, every list www, every term ppp of sort ttt (implicit), every term qqq of sort sss (explicit), and every term-vector pspsps over www (implicit), if TermVec.Mem p ps\mathrm{TermVec.Mem}\,p\,psTermVec.Mempps holds then TermVec.Mem p (cons q ps)\mathrm{TermVec.Mem}\,p\,(\mathrm{cons}\,q\,ps)TermVec.Memp(consqps) holds, where cons q ps\mathrm{cons}\,q\,psconsqps is a term-vector over s::ws :: ws::w.

In particular no term is a member of nil\mathrm{nil}nil, and membership of ppp (of sort ttt) in a vector over www is derivable only when ttt actually occurs among the sorts listed in www at a position holding a term equal to ppp.

ImmSub\mathrm{ImmSub}ImmSub. For two sorted terms a,b:STerm sig Xa, b : \mathrm{STerm}\,sig\,Xa,b:STermsigX, the proposition ImmSub a b\mathrm{ImmSub}\,a\,bImmSubab ("aaa is an immediate subterm of bbb") is defined to hold iff there exist a list of sorts w:List Sw : \mathrm{List}\,Sw:ListS, an operation symbol σ:sig  w  b1\sigma : sig\;w\;b_1σ:sigwb1​ (input profile www, output sort equal to the sort b1b_1b1​ of bbb), and a term-vector tststs over www, such that both:

b2=app σ tsandTermVec.Mem a2 ts.b_2 = \mathrm{app}\,\sigma\,ts \qquad\text{and}\qquad \mathrm{TermVec.Mem}\,a_2\,ts.b2​=appσtsandTermVec.Mema2​ts.

The first conjunct is an equality of terms of sort b1b_1b1​ stating that the term component of bbb is literally the application of σ\sigmaσ to tststs; the second conjunct states that the term component a2a_2a2​ of aaa (a term of sort a1a_1a1​) occurs as an entry of the vector tststs. Nothing constrains a1a_1a1​ except through this membership. If b2b_2b2​ is a variable, or an application whose argument vector is nil\mathrm{nil}nil, then no such σ,ts\sigma, tsσ,ts with a member exist and ImmSub a b\mathrm{ImmSub}\,a\,bImmSubab is false for every aaa.

SubtermLT\mathrm{SubtermLT}SubtermLT. For sorted terms a,b:STerm sig Xa, b : \mathrm{STerm}\,sig\,Xa,b:STermsigX, the proposition SubtermLT a b\mathrm{SubtermLT}\,a\,bSubtermLTab is defined to be the transitive closure TransGen ImmSub a b\mathrm{TransGen}\,\mathrm{ImmSub}\,a\,bTransGenImmSubab of the relation ImmSub\mathrm{ImmSub}ImmSub: there is a finite chain a=c0,c1,…,cn=ba = c_0, c_1, \dots, c_n = ba=c0​,c1​,…,cn​=b with n≥1n \ge 1n≥1 and ImmSub ci ci+1\mathrm{ImmSub}\,c_{i}\,c_{i+1}ImmSubci​ci+1​ for each 0≤i<n0 \le i < n0≤i<n. At least one ImmSub\mathrm{ImmSub}ImmSub step is required; this relation is not reflexive.

SubtermLE\mathrm{SubtermLE}SubtermLE. For sorted terms a,b:STerm sig Xa, b : \mathrm{STerm}\,sig\,Xa,b:STermsigX, the proposition SubtermLE a b\mathrm{SubtermLE}\,a\,bSubtermLEab is defined to be the reflexive–transitive closure ReflTransGen ImmSub a b\mathrm{ReflTransGen}\,\mathrm{ImmSub}\,a\,bReflTransGenImmSubab of the relation ImmSub\mathrm{ImmSub}ImmSub: either a=ba = ba=b, or there is a finite chain of one or more ImmSub\mathrm{ImmSub}ImmSub steps from aaa to bbb. Equivalently, a chain of zero or more ImmSub\mathrm{ImmSub}ImmSub steps connects aaa to bbb.

Min\mathrm{Min}Min. For a sorted term b:STerm sig Xb : \mathrm{STerm}\,sig\,Xb:STermsigX, the proposition Min b\mathrm{Min}\,bMinb is defined as

∀ a:STerm sig X,    ¬ ImmSub a b,\forall\, a : \mathrm{STerm}\,sig\,X,\;\; \neg\,\mathrm{ImmSub}\,a\,b,∀a:STermsigX,¬ImmSubab,

i.e. no sorted term aaa is an immediate subterm of bbb. Given the definition of ImmSub\mathrm{ImmSub}ImmSub, this holds exactly when the term component b2b_2b2​ of bbb is a variable, or is an application app σ ts\mathrm{app}\,\sigma\,tsappσts whose argument term-vector tststs has no entries (is nil\mathrm{nil}nil).

Subt\mathrm{Subt}Subt. Given an implicit sort s:Ss : Ss:S and a term PPP of sort sss, the object Subt P\mathrm{Subt}\,PSubtP has type SSub (Term sig X)\mathrm{SSub}\,(\mathrm{Term}\,sig\,X)SSub(TermsigX), which unfolds to (t:S)→Set (Term sig X t)(t : S) \to \mathrm{Set}\,(\mathrm{Term}\,sig\,X\,t)(t:S)→Set(TermsigXt): an SSS-indexed family of sets of terms. It is defined by

Subt P  :=  λt.  { Q:Term sig X t  ∣  SubtermLE ⟨t,Q⟩ ⟨s,P⟩ },\mathrm{Subt}\,P \;:=\; \lambda t.\; \{\, Q : \mathrm{Term}\,sig\,X\,t \;\mid\; \mathrm{SubtermLE}\,\langle t, Q\rangle\,\langle s, P\rangle \,\},SubtP:=λt.{Q:TermsigXt∣SubtermLE⟨t,Q⟩⟨s,P⟩},

that is, for each sort ttt, the set of all terms QQQ of sort ttt such that the sorted term ⟨t,Q⟩\langle t, Q\rangle⟨t,Q⟩ stands in the relation SubtermLE\mathrm{SubtermLE}SubtermLE to the sorted term ⟨s,P⟩\langle s, P\rangle⟨s,P⟩ — i.e. ⟨t,Q⟩\langle t, Q\rangle⟨t,Q⟩ is reachable from... reachable to ⟨s,P⟩\langle s, P\rangle⟨s,P⟩ by zero or more ImmSub\mathrm{ImmSub}ImmSub steps (a non-strict subterm of PPP, allowing Q=PQ = PQ=P when t=st = st=s).

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