Hybrid "Pincer" Verification
An optimisation that is fast but wrong is a critical flaw. X-Lang guarantees the mathematical soundness of its aggressive transformations using a two-pronged formal verification strategy.
The Limits of Testing
"Program testing can be used to show the presence of bugs, but never to show their absence."
— Edsger W. Dijkstra
Modern compilers perform millions of complex transformations. A single error in the logic of an optimization pass can lead to silent data corruption, security vulnerabilities, or non-deterministic behavior.
While empirical testing is essential, it covers only a minuscule fraction of all possible valid programs. To provide a true guarantee of correctness—a proof that our optimizations are sound for all possible programs that satisfy the semantic contracts—we must turn to formal mathematical methods.
The Danger of Unsound Optimisation
Let P be a program.
Let T be a compiler transformation.
Let ⟦ · ⟧ be the mathematical meaning (semantics).
If there exists even one program where the transformed meaning differs from the original, the compiler pass is fundamentally flawed.
The Methodology
How We Guarantee Soundness
Full end-to-end compiler verification (like CompCert) often restricts the aggressiveness of optimizations. X-Lang adopts a pragmatic "Pincer" methodology to verify just the semantic bridge.
Language Semantics
Lean 4 Theorem Prover
The top pincer uses the Lean 4 proof assistant to formally prove that the X-Lang front-end's semantic analysis is sound. It proves that when the compiler validates a keyword (like @unique), the program genuinely possesses the mathematical property of non-aliasing. This establishes a "Certificate of Truth."
IR Translation Validation
Alive2 Validator
The bottom pincer uses Alive2 to prove that our compiler's transformations at the LLVM IR level are correct refinements. Taking the Lean certificate as a formal precondition, Alive2 verifies that adding attributes like noalias does not introduce any invalid behavior into the intermediate representation.
The Verification Matrix
From high-level intent to formally verified LLVM transformation.
| X-Lang Keyword | Formal Guarantee (Lean 4) | LLVM Transformation (Alive2) |
|---|---|---|
| @pure | Sound Purity Analysis (purity_checker_sound) | Add readonly / readnone attributes |
| @unique | Non-Interference (unique_move_point_does_not_interfere) | Add noalias parameter attribute |
| @layout(SoA) | Semantic Equivalence (layout_transformation_is_correct) | Transform aggregate types & GEP instructions |
| @parallel | Equivalence of sequential & parallel final state | Re-associate arithmetic operations for reduction |
| @static | Compile-time specialisation == runtime evaluation | Constant folding & branch pruning (Dead Code Elim.) |
Formal Proof Library
The complete mathematical foundation for all 10 semantic contracts. For each keyword, explore the high-level language soundness proof in Lean 4 alongside its corresponding low-level IR translation validation in Alive2.
@layout(SoA)
Data-Oriented Design for Vectorisation
Semantic Equivalence of Data Transformation
Proves that extracting a field from the original Array-of-Structures (AoS) representation is mathematically equivalent to accessing the same field from the compiler-transformed Structure-of-Arrays (SoA) representation.
Store-to-Load Forwarding in SoA
Validates that the structured SoA memory accesses allow the compiler to safely forward stored values directly to subsequent loads, bypassing memory.
@unique
Eliminating Alias Analysis Pessimism
Non-Interference Guarantee
Proves the core property of non-aliasing: a mutation operation via a unique pointer on the heap is guaranteed to be completely unchanged when observed from any other distinct pointer.
Preventing Unsafe Forwarding (Counterexample)
Demonstrates that without the 'noalias' guarantee, store-to-load forwarding is invalid. Alive2 generates a counterexample, proving the semantic contract's absolute necessity.
@invariant
Axiomatic Check Elimination
Preservation of Invariant Properties
Proves that if an axiomatic property (like length < capacity) holds true initially, subsequent operations structurally preserve that invariant across the data structure's lifetime.
Branch Pruning via llvm.assume
Shows how injecting an 'llvm.assume' intrinsic based on the invariant axiom allows the optimizer to formally prove a defensive panic path is unreachable dead code.
@pure
Guaranteeing Referential Transparency
Sound Purity Analysis
Proves that the compiler's efficient AST traversal (is_pure_checker) correctly implies the abstract, logical specification of referential transparency (CallsOnlyPureFunctions).
Lowering to readnone Attribute
Validates that appending the 'readnone' attribute to a pure function's LLVM IR correctly restricts its allowed behaviors without altering semantics, safely enabling CSE/DCE.
@memoise
Automated Dynamic Programming
Observational Equivalence of Memoisation
Guarantees that for pure deterministic functions, intercepting execution with a stateful caching mechanism yields the exact same observable results as unmemoised re-computation.
Safe Function Attributes for Wrappers
Verifies that appending the 'readnone' attribute to a pure, memoised function's LLVM IR correctly restricts its allowed behaviors without altering semantics.
@static
Partial Evaluation via Function Specialisation
Compile-Time Specialisation is Equivalent to Runtime Evaluation
Proves that partially evaluating a function with a known static compile-time constant is logically indistinguishable from invoking the generic function with that same value at runtime.
Constant Folding & Branch Pruning
Proves that cloning a function and replacing the static parameter with a constant safely allows the optimizer to fold branches and prune dead code blocks without altering valid paths.
@parallel
Programmer-Guided Fork-Join Concurrency
Equivalence of Sequential & Parallel Final State
Formally guarantees that the final state of an array after a sequential loop of updates is completely identical to the final state produced by a data-independent parallel mapping.
Parallel-Friendly Association
Models the re-grouping of a parallel reduction. Alive2 proves that transforming sequential additions into a tree-like structure is a safe optimization.
@become
Guaranteed Tail-Call Optimisation (TCO)
Algebraic Proof of Iterative vs Recursive Tail Calls
A formal induction proof demonstrating that resolving a tail-recursive function iteratively provides the identical mathematical result, justifying the TCO optimization.
TCO to Loop Transformation
Validates that lowering a guaranteed tail call into an explicit loop with phi nodes preserves exact program semantics while executing in O(1) stack space.
@multi
Extensible and Efficient Multiple Dispatch
Correctness of the Generated Dispatcher
Proves that replacing chained virtual functions with the compiler-generated multi-resolver correctly dispatches to the exact same monomorphic implementations.
Switch / Jump Table Lowering
Proves that collapsing a complex tree of if/else dynamic type checks into a single 64-bit key and a jump-table (switch) preserves dispatch semantics perfectly.
@no throws
Managing the High Cost of Exceptions
Guaranteeing Nounwind Control Flow
Proves that a program exclusively comprising guaranteed non-throwing functions resolves cleanly, justifying the absence of invoke structures and landing pads.
Safe 'nounwind' Optimization
Validates that a function guaranteed not to throw can be safely annotated with 'nounwind', allowing callers to generate simple 'call' instructions instead of 'invoke'.
