Skip to content

Commit

Permalink
releasing version 0.1.4
Browse files Browse the repository at this point in the history
parAnnihilator, pmin, pmax, pmult

darcs-hash:20081217201031-fb517-63cad013ac2677f422e67106f78d9655cd3eaa02.gz
  • Loading branch information
conal committed Dec 17, 2008
1 parent 12ad2c8 commit 5b9cad0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
37 changes: 29 additions & 8 deletions src/Data/Unamb.hs
Expand Up @@ -23,7 +23,7 @@ module Data.Unamb
unamb, assuming, asAgree
, amb, race
-- * Some useful special applications of 'amb'
, parCommute, por, pand
, parCommute, parAnnihilator, por, pand, pmin, pmax, pmult
) where

import Prelude hiding (catch)
Expand Down Expand Up @@ -87,7 +87,8 @@ forkPut act v = forkIO ((act >>= putMVar v) `catch` uhandler `catch` bhandler)

-- | Yield a value if a condition is true. Otherwise wait forever.
assuming :: Bool -> a -> a
assuming c a = if c then a else undefined
assuming True a = a
assuming False _ = undefined

-- | The value of agreeing values (or hang)
asAgree :: Eq a => a -> a -> a
Expand All @@ -107,16 +108,32 @@ parCommute op x y = (x `op` y) `unamb` (y `op` x)

-- | Parallel or
por :: Bool -> Bool -> Bool

a `por` b = (a || b) `unamb` (b || a)


-- por = parCommute (||)
por = parCommute (||)

-- | Parallel and
pand :: Bool -> Bool -> Bool
pand = parCommute (&&)

-- | Commutative operation with annihilator, in parallel. For instance,
-- '(*)'/0, '(&&)'/'False', '(||)'/'True', 'min'/'minBound', 'max'/'maxBound'.
parAnnihilator :: Eq a => (a -> a -> a) -> a -> (a -> a -> a)
parAnnihilator op ann = parCommute op'
where
op' u v | u == ann = u
| otherwise = op u v

-- | Parallel min with minBound short-circuit
pmin :: (Ord a, Bounded a) => a -> a -> a
pmin = parAnnihilator min minBound

-- | Parallel max with minBound short-circuit
pmax :: (Ord a, Bounded a) => a -> a -> a
pmax = parAnnihilator max maxBound

-- | Parallel multiplication with 0 short-circuit
pmult :: Num a => a -> a -> a
pmult = parAnnihilator (*) 0

{-
-- Examples:
Expand All @@ -127,5 +144,9 @@ True `por` undefined
undefined `pand` False
False `pand` undefined
-}
0 `pmult` undefined
undefined `pmult` 0
LT `pmin` undefined
-}
2 changes: 1 addition & 1 deletion unamb.cabal
@@ -1,5 +1,5 @@
Name: unamb
Version: 0.1.3
Version: 0.1.4
Cabal-Version: >= 1.2
Synopsis: Unambiguous choice
Category: Concurrency, Data, Other
Expand Down

0 comments on commit 5b9cad0

Please sign in to comment.