Documentation

MIPRE.Foundations.Cost.Codable

Data as a primcodable type #

Data is denumerable through Cantor pairing (Data.encodeNat/Data.decodeNat), hence Primcodable, with primitive recursive constructor, projections, a tree-recursion principle (Data.primrec_recD), unary numerals, list indexing and the decoders of bit strings. This is the interface between the ambient model and Mathlib's computability (Cost/Partrec.lean): everything the model manipulates is Data — programs included, through Prog.toData — so no separate encoding of Prog is needed.

Data ≃ ℕ #

Data as natural numbers through Cantor pairing: nil ↦ 0, cons a b ↦ pair a b + 1.

Equations
Instances For

    Data ≃ ℕ.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Primitive recursive constructor and projections #

      The head of a pair (nil on nil).

      Equations
      Instances For

        The tail of a pair (nil on nil).

        Equations
        Instances For
          @[simp]
          theorem MIPRE.Cost.Data.left_cons (a b : Data) :
          (a.cons b).left = a
          @[simp]
          theorem MIPRE.Cost.Data.right_cons (a b : Data) :
          (a.cons b).right = b
          theorem MIPRE.Cost.Data.primrec_ite_nil {α : Type u_1} {σ : Type u_2} [Primcodable α] [Primcodable σ] {f : α → Data} {g : α → σ} {h : α → Data → Data → σ} (hf : Primrec f) (hg : Primrec g) (hh : Primrec fun (p : α × Data × Data) => h p.1 p.2.1 p.2.2) :
          Primrec fun (a : α) => if f a = nil then g a else h a (f a).left (f a).right

          Case analysis on Data, primitive recursively.

          Tree recursion #

          def MIPRE.Cost.Data.recD {σ : Type u_1} (base : σ) (step : Data → Data → σ → σ → σ) :
          Data → σ

          Structural recursion on Data with a first-order result.

          Equations
          Instances For
            @[simp]
            theorem MIPRE.Cost.Data.recD_nil {σ : Type u_1} (base : σ) (step : Data → Data → σ → σ → σ) :
            recD base step nil = base
            @[simp]
            theorem MIPRE.Cost.Data.recD_cons {σ : Type u_1} (base : σ) (step : Data → Data → σ → σ → σ) (a b : Data) :
            recD base step (a.cons b) = step a b (recD base step a) (recD base step b)
            theorem MIPRE.Cost.Data.getD_range_map {σ : Type u_1} [Inhabited σ] (F : ℕ → σ) {n i : ℕ} (hi : i < n) :
            theorem MIPRE.Cost.Data.primrec_recD {σ : Type u_1} [Primcodable σ] [Inhabited σ] (base : σ) (step : Data → Data → σ → σ → σ) (hstep : Primrec fun (p : (Data × Data) × σ × σ) => step p.1.1 p.1.2 p.2.1 p.2.2) :
            Primrec (recD base step)

            Tree recursion is primitive recursive (through the pairing encoding, structural recursion on Data is strong recursion on ℕ).

            Unary numerals and list indexing #

            The length of the right spine: reads a unary numeral (unaryToNat (ofNat n) = n).

            Equations
            Instances For

              The n-th element of a cons-chain (nil beyond the end).

              Equations
              Instances For

                Decoding bit strings #

                theorem MIPRE.Cost.Data.toList?_eq_recD {α : Type u_1} (g : Data → Option α) (d : Data) :
                toList? g d = recD (some []) (fun (a x : Data) (x_1 rb : Option (List α)) => (g a).bind fun (x : α) => Option.map (fun (x_2 : List α) => x :: x_2) rb) d
                theorem MIPRE.Cost.Data.primrec_toList? {α : Type u_1} [Primcodable α] {g : Data → Option α} (hg : Primrec g) :

                Decoding bit strings from Data is primitive recursive.

                Encoding naturals #

                The companion of primrec_decode_bitStr in the other direction. A natural is encoded as the bit string of Nat.bits, so its encoding satisfies the halving recursion below, and strong recursion on ℕ turns that into a primitive recursive definition. Every budgeted run of the tabulation feeds the machine an encode (n, …), so this is on the critical path of MIPRE.Halting.tab_computable.

                The binary encoding of a natural as data, in recursive form.

                Encoding a natural as data is primitive recursive.

                Two small gaps in the ambient library #

                theorem MIPRE.Cost.primrec_nat_pow :
                Primrec₂ fun (x1 x2 : ℕ) => x1 ^ x2

                Exponentiation is primitive recursive. Mathlib has Nat.Primrec.pow on the unpaired form but no Primrec₂ spelling.

                theorem MIPRE.Cost.List.idxOf_congr_inst {α : Type u_1} (i1 i2 : BEq α) [LawfulBEq α] [LawfulBEq α] (a : α) (l : List α) :

                Two LawfulBEq instances give the same List.idxOf. List Bool has its own BEq, while Mathlib's Primrec.list_idxOf is stated at the one derived from DecidableEq; the two are propositionally equal, but reducing them to a common normal form sends whnf a very long way indeed, so the bridge is proved once, here, rather than left to unification.

                The index of a bit string in a list of bit strings, at the instance the enumerations of Halting/Enumerate.lean actually elaborate to.