Documentation

Lean.Elab.PreDefinition.FixedParams

This module contains the logic for figuring out, given mutually recursive predefinitions, which parameters are “fixed”. This used to be a simple task when we only considered a fixed prefix, but becomes a quite involved task if we allow fixed parameters also later in the parameter list, and possibly in a different order in different modules.

The main components of this module are

To determine which parameters in mutually recursive predefinitions are fixed, and how they correspond to each other, we run an analysis that aggregates information in the Info data type.

Abstractly, this represents

  • a set varying of (funIdx × paramIdx) pairs known to be varying, initially empty
  • a directed graph whose nodes are (funIdx × paramIdx) pairs, initially empty

We find the largest set and graph that satisfies these rules:

  • Every parameter has to be related to itself: (funIdx, paramIdx) → (funIdx, paramIdx).
  • whenever the function with index caller calls callee and the argIdx's argument is reducibly defeq to paramIdx, then we have an edge (caller, paramIdx) → (callee, argIdx).
  • whenever the function with index caller calls callee and the argIdx's argument is not reducibly defeq to any of the caller's parameters, then (callee, argIdx) ∈ varying.
  • If we have (caller, paramIdx₁) → (callee, argIdx) and (caller, paramIdx₂) → (callee, argIdx) with paramIdx₁ ≠ paramIdx₂, then (callee, argIdx) ∈ varying.
  • The graph is transitive
  • If we have (caller, paramIdx) → (callee, argIdx) and (caller, paramIdx) ∈ varying, then (callee, argIdx) ∈ varying
  • If the type of funIdx’s parameter paramIdx₂ depends on the paramIdx₁and `(funIdx, paramIdx₁) ∈ varying, then (funIdx, paramIdx₁) ∈ varying`
  • For structural recursion: The target and all its indices are varying. (This is taking into account post-hoc, using FixedParamPerms.erase)

Under the assumption that the predefintions indeed are mutually recursive, then the resulting graph, restricted to the non-varying nodes, should partition into cliques that have one member from each function. Every such clique becomes a fixed parameter.

Instances For
    Equations
      Instances For
        Equations
          Instances For
            def Lean.Elab.FixedParams.Info.mayBeFixed (callerIdx paramIdx : Nat) (info : Info) :

            Is this parameter still plausibly a fixed parameter?

            Equations
              Instances For
                partial def Lean.Elab.FixedParams.Info.setVarying (funIdx paramIdx : Nat) (info : Info) :

                This parameter is varying. Set and propagate that information.

                def Lean.Elab.FixedParams.Info.getCallerParam? (calleeIdx argIdx callerIdx : Nat) (info : Info) :
                Equations
                  Instances For
                    partial def Lean.Elab.FixedParams.Info.setCallerParam (calleeIdx argIdx callerIdx paramIdx : Nat) (info : Info) :

                    We observe a possibly valid edge.

                    Equations
                      Instances For
                        Equations
                          Instances For
                            @[reducible, inline]

                            For a given function, a mapping from its parameters to the (indices of the) fixed parameters of the recursive group. The length of the array is the arity of the function, as determined from its body, consistent with the arity used by well-founded recursion. For the first function, they appear in order; for other functions they may be reordered.

                            Equations
                              Instances For

                                This data structure stores the result of the fixed parameter analysis. See FixedParams.Info for details on the analysis.

                                • numFixed : Nat

                                  Number of fixed parameters

                                • For each function in the clique, a mapping from its parameters to the fixed parameters. For the first function, they appear in order; for other functions they may be reordered.

                                • revDeps : Array (Array (Array Nat))

                                  The dependencies among the parameters. See FixedParams.Info.revDeps. We need this for the FixedParamsPerm.erase operation.

                                Instances For
                                  Equations
                                    Instances For
                                      Equations
                                        Instances For
                                          def Lean.Elab.FixedParamPerm.forallTelescope {n : TypeType u_1} {α : Type} [MonadControlT MetaM n] [Monad n] (perm : FixedParamPerm) (type : Expr) (k : Array Exprn α) :
                                          n α
                                          Equations
                                            Instances For

                                              If type is the type of the funIdx's function, instantiate the fixed parameters.

                                              Equations
                                                Instances For

                                                  If value is the body of the funIdx's function, instantiate the fixed parameters. Expects enough manifest lambdas to instantiate all fixed parameters, but can handle eta-contracted definitions beyond that.

                                                  Equations
                                                    Instances For
                                                      def Lean.Elab.FixedParamPerm.pickFixed {α : Type u_1} (perm : FixedParamPerm) (xs : Array α) :

                                                      If xs are arguments to the funIdx's function, pick only the fixed ones, and reorder appropriately. Expects xs to match the arity of the function.

                                                      Equations
                                                        Instances For
                                                          Equations
                                                            Instances For

                                                              If xs are arguments to the funIdx's function, pick only the varying ones. Unlike pickFixed, this function can handle over- or under-application.

                                                              Equations
                                                                Instances For
                                                                  def Lean.Elab.FixedParamPerm.buildArgs {α : Type u_1} (perm : FixedParamPerm) (fixedArgs varyingArgs : Array α) :

                                                                  Intersperses the fixed and varying parameters to be in the original parameter order. Can handle over- or und-application (extra or missing varying args), as long as there are all varying parameters that go before fixed parameters. (We expect to always find all fixed parameters, else they wouldn't be fixed parameters.)

                                                                  Equations
                                                                    Instances For
                                                                      partial def Lean.Elab.FixedParamPerm.buildArgs.go {α : Type u_1} (perm : FixedParamPerm) (fixedArgs varyingArgs : Array α) (i : Nat) (j : Std.PRange.Bound { lower := Std.PRange.BoundShape.closed, upper := Std.PRange.BoundShape.unbounded }.lower Nat) (xs : Array α) :

                                                                      Are all fixed parameters a non-reordered prefix?

                                                                      Equations
                                                                        Instances For

                                                                          If xs are the fixed parameters that are in scope, and toErase are, for each function, the positions of arguments that must no longer be fixed parameters, then this function splits partitions xs into those to keep and those to erase, and updates FixedParams accordingly.

                                                                          This is used in structural recursion, where we may discover that some fixed parameters are actually indices and need to be treated as varying, including all parameters that depend on them.

                                                                          Equations
                                                                            Instances For