Documentation

Lean.Elab.PreDefinition.WF.GuessLex

This module finds lexicographic termination arguments for well-founded recursion.

Starting with basic measures (sizeOf xᵢ for all parameters xᵢ), and complex measures (e.g. e₂ - e₁ if e₁ < e₂ is found in the context of a recursive call) it tries all combinations until it finds one where all proof obligations go through with the given tactic (decerasing_by), if given, or the default decreasing_tactic.

For mutual recursion, a single measure is not just one parameter, but one from each recursive function. Enumerating these can lead to a combinatoric explosion, so we bound the nubmer of measures tried.

In addition to measures derived from sizeOf xᵢ, it also considers measures that assign an order to the functions themselves. This way we can support mutual function definitions where no arguments decrease from one function to another.

The result of this module is a TerminationWF, which is then passed on to wfRecursion; this design is crucial so that whatever we infer in this module could also be written manually by the user. It would be bad if there are function definitions that can only be processed with the guessed lexicographic order.

The following optimizations are applied to make this feasible:

  1. The crucial optimiziation is to look at each argument of each recursive call once, try to prove < and (if that fails ), and then look at that table to pick a suitable measure.

  2. The next-crucial optimization is to fill that table lazily. This way, we run the (likely expensive) tactics as few times as possible, while still being able to consider a possibly large number of combinations.

  3. Before we even try to prove <, we check if the arguments are equal (=). No well-founded measure will relate equal terms, likely this check is faster than firing up the tactic engine, and it adds more signal to the output.

  4. Instead of traversing the whole function body over and over, we traverse it once and store the arguments (in unpacked form) and the local MetaM state at each recursive call (see collectRecCalls), which we then re-use for the possibly many proof attempts.

The logic here is based on “Finding Lexicographic Orders for Termination Proofs in Isabelle/HOL” by Lukas Bulwahn, Alexander Krauss, and Tobias Nipkow, 10.1007/978-3-540-74591-4_5 https://www21.in.tum.de/~nipkow/pubs/tphols07.pdf.

We got the idea of considering the measure e₂ - e₁ if we see e₁ < e₂ from “Termination Analysis with Calling Context Graphs” by Panagiotis Manolios & Daron Vroon, https://doi.org/10.1007/11817963_36.

Given a predefinition, return the variabe names in the outermost lambdas. Includes the “fixed prefix”.

The length of the returned array is also used to determine the arity of the function, so it should match what packDomain does.

