prelude-generalize 0.1.1 → 0.2
raw patch · 2 files changed
+138/−12 lines, 2 files
Files
- Prelude/Generalize.hs +137/−11
- prelude-generalize.cabal +1/−1
Prelude/Generalize.hs view
@@ -15,7 +15,12 @@ Part2M, Part2(..), Part3M, Part3(..), Part4M, Part4(..), Part5M, Part5(..), Part6M, Part6(..), QuestionMarkOp(..), selectItems, selectBits, hGetByte, hPutByte, module System.IO, (>>=||), (>>=|||), (>>=|\/), (>>=\/), azero, - aplus, unnull + aplus, unnull, on, sortBy, sort, intersperse, intersperse', intercalate, + stripPrefix, stripPrefixBy, isPrefixOf, isSuffixOf, isInfixOf, (\\), nub, + nubBy, deleteF, delete, group, insert, insertBy, intersect, intersectBy, + partition, permutations, subsequences, transpose, union, unionBy, unzip4, + unzip5, unzip6, unzip7, zip4, zip5, zip6, zip7, zipWith4, zipWith5, + zipWith6, zipWith7 ) where { import Prelude hiding (head, tail, (!!), foldr, foldl, length, filter, mapM_, @@ -27,7 +32,8 @@ foldl1, maximum, minimum, product, sum, all, and, or, any, concat, concatMap, elem, foldr1, notElem, (++), iterate, unfoldr, drop, take, find, takeWhile, dropWhile, tails, findIndex, cycle, repeat, replicate, minimumBy, maximumBy, - groupBy, null, concat); + groupBy, null, concat, intersperse, intercalate, delete, deleteBy, (\\), + group); import Data.Foldable hiding (find, concat); import Control.Arrow; import Control.Applicative; @@ -78,7 +84,6 @@ ($) :: f -> i -> o; infixl 0 $; }; -{-# INLINABLE ($) #-}; class Swap f where { swap :: f x y -> f y x; -- swap . swap = id @@ -151,6 +156,66 @@ predP x = Just (pred x); }; + instance Peanoid Int16 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Int16 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + + instance Peanoid Int32 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Int32 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + + instance Peanoid Int64 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Int64 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + + instance Peanoid Word8 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Word8 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + + instance Peanoid Word16 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Word16 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + + instance Peanoid Word32 where { + zeroP = 0; + succP = succ; + }; + + instance Copeanoid Word32 where { + predP 0 = Nothing; + predP x = Just (pred x); + }; + instance Peanoid Bool where { zeroP = False; succP = not; @@ -348,6 +413,27 @@ idQMO = (LT, EQ, GT); }; + instance QuestionMarkOp [x] (a, x -> [x] -> a) a where { + [] ? (x, _) = x; + (h : t) ? (_, x) = x h t; + idQMO = ([], (:)); + }; + + instance QuestionMarkOp (x, y) (x -> y -> a) a where { + (x, y) ? f = f x y; + idQMO = (,); + }; + + instance QuestionMarkOp (x, y, z) (x -> y -> z -> a) a where { + (x, y, z) ? f = f x y z; + idQMO = (,,); + }; + + instance QuestionMarkOp (Identity x) (x -> a) a where { + Identity x ? f = f x; + idQMO = Identity; + }; + bool :: x -> x -> Bool -> x; bool x _ False = x; bool _ x True = x; @@ -366,8 +452,8 @@ foldl (\y x -> y >>= maybe (Left x) Right . predP) (Right n) x; {-# RULES "L.!!" (!!) = (L.!!) #-}; - (!!!) :: (Copeanoid i, Foldable t) => t x -> i -> Maybe x; - x !!! n = either Just (const Nothing) $ + (!!!) :: (Copeanoid i, Foldable t, Alternative f) => t x -> i -> f x; + x !!! n = either pure (const empty) $ foldl (\y x -> y >>= maybe (Left x) Right . predP) (Right n) x; length :: (Peanoid i, Foldable t) => t x -> i; @@ -474,6 +560,10 @@ groupBy f = (uncurry $ maybe id (option . snd)) . foldl (foldGroup f) (Nothing, empty); {-# RULES "L.groupBy" groupBy = L.groupBy #-}; + group :: (Alternative g, Alternative f, Foldable t, Eq a) => t a -> f (g a); + group = groupBy (==); +{-# RULES "L.group" group = L.group #-}; + foldGroup :: (Alternative f, Alternative g) => (a -> a -> Bool) -> (Maybe (a, g a), f (g a)) -> a -> (Maybe (a, g a), f (g a)); foldGroup f (Nothing, x) a = (Just (a, pure a), x); foldGroup f (Just (y, z), x) a = bool (Just (a, pure a), option z x) (Just (a, option a z), x) (f a y); @@ -500,20 +590,26 @@ atLeast x (Peano []) = null $ predP x; atLeast x (Peano (_ : y)) = maybe True (flip atLeast $ Peano y) $ predP x; - (.:) :: Category cat => cat b c -> (a -> cat a1 b) -> a -> cat a1 c; - (.:) = (.) . (.); +-- (.:) :: Category cat => cat b c -> (a -> cat a1 b) -> a -> cat a1 c; +-- (.:) = (.) . (.); + (.:) :: (Category cat, Functor f) => cat b c -> f (cat a b) -> f (cat a c); + (.:) = fmap <$> (.); infixr 9 .:; {-# SPECIALIZE (.:) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c #-}; {-# INLINE (.:) #-}; - (.::) :: Category cat => cat b c -> (a -> a1 -> cat a2 b) -> a -> a1 -> cat a2 c; - (.::) = (.) . (.) . (.); +-- (.::) :: Category cat => cat b c -> (a -> a1 -> cat a2 b) -> a -> a1 -> cat a2 c; +-- (.::) = (.) . (.) . (.); + (.::) :: (Category cat, Functor f, Functor g) => cat b c -> f (g (cat a b)) -> f (g (cat a c)); + (.::) = fmap <$> (.:); infixr 9 .::; {-# SPECIALIZE (.::) :: (b -> c) -> (a -> a1 -> a2 -> b) -> a -> a1 -> a2 -> c #-}; {-# INLINE (.::) #-}; - (.:::) :: Category cat => cat b c -> (a -> a1 -> a2 -> cat a3 b) -> a -> a1 -> a2 -> cat a3 c; - (.:::) = (.) . (.) . (.) . (.); +-- (.:::) :: Category cat => cat b c -> (a -> a1 -> a2 -> cat a3 b) -> a -> a1 -> a2 -> cat a3 c; +-- (.:::) = (.) . (.) . (.) . (.); + (.:::) :: (Category cat, Functor f, Functor g, Functor h) => cat b c -> f (g (h (cat a b))) -> f (g (h (cat a c))); + (.:::) = fmap <$> (.::); infixr 9 .:::; {-# SPECIALIZE (.:::) :: (b -> c) -> (a -> a1 -> a2 -> a3 -> b) -> a -> a1 -> a2 -> a3 -> c #-}; {-# INLINE (.:::) #-}; @@ -581,4 +677,34 @@ aplus :: (Applicative f, Monoid x) => f x -> f x -> f x; aplus = liftA2 mappend; + + intersperse :: MonadLogic m => x -> m x -> m x; + intersperse x = msplit >=> maybe mzero (\(y, z) -> return y ++ intersperse' x z); +{-# RULES "L.intersperse" intersperse = L.intersperse #-}; + + intersperse' :: MonadLogic m => x -> m x -> m x; + intersperse' x = msplit >=> maybe mzero (\(y, z) -> return x ++ return y ++ intersperse' x z); + + intercalate :: MonadLogic m => m x -> m (m x) -> m x; + intercalate = join .: intersperse; +{-# RULES "L.intercalate" intercalate = L.intercalate #-}; + + deleteF :: MonadLogic m => (x -> Bool) -> m x -> m x; + deleteF f = msplit >=> maybe mzero (\(x, y) -> bool (return x ++ deleteF f y) y (f x)); +{-# SPECIALIZE deleteF :: (x -> Bool) -> [x] -> [x] #-}; + + delete :: (Eq x, MonadLogic m) => x -> m x -> m x; + delete = deleteF . (==); +{-# RULES "L.delete" delete = L.delete #-}; + + (\\) :: (MonadLogic m, Foldable t, Eq b) => m b -> t b -> m b; + (\\) = foldl (flip delete); + infixl 5 \\; +{-# RULES "L.\\" (\\) = (L.\\) #-}; + + stripPrefixBy :: (a -> a -> Bool) -> [a] -> [a] -> Maybe [a]; + stripPrefixBy _ [] y = Just y; + stripPrefixBy f (x : xs) (y : ys) | f x y = stripPrefixBy f xs ys; + stripPrefixBy _ _ _ = Nothing; + }
prelude-generalize.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented. -Version: 0.1.1 +Version: 0.2 -- A short (one-line) description of the package. Synopsis: Another kind of alternate Prelude file