Project

EarthSciAST

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

Contents

This is the documentation for the older version v0.15.1. For the latest release, see v0.17.0.

The skeleton as a sequence of constructs

Lean module Esm.Document.Source.Items, formal/Esm/Document/Source/Items.lean at 86bed77c797f
Imports Esm.Document.Source.SkeletonRT, Esm.Document.Notation

A container spells a skeleton as a sequence of constructs: description blocks, path blocks, component directives, declarations, equations (model, initialization and constraint) and reactions. This is the container-independent middle layer:

  • items lists the constructs of a skeleton in the printer’s order;
  • unitems reads a construct sequence back into a skeleton. A construct belongs to the section of the last component directive before it, or to the document when there is none; within a section, constructs may come in any order (a document can interleave declarations and equations with its prose), except that the math entries of one field keep their order. unitems_items is the round trip.

Math entries are held as the container’s math text (Doc MR) at this layer; rawDoc prints them under each component’s symbol table and cookDoc reads them back (cookDoc_rawDoc ), the symbol table coming from the rest of the skeleton (symFor_mapMath), so both sides use the same table.

inductive RItem source
inductive RItem where
  | desc (s : String)
  | block (k : String) (v : Json)
  | comp (K : Kind) (name : String)
  | decl (d : Decl)
  | eqn (raw : String)
  | eqx (r : Role) (raw : String)
  | rxn (x : Rxn String)
  | plot (attrs : List (String × Json))
  | tm (t : Tm String)

One construct of a document. An equation is its math text, uninterpreted; so is a reaction’s rate. eqn is a model equation, eqx an equation of the field of its role (the containers write it for initialization equations and constraints).

def mItem : MR → RItem

The construct of a math entry.

def compItems (c : Comp MR) : List RItem

The constructs of a component section.

def items (D : Doc MR) : List RItem

The constructs of a skeleton, in the printer’s order: the document description, the top-level blocks, the top-level templates, then each component section.

Reading constructs back

def Comp.fresh (K : Kind) (n : String) : Comp MR

A fresh component section.

def addTo (c : Comp MR) : RItem → Option (Comp MR)

Add a construct to a component section. A second description is refused (and a math field written twice is refused when the section is rebuilt).

structure St source
structure St where
  desc : Option String
  blocks : List (String × Json)
  math : List MR
  comps : List (Comp MR)
  cur : Option (Comp MR)

The reader’s state: the document description, blocks and templates, the finished sections, and the open one.

def step (st : St) : RItem → Option St

Read one construct. Before the first directive only a description, blocks and templates are allowed.

def unitems (front : List (String × Json)) (its : List RItem) : Option (Doc MR)

Read a construct sequence (with the front matter) into a skeleton.

The round trip

def itemsOK (D : Doc MR) : Prop

A skeleton the constructs can carry: a component’s blocks are not keyed by a math field, and its math blocks are (the parser files a block by its key); the top-level entries are templates.

theorem unitems_items proved source
theorem unitems_items (D : Doc MR) (hD : itemsOK D) : unitems D.front (items D) = some D

The construct round trip.

Expressions inside path blocks

Notation.compExprPaths and Notation.topExprPaths list the expression-valued fields of path blocks. The printer turns a value there into a math leaf (Json.math) when it is a well-formed expression whose math text the container can delimit (Json.mathOK , and the container’s own test ok: Yaml.plainMathOK for Markdown); the reader reads every math leaf back, wherever it is.

def stepKey (k : Option String) (ps : List (List Notation.Seg)) : List (List Notation.Seg)

The paths below the entry k of an object, or below an element of an array (none, which Seg.any and Seg.elem match).

def atPaths (leaf : Json → Json) (ps : List (List Notation.Seg)) : Json → Json

leaf applied to every value of v at one of the paths ps (the shortest first: a value at a path is not looked into); every other value as it is.

def mathLeaf (prR : Sym → Json → String) (ok : String → Bool) (S : Sym) (v : Json) : Json

A value at an expression path: its math text, when it is a well-formed expression whose text the container can delimit (Json.mathOK and ok); otherwise the value.

def mathify (prR : Sym → Json → String) (ok : String → Bool) (S : Sym)
    (ps : List (List Notation.Seg)) (bs : List (String × Json)) : List (String × Json)

Print a section’s path blocks: every expression at one of the paths ps (keyed by the block’s key) as math under the symbol table S.

def cookV (ck : String → Option Json) : Json → Option Json

Every math leaf of a value read back with ck.

def cookBlocks (ckR : Sym → String → Option Json) (S : Sym) (bs : List (String × Json)) :
    Option (List (String × Json))

