TorchLean API

FloatLib.Floats.Formats.BinaryInterchange.Transcendentals.Contract

Proof contracts for transcendental approximations #

These contracts state and prove accuracy claims about an approximation:

  1. a real function value lies in a proved enclosure;
  2. an executable result satisfies a proved absolute-error budget; and
  3. an enclosure is narrow enough that both endpoints round to the same destination value.

When both endpoints round alike, monotonicity forces the enclosed function value to round there too. StableEnclosure.roundAt_eq_roundAt_lower proves this fact. Constructing an enclosure requires Lean proofs of both bounds; external numerical output alone does not supply them.

IEEE exceptional-value behavior and the sign of an exact zero remain separate bit-level obligations. roundAt is the finite real grid semantics.

ApproximationCertificateOn and CorrectlyRoundedCertificateOn restrict the finite-real guarantees to an explicit predicate on encoded inputs. For example, a logarithm certificate can use fun input ↦ 0 < toReal input; an exponential certificate can use a proved input range on which the result remains finite. Membership never replaces the obligation to prove output finiteness. Outside the domain these certificates make no claim, so exceptional-value and overflow behavior need separate specifications. The certificates without On apply to every finite input and are equivalent to the domain-restricted certificates with domain fun _ ↦ True.

References #

A checked closed real enclosure of f x.

  • lower :

    Proved lower endpoint.

  • upper :

    Proved upper endpoint.

  • lower_le : self.lower f x

    The lower endpoint does not exceed the exact value.

  • le_upper : f x self.upper

    The exact value does not exceed the upper endpoint.

