packages feed

kure 2.4.0 → 2.4.2

raw patch · 3 files changed

+28/−27 lines, 3 files

Files

Language/KURE/Combinators.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE TypeOperators, TupleSections, FlexibleInstances #-}+{-# LANGUAGE TupleSections, FlexibleInstances #-}  -- | -- Module: Language.KURE.Combinators@@ -162,101 +162,101 @@ -- -- > failT msg `catchT` f == f msg -class Category (~>) => CategoryCatch (~>) where+class Category arr => CategoryCatch arr where   -- | The failing 'Category'.-  failT :: String -> a ~> b+  failT :: String -> arr a b    -- | A catch on 'Category's.-  catchT :: (a ~> b) -> (String -> (a ~> b)) -> (a ~> b)+  catchT :: arr a b -> (String -> arr a b) -> arr a b   -- | Left-biased choice.-(<+) :: CategoryCatch (~>) => (a ~> b) -> (a ~> b) -> (a ~> b)+(<+) :: CategoryCatch arr => arr a b -> arr a b -> arr a b f <+ g = f `catchT` \ _ -> g  -- | Look at the argument to the 'Arrow' before choosing which 'Arrow' to use.-readerT :: ArrowApply (~>) => (a -> (a ~> b)) -> (a ~> b)+readerT :: ArrowApply arr => (a -> arr a b) -> arr a b readerT f = (f &&& id) ^>> app  -- | Look at the argument to an 'Arrow', and choose to be either the identity arrow or a failure.-acceptR :: (CategoryCatch (~>), ArrowApply (~>)) => (a -> Bool) -> String -> (a ~> a)+acceptR :: (CategoryCatch arr, ArrowApply arr) => (a -> Bool) -> String -> arr a a acceptR p msg = readerT $ \ a -> if p a then id else failT msg  -- | Look at the argument to an 'Arrow', and choose to be either the identity arrow or a failure. --   This is a generalisation of 'acceptR' to any 'Arrow'.-accepterR :: (CategoryCatch (~>), ArrowApply (~>)) => (a ~> Bool) -> String -> (a ~> a)+accepterR :: (CategoryCatch arr, ArrowApply arr) => arr a Bool -> String -> arr a a accepterR t msg = forkFirst t >>> readerT (\ (b,a) -> if b then constant a else failT msg)  -- | Catch a failing 'CategoryCatch', making it into an identity.-tryR :: CategoryCatch (~>) => (a ~> a) -> (a ~> a)+tryR :: CategoryCatch arr => arr a a -> arr a a tryR r = r <+ id  -- | Catch a failing 'Arrow', making it succeed with a Boolean flag. --   Useful when defining 'Language.KURE.Walker.anyR' instances.-attemptR :: (CategoryCatch (~>), Arrow (~>)) => (a ~> a) -> (a ~> (Bool,a))+attemptR :: (CategoryCatch arr, Arrow arr) => arr a a -> arr a (Bool,a) attemptR r = (r >>^ (True,)) <+ arr (False,)  -- | Makes an 'Arrow' fail if the result value equals the argument value.-changedR :: (CategoryCatch (~>), ArrowApply (~>), Eq a) => (a ~> a) -> (a ~> a)+changedR :: (CategoryCatch arr, ArrowApply arr, Eq a) => arr a a -> arr a a changedR r = readerT (\ a -> r >>> acceptR (/=a) "changedR: value is unchanged")  -- | Repeat a 'CategoryCatch' until it fails, then return the result before the failure. --   Requires at least the first attempt to succeed.-repeatR :: CategoryCatch (~>) => (a ~> a) -> (a ~> a)+repeatR :: CategoryCatch arr => arr a a -> arr a a repeatR r = r >>> tryR (repeatR r)  -- | Attempt two 'Arrow's in sequence, succeeding if one or both succeed.-(>+>) :: (CategoryCatch (~>), ArrowApply (~>)) => (a ~> a) -> (a ~> a) -> (a ~> a)+(>+>) :: (CategoryCatch arr, ArrowApply arr) => arr a a -> arr a a -> arr a a r1 >+> r2 = attemptR r1 >>> readerT (\ (b,_) -> snd ^>> if b then tryR r2 else r2)  -- | Sequence a list of 'Arrow's, succeeding if any succeed.-orR :: (CategoryCatch (~>), ArrowApply (~>)) => [a ~> a] -> (a ~> a)+orR :: (CategoryCatch arr, ArrowApply arr) => [arr a a] -> arr a a orR = foldr (>+>) (failT "orR failed")  -- | Sequence a list of 'Category's, succeeding if all succeed.-andR :: Category (~>) => [a ~> a] -> (a ~> a)+andR :: Category arr => [arr a a] -> arr a a andR = foldr (>>>) id  -- | Select the first 'CategoryCatch' that succeeds, discarding any thereafter.-catchesT :: CategoryCatch (~>) => [a ~> b] -> (a ~> b)+catchesT :: CategoryCatch arr => [arr a b] -> arr a b catchesT = foldr (<+) (failT "catchesT failed")  ------------------------------------------------------------------------------------------  -- | Apply a pure function to the result of an 'Arrow'.-result :: Arrow (~>) => (b -> c) -> (a ~> b) -> (a ~> c)+result :: Arrow arr => (b -> c) -> arr a b -> arr a c result f a = a >>^ f  -- | Apply a pure function to the argument to an 'Arrow'.-argument :: Arrow (~>) => (a -> b) -> (b ~> c) -> (a ~> c)+argument :: Arrow arr => (a -> b) -> arr b c -> arr a c argument f a = f ^>> a  -- | Apply an 'Arrow' to the first element of a pair, discarding the second element.-toFst :: Arrow (~>) => (a ~> b) -> ((a,x) ~> b)+toFst :: Arrow arr => arr a b -> arr (a,x) b toFst f = fst ^>> f  -- | Apply an 'Arrow' to the second element of a pair, discarding the first element.-toSnd :: Arrow (~>) => (a ~> b) -> ((x,a) ~> b)+toSnd :: Arrow arr => arr a b -> arr (x,a) b toSnd f = snd ^>> f  -- | A pure 'Arrow' that swaps the elements of a pair.-swap :: Arrow (~>) => ((a,b) ~> (b,a))+swap :: Arrow arr => arr (a,b) (b,a) swap = arr (\(a,b) -> (b,a))  -- | A pure 'Arrow' that duplicates its argument.-fork :: Arrow (~>) => (a ~> (a,a))+fork :: Arrow arr => arr a (a,a) fork = arr (\a -> (a,a))  -- | Tag the result of an 'Arrow' with its argument.-forkFirst :: Arrow (~>) => (a ~> b) -> (a ~> (b , a))+forkFirst :: Arrow arr => arr a b -> arr a (b,a) forkFirst sf = fork >>> first sf  -- | Tag the result of an 'Arrow' with its argument.-forkSecond :: Arrow (~>) => (a ~> b) -> (a ~> (a , b))+forkSecond :: Arrow arr => arr a b -> arr a (a,b) forkSecond sf = fork >>> second sf  -- | An arrow with a constant result.-constant :: Arrow (~>) => b -> (a ~> b)+constant :: Arrow arr => b -> arr a b constant b = arr (const b)  -------------------------------------------------------------------------------
Language/KURE/Utilities.hs view
@@ -94,7 +94,8 @@  -- | 'KureMonad' is the minimal monad that can be an instance of 'MonadCatch'. instance MonadCatch KureMonad where--- catchM :: KureMonad a -> (a -> KureMonad b) -> KureMonad b+-- catchM :: KureMonad a -> (String -> KureMonad a) -> KureMonad a+    (Success a)   `catchM` _ = Success a    (Failure msg) `catchM` f = f msg 
kure.cabal view
@@ -1,5 +1,5 @@ Name:                kure-Version:             2.4.0+Version:             2.4.2 Synopsis:            Combinators for Strategic Programming Description:	     The Kansas University Rewrite Engine (KURE) is a DSL for strategic rewriting. 	 	     KURE shares concepts with Stratego, but unlike Stratego, KURE is strongly typed.