Project

EarthSciAST

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

Contents

The initialization_equations migration of the canonical form (esm-spec §11.4.4)

Lean module Esm.Document.Source.InitMigration, formal/Esm/Document/Source/InitMigration.lean at 29ec73bddfdc
Imports Esm.Document.Source.Keys

initialization_equations is deprecated (esm 2.5.0). The canonical form, and so the Markdown and Typst printers that print it, rewrite the field as a loader does (esm-spec §11.4.4 and the deprecated_field row of §9.6.6), so a printed document never carries it. For each model, top level or an inline subsystem at any depth (deepest first):

  • first, an initial condition of a parameter (esm-spec §11.4.4 “An initial condition of a parameter”) becomes the parameter’s default: in a document declaring esm below 2.5.0, each equations entry whose left-hand side is exactly ic(k), then each initialization_equations entry whose left-hand side is k or ic(k), k declared in the model’s own variables with type "parameter", is removed and its right-hand side (a JSON number, else ic_invalid_argument) written as k’s default, in that order, a later one replacing an earlier value;

then, when the model holds initialization_equations, the field is removed and its remaining equations go:

  • a model with a time derivative (an equation of equations whose left-hand side holds an op D anywhere): each initialization equation, in the field’s order, is appended to equations with its lhs and rhs rewritten by wrap : every reference to an unknown w becomes ic(w) and every time derivative D(w) of an unknown becomes ic(D(w));
  • a model with none: each v ~ e whose left-hand side is a bare name declared in the model’s variables with type "unknown" becomes guesses[v] = e (in the field’s order, an entry guesses already has, or an earlier equation gave, is kept); any other equation is refused (deprecated_field, an error).

A document with a refused equation (either refusal above, or an initial condition of a parameter whose value is not a number) is left as it is (migrateOrKeep ): the canonical form of a document the loader rejects is the document unmigrated, and the printers then write its initialization container, as every binding does.

Unproved executable code (the printers’ round-trip theorems are about the canonical form this produces), pinned by the document-format golden files and the cross-binding gate.

def field (k : String) : Json → Option Json

Field k of an object.

def isUnknownVar (m : Json) (v : String) : Bool

Whether model m declares v in its variables with type "unknown".

def descend (m : Json) : List String → Option Json

The inline subsystem of m at a path of subsystem names.

def namesUnknown (root model : Json) (name : String) : Bool

Whether name names an unknown seen from model: a bare name declared in the model’s variables with type "unknown"; a scoped name a.b.….v (split at every ., the last segment the variable) resolving to such a declaration first through the model’s own subsystems (a, then b, …), then from the top-level model a through its subsystems; or, failing both, naming a species v of the top-level reaction system a (through its subsystems). root is the document before migration.

partial def containsD : Json → Bool

Whether an expression holds an op D node anywhere.

def icOf (a : Json) : Json

ic(a), keys sorted.

def exprKeys : List String

The expression-valued fields of a node the rewrite descends into (besides bindings, a name-keyed map of expressions).

partial def wrap (isU : String → Bool) : Json → Json

e with every unknown reference w written ic(w) and every time derivative D(w) (wrt absent, null or "t", first argument an unknown name) written ic(D(w)). An ic node is kept as it is. Other nodes are rebuilt with wrapIn applied to the values of exprKeys and to every value of bindings; their other fields are kept.

partial def wrapIn (isU : String → Bool) : Json → Json

wrap through arrays, at any depth.

def isParamVar (m : Json) (v : String) : Bool

Whether model m declares v in its variables with type "parameter".

def paramTarget (model : Json) (bare : Bool) (eq : Json) : Option String

The parameter of model an equation sets: its left-hand side is ic(k) (exactly: op ic, args the one name k), or, when bare, the name k; k a parameter the model declares.

def below250 (doc : Json) : Bool

Whether the document declares an esm version below 2.5.0 (its esm string’s first two dot-separated parts read as natural numbers; any other esm is not below).

partial def modelHas (legacy : Bool) : Json → Bool

Whether a model, or an inline subsystem of it at any depth, has something to migrate: the field, or (legacy: the document is below esm 2.5.0) an ic(k) equation of a parameter.

def takeParamICs (model : Json) (ptr fld : String) (bare : Bool) :
    Nat → List Json → Except String (List Json × List (String × Json))

The equations of es that set a parameter (paramTarget ), taken out: the others, and the (k, value) each sets, in order. A value that is not a number is refused. fld, i: the list’s field and the first equation’s index, for the refusal.

def setDefaults (kvs : List (String × Json)) (sets : List (String × Json)) :
    List (String × Json)

kvs (a model) with each parameter’s default set to its value, in order.

def refusal (ptr : String) (i : Nat) : String

The refusal of an initialization equation of a model with no time derivative.

def toGuesses (model : Json) (ptr : String) :
    Nat → List (String × Json) → List Json → Except String (List (String × Json))

The guesses a model with no time derivative gets: g with v ↦ e added for each v ~ e, in order, unless v is already there.

partial def migrateModel (root : Json) (legacy : Bool) (ptr : String) :
    Json → Except String Json

One model migrated (its subsystems first). root: the document before migration; ptr: the model’s JSON pointer, for the refusal.

def migrate (doc : Json) : Except String Json

Every model migrated, or the refusal of the first equation that has no reading. A document without the field is returned as it is.

def migrateOrKeep (doc : Json) : Json

The canonical form’s migration: migrate , or the document unmigrated when the loader would refuse it.