packages feed

predicate-transformers 0.9.0.0 → 0.10.0.0

raw patch · 3 files changed

+54/−22 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- PredicateTransformers: match :: Predicatory p => APrism s t a b -> PT p a s
- PredicateTransformers: oneOfTwo :: Predicatory a => a -> a -> a
- PredicateTransformers: only :: (Predicatory p, Foldable f) => PT p a (f a)
+ PredicateTransformers: equals :: (Predicatory p, Eq a) => a -> a -> p
+ PredicateTransformers: infixr 2 `otherHand`
+ PredicateTransformers: infixr 3 `also`
+ PredicateTransformers: infixr 9 !
+ PredicateTransformers: onlyContains :: (Predicatory p, Foldable f) => PT p a (f a)
+ PredicateTransformers: otherHand :: Predicatory a => a -> a -> a

Files

CHANGELOG.md view
@@ -1,13 +1,28 @@ # Revision history for predicate-transformers +## 0.10.0.0 -- 2024-07-21++* Rename `oneOfTwo` to `otherHand`, for easier reading.+* Set `also` and `otherHand` precedences to those of `&&` and `||`+  respectively. That makes them work better with the precedence of+  `!`, allowing easier composition.+* Rename `only` to `onlyContains`, for easier reading and to avoid a+  name conflict with `Control.Lens.only`.+* Set `!` precedence to be equal to that of `.`.+* Add `equals` predicate.+* Delete `match`. `allOf1` does the same thing more generally, with+  `Fold` instead of `Prism`.+* Make `otherHand` stop catching async exceptions. Otherwise a thread+  being killed may appear as a predicate failure.+ ## 0.9.0.0 -- 2024-07-21 -* Add instance Predicatory (e -> a). This will allow for adding extra+* Add instance `Predicatory (e -> a)`. This will allow for adding extra   parameters to predicates, making it easier to compose them; maybe   these are called "functional predicates".-* Added traceFailFun. This version of traceFail works on functional-  predicates.-* Tupling sugar renamed from ==> to :=>, to allow i to be a pattern synonym.+* Added `traceFailFun`. This version of traceFail works on functional+  predicates.+* Tupling sugar renamed from `==>` to `:=>`, to allow it to be a pattern synonym. * Minor code style changes.  ## 0.1.0.0 -- 2019-10-05
predicate-transformers.cabal view
@@ -1,7 +1,7 @@ cabal-version: 2.4  name: predicate-transformers-version: 0.9.0.0+version: 0.10.0.0 synopsis: A library for writing predicates and transformations over predicates in Haskell description:   This package provides ways to write predicates such that they compose nicely and are easy to debug.
src/PredicateTransformers.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE PatternSynonyms #-}+{-# LANGUAGE ScopedTypeVariables #-}  -- | This library is based on the notion of a predicate transformer, the below -- type @PT a b@, which is a function from @a@ to predicates on @b@.@@ -18,8 +19,8 @@  import Control.Applicative import Control.DeepSeq (NFData, force)-import Control.Exception (Exception, SomeException, evaluate, throwIO, try)-import Control.Lens hiding (index, zoom)+import Control.Exception+import Control.Lens hiding (index) import Control.Monad import Control.Monad.Writer (execWriter, tell) import Data.Bool@@ -29,29 +30,36 @@ import Data.Typeable import Debug.Trace import System.IO.Unsafe+import Control.Concurrent (myThreadId, throwTo)+import Control.Exception (SomeAsyncException)  class Predicatory a where-  oneOfTwo :: a -> a -> a+  otherHand :: a -> a -> a   also :: a -> a -> a   stop :: a   continue :: a  instance Predicatory a => Predicatory (e -> a) where-  (f `oneOfTwo` f') e = f e `oneOfTwo` f' e+  (f `otherHand` f') e = f e `otherHand` f' e   (f `also` f') e = f e `also` f' e   stop = \_ -> stop   continue = \_ -> continue +infixr 3 `also`+infixr 2 `otherHand`++-- | Class of predicate results which can be checked for failure, by surfacing an exception. class Exceptional a where   assess :: a -> IO () +-- | The exception thrown by predicates of type `IO ()` by default. Other IOExceptions will work fine. data PredicateFailed = PredicateFailed   deriving (Show, Typeable)  instance Exception PredicateFailed  instance Predicatory Bool where-  oneOfTwo = (||)+  otherHand = (||)   also = (&&)   stop = False   continue = True@@ -62,7 +70,15 @@     unless b stop  instance Predicatory (IO ()) where-  oneOfTwo x y = x <|> y+  otherHand x y = do+    catches x+      -- explicitly do not handle async exceptions.+      -- otherwise, a thread being killed may appear as a predicate failure.+      [ Handler $ \(ex :: SomeAsyncException) -> do+        tid <- myThreadId+        throwTo tid ex+      , Handler $ \(_ex :: SomeException) -> y+      ]   also = (>>)   stop = throwIO PredicateFailed   continue = return ()@@ -79,21 +95,17 @@ --  for instance `Data.Foldable.all` and `Data.Foldable.any`. type PT p a b = (a -> p) -> (b -> p) --- | Operate on the target of a prism, or fail.-match :: Predicatory p => APrism s t a b -> PT p a s-match p pred = either (const stop) pred . matching p- -- | Operate on the `Just` branch of a `Maybe`, or fail. just :: Predicatory p => PT p a (Maybe a)-just = match _Just+just = allOf1 _Just  -- | Operate on the `Left` branch of an `Either`, or fail. left :: Predicatory p => PT p e (Either e a)-left = match _Left+left = allOf1 _Left  -- | Operate on the `Right` branch of an `Either`, or fail. right :: Predicatory p => PT p a (Either e a)-right = match _Right+right = allOf1 _Right  -- | Operate on the last value in a foldable, or fail if it's not present. endingWith :: (Predicatory p, Foldable f) => PT p a (f a)@@ -108,10 +120,10 @@ {-# INLINEABLE startingWith #-}  -- | Require that a foldable has a single element, and operate on that element.-only :: (Predicatory p, Foldable f) => PT p a (f a)-only p (toList -> [x]) = p x-only _ _ = stop-{-# INLINEABLE only #-}+onlyContains :: (Predicatory p, Foldable f) => PT p a (f a)+onlyContains p (toList -> [x]) = p x+onlyContains _ _ = stop+{-# INLINEABLE onlyContains #-}  -- | Only test the @k@th element of a foldable. kth :: (Predicatory p, Foldable f) => Int -> PT p a (f a)@@ -168,6 +180,7 @@ -- | Flipped function composition; @f !@ for a function @f@ is a predicate transformer. (!) :: (b -> a) -> (a -> c) -> b -> c (!) = flip (.)+infixr 9 !  -- | Prints the input of a predicate, for debugging. traced :: Show a => (a -> c) -> a -> c@@ -207,3 +220,7 @@ --  Useful for testing that an exception isn't thrown. forced :: (Predicatory p, NFData a) => a -> p forced a = force a `seq` continue++-- | Predicate on equality.+equals :: (Predicatory p, Eq a) => a -> a -> p+equals a a' = bool stop continue (a == a')