Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Finite expression syntax, replacement, Boolean evaluation, and the primary algebra

Definition
LawsOfForm_Algebra

by FiniteUniverse · Sep 7, 2026 · Mathlib 0df444a (Lean v4.33.1)

laws-of-formlogic

Defines finite expressions with natural-number variables; blank, crossing, adjacency and finite joins; simultaneous substitution and one-variable replacement; Boolean evaluation and arithmetic embedding; the position and transposition expressions; the inductively generated family AlgebraBy with separately enabled initials, all structural congruences and replacement; full Algebra and independent universal semantic validity. No consequence, completeness or independence theorem is assumed.

Definition code
import Definitions.Def_LawsOfForm_Arithmetic
import Mathlib.Data.List.Basic

set_option autoImplicit false

namespace LawsOfForm

inductive Expr where
  | blank : Expr
  | var : Nat → Expr
  | cross : Expr → Expr
  | beside : Expr → Expr → Expr
  deriving DecidableEq, Repr

def eMarked : Expr := .cross .blank

def join : List Expr → Expr
  | [] => .blank
  | a :: rest => .beside a (join rest)

def subst (σ : Nat → Expr) : Expr → Expr
  | .blank => .blank
  | .var x => σ x
  | .cross a => .cross (subst σ a)
  | .beside a b => .beside (subst σ a) (subst σ b)

def replaceVar (x : Nat) (v : Expr) : Expr → Expr :=
  subst (fun y => if y = x then v else .var y)

def eval (ρ : Nat → Bool) : Expr → Bool
  | .blank => false
  | .var x => ρ x
  | .cross a => !(eval ρ a)
  | .beside a b => eval ρ a || eval ρ b

def embed : Form → Expr
  | .blank => .blank
  | .cross a => .cross (embed a)
  | .beside a b => .beside (embed a) (embed b)

def positionL (a : Expr) : Expr := .cross (.beside (.cross a) a)

def transpositionL (a b r : Expr) : Expr :=
  .cross (.beside (.cross (.beside a r)) (.cross (.beside b r)))

def transpositionR (a b r : Expr) : Expr :=
  .beside (.cross (.beside (.cross a) (.cross b))) r

inductive AlgebraBy (position transposition : Bool) : Expr → Expr → Prop where
  | refl (a : Expr) : AlgebraBy position transposition a a
  | symm {a b : Expr} : AlgebraBy position transposition a b → AlgebraBy position transposition b a
  | trans {a b c : Expr} : AlgebraBy position transposition a b → AlgebraBy position transposition b c → AlgebraBy position transposition a c
  | assoc (a b c : Expr) : AlgebraBy position transposition (.beside (.beside a b) c) (.beside a (.beside b c))
  | comm (a b : Expr) : AlgebraBy position transposition (.beside a b) (.beside b a)
  | unit (a : Expr) : AlgebraBy position transposition (.beside .blank a) a
  | cross {a b : Expr} : AlgebraBy position transposition a b → AlgebraBy position transposition (.cross a) (.cross b)
  | left {a b : Expr} (c : Expr) : AlgebraBy position transposition a b → AlgebraBy position transposition (.beside a c) (.beside b c)
  | right (a : Expr) {b c : Expr} : AlgebraBy position transposition b c → AlgebraBy position transposition (.beside a b) (.beside a c)
  | position_rule (h : position = true) (a : Expr) : AlgebraBy position transposition (positionL a) .blank
  | transposition_rule (h : transposition = true) (a b r : Expr) : AlgebraBy position transposition (transpositionL a b r) (transpositionR a b r)
  | replacement (σ : Nat → Expr) {a b : Expr} : AlgebraBy position transposition a b → AlgebraBy position transposition (subst σ a) (subst σ b)

def Algebra : Expr → Expr → Prop := AlgebraBy true true

def Valid (a b : Expr) : Prop := ∀ ρ : Nat → Bool, eval ρ a = eval ρ b

