File: .lake/packages/lean4/src/lean/Lean/Meta/Tactic/LibrarySearch.lean

full_name: Lean.Meta.LibrarySearch.initFn._@..lake.packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch._hyg.5
code: builtin_initialize registerTraceClass `Tactic.librarySearch
goal: IO Unit

full_name: Lean.Meta.LibrarySearch.initFn._@..lake.packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch._hyg.58
code: builtin_initialize registerTraceClass `Tactic.librarySearch.lemmas
goal: IO Unit

full_name: Lean.Meta.LibrarySearch.solveByElim
code: /--
Wrapper for calling `Lean.Meta.SolveByElim.solveByElim with
appropriate arguments for library search.
-/
def solveByElim (required : List Expr) (exfalso : Bool) (goals : List MVarId) (maxDepth : Nat) 
goal: Lean.MetaM (List Lean.MVarId)

full_name: Lean.Meta.LibrarySearch.DeclMod
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Type

full_name: Lean.Meta.LibrarySearch.instDecidableEqDeclMod
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: DecidableEq Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.DeclMod.mpr
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.instInhabitedDeclMod
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Inhabited Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.DeclMod.mp
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.instOrdDeclMod
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Ord Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.DeclMod.none
code: /--
A "modifier" for a declaration.
* `none` indicates the original declaration,
* `mp` indicates that (possibly after binders) the declaration is an `↔`,
  and we want to consider the forward direction,
* `mpr` similarly, but for the backward direction.
-/
inductive DeclMod
  | /-- the original declaration -/ none
  | /-- the forward direction of an `iff` -/ mp
  | /-- the backward direction of an `iff` -/ mpr
deriving DecidableEq, Inhabited, Ord
goal: Lean.Meta.LibrarySearch.DeclMod

full_name: Lean.Meta.LibrarySearch.CandidateFinder
code: /--
LibrarySearch has an extension mechanism for replacing the function used
to find candidate lemmas.
-/
@[reducible]
def CandidateFinder 
goal: Type

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.addImport
code: private def addImport (name : Name) (constInfo : ConstantInfo) :
    MetaM (Array (InitEntry (Name × DeclMod))) 
goal: Lean.MetaM (Array (Lean.Meta.LazyDiscrTree.InitEntry (Lean.Name × Lean.Meta.LibrarySearch.DeclMod)))

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.LibSearchState
code: /-- Stores import discrimination tree. -/
private def LibSearchState 
goal: Type

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.defaultLibSearchState
code: private builtin_initialize defaultLibSearchState : IO.Ref (Option (LazyDiscrTree (Name × DeclMod))) ← do
  IO.mkRef .none
goal: IO.Ref (Option (Lean.Meta.LazyDiscrTree (Lean.Name × Lean.Meta.LibrarySearch.DeclMod)))

full_name: Lean.Meta.LibrarySearch.initFn._@..lake.packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch._hyg.622
code: private builtin_initialize defaultLibSearchState : IO.Ref (Option (LazyDiscrTree (Name × DeclMod))) ← do
  IO.mkRef .none
goal: IO (IO.Ref (Option (Lean.Meta.LazyDiscrTree (Lean.Name × Lean.Meta.LibrarySearch.DeclMod))))

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.instInhabitedLibSearchState
code: private instance : Inhabited LibSearchState 
goal: Inhabited Lean.Meta.LibrarySearch.LibSearchState

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.ext
code: private builtin_initialize ext : EnvExtension LibSearchState ←
  registerEnvExtension (IO.mkRef .none)
goal: Lean.EnvExtension Lean.Meta.LibrarySearch.LibSearchState

full_name: Lean.Meta.LibrarySearch.initFn._@..lake.packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch._hyg.678
code: private builtin_initialize ext : EnvExtension LibSearchState ←
  registerEnvExtension (IO.mkRef .none)
goal: IO (Lean.EnvExtension Lean.Meta.LibrarySearch.LibSearchState)

full_name: Lean.Meta.LibrarySearch.droppedKeys
code: /--
We drop `.star` and `Eq * * *` from the discriminator trees because
they match too much.
-/
def droppedKeys : List (List LazyDiscrTree.Key) 
goal: List (List Lean.Meta.LazyDiscrTree.Key)

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.constantsPerImportTask
code: /--
The maximum number of constants an individual task may perform.

The value was picked because it roughly correponded to 50ms of work on the
machine this was developed on.  Smaller numbers did not seem to improve
performance when importing Std and larger numbers (<10k) seemed to degrade
initialization performance.
-/
private def constantsPerImportTask : Nat 
goal: Nat

full_name: Lean.Meta.LibrarySearch.libSearchFindDecls
code: /-- Create function for finding relevant declarations. -/
def libSearchFindDecls : Expr → MetaM (Array (Name × DeclMod)) 
goal: Lean.Expr → Lean.MetaM (Array (Lean.Name × Lean.Meta.LibrarySearch.DeclMod))

full_name: Lean.Meta.LibrarySearch.mkHeartbeatCheck
code: /--
Return an action that returns true when the remaining heartbeats is less
than the currently remaining heartbeats * leavePercent / 100.
-/
def mkHeartbeatCheck (leavePercent : Nat) : MetaM (MetaM Bool) 
goal: Lean.MetaM (Lean.MetaM Bool)

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.librarySearchEmoji
code: private def librarySearchEmoji : Except ε (Option α) → String

goal: Except ε (Option α) → String

full_name: Lean.Meta.LibrarySearch.interleaveWith
code: /--
Interleave x y interleaves the elements of x and y until one is empty and then returns
final elements in other list.
-/
def interleaveWith {α β γ} (f : α → γ) (x : Array α) (g : β → γ) (y : Array β) : Array γ 
goal: Array γ

full_name: Lean.Meta.LibrarySearch.initFn._@..lake.packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch._hyg.1161
code: /--
An exception ID that indicates further speculation on candidate lemmas should stop
and current results should be returned.
-/
private builtin_initialize abortSpeculationId : InternalExceptionId ←
  registerInternalExceptionId `Lean.Meta.LibrarySearch.abortSpeculation
