std:Result
lib/Result.omg
lib/Result.omg102 lines
1{- Results: success or failure.
2
3 `Result e a` is an ordinary union type, defined here rather than built
4 in. A value is either `(Result e a).Err` applied to an error or
5 `(Result e a).Ok` applied to a result; `merge` takes it apart. The
6 errors and the results can be of any universe, each its own.
7
8 `monad e` is the monad of the computations that may fail with an error
9 of type `e`, for a `do` block that stops at the first error:
10
11 let Result = std:Result
12 let parse = \(t : Text) -> Result.fromOptional "not a number: ${t}" (Natural/parse t)
13 in do (Result.monad Text) { x <- parse "1"; y <- parse "2"; Result.ok (x + y) }
14
15 A monad has a single universe, though: `monad e` is the one for results
16 in the universe of `e` (`andThen` itself joins results of any universes).
17-}
18
19let Result
20 : forall {le la : Level} -> Type le -> Type la -> Type (Level/max le la)
21 = \e a -> < Err : e | Ok : a >
22
23let ok
24 : forall {le la : Level} {e : Type le} {a : Type la} -> a -> Result e a
25 = \{le} {la} {e} {a} x -> (Result e a).Ok x
26
27let err
28 : forall {le la : Level} {e : Type le} {a : Type la} -> e -> Result e a
29 = \{le} {la} {e} {a} x -> (Result e a).Err x
30
31-- fold onErr onOk result
32let fold
33 : forall {le la lb : Level} {e : Type le} {a : Type la} {b : Type lb}
34 -> (e -> b)
35 -> (a -> b)
36 -> Result e a
37 -> b
38 = \onErr onOk result -> merge { Err = onErr, Ok = onOk } result
39
40let map
41 : forall {le la lb : Level} {e : Type le} {a : Type la} {b : Type lb}
42 -> (a -> b)
43 -> Result e a
44 -> Result e b
45 = \{le} {la} {lb} {e} {a} {b} f -> fold (\(x : e) -> err x) (\(x : a) -> ok (f x))
46
47let mapErr
48 : forall {le lf la : Level} {e : Type le} {f : Type lf} {a : Type la}
49 -> (e -> f)
50 -> Result e a
51 -> Result f a
52 = \{le} {lf} {la} {e} {f} {a} g -> fold (\(x : e) -> err (g x)) (\(x : a) -> ok x)
53
54let andThen
55 : forall {le la lb : Level} {e : Type le} {a : Type la} {b : Type lb}
56 -> Result e a
57 -> (a -> Result e b)
58 -> Result e b
59 = \{le} {la} {lb} {e} {a} {b} result f -> fold (\(x : e) -> err x) f result
60
61let withDefault
62 : forall {le la : Level} {e : Type le} {a : Type la} -> a -> Result e a -> a
63 = \{le} {la} {e} {a} fallback -> fold (\(_ : e) -> fallback) (\(x : a) -> x)
64
65let toOptional
66 : forall {le la : Level} {e : Type le} {a : Type la} -> Result e a -> Optional a
67 = \{le} {la} {e} {a} -> fold (\(_ : e) -> None a) (\(x : a) -> Some x)
68
69let isOk
70 : forall {le la : Level} {e : Type le} {a : Type la} -> Result e a -> Bool
71 = \{le} {la} {e} {a} -> fold (\(_ : e) -> False) (\(_ : a) -> True)
72
73let isErr
74 : forall {le la : Level} {e : Type le} {a : Type la} -> Result e a -> Bool
75 = \{le} {la} {e} {a} -> fold (\(_ : e) -> True) (\(_ : a) -> False)
76
77-- The value of an option, or the given error if there is none.
78let fromOptional
79 : forall {le la : Level} {e : Type le} {a : Type la} -> e -> Optional a -> Result e a
80 = \{le} {la} {e} {a} error ->
81 Optional/elim (\(_ : Optional a) -> Result e a) (err error) (\(x : a) -> ok x)
82
83-- The functions `ok` and `andThen` above, as a monad, for results in the
84-- universe of the errors.
85let monad
86 : forall {l : Level} (e : Type l) -> std:Monad.Monad @l (Result e)
87 = \e -> { pure = ok, bind = andThen }
88
89in { Result
90 , ok
91 , err
92 , fold
93 , map
94 , mapErr
95 , andThen
96 , withDefault
97 , toOptional
98 , isOk
99 , isErr
100 , fromOptional
101 , monad
102 }