{-# 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
         )

-- The following were moved here from module Data.List.NonEmpty of the base
-- package: map.

-- | Map a function over a 'NonEmpty' stream.
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

-- The following orphan instances were moved here from module GHC.Internal.Base:
-- Semigroup, Functor, Applicative and Monad.

-- | @since base-4.9.0.0
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

-- | @since base-4.9.0.0
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)

-- | @since base-4.9.0.0
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

-- | @since base-4.9.0.0
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