Size-aware encodings #
Interface between ordinary types and the ambient model: a SizedEncoding α renders
a : α as a value encode a : Data, and esize a is its size (the length of its
preorder serialization Data.toBits), an honest bit-length up to a constant factor.
All polynomial-time statements (Cost.PolyTime) are relative to these encodings.
Conventions:
- bits are
false ↦ nilandtrue ↦ cons nil nil; - lists are
cons-chains ending innil, so a bit string of lengthnhas size between2n + 1and4n + 1; - natural numbers are encoded in binary (LSB first,
Nat.bits), soesize n ≤ 4 · Nat.size n + 1; - pairs are
cons:esize (a, b) = esize a + esize b + 1— length-additive pairing, with no separators; - programs are their tree encoding
Prog.toData, a length-faithful serialization (this, not any numeric Gödel numbering, is how programs occur as data).
This deliberately does not reuse Mathlib's Encodable/Primcodable: those encode into
ℕ via the quadratic pairing Nat.pair, whose iterated use distorts lengths exponentially.
The size of a value: the number of nodes of its encoding.
Equations
Instances For
Binary strings, the {0,1}* of the paper.
Equations
Instances For
Bits: false ↦ nil, true ↦ cons nil nil.
Equations
Instances For
Lists: cons-chains ending in nil.
Equations
- MIPRE.Cost.Data.ofList f [] = MIPRE.Cost.Data.nil
- MIPRE.Cost.Data.ofList f (a :: l) = (f a).cons (MIPRE.Cost.Data.ofList f l)
Instances For
Reading a list back, elementwise.
Equations
- MIPRE.Cost.Data.toList? g MIPRE.Cost.Data.nil = some []
- MIPRE.Cost.Data.toList? g (a.cons d) = do let x ← g a let l ← MIPRE.Cost.Data.toList? g d pure (x :: l)
Instances For
Equations
- MIPRE.Cost.instSizedEncodingBool = { encode := MIPRE.Cost.Data.ofBool, decode := MIPRE.Cost.Data.toBool?, decode_encode := MIPRE.Cost.Data.toBool?_ofBool }
Bit strings encode as lists of bits.
Equations
- MIPRE.Cost.instSizedEncodingBitStr = { encode := MIPRE.Cost.Data.ofList MIPRE.Cost.Data.ofBool, decode := MIPRE.Cost.Data.toList? MIPRE.Cost.Data.toBool?, decode_encode := ⋯ }
Folding Nat.bit over the binary digits of n recovers n.
Natural numbers encode in binary (LSB first): the bit string n.bits. This is the
encoding under which indices "n in binary" enter succinct descriptions (blueprint
def:succinct).
Equations
- One or more equations did not get rendered due to their size.
Length-additive pairing: encode (a, b) = cons (encode a) (encode b).
Equations
- One or more equations did not get rendered due to their size.
Programs as data #
Programs as data: a unary tag (0–6) paired with the fields.
Equations
- (MIPRE.Cost.Prog.var i).toData = (MIPRE.Cost.Data.ofNat 0).cons (MIPRE.Cost.Data.ofNat i)
- MIPRE.Cost.Prog.nil.toData = (MIPRE.Cost.Data.ofNat 1).cons MIPRE.Cost.Data.nil
- (h.cons t).toData = (MIPRE.Cost.Data.ofNat 2).cons (h.toData.cons t.toData)
- (MIPRE.Cost.Prog.elim i n c).toData = (MIPRE.Cost.Data.ofNat 3).cons ((MIPRE.Cost.Data.ofNat i).cons (n.toData.cons c.toData))
- (e.let_ b).toData = (MIPRE.Cost.Data.ofNat 4).cons (e.toData.cons b.toData)
- b.loop.toData = (MIPRE.Cost.Data.ofNat 5).cons b.toData
- (MIPRE.Cost.Prog.const d).toData = (MIPRE.Cost.Data.ofNat 6).cons d
Instances For
Reading a program back from its data (ofData_toData).
Equations
- One or more equations did not get rendered due to their size.
- MIPRE.Cost.Prog.ofData (MIPRE.Cost.Data.nil.cons i) = Option.map MIPRE.Cost.Prog.var i.toNat?
- MIPRE.Cost.Prog.ofData ((MIPRE.Cost.Data.nil.cons MIPRE.Cost.Data.nil).cons MIPRE.Cost.Data.nil) = some MIPRE.Cost.Prog.nil
- MIPRE.Cost.Prog.ofData x✝ = none
Instances For
Equations
- MIPRE.Cost.Prog.instSizedEncoding = { encode := MIPRE.Cost.Prog.toData, decode := MIPRE.Cost.Prog.ofData, decode_encode := MIPRE.Cost.Prog.ofData_toData }