The regular signature
DefinitionMSKleene_RegSig(updated) The regular signature . See the mission's other definition items for the surrounding development.
/-
The regular signature `Reg(S,Σ,Z)` (Definition 4.1) and regular expressions
(Definition 4.2).
`Reg(S,Σ,Z)` expands `Σ`, for a fixed `S`-sorted set `Z`, by:
* an empty constant `∅_s` of coarity `s` (rank `([], s)`);
* a binary sum `+_s` of coarity `s` (rank `([s,s], s)`);
* a unary `z`-iteration `(·)^{⋆z}` for each `z ∈ Z_s` (rank `([s], s)`);
* a `z`-substitution `⟨z/·⟩^♯ᵖ_s(·)` for each `z ∈ Z_t` (rank `([t,s], s)`).
A **regular expression over `(S,Σ,Z)` of type `s`** is a term of the free
`Reg(S,Σ,Z)`-algebra on `Z`, i.e. an element of `Term (regSig sig Z) Z s`.
Also here: `Term.relabel`, the action of `T_Σ(-)` on an `S`-sorted map of
variables, used to view `T_Σ(X)` inside `T_Σ(Z)` when `X ⊆ Z`.
-/
import Definitions.Def_MSKleene_Term
namespace MSKleene
universe u
variable {S : Type u}
/-- The operation symbols of the regular signature `Reg(S,Σ,Z)` (Definition 4.1),
indexed by rank `(w, s)`. `base` re-uses every symbol of `Σ`; the four extra
constructors are `∅_s`, `+_s`, `(·)^{⋆z}`, and `⟨z/·⟩^♯ᵖ_s(·)`. -/
inductive RegSym (sig : Signature S) (Z : SSet S) : List S → S → Type u where
| base {w : List S} {s : S} : sig w s → RegSym sig Z w s
| empty (s : S) : RegSym sig Z [] s
| iter (s : S) : Z s → RegSym sig Z [s] s
| plus (s : S) : RegSym sig Z [s, s] s
| subst (t s : S) : Z t → RegSym sig Z [t, s] s
/-- The regular signature `Reg(S,Σ,Z)` as an `S`-sorted signature. -/
def regSig (sig : Signature S) (Z : SSet S) : Signature S := RegSym sig Z
/-- A regular expression over `(S,Σ,Z)` of type `s` (Definition 4.2). -/
abbrev RegExpr (sig : Signature S) (Z : SSet S) (s : S) : Type u :=
Term (regSig sig Z) Z s
-- Relabel the variables of a term along an `S`-sorted map `ι : X → Y`
-- (functoriality of `T_Σ(-)`). Used with an inclusion `X ↪ Z`.
mutual
def Term.relabel {sig : Signature S} {X Y : SSet S} (ι : SMap X Y) :
{s : S} → Term sig X s → Term sig Y s
| _, .var x => Term.var (ι _ x)
| _, .app σ ts => Term.app σ (TermVec.relabel ι ts)
def TermVec.relabel {sig : Signature S} {X Y : SSet S} (ι : SMap X Y) :
{w : List S} → TermVec sig X w → TermVec sig Y w
| _, .nil => .nil
| _, .cons t ts => .cons (Term.relabel ι t) (TermVec.relabel ι ts)
end
-- Embed a `Σ`-term as a regular expression over `(S,Σ,Z)` of the same type:
-- every operation symbol `σ` becomes `RegSym.base σ` (Lemma 4.9).
mutual
def Term.toReg {sig : Signature S} {Z : SSet S} :
{s : S} → Term sig Z s → RegExpr sig Z s
| _, .var x => Term.var x
| _, .app σ ts => Term.app (RegSym.base σ) (TermVec.toReg ts)
def TermVec.toReg {sig : Signature S} {Z : SSet S} :
{w : List S} → TermVec sig Z w → TermVec (regSig sig Z) Z w
| _, .nil => .nil
| _, .cons t ts => .cons (Term.toReg t) (TermVec.toReg ts)
end
end MSKleene
Read-back
What the Lean code literally says, in plain math · claude-sonnet-5
RegSym
RegSym is an inductive family of types. Its ambient data are: an implicit sort type in universe ; an explicit sig : Signature S, where Signature S unfolds by definition to (so sig assigns to each arity — a finite list of sorts — and each result sort a type of operation symbols); and an explicit Z : SSet S, where SSet S unfolds to (so assigns a type to each sort ). For fixed sig and Z, the family RegSym sig Z has type , i.e. it is itself a signature over ; for an arity and result sort the type RegSym sig Z w s is generated by exactly the following constructors:
base: from an implicit , an implicit , and an element of , produces an element ofRegSym sig Z w s— same arity , same result sort .empty: for every explicit , an element ofRegSym sig Z [] s— arity the empty list , result sort .iter: for every explicit and every element of , an element ofRegSym sig Z [s] s— arity the one-element list , result sort .plus: for every explicit , an element ofRegSym sig Z [s, s] s— arity the two-element list , result sort .subst: for every pair of explicit sorts and every element of , an element ofRegSym sig Z [t, s] s— arity the two-element list , result sort .
There are no other constructors. Note the edge behavior: empty and plus produce a symbol for every sort unconditionally; iter at sort produces symbols only insofar as has elements (none if is empty); subst with argument sorts produces symbols only insofar as has elements; and base embeds the symbols of sig at every arity and result sort, one base-symbol per sig-symbol.
regSig
regSig takes an implicit sort type , an explicit sig : Signature S (assigning a type of operation symbols to each arity and result sort ), and an explicit Z : SSet S (assigning a type to each sort ), and returns a term of type Signature S, namely the family . Thus is the signature whose symbols of arity and result sort are precisely the elements of RegSym sig Z w s as described above (the base copies of sig's symbols together with the empty, iter, plus, and subst symbols). This is a purely definitional repackaging: no data is added or removed.
RegExpr
RegExpr is an abbreviation. Given an implicit sort type , an explicit sig : Signature S, an explicit Z : SSet S, and an explicit sort s : S, the type RegExpr sig Z s (in ) unfolds to Term (regSig sig Z) Z s. Here Term σ X is the mutually-inductive family of terms over a signature with variables drawn from a sorted set : a value at sort is either a variable var x with , or an application app f ts of a symbol of of arity and result sort to a vector ts of subterms whose sorts match the list . Therefore a RegExpr sig Z s is a term of result sort whose variables are elements of the families and whose operation nodes are labelled by regular symbols of (i.e. base σ, empty, iter, plus, or subst).
Term.relabel
Term.relabel is one of two functions defined together by mutual structural recursion (with TermVec.relabel). It takes an implicit sort type , an implicit sig : Signature S, two implicit sorted sets X Y : SSet S, and an explicit ι : SMap X Y, where SMap X Y unfolds to — a family of functions , one per sort. It then takes an implicit sort and a value of Term sig X s, and returns a value of Term sig Y s (same signature sig, same result sort , but with variables now taken from ). It is defined by cases on the input term:
- on a variable
var xwith , it returnsvar (ι s x), the variable ; - on an application
app σ ts, where is a symbol ofsigof arity and result sort andtsis of typeTermVec sig X w, it returnsapp σ (TermVec.relabel ι ts)— the same symbol applied to the argument vector obtained by recursively relabellingts.
TermVec.relabel
TermVec.relabel is the companion of Term.relabel in the same mutual block. With the same implicit data (sort type , sig : Signature S, sorted sets X Y : SSet S) and the same explicit ι : SMap X Y (the family ), it takes an implicit arity w : List S and a value of TermVec sig X w, and returns a value of TermVec sig Y w. Here TermVec sig X w is the type of finite tuples of terms indexed by the list of sorts : the constructor nil gives the empty tuple at arity , and cons t ts gives, at arity , a tuple whose head t has type Term sig X s and whose tail ts has type TermVec sig X w. The definition:
- on
nil, it returnsnil; - on
cons t ts, it returnscons (Term.relabel ι t) (TermVec.relabel ι ts)— relabel the head term viaTerm.relabeland recursively relabel the tail vector.
Term.toReg
Term.toReg is one of two functions defined together by mutual structural recursion (with TermVec.toReg). It takes an implicit sort type , an implicit sig : Signature S, and an implicit Z : SSet S; then an implicit sort and a value of Term sig Z s — a term over the signature sig whose variables are elements of the families, of result sort . It returns a value of RegExpr sig Z s, which unfolds to Term (regSig sig Z) Z s — a term over the signature , with the same variable family and the same result sort . It is defined by cases:
- on a variable
var xwith , it returnsvar x— the same variable, now regarded as a variable in the term family over ; - on an application
app σ ts, where is a symbol ofsigof arity and result sort andtsis of typeTermVec sig Z w, it returnsapp (RegSym.base σ) (TermVec.toReg ts)— the symbol re-tagged through thebaseconstructor as a symbol ofRegSym sig Z w s, applied to the argument vector obtained by recursively convertingts.
TermVec.toReg
TermVec.toReg is the companion of Term.toReg in the same mutual block. With implicit sort type , implicit sig : Signature S, and implicit Z : SSet S, it takes an implicit arity w : List S and a value of TermVec sig Z w, and returns a value of TermVec (regSig sig Z) Z w — the argument-vector type over the regular signature , with the same list of sorts and the same variable family . It is defined by cases:
- on
nil, it returnsnil; - on
cons t ts, it returnscons (Term.toReg t) (TermVec.toReg ts)— convert the head term viaTerm.toRegand recursively convert the tail vector.
Confirmed by the mission captain (proposal self-audit).