{- ORMOLU_DISABLE -}
-- Options passed to GHC
{-# OPTIONS_GHC -O2 #-}
-- Options passed to Haddock
{-# OPTIONS_HADDOCK hide #-}

-----------------------------------------------------------------------------

{-# LANGUAGE CPP #-}

-- CPP: GHC >= 7.8 for Safe Haskell
#if __GLASGOW_HASKELL__ >= 708
{-

This module imports the unsafe module 'GHC.Exts' for 'IsList' but does not use
any unsafe features. Therefore, we mark the module as trustworthy.

-}
{-# LANGUAGE Trustworthy #-}
#endif

-- For the IsList and IsString instances
{-# LANGUAGE TypeFamilies #-}

-----------------------------------------------------------------------------

{-|

Module: Data.DList.DNonEmpty.Internal
Copyright: © 2017-2020 Oleg Grenrus, 2020 Sean Leather
License: BSD-3-Clause

Maintainer: sean.leather@gmail.com
Stability: stable

This module includes everything related to 'DNonEmpty' and is not exposed to
users of the @dlist@ package.

-}
{- ORMOLU_ENABLE -}

module Data.DList.DNonEmpty.Internal where

-----------------------------------------------------------------------------

import qualified Control.Applicative as Applicative
import Control.DeepSeq (NFData (..))
import qualified Control.Monad as Monad
import Data.DList (DList)
import qualified Data.DList as DList
import qualified Data.Foldable as Foldable
import Data.Function (on)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Semigroup as Semigroup
import Data.String (IsString (..))
import qualified GHC.Exts as Exts
import qualified Text.Read as Read
import Prelude hiding (head, map, tail)

-----------------------------------------------------------------------------

{- ORMOLU_DISABLE -}
{-|

A non-empty difference list is a pair of a 'head' element and a (possibly empty)
difference list.

Just as 'DList' is a representation of a list, so is @DNonEmpty@ a
representation of a 'NonEmpty'. @DNonEmpty@ supports \(\mathcal{O}\)(@1@)
'append' and 'snoc' operations, making it useful for replacing frequent
applications of 'Semigroup.<>' on 'NonEmpty' (which is implemented with '++'),
especially if those uses are left-nested (e.g.&#x00A0;@(a __'Semigroup.<>'__ b)
'Semigroup.<>' c@ ).

Unlike 'DList', @DNonEmpty@ is not an abstract type: its constructor is
exported. An alternative definition of @DNonEmpty@ is:

@
newtype DNonEmpty a = DNonEmpty ([a] -> 'NonEmpty' a)
@

This type would need to be abstract to avoid producing @DNonEmpty@ values that
are not isomorphic to 'NonEmpty' values. However, this type would also require
some functions (such as 'map') to be implemented with 'fromNonEmpty' (and thus
'++'), which could introduce efficiencies.

-}
{- ORMOLU_ENABLE -}

infixr 5 :|

data DNonEmpty a = a :| DList a

{- ORMOLU_DISABLE -}
{-|

__@fromNonEmpty xs@__ is a 'DNonEmpty' representing the 'NonEmpty' __@xs@__.

@fromNonEmpty@ obeys the laws:

@
'toNonEmpty' . __fromNonEmpty__ = 'id'
__fromNonEmpty__ . 'toNonEmpty' = 'id'
@

As with 'DList.fromList', this function is implemented with '++'. Repeated uses
of @fromNonEmpty@ are just as inefficient as repeated uses of '++'. If you find
yourself doing some form of the following (possibly indirectly), you may not be
taking advantage of the 'DNonEmpty' representation and library:

@
__fromNonEmpty__ . f . 'toNonEmpty'
@

More likely, you will convert from a 'NonEmpty', perform some operation on the
'DNonEmpty', and convert back to a 'NonEmpty':

@
'toNonEmpty' . g . __fromNonEmpty__
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE fromNonEmpty #-}
fromNonEmpty :: NonEmpty a -> DNonEmpty a
fromNonEmpty :: forall a. NonEmpty a -> DNonEmpty a
fromNonEmpty ~(a
x NonEmpty.:| [a]
xs) = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| [a] -> DList a
forall a. [a] -> DList a
DList.fromList [a]
xs

{- ORMOLU_DISABLE -}
{-|

__@toNonEmpty xs@__ is the 'NonEmpty' represented by __@xs@__.

@toNonEmpty@ obeys the laws:

@
__toNonEmpty__ . 'fromNonEmpty' = 'id'
'fromNonEmpty' . __toNonEmpty__ = 'id'
@

As with 'DList.toList', evaluating @toNonEmpty xs@ may “collapse” the chain of
function composition underlying many 'DList' functions ('DList.append' in
particular) used to construct the 'tail' of @xs@. This may affect any efficiency
you achieved due to laziness in the construction.

-}
{- ORMOLU_ENABLE -}

{-# INLINE toNonEmpty #-}
toNonEmpty :: DNonEmpty a -> NonEmpty a
toNonEmpty :: forall a. DNonEmpty a -> NonEmpty a
toNonEmpty ~(a
x :| DList a
xs) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
NonEmpty.:| DList a -> [a]
forall a. DList a -> [a]
DList.toList DList a
xs

{- ORMOLU_DISABLE -}
{-|

__@toDList xs@__ is the non-empty 'DList' represented by __@xs@__.

@toDList@ obeys the law:

@
__toDList__ (x ':|' xs) = 'DList.cons' x xs
@

Note that this function is used only in this module.

-}
{- ORMOLU_ENABLE -}

toDList :: DNonEmpty a -> DList a
toDList :: forall a. DNonEmpty a -> DList a
toDList ~(a
x :| DList a
xs) = a -> DList a -> DList a
forall a. a -> DList a -> DList a
DList.cons a
x DList a
xs

{- ORMOLU_DISABLE -}
{-|

__@toList xs@__ is the non-empty list represented by __@xs@__.

@toList@ obeys the law:

@
__toList__ xs = 'NonEmpty.toList' ('toNonEmpty' xs)
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE toList #-}
toList :: DNonEmpty a -> [a]
toList :: forall a. DNonEmpty a -> [a]
toList = DList a -> [a]
forall a. DList a -> [a]
DList.toList (DList a -> [a]) -> (DNonEmpty a -> DList a) -> DNonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> DList a
forall a. DNonEmpty a -> DList a
toDList

{- ORMOLU_DISABLE -}
{-|

__@fromList xs@__ is a 'DNonEmpty' representing the list __@xs@__. If @xs@ is
empty, an 'error' is raised.

@fromList@ obeys the law:

@
__fromList__ xs = 'fromNonEmpty' ('NonEmpty.fromList' xs)
@

-}
{- ORMOLU_ENABLE -}

fromList :: [a] -> DNonEmpty a
fromList :: forall a. [a] -> DNonEmpty a
fromList (a
x : [a]
xs) = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| [a] -> DList a
forall a. [a] -> DList a
DList.fromList [a]
xs
fromList [] = [Char] -> DNonEmpty a
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.DList.DNonEmpty.fromList: empty list"

{- ORMOLU_DISABLE -}
{-|

__@singleton x@__ is a 'DNonEmpty' with the single element __@x@__.

@singleton@ obeys the law:

@
'toNonEmpty' (__singleton__ x) = x 'NonEmpty.:|' []
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE singleton #-}
singleton :: a -> DNonEmpty a
singleton :: forall a. a -> DNonEmpty a
singleton a
x = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| DList a
forall a. DList a
DList.empty

{- ORMOLU_DISABLE -}
{-|

__@cons x xs@__ is a 'DNonEmpty' with the 'head' __@x@__ and the 'tail' __@xs@__.

\(\mathcal{O}\)(@1@).

@cons@ obeys the law:

@
'toNonEmpty' (__cons__ x xs) = 'NonEmpty.cons' x ('toNonEmpty' xs)
@

-}
{- ORMOLU_ENABLE -}

infixr 9 `cons`

{-# INLINE cons #-}
cons :: a -> DNonEmpty a -> DNonEmpty a
cons :: forall a. a -> DNonEmpty a -> DNonEmpty a
cons a
x ~(a
y :| DList a
ys) = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| a -> DList a -> DList a
forall a. a -> DList a -> DList a
DList.cons a
y DList a
ys

infixl 9 `snoc`

{- ORMOLU_DISABLE -}
{-|

__@snoc xs x@__ is a 'DNonEmpty' with the initial 'DNonEmpty' __@xs@__ and the
last element __@x@__.

\(\mathcal{O}\)(@1@).

@snoc@ obeys the law:

@
'toNonEmpty' (__snoc__ xs x) = 'toNonEmpty' xs 'Semigroup.<>' (x 'NonEmpty.:|' [])
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE snoc #-}
snoc :: DNonEmpty a -> a -> DNonEmpty a
snoc :: forall a. DNonEmpty a -> a -> DNonEmpty a
snoc ~(a
x :| DList a
xs) a
y = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| DList a -> a -> DList a
forall a. DList a -> a -> DList a
DList.snoc DList a
xs a
y

{- ORMOLU_DISABLE -}
{-|

__@append xs ys@__ is a 'DNonEmpty' obtained from the concatenation of the
elements of __@xs@__ and __@ys@__.

\(\mathcal{O}\)(@1@).

@append@ obeys the law:

@
'toNonEmpty' (__append__ xs ys) = 'toNonEmpty' xs 'Semigroup.<>' 'toNonEmpty' ys
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE append #-}
append :: DNonEmpty a -> DNonEmpty a -> DNonEmpty a
append :: forall a. DNonEmpty a -> DNonEmpty a -> DNonEmpty a
append (a
x :| DList a
xs) ~(a
y :| DList a
ys) = a
x a -> DList a -> DNonEmpty a
forall a. a -> DList a -> DNonEmpty a
:| DList a -> DList a -> DList a
forall a. DList a -> DList a -> DList a
DList.append DList a
xs (a -> DList a -> DList a
forall a. a -> DList a -> DList a
DList.cons a
y DList a
ys)

{- ORMOLU_DISABLE -}
{-|

__@head xs@__ is the first element of __@xs@__.

\(\mathcal{O}\)(@1@).

@head@ obeys the law:

@
__head__ xs = 'NonEmpty.head' ('toNonEmpty' xs)
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE head #-}
head :: DNonEmpty a -> a
head :: forall a. DNonEmpty a -> a
head ~(a
x :| DList a
_) = a
x

{- ORMOLU_DISABLE -}
{-|

__@tail xs@__ is a 'DList' of the elements in __@xs@__ excluding the first
element.

\(\mathcal{O}\)(@1@).

@tail@ obeys the law:

@
'DList.toList' (__tail__ xs) = 'NonEmpty.tail' ('toNonEmpty' xs)
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE tail #-}
tail :: DNonEmpty a -> DList a
tail :: forall a. DNonEmpty a -> DList a
tail ~(a
_ :| DList a
xs) = DList a
xs

{- ORMOLU_DISABLE -}
{-|

__@unfoldr f z@__ is the 'DNonEmpty' constructed from the recursive application
of __@f@__. The recursion starts with the seed value __@z@__ and ends when, for
some @z' : b@, @f z' == 'Nothing'@.

\(\mathcal{O}\)(@'NonEmpty.length' ('NonEmpty.unfoldr' f z)@).

@unfoldr@ obeys the law:

@
'toNonEmpty' (__unfoldr__ f z) = 'NonEmpty.unfoldr' f z
@

-}
{- ORMOLU_ENABLE -}

unfoldr :: (b -> (a, Maybe b)) -> b -> DNonEmpty a
unfoldr :: forall b a. (b -> (a, Maybe b)) -> b -> DNonEmpty a
unfoldr b -> (a, Maybe b)
f b
z =
  case b -> (a, Maybe b)
f b
z of
    (a
x, Maybe b
Nothing) -> a -> DNonEmpty a
forall a. a -> DNonEmpty a
singleton a
x
    (a
x, Just b
z') -> a -> DNonEmpty a -> DNonEmpty a
forall a. a -> DNonEmpty a -> DNonEmpty a
cons a
x (DNonEmpty a -> DNonEmpty a) -> DNonEmpty a -> DNonEmpty a
forall a b. (a -> b) -> a -> b
$ (b -> (a, Maybe b)) -> b -> DNonEmpty a
forall b a. (b -> (a, Maybe b)) -> b -> DNonEmpty a
unfoldr b -> (a, Maybe b)
f b
z'

{- ORMOLU_DISABLE -}
{-|

__@map f xs@__ is the 'DNonEmpty' obtained by applying __@f@__ to each element
of __@xs@__.

\(\mathcal{O}\)(@'NonEmpty.length' ('toNonEmpty' xs)@).

@map@ obeys the law:

@
'toNonEmpty' (__map__ f xs) = 'NonEmpty.map' f ('toNonEmpty' xs)
@

-}
{- ORMOLU_ENABLE -}

{-# INLINE map #-}
map :: (a -> b) -> DNonEmpty a -> DNonEmpty b
map :: forall a b. (a -> b) -> DNonEmpty a -> DNonEmpty b
map a -> b
f ~(a
x :| DList a
xs) = a -> b
f a
x b -> DList b -> DNonEmpty b
forall a. a -> DList a -> DNonEmpty a
:| (a -> b) -> DList a -> DList b
forall a b. (a -> b) -> DList a -> DList b
DList.map a -> b
f DList a
xs

instance Eq a => Eq (DNonEmpty a) where
  == :: DNonEmpty a -> DNonEmpty a -> Bool
(==) = NonEmpty a -> NonEmpty a -> Bool
forall a. Eq a => a -> a -> Bool
(==) (NonEmpty a -> NonEmpty a -> Bool)
-> (DNonEmpty a -> NonEmpty a)
-> DNonEmpty a
-> DNonEmpty a
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

instance Ord a => Ord (DNonEmpty a) where
  compare :: DNonEmpty a -> DNonEmpty a -> Ordering
compare = NonEmpty a -> NonEmpty a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (NonEmpty a -> NonEmpty a -> Ordering)
-> (DNonEmpty a -> NonEmpty a)
-> DNonEmpty a
-> DNonEmpty a
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

instance Read a => Read (DNonEmpty a) where
  readPrec :: ReadPrec (DNonEmpty a)
readPrec = ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a)
forall a. ReadPrec a -> ReadPrec a
Read.parens (ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a))
-> ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a)
forall a b. (a -> b) -> a -> b
$
    Int -> ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a)
forall a. Int -> ReadPrec a -> ReadPrec a
Read.prec Int
10 (ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a))
-> ReadPrec (DNonEmpty a) -> ReadPrec (DNonEmpty a)
forall a b. (a -> b) -> a -> b
$ do
      Read.Ident [Char]
"fromNonEmpty" <- ReadPrec Lexeme
Read.lexP
      NonEmpty a
dl <- ReadPrec (NonEmpty a)
forall a. Read a => ReadPrec a
Read.readPrec
      DNonEmpty a -> ReadPrec (DNonEmpty a)
forall a. a -> ReadPrec a
forall (m :: * -> *) a. Monad m => a -> m a
return (DNonEmpty a -> ReadPrec (DNonEmpty a))
-> DNonEmpty a -> ReadPrec (DNonEmpty a)
forall a b. (a -> b) -> a -> b
$ NonEmpty a -> DNonEmpty a
forall a. NonEmpty a -> DNonEmpty a
fromNonEmpty NonEmpty a
dl
  readListPrec :: ReadPrec [DNonEmpty a]
readListPrec = ReadPrec [DNonEmpty a]
forall a. Read a => ReadPrec [a]
Read.readListPrecDefault

instance Show a => Show (DNonEmpty a) where
  showsPrec :: Int -> DNonEmpty a -> ShowS
showsPrec Int
p DNonEmpty a
dl =
    Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
      [Char] -> ShowS
showString [Char]
"fromNonEmpty " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> ShowS
forall a. Show a => a -> ShowS
shows (DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty DNonEmpty a
dl)

instance Functor DNonEmpty where
  {-# INLINE fmap #-}
  fmap :: forall a b. (a -> b) -> DNonEmpty a -> DNonEmpty b
fmap = (a -> b) -> DNonEmpty a -> DNonEmpty b
forall a b. (a -> b) -> DNonEmpty a -> DNonEmpty b
map

instance Applicative.Applicative DNonEmpty where
  {-# INLINE pure #-}
  pure :: forall a. a -> DNonEmpty a
pure = a -> DNonEmpty a
forall a. a -> DNonEmpty a
singleton

  {-# INLINE (<*>) #-}
  <*> :: forall a b. DNonEmpty (a -> b) -> DNonEmpty a -> DNonEmpty b
(<*>) = DNonEmpty (a -> b) -> DNonEmpty a -> DNonEmpty b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
Monad.ap

instance Monad DNonEmpty where
  ~(a
x :| DList a
xs) >>= :: forall a b. DNonEmpty a -> (a -> DNonEmpty b) -> DNonEmpty b
>>= a -> DNonEmpty b
k = b
y b -> DList b -> DNonEmpty b
forall a. a -> DList a -> DNonEmpty a
:| DList b -> DList b -> DList b
forall a. DList a -> DList a -> DList a
DList.append DList b
ys (DList a
xs DList a -> (a -> DList b) -> DList b
forall a b. DList a -> (a -> DList b) -> DList b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DNonEmpty b -> DList b
forall a. DNonEmpty a -> DList a
toDList (DNonEmpty b -> DList b) -> (a -> DNonEmpty b) -> a -> DList b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DNonEmpty b
k)
    where
      b
y :| DList b
ys = a -> DNonEmpty b
k a
x

  {-# INLINE return #-}
  return :: forall a. a -> DNonEmpty a
return = a -> DNonEmpty a
forall a. a -> DNonEmpty a
forall (f :: * -> *) a. Applicative f => a -> f a
Applicative.pure

instance Foldable.Foldable DNonEmpty where
  {-# INLINE fold #-}
  fold :: forall m. Monoid m => DNonEmpty m -> m
fold = NonEmpty m -> m
forall m. Monoid m => NonEmpty m -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
Foldable.fold (NonEmpty m -> m)
-> (DNonEmpty m -> NonEmpty m) -> DNonEmpty m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty m -> NonEmpty m
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldMap #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> DNonEmpty a -> m
foldMap a -> m
f = (a -> m) -> NonEmpty a -> m
forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap a -> m
f (NonEmpty a -> m)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldr #-}
  foldr :: forall a b. (a -> b -> b) -> b -> DNonEmpty a -> b
foldr a -> b -> b
f b
x = (a -> b -> b) -> b -> NonEmpty a -> b
forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr a -> b -> b
f b
x (NonEmpty a -> b)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldl #-}
  foldl :: forall b a. (b -> a -> b) -> b -> DNonEmpty a -> b
foldl b -> a -> b
f b
x = (b -> a -> b) -> b -> NonEmpty a -> b
forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl b -> a -> b
f b
x (NonEmpty a -> b)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldr1 #-}
  foldr1 :: forall a. (a -> a -> a) -> DNonEmpty a -> a
foldr1 a -> a -> a
f = (a -> a -> a) -> NonEmpty a -> a
forall a. (a -> a -> a) -> NonEmpty a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldr1 a -> a -> a
f (NonEmpty a -> a)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldl1 #-}
  foldl1 :: forall a. (a -> a -> a) -> DNonEmpty a -> a
foldl1 a -> a -> a
f = (a -> a -> a) -> NonEmpty a -> a
forall a. (a -> a -> a) -> NonEmpty a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldl1 a -> a -> a
f (NonEmpty a -> a)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldl' #-}
  foldl' :: forall b a. (b -> a -> b) -> b -> DNonEmpty a -> b
foldl' b -> a -> b
f b
x = (b -> a -> b) -> b -> NonEmpty a -> b
forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' b -> a -> b
f b
x (NonEmpty a -> b)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE foldr' #-}
  foldr' :: forall a b. (a -> b -> b) -> b -> DNonEmpty a -> b
foldr' a -> b -> b
f b
x = (a -> b -> b) -> b -> NonEmpty a -> b
forall a b. (a -> b -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' a -> b -> b
f b
x (NonEmpty a -> b)
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

  {-# INLINE toList #-}
  toList :: forall a. DNonEmpty a -> [a]
toList = DNonEmpty a -> [a]
forall a. DNonEmpty a -> [a]
toList

instance NFData a => NFData (DNonEmpty a) where
  {-# INLINE rnf #-}
  rnf :: DNonEmpty a -> ()
rnf = NonEmpty a -> ()
forall a. NFData a => a -> ()
rnf (NonEmpty a -> ())
-> (DNonEmpty a -> NonEmpty a) -> DNonEmpty a -> ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DNonEmpty a -> NonEmpty a
forall a. DNonEmpty a -> NonEmpty a
toNonEmpty

{-

The 'IsString' instance is _not_ a flexible instance to allow certain uses of
overloaded strings. See tests/OverloadedStrings.hs for an example and
https://gitlab.haskell.org/ghc/ghc/-/commit/b225b234a6b11e42fef433dcd5d2a38bb4b466bf
for the same change made to the IsString instance for lists.

-}

instance a ~ Char => IsString (DNonEmpty a) where
  {-# INLINE fromString #-}
  fromString :: [Char] -> DNonEmpty a
fromString = [Char] -> DNonEmpty a
[Char] -> DNonEmpty Char
forall a. [a] -> DNonEmpty a
fromList

instance Exts.IsList (DNonEmpty a) where
  type Item (DNonEmpty a) = a

  {-# INLINE fromList #-}
  fromList :: [Item (DNonEmpty a)] -> DNonEmpty a
fromList = [a] -> DNonEmpty a
[Item (DNonEmpty a)] -> DNonEmpty a
forall a. [a] -> DNonEmpty a
fromList

  {-# INLINE toList #-}
  toList :: DNonEmpty a -> [Item (DNonEmpty a)]
toList = DNonEmpty a -> [a]
DNonEmpty a -> [Item (DNonEmpty a)]
forall a. DNonEmpty a -> [a]
toList

instance Semigroup.Semigroup (DNonEmpty a) where
  {-# INLINE (<>) #-}
  <> :: DNonEmpty a -> DNonEmpty a -> DNonEmpty a
(<>) = DNonEmpty a -> DNonEmpty a -> DNonEmpty a
forall a. DNonEmpty a -> DNonEmpty a -> DNonEmpty a
append