Documentation

Cslib.Computability.URM.Execution

URM Execution Semantics #

Single-step and multi-step execution semantics for URMs.

Main definitions #

Bridge lemmas:

Notation (scoped to URM namespace) #

Standard computability theory notation:

Main results #

inductive Cslib.URM.Step (p : Program) :
StateStateProp

Single-step execution relation for URMs.

Each constructor corresponds to one of the four instruction types:

  • zero: Execute Z n (set register n to 0)
  • succ: Execute S n (increment register n)
  • transfer: Execute T m n (copy register m to register n)
  • jump_eq: Execute J m n q when registers m and n are equal (jump to q)
  • jump_ne: Execute J m n q when registers m and n differ (proceed to next)
Instances For
    @[reducible, inline]
    abbrev Cslib.URM.Steps (p : Program) :
    StateStateProp

    Multi-step execution: the reflexive-transitive closure of Step.

    Equations
    Instances For

      The step relation is deterministic: each state has at most one successor.

      theorem Cslib.URM.Step.no_step_of_halted {p : Program} {s s' : State} (hhalted : s.isHalted p) :
      ¬Step p s s'

      A halted state has no successor in the step relation.

      theorem Cslib.URM.Step.preserves_register {p : Program} {s s' : State} {r : } (hstep : Step p s s') (hr : ∀ (instr : Instr), p[s.pc]? = some instrinstr.writesTo some r) :
      s'.regs.read r = s.regs.read r

      A single step preserves registers not written to by the current instruction.

      This is a fundamental property of URM execution: each instruction modifies at most one register (Z, S, T write to one register; J writes to none).

      Step Properties #

      A state is halted iff it is normal (has no successor) in the reduction system.

      The step relation is confluent.

      theorem Cslib.URM.Steps.preserves_register {p : Program} {s s' : State} {r : } (hsteps : Steps p s s') (hr : instrp, instr.writesTo some r) :
      s'.regs.read r = s.regs.read r

      Multi-step execution preserves registers not written by any executed instruction.

      theorem Cslib.URM.Steps.eq_of_halts {p : Program} {init s₁ s₂ : State} (h1 : Steps p init s₁) (hh1 : s₁.isHalted p) (h2 : Steps p init s₂) (hh2 : s₂.isHalted p) :
      s₁ = s₂

      If two halted states are reachable from the same start, they are equal.

      This follows from confluence: since Step p is confluent and both s₁ and s₂ are normal forms reachable from init, they must be equal.

      def Cslib.URM.Halts (p : Program) (inputs : List ) :

      A program halts on given inputs if execution reaches a halted state.

      This is equivalent to (Step p).Normalizable (State.init inputs) — see halts_iff_normalizable.

      Equations
      Instances For

        Halting is equivalent to normalizability in the reduction system.

        def Cslib.URM.Diverges (p : Program) (inputs : List ) :

        A program diverges on given inputs if it does not halt.

        Equations
        Instances For
          def Cslib.URM.HaltsWithResult (p : Program) (inputs : List ) (result : ) :

          A program halts on given inputs with a specific result in R[0].

          Equations
          Instances For

            Notation for halting: p ↓ inputs means program p halts on inputs.

            Equations
            Instances For

              Notation for divergence: p ↑ inputs means program p diverges on inputs.

              Equations
              Instances For

                Notation for halting with result: p ↓ inputs ≫ result means program p halts on inputs with result in R[0].

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem Cslib.URM.HaltsWithResult.toHalts {p : Program} {inputs : List } {result : } (h : HaltsWithResult p inputs result) :
                  Halts p inputs

                  If a program halts with a result, it halts.

                  noncomputable def Cslib.URM.evalState (p : Program) (inputs : List ) :

                  Evaluation returning the full halting state.

                  Equations
                  Instances For
                    theorem Cslib.URM.evalState_spec (p : Program) {inputs : List } (h : (evalState p inputs).Dom) :
                    have s := (evalState p inputs).get h; Steps p (State.init inputs) s s.isHalted p

                    Specification: the state from evalState satisfies Steps and isHalted.

                    theorem Cslib.URM.Halts.toHaltsWithResult {p : Program} {inputs : List } (h : Halts p inputs) :
                    HaltsWithResult p inputs ((evalState p inputs).get h).regs.output

                    If a program halts, it halts with the output of the final state.

                    noncomputable def Cslib.URM.eval (p : Program) (inputs : List ) :

                    Evaluation as a partial function using Part. Defined when the program halts, returning the value in register 0.

                    Equations
                    Instances For
                      theorem Cslib.URM.haltsWithResult_iff_eval (p : Program) {inputs : List } {result : } :
                      HaltsWithResult p inputs result eval p inputs = Part.some result

                      A program halts with result a iff evaluation returns Part.some a.

                      Two programs are equivalent if they produce the same result for all inputs.

                      Equations
                      Instances For

                        Program equivalence is an equivalence relation.

                        @[implicit_reducible]

                        Setoid instance for programs, enabling the ≈ notation.

                        Equations