-
Notifications
You must be signed in to change notification settings - Fork 423
/
Renaming.lean
61 lines (54 loc) · 2.1 KB
/
Renaming.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
/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Lean.Compiler.LCNF.CompilerM
namespace Lean.Compiler.LCNF
/--
A mapping from free variable id to binder name.
-/
abbrev Renaming := FVarIdMap Name
def Param.applyRenaming (param : Param) (r : Renaming) : CompilerM Param := do
if let some binderName := r.find? param.fvarId then
let param := { param with binderName }
modifyLCtx fun lctx => lctx.addParam param
return param
else
return param
def LetDecl.applyRenaming (decl : LetDecl) (r : Renaming) : CompilerM LetDecl := do
if let some binderName := r.find? decl.fvarId then
let decl := { decl with binderName }
modifyLCtx fun lctx => lctx.addLetDecl decl
return decl
else
return decl
mutual
partial def FunDeclCore.applyRenaming (decl : FunDecl) (r : Renaming) : CompilerM FunDecl := do
if let some binderName := r.find? decl.fvarId then
let decl := { decl with binderName }
modifyLCtx fun lctx => lctx.addFunDecl decl
decl.updateValue (← decl.value.applyRenaming r)
else
decl.updateValue (← decl.value.applyRenaming r)
partial def Code.applyRenaming (code : Code) (r : Renaming) : CompilerM Code := do
match code with
| .let decl k => return code.updateLet! (← decl.applyRenaming r) (← k.applyRenaming r)
| .fun decl k | .jp decl k => return code.updateFun! (← decl.applyRenaming r) (← k.applyRenaming r)
| .cases c =>
let alts ← c.alts.mapMonoM fun alt =>
match alt with
| .default k => return alt.updateCode (← k.applyRenaming r)
| .alt _ ps k => return alt.updateAlt! (← ps.mapMonoM (·.applyRenaming r)) (← k.applyRenaming r)
return code.updateAlts! alts
| .jmp .. | .unreach .. | .return .. => return code
end
def Decl.applyRenaming (decl : Decl) (r : Renaming) : CompilerM Decl := do
if r.isEmpty then
return decl
else
let params ← decl.params.mapMonoM (·.applyRenaming r)
let value ← decl.value.applyRenaming r
return { decl with params, value }
end Lean.Compiler.LCNF