Guide

The library

The public imports, how the modules are organized, the registries that extend the tactics, and how to check a change.

Most of the time you want import CAS, which brings in the calculator and the tactics. A file that needs less can import just the module that owns what it uses. If a file only proves derivatives with cas, say, it needs no more than

import CAS.ExactTactic

In library code my rule is stricter: always import the narrowest module that defines what you use.

The tactics and commands give the same results in a file outside the module system as in a module. That takes some care since Mathlib v4.36.0, which hides the bodies of the operations of ℝ, of the integral, of fderiv, of nhds and of many other definitions from modules. A file outside the module system still loads them, and there Lean unfolds them where it used to stop: a comparison of two different real terms can run for seconds. So wherever SymLean compares two terms, tries a rule or closes a goal by reflexivity or by a hypothesis, it does so in stages (CAS.Elaboration.DefEq): on the syntax, then with reducible definitions unfolded, and only then by unfolding definitions, without the ones that no module can unfold. Lean records which definitions those are, so there is no list to maintain. The results are the same; the cost is not always. While SymLean holds those definitions folded, Lean keeps fewer of its caches, so a comparison that fails on deeply nested functions costs more outside the module system, and at about nine levels of nesting cas can run out of heartbeats where a module file gets its answer. A module file does not pay this.

CAS.Irreducible is a separate compatibility import, and nothing loads it for you. It marks 73 of those Mathlib definitions irreducible again for the whole file that imports it. SymLean does not need it. Import it only if proofs of your own, in a file outside the module system, relied on these definitions being irreducible, and keep in mind that it changes elaboration in every file downstream of the import.

Public imports#

The exact theorems, the numerical certificates and the polynomial algorithms have no umbrella module, and that’s deliberate. A file imports the modules of ClosedForm, Exact, Continuum or Polynomial that it uses, and nothing else.

CAS.ExactTactic, CAS.IntegrationTactic and CAS.CEGA import no differential operator beyond Mathlib’s. A package that defines its own Euclidean derivative operators registers a backend through CAS.Tactic.DerivativeRegistry, and importing the operator definitions alone doesn’t change what the tactics prove.

How the library is organized#

The modules under CAS fall into layers, and the module documentation of each one states its contracts. These are the main layers:

Four smaller layers support them. Analysis holds analytic facts, most of which a command, a tactic or a certificate uses: splines with cubic Hermite interpolation, exact integrals of a polynomial times a logarithm, a discriminant bound for a mixed quadratic term, quadratic forms with the positive-definiteness certificate, and the spectrum of a rational matrix. Its part on special functions has the error function, values of the Bessel functions Jn, the Gamma function at half-integers, Bernoulli numbers and polynomials, the Legendre, Chebyshev and Hermite polynomials, bounds for logarithms, and exact quadrature rules for cosine series with multiple-angle bounds. Domain states the side condition NonzeroOn on a patch, together with the patch covers. NumberTheory has theorems on congruences, divisors, the Jacobi symbol and modular powers. Route pairs certificates with the planner steps that built them, and the steps there are diagnostic data, not evidence.

Certificates and backends#

Under the tactics, the lower-level interfaces expose Verified.Expr in typed certificate signatures. You use them to build reusable exact, residual, enclosure, approximation and quadrature certificates. The conclusions of these certificates are ordinary propositions, Set.EqOn, ResidualBound, EnclosedOn, BoundOn and ApproxOn, so a certificate’s conclusion can be used like any other theorem. The certificate structures live in focused modules under CAS/Certificate, again with no umbrella module. Ordinary Lean theorems restrict a certificate to a smaller domain or give it a weaker error budget, and cega using certificate projects it.

The numerical engine is FloatLib. RationalInterval extends FloatLib’s rational intervals with the invariant lo ≤ hi, and the adapters of CAS/Continuum/Interval carry FloatLib’s containment theorems over to real enclosures. When sums need controlled denominators, dyadicHull rounds the endpoints outward onto a common grid, the lower endpoint down and the upper one up, the way (5.14) of Rump 2010 rounds an interval to floating-point bounds. RealIntervalBackend turns interval membership into real enclosures and error budgets. CAS.Expr.FloatLib translates an expression with Expr.toFloatLib, and eval_toFloatLib preserves its real value when the coordinate tables agree.

Large certificates come with data files, and data files bring a problem. A certificate program outside the library reads its tables with include_str, as the JSON importers of Numerics expect. But Lake rebuilds a module when its source changes, not when such a file does, so after you edit a table the build could keep a theorem about the old one. To prevent that, the program’s library lists the facet SymLean.embeddedInputs that SymLean’s lakefile.lean defines, as SymLean’s own libraries do:

lean_lib Certificates where
  needs := #[`@/Certificates:SymLean.embeddedInputs]

