Inliner

scalus.uplc.transform.Inliner
See theInliner companion object
class Inliner(logger: Logger = ...) extends Optimizer

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())

Attributes

See also

TermAnalysis.isPure for purity analysis used in dead code elimination

Optimizer for the base optimizer trait

Companion
object
Graph
Supertypes
trait Optimizer
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def apply(term: Term): Term

Applies the optimization to a UPLC term.

Applies the optimization to a UPLC term.

Value parameters

term

The UPLC term to optimize

Attributes

Returns

The optimized UPLC term, semantically equivalent to the input

def inlineConstVarBuiltin(name: String, body: Term, inlining: Term, occurances: Int): Boolean

Determines if a term is safe to inline based on its type and occurrence count.

Determines if a term is safe to inline based on its type and occurrence count.

This is the default inlining policy used by the Inliner. It follows these rules:

  • '''Variables''': Always safe to inline (no duplication cost)
  • '''Small constants''': Safe if ≤64 bits or used once
  • '''Builtins''': Always safe (just references)
  • '''Everything else''': Not safe to inline

Value parameters

body

The lambda body where the variable appears

inlining

The term being inlined in place of the variable

name

The variable name being inlined

occurances

Number of times the variable appears in the body

Attributes

Returns

true if the term should be inlined, false otherwise

def logs: Seq[String]

Returns the log messages from optimization operations.

Returns the log messages from optimization operations.

Each log entry describes an optimization that was applied, useful for debugging and understanding the optimization process.

Attributes

Returns

Sequence of log messages describing applied optimizations

def substitute(term: Term, name: String, replacement: Term): Term

Performs capture-avoiding substitution [x → s]t.

Performs capture-avoiding substitution [x → s]t.

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