Unified Expression AST #
This file defines the unified AST for real expressions (Expr) and its
evaluation semantics. All numerical algorithms in LeanCert operate on this
single expression type.
Main definitions #
LeanCert.Core.Expr- The expression AST supporting algebraic and transcendental operationsLeanCert.Core.Expr.eval- Evaluation of expressions given a variable assignment
Design notes #
The expression type uses natural number indices for variables. This simplifies the interval evaluation and automatic differentiation implementations.
Auxiliary definition for atanh #
Since Mathlib doesn't provide Real.atanh, we define it here using the
standard formula: atanh x = (1/2) * log((1+x)/(1-x)) for |x| < 1.
atanh is strictly monotone on (-1, 1)
The error function: erf(x) = (2/√π) ∫₀ˣ exp(-t²) dt. Essential for statistical and financial modeling (normal distribution CDF). Uses interval integral notation (∫ t in 0..x) which handles negative x correctly.
Instances For
The error function is bounded above by 1.
The error function is bounded below by -1.
Named mathematical constants with known interval bounds. Adding a new constant (e.g., Catalan's) only requires extending this enum and its lookup tables — zero evaluator files need updating.
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
The real value of a named mathematical constant.
Equations
Instances For
Float approximation for heuristic evaluation (unverified).
Equations
- LeanCert.Core.MathConst.pi.toFloat = 3.141592653589793
- LeanCert.Core.MathConst.eulerMascheroni.toFloat = 0.5772156649015329
Instances For
Rational approximation for display/debugging (unverified).
Equations
- LeanCert.Core.MathConst.pi.toRatApprox = 157 / 50
- LeanCert.Core.MathConst.eulerMascheroni.toRatApprox = 577 / 1000
Instances For
Unified AST for real-valued expressions.
- const
(q : ℚ)
: Expr
Rational constant
- var
(idx : ℕ)
: Expr
Variable with de Bruijn-style index
- add
(e₁ e₂ : Expr)
: Expr
Addition
- mul
(e₁ e₂ : Expr)
: Expr
Multiplication
- neg
(e : Expr)
: Expr
Negation
- inv
(e : Expr)
: Expr
Multiplicative inverse (partial: undefined at 0)
- exp
(e : Expr)
: Expr
Exponential function
- sin
(e : Expr)
: Expr
Sine function
- cos
(e : Expr)
: Expr
Cosine function
- log
(e : Expr)
: Expr
Natural logarithm (partial: undefined for x ≤ 0)
- atan
(e : Expr)
: Expr
Arctangent function
- arsinh
(e : Expr)
: Expr
Inverse hyperbolic sine (arsinh)
- atanh
(e : Expr)
: Expr
Inverse hyperbolic tangent (partial: undefined for |x| ≥ 1)
- sinc
(e : Expr)
: Expr
Sinc function: sinc(x) = sin(x)/x for x ≠ 0, sinc(0) = 1
- erf
(e : Expr)
: Expr
Error function: erf(x) = (2/√π) ∫₀ˣ exp(-t²) dt
- sinh
(e : Expr)
: Expr
Hyperbolic sine: sinh(x) = (exp(x) - exp(-x)) / 2
- cosh
(e : Expr)
: Expr
Hyperbolic cosine: cosh(x) = (exp(x) + exp(-x)) / 2
- tanh
(e : Expr)
: Expr
Hyperbolic tangent: tanh(x) = sinh(x) / cosh(x) ∈ (-1, 1)
- sqrt
(e : Expr)
: Expr
Square root (partial: undefined for x < 0)
- namedConst
(c : MathConst)
: Expr
A named mathematical constant (π, γ, …) looked up from a table.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- LeanCert.Core.instReprExpr = { reprPrec := LeanCert.Core.instReprExpr.repr }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
Evaluate an expression given a variable assignment ρ : Nat → ℝ
Equations
- LeanCert.Core.Expr.eval ρ (LeanCert.Core.Expr.const a) = ↑a
- LeanCert.Core.Expr.eval ρ (LeanCert.Core.Expr.var a) = ρ a
- LeanCert.Core.Expr.eval ρ (a.add a_1) = LeanCert.Core.Expr.eval ρ a + LeanCert.Core.Expr.eval ρ a_1
- LeanCert.Core.Expr.eval ρ (a.mul a_1) = LeanCert.Core.Expr.eval ρ a * LeanCert.Core.Expr.eval ρ a_1
- LeanCert.Core.Expr.eval ρ a.neg = -LeanCert.Core.Expr.eval ρ a
- LeanCert.Core.Expr.eval ρ a.inv = (LeanCert.Core.Expr.eval ρ a)⁻¹
- LeanCert.Core.Expr.eval ρ a.exp = Real.exp (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.sin = Real.sin (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.cos = Real.cos (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.log = Real.log (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.atan = Real.arctan (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.arsinh = Real.arsinh (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.atanh = LeanCert.Core.Real.atanh (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.sinc = Real.sinc (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.erf = LeanCert.Core.Real.erf (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.sinh = Real.sinh (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.cosh = Real.cosh (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.tanh = Real.tanh (LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ a.sqrt = √(LeanCert.Core.Expr.eval ρ a)
- LeanCert.Core.Expr.eval ρ (LeanCert.Core.Expr.namedConst a) = a.toReal
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Evaluate e as a scalar function of variable idx, with all other
variables fixed by ρ. This represents the map t ↦ eval ρ[idx ↦ t] e.
Instances For
The set of free variable indices in an expression
Equations
- (LeanCert.Core.Expr.const a).freeVars = ∅
- (LeanCert.Core.Expr.var a).freeVars = {a}
- (a.add a_1).freeVars = a.freeVars ∪ a_1.freeVars
- (a.mul a_1).freeVars = a.freeVars ∪ a_1.freeVars
- a.neg.freeVars = a.freeVars
- a.inv.freeVars = a.freeVars
- a.exp.freeVars = a.freeVars
- a.sin.freeVars = a.freeVars
- a.cos.freeVars = a.freeVars
- a.log.freeVars = a.freeVars
- a.atan.freeVars = a.freeVars
- a.arsinh.freeVars = a.freeVars
- a.atanh.freeVars = a.freeVars
- a.sinc.freeVars = a.freeVars
- a.erf.freeVars = a.freeVars
- a.sinh.freeVars = a.freeVars
- a.cosh.freeVars = a.freeVars
- a.tanh.freeVars = a.freeVars
- a.sqrt.freeVars = a.freeVars
- (LeanCert.Core.Expr.namedConst a).freeVars = ∅
Instances For
Backward-compat alias for simp lists referencing eval_pi.
Single-variable expressions #
For 1D optimization and root finding, we often work with expressions that only use variable 0. These lemmas establish that such expressions can be evaluated equivalently with different environment representations.
Check if an expression only uses variable 0 (computable)
Equations
- (LeanCert.Core.Expr.const a).usesOnlyVar0 = true
- (LeanCert.Core.Expr.var a).usesOnlyVar0 = (a == 0)
- (a.add a_1).usesOnlyVar0 = (a.usesOnlyVar0 && a_1.usesOnlyVar0)
- (a.mul a_1).usesOnlyVar0 = (a.usesOnlyVar0 && a_1.usesOnlyVar0)
- a.neg.usesOnlyVar0 = a.usesOnlyVar0
- a.inv.usesOnlyVar0 = a.usesOnlyVar0
- a.exp.usesOnlyVar0 = a.usesOnlyVar0
- a.sin.usesOnlyVar0 = a.usesOnlyVar0
- a.cos.usesOnlyVar0 = a.usesOnlyVar0
- a.log.usesOnlyVar0 = a.usesOnlyVar0
- a.atan.usesOnlyVar0 = a.usesOnlyVar0
- a.arsinh.usesOnlyVar0 = a.usesOnlyVar0
- a.atanh.usesOnlyVar0 = a.usesOnlyVar0
- a.sinc.usesOnlyVar0 = a.usesOnlyVar0
- a.erf.usesOnlyVar0 = a.usesOnlyVar0
- a.sinh.usesOnlyVar0 = a.usesOnlyVar0
- a.cosh.usesOnlyVar0 = a.usesOnlyVar0
- a.tanh.usesOnlyVar0 = a.usesOnlyVar0
- a.sqrt.usesOnlyVar0 = a.usesOnlyVar0
- (LeanCert.Core.Expr.namedConst a).usesOnlyVar0 = true