With the facet listed, editing a data file rebuilds the modules of the library, meaning all the modules that Lake builds as part of it: those of its globs, their local imports and every module under the directory of a root. The facet finds the files by searching the text of each module for include_str followed by a string literal, over several lines and comments if need be, and it decodes the literal as Lean does. A text search only works if the text is what Lean reads, and the check of CAS.IncludeStr, which CAS.Numerics.Json imports publicly, makes sure of that. It accepts an include_str only when its keyword and its path, one string literal with only whitespace and comments before it, are the text of the module’s file at that place, which is where the search looks. It refuses any other form with a message that says what to write instead, for instance a path "a" ++ ".json", Lean’s own "dir" / "file", an include_str that a macro or notation produces, or one that code parses from a string and elaborates.

The facet also guards the check itself. It stops the build at a module of the library that

Put together, this means that in a library that lists the facet, every file that Lean reads through include_str while it elaborates a module of the library is tracked, or the build stops. Three things stay outside. An elaborator or meta code that reads the file itself, with IO.FS.readFile say. Code that builds include_str at run time from pieces of text, so that neither include_str nor includeStr is in its text, or elaborates it under a file name and a file map that it sets up itself. And macros from outside the workspace, which means Lean’s own modules, none of which produces include_str.

The facet’s name carries the package’s because Lake keeps one facet table for the whole workspace, and a facet replaces any earlier one of the same name.

Extending the tactics#

You extend the tactics through explicit registries, and adding an entry to one adds no assumption:

My rule here is to extend a registry only when the semantic theorem behind the new entry already exists. A registry chooses which theorem to apply. It never replaces one.

Checking a change#

These are the standards I hold the library to, and I’d ask the same of any change. Follow Mathlib’s conventions and the layout of the nearest file: a docstring on every public declaration, a module docstring in every file, lines within 100 characters, and no sorry, admit, new axiom or direct native_decide. Reuse Mathlib’s lemmas and FloatLib’s interval arithmetic instead of writing local copies. Two rules matter more than the rest: a route may close a goal only with a proof of the original goal, and a refusal must restore the proof state.

Cite each idea you borrow and each program you use under ## References in the module docstring. A paper or a book gets its authors, title, venue, year and link. A program gets its authors or project, title and link, or the paper its project asks users to cite, along with the version tested or the release or revision pinned. The program itself gets a year only where the project’s citation page or release record gives one for that version.

To check a change, build the changed module first and then everything:

lake build +CAS.Calculus.Series
lake build
bash tests/cega/run.sh
bash tests/cert/run.sh
bash tests/docs/run.sh
bash tests/floats/run.sh
bash tests/kinds/run.sh
bash tests/lake/run.sh

lake build applies the repository’s options, warnings as errors among them, and compiles the examples. The six test scripts cover what the library build can’t. tests/cega holds the native certificate tests, which run outside the library build because they turn native checking on. tests/cert holds the tests of the certificates of any size: the messages of refused statements, a certificate whose modules are written from a manifest and whose data are files, which the library doesn’t embed, the same certificate with compiled code, and the run over files. tests/docs holds the test of the documented tactic forms, which reads docstrings that a module of the library cannot see, the test of the compatibility import CAS.Irreducible, which needs a file outside the module system, and what must stay out of the library: the routes and the derivative backend that tests register, a table read from a data file with include_str, and literals large enough to get a compiled implementation. tests/floats holds the test of the arithmetic of cega as a setting of a whole build: Lake builds a small package with exact rationals, then with binary64 floats from one flag of the command line and no change to a Lean file, a second package that sets the arithmetic in lakefile.toml, and one file with the option on the command line of lean. tests/kinds elaborates the same goals and commands twice, as a file outside the module system and as a module, and both must give the same answers: the modules of the library can’t show what a tactic does where every body is visible. And tests/lake holds the tests of lakefile.lean: the certificate profiles, the tracking of the files that modules embed, also in a copy of the tree, the paths that the build refuses to track, the time that the scan of a large module takes, and a dependent package with a module setup facet and a library facet of its own. After lake build, bash scripts/dups.sh runs the duplicate gate. It counts the duplicate definitions and theorems, the restatements of theorems outside SymLean, the unused declarations, the equivalence pairs, and the names of scripts/downstream.names that are no longer declarations, and it fails when a count is larger than in scripts/dups.baseline. For changes to CASOracle, run the tests on the oracle page.

This site is checked the same way, because an example nobody ran is one I wouldn’t trust. bash Site/Tools/build.sh compiles every cell of every page against the library, except the quoted ones, and the build fails when a cell that should run reports an error or a warning, when a cell that shows a failure doesn’t fail, when a cell’s output lacks the text its fence expects, when a quoted cell is not verbatim in the file it quotes, when a link or an anchor is broken, or when a form or a call of the command registry has no reference page.