{-# OPTIONS -fplugin=Rattus.Plugin #-}
{-# LANGUAGE TypeOperators #-}

-- | Programming with futures, i.e. data that will arrive at some time
-- in the future

module Rattus.Future
  ( map
  , never
  , switch
  , switchTrans
  , whenJust
  , Future(..)
  , await
  , trigger
  , triggerMap
  )

where

import Rattus
import Rattus.Stream hiding (map)

import Prelude hiding (map)


-- | A future may either be available now or later.
data Future a = Now !a | Wait !(O (Future a))

-- all functions in this module are in Rattus 
{-# ANN module Rattus #-}

-- | Apply a function to the value of the future (if it ever occurs).
{-# NOINLINE [1] map #-}
map :: Box (a -> b) -> Future a -> Future b
map :: Box (a -> b) -> Future a -> Future b
map Box (a -> b)
f (Now a
x) = b -> Future b
forall a. a -> Future a
Now (Box (a -> b) -> a -> b
forall a. Box a -> a
unbox Box (a -> b)
f a
x)
map Box (a -> b)
f (Wait O (Future a)
x) = O (Future b) -> Future b
forall a. O (Future a) -> Future a
Wait ((Future a -> Future b) -> O (Future a -> Future b)
forall a. a -> O a
delay (Box (a -> b) -> Future a -> Future b
forall a b. Box (a -> b) -> Future a -> Future b
map Box (a -> b)
f) O (Future a -> Future b) -> O (Future a) -> O (Future b)
forall a b. O (a -> b) -> O a -> O b
<#> O (Future a)
x)

-- | A future that will never happen.
never :: Future a
never :: Future a
never = O (Future a) -> Future a
forall a. O (Future a) -> Future a
Wait (Future a -> O (Future a)
forall a. a -> O a
delay Future a
forall a. Future a
never)


-- | @switch s e@ will behave like @s@ until the future @e@ is
-- available with value @s'@, in which case it will behave as @s'@.
switch :: Str a -> Future (Str a) -> Str a
switch :: Str a -> Future (Str a) -> Str a
switch (a
x ::: O (Str a)
xs) (Wait O (Future (Str a))
fas) = a
x a -> O (Str a) -> Str a
forall a. a -> O (Str a) -> Str a
::: ((Str a -> Future (Str a) -> Str a)
-> O (Str a -> Future (Str a) -> Str a)
forall a. a -> O a
delay Str a -> Future (Str a) -> Str a
forall a. Str a -> Future (Str a) -> Str a
switch O (Str a -> Future (Str a) -> Str a)
-> O (Str a) -> O (Future (Str a) -> Str a)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str a)
xs O (Future (Str a) -> Str a) -> O (Future (Str a)) -> O (Str a)
forall a b. O (a -> b) -> O a -> O b
<#> O (Future (Str a))
fas)
switch Str a
_xs        (Now Str a
ys)   = Str a
ys 

-- | Turn a stream of 'Maybe''s into a future. The future will occur
-- whenever the stream has a value of the form @Just' v@, and the
-- future then has value @v@.
firstJust :: Str (Maybe' a) -> Future a
firstJust :: Str (Maybe' a) -> Future a
firstJust (Just' a
x ::: O (Str (Maybe' a))
_) = a -> Future a
forall a. a -> Future a
Now a
x
firstJust (Maybe' a
Nothing' ::: O (Str (Maybe' a))
xs) = O (Future a) -> Future a
forall a. O (Future a) -> Future a
Wait ((Str (Maybe' a) -> Future a) -> O (Str (Maybe' a) -> Future a)
forall a. a -> O a
delay Str (Maybe' a) -> Future a
forall a. Str (Maybe' a) -> Future a
firstJust O (Str (Maybe' a) -> Future a)
-> O (Str (Maybe' a)) -> O (Future a)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str (Maybe' a))
xs)

-- | Turn a stream of 'Maybe''s into a stream of futures. Each such
-- future behaves as if created by 'firstJust'.
whenJust :: Str (Maybe' a) -> Str (Future a)
whenJust :: Str (Maybe' a) -> Str (Future a)
whenJust cur :: Str (Maybe' a)
cur@(Maybe' a
_ ::: O (Str (Maybe' a))
xs) = Str (Maybe' a) -> Future a
forall a. Str (Maybe' a) -> Future a
firstJust Str (Maybe' a)
cur Future a -> O (Str (Future a)) -> Str (Future a)
forall a. a -> O (Str a) -> Str a
::: ((Str (Maybe' a) -> Str (Future a))
-> O (Str (Maybe' a) -> Str (Future a))
forall a. a -> O a
delay Str (Maybe' a) -> Str (Future a)
forall a. Str (Maybe' a) -> Str (Future a)
whenJust O (Str (Maybe' a) -> Str (Future a))
-> O (Str (Maybe' a)) -> O (Str (Future a))
forall a b. O (a -> b) -> O a -> O b
<#> O (Str (Maybe' a))
xs)


-- | Like 'switch' but works on stream functions instead of
-- streams. That is, @switchTrans s e@ will behave like @s@ until the
-- future @e@ occurs with value @s'@, in which case it will behave as
-- @s'@.
switchTrans :: (Str a -> Str b) -> Future (Str a -> Str b) -> (Str a -> Str b)
switchTrans :: (Str a -> Str b) -> Future (Str a -> Str b) -> Str a -> Str b
switchTrans Str a -> Str b
f Future (Str a -> Str b)
es Str a
as = Str b -> Future (Str a -> Str b) -> Str a -> Str b
forall b a. Str b -> Future (Str a -> Str b) -> Str a -> Str b
switchTrans' (Str a -> Str b
f Str a
as) Future (Str a -> Str b)
es Str a
as

-- | Helper function for 'switchTrans'.
switchTrans' :: Str b -> Future (Str a -> Str b) -> Str a -> Str b
switchTrans' :: Str b -> Future (Str a -> Str b) -> Str a -> Str b
switchTrans' (b
x ::: O (Str b)
xs) (Wait O (Future (Str a -> Str b))
fas) (a
_:::O (Str a)
is) = b
x b -> O (Str b) -> Str b
forall a. a -> O (Str a) -> Str a
::: ((Str b -> Future (Str a -> Str b) -> Str a -> Str b)
-> O (Str b -> Future (Str a -> Str b) -> Str a -> Str b)
forall a. a -> O a
delay Str b -> Future (Str a -> Str b) -> Str a -> Str b
forall b a. Str b -> Future (Str a -> Str b) -> Str a -> Str b
switchTrans' O (Str b -> Future (Str a -> Str b) -> Str a -> Str b)
-> O (Str b) -> O (Future (Str a -> Str b) -> Str a -> Str b)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str b)
xs O (Future (Str a -> Str b) -> Str a -> Str b)
-> O (Future (Str a -> Str b)) -> O (Str a -> Str b)
forall a b. O (a -> b) -> O a -> O b
<#> O (Future (Str a -> Str b))
fas O (Str a -> Str b) -> O (Str a) -> O (Str b)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str a)
is)
switchTrans' Str b
_xs        (Now Str a -> Str b
ys)   Str a
is = Str a -> Str b
ys Str a
is

-- | Helper function for 'await'.
await1 :: Stable a => a -> Future b -> Future (a :* b)
await1 :: a -> Future b -> Future (a :* b)
await1 a
a (Wait O (Future b)
eb) = O (Future (a :* b)) -> Future (a :* b)
forall a. O (Future a) -> Future a
Wait ((a -> Future b -> Future (a :* b))
-> O (a -> Future b -> Future (a :* b))
forall a. a -> O a
delay a -> Future b -> Future (a :* b)
forall a b. Stable a => a -> Future b -> Future (a :* b)
await1 O (a -> Future b -> Future (a :* b))
-> a -> O (Future b -> Future (a :* b))
forall a b. Stable a => O (a -> b) -> a -> O b
<## a
a O (Future b -> Future (a :* b))
-> O (Future b) -> O (Future (a :* b))
forall a b. O (a -> b) -> O a -> O b
<#> O (Future b)
eb)
await1 a
a (Now  b
b)  = (a :* b) -> Future (a :* b)
forall a. a -> Future a
Now  (a
a a -> b -> a :* b
forall a b. a -> b -> a :* b
:* b
b)

-- | Helper function for 'await'.
await2 :: Stable b => b -> Future a -> Future (a :* b)
await2 :: b -> Future a -> Future (a :* b)
await2 b
b (Wait O (Future a)
ea) = O (Future (a :* b)) -> Future (a :* b)
forall a. O (Future a) -> Future a
Wait ((b -> Future a -> Future (a :* b))
-> O (b -> Future a -> Future (a :* b))
forall a. a -> O a
delay b -> Future a -> Future (a :* b)
forall b a. Stable b => b -> Future a -> Future (a :* b)
await2 O (b -> Future a -> Future (a :* b))
-> b -> O (Future a -> Future (a :* b))
forall a b. Stable a => O (a -> b) -> a -> O b
<## b
b O (Future a -> Future (a :* b))
-> O (Future a) -> O (Future (a :* b))
forall a b. O (a -> b) -> O a -> O b
<#> O (Future a)
ea)
await2 b
b (Now  a
a)  = (a :* b) -> Future (a :* b)
forall a. a -> Future a
Now  (a
a a -> b -> a :* b
forall a b. a -> b -> a :* b
:* b
b)

-- | Synchronise two futures. The resulting future occurs after both
-- futures have occurred (coinciding with whichever future occurred
-- last.
await :: (Stable a, Stable b) => Future a -> Future b -> Future(a :* b)
await :: Future a -> Future b -> Future (a :* b)
await (Wait O (Future a)
ea) (Wait O (Future b)
eb)  = O (Future (a :* b)) -> Future (a :* b)
forall a. O (Future a) -> Future a
Wait ((Future a -> Future b -> Future (a :* b))
-> O (Future a -> Future b -> Future (a :* b))
forall a. a -> O a
delay Future a -> Future b -> Future (a :* b)
forall a b.
(Stable a, Stable b) =>
Future a -> Future b -> Future (a :* b)
await O (Future a -> Future b -> Future (a :* b))
-> O (Future a) -> O (Future b -> Future (a :* b))
forall a b. O (a -> b) -> O a -> O b
<#> O (Future a)
ea O (Future b -> Future (a :* b))
-> O (Future b) -> O (Future (a :* b))
forall a b. O (a -> b) -> O a -> O b
<#> O (Future b)
eb)
await (Now a
a)   Future b
eb         = a -> Future b -> Future (a :* b)
forall a b. Stable a => a -> Future b -> Future (a :* b)
await1 a
a Future b
eb
await Future a
ea        (Now b
b)    = b -> Future a -> Future (a :* b)
forall b a. Stable b => b -> Future a -> Future (a :* b)
await2 b
b Future a
ea

-- | Trigger a future as soon as the given predicate turns true on the
-- given stream. The value of the future is the same as that of the
-- stream at that time.
trigger :: Box (a -> Bool) -> Str a -> Future a
trigger :: Box (a -> Bool) -> Str a -> Future a
trigger Box (a -> Bool)
p (a
x ::: O (Str a)
xs)
  | Box (a -> Bool) -> a -> Bool
forall a. Box a -> a
unbox Box (a -> Bool)
p a
x  = a -> Future a
forall a. a -> Future a
Now a
x
  | Bool
otherwise  = O (Future a) -> Future a
forall a. O (Future a) -> Future a
Wait ((Str a -> Future a) -> O (Str a -> Future a)
forall a. a -> O a
delay (Box (a -> Bool) -> Str a -> Future a
forall a. Box (a -> Bool) -> Str a -> Future a
trigger Box (a -> Bool)
p) O (Str a -> Future a) -> O (Str a) -> O (Future a)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str a)
xs)


-- | Trigger a future as soon as the given function produces a 'Just''
-- value.
triggerMap :: Box (a -> Maybe' b) -> Str a -> Future b
triggerMap :: Box (a -> Maybe' b) -> Str a -> Future b
triggerMap Box (a -> Maybe' b)
f (a
x ::: O (Str a)
xs) =
  case Box (a -> Maybe' b) -> a -> Maybe' b
forall a. Box a -> a
unbox Box (a -> Maybe' b)
f a
x of
    Just' b
y  -> b -> Future b
forall a. a -> Future a
Now b
y
    Maybe' b
Nothing' -> O (Future b) -> Future b
forall a. O (Future a) -> Future a
Wait ((Str a -> Future b) -> O (Str a -> Future b)
forall a. a -> O a
delay (Box (a -> Maybe' b) -> Str a -> Future b
forall a b. Box (a -> Maybe' b) -> Str a -> Future b
triggerMap Box (a -> Maybe' b)
f) O (Str a -> Future b) -> O (Str a) -> O (Future b)
forall a b. O (a -> b) -> O a -> O b
<#> O (Str a)
xs)

{-# RULES

  "map/map" forall f g xs.
    map f (map g xs) = map (box (unbox f . unbox g)) xs ;

#-}