std:Transformer

lib/Transformer.omg

lib/Transformer.omg142 lines
1{-  Monad transformers: a reader or a state on top of any monad.
2
3    A transformer takes a monad `m` (a record of `std:Monad`) to a monad
4    with one more capability: `ReaderT r m` reads an environment of type
5    `r`, `StateT s m` reads and updates a state of type `s`, and both can
6    still run the actions of `m`, lifted.  Stacking them is how an EVM
7    transition is built (docs/evm.md, section 3.1):
8
9        ReaderT Env (StateT Cache Base)
10
11    so that running one in a test is choosing an environment, an initial
12    state and a base monad.
13
14    The transformers are universe polymorphic, within the limit of
15    `std:Monad`: a monad takes the types of one universe, `Type l`, to
16    types of the same universe, so the environment and the state live in
17    `Type l` too.
18-}
19
20let Monad = std:Monad.Monad
21
22let ReaderT
23    : forall {l : Level} -> Type l -> (Type l -> Type l) -> Type l -> Type l
24    = \r m a -> r -> m a
25
26let StateT
27    : forall {l : Level} -> Type l -> (Type l -> Type l) -> Type l -> Type l
28    = \s m a -> s -> m (a * s)
29
30-- The monad of a reader: every action is given the same environment.
31let readerT
32    : forall {l : Level} (r : Type l) {m : Type l -> Type l} -> Monad m -> Monad (ReaderT r m)
33    = \{l} r {m} monad ->
34        { pure = \{a} (x : a) (_ : r) -> monad.pure x
35        , bind =
36            \{a} {b} (x : ReaderT r m a) (f : a -> ReaderT r m b) (env : r) ->
37              monad.bind (x env) (\(y : a) -> f y env)
38        }
39
40-- The monad of a state: each action passes the state on to the next.
41let stateT
42    : forall {l : Level} (s : Type l) {m : Type l -> Type l} -> Monad m -> Monad (StateT s m)
43    = \{l} s {m} monad ->
44        { pure = \{a} (x : a) (state : s) -> monad.pure (x, state)
45        , bind =
46            \{a} {b} (x : StateT s m a) (f : a -> StateT s m b) (state : s) ->
47              monad.bind (x state) (\(step : a * s) -> f step.1 step.2)
48        }
49
50-- The reader's operations.
51
52let ask
53    : forall {l : Level} {r : Type l} {m : Type l -> Type l} -> Monad m -> ReaderT r m r
54    = \{l} {r} {m} monad env -> monad.pure env
55
56let asks
57    : forall {l : Level} {r : Type l} {m : Type l -> Type l}
58      -> Monad m
59      -> forall {a : Type l}
60      -> (r -> a)
61      -> ReaderT r m a
62    = \{l} {r} {m} monad {a} f env -> monad.pure (f env)
63
64-- Run an action in a changed environment.
65let local
66    : forall {l : Level} {r : Type l} {m : Type l -> Type l} {a : Type l}
67      -> (r -> r)
68      -> ReaderT r m a
69      -> ReaderT r m a
70    = \f x env -> x (f env)
71
72-- An action of the monad below, which ignores the environment.
73let liftR
74    : forall {l : Level} {r : Type l} {m : Type l -> Type l} {a : Type l} -> m a -> ReaderT r m a
75    = \x _ -> x
76
77-- Run a reader in an environment.
78let runReaderT
79    : forall {l : Level} {r : Type l} {m : Type l -> Type l} {a : Type l}
80      -> ReaderT r m a
81      -> r
82      -> m a
83    = \x env -> x env
84
85-- The state's operations.
86
87let get
88    : forall {l : Level} {s : Type l} {m : Type l -> Type l} -> Monad m -> StateT s m s
89    = \{l} {s} {m} monad state -> monad.pure (state, state)
90
91let gets
92    : forall {l : Level} {s : Type l} {m : Type l -> Type l}
93      -> Monad m
94      -> forall {a : Type l}
95      -> (s -> a)
96      -> StateT s m a
97    = \{l} {s} {m} monad {a} f state -> monad.pure (f state, state)
98
99let put
100    : forall {l : Level} {s : Type l} {m : Type l -> Type l} -> Monad m -> s -> StateT s m (unit @l)
101    = \{l} {s} {m} monad new _ -> monad.pure ({=}, new)
102
103let modify
104    : forall {l : Level} {s : Type l} {m : Type l -> Type l}
105      -> Monad m
106      -> (s -> s)
107      -> StateT s m (unit @l)
108    = \{l} {s} {m} monad f state -> monad.pure ({=}, f state)
109
110-- An action of the monad below, which leaves the state as it is.
111let liftS
112    : forall {l : Level} {s : Type l} {m : Type l -> Type l}
113      -> Monad m
114      -> forall {a : Type l}
115      -> m a
116      -> StateT s m a
117    = \{l} {s} {m} monad {a} x state -> monad.bind x (\(y : a) -> monad.pure (y, state))
118
119-- Run a state from an initial state: the result and the final state.
120let runStateT
121    : forall {l : Level} {s : Type l} {m : Type l -> Type l} {a : Type l}
122      -> StateT s m a
123      -> s
124      -> m (a * s)
125    = \x state -> x state
126
127in  { ReaderT
128    , StateT
129    , readerT
130    , stateT
131    , ask
132    , asks
133    , local
134    , liftR
135    , runReaderT
136    , get
137    , gets
138    , put
139    , modify
140    , liftS
141    , runStateT
142    }
Your result
Complete file: lib/Transformer.omg · with imports