Documentation

Cslib.Foundations.Data.StackTape

StackTape: Infinite, eventually-none lists of Options #

This file defines StackTape, a stack-like data structure of Option values, where the tape is considered to be infinite and eventually all nones. This is useful as a data structure with a simple API for manipulation by Turing machines.

Design #

StackTape is represented as a list of Option values where the list cannot end with none. The end of the list is then treated as the start of an infinite sequence of none values by the low-level API. This design makes it convenient to express the length of the tape in terms of the list length.

An alternative design would be to represent the tape as a Stream' (Option Symbol), with additional fields tracking the length and the fact that the stream eventually becomes none. This design might complicate reasoning about the length of the tape, but could make other operations more straightforward.

Future design work might explore this alternative representation and compare its advantages and disadvantages.

TODO #

structure Turing.StackTape (Symbol : Type) :

An infinite tape representation using a list of Option values, where the list is eventually none.

Represented as a List (Option Symbol) that does not end with none.

  • toList : List (Option Symbol)

    The underlying list representation

  • toList_getLast?_ne_some_none : self.toList.getLast? some none

    The list can be empty (i.e. none), but if it is not empty, the last element is not (some) none

Instances For
    def Turing.StackTape.nil {Symbol : Type} :
    StackTape Symbol

    The empty StackTape

    Equations
    Instances For
      @[implicit_reducible]
      Equations
      @[implicit_reducible]
      Equations
      @[simp]
      @[simp]
      def Turing.StackTape.cons {Symbol : Type} (x : Option Symbol) (xs : StackTape Symbol) :
      StackTape Symbol

      Prepend an Option to the StackTape

      Equations
      Instances For
        @[simp]
        theorem Turing.StackTape.cons_some_toList {Symbol : Type} (a : Symbol) (l : StackTape Symbol) :
        (cons (some a) l).toList = some a :: l.toList
        def Turing.StackTape.tail {Symbol : Type} (l : StackTape Symbol) :
        StackTape Symbol

        Remove the first element of the StackTape, returning the rest

        Equations
        Instances For
          def Turing.StackTape.head {Symbol : Type} (l : StackTape Symbol) :
          Option Symbol

          Get the first element of the StackTape.

          Equations
          Instances For
            theorem Turing.StackTape.eq_iff {Symbol : Type} (l1 l2 : StackTape Symbol) :
            l1 = l2 l1.head = l2.head l1.tail = l2.tail
            @[simp]
            theorem Turing.StackTape.head_cons {Symbol : Type} (o : Option Symbol) (l : StackTape Symbol) :
            (cons o l).head = o
            @[simp]
            theorem Turing.StackTape.tail_cons {Symbol : Type} (o : Option Symbol) (l : StackTape Symbol) :
            (cons o l).tail = l
            @[simp]
            theorem Turing.StackTape.cons_head_tail {Symbol : Type} (l : StackTape Symbol) :
            cons l.head l.tail = l
            def Turing.StackTape.map_some {Symbol : Type} (l : List Symbol) :
            StackTape Symbol

            Create a StackTape from a list by mapping all elements to some

            Equations
            Instances For
              def Turing.StackTape.length {Symbol : Type} (l : StackTape Symbol) :

              The length of the StackTape is the number of elements up to the last non-none element

              Equations
              Instances For
                theorem Turing.StackTape.length_cons_some {Symbol : Type} (a : Symbol) (l : StackTape Symbol) :
                (cons (some a) l).length = l.length + 1
                theorem Turing.StackTape.length_cons_le {Symbol : Type} (o : Option Symbol) (l : StackTape Symbol) :
                (cons o l).length l.length + 1
                @[simp]
                theorem Turing.StackTape.length_map_some {Symbol : Type} (l : List Symbol) :
                @[simp]