Optimizer that performs function inlining, beta-reduction, and dead code elimination.
The Inliner performs several transformations:
'''Beta-reduction''': Replaces function application with direct substitution when safe
'''Identity function inlining''': Eliminates identity functions like λx.x
'''Dead code elimination''': Removes unused lambda parameters when the argument is pure
'''Small value inlining''': Inlines variables, small constants, and builtins
'''Force/Delay elimination''': Simplifies Force(Delay(t)) to t
==Inlining Strategy==
The inliner uses occurrence counting and purity analysis to decide what is safe to inline:
Variables, builtins, and small constants (≤64 bits) can be duplicated safely
Larger values are only inlined if used once
Pure unused arguments are eliminated entirely
==Example==
// Input: (λx. λy. x) 42 100
// After inlining identity and dead code elimination:
// Output: 42
val inliner = new Inliner()
val optimized = inliner(term)
// Check what was optimized
println(inliner.logs.mkString("\n"))
==Implementation Details==
The inliner performs capture-avoiding substitution to prevent variable capture during beta-reduction. It maintains an environment of inlined bindings and recursively processes the term tree.
Value parameters
logger
Logger for tracking inlining operations (defaults to new Log())
Substitutes all free occurrences of variable x with term s in term t, while avoiding variable capture. This is the fundamental operation for beta-reduction.
==Capture Avoidance==
When substituting under a lambda that binds a variable that's free in the replacement term, alpha-conversion (renaming) is performed to avoid capture:
// [x → y](λy. x) would incorrectly become λy. y without alpha-conversion
// With alpha-conversion:
// [x → y](λy. x) → [x → y](λy'. x) → λy'. y
==Bound Variable Handling==
Substitution stops at lambda bindings that shadow the variable:
[x → 42](λx. x) → λx. x // inner x refers to lambda parameter, not substituted
Value parameters
name
The variable name to replace
replacement
The term to substitute in place of the variable
term
The term in which to perform substitution
Attributes
Returns
The term with all free occurrences of name replaced by replacement