{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TupleSections #-}
module Control.Applicative.Combinators
(
(<|>),
many,
some,
optional,
empty,
between,
choice,
count,
count',
eitherP,
endBy,
endBy1,
manyTill,
manyTill_,
someTill,
someTill_,
option,
sepBy,
sepBy1,
sepEndBy,
sepEndBy1,
skipMany,
skipSome,
skipCount,
skipManyTill,
skipSomeTill,
)
where
import Control.Applicative
import Control.Monad (replicateM, replicateM_)
import Data.Foldable
between :: Applicative m => m open -> m close -> m a -> m a
between :: m open -> m close -> m a -> m a
between m open
open m close
close m a
p = m open
open m open -> m a -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a
p m a -> m close -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* m close
close
{-# INLINE between #-}
choice :: (Foldable f, Alternative m) => f (m a) -> m a
choice :: f (m a) -> m a
choice = f (m a) -> m a
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
{-# INLINE choice #-}
count :: Applicative m => Int -> m a -> m [a]
count :: Int -> m a -> m [a]
count = Int -> m a -> m [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM
{-# INLINE count #-}
count' :: Alternative m => Int -> Int -> m a -> m [a]
count' :: Int -> Int -> m a -> m [a]
count' Int
m' Int
n' m a
p = Int -> Int -> m [a]
forall a. (Ord a, Num a) => a -> a -> m [a]
go Int
m' Int
n'
where
go :: a -> a -> m [a]
go !a
m !a
n
| a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
|| a
m a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
n = [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
| a
m a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p (a -> a -> m [a]
go (a
m a -> a -> a
forall a. Num a => a -> a -> a
- a
1) (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1))
| Bool
otherwise = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p (a -> a -> m [a]
go a
0 (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1)) m [a] -> m [a] -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# INLINE count' #-}
eitherP :: Alternative m => m a -> m b -> m (Either a b)
eitherP :: m a -> m b -> m (Either a b)
eitherP m a
a m b
b = (a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> m a -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
a) m (Either a b) -> m (Either a b) -> m (Either a b)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> m b -> m (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
b)
{-# INLINE eitherP #-}
endBy :: Alternative m => m a -> m sep -> m [a]
endBy :: m a -> m sep -> m [a]
endBy m a
p m sep
sep = m a -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (m a
p m a -> m sep -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* m sep
sep)
{-# INLINE endBy #-}
endBy1 :: Alternative m => m a -> m sep -> m [a]
endBy1 :: m a -> m sep -> m [a]
endBy1 m a
p m sep
sep = m a -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (m a
p m a -> m sep -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* m sep
sep)
{-# INLINE endBy1 #-}
manyTill :: Alternative m => m a -> m end -> m [a]
manyTill :: m a -> m end -> m [a]
manyTill m a
p m end
end = m [a]
go
where
go :: m [a]
go = ([] [a] -> m end -> m [a]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ m end
end) m [a] -> m [a] -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p m [a]
go
{-# INLINE manyTill #-}
manyTill_ :: Alternative m => m a -> m end -> m ([a], end)
manyTill_ :: m a -> m end -> m ([a], end)
manyTill_ m a
p m end
end = m ([a], end)
go
where
go :: m ([a], end)
go = (([],) (end -> ([a], end)) -> m end -> m ([a], end)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m end
end) m ([a], end) -> m ([a], end) -> m ([a], end)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> ([a], end) -> ([a], end))
-> m a -> m ([a], end) -> m ([a], end)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
x ([a]
xs, end
y) -> (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs, end
y)) m a
p m ([a], end)
go
{-# INLINE manyTill_ #-}
someTill :: Alternative m => m a -> m end -> m [a]
someTill :: m a -> m end -> m [a]
someTill m a
p m end
end = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p (m a -> m end -> m [a]
forall (m :: * -> *) a end. Alternative m => m a -> m end -> m [a]
manyTill m a
p m end
end)
{-# INLINE someTill #-}
someTill_ :: Alternative m => m a -> m end -> m ([a], end)
someTill_ :: m a -> m end -> m ([a], end)
someTill_ m a
p m end
end =
(a -> ([a], end) -> ([a], end))
-> m a -> m ([a], end) -> m ([a], end)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a
x ([a]
xs, end
y) -> (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs, end
y)) m a
p (m a -> m end -> m ([a], end)
forall (m :: * -> *) a end.
Alternative m =>
m a -> m end -> m ([a], end)
manyTill_ m a
p m end
end)
{-# INLINE someTill_ #-}
option :: Alternative m => a -> m a -> m a
option :: a -> m a -> m a
option a
x m a
p = m a
p m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# INLINE option #-}
sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy :: m a -> m sep -> m [a]
sepBy m a
p m sep
sep = m a -> m sep -> m [a]
forall (m :: * -> *) a end. Alternative m => m a -> m end -> m [a]
sepBy1 m a
p m sep
sep m [a] -> m [a] -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# INLINE sepBy #-}
sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 :: m a -> m sep -> m [a]
sepBy1 m a
p m sep
sep = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p (m a -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (m sep
sep m sep -> m a -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a
p))
{-# INLINE sepBy1 #-}
sepEndBy :: Alternative m => m a -> m sep -> m [a]
sepEndBy :: m a -> m sep -> m [a]
sepEndBy m a
p m sep
sep = m a -> m sep -> m [a]
forall (m :: * -> *) a end. Alternative m => m a -> m end -> m [a]
sepEndBy1 m a
p m sep
sep m [a] -> m [a] -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# INLINE sepEndBy #-}
sepEndBy1 :: Alternative m => m a -> m sep -> m [a]
sepEndBy1 :: m a -> m sep -> m [a]
sepEndBy1 m a
p m sep
sep = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
p ((m sep
sep m sep -> m [a] -> m [a]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a -> m sep -> m [a]
forall (m :: * -> *) a end. Alternative m => m a -> m end -> m [a]
sepEndBy m a
p m sep
sep) m [a] -> m [a] -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
{-# INLINEABLE sepEndBy1 #-}
skipMany :: Alternative m => m a -> m ()
skipMany :: m a -> m ()
skipMany m a
p = m ()
go
where
go :: m ()
go = (m a
p m a -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m ()
go) m () -> m () -> m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE skipMany #-}
skipSome :: Alternative m => m a -> m ()
skipSome :: m a -> m ()
skipSome m a
p = m a
p m a -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a -> m ()
forall (m :: * -> *) a. Alternative m => m a -> m ()
skipMany m a
p
{-# INLINE skipSome #-}
skipCount :: Applicative m => Int -> m a -> m ()
skipCount :: Int -> m a -> m ()
skipCount = Int -> m a -> m ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_
{-# INLINE skipCount #-}
skipManyTill :: Alternative m => m a -> m end -> m end
skipManyTill :: m a -> m end -> m end
skipManyTill m a
p m end
end = m end
go
where
go :: m end
go = m end
end m end -> m end -> m end
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (m a
p m a -> m end -> m end
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m end
go)
{-# INLINE skipManyTill #-}
skipSomeTill :: Alternative m => m a -> m end -> m end
skipSomeTill :: m a -> m end -> m end
skipSomeTill m a
p m end
end = m a
p m a -> m end -> m end
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a -> m end -> m end
forall (m :: * -> *) a end. Alternative m => m a -> m end -> m end
skipManyTill m a
p m end
end
{-# INLINE skipSomeTill #-}