Skip to content

Commit

Permalink
Add conditional compilation of a pipes subset to Haskell98.
Browse files Browse the repository at this point in the history
  • Loading branch information
archblob committed Nov 16, 2013
1 parent 4aeaf56 commit 88a6169
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 87 deletions.
22 changes: 19 additions & 3 deletions pipes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ Source-Repository head
Location: https://github.com/Gabriel439/Haskell-Pipes-Library

Library
Default-Language: Haskell2010
if !flag(haskell98)
Default-Language: Haskell2010
else
Default-Language: Haskell98

HS-Source-Dirs: src
Build-Depends:
base >= 4 && < 5 ,
mmorph >= 1.0.0 && < 1.1,
mtl >= 2.0.1.0 && < 2.2,
transformers >= 0.2.0.0 && < 0.4,
void < 0.7

if !flag(haskell98)
Build-Depends:
mmorph >= 1.0.0 && < 1.1,
mtl >= 2.0.1.0 && < 2.2

Exposed-Modules:
Pipes,
Pipes.Core,
Expand All @@ -56,6 +64,10 @@ Library
Pipes.Tutorial
GHC-Options: -O2 -Wall

if flag(haskell98)
CPP-Options: -Dhaskell98


Benchmark prelude-benchmarks
Default-Language: Haskell2010
Type: exitcode-stdio-1.0
Expand Down Expand Up @@ -99,3 +111,7 @@ Benchmark lift-benchmarks
mtl >= 2.0.1.0 && < 2.2,
pipes >= 4.0.0 && < 4.1,
transformers >= 0.2.0.0 && < 0.4

Flag haskell98
Description: Haskell98 compliant subset of pipes.
Default: False
26 changes: 17 additions & 9 deletions src/Pipes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,25 @@ module Pipes (
-- $reexports
module Control.Monad.IO.Class,
module Control.Monad.Trans.Class,
#ifndef haskell98
module Control.Monad.Morph,
#endif
module Data.Foldable,
module Data.Void
) where

import Control.Applicative (Applicative(pure, (<*>)), Alternative(empty, (<|>)))
import Control.Monad (MonadPlus(mzero, mplus))
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.IO.Class (MonadIO(liftIO)) -- transformers
#ifndef haskell98
import Control.Monad.Error (MonadError(..), ErrorT(runErrorT))
import Control.Monad.Reader (MonadReader(..))
import Control.Monad.State (MonadState(..))
import Control.Monad.Writer (MonadWriter(..))
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.Trans.Identity (IdentityT(runIdentityT))
import Control.Monad.Trans.Maybe (MaybeT(runMaybeT))
#endif
import Control.Monad.Trans.Class (MonadTrans(lift)) --transformers
import Control.Monad.Trans.Identity (IdentityT(runIdentityT)) --transformers
import Control.Monad.Trans.Maybe (MaybeT(runMaybeT)) --transformers
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Monoid (Monoid(..))
Expand All @@ -86,7 +90,9 @@ import Pipes.Internal (Proxy(..))
import Pipes.Core

-- Re-exports
#ifndef haskell98
import Control.Monad.Morph (MFunctor(hoist))
#endif

infixl 4 <~
infixr 4 ~>
Expand Down Expand Up @@ -356,28 +362,29 @@ instance (Monad m) => Alternative (ListT m) where
instance (Monad m) => MonadPlus (ListT m) where
mzero = empty
mplus = (<|>)


#ifndef haskell98
instance MFunctor ListT where
hoist morph = Select . hoist morph . enumerate
#endif

instance (Monad m) => Monoid (ListT m a) where
mempty = empty
mappend = (<|>)

#ifndef haskell98
instance (MonadState s m) => MonadState s (ListT m) where
get = lift get

put s = lift (put s)

#if MIN_VERSION_mtl(2,1,0)
state f = lift (state f)
#else
#endif

