Documentation

Lean.Meta.Sym.SymM

Sym Extensions #

Extensible state mechanism for SymM, allowing modules to register persistent state that lives across simp invocations within a sym => block. Follows the same pattern as Grind.SolverExtension in Lean/Meta/Tactic/Grind/Types.lean.

Opaque extension state type used to store type-erased extension values.

A registered extension for SymM. Each extension gets a unique index into the extensions array in Sym.State. Can only be created via registerSymExtension.

  • id : Nat
  • mkInitial : IO σ
Instances For
    def Lean.Meta.Sym.registerSymExtension {σ : Type} (mkInitial : IO σ) :

    Registers a new SymM state extension. Extensions can only be registered during initialization. Returns a handle for typed access to the extension's state.

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

      Returns initial state for all registered extensions.

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

        Information about a single argument position in a function's type signature.

        This is used during pattern matching and structural definitional equality tests to identify arguments that can be skipped or handled specially (e.g., instance arguments can be synthesized, proof arguments can be inferred).

        • isProof : Bool

          true if this argument is a proof (its type is a Prop).

        • isInstance : Bool

          true if this argument is a type class instance.

        Instances For

          Information about a function symbol. It stores which argument positions are proofs or instances, enabling optimizations during pattern matching and structural definitional equality tests such as skipping proof arguments or deferring instance synthesis.

          Instances For

            Information on how to build congruence proofs for function applications. This enables efficient rewriting of subterms without repeatedly inferring types or instances.

            • none : CongrInfo

              None of the arguments of the function can be rewritten.

            • fixedPrefix (prefixSize suffixSize : Nat) : CongrInfo

              For functions with a fixed prefix of implicit/instance arguments followed by explicit non-dependent arguments that can be rewritten independently.

              • prefixSize: Number of leading arguments (types, instances) that are determined by the suffix arguments and should not be rewritten directly.
              • suffixSize: Number of trailing arguments that can be rewritten using simple congruence.

              Examples (showing prefixSize, suffixSize):

              • HAdd.hAdd {α β γ} [HAdd α β γ] (a : α) (b : β): (4, 2) — rewrite a and b
              • And (p q : Prop): (0, 2) — rewrite both propositions
              • Eq {α} (a b : α): (1, 2) — rewrite a and b, type α is fixed
              • Neg.neg {α} [Neg α] (a : α): (2, 1) — rewrite just a
            • interlaced (rewritable : Array Bool) : CongrInfo

              For functions with interlaced rewritable and non-rewritable arguments. Each element indicates whether the corresponding argument position can be rewritten.

              Example: For HEq {α : Sort u} (a : α) {β : Sort u} (b : β), the mask would be #[false, true, false, true] — we can rewrite a and b, but not α or β.

            • congrTheorem (thm : CongrTheorem) : CongrInfo

              For functions that have proofs and Decidable arguments. For this kind of function we generate a custom theorem. Example: Array.eraseIdx {α : Type u} (xs : Array α) (i : Nat) (h : i < xs.size) : Array α. The proof argument h depends on xs and i. To be able to rewrite xs and i, we use the auto-generated theorem.

              Array.eraseIdx.congr_simp {α : Type u} (xs xs' : Array α) (e_xs : xs = xs')
                  (i i' : Nat) (e_i : i = i') (h : i < xs.size) : xs.eraseIdx i h = xs'.eraseIdx i' ⋯
              
            Instances For

              Pre-shared expressions for commonly used terms.

              Instances For

                Configuration options for the symbolic computation framework.

                • verbose : Bool

                  When true, issues are collected during proof search and reported on failure.

                • enforceUnfoldReducible : Bool

                  When true, shareCommon maintains the SymM invariant that reducible constants have been eagerly unfolded: if a new term contains one, shareCommon unfolds it before adding the term to the table of maximally shared terms.

                • enforceFoldProjs : Bool

                  When true, shareCommon maintains the SymM invariant that kernel projections have been folded into projection function applications: if a new term contains one, shareCommon folds it before adding the term to the table of maximally shared terms. cbv disables this check because it processes kernel projections natively.

                Instances For

                  Readonly context for the symbolic computation framework.

                  Instances For
                    • Cache for value-level canonicalization (no type reductions applied).

                    • cacheInType : Std.HashMap Expr Expr

                      Cache for type-level canonicalization (reductions applied).

                    Instances For

                      Mutable state for the symbolic computation framework.

                      • ShareCommon (aka Hash-consing) state.

                      • Maps expressions to their maximal free variable (by declaration index).

                        • maxFVar[e] = some fvarId means fvarId is the free variable with the largest declaration index occurring in e.
                        • maxFVar[e] = none means e contains no free variables (but may contain metavariables).

                        Recall that if e contains a metavariable ?m, then we assume e may contain any free variable in the local context associated with ?m.

                        This mapping enables O(1) local context compatibility checks during metavariable assignment. Instead of traversing local contexts with isSubPrefixOf, we check if the maximal free variable in the assigned value is in scope of the metavariable's local context.

                        Note: We considered using a mapping PHashMap ExprPtr FVarId. However, there is a corner case that is not supported by this representation. e contains a metavariable (with an empty local context), and no free variables.

                      • proofInstInfo : PHashMap Name (Option ProofInstInfo)
                      • inferType : PHashMap ExprPtr Expr

                        Cache for inferType results, keyed by pointer equality. SymM uses a fixed configuration, so we can use a simpler key than MetaM. Remark: type inference is a bottleneck on Meta.Tactic.Simp simplifier.

                      • Cache for getLevel results, keyed by pointer equality.

                      • Cache for isDefEqI results

                      • State for registered SymExtensions, indexed by extension id.

                      • issues : List MessageData

                        Issues found during symbolic computation. Accumulated across operations within a sym => block and reported when a tactic fails.

                      • canon : Canon.State
                      • instanceOverrides : PHashMap Expr Expr

                        Instances registered by satellite modules for types they create themselves (e.g., grind linarith's envelope type IntModule.OfNatModule.Q). Sym.synthInstance? consults this table (keyed by the instance type, structural equality) before running typeclass resolution. This both skips searches whose results are known in advance, and ensures the registered term is used as the canonical representative.

                        We currently use this table to reduce the overhead of type class resolution in grind.

                      • debug : Bool
                      Instances For

                        Auxiliary function for implementing unfoldReducible and unfoldReducibleSimproc. Performs a single step.

                        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

                            Unfolds all reducible declarations occurring in e. This is meant as a preprocessing step. It does not guarantee maximally shared terms

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

                              Converts nested Expr.projs into projection applications if possible. The structural simplifier and pattern matcher do not handle kernel projection terms; this preprocessing step folds them into projection function applications.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                def Lean.Meta.Sym.SymM.run {α : Type} (x : SymM α) :
                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Returns maximally shared commonly used terms

                                  Equations
                                  Instances For

                                    Returns the internalized True constant.

                                    Equations
                                    Instances For

                                      Returns true if e is the internalized True expression.

                                      Equations
                                      Instances For

                                        Returns the internalized False constant.

                                        Equations
                                        Instances For

                                          Returns true if e is the internalized False expression.

                                          Equations
                                          Instances For

                                            Returns the internalized Bool.true.

                                            Equations
                                            Instances For

                                              Returns true if e is the internalized Bool.true expression.

                                              Equations
                                              Instances For

                                                Returns the internalized Bool.false.

                                                Equations
                                                Instances For

                                                  Returns true if e is the internalized Bool.false expression.

                                                  Equations
                                                  Instances For

                                                    Returns the internalized 0 : Nat numeral.

                                                    Equations
                                                    Instances For

                                                      Returns the internalized Ordering.eq.

                                                      Equations
                                                      Instances For

                                                        Returns the internalized Int.

                                                        Equations
                                                        Instances For

                                                          Runs an AlphaShareCommonM action over the SymM hash-consing state. On a check violation, returns the cache accumulated before the failure as .error; the terms hash-consed before the failure are kept in the state, since they individually satisfy the invariants.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For
                                                            def Lean.Meta.Sym.withoutFoldProjsCheck {m : TypeType u_1} {α : Type} [MonadWithReaderOf Context m] (x : m α) :
                                                            m α

                                                            Runs x with the shareCommon kernel-projection check disabled. Use this to wrap code that temporarily violates the SymM folded-projections invariant, e.g., a head reducer that exposes kernel projections in intermediate terms and restores the invariant in its final result.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              def Lean.Meta.Sym.withoutShareCommonChecks {m : TypeType u_1} {α : Type} [MonadWithReaderOf Context m] (x : m α) :
                                                              m α
                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                Applies hash-consing to e. Recall that all expressions in a grind goal have been hash-consed. We perform this step before we internalize expressions.

                                                                This function does not try to unfoldReducible and foldProjs like shareCommon. cache may contain the results accumulated by a run of shareCommon that was interrupted by a check violation; see repairAndShare.

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

                                                                  Applies hash-consing to e. Recall that all expressions in a grind goal have been hash-consed. We perform this step before we internalize expressions.

                                                                  Depending on the current configuration, this function also maintains the SymM invariants that reducible constants have been unfolded and kernel projections folded. The checks are node-local and are only performed on terms that have not been hash-consed before, so already-shared subterms cost nothing. When a violation is found, the term is repaired using unfoldReducible/foldProjs and re-processed.

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

                                                                    Incremental variant of shareCommon for expressions constructed from already-shared subterms.

                                                                    Use this when an expression e was produced by a Lean API (e.g., inferType, mkApp4) that does not preserve maximal sharing, but the inputs to that API were already maximally shared.

                                                                    Unlike shareCommon, this function does not use a local Std.HashMap ExprPtr Expr to track visited nodes. This is more efficient when the number of new (unshared) nodes is small, which is the common case when wrapping API calls that build a few constructor nodes around shared inputs.

                                                                    Example:

                                                                    -- `a` and `b` are already maximally shared
                                                                    let result := mkApp2 f a b  -- result is not maximally shared
                                                                    let result ← shareCommonInc result -- efficiently restore sharing
                                                                    
                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For
                                                                      @[reducible, inline]

                                                                      Incremental variant of shareCommon for expressions constructed from already-shared subterms.

                                                                      Use this when an expression e was produced by a Lean API (e.g., inferType, mkApp4) that does not preserve maximal sharing, but the inputs to that API were already maximally shared.

                                                                      Unlike shareCommon, this function does not use a local Std.HashMap ExprPtr Expr to track visited nodes. This is more efficient when the number of new (unshared) nodes is small, which is the common case when wrapping API calls that build a few constructor nodes around shared inputs.

                                                                      Example:

                                                                      -- `a` and `b` are already maximally shared
                                                                      let result := mkApp2 f a b  -- result is not maximally shared
                                                                      let result ← shareCommonInc result -- efficiently restore sharing
                                                                      
                                                                      Equations
                                                                      Instances For
                                                                        @[inline]

                                                                        Returns true if sym.debug is set

                                                                        Equations
                                                                        Instances For

                                                                          Adds an issue message to the issue tracker.

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

                                                                            Reports an issue if verbose mode is enabled. Does nothing if verbose is false.

                                                                            Equations
                                                                            Instances For

                                                                              Reports an issue if verbose mode is enabled.

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

                                                                                Reports an issue if both verbose and sym.debug are enabled. Does nothing otherwise.

                                                                                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

                                                                                    Similar to reportIssue!, but only reports issue if sym.debug is set to true.

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

                                                                                      Returns all accumulated issues without clearing them.

                                                                                      Equations
                                                                                      Instances For

                                                                                        Runs x with a fresh issue context. Issues reported during x are prepended to the issues that existed before the call.

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

                                                                                          Similar to Meta.isDefEqI, but the result is cache using pointer equality.

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

                                                                                            SymExtension accessors #

                                                                                            @[implemented_by _private.Lean.Meta.Sym.SymM.0.Lean.Meta.Sym.SymExtension.getStateCoreImpl]
                                                                                            Equations
                                                                                            Instances For
                                                                                              @[implemented_by _private.Lean.Meta.Sym.SymM.0.Lean.Meta.Sym.SymExtension.modifyStateImpl]
                                                                                              opaque Lean.Meta.Sym.SymExtension.modifyState {σ : Type} (ext : SymExtension σ) (f : σσ) :