Papers
Topics
Authors
Recent
Search
2000 character limit reached

Instruction Substitution: A Unified Principle

Updated 12 July 2026
  • Instruction substitution is a technique for replacing program instructions with semantically equivalent expressions while preserving their intended behavior.
  • It underpins processes such as source-level refactoring, lambda-calculus evaluation, and compiler instruction selection through formal rewriting and normalization.
  • This approach integrates explicit substitution steps with context-sensitive rewrites to maintain well-formedness and guarantee semantic invariance.

Searching arXiv for recent and relevant papers on instruction substitution, substitution-based refactoring, instruction selection rewrites, and related formal substitution frameworks. Instruction substitution denotes a family of semantics-preserving transformations in which one instruction, instruction pattern, or named program entity is replaced by another expression, sequence, or structured implementation, typically under an explicit equivalence relation and often followed by normalization or simplification. Across the literature, the term spans several abstraction levels: source-level refactoring formulated as substitution plus rewriting; λ-calculus substitution as the operational core of application; compiler instruction selection as rewrite-rule synthesis between IR and ISA patterns; low-level instruction-sequence transformations in Program Algebra; heuristic successor restriction in inductive programming via instruction digrams; and semantic update operators in modal substitution logics (Thompson et al., 2022). A unifying theme is the separation between an equivalence-bearing replacement step and a context-sensitive mechanism that restores well-formedness, normal form, or executability.

1. Formal characterizations and scope

A concise formulation appears in the refactoring literature as

Refactoring  =  Substitution  +  Rewriting.\text{Refactoring} \;=\; \text{Substitution} \;+\; \text{Rewriting}.

Under this view, a wide class of refactorings is specified by giving an implementation of an old entity in terms of a new entity, substituting references to the old entity by that implementation, and then applying semantics-preserving rewrites such as beta-reduction, eta-reduction, or desugaring (Thompson et al., 2022). The substitution step is described as language-independent because it can be formulated over generic terms with variables, applications, lambdas, and names, whereas rewriting is language-specific because it depends on concrete syntax, currying conventions, closure semantics, and idiomatic surface forms (Thompson et al., 2022).

In the λ-calculus, substitution is the execution principle behind application. The paper on the Curry school fixes pure, type-free λ-terms by

A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),

introduces environments σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D, and interprets application by changing environments, thereby making substitution the operational reflection of environment update (Kamareddine, 2024). The naïve replacement operation called grafting,

A{v:=B},A\{v := B\},

formalizes direct syntactic replacement, but the paper shows that it is unsound because of variable capture. Safe substitution is therefore defined by a replacement operation

Av:=BA\langle\langle v := B\rangle\rangle

that renames binders before substitution when needed, and β-reduction is then defined as

(λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.

This makes “instruction substitution” exact at the term level: execute a call by substituting an actual argument for a formal parameter while preserving binding structure (Kamareddine, 2024).

A different formalization appears in modal logic, where substitution is not merely syntactic replacement but a semantic update of valuations in Kripke models. Modal Substitution Logic extends modal logic with operators of the form

p:=ψφ,{p:=\psi}\varphi,

interpreted by changing the valuation of propositional letter pp to the truth set of ψ\psi, and Modal Iterative Substitution Logic adds

(p:=ψ)φ,{(p:=\psi)^*}\varphi,

quantifying over finite iteration counts of that update (Tu et al., 16 Jul 2025). Here substitution is literally an instruction on models: “set A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),0 to the result of computing A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),1.”

At the compiler level, instruction substitution becomes rewrite-rule application between small programs. An instruction-selection rule is modeled as a pair

A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),2

where both sides are loop-free programs over component libraries and are required to be functionally equivalent for all inputs (Daly et al., 2024). This generalizes classical many-to-one selection to one-to-many and many-to-many substitutions, and makes instruction substitution a formally verified relation between IR and ISA patterns.

2. Substitution, binding, and correctness conditions