goal: IO Lean.InternalExceptionId

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.abortSpeculationId
code: /--
An exception ID that indicates further speculation on candidate lemmas should stop
and current results should be returned.
-/
private builtin_initialize abortSpeculationId : InternalExceptionId ←
  registerInternalExceptionId `Lean.Meta.LibrarySearch.abortSpeculation
goal: Lean.InternalExceptionId

full_name: Lean.Meta.LibrarySearch.abortSpeculation
code: /--
Called to abort speculative execution in library search.
-/
def abortSpeculation [MonadExcept Exception m] : m α 
goal: m α

full_name: Lean.Meta.LibrarySearch.isAbortSpeculation
code: /-- Returns true if this is an abort speculation exception. -/
def isAbortSpeculation : Exception → Bool

goal: Lean.Exception → Bool

full_name: Lean.Meta.LibrarySearch.Candidate
code: /--
A library search candidate using symmetry includes the goal to solve, the metavar
context for that goal, and the name and orientation of a rule to try using with goal.
-/
@[reducible]
def Candidate 
goal: Type

full_name: Lean.Meta.LibrarySearch.librarySearchSymm
code: /--
Run `searchFn` on both the goal and `symm` applied to the goal.
-/
def librarySearchSymm (searchFn : CandidateFinder) (goal : MVarId) : MetaM (Array Candidate) 
goal: Lean.MetaM (Array Lean.Meta.LibrarySearch.Candidate)

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.emoji
code: private def emoji (e : Except ε α) 
goal: String

full_name: Lean.Meta.LibrarySearch.mkLibrarySearchLemma
code: /-- Create lemma from name and mod. -/
def mkLibrarySearchLemma (lem : Name) (mod : DeclMod) : MetaM Expr 
goal: Lean.MetaM Lean.Expr

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.isVar
code: private def isVar (e : Expr) : Bool 
goal: Bool

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.librarySearchLemma
code: /--
Tries to apply the given lemma (with symmetry modifier) to the goal,
then tries to close subsequent goals using `solveByElim`.
If `solveByElim` succeeds, `[]` is returned as the list of new subgoals,
otherwise the full list of subgoals is returned.
-/
private def librarySearchLemma (cfg : ApplyConfig) (act : List MVarId → MetaM (List MVarId))
    (allowFailure : MVarId → MetaM Bool) (cand : Candidate)  : MetaM (List MVarId) 
goal: Lean.MetaM (List Lean.MVarId)

full_name: Lean.Meta.LibrarySearch.tryOnEach
code: /--
Sequentially invokes a tactic `act` on each value in candidates on the current state.

The tactic `act` should return a list of meta-variables that still need to be resolved.
If this list is empty, then no variables remain to be solved, and `tryOnEach` returns
`none` with the environment set so each goal is resolved.

If the action throws an internal exception with the `abortSpeculationId` id then
further computation is stopped and intermediate results returned. If any other
exception is thrown, then it is silently discarded.
-/
def tryOnEach
    (act : Candidate → MetaM (List MVarId))
    (candidates : Array Candidate) :
    MetaM (Option (Array (List MVarId × MetavarContext))) 
goal: Lean.MetaM (Option (Array (List Lean.MVarId × Lean.MetavarContext)))

full_name: _private.«.lake».packages.lean4.src.lean.Lean.Meta.Tactic.LibrarySearch.0.Lean.Meta.LibrarySearch.librarySearch'
code: private def librarySearch' (goal : MVarId)
    (tactic : List MVarId → MetaM (List MVarId))
    (allowFailure : MVarId → MetaM Bool)
    (leavePercentHeartbeats : Nat) :
    MetaM (Option (Array (List MVarId × MetavarContext))) 
goal: Lean.MetaM (Option (Array (List Lean.MVarId × Lean.MetavarContext)))

full_name: Lean.Meta.LibrarySearch.librarySearch
code: /--
Tries to solve the goal either by:
* calling `tactic true`
* or applying a library lemma then calling `tactic false` on the resulting goals.

Typically here `tactic` is `solveByElim`,
with the `Bool` flag indicating whether it may retry with `exfalso`.

If it successfully closes the goal, returns `none`.
Otherwise, it returns `some a`, where `a : Array (List MVarId × MetavarContext)`,
with an entry for each library lemma which was successfully applied,
containing a list of the subsidiary goals, and the metavariable context after the application.

(Always succeeds, and the metavariable context stored in the monad is reverted,
unless the goal was completely solved.)

(Note that if `solveByElim` solves some but not all subsidiary goals,
this is not currently tracked.)
-/
def librarySearch (goal : MVarId)
    (tactic : Bool → List MVarId → MetaM (List MVarId) :=
      fun initial g => solveByElim [] (maxDepth := 6) (exfalso := initial) g)
    (allowFailure : MVarId → MetaM Bool := fun _ => pure true)
    (leavePercentHeartbeats : Nat := 10) :
    MetaM (Option (Array (List MVarId × MetavarContext))) 
goal: Lean.MetaM (Option (Array (List Lean.MVarId × Lean.MetavarContext)))