Documentation

Complexity.Computability.Ram.Execution.Fast

Executable array/tree-map representation of the same RAM #

Instruction fetch and register lookup use arrays. Sparse memory uses a balanced Std.TreeMap keyed by decoded word addresses. Updates do not accumulate the reference machine's nested lookup functions. Registers grow only when a write uses a previously unallocated index; callers may preallocate their fixed program's register bound when constructing the initial state.

The representation is not a different cost model. State.toState observes it as the existing RAM state, and the instruction/step theorems below prove exact correspondence. Host array growth, tree balancing, and interpreter wall time are not additional abstract RAM transitions or performance measurements.

A single scan supplies an optional preallocation size. This does not limit the semantics: out-of-capacity writes are still handled by setGrow.

Equations
Instances For
    def Ram.Fast.setGrow {w : Nat} (xs : Array (Word w)) (i : Nat) (value : Word w) :

    Total array update with zero-filled extension, preserving the reference machine's unbounded register-name semantics rather than dropping writes.

    Equations
    Instances For
      @[simp]
      theorem Ram.Fast.getD_replicate_zero {w : Nat} (n r : Nat) :
      @[simp]
      theorem Ram.Fast.getD_setGrow {w : Nat} (xs : Array (Word w)) (i : Nat) (value : Word w) (r : Nat) :
      (setGrow xs i value)[r]?.getD 0 = if r = i then value else xs[r]?.getD 0
      structure Ram.Fast.State (w : Nat) :
      Instances For
        @[inline]
        def Ram.Fast.State.getReg {w : Nat} (s : State w) (r : Reg) :
        Equations
        Instances For
          @[inline]
          def Ram.Fast.State.getMem {w : Nat} (s : State w) (a : Word w) :
          Equations
          Instances For
            Equations
            Instances For
              def Ram.Fast.State.initial {w : Nat} (input : List (Word w)) (registerCapacity : Nat := 0) :
              Equations
              Instances For
                @[inline]
                def Ram.Fast.State.setReg {w : Nat} (s : State w) (r : Reg) (value : Word w) :
                Equations
                Instances For
                  @[inline]
                  def Ram.Fast.State.setMem {w : Nat} (s : State w) (a value : Word w) :
                  Equations
                  Instances For
                    @[inline]
                    def Ram.Fast.State.next {w : Nat} (s : State w) :
                    Equations
                    Instances For
                      def Ram.Fast.State.output {w : Nat} (s : State w) :
                      Equations
                      Instances For
                        @[simp]
                        theorem Ram.Fast.State.toState_pc {w : Nat} (s : State w) :
                        @[simp]
                        @[simp]
                        @[simp]
                        @[simp]
                        theorem Ram.Fast.State.toState_with_pc {w : Nat} (s : State w) (pc : Nat) :
                        { pc := pc, regs := s.regs, mem := s.mem, input := s.input, outputRev := s.outputRev, status := s.status }.toState = have __src := s.toState; { pc := pc, regs := __src.regs, mem := __src.mem, input := __src.input, outputRev := __src.outputRev, status := __src.status }
                        @[simp]
                        theorem Ram.Fast.State.toState_with_input {w : Nat} (s : State w) (input : List (Word w)) :
                        { pc := s.pc, regs := s.regs, mem := s.mem, input := input, outputRev := s.outputRev, status := s.status }.toState = have __src := s.toState; { pc := __src.pc, regs := __src.regs, mem := __src.mem, input := input, outputRev := __src.outputRev, status := __src.status }
                        @[simp]
                        theorem Ram.Fast.State.toState_with_outputRev {w : Nat} (s : State w) (outputRev : List (Word w)) :
                        { pc := s.pc, regs := s.regs, mem := s.mem, input := s.input, outputRev := outputRev, status := s.status }.toState = have __src := s.toState; { pc := __src.pc, regs := __src.regs, mem := __src.mem, input := __src.input, outputRev := outputRev, status := __src.status }
                        @[simp]
                        theorem Ram.Fast.State.toState_with_status {w : Nat} (s : State w) (status : Status) :
                        { pc := s.pc, regs := s.regs, mem := s.mem, input := s.input, outputRev := s.outputRev, status := status }.toState = have __src := s.toState; { pc := __src.pc, regs := __src.regs, mem := __src.mem, input := __src.input, outputRev := __src.outputRev, status := status }
                        @[simp]
                        theorem Ram.Fast.State.getReg_setReg {w : Nat} (s : State w) (r : Reg) (value : Word w) (query : Reg) :
                        (s.setReg r value).getReg query = if query = r then value else s.getReg query
                        @[simp]
                        theorem Ram.Fast.State.toState_setReg {w : Nat} (s : State w) (r : Reg) (value : Word w) :
                        (s.setReg r value).toState = s.toState.setReg r value
                        @[simp]
                        theorem Ram.Fast.State.getMem_setMem {w : Nat} (s : State w) (a value query : Word w) :
                        (s.setMem a value).getMem query = if query = a then value else s.getMem query
                        @[simp]
                        theorem Ram.Fast.State.toState_setMem {w : Nat} (s : State w) (a value : Word w) :
                        (s.setMem a value).toState = s.toState.setMem a value
                        @[simp]
                        theorem Ram.Fast.State.toState_initial {w : Nat} (input : List (Word w)) (registerCapacity : Nat) :
                        (initial input registerCapacity).toState = Ram.State.initial input
                        def Ram.Fast.execInstr {w : Nat} (i : Instr) (s : State w) :

                        The same fixed instruction vocabulary, using the executable storage representation. Every branch corresponds to Ram.execInstr.

                        Equations
                        Instances For
                          def Ram.Fast.step {w : Nat} (code : Array Instr) (s : State w) :

                          Fetch from an array; halted/fault states and invalid PCs have no successor, exactly as in the reference transition.

                          Equations
                          Instances For
                            @[simp]

                            Every executable instruction has exactly the reference instruction's effect, including exhausted input and control flow.

                            @[simp]
                            theorem Ram.Fast.map_step {w : Nat} (code : Array Instr) (s : State w) :

                            Full transition correspondence, including absence of a successor. Array fetch changes representation only; each successful transition is one RAM step.

                            theorem Ram.Fast.step_sound {w : Nat} {code : Array Instr} {s t : State w} (h : step code s = some t) :
                            def Ram.Fast.run {w : Nat} (code : Array Instr) (budget : Nat) (s : State w) :

                            The shared budget driver on executable storage. It retains the final state, exact transition count, and stopping reason without decoding the state during execution.

                            Equations
                            Instances For
                              @[simp]
                              theorem Ram.Fast.map_run {w : Nat} (code : Array Instr) (budget : Nat) (s : State w) :
                              RunResult.map State.toState (run code budget s) = Ram.run code.toList budget s.toState

                              The complete result agrees with the reference backend, not merely its successful steps. Halt, fault, invalid PC, and exhausted fuel all agree.

                              theorem Ram.Fast.run_exec {w : Nat} (code : Array Instr) (budget : Nat) (s : State w) :
                              (run code budget s).steps ≤ budget ∧ Exec code.toList (run code budget s).steps s.toState (run code budget s).state.toState
                              theorem Ram.Fast.run_outcome {w : Nat} (code : Array Instr) (budget : Nat) (s : State w) :
                              Runner.Outcome (step code) State.status budget (run code budget s)