chore: use StateRefT macro
This commit is contained in:
parent
c43d2c8a7f
commit
633578cfaf
15 changed files with 24 additions and 28 deletions
|
|
@ -10,8 +10,8 @@ import Init.System.IO
|
|||
import Init.Control.State
|
||||
|
||||
def StateRefT' (ω : Type) (σ : Type) (m : Type → Type) (α : Type) : Type := ReaderT (ST.Ref ω σ) m α
|
||||
-- TODO: remove `[STWorld ω m]`. We should use a tactic for synthesizing ω, and the tactic infers the instance `[STWorld ω m]`
|
||||
abbrev StateRefT {ω : Type} (σ : Type) (m : Type → Type) [STWorld ω m] (α : Type) := StateRefT' ω σ m α
|
||||
|
||||
/- Recall that `StateRefT` is a macro that infers `ω` from the `m`. -/
|
||||
|
||||
@[inline] def StateRefT'.run {ω σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST ω) m] {α : Type} (x : StateRefT' ω σ m α) (s : σ) : m (α × σ) := do
|
||||
let ref ← ST.mkRef s
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ structure Context :=
|
|||
(maxRecDepth : Nat := 1000)
|
||||
(ref : Syntax := Syntax.missing)
|
||||
|
||||
abbrev CoreM := ReaderT Context $ StateRefT State $ EIO Exception
|
||||
abbrev CoreM := ReaderT Context $ StateRefT State (EIO Exception)
|
||||
|
||||
instance {α} : Inhabited (CoreM α) := ⟨fun _ _ => throw $ arbitrary _⟩
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ structure Context :=
|
|||
(currMacroScope : MacroScope := firstFrontendMacroScope)
|
||||
(ref : Syntax := Syntax.missing)
|
||||
|
||||
abbrev CommandElabCoreM (ε) := ReaderT Context $ StateRefT State $ EIO ε
|
||||
abbrev CommandElabCoreM (ε) := ReaderT Context $ StateRefT State (EIO ε)
|
||||
abbrev CommandElabM := CommandElabCoreM Exception
|
||||
abbrev CommandElab := Syntax → CommandElabM Unit
|
||||
abbrev Linter := Syntax → CommandElabM Unit
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ structure BacktrackableState :=
|
|||
(mctx : MetavarContext)
|
||||
(goals : List MVarId)
|
||||
|
||||
abbrev TacticM := ReaderT Context $ StateRefT State $ TermElabM
|
||||
abbrev TacticM := ReaderT Context $ StateRefT State TermElabM
|
||||
abbrev Tactic := Syntax → TacticM Unit
|
||||
|
||||
def saveBacktrackableState : TacticM BacktrackableState := do
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ structure State :=
|
|||
|
||||
instance : Inhabited State := ⟨{}⟩
|
||||
|
||||
abbrev TermElabM := ReaderT Context $ StateRefT State $ MetaM
|
||||
abbrev TermElabM := ReaderT Context $ StateRefT State MetaM
|
||||
abbrev TermElab := Syntax → Option Expr → TermElabM Expr
|
||||
|
||||
open Meta
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ structure Context :=
|
|||
structure State :=
|
||||
(nextIdx : Nat := 1)
|
||||
|
||||
abbrev M := ReaderT Context $ MonadCacheT Expr Expr $ StateRefT State $ MetaM
|
||||
abbrev M := ReaderT Context $ MonadCacheT Expr Expr $ StateRefT State MetaM
|
||||
|
||||
private def mkAuxLemma (e : Expr) : M Expr := do
|
||||
let ctx ← read
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ structure Context :=
|
|||
(lctx : LocalContext := {})
|
||||
(localInstances : LocalInstances := #[])
|
||||
|
||||
abbrev MetaM := ReaderT Context $ StateRefT State $ CoreM
|
||||
abbrev MetaM := ReaderT Context $ StateRefT State CoreM
|
||||
|
||||
instance : MonadIO MetaM :=
|
||||
{ liftIO := fun x => liftM (liftIO x : CoreM _) }
|
||||
|
|
|
|||
|
|
@ -414,7 +414,7 @@ structure Context :=
|
|||
(hasCtxLocals : Bool)
|
||||
(rhs : Expr)
|
||||
|
||||
abbrev CheckAssignmentM := ReaderT Context $ StateRefT State $ MetaM
|
||||
abbrev CheckAssignmentM := ReaderT Context $ StateRefT State MetaM
|
||||
|
||||
def throwCheckAssignmentFailure {α} : CheckAssignmentM α :=
|
||||
throw $ Exception.internal checkAssignmentExceptionId
|
||||
|
|
|
|||
|
|
@ -594,7 +594,7 @@ def instantiateMVars (mctx : MetavarContext) (e : Expr) : Expr × MetavarContext
|
|||
if !e.hasMVar then
|
||||
(e, mctx)
|
||||
else
|
||||
let instantiate {ω} (e : Expr) : (MonadCacheT Expr Expr $ StateRefT MetavarContext $ ST ω) Expr :=
|
||||
let instantiate {ω} (e : Expr) : (MonadCacheT Expr Expr $ StateRefT MetavarContext (ST ω)) Expr :=
|
||||
instantiateExprMVars e
|
||||
runST fun _ => instantiate e $.run $.run mctx
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ structure State :=
|
|||
|
||||
end Formatter
|
||||
|
||||
abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State $ CoreM
|
||||
abbrev FormatterM := ReaderT Formatter.Context $ StateRefT Formatter.State CoreM
|
||||
|
||||
@[inline] def FormatterM.orelse {α} (p₁ p₂ : FormatterM α) : FormatterM α := do
|
||||
let s ← get
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ structure Context :=
|
|||
|
||||
end Parenthesizer
|
||||
|
||||
abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State $ CoreM
|
||||
abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM
|
||||
abbrev Parenthesizer := ParenthesizerM Unit
|
||||
|
||||
@[inline] def ParenthesizerM.orelse {α} (p₁ p₂ : ParenthesizerM α) : ParenthesizerM α := do
|
||||
|
|
|
|||
|
|
@ -59,18 +59,18 @@ namespace MonadCacheT
|
|||
variables {ω α β : Type} {m : Type → Type} [STWorld ω m] [HasBeq α] [Hashable α] [MonadLiftT (ST ω) m] [Monad m]
|
||||
|
||||
instance : MonadHashMapCacheAdapter α β (MonadCacheT α β m) :=
|
||||
{ getCache := (get : StateRefT _ _ _),
|
||||
modifyCache := fun f => (modify f : StateRefT _ _ _) }
|
||||
{ getCache := (get : StateRefT' ..),
|
||||
modifyCache := fun f => (modify f : StateRefT' ..) }
|
||||
|
||||
@[inline] def run {σ} (x : MonadCacheT α β m σ) : m σ :=
|
||||
x.run' Std.mkHashMap
|
||||
|
||||
instance : Monad (MonadCacheT α β m) := inferInstanceAs (Monad (StateRefT _ _))
|
||||
instance : MonadLift m (MonadCacheT α β m) := inferInstanceAs (MonadLift m (StateRefT _ _))
|
||||
instance [MonadIO m] : MonadIO (MonadCacheT α β m) := inferInstanceAs (MonadIO (StateRefT _ _))
|
||||
instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (MonadCacheT α β m) := inferInstanceAs (MonadExceptOf ε (StateRefT _ _))
|
||||
instance : MonadControl m (MonadCacheT α β m) := inferInstanceAs (MonadControl m (StateRefT _ _))
|
||||
instance [MonadFinally m] : MonadFinally (MonadCacheT α β m) := inferInstanceAs (MonadFinally (StateRefT _ _))
|
||||
instance : Monad (MonadCacheT α β m) := inferInstanceAs (Monad (StateRefT' _ _ _))
|
||||
instance : MonadLift m (MonadCacheT α β m) := inferInstanceAs (MonadLift m (StateRefT' _ _ _))
|
||||
instance [MonadIO m] : MonadIO (MonadCacheT α β m) := inferInstanceAs (MonadIO (StateRefT' _ _ _))
|
||||
instance (ε) [MonadExceptOf ε m] : MonadExceptOf ε (MonadCacheT α β m) := inferInstanceAs (MonadExceptOf ε (StateRefT' _ _ _))
|
||||
instance : MonadControl m (MonadCacheT α β m) := inferInstanceAs (MonadControl m (StateRefT' _ _ _))
|
||||
instance [MonadFinally m] : MonadFinally (MonadCacheT α β m) := inferInstanceAs (MonadFinally (StateRefT' _ _ _))
|
||||
|
||||
end MonadCacheT
|
||||
end Lean
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
|
||||
|
||||
def checkM (b : IO Bool) : IO Unit :=
|
||||
unlessM b (throw $ IO.userError "failed")
|
||||
|
||||
abbrev M := ExceptT String $ StateRefT Nat $ IO
|
||||
abbrev M := ExceptT String $ StateRefT Nat IO
|
||||
|
||||
def f1 : M Nat :=
|
||||
throw "error 1"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
|
||||
|
||||
def f (v : Nat) : StateRefT Nat IO Nat := do
|
||||
IO.println "hello"
|
||||
modify fun s => s - v
|
||||
|
|
@ -41,7 +39,7 @@ instance monadState.hasGetAt (β : Type) (v : β) (α : Type) (m : Type → Type
|
|||
|
||||
export HasGetAt (getAt)
|
||||
|
||||
abbrev M := StateRefT (Label 0 Nat) $ StateRefT (Label 1 Nat) $ StateRefT (Label 2 Nat) IO
|
||||
abbrev M := StateRefT (Label 0 Nat) (StateRefT (Label 1 Nat) (StateRefT (Label 2 Nat) IO))
|
||||
|
||||
def f4 : M Nat := do
|
||||
let a0 : Nat ← getAt 0
|
||||
|
|
@ -54,7 +52,7 @@ pure (a0 + a1 + a2)
|
|||
|
||||
#eval f4.run' ⟨10⟩ $.run' ⟨20⟩ $.run' ⟨30⟩
|
||||
|
||||
abbrev S (ω : Type) := StateRefT Nat $ StateRefT String $ ST ω
|
||||
abbrev S (ω : Type) := StateRefT Nat (StateRefT String (ST ω))
|
||||
|
||||
def f5 {ω} : S ω Unit := do
|
||||
let s ← getThe String
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@ but is expected to have type
|
|||
typeMismatch.lean:12:0: error: type mismatch
|
||||
Meta.isDefEq x x
|
||||
has type
|
||||
ReaderT Meta.Context (StateRefT Meta.State CoreM) Bool
|
||||
ReaderT Meta.Context (StateRefT' IO.RealWorld Meta.State CoreM) Bool
|
||||
but is expected to have type
|
||||
MetaM Unit
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue