Documentation

Complexitylib.Models.TuringMachine.Hoare.Defs

Hoare-style specifications for Turing machines #

This file defines Hoare triples for reasoning about TM behavior in terms of tape preconditions and postconditions. This provides a compositional framework for building and verifying complex machines from simpler components.

Main definitions #

Design notes #

Hoare triples abstract away the internal state Q, reasoning purely about tape contents and head positions. This makes them ideal for compositional reasoning: the pre/postconditions of composed machines can be stated without reference to the internal state types of the components.

The precondition must imply that the starting configuration has the machine's qstart state. The postcondition holds at halting.

@[reducible, inline]

A predicate on the tape configuration: input tape, work tapes, output tape.

Equations
Instances For
    @[reducible, inline]

    A predicate on the tape configuration: input tape, work tapes, output tape.

    Equations
    Instances For
      def Complexity.TM.HoareTime {n : } (tm : TM n) (pre post : TapePred n) (bound : ) :

      Time-bounded Hoare triple: for any tapes satisfying pre, starting from qstart, the machine halts within bound steps with tapes satisfying post.

      This is the core specification type for compositional TM reasoning. Captures both correctness (pre/post) and efficiency (time bound).

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        def Complexity.TM.Hoare {n : } (tm : TM n) (pre post : TapePred n) :

        Unbounded Hoare triple: the machine halts with tapes satisfying post, without a time bound. Useful when only correctness matters.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Complexity.TM.HoareTime.consequence {n : } {tm : TM n} {pre pre' post post' : TapePred n} {b b' : } (h : tm.HoareTime pre post b) (hpre : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), pre' inp work outpre inp work out) (hpost : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), post inp work outpost' inp work out) (hbound : b b') :
          tm.HoareTime pre' post' b'

          Consequence rule: weaken the precondition and strengthen the postcondition.

          theorem Complexity.TM.HoareTime.weaken_pre {n : } {tm : TM n} {pre pre' post : TapePred n} {b : } (h : tm.HoareTime pre post b) (hpre : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), pre' inp work outpre inp work out) :
          tm.HoareTime pre' post b

          Precondition weakening: if pre' implies pre, lift the Hoare triple.

          theorem Complexity.TM.HoareTime.strengthen_post {n : } {tm : TM n} {pre post post' : TapePred n} {b : } (h : tm.HoareTime pre post b) (hpost : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), post inp work outpost' inp work out) :
          tm.HoareTime pre post' b

          Postcondition strengthening: if post implies post', lift the triple.

          theorem Complexity.TM.HoareTime.mono_bound {n : } {tm : TM n} {pre post : TapePred n} {b b' : } (h : tm.HoareTime pre post b) (hle : b b') :
          tm.HoareTime pre post b'

          Time monotonicity: increase the time bound.

          theorem Complexity.TM.HoareTime.toHoare {n : } {tm : TM n} {pre post : TapePred n} {b : } (h : tm.HoareTime pre post b) :
          tm.Hoare pre post

          Bounded implies unbounded.

          theorem Complexity.TM.hoareTime_of_decidesInTime {n : } {tm : TM n} {L : Language} {T : } (h : tm.DecidesInTime L T) (x : List Bool) :
          tm.HoareTime (fun (inp : Tape) (work : Fin nTape) (out : Tape) => inp = Tape.init (List.map Γ.ofBool x) (work = fun (x : Fin n) => Tape.init []) out = Tape.init []) (fun (x_1 : Tape) (x_2 : Fin nTape) (out : Tape) => (x Lout.cells 1 = Γ.one) (xLout.cells 1 = Γ.zero)) (T x.length)

          DecidesInTime implies a family of Hoare triples, one per input.

          @[reducible, inline]

          A predicate on the tape configuration: input tape, work tapes, output tape.

          Equations
          Instances For
            def Complexity.NTM.HoareTime {n : } (tm : NTM n) (pre post : TapePred n) (bound : ) :

            Time-bounded Hoare triple for nondeterministic machines: every choice sequence of the given length reaches a halted configuration satisfying post. NTM.trace already keeps halted configurations fixed, so this also covers machines that halt earlier than the bound.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Complexity.NTM.HoareTime.consequence {n : } {tm : NTM n} {pre pre' post post' : TapePred n} {b : } (h : tm.HoareTime pre post b) (hpre : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), pre' inp work outpre inp work out) (hpost : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), post inp work outpost' inp work out) :
              tm.HoareTime pre' post' b

              Consequence rule for NTM Hoare triples.

              theorem Complexity.NTM.HoareTime.weaken_pre {n : } {tm : NTM n} {pre pre' post : TapePred n} {b : } (h : tm.HoareTime pre post b) (hpre : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), pre' inp work outpre inp work out) :
              tm.HoareTime pre' post b

              Precondition weakening for NTM Hoare triples.

              theorem Complexity.NTM.HoareTime.strengthen_post {n : } {tm : NTM n} {pre post post' : TapePred n} {b : } (h : tm.HoareTime pre post b) (hpost : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), post inp work outpost' inp work out) :
              tm.HoareTime pre post' b

              Postcondition strengthening for NTM Hoare triples.

              theorem Complexity.NTM.HoareTime.mono_bound {n : } {tm : NTM n} {pre post : TapePred n} {b b' : } (h : tm.HoareTime pre post b) (hle : b b') :
              tm.HoareTime pre post b'

              Time monotonicity for NTM Hoare triples. Since NTM.trace is fixed after halting, a proof for b steps also gives a proof for any larger bound.

              theorem Complexity.NTM.HoareTime.consequence_bound {n : } {tm : NTM n} {pre pre' post post' : TapePred n} {b b' : } (h : tm.HoareTime pre post b) (hpre : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), pre' inp work outpre inp work out) (hpost : ∀ (inp : Tape) (work : Fin nTape) (out : Tape), post inp work outpost' inp work out) (hbound : b b') :
              tm.HoareTime pre' post' b'

              Consequence rule plus time-bound weakening for NTM Hoare triples.

              theorem Complexity.NTM.exists_first_halt_time_of_trace_halted {n : } (tm : NTM n) (T : ) (choices : Fin TBool) (c : Cfg n tm.Q) (hhalt : tm.halted (tm.trace T choices c)) :
              ∃ (t : ) (ht : t T), tm.halted (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) c) ∀ (s : ) (hs : s < t), ¬tm.halted (tm.trace s (fun (i : Fin s) => choices (Fin.castLE i)) c)

              If a finite NTM trace is halted at time T, then it has a least halted prefix time. This is useful for phase-composed machines: Hoare-time facts give halting by a fixed bound, while phase exits need the first local halt time.

              theorem Complexity.NTM.HoareTime.exists_first_halt_time {n : } {tm : NTM n} {pre post : TapePred n} {bound : } (h : tm.HoareTime pre post bound) {inp : Tape} {work : Fin nTape} {out : Tape} (hpre : pre inp work out) (choices : Fin boundBool) :
              ∃ (t : ) (ht : t bound), tm.halted (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) { state := tm.qstart, input := inp, work := work, output := out }) ∀ (s : ) (hs : s < t), ¬tm.halted (tm.trace s (fun (i : Fin s) => choices (Fin.castLE i)) { state := tm.qstart, input := inp, work := work, output := out })

              Hoare-time corollary of exists_first_halt_time_of_trace_halted: every all-path halting proof yields a least halting prefix for each fixed choice sequence.

              theorem Complexity.NTM.HoareTime.exists_first_halt_time_with_post {n : } {tm : NTM n} {pre post : TapePred n} {bound : } (h : tm.HoareTime pre post bound) {inp : Tape} {work : Fin nTape} {out : Tape} (hpre : pre inp work out) (choices : Fin boundBool) :
              ∃ (t : ) (ht : t bound), have c0 := { state := tm.qstart, input := inp, work := work, output := out }; tm.halted (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) c0) post (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) c0).input (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) c0).work (tm.trace t (fun (i : Fin t) => choices (Fin.castLE ht i)) c0).output ∀ (s : ) (hs : s < t), ¬tm.halted (tm.trace s (fun (i : Fin s) => choices (Fin.castLE i)) c0)

              Hoare-time first-halt extraction, preserving the Hoare postcondition at the first halted prefix.

              theorem Complexity.TM.HoareTime.toNTM {n : } {tm : TM n} {pre post : TapePred n} {b : } (h : tm.HoareTime pre post b) :
              tm.toNTM.HoareTime pre post b

              A deterministic Hoare triple lifts to an NTM Hoare triple for TM.toNTM. The bound is unchanged because toNTM ignores the choice bit.