summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrewCoppin <>2010-11-07 12:14:38 (GMT)
committerLuite Stegeman <luite@luite.com>2010-11-07 12:14:38 (GMT)
commit32503caccdc7bdcca932bb5f86d40dfc1b44f950 (patch)
tree25833fe831948852e87546474c6ea15dd346fdf2
version 1.0.01.0.0
-rw-r--r--AC-Boolean.cabal26
-rw-r--r--Data/Boolean.hs105
-rw-r--r--License.txt10
-rw-r--r--Setup.hs3
4 files changed, 144 insertions, 0 deletions
diff --git a/AC-Boolean.cabal b/AC-Boolean.cabal
new file mode 100644
index 0000000..4fc389f
--- /dev/null
+++ b/AC-Boolean.cabal
@@ -0,0 +1,26 @@
+Cabal-Version: >= 1.6
+Name: AC-Boolean
+Version: 1.0.0
+Stability: Experimental
+Synopsis: Handle Boolean values generatically.
+
+Description:
+
+ This package provides various functions and classes for dealing
+ with things which are like Boolean values. It also defines a
+ 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.
+
+Category: Data, Logic, Math
+License: BSD3
+License-file: License.txt
+Author: Andrew Coppin
+Maintainer: MathematicalOrchid@hotmail.com
+Build-Type: Simple
+Tested-With: GHC == 6.12.3
+
+Library
+ Exposed-modules: Data.Boolean
+ Build-Depends: base >= 4 && < 5
+ HS-Source-Dirs: .
diff --git a/Data/Boolean.hs b/Data/Boolean.hs
new file mode 100644
index 0000000..43481fc
--- /dev/null
+++ b/Data/Boolean.hs
@@ -0,0 +1,105 @@
+{- |
+ Type classes (and instances) for things that are like Booleans.
+
+ The names of methods in 'Boolean' clash with the standard Prelude,
+ so you probably want to inport the Prelude hiding these three
+ 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.)
+-}
+
+{-# LANGUAGE FlexibleInstances #-}
+
+module Data.Boolean where
+
+import qualified Prelude as P
+
+{- |
+ Typeclass for things that have true and false values.
+
+ Instances:
+
+ * 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.)
+
+ * Any monadic action that yields a 'BoolValue' as its result.
+ (This is just 'P.return' applied to the appropriate 'BoolValue'.)
+-}
+class BoolValue b where
+ true :: b
+ false :: b
+
+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
+
+-- | Convert a 'P.Bool' value to the appropriate 'BoolValue'.
+lift_bool :: (BoolValue b) => P.Bool -> b
+lift_bool b = if b then true else false
+
+{- |
+ Typeclass for things that support Boolean operators.
+
+ Instances:
+
+ * 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).)
+
+ * 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).)
+-}
+class Boolean b where
+ -- | Logical-AND of two values.
+ (&&) :: b -> b -> b
+
+ -- | Logical-OR of two values. (Inclusive-OR.)
+ (||) :: b -> b -> b
+
+ -- | Logical-NOT of two values. (Logical inverse.)
+ not :: b -> b
+
+ {- |
+ Exclusive-OR (XOR). There is a default implementation, but you
+ can override it for efficiency if desired.
+ -}
+ xor :: b -> b -> b
+
+ x `xor` y = (x || y) && (not (x && y))
+
+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)
+
+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)
diff --git a/License.txt b/License.txt
new file mode 100644
index 0000000..60dd2ca
--- /dev/null
+++ b/License.txt
@@ -0,0 +1,10 @@
+Copyright (c) 2009, Andrew Coppin
+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 Andrew Coppin nor the names of the 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 HOLDER 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.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..40f133f
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain