Documentation

Mathlib.Tactic.Simproc.ExistsAndEq

Simprocs for ∃ a', ... ∧ a' = a ∧ ... and ∀ a', ... → a' = a → ... #

This module implements the existsAndEq simproc, which triggers on goals of the form ∃ a, P. It checks whether P allows only one possible value for a, and if so, substitutes it, eliminating the leading quantifier.

The procedure traverses the body, branching at each ∧ and entering existential quantifiers, searching for a subexpression of the form a = a' or a' = a for a' that is independent of a. If such an expression is found, all occurrences of a are replaced with a'. If a' depends on variables bound by existential quantifiers, those quantifiers are moved outside.

For example, ∃ a, p a ∧ ∃ b, a = f b ∧ q b will be rewritten as ∃ b, p (f b) ∧ q b.

It also implements the dual forallImpEq simproc, which triggers on goals of the form ∀ a, P. It traverses the binders of P (universal quantifiers and implications), searching for a hypothesis of the form a = a' or a' = a for a' that is independent of a and of the binders that stay in place. If such an hypothesis is found, all occurrences of a are replaced with a'. Universal quantifiers on the way that do not depend on a are moved outside.

For example, ∀ a, p a → ∀ b, a = f b → q b will be rewritten as ∀ b, p (f b) → f b = f b → q b.

Type for storing the step taken at each node: the chosen branch at an And node, and, at an Exists node, whether we enter its binder type (existsType, and then the quantifier stays in place) or its body (existsBody, and then the quantifier is moved to the front).