Read a section’s path blocks: every math leaf, wherever it is, read back as an expression under the symbol table S.

The path-block round trip

theorem cookV_wf proved source
theorem cookV_wf (ck : String → Option Json) : ∀ v : Json, v.WF → cookV ck v = some v

A value with no math leaf reads back as itself.

theorem cookV_mathLeaf proved source
theorem cookV_mathLeaf (prR : Sym → Json → String) (ok : String → Bool) (ck : String → Option Json) (S : Sym)
    (hck : ∀ e, isE e = true → ck (prR S e) = some e) (v : Json) (hv : v.WF) :
    cookV ck (mathLeaf prR ok S v) = some v

The math leaf of a value reads back as the value.

theorem cookBlocks_mathify proved source
theorem cookBlocks_mathify (prR : Sym → Json → String) (ok : String → Bool) (ckR : Sym → String → Option Json)
    (S : Sym) (hck : ∀ e, isE e = true → ckR S (prR S e) = some e) (ps : List (List Notation.Seg)) :
    ∀ bs : List (String × Json), (∀ kv ∈ bs, kv.2.WF) → cookBlocks ckR S (mathify prR ok S ps bs) = some bs

Path blocks read back: the math the printer put in reads back under the same table, and every other value is as it was.

What the printer leaves for the codecs

theorem mathify_wfm proved source
theorem mathify_wfm (prR : Sym → Json → String) (ok : String → Bool) (S : Sym) (ps : List (List Notation.Seg))
    (bs : List (String × Json)) (h : ∀ kv ∈ bs, kv.2.WF) :
    ∀ kv ∈ mathify prR ok S ps bs, kv.2.WFm

Every printed block value is well formed with math leaves.

The symbol tables do not change

theorem atPaths_nil proved source
theorem atPaths_nil (leaf : Json → Json) : ∀ v : Json, atPaths leaf [] v = v

Away from every path the value is as it was.

theorem keysOf_atPaths proved source
theorem keysOf_atPaths (leaf : Json → Json) (ps : List (List Notation.Seg)) (h : ps.contains [] = false)
    (v : Json) : keysOf (some (atPaths leaf ps v)) = keysOf (some v)

With no path ending here, an object keeps its keys.

theorem keysOf_mathify proved source
theorem keysOf_mathify (prR : Sym → Json → String) (ok : String → Bool) (S : Sym) (ps : List (List Notation.Seg)) (k : String)
    (h : (stepKey (some k) ps).contains [] = false) (bs : List (String × Json)) :
    keysOf (look k (mathify prR ok S ps bs)) = keysOf (look k bs)

The keys of a block are the same after printing, when no path ends at the block.

theorem look_mathify_none proved source
theorem look_mathify_none (prR : Sym → Json → String) (ok : String → Bool) (S : Sym) (ps : List (List Notation.Seg)) (k : String)
    (h : stepKey (some k) ps = []) (bs : List (String × Json)) :
    look k (mathify prR ok S ps bs) = look k bs

A block no path starts at is the same after printing.

def mathifyDoc {α ρ : Type} (prR : Sym → Json → String) (ok : String → Bool) (D : Doc (Mx α ρ)) : Doc (Mx α ρ)

Print the path blocks of a skeleton: each section’s under its symbol table (a component’s, or the document’s for the top-level blocks).

def cookDocBlocks {α ρ : Type} (ckR : Sym → String → Option Json) (D : Doc (Mx α ρ)) :
    Option (Doc (Mx α ρ))

Read the path blocks of a skeleton: every math leaf of a section’s blocks under the section’s symbol table.

theorem stepKey_table proved source
theorem stepKey_table :
    (stepKey (some "index_sets") Notation.topExprPaths).contains [] = false ∧
    stepKey (some "operators") Notation.topExprPaths = [] ∧
    (stepKey (some "variables") Notation.compExprPaths).contains [] = false ∧
    (stepKey (some "species") Notation.compExprPaths).contains [] = false ∧
    (stepKey (some "parameters") Notation.compExprPaths).contains [] = false

No expression path ends at a block, and none starts at the blocks the symbol tables read (index_sets, variables, species, parameters, operators).

theorem topSym_mathifyDoc proved source
theorem topSym_mathifyDoc {α ρ : Type} (prR : Sym → Json → String) (ok : String → Bool) (D : Doc (Mx α ρ)) :
    topSym (mathifyDoc prR ok D) = topSym D

The document’s table is the same after printing its blocks.

