Skip to content

Add Alt, Plus, MonadPlus, update Alternative #8

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

Merged
merged 2 commits into from
Aug 11, 2014
Merged
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: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"purescript-tuples": "*",
"purescript-either": "*",
"purescript-monoid": "*",
"purescript-arrays": "*",
"purescript-control": "*"
"purescript-arrays": "~0.2.0",
"purescript-control": "~0.2.0"
},
"devDependencies": {
"purescript-strings": "*"
Expand Down
34 changes: 31 additions & 3 deletions docs/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@

### Type Class Instances

instance errorEitherAlt :: (Error e) => Alt (Either e)

instance errorEitherAlternative :: (Error e) => Alternative (Either e)

instance errorEitherPlus :: (Error e) => Plus (Either e)

instance errorString :: Error String


Expand Down Expand Up @@ -107,6 +111,8 @@

### Type Class Instances

instance altErrorT :: (Monad m, Error e) => Alt (ErrorT e m)

instance alternativeErrorT :: (Monad m, Error e) => Alternative (ErrorT e m)

instance applicativeErrorT :: (Functor m, Monad m) => Applicative (ErrorT e m)
Expand All @@ -119,8 +125,12 @@

instance monadErrorT :: (Monad m, Error e) => Monad (ErrorT e m)

instance monadPlusErrorT :: (Monad m, Error e) => MonadPlus (ErrorT e m)

instance monadTransErrorT :: (Error e) => MonadTrans (ErrorT e)

instance plusErrorT :: (Monad m, Error e) => Plus (ErrorT e m)


### Values

Expand Down Expand Up @@ -410,6 +420,8 @@

### Type Class Instances

instance altReaderT :: (Alt m) => Alt (ReaderT r m)

instance alternativeReaderT :: (Alternative m) => Alternative (ReaderT r m)

instance applicativeReaderT :: (Applicative m) => Applicative (ReaderT r m)
Expand All @@ -420,10 +432,14 @@

instance functorReaderT :: (Functor m) => Functor (ReaderT r m)

instance monadPlusReaderT :: (MonadPlus m) => MonadPlus (ReaderT r m)

instance monadReaderT :: (Monad m) => Monad (ReaderT r m)

instance monadTransReaderT :: MonadTrans (ReaderT r)

instance plusReaderT :: (Plus m) => Plus (ReaderT r m)


### Values

Expand Down Expand Up @@ -506,7 +522,9 @@

### Type Class Instances

instance alternativeStateT :: (Alternative m) => Alternative (StateT s m)
instance altStateT :: (Monad m, Alt m) => Alt (StateT s m)

instance alternativeStateT :: (Monad m, Alternative m) => Alternative (StateT s m)

instance applicativeStateT :: (Monad m) => Applicative (StateT s m)

Expand All @@ -516,10 +534,14 @@

instance functorStateT :: (Monad m) => Functor (StateT s m)

instance monadPlusStateT :: (MonadPlus m) => MonadPlus (StateT s m)

instance monadStateT :: (Monad m) => Monad (StateT s m)

instance monadTransStateT :: MonadTrans (StateT s)

instance plusStateT :: (Monad m, Plus m) => Plus (StateT s m)


### Values

Expand Down Expand Up @@ -642,20 +664,26 @@

### Type Class Instances

instance altWriterT :: (Monoid w, Alt m) => Alt (WriterT w m)

instance alternativeWriterT :: (Monoid w, Alternative m) => Alternative (WriterT w m)

instance applicativeWriterT :: (Monoid w, Functor m, Applicative m) => Applicative (WriterT w m)
instance applicativeWriterT :: (Monoid w, Applicative m) => Applicative (WriterT w m)

instance applyWriterT :: (Monoid w, Functor m, Applicative m) => Apply (WriterT w m)
instance applyWriterT :: (Monoid w, Apply m) => Apply (WriterT w m)

instance bindWriterT :: (Monoid w, Monad m) => Bind (WriterT w m)

instance functorWriterT :: (Functor m) => Functor (WriterT w m)

instance monadPlusWriterT :: (Monoid w, MonadPlus m) => MonadPlus (WriterT w m)

instance monadTransWriterT :: (Monoid w) => MonadTrans (WriterT w)

instance monadWriterT :: (Monoid w, Monad m) => Monad (WriterT w m)

instance plusWriterT :: (Monoid w, Plus m) => Plus (WriterT w m)


### Values

Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Cont/Class.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Cont.Class where

import Prelude
import Control.Monad.Error
import qualified Control.Monad.Cont.Trans as Cont
import Control.Monad.Error
Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Cont/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Cont.Trans where

import Prelude
import Control.Monad.Trans

newtype ContT r m a = ContT ((a -> m r) -> m r)
Expand Down
14 changes: 10 additions & 4 deletions src/Control/Monad/Error.purs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
module Control.Monad.Error where

import Prelude
import Control.Alt
import Control.Alternative
import Control.Plus
import Data.Either

class Error a where
noMsg :: a
strMsg :: String -> a

instance errorString :: Error String where
noMsg = ""
strMsg = id

instance errorEitherAlternative :: (Error e) => Alternative (Either e) where
empty = Left noMsg
instance errorEitherAlt :: (Error e) => Alt (Either e) where
(<|>) (Left _) n = n
(<|>) m _ = m

instance errorEitherPlus :: (Error e) => Plus (Either e) where
empty = Left noMsg

instance errorEitherAlternative :: (Error e) => Alternative (Either e)
3 changes: 1 addition & 2 deletions src/Control/Monad/Error/Class.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Error.Class where

import Prelude
import Control.Monad.Trans
import Control.Monad.Error
import Control.Monad.Error.Trans
Expand All @@ -14,7 +13,7 @@ import Data.Monoid
class MonadError e m where
throwError :: forall a. e -> m a
catchError :: forall a. m a -> (e -> m a) -> m a

instance monadErrorError :: (Error e) => MonadError e (Either e) where
throwError = Left
catchError (Left e) h = h e
Expand Down
15 changes: 12 additions & 3 deletions src/Control/Monad/Error/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module Control.Monad.Error.Trans where

import Prelude
import Control.Alt
import Control.Alternative
import Control.Plus
import Control.Monad.Error
import Control.Monad.Trans
import Control.MonadPlus
import Data.Either
import Data.Monoid
import Data.Tuple
Expand Down Expand Up @@ -32,12 +35,16 @@ instance applyErrorT :: (Functor m, Monad m) => Apply (ErrorT e m) where
instance applicativeErrorT :: (Functor m, Monad m) => Applicative (ErrorT e m) where
pure a = ErrorT $ pure $ Right a

instance alternativeErrorT :: (Monad m, Error e) => Alternative (ErrorT e m) where
empty = ErrorT (return (Left $ strMsg "No alternative"))
instance altErrorT :: (Monad m, Error e) => Alt (ErrorT e m) where
(<|>) x y = ErrorT $ runErrorT x >>= \e -> case e of
Left _ -> runErrorT y
r -> return r

instance plusErrorT :: (Monad m, Error e) => Plus (ErrorT e m) where
empty = ErrorT (return (Left $ strMsg "No alternative"))

instance alternativeErrorT :: (Monad m, Error e) => Alternative (ErrorT e m)

instance bindErrorT :: (Monad m, Error e) => Bind (ErrorT e m) where
(>>=) m f = ErrorT $ do
a <- runErrorT m
Expand All @@ -47,6 +54,8 @@ instance bindErrorT :: (Monad m, Error e) => Bind (ErrorT e m) where

instance monadErrorT :: (Monad m, Error e) => Monad (ErrorT e m)

instance monadPlusErrorT :: (Monad m, Error e) => MonadPlus (ErrorT e m)

instance monadTransErrorT :: (Error e) => MonadTrans (ErrorT e) where
lift m = ErrorT $ do
a <- m
Expand Down
2 changes: 0 additions & 2 deletions src/Control/Monad/Identity.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Control.Monad.Identity where

import Prelude

newtype Identity a = Identity a

runIdentity :: forall a. Identity a -> a
Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Maybe/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Maybe.Trans where

import Prelude
import Control.Monad
import Control.Monad.Trans
import Data.Either
Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Reader.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Reader where

import Prelude
import Control.Monad.Identity
import Control.Monad.Reader.Trans

Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Reader/Class.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Reader.Class where

import Prelude
import Control.Monad.Trans
import Control.Monad.Reader.Trans
import Control.Monad.Error
Expand Down
15 changes: 12 additions & 3 deletions src/Control/Monad/Reader/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module Control.Monad.Reader.Trans where

import Prelude
import Control.Alt
import Control.Alternative
import Control.Plus
import Control.Monad.Trans
import Control.MonadPlus

newtype ReaderT r m a = ReaderT (r -> m a)

Expand All @@ -26,17 +29,23 @@ instance applyReaderT :: (Applicative m) => Apply (ReaderT r m) where
instance applicativeReaderT :: (Applicative m) => Applicative (ReaderT r m) where
pure = liftReaderT <<< pure

instance alternativeReaderT :: (Alternative m) => Alternative (ReaderT r m) where
empty = liftReaderT empty
instance altReaderT :: (Alt m) => Alt (ReaderT r m) where
(<|>) m n = ReaderT \r -> runReaderT m r <|> runReaderT n r

instance plusReaderT :: (Plus m) => Plus (ReaderT r m) where
empty = liftReaderT empty

instance alternativeReaderT :: (Alternative m) => Alternative (ReaderT r m)

instance bindReaderT :: (Monad m) => Bind (ReaderT r m) where
(>>=) m k = ReaderT \r -> do
a <- runReaderT m r
runReaderT (k a) r

instance monadReaderT :: (Monad m) => Monad (ReaderT r m)

instance monadPlusReaderT :: (MonadPlus m) => MonadPlus (ReaderT r m)

instance monadTransReaderT :: MonadTrans (ReaderT r) where
lift = liftReaderT

Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/State.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.State where

import Prelude
import Control.Monad.Identity
import Control.Monad.State.Trans
import Data.Tuple
Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/State/Class.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.State.Class where

import Prelude
import Control.Monad.Trans
import Control.Monad.State.Trans
import Control.Monad.Error
Expand Down
15 changes: 12 additions & 3 deletions src/Control/Monad/State/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module Control.Monad.State.Trans where

import Prelude
import Control.Alt
import Control.Alternative
import Control.Plus
import Control.Monad.Trans
import Control.MonadPlus
import Data.Tuple

newtype StateT s m a = StateT (s -> m (Tuple a s))
Expand Down Expand Up @@ -30,17 +33,23 @@ instance applyStateT :: (Monad m) => Apply (StateT s m) where
instance applicativeStateT :: (Monad m) => Applicative (StateT s m) where
pure a = StateT $ \s -> return $ Tuple a s

instance alternativeStateT :: (Alternative m) => Alternative (StateT s m) where
empty = StateT $ \_ -> empty
instance altStateT :: (Monad m, Alt m) => Alt (StateT s m) where
(<|>) x y = StateT $ \s -> runStateT x s <|> runStateT y s

instance plusStateT :: (Monad m, Plus m) => Plus (StateT s m) where
empty = StateT $ \_ -> empty

instance alternativeStateT :: (Monad m, Alternative m) => Alternative (StateT s m)

instance bindStateT :: (Monad m) => Bind (StateT s m) where
(>>=) (StateT x) f = StateT \s -> do
Tuple v s' <- x s
runStateT (f v) s'

instance monadStateT :: (Monad m) => Monad (StateT s m)

instance monadPlusStateT :: (MonadPlus m) => MonadPlus (StateT s m)

instance monadTransStateT :: MonadTrans (StateT s) where
lift m = StateT \s -> do
x <- m
Expand Down
2 changes: 0 additions & 2 deletions src/Control/Monad/Trans.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module Control.Monad.Trans where

import Prelude

class MonadTrans t where
lift :: forall m a. (Monad m) => m a -> t m a
1 change: 0 additions & 1 deletion src/Control/Monad/Writer.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Writer where

import Prelude
import Control.Monad.Identity
import Control.Monad.Writer.Trans
import Data.Monoid
Expand Down
1 change: 0 additions & 1 deletion src/Control/Monad/Writer/Class.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Control.Monad.Writer.Class where

import Prelude
import Control.Monad.Trans
import Control.Monad.Writer.Trans
import Control.Monad.Error
Expand Down
Loading