Project

EarthSciAST

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

Contents

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

Unit preservation for scalar expressions (esm-spec §4.8.3–4.8.4)

Lean module Esm.Units.ScalarCheck, formal/Esm/Units/ScalarCheck.lean at d23ee3b8c2d5
Imports Esm.Expr.ArrayConformance, Esm.Units.Laws

These rules consume the unit verdicts of their child expressions. A successful verdict can name a resolved unit or report an undeterminable one. Invalid declarations and mismatches remain errors. A scalar expression’s evaluator does not need its unit annotation, so the verdicts are passed separately from the expression AST.

def PureAngle (d : Dim) : Prop

A nonzero power of the plane-angle axis, with all other axes absent.

def Compatible (u v : U) : Prop

The exact unit comparison for extrema and ifelse. A pure angle can agree with a pure number at the same scale. Comparing two angular operands still requires the same dimension.

def chooseUnit (known : List U) : Option U

When a pure number and a pure angle meet, select the pure number’s unit. Otherwise retain the first determinable operand’s unit.

def checkKnown (known : List U) : Verdict

Check every pair of determinable inputs. This also compares two angle operands when a pure number appears between them.

def checkExtrema (operands : List Verdict) : Verdict

The min/max rule and the value-operand part of the ifelse rule.

def checkIfelse (condition yes no : Verdict) : Verdict

Check the condition for errors but derive the result from both branches.

Sound rescaling on the exact scalar evaluator

noncomputable def scaleValue (s : ℝ) (v : Num) : Num

Multiply a finite value by a scale and preserve a NaN.

theorem checked_extremum_scaling (maximum : Bool) (a b : U × Num)
    (rest : List (U × Num)) (result : U)
    (h : checkKnown ((a :: b :: rest).map Prod.fst) = .ok (some result)) :
    applyStrict (if maximum then .max else .min)
        ((a :: b :: rest).map (fun x => .num (scaleValue x.1.scale.real x.2))) =
      (applyStrict (if maximum then .max else .min)
        ((a :: b :: rest).map (fun x => .num x.2))).map (scaleValue result.scale.real)

With a determinable unit on every operand, the checked min or max commutes with conversion from its authored units to their canonical scales. Undeterminable operands need a separate semantic premise about their numbers.

theorem checked_ifelse_scaling (condition : Verdict) (c : Num) (cs : ℝ) (hc : 0 < cs)
    (yes no : U × Num) (result : U)
    (h : checkIfelse condition (.ok (some yes.1)) (.ok (some no.1)) = .ok (some result)) :
    (if truthy (scaleValue cs c) then scaleValue yes.1.scale.real yes.2
      else scaleValue no.1.scale.real no.2) =
    scaleValue result.scale.real (if truthy c then yes.2 else no.2)

The ifelse truth test selects the same branch under a positive condition rescaling. Given the evaluated branch values in determinable units, converting the selected branch and converting the result give the same value.

Compositional soundness for resolved scalar arithmetic

This fragment uses the same numeric expression AST and evaluator as the conformance adapter. Its inputs carry their exact declared units. Inference here requires every subexpression to have a determinable unit. The neutral literal rule permits a broader set of expressions in a document, but does not assign a conversion scale to a bare number (§4.8.4–4.8.6).

def checkPure (u : U) : Verdict

Strict transcendentals admit a scale-one pure number, including the pure-angle exception of §4.8.3.

def checkAtan2 (y x : U) : Verdict

The two coordinates passed to atan2 must have the same dimension and exact scale (§4.8.3).

def rationalExponent (q : ℚ) : Ast

A folded rational exponent is written as the exact numerator divided by its nonzero denominator, both integer literals.

inductive ScalarTerm source
inductive ScalarTerm where
  | var (name : String)
  | unary (op : Unary) (arg : ScalarTerm)
  | binary (op : Binary) (left right : ScalarTerm)
  | power (base : ScalarTerm) (exponent : ℚ)
  | ifelse (condition yes no : ScalarTerm)

A scalar expression over declared inputs and the operator forms handled by the exact unit rescaling theorem. Conditions can carry a unit of their own.

def ScalarTerm.check (units : List (String × U)) : ScalarTerm → Verdict

The checker for the resolved fragment. An absent declaration yields an undeterminable unit and keeps the tree outside the rescaling guarantee.

abbrev ScalarBindings

Declarations pairing each unit with its authored numeric input. Both numeric environments use the same names and shadowing as the checker.

theorem checked_scalar_rescaling (bindings : ScalarBindings) (term : ScalarTerm) (result : U)
    (checked : term.check (scalarUnits bindings) = .ok (some result)) :
    ∃ value : Num, eval term.ast (scalarInputs bindings) = .ok (.num value) ∧
      eval term.ast (canonicalInputs bindings) = .ok (.num (scaleValue result.scale.real value))

For any expression in the resolved scalar fragment, checking fixes the conversion scale of the result. Converting all of its declared inputs before evaluation gives exactly the canonical value of the authored computation.

A bare additive literal can pass dimensional checking without specifying how that literal must change when the surrounding expression is converted.

Checker controls. These compute the verdicts with the exact scale comparison.

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