---
title: 'Instruction Substitution: A Unified Principle'
url: https://www.emergentmind.com/topics/instruction-substitution
type: topic
---

# Instruction Substitution: A Unified Principle

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 [2211.11550]. 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
\[
\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 [2211.11550]. 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 [2211.11550].

In the λ-calculus, substitution is the execution principle behind application. The paper on the Curry school fixes pure, type-free λ-terms by
\[
A ::= v \mid (v.A) \mid (A\,A),
\]
introduces environments \(\sigma \in ENV = \mathcal V \to \mathcal D\), and interprets application by changing environments, thereby making substitution the operational reflection of environment update [2401.02745]. The naïve replacement operation called grafting,
\[
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
\[
A\langle\langle v := B\rangle\rangle
\]
that renames binders before substitution when needed, and β-reduction is then defined as
\[
(\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 [2401.02745].

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:=\psi}\varphi,
\]
interpreted by changing the valuation of propositional letter \(p\) to the truth set of \(\psi\), and Modal Iterative Substitution Logic adds
\[
{(p:=\psi)^*}\varphi,
\]
quantifying over finite iteration counts of that update [2507.12320]. Here substitution is literally an instruction on models: “set \(p\) to the result of computing \(\psi\).”

At the compiler level, instruction substitution becomes rewrite-rule application between small programs. An instruction-selection rule is modeled as a pair
\[
\mathcal R := (P^{IR}, P^{ISA}),
\]
where both sides are loop-free programs over component libraries and are required to be functionally equivalent for all inputs [2405.06127]. 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 [2401.02745]. One route defines α-reduction using safe replacement:
\[
(\alpha)\quad \lambda v.A \rightarrow_\alpha \lambda v'.A\langle\langle v := v'\rangle\rangle
\quad\text{where } v' \notin FV(A),
\]
while another route defines a constrained α′ using grafting only in cases guaranteed to be safe. The two coincide, yielding a robust equivalence relation \(\equiv\) “up to bound names” [2401.02745]. The resulting standard substitution
\[
A[v:=B]
\]
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:
```agda
data Sort : Set where
  V      : Sort
  T>V    : (s : Sort) → IsV s → Sort

pattern T = T>V V isV
```
and a single substitution operator
\[
\_[\_] : \Gamma ⊢[q] A \to \Delta ⊢s[r] \Gamma \to \Delta ⊢[q \sqcup r] A.
\]
This unifies renaming and substitution, and proves identity and composition laws once rather than in several duplicated variants [2510.12304]. 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 [2510.12304].

Correctness decompositions in refactoring and instruction selection echo this structure. For refactoring, each specific transformation requires a semantic equivalence
\[
\text{old} \equiv \text{impl}_{\text{new}},
\]
while the rewrite system must be shown semantics-preserving at the language level [2211.11550]. For instruction selection, equivalence is enforced by a quantified SMT formula requiring output equality of IR-side and ISA-side programs under equal inputs [2405.06127]. 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
```erlang
f(X) -> X+1.
g(Y) -> f(Y+2) - f(Y-2).
```
and is specified by a new definition
```erlang
h(X) -> X+1.
```
together with the semantic equation
```erlang
f = fun(X) -> h(X) end
```
which is not presented as a new program definition but as an implementation relation. Substituting \(f\) by \(\lambda X.\, h(X)\) in the body of \(g\) yields anonymous-function applications, which are then simplified by beta-reduction to
```erlang
g(Y) -> h(Y+2) - h(Y-2).
```
The same scheme handles function generalization, argument reversal, API migration, and constructor refactorings [2211.11550].

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 [2211.11550]. The correctness burden splits modularly: per-refactoring equivalence proofs for the adapter equations, and per-language proofs that the rewrite rules preserve behavior [2211.11550]. Experimental implementations cited there include Wrangler for Erlang and Rotor for OCaml, both using a generic substitution strategy together with language-specific rewrite rules [2211.11550].

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
\[
\theta : \BPInstr \times (\mapof{[1,\maxr]}{[0,\maxn]}) \to \BAct.
\]
It then provides two eliminations of these dynamic instructions into ordinary instruction sequences. The first simply maps each proto-instruction \(e\) to a service call \(\rfdt.e\), with a method-to-action translator service \(\RFDT\) 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
\[
\forall P \in \TProg_\sPGLDdii:\quad \extr{P}_\sPGLDdii = \extr{P}'_\sPGLDdii
\]
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
\[
(u_1;\dots;u_n)^m;\backslash\#mn = u_1;\dots;u_n;\backslash\#n
\]
and
\[
\backslash\#n;X = \backslash\#n
\]
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
\[
K_k := (\mathbf{I}_k, O_k, \phi_k(\mathbf{I}_k, O_k)),
\]
with bit-vector semantics in QF\_BV, and each program as a well-formed DAG of such components linked by location variables [2405.06127]. A rewrite rule is a pair of equivalent loop-free programs, one over IR components and one over ISA components:
\[
\mathcal R := (P^{IR}, P^{ISA}).
\]
Functional equivalence is encoded by a quantified verification formula requiring equal outputs whenever the program inputs are equal [2405.06127].

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 [2405.06127]. 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 [2405.06127]. 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 [2405.06127].

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 [2405.06127]. The optimized algorithms yield synthesis speed-ups of up to \(768\times\) and \(4004\times\) for the unique-rule and lowest-cost variants respectively [2405.06127]. 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 \(+\varphi\) can be transformed into a fragment of atomic steering instructions and jumps, preserving thread semantics under short-circuit evaluation and reactive valuations [1010.2850]. For example,
\[
X ; +(\neg a \land_{\text{seq}} (b \lor_{\text{seq}} c)) ; u ; Y
\]
can be replaced by
\[
X ; +a ; \#5 ; +b ; \#2 ; +c ; u ; Y.
\]
This substitution is semantics-preserving only under strict conditions on evaluation order, side effects, and jump locality [1010.2850]. 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 [1010.2850].

## 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 [1312.1529]. The long-multiplication sequence \(\nm{LMUL}_N\) has exact length
\[
\len(\nm{LMUL}_N) = 36N^2 + 24N + 2,
\]
whereas the Karatsuba sequence \(\nm{KMUL}_N\) has asymptotic length
\[
\Theta(N^{\log_2(3)}) = \Theta(N^{1.5849\ldots}).
\]
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 \(2^8\), and that \(\len(\nm{LMUL}_N) > \len(\nm{KMUL}_N)\) for all \(N > 2^{13}\) [1312.1529]. 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 \(\LMULiii{N}\) has length
\[
\len(\LMULiii{N}) = 66N + 8\floor{\log_2(N)} + 13,
\]
hence \(\Theta(N)\), 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 \(2\) [1312.1529]. 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 [1312.1529]. The Karatsuba encoding uses substantially more auxiliary registers than long multiplication, even when shorter in instruction length [1312.1529]. 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 [2305.13347]. 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:
\[
[F_1, F_2].
\]
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 [2305.13347].

Zoea exploits this sparsity as a binary constraint rather than a weighted model: at search depth \(k>1\), an instruction \(F_2\) may be used only if some previously used instruction \(F_1\) forms an observed digram \([F_1,F_2]\) within the current instruction subset [2305.13347]. 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 [2305.13347]. The paper explicitly warns that excluding rare or unseen combinations may eliminate “simpler and innovative solutions” [2305.13347]. 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 \(MSL\), the semantics of
\[
{p:=\psi}\varphi
\]
updates the valuation of \(p\) to the truth set of \(\psi\), and in \(MISL\),
\[
{(p:=\psi)^*}\varphi
\]
quantifies over some finite number of repeated applications of that instruction [2507.12320]. Under a cleanliness condition, single-step semantic substitution coincides with syntactic replacement:
\[
{p:=\psi}\varphi \leftrightarrow \varphi[\psi/p].
\]
Iterative substitution supports dynamic reasoning tasks such as backward induction on finite game boards using formulas like
\[
p := \Box\bot;\; (p := p \lor \Box\Diamond p)^*,
\]
and the satisfiability problem for \(MISL\) is shown to be \(\Sigma^1_1\)-complete [2507.12320]. 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 large language models. “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 [2409.14254]. 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 [2409.14254]. 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 [2409.14254]. 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 [2401.02745]. 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 [2211.11550]. In steering fragments, logically equivalent propositional expressions are not necessarily interchangeable as instructions because repeated evaluation and side effects may alter thread behavior [1010.2850].

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 [2405.06127].

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 [2211.11550]. The typed substitution literature supports this by showing that identity, composition, weakening, and lifting can often be organized once at a parametric level [2510.12304]. 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 [1010.2850][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 [2211.11550][2405.06127][2401.02745]. 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.

Source: https://www.emergentmind.com/topics/instruction-substitution