end LawsOfForm
Source
G. Spencer-Brown, Laws of Form, Julian Press, New York, first American edition, 1972. Page authority: complete user-supplied G-Spencer-Brown-Laws-of-Form.pdf, SHA-256 1d1112350b9cf616a52bd406284f05a7fa8efe54dab6cbd37b21c005ba3928eb. Printed p = one-based PDF p+24. Edition record: https://openlibrary.org/books/OL5299727M/Laws_of_form. Chapters 4–6, printed pp. 22–37; the disabled-initial interface supports Chapter 10, p. 53.
Read-back

What the Lean code literally says, in plain math · Codex (GPT-6)

Expr. An expression is a finite formal tree freely generated by a blank expression, a variable indexed by any natural number, a crossing containing one expression, or a juxtaposition containing an ordered pair of expressions. Natural-number indices include zero and have no upper bound. These four constructors are distinct and injective in their arguments; no equations identifying differently constructed trees are imposed by this definition, so, for example, reversing a juxtaposition or adding a blank changes its syntax. The type is equipped with decidable equality of these trees and a printable representation.

eMarked. In the type of finite formal expressions generated by a blank, natural-number-indexed variables, unary crossing, and ordered binary juxtaposition, the marked expression is defined to be precisely one crossing containing the blank. This definition specifies that particular tree without identifying it with any other tree.

join. For every finite list of expressions, where expressions are formal trees generated by a blank ε\varepsilonε, variables indexed by natural numbers including zero, unary crossing, and ordered binary juxtaposition BBB, the function JJJ is defined recursively by J([])=εJ([])=\varepsilonJ([])=ε and J(a::L)=B(a,J(L))J(a::L)=B(a,J(L))J(a::L)=B(a,J(L)). Thus the empty list gives the blank, a singleton list gives B(a,ε)B(a,\varepsilon)B(a,ε), and a list [a1,…,an][a_1,\ldots,a_n][a1​,…,an​] with n≥1n\geq 1n≥1 gives the right-associated tree B(a1,B(a2,…,B(an,ε)…))B(a_1,B(a_2,\ldots,B(a_n,\varepsilon)\ldots))B(a1​,B(a2​,…,B(an​,ε)…)), retaining the final blank and every listed entry without any syntactic simplification.

subst. For every function σ:N→E\sigma:\mathbb N\to Eσ:N→E, where EEE consists of finite formal trees built from a blank ε\varepsilonε, variables vnv_nvn​ for all natural numbers nnn including zero, unary crossing CCC, and ordered binary juxtaposition BBB, substitution Sσ:E→ES_\sigma:E\to ESσ​:E→E is defined by Sσ(ε)=εS_\sigma(\varepsilon)=\varepsilonSσ​(ε)=ε, Sσ(vn)=σ(n)S_\sigma(v_n)=\sigma(n)Sσ​(vn​)=σ(n), Sσ(C(a))=C(Sσ(a))S_\sigma(C(a))=C(S_\sigma(a))Sσ​(C(a))=C(Sσ​(a)), and Sσ(B(a,b))=B(Sσ(a),Sσ(b))S_\sigma(B(a,b))=B(S_\sigma(a),S_\sigma(b))Sσ​(B(a,b))=B(Sσ​(a),Sσ​(b)). The function σ\sigmaσ is arbitrary and is defined at every natural number; its expression values may contain any variables, including the variable being replaced. A replacement expression σ(n)\sigma(n)σ(n) is returned directly when the original tree is the variable vnv_nvn​, so substitution does not continue recursively inside that inserted expression.

replaceVar. For every natural number xxx, every expression vvv, and every input expression aaa, where expressions are finite trees built from a blank, variables vnv_nvn​ indexed by natural numbers including zero, unary crossing, and ordered binary juxtaposition, replacement of variable xxx by vvv traverses the original tree aaa, leaves its blanks unchanged, preserves its crossing and juxtaposition constructors, replaces each original occurrence of vxv_xvx​ by the whole expression vvv, and leaves every other original variable vyv_yvy​ as vyv_yvy​. This is simultaneous substitution using the function that has value vvv at xxx and value vyv_yvy​ at each y≠xy\ne xy=x. The inserted expression may itself contain vxv_xvx​ or other variables, and its contents are not subjected to further replacement during that insertion; if the original tree has no occurrence of vxv_xvx​, the tree is unchanged.

