Documentation

Complexitylib.Encoding.Data

Main internal data type for the rose tree machine (RTM) #

This file contains the main internal data structure for the RTM, Data, a rose tree.

Main definitions and notations #

Rose-tree data structure, it allows us to encode most of Lean's data structures in a "natural" manner

Instances For

    Decidable equality for Data, defined jointly with Data.listDecEq.

    Equations
    Instances For

      Decidable equality for List Data, defined jointly with Data.decEq.

      Equations
      Instances For
        @[reducible, inline]

        The empty Data node, Data.l [].

        Equations
        Instances For

          The list of children of a Data node.

          Equations
          Instances For
            @[simp]
            @[simp]
            theorem Complexity.Data.l_asList (xs : List Data) :
            (l xs).asList = xs
            @[irreducible]

            The encoding length of d, relevant for complexity. This is the encoded size assuming an encoding into parenthesized expressions.

            Equations
            Instances For
              @[simp]
              theorem Complexity.Data.size_le {d : Data} :
              0 < d.size
              @[simp]
              theorem Complexity.Data.cons_size {h : Data} {t : List Data} :
              (l (h :: t)).size = h.size + (l t).size
              theorem Complexity.Data.size_lt_of_mem {c : Data} {xs : List Data} (hc : c xs) :
              c.size < (l xs).size
              @[irreducible]
              def Complexity.Data.recL {motive : DataSort u_1} (nil : motive (l [])) (cons : (x : Data) → (xs : List Data) → motive xmotive (l xs)motive (l (x :: xs))) (d : Data) :
              motive d

              Recursion principle for Data.

              Equations
              Instances For
                theorem Complexity.Data.inductionL {motive : DataProp} (nil : motive (l [])) (cons : ∀ (x : Data) (xs : List Data), motive xmotive (l xs)motive (l (x :: xs))) (d : Data) :
                motive d

                Induction principle for Data, the Prop-valued companion to Data.recL.

                Bitstring serialization #

                Data.toBits serializes a Data value into a List Bool using a parenthesized (balanced-bracket) encoding: false opens a node, its children are serialized in order, and true closes the node. This matches Data.size exactly (Data.length_toBits) and is injective (Data.toBits_injective), so any DataEncode instance yields an injective bitstring encoding (see Complexitylib.Encoding.DataEncode).

                @[irreducible]

                Serialize Data into a bitstring with a parenthesized (balanced-bracket) encoding: false opens a node, the children are serialized in order, and true closes the node.

                Equations
                Instances For

                  One step of the stack-based Data.fromBits parser. The state is a stack of frames, each a list of the sibling nodes completed so far at that nesting depth (outermost frame at the bottom). Reading false opens a new (empty) frame; reading true closes the top frame into a Data.l node and appends it to its parent. none is a permanent failure state (an unmatched true).

                  Equations
                  Instances For

                    Decode a bitstring produced by Data.toBits back into a Data value, or none if it is not a valid single serialization. This is a left inverse of Data.toBits (Data.fromBits_toBits).

                    Equations
                    Instances For
                      theorem Complexity.Data.foldl_fromBitsStep_toBits (d : Data) (top : List Data) (rest : List (List Data)) :
                      List.foldl fromBitsStep (some (top :: rest)) d.toBits = some ((top ++ [d]) :: rest)

                      Running Data.fromBitsStep over d.toBits appends the decoded d to the top frame of the stack, leaving the rest of the stack untouched. This is the key lemma behind Data.fromBits_toBits.

                      @[simp]

                      Data.fromBits recovers any value serialized by Data.toBits.

                      Data.toBits is injective: the parenthesized serialization determines the value. This follows from Data.fromBits being a left inverse.