Instances For

    Use proved executable rational endpoints in a real-valued approximation contract.

    Instances For

      A Taylor enclosure whose rational endpoints are computed inside FloatLib.

      Instances For

        A proved rational logarithm enclosure for a positive input.

        Instances For

          The exact function value belongs to its enclosure.

          Every proved enclosure has ordered endpoints.

          theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.RealEnclosure.abs_sub_le_width {f : } {x candidate : } (enclosure : RealEnclosure f x) (hcandidate : enclosure.lower candidate candidate enclosure.upper) :
          |candidate - f x| enclosure.upper - enclosure.lower

          Any candidate lying in the same enclosure has absolute error at most its width.

          This conservative bound is often sufficient for a first verified implementation. A sharper algorithm-specific proof can populate ApproximationCertificate directly.

          Build an enclosure by monotonicity from a real input interval.

          Instances For

            Round a proved real enclosure outward with any sound endpoint rounder.

            Instances For

              Sound exponential enclosure on an input interval.

              Instances For

                Sound hyperbolic-sine enclosure on an input interval.

                Instances For

                  Enclose hyperbolic tangent in [-1, 1] for every real argument.

                  Instances For

                    Sound hyperbolic-cosine enclosure on an input interval.

                    The upper endpoint uses the largest absolute endpoint because cosh is even and increases with absolute value.

                    Instances For

                      Convert the existing proved logarithm interval into the common enclosure contract.

                      Instances For

                        Convert the existing proved square-root interval into the common enclosure contract.

                        No sign hypothesis is needed: Real.sqrt is zero on negative arguments and monotone on all of .

                        Instances For

                          A proved enclosure whose endpoints select one nearest-even destination-grid value.

                          The inherited enclosure bounds and the endpoint-rounding equality are all proof obligations.

                          Instances For

                            A stable enclosure determines the correctly rounded real value of the exact function.

                            A finite encoded result backed by a stable real enclosure.

                            The final equality connects the stored result to the lower endpoint's rounded value. Together with stability, this proves correct nearest-even rounding of the exact function in finite real semantics.

                            Instances For

                              The certified encoded result has the correctly rounded real value of f x.

                              structure FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.ApproximationCertificate (fmt : FloatFormat) (f : ) (approximation : Model fmtModel fmt) (errorBudget : Model fmt) :

                              Whole-algorithm absolute-error contract for an executable unary approximation.

                              The fields require output finiteness and an error bound for every finite input.

                              • budget_nonnegative (input : Model fmt) : 0 errorBudget input

                                Every advertised error budget is nonnegative.

                              • output_finite (input : Model fmt) : input.isFinite = true(approximation input).isFinite = true

                                Finite inputs in scope produce finite outputs.

                              • error_le (input : Model fmt) : input.isFinite = true|(approximation input).toReal - f input.toReal| errorBudget input

                                The decoded result satisfies the advertised real absolute-error budget.

                              Instances For

                                Whole-algorithm correctly-rounded finite-real contract.

                                Both components are propositions, so the certificate adds no runtime data.

                                Instances For
                                  theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificate.output_finite {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} (certificate : CorrectlyRoundedCertificate fmt f implementation) (input : Model fmt) :
                                  input.isFinite = true(implementation input).isFinite = true

                                  Finite inputs in scope produce finite outputs.

                                  theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificate.toReal_eq_roundAt {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} (certificate : CorrectlyRoundedCertificate fmt f implementation) (input : Model fmt) :
                                  input.isFinite = true(implementation input).toReal = roundAt fmt (f input.toReal)

                                  At every finite input, the result equals nearest-even rounding of the exact real function.

                                  structure FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.ApproximationCertificateOn (fmt : FloatFormat) (f : ) (approximation : Model fmtModel fmt) (errorBudget : Model fmt) (domain : Model fmtProp) :

                                  Absolute-error contract on an explicit domain of encoded inputs.

                                  The domain can express mathematical restrictions, such as positivity for logarithms, and the range on which finite output is promised. Budgets must be nonnegative on the domain; output finiteness and accuracy are required for every finite input in that domain. In particular, merely restricting the mathematical domain does not discharge the output-finiteness obligation.

                                  • budget_nonnegative (input : Model fmt) : domain input0 errorBudget input

                                    Every budget advertised on the domain is nonnegative.

                                  • output_finite (input : Model fmt) : input.isFinite = truedomain input(approximation input).isFinite = true

                                    Finite inputs in the domain produce finite outputs.

                                  • error_le (input : Model fmt) : input.isFinite = truedomain input|(approximation input).toReal - f input.toReal| errorBudget input

                                    The decoded result satisfies the budget at every finite input in the domain.

                                  Instances For
                                    theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.ApproximationCertificateOn.mono {fmt : FloatFormat} {f : } {approximation : Model fmtModel fmt} {errorBudget : Model fmt} {domain smallerDomain : Model fmtProp} (certificate : ApproximationCertificateOn fmt f approximation errorBudget domain) (hsub : ∀ (input : Model fmt), smallerDomain inputdomain input) :
                                    ApproximationCertificateOn fmt f approximation errorBudget smallerDomain

                                    Restrict an absolute-error certificate to a smaller domain.

                                    theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.ApproximationCertificate.on_domain {fmt : FloatFormat} {f : } {approximation : Model fmtModel fmt} {errorBudget : Model fmt} (certificate : ApproximationCertificate fmt f approximation errorBudget) (domain : Model fmtProp) :
                                    ApproximationCertificateOn fmt f approximation errorBudget domain

                                    A whole-algorithm absolute-error certificate applies on any chosen domain.

                                    @[simp]
                                    theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.ApproximationCertificateOn.true_iff {fmt : FloatFormat} {f : } {approximation : Model fmtModel fmt} {errorBudget : Model fmt} :
                                    (ApproximationCertificateOn fmt f approximation errorBudget fun (x : Model fmt) => True) ApproximationCertificate fmt f approximation errorBudget

                                    The unrestricted domain gives the absolute-error contract for all finite inputs.

                                    Correct nearest-even finite-real rounding on an explicit domain of encoded inputs.

                                    Every finite input in the domain must produce a finite result with the stated rounded real value. This does not certify overflow, exceptional values, or the sign of zero: roundAt has no upper exponent bound, and those behaviors need separate bit-level specifications.

                                    Instances For
                                      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificateOn.output_finite {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} {domain : Model fmtProp} (certificate : CorrectlyRoundedCertificateOn fmt f implementation domain) (input : Model fmt) :
                                      input.isFinite = truedomain input(implementation input).isFinite = true

                                      Finite inputs in the certified domain produce finite outputs.

                                      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificateOn.toReal_eq_roundAt {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} {domain : Model fmtProp} (certificate : CorrectlyRoundedCertificateOn fmt f implementation domain) (input : Model fmt) :
                                      input.isFinite = truedomain input(implementation input).toReal = roundAt fmt (f input.toReal)

                                      At finite inputs in the domain, the result equals rounding of the exact real function.

                                      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificateOn.mono {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} {domain smallerDomain : Model fmtProp} (certificate : CorrectlyRoundedCertificateOn fmt f implementation domain) (hsub : ∀ (input : Model fmt), smallerDomain inputdomain input) :
                                      CorrectlyRoundedCertificateOn fmt f implementation smallerDomain

                                      Restrict a correctly-rounded certificate to a smaller domain.

                                      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificateOn.of_certified_results {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} {domain : Model fmtProp} (results : (input : Model fmt) → input.isFinite = truedomain inputCertifiedRoundedResult fmt f input.toReal) (hvalue : ∀ (input : Model fmt) (hfinite : input.isFinite = true) (hdomain : domain input), (results input hfinite hdomain).value = implementation input) :
                                      CorrectlyRoundedCertificateOn fmt f implementation domain

                                      Assemble a domain certificate from stable enclosures certifying the implementation's results.

                                      theorem FloatLib.Floats.Formats.BinaryInterchange.Model.Transcendentals.Contract.CorrectlyRoundedCertificate.on_domain {fmt : FloatFormat} {f : } {implementation : Model fmtModel fmt} (certificate : CorrectlyRoundedCertificate fmt f implementation) (domain : Model fmtProp) :
                                      CorrectlyRoundedCertificateOn fmt f implementation domain

                                      A whole-algorithm correctly-rounded certificate applies on any chosen domain.

                                      @[simp]

                                      The unrestricted domain gives the correct-rounding contract for all finite inputs.