std:Monad
lib/Monad.omg
lib/Monad.omg127 lines
1{- Monads.
2
3 There are no type classes: a monad is a record of its operations, passed
4 explicitly. A `do` block names the record to use:
5
6 let Monad = std:Monad
7 in do Monad.optional { x <- Natural/parse "1"; y <- Natural/parse "2"; Monad.optional.pure (x + y) }
8
9 and a `do` block without a record is in the builtin `IO` monad.
10
11 A monad `m` takes the types of a universe, `Type l`, to types of the same
12 universe. `Optional`, `List` and `IO` are monads at every level, and so
13 are their records here; `forEach` and `when` produce `unit @l`, the empty
14 record type of that universe. The record of a monad on `Type 0` is a
15 `Type` itself (`Type 0` is impredicative); above it, the record is one
16 universe up: `Monad m` is a `Type (Level/imax (l + 1) l)`.
17
18 So a type constructor whose results live in a higher universe than its
19 arguments has a `Functor` or `Monad` record only from the universe where
20 it stops raising them: `std:Map.Map Type` and `std:Result.Result Type`
21 take a type of `Type` to one of `Type 1`, and keep `Type 1` and the
22 universes above, so `Functor @1 (std:Map.Map Type)` is a type and
23 `std:Result.monad Type` a monad. The functions of their modules
24 (`std:Map.map`, `std:Result.andThen`, ...) work at every level without
25 one.
26-}
27
28let Functor =
29 \{l : Level} (f : Type l -> Type l) ->
30 { map : forall {a : Type l} {b : Type l} -> (a -> b) -> f a -> f b }
31
32let Monad =
33 \{l : Level} (m : Type l -> Type l) ->
34 { pure : forall {a : Type l} -> a -> m a
35 , bind : forall {a : Type l} {b : Type l} -> m a -> (a -> m b) -> m b
36 }
37
38let map
39 : forall {l : Level} {m : Type l -> Type l}
40 -> Monad m
41 -> forall {a : Type l} {b : Type l}
42 -> (a -> b)
43 -> m a
44 -> m b
45 = \{l} {m} monad {a} {b} f x -> monad.bind x (\(y : a) -> monad.pure (f y))
46
47let functor
48 : forall {l : Level} {m : Type l -> Type l} -> Monad m -> Functor m
49 = \{l} {m} monad -> { map = \{a} {b} -> map monad @a @b }
50
51let join
52 : forall {l : Level} {m : Type l -> Type l} -> Monad m -> forall {a : Type l} -> m (m a) -> m a
53 = \{l} {m} monad {a} x -> monad.bind x (\(y : m a) -> y)
54
55-- Run two computations in sequence, keeping the result of the second.
56let andThen
57 : forall {l : Level} {m : Type l -> Type l}
58 -> Monad m
59 -> forall {a : Type l} {b : Type l}
60 -> m a
61 -> m b
62 -> m b
63 = \{l} {m} monad {a} {b} x y -> monad.bind x (\(_ : a) -> y)
64
65-- Run a computation for each element of a list and collect the results. The
66-- elements can be of any universe.
67let traverse
68 : forall {l : Level} {m : Type l -> Type l}
69 -> Monad m
70 -> forall {la : Level} {a : Type la} {b : Type l}
71 -> (a -> m b)
72 -> List a
73 -> m (List b)
74 = \{l} {m} monad {la} {a} {b} f ->
75 List/elim
76 (\(_ : List a) -> m (List b))
77 (monad.pure ([] : List b))
78 (\(x : a) (_ : List a) (rest : m (List b)) ->
79 monad.bind
80 (f x)
81 (\(y : b) -> monad.bind rest (\(ys : List b) -> monad.pure ([ y ] # ys))))
82
83-- Run a computation for each element of a list, for its effects only.
84let forEach
85 : forall {l : Level} {m : Type l -> Type l}
86 -> Monad m
87 -> forall {la : Level} {a : Type la}
88 -> List a
89 -> (a -> m (unit @l))
90 -> m (unit @l)
91 = \{l} {m} monad {la} {a} xs f ->
92 List/elim
93 (\(_ : List a) -> m (unit @l))
94 (monad.pure {=})
95 (\(x : a) (_ : List a) (rest : m (unit @l)) -> monad.bind (f x) (\(_ : unit @l) -> rest))
96 xs
97
98-- Run a computation only if a condition holds.
99let when
100 : forall {l : Level} {m : Type l -> Type l} -> Monad m -> Bool -> m (unit @l) -> m (unit @l)
101 = \{l} {m} monad condition action -> if condition then action else monad.pure {=}
102
103-- The monads of the builtin types, at every level.
104
105let optional
106 : forall {l : Level} -> Monad @l Optional
107 = { pure = \{a} (x : a) -> Some x
108 , bind =
109 \{a} {b} (o : Optional a) (f : a -> Optional b) ->
110 Optional/elim (\(_ : Optional a) -> Optional b) (None b) f o
111 }
112
113let list
114 : forall {l : Level} -> Monad @l List
115 = { pure = \{a} (x : a) -> [ x ]
116 , bind =
117 \{a} {b} (xs : List a) (f : a -> List b) ->
118 List/elim
119 (\(_ : List a) -> List b)
120 ([] : List b)
121 (\(x : a) (_ : List a) (rest : List b) -> f x # rest)
122 xs
123 }
124
125let io : forall {l : Level} -> Monad @l IO = { pure = IO/pure, bind = IO/bind }
126
127in { Functor, Monad, map, functor, join, andThen, traverse, forEach, when, optional, list, io }