Capture avoidance is the central correctness condition in calculi with binders. The λ-calculus paper emphasizes the apparent circularity that α-congruence seems to require substitution while substitution seems to require α-congruence, and resolves it by constructing α-equivalence in two ways and proving coincidence (Kamareddine, 2024). One route defines α-reduction using safe replacement: A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),3 while another route defines a constrained α′ using grafting only in cases guaranteed to be safe. The two coincide, yielding a robust equivalence relation A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),4 “up to bound names” (Kamareddine, 2024). The resulting standard substitution

A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),5

is therefore justified as a shorthand for a more elaborate capture-avoiding construction. This directly conditions any substitution-based program transformation involving binders, inlining, macro expansion, or code motion.

The Agda development on substitution without duplication provides a typed generalization of this concern. It replaces separate definitions for variables versus terms and renamings versus substitutions by a single family indexed by a small sort lattice: p:=ψφ,{p:=\psi}\varphi,1 and a single substitution operator

A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),6

This unifies renaming and substitution, and proves identity and composition laws once rather than in several duplicated variants (Altenkirch et al., 14 Oct 2025). The paper then packages the machinery into a simply typed category with families, where substitutions are context morphisms, terms form a presheaf, and context extension satisfies β/η-style equations. A plausible implication is that instruction substitution in typed languages can often be organized more economically by explicitly parameterizing over “kind of substitutable object” rather than duplicating operations at each syntactic layer (Altenkirch et al., 14 Oct 2025).

Correctness decompositions in refactoring and instruction selection echo this structure. For refactoring, each specific transformation requires a semantic equivalence

A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),7

while the rewrite system must be shown semantics-preserving at the language level (Thompson et al., 2022). For instruction selection, equivalence is enforced by a quantified SMT formula requiring output equality of IR-side and ISA-side programs under equal inputs (Daly et al., 2024). In both settings, substitution is admissible only when a compositional semantic invariant has been established.

3. Program transformation and refactoring frameworks

The paper “Refactoring = Substitution + Rewriting” develops instruction substitution most explicitly at the source level. A rename example in Erlang begins with p:=ψφ,{p:=\psi}\varphi,2 and is specified by a new definition p:=ψφ,{p:=\psi}\varphi,3 together with the semantic equation p:=ψφ,{p:=\psi}\varphi,4 which is not presented as a new program definition but as an implementation relation. Substituting A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),8 by A::=v(v.A)(AA),A ::= v \mid (v.A) \mid (A\,A),9 in the body of σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D0 yields anonymous-function applications, which are then simplified by beta-reduction to p:=ψφ,{p:=\psi}\varphi,5 The same scheme handles function generalization, argument reversal, API migration, and constructor refactorings (Thompson et al., 2022).

The central claim is not limited to renaming. In API migration, an adapter layer defines deprecated API calls in terms of new ones, then substitution replaces call sites with adapter bodies, after which a rewrite system removes dead branches, redundant wrappers, and trivial handlers (Thompson et al., 2022). The correctness burden splits modularly: per-refactoring equivalence proofs for the adapter equations, and per-language proofs that the rewrite rules preserve behavior (Thompson et al., 2022). Experimental implementations cited there include Wrangler for Erlang and Rotor for OCaml, both using a generic substitution strategy together with language-specific rewrite rules (Thompson et al., 2022).

A related but lower-level transformation discipline appears in “Instruction sequences with dynamically instantiated instructions” (0711.4217). That paper extends Program Algebra with proto-instructions whose runtime meaning is determined by a register file state via

σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D1

It then provides two eliminations of these dynamic instructions into ordinary instruction sequences. The first simply maps each proto-instruction σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D2 to a service call σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D3, with a method-to-action translator service σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D4 converting the method into the appropriate basic action. The second expands each proto-instruction into a larger static decision tree that tests register contents and branches to the corresponding concrete instruction (0711.4217). The theorem

σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D5

