AC-Boolean 1.0.0 → 1.1.0
raw patch · 2 files changed
+40/−28 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Boolean: instance (BoolValue b) => BoolValue (x -> b)
- Data.Boolean: instance (Boolean b) => Boolean (x -> b)
- Data.Boolean: (&&) :: (Boolean b) => b -> b -> b
+ Data.Boolean: (&&) :: Boolean b => b -> b -> b
- Data.Boolean: (||) :: (Boolean b) => b -> b -> b
+ Data.Boolean: (||) :: Boolean b => b -> b -> b
- Data.Boolean: false :: (BoolValue b) => b
+ Data.Boolean: false :: BoolValue b => b
- Data.Boolean: lift_bool :: (BoolValue b) => Bool -> b
+ Data.Boolean: lift_bool :: BoolValue b => Bool -> b
- Data.Boolean: not :: (Boolean b) => b -> b
+ Data.Boolean: not :: Boolean b => b -> b
- Data.Boolean: true :: (BoolValue b) => b
+ Data.Boolean: true :: BoolValue b => b
- Data.Boolean: xor :: (Boolean b) => b -> b -> b
+ Data.Boolean: xor :: Boolean b => b -> b -> b
Files
- AC-Boolean.cabal +10/−1
- Data/Boolean.hs +30/−27
AC-Boolean.cabal view
@@ -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 @@ 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
Data/Boolean.hs view
@@ -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 @@ * 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 @@ 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 @@ * 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 @@ (&&) = (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