module Control.Arrow.ArrowIf
( module Control.Arrow.ArrowIf
)
where
import Control.Arrow
import Control.Arrow.ArrowList
import Data.List
( partition )
class ArrowList a => ArrowIf a where
ifA :: a b c -> a b d -> a b d -> a b d
ifP :: (b -> Bool) -> a b d -> a b d -> a b d
ifP b -> Bool
p = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA (forall (a :: * -> * -> *) b. ArrowList a => (b -> Bool) -> a b b
isA b -> Bool
p)
{-# INLINE ifP #-}
neg :: a b c -> a b b
neg a b c
f = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
f forall (a :: * -> * -> *) b c. ArrowList a => a b c
none forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE neg #-}
when :: a b b -> a b c -> a b b
a b b
f `when` a b c
g = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
g a b b
f forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE when #-}
whenP :: a b b -> (b -> Bool) -> a b b
a b b
f `whenP` b -> Bool
g = forall (a :: * -> * -> *) b d.
ArrowIf a =>
(b -> Bool) -> a b d -> a b d -> a b d
ifP b -> Bool
g a b b
f forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE whenP #-}
whenNot :: a b b -> a b c -> a b b
a b b
f `whenNot` a b c
g = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
g forall (a :: * -> * -> *) b. ArrowList a => a b b
this a b b
f
{-# INLINE whenNot #-}
whenNotP :: a b b -> (b -> Bool) -> a b b
a b b
f `whenNotP` b -> Bool
g = forall (a :: * -> * -> *) b d.
ArrowIf a =>
(b -> Bool) -> a b d -> a b d -> a b d
ifP b -> Bool
g forall (a :: * -> * -> *) b. ArrowList a => a b b
this a b b
f
{-# INLINE whenNotP #-}
guards :: a b c -> a b d -> a b d
a b c
f `guards` a b d
g = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
f a b d
g forall (a :: * -> * -> *) b c. ArrowList a => a b c
none
{-# INLINE guards #-}
guardsP :: (b -> Bool) -> a b d -> a b d
b -> Bool
f `guardsP` a b d
g = forall (a :: * -> * -> *) b d.
ArrowIf a =>
(b -> Bool) -> a b d -> a b d -> a b d
ifP b -> Bool
f a b d
g forall (a :: * -> * -> *) b c. ArrowList a => a b c
none
{-# INLINE guardsP #-}
filterA :: a b c -> a b b
filterA a b c
f = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
f forall (a :: * -> * -> *) b. ArrowList a => a b b
this forall (a :: * -> * -> *) b c. ArrowList a => a b c
none
{-# INLINE filterA #-}
containing :: a b c -> a c d -> a b c
a b c
f `containing` a c d
g = a b c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a c d
g forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d
`guards` forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE containing #-}
notContaining :: a b c -> a c d -> a b c
a b c
f `notContaining` a c d
g = a b c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a c d
g forall (a :: * -> * -> *) b c. ArrowList a => a b c
none forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE notContaining #-}
orElse :: a b c -> a b c -> a b c
choiceA :: [IfThen (a b c) (a b d)] -> a b d
choiceA = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {a :: * -> * -> *} {b} {c} {d}.
ArrowIf a =>
IfThen (a b c) (a b d) -> a b d -> a b d
ifA' forall (a :: * -> * -> *) b c. ArrowList a => a b c
none
where
ifA' :: IfThen (a b c) (a b d) -> a b d -> a b d
ifA' (a b c
g :-> a b d
f) = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
g a b d
f
tagA :: a b c -> a b (Either b b)
tagA a b c
p = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA a b c
p (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. a -> Either a b
Left) (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. b -> Either a b
Right)
spanA :: a b b -> a [b] ([b],[b])
spanA a b b
p = forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d -> a b d
ifA ( forall (a :: * -> * -> *) b c. ArrowList a => (b -> [c]) -> a b c
arrL (forall a. Int -> [a] -> [a]
take Int
1) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a b b
p )
( forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a. [a] -> a
head forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a. [a] -> [a]
tail forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b. ArrowIf a => a b b -> a [b] ([b], [b])
spanA a b b
p)
forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\ ~(b
x, ~([b]
xs,[b]
ys)) -> (b
x forall a. a -> [a] -> [a]
: [b]
xs, [b]
ys))
)
( forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\ [b]
l -> ([],[b]
l)) )
partitionA :: a b b -> a [b] ([b],[b])
partitionA a b b
p = forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA ( forall (a :: * -> * -> *) b c. ArrowList a => (b -> [c]) -> a b c
arrL forall a. a -> a
id forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c.
ArrowIf a =>
a b c -> a b (Either b b)
tagA a b b
p )
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^
( (\ ~([Either b b]
l1, [Either b b]
l2) -> (forall {b}. [Either b b] -> [b]
unTag [Either b b]
l1, forall {b}. [Either b b] -> [b]
unTag [Either b b]
l2) ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (forall {a} {b}. Either a b -> Bool
isLeft) )
where
isLeft :: Either a b -> Bool
isLeft (Left a
_) = Bool
True
isLeft Either a b
_ = Bool
False
unTag :: [Either b b] -> [b]
unTag = forall a b. (a -> b) -> [a] -> [b]
map (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id forall a. a -> a
id)
data IfThen a b = a :-> b