eval. For every assignment ρ:N→{false,true}\rho:\mathbb N\to\{\mathrm{false},\mathrm{true}\}ρ:N→{false,true} and every finite formal expression built from a blank ε\varepsilonε, variables vnv_nvn​ for natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, its Boolean evaluation EρE_\rhoEρ​ is defined by Eρ(ε)=falseE_\rho(\varepsilon)=\mathrm{false}Eρ​(ε)=false, Eρ(vn)=ρ(n)E_\rho(v_n)=\rho(n)Eρ​(vn​)=ρ(n), Eρ(C(a))=¬Eρ(a)E_\rho(C(a))=\neg E_\rho(a)Eρ​(C(a))=¬Eρ​(a), and Eρ(B(a,b))=Eρ(a)∨Eρ(b)E_\rho(B(a,b))=E_\rho(a)\lor E_\rho(b)Eρ​(B(a,b))=Eρ​(a)∨Eρ​(b), where negation exchanges the two Boolean values and disjunction is true exactly when at least one argument is true. Assignments are arbitrary total functions on all natural numbers; no restriction is placed on the number of indices assigned true. In particular, evaluation is defined for the blank and for every expression with no variables.

embed. A form is a finite formal tree generated by a blank, unary crossing, and ordered binary juxtaposition; an expression is a finite formal tree with those same three kinds of constructor and, additionally, variables indexed by natural numbers including zero. The function from forms to expressions sends a form's blank to the expression blank, sends a crossing of a form aaa to the expression crossing of the image of aaa, and sends a juxtaposition of forms a,ba,ba,b to the expression juxtaposition of their images in the same order. Thus the map applies to every form, including the blank, preserves its complete constructor structure, and introduces no variable nodes.

positionL. For every expression aaa, where expressions are finite formal trees generated by a blank, natural-number-indexed variables including index zero, unary crossing CCC, and ordered binary juxtaposition BBB, the function called the left position expression returns exactly C(B(C(a),a))C(B(C(a),a))C(B(C(a),a)): one crossing around a juxtaposition whose left child is a crossing around aaa and whose right child is aaa itself. There is no restriction on aaa, so it may be the blank, a variable, or any composite expression; this definition constructs a tree and asserts no equation relating that tree to the blank.

transpositionL. For every three expressions a,b,ra,b,ra,b,r, where expressions are finite formal trees generated by a blank, variables indexed by natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, the function called the left transposition expression returns exactly C(B(C(B(a,r)),C(B(b,r))))C(B(C(B(a,r)),C(B(b,r))))C(B(C(B(a,r)),C(B(b,r)))). Its outer crossing contains a juxtaposition of the crossing around B(a,r)B(a,r)B(a,r) and the crossing around B(b,r)B(b,r)B(b,r), in that order. The three input expressions are arbitrary, may coincide with one another, and may be blank or contain variables; no simplification or equality to another expression is part of this definition.

transpositionR. For every three expressions a,b,ra,b,ra,b,r, where expressions are finite formal trees generated by a blank, variables indexed by natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, the function called the right transposition expression returns exactly B(C(B(C(a),C(b))),r)B(C(B(C(a),C(b))),r)B(C(B(C(a),C(b))),r). Its left child is a crossing around the juxtaposition of a crossing around aaa and a crossing around bbb, and its right child is rrr. The three expressions are unrestricted, may coincide with one another, and may be blank or contain variables; this definition constructs that precise tree without asserting any equation.

