{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module GHC.Internal.Data.NonEmpty
( NonEmpty (..)
, map
) where
import GHC.Internal.Base
( Applicative (..), Functor (..), Monad (..), NonEmpty (..)
, Semigroup (..), (++), (.), ap, liftM2
)
map :: (a -> b) -> NonEmpty a -> NonEmpty b
map :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
map a -> b
f (a
a :| [a]
as) = a -> b
f a
a b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
as
instance Semigroup (NonEmpty a) where
(a
a :| [a]
as) <> :: NonEmpty a -> NonEmpty a -> NonEmpty a
<> NonEmpty a
bs = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| ([a]
as [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ NonEmpty a -> [a]
forall {a}. NonEmpty a -> [a]
toList NonEmpty a
bs)
where
toList :: NonEmpty a -> [a]
toList (a
c :| [a]
cs) = a
c a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
cs
instance Functor NonEmpty where
fmap :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
fmap = (a -> b) -> NonEmpty a -> NonEmpty b
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
map
a
b <$ :: forall a b. a -> NonEmpty b -> NonEmpty a
<$ (b
_ :| [b]
as) = a
b a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| (a
b a -> [b] -> [a]
forall a b. a -> [b] -> [a]
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [b]
as)
instance Applicative NonEmpty where
pure :: forall a. a -> NonEmpty a
pure a
a = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
<*> :: forall a b. NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
(<*>) = NonEmpty (a -> b) -> NonEmpty a -> NonEmpty b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
liftA2 :: forall a b c.
(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
liftA2 = (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2
instance Monad NonEmpty where
(a
a :| [a]
as) >>= :: forall a b. NonEmpty a -> (a -> NonEmpty b) -> NonEmpty b
>>= a -> NonEmpty b
f =
case a -> NonEmpty b
f a
a of
b
b :| [b]
bs -> b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| ([b]
bs [b] -> [b] -> [b]
forall a. [a] -> [a] -> [a]
++ [b]
bs')
where
bs' :: [b]
bs' = [a]
as [a] -> (a -> [b]) -> [b]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NonEmpty b -> [b]
forall {a}. NonEmpty a -> [a]
toList (NonEmpty b -> [b]) -> (a -> NonEmpty b) -> a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> NonEmpty b
f
toList :: NonEmpty a -> [a]
toList (a
c :| [a]
cs) = a
c a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
cs