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.
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)
- pi : Expr
The mathematical constant π
Instances For
Equations
- LeanCert.Core.instReprExpr = { reprPrec := LeanCert.Core.instReprExpr.repr }
Equations
- One or more equations did not get rendered due to their size.
- LeanCert.Core.instReprExpr.repr LeanCert.Core.Expr.pi prec✝ = Repr.addAppParen (Std.Format.nest (if prec✝ ≥ 1024 then 1 else 2) (Std.Format.text "LeanCert.Core.Expr.pi")).group prec✝
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Equations
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.pi = Real.pi
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.pi.freeVars = ∅
Instances For
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.pi.usesOnlyVar0 = true