-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRP.idr
384 lines (332 loc) · 11.6 KB
/
FRP.idr
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
module FRP
import Debug.Trace
import Document
import Animation
import Element
import Event
%default total
%include JavaScript "ioref.js"
-----------
-- IORef --
-----------
data IORef : Type -> Type where
MkIORef : Ptr -> IORef a
newIORef : a -> IO (IORef a)
newIORef x = MkIORef `map`
mkForeign (FFun "newIORefRaw(%0)" [FAny a] FPtr) x
newIORef' : a -> IORef a
newIORef' x = unsafePerformIO $ newIORef x
readIORef : IORef a -> IO a
readIORef {a} (MkIORef ref) =
mkForeign (FFun "readIORefRaw(%0)" [FPtr] (FAny a)) ref
writeIORef : IORef a -> a -> IO ()
writeIORef {a} (MkIORef ref) val =
mkForeign (FFun "writeIORefRaw(%0, %1)" [FPtr, FAny a] FUnit) ref val
modifyIORef : IORef a -> (a -> a) -> IO ()
modifyIORef ref f = readIORef ref >>= writeIORef ref . f
nullJS : a
nullJS {a} = unsafePerformIO $
mkForeign (FFun "null" [] (FAny a))
---------------
-- Idris API --
---------------
data Signal : Type -> Type where
MkSig : Bool -> -- | Connected to world.
-- (addEventListener or
-- setInterval has been called on
-- this signal's recv, or one of its
-- ancestor's recv.)
IORef a -> -- | Time-varying value.
IO () -> -- | Initialize value.
IORef (IO ()) -> -- | Recieve Event.
Signal a
getSigVal : Signal a -> IO a
getSigVal (MkSig _ ref _ _) = readIORef ref
getSigRef : Signal a -> IORef a
getSigRef (MkSig _ ref _ _) = ref
getSigRecv : Signal a -> IORef (IO ())
getSigRecv (MkSig _ _ _ recv) = recv
getSigInit : Signal a -> IO ()
getSigInit (MkSig _ _ initS _) = initS
sigConnected : Signal a -> Bool
sigConnected (MkSig con _ _ _) = con
sigInit : Signal a -> IO ()
sigInit (MkSig _ _ sinit _) = sinit
addChildRecv : Signal a -> IORef (IO ()) -> IO ()
addChildRecv (MkSig _ _ _ parentRecv) childRecv =
modifyIORef parentRecv ($> flatten (readIORef childRecv))
mkSig' : Foldable t => t (Signal b) -> (IORef a -> IO ()) -> Signal a
mkSig' xs action =
let this = newIORef' nullJS
thisRecv = newIORef' $ action this
thisInit = do
traverse_ sigInit xs
traverse_ (flip addChildRecv thisRecv) xs
in MkSig (any sigConnected xs) this thisInit thisRecv
mkSig : IO a -> Signal b -> (IORef a -> IO ()) -> Signal a
mkSig initS x action =
let this = newIORef' nullJS
thisRecv = newIORef' $ action this
thisInit = do
sigInit x
initS >>= writeIORef this
addChildRecv x thisRecv
in MkSig (sigConnected x) this thisInit thisRecv
mkSig2 : IO a -> Signal b -> Signal c -> (IORef a -> IO ()) -> Signal a
mkSig2 initS x y action =
let this = newIORef' nullJS
thisRecv = newIORef' $ action this
thisInit = do
sigInit x $> sigInit y
initS >>= writeIORef this
addChildRecv x thisRecv
addChildRecv y thisRecv
connected = sigConnected x || sigConnected y
in MkSig connected this thisInit thisRecv
mkSig3 : IO a -> Signal b -> Signal c -> Signal d ->
(IORef a -> IO ()) -> Signal a
mkSig3 initS x y z action =
let this = newIORef' nullJS
thisRecv = newIORef' $ action this
thisInit = do
sigInit x $> sigInit y $> sigInit z
initS >>= writeIORef this
addChildRecv x thisRecv
addChildRecv y thisRecv
addChildRecv z thisRecv
connected = sigConnected x || sigConnected y || sigConnected z
in MkSig connected this thisInit thisRecv
mkSig4 : IO a -> Signal b -> Signal c -> Signal d -> Signal e ->
(IORef a -> IO ()) -> Signal a
mkSig4 initS x y z w action =
let this = newIORef' nullJS
thisRecv = newIORef' $ action this
thisInit = do
sigInit x $> sigInit y $> sigInit z $> sigInit w
initS >>= writeIORef this
addChildRecv x thisRecv
addChildRecv y thisRecv
addChildRecv z thisRecv
addChildRecv w thisRecv
connected = sigConnected x || sigConnected y ||
sigConnected z || sigConnected w
in MkSig connected this thisInit thisRecv
-- Technically not Semigroup, l <+> r /= r <+> l
instance Semigroup (Signal a) where
(<+>) sig1 sig2 =
mkSig2 (return nullJS) sig1 sig2 $ \this =>
getSigVal sig1 >>= writeIORef this
instance Monoid a => Monoid (Signal a) where
neutral =
MkSig False (newIORef' neutral) (return ())
(newIORef' $ return ())
instance Functor Signal where
map f sig =
let initVal = f `map` getSigVal sig
in mkSig initVal sig $ \this => do
val <- getSigVal sig
writeIORef this (f val)
instance Applicative Signal where
pure x =
let recv = newIORef' $ return ()
in MkSig False (newIORef' x)
(return ())
recv
(<$>) funcSig valSig =
let initS = do
func <- getSigVal funcSig
val <- getSigVal valSig
return $ func val
in mkSig2 initS funcSig valSig $ \this => do
func <- getSigVal funcSig
val <- getSigVal valSig
writeIORef this (func val)
instance Monad Signal where
(>>=) (MkSig connected valRef valInit valRecv) func =
mkSig nullJS (MkSig connected valRef valInit valRecv)
$ \this => do
val <- readIORef valRef
let MkSig newCon newRef newInit newRecv = func val
modifyIORef valRecv ($> flatten (readIORef newRecv))
newVal <- readIORef newRef
writeIORef this newVal
fps : Int -> Signal Float
fps desiredFPS =
let lastFrame = newIORef' 0.0
msPerFrame = 1000.0 / cast desiredFPS
delta = newIORef' 0.0
thisRecv = newIORef' $ do
lastTime <- readIORef lastFrame
curTime <- getTime
writeIORef delta (curTime - lastTime)
writeIORef lastFrame curTime
thisInit = do
getTime >>= writeIORef lastFrame
setInterval (flatten $ readIORef thisRecv) msPerFrame
return ()
in MkSig True delta thisInit thisRecv
where
getTime : IO Float
getTime = mkForeign (FFun "(new Date()).getTime()" [] FFloat)
infixl 4 <~
(<~) : (a -> b) -> Signal a -> Signal b
(<~) = map
infixl 4 ~~
(~~) : Signal (a -> b) -> Signal a -> Signal b
(~~) = (<$>)
lift : (a -> b) -> Signal a -> Signal b
lift = map
lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
lift2 f sig1 sig2 =
let initS = do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
return $ f val1 val2
in mkSig2 initS sig1 sig2 $ \this => do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
writeIORef this (f val1 val2)
lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
lift3 f sig1 sig2 sig3 =
let initS = do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
val3 <- getSigVal sig3
return $ f val1 val2 val3
in mkSig3 initS sig1 sig2 sig3 $ \this => do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
val3 <- getSigVal sig3
writeIORef this (f val1 val2 val3)
lift4 : (a -> b -> c -> d -> e) ->
Signal a -> Signal b -> Signal c -> Signal d -> Signal e
lift4 f sig1 sig2 sig3 sig4 =
let initS = do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
val3 <- getSigVal sig3
val4 <- getSigVal sig4
return $ f val1 val2 val3 val4
in mkSig4 initS sig1 sig2 sig3 sig4 $ \this => do
val1 <- getSigVal sig1
val2 <- getSigVal sig2
val3 <- getSigVal sig3
val4 <- getSigVal sig4
writeIORef this (f val1 val2 val3 val4)
instance Num a => Num (Signal a) where
(+) = lift2 (+)
(-) = lift2 (-)
(*) = lift2 (*)
abs = lift abs
fromInteger = pure . fromInteger
instance Integral a => Integral (Signal a) where
div = lift2 div
mod = lift2 mod
effectful : IO a -> Signal a
effectful action =
let this = newIORef' nullJS
thisRecv = action >>= writeIORef this
in MkSig False this thisRecv (newIORef' thisRecv)
merge : Signal a -> Signal a -> Signal a
merge = (<+>)
merges : Vect (S n) (Signal a) -> Signal a
merges = foldr1 merge
combine : List (Signal a) -> Signal (List a)
combine = foldr (lift2 (::)) (pure [])
count : Num b => Signal a -> Signal b
count from =
mkSig (return 0) from $ \this => do
curVal <- readIORef this
writeIORef this (curVal+1)
liftM : (a -> IO b) -> Signal a -> Signal b
liftM f from =
let initS = getSigVal from >>= f
in mkSig initS from $ \this => do
val <- getSigVal from
f val >>= writeIORef this
foldp : (a -> b -> b) -> b -> Signal a -> Signal b
foldp func ival from =
mkSig (return ival) from $ \this => do
aVal <- getSigVal from
currentVal <- readIORef this
writeIORef this (func aVal currentVal)
foldpM : (a -> b -> IO b) -> b -> Signal a -> Signal b
foldpM func ival from =
mkSig (return ival) from $ \this => do
aVal <- getSigVal from
currentVal <- readIORef this
func aVal currentVal >>= writeIORef this
mousePosition : Signal (Int, Int)
mousePosition =
let this = newIORef' (0,0)
recv = newIORef' $ return ()
sigInit = do
doc <- documentElement
addEventListener MouseMove doc $ \ev => do
eventMousePos ev >>= writeIORef this
flatten $ readIORef recv
in MkSig True this sigInit recv
keyDown : Signal Char
keyDown =
let this = newIORef' $ '\0'
recv = newIORef' $ return ()
sigInit = do
doc <- documentElement
addEventListener KeyDown doc $ \ev => do
eventKeycode ev >>= writeIORef this
flatten $ readIORef recv
in MkSig True this sigInit recv
keyPress : Signal Char
keyPress =
let this = newIORef' $ '\0'
recv = newIORef' $ return ()
sigInit = do
doc <- documentElement
addEventListener KeyPress doc $ \ev => do
eventKeycode ev >>= writeIORef this
flatten $ readIORef recv
in MkSig True this sigInit recv
keyUp : Signal Char
keyUp =
let this = newIORef' $ '\0'
recv = newIORef' $ return ()
sigInit = do
doc <- documentElement
addEventListener KeyUp doc $ \ev => do
eventKeycode ev >>= writeIORef this
flatten $ readIORef recv
in MkSig True this sigInit recv
isDown : Char -> Signal Bool
isDown myKey =
let this = newIORef' False
recv = newIORef' $ return ()
sigInit = do
doc <- documentElement
addEventListener KeyPress doc $ \ev => do
key <- eventKeycode ev
if key == myKey
then do
writeIORef this True
flatten $ readIORef recv
else writeIORef this False
in MkSig True this sigInit recv
start : Show a => Signal a -> IO ()
start (MkSig connected ref sigInit recv) = do
sigInit
when (not connected) $ do
setInterval (flatten $ readIORef recv) 16
return ()
secondSig : Signal Int
secondSig = foldpM
(\a, b => putStrLn ("secondSig: " ++ show b) $> return (b+1))
0 (pure 3)
mySig : Signal Int
mySig = foldpM func 0 (isDown 'a') <+>
foldpM (\a,b=>print a$>return b) 0 (fps 1)
where
func : Bool -> Int -> IO Int
func a b = do
print "Pressed 'a'"
return b
sigTest : IO ()
sigTest = start mySig