Documentation

CslibFrontier.Computability.WordRAM.Realization.Basic

Graded realization of memory-oracle clients #

Realization.Realizes certifies one uniform native emitter for an existing Memory.Prog family. Arguments and results have explicit faithful finite-register encodings. Its memory abstraction is the entire memory function together with one frontier register; scratch consists only of fresh registers. The entry predicate and local-work charge are visible parameters.

The charged currency adds certified local computation and control work to Memory.charge. In particular, a query-free loop cannot acquire a constant instruction bound merely because its memory history is empty. The emitter, scratch size and exchange constant precede every runtime argument, word width and initial memory. Refines is a semantic projection of this certificate, not a competing execution semantics or a second client language.

Main definitions #

Main results #

The realization certificate and its grading are original infrastructure. The memory interface, free-monad interpreter and native execution rules are reused unchanged.

The abstraction retains every cell, including unallocated and inaccessible cells.

Instances For

    Bounded native data supplies the memory invariant, independently of any scratch layout.

    The memory-level instruction currency uses the existing deterministic interpreter. Its transition function does not depend on the word width; admissibility is a separate premise.

    Instances For

      Uniformity of the mathematical interpreter across widths: only valid depends on w.

      The result of the memory interpreter is independent of the admissibility width.

      The final memory/frontier state is independent of the admissibility width.

      def Cslib.WordRAM.Realization.appendWords {k l : ℕ} {δ α : Type} (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) :
      δ × α ↪ Fin (k + l) → ℕ

      Faithfully append a returned word vector to the original input environment. This is static register wiring; it performs no native computation or copies.

      Instances For
        @[simp]

        A semantic return issues no memory operation. Its native glue is charged separately.

        Memory charges add at the actual intermediate state; the local bind charge is separate.

        def Cslib.WordRAM.Realization.Realizes.Spec {k l : ℕ} {δ α : Type} (B : Basis) (p : δ → DataStructure.Memory.Prog α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (work : δ → DataStructure.Memory.State → ℕ) (n C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r) :

        The execution contract for specified scratch, exchange factor and emitter. Both the existential judgment and data witnesses use this same contract.

        Instances For
          def Cslib.WordRAM.Realization.Realizes {k l : ℕ} {δ α : Type} (B : Basis) (p : δ → DataStructure.Memory.Prog α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (work : δ → DataStructure.Memory.State → ℕ) :

          Existing Memory.Prog clients have a fixed emitter and a combined charge. The explicit safe predicate may contain glue domains and fit envelopes; it is never existentially chosen. The existing valid-history premise supplies memory admissibility. The extra one pays uniform entry/exit overhead, so the native bound is C * charged + C with one fixed C.

          Input registers may alias each other. The injective scratch bank is disjoint from all inputs and the frontier; its first l registers hold the faithfully encoded result. Every other register, except the frontier, is framed. All native registers and memory are bounded at entry, separately from the functional representation.

          Instances For
            theorem Cslib.WordRAM.Realization.Realizes.Spec.consequence {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} {work' : δ → DataStructure.Memory.State → ℕ} {n C : ℕ} {emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r} (he : Spec B p input output safe work n C fun {r : ℕ} => emit) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → safe' w d s → safe w d s) (hwork : ∀ (d : δ) (s : DataStructure.Memory.State), work d s ≤ work' d s) :
            Spec B p input output safe' work' n C fun {r : ℕ} => emit

            Strengthen the visible entry domain or enlarge the local-work charge for a specified scratch size, exchange factor and emitter, all of which are unchanged.

            theorem Cslib.WordRAM.Realization.Realizes.consequence {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} {work' : δ → DataStructure.Memory.State → ℕ} (h : Realizes B p input output safe work) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → safe' w d s → safe w d s) (hwork : ∀ (d : δ) (s : DataStructure.Memory.State), work d s ≤ work' d s) :
            Realizes B p input output safe' work'

            Strengthen the visible entry domain or enlarge the local-work charge. The entry word and memory bounds may discharge fit conditions; no executable code or domain is hidden by this rule.

            theorem Cslib.WordRAM.Realization.Realizes.mapSimulates {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {B' : Basis} (h : Realizes B p input output safe work) (φ : B.Simulates B') :
            Realizes B' p input output safe work

            Existing defined-evaluation basis simulation preserves the complete realization certificate.

            theorem Cslib.WordRAM.Realization.Realizes.exists_refines {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Realizes B p input output safe work) :
            ∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (l + n + 1) → Fin r), Function.Injective tmp → (∀ (i : Fin k) (j : Fin (l + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (l + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (w : ℕ) (d : δ), Refines w (fun (s : (DataStructure.Memory.spec w).State) (x : Data r) => s = memoryState frontier x ∧ Data.Bounded w x ∧ x.regs ∘ src = input d ∧ safe w d s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (p d))) (p d) (fun (a : α) (s : (DataStructure.Memory.spec w).State) (y : Data r) => memoryState frontier y = s ∧ ∀ (i : Fin l), y.regs (tmp (Fin.castAdd (n + 1) i)) = output a i) (fun (s : (DataStructure.Memory.spec w).State) (x : Data r) => C * (memoryCost (p d) s + work d s + 1)) (emit frontier src tmp)

            The same witness derives the existing semantic Refines judgment. The intermediate representation is fixed by the functional memory abstraction and argument/result registers.

            def Cslib.WordRAM.Realization.Realizes.SpecBounded {k l : ℕ} {δ α : Type} (B : Basis) (p : δ → DataStructure.Memory.Prog α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (work : δ → DataStructure.Memory.State → ℕ) (n C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r) :

            Frontier-bounded execution contract strengthening Realizes.Spec with ExecBounded w (y.regs frontier).

            Instances For
              theorem Cslib.WordRAM.Realization.Realizes.SpecBounded.toSpec {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {n C : ℕ} {emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r} (he : SpecBounded B p input output safe work n C fun {r : ℕ} => emit) :
              Spec B p input output safe work n C fun {r : ℕ} => emit

              Erase the frontier-access bound from SpecBounded back to Spec.

              theorem Cslib.WordRAM.Realization.Realizes.SpecBounded.consequence {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} {work' : δ → DataStructure.Memory.State → ℕ} {n C : ℕ} {emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r} (he : SpecBounded B p input output safe work n C fun {r : ℕ} => emit) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → safe' w d s → safe w d s) (hwork : ∀ (d : δ) (s : DataStructure.Memory.State), work d s ≤ work' d s) :
              SpecBounded B p input output safe' work' n C fun {r : ℕ} => emit

              Strengthen the visible entry domain or enlarge the local-work charge for SpecBounded.

              def Cslib.WordRAM.Realization.RealizesBounded {k l : ℕ} {δ α : Type} (B : Basis) (p : δ → DataStructure.Memory.Prog α) (input : δ ↪ Fin k → ℕ) (output : α ↪ Fin l → ℕ) (safe : ℕ → δ → DataStructure.Memory.State → Prop) (work : δ → DataStructure.Memory.State → ℕ) :

              Uniform frontier-bounded realization of a Memory.Prog family, certifying that every native load and store accesses an address strictly below y.regs frontier.

              Instances For
                theorem Cslib.WordRAM.Realization.RealizesBounded.toRealizes {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : RealizesBounded B p input output safe work) :
                Realizes B p input output safe work

                Erase the frontier-access bound back to Realizes.

                theorem Cslib.WordRAM.Realization.RealizesBounded.consequence {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} {work' : δ → DataStructure.Memory.State → ℕ} (h : RealizesBounded B p input output safe work) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → safe' w d s → safe w d s) (hwork : ∀ (d : δ) (s : DataStructure.Memory.State), work d s ≤ work' d s) :
                RealizesBounded B p input output safe' work'

                Strengthen the visible entry domain or enlarge the local-work charge.

                theorem Cslib.WordRAM.Realization.RealizesBounded.mapSimulates {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {B' : Basis} (h : RealizesBounded B p input output safe work) (φ : B.Simulates B') :
                RealizesBounded B' p input output safe work

                Transport a frontier-bounded realization certificate along a basis simulation.

                theorem Cslib.WordRAM.Realization.Realizes.SpecBounded.reindex {B : Basis} {ε δ α : Type} {k m l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {n C : ℕ} {emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r} (he : SpecBounded B p input output safe work n C fun {r : ℕ} => emit) (f : ε → δ) (env : ε ↪ Fin m → ℕ) (ix : Fin k → Fin m) (hw : ∀ (e : ε) (i : Fin k), input (f e) i = env e (ix i)) :
                SpecBounded B (fun (e : ε) => p (f e)) env output (fun (w : ℕ) (e : ε) (s : DataStructure.Memory.State) => safe w (f e) s) (fun (e : ε) (s : DataStructure.Memory.State) => work (f e) s) n C fun {r : ℕ} (frontier : Fin r) (src : Fin m → Fin r) (tmp : Fin (l + n + 1) → Fin r) => emit frontier (src ∘ ix) tmp

                Rewire a SpecBounded emitter's inputs by a fixed index map.

                theorem Cslib.WordRAM.Realization.RealizesBounded.reindex {B : Basis} {ε δ α : Type} {k m l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : RealizesBounded B p input output safe work) (f : ε → δ) (env : ε ↪ Fin m → ℕ) (ix : Fin k → Fin m) (hw : ∀ (e : ε) (i : Fin k), input (f e) i = env e (ix i)) :
                RealizesBounded B (fun (e : ε) => p (f e)) env output (fun (w : ℕ) (e : ε) (s : DataStructure.Memory.State) => safe w (f e) s) fun (e : ε) (s : DataStructure.Memory.State) => work (f e) s

                Pull a RealizesBounded certificate back along a fixed register-index map.

                theorem Cslib.WordRAM.Realization.RealizesBounded.mono_mul {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work work' : δ → DataStructure.Memory.State → ℕ} (h : RealizesBounded B p input output safe work) (K : ℕ) (hwork : ∀ (d : δ) (s : DataStructure.Memory.State), work d s ≤ K * (memoryCost (p d) s + work' d s + 1)) :
                RealizesBounded B p input output safe work'

                Absorb local work dominated by a constant multiple of the memory charge plus another grading.

                theorem Cslib.WordRAM.Realization.RealizesBounded.congr {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {p' : δ → DataStructure.Memory.Prog α} (h : RealizesBounded B p input output safe work) (hp : ∀ (d : δ), p d = p' d) :
                RealizesBounded B p' input output safe work

                Pointwise equal programs have the same frontier-bounded certificates.

                theorem Cslib.WordRAM.Realization.RealizesBounded.consequence_valid {B : Basis} {δ α : Type} {k l : ℕ} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {safe' : ℕ → δ → DataStructure.Memory.State → Prop} (h : RealizesBounded B p input output safe work) (hsafe : ∀ (w : ℕ) (d : δ) (s : DataStructure.Memory.State), (∀ (i : Fin k), Fits w (input d i)) → DataStructure.Memory.WellFormed w s → (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (p d)) → safe' w d s → safe w d s) :
                RealizesBounded B p input output safe' work

                Strengthen the visible domain under the client's own valid history.