Diagrams
This commit is contained in:
1 parent
022342af32
commit
617bd255ff
10 files changed
+124
-71
No files matched your search
+24
-22
@@ -5,7 +5,7 @@ module AFRP
|
||||
, Auto(..)
|
||||
, DecodedAuto(..)
|
||||
, eff
|
||||
, withEntities
|
||||
, named
|
||||
, Event(..)
|
||||
, hold
|
||||
, holdKnown
|
||||
@@ -67,6 +67,8 @@ import qualified Data.Conduit.Cereal as CC
|
||||
import qualified Conduit as C
|
||||
import Control.Monad.Catch (MonadCatch, handle)
|
||||
import Control.Exception.Annotated (throw)
|
||||
import AFRP.Diagram
|
||||
import Data.Text (Text)
|
||||
|
||||
|
||||
data Codec s = Codec { getter :: !(Get s), putter :: !(Putter s) }
|
||||
@@ -257,54 +259,54 @@ load path a = handle defaultOnMissingFile (flip deserialize a <$> liftIO (B.read
|
||||
| isDoesNotExistError e = pure $ FailDecode "State doesn't exist yet" a
|
||||
| otherwise = throw e
|
||||
|
||||
|
||||
-- | The set of entity ids an arrow subscribes to. Static: it does not
|
||||
-- change as the machine steps, so the runtime can read it once to build
|
||||
-- trigger subscriptions.
|
||||
data Mealy eff a b = Mealy
|
||||
{ entities :: S.Set T.Text
|
||||
, runDiagramC :: forall x. (DiagramF x -> x) -> x
|
||||
, runMealy :: forall m. Monad m => (forall x. eff x -> m x) -> Auto m a b
|
||||
}
|
||||
|
||||
-- Provide a name
|
||||
named :: Text -> Mealy eff a b -> Mealy eff a b
|
||||
named n f = f{runDiagramC = \run -> run (Step n (emptyDiagram run))}
|
||||
|
||||
instance Semigroup b => Semigroup (Mealy eff a b) where
|
||||
Mealy ast af <> Mealy bst bf = Mealy (ast <> bst) $ \nt -> af nt <> bf nt
|
||||
Mealy ast ad af <> Mealy bst bd bf = Mealy (ast <> bst) (combineDiagram Merge ad bd) $ \nt -> af nt <> bf nt
|
||||
|
||||
|
||||
instance Monoid b => Monoid (Mealy eff a b) where
|
||||
mempty = Mealy mempty $ \_nt -> mempty
|
||||
mempty = Mealy mempty emptyDiagram $ \_nt -> mempty
|
||||
|
||||
eff :: (Request -> a -> eff b) -> Mealy eff a b
|
||||
eff f = Mealy mempty $ \nt -> do
|
||||
eff f = Mealy mempty emptyDiagram $ \nt -> do
|
||||
Stateful (Codec get put) (State () False) $ \s req a -> do
|
||||
b <- nt (f req a)
|
||||
pure (b, s)
|
||||
|
||||
-- | Override the static entity set of an arrow. Use when a combinator
|
||||
-- (e.g. 'switch') hides continuation entities from the runtime's
|
||||
-- startup subscription scan.
|
||||
withEntities :: S.Set T.Text -> Mealy eff a b -> Mealy eff a b
|
||||
withEntities es (Mealy _ f) = Mealy es f
|
||||
|
||||
instance Category (Mealy eff) where
|
||||
{- HLINT ignore "Use const" -}
|
||||
id = Mealy mempty $ \_nt -> id
|
||||
(Mealy ast f) . (Mealy bst g) = Mealy (ast <> bst) $ \nt -> do
|
||||
id = Mealy mempty emptyDiagram $ \_nt -> id
|
||||
(Mealy ast ad f) . (Mealy bst bd g) = Mealy (ast <> bst) (combineDiagram Then ad bd) $ \nt -> do
|
||||
f nt . g nt
|
||||
|
||||
instance Arrow (Mealy eff) where
|
||||
arr f = Mealy mempty $ \_nt -> arr f
|
||||
first (Mealy st f) = Mealy st $ \nt -> first (f nt)
|
||||
arr f = Mealy mempty emptyDiagram $ \_nt -> arr f
|
||||
first (Mealy st d f) = Mealy st d $ \nt -> first (f nt)
|
||||
|
||||
instance ArrowChoice (Mealy eff) where
|
||||
left (Mealy st f) = Mealy st $ \nt -> left (f nt)
|
||||
left (Mealy st d f) = Mealy st d $ \nt -> left (f nt)
|
||||
|
||||
|
||||
instance Functor (Mealy eff a) where
|
||||
fmap f (Mealy st g) = Mealy st $ \nt -> fmap f (g nt)
|
||||
fmap f (Mealy st d g) = Mealy st d $ \nt -> fmap f (g nt)
|
||||
|
||||
instance Applicative (Mealy eff a) where
|
||||
pure b = Mealy mempty $ \_ -> pure b
|
||||
Mealy ast f <*> Mealy bst x = Mealy (ast <> bst) $ \nt ->
|
||||
pure b = Mealy mempty emptyDiagram $ \_ -> pure b
|
||||
Mealy ast ad f <*> Mealy bst bd x = Mealy (ast <> bst) (combineDiagram Compose ad bd) $ \nt ->
|
||||
f nt <*> x nt
|
||||
|
||||
data Event a
|
||||
@@ -369,10 +371,10 @@ preMapAccum' f x extract = Stateful (Codec get put) (pure x) (\s _req a -> pure
|
||||
|
||||
|
||||
mapAccum :: (Eq x, Serialize x) => (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||
mapAccum step x extract = Mealy mempty $ \_nt -> mapAccum' step x extract
|
||||
mapAccum step x extract = Mealy mempty emptyDiagram $ \_nt -> mapAccum' step x extract
|
||||
|
||||
preMapAccum :: (Eq x, Serialize x) => (x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||
preMapAccum step x extract = Mealy mempty $ \_nt -> preMapAccum' step x extract
|
||||
preMapAccum step x extract = Mealy mempty emptyDiagram $ \_nt -> preMapAccum' step x extract
|
||||
|
||||
mapAccum' :: forall m x a b. (Monad m, Eq x, Serialize x) => (x -> a -> x) -> x -> (x -> b) -> Auto m a b
|
||||
mapAccum' f x extract = Stateful (Codec get put) (pure x) (\s _req a -> pure $ step s a)
|
||||
@@ -381,7 +383,7 @@ mapAccum' f x extract = Stateful (Codec get put) (pure x) (\s _req a -> pure $ s
|
||||
step s a = let s' = f (state s) a in (extract s', State s' (dirty s || state s /= s'))
|
||||
|
||||
preMapAccumRequest :: (Serialize x, Eq x) => (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||
preMapAccumRequest step x extract = Mealy mempty $ \_ -> preMapAccumRequest' step x extract
|
||||
preMapAccumRequest step x extract = Mealy mempty emptyDiagram $ \_ -> preMapAccumRequest' step x extract
|
||||
|
||||
preMapAccumRequest' :: forall m x a b. (Serialize x, Eq x, Monad m) => (Request -> x -> a -> x) -> x -> (x -> b) -> Auto m a b
|
||||
preMapAccumRequest' f x extract = Stateful (Codec get put) (State x False) (\s req a -> pure $ step s req a)
|
||||
@@ -390,7 +392,7 @@ preMapAccumRequest' f x extract = Stateful (Codec get put) (State x False) (\s r
|
||||
step s req a = let s' = f req (state s) a in (extract (state s), State s' (dirty s || state s /= s'))
|
||||
|
||||
mapAccumRequest :: (Serialize x, Eq x) => (Request -> x -> a -> x) -> x -> (x -> b) -> Mealy eff a b
|
||||
mapAccumRequest step x extract = Mealy mempty $ \_ -> mapAccumRequest' step x extract
|
||||
mapAccumRequest step x extract = Mealy mempty emptyDiagram $ \_ -> mapAccumRequest' step x extract
|
||||
|
||||
mapAccumRequest' :: forall m x a b. (Monad m, Serialize x, Eq x) => (Request -> x -> a -> x) -> x -> (x -> b) -> Auto m a b
|
||||
mapAccumRequest' f x extract = Stateful (Codec get put) (State x False) (\s req a -> pure $ step s req a)
|
||||
@@ -606,7 +608,7 @@ requestLocalTime :: Request -> LocalTime
|
||||
requestLocalTime Request{requestTime, requestTimeZone} = utcToLocalTime requestTimeZone requestTime
|
||||
|
||||
currentTime :: Mealy eff a LocalTime
|
||||
currentTime = Mealy mempty $ \_nt -> Fun $ \req _ ->
|
||||
currentTime = Mealy mempty emptyDiagram $ \_nt -> Fun $ \req _ ->
|
||||
requestLocalTime req
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user