/
StateFields.hs
180 lines (146 loc) · 4.9 KB
/
StateFields.hs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
module Data.Record.StateFields
( IdField(..)
, StateField(..)
, idStateField
, StateTField(..)
, idStateTField
, ReaderField(..)
, idReaderField
, FieldPath(..)
, FieldState(..)
, modf
, enter
, enterT
, FieldReader(..)
, proj
, projT
, record
) where
import Data.Record.StateFields.Core
import Data.Record.StateFields.Templates
import Control.Monad.Reader
import Control.Monad.State
-- | A field descriptor with stateful effects.
data StateField a b = StateField
{ getFieldM :: (MonadState a m) => m b
, putFieldM :: (MonadState a m) => b -> m ()
}
-- | Make a StateField from an IdField.
idStateField f =
StateField
{ getFieldM = gets $ getField f
, putFieldM = \b -> get >>= \a -> put $ putField f b a
}
-- | A field descriptor with stateful and other effects.
data StateTField m a b = StateTField
{ getFieldT :: StateT a m b
, putFieldT :: b -> StateT a m ()
}
-- | Make a StateTField from an IdField.
idStateTField f =
StateTField
{ getFieldT = gets $ getField f
, putFieldT = \b -> get >>= \a -> put $ putField f b a
}
-- | A field descriptor which operates in an environment.
data ReaderField a b = ReaderField
{ askFieldM :: (MonadReader a m) => m b
}
-- | Make a ReaderField from an IdField.
idReaderField f =
ReaderField
{ askFieldM = asks $ getField f
}
infixl 9 //
-- | The class of field descriptors which can be chained into paths.
class FieldPath f g h | f g -> h where
-- | Join two field descriptors into a compound.
-- '//' is left-associative with precedence level 9.
(//) :: f -> g -> h
instance FieldPath (IdField a b) (IdField b c) (IdField a c) where
ab // bc =
IdField
{ getField = getField bc . getField ab
, putField = \c a -> putField ab (putField bc c $ getField ab a) a
}
instance FieldPath (IdField a b) (StateField b c) (StateField a c) where
ab // bc = idStateField ab // bc
instance FieldPath (StateField a b) (IdField b c) (StateField a c) where
ab // bc = ab // idStateField bc
instance (Monad m) => FieldPath (IdField a b) (StateTField m b c) (StateTField m a c) where
ab // bc = (idStateTField ab :: StateTField m a b) // bc
instance (Monad m) => FieldPath (StateTField m a b) (IdField b c) (StateTField m a c) where
ab // bc = ab // (idStateTField bc :: StateTField m b c)
instance FieldPath (IdField a b) (ReaderField b c) (ReaderField a c) where
ab // bc = idReaderField ab // bc
instance FieldPath (ReaderField a b) (IdField b c) (ReaderField a c) where
ab // bc = ab // idReaderField bc
instance FieldPath (StateField a b) (StateField b c) (StateField a c) where
ab // bc =
StateField
{ getFieldM = enter ab $ getFieldM bc
, putFieldM = enter ab . putFieldM bc
}
instance (Monad m) =>
FieldPath (StateTField m a b) (StateTField m b c) (StateTField m a c) where
ab // bc =
StateTField
{ getFieldT = enterT ab $ getFieldT bc
, putFieldT = enterT ab . putFieldT bc
}
instance FieldPath (ReaderField a b) (ReaderField b c) (ReaderField a c) where
ab // bc =
ReaderField
{ askFieldM = proj ab $ askFieldM bc
}
-- | The class of field descriptors usable with a State monad.
class (MonadState a m) => FieldState f a m | f -> a where
getf :: f b -> m b
putf :: f b -> b -> m ()
instance (MonadState a m) => FieldState (IdField a) a m where
getf = gets . getField
putf f b = get >>= put . putField f b
instance (MonadState a m) => FieldState (StateField a) a m where
getf = getFieldM
putf = putFieldM
instance (Monad m) => FieldState (StateTField m a) a (StateT a m) where
getf = getFieldT
putf = putFieldT
-- | Modify the value of a field by applying a function.
modf :: (FieldState f a m, MonadState a m) => f b -> (b -> b) -> m ()
modf f g = getf f >>= putf f . g
-- | Run a stateful computation with a field as the state.
enter :: (FieldState f a m, MonadState a m) => f b -> State b x -> m x
enter f m = do
b <- getf f
let (x, b') = runState m b
putf f b'
return x
-- | Like 'enter', but allows the subcomputation to share the underlying
-- monad of the enclosing computation.
enterT :: (Monad m, FieldState f a (StateT a m)) => f b -> StateT b m x -> StateT a m x
enterT f m = do
b <- getf f
(x, b') <- lift $ runStateT m b
putf f b'
return x
-- | The class of field descriptors usable with a Reader monad.
class FieldReader f where
askf :: (MonadReader a m) => f a b -> m b
instance FieldReader IdField where
askf = asks . getField
instance FieldReader ReaderField where
askf = askFieldM
-- | Run a computation with a field as the environment.
proj :: (FieldReader f, MonadReader a m) => f a b -> Reader b x -> m x
proj f m = do
b <- askf f
let x = runReader m b
return x
-- | Like 'proj', but allows the subcomputation to share the underlying
-- monad of the enclosing computation.
projT :: (Monad m, FieldReader f) => f a b -> ReaderT b m x -> ReaderT a m x
projT f m = do
b <- askf f
x <- lift $ runReaderT m b
return x