Skip to content

Commit

Permalink
fix: take synthPendingDepth into account when caching TC results
Browse files Browse the repository at this point in the history
closes #2522
  • Loading branch information
leodemoura authored and kim-em committed May 14, 2024
1 parent 7db8e64 commit 0038581
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Lean/Meta/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,17 @@ instance : Hashable InfoCacheKey :=
fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) <| mixHash (hash expr) (hash nargs)⟩
end InfoCacheKey

abbrev SynthInstanceCache := PersistentHashMap (LocalInstances × Expr) (Option Expr)
structure SynthInstanceCacheKey where
localInsts : LocalInstances
type : Expr
/--
Value of `synthPendingDepth` when instance was synthesized or failed to be synthesized.
See issue #2522.
-/
synthPendingDepth : Nat
deriving Hashable, BEq

abbrev SynthInstanceCache := PersistentHashMap SynthInstanceCacheKey (Option Expr)

abbrev InferTypeCache := PersistentExprStructMap Expr
abbrev FunInfoCache := PersistentHashMap InfoCacheKey FunInfo
Expand Down
5 changes: 3 additions & 2 deletions src/Lean/Meta/SynthInstance.lean
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (
unless defEq do
trace[Meta.synthInstance] "{crossEmoji} result type{indentExpr resultType}\nis not definitionally equal to{indentExpr type}"
return defEq
match s.cache.synthInstance.find? (localInsts, type) with
let cacheKey := { localInsts, type, synthPendingDepth := (← read).synthPendingDepth }
match s.cache.synthInstance.find? cacheKey with
| some result =>
trace[Meta.synthInstance] "result {result} (cached)"
if let some inst := result then
Expand Down Expand Up @@ -767,7 +768,7 @@ def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (
pure (some result)
else
pure none
modify fun s => { s with cache.synthInstance := s.cache.synthInstance.insert (localInsts, type) result? }
modify fun s => { s with cache.synthInstance := s.cache.synthInstance.insert cacheKey result? }
pure result?

/--
Expand Down

0 comments on commit 0038581

Please sign in to comment.