Instances For
    Equations
    Instances For
      @[reducible, inline]

      Type for storing the path in the body expression leading to a = a'. We store only the chosen directions at each And node because there is no branching at Exists nodes, and Exists nodes will be removed from the body.

      Equations
      Instances For
        @[reducible, inline]

        Qq-fied version of Expr. Here, we use it to store free variables introduced when unpacking existential quantifiers.

        Equations
        Instances For
          @[reducible, inline]

          Qq-fied version of Expr proving some P : Prop.

          Equations
          Instances For

            Checks whether the equation with sides x and y determines the free variable a as y: x is a itself, and y doesn't mention a (in a = f a, for example, it does). The callers try both orientations.

            Equations
            Instances For
              partial def ExistsAndEq.findEqPath {u : Lean.Level} {α : Q(Sort u)} (a : Q(«$α»)) (P : Q(Prop)) :

              Finds a Path for findEq. It leads to a subexpression a = a' or a' = a, where a' doesn't contain the free variable a. This is a fast version that quickly returns none when the simproc is not applicable.

              partial def ExistsAndEq.proveFromHoisted {T T' : Q(Prop)} (h : Q(«$T'»)) (exs : List VarQ) (path : Path) :
              Lean.MetaM Q(«$T»)

              Assuming T' is T with existential quantifiers removed and abstracted as exs this function proves the original T from h : T'. This is used when we hoist binders from inside P in ∃ h : P, Q h and restore the proof h that Q consumes.

              def ExistsAndEq.findEq {u : Lean.Level} {α : Q(Sort u)} (a : Q(«$α»)) (P : Q(Prop)) (path : Path) :

              Given P : Prop and a : α, traverses the expression P to find a subexpression of the form a = a' or a' = a for some a'. It branches at each And and walks into existential quantifiers.

              Returns a tuple (fvars, lctx, P', a'), where:

              • fvars is a list of all variables bound by existential quantifiers along the path.
              • lctx is the local context containing all these free variables.
              • P' is P with all existential quantifiers along the path removed, and corresponding bound variables replaced with fvars.
              • a' is the expression found that must be equal to a. It may contain free variables from fvars.
              Equations
              Instances For
                def ExistsAndEq.mkNestedExists (fvars : List VarQ) (body : Q(Prop)) :

                Constructs ∃ f₁ f₂ ... fₙ, body, where [f₁, ..., fₙ] = fvars.

                Equations
                Instances For

                  The path to the equation in the result formula: the quantifiers entered along path are moved to the front, so their existsBody steps come first, followed by the And steps in their original order.

                  Equations
                  Instances For
                    partial def ExistsAndEq.destruct {P goal : Q(Prop)} (h : Q(«$P»)) (exs : List VarQ) (path : Path) (acc : List HypQ) (k : List HypQ → HypQ → Lean.MetaM Q(«$goal»)) :
                    Lean.MetaM Q(«$goal»)

                    Destructs h : P following path, as the chain of refine h.elim fun … ↦ ?_ in the docstring of mkBeforeToAfter does: at an existsBody step the quantifier is unpacked with Exists.elim, using the next variable of exs as the bound variable; at an andLeft/andRight step the conjunction is split with And.elim, and the part outside the path becomes a leaf. The continuation k receives the leaves (in path order, after those in acc) and the hypothesis at the end of the path, i.e. the equation. All of them are local hypotheses.

                    partial def ExistsAndEq.construct {goal : Q(Prop)} (exs : List VarQ) (path : Path) (leaves : List HypQ) :
                    Lean.MetaM Q(«$goal»)

                    Constructs a proof of goal following path, as the chain of refine Exists.intro … ?_ and refine And.intro … ?_ in the docstring of mkBeforeToAfter does: at an existsBody step the next variable of exs is the witness; at an andLeft/andRight step the part outside the path is proved by the next leaf; the equation at the end of the path is closed by rfl.

                    def ExistsAndEq.mkBeforeToAfter {u : Lean.Level} {α : Q(Sort u)} {p : Q(«$α» → Prop)} {P' : Q(Prop)} (fvars : List VarQ) (path : Path) :
                    Lean.MetaM Q((∃ (a : «$α»), «$p» a) → «$P'»)

                    Generates a proof of (∃ a, p a) → P'. We assume that fvars = [f₁, ..., fₙ] are free variables and P' = ∃ f₁ ... fₙ, newBody, and path leads to a = a' in ∃ a, p a.

                    The proof follows the following structure:

                    example (f : β → α) {P Q : β → Prop} :
                        (∃ x b, P b ∧ (∃ c, f c = x ∧ Q c) ∧ Q b) → ∃ b c, P b ∧ (f c = f c ∧ Q c) ∧ Q b := by
                      -- path : existsBody, andRight, andLeft, existsBody, andLeft
                      intro ⟨x, h₁⟩
                      -- destruct the input following the path:
                      -- obtain ⟨e1, a1, ⟨e2, h_eq, a3⟩, a2⟩ := h₁
                      refine
                        h₁.elim fun e1 h₂ ↦           -- existsBody
                        h₂.elim fun a1 h₃ ↦           -- andRight
                        h₃.elim fun h₄ a2 ↦           -- andLeft
                        h₄.elim fun e2 h₅ ↦           -- existsBody
                        h₅.elim fun h_eq a3 ↦ ?_      -- andLeft
                      -- subst the equation (`substCore`)
                      subst h_eq
                      -- construct the output following the path of the result (with all existsBody moved left):
                      -- existsBody, existsBody, andRight, andLeft, andLeft
                      -- exact ⟨e1, e2, a1, ⟨rfl, a3⟩, a2⟩
                      refine Exists.intro e1 ?_       -- existsBody
                      refine Exists.intro e2 ?_       -- existsBody
                      refine And.intro a1 ?_          -- andRight
                      refine And.intro ?_ a2          -- andLeft
                      refine And.intro ?_ a3          -- andLeft
                      exact rfl
                    
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      def ExistsAndEq.mkAfterToBefore {u : Lean.Level} {α : Q(Sort u)} {p : Q(«$α» → Prop)} {P' : Q(Prop)} (a' : Q(«$α»)) (fvars : List VarQ) (path : Path) :
                      Lean.MetaM Q(«$P'» → ∃ (a : «$α»), «$p» a)

                      Generates a proof of P' → ∃ a, p a. We assume that fvars = [f₁, ..., fₙ] are free variables and P' = ∃ f₁ ... fₙ, newBody, and path leads to a = a' in ∃ a, p a.

                      The proof follows the following structure:

                      example (f : β → α) {P Q : β → Prop} :
                          (∃ b c, P b ∧ (f c = f c ∧ Q c) ∧ Q b) → ∃ x b, P b ∧ (∃ c, f c = x ∧ Q c) ∧ Q b := by
                        intro h₁
                        -- destruct the input following the path of the result (with all existsBody moved left):
                        -- existsBody, existsBody, andRight, andLeft, andLeft
                        -- obtain ⟨e1, e2, a1, ⟨_, a3⟩, a2⟩ := h₁
                        refine
                          h₁.elim fun e1 h₂ ↦           -- existsBody
                          h₂.elim fun e2 h₃ ↦           -- existsBody
                          h₃.elim fun a1 h₄ ↦           -- andRight
                          h₄.elim fun h₅ a2 ↦           -- andLeft
                          h₅.elim fun _ a3 ↦ ?_         -- andLeft
                        -- construct the output following the path: existsBody, andRight, andLeft, existsBody, andLeft
                        -- exact ⟨f e2, e1, a1, ⟨e2, rfl, a3⟩, a2⟩
                        refine Exists.intro (f e2) ?_   -- `a'`
                        refine Exists.intro e1 ?_       -- existsBody
                        refine And.intro a1 ?_          -- andRight
                        refine And.intro ?_ a2          -- andLeft
                        refine Exists.intro e2 ?_       -- existsBody
                        refine And.intro ?_ a3          -- andLeft
                        exact rfl
                      
                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For

                        Runs k on e with the metavariables occurring in e replaced by local variables, and substitutes the metavariables back into the resulting Simp.Step. In some cases (e.g. under aesop) the goal contains metavariables, and this is needed to handle them properly: the proof built by substCore can only be instantiated when the goal contains none.

                        The abstraction is done by abstractMVars, so that metavariables occurring in the types of other metavariables (as in ?f : α → ?β) are handled consistently.

                        TODO: this is a general simproc infrastructure, should we moved somewhere else?

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

                          The implementation of existsAndEq, for an expression without metavariables.

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

                            Triggers at goals of the form ∃ a, body and checks if body allows a single value a' for a. If so, replaces a with a' and removes quantifier.

                            It looks through nested quantifiers and conjunctions searching for a a = a' or a' = a subexpression.

                            Equations
                            Instances For

                              Checks whether the binders of e, read as a function of the bound variable .bvar d, contain a hypothesis of the form .bvar d = _ or _ = .bvar d. This is a fast check that quickly rules out most expressions on which forallImpEq is not applicable.

                              Equations
                              Instances For
                                partial def ForallImpEq.walk {β : Type} (a e : Lean.Expr) (forbidden : Array Lean.FVarId) (xs : Array Lean.Expr) (hoisted : Array Bool) (k : Array Lean.Expr → Array Bool → Lean.Expr → Lean.Expr → Lean.Expr → Bool → Lean.MetaM β) :

                                Walks the binders of e, introducing them as local variables, until it finds a hypothesis of the form a = a' or a' = a where a' doesn't contain any of the free variables in forbidden.

                                At each binder x : T on the way, x is marked as hoisted (to be moved in front of the quantifier over a) if the rest of e depends on x and T doesn't contain any of the free variables in forbidden. Otherwise x stays in place, and is added to forbidden.

                                Once the equation is found, calls k xs hoisted h rest a' symm, where:

                                • xs are the binders on the way, and hoisted says which of them are hoisted.
                                • h is the hypothesis a = a' or a' = a.
                                • rest is the remainder of e after h.
                                • symm is true if h : a' = a and false if h : a = a'.

                                The implementation of forallImpEq, for an expression without metavariables.

                                Given e = ∀ a, ∀ x₁ ... xₙ, ∀ h : a = a', R, where the xᵢ are partitioned into hoisted binders ys and kept binders zs, it produces e' = ∀ ys, ∀ zs[a'/a], ∀ h : a' = a', R[a'/a], along with the proof of e = e':

                                • e → e' is fun H ys zs h ↦ H a' x₁ ... xₙ h.
                                • e' → e is fun H' a x₁ ... xₙ h ↦ ?_, where the goal is closed by substituting a with a' (substCore) and applying H'.
                                Equations
                                Instances For

                                  Triggers at goals of the form ∀ a, body and checks if body has a hypothesis a = a' or a' = a. If so, replaces a with a' and removes the quantifier.

                                  It looks through nested universal quantifiers and implications searching for the hypothesis. Quantifiers on the way that do not depend on a are moved outside.

                                  This is the dual of existsAndEq.

                                  Equations
                                  Instances For