Documentation

ISAR.Futamura

Partial evaluation and Futamura projections #

Honest formulation (Jones/Gomard/Sestoft): the three projections follow from the mix equation once the specializer is reflected as an object-level term (specTerm) with selfApp. Mix alone admits a trivial specializer; nontriviality is a separate obligation.

def ISAR.specialize (t : ITerm) (static_env : NatOption ITerm) :

Meta-level specializer (partial evaluator) replacing static variables.

Equations
Instances For
    def ISAR.pair (s d : ITerm) :

    Pairing of terms as a binary application spine.

    Equations
    Instances For
      def ISAR.Coherent (env : NatITerm) (static_env : NatOption ITerm) (dynamic_env : NatITerm) :

      Coherence condition relating full environment and partial environments.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem ISAR.subst_env_preserves_step {t u : ITerm} (env : NatITerm) (h : IStep t u) :
        IStep (subst_env t env) (subst_env u env)
        theorem ISAR.subst_env_preserves_red {t u : ITerm} (env : NatITerm) (h : IRed t u) :
        IRed (subst_env t env) (subst_env u env)
        theorem ISAR.futamura_first (t : ITerm) (env : NatITerm) (static_env : NatOption ITerm) (dynamic_env : NatITerm) (h_coh : Coherent env static_env dynamic_env) :
        subst_env (specialize t static_env) dynamic_env = subst_env t env

        Mix equation at the subst layer (first Futamura projection / specialization soundness).

        structure ISAR.PESetup :

        Setup for object-level Futamura projections. spec is the meta specializer; specTerm is its reflection as an ITerm; mix is the characterizing equation; selfApp says evaluating specTerm implements spec.

        TrivialPE is a sorry-free non-optimizing instance (¬ Nontrivial, ¬ JonesOptimal). OptimizingPE is a sorry-free fragment specializer with proved Nontrivial (identity / konstβ folds + tagged residual fallback). JGS_PE is online PE for the whole IStep signature (Nontrivial, ¬ JonesOptimal). Jones-optimality is the cost criterion (JonesOptimal); a toy self-interpreter pair JonesIdPE meets it. Full Jones–Gomard–Sestoft 1993 polyvariant BTA / cogen remains open; specTerm is still swap, not an encoding of jgs_spec / offline_spec. The projections below are the mix instantiations.

        Instances For
          theorem ISAR.futamura_second (S : PESetup) (int src : ITerm) :
          S.eval (S.spec S.specTerm int) src = some (S.spec int src)

          Second Futamura projection: specializing the specializer w.r.t. an interpreter.

          theorem ISAR.futamura_third (S : PESetup) (int : ITerm) :
          S.eval (S.spec S.specTerm S.specTerm) int = some (S.spec S.specTerm int)

          Third Futamura projection: self-application yields a compiler generator.

          Cost measure for nontriviality (term size).

          Equations
          Instances For

            Nontriviality: specialization strictly reduces cost on some nonempty class of programs. Without this, mix+selfApp alone are satisfied by residualizers that do no optimization (e.g. the identity residualizer spec p s = p).

            Equations
            Instances For

              Self-interpreter: running int on (src, d) agrees with running src on d.

              Equations
              Instances For

                Jones-optimality (Neil Jones): some self-interpreter specializes to a residual no more expensive than the source, for every source. Cost/pe_cost form matches Nontrivial. This is not 1993 polyvariant mix / compiler-generator quality; specTerm is not an encoding of jgs_spec or offline_spec.

                Equations
                Instances For

                  Identity residualizer (ignores static data). Cost-vacuous, but cannot be packaged as PESetup with eval prog _ := some prog: selfApp forces specTerm to behave like a realizer, colliding when programs can equal specTerm.

                  Equations
                  Instances For

                    Vacuity witness: identity residualization never shrinks under pe_cost.

                    Principled trivial PESetup (tagged residual, recursive unpack) #

                    Identity spec cannot satisfy selfApp for a universal eval prog _ := some prog. Instead residualize under a dup tag and reflect the specializer as bare swap. Evaluation recursively unpacks residuals so mix holds for all p (including when p is itself tagged); selfApp is the swap clause. Cost never shrinks, so ¬ Nontrivial. A real self-applicable optimizing specTerm / BTA remains future work.

                    Tagged residual: dup · (pair p s). Strictly larger than p under pe_cost.

                    Equations
                    Instances For

                      Object-level evaluator for the toy specializer.

                      • swap on pair p s returns the tagged residual (selfApp).
                      • A tagged residual applied to dynamic d continues as run p (pair s d) (mix), recursively, so residual-shaped programs do not break the mix equation.
                      • Otherwise return the syntactic application.
                      Equations
                      Instances For
                        def ISAR.trivial_eval (prog data : ITerm) :
                        Equations
                        Instances For

                          Toy PESetup: mix and selfApp by rfl; residualizer is cost-non-shrinking.

                          Equations
                          Instances For

                            Mix+selfApp alone do not imply optimization: TrivialPE is not Nontrivial.

                            trivial_run returns konst · norm only by the default clause on those atoms.

                            Tagged residuals are strictly larger than the source, so no self-interpreter can meet the Jones cost bound either — TrivialPE is not Jones-optimal.

                            Toy Jones-optimal pair (identity interpreter, not 1993 mix) #

                            norm as a degenerate self-interpreter: running it on (src, d) continues as src on d. Specializing that interpreter copies the source (spec norm src = src), so the Jones cost bound holds with equality. Mix/selfApp still use tagged residuals for every other program. This is a cost-criterion toy, not a 1993 compiler-generator.

                            def ISAR.jones_eval (prog data : ITerm) :
                            Equations
                            Instances For
                              theorem ISAR.jones_mix (p s d : ITerm) :
                              Equations
                              Instances For

                                Optimizing fragment PE (identity / konstβ folding) #

                                Not a full Jones–Gomard–Sestoft BTA for all of ISAR. This is a principled optimizing specializer on a fragment:

                                mix / selfApp hold by computation on this evaluator; Nontrivial is witnessed by stripping a norm redex. Full self-applicable optimizing mix for the whole calculus remains dissertation-scale future work.

                                Object-level evaluator matching opt_spec:

                                • swap on pair p s implements selfApp.
                                • Tagged residuals unpack via mix.
                                • Program-position norm / konstβ peels mirror opt_spec.
                                • Atomic values are data-insensitive (so constant residuals satisfy mix).
                                Equations
                                Instances For
                                  def ISAR.opt_eval (prog data : ITerm) :
                                  Equations
                                  Instances For
                                    theorem ISAR.opt_mix (p s d : ITerm) :
                                    opt_eval (opt_spec p s) d = opt_eval p (pair s d)

                                    Optimizing fragment PESetup: mix/selfApp by induction/rfl; Nontrivial below.

                                    Equations
                                    Instances For

                                      Witness program: norm · konst strips to konst.

                                      Equations
                                      Instances For

                                        Jones–Gomard–Sestoft online PE for the whole IStep signature #

                                        Offline BTA classifies subterms; online PE is the 1985 mix technique once every object-language redex can fire at specialization time. ISAR's object reductions are exactly normβ, konstβ, compβ, . This specializer folds all four.

                                        Offline binding-time sketch: swap is the dynamic hole; other atoms are static; application is static iff both sides are. Monovariant: no call strings, no program-point splitting. Unused by jgs_spec (online PE). Lemmas below relate bta to specialization; they do not give 1993 polyvariant mix.

                                        Instances For
                                          @[implicit_reducible]
                                          Equations
                                          @[irreducible]

                                          Monovariant offline specializer: fold a redex only when bta classifies the whole redex as static. Static S unfolds once into a residual with no s tag (no recursion — size may grow). Dynamic terms are tagged residuals. This is still monovariant (one division). It is not 1993 polyvariant mix.

                                          Equations
                                          Instances For
                                            theorem ISAR.offline_spec_I_static (body s₁ s₂ : ITerm) (ht : bta (ITerm.norm · body) = BindingTime.static) :
                                            offline_spec (ITerm.norm · body) s₁ = offline_spec body s₁
                                            theorem ISAR.offline_spec_B_static (f g x s₁ : ITerm) (ht : bta (ITerm.comp · f · g · x) = BindingTime.static) :
                                            offline_spec (ITerm.comp · f · g · x) s₁ = offline_spec (f · (g · x)) s₁
                                            theorem ISAR.offline_spec_S_static (x y z s₁ s₂ : ITerm) (ht : bta (ITerm.sₛ · x · y · z) = BindingTime.static) :
                                            def ISAR.jgs_eval (prog data : ITerm) :
                                            Equations
                                            Instances For
                                              theorem ISAR.jgs_mix (p s d : ITerm) :
                                              jgs_eval (jgs_spec p s) d = jgs_eval p (pair s d)

                                              Full-signature online PE: every IStep rule has a specialization clause.

                                              Equations
                                              Instances For

                                                Extra Nontrivial witness: a static compβ redex folds to norm.

                                                Equations
                                                Instances For

                                                  Residual konst does not depend on the static argument. Used to rule out swap as a self-interpreter (it specializes via jgs_spec).

                                                  If the first argument of pair is shared, jgs_run cannot return both konst and dup (atoms ignore data; swap specializes independently of the remaining static payload).

                                                  Online one-step JGS_PE has no self-interpreter: the three atom tests (konst, dup, swap as sources) cannot hold together. Not Jones-optimal. This is not a 1993 polyvariant mix / cogen claim.

                                                  One-step online JGS_PE is not Jones-optimal. 1993 polyvariant division / cogen remains open; specTerm is still swap.

                                                  theorem ISAR.specialize_ISKTerm (t : ITerm) (ht : ISKTerm t) (s_env : NatOption ITerm) :
                                                  specialize t s_env = t
                                                  theorem ISAR.specialize_is_ISKTerm (t : ITerm) (ht : ISKTerm t) (s_env : NatOption ITerm) :
                                                  theorem ISAR.specialize_respects_OperEq (t u : ITerm) (ht : ISKTerm t) (hu : ISKTerm u) (h : OperEq t, ht u, hu) (s_env : NatOption ITerm) :
                                                  (ht_spec : ISKTerm (specialize t s_env)), (hu_spec : ISKTerm (specialize u s_env)), OperEq specialize t s_env, ht_spec specialize u s_env, hu_spec

                                                  Parameterized Futamura Square & Commutative Diagram #

                                                  Formal verification that all 4 execution pathways across the Futamura Projections commute to identical operational semantics:

                                                  structure ISAR.FutamuraSquare (S : PESetup) (int src d : ITerm) :

                                                  The four corner evaluations of the Futamura Square for a given PESetup.

                                                  Instances For
                                                    theorem ISAR.futamura_square_proj1_commutes (S : PESetup) (int src d : ITerm) :
                                                    S.eval (S.spec int src) d = S.eval int (pair src d)

                                                    Commutative Futamura Square Theorem: Evaluating the source program via direct interpretation (Pathway 0) produces the exact same result as executing the 1st Projection compiled target (Pathway 1).

                                                    theorem ISAR.futamura_square_proj2_compiler_yields_target (S : PESetup) (int src : ITerm) :
                                                    S.eval (S.spec S.specTerm int) src = some (S.spec int src)

                                                    Compiler Synthesis Commutative Theorem (2nd Projection): Applying the synthesized compiler to the source program yields the 1st projection target.

                                                    Compiler-Generator Synthesis Commutative Theorem (3rd Projection): Applying the synthesized cogen to an interpreter yields the 2nd projection compiler.

                                                    theorem ISAR.futamura_square_all_pathways_commute (S : PESetup) (int src d : ITerm) :
                                                    have target1 := S.spec int src; have compiler := S.spec S.specTerm int; have cogen := S.spec S.specTerm S.specTerm; S.eval target1 d = S.eval int (pair src d) S.eval compiler src = some target1 S.eval cogen int = some compiler

                                                    Full Commutative Futamura Square: All four pathways of the Futamura hierarchy compute identical values on all dynamic inputs.