instance (MonadWriter w m) => MonadWriter w (ListT m) where
#if MIN_VERSION_mtl(2,1,0)
writer = lift . writer
#else
#endif

tell w = lift (tell w)
Expand Down Expand Up @@ -410,14 +417,13 @@ instance (MonadReader i m) => MonadReader i (ListT m) where

#if MIN_VERSION_mtl(2,1,0)
reader f = lift (reader f)
#else
#endif

instance (MonadError e m) => MonadError e (ListT m) where
throwError e = lift (throwError e)

catchError l k = Select (catchError (enumerate l) (\e -> enumerate (k e)))

#endif
{-| 'Enumerable' generalizes 'Data.Foldable.Foldable', converting effectful
containers to 'ListT's.
-}
Expand All @@ -439,12 +445,14 @@ instance Enumerable MaybeT where
Nothing -> return ()
Just a -> yield a

#ifndef haskell98
instance Enumerable (ErrorT e) where
toListT m = Select $ do
x <- lift $ runErrorT m
case x of
Left _ -> return ()
Right a -> yield a
#endif

{-| Consume the first value from a 'Producer'
Expand Down
14 changes: 10 additions & 4 deletions src/Pipes/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ module Pipes.Internal (
) where

import Control.Applicative (Applicative(pure, (<*>)), Alternative(empty, (<|>)))
import Control.Monad (liftM, MonadPlus(..))
import Control.Monad (MonadPlus(..))
import Control.Monad.IO.Class (MonadIO(liftIO))
#ifndef haskell98
import Control.Monad.Morph (MFunctor(hoist))
#endif
import Control.Monad.Trans.Class (MonadTrans(lift))
#ifndef haskell98
import Control.Monad (liftM)
import Control.Monad.Error (MonadError(..))
import Control.Monad.Reader (MonadReader(..))
import Control.Monad.State (MonadState(..))
import Control.Monad.Writer (MonadWriter(..))
import Data.Monoid (mempty,mappend)
#endif

{-| A 'Proxy' is a monad transformer that receives and sends information on both
an upstream and downstream interface.
Expand Down Expand Up @@ -129,17 +134,20 @@ unsafeHoist nat = go
Pure r -> Pure r
{-# INLINABLE unsafeHoist #-}

#ifndef haskell98
instance MFunctor (Proxy a' a b' b) where
hoist nat p0 = go (observe p0) where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
M m -> M (nat (m >>= \p' -> return (go p')))
Pure r -> Pure r
#endif

instance (MonadIO m) => MonadIO (Proxy a' a b' b m) where
liftIO m = M (liftIO (m >>= \r -> return (Pure r)))

#ifndef haskell98
instance (MonadReader r m) => MonadReader r (Proxy a' a b' b m) where
ask = lift ask
local f = go
Expand All @@ -151,21 +159,18 @@ instance (MonadReader r m) => MonadReader r (Proxy a' a b' b m) where
M m -> M (go `liftM` local f m)
#if MIN_VERSION_mtl(2,1,0)
reader = lift . reader
#else
#endif

instance (MonadState s m) => MonadState s (Proxy a' a b' b m) where
get = lift get
put = lift . put
#if MIN_VERSION_mtl(2,1,0)
state = lift . state
#else
#endif

instance (MonadWriter w m) => MonadWriter w (Proxy a' a b' b m) where
#if MIN_VERSION_mtl(2,1,0)
writer = lift . writer
#else
#endif
tell = lift . tell
listen p0 = go p0 mempty
Expand Down Expand Up @@ -199,6 +204,7 @@ instance (MonadError e m) => MonadError e (Proxy a' a b' b m) where
M m -> M ((do
p' <- m
return (go p') ) `catchError` (\e -> return (f e)) )
#endif

instance (MonadPlus m) => Alternative (Proxy a' a b' b m) where
empty = mzero
Expand Down
Loading

0 comments on commit 88a6169

Please sign in to comment.