-
Notifications
You must be signed in to change notification settings - Fork 91
/
InstantiateMVars.lean
65 lines (58 loc) · 2.17 KB
/
InstantiateMVars.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/-
Copyright (c) 2022 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/
import Std.Lean.Meta.Basic
open Lean Lean.Meta
namespace Lean.MVarId
/--
Instantiate metavariables in the type of the given metavariable, update the
metavariable's declaration and return the new type.
-/
def instantiateMVarsInType [Monad m] [MonadMCtx m] [MonadError m]
(mvarId : MVarId) : m Expr := do
let mdecl ← (← getMCtx).getExprMVarDecl mvarId
let type := mdecl.type
if type.hasMVar then
let type ← instantiateMVars type
let mdecl := { mdecl with type }
modifyMCtx (·.declareExprMVar mvarId mdecl)
return type
else
return type
/--
Instantiate metavariables in the `LocalDecl` of the given fvar, update the
`LocalDecl` and return the new `LocalDecl.`
-/
def instantiateMVarsInLocalDecl [Monad m] [MonadMCtx m] [MonadError m]
(mvarId : MVarId) (fvarId : FVarId) : m LocalDecl := do
let mdecl ← (← getMCtx).getExprMVarDecl mvarId
let (some ldecl) := mdecl.lctx.find? fvarId | throwError
"unknown fvar '{fvarId.name}' (in local context of mvar '?{mvarId.name}')"
let ldecl ← Lean.instantiateLocalDeclMVars ldecl
let mdecl :=
{ mdecl with lctx := mdecl.lctx.modifyLocalDecl fvarId fun _ => ldecl }
modifyMCtx (·.declareExprMVar mvarId mdecl)
return ldecl
/--
Instantiate metavariables in the local context of the given metavariable, update
the metavariable's declaration and return the new local context.
-/
def instantiateMVarsInLocalContext [Monad m] [MonadMCtx m] [MonadError m]
(mvarId : MVarId) : m LocalContext := do
let mdecl ← (← getMCtx).getExprMVarDecl mvarId
let lctx ← instantiateLCtxMVars mdecl.lctx
modifyMCtx (·.declareExprMVar mvarId { mdecl with lctx })
return lctx
/--
Instantiate metavariables in the local context and type of the given
metavariable.
-/
def instantiateMVars [Monad m] [MonadMCtx m] [MonadError m] (mvarId : MVarId) :
m Unit := do
discard $ (← getMCtx).getExprMVarDecl mvarId
-- The line above throws an error if the `mvarId` is not declared. The line
-- below panics.
instantiateMVarDeclMVars mvarId
end Lean.MVarId