states that the two substitution schemes are behaviorally equivalent (0711.4217). This is an exact example of replacing an extended instruction by either a service-mediated or code-expanded equivalent.

Program Algebra also supplies equational frameworks for substitution among instruction fragments. In periodic single-pass instruction sequences, single-pass congruence and structural congruence determine when one finite sequence with repeaters and jumps may replace another without changing the produced instruction stream or extracted thread (0810.1151). Axioms such as

σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D6

and

σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D7

permit normalization of periodic fragments, while structural congruence reduces chained jumps and minimizes jumps into periodic segments (0810.1151). This is substitution as algebraic replacement inside a semigroup of instruction sequences.

4. Compiler-level instruction substitution and rule synthesis

In compiler back ends, instruction substitution is classically realized as instruction selection. The SMT-based framework for synthesizing lowest-cost rewrite rules models each instruction as a component

σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D8

with bit-vector semantics in QF_BV, and each program as a well-formed DAG of such components linked by location variables (Daly et al., 2024). A rewrite rule is a pair of equivalent loop-free programs, one over IR components and one over ISA components: σENV=VD\sigma \in ENV = \mathcal V \to \mathcal D9 Functional equivalence is encoded by a quantified verification formula requiring equal outputs whenever the program inputs are equal (Daly et al., 2024).

The synthesis procedure generalizes prior work by allowing multi-instruction patterns on both sides. It therefore supports many-to-one, one-to-many, and many-to-many substitutions rather than only single-instruction ISA patterns (Daly et al., 2024). The search space, however, is dominated by redundant rules. The paper formalizes several equivalence relations—commutative equivalence, same-kind equivalence, data-dependency equivalence, and input renaming—to exclude duplicate rules, and defines composite rules as those reproducible by composition of previously synthesized smaller rules (Daly et al., 2024). It further sorts ISA multisets by cost and blocks any higher-cost realization of an already covered IR pattern, yielding only lowest-cost substitutions under a chosen cost metric such as code size or energy (Daly et al., 2024).

The quantitative effect is pronounced. For ISA 1a up to IR size 2 and ISA size 3, 96.2% of synthesized rules are duplicates or composites, and 99.7% are high-cost; for ISA 2 up to IR size 3 and ISA size 2, 99.5% are duplicates/composites and 99.7% are high-cost (Daly et al., 2024). The optimized algorithms yield synthesis speed-ups of up to A{v:=B},A\{v := B\},0 and A{v:=B},A\{v := B\},1 for the unique-rule and lowest-cost variants respectively (Daly et al., 2024). These figures are not incidental: they indicate that instruction substitution libraries in realistic compilers are dominated by equivalence-class structure and cost redundancy, so semantic blocking is part of the transformation problem itself.

A different compiler-adjacent notion appears in Bergstra’s treatment of steering fragments. There, a composed steering instruction A{v:=B},A\{v := B\},2 can be transformed into a fragment of atomic steering instructions and jumps, preserving thread semantics under short-circuit evaluation and reactive valuations (Bergstra, 2010). For example,

A{v:=B},A\{v := B\},3

can be replaced by

A{v:=B},A\{v := B\},4

This substitution is semantics-preserving only under strict conditions on evaluation order, side effects, and jump locality (Bergstra, 2010). The paper also emphasizes that algebraic rewrites that duplicate or reorder tests may fail under free valuation semantics, so not every propositional equivalence yields a valid instruction substitution in a side-effectful setting (Bergstra, 2010).

5. Instruction sequences, algorithmic encodings, and cost trade-offs

Instruction substitution is also a way of comparing alternative low-level realizations of the same algorithmic function. The multiplication study in Program Algebra constructs finite instruction sequences over Boolean registers for long multiplication and Karatsuba multiplication (Bergstra et al., 2013). The long-multiplication sequence A{v:=B},A\{v := B\},5 has exact length

A{v:=B},A\{v := B\},6

whereas the Karatsuba sequence A{v:=B},A\{v := B\},7 has asymptotic length

