summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrewCoppin <>2010-11-09 11:39:08 (GMT)
committerLuite Stegeman <luite@luite.com>2010-11-09 11:39:08 (GMT)
commit2210cf3e523914e1b05c9f8bcb9f9aa259b54498 (patch)
treef46afd7b0ecca002a0dd0ab1514e1a9e8462cdfa
parent32503caccdc7bdcca932bb5f86d40dfc1b44f950 (diff)
version 1.1.0HEAD1.1.0master
-rw-r--r--AC-Boolean.cabal11
-rw-r--r--Data/Boolean.hs57
2 files changed, 40 insertions, 28 deletions
diff --git a/AC-Boolean.cabal b/AC-Boolean.cabal
index 4fc389f..384bdc8 100644
--- a/AC-Boolean.cabal
+++ b/AC-Boolean.cabal
@@ -1,6 +1,6 @@
Cabal-Version: >= 1.6
Name: AC-Boolean
-Version: 1.0.0
+Version: 1.1.0
Stability: Experimental
Synopsis: Handle Boolean values generatically.
@@ -11,6 +11,15 @@ Description:
few useful instances. The main benefit is the ability to use
the usual @&&@, etc. operators without having to invent new
operator names for every kind of Bool-like thing.
+ .
+ Changes:
+ .
+ * Increase efficiency of @Boolean@ instance for @Bool@.
+ .
+ * Shorten source code.
+ .
+ * Remove function instance. (@Control.Monad.Instances@ provides
+ an equivilent @Monad@ instance.)
Category: Data, Logic, Math
License: BSD3
diff --git a/Data/Boolean.hs b/Data/Boolean.hs
index 43481fc..6161371 100644
--- a/Data/Boolean.hs
+++ b/Data/Boolean.hs
@@ -6,12 +6,23 @@
names (since the class methods do the same thing, but with more
general type signatures).
- An interesting consequence of the 'Boolean' instance for monads is
- that 'P.Maybe' 'P.Bool' is a 'Boolean'. You can use this to implement
- 3-value logic (\"true\", \"false\" and \"other\"), with 'P.Nothing'
- implementing \"other\". Any logical operations yield 'P.Nothing'
- unless all arguments are 'P.Just' something. (This is usually the
- behaviour you want.)
+ Please note the following points:
+
+ * This module imports "Control.Monad.Instances", which brings
+ several new 'P.Monad' instances into scope.
+
+ * Among other things, a monad instance for functions is brought
+ into scope. This, combined with the 'Boolean' instance for
+ monads, causes any function that returns a 'Boolean' to become
+ a 'Boolean' itself. This allows you to write constructions such
+ as @(> 5) && (< 9)@, which has the obvious meaning.
+
+ * Another interesting consequence of the 'Boolean' instance for
+ monads is that 'P.Maybe' 'P.Bool' is a 'Boolean'. You can use
+ this to implement 3-value logic (\"true\", \"false\" and
+ \"other\"), with 'P.Nothing' implementing \"other\". Any logical
+ operations yield 'P.Nothing' unless all arguments are 'P.Just'
+ something. (This is usually the behaviour you want.)
-}
{-# LANGUAGE FlexibleInstances #-}
@@ -19,6 +30,8 @@
module Data.Boolean where
import qualified Prelude as P
+import qualified Control.Monad as M
+import Control.Monad.Instances -- For the Monad ((->) r) instance.
{- |
Typeclass for things that have true and false values.
@@ -28,11 +41,11 @@ import qualified Prelude as P
* Normal 'P.Bool' values (obviously).
* Any function that yields a 'BoolValue' as its result.
- (For example, 'true' is just a constant function that always
- returns a truth value, regardless of its input.)
+ (@'true' = 'P.const' 'P.True'@, @'false' = 'P.const' 'P.False'@)
+ This instance arrises due to the monad instance for functions.
* Any monadic action that yields a 'BoolValue' as its result.
- (This is just 'P.return' applied to the appropriate 'BoolValue'.)
+ (@'true' = 'P.return' 'P.True'@, @'false' = 'P.return' 'P.False'@)
-}
class BoolValue b where
true :: b
@@ -42,10 +55,6 @@ instance BoolValue P.Bool where
true = P.True
false = P.False
-instance (BoolValue b) => BoolValue (x -> b) where
- true = \ _ -> true
- false = \ _ -> false
-
instance (P.Monad m, BoolValue b) => BoolValue (m b) where
true = P.return true
false = P.return false
@@ -62,12 +71,11 @@ lift_bool b = if b then true else false
* Normal 'P.Bool' values (obviously).
* Any function that returns a 'Boolean'.
- (The result is a new function that runs the old function(s) and
- applies the appropriate operator to the result(s).)
+ This instance arrises due to the monad instance for functions.
* Any monadic action that returns a 'Boolean'.
- (Again, the result is a new action that runs the existing
- action(s) and applies the appropriate operator to the result(s).)
+ The left action is performed before the right action (which may
+ be significant, depending on the monad).
-}
class Boolean b where
-- | Logical-AND of two values.
@@ -91,15 +99,10 @@ instance Boolean P.Bool where
(&&) = (P.&&)
(||) = (P.||)
not = P.not
-
-instance (Boolean b) => Boolean (x -> b) where
- f && g = \ x -> f x && g x
- f || g = \ x -> f x || g x
- not f = \ x -> not (f x)
- f `xor` g = \ x -> (f x) `xor` (g x)
+ xor = (P.==)
instance (P.Monad m, Boolean b) => Boolean (m b) where
- f && g = f P.>>= \ x -> g P.>>= \ y -> P.return (x && y)
- f || g = f P.>>= \ x -> g P.>>= \ y -> P.return (x || y)
- not f = f P.>>= \ x -> P.return (not x)
- f `xor` g = f P.>>= \ x -> g P.>>= \ y -> P.return (x `xor` y)
+ (&&) = M.liftM2 (&&)
+ (||) = M.liftM2 (||)
+ not = M.liftM not
+ xor = M.liftM2 xor