Project

EarthSciAST

The EarthSciML Abstract Syntax Tree format: schema, operators, coupling, and five language bindings.

Contents

JSON values of an ESM document, in canonical form

Lean module Esm.Document.Json, formal/Esm/Document/Json.lean at 29ec73bddfdc
Imports Mathlib.Data.List.Nodup

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 Int64 is the integer literal (1.0 IS the integer 1), so it is JNum.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 source
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 when z fits in Int64).
  • dec neg ds e10: the value ±d₁.d₂d₃… × 10^e10, - when neg, with ds the shortest round-trip digits.
def InInt64 (z : Int) : Prop

z is representable in Int64.

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 int fits in Int64;
  • a dec has nonempty digits, each below 10, with no leading and no trailing zero, and is not an integral Int64 value (that number is an int, §5.5.3.1 rule 1).
def wfb : JNum → Bool

WF , as a check.

inductive Json source
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 WFList : List Json → Prop

Every element is well formed.

def WFObj : List (String × Json) → Prop

Every entry’s value is well formed.

def wfb : Json → Bool

WF , as a check.

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.

def wfmb : Json → Bool

WFm , as a check.

theorem WF.wfm proved source
theorem WF.wfm : ∀ {v : Json}, v.WF → v.WFm

A well-formed value is well formed with math leaves (it has none).

def size : Json → Nat

A size measure: the parsers’ fuel is bounded below by it.

This module also proves 11 lemmas without doc comments; they are in the source.