Documentation

LeanCert.Engine.Extended

Extended Interval Arithmetic #

This file implements Extended Interval Arithmetic, which handles singularities by returning a disjoint union of intervals instead of a single interval.

Motivation #

Standard interval arithmetic returns overly conservative bounds when evaluating expressions with singularities. For example, 1/[-1, 1] returns (-∞, ∞) as a single interval, losing all useful information.

Extended interval arithmetic instead returns (-∞, -1] ∪ [1, ∞), preserving the fact that the result is bounded away from zero.

Main definitions #

Main theorems #

Design notes #

The largeBound parameter represents "infinity" for practical computation. Using 10^30 is sufficient for most numerical applications while keeping arithmetic tractable.

The Extended Interval Data Structure #

Extended Interval: A union of rational intervals. Used to handle singularities (e.g., 1/[-1, 1] becomes (-∞, -1] ∪ [1, ∞)).

Design choices:

  • Empty list represents the empty set (e.g., sqrt(-1) for strict domains)
  • Intervals in the list may overlap (we don't enforce disjointness for simplicity)
  • Operations conservatively handle all parts
  • The intervals making up this extended interval. Empty list means the empty set.

Instances For
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      The empty set (for undefined operations)

      Equations
      Instances For
        def LeanCert.Engine.ExtendedInterval.whole (large_bound : ) (h : 0 large_bound) :

        The entire real line (represented by a very wide interval)

        Equations
        Instances For

          Check if an extended interval is empty

          Equations
          Instances For

            Number of parts in the extended interval

            Equations
            Instances For

              Compute the convex hull: merge all parts into a single interval. Returns none if the extended interval is empty.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                Compute the convex hull with a default for empty intervals

                Equations
                Instances For

                  Membership: x is in the extended interval if it's in any part

                  Equations
                  Instances For
                    theorem LeanCert.Engine.ExtendedInterval.foldl_contains_head (t : List Core.IntervalRat) (h : Core.IntervalRat) :
                    have result := List.foldl (fun (acc I : Core.IntervalRat) => { lo := min acc.lo I.lo, hi := max acc.hi I.hi, le := }) h t; result.lo h.lo h.hi result.hi

                    Helper: the foldl operation maintains the property that the accumulated interval has lo ≤ head.lo and hi ≥ head.hi

                    theorem LeanCert.Engine.ExtendedInterval.foldl_contains_tail (t : List Core.IntervalRat) (h I : Core.IntervalRat) (hI : I t) :
                    have result := List.foldl (fun (acc J : Core.IntervalRat) => { lo := min acc.lo J.lo, hi := max acc.hi J.hi, le := }) h t; result.lo I.lo I.hi result.hi

                    Helper: the foldl operation maintains the property that the accumulated interval contains any element from the tail

                    theorem LeanCert.Engine.ExtendedInterval.mem_hull {x : } {E : ExtendedInterval} (hx : x E) (hne : E.parts []) :
                    x E.hull

                    If x is in some part, then x is in the hull. The proof follows from the fact that hull.lo = min of all lo's ≤ I.lo ≤ x and x ≤ I.hi ≤ max of all hi's = hull.hi.

                    Extended Inversion #

                    The default "large bound" representing infinity for extended arithmetic. Using 10^30 is sufficient for most numerical applications.

                    Equations
                    Instances For

                      Helper: defaultLargeBound is positive

                      Extended inversion: handles 1/I when I may contain zero.

                      Cases:

                      1. I strictly positive → standard inversion [1/hi, 1/lo]
                      2. I strictly negative → standard inversion [1/hi, 1/lo]
                      3. I straddles zero → split into two parts: (-∞, 1/lo] ∪ [1/hi, ∞)
                      4. I = [0, b] with b > 0 → [1/b, ∞)
                      5. I = [a, 0] with a < 0 → (-∞, 1/a]
                      6. I = [0, 0] → empty (1/0 is undefined)

                      NOTE: Uses large_bound (default 10^30) to represent ±∞. For inputs with |lo|, |hi| ≥ 10^-30, the bounds are guaranteed valid.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem LeanCert.Engine.mem_invExtended {x : } {I : Core.IntervalRat} (hx : x I) (hxne : x 0) (large_bound : := defaultLargeBound) (hlb : |x⁻¹| large_bound) :
                        x⁻¹ invExtended I large_bound

                        Soundness of extended inversion: if x ∈ I and x ≠ 0, then 1/x ∈ invExtended I.

                        The hypothesis hlb : |x⁻¹| ≤ large_bound ensures that 1/x fits within the finite bounds. This is the key assumption that makes extended inversion sound - without it, 1/x could be arbitrarily large for x close to 0.

                        NOTE: This theorem has remaining sorries because the invExtended function itself has design assumption sorries for interval validity when constructing intervals near zero. The membership logic is correct modulo these construction assumptions.

                        Lifting Operations to Extended Intervals #

                        Lift a unary operation to extended intervals

                        Equations
                        Instances For
                          theorem LeanCert.Engine.mem_liftUnary {f : } {op : Core.IntervalRatCore.IntervalRat} (hop : ∀ (x : ) (I : Core.IntervalRat), x If x op I) {x : } {E : ExtendedInterval} (hx : x E) :
                          f x liftUnary op E

                          Soundness of unary lifting

                          Lift a binary operation to extended intervals (Cartesian product)

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            theorem LeanCert.Engine.mem_liftBinary {f : } {op : Core.IntervalRatCore.IntervalRatCore.IntervalRat} (hop : ∀ (x y : ) (I J : Core.IntervalRat), x Iy Jf x y op I J) {x y : } {E₁ E₂ : ExtendedInterval} (hx : x E₁) (hy : y E₂) :
                            f x y liftBinary op E₁ E₂

                            Soundness of binary lifting

                            Extended Interval Evaluator #

                            Configuration for extended interval evaluation

                            • taylorDepth :

                              Taylor series depth for transcendental functions

                            • largeBound :

                              Large bound representing "infinity"

                            Instances For
                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                @[reducible, inline]

                                Environment mapping variable indices to extended intervals

                                Equations
                                Instances For

                                  Convert a standard interval environment to an extended environment

                                  Equations
                                  Instances For

                                    Extended interval evaluator.

                                    This evaluator handles singularities by splitting intervals at discontinuities. The key improvement over standard evaluation is in inv: when the denominator interval contains zero, we return two separate intervals rather than one huge interval spanning everything.

                                    For other operations, we lift the standard interval operations to work on all pairs of interval parts.

                                    Equations
                                    Instances For

                                      Convenience function for single-variable extended evaluation

                                      Equations
                                      Instances For

                                        Collapse extended evaluation to standard evaluation via hull

                                        Equations
                                        Instances For

                                          Soundness Theorem #

                                          def LeanCert.Engine.extendedEnvMem (ρ_real : ) (ρ_ext : ExtendedEnv) :

                                          Real environment membership in extended environment

                                          Equations
                                          Instances For

                                            Domain validity for Extended evaluation. For log, we require the argument's hull to have positive lower bound, ensuring all parts have positive lo and the filter keeps them.

                                            Equations
                                            Instances For

                                              Domain validity is trivially true for ExprSupported expressions (which exclude log).

                                              theorem LeanCert.Engine.evalExtended_correct_core (e : Core.Expr) (hsupp : ExprSupportedCore e) (ρ_real : ) (ρ_ext : ExtendedEnv) ( : extendedEnvMem ρ_real ρ_ext) (cfg : ExtendedConfig := { }) (hdom : evalDomainValidExtended e ρ_ext cfg) :
                                              Core.Expr.eval ρ_real e evalExtended e ρ_ext cfg

                                              Soundness of extended evaluation for the core expression subset.

                                              If all variables are in their respective extended intervals, and the expression is in the supported core subset (no partial functions), then the result is in the extended interval.

                                              Utility: Hull Soundness #

                                              theorem LeanCert.Engine.mem_evalExtendedHull_of_mem_evalExtended (e : Core.Expr) (ρ_real : ) (ρ_ext : ExtendedEnv) (heval : Core.Expr.eval ρ_real e evalExtended e ρ_ext) (hne : (evalExtended e ρ_ext).parts []) :

                                              If x is in the extended interval, it's in the hull