JSON values of an ESM document, in canonical form
The value type the document codecs (Esm.Document.Yaml
, Esm.Document.TypstDict
)
print and read. Numbers are already in the canonical form of CONFORMANCE §5.5.3.1:
- rule 1 and rule 3: a number whose value is integral and representable in
Int64is the integer literal (1.0IS the integer1), so it isJNum.int; - rule 2: every other finite number is
JNum.dec, its shortest round-trip decimal digits and the decimal exponent of the first digit. Binary64 is not modelled; the shortest digits are given data.
Object key order is part of the value (the codecs preserve it; they never sort).
inductive JNum where
| int (z : Int)
| dec (neg : Bool) (digits : List Nat) (e10 : Int)
deriving DecidableEq, Repr
A JSON number in canonical form.
int z: an integer literal (well formed whenzfits inInt64).dec neg ds e10: the value±d₁.d₂d₃… × 10^e10,-whenneg, withdsthe shortest round-trip digits.
def digitsVal (ds : List Nat) : Nat
The value of a big-endian decimal digit list.
def decInt? (neg : Bool) (ds : List Nat) (e10 : Int) : Option Int
The integer a decimal denotes, when the decimal is integral (its last digit sits at
or left of the units place), else none.
def WF : JNum → Prop
Canonical form of a number:
- an
intfits inInt64; - a
dechas nonempty digits, each below 10, with no leading and no trailing zero, and is not an integralInt64value (that number is anint, §5.5.3.1 rule 1).
inductive Json where
| null
| bool (b : Bool)
| num (n : JNum)
| str (s : String)
| arr (xs : List Json)
| obj (kvs : List (String × Json))
| math (s : String)
deriving Repr
A JSON value. Strings and keys are arbitrary Lean strings (any Unicode scalar values); object entries keep their order.
math s is not JSON: it is the math text of an expression inside a path block, the
form a container prints and reads there (Source.mathify
, Source.cookBlocks
). It
lives only between a container’s text and the skeleton; a document never holds one
(WF
is false on it).
def mathOK (s : String) : Bool
Math text a container can delimit with $: not empty, no $, no line break, and
not ending in a backslash (which would escape the closing $).
def mathBody : List Char → Option (List Char × List Char)
The text of a math leaf after its $, up to the next $.
def pMath (r : List Char) : Option (Json × List Char)
A math leaf after its $: the text up to the next $, which must be mathOK
.
def WF : Json → Prop
Well formed: every number is canonical, every object has distinct keys, and there is no math leaf.
def WFm : Json → Prop
Well formed with math leaves: WF
, except that a math leaf whose text is mathOK
is allowed. What the codecs print and read: a path block’s value as a container holds
it.
theorem WF.wfm : ∀ {v : Json}, v.WF → v.WFm
A well-formed value is well formed with math leaves (it has none).