Skip to content

Commit 94f1c74

Browse files
committed
simplify
1 parent 1314af5 commit 94f1c74

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

src/Lean/Elab/Command.lean

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ structure State where
3131
snapshotTasks : Array (Language.SnapshotTask Language.SnapshotTree) := #[]
3232
deriving Nonempty
3333

34-
structure DerivedState extends State where
35-
private coreCtx : Core.Context
36-
private coreState : Core.State
37-
3834
structure Context where
3935
fileName : String
4036
fileMap : FileMap
@@ -63,7 +59,7 @@ structure Context where
6359
-/
6460
suppressElabErrors : Bool := false
6561

66-
abbrev CommandElabM := ReaderT Context $ StateRefT DerivedState $ EIO Exception
62+
abbrev CommandElabM := ReaderT Context $ StateRefT State $ EIO Exception
6763
abbrev CommandElab := Syntax → CommandElabM Unit
6864
structure Linter where
6965
run : Syntax → CommandElabM Unit
@@ -78,9 +74,22 @@ Remark: see comment at TermElabM
7874
@[always_inline]
7975
instance : Monad CommandElabM := let i := inferInstanceAs (Monad CommandElabM); { pure := i.pure, bind := i.bind }
8076

81-
private def DerivedState.derive (ctx : Context) (s : State) : DerivedState where
82-
toState := s
83-
coreCtx := {
77+
private def State.updateCore (s : State) (coreS : Core.State) : State :=
78+
{ s with
79+
env := coreS.env
80+
nextMacroScope := coreS.nextMacroScope
81+
ngen := coreS.ngen
82+
auxDeclNGen := coreS.auxDeclNGen
83+
infoState := coreS.infoState
84+
traceState := coreS.traceState
85+
snapshotTasks := coreS.snapshotTasks
86+
messages := coreS.messages }
87+
88+
private def liftCoreMDirect (x : CoreM α) : CommandElabM α := do
89+
let ctx ← read
90+
let s ← get
91+
let scope := s.scopes.head!
92+
let coreCtx := {
8493
fileName := ctx.fileName
8594
fileMap := ctx.fileMap
8695
currRecDepth := ctx.currRecDepth
@@ -94,7 +103,7 @@ private def DerivedState.derive (ctx : Context) (s : State) : DerivedState where
94103
options := scope.opts
95104
cancelTk? := ctx.cancelTk?
96105
suppressElabErrors := ctx.suppressElabErrors }
97-
coreState := {
106+
let coreState := {
98107
env := s.env
99108
ngen := s.ngen
100109
auxDeclNGen := s.auxDeclNGen
@@ -104,40 +113,17 @@ private def DerivedState.derive (ctx : Context) (s : State) : DerivedState where
104113
snapshotTasks := s.snapshotTasks
105114
messages := s.messages
106115
}
107-
where
108-
scope := s.scopes.head!
109-
110-
private def State.updateCore (s : State) (coreS : Core.State) : State :=
111-
{ s with
112-
env := coreS.env
113-
nextMacroScope := coreS.nextMacroScope
114-
ngen := coreS.ngen
115-
auxDeclNGen := coreS.auxDeclNGen
116-
infoState := coreS.infoState
117-
traceState := coreS.traceState
118-
snapshotTasks := coreS.snapshotTasks
119-
messages := coreS.messages }
120-
121-
instance : MonadStateOf State CommandElabM where
122-
get := return (← getThe DerivedState).toState
123-
set s := private do set (DerivedState.derive (← read) s)
124-
modifyGet f := private do
125-
let ctx ← read
126-
modifyGetThe DerivedState fun ds =>
127-
let (a, s') := f ds.toState
128-
(a, DerivedState.derive ctx s')
116+
let (a, coreS) ← x.run coreCtx coreState
117+
modify (·.updateCore coreS)
118+
return a
129119

130120
instance : MonadLift CoreM CommandElabM where
131-
monadLift x := private do
132-
let ds ← getThe DerivedState
133-
let (a, coreS) ← x.run ds.coreCtx ds.coreState
134-
set { ds with toState := ds.toState.updateCore coreS, coreState := coreS }
135-
return a
121+
monadLift := private liftCoreMDirect
122+
123+
instance : MonadStateOf State CommandElabM := inferInstance
136124

137125
protected nonrec def CommandElabM.run (ctx : Context) (s : State) (x : CommandElabM α) : EIO Exception (α × State) := do
138-
let ds := DerivedState.derive ctx s
139-
let (a, ds') ← x.run ctx |>.run ds
140-
return (a, ds'.toState)
126+
x.run ctx |>.run s
141127

142128
protected def CommandElabM.run' (ctx : Context) (s : State) (x : CommandElabM α) : EIO Exception α := do
143129
(·.1) <$> x.run ctx s
@@ -229,17 +215,17 @@ instance : MonadQuotation CommandElabM where
229215
withFreshMacroScope := Command.withFreshMacroScope
230216

231217
private def runCore (x : CoreM α) : CommandElabM α := do
232-
let s ← getThe DerivedState
233218
let ctx ← read
234219
let heartbeats ← IO.getNumHeartbeats
235-
let env := Kernel.resetDiag s.env
236-
let coreCtx : Core.Context := { s.coreCtx with initHeartbeats := heartbeats }
237-
let x : EIO _ _ := x.run coreCtx { s.coreState with env }
238-
let (ea, coreS) ← liftM x
239-
modify fun s => s.updateCore { coreS with
240-
traceState.traces := coreS.traceState.traces.map fun t => { t with ref := replaceRef t.ref ctx.ref }
241-
}
242-
return ea
220+
let env := Kernel.resetDiag (← getEnv)
221+
liftM (m := CoreM) do
222+
withReader ({ · with initHeartbeats := heartbeats }) do
223+
modify ({ · with env })
224+
let a ← x
225+
modify fun s => { s with
226+
traceState.traces := s.traceState.traces.map fun t => { t with ref := replaceRef t.ref ctx.ref }
227+
}
228+
return a
243229

244230
def liftCoreM (x : CoreM α) : CommandElabM α := do
245231
MonadExcept.ofExcept (← runCore (observing x))

0 commit comments

Comments
 (0)