forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.lean
41 lines (33 loc) · 1.13 KB
/
Error.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
/-
Copyright (c) 2021 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
namespace Lake
class MonadError (m : Type u → Type v) where
error {α : Type u} : String → m α
export MonadError (error)
instance [MonadLift m n] [MonadError m] : MonadError n where
error msg := liftM (m := m) <| error msg
instance : MonadError IO where
error msg := throw <| IO.userError msg
instance : MonadError (EIO String) where
error msg := throw msg
instance : MonadError (Except String) where
error msg := throw msg
/--
Perform an EIO action.
If it throws an error, invoke `error` with its string representation.
-/
@[inline] protected def MonadError.runEIO [Monad m]
[MonadError m] [MonadLiftT BaseIO m] [ToString ε] (x : EIO ε α) : m α := do
match (← x.toBaseIO) with
| Except.ok a => pure a
| Except.error e => error (toString e)
/--
Perform an IO action.
If it throws an error, invoke `error` with its string representation.
-/
@[inline] protected def MonadError.runIO
[Monad m] [MonadError m] [MonadLiftT BaseIO m] (x : IO α) : m α :=
MonadError.runEIO x