-
Notifications
You must be signed in to change notification settings - Fork 30
add ListT monad transformer and relevant instances #71
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
Open
chessai
wants to merge
3
commits into
master
Choose a base branch
from
listT-54
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
| {-# LANGUAGE MultiParamTypeClasses #-} | ||
| {-# LANGUAGE UndecidableInstances #-} | ||
|
|
||
| {-# OPTIONS_GHC -Wall #-} | ||
|
|
||
| module Streaming.ListT | ||
| ( ListT(..) | ||
| --, runListT | ||
| ) where | ||
|
|
||
| import Streaming.Internal | ||
| import Data.Functor.Of | ||
| import qualified Streaming.Prelude as S | ||
|
|
||
| import Control.Applicative (Applicative (..)) --, Alternative (..)) | ||
| import Control.Monad.IO.Class (MonadIO (liftIO)) | ||
| import Control.Monad.Morph | ||
| import Control.Monad.Error.Class | ||
| import Control.Monad.Reader.Class | ||
| import Control.Monad.State.Class | ||
| import Control.Monad.Writer.Class | ||
| import Control.Monad.Zip | ||
| import Data.Functor (Functor (..)) | ||
| --import Data.Semigroup (Semigroup ((<>))) | ||
|
|
||
| newtype ListT m a = Select { enumerate :: Stream (Of a) m () } | ||
|
|
||
| instance Monad m => Functor (ListT m) where | ||
| fmap f (Select p) = Select (S.map f p) | ||
| {-# INLINE fmap #-} | ||
|
|
||
| instance Monad m => Applicative (ListT m) where | ||
| pure a = Select (S.yield a) | ||
| {-# INLINE pure #-} | ||
| mf <*> mx = Select | ||
| ( S.for (enumerate mf) (\f -> | ||
| S.for (enumerate mx) (\x -> | ||
| S.yield (f x))) | ||
| ) | ||
|
|
||
| instance Monad m => Monad (ListT m) where | ||
| return = pure | ||
| {-# INLINE return #-} | ||
| m >>= f = Select (S.for (enumerate m) (\a -> enumerate (f a))) | ||
| {-# INLINE (>>=) #-} | ||
|
|
||
| instance (Monad m, Foldable m) => Foldable (ListT m) where | ||
| foldMap f (Select p) = foldMap id (S.foldMap_ f p) | ||
|
|
||
| instance (Monad m, Traversable m) => Traversable (ListT m) where | ||
| traverse k (Select p) = fmap Select (t_ p) | ||
| where | ||
| t_ x = case x of | ||
| Return () -> pure (Return ()) | ||
| Effect m -> fmap Effect (traverse t_ m) | ||
| Step (a :> rest) -> (\a_ rest_ -> Step (a_ :> rest_)) <$> k a <*> t_ rest | ||
|
|
||
| instance MonadTrans ListT where | ||
| lift m = Select (do | ||
| a <- lift m | ||
| S.yield a) | ||
|
|
||
| instance MonadIO m => MonadIO (ListT m) where | ||
| liftIO m = lift (liftIO m) | ||
| {-# INLINE liftIO #-} | ||
|
|
||
| -- what should this be? | ||
| --instance Monad m => Alternative (ListT m) where | ||
|
|
||
| --instance Monad m => MonadPlus (ListT m) where | ||
| -- mzero = empty | ||
| -- {-# INLINE mzero #-} | ||
| -- mplus = (<|>) | ||
| -- {-# INLINE mplus #-} | ||
|
|
||
| instance MFunctor ListT where | ||
| hoist morph = Select . hoist morph . enumerate | ||
| {-# INLINE hoist #-} | ||
|
|
||
| instance MMonad ListT where | ||
| embed f (Select p0) = Select (loop p0) | ||
| where | ||
| loop x = case x of | ||
| Return () -> Return () | ||
| Effect m -> S.for (enumerate (fmap loop (f m))) id | ||
| Step (a :> rest) -> Step (a :> loop rest) | ||
| {-# INLINE embed #-} | ||
|
|
||
| instance (MonadState s m) => MonadState s (ListT m) where | ||
| get = lift get | ||
| {-# INLINE get #-} | ||
|
|
||
| put s = lift (put s) | ||
| {-# INLINE put #-} | ||
|
|
||
| state f = lift (state f) | ||
| {-# INLINE state #-} | ||
|
|
||
| instance (MonadWriter w m) => MonadWriter w (ListT m) where | ||
| writer = lift . writer | ||
| {-# INLINE writer #-} | ||
|
|
||
| tell w = lift (tell w) | ||
| {-# INLINE tell #-} | ||
|
|
||
| listen l = Select (go (enumerate l) mempty) | ||
| where | ||
| go p w = case p of | ||
| Return () -> Return () | ||
| Effect m -> Effect (do | ||
| (p', w') <- listen m | ||
| pure (go p' $! mappend w w') ) | ||
| Step (a :> rest) -> Step ((a,w) :> go rest w) | ||
|
|
||
| pass l = Select (go (enumerate l) mempty) | ||
| where | ||
| go p w = case p of | ||
| Return () -> Return () | ||
| Effect m -> Effect (do | ||
| (p', w') <- listen m | ||
| pure (go p' $! mappend w w')) | ||
| Step ((b,f) :> rest) -> Effect (pass (pure | ||
| (Step (b :> (go rest (f w))), \_ -> f w) )) | ||
|
|
||
| instance (MonadReader i m) => MonadReader i (ListT m) where | ||
| ask = lift ask | ||
| {-# INLINE ask #-} | ||
|
|
||
| local f l = Select (local f (enumerate l)) | ||
| {-# INLINE local #-} | ||
|
|
||
| reader f = lift (reader f) | ||
| {-# INLINE reader #-} | ||
|
|
||
| instance (MonadError e m) => MonadError e (ListT m) where | ||
| throwError e = lift (throwError e) | ||
| {-# INLINE throwError #-} | ||
|
|
||
| catchError l k = Select (catchError (enumerate l) (\e -> enumerate (k e))) | ||
| {-# INLINE catchError #-} | ||
|
|
||
| {- These instances require a dependency on `exceptions`. | ||
| instance MonadThrow m => MonadThrow (ListT m) where | ||
| throwM = Select . throwM | ||
| {-# INLINE throwM #-} | ||
|
|
||
| instance MonadCatch m => MonadCatch (ListT m) where | ||
| catch l k = Select (Control.Monad.Catch.catch (enumerate l) (\e -> enumerate (k e))) | ||
| {-# INLINE catch #-} | ||
| -} | ||
|
|
||
| instance Monad m => MonadZip (ListT m) where | ||
| mzipWith f (Select p) (Select p') = Select (S.zipWith f p p') | ||
|
|
||
| -- no MonadPlus instance yet | ||
| --runListT :: Monad m => ListT m a -> m () | ||
| --runListT l = S.effects (enumerate (l >> mzero)) | ||
| --{-# INLINABLE runListT #-} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.