AlgebraBy. Fix any two Boolean parameters p,tp,tp,t, each independently either false or true. On the set EEE of finite formal expressions freely built from a blank ε\varepsilonε, variables vnv_nvn​ indexed by all natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, the relation Rp,tR_{p,t}Rp,t​ is the least relation generated by the following rules, with the same fixed parameters throughout: for every a∈Ea\in Ea∈E, a Rp,t aa\,R_{p,t}\,aaRp,t​a; for every a,b∈Ea,b\in Ea,b∈E, a Rp,t ba\,R_{p,t}\,baRp,t​b implies b Rp,t ab\,R_{p,t}\,abRp,t​a; for every a,b,c∈Ea,b,c\in Ea,b,c∈E, a Rp,t ba\,R_{p,t}\,baRp,t​b and b Rp,t cb\,R_{p,t}\,cbRp,t​c imply a Rp,t ca\,R_{p,t}\,caRp,t​c; for every a,b,c∈Ea,b,c\in Ea,b,c∈E, B(B(a,b),c) Rp,t B(a,B(b,c))B(B(a,b),c)\,R_{p,t}\,B(a,B(b,c))B(B(a,b),c)Rp,t​B(a,B(b,c)); for every a,b∈Ea,b\in Ea,b∈E, B(a,b) Rp,t B(b,a)B(a,b)\,R_{p,t}\,B(b,a)B(a,b)Rp,t​B(b,a); for every a∈Ea\in Ea∈E, B(ε,a) Rp,t aB(\varepsilon,a)\,R_{p,t}\,aB(ε,a)Rp,t​a; for every a,b∈Ea,b\in Ea,b∈E, a Rp,t ba\,R_{p,t}\,baRp,t​b implies C(a) Rp,t C(b)C(a)\,R_{p,t}\,C(b)C(a)Rp,t​C(b); for every a,b,c∈Ea,b,c\in Ea,b,c∈E, a Rp,t ba\,R_{p,t}\,baRp,t​b implies B(a,c) Rp,t B(b,c)B(a,c)\,R_{p,t}\,B(b,c)B(a,c)Rp,t​B(b,c); for every a,b,c∈Ea,b,c\in Ea,b,c∈E, b Rp,t cb\,R_{p,t}\,cbRp,t​c implies B(a,b) Rp,t B(a,c)B(a,b)\,R_{p,t}\,B(a,c)B(a,b)Rp,t​B(a,c); if p=truep=\mathrm{true}p=true, then for every a∈Ea\in Ea∈E, C(B(C(a),a)) Rp,t εC(B(C(a),a))\,R_{p,t}\,\varepsilonC(B(C(a),a))Rp,t​ε; if t=truet=\mathrm{true}t=true, then for every a,b,r∈Ea,b,r\in Ea,b,r∈E, C(B(C(B(a,r)),C(B(b,r)))) Rp,t B(C(B(C(a),C(b))),r)C(B(C(B(a,r)),C(B(b,r))))\,R_{p,t}\,B(C(B(C(a),C(b))),r)C(B(C(B(a,r)),C(B(b,r))))Rp,t​B(C(B(C(a),C(b))),r); and for every function σ:N→E\sigma:\mathbb N\to Eσ:N→E and every a,b∈Ea,b\in Ea,b∈E, a Rp,t ba\,R_{p,t}\,baRp,t​b implies Sσ(a) Rp,t Sσ(b)S_\sigma(a)\,R_{p,t}\,S_\sigma(b)Sσ​(a)Rp,t​Sσ​(b), where SσS_\sigmaSσ​ fixes the blank, sends each variable vnv_nvn​ directly to σ(n)\sigma(n)σ(n) without further substitution inside that inserted tree, and preserves crossing and juxtaposition while recursively substituting in their original children. Every expression argument in these rules may be blank or may coincide with another argument. When ppp is false, the premise p=truep=\mathrm{true}p=true cannot hold, so that conditional rule supplies no generating instances; the corresponding statement applies to ttt and its conditional rule. These false settings do not themselves assert the absence of those expression pairs from the relation generated by the remaining rules. Membership in this inductively generated relation consists of a finite derivation using the stated rules.

