on b511a57 help sign in
default.nixdeleted−43
since splitsnapshots
unifiedsplit
@@ -1,43 +0,0 @@
1−{ mkDerivation, ghc, base, bifunctors, containers, deepseq
2−, dependent-map, dependent-sum, exception-transformers
3−, haskell-src-exts, haskell-src-meta, hlint, lens, MemoTrie
4−, monad-control, mtl, primitive, random, ref-tf
5−, semigroupoids , semigroups, split, stdenv, stm, syb
6−, template-haskell , these, time, transformers
7−, transformers-compat, unbounded-delays, prim-uniq
8−, data-default, filepath, directory, filemanip, ghcjs-base
9−, monoidal-containers, witherable, profunctors
10−, semialign ? null, splitThese ? (semialign != null)
11−, useTemplateHaskell ? true
12−}:
13−mkDerivation {
14− pname = "reflex";
15− version = "0.6.2.4";
16− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
17− libraryHaskellDepends = [
18− base bifunctors containers dependent-map dependent-sum
19− exception-transformers lens
20− MemoTrie monad-control mtl primitive ref-tf semigroupoids
21− semigroups stm syb template-haskell these transformers
22− transformers-compat prim-uniq
23− base bifunctors containers deepseq dependent-map dependent-sum
24− mtl ref-tf split transformers data-default
25− random time unbounded-delays monoidal-containers witherable
26− profunctors
27− ] ++ (if ghc.isGhcjs or false then [
28− ghcjs-base
29− ] else []) ++ (if !useTemplateHaskell then [] else [
30− haskell-src-exts haskell-src-meta
31− ]) ++ (if splitThese then [
32− semialign
33− ] else []);
34− testHaskellDepends = if ghc.isGhcjs or false then [] else [
35− hlint filepath directory filemanip
36− ];
37− configureFlags =
38− stdenv.lib.optional (!useTemplateHaskell) [ "-f-use-template-haskell" ] ++
39− stdenv.lib.optional (!splitThese) [ "-f-split-these" ];
40− homepage = "https://github.com/reflex-frp/reflex";
41− description = "Higher-order Functional Reactive Programming";
42− license = stdenv.lib.licenses.bsd3;
43−}
reflex.cabal+17−4
@@ -57,6 +57,7 @@ library
57 containers >= 0.6 && < 0.7,
57 containers >= 0.6 && < 0.7,
58 data-default >= 0.5 && < 0.8,
58 data-default >= 0.5 && < 0.8,
59 dependent-map >= 0.3 && < 0.4,
59 dependent-map >= 0.3 && < 0.4,
60+ dlist == 0.8.*,
60 exception-transformers == 0.4.*,
61 exception-transformers == 0.4.*,
61 profunctors >= 5.3 && < 5.5,
62 profunctors >= 5.3 && < 5.5,
62 lens >= 4.7 && < 5,
63 lens >= 4.7 && < 5,
@@ -90,13 +91,17 @@ library
90 Data.FastMutableIntMap,
91 Data.FastMutableIntMap,
91 Data.FastWeakBag,
92 Data.FastWeakBag,
92 Data.Functor.Misc,
93 Data.Functor.Misc,
94+ Data.List.NonEmpty.Deferred,
93 Data.Map.Misc,
95 Data.Map.Misc,
96+ Data.TagMap,
97+ Data.Unique.Tag.Local,
98+ Data.Unique.Tag.Local.Internal,
94 Data.WeakBag,
99 Data.WeakBag,
95 Reflex,
100 Reflex,
96− Reflex.Class,
97 Reflex.Adjustable.Class,
101 Reflex.Adjustable.Class,
98 Reflex.BehaviorWriter.Base,
102 Reflex.BehaviorWriter.Base,
99 Reflex.BehaviorWriter.Class,
103 Reflex.BehaviorWriter.Class,
104+ Reflex.Class,
100 Reflex.Collection,
105 Reflex.Collection,
101 Reflex.Dynamic,
106 Reflex.Dynamic,
102 Reflex.Dynamic.Uniq,
107 Reflex.Dynamic.Uniq,
@@ -106,6 +111,7 @@ library
106 Reflex.EventWriter,
111 Reflex.EventWriter,
107 Reflex.EventWriter.Base,
112 Reflex.EventWriter.Base,
108 Reflex.EventWriter.Class,
113 Reflex.EventWriter.Class,
114+ Reflex.FanTag,
109 Reflex.FastWeak,
115 Reflex.FastWeak,
110 Reflex.FunctorMaybe,
116 Reflex.FunctorMaybe,
111 Reflex.Host.Class,
117 Reflex.Host.Class,
@@ -127,6 +133,7 @@ library
127 Reflex.Query.Base,
133 Reflex.Query.Base,
128 Reflex.Query.Class,
134 Reflex.Query.Class,
129 Reflex.Requester.Base,
135 Reflex.Requester.Base,
136+ Reflex.Requester.Base.Internal,
130 Reflex.Requester.Class,
137 Reflex.Requester.Class,
131 Reflex.Spider,
138 Reflex.Spider,
132 Reflex.Spider.Internal,
139 Reflex.Spider.Internal,
@@ -137,6 +144,7 @@ library
137 Reflex.Workflow
144 Reflex.Workflow
138
145
139 ghc-options: -Wall -fwarn-redundant-constraints -fwarn-tabs -funbox-strict-fields -O2 -fspecialise-aggressively
146 ghc-options: -Wall -fwarn-redundant-constraints -fwarn-tabs -funbox-strict-fields -O2 -fspecialise-aggressively
147+ ghc-prof-options: -fprof-auto-calls
140
148
141 if flag(debug-trace-events)
149 if flag(debug-trace-events)
142 cpp-options: -DDEBUG_TRACE_EVENTS
150 cpp-options: -DDEBUG_TRACE_EVENTS
@@ -172,6 +180,7 @@ test-suite semantics
172 main-is: semantics.hs
180 main-is: semantics.hs
173 hs-source-dirs: test
181 hs-source-dirs: test
174 ghc-options: -O2 -Wall -rtsopts
182 ghc-options: -O2 -Wall -rtsopts
183+ ghc-prof-options: -fprof-auto-calls
175 build-depends:
184 build-depends:
176 base,
185 base,
177 bifunctors,
186 bifunctors,
@@ -197,6 +206,7 @@ test-suite CrossImpl
197 main-is: Reflex/Test/CrossImpl.hs
206 main-is: Reflex/Test/CrossImpl.hs
198 hs-source-dirs: test
207 hs-source-dirs: test
199 ghc-options: -O2 -Wall -rtsopts
208 ghc-options: -O2 -Wall -rtsopts
209+ ghc-prof-options: -fprof-auto-calls
200 build-depends:
210 build-depends:
201 base,
211 base,
202 containers,
212 containers,
@@ -259,14 +269,15 @@ test-suite RequesterT
259 build-depends: base
269 build-depends: base
260 , containers
270 , containers
261 , deepseq
271 , deepseq
262− , dependent-sum
263 , dependent-map
272 , dependent-map
273+ , dependent-sum
264 , lens
274 , lens
265 , mtl
275 , mtl
276+ , primitive
277+ , ref-tf
278+ , reflex
266 , these
279 , these
267 , transformers
280 , transformers
268− , reflex
269− , ref-tf
270
281
271 if flag(split-these)
282 if flag(split-these)
272 build-depends: these-lens
283 build-depends: these-lens
@@ -348,6 +359,7 @@ benchmark spider-bench
348 hs-source-dirs: bench test
359 hs-source-dirs: bench test
349 main-is: Main.hs
360 main-is: Main.hs
350 ghc-options: -Wall -O2 -rtsopts
361 ghc-options: -Wall -O2 -rtsopts
362+ ghc-prof-options: -fprof-auto-calls
351 build-depends:
363 build-depends:
352 base,
364 base,
353 containers,
365 containers,
@@ -373,6 +385,7 @@ benchmark saulzar-bench
373 c-sources: bench-cbits/checkCapability.c
385 c-sources: bench-cbits/checkCapability.c
374 main-is: RunAll.hs
386 main-is: RunAll.hs
375 ghc-options: -Wall -O2 -rtsopts -threaded
387 ghc-options: -Wall -O2 -rtsopts -threaded
388+ ghc-prof-options: -fprof-auto-calls
376 build-depends:
389 build-depends:
377 base,
390 base,
378 containers,
391 containers,
src/Data/List/NonEmpty/Deferred.hsnew file+30
@@ -0,0 +1,30 @@
1+-- | Uses a non-associative internal structure to represent a NonEmpty list, but
2+-- prevents external observers from observing the non-associativity. This
3+-- allows O(1) '(<>)'.
4+
5+{-# LANGUAGE LambdaCase #-}
6+module Data.List.NonEmpty.Deferred
7+ ( NonEmptyDeferred
8+ , singleton
9+ , toNonEmpty
10+ ) where
11+
12+import Data.List.NonEmpty (NonEmpty (..))
13+import qualified Data.List.NonEmpty as NonEmpty
14+
15+data NonEmptyDeferred a
16+ = NonEmptyDeferred_Singleton a
17+ | NonEmptyDeferred_Append !(NonEmptyDeferred a) !(NonEmptyDeferred a)
18+
19+singleton :: a -> NonEmptyDeferred a
20+singleton = NonEmptyDeferred_Singleton
21+
22+{-# INLINE toNonEmpty #-}
23+toNonEmpty :: NonEmptyDeferred a -> NonEmpty a
24+toNonEmpty = go []
25+ where go t = \case
26+ NonEmptyDeferred_Singleton a -> a :| t
27+ NonEmptyDeferred_Append a b -> go (NonEmpty.toList $ go t b) a
28+
29+instance Semigroup (NonEmptyDeferred a) where
30+ (<>) = NonEmptyDeferred_Append
src/Data/TagMap.hsnew file+39
@@ -0,0 +1,39 @@
1+{-# LANGUAGE PolyKinds #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+module Data.TagMap
4+ ( TagMap
5+ , unTagMap
6+ , fromDMap
7+ , toDMap
8+ , fromList
9+ , insert
10+ , size
11+ ) where
12+
13+import Data.IntMap (IntMap)
14+import qualified Data.IntMap as IntMap
15+import Data.Dependent.Map (DMap)
16+import qualified Data.Dependent.Map as DMap
17+import Data.Dependent.Sum (DSum (..))
18+import Data.Unique.Tag.Local
19+
20+import GHC.Exts (Any)
21+import Unsafe.Coerce
22+
23+-- | Like DMap, but with 'Data.Unique.Tag.Tag' as the keys. Implemented using 'Data.IntMap.IntMap' under the hood.
24+newtype TagMap x (v :: k -> *) = TagMap { unTagMap :: IntMap Any }
25+
26+fromDMap :: forall k x (v :: k -> *). DMap (Tag x) v -> TagMap x v
27+fromDMap = TagMap . IntMap.fromDistinctAscList . fmap (\((k :: Tag x (a :: k)) :=> v) -> (tagId k, (unsafeCoerce :: v a -> Any) v)) . DMap.toAscList
28+
29+toDMap :: forall x v. TagMap x v -> DMap (Tag x) v
30+toDMap = DMap.fromDistinctAscList . fmap (\(k, v) -> (unsafeTagFromId k :=> (unsafeCoerce :: Any -> v a) v)) . IntMap.toAscList . unTagMap
31+
32+insert :: forall x a v. Tag x a -> v a -> TagMap x v -> TagMap x v
33+insert k v = TagMap . IntMap.insert (tagId k) ((unsafeCoerce :: v a -> Any) v) . unTagMap
34+
35+fromList :: [DSum (Tag x) v] -> TagMap x v
36+fromList = TagMap . IntMap.fromList . fmap (\(t :=> v) -> (tagId t, (unsafeCoerce :: v a -> Any) v))
37+
38+size :: TagMap x v -> Int
39+size = IntMap.size . unTagMap
src/Data/Unique/Tag/Local.hsnew file+11
@@ -0,0 +1,11 @@
1+module Data.Unique.Tag.Local
2+ ( Tag
3+ , TagGen (..)
4+ , tagId
5+ , unsafeTagFromId
6+ , newTag
7+ , newTagGen
8+ , withTagGen
9+ ) where
10+
11+import Data.Unique.Tag.Local.Internal
src/Data/Unique/Tag/Local/Internal.hsnew file+59
@@ -0,0 +1,59 @@
1+-- | The type-safety of this module depends on two assumptions:
2+-- 1. If the `s` parameters on two `TagGen`s can unify, then they contain the same MutVar
3+-- 2. Two Tag values made from the same TagGen never contain the same Int
4+
5+{-# LANGUAGE EmptyDataDecls #-}
6+{-# LANGUAGE DeriveFunctor #-}
7+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
8+{-# LANGUAGE TypeFamilies #-}
9+{-# LANGUAGE PolyKinds #-}
10+{-# LANGUAGE RankNTypes #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
12+{-# LANGUAGE MagicHash #-}
13+module Data.Unique.Tag.Local.Internal where
14+
15+import Control.Monad.Exception
16+import Control.Monad.Primitive
17+import Control.Monad.Reader
18+import Data.Primitive.MutVar
19+import Data.GADT.Compare
20+import Data.Some
21+import GHC.Exts (Int (..), Int#, MutVar#, unsafeCoerce#)
22+
23+import Unsafe.Coerce
24+
25+newtype Tag x a = Tag Int
26+
27+tagId :: Tag x a -> Int
28+tagId (Tag n) = n
29+
30+-- | WARNING: If you construct a tag with the wrong type, it will result in
31+-- incorrect unsafeCoerce applications, which can segfault or cause arbitrary
32+-- other damage to your program
33+unsafeTagFromId :: Int -> Tag x a
34+unsafeTagFromId n = Tag n
35+
36+-- We use Int because it is supported by e.g. IntMap
37+newtype TagGen ps s = TagGen { unTagGen :: MutVar ps Int }
38+
39+instance Show (TagGen ps s) where
40+ show (TagGen (MutVar m)) = show $ I# ((unsafeCoerce# :: MutVar# ps Int -> Int#) m)
41+
42+instance GEq (TagGen ps) where
43+ TagGen a `geq` TagGen b =
44+ if a == b
45+ then Just $ unsafeCoerce Refl
46+ else Nothing
47+
48+newTag :: PrimMonad m => TagGen (PrimState m) s -> m (Tag s a)
49+newTag (TagGen r) = do
50+ n <- atomicModifyMutVar' r $ \x -> (succ x, x)
51+ pure $ Tag n
52+
53+newTagGen :: PrimMonad m => m (Some (TagGen (PrimState m)))
54+newTagGen = Some . TagGen <$> newMutVar minBound
55+
56+withTagGen :: PrimMonad m => (forall s. TagGen (PrimState m) s -> m a) -> m a
57+withTagGen f = do
58+ g <- newTagGen
59+ withSome g f
src/Reflex/Class.hs+30−14
@@ -169,6 +169,7 @@ module Reflex.Class
169 , tagCheap
169 , tagCheap
170 , mergeWithCheap
170 , mergeWithCheap
171 , mergeWithCheap'
171 , mergeWithCheap'
172+ , sconcatCheap
172 -- * Slow, but general, implementations
173 -- * Slow, but general, implementations
173 , slowHeadE
174 , slowHeadE
174 ) where
175 ) where
@@ -890,6 +891,9 @@ instance (Semigroup a, Reflex t) => Semigroup (Event t a) where
890 sconcat = fmap sconcat . mergeList . toList
891 sconcat = fmap sconcat . mergeList . toList
891 stimes n = fmap $ stimes n
892 stimes n = fmap $ stimes n
892
893
894+sconcatCheap :: (Semigroup a, Reflex t) => NonEmpty (Event t a) -> Event t a
895+sconcatCheap = fmapCheap sconcat . mergeList . toList
896+
893instance (Semigroup a, Reflex t) => Monoid (Event t a) where
897instance (Semigroup a, Reflex t) => Monoid (Event t a) where
894 mempty = never
898 mempty = never
895 mappend = (<>)
899 mappend = (<>)
@@ -904,6 +908,8 @@ mergeWith = mergeWith' id
904
908
905{-# INLINE mergeWith' #-}
909{-# INLINE mergeWith' #-}
906mergeWith' :: Reflex t => (a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
910mergeWith' :: Reflex t => (a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
911+mergeWith' _ _ [] = never
912+mergeWith' f _ [e] = fmap f e
907mergeWith' f g es = fmap (Prelude.foldl1 g . fmap f)
913mergeWith' f g es = fmap (Prelude.foldl1 g . fmap f)
908 . mergeInt
914 . mergeInt
909 . IntMap.fromDistinctAscList
915 . IntMap.fromDistinctAscList
@@ -921,6 +927,7 @@ leftmost = mergeWith const
921-- time.
927-- time.
922mergeList :: Reflex t => [Event t a] -> Event t (NonEmpty a)
928mergeList :: Reflex t => [Event t a] -> Event t (NonEmpty a)
923mergeList [] = never
929mergeList [] = never
930+mergeList [e] = fmapCheap (:|[]) e
924mergeList es = mergeWithFoldCheap' id es
931mergeList es = mergeWithFoldCheap' id es
925
932
926unsafeMapIncremental
933unsafeMapIncremental
@@ -937,10 +944,6 @@ unsafeMapIncremental f g a = unsafeBuildIncremental (fmap f $ sample $ currentIn
937mergeMap :: (Reflex t, Ord k) => Map k (Event t a) -> Event t (Map k a)
944mergeMap :: (Reflex t, Ord k) => Map k (Event t a) -> Event t (Map k a)
938mergeMap = fmap dmapToMap . merge . mapWithFunctorToDMap
945mergeMap = fmap dmapToMap . merge . mapWithFunctorToDMap
939
946
940−-- | Like 'mergeMap' but for 'IntMap'.
941−mergeIntMap :: Reflex t => IntMap (Event t a) -> Event t (IntMap a)
942−mergeIntMap = fmap dmapToIntMap . merge . intMapWithFunctorToDMap
943−
944-- | Create a merge whose parents can change over time
947-- | Create a merge whose parents can change over time
945mergeMapIncremental :: (Reflex t, Ord k) => Incremental t (PatchMap k (Event t a)) -> Event t (Map k a)
948mergeMapIncremental :: (Reflex t, Ord k) => Incremental t (PatchMap k (Event t a)) -> Event t (Map k a)
946mergeMapIncremental = fmap dmapToMap . mergeIncremental . unsafeMapIncremental mapWithFunctorToDMap (const2PatchDMapWith id)
949mergeMapIncremental = fmap dmapToMap . mergeIncremental . unsafeMapIncremental mapWithFunctorToDMap (const2PatchDMapWith id)
@@ -1017,23 +1020,27 @@ switchHoldPromptly ea0 eea = do
1017switchHoldPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1020switchHoldPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1018switchHoldPromptOnly e0 e' = do
1021switchHoldPromptOnly e0 e' = do
1019 eLag <- switch <$> hold e0 e'
1022 eLag <- switch <$> hold e0 e'
1020− return $ coincidence $ leftmost [e', eLag <$ eLag]
1023+ return $ fmapMaybeCheap id $ leftmost
1024+ [ fmapCheap Just $ coincidence e'
1025+ , fmapCheap (const Nothing) e'
1026+ , fmapCheap Just eLag
1027+ ]
1021
1028
1022-- | When the given outer event fires, condense the inner events into the contained patch. Non-firing inner events will be replaced with deletions.
1029-- | When the given outer event fires, condense the inner events into the contained patch. Non-firing inner events will be replaced with deletions.
1023coincidencePatchMap :: (Reflex t, Ord k) => Event t (PatchMap k (Event t v)) -> Event t (PatchMap k v)
1030coincidencePatchMap :: (Reflex t, Ord k) => Event t (PatchMap k (Event t v)) -> Event t (PatchMap k v)
1024−coincidencePatchMap e = fmapCheap PatchMap $ coincidence $ ffor e $ \(PatchMap m) -> mergeMap $ ffor m $ \case
1031+coincidencePatchMap e = fmapCheap PatchMap $ coincidence $ fforCheap e $ \(PatchMap m) -> mergeMap $ ffor m $ \case
1025 Nothing -> fmapCheap (const Nothing) e
1032 Nothing -> fmapCheap (const Nothing) e
1026 Just ev -> leftmost [fmapCheap Just ev, fmapCheap (const Nothing) e]
1033 Just ev -> leftmost [fmapCheap Just ev, fmapCheap (const Nothing) e]
1027
1034
1028-- | See 'coincidencePatchMap'
1035-- | See 'coincidencePatchMap'
1029coincidencePatchIntMap :: Reflex t => Event t (PatchIntMap (Event t v)) -> Event t (PatchIntMap v)
1036coincidencePatchIntMap :: Reflex t => Event t (PatchIntMap (Event t v)) -> Event t (PatchIntMap v)
1030−coincidencePatchIntMap e = fmapCheap PatchIntMap $ coincidence $ ffor e $ \(PatchIntMap m) -> mergeIntMap $ ffor m $ \case
1037+coincidencePatchIntMap e = fmapCheap PatchIntMap $ coincidence $ fforCheap e $ \(PatchIntMap m) -> mergeIntMap $ ffor m $ \case
1031 Nothing -> fmapCheap (const Nothing) e
1038 Nothing -> fmapCheap (const Nothing) e
1032 Just ev -> leftmost [fmapCheap Just ev, fmapCheap (const Nothing) e]
1039 Just ev -> leftmost [fmapCheap Just ev, fmapCheap (const Nothing) e]
1033
1040
1034-- | See 'coincidencePatchMap'
1041-- | See 'coincidencePatchMap'
1035coincidencePatchMapWithMove :: (Reflex t, Ord k) => Event t (PatchMapWithMove k (Event t v)) -> Event t (PatchMapWithMove k v)
1042coincidencePatchMapWithMove :: (Reflex t, Ord k) => Event t (PatchMapWithMove k (Event t v)) -> Event t (PatchMapWithMove k v)
1036−coincidencePatchMapWithMove e = fmapCheap unsafePatchMapWithMove $ coincidence $ ffor e $ \p -> mergeMap $ ffor (unPatchMapWithMove p) $ \ni -> case PatchMapWithMove._nodeInfo_from ni of
1043+coincidencePatchMapWithMove e = fmapCheap unsafePatchMapWithMove $ coincidence $ fforCheap e $ \p -> mergeMap $ ffor (unPatchMapWithMove p) $ \ni -> case PatchMapWithMove._nodeInfo_from ni of
1037 PatchMapWithMove.From_Delete -> fforCheap e $ \_ ->
1044 PatchMapWithMove.From_Delete -> fforCheap e $ \_ ->
1038 ni { PatchMapWithMove._nodeInfo_from = PatchMapWithMove.From_Delete }
1045 ni { PatchMapWithMove._nodeInfo_from = PatchMapWithMove.From_Delete }
1039 PatchMapWithMove.From_Move k -> fforCheap e $ \_ ->
1046 PatchMapWithMove.From_Move k -> fforCheap e $ \_ ->
@@ -1293,7 +1300,7 @@ accumMaybeMDyn
1293 -> Event t b
1300 -> Event t b
1294 -> m (Dynamic t a)
1301 -> m (Dynamic t a)
1295accumMaybeMDyn f z e = do
1302accumMaybeMDyn f z e = do
1296− rec let e' = flip push e $ \o -> do
1303+ rec let e' = flip pushCheap e $ \o -> do
1297 v <- sample $ current d'
1304 v <- sample $ current d'
1298 f v o
1305 f v o
1299 d' <- holdDyn z e'
1306 d' <- holdDyn z e'
@@ -1349,8 +1356,8 @@ mapAccumMaybeMDyn f z e = do
1349 return $ case result of
1356 return $ case result of
1350 (Nothing, Nothing) -> Nothing
1357 (Nothing, Nothing) -> Nothing
1351 _ -> Just result
1358 _ -> Just result
1352− d' <- holdDyn z $ mapMaybe fst e'
1359+ d' <- holdDyn z $ fmapMaybeCheap fst e'
1353− return (d', mapMaybe snd e')
1360+ return (d', fmapMaybeCheap snd e')
1354
1361
1355-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1362-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
1356-- with the provided function.
1363-- with the provided function.
@@ -1388,7 +1395,7 @@ accumMaybeB f = accumMaybeMB $ \v o -> return $ f v o
1388{-# INLINE accumMaybeMB #-}
1395{-# INLINE accumMaybeMB #-}
1389accumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
1396accumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
1390accumMaybeMB f z e = do
1397accumMaybeMB f z e = do
1391− rec let e' = flip push e $ \o -> do
1398+ rec let e' = flip pushCheap e $ \o -> do
1392 v <- sample d'
1399 v <- sample d'
1393 f v o
1400 f v o
1394 d' <- hold z e'
1401 d' <- hold z e'
@@ -1438,8 +1445,8 @@ mapAccumMaybeMB f z e = do
1438 return $ case result of
1445 return $ case result of
1439 (Nothing, Nothing) -> Nothing
1446 (Nothing, Nothing) -> Nothing
1440 _ -> Just result
1447 _ -> Just result
1441− d' <- hold z $ mapMaybe fst e'
1448+ d' <- hold z $ fmapMaybeCheap fst e'
1442− return (d', mapMaybe snd e')
1449+ return (d', fmapMaybeCheap snd e')
1443
1450
1444-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1451-- | Accumulate occurrences of an 'Event', producing an output occurrence each
1445-- time. Discard the underlying 'Accumulator'.
1452-- time. Discard the underlying 'Accumulator'.
@@ -1649,6 +1656,8 @@ mergeWithCheap' f g = mergeWithFoldCheap' $ foldl1 g . fmap f
1649-- | A "cheap" version of 'mergeWithFoldCheap''. See the performance note on 'pushCheap'.
1656-- | A "cheap" version of 'mergeWithFoldCheap''. See the performance note on 'pushCheap'.
1650{-# INLINE mergeWithFoldCheap' #-}
1657{-# INLINE mergeWithFoldCheap' #-}
1651mergeWithFoldCheap' :: Reflex t => (NonEmpty a -> b) -> [Event t a] -> Event t b
1658mergeWithFoldCheap' :: Reflex t => (NonEmpty a -> b) -> [Event t a] -> Event t b
1659+mergeWithFoldCheap' f [] = never
1660+mergeWithFoldCheap' f [e] = fmapCheap (f . (:|[])) e
1652mergeWithFoldCheap' f es =
1661mergeWithFoldCheap' f es =
1653 fmapCheap (f . (\(h : t) -> h :| t) . IntMap.elems)
1662 fmapCheap (f . (\(h : t) -> h :| t) . IntMap.elems)
1654 . mergeInt
1663 . mergeInt
@@ -1663,7 +1672,14 @@ mergeWithFoldCheap' f es =
1663-- | See 'switchHoldPromptly'
1672-- | See 'switchHoldPromptly'
1664switchPromptly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1673switchPromptly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1665switchPromptly = switchHoldPromptly
1674switchPromptly = switchHoldPromptly
1675+
1666{-# DEPRECATED switchPromptOnly "Use 'switchHoldPromptOnly' instead. The 'switchHold*' naming convention was chosen because those functions are more closely related to each other than they are to 'switch'. " #-}
1676{-# DEPRECATED switchPromptOnly "Use 'switchHoldPromptOnly' instead. The 'switchHold*' naming convention was chosen because those functions are more closely related to each other than they are to 'switch'. " #-}
1667-- | See 'switchHoldPromptOnly'
1677-- | See 'switchHoldPromptOnly'
1668switchPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1678switchPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
1669switchPromptOnly = switchHoldPromptOnly
1679switchPromptOnly = switchHoldPromptOnly
1680+
1681+{-# DEPRECATED mergeIntMap "Use 'mergeInt' instead" #-}
1682+-- | Like 'mergeMap' but for 'IntMap'.
1683+mergeIntMap :: Reflex t => IntMap (Event t a) -> Event t (IntMap a)
1684+mergeIntMap = mergeInt
1685+
src/Reflex/Dynamic.hs+1
@@ -191,6 +191,7 @@ switchPromptlyDyn de =
191 let eLag = switch $ current de
191 let eLag = switch $ current de
192 eCoincidences = coincidence $ updated de
192 eCoincidences = coincidence $ updated de
193 in leftmost [eCoincidences, eLag]
193 in leftmost [eCoincidences, eLag]
194+--TODO: switchPromptlyDyn should get the `only` treatment like switchHoldPromptOnly
194
195
195-- | Split a 'Dynamic' pair into a pair of 'Dynamic's
196-- | Split a 'Dynamic' pair into a pair of 'Dynamic's
196splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
197splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
src/Reflex/EventWriter/Base.hs+9−51
@@ -47,6 +47,8 @@ import Data.GADT.Compare (GCompare (..), GEq (..), GOrdering (..))
47import Data.IntMap.Strict (IntMap)
47import Data.IntMap.Strict (IntMap)
48import qualified Data.IntMap.Strict as IntMap
48import qualified Data.IntMap.Strict as IntMap
49import Data.List.NonEmpty (NonEmpty (..))
49import Data.List.NonEmpty (NonEmpty (..))
50+import Data.List.NonEmpty.Deferred (NonEmptyDeferred)
51+import qualified Data.List.NonEmpty.Deferred as NonEmptyDeferred
50import Data.Map (Map)
52import Data.Map (Map)
51import qualified Data.Map as Map
53import qualified Data.Map as Map
52import Data.Semigroup
54import Data.Semigroup
@@ -54,45 +56,7 @@ import Data.Some (Some)
54import Data.Tuple
56import Data.Tuple
55import Data.Type.Equality
57import Data.Type.Equality
56
58
57−import Unsafe.Coerce
58−
59−{-# DEPRECATED TellId "Do not construct this directly; use tellId instead" #-}
60−newtype TellId w x
61− = TellId Int -- ^ WARNING: Do not construct this directly; use 'tellId' instead
62− deriving (Show, Eq, Ord, Enum)
63−
64−tellId :: Int -> TellId w w
65−tellId = TellId
66−{-# INLINE tellId #-}
67−
68−tellIdRefl :: TellId w x -> w :~: x
69−tellIdRefl _ = unsafeCoerce Refl
70−
71−withTellIdRefl :: TellId w x -> (w ~ x => r) -> r
72−withTellIdRefl tid r = case tellIdRefl tid of
73− Refl -> r
74−
75−instance GEq (TellId w) where
76− a `geq` b =
77− withTellIdRefl a $
78− withTellIdRefl b $
79− if a == b
80− then Just Refl
81− else Nothing
82−
83−instance GCompare (TellId w) where
84− a `gcompare` b =
85− withTellIdRefl a $
86− withTellIdRefl b $
87− case a `compare` b of
88− LT -> GLT
89− EQ -> GEQ
90− GT -> GGT
91−
92−data EventWriterState t w = EventWriterState
59+type EventWriterState t w = Maybe (NonEmptyDeferred (Event t w))
93− { _eventWriterState_nextId :: {-# UNPACK #-} !Int -- Always negative (and decreasing over time)
94− , _eventWriterState_told :: ![DSum (TellId w) (Event t)] -- In increasing order
95− }
96
60
97-- | A basic implementation of 'EventWriter'.
61-- | A basic implementation of 'EventWriter'.
98newtype EventWriterT t w m a = EventWriterT { unEventWriterT :: StateT (EventWriterState t w) m a }
62newtype EventWriterT t w m a = EventWriterT { unEventWriterT :: StateT (EventWriterState t w) m a }
@@ -101,20 +65,11 @@ newtype EventWriterT t w m a = EventWriterT { unEventWriterT :: StateT (EventWri
101-- | Run a 'EventWriterT' action.
65-- | Run a 'EventWriterT' action.
102runEventWriterT :: forall t m w a. (Reflex t, Monad m, Semigroup w) => EventWriterT t w m a -> m (a, Event t w)
66runEventWriterT :: forall t m w a. (Reflex t, Monad m, Semigroup w) => EventWriterT t w m a -> m (a, Event t w)
103runEventWriterT (EventWriterT a) = do
67runEventWriterT (EventWriterT a) = do
104− (result, requests) <- runStateT a $ EventWriterState (-1) []
68+ (result, requests) <- runStateT a Nothing
105− let combineResults :: DMap (TellId w) Identity -> w
69+ return (result, maybe never (sconcatCheap . NonEmptyDeferred.toNonEmpty) requests)
106− combineResults = sconcat
107− . (\(h : t) -> h :| t) -- Unconditional; 'merge' guarantees that it will only fire with non-empty DMaps
108− . DMap.foldlWithKey (\vs tid (Identity v) -> withTellIdRefl tid $ v : vs) [] -- This is where we finally reverse the DMap to get things in the correct order
109− return (result, fmap combineResults $ merge $ DMap.fromDistinctAscList $ _eventWriterState_told requests) --TODO: We can probably make this fromDistinctAscList more efficient by knowing the length in advance, but this will require exposing internals of DMap; also converting it to use a strict list might help
110
70
111instance (Reflex t, Monad m, Semigroup w) => EventWriter t w (EventWriterT t w m) where
71instance (Reflex t, Monad m, Semigroup w) => EventWriter t w (EventWriterT t w m) where
112− tellEvent w = EventWriterT $ modify $ \old ->
72+ tellEvent w = EventWriterT $ modify (<> Just (NonEmptyDeferred.singleton w))
113− let myId = _eventWriterState_nextId old
114− in EventWriterState
115− { _eventWriterState_nextId = pred myId
116− , _eventWriterState_told = (tellId myId :=> w) : _eventWriterState_told old
117− }
118
73
119instance MonadTrans (EventWriterT t w) where
74instance MonadTrans (EventWriterT t w) where
120 lift = EventWriterT . lift
75 lift = EventWriterT . lift
@@ -200,6 +155,9 @@ sequenceIntMapWithAdjustEventWriterTWith base mergePatchIncremental coincidenceP
200 h : t -> Just $ sconcat $ h :| t
155 h : t -> Just $ sconcat $ h :| t
201 return (result0, result')
156 return (result0, result')
202
157
158+switchHoldPromptOnlyIntMapIncremental :: (Reflex t, MonadHold t m) => IntMap (Event t a) -> Event t (PatchIntMap (Event t a)) -> m (Event t (IntMap a))
159+switchHoldPromptOnlyIntMapIncremental = switchHoldPromptOnlyIncremental mergeIntIncremental coincidencePatchIntMap
160+
203-- | Like 'runWithReplaceEventWriterTWith', but for 'sequenceDMapWithAdjust'.
161-- | Like 'runWithReplaceEventWriterTWith', but for 'sequenceDMapWithAdjust'.
204sequenceDMapWithAdjustEventWriterTWith
162sequenceDMapWithAdjustEventWriterTWith
205 :: forall t m p p' w k v v'
163 :: forall t m p p' w k v v'
src/Reflex/FanTag.hsnew file+23
@@ -0,0 +1,23 @@
1+{-# LANGUAGE PolyKinds #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+module Reflex.FanTag
4+ ( EventSelectorTag
5+ , unEventSelectorTag
6+ , fanTag
7+ , selectTag
8+ ) where
9+
10+import Data.Unique.Tag.Local
11+import Data.TagMap
12+import Reflex.Class
13+
14+import GHC.Exts (Any)
15+import Unsafe.Coerce
16+
17+newtype EventSelectorTag t x (v :: k -> *) = EventSelectorTag { unEventSelectorTag :: EventSelectorInt t Any }
18+
19+fanTag :: Reflex t => Event t (TagMap x v) -> EventSelectorTag t x v
20+fanTag = EventSelectorTag . fanInt . fmapCheap unTagMap
21+
22+selectTag :: forall t x v a. Reflex t => EventSelectorTag t x v -> Tag x a -> Event t (v a)
23+selectTag (EventSelectorTag s) = fmapCheap (unsafeCoerce :: Any -> v a) . selectInt s . tagId
src/Reflex/NotReady/Class.hs+5
@@ -25,6 +25,7 @@ import Reflex.PerformEvent.Base (PerformEventT (..))
25import Reflex.PostBuild.Base (PostBuildT)
25import Reflex.PostBuild.Base (PostBuildT)
26import Reflex.Query.Base (QueryT)
26import Reflex.Query.Base (QueryT)
27import Reflex.Requester.Base (RequesterT)
27import Reflex.Requester.Base (RequesterT)
28+import Reflex.Requester.Base.Internal (RequesterInternalT)
28import Reflex.TriggerEvent.Base (TriggerEventT)
29import Reflex.TriggerEvent.Base (TriggerEventT)
29
30
30class Monad m => NotReady t m | m -> t where
31class Monad m => NotReady t m | m -> t where
@@ -72,6 +73,10 @@ instance NotReady t m => NotReady t (RequesterT t request response m) where
72 notReadyUntil = lift . notReadyUntil
73 notReadyUntil = lift . notReadyUntil
73 notReady = lift notReady
74 notReady = lift notReady
74
75
76+instance NotReady t m => NotReady t (RequesterInternalT s t request response m) where
77+ notReadyUntil = lift . notReadyUntil
78+ notReady = lift notReady
79+
75instance NotReady t m => NotReady t (TriggerEventT t m) where
80instance NotReady t m => NotReady t (TriggerEventT t m) where
76 notReadyUntil = lift . notReadyUntil
81 notReadyUntil = lift . notReadyUntil
77 notReady = lift notReady
82 notReady = lift notReady
src/Reflex/Patch/IntMap.hs+19−8
@@ -1,12 +1,16 @@
1{-# LANGUAGE DeriveFoldable #-}
1{-# LANGUAGE DeriveFoldable #-}
2{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE DeriveTraversable #-}
4+{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+{-# LANGUAGE MultiParamTypeClasses #-}
7+{-# LANGUAGE TemplateHaskell #-}
5{-# LANGUAGE TypeFamilies #-}
8{-# LANGUAGE TypeFamilies #-}
6-- | Module containing 'PatchIntMap', a 'Patch' for 'IntMap' which allows for
9-- | Module containing 'PatchIntMap', a 'Patch' for 'IntMap' which allows for
7-- insert/update or delete of associations.
10-- insert/update or delete of associations.
8module Reflex.Patch.IntMap where
11module Reflex.Patch.IntMap where
9
12
13+import Control.Lens
10import Data.IntMap.Strict (IntMap)
14import Data.IntMap.Strict (IntMap)
11import qualified Data.IntMap.Strict as IntMap
15import qualified Data.IntMap.Strict as IntMap
12import Data.Maybe
16import Data.Maybe
@@ -18,14 +22,6 @@ import Reflex.Patch.Class
18-- and @Nothing@ means delete.
22-- and @Nothing@ means delete.
19newtype PatchIntMap a = PatchIntMap (IntMap (Maybe a)) deriving (Functor, Foldable, Traversable, Monoid)
23newtype PatchIntMap a = PatchIntMap (IntMap (Maybe a)) deriving (Functor, Foldable, Traversable, Monoid)
20
24
21−-- | Apply the insertions or deletions to a given 'IntMap'.
22−instance Patch (PatchIntMap a) where
23− type PatchTarget (PatchIntMap a) = IntMap a
24− apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
25− let removes = IntMap.filter isNothing p
26− adds = IntMap.mapMaybe id p
27− in IntMap.union adds $ v `IntMap.difference` removes
28−
29-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
25-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
30-- If the same key is modified by both patches, the one on the left will take
26-- If the same key is modified by both patches, the one on the left will take
31-- precedence.
27-- precedence.
@@ -34,11 +30,26 @@ instance Semigroup (PatchIntMap v) where
34 -- PatchMap is idempotent, so stimes n is id for every n
30 -- PatchMap is idempotent, so stimes n is id for every n
35 stimes = stimesIdempotentMonoid
31 stimes = stimesIdempotentMonoid
36
32
33+makeWrapped ''PatchIntMap
34+
35+-- | Apply the insertions or deletions to a given 'IntMap'.
36+instance Patch (PatchIntMap a) where
37+ type PatchTarget (PatchIntMap a) = IntMap a
38+ apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
39+ let removes = IntMap.filter isNothing p
40+ adds = IntMap.mapMaybe id p
41+ in IntMap.union adds $ v `IntMap.difference` removes
42+
37-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
43-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
38-- (that is, all inserts/updates), producing a @PatchIntMap b@.
44-- (that is, all inserts/updates), producing a @PatchIntMap b@.
39mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
45mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
40mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
46mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
41
47
48+instance FunctorWithIndex Int PatchIntMap
49+instance FoldableWithIndex Int PatchIntMap
50+instance TraversableWithIndex Int PatchIntMap where
51+ itraversed = _Wrapped . itraversed . traversed
52+
42-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
53-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
43-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
54-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
44traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
55traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
src/Reflex/PerformEvent/Base.hs+84−12
@@ -28,8 +28,10 @@ import Reflex.Class
28import Reflex.Adjustable.Class
28import Reflex.Adjustable.Class
29import Reflex.Host.Class
29import Reflex.Host.Class
30import Reflex.PerformEvent.Class
30import Reflex.PerformEvent.Class
31−import Reflex.Requester.Base
31+import Reflex.Requester.Base.Internal
32import Reflex.Requester.Class
32import Reflex.Requester.Class
33+import Reflex.EventWriter.Class
34+import Reflex.EventWriter.Base
33
35
34import Control.Lens
36import Control.Lens
35import Control.Monad.Exception
37import Control.Monad.Exception
@@ -37,13 +39,25 @@ import Control.Monad.Identity
37import Control.Monad.Primitive
39import Control.Monad.Primitive
38import Control.Monad.Reader
40import Control.Monad.Reader
39import Control.Monad.Ref
41import Control.Monad.Ref
40−import Data.Coerce
41import Data.Dependent.Map (DMap)
42import Data.Dependent.Map (DMap)
42import qualified Data.Dependent.Map as DMap
43import qualified Data.Dependent.Map as DMap
43import Data.Dependent.Sum
44import Data.Dependent.Sum
45+import Data.Foldable
46+import Data.Functor.Compose
47+import Data.Functor.Misc
44import Data.IntMap.Strict (IntMap)
48import Data.IntMap.Strict (IntMap)
45import qualified Data.IntMap.Strict as IntMap
49import qualified Data.IntMap.Strict as IntMap
50+import Data.List.NonEmpty (NonEmpty (..))
51+import Data.List.NonEmpty.Deferred (NonEmptyDeferred)
52+import qualified Data.List.NonEmpty.Deferred as NonEmptyDeferred
53+import Data.Map (Map)
54+import qualified Data.Map as Map
55+import Data.Semigroup (Semigroup (sconcat))
46import qualified Data.Semigroup as S
56import qualified Data.Semigroup as S
57+import Data.Unique.Tag.Local
58+import Data.Sequence (Seq)
59+import Data.Tuple
60+import qualified Data.TagMap as TagMap
47
61
48-- | A function that fires events for the given 'EventTrigger's and then runs
62-- | A function that fires events for the given 'EventTrigger's and then runs
49-- any followup actions provided via 'PerformEvent'. The given 'ReadPhase'
63-- any followup actions provided via 'PerformEvent'. The given 'ReadPhase'
@@ -68,7 +82,7 @@ instance (PrimMonad (HostFrame t), ReflexHost t) => PrimMonad (PerformEventT t m
68 type PrimState (PerformEventT t m) = PrimState (HostFrame t)
82 type PrimState (PerformEventT t m) = PrimState (HostFrame t)
69 primitive = PerformEventT . lift . primitive
83 primitive = PerformEventT . lift . primitive
70
84
71−instance (ReflexHost t, Ref m ~ Ref IO) => PerformEvent t (PerformEventT t m) where
85+instance (ReflexHost t, Ref m ~ Ref IO, PrimMonad (HostFrame t)) => PerformEvent t (PerformEventT t m) where
72 type Performable (PerformEventT t m) = HostFrame t
86 type Performable (PerformEventT t m) = HostFrame t
73 {-# INLINABLE performEvent_ #-}
87 {-# INLINABLE performEvent_ #-}
74 performEvent_ = PerformEventT . requesting_
88 performEvent_ = PerformEventT . requesting_
@@ -76,15 +90,70 @@ instance (ReflexHost t, Ref m ~ Ref IO) => PerformEvent t (PerformEventT t m) wh
76 performEvent = PerformEventT . requestingIdentity
90 performEvent = PerformEventT . requestingIdentity
77
91
78instance (ReflexHost t, PrimMonad (HostFrame t)) => Adjustable t (PerformEventT t m) where
92instance (ReflexHost t, PrimMonad (HostFrame t)) => Adjustable t (PerformEventT t m) where
79− runWithReplace outerA0 outerA' = PerformEventT $ runWithReplaceRequesterTWith f (coerce outerA0) (coerceEvent outerA')
93+ runWithReplace a0 a' = PerformEventT $ RequesterT $ do
80− where f :: HostFrame t a -> Event t (HostFrame t b) -> RequesterT t (HostFrame t) Identity (HostFrame t) (a, Event t b)
94+ env@(_, _ :: TagGen (PrimState (HostFrame t)) FakeRequesterStatePhantom) <- RequesterInternalT ask
81− f a0 a' = do
95+ let runA :: forall a. PerformEventT t m a -> HostFrame t (a, Event t (NonEmptyDeferred (RequestEnvelope FakeRequesterStatePhantom (HostFrame t))))
96+ runA (PerformEventT (RequesterT a)) = runEventWriterT $ runReaderT (unRequesterInternalT a) env
82− result0 <- lift a0
97+ (result0, requests0) <- lift $ runA a0
83− result' <- requestingIdentity a'
98+ newA <- requestingIdentity $ runA <$> a'
84− return (result0, result')
99+ requests <- switchHoldPromptOnly requests0 $ fmapCheap snd newA
85− traverseIntMapWithKeyWithAdjust f outerDm0 outerDm' = PerformEventT $ traverseIntMapWithKeyWithAdjustRequesterTWith (defaultAdjustIntBase traverseIntMapPatchWithKey) patchIntMapNewElementsMap mergeIntIncremental (\k v -> unPerformEventT $ f k v) (coerce outerDm0) (coerceEvent outerDm')
100+ --TODO: promptly *prevent* events, then sign up the new ones; this is a serious breaking change to PerformEvent
86− traverseDMapWithKeyWithAdjust f outerDm0 outerDm' = PerformEventT $ traverseDMapWithKeyWithAdjustRequesterTWith (defaultAdjustBase traversePatchDMapWithKey) mapPatchDMap weakenPatchDMapWith patchMapNewElementsMap mergeMapIncremental (\k v -> unPerformEventT $ f k v) (coerce outerDm0) (coerceEvent outerDm')
101+ RequesterInternalT $ tellEvent requests
87− traverseDMapWithKeyWithAdjustWithMove f outerDm0 outerDm' = PerformEventT $ traverseDMapWithKeyWithAdjustRequesterTWith (defaultAdjustBase traversePatchDMapWithMoveWithKey) mapPatchDMapWithMove weakenPatchDMapWithMoveWith patchMapWithMoveNewElementsMap mergeMapIncrementalWithMove (\k v -> unPerformEventT $ f k v) (coerce outerDm0) (coerceEvent outerDm')
102+ pure (result0, fmapCheap fst newA)
103+ traverseIntMapWithKeyWithAdjust f a0 a' = PerformEventT $ RequesterT $ do
104+ env@(_, _ :: TagGen (PrimState (HostFrame t)) FakeRequesterStatePhantom) <- RequesterInternalT ask
105+ let runA :: forall a. PerformEventT t m a -> HostFrame t (a, Event t (NonEmptyDeferred (RequestEnvelope FakeRequesterStatePhantom (HostFrame t))))
106+ runA (PerformEventT (RequesterT a)) = runEventWriterT $ runReaderT (unRequesterInternalT a) env
107+ children' <- requestingIdentity $ itraverse (\k -> runA . f k) <$> a'
108+ children0 <- lift $ itraverse (\k -> runA . f k) a0
109+ let results0 = fmap fst children0
110+ requests0 = fmap snd children0
111+ results' = fmap fst <$> children'
112+ requests' = fmap snd `fmapCheap` children'
113+ requests <- switchHoldPromptOnlyIncremental mergeIntIncremental coincidencePatchIntMap requests0 requests'
114+ --TODO: promptly *prevent* events, then sign up the new ones; this is a serious breaking change to PerformEvent
115+ RequesterInternalT $ tellEvent $ fforMaybeCheap requests concatIntMapMaybe
116+ pure (results0, results')
117+ traverseDMapWithKeyWithAdjust (f :: forall a. k a -> v a -> PerformEventT t m (v' a)) (a0 :: DMap k v) a' = PerformEventT $ RequesterT $ do
118+ env@(_, _ :: TagGen (PrimState (HostFrame t)) FakeRequesterStatePhantom) <- RequesterInternalT ask
119+ let runA :: forall a. k a -> v a -> HostFrame t (Compose ((,) (Event t (NonEmptyDeferred (RequestEnvelope FakeRequesterStatePhantom (HostFrame t))))) v' a)
120+ runA k v = fmap (Compose . swap) $ runEventWriterT $ runReaderT (unRequesterInternalT a) env
121+ where (PerformEventT (RequesterT a)) = f k v
122+ children' <- requestingIdentity $ traversePatchDMapWithKey runA <$> a'
123+ children0 <- lift $ DMap.traverseWithKey runA a0
124+ let results0 = DMap.map (snd . getCompose) children0
125+ requests0 = weakenDMapWith (fst . getCompose) children0
126+ results' = mapPatchDMap (snd . getCompose) <$> children'
127+ requests' = weakenPatchDMapWith (fst . getCompose) `fmapCheap` children'
128+ requests <- switchHoldPromptOnlyIncremental mergeMapIncremental coincidencePatchMap requests0 requests'
129+ --TODO: promptly *prevent* events, then sign up the new ones; this is a serious breaking change to PerformEvent
130+ RequesterInternalT $ tellEvent $ fforMaybeCheap requests concatMapMaybe
131+ pure (results0, results')
132+ traverseDMapWithKeyWithAdjustWithMove (f :: forall a. k a -> v a -> PerformEventT t m (v' a)) (a0 :: DMap k v) a' = PerformEventT $ RequesterT $ do
133+ env@(_, _ :: TagGen (PrimState (HostFrame t)) FakeRequesterStatePhantom) <- RequesterInternalT ask
134+ let runA :: forall a. k a -> v a -> HostFrame t (Compose ((,) (Event t (NonEmptyDeferred (RequestEnvelope FakeRequesterStatePhantom (HostFrame t))))) v' a)
135+ runA k v = fmap (Compose . swap) $ runEventWriterT $ runReaderT (unRequesterInternalT a) env
136+ where (PerformEventT (RequesterT a)) = f k v
137+ children' <- requestingIdentity $ traversePatchDMapWithMoveWithKey runA <$> a'
138+ children0 <- lift $ DMap.traverseWithKey runA a0
139+ let results0 = DMap.map (snd . getCompose) children0
140+ requests0 = weakenDMapWith (fst . getCompose) children0
141+ results' = mapPatchDMapWithMove (snd . getCompose) <$> children'
142+ requests' = weakenPatchDMapWithMoveWith (fst . getCompose) `fmapCheap` children'
143+ requests <- switchHoldPromptOnlyIncremental mergeMapIncrementalWithMove coincidencePatchMapWithMove requests0 requests'
144+ --TODO: promptly *prevent* events, then sign up the new ones; this is a serious breaking change to PerformEvent
145+ RequesterInternalT $ tellEvent $ fforMaybeCheap requests concatMapMaybe
146+ pure (results0, results')
147+
148+concatIntMapMaybe :: Semigroup a => IntMap a -> Maybe a
149+concatIntMapMaybe m = case IntMap.elems m of
150+ [] -> Nothing
151+ h : t -> Just $ sconcat $ h :| t
152+
153+concatMapMaybe :: Semigroup a => Map k a -> Maybe a
154+concatMapMaybe m = case Map.elems m of
155+ [] -> Nothing
156+ h : t -> Just $ sconcat $ h :| t
88
157
89defaultAdjustBase :: forall t v v2 k' p. (Monad (HostFrame t), PrimMonad (HostFrame t), Reflex t)
158defaultAdjustBase :: forall t v v2 k' p. (Monad (HostFrame t), PrimMonad (HostFrame t), Reflex t)
90 => ((forall a. k' a -> v a -> HostFrame t (v2 a)) -> p k' v -> HostFrame t (p k' v2))
159 => ((forall a. k' a -> v a -> HostFrame t (v2 a)) -> p k' v -> HostFrame t (p k' v2))
@@ -124,6 +193,7 @@ hostPerformEventT :: forall t m a.
124 , MonadReflexHost t m
193 , MonadReflexHost t m
125 , MonadRef m
194 , MonadRef m
126 , Ref m ~ Ref IO
195 , Ref m ~ Ref IO
196+ , PrimMonad (HostFrame t)
127 )
197 )
128 => PerformEventT t m a
198 => PerformEventT t m a
129 -> m (a, FireCommand t m)
199 -> m (a, FireCommand t m)
@@ -142,6 +212,8 @@ hostPerformEventT a = do
142 Nothing -> return [result']
212 Nothing -> return [result']
143 Just toPerform -> do
213 Just toPerform -> do
144 responses <- runHostFrame $ traverseRequesterData (Identity <$>) toPerform
214 responses <- runHostFrame $ traverseRequesterData (Identity <$>) toPerform
215+ let responseLength = case responses of
216+ ResponseData _ m -> TagMap.size m
145 mrt <- readRef responseTrigger
217 mrt <- readRef responseTrigger
146 let followupEventTriggers = case mrt of
218 let followupEventTriggers = case mrt of
147 Just rt -> [rt :=> Identity responses]
219 Just rt -> [rt :=> Identity responses]
src/Reflex/Requester/Base.hs+14−529
@@ -1,537 +1,22 @@
1-- | This module provides 'RequesterT', the standard implementation of
1-- | This module provides 'RequesterT', the standard implementation of
2-- 'Requester'.
2-- 'Requester'.
3−{-# LANGUAGE AllowAmbiguousTypes #-}
4−{-# LANGUAGE CPP #-}
5−{-# LANGUAGE EmptyDataDecls #-}
6−{-# LANGUAGE ExistentialQuantification #-}
7−{-# LANGUAGE FlexibleInstances #-}
8−{-# LANGUAGE GADTs #-}
9−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10−{-# LANGUAGE MultiParamTypeClasses #-}
11−{-# LANGUAGE Rank2Types #-}
12−{-# LANGUAGE RecursiveDo #-}
13−{-# LANGUAGE ScopedTypeVariables #-}
14−{-# LANGUAGE StandaloneDeriving #-}
15−{-# LANGUAGE TypeFamilies #-}
16−{-# LANGUAGE TypeOperators #-}
17−{-# LANGUAGE UndecidableInstances #-}
18−#ifdef USE_REFLEX_OPTIMIZER
19−{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
20−#endif
21module Reflex.Requester.Base
3module Reflex.Requester.Base
22 ( RequesterT (..)
4 ( RequesterT (..)
5+ , RequestData (..)
6+ , ResponseData (..)
7+ , RequestEnvelope (..)
23 , runRequesterT
8 , runRequesterT
24− , withRequesterT
9+-- , withRequesterT
25− , runWithReplaceRequesterTWith
26− , traverseIntMapWithKeyWithAdjustRequesterTWith
27− , traverseDMapWithKeyWithAdjustRequesterTWith
28− , RequesterData
10+-- , RequesterData
29− , RequesterDataKey
11+-- , RequesterDataKey
30 , traverseRequesterData
12 , traverseRequesterData
31− , forRequesterData
13+-- , forRequesterData
32− , requesterDataToList
14+-- , requesterDataToList
33− , singletonRequesterData
15+-- , singletonRequesterData
34− , matchResponsesWithRequests
16+-- , matchResponsesWithRequests
35− , multiEntry
17+-- , multiEntry
36− , unMultiEntry
18+-- , unMultiEntry
37− , requesting'
19+-- , requesting'
38 ) where
20 ) where
39
21
40−import Reflex.Class
22+import Reflex.Requester.Base.Internal
41−import Reflex.Adjustable.Class
42−import Reflex.Dynamic
43−import Reflex.Host.Class
44−import Reflex.PerformEvent.Class
45−import Reflex.PostBuild.Class
46−import Reflex.Requester.Class
47−import Reflex.TriggerEvent.Class
48−
49−import Control.Applicative (liftA2)
50−import Control.Monad.Exception
51−import Control.Monad.Identity
52−import Control.Monad.Primitive
53−import Control.Monad.Reader
54−import Control.Monad.Ref
55−import Control.Monad.State.Strict
56−import Data.Bits
57−import Data.Coerce
58−import Data.Dependent.Map (DMap, DSum (..))
59−import qualified Data.Dependent.Map as DMap
60−import Data.Functor.Compose
61−import Data.Functor.Misc
62−import Data.IntMap.Strict (IntMap)
63−import qualified Data.IntMap.Strict as IntMap
64−import Data.Map (Map)
65−import qualified Data.Map as Map
66−import Data.Monoid ((<>))
67−import Data.Proxy
68−import qualified Data.Semigroup as S
69−import Data.Some (Some(Some))
70−import Data.Type.Equality
71−import Data.Unique.Tag
72−
73−import GHC.Exts (Any)
74−import Unsafe.Coerce
75−
76−--TODO: Make this module type-safe
77−
78−newtype TagMap (f :: * -> *) = TagMap (IntMap Any)
79−
80−newtype RequesterData f = RequesterData (TagMap (Entry f))
81−
82−data RequesterDataKey a where
83− RequesterDataKey_Single :: {-# UNPACK #-} !(MyTag (Single a)) -> RequesterDataKey a
84− RequesterDataKey_Multi :: {-# UNPACK #-} !(MyTag Multi) -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a --TODO: Don't put a second Int here (or in the other Multis); use a single Int instead
85− RequesterDataKey_Multi2 :: {-# UNPACK #-} !(MyTag (Multi2 k)) -> !(Some k) -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a
86− RequesterDataKey_Multi3 :: {-# UNPACK #-} !(MyTag Multi3) -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a
87−
88−singletonRequesterData :: RequesterDataKey a -> f a -> RequesterData f
89−singletonRequesterData rdk v = case rdk of
90− RequesterDataKey_Single k -> RequesterData $ singletonTagMap k $ Entry v
91− RequesterDataKey_Multi k k' k'' -> RequesterData $ singletonTagMap k $ Entry $ IntMap.singleton k' $ singletonRequesterData k'' v
92− RequesterDataKey_Multi2 k k' k'' k''' -> RequesterData $ singletonTagMap k $ Entry $ Map.singleton k' $ IntMap.singleton k'' $ singletonRequesterData k''' v
93− RequesterDataKey_Multi3 k k' k'' k''' -> RequesterData $ singletonTagMap k $ Entry $ IntMap.singleton k' $ IntMap.singleton k'' $ singletonRequesterData k''' v
94−
95−requesterDataToList :: RequesterData f -> [DSum RequesterDataKey f]
96−requesterDataToList (RequesterData m) = do
97− k :=> Entry e <- tagMapToList m
98− case myKeyType k of
99− MyTagType_Single -> return $ RequesterDataKey_Single k :=> e
100− MyTagType_Multi -> do
101− (k', e') <- IntMap.toList e
102− k'' :=> e'' <- requesterDataToList e'
103− return $ RequesterDataKey_Multi k k' k'' :=> e''
104− MyTagType_Multi2 -> do
105− (k', e') <- Map.toList e
106− (k'', e'') <- IntMap.toList e'
107− k''' :=> e''' <- requesterDataToList e''
108− return $ RequesterDataKey_Multi2 k k' k'' k''' :=> e'''
109− MyTagType_Multi3 -> do
110− (k', e') <- IntMap.toList e
111− (k'', e'') <- IntMap.toList e'
112− k''' :=> e''' <- requesterDataToList e''
113− return $ RequesterDataKey_Multi3 k k' k'' k''' :=> e'''
114−
115−singletonTagMap :: forall f a. MyTag a -> f a -> TagMap f
116−singletonTagMap (MyTag k) v = TagMap $ IntMap.singleton k $ (unsafeCoerce :: f a -> Any) v
117−
118−tagMapToList :: forall f. TagMap f -> [DSum MyTag f]
119−tagMapToList (TagMap m) = f <$> IntMap.toList m
120− where f :: (Int, Any) -> DSum MyTag f
121− f (k, v) = MyTag k :=> (unsafeCoerce :: Any -> f a) v
122−
123−traverseTagMapWithKey :: forall t f g. Applicative t => (forall a. MyTag a -> f a -> t (g a)) -> TagMap f -> t (TagMap g)
124−traverseTagMapWithKey f (TagMap m) = TagMap <$> IntMap.traverseWithKey g m
125− where
126− g :: Int -> Any -> t Any
127− g k v = (unsafeCoerce :: g a -> Any) <$> f (MyTag k) ((unsafeCoerce :: Any -> f a) v)
128−
129−-- | Runs in reverse to accommodate for the fact that we accumulate it in reverse
130−traverseRequesterData :: forall m request response. Applicative m => (forall a. request a -> m (response a)) -> RequesterData request -> m (RequesterData response)
131−traverseRequesterData f (RequesterData m) = RequesterData <$> traverseTagMapWithKey go m --TODO: reverse this, since our tags are in reverse order
132− where go :: forall x. MyTag x -> Entry request x -> m (Entry response x)
133− go k (Entry request) = Entry <$> case myKeyType k of
134− MyTagType_Single -> f request
135− MyTagType_Multi -> traverse (traverseRequesterData f) request
136− MyTagType_Multi2 -> traverse (traverse (traverseRequesterData f)) request
137− MyTagType_Multi3 -> traverse (traverse (traverseRequesterData f)) request
138−
139−-- | 'traverseRequesterData' with its arguments flipped
140−forRequesterData :: forall request response m. Applicative m => RequesterData request -> (forall a. request a -> m (response a)) -> m (RequesterData response)
141−forRequesterData r f = traverseRequesterData f r
142−
143−data MyTagType :: * -> * where
144− MyTagType_Single :: MyTagType (Single a)
145− MyTagType_Multi :: MyTagType Multi
146− MyTagType_Multi2 :: MyTagType (Multi2 k)
147− MyTagType_Multi3 :: MyTagType Multi3
148−
149−myKeyType :: MyTag x -> MyTagType x
150−myKeyType (MyTag k) = case k .&. 0x3 of
151− 0x0 -> unsafeCoerce MyTagType_Single
152− 0x1 -> unsafeCoerce MyTagType_Multi
153− 0x2 -> unsafeCoerce MyTagType_Multi2
154− 0x3 -> unsafeCoerce MyTagType_Multi3
155− t -> error $ "Reflex.Requester.Base.myKeyType: no such key type" <> show t
156−
157−data Single a
158−data Multi
159−data Multi2 (k :: * -> *)
160−data Multi3
161−
162−class MyTagTypeOffset x where
163− myTagTypeOffset :: proxy x -> Int
164−
165−instance MyTagTypeOffset (Single a) where
166− myTagTypeOffset _ = 0x0
167−
168−instance MyTagTypeOffset Multi where
169− myTagTypeOffset _ = 0x1
170−
171−instance MyTagTypeOffset (Multi2 k) where
172− myTagTypeOffset _ = 0x2
173−
174−instance MyTagTypeOffset Multi3 where
175− myTagTypeOffset _ = 0x3
176−
177−type family EntryContents request a where
178− EntryContents request (Single a) = request a
179− EntryContents request Multi = IntMap (RequesterData request)
180− EntryContents request (Multi2 k) = Map (Some k) (IntMap (RequesterData request))
181− EntryContents request Multi3 = IntMap (IntMap (RequesterData request))
182−
183−newtype Entry request x = Entry { unEntry :: EntryContents request x }
184−
185−{-# INLINE singleEntry #-}
186−singleEntry :: f a -> Entry f (Single a)
187−singleEntry = Entry
188−
189−{-# INLINE multiEntry #-}
190−multiEntry :: IntMap (RequesterData f) -> Entry f Multi
191−multiEntry = Entry
192−
193−{-# INLINE unMultiEntry #-}
194−unMultiEntry :: Entry f Multi -> IntMap (RequesterData f)
195−unMultiEntry = unEntry
196−
197−-- | We use a hack here to pretend we have x ~ request a; we don't want to use a GADT, because GADTs (even with zero-size existential contexts) can't be newtypes
198−-- WARNING: This type should never be exposed. In particular, this is extremely unsound if a MyTag from one run of runRequesterT is ever compared against a MyTag from another
199−newtype MyTag x = MyTag Int deriving (Show, Eq, Ord, Enum)
200−
201−newtype MyTagWrap (f :: * -> *) x = MyTagWrap Int deriving (Show, Eq, Ord, Enum)
202−
203−{-# INLINE castMyTagWrap #-}
204−castMyTagWrap :: MyTagWrap f (Entry f x) -> MyTagWrap g (Entry g x)
205−castMyTagWrap = coerce
206−
207−instance GEq MyTag where
208− (MyTag a) `geq` (MyTag b) =
209− if a == b
210− then Just $ unsafeCoerce Refl
211− else Nothing
212−
213−instance GCompare MyTag where
214− (MyTag a) `gcompare` (MyTag b) =
215− case a `compare` b of
216− LT -> GLT
217− EQ -> unsafeCoerce GEQ
218− GT -> GGT
219−
220−instance GEq (MyTagWrap f) where
221− (MyTagWrap a) `geq` (MyTagWrap b) =
222− if a == b
223− then Just $ unsafeCoerce Refl
224− else Nothing
225−
226−instance GCompare (MyTagWrap f) where
227− (MyTagWrap a) `gcompare` (MyTagWrap b) =
228− case a `compare` b of
229− LT -> GLT
230− EQ -> unsafeCoerce GEQ
231− GT -> GGT
232−
233−data RequesterState t (request :: * -> *) = RequesterState
234− { _requesterState_nextMyTag :: {-# UNPACK #-} !Int -- Starts at -4 and goes down by 4 each time, to accommodate two 'type' bits at the bottom
235− , _requesterState_requests :: ![(Int, Event t Any)]
236− }
237−
238−-- | A basic implementation of 'Requester'.
239−newtype RequesterT t request (response :: * -> *) m a = RequesterT { unRequesterT :: StateT (RequesterState t request) (ReaderT (EventSelectorInt t Any) m) a }
240− deriving (Functor, Applicative, Monad, MonadFix, MonadIO, MonadException
241−-- MonadAsyncException can't be derived on ghc-8.0.1; we use base-4.9.1 as a proxy for ghc-8.0.2
242−#if MIN_VERSION_base(4,9,1)
243− , MonadAsyncException
244−#endif
245− )
246−
247−deriving instance MonadSample t m => MonadSample t (RequesterT t request response m)
248−deriving instance MonadHold t m => MonadHold t (RequesterT t request response m)
249−deriving instance PostBuild t m => PostBuild t (RequesterT t request response m)
250−deriving instance TriggerEvent t m => TriggerEvent t (RequesterT t request response m)
251−
252−instance PrimMonad m => PrimMonad (RequesterT t request response m) where
253− type PrimState (RequesterT t request response m) = PrimState m
254− primitive = lift . primitive
255−
256−-- TODO: Monoid and Semigroup can likely be derived once StateT has them.
257−instance (Monoid a, Monad m) => Monoid (RequesterT t request response m a) where
258− mempty = pure mempty
259− mappend = liftA2 mappend
260−
261−instance (S.Semigroup a, Monad m) => S.Semigroup (RequesterT t request response m a) where
262− (<>) = liftA2 (S.<>)
263−
264−
265−-- | Run a 'RequesterT' action. The resulting 'Event' will fire whenever
266−-- requests are made, and responses should be provided in the input 'Event'.
267−-- The 'Tag' keys will be used to return the responses to the same place the
268−-- requests were issued.
269−runRequesterT :: (Reflex t, Monad m)
270− => RequesterT t request response m a
271− -> Event t (RequesterData response) --TODO: This DMap will be in reverse order, so we need to make sure the caller traverses it in reverse
272− -> m (a, Event t (RequesterData request)) --TODO: we need to hide these 'MyTag's here, because they're unsafe to mix in the wild
273−runRequesterT (RequesterT a) responses = do
274− (result, s) <- runReaderT (runStateT a $ RequesterState (-4) []) $ fanInt $
275− coerceEvent responses
276− return (result, fmapCheap (RequesterData . TagMap) $ mergeInt $ IntMap.fromDistinctAscList $ _requesterState_requests s)
277−
278−-- | Map a function over the request and response of a 'RequesterT'
279−withRequesterT
280− :: (Reflex t, MonadFix m)
281− => (forall x. req x -> req' x) -- ^ The function to map over the request
282− -> (forall x. rsp' x -> rsp x) -- ^ The function to map over the response
283− -> RequesterT t req rsp m a -- ^ The internal 'RequesterT' whose input and output will be transformed
284− -> RequesterT t req' rsp' m a -- ^ The resulting 'RequesterT'
285−withRequesterT freq frsp child = do
286− rec let rsp = fmap (runIdentity . traverseRequesterData (Identity . frsp)) rsp'
287− (a, req) <- lift $ runRequesterT child rsp
288− rsp' <- fmap (flip selectInt 0 . fanInt . fmapCheap unMultiEntry) $ requesting' $
289− fmapCheap (multiEntry . IntMap.singleton 0) $ fmap (runIdentity . traverseRequesterData (Identity . freq)) req
290− return a
291−
292−instance (Reflex t, Monad m) => Requester t (RequesterT t request response m) where
293− type Request (RequesterT t request response m) = request
294− type Response (RequesterT t request response m) = response
295− requesting = fmap coerceEvent . responseFromTag . castMyTagWrap <=< tagRequest . (coerceEvent :: Event t (request a) -> Event t (Entry request (Single a)))
296− requesting_ = void . tagRequest . fmapCheap singleEntry
297−
298−{-# INLINE tagRequest #-}
299−tagRequest :: forall m x t request response. (Monad m, MyTagTypeOffset x) => Event t (Entry request x) -> RequesterT t request response m (MyTagWrap request (Entry request x))
300−tagRequest req = do
301− old <- RequesterT get
302− let n = _requesterState_nextMyTag old .|. myTagTypeOffset (Proxy :: Proxy x)
303− t = MyTagWrap n
304− RequesterT $ put $ RequesterState
305− { _requesterState_nextMyTag = _requesterState_nextMyTag old - 0x4
306− , _requesterState_requests = (n, (unsafeCoerce :: Event t (Entry request x) -> Event t Any) req) : _requesterState_requests old
307− }
308− return t
309−
310−{-# INLINE responseFromTag #-}
311−responseFromTag :: Monad m => MyTagWrap response (Entry response x) -> RequesterT t request response m (Event t (Entry response x))
312−responseFromTag (MyTagWrap t) = do
313− responses :: EventSelectorInt t Any <- RequesterT ask
314− return $ (unsafeCoerce :: Event t Any -> Event t (Entry response x)) $ selectInt responses t
315−
316−instance MonadTrans (RequesterT t request response) where
317− lift = RequesterT . lift . lift
318−
319−instance PerformEvent t m => PerformEvent t (RequesterT t request response m) where
320− type Performable (RequesterT t request response m) = Performable m
321− performEvent_ = lift . performEvent_
322− performEvent = lift . performEvent
323−
324−instance MonadRef m => MonadRef (RequesterT t request response m) where
325− type Ref (RequesterT t request response m) = Ref m
326− newRef = lift . newRef
327− readRef = lift . readRef
328− writeRef r = lift . writeRef r
329−
330−instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (RequesterT t request response m) where
331− newEventWithTrigger = lift . newEventWithTrigger
332− newFanEventWithTrigger f = lift $ newFanEventWithTrigger f
333−
334−instance MonadReader r m => MonadReader r (RequesterT t request response m) where
335− ask = lift ask
336− local f (RequesterT a) = RequesterT $ mapStateT (mapReaderT $ local f) a
337− reader = lift . reader
338−
339−instance (Reflex t, Adjustable t m, MonadHold t m, MonadFix m) => Adjustable t (RequesterT t request response m) where
340− runWithReplace = runWithReplaceRequesterTWith $ \dm0 dm' -> lift $ runWithReplace dm0 dm'
341− traverseIntMapWithKeyWithAdjust = traverseIntMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseIntMapWithKeyWithAdjust f dm0 dm') patchIntMapNewElementsMap mergeIntIncremental
342− {-# INLINABLE traverseDMapWithKeyWithAdjust #-}
343− traverseDMapWithKeyWithAdjust = traverseDMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseDMapWithKeyWithAdjust f dm0 dm') mapPatchDMap weakenPatchDMapWith patchMapNewElementsMap mergeMapIncremental
344− traverseDMapWithKeyWithAdjustWithMove = traverseDMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseDMapWithKeyWithAdjustWithMove f dm0 dm') mapPatchDMapWithMove weakenPatchDMapWithMoveWith patchMapWithMoveNewElementsMap mergeMapIncrementalWithMove
345−
346−requesting' :: (MyTagTypeOffset x, Monad m) => Event t (Entry request x) -> RequesterT t request response m (Event t (Entry response x))
347−requesting' = responseFromTag . castMyTagWrap <=< tagRequest
348−
349−{-# INLINABLE runWithReplaceRequesterTWith #-}
350−runWithReplaceRequesterTWith :: forall m t request response a b. (Reflex t, MonadHold t m
351− , MonadFix m
352− )
353− => (forall a' b'. m a' -> Event t (m b') -> RequesterT t request response m (a', Event t b'))
354− -> RequesterT t request response m a
355− -> Event t (RequesterT t request response m b)
356− -> RequesterT t request response m (a, Event t b)
357−runWithReplaceRequesterTWith f a0 a' = do
358− rec na' <- numberOccurrencesFrom 1 a'
359− responses <- fmap (fmapCheap unMultiEntry) $ requesting' $ fmapCheap multiEntry $ switchPromptlyDyn requests --TODO: Investigate whether we can really get rid of the prompt stuff here
360− let responses' = fanInt responses
361− ((result0, requests0), v') <- f (runRequesterT a0 (selectInt responses' 0)) $ fmapCheap (\(n, a) -> fmap ((,) n) $ runRequesterT a $ selectInt responses' n) na'
362− requests <- holdDyn (fmapCheap (IntMap.singleton 0) requests0) $ fmapCheap (\(n, (_, reqs)) -> fmapCheap (IntMap.singleton n) reqs) v'
363− return (result0, fmapCheap (fst . snd) v')
364−
365−{-# INLINE traverseIntMapWithKeyWithAdjustRequesterTWith #-}
366−traverseIntMapWithKeyWithAdjustRequesterTWith :: forall t request response m v v' p.
367− ( Reflex t
368− , MonadHold t m
369− , PatchTarget (p (Event t (IntMap (RequesterData request)))) ~ IntMap (Event t (IntMap (RequesterData request)))
370− , Patch (p (Event t (IntMap (RequesterData request))))
371− , Functor p
372− , MonadFix m
373− )
374− => ( (IntMap.Key -> (IntMap.Key, v) -> m (Event t (IntMap (RequesterData request)), v'))
375− -> IntMap (IntMap.Key, v)
376− -> Event t (p (IntMap.Key, v))
377− -> RequesterT t request response m (IntMap (Event t (IntMap (RequesterData request)), v'), Event t (p (Event t (IntMap (RequesterData request)), v')))
378− )
379− -> (p (Event t (IntMap (RequesterData request))) -> IntMap (Event t (IntMap (RequesterData request))))
380− -> (Incremental t (p (Event t (IntMap (RequesterData request)))) -> Event t (IntMap (IntMap (RequesterData request))))
381− -> (IntMap.Key -> v -> RequesterT t request response m v')
382− -> IntMap v
383− -> Event t (p v)
384− -> RequesterT t request response m (IntMap v', Event t (p v'))
385−traverseIntMapWithKeyWithAdjustRequesterTWith base patchNewElements mergePatchIncremental f dm0 dm' = do
386− rec response <- requesting' $ fmapCheap pack $ promptRequests `mappend` mergePatchIncremental requests --TODO: Investigate whether we can really get rid of the prompt stuff here
387− let responses :: EventSelectorInt t (IntMap (RequesterData response))
388− responses = fanInt $ fmapCheap unpack response
389− unpack :: Entry response Multi3 -> IntMap (IntMap (RequesterData response))
390− unpack = unEntry
391− pack :: IntMap (IntMap (RequesterData request)) -> Entry request Multi3
392− pack = Entry
393− f' :: IntMap.Key -> (Int, v) -> m (Event t (IntMap (RequesterData request)), v')
394− f' k (n, v) = do
395− (result, myRequests) <- runRequesterT (f k v) $ mapMaybeCheap (IntMap.lookup n) $ selectInt responses k --TODO: Instead of doing mapMaybeCheap, can we share a fanInt across all instances of a given key, or at least the ones that are adjacent in time?
396− return (fmapCheap (IntMap.singleton n) myRequests, result)
397− ndm' <- numberOccurrencesFrom 1 dm'
398− (children0, children') <- base f' (fmap ((,) 0) dm0) $ fmap (\(n, dm) -> fmap ((,) n) dm) ndm' --TODO: Avoid this somehow, probably by adding some sort of per-cohort information passing to Adjustable
399− let result0 = fmap snd children0
400− result' = fforCheap children' $ fmap snd
401− requests0 :: IntMap (Event t (IntMap (RequesterData request)))
402− requests0 = fmap fst children0
403− requests' :: Event t (p (Event t (IntMap (RequesterData request))))
404− requests' = fforCheap children' $ fmap fst
405− promptRequests :: Event t (IntMap (IntMap (RequesterData request)))
406− promptRequests = coincidence $ fmapCheap (mergeInt . patchNewElements) requests' --TODO: Create a mergeIncrementalPromptly, and use that to eliminate this 'coincidence'
407− requests <- holdIncremental requests0 requests'
408− return (result0, result')
409−
410−{-# INLINE traverseDMapWithKeyWithAdjustRequesterTWith #-}
411−traverseDMapWithKeyWithAdjustRequesterTWith :: forall k t request response m v v' p p'.
412− ( GCompare k
413− , Reflex t
414− , MonadHold t m
415− , PatchTarget (p' (Some k) (Event t (IntMap (RequesterData request)))) ~ Map (Some k) (Event t (IntMap (RequesterData request)))
416− , Patch (p' (Some k) (Event t (IntMap (RequesterData request))))
417− , MonadFix m
418− )
419− => (forall k' v1 v2. GCompare k'
420− => (forall a. k' a -> v1 a -> m (v2 a))
421− -> DMap k' v1
422− -> Event t (p k' v1)
423− -> RequesterT t request response m (DMap k' v2, Event t (p k' v2))
424− )
425− -> (forall v1 v2. (forall a. v1 a -> v2 a) -> p k v1 -> p k v2)
426− -> (forall v1 v2. (forall a. v1 a -> v2) -> p k v1 -> p' (Some k) v2)
427− -> (forall v2. p' (Some k) v2 -> Map (Some k) v2)
428− -> (forall a. Incremental t (p' (Some k) (Event t a)) -> Event t (Map (Some k) a))
429− -> (forall a. k a -> v a -> RequesterT t request response m (v' a))
430− -> DMap k v
431− -> Event t (p k v)
432− -> RequesterT t request response m (DMap k v', Event t (p k v'))
433−traverseDMapWithKeyWithAdjustRequesterTWith base mapPatch weakenPatchWith patchNewElements mergePatchIncremental f dm0 dm' = do
434− rec response <- requesting' $ fmapCheap pack $ promptRequests `mappend` mergePatchIncremental requests --TODO: Investigate whether we can really get rid of the prompt stuff here
435− let responses :: EventSelector t (Const2 (Some k) (IntMap (RequesterData response)))
436− responses = fanMap $ fmapCheap unpack response
437− unpack :: Entry response (Multi2 k) -> Map (Some k) (IntMap (RequesterData response))
438− unpack = unEntry
439− pack :: Map (Some k) (IntMap (RequesterData request)) -> Entry request (Multi2 k)
440− pack = Entry
441− f' :: forall a. k a -> Compose ((,) Int) v a -> m (Compose ((,) (Event t (IntMap (RequesterData request)))) v' a)
442− f' k (Compose (n, v)) = do
443− (result, myRequests) <- runRequesterT (f k v) $ mapMaybeCheap (IntMap.lookup n) $ select responses (Const2 (Some k))
444− return $ Compose (fmapCheap (IntMap.singleton n) myRequests, result)
445− ndm' <- numberOccurrencesFrom 1 dm'
446− (children0, children') <- base f' (DMap.map (\v -> Compose (0, v)) dm0) $ fmap (\(n, dm) -> mapPatch (\v -> Compose (n, v)) dm) ndm'
447− let result0 = DMap.map (snd . getCompose) children0
448− result' = fforCheap children' $ mapPatch $ snd . getCompose
449− requests0 :: Map (Some k) (Event t (IntMap (RequesterData request)))
450− requests0 = weakenDMapWith (fst . getCompose) children0
451− requests' :: Event t (p' (Some k) (Event t (IntMap (RequesterData request))))
452− requests' = fforCheap children' $ weakenPatchWith $ fst . getCompose
453− promptRequests :: Event t (Map (Some k) (IntMap (RequesterData request)))
454− promptRequests = coincidence $ fmapCheap (mergeMap . patchNewElements) requests' --TODO: Create a mergeIncrementalPromptly, and use that to eliminate this 'coincidence'
455− requests <- holdIncremental requests0 requests'
456− return (result0, result')
457−
458−data Decoder rawResponse response =
459− forall a. Decoder (RequesterDataKey a) (rawResponse -> response a)
460−
461−-- | Matches incoming responses with previously-sent requests
462−-- and uses the provided request "decoder" function to process
463−-- incoming responses.
464−matchResponsesWithRequests
465− :: forall t rawRequest rawResponse request response m.
466− ( MonadFix m
467− , MonadHold t m
468− , Reflex t
469− )
470− => (forall a. request a -> (rawRequest, rawResponse -> response a))
471− -- ^ Given a request (from 'Requester'), produces the wire format of the
472− -- request and a function used to process the associated response
473− -> Event t (RequesterData request)
474− -- ^ The outgoing requests
475− -> Event t (Int, rawResponse)
476− -- ^ The incoming responses, tagged by an identifying key
477− -> m ( Event t (Map Int rawRequest)
478− , Event t (RequesterData response)
479− )
480− -- ^ A map of outgoing wire-format requests and an event of responses keyed
481− -- by the 'RequesterData' key of the associated outgoing request
482−matchResponsesWithRequests f send recv = do
483− rec nextId <- hold 1 $ fmap (\(next, _, _) -> next) outgoing
484− waitingFor :: Incremental t (PatchMap Int (Decoder rawResponse response)) <-
485− holdIncremental mempty $ leftmost
486− [ fmap (\(_, outstanding, _) -> outstanding) outgoing
487− , snd <$> incoming
488− ]
489− let outgoing = processOutgoing nextId send
490− incoming = processIncoming waitingFor recv
491− return (fmap (\(_, _, rawReqs) -> rawReqs) outgoing, fst <$> incoming)
492− where
493− -- Tags each outgoing request with an identifying integer key
494− -- and returns the next available key, a map of response decoders
495− -- for requests for which there are outstanding responses, and the
496− -- raw requests to be sent out.
497− processOutgoing
498− :: Behavior t Int
499− -- The next available key
500− -> Event t (RequesterData request)
501− -- The outgoing request
502− -> Event t ( Int
503− , PatchMap Int (Decoder rawResponse response)
504− , Map Int rawRequest )
505− -- The new next-available-key, a map of requests expecting responses, and the tagged raw requests
506− processOutgoing nextId out = flip pushAlways out $ \dm -> do
507− oldNextId <- sample nextId
508− let (result, newNextId) = flip runState oldNextId $ forM (requesterDataToList dm) $ \(k :=> v) -> do
509− n <- get
510− put $ succ n
511− let (rawReq, rspF) = f v
512− return (n, rawReq, Decoder k rspF)
513− patchWaitingFor = PatchMap $ Map.fromList $
514− (\(n, _, dec) -> (n, Just dec)) <$> result
515− toSend = Map.fromList $ (\(n, rawReq, _) -> (n, rawReq)) <$> result
516− return (newNextId, patchWaitingFor, toSend)
517− -- Looks up the each incoming raw response in a map of response
518− -- decoders and returns the decoded response and a patch that can
519− -- be used to clear the ID of the consumed response out of the queue
520− -- of expected responses.
521− processIncoming
522− :: Incremental t (PatchMap Int (Decoder rawResponse response))
523− -- A map of outstanding expected responses
524− -> Event t (Int, rawResponse)
525− -- A incoming response paired with its identifying key
526− -> Event t (RequesterData response, PatchMap Int v)
527− -- The decoded response and a patch that clears the outstanding responses queue
528− processIncoming waitingFor inc = flip push inc $ \(n, rawRsp) -> do
529− wf <- sample $ currentIncremental waitingFor
530− case Map.lookup n wf of
531− Nothing -> return Nothing -- TODO How should lookup failures be handled here? They shouldn't ever happen..
532− Just (Decoder k rspF) -> do
533− let rsp = rspF rawRsp
534− return $ Just
535− ( singletonRequesterData k rsp
536− , PatchMap $ Map.singleton n Nothing
537− )
src/Reflex/Requester/Base/Internal.hsnew file+681
@@ -0,0 +1,681 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE EmptyDataDecls #-}
4+{-# LANGUAGE ExistentialQuantification #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
8+{-# LANGUAGE MultiParamTypeClasses #-}
9+{-# LANGUAGE Rank2Types #-}
10+{-# LANGUAGE RecursiveDo #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
12+{-# LANGUAGE StandaloneDeriving #-}
13+{-# LANGUAGE TypeFamilies #-}
14+{-# LANGUAGE TypeOperators #-}
15+{-# LANGUAGE UndecidableInstances #-}
16+{-# LANGUAGE InstanceSigs #-}
17+#ifdef USE_REFLEX_OPTIMIZER
18+{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
19+#endif
20+module Reflex.Requester.Base.Internal where
21+
22+import Reflex.Class
23+import Reflex.Adjustable.Class
24+import Reflex.Dynamic
25+import Reflex.Host.Class
26+import Reflex.PerformEvent.Class
27+import Reflex.PostBuild.Class
28+import Reflex.Requester.Class
29+import Reflex.TriggerEvent.Class
30+import Reflex.EventWriter.Base
31+import Reflex.EventWriter.Class
32+
33+import Control.Applicative (liftA2)
34+import Control.Monad.Exception
35+import Control.Monad.Identity
36+import Control.Monad.Primitive
37+import Control.Monad.Reader
38+import Control.Monad.Ref
39+import Control.Monad.State.Strict
40+import Data.Bits
41+import Data.Coerce
42+import Data.Dependent.Map (DMap, DSum (..))
43+import qualified Data.Dependent.Map as DMap
44+import Data.Functor.Compose
45+import Data.Functor.Misc
46+import Data.IntMap.Strict (IntMap)
47+import qualified Data.IntMap.Strict as IntMap
48+import qualified Data.List.NonEmpty as NonEmpty
49+import Data.List.NonEmpty.Deferred (NonEmptyDeferred)
50+import qualified Data.List.NonEmpty.Deferred as NonEmptyDeferred
51+import Data.Map (Map)
52+import qualified Data.Map as Map
53+import Data.Monoid ((<>))
54+import Data.Proxy
55+import qualified Data.Semigroup as S
56+import Data.Some (Some(Some))
57+import Data.Type.Equality
58+import Data.Sequence (Seq)
59+import qualified Data.Sequence as Seq
60+import Data.TagMap (TagMap)
61+import qualified Data.TagMap as TagMap
62+import Reflex.FanTag
63+import Data.Unique.Tag.Local
64+import qualified Data.Unique.Tag as Global
65+import Data.GADT.Compare
66+import Data.Witherable
67+import Data.Foldable
68+
69+import Unsafe.Coerce
70+
71+import Debug.Trace
72+
73+data RequestData ps request = forall s. RequestData !(TagGen ps s) !(NonEmptyDeferred (RequestEnvelope s request))
74+data ResponseData ps response = forall s. ResponseData !(TagGen ps s) !(TagMap s response)
75+
76+traverseRequesterData :: forall m request response. Applicative m => (forall a. request a -> m (response a)) -> RequestData (PrimState m) request -> m (ResponseData (PrimState m) response)
77+traverseRequesterData f (RequestData tg es) = ResponseData tg . TagMap.fromList <$> wither g (NonEmpty.toList $ NonEmptyDeferred.toNonEmpty es)
78+ where g (RequestEnvelope mt req) = case mt of
79+ Just t -> Just . (t :=>) <$> f req
80+ Nothing -> Nothing <$ f req
81+
82+runRequesterT :: forall t request response m a
83+ . ( Reflex t
84+ , PrimMonad m
85+ , MonadFix m
86+ )
87+ => RequesterT t request response m a
88+ -> Event t (ResponseData (PrimState m) response)
89+ -> m (a, Event t (RequestData (PrimState m) request))
90+runRequesterT (RequesterT a) wrappedResponses = withRequesterInternalT $ \(requests :: Event t (NonEmptyDeferred (RequestEnvelope s request))) -> do
91+ (_, tg) <- RequesterInternalT ask
92+ result <- (unsafeCoerce :: RequesterInternalT FakeRequesterStatePhantom t request response m a -> RequesterInternalT s t request response m a) a
93+ let responses = fforMaybe wrappedResponses $ \(ResponseData tg' m) -> case tg `geq` tg' of
94+ Nothing -> trace ("runRequesterT: bad TagGen: expected " <> show tg <> " but got " <> show tg') Nothing --TODO: Warn somehow
95+ Just Refl -> Just m
96+ pure (responses, (result, fmapCheap (RequestData tg) requests))
97+
98+instance MonadTrans (RequesterInternalT s t request response) where
99+ lift = RequesterInternalT . lift . lift
100+
101+instance MonadTrans (RequesterT t request response) where
102+ lift a = RequesterT $ RequesterInternalT $ lift $ lift a
103+
104+-- | Run a 'RequesterT' action. The resulting 'Event' will fire whenever
105+-- requests are made, and responses should be provided in the input 'Event'.
106+-- The 'Tag' keys will be used to return the responses to the same place the
107+-- requests were issued.
108+withRequesterInternalT
109+ :: ( Reflex t
110+ , PrimMonad m
111+ , MonadFix m
112+ )
113+ => (forall s. Event t (NonEmptyDeferred (RequestEnvelope s request)) -> RequesterInternalT s t request response m (Event t (TagMap s response), a))
114+ -> m a
115+withRequesterInternalT f = withTagGen $ \tg -> do
116+ rec let RequesterInternalT a = f requests
117+ ((responses, result), requests) <- runEventWriterT $ runReaderT a (fanTag responses, tg)
118+ pure result
119+
120+data RequestEnvelope s request = forall a. RequestEnvelope {-# UNPACK #-} !(Maybe (Tag s a)) !(request a)
121+
122+-- This is because using forall ruins inlining
123+data FakeRequesterStatePhantom
124+
125+newtype RequesterT t (request :: * -> *) (response :: * -> *) m a = RequesterT { unRequesterT :: RequesterInternalT FakeRequesterStatePhantom t request response m a }
126+ deriving (Functor, Applicative, Monad, MonadFix, MonadIO, MonadException)
127+
128+newtype RequesterInternalT s t request response m a = RequesterInternalT { unRequesterInternalT :: ReaderT (EventSelectorTag t s response, TagGen (PrimState m) s) (EventWriterT t (NonEmptyDeferred (RequestEnvelope s request)) m) a }
129+ deriving
130+ ( Functor, Applicative, Monad, MonadFix, MonadIO, MonadException
131+#if MIN_VERSION_base(4,9,1)
132+ , MonadAsyncException
133+#endif
134+ )
135+
136+-- I don't think this can actually be supported without an unsafeCoerce
137+-- #if MIN_VERSION_base(4,9,1)
138+-- instance MonadAsyncException m => MonadAsyncException (RequesterT t request response m) where
139+-- mask f = RequesterT $ mask $ \unmask -> unRequesterT $ f $ \x -> RequesterT $ unmask $ unRequesterT x
140+-- #endif
141+
142+instance MonadSample t m => MonadSample t (RequesterT t request response m)
143+instance MonadHold t m => MonadHold t (RequesterT t request response m)
144+instance PostBuild t m => PostBuild t (RequesterT t request response m)
145+instance TriggerEvent t m => TriggerEvent t (RequesterT t request response m)
146+
147+instance MonadSample t m => MonadSample t (RequesterInternalT s t request response m)
148+instance MonadHold t m => MonadHold t (RequesterInternalT s t request response m)
149+instance PostBuild t m => PostBuild t (RequesterInternalT s t request response m)
150+instance TriggerEvent t m => TriggerEvent t (RequesterInternalT s t request response m)
151+
152+instance PrimMonad m => PrimMonad (RequesterT t request response m) where
153+ type PrimState (RequesterT t request response m) = PrimState m
154+ primitive = lift . primitive
155+
156+-- TODO: Monoid and Semigroup can likely be derived once StateT has them.
157+instance (Monoid a, Monad m) => Monoid (RequesterT t request response m a) where
158+ mempty = pure mempty
159+ mappend = liftA2 mappend
160+
161+instance (S.Semigroup a, Monad m) => S.Semigroup (RequesterT t request response m a) where
162+ (<>) = liftA2 (S.<>)
163+
164+instance PerformEvent t m => PerformEvent t (RequesterT t request response m) where
165+ type Performable (RequesterT t request response m) = Performable m
166+ performEvent_ = lift . performEvent_
167+ performEvent = lift . performEvent
168+
169+instance MonadRef m => MonadRef (RequesterT t request response m) where
170+ type Ref (RequesterT t request response m) = Ref m
171+ newRef = lift . newRef
172+ readRef = lift . readRef
173+ writeRef r = lift . writeRef r
174+
175+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (RequesterT t request response m) where
176+ newEventWithTrigger = lift . newEventWithTrigger
177+ newFanEventWithTrigger f = lift $ newFanEventWithTrigger f
178+
179+instance MonadReader r m => MonadReader r (RequesterT t request response m) where
180+ ask = lift ask
181+ local f (RequesterT a) = RequesterT $ RequesterInternalT $ mapReaderT (mapEventWriterT $ local f) $ unRequesterInternalT a
182+ reader = lift . reader
183+
184+instance (Reflex t, PrimMonad m) => Requester t (RequesterT t request response m) where
185+ type Request (RequesterT t request response m) = request
186+ type Response (RequesterT t request response m) = response
187+ requesting e = RequesterT $ requesting e
188+ requesting_ e = RequesterT $ requesting_ e
189+
190+instance (Reflex t, PrimMonad m) => Requester t (RequesterInternalT s t request response m) where
191+ type Request (RequesterInternalT s t request response m) = request
192+ type Response (RequesterInternalT s t request response m) = response
193+ requesting e = RequesterInternalT $ do
194+ (s, tg) <- ask
195+ t <- lift $ lift $ newTag tg
196+ tellEvent $ fmapCheap (NonEmptyDeferred.singleton . RequestEnvelope (Just t)) e
197+ pure $ selectTag s t
198+ requesting_ e = RequesterInternalT $ do
199+ tellEvent $ fmapCheap (NonEmptyDeferred.singleton . RequestEnvelope Nothing) e
200+
201+instance (Adjustable t m, MonadHold t m) => Adjustable t (RequesterInternalT s t request response m) where
202+ {-# INLINABLE runWithReplace #-}
203+ runWithReplace (RequesterInternalT a0) a' = RequesterInternalT $ runWithReplace a0 (coerceEvent a')
204+ {-# INLINABLE traverseIntMapWithKeyWithAdjust #-}
205+ traverseIntMapWithKeyWithAdjust f dm0 dm' = RequesterInternalT $ traverseIntMapWithKeyWithAdjust (coerce . f) dm0 dm'
206+ {-# INLINABLE traverseDMapWithKeyWithAdjust #-}
207+ traverseDMapWithKeyWithAdjust f dm0 dm' = RequesterInternalT $ traverseDMapWithKeyWithAdjust (coerce . f) dm0 dm'
208+ {-# INLINABLE traverseDMapWithKeyWithAdjustWithMove #-}
209+ traverseDMapWithKeyWithAdjustWithMove f dm0 dm' = RequesterInternalT $ traverseDMapWithKeyWithAdjustWithMove (coerce . f) dm0 dm'
210+
211+instance (Adjustable t m, MonadHold t m) => Adjustable t (RequesterT t request response m) where
212+ {-# INLINABLE runWithReplace #-}
213+ runWithReplace (RequesterT a0) a' = RequesterT $ runWithReplace a0 (fmapCheap unRequesterT a')
214+ {-# INLINABLE traverseIntMapWithKeyWithAdjust #-}
215+ traverseIntMapWithKeyWithAdjust f dm0 dm' = RequesterT $ traverseIntMapWithKeyWithAdjust (\k v -> unRequesterT $ f k v) dm0 dm'
216+ {-# INLINABLE traverseDMapWithKeyWithAdjust #-}
217+ traverseDMapWithKeyWithAdjust f dm0 dm' = RequesterT $ traverseDMapWithKeyWithAdjust (\k v -> unRequesterT $ f k v) dm0 dm'
218+ {-# INLINABLE traverseDMapWithKeyWithAdjustWithMove #-}
219+ traverseDMapWithKeyWithAdjustWithMove f dm0 dm' = RequesterT $ traverseDMapWithKeyWithAdjustWithMove (\k v -> unRequesterT $ f k v) dm0 dm'
220+
221+{-# INLINABLE runWithReplaceRequesterTWith #-}
222+runWithReplaceRequesterTWith :: forall m t request response a b. (Reflex t, MonadHold t m
223+ , MonadFix m
224+ )
225+ => (forall a' b'. m a' -> Event t (m b') -> RequesterT t request response m (a', Event t b'))
226+ -> RequesterT t request response m a
227+ -> Event t (RequesterT t request response m b)
228+ -> RequesterT t request response m (a, Event t b)
229+runWithReplaceRequesterTWith f (RequesterT a0) a' = RequesterT $ do
230+ pure undefined
231+
232+{-
233+import GHC.Exts (Any)
234+import Unsafe.Coerce
235+
236+--TODO: Make this module type-safe
237+
238+newtype TagMap (f :: * -> *) = TagMap (IntMap Any)
239+
240+newtype RequesterData f = RequesterData (TagMap (Entry f))
241+
242+data RequesterDataKey a where
243+ RequesterDataKey_Single :: {-# UNPACK #-} !(MyTag (Single a)) -> RequesterDataKey a
244+ RequesterDataKey_Multi :: {-# UNPACK #-} !(MyTag Multi) -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a --TODO: Don't put a second Int here (or in the other Multis); use a single Int instead
245+ RequesterDataKey_Multi2 :: {-# UNPACK #-} !(MyTag (Multi2 k)) -> !(Some k) -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a
246+ RequesterDataKey_Multi3 :: {-# UNPACK #-} !(MyTag Multi3) -> {-# UNPACK #-} !Int -> {-# UNPACK #-} !Int -> !(RequesterDataKey a) -> RequesterDataKey a
247+
248+singletonRequesterData :: RequesterDataKey a -> f a -> RequesterData f
249+singletonRequesterData rdk v = case rdk of
250+ RequesterDataKey_Single k -> RequesterData $ singletonTagMap k $ Entry v
251+ RequesterDataKey_Multi k k' k'' -> RequesterData $ singletonTagMap k $ Entry $ IntMap.singleton k' $ singletonRequesterData k'' v
252+ RequesterDataKey_Multi2 k k' k'' k''' -> RequesterData $ singletonTagMap k $ Entry $ Map.singleton k' $ IntMap.singleton k'' $ singletonRequesterData k''' v
253+ RequesterDataKey_Multi3 k k' k'' k''' -> RequesterData $ singletonTagMap k $ Entry $ IntMap.singleton k' $ IntMap.singleton k'' $ singletonRequesterData k''' v
254+
255+requesterDataToList :: RequesterData f -> [DSum RequesterDataKey f]
256+requesterDataToList (RequesterData m) = do
257+ k :=> Entry e <- tagMapToList m
258+ case myKeyType k of
259+ MyTagType_Single -> return $ RequesterDataKey_Single k :=> e
260+ MyTagType_Multi -> do
261+ (k', e') <- IntMap.toList e
262+ k'' :=> e'' <- requesterDataToList e'
263+ return $ RequesterDataKey_Multi k k' k'' :=> e''
264+ MyTagType_Multi2 -> do
265+ (k', e') <- Map.toList e
266+ (k'', e'') <- IntMap.toList e'
267+ k''' :=> e''' <- requesterDataToList e''
268+ return $ RequesterDataKey_Multi2 k k' k'' k''' :=> e'''
269+ MyTagType_Multi3 -> do
270+ (k', e') <- IntMap.toList e
271+ (k'', e'') <- IntMap.toList e'
272+ k''' :=> e''' <- requesterDataToList e''
273+ return $ RequesterDataKey_Multi3 k k' k'' k''' :=> e'''
274+
275+singletonTagMap :: forall f a. MyTag a -> f a -> TagMap f
276+singletonTagMap (MyTag k) v = TagMap $ IntMap.singleton k $ (unsafeCoerce :: f a -> Any) v
277+
278+tagMapToList :: forall f. TagMap f -> [DSum MyTag f]
279+tagMapToList (TagMap m) = f <$> IntMap.toList m
280+ where f :: (Int, Any) -> DSum MyTag f
281+ f (k, v) = MyTag k :=> (unsafeCoerce :: Any -> f a) v
282+
283+traverseTagMapWithKey :: forall t f g. Applicative t => (forall a. MyTag a -> f a -> t (g a)) -> TagMap f -> t (TagMap g)
284+traverseTagMapWithKey f (TagMap m) = TagMap <$> IntMap.traverseWithKey g m
285+ where
286+ g :: Int -> Any -> t Any
287+ g k v = (unsafeCoerce :: g a -> Any) <$> f (MyTag k) ((unsafeCoerce :: Any -> f a) v)
288+
289+-- | Runs in reverse to accommodate for the fact that we accumulate it in reverse
290+traverseRequesterData :: forall m request response. Applicative m => (forall a. request a -> m (response a)) -> RequesterData request -> m (RequesterData response)
291+traverseRequesterData f (RequesterData m) = RequesterData <$> traverseTagMapWithKey go m --TODO: reverse this, since our tags are in reverse order
292+ where go :: forall x. MyTag x -> Entry request x -> m (Entry response x)
293+ go k (Entry request) = Entry <$> case myKeyType k of
294+ MyTagType_Single -> f request
295+ MyTagType_Multi -> traverse (traverseRequesterData f) request
296+ MyTagType_Multi2 -> traverse (traverse (traverseRequesterData f)) request
297+ MyTagType_Multi3 -> traverse (traverse (traverseRequesterData f)) request
298+
299+-- | 'traverseRequesterData' with its arguments flipped
300+forRequesterData :: forall request response m. Applicative m => RequesterData request -> (forall a. request a -> m (response a)) -> m (RequesterData response)
301+forRequesterData r f = traverseRequesterData f r
302+
303+data MyTagType :: * -> * where
304+ MyTagType_Single :: MyTagType (Single a)
305+ MyTagType_Multi :: MyTagType Multi
306+ MyTagType_Multi2 :: MyTagType (Multi2 k)
307+ MyTagType_Multi3 :: MyTagType Multi3
308+
309+myKeyType :: MyTag x -> MyTagType x
310+myKeyType (MyTag k) = case k .&. 0x3 of
311+ 0x0 -> unsafeCoerce MyTagType_Single
312+ 0x1 -> unsafeCoerce MyTagType_Multi
313+ 0x2 -> unsafeCoerce MyTagType_Multi2
314+ 0x3 -> unsafeCoerce MyTagType_Multi3
315+ t -> error $ "Reflex.Requester.Base.myKeyType: no such key type" <> show t
316+
317+data Single a
318+data Multi
319+data Multi2 (k :: * -> *)
320+data Multi3
321+
322+class MyTagTypeOffset x where
323+ myTagTypeOffset :: proxy x -> Int
324+
325+instance MyTagTypeOffset (Single a) where
326+ myTagTypeOffset _ = 0x0
327+
328+instance MyTagTypeOffset Multi where
329+ myTagTypeOffset _ = 0x1
330+
331+instance MyTagTypeOffset (Multi2 k) where
332+ myTagTypeOffset _ = 0x2
333+
334+instance MyTagTypeOffset Multi3 where
335+ myTagTypeOffset _ = 0x3
336+
337+type family EntryContents request a where
338+ EntryContents request (Single a) = request a
339+ EntryContents request Multi = IntMap (RequesterData request)
340+ EntryContents request (Multi2 k) = Map (Some k) (IntMap (RequesterData request))
341+ EntryContents request Multi3 = IntMap (IntMap (RequesterData request))
342+
343+newtype Entry request x = Entry { unEntry :: EntryContents request x }
344+
345+{-# INLINE singleEntry #-}
346+singleEntry :: f a -> Entry f (Single a)
347+singleEntry = Entry
348+
349+{-# INLINE multiEntry #-}
350+multiEntry :: IntMap (RequesterData f) -> Entry f Multi
351+multiEntry = Entry
352+
353+{-# INLINE unMultiEntry #-}
354+unMultiEntry :: Entry f Multi -> IntMap (RequesterData f)
355+unMultiEntry = unEntry
356+
357+-- | We use a hack here to pretend we have x ~ request a; we don't want to use a GADT, because GADTs (even with zero-size existential contexts) can't be newtypes
358+-- WARNING: This type should never be exposed. In particular, this is extremely unsound if a MyTag from one run of runRequesterT is ever compared against a MyTag from another
359+newtype MyTag x = MyTag Int deriving (Show, Eq, Ord, Enum)
360+
361+newtype MyTagWrap (f :: * -> *) x = MyTagWrap Int deriving (Show, Eq, Ord, Enum)
362+
363+{-# INLINE castMyTagWrap #-}
364+castMyTagWrap :: MyTagWrap f (Entry f x) -> MyTagWrap g (Entry g x)
365+castMyTagWrap = coerce
366+
367+instance GEq MyTag where
368+ (MyTag a) `geq` (MyTag b) =
369+ if a == b
370+ then Just $ unsafeCoerce Refl
371+ else Nothing
372+
373+instance GCompare MyTag where
374+ (MyTag a) `gcompare` (MyTag b) =
375+ case a `compare` b of
376+ LT -> GLT
377+ EQ -> unsafeCoerce GEQ
378+ GT -> GGT
379+
380+instance GEq (MyTagWrap f) where
381+ (MyTagWrap a) `geq` (MyTagWrap b) =
382+ if a == b
383+ then Just $ unsafeCoerce Refl
384+ else Nothing
385+
386+instance GCompare (MyTagWrap f) where
387+ (MyTagWrap a) `gcompare` (MyTagWrap b) =
388+ case a `compare` b of
389+ LT -> GLT
390+ EQ -> unsafeCoerce GEQ
391+ GT -> GGT
392+
393+data RequesterState t (request :: * -> *) = RequesterState
394+ { _requesterState_nextMyTag :: {-# UNPACK #-} !Int -- Starts at -4 and goes down by 4 each time, to accommodate two 'type' bits at the bottom
395+ , _requesterState_requests :: ![(Int, Event t Any)]
396+ }
397+
398+-- | A basic implementation of 'Requester'.
399+newtype RequesterT t request (response :: * -> *) m a = RequesterT { unRequesterT :: StateT (RequesterState t request) (ReaderT (EventSelectorInt t Any) m) a }
400+ deriving (Functor, Applicative, Monad, MonadFix, MonadIO, MonadException
401+-- MonadAsyncException can't be derived on ghc-8.0.1; we use base-4.9.1 as a proxy for ghc-8.0.2
402+#if MIN_VERSION_base(4,9,1)
403+ , MonadAsyncException
404+#endif
405+ )
406+
407+
408+-- | Run a 'RequesterT' action. The resulting 'Event' will fire whenever
409+-- requests are made, and responses should be provided in the input 'Event'.
410+-- The 'Tag' keys will be used to return the responses to the same place the
411+-- requests were issued.
412+runRequesterT :: (Reflex t, Monad m)
413+ => RequesterT t request response m a
414+ -> Event t (RequesterData response) --TODO: This DMap will be in reverse order, so we need to make sure the caller traverses it in reverse
415+ -> m (a, Event t (RequesterData request)) --TODO: we need to hide these 'MyTag's here, because they're unsafe to mix in the wild
416+runRequesterT (RequesterT a) responses = do
417+ (result, s) <- runReaderT (runStateT a $ RequesterState (-4) []) $ fanInt $
418+ coerceEvent responses
419+ return (result, fmapCheap (RequesterData . TagMap) $ mergeInt $ IntMap.fromDistinctAscList $ _requesterState_requests s)
420+
421+-- | Map a function over the request and response of a 'RequesterT'
422+withRequesterT
423+ :: (Reflex t, MonadFix m)
424+ => (forall x. req x -> req' x) -- ^ The function to map over the request
425+ -> (forall x. rsp' x -> rsp x) -- ^ The function to map over the response
426+ -> RequesterT t req rsp m a -- ^ The internal 'RequesterT' whose input and output will be transformed
427+ -> RequesterT t req' rsp' m a -- ^ The resulting 'RequesterT'
428+withRequesterT freq frsp child = do
429+ rec let rsp = fmap (runIdentity . traverseRequesterData (Identity . frsp)) rsp'
430+ (a, req) <- lift $ runRequesterT child rsp
431+ rsp' <- fmap (flip selectInt 0 . fanInt . fmapCheap unMultiEntry) $ requesting' $
432+ fmapCheap (multiEntry . IntMap.singleton 0) $ fmap (runIdentity . traverseRequesterData (Identity . freq)) req
433+ return a
434+
435+instance (Reflex t, Monad m) => Requester t (RequesterT t request response m) where
436+ type Request (RequesterT t request response m) = request
437+ type Response (RequesterT t request response m) = response
438+ requesting = fmap coerceEvent . responseFromTag . castMyTagWrap <=< tagRequest . (coerceEvent :: Event t (request a) -> Event t (Entry request (Single a)))
439+ requesting_ = void . tagRequest . fmapCheap singleEntry
440+
441+{-# INLINE tagRequest #-}
442+tagRequest :: forall m x t request response. (Monad m, MyTagTypeOffset x) => Event t (Entry request x) -> RequesterT t request response m (MyTagWrap request (Entry request x))
443+tagRequest req = do
444+ old <- RequesterT get
445+ let n = _requesterState_nextMyTag old .|. myTagTypeOffset (Proxy :: Proxy x)
446+ t = MyTagWrap n
447+ RequesterT $ put $ RequesterState
448+ { _requesterState_nextMyTag = _requesterState_nextMyTag old - 0x4
449+ , _requesterState_requests = (n, (unsafeCoerce :: Event t (Entry request x) -> Event t Any) req) : _requesterState_requests old
450+ }
451+ return t
452+
453+{-# INLINE responseFromTag #-}
454+responseFromTag :: Monad m => MyTagWrap response (Entry response x) -> RequesterT t request response m (Event t (Entry response x))
455+responseFromTag (MyTagWrap t) = do
456+ responses :: EventSelectorInt t Any <- RequesterT ask
457+ return $ (unsafeCoerce :: Event t Any -> Event t (Entry response x)) $ selectInt responses t
458+
459+instance MonadTrans (RequesterT t request response) where
460+ lift = RequesterT . lift . lift
461+
462+instance PerformEvent t m => PerformEvent t (RequesterT t request response m) where
463+ type Performable (RequesterT t request response m) = Performable m
464+ performEvent_ = lift . performEvent_
465+ performEvent = lift . performEvent
466+
467+instance MonadRef m => MonadRef (RequesterT t request response m) where
468+ type Ref (RequesterT t request response m) = Ref m
469+ newRef = lift . newRef
470+ readRef = lift . readRef
471+ writeRef r = lift . writeRef r
472+
473+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (RequesterT t request response m) where
474+ newEventWithTrigger = lift . newEventWithTrigger
475+ newFanEventWithTrigger f = lift $ newFanEventWithTrigger f
476+
477+instance MonadReader r m => MonadReader r (RequesterT t request response m) where
478+ ask = lift ask
479+ local f (RequesterT a) = RequesterT $ mapStateT (mapReaderT $ local f) a
480+ reader = lift . reader
481+
482+instance (Reflex t, Adjustable t m, MonadHold t m, MonadFix m) => Adjustable t (RequesterT t request response m) where
483+ runWithReplace = runWithReplaceRequesterTWith $ \dm0 dm' -> lift $ runWithReplace dm0 dm'
484+ traverseIntMapWithKeyWithAdjust = traverseIntMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseIntMapWithKeyWithAdjust f dm0 dm') patchIntMapNewElementsMap mergeIntIncremental
485+ {-# INLINABLE traverseDMapWithKeyWithAdjust #-}
486+ traverseDMapWithKeyWithAdjust = traverseDMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseDMapWithKeyWithAdjust f dm0 dm') mapPatchDMap weakenPatchDMapWith patchMapNewElementsMap mergeMapIncremental
487+ traverseDMapWithKeyWithAdjustWithMove = traverseDMapWithKeyWithAdjustRequesterTWith (\f dm0 dm' -> lift $ traverseDMapWithKeyWithAdjustWithMove f dm0 dm') mapPatchDMapWithMove weakenPatchDMapWithMoveWith patchMapWithMoveNewElementsMap mergeMapIncrementalWithMove
488+
489+requesting' :: (MyTagTypeOffset x, Monad m) => Event t (Entry request x) -> RequesterT t request response m (Event t (Entry response x))
490+requesting' = responseFromTag . castMyTagWrap <=< tagRequest
491+
492+{-# INLINABLE runWithReplaceRequesterTWith #-}
493+runWithReplaceRequesterTWith :: forall m t request response a b. (Reflex t, MonadHold t m
494+ , MonadFix m
495+ )
496+ => (forall a' b'. m a' -> Event t (m b') -> RequesterT t request response m (a', Event t b'))
497+ -> RequesterT t request response m a
498+ -> Event t (RequesterT t request response m b)
499+ -> RequesterT t request response m (a, Event t b)
500+runWithReplaceRequesterTWith f a0 a' = do
501+ rec na' <- numberOccurrencesFrom 1 a'
502+ responses <- fmap (fmapCheap unMultiEntry) $ requesting' $ fmapCheap multiEntry $ switchPromptlyDyn requests --TODO: Investigate whether we can really get rid of the prompt stuff here
503+ let responses' = fanInt responses
504+ ((result0, requests0), v') <- f (runRequesterT a0 (selectInt responses' 0)) $ fmapCheap (\(n, a) -> fmap ((,) n) $ runRequesterT a $ selectInt responses' n) na'
505+ requests <- holdDyn (fmapCheap (IntMap.singleton 0) requests0) $ fmapCheap (\(n, (_, reqs)) -> fmapCheap (IntMap.singleton n) reqs) v'
506+ return (result0, fmapCheap (fst . snd) v')
507+
508+{-# INLINE traverseIntMapWithKeyWithAdjustRequesterTWith #-}
509+traverseIntMapWithKeyWithAdjustRequesterTWith :: forall t request response m v v' p.
510+ ( Reflex t
511+ , MonadHold t m
512+ , PatchTarget (p (Event t (IntMap (RequesterData request)))) ~ IntMap (Event t (IntMap (RequesterData request)))
513+ , Patch (p (Event t (IntMap (RequesterData request))))
514+ , Functor p
515+ , MonadFix m
516+ )
517+ => ( (IntMap.Key -> (IntMap.Key, v) -> m (Event t (IntMap (RequesterData request)), v'))
518+ -> IntMap (IntMap.Key, v)
519+ -> Event t (p (IntMap.Key, v))
520+ -> RequesterT t request response m (IntMap (Event t (IntMap (RequesterData request)), v'), Event t (p (Event t (IntMap (RequesterData request)), v')))
521+ )
522+ -> (p (Event t (IntMap (RequesterData request))) -> IntMap (Event t (IntMap (RequesterData request))))
523+ -> (Incremental t (p (Event t (IntMap (RequesterData request)))) -> Event t (IntMap (IntMap (RequesterData request))))
524+ -> (IntMap.Key -> v -> RequesterT t request response m v')
525+ -> IntMap v
526+ -> Event t (p v)
527+ -> RequesterT t request response m (IntMap v', Event t (p v'))
528+traverseIntMapWithKeyWithAdjustRequesterTWith base patchNewElements mergePatchIncremental f dm0 dm' = do
529+ rec response <- requesting' $ fmapCheap pack $ promptRequests `mappend` mergePatchIncremental requests --TODO: Investigate whether we can really get rid of the prompt stuff here
530+ let responses :: EventSelectorInt t (IntMap (RequesterData response))
531+ responses = fanInt $ fmapCheap unpack response
532+ unpack :: Entry response Multi3 -> IntMap (IntMap (RequesterData response))
533+ unpack = unEntry
534+ pack :: IntMap (IntMap (RequesterData request)) -> Entry request Multi3
535+ pack = Entry
536+ f' :: IntMap.Key -> (Int, v) -> m (Event t (IntMap (RequesterData request)), v')
537+ f' k (n, v) = do
538+ (result, myRequests) <- runRequesterT (f k v) $ mapMaybeCheap (IntMap.lookup n) $ selectInt responses k --TODO: Instead of doing mapMaybeCheap, can we share a fanInt across all instances of a given key, or at least the ones that are adjacent in time?
539+ return (fmapCheap (IntMap.singleton n) myRequests, result)
540+ ndm' <- numberOccurrencesFrom 1 dm'
541+ (children0, children') <- base f' (fmap ((,) 0) dm0) $ fmap (\(n, dm) -> fmap ((,) n) dm) ndm' --TODO: Avoid this somehow, probably by adding some sort of per-cohort information passing to Adjustable
542+ let result0 = fmap snd children0
543+ result' = fforCheap children' $ fmap snd
544+ requests0 :: IntMap (Event t (IntMap (RequesterData request)))
545+ requests0 = fmap fst children0
546+ requests' :: Event t (p (Event t (IntMap (RequesterData request))))
547+ requests' = fforCheap children' $ fmap fst
548+ promptRequests :: Event t (IntMap (IntMap (RequesterData request)))
549+ promptRequests = coincidence $ fmapCheap (mergeInt . patchNewElements) requests' --TODO: Create a mergeIncrementalPromptly, and use that to eliminate this 'coincidence'
550+ requests <- holdIncremental requests0 requests'
551+ return (result0, result')
552+
553+{-# INLINE traverseDMapWithKeyWithAdjustRequesterTWith #-}
554+traverseDMapWithKeyWithAdjustRequesterTWith :: forall k t request response m v v' p p'.
555+ ( GCompare k
556+ , Reflex t
557+ , MonadHold t m
558+ , PatchTarget (p' (Some k) (Event t (IntMap (RequesterData request)))) ~ Map (Some k) (Event t (IntMap (RequesterData request)))
559+ , Patch (p' (Some k) (Event t (IntMap (RequesterData request))))
560+ , MonadFix m
561+ )
562+ => (forall k' v1 v2. GCompare k'
563+ => (forall a. k' a -> v1 a -> m (v2 a))
564+ -> DMap k' v1
565+ -> Event t (p k' v1)
566+ -> RequesterT t request response m (DMap k' v2, Event t (p k' v2))
567+ )
568+ -> (forall v1 v2. (forall a. v1 a -> v2 a) -> p k v1 -> p k v2)
569+ -> (forall v1 v2. (forall a. v1 a -> v2) -> p k v1 -> p' (Some k) v2)
570+ -> (forall v2. p' (Some k) v2 -> Map (Some k) v2)
571+ -> (forall a. Incremental t (p' (Some k) (Event t a)) -> Event t (Map (Some k) a))
572+ -> (forall a. k a -> v a -> RequesterT t request response m (v' a))
573+ -> DMap k v
574+ -> Event t (p k v)
575+ -> RequesterT t request response m (DMap k v', Event t (p k v'))
576+traverseDMapWithKeyWithAdjustRequesterTWith base mapPatch weakenPatchWith patchNewElements mergePatchIncremental f dm0 dm' = do
577+ rec response <- requesting' $ fmapCheap pack $ promptRequests `mappend` mergePatchIncremental requests --TODO: Investigate whether we can really get rid of the prompt stuff here
578+ let responses :: EventSelector t (Const2 (Some k) (IntMap (RequesterData response)))
579+ responses = fanMap $ fmapCheap unpack response
580+ unpack :: Entry response (Multi2 k) -> Map (Some k) (IntMap (RequesterData response))
581+ unpack = unEntry
582+ pack :: Map (Some k) (IntMap (RequesterData request)) -> Entry request (Multi2 k)
583+ pack = Entry
584+ f' :: forall a. k a -> Compose ((,) Int) v a -> m (Compose ((,) (Event t (IntMap (RequesterData request)))) v' a)
585+ f' k (Compose (n, v)) = do
586+ (result, myRequests) <- runRequesterT (f k v) $ mapMaybeCheap (IntMap.lookup n) $ select responses (Const2 (Some k))
587+ return $ Compose (fmapCheap (IntMap.singleton n) myRequests, result)
588+ ndm' <- numberOccurrencesFrom 1 dm'
589+ (children0, children') <- base f' (DMap.map (\v -> Compose (0, v)) dm0) $ fmap (\(n, dm) -> mapPatch (\v -> Compose (n, v)) dm) ndm'
590+ let result0 = DMap.map (snd . getCompose) children0
591+ result' = fforCheap children' $ mapPatch $ snd . getCompose
592+ requests0 :: Map (Some k) (Event t (IntMap (RequesterData request)))
593+ requests0 = weakenDMapWith (fst . getCompose) children0
594+ requests' :: Event t (p' (Some k) (Event t (IntMap (RequesterData request))))
595+ requests' = fforCheap children' $ weakenPatchWith $ fst . getCompose
596+ promptRequests :: Event t (Map (Some k) (IntMap (RequesterData request)))
597+ promptRequests = coincidence $ fmapCheap (mergeMap . patchNewElements) requests' --TODO: Create a mergeIncrementalPromptly, and use that to eliminate this 'coincidence'
598+ requests <- holdIncremental requests0 requests'
599+ return (result0, result')
600+
601+data Decoder rawResponse response =
602+ forall a. Decoder (RequesterDataKey a) (rawResponse -> response a)
603+
604+-- | Matches incoming responses with previously-sent requests
605+-- and uses the provided request "decoder" function to process
606+-- incoming responses.
607+matchResponsesWithRequests
608+ :: forall t rawRequest rawResponse request response m.
609+ ( MonadFix m
610+ , MonadHold t m
611+ , Reflex t
612+ )
613+ => (forall a. request a -> (rawRequest, rawResponse -> response a))
614+ -- ^ Given a request (from 'Requester'), produces the wire format of the
615+ -- request and a function used to process the associated response
616+ -> Event t (RequesterData request)
617+ -- ^ The outgoing requests
618+ -> Event t (Int, rawResponse)
619+ -- ^ The incoming responses, tagged by an identifying key
620+ -> m ( Event t (Map Int rawRequest)
621+ , Event t (RequesterData response)
622+ )
623+ -- ^ A map of outgoing wire-format requests and an event of responses keyed
624+ -- by the 'RequesterData' key of the associated outgoing request
625+matchResponsesWithRequests f send recv = do
626+ rec nextId <- hold 1 $ fmap (\(next, _, _) -> next) outgoing
627+ waitingFor :: Incremental t (PatchMap Int (Decoder rawResponse response)) <-
628+ holdIncremental mempty $ leftmost
629+ [ fmap (\(_, outstanding, _) -> outstanding) outgoing
630+ , snd <$> incoming
631+ ]
632+ let outgoing = processOutgoing nextId send
633+ incoming = processIncoming waitingFor recv
634+ return (fmap (\(_, _, rawReqs) -> rawReqs) outgoing, fst <$> incoming)
635+ where
636+ -- Tags each outgoing request with an identifying integer key
637+ -- and returns the next available key, a map of response decoders
638+ -- for requests for which there are outstanding responses, and the
639+ -- raw requests to be sent out.
640+ processOutgoing
641+ :: Behavior t Int
642+ -- The next available key
643+ -> Event t (RequesterData request)
644+ -- The outgoing request
645+ -> Event t ( Int
646+ , PatchMap Int (Decoder rawResponse response)
647+ , Map Int rawRequest )
648+ -- The new next-available-key, a map of requests expecting responses, and the tagged raw requests
649+ processOutgoing nextId out = flip pushAlways out $ \dm -> do
650+ oldNextId <- sample nextId
651+ let (result, newNextId) = flip runState oldNextId $ forM (requesterDataToList dm) $ \(k :=> v) -> do
652+ n <- get
653+ put $ succ n
654+ let (rawReq, rspF) = f v
655+ return (n, rawReq, Decoder k rspF)
656+ patchWaitingFor = PatchMap $ Map.fromList $
657+ (\(n, _, dec) -> (n, Just dec)) <$> result
658+ toSend = Map.fromList $ (\(n, rawReq, _) -> (n, rawReq)) <$> result
659+ return (newNextId, patchWaitingFor, toSend)
660+ -- Looks up the each incoming raw response in a map of response
661+ -- decoders and returns the decoded response and a patch that can
662+ -- be used to clear the ID of the consumed response out of the queue
663+ -- of expected responses.
664+ processIncoming
665+ :: Incremental t (PatchMap Int (Decoder rawResponse response))
666+ -- A map of outstanding expected responses
667+ -> Event t (Int, rawResponse)
668+ -- A incoming response paired with its identifying key
669+ -> Event t (RequesterData response, PatchMap Int v)
670+ -- The decoded response and a patch that clears the outstanding responses queue
671+ processIncoming waitingFor inc = flip push inc $ \(n, rawRsp) -> do
672+ wf <- sample $ currentIncremental waitingFor
673+ case Map.lookup n wf of
674+ Nothing -> return Nothing -- TODO How should lookup failures be handled here? They shouldn't ever happen..
675+ Just (Decoder k rspF) -> do
676+ let rsp = rspF rawRsp
677+ return $ Just
678+ ( singletonRequesterData k rsp
679+ , PatchMap $ Map.singleton n Nothing
680+ )
681+-}
src/Reflex/Requester/Class.hs+1−1
@@ -40,7 +40,7 @@ class (Reflex t, Monad m) => Requester t m | m -> t where
40 requesting :: Event t (Request m a) -> m (Event t (Response m a))
40 requesting :: Event t (Request m a) -> m (Event t (Response m a))
41 -- | Emit a request whenever the given 'Event' fires, and ignore all responses.
41 -- | Emit a request whenever the given 'Event' fires, and ignore all responses.
42 requesting_ :: Event t (Request m a) -> m ()
42 requesting_ :: Event t (Request m a) -> m ()
43−
43+--TODO: Make Requester polykinded, where Request m and Response m both take an argument of the same kind
44
44
45instance Requester t m => Requester t (ReaderT r m) where
45instance Requester t m => Requester t (ReaderT r m) where
46 type Request (ReaderT r m) = Request m
46 type Request (ReaderT r m) = Request m
test/EventWriterT.hs+15−11
@@ -24,26 +24,30 @@ import Test.Run
24
24
25main :: IO ()
25main :: IO ()
26main = do
26main = do
27− os1@[[Just [10,9,8,7,6,5,4,3,2,1]]] <- runApp' (unwrapApp testOrdering) $
27+ os1 <- runApp' (unwrapApp testOrdering) $
28 [ Just ()
28 [ Just ()
29 ]
29 ]
30 print os1
30 print os1
31− os2@[[Just [1,3,5,7,9]],[Nothing,Nothing],[Just [2,4,6,8,10]],[Just [2,4,6,8,10],Nothing]]
32− <- runApp' (unwrapApp testSimultaneous) $ map Just $
31+ os2 <- runApp' (unwrapApp testSimultaneous) $ map Just $
33− [ This ()
32+ [ This ()
34− , That ()
33+ , That ()
35− , This ()
34+ , This ()
36− , These () ()
35+ , These () ()
37− ]
36+ ]
38 print os2
37 print os2
39− os3@[[Nothing, Just [2]]] <- runApp' (unwrapApp testMoribundTellEvent) [Just ()]
38+ os3 <- runApp' (unwrapApp testMoribundTellEvent) [Just ()]
40 print os3
39 print os3
41− os4@[[Nothing, Just [2]]] <- runApp' (unwrapApp testMoribundTellEventDMap) [Just ()]
40+ os4 <- runApp' (unwrapApp testMoribundTellEventDMap) [Just ()]
42 print os4
41 print os4
43− os5@[[Nothing, Just [1, 2]]] <- runApp' (unwrapApp testLiveTellEventDMap) [Just ()]
42+ os5 <- runApp' (unwrapApp testLiveTellEventDMap) [Just ()]
44 print os5
43 print os5
45 os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
44 os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
46 print os6
45 print os6
46+ let ![[Just [10,9,8,7,6,5,4,3,2,1]]] = os1
47+ let ![[Just [1,3,5,7,9]],[Nothing,Nothing],[Just [2,4,6,8,10]],[Just [2,4,6,8,10],Nothing]] = os2
48+ let ![[Nothing, Just [2]]] = os3
49+ let ![[Nothing, Just [2]]] = os4
50+ let ![[Nothing, Just [1, 2]]] = os5
47 let ![[Nothing, Nothing]] = os6
51 let ![[Nothing, Nothing]] = os6
48 return ()
52 return ()
49
53
test/RequesterT.hs+31−26
@@ -10,11 +10,13 @@ module Main where
10import Control.Lens
10import Control.Lens
11import Control.Monad
11import Control.Monad
12import Control.Monad.Fix
12import Control.Monad.Fix
13+import Control.Monad.Primitive
13import qualified Data.Dependent.Map as DMap
14import qualified Data.Dependent.Map as DMap
14import Data.Dependent.Sum
15import Data.Dependent.Sum
15import Data.Functor.Misc
16import Data.Functor.Misc
16import qualified Data.Map as M
17import qualified Data.Map as M
17import Data.These
18import Data.These
19+import Data.Foldable
18
20
19#if defined(MIN_VERSION_these_lens) || (MIN_VERSION_these(0,8,0) && !MIN_VERSION_these(0,9,0))
21#if defined(MIN_VERSION_these_lens) || (MIN_VERSION_these(0,8,0) && !MIN_VERSION_these(0,9,0))
20import Data.These.Lens
22import Data.These.Lens
@@ -30,43 +32,46 @@ data RequestInt a where
30
32
31main :: IO ()
33main :: IO ()
32main = do
34main = do
33− os1 <- runApp' (unwrapApp testOrdering) $
35+-- os1 <- runApp' (unwrapApp testOrdering) $
34− [ Just ()
36+-- [ Just ()
35− ]
37+-- ]
36− print os1
38+-- print os1
37− os2 <- runApp' (unwrapApp testSimultaneous) $ map Just $
39+-- os2 <- runApp' (unwrapApp testSimultaneous) $ map Just $
38− [ This ()
40+-- [ This ()
39− , That ()
41+-- , That ()
40− , This ()
42+-- , This ()
41− , These () ()
43+-- , These () ()
42− ]
44+-- ]
43− print os2
45+-- print os2
44− os3 <- runApp' (unwrapApp testMoribundRequest) [Just ()]
46+-- os3 <- runApp' (unwrapApp testMoribundRequest) [Just ()]
45− print os3
47+-- print os3
46 os4 <- runApp' (unwrapApp testMoribundRequestDMap) [Just ()]
48 os4 <- runApp' (unwrapApp testMoribundRequestDMap) [Just ()]
47 print os4
49 print os4
48− os5 <- runApp' (unwrapApp testLiveRequestDMap) [Just ()]
50+-- os5 <- runApp' (unwrapApp testLiveRequestDMap) [Just ()]
49− print os5
51+-- print os5
50− os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
52+-- os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
51− print os6
53+-- print os6
52− let ![[Just [1,2,3,4,5,6,7,8,9,10]]] = os1 -- The order is reversed here: see the documentation for 'runRequesterT'
54+-- let ![[Just [10,9,8,7,6,5,4,3,2,1]]] = os1
53− let ![[Just [9,7,5,3,1]],[Nothing,Nothing],[Just [10,8,6,4,2]],[Just [10,8,6,4,2],Nothing]] = os2
55+-- let ![[Just [1,3,5,7,9]],[Nothing,Nothing],[Just [2,4,6,8,10]],[Just [2,4,6,8,10],Nothing]] = os2
54− let ![[Nothing, Just [2]]] = os3
56+-- let ![[Nothing, Just [2]]] = os3
55 let ![[Nothing, Just [2]]] = os4
57 let ![[Nothing, Just [2]]] = os4
56− let ![[Nothing, Just [1, 2]]] = os5
58+-- let ![[Nothing, Just [1, 2]]] = os5
57 -- let ![[Nothing, Nothing]] = os6 -- TODO re-enable this test after issue #233 has been resolved
59 -- let ![[Nothing, Nothing]] = os6 -- TODO re-enable this test after issue #233 has been resolved
58 return ()
60 return ()
59
61
60−unwrapRequest :: DSum tag RequestInt -> Int
62+unwrapApp :: forall t m a.
61−unwrapRequest (_ :=> RequestInt i) = i
62−
63−unwrapApp :: ( Reflex t, Monad m )
63+ ( Reflex t
64+ , MonadFix m
65+ , PrimMonad m
66+ )
64 => (a -> RequesterT t RequestInt Identity m ())
67 => (a -> RequesterT t RequestInt Identity m ())
65 -> a
68 -> a
66 -> m (Event t [Int])
69 -> m (Event t [Int])
67unwrapApp x appIn = do
70unwrapApp x appIn = do
68 ((), e) <- runRequesterT (x appIn) never
71 ((), e) <- runRequesterT (x appIn) never
69− return $ fmap (map unwrapRequest . requesterDataToList) e
72+ let unwrapRequests :: forall x. RequestData (PrimState m) RequestInt -> [Int]
73+ unwrapRequests (RequestData _ es) = fmap (\(RequestEnvelope _ (RequestInt i)) -> i) $ toList es
74+ return $ fmap unwrapRequests e
70
75
71testOrdering :: ( Response m ~ Identity
76testOrdering :: ( Response m ~ Identity
72 , Request m ~ RequestInt
77 , Request m ~ RequestInt