Depth, variable counts, free variables, and one-hole contexts
DefinitionLawsOfForm_CanonicalDefines nesting depth, multiplicity of a selected variable, the finite set of variable indices, finite contexts with exactly one hole, and context filling for the existing expression syntax. Adjacency takes maximum depth; crossing adds one. Blank and variable leaves have depth zero. No canonical-form existence theorem is built into these definitions.
import Definitions.Def_LawsOfForm_Algebra
import Mathlib.Data.Finset.Basic
set_option autoImplicit false
namespace LawsOfForm
def depth : Expr → Nat
| .blank => 0
| .var _ => 0
| .cross a => depth a + 1
| .beside a b => max (depth a) (depth b)
def occurrences (x : Nat) : Expr → Nat
| .blank => 0
| .var y => if y = x then 1 else 0
| .cross a => occurrences x a
| .beside a b => occurrences x a + occurrences x b
def freeVars : Expr → Finset Nat
| .blank => ∅
| .var x => {x}
| .cross a => freeVars a
| .beside a b => freeVars a ∪ freeVars b
inductive ExprContext where
| hole : ExprContext
| cross : ExprContext → ExprContext
| left : ExprContext → Expr → ExprContext
| right : Expr → ExprContext → ExprContext
def fill : ExprContext → Expr → Expr
| .hole, a => a
| .cross c, a => .cross (fill c a)
| .left c b, a => .beside (fill c a) b
| .right b c, a => .beside b (fill c a)
end LawsOfForm
Read-back
What the Lean code literally says, in plain math · Codex (GPT-6)
depth. An expression is a finite syntax tree built from a blank symbol , a variable for any natural number including zero, a unary constructor , and an ordered binary constructor ; these constructors do not identify different trees. The function assigns a natural number to every such expression by , for every , for every expression , and for every pair of expressions . Thus blank and every variable have depth zero, binary constructors alone do not increase depth, and depth counts the largest number of unary constructors on a path through the tree.
occurrences. An expression is a finite syntax tree built from a blank symbol , variables indexed by all natural numbers including zero, a unary constructor , and an ordered binary constructor , with no identifications between distinct trees. For every natural number , the function assigns a natural number to every expression by , when and when , for every expression , and for every pair of expressions . The value counts all variable leaves indexed by , including repeated leaves in either branch and leaves inside any number of unary constructors; it is zero when no such leaf occurs. The index follows exactly the same clauses as every other index, and variable indices have no upper bound.
freeVars. An expression is a finite syntax tree built from a blank symbol , a variable for any natural number including zero, a unary constructor , and an ordered binary constructor , without identifying different trees. The function assigns a finite set of natural numbers to every expression by , for every , for every expression , and for every pair of expressions . Consequently the set contains exactly the variable indices appearing anywhere in the tree, even inside unary constructors, with each index represented only once regardless of repetition; expressions with no variable leaves receive the empty set. Neither constructor binds or removes a variable index.
ExprContext. Expressions are finite syntax trees freely built from a blank symbol, variables indexed by any natural number including zero, a unary constructor , and an ordered binary constructor . A context is a separate inductively generated finite syntax tree with precisely four construction rules: the hole is a context; for any context , is a context; for any context and expression , is a context; and for any expression and context , is a context. These are the only construction rules, and distinct constructor trees remain distinct. Each context contains exactly one hole: the unary constructor retains the hole of its context argument, and each binary constructor retains the hole of its sole context argument while its expression argument contains no holes. The hole alone is allowed, nesting has no fixed finite bound, and each fixed expression argument may be blank or contain arbitrary natural-number-indexed variables.
fill. Expressions are finite syntax trees freely built from a blank symbol , variables for arbitrary natural numbers including zero, a unary constructor , and an ordered binary constructor . Contexts are finite trees built from a single-hole constructor , a unary constructor on a context , a constructor on a context and a fixed expression , and a constructor on a fixed expression and a context ; each context therefore contains exactly one hole. For every context and expression , the function is an expression defined by , , , and . The clauses apply to every choice of the context and expressions , including blank expressions and expressions with repeated or coinciding variable indices. Filling the hole-only context returns its argument, while each enclosing context constructor is retained as the corresponding expression constructor with its fixed expression in the specified left or right position; the clauses perform no simplification or renaming of variables.