Algebra. On finite formal expressions freely built from a blank ε\varepsilonε, variables vnv_nvn​ indexed by natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, the relation ∼\sim∼ is defined to be the least relation with all of the following properties: every expression is related to itself; for every a,ba,ba,b, a∼ba\sim ba∼b implies b∼ab\sim ab∼a; for every a,b,ca,b,ca,b,c, a∼ba\sim ba∼b and b∼cb\sim cb∼c imply a∼ca\sim ca∼c; for all expressions a,b,ca,b,ca,b,c, B(B(a,b),c)∼B(a,B(b,c))B(B(a,b),c)\sim B(a,B(b,c))B(B(a,b),c)∼B(a,B(b,c)); for all a,ba,ba,b, B(a,b)∼B(b,a)B(a,b)\sim B(b,a)B(a,b)∼B(b,a); for every aaa, B(ε,a)∼aB(\varepsilon,a)\sim aB(ε,a)∼a; for all a,ba,ba,b, a∼ba\sim ba∼b implies C(a)∼C(b)C(a)\sim C(b)C(a)∼C(b); for all a,b,ca,b,ca,b,c, a∼ba\sim ba∼b implies B(a,c)∼B(b,c)B(a,c)\sim B(b,c)B(a,c)∼B(b,c); for all a,b,ca,b,ca,b,c, b∼cb\sim cb∼c implies B(a,b)∼B(a,c)B(a,b)\sim B(a,c)B(a,b)∼B(a,c); for every aaa, C(B(C(a),a))∼εC(B(C(a),a))\sim\varepsilonC(B(C(a),a))∼ε; for every a,b,ra,b,ra,b,r, C(B(C(B(a,r)),C(B(b,r))))∼B(C(B(C(a),C(b))),r)C(B(C(B(a,r)),C(B(b,r))))\sim B(C(B(C(a),C(b))),r)C(B(C(B(a,r)),C(B(b,r))))∼B(C(B(C(a),C(b))),r); and for every function σ:N→E\sigma:\mathbb N\to Eσ:N→E into this expression set and every a,ba,ba,b, a∼ba\sim ba∼b implies Sσ(a)∼Sσ(b)S_\sigma(a)\sim S_\sigma(b)Sσ​(a)∼Sσ​(b), where SσS_\sigmaSσ​ fixes the blank, replaces each original variable vnv_nvn​ directly by σ(n)\sigma(n)σ(n) without continuing substitution inside that inserted expression, and recursively preserves crossing and juxtaposition. This definition sets both Boolean switches of the generating relation to true, so both displayed special equations are available for every choice of their arguments. All expression arguments are unrestricted, including blanks, repeated arguments, and variable-containing trees, and relatedness is witnessed by a finite derivation from these rules.

Valid. For any two expressions a,ba,ba,b, where expressions are finite formal trees built from a blank ε\varepsilonε, variables vnv_nvn​ indexed by natural numbers including zero, unary crossing CCC, and ordered binary juxtaposition BBB, validity of the pair means that for every total Boolean assignment ρ:N→{false,true}\rho:\mathbb N\to\{\mathrm{false},\mathrm{true}\}ρ:N→{false,true}, the Boolean values of aaa and bbb are equal. Here the value of ε\varepsilonε is false, the value of vnv_nvn​ is ρ(n)\rho(n)ρ(n), the value of C(u)C(u)C(u) is the Boolean negation of the value of uuu, and the value of B(u,v)B(u,v)B(u,v) is the Boolean disjunction of the values of uuu and vvv. All assignments are quantified over without any finite-support condition, including the constant-false and constant-true assignments; the definition also applies when either or both expressions have no variables or are blank. The condition is equality of these evaluations under every assignment and contains no hypothesis or conclusion asserting that the pair has a derivation in an algebraic relation.

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 paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me