Skip to content

Commit

Permalink
Merge branch 'develop-style-partial' into develop. Close #285.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanperez-keera committed Jun 3, 2022
2 parents 7f221dd + a7e88ea commit f116cb4
Show file tree
Hide file tree
Showing 23 changed files with 180 additions and 129 deletions.
6 changes: 3 additions & 3 deletions dunai/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
2022-05-07 Ivan Perez <ivan.perez@keera.co.uk>
* src/: Simplify implementation of swtich (#276), remove redundant
2022-06-02 Ivan Perez <ivan.perez@keera.co.uk>
* src/: Simplify implementation of switch (#276), remove redundant
imports (#281), remove redundant pragma (#282), use external
Void type (#79), remove redundant imports (#283), disable warning
pertaining to orphan instances (#284).
pertaining to orphan instances (#284), style fixes (partial) (#285).

2022-04-21 Ivan Perez <ivan.perez@keera.co.uk>
* dunai.cabal: Version bump (0.8.2) (#280), syntax rules (#271),
Expand Down
3 changes: 2 additions & 1 deletion dunai/src/Control/Arrow/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
-- Utility functions to work with 'Arrow's.
module Control.Arrow.Util where

import Control.Arrow
-- External imports
import Control.Arrow (Arrow, arr, (&&&), (>>^), (^<<))

-- | Constantly produce the same output.
constantly :: Arrow a => b -> a c b
Expand Down
1 change: 1 addition & 0 deletions dunai/src/Control/Monad/Trans/MSF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Control.Monad.Trans.MSF
)
where

-- Internal imports
import Control.Monad.Trans.MSF.Except
import Control.Monad.Trans.MSF.Maybe
import Control.Monad.Trans.MSF.Random
Expand Down
47 changes: 28 additions & 19 deletions dunai/src/Control/Monad/Trans/MSF/Except.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ module Control.Monad.Trans.MSF.Except
, module Control.Monad.Trans.Except
) where

-- External
-- External imports
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
import Control.Applicative (Applicative (..), (<$>))
#endif

import Control.Arrow (arr, returnA, (<<<), (>>>))
import qualified Control.Category as Category
import Control.Monad (ap, liftM)
import Control.Monad.Trans.Class
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except hiding (liftCallCC, liftListen,
liftPass)
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
import Data.Void (Void)

-- Internal
import Data.MonadicStreamFunction
import Data.MonadicStreamFunction.InternalCore
-- Internal imports
import Data.MonadicStreamFunction (arrM, constM, count, feedback,
liftTransS, mapMaybeS, morphS,
reactimate)
import Data.MonadicStreamFunction.InternalCore (MSF (MSF, unMSF))

-- External, necessary for older base versions
#if __GLASGOW_HASKELL__ < 802
Expand Down Expand Up @@ -198,7 +201,8 @@ handleExceptT msf f = flip handleGen msf $ \a mbcont -> do
Left e -> unMSF (f e) a
Right (b, msf') -> return (b, handleExceptT msf' f)

-- | If no exception can occur, the 'MSF' can be executed without the 'ExceptT' layer.
-- | If no exception can occur, the 'MSF' can be executed without the 'ExceptT'
-- layer.
safely :: Monad m => MSFExcept m a b Void -> MSF m a b
safely (MSFExcept msf) = morphS fromExcept msf
where
Expand Down Expand Up @@ -245,10 +249,10 @@ listToMSFExcept = mapM_ step_

-- * Utilities definable in terms of 'MSFExcept'

-- TODO This is possibly not the best location for these functions,
-- but moving them to Data.MonadicStreamFunction.Util would form an import cycle
-- that could only be broken by moving a few things to Data.MonadicStreamFunction.Core
-- (that probably belong there anyways).
-- TODO This is possibly not the best location for these functions, but moving
-- them to Data.MonadicStreamFunction.Util would form an import cycle that
-- could only be broken by moving a few things to
-- Data.MonadicStreamFunction.Core (that probably belong there anyways).

-- | Extract an 'MSF' from a monadic action.
--
Expand All @@ -273,7 +277,8 @@ reactimateB sf = reactimateExcept $ try $ liftTransS sf >>> throwOn ()
-- | Run first MSF until the second value in the output tuple is @Just c@ (for
-- some @c@), then start the second MSF.
--
-- Analog to Yampa's [@switch@](https://hackage.haskell.org/package/Yampa/docs/FRP-Yampa-Switches.html#v:switch),
-- Analog to Yampa's
-- [@switch@](https://hackage.haskell.org/package/Yampa/docs/FRP-Yampa-Switches.html#v:switch),
-- with 'Maybe' instead of @Event@.
switch :: Monad m => MSF m a (b, Maybe c) -> (c -> MSF m a b) -> MSF m a b
switch sf f = catchS ef f
Expand All @@ -288,7 +293,8 @@ switch sf f = catchS ef f
-- | Run first MSF until the second value in the output tuple is @Just c@ (for
-- some @c@), then start the second MSF.
--
-- Analog to Yampa's [@dswitch@](https://hackage.haskell.org/package/Yampa/docs/FRP-Yampa-Switches.html#v:dSwitch),
-- Analog to Yampa's
-- [@dswitch@](https://hackage.haskell.org/package/Yampa/docs/FRP-Yampa-Switches.html#v:dSwitch),
-- with 'Maybe' instead of @Event@.
dSwitch :: Monad m => MSF m a (b, Maybe c) -> (c -> MSF m a b) -> MSF m a b
dSwitch sf f = catchS ef f
Expand All @@ -307,11 +313,14 @@ transG :: (Monad m1, Monad m2)
-> MSF m1 a1 b1
-> MSF m2 a2 b2
transG transformInput transformOutput msf = go
where go = MSF $ \a2 -> do
(b2, msf') <- transformOutput a2 $ unMSF msf =<< transformInput a2
case msf' of
Just msf'' -> return (b2, transG transformInput transformOutput msf'')
Nothing -> return (b2, go)
where
go = MSF $ \a2 -> do
(b2, msf') <- transformOutput a2 $ unMSF msf =<< transformInput a2
case msf' of
Just msf'' ->
return (b2, transG transformInput transformOutput msf'')
Nothing ->
return (b2, go)

-- | Use a generic handler to handle exceptions in MSF processing actions.
handleGen :: (a -> m1 (b1, MSF m1 a b1) -> m2 (b2, MSF m2 a b2))
Expand Down
7 changes: 4 additions & 3 deletions dunai/src/Control/Monad/Trans/MSF/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module Control.Monad.Trans.MSF.List
, module Control.Monad.Trans.List
) where

-- External
-- External imports
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif

import Control.Monad.Trans.List hiding (liftCallCC, liftCatch)

-- Internal
-- Internal imports
import Data.MonadicStreamFunction.InternalCore (MSF (MSF, unMSF))

-- * List monad
Expand All @@ -28,7 +28,8 @@ widthFirst msf = widthFirst' [msf] where
return (bs, widthFirst' msfs')


-- Name alternatives: "choose", "parallely" (problematic because it's not multicore)
-- Name alternatives: "choose", "parallely" (problematic because it's not
-- multicore)
sequenceS :: Monad m => [MSF m a b] -> MSF (ListT m) a b
sequenceS msfs = MSF $ \a -> ListT $ sequence $ apply a <$> msfs
where
Expand Down
30 changes: 20 additions & 10 deletions dunai/src/Control/Monad/Trans/MSF/Maybe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ module Control.Monad.Trans.MSF.Maybe
, maybeToExceptS
) where

-- External
-- External imports
import Control.Arrow (returnA, (>>>), arr)
import Control.Monad.Trans.Maybe hiding (liftCallCC, liftCatch, liftListen,
liftPass)

-- Internal
import Control.Monad.Trans.MSF.Except
import Data.MonadicStreamFunction
-- Internal imports
import Control.Monad.Trans.MSF.Except (ExceptT, exceptS, listToMSFExcept,
maybeToExceptS, reactimateExcept,
runExceptT, runMSFExcept, safe, safely,
try)
import Data.MonadicStreamFunction (MSF, arrM, constM, liftTransS, morphS)

-- * Throwing 'Nothing' as an exception ("exiting")

Expand Down Expand Up @@ -51,14 +55,16 @@ inMaybeT = arrM $ MaybeT . return

-- * Catching Maybe exceptions

-- | Run the first @msf@ until the second one produces 'True' from the output of the first.
-- | Run the first @msf@ until the second one produces 'True' from the output
-- of the first.
untilMaybe :: Monad m => MSF m a b -> MSF m b Bool -> MSF (MaybeT m) a b
untilMaybe msf cond = proc a -> do
b <- liftTransS msf -< a
c <- liftTransS cond -< b
inMaybeT -< if c then Nothing else Just b

-- | When an exception occurs in the first 'msf', the second 'msf' is executed from there.
-- | When an exception occurs in the first 'msf', the second 'msf' is executed
-- from there.
catchMaybe
:: (Functor m, Monad m)
=> MSF (MaybeT m) a b -> MSF m a b -> MSF m a b
Expand All @@ -69,8 +75,11 @@ catchMaybe msf1 msf2 = safely $ do
-- * Converting to and from 'MaybeT'

-- | Convert exceptions into `Nothing`, discarding the exception value.
exceptToMaybeS :: (Functor m, Monad m) => MSF (ExceptT e m) a b -> MSF (MaybeT m) a b
exceptToMaybeS = morphS $ MaybeT . fmap (either (const Nothing) Just) . runExceptT
exceptToMaybeS :: (Functor m, Monad m)
=> MSF (ExceptT e m) a b
-> MSF (MaybeT m) a b
exceptToMaybeS =
morphS $ MaybeT . fmap (either (const Nothing) Just) . runExceptT

-- | Converts a list to an 'MSF' in 'MaybeT',
-- which outputs an element of the list at each step,
Expand All @@ -79,8 +88,9 @@ listToMaybeS :: (Functor m, Monad m) => [b] -> MSF (MaybeT m) a b
listToMaybeS = exceptToMaybeS . runMSFExcept . listToMSFExcept

-- * Running 'MaybeT'
-- | Remove the 'MaybeT' layer by outputting 'Nothing' when the exception occurs.
-- The continuation in which the exception occurred is then tested on the next input.
-- | Remove the 'MaybeT' layer by outputting 'Nothing' when the exception
-- occurs. The continuation in which the exception occurred is then tested on
-- the next input.
runMaybeS :: (Functor m, Monad m) => MSF (MaybeT m) a b -> MSF m a (Maybe b)
runMaybeS msf = exceptS (maybeToExceptS msf) >>> arr eitherToMaybe
where
Expand Down
8 changes: 4 additions & 4 deletions dunai/src/Control/Monad/Trans/MSF/RWS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ module Control.Monad.Trans.MSF.RWS
, module Control.Monad.Trans.RWS.Strict
) where

-- External
-- External imports
import Control.Monad.Trans.RWS.Strict hiding (liftCallCC, liftCatch)

#if !MIN_VERSION_base(4,8,0)
import Data.Functor ((<$>))
import Data.Monoid
import Data.Monoid (Monoid)
#endif

-- Internal
import Data.MonadicStreamFunction
-- Internal imports
import Data.MonadicStreamFunction (MSF, morphGS)

-- * 'RWS' (Reader-Writer-State) monad

Expand Down
12 changes: 7 additions & 5 deletions dunai/src/Control/Monad/Trans/MSF/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ module Control.Monad.Trans.MSF.Random
, getRandomsRS_
) where

-- External
import Control.Monad.Random
-- External imports
import Control.Arrow (arr, (>>>))
import Control.Monad.Random (MonadRandom, RandT, Random, RandomGen, getRandom,
getRandomR, getRandomRs, getRandoms, runRandT)

-- Internal
import Control.Monad.Trans.MSF.State
import Data.MonadicStreamFunction
-- Internal imports
import Control.Monad.Trans.MSF.State (StateT (..), runStateS_)
import Data.MonadicStreamFunction (MSF, arrM, constM, morphS)

-- | Run an 'MSF' in the 'RandT' random number monad transformer
-- by supplying an initial random generator.
Expand Down
9 changes: 5 additions & 4 deletions dunai/src/Control/Monad/Trans/MSF/Reader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ module Control.Monad.Trans.MSF.Reader
, runReaderS_
) where

-- External
-- External imports
import Control.Arrow (arr, (>>>))
import Control.Monad.Trans.Reader hiding (liftCallCC, liftCatch)

-- Internal
import Data.MonadicStreamFunction
-- Internal imports
import Data.MonadicStreamFunction (MSF, morphGS)

-- * Reader 'MSF' running and wrapping

Expand All @@ -40,4 +41,4 @@ runReaderS = morphGS $ \f (r, a) -> runReaderT (f a) r
-- | Build an 'MSF' /function/ that takes a fixed environment as additional
-- input, from an MSF in the 'Reader' monad.
runReaderS_ :: Monad m => MSF (ReaderT s m) a b -> s -> MSF m a b
runReaderS_ msf s = arr (\a -> (s,a)) >>> runReaderS msf
runReaderS_ msf s = arr (\a -> (s, a)) >>> runReaderS msf
19 changes: 12 additions & 7 deletions dunai/src/Control/Monad/Trans/MSF/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ module Control.Monad.Trans.MSF.State
, runStateS__
) where

-- External
-- External imports
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
import Control.Applicative ((<$>))
#endif

import Control.Arrow (arr, (>>>))
import Control.Monad.Trans.State.Strict hiding (liftCallCC, liftCatch,
liftListen, liftPass)
import Data.Tuple (swap)

-- Internal
import Data.MonadicStreamFunction.Core
-- Internal imports
import Data.MonadicStreamFunction.Core (MSF, morphGS, feedback)

-- * 'State' 'MSF' running and wrapping

Expand All @@ -54,9 +55,13 @@ runStateS = morphGS $ \f (s, a) -> (\((b, c), s') -> ((s', b), c))
-- | Build an 'MSF' /function/ that takes a fixed state as additional input,
-- from an 'MSF' in the 'State' monad, and outputs the new state with every
-- transformation step.
runStateS_ :: (Functor m, Monad m) => MSF (StateT s m) a b -> s -> MSF m a (s, b)
runStateS_ msf s = feedback s
$ arr swap >>> runStateS msf >>> arr (\(s', b) -> ((s', b), s'))
runStateS_ :: (Functor m, Monad m)
=> MSF (StateT s m) a b
-> s
-> MSF m a (s, b)
runStateS_ msf s =
feedback s $
arr swap >>> runStateS msf >>> arr (\(s', b) -> ((s', b), s'))

-- TODO Rename this to execStateS!
-- | Build an 'MSF' /function/ that takes a fixed state as additional
Expand Down
8 changes: 4 additions & 4 deletions dunai/src/Control/Monad/Trans/MSF/Writer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ module Control.Monad.Trans.MSF.Writer
, runWriterS
) where

-- External
-- External imports
import Control.Monad.Trans.Writer.Strict hiding (liftCallCC, liftCatch, pass)

#if !MIN_VERSION_base(4,8,0)
import Data.Functor ((<$>))
import Data.Monoid
import Data.Monoid (Monoid)
#endif

-- Internal
import Data.MonadicStreamFunction
-- Internal imports
import Data.MonadicStreamFunction (MSF, morphGS)

-- * 'Writer' 'MSF' running and wrapping

Expand Down
6 changes: 3 additions & 3 deletions dunai/src/Data/MonadicStreamFunction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ module Data.MonadicStreamFunction
)
where

-- External
-- External imports
import Control.Arrow

-- Internal
-- Internal imports
import Data.MonadicStreamFunction.Core
import Data.MonadicStreamFunction.Util

-- Internal (Instances)
-- Internal imports (instances)
import Data.MonadicStreamFunction.Instances.ArrowChoice ()
import Data.MonadicStreamFunction.Instances.ArrowLoop ()
import Data.MonadicStreamFunction.Instances.ArrowPlus ()
Loading

0 comments on commit f116cb4

Please sign in to comment.