reimplement matchDecls
This commit is contained in:
@@ -20,5 +20,7 @@ def abstractCtx (goal : MVarId) : MetaM AbstractCtxResult := do
|
|||||||
def openAbstractCtxResult (res : AbstractCtxResult) (k : Array Expr → Expr → MetaM α) : MetaM α := do
|
def openAbstractCtxResult (res : AbstractCtxResult) (k : Array Expr → Expr → MetaM α) : MetaM α := do
|
||||||
let (mvars, binderInfo, expr) ← openAbstractMVarsResult res.abstractMVarsResult
|
let (mvars, binderInfo, expr) ← openAbstractMVarsResult res.abstractMVarsResult
|
||||||
lambdaLetTelescope (← instantiateMVars expr) k
|
lambdaLetTelescope (← instantiateMVars expr) k
|
||||||
|
-- TODO: Unfornately, lambdaLetTelescope does not allow us to provide the number of arguments.
|
||||||
|
-- If the goal is a function, this will not work.
|
||||||
|
|
||||||
end AbstractCtx
|
end AbstractCtx
|
||||||
|
|||||||
@@ -79,28 +79,30 @@ partial def matchExpr (pattern : Expr) (e : Expr) (bij : FVarBijection := {}) :
|
|||||||
| _, _ => none
|
| _, _ => none
|
||||||
|
|
||||||
/-- Check if each fvar in `patterns` has a matching fvar in `fvars` -/
|
/-- Check if each fvar in `patterns` has a matching fvar in `fvars` -/
|
||||||
def matchDecls (patterns : Array Expr) (fvars : Array Expr) : Bool :=
|
def matchDecls (patterns : Array Expr) (fvars : Array Expr) (strict := true) (initBij : FVarBijection := {}) : MetaM Bool := do
|
||||||
-- We iterate through the array backwards hoping that this will find us faster results
|
-- We iterate through the array backwards hoping that this will find us faster results
|
||||||
-- TODO: implement backtracking
|
-- TODO: implement backtracking
|
||||||
-- TODO: reimplement this
|
let mut bij := initBij
|
||||||
patterns.size = fvars.size
|
for i in [:patterns.size] do
|
||||||
-- let mut usedFvars := (List.replicate declFvars.size false).toArray
|
let pattern := patterns[patterns.size - i - 1]!
|
||||||
-- -- `usedFvars` keeps track of the fvars that were already used to match an mvar.
|
if bij.forward.contains pattern.fvarId! then
|
||||||
-- for i in [:declMvars.size] do
|
continue
|
||||||
-- let declMvar := declMvars[declMvars.size - i - 1]!
|
for j in [:fvars.size] do
|
||||||
-- let mut found := false
|
let fvar := fvars[fvars.size - j - 1]!
|
||||||
-- for j in [:declFvars.size] do
|
if bij.backward.contains fvar.fvarId! then
|
||||||
-- let declFvar := declFvars[declFvars.size - j - 1]!
|
continue
|
||||||
-- let usedFvar := usedFvars[declFvars.size - j - 1]!
|
|
||||||
-- if ¬ usedFvar then
|
if let some bij' := matchExpr
|
||||||
-- if let some _ := matchExpr declMvar declFvar then
|
(← instantiateMVars $ ← inferType pattern)
|
||||||
-- usedFvars := usedFvars.set! (declFvars.size - j - 1) true
|
(← instantiateMVars $ ← inferType fvar) bij then
|
||||||
-- found := true
|
-- usedFvars := usedFvars.set! (fvars.size - j - 1) true
|
||||||
-- break
|
bij := bij'.insert pattern.fvarId! fvar.fvarId!
|
||||||
-- else
|
break
|
||||||
-- continue
|
if ! bij.forward.contains pattern.fvarId! then return false
|
||||||
-- if ¬ found then return false
|
|
||||||
-- return true
|
if strict then
|
||||||
|
return fvars.all (fun fvar => bij.backward.contains fvar.fvarId!)
|
||||||
|
return true
|
||||||
|
|
||||||
unsafe def evalHintMessageUnsafe : Expr → MetaM (Array Expr → MessageData) :=
|
unsafe def evalHintMessageUnsafe : Expr → MetaM (Array Expr → MessageData) :=
|
||||||
evalExpr (Array Expr → MessageData)
|
evalExpr (Array Expr → MessageData)
|
||||||
@@ -118,10 +120,10 @@ def findHints (goal : MVarId) (doc : FileWorker.EditableDocument) : MetaM (Array
|
|||||||
| throwError "Level not found: {doc.meta.mkInputContext.fileName}"
|
| throwError "Level not found: {doc.meta.mkInputContext.fileName}"
|
||||||
let hints ← level.hints.filterMapM fun hint => do
|
let hints ← level.hints.filterMapM fun hint => do
|
||||||
openAbstractCtxResult hint.goal fun hintFVars hintGoal => do
|
openAbstractCtxResult hint.goal fun hintFVars hintGoal => do
|
||||||
if let some _ := matchExpr (← instantiateMVars $ hintGoal) (← instantiateMVars $ ← inferType $ mkMVar goal)
|
if let some fvarBij := matchExpr (← instantiateMVars $ hintGoal) (← instantiateMVars $ ← inferType $ mkMVar goal)
|
||||||
then
|
then
|
||||||
let lctx := (← goal.getDecl).lctx
|
let lctx := (← goal.getDecl).lctx
|
||||||
if matchDecls hintFVars lctx.getFVars
|
if ← matchDecls hintFVars lctx.getFVars (strict := true) (initBij := fvarBij)
|
||||||
then
|
then
|
||||||
let text := (← evalHintMessage hint.text) hintFVars
|
let text := (← evalHintMessage hint.text) hintFVars
|
||||||
let ctx := {env := ← getEnv, mctx := ← getMCtx, lctx := ← getLCtx, opts := {}}
|
let ctx := {env := ← getEnv, mctx := ← getMCtx, lctx := ← getLCtx, opts := {}}
|
||||||
|
|||||||
Reference in New Issue
Block a user