A{v:=B},A\{v := B\},8

The paper concludes that the sequence expressing long multiplication is longer than the one expressing Karatsuba multiplication only if the bit-length is greater than A{v:=B},A\{v := B\},9, and that Av:=BA\langle\langle v := B\rangle\rangle0 for all Av:=BA\langle\langle v := B\rangle\rangle1 (Bergstra et al., 2013). This gives a concrete threshold beyond which substituting one algorithmic instruction sequence for another is size-beneficial.

The same paper then considers a variant with backward jumps. A loop-based long-multiplication sequence Av:=BA\langle\langle v := B\rangle\rangle2 has length

Av:=BA\langle\langle v := B\rangle\rangle3

hence Av:=BA\langle\langle v := B\rangle\rangle4, and is shorter than both forward-only long multiplication and the forward-only Karatsuba construction for sufficiently small thresholds: it is shorter than the other two if the length of the bit strings involved is greater than Av:=BA\langle\langle v := B\rangle\rangle5 (Bergstra et al., 2013). This is a particularly sharp example of instruction substitution across control regimes: unrolled forward-only code can be replaced by a loop with backward jumps, preserving the computed function while dramatically changing code length and the associated reasoning principles.

The broader implication is not merely that “better algorithms produce shorter sequences.” Rather, the paper isolates several substitution axes: long versus divide-and-conquer arithmetic, forward-only versus backward-jump control, and register-footprint versus sequence-length trade-offs (Bergstra et al., 2013). The Karatsuba encoding uses substantially more auxiliary registers than long multiplication, even when shorter in instruction length (Bergstra et al., 2013). Thus instruction substitution at this level is a multi-objective transformation problem rather than a single metric optimization.

6. Search heuristics, dynamic systems, and emergent instruction behavior

Not all instruction substitution is exact equivalence at the syntactic or operational level. In inductive programming, “instruction digrams” act as a data-driven restriction on which instructions may follow which others during search (McDaid et al., 2023). A digram is defined as an ordered pair of instruction identifiers corresponding to the direct application of one instruction to the return value of another: Av:=BA\langle\langle v := B\rangle\rangle6 The corpus study over approximately 14.75 million lines of Python from the largest 1000 GitHub repositories found that the distribution of such digrams is highly skewed: over 50% occur only once, over 90% occur 10 or fewer times, and only 6.25% of all possible instruction digrams occur at all, meaning that 93.75% of possible digrams are absent from the sample (McDaid et al., 2023).

