-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReflection.hs
255 lines (248 loc) · 8.06 KB
/
Reflection.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Reflection where
import Capability.Error
import Capability.Reader
import Capability.Reflection
import Capability.Sink
import Capability.Source
import Capability.State
import Capability.Writer
import Control.Monad (forM_, replicateM)
import qualified Control.Monad.Except as MTL
import qualified Control.Monad.State as MTL
import Data.DList (DList)
import qualified Data.DList as DList
import Data.Monoid (Product (..))
import Test.Hspec
----------------------------------------------------------------------
-- Instances
newtype Logger s a = Logger (MTL.State (DList String, s) a)
deriving (Functor, Applicative, Monad)
deriving
(HasSink "log" String)
via SinkDList (SinkLog (Rename 1 (Pos 1 () (MonadState (MTL.State (DList String, s))))))
deriving
(HasState "test" s, HasSource "test" s, HasSink "test" s)
via Rename 2 (Pos 2 () (MonadState (MTL.State (DList String, s))))
runLogger :: s -> Logger s a -> (a, s, [String])
runLogger s0 (Logger m) =
case MTL.runState m (mempty, s0) of
(a, (log', s)) -> (a, s, DList.toList log')
----------------------------------------------------------------------
-- Test Cases
spec :: Spec
spec = do
describe "interpret_" $ do
it "evaluates HasSource" $ do
runLogger
[1, 2, 3 :: Int]
( interpret_ @"source"
ReifiedSource
{ _await = do
yield @"log" "await"
state @"test" $ \case
(x : xs) -> (x, xs)
[] -> error "empty stream"
}
(replicateM 3 $ await @"source")
)
`shouldBe` ([1, 2, 3], [], ["await", "await", "await"])
it "evaluates HasSink" $ do
runLogger
[]
( interpret_ @"sink"
ReifiedSink
{ _yield = \x -> do
yield @"log" "yield"
modify @"test" (x :)
}
(forM_ [1, 2, 3 :: Int] $ yield @"sink")
)
`shouldBe` ((), [3, 2, 1], ["yield", "yield", "yield"])
it "evaluates HasReader" $ do
runLogger
(42 :: Int)
( interpret_ @"reader"
ReifiedReader
{ _readerSource =
ReifiedSource
{ _await = do
yield @"log" "ask"
get @"test"
},
_local = \f m -> do
yield @"log" "local"
r <- state @"test" $ \r -> (r, f r)
a <- m
put @"test" r
pure a,
_reader = \f -> do
yield @"log" "reader"
f <$> get @"test"
}
( sequence
[ ask @"reader",
local @"reader" succ $ do
asks @"reader" succ,
reader @"reader" pred
]
)
)
`shouldBe` ([42, 44, 41], 42, ["ask", "local", "ask", "reader"])
it "evaluates HasState" $ do
runLogger
(42 :: Int)
( interpret_ @"state"
ReifiedState
{ _stateSource =
ReifiedSource
{ _await = do
yield @"log" "get"
get @"test"
},
_stateSink =
ReifiedSink
{ _yield = \x -> do
yield @"log" "put"
put @"test" x
},
_state = \f -> do
yield @"log" "state"
state @"test" f
}
( do
old <- get @"state"
put @"state" 0
state @"state" (\new -> (old, succ new))
)
)
`shouldBe` (42, 1, ["get", "put", "state"])
it "evaluates HasWriter" $ do
runLogger
(mempty :: Product Int)
( interpret_ @"writer"
ReifiedWriter
{ _writerSink =
ReifiedSink
{ _yield = \w -> do
yield @"log" "tell"
modify @"test" (<> w)
},
_writer = \(a, w) -> do
yield @"log" "writer"
modify @"test" (<> w)
pure a,
_listen = \m -> do
yield @"log" "listen"
w0 <- state @"test" $ \s -> (s, mempty)
a <- m
w <- state @"test" $ \s -> (s, w0 <> s)
pure (a, w),
_pass = \m -> do
yield @"log" "pass"
w0 <- state @"test" $ \s -> (s, mempty)
(a, f) <- m
modify' @"test" $ \s -> w0 <> f s
pure a
}
( do
tell @"writer" 2
writer @"writer" ((), 3)
((), w) <- listen @"writer" $ do
tell @"writer" 5
pass @"writer" $ do
tell @"writer" 6
pure ((), (+ 1))
pure w
)
)
`shouldBe` (5, 2 * 3 * 5 * 7, ["tell", "writer", "listen", "tell", "pass", "tell"])
it "evaluates HasThrow" $ do
runLogger
()
( MTL.runExceptT $
interpret_ @"throw"
ReifiedThrow
{ _throw = \e -> do
MTL.lift $ yield @"log" "throw"
MTL.throwError e
}
( do
_ <- throw @"throw" "some error"
pure $! tail ""
)
)
`shouldBe` (Left "some error", (), ["throw"])
it "evaluates HasCatch" $ do
runLogger
()
( MTL.runExceptT $
interpret_ @"catch"
ReifiedCatch
{ _catchThrow =
ReifiedThrow
{ _throw = \e -> do
MTL.lift $ yield @"log" "throw"
MTL.throwError e
},
_catch = \m h -> do
MTL.lift $ yield @"log" "catch"
MTL.catchError m h,
_catchJust = \f m h -> do
MTL.lift $ yield @"log" "catchJust"
MTL.catchError m $ \e ->
case f e of
Just b -> h b
Nothing -> MTL.throwError e
}
( do
e1 <-
catch @"catch"
( do
_ <- throw @"catch" "some error"
pure $! tail ""
)
pure
catchJust @"catch"
(const Nothing)
( do
_ <- throw @"catch" ("again " ++ e1)
pure $! tail ""
)
pure
)
)
`shouldBe` (Left "again some error", (), ["catch", "throw", "catchJust", "throw"])
describe "interpret" $ do
it "can be nested" $ do
runLogger
()
( interpret @"source" @'[HasSink "log" String]
ReifiedSource
{ _await = do
yield @"log" "await"
pure ()
}
( interpret @"sink" @'[HasSink "log" String, HasSource "source" ()]
ReifiedSink
{ _yield = \() -> do
yield @"log" "yield"
pure ()
}
( do
yield @"log" "begin"
await @"source"
yield @"sink" ()
yield @"log" "end"
)
)
)
`shouldBe` ((), (), ["begin", "await", "yield", "end"])