theorem symFor_mathifyDoc proved source
theorem symFor_mathifyDoc {α ρ : Type} (prR : Sym → Json → String) (ok : String → Bool) (D : Doc (Mx α ρ))
    (c : Comp (Mx α ρ)) :
    symFor (mathifyDoc prR ok D) { c with blocks := mathify prR ok (symFor D c) Notation.compExprPaths c.blocks } =
      symFor D c

A component’s table is the same after printing the blocks.

theorem cookDocBlocks_mathifyDoc {α ρ : Type} (prR : Sym → Json → String) (ok : String → Bool)
    (ckR : Sym → String → Option Json) (hck : ∀ S e, isE e = true → ckR S (prR S e) = some e)
    (D : Doc (Mx α ρ)) (hb : ∀ kv ∈ D.blocks, kv.2.WF)
    (hc : ∀ c ∈ D.comps, ∀ kv ∈ c.blocks, kv.2.WF) :
    cookDocBlocks ckR (mathifyDoc prR ok D) = some D

The path blocks of a skeleton read back when every expression does under its table (the math round trips parseLatex_printLatex, parseTypst_printTypst).

theorem items_mathifyDoc_ok (ok : RItem → Bool) (hblk : ∀ k v, ok (.block k v) = v.wfmb)
    (prR : Sym → Json → String) (mok : String → Bool) (X : Doc MR) (hX : ∀ it ∈ items X, ok it = true)
    (hb : ∀ kv ∈ X.blocks, kv.2.WF) (hc : ∀ c ∈ X.comps, ∀ kv ∈ c.blocks, kv.2.WF) :
    ∀ it ∈ items (mathifyDoc prR mok X), ok it = true

The printed constructs stay readable: printing the blocks only changes block values, each into one well formed with math leaves. ok is a container’s itemOK, whose test of a block is Json.wfmb .

theorem mem_mathify_key proved source
theorem mem_mathify_key {prR : Sym → Json → String} {ok : String → Bool} {S : Sym} {ps : List (List Notation.Seg)}
    {bs : List (String × Json)} {kv : String × Json} (h : kv ∈ mathify prR ok S ps bs) :
    ∃ kv0 ∈ bs, kv0.1 = kv.1

Printing the blocks keeps every key.

theorem itemsOK_mathifyDoc proved source
theorem itemsOK_mathifyDoc (prR : Sym → Json → String) (ok : String → Bool) (X : Doc MR) (h : itemsOK X) :
    itemsOK (mathifyDoc prR ok X)

The skeleton with printed blocks satisfies itemsOK when the skeleton does.

Math entries: printed under the symbol table, and read back

def rawDoc (pr : Sym → MC → MR) (D : Doc MC) : Doc MR

Print every math entry under its symbol table: a top-level template under the document’s, a component’s entry under the component’s.

def cookComp (ck : Sym → MR → Option (List MC)) (D : Doc MR) (c : Comp MR) : Option (Comp MC)

Read a component’s math entries under its symbol table; one math text may hold several equations (the multi-line variants).

def cookDoc (ck : Sym → MR → Option (List MC)) (D : Doc MR) : Option (Doc MC)

Read every math entry of a skeleton.

theorem topSym_rawDoc proved source
theorem topSym_rawDoc (pr : Sym → MC → MR) (hpr : ∀ S, HeadsKept (pr S)) (D : Doc MC) :
    topSym (rawDoc pr D) = topSym D

The tables of a printed skeleton are those of the skeleton: the printer keeps every header.

theorem cookDoc_rawDoc proved source
theorem cookDoc_rawDoc (pr : Sym → MC → MR) (ck : Sym → MR → Option (List MC))
    (hpr : ∀ S, HeadsKept (pr S)) (D : Doc MC)
    (ht : ∀ m ∈ D.math, ck (topSym D) (pr (topSym D) m) = some [m])
    (h : ∀ c ∈ D.comps, ∀ m ∈ c.math, ck (symFor D c) (pr (symFor D c) m) = some [m]) :
    cookDoc ck (rawDoc pr D) = some D

Math entries read back when each one does under its table. The table is the same on both sides: symFor and topSym read headers, never math, and the printer keeps headers.

A container’s math printer and reader, entry by entry

def prMx (prE : Sym → Eqn → String) (prR : Sym → Json → String) (S : Sym) : MC → MR

Print a math entry with the container’s equation printer and expression printer.

def ckMx (ckE : Sym → String → Option (List Eqn)) (ckR : Sym → String → Option Json) (S : Sym) :
    MR → Option (List MC)

Read a math entry with the container’s equation reader and expression reader.

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