Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traverseFactorableSumWithAdjust and networkHoldFactorableSum #212

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Reflex/Adjustable/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand All @@ -24,10 +26,12 @@ module Reflex.Adjustable.Class
, sequenceDMapWithAdjust
, sequenceDMapWithAdjustWithMove
, mapMapWithAdjustWithMove
, traverseFactorableSumWithAdjust
-- * Deprecated aliases
, MonadAdjust
) where

import Control.Lens (Prism', (^?), review)
import Control.Monad.Identity
import Control.Monad.Reader
import Data.Dependent.Map (DMap)
Expand Down Expand Up @@ -131,6 +135,76 @@ mapMapWithAdjustWithMove f m0 m' = do
(out0 :: DMap (Const2 k v) (Constant v'), out') <- traverseDMapWithKeyWithAdjustWithMove (\(Const2 k) (Identity v) -> Constant <$> f k v) (mapToDMap m0) (const2PatchDMapWithMoveWith Identity <$> m')
return (dmapToMapWith (\(Constant v') -> v') out0, patchDMapWithMoveToPatchMapWithMoveWith (\(Constant v') -> v') <$> out')


-- | 'Adjustable' helper for sum types.
--
-- It's often the case that some adjustable computation fed with sum types only
-- needs incremental adjustments when the variant stays the same but data within
-- changes, and only need be done from scatch when the the variant changes. This
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'scratch' ?

-- function is a specialization of 'runWithReplace' but with more parameters
-- prepended to help provide that incrementalism.
--
-- The singleton ['sing'], eliminator, and 'Prism'' family together connect the
-- sum type ['sum'] with its variants. The singleton must be total in that there
-- must be a singleon singleton assigned to each variant. The 'Prism'' family
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'singleon' ?

-- must be "honest" in that the prism indexed by the singleton for each variant
-- must actually match that variant. Together this is formalized as:
--
-- forall s :: sum. exists tp (i :: sing tp) (v :: tp).
-- s ^? (prismForVariant i) = Just v
--
-- (Note this technically allows the same singleton to be assigned to multiple
-- variants, but that is needlessly confusing and you shouldn't do that.)
--
-- Given this ability to "factor" the sum, 'traverseFactorableSumWithAdjust'
-- splits the update event "runs" of updates of the same variant, and then feeds
-- the first and rest firing(s) of each run into the adjustable action
-- constructor for the variant of that run. If 'f' is suitably incremental, the
-- whole computation is too as the from-scratch reapplications of 'f' only
-- happen at run boundaries.
--
-- The "out functor" might reasonably be instantiated with 'Dynamic t a', or
-- something like '\a -> (a, Event t a)' (like the return type of
-- 'runWithReplace'). The latter is a more general return type, which is why
-- 'runWithReplace' hard-codes it, but for 'traverseFactorableSumWithAdjust'
-- that puts a more difficult obligation on the constructor, so neither
-- instantiation is more general than the other. That is why this is made a
-- parameter.
traverseFactorableSumWithAdjust
:: forall sum sing t m outFunctor
. (Reflex t, MonadHold t m, MonadFix m, Adjustable t m, Functor outFunctor)
=> (forall a. sum -> (forall tp. sing tp -> tp -> a) -> a)
-- ^ eliminator for the sum type using the 'sing' singleton to constrain the
-- 'tp' parameter over valid variant types
-> (forall tp. sing tp -> Prism' sum tp)
-- ^ family of ''Prism''s per variant
-> (forall tp. sing tp -> tp -> Event t tp -> m (outFunctor tp))
-- ^ family of constructors of adjustable actions, one constructor per variant
-> sum
-- ^ initial value to be held
-> Event t sum
-- ^ updates to be held
-> m (outFunctor sum, Event t (outFunctor sum))
traverseFactorableSumWithAdjust withVariant prismForVariant f iv ev = do
(initM :: m (outFunctor sum), rest :: Event t sum) <- f' iv ev
rec
let eBoth = pushAlways (`f'` rest) e'
l = fst <$> eBoth
r = snd <$> eBoth
e' <- switchHold rest r
runWithReplace initM l
where
f' :: forall m'. (MonadHold t m', MonadFix m')
=> sum
-> Event t sum
-> m' (m (outFunctor sum), Event t sum)
f' iv' ev' = withVariant iv' $ \(ot :: sing tp) (i :: tp) -> do
(sames :: Event t tp, rest :: Event t sum)
<- takeDropWhileJustE (^? prismForVariant ot) ev'
let firstRun = fmap (review (prismForVariant ot)) <$>
f ot i sames
pure (firstRun, rest)

--------------------------------------------------------------------------------
-- Deprecated functions
--------------------------------------------------------------------------------
Expand Down
36 changes: 36 additions & 0 deletions src/Reflex/Network.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

#ifdef USE_REFLEX_OPTIMIZER
{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
#endif

-- |
-- Module:
-- Reflex.Network
Expand All @@ -11,9 +16,14 @@
module Reflex.Network
( networkView
, networkHold
, networkHoldFactorableSum
, untilReady
) where

import Control.Lens (Prism')
import Control.Monad (join)
import Control.Monad.Fix (MonadFix)

import Reflex.Class
import Reflex.Adjustable.Class
import Reflex.NotReady.Class
Expand All @@ -37,6 +47,32 @@ networkHold child0 newChild = do
(result0, newResult) <- runWithReplace child0 newChild
holdDyn result0 newResult

-- | 'runFactorableSumWithReplace' specialized to 'Dynamic', and then held and
-- joined down to one 'Dynamic'. This is to 'runFactorableSumWithReplace', as
-- 'networkHold' is to 'runWithReplace'.
--
-- N.B. A similar outcome could be achieved with some combination of 'factorDyn'
-- and 'networkHold', but this way avoids the initial 'sample' in 'factorDyn'
-- which is a bit too easy to use improperly and get diverging cycles.
networkHoldFactorableSum
:: forall sum sing t m
. (Reflex t, MonadHold t m, MonadFix m, Adjustable t m)
=> (forall a. sum -> (forall tp. sing tp -> tp -> a) -> a)
-- ^ eliminator for the sum type using the 'sing' singleton to constrain the
-- 'tp' parameter over valid variant types
-> (forall tp. sing tp -> Prism' sum tp)
-- ^ family of ''Prism''s per variant
-> (forall tp. sing tp -> tp -> Event t tp -> m (Dynamic t tp))
-- ^ family of constructors of adjustable actions, one constructor per variant
-> sum
-- ^ initial value to be held
-> Event t sum
-- ^ updates to be held
-> m (Dynamic t sum)
networkHoldFactorableSum wV pFV f iv ev = do
(result0, newResult) <- traverseFactorableSumWithAdjust wV pFV f iv ev
Control.Monad.join <$> holdDyn result0 newResult

-- | Render a placeholder network to be shown while another network is not yet
-- done building
untilReady :: (Adjustable t m, PostBuild t m) => m a -> m b -> m (a, Event t b)
Expand Down