packages feed

control-bool (empty) → 0.1

raw patch · 4 files changed

+144/−0 lines, 4 filesdep +basesetup-changed

Dependencies added: base

Files

+ Control/Bool.hs view
@@ -0,0 +1,86 @@+-----------------------------------------------------------------------------
+-- |
+-- Module      :  Control.Bool
+-- Copyright   :  (C) 2013 Fumiaki Kinoshita
+-- License     :  BSD-style (see the file LICENSE)
+--
+-- Maintainer  :  Fumiaki Kinoshita <fumiexcel@gmail.com>
+-- Stability   :  experimental
+-- Portability :  non-portable
+--
+-- Useful combinators for boolean expressions
+----------------------------------------------------------------------------
+
+module Control.Bool (
+-- * A pure combinator
+bool
+-- * Applicative Combinators
+, notF
+, (<||>)
+, (<&&>)
+, aguard'
+-- * Monadic combinators
+, notM
+, (<|=>)
+, (<&=>)
+, guard'
+, guardM'
+, whenM
+, unlessM
+) where
+
+import Control.Applicative
+import Control.Monad
+import Data.Monoid
+
+infixr 3 <&&>, <&=>
+infixr 2 <||>, <|=>
+
+-- | Return its second argument if the boolean value is 'True', otherwise return first.
+bool :: a -> a -> Bool -> a
+bool x _ False = x
+bool _ y True = y
+
+-- | A lifted 'not'.
+notF :: Functor f => f Bool -> f Bool
+notF = fmap not
+
+-- | A lifted 'not'.
+notM :: Monad m => m Bool -> m Bool
+notM = liftM not
+
+-- | A lifted ('&&').
+(<&&>) :: Applicative f => f Bool -> f Bool -> f Bool
+(<&&>) = liftA2 (&&)
+
+-- | A lifted ('&&').
+(<&=>) :: Monad m => m Bool -> m Bool -> m Bool
+(<&=>) = liftM2 (&&)
+
+-- | A lifted ('||').
+(<||>) :: Applicative f => f Bool -> f Bool -> f Bool
+(<||>) = liftA2 (||)
+
+-- | A lifted ('||').
+(<|=>) :: Monad m => m Bool -> m Bool -> m Bool
+(<|=>) = liftM2 (||)
+
+-- | Run the action if the given monadic condition becomes 'True'.
+whenM :: (Monoid a, Monad m) => m Bool -> m a -> m a
+whenM mp m = mp >>= bool (return mempty) m
+
+-- | Run the action if the given monadic condition becomes 'False'.
+unlessM :: (Monoid a, Monad m) => m Bool -> m a -> m a
+unlessM mp m = mp >>= bool m (return mempty)
+
+-- | 'guard'' b returns the second argument if b is True, otherwise becomes 'mzero'.
+guard' :: MonadPlus m => Bool -> a -> m a
+guard' b a = bool mzero (return a) b
+
+-- | 'aguard'' b returns the second argument if b is True, otherwise becomes 'empty'.
+aguard' :: Alternative m => Bool -> a -> m a
+aguard' b a = bool empty (pure a) b
+
+-- | 'guard'' b returns the second argument if b is True, otherwise becomes mzero.
+guardM' :: MonadPlus m => m Bool -> a -> m a
+guardM' mp a = mp >>= flip guard' a
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Fumiaki Kinoshita
+
+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.
+
+    * Neither the name of Fumiaki Kinoshita nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"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
+OWNER OR CONTRIBUTORS 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple
+main = defaultMain
+ control-bool.cabal view
@@ -0,0 +1,26 @@+name:                control-bool
+version:             0.1
+synopsis:            Useful combinators for boolean expressions
+description:         
+homepage:            https://github.com/fumieval/control-bool
+bug-reports:         https://github.com/fumieval/control-bool/issues
+license:             BSD3
+license-file:        LICENSE
+author:              Fumiaki Kinoshita
+maintainer:          Fumiaki Kinoshita <fumiexcel@gmail.com>
+copyright:           Copyright (C) 2012-2013 Fumiaki Kinoshita
+category:            Control
+build-type:          Simple
+stability:           experimental
+cabal-version:       >=1.10
+
+source-repository head
+  type: git
+  location: https://github.com/fumieval/control-bool.git
+
+library
+  default-language:   Haskell2010
+  exposed-modules: Control.Bool
+  ghc-options: -Wall
+
+  build-depends: base == 4.*