Zoea exploits this sparsity as a binary constraint rather than a weighted model: at search depth Av:=BA\langle\langle v := B\rangle\rangle7, an instruction Av:=BA\langle\langle v := B\rangle\rangle8 may be used only if some previously used instruction Av:=BA\langle\langle v := B\rangle\rangle9 forms an observed digram (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.0 within the current instruction subset (McDaid et al., 2023). On top of prior subset restrictions, digrams reduce search space by one to five orders of magnitude depending on subset size and depth, and for subset size 10 allow exploration of search graphs between 1 and 4 levels deeper for the same resources (McDaid et al., 2023). The paper explicitly warns that excluding rare or unseen combinations may eliminate “simpler and innovative solutions” (McDaid et al., 2023). A plausible implication is that instruction substitution here is heuristic successor substitution rather than semantic replacement: the system substitutes a large transition relation by a much smaller empirically sanctioned one.

Modal substitution logic treats substitution as state update rather than code rewrite. In (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.1, the semantics of

(λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.2

updates the valuation of (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.3 to the truth set of (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.4, and in (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.5,

(λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.6

quantifies over some finite number of repeated applications of that instruction (Tu et al., 16 Jul 2025). Under a cleanliness condition, single-step semantic substitution coincides with syntactic replacement: (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.7 Iterative substitution supports dynamic reasoning tasks such as backward induction on finite game boards using formulas like

(λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.8

and the satisfiability problem for (λv.A)BβAv:=B.(\lambda v.A)B \rightarrow_{\overline\beta} A\langle\langle v:=B\rangle\rangle.9 is shown to be p:=ψφ,{p:=\psi}\varphi,0-complete (Tu et al., 16 Jul 2025). This line of work broadens the meaning of instruction substitution from program transformation to logic of repeated semantic commands.

A more recent but conceptually distinct use of the phrase arises in LLMs. “Instruction Following without Instruction Tuning” identifies several substitutes for broad instruction-response finetuning: response-only training, single-task finetuning, and a rule-based product-of-experts adapter (Hewitt et al., 2024). With LIMA responses but no instructions, response-tuned Llama-2-7B achieves a 43.3% AlpacaEval win rate against its explicitly instruction-tuned counterpart, compared with 2.4% for the base model; response-tuned OLMo-7B-Feb2024 reaches 43.7%, compared with 4.7% for the base model (Hewitt et al., 2024). A hand-written rule-based expert that slowly increases EOS probability, penalizes repetition, and uniformly changes 15 token probabilities yields a 24.4% win rate when combined with the pretrained model via a product-of-experts (Hewitt et al., 2024). These are not semantics-preserving substitutions in the classical program-transformational sense, but they do instantiate the idea that broad instruction-following behavior can be obtained by replacing one adaptation regime with another.

7. Misconceptions, controversies, and unifying perspective

A common misconception is that instruction substitution is always a matter of textual replacement. The λ-calculus literature shows that naïve grafting fails because of variable capture, and that correct substitution requires either α-aware replacement or working modulo α-equivalence (Kamareddine, 2024). In refactoring, the replacement step is not itself the final program transformation; language-specific rewriting is required to re-establish idiomatic structure and sometimes even typeable or executable surface form (Thompson et al., 2022). In steering fragments, logically equivalent propositional expressions are not necessarily interchangeable as instructions because repeated evaluation and side effects may alter thread behavior (Bergstra, 2010).

Another misconception is that local equivalence automatically implies contextual substitutability. Program Algebra distinguishes between behavioral equivalence and congruence; for example, two instruction sequences can define the same thread in isolation yet cease to be interchangeable under concatenation (0810.1151). Likewise, compiler rewrite rules must account for structural equivalence classes, input renaming, and composite decompositions, not just semantic output equivalence of one isolated graph (Daly et al., 2024).

There is also a recurrent controversy over the boundary between generic and language-specific reasoning. The refactoring decomposition argues strongly that substitution is generic while rewriting is language-specific (Thompson et al., 2022). The typed substitution literature supports this by showing that identity, composition, weakening, and lifting can often be organized once at a parametric level (Altenkirch et al., 14 Oct 2025). But Bergstra’s work on steering fragments and dynamic instruction instantiation shows that even very low-level substitutions depend intimately on the operational model of tests, jumps, services, and side effects (Bergstra, 2010, 0711.4217). A plausible implication is that “generic substitution” is best understood as generic only relative to an explicitly delimited semantic core.

Across the surveyed work, a stable synthesis emerges. Instruction substitution is a general transformation schema with at least four recurrent components: an object language or machine model; an equivalence notion governing admissible replacement; an implementation mechanism for performing the replacement; and a normalization or control discipline ensuring that the replaced artifact remains well-formed, executable, or searchable. At the source level this appears as substitution plus rewriting; in calculi with binders, as capture-avoiding replacement modulo α; in compilers, as SMT-synthesized rewrite rules over DAGs; in instruction-sequence algebra, as congruence-preserving replacement; in inductive programming, as corpus-grounded restriction of successor choices; and in modal logics, as semantic update operators with optional iteration (Thompson et al., 2022, Daly et al., 2024, Kamareddine, 2024). The breadth of these formulations suggests that “instruction substitution” is less a single technique than a structural principle for relating representations while preserving a chosen semantics or search objective.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Instruction Substitution.