From 1f47000a0c63a7c2fb17a38561118cb58feaf8f9 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Tue, 29 Jun 2010 01:41:04 +0300 Subject: [PATCH] Initial version of library --- .gitignore | 2 ++ Data/Neither.hs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 25 ++++++++++++++++ neither.cabal | 29 ++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 Data/Neither.hs create mode 100644 LICENSE create mode 100644 neither.cabal diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d52b5cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +/dist diff --git a/Data/Neither.hs b/Data/Neither.hs new file mode 100644 index 0000000..e4fe101 --- /dev/null +++ b/Data/Neither.hs @@ -0,0 +1,80 @@ +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE PackageImports #-} +-- | This module provides three different datatypes: 'AEither' is the +-- applicative version of Either. It does not provide a monad instance, and +-- 'mappend's together error values. 'MEither' is the monadic version, which +-- only holds onto the first error value. 'MEitherT' is a monad transformer. +module Data.Neither + ( -- * Applicative version + AEither (..) + , aeither + -- * Monadic version + , MEither (..) + , meither + -- * Monad transformer + , MEitherT (..) + , mapMEitherT + ) where + +import Prelude hiding (catch) +import Control.Applicative +import Control.Monad +import Data.Typeable +import Data.Data +import Data.Monoid +import Control.Monad.IO.Class +import Control.Monad.Trans.Class +import "MonadCatchIO-transformers" Control.Monad.CatchIO (MonadCatchIO (..)) + +data MEither a b = MLeft a | MRight b + deriving (Typeable, Eq, Data, Ord, Read, Show) +instance Monad (MEither a) where + return = MRight + (MLeft a) >>= _ = MLeft a + (MRight b) >>= f = f b +instance Functor (MEither a) where + fmap = liftM +instance Applicative (MEither a) where + pure = return + (<*>) = ap +meither :: (a -> c) -> (b -> c) -> MEither a b -> c +meither f _ (MLeft a) = f a +meither _ f (MRight b) = f b + +data AEither a b = ALeft a | ARight b + deriving (Typeable, Eq, Data, Ord, Read, Show) +instance Functor (AEither a) where + fmap _ (ALeft a) = ALeft a + fmap f (ARight b) = ARight $ f b +instance Monoid a => Applicative (AEither a) where + pure = ARight + ALeft x <*> ALeft y = ALeft $ x `mappend` y + ALeft x <*> _ = ALeft x + _ <*> ALeft y = ALeft y + ARight x <*> ARight y = ARight $ x y +aeither :: (a -> c) -> (b -> c) -> AEither a b -> c +aeither f _ (ALeft a) = f a +aeither _ f (ARight b) = f b + +newtype MEitherT e m a = MEitherT + { runMEitherT :: m (MEither e a) + } +mapMEitherT :: (m (MEither e a) -> n (MEither e' b)) + -> MEitherT e m a + -> MEitherT e' n b +mapMEitherT f m = MEitherT $ f (runMEitherT m) + +instance Functor m => Functor (MEitherT e m) where + fmap f = MEitherT . fmap (fmap f) . runMEitherT +instance Monad m => Monad (MEitherT e m) where + return = MEitherT . return . return + (MEitherT x) >>= f = MEitherT $ + x >>= meither (return . MLeft) (runMEitherT . f) +instance MonadTrans (MEitherT e) where + lift = MEitherT . liftM MRight +instance MonadIO m => MonadIO (MEitherT e m) where + liftIO = lift . liftIO +instance MonadCatchIO m => MonadCatchIO (MEitherT e m) where + m `catch` f = mapMEitherT (\m' -> m' `catch` \e -> runMEitherT $ f e) m + block = mapMEitherT block + unblock = mapMEitherT unblock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8643e5d --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +The following license covers this documentation, and the source code, except +where otherwise indicated. + +Copyright 2010, Michael Snoyman. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/neither.cabal b/neither.cabal new file mode 100644 index 0000000..a8224fb --- /dev/null +++ b/neither.cabal @@ -0,0 +1,29 @@ +name: neither +version: 0.0.0 +license: BSD3 +license-file: LICENSE +author: Michael Snoyman +maintainer: Michael Snoyman +synopsis: Provide versions of Either with good monad and applicative instances. +description: + The standard Either datatype suffers from a lack of monad and applicative instances. To make matters worse, the mtl and transformers packages provide orphan instances which conflict with each other, as well as defining a transformer version which has an usually unnecessary superclass constraint. + . + Besides these annoyances, there is another issue: there exist two reasonable definitions of the Applicative instance for Either: one the holds onto only the first Left value, or one that appends all Left values together via a Monoid instance. The former is compatible with the monad instance, while the latter is not. + . + This package defines three datatypes, some helpers functions and instances. The data types are AEither, MEither and MEitherT. AEither provides an Applicative instance which appends Left values, MEither provides the monadic definition, and MEitherT is a monad transformer. +category: Data +stability: Stable +cabal-version: >= 1.6 +build-type: Simple +homepage: http://github.com/snoyberg/neither + +library + build-depends: base >= 4 && < 5, + transformers >= 0.2.1 && < 0.3, + MonadCatchIO-transformers >= 0.2.2 && < 0.3 + exposed-modules: Data.Neither + ghc-options: -Wall + +source-repository head + type: git + location: git://github.com/snoyberg/neither.git