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 instances for Profunctor and Traversing. #247

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion dunai/dunai.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ library
Data.MonadicStreamFunction.Instances.ArrowLoop
Data.MonadicStreamFunction.Instances.ArrowPlus
Data.MonadicStreamFunction.Instances.Num
Data.MonadicStreamFunction.Instances.Profunctor
Data.MonadicStreamFunction.Instances.VectorSpace
Data.MonadicStreamFunction.InternalCore
Data.MonadicStreamFunction.Parallel
Expand All @@ -82,7 +83,8 @@ library
transformers,
transformers-base,
MonadRandom,
simple-affine-space
simple-affine-space,
profunctors
freckletonj marked this conversation as resolved.
Show resolved Hide resolved

hs-source-dirs: src
default-language: Haskell2010
Expand Down
1 change: 1 addition & 0 deletions dunai/src/Data/MonadicStreamFunction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ import Data.MonadicStreamFunction.Util
import Data.MonadicStreamFunction.Instances.ArrowChoice ()
import Data.MonadicStreamFunction.Instances.ArrowLoop ()
import Data.MonadicStreamFunction.Instances.ArrowPlus ()
import Data.MonadicStreamFunction.Instances.Profunctor ()
47 changes: 47 additions & 0 deletions dunai/src/Data/MonadicStreamFunction/Instances/Profunctor.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -ddump-simpl #-}

-- | Instances of 'Profunctor' and 'Traversing' for Monadic Stream Functions ('MSF').
--
-- Import this module to include the (orphan) instances.
module Data.MonadicStreamFunction.Instances.Profunctor where

-- base
import Control.Arrow

#if !MIN_VERSION_base(4,8,0)
import Data.Traversable (traverse)
import Control.Applicative (Applicative)
#endif

-- profunctors
import Data.Profunctor
import Data.Profunctor.Traversing

-- dunai
import Data.MonadicStreamFunction.Core
import Data.MonadicStreamFunction.InternalCore
import Data.MonadicStreamFunction.Instances.ArrowChoice ()

-- transformers
import Control.Monad.Trans.State.Strict

instance Monad m => Choice (MSF m) where
left' = left

instance Monad m => Strong (MSF m) where
first' = first

instance Monad m => Profunctor (MSF m) where
dimap l r p = arr l >>> p >>> arr r

-- | This 'Traversing' instance will step the internal state of the 'MSF' once
-- for every element of the input 'Traversable'.
instance (Functor m, Applicative m, Monad m) => Traversing (MSF m) where
freckletonj marked this conversation as resolved.
Show resolved Hide resolved
traverse' msf =
MSF $ \xs -> do
-- `StateT (MSF m a b) m a` is isomorphic to `a -> m (b, MSF m a b)`, and
-- `StateT`'s Applicative instance is needed for writing this traversal
(fb, msf') <- runStateT (traverse (StateT . flip unMSF) xs) msf
return (fb, traverse' msf')