refactor: rename MonadFinally.finally' => MonadFinally.tryFinally'

This commit is contained in:
Leonardo de Moura 2020-10-22 17:40:30 -07:00
parent 7c0b55ec6a
commit d4a67baa8e
5 changed files with 17 additions and 13 deletions

View file

@ -120,7 +120,7 @@ instance {δ} [Backtrackable δ σ] : MonadExceptOf ε (EStateM ε σ) :=
{ throw := @EStateM.throw _ _, tryCatch := @EStateM.tryCatch _ _ _ _ }
instance : MonadFinally (EStateM ε σ) :=
{ finally' := fun α β x h s =>
{ tryFinally' := fun α β x h s =>
let r := x s;
match r with
| Result.ok a s => match h (some a) s with

View file

@ -188,23 +188,27 @@ instance ExceptT.monadControl (ε : Type u) (m : Type u → Type v) [Monad m] :
}
class MonadFinally (m : Type u → Type v) :=
(finally' {α β} : m α → (Option α → m β) → m (α × β))
(tryFinally' {α β} : m α → (Option α → m β) → m (α × β))
export MonadFinally (finally')
export MonadFinally (tryFinally')
/-- Execute `x` and then execute `finalizer` even if `x` threw an exception -/
@[inline] abbrev tryFinally {m : Type u → Type v} {α β : Type u} [MonadFinally m] [Functor m] (x : m α) (finalizer : m β) : m α := do
Prod.fst <$> tryFinally' x (fun _ => finalizer)
-- TODO delete
@[inline] abbrev finally {m : Type u → Type v} {α β : Type u} [MonadFinally m] [Functor m] (x : m α) (finalizer : m β) : m α := do
Prod.fst <$> finally' x (fun _ => finalizer)
Prod.fst <$> tryFinally' x (fun _ => finalizer)
instance Id.finally : MonadFinally Id :=
{ finally' := fun α β x h =>
{ tryFinally' := fun α β x h =>
let a := x;
let b := h (some x);
pure (a, b) }
instance ExceptT.finally {m : Type u → Type v} {ε : Type u} [MonadFinally m] [Monad m] : MonadFinally (ExceptT ε m) :=
{ finally' := fun α β x h => ExceptT.mk do
r ← finally' x (fun e? => match e? with
{ tryFinally' := fun α β x h => ExceptT.mk do
r ← tryFinally' x (fun e? => match e? with
| some (Except.ok a) => h (some a)
| _ => h none);
match r with

View file

@ -118,8 +118,8 @@ instance ReaderT.monadControl (ρ : Type u) (m : Type u → Type v) : MonadContr
restoreM := fun α x ctx => x,
}
instance ReaderT.finally {m : Type u → Type v} {ρ : Type u} [MonadFinally m] [Monad m] : MonadFinally (ReaderT ρ m) :=
{ finally' := fun α β x h ctx => finally' (x ctx) (fun a? => h a? ctx) }
instance ReaderT.tryFinally {m : Type u → Type v} {ρ : Type u} [MonadFinally m] [Monad m] : MonadFinally (ReaderT ρ m) :=
{ tryFinally' := fun α β x h ctx => tryFinally' (x ctx) (fun a? => h a? ctx) }
class MonadWithReaderOf (ρ : Type u) (m : Type u → Type v) :=
(withReader {α : Type u} : (ρρ) → m α → m α)

View file

@ -154,9 +154,9 @@ instance StateT.monadControl (σ : Type u) (m : Type u → Type v) [Monad m] : M
restoreM := fun α x => do (a, s) ← liftM x; set s; pure a
}
instance StateT.finally {m : Type u → Type v} {σ : Type u} [MonadFinally m] [Monad m] : MonadFinally (StateT σ m) :=
{ finally' := fun α β x h s => do
((a, _), (b, s'')) ← finally' (x s)
instance StateT.tryFinally {m : Type u → Type v} {σ : Type u} [MonadFinally m] [Monad m] : MonadFinally (StateT σ m) :=
{ tryFinally' := fun α β x h s => do
((a, _), (b, s'')) ← tryFinally' (x s)
(fun p? => match p? with
| some (a, s') => h (some a) s'
| none => h none s);

View file

@ -1400,7 +1400,7 @@ def doTryToCode (doSeqToCode : List Syntax → M CodeBlock) (doTry : Syntax) (do
if hasBreakContinueReturn finallyCode.code then
throwError "'finally' currently does 'return', 'break', nor 'continue'"
let finallyTerm ← liftMacroM $ ToTerm.run finallyCode.code ctx.m {} ToTerm.Kind.regular
`(«finally» $term $finallyTerm)
`(tryFinally $term $finallyTerm)
let doElemsNew ← liftMacroM $ ToTerm.matchNestedTermResult ref term uvars a r bc
doSeqToCode (doElemsNew ++ doElems)