Equations
Instances For

    Given the original parameter names from originalVarNames, find good variable names to be used when talking about termination arguments: Use user-given parameter names if present; use x1...xn otherwise.

    The names ought to accessible (no macro scopes) and fresh wrt to the current environment, so that with showInferredTerminationBy we can print them to the user reliably. We do that by appending ' as needed.

    It is possible (but unlikely without malice) that some of the user-given names shadow each other, and the guessed relation refers to the wrong one. In that case, the user gets to keep both pieces (and may have to rename variables).

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

      A termination measure with extra fields for use within GuessLex

      Instances For
        Equations

        String description of this measure

        Equations
        Instances For

          Determine if the measure for parameter x should be sizeOf x or just x.

          For non-mutual definitions, we omit sizeOf when the argument does not depend on the other varying parameters, and its WellFoundedRelation instance goes via SizeOf.

          For mutual definitions, we omit sizeOf only when the argument is (at reducible transparency!) of type Nat (else we'd have to worry about differently-typed measures from different functions to line up).

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

            Sets the user names for the given freevars in xs.

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

              Create one measure for each (eligible) parameter of the given predefintion.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                @[reducible, inline]
                abbrev Lean.Elab.WF.GuessLex.M (recFnName : Lake.Name) (α : Type) (β : Type) :

                Internal monad used by withRecApps

                Equations
                Instances For
                  def Lean.Elab.WF.GuessLex.withRecApps {α : Type} (recFnName : Lake.Name) (fixedPrefixSize : Nat) (param : Lean.Expr) (e : Lean.Expr) (k : Lean.ExprArray Lean.ExprLean.MetaM α) :

                  Traverses the given expression e, and invokes the continuation k at every saturated call to recFnName.

                  The expression param is passed along, and refined when going under a matcher or casesOn application.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    partial def Lean.Elab.WF.GuessLex.withRecApps.processRec {α : Type} (recFnName : Lake.Name) (fixedPrefixSize : Nat) (k : Lean.ExprArray Lean.ExprLean.MetaM α) (param : Lean.Expr) (e : Lean.Expr) :
                    partial def Lean.Elab.WF.GuessLex.withRecApps.processApp {α : Type} (recFnName : Lake.Name) (fixedPrefixSize : Nat) (k : Lean.ExprArray Lean.ExprLean.MetaM α) (param : Lean.Expr) (e : Lean.Expr) :
                    partial def Lean.Elab.WF.GuessLex.withRecApps.loop {α : Type} (recFnName : Lake.Name) (fixedPrefixSize : Nat) (k : Lean.ExprArray Lean.ExprLean.MetaM α) (param : Lean.Expr) (e : Lean.Expr) :

                    A SavedLocalContext captures the state and local context of a MetaM, to be continued later.

                    Instances For

                      Capture the MetaM state including local context.

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

                        Run a MetaM action in the saved state.

                        Equations
                        Instances For

                          A RecCallWithContext focuses on a single recursive call in a unary predefinition, and runs the given action in the context of that call.

                          Instances For

                            Store the current recursive call and its context.

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

                              The elaborator is prone to duplicate terms, including recursive calls, even if the user only wrote a single one. This duplication is wasteful if we run the tactics on duplicated calls, and confusing in the output of GuessLex. So prune the list of recursive calls, and remove those where another call exists that has the same goal and context that is no more specific.

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

                                Traverse a unary PreDefinition, and returns a WithRecCall closure for each recursive call site.

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

                                  Is the expression a <-like comparison of Nat expressions

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

                                      A GuessLexRel described how a recursive call affects a measure; whether it decreases strictly, non-strictly, is equal, or else.

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

                                        Given a GuessLexRel, produce a binary Expr that relates two Nat values accordingly.

                                        Equations
                                        Instances For

                                          For a given recursive call, and a choice of parameter and argument index, try to prove equality, < or ≤.

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

                                            Create a cache to memoize calls to evalRecCall descTactic? rcc

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

                                              Run evalRecCall and cache there result

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

                                                Print a single cache entry as a string, without forcing it

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

                                                  The measures that we order lexicographically can be comparing arguments, or numbering the functions

                                                  Instances For

                                                    Evaluate a recursive call at a given MutualMeasure

                                                    Equations
                                                    Instances For

                                                      Generate all combination of measures. Assumes we have numbered the measures of each function, and their counts is in numMeasures.

                                                      This puts the uniform combinations ([0,0,0], [1,1,1]) to the front; they are commonly most useful to try first, when the mutually recursive functions have similar argument structures

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

                                                        Enumerate all meausures we want to try.

                                                        All arguments (resp. combinations thereof) and possible orderings of functions (if more than one)

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          def Lean.Elab.WF.GuessLex.solve {m : TypeType} {α : Type} [Monad m] (measures : Array α) (calls : Array (αm Lean.Elab.WF.GuessLex.GuessLexRel)) :
                                                          m (Option (Array α))

                                                          The core logic of guessing the lexicographic order Given a matrix that for each call and measure indicates whether that measure is decreasing, equal, less-or-equal or unknown, It finds a sequence of measures that is lexicographically decreasing.

                                                          The matrix is implemented here as an array of monadic query methods only so that we can fill is lazily. Morally, this is a pure function

                                                          Equations
                                                          Instances For
                                                            partial def Lean.Elab.WF.GuessLex.solve.go {m : TypeType} {α : Type} [Monad m] (measures : Array α) (calls : Array (αm Lean.Elab.WF.GuessLex.GuessLexRel)) (acc : Array α) :
                                                            m (Option (Array α))

                                                            Given a matrix (row-major) of strings, arranges them in tabular form. First column is left-aligned, others right-aligned. Single space as column separator.

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

                                                              Concise textual representation of the source location of a recursive call

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

                                                                How to present the measure in the table header, possibly abbreviated.

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

                                                                    Explain what we found out about the recursive calls (non-mutual case)

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

                                                                      Explain what we found out about the recursive calls (mutual case)

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

                                                                          For #[x₁, .., xₙ] create (x₁, .., xₙ).

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

                                                                              Shows the inferred termination argument to the user, and implements termination_by?

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

                                                                                Main entry point of this module:

                                                                                Try to find a lexicographic ordering of the arguments for which the recursive definition terminates. See the module doc string for a high-level overview.

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