Skip to content

Commit

Permalink
Use same DiscrTree config as simp for indexes (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLimperg authored Dec 5, 2024
1 parent 1c1c651 commit 43bcb19
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Aesop/Builder/Cases.lean
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def check (decl : Name) (p : CasesPattern) : MetaM Unit :=
throwError "expected pattern '{p}' ({toString p}) to be an application of '{decl}'"

def toIndexingMode (p : CasesPattern) : MetaM IndexingMode :=
withoutModifyingState do
.hyps <$> (withConfigWithKey discrTreeConfig <| DiscrTree.mkPath (← p.toExpr))
withoutModifyingState do .hyps <$> mkDiscrTreePath (← p.toExpr)

end CasesPattern

Expand Down
2 changes: 1 addition & 1 deletion Aesop/Builder/Forward.lean
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private def forwardIndexingModeCore (type : Expr)
match args.get? i with
| some arg =>
let argT := (← arg.mvarId!.getDecl).type
let keys ← withConfigWithKey discrTreeConfig <| DiscrTree.mkPath argT
let keys ← mkDiscrTreePath argT
return .hyps keys
| none => throwError
"aesop: internal error: immediate arg for forward rule is out of range"
Expand Down
3 changes: 1 addition & 2 deletions Aesop/Frontend/RuleExpr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ def elabSingleIndexingMode (stx : Syntax) : ElabM IndexingMode :=
where
elabKeys (stx : Syntax) : ElabM (Array DiscrTree.Key) :=
show TermElabM _ from withoutModifyingState do
let e ← elabPattern stx
withConfigWithKey discrTreeConfig <| DiscrTree.mkPath (← instantiateMVars e)
mkDiscrTreePath (← elabPattern stx)

def IndexingMode.elab (stxs : Array Syntax) : ElabM IndexingMode :=
.or <$> stxs.mapM elabSingleIndexingMode
Expand Down
4 changes: 2 additions & 2 deletions Aesop/Index.lean
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private def applicableByTargetRules (ri : Index α) (goal : MVarId)
(include? : Rule α → Bool) :
MetaM (Array (Rule α × Array IndexMatchLocation)) :=
goal.withContext do
let rules ← withConfigWithKey discrTreeConfig <| ri.byTarget.getUnify (← goal.getType)
let rules ← getUnify ri.byTarget (← goal.getType)
let mut rs := Array.mkEmpty rules.size
-- Assumption: include? is true for most rules.
for r in rules do
Expand All @@ -114,7 +114,7 @@ private def applicableByHypRules (ri : Index α) (goal : MVarId)
for localDecl in ← getLCtx do
if localDecl.isImplementationDetail then
continue
let rules ← withConfigWithKey discrTreeConfig <| ri.byHyp.getUnify localDecl.type
let rules ← getUnify ri.byHyp localDecl.type
for r in rules do
if include? r then
rs := rs.push (r, #[.hyp localDecl])
Expand Down
2 changes: 1 addition & 1 deletion Aesop/Index/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ instance : ToFormat IndexingMode :=
⟨IndexingMode.format⟩

def targetMatchingConclusion (type : Expr) : MetaM IndexingMode := do
let path ← getConclusionDiscrTreeKeys type discrTreeConfig
let path ← getConclusionDiscrTreeKeys type
return target path

def hypsMatchingConst (decl : Name) : MetaM IndexingMode := do
Expand Down
28 changes: 28 additions & 0 deletions Aesop/Index/DiscrKeyConfig.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/-
Copyright (c) 2024 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/

import Lean

open Lean Lean.Meta

namespace Aesop

/-- The configuration used by all Aesop indices. -/
-- I don't really know what I'm doing here. I'm just copying the config used
-- by `simp`; see `Meta/Simp/Types.lean:mkIndexConfig`.
def indexConfig : ConfigWithKey :=
({ proj := .no, transparency := .reducible : Config }).toConfigWithKey

def mkDiscrTreePath (e : Expr) : MetaM (Array DiscrTree.Key) :=
withConfigWithKey indexConfig $ DiscrTree.mkPath e

def getUnify (t : DiscrTree α) (e : Expr) : MetaM (Array α) :=
withConfigWithKey indexConfig $ t.getUnify e

def getMatch (t : DiscrTree α) (e : Expr) : MetaM (Array α) :=
withConfigWithKey indexConfig $ t.getMatch e

end Aesop
28 changes: 28 additions & 0 deletions Aesop/Index/DiscrTreeConfig.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/-
Copyright (c) 2024 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/

import Lean

open Lean Lean.Meta

namespace Aesop

/-- The configuration used by all Aesop indices. -/
-- I don't really know what I'm doing here. I'm just copying the config used
-- by `simp`; see `Meta/Simp/Types.lean:mkIndexConfig`.
def indexConfig : ConfigWithKey :=
({ proj := .no, transparency := .reducible : Config }).toConfigWithKey

def mkDiscrTreePath (e : Expr) : MetaM (Array DiscrTree.Key) :=
withConfigWithKey indexConfig $ DiscrTree.mkPath e

def getUnify (t : DiscrTree α) (e : Expr) : MetaM (Array α) :=
withConfigWithKey indexConfig $ t.getUnify e

def getMatch (t : DiscrTree α) (e : Expr) : MetaM (Array α) :=
withConfigWithKey indexConfig $ t.getMatch e

end Aesop
6 changes: 3 additions & 3 deletions Aesop/Util/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg, Asta Halkjær From
-/

import Aesop.Index.DiscrTreeConfig
import Aesop.Nanos
import Aesop.Util.UnorderedArraySet
import Batteries.Lean.Expr
Expand Down Expand Up @@ -78,11 +79,10 @@ open DiscrTree

-- For `type = ∀ (x₁, ..., xₙ), T`, returns keys that match `T * ... *` (with
-- `n` stars).
def getConclusionDiscrTreeKeys (type : Expr) (config : ConfigWithKey) :
MetaM (Array Key) :=
def getConclusionDiscrTreeKeys (type : Expr) : MetaM (Array Key) :=
withoutModifyingState do
let (_, _, conclusion) ← forallMetaTelescope type
withConfigWithKey config <| mkPath conclusion
mkDiscrTreePath conclusion
-- We use a meta telescope because `DiscrTree.mkPath` ignores metas (they
-- turn into `Key.star`) but not fvars.

Expand Down

0 comments on commit 43bcb19

Please sign in to comment.