Finite expression syntax, replacement, Boolean evaluation, and the primary algebra
DefinitionLawsOfForm_AlgebraDefines 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.
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
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 , variables indexed by natural numbers including zero, unary crossing, and ordered binary juxtaposition , the function is defined recursively by and . Thus the empty list gives the blank, a singleton list gives , and a list with gives the right-associated tree , retaining the final blank and every listed entry without any syntactic simplification.
subst. For every function , where consists of finite formal trees built from a blank , variables for all natural numbers including zero, unary crossing , and ordered binary juxtaposition , substitution is defined by , , , and . The function is arbitrary and is defined at every natural number; its expression values may contain any variables, including the variable being replaced. A replacement expression is returned directly when the original tree is the variable , so substitution does not continue recursively inside that inserted expression.
replaceVar. For every natural number , every expression , and every input expression , where expressions are finite trees built from a blank, variables indexed by natural numbers including zero, unary crossing, and ordered binary juxtaposition, replacement of variable by traverses the original tree , leaves its blanks unchanged, preserves its crossing and juxtaposition constructors, replaces each original occurrence of by the whole expression , and leaves every other original variable as . This is simultaneous substitution using the function that has value at and value at each . The inserted expression may itself contain or other variables, and its contents are not subjected to further replacement during that insertion; if the original tree has no occurrence of , the tree is unchanged.
eval. For every assignment and every finite formal expression built from a blank , variables for natural numbers including zero, unary crossing , and ordered binary juxtaposition , its Boolean evaluation is defined by , , , and , 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 to the expression crossing of the image of , and sends a juxtaposition of forms 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 , where expressions are finite formal trees generated by a blank, natural-number-indexed variables including index zero, unary crossing , and ordered binary juxtaposition , the function called the left position expression returns exactly : one crossing around a juxtaposition whose left child is a crossing around and whose right child is itself. There is no restriction on , 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 , where expressions are finite formal trees generated by a blank, variables indexed by natural numbers including zero, unary crossing , and ordered binary juxtaposition , the function called the left transposition expression returns exactly . Its outer crossing contains a juxtaposition of the crossing around and the crossing around , 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 , where expressions are finite formal trees generated by a blank, variables indexed by natural numbers including zero, unary crossing , and ordered binary juxtaposition , the function called the right transposition expression returns exactly . Its left child is a crossing around the juxtaposition of a crossing around and a crossing around , and its right child is . 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 , each independently either false or true. On the set of finite formal expressions freely built from a blank , variables indexed by all natural numbers including zero, unary crossing , and ordered binary juxtaposition , the relation is the least relation generated by the following rules, with the same fixed parameters throughout: for every , ; for every , implies ; for every , and imply ; for every , ; for every , ; for every , ; for every , implies ; for every , implies ; for every , implies ; if , then for every , ; if , then for every , ; and for every function and every , implies , where fixes the blank, sends each variable directly to 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 is false, the premise cannot hold, so that conditional rule supplies no generating instances; the corresponding statement applies to 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 , variables indexed by natural numbers including zero, unary crossing , and ordered binary juxtaposition , the relation is defined to be the least relation with all of the following properties: every expression is related to itself; for every , implies ; for every , and imply ; for all expressions , ; for all , ; for every , ; for all , implies ; for all , implies ; for all , implies ; for every , ; for every , ; and for every function into this expression set and every , implies , where fixes the blank, replaces each original variable directly by 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 , where expressions are finite formal trees built from a blank , variables indexed by natural numbers including zero, unary crossing , and ordered binary juxtaposition , validity of the pair means that for every total Boolean assignment , the Boolean values of and are equal. Here the value of is false, the value of is , the value of is the Boolean negation of the value of , and the value of is the Boolean disjunction of the values of and . 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.