03 · Formal Verification & Lean 4 Proofs#
LeanUI combines instant static analysis (running on every edit in <15ms) with real machine-checked Lean 4 theorem proving (running the actual Lean 4 compiler in WebAssembly or natively via Lake).
Two-Tier Verification Architecture#
| Tier | Engine | Latency | What It Checks |
|---|---|---|---|
| Tier 1 · Instant Static Verifier | @leanui/verifier (TypeScript) | ~2–15 ms | Port type compatibility, computed cycles, expression typing, bounded invariant counterexample search, WCAG AA contrast math, i18n locale coverage, View A11y (alt, labels, slider bounds), user proof_rule checks, and incremental Solution contracts. |
| Tier 2 · Real Lean 4 Kernel | @leanui/lean-wasm (Browser WASM) or @leanui/lean-server (Native lake env lean) | ~0.4–1.5 s | Compiles the generated .lean specification through the actual Lean 4.14.0 typechecker & kernel, mechanically verifying every theorem with zero sorry placeholders. |
Choosing the Lean 4 Backend#
In the Verification Console (▤ Console), switch the backend dropdown between:
WASM (Browser)— RunsInit-only Lean 4 inside a persistent browser Web Worker (zero backend needed; works on GitHub Pages).Native (Local)— Connects to the local Vite/Node bridge (@leanui/lean-server) invoking your machine'slake env lean --json.
Click λ Prove in the topbar (or inside the ✓ Proofs panel) to run the Lean 4 kernel on the active specification.
All 14 Built-In Lean 4 Theorems & User-Authored Proof Language#
Every generated Lean 4 specification emits your user-authored Lean 4 declarations (lean_type, lean_tactic, lean_def, lean_theorem) together with a formal MVU model (structure ViewModel, def initState, inductive Action, def update, def Invariant, def rootView) and machine-checked theorems:
| Theorem | Scope | Formal Statement |
|---|---|---|
init_preserves_inv | Initial State Safety | Invariant initState |
step_preserves_inv | Action & Setter Induction | ∀ s a, Invariant s → Invariant (update s a) |
uiSpec.reachable_preserves_inv | Multi-Step Reachability | ∀ trace : List Action, Invariant (runActions initState trace) |
view_well_formed | State-Dependent View Soundness | ∀ s, Invariant s → VNode.wellFormed s rootView (proves slider min < max & value bounds, non-empty alt text, accessible labels, valid structure across all 37 View kinds) |
reachable_view_well_formed | Reachable View Soundness | ∀ trace, VNode.wellFormed (runActions initState trace) rootView |
update_deterministic | Reducer Determinism | ∀ s a s₁ s₂, update s a = s₁ → update s a = s₂ → s₁ = s₂ |
styles_wcag_aa (when style nodes exist) | Design System Contrast Proof | StyleBindingsValid styleSheet usedStyleTokens (proves every style token meets WCAG AA luminance contrast & every bound view token exists) |
resources_resolved (when resource nodes exist) | i18n Resource Coverage Proof | ResourceBundleValid locales entries usedKeys (proves every locale defines every key & every view resourceKey resolves) |
data_sources_valid (when datasource nodes exist) | Data Source Schema & Endpoint Proof | DataSourcesValid dataSources (proves non-empty schemas, unique fields, valid online endpoints, and defined primary keys) |
foreign_keys_valid (when FKs exist) | Relational Foreign Key Integrity Proof | ForeignKeysValid foreignKeyChecks (proves every MS Access / SQL foreign key references an existing table & column and every seed record satisfies referential integrity) |
data_queries_valid (when data_query nodes exist) | Declarative Query Schema Proof | DataQueriesValid dataQueries (proves every wired data_query has a valid primary source, join keys, and schema-resolved sort/search/aggregate fields) |
control_templates_valid (when control_template or template-scope nodes exist) | Templated Control, Root Container & Resource Proof | ControlTemplatesValid controlTemplates (proves every template has ≥ 1 visual part or nested View control, every {tpl:prop} across root/parts/nested controls is declared, and every {res:token} resolves in the wired Resource Dictionary; paired with static TPL001–TPL005 checks including grid columns & scroll min-height bounds — see Chapter 08) |
state_machines_valid (when state_machine nodes exist) | Finite State Machine Transition Soundness | FSMsValid stateMachines := by decide (proves every state_machine declares ≥ 1 state, its initialState belongs to states, and every transition's from/to endpoints are valid declared states with non-empty event names) |
validators_valid (when validator nodes exist) | Multi-Rule Validator Completeness | ValidatorsValid validators := by decide (proves every validator declares ≥ 1 rule and every rule carries non-empty label, expr, and error message) |
Authoring Full Lean 4 Proofs & App-Callable Functions#
Use the Lean 4 Proof Language nodes directly on the canvas (see the Lean 4 Proof & App Workbench preset):
lean_type: Define algebraicinductivetypes,structurerecords,classtypeclasses, orabbrevaliases withderivingclauses.lean_def: Define verified pure functions (def,partial def,noncomputable def,instance) that are simultaneously proved by yourlean_theoremnodes AND called directly inside Appcomputed,predicate, andactionexpressions (e.g.,used = safeConsume(used, 10, tierLimit(tier))).lean_theorem: Write standalone or app-linkedtheorem,lemma,example, oraxiomblocks using either raw Lean 4 tactics/terms OR the Visual Tactic Proof Builder (ƒ) (intro,simp,rw,cases,induction,split,apply,exact,have,omega,decide,rfl,constructor). Insideƒ, clickλ Check Proofto run the live Lean 4 WASM/Native kernel on your draft tactic script and inspect goal diagnostics, or click ⚡ Quick-Fix Tactics (+ omega,+ simp,+ decide,+ rfl,+ intro h,+ cases) to splice tactics directly before∎ QED. Also supportscustomImportsfor referencing shared Lake modules in native mode.lean_tactic: Define reusablemacro,syntax,notation, and@[simp]tactic helpers.
User-Defined Verification Rules (proof_rule)#
In addition to built-in invariants, you can author custom mathematical theorems visually using proof_rule nodes (click + Proof Rule in the ✓ Proofs panel or Palette):
ViewConstraint— Proves a state-conditioned UI/ViewModel property across all invariant-preserving states (∀ s, Invariant s → Pre s → Conclusion s).HoareTriple(ruleKind: 'transition') — Proves a Hoare triple{Invariant ∧ Pre} Action {Conclusion}for a specific target Action constructor (targetActionName).ReachableGoal(ruleKind: 'reachability') — Proves a safety conclusion holds across all reachable action traces (theorem reachable_rule_<name>).
Each proof_rule supports:
- Precondition Expression (
preconditionExpr, defaults totrue) - Conclusion Expression (
expr, editable via text or the Visual Logic Node Editorƒ) - Live Verification: Checked immediately by the static verifier, evaluated frame-by-frame in the Runtime Timeline Debugger, and compiled into a formal Lean 4 theorem verified by the Lean kernel.
Verified Styles (Inheritance & WCAG AA/AAA) & Parameterized ICU i18n Resources#
- Style Nodes (
style):- Define
tokenName, optional parentextendsToken(style token inheritance with automatic cycle/missing-parent detectionSTYLE004),bgHex,fgHex,borderHex,fontSizePx,radiusPx,paddingPx, andminContrastX10(default45=4.5:1WCAG AA;70=7.0:1WCAG AAA). - Wire the
styleoutput port into any View node'sstyleinput port. - Both
@leanui/verifierand the Lean 4styles_wcag_aatheorem evaluate the resolved inherited style and reject insufficient contrast or dangling token references.
- Define
- Resource Nodes (
resource):- Define supported
locales(e.g.,["en", "de", "pl"]), keyed translationentrieswith parameterized{name}& ICU pluralization ({count, plural, =0 {Empty} one {# item} other {# items}}), and design tokens supporting semantic{res:alias}→{res:base}chains. @leanui/verifierenforces full locale coverage (RES001), valid view keys (RES002), cross-locale ICU placeholder parity (RES003), and token alias cycle detection (RES004) alongside the Lean 4resources_resolvedtheorem.- Switch locales live inside the Live Preview dropdown (
🌐 EN/DE/PL) and test responsive layouts acrossDesk | Tab | Mobpreview frames.
- Define supported
Using the Proofs Matrix & Interactive Proof Visualizer (✓ Proofs)#
The ✓ Proofs dockable panel gives both a real-time theorem matrix and an Interactive Lean 4 Proof Visualizer with 5 sub-views:
Matrix: Complete checklist of built-in theorems, userproof_rulecards, WCAG style contrast proofs, and i18n locale coverage with◫(Proof Tree),▶(Infoview Step-Through),λ(jump-to-Lean),●(breakpoints), andƒ(Visual Logic Editor).Blueprint(Lean Blueprint–style SVG DAG): Visualizes the full 4-stage validation pipeline (1. Static Gate→2. Base Proofs→3. Induction & Rules→4. Lean 4 Kernel QED); click any stage or theorem to highlight its formal dependency ancestry.Proof Tree(Paperproof-style Deduction Tree): Displays Given Hypotheses, Tactic Derivation rules, branching subgoals across every Action/Setter/View/FK obligation, and✓ QED/✗ Refutedleaves.Infoview(Lean 4 Tactic Goal Step-Through): Steps line-by-line (◀ Prev,▶ Auto-Step,Next ▶) through the actual generated Lean 4 theorem showing live Hypotheses (+diff highlights),⊢Turnstile goals, and plain-English explanations ofintro,cases,simp,omega, anddecide.Induction(State-Space Safety Envelope): Interactive SVG diagram showinginit_preserves_inv(base case),step_preserves_inv(inductive transition arcs staying inside the safe invariant boundary), and your live Preview state cursor.