{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE Trustworthy #-}
#endif
{-# LANGUAGE TypeFamilies #-}
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)
infixr 5 :|
data DNonEmpty a = a :| DList a
{-# INLINE fromNonEmpty #-}
fromNonEmpty :: NonEmpty a -> DNonEmpty a
fromNonEmpty :: forall a. NonEmpty a -> DNonEmpty a
fromNonEmpty ~(a
x NonEmpty.:| [a]
xs) = a
x forall a. a -> DList a -> DNonEmpty a
:| forall a. [a] -> DList a
DList.fromList [a]
xs
{-# INLINE toNonEmpty #-}
toNonEmpty :: DNonEmpty a -> NonEmpty a
toNonEmpty :: forall a. DNonEmpty a -> NonEmpty a
toNonEmpty ~(a
x :| DList a
xs) = a
x forall a. a -> [a] -> NonEmpty a
NonEmpty.:| forall a. DList a -> [a]
DList.toList DList a
xs
toDList :: DNonEmpty a -> DList a
toDList :: forall a. DNonEmpty a -> DList a
toDList ~(a
x :| DList a
xs) = forall a. a -> DList a -> DList a
DList.cons a
x DList a
xs
{-# INLINE toList #-}
toList :: DNonEmpty a -> [a]
toList :: forall a. DNonEmpty a -> [a]
toList = forall a. DList a -> [a]
DList.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> DList a
toDList
fromList :: [a] -> DNonEmpty a
fromList :: forall a. [a] -> DNonEmpty a
fromList (a
x : [a]
xs) = a
x forall a. a -> DList a -> DNonEmpty a
:| forall a. [a] -> DList a
DList.fromList [a]
xs
fromList [] = forall a. HasCallStack => [Char] -> a
error [Char]
"Data.DList.DNonEmpty.fromList: empty list"
{-# INLINE singleton #-}
singleton :: a -> DNonEmpty a
singleton :: forall a. a -> DNonEmpty a
singleton a
x = a
x forall a. a -> DList a -> DNonEmpty a
:| forall a. DList a
DList.empty
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 forall a. a -> DList a -> DNonEmpty a
:| forall a. a -> DList a -> DList a
DList.cons a
y DList a
ys
infixl 9 `snoc`
{-# 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 forall a. a -> DList a -> DNonEmpty a
:| forall a. DList a -> a -> DList a
DList.snoc DList a
xs a
y
{-# 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 forall a. a -> DList a -> DNonEmpty a
:| forall a. DList a -> DList a -> DList a
DList.append DList a
xs (forall a. a -> DList a -> DList a
DList.cons a
y DList a
ys)
{-# INLINE head #-}
head :: DNonEmpty a -> a
head :: forall a. DNonEmpty a -> a
head ~(a
x :| DList a
_) = a
x
{-# INLINE tail #-}
tail :: DNonEmpty a -> DList a
tail :: forall a. DNonEmpty a -> DList a
tail ~(a
_ :| DList a
xs) = DList a
xs
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) -> forall a. a -> DNonEmpty a
singleton a
x
(a
x, Just b
z') -> forall a. a -> DNonEmpty a -> DNonEmpty a
cons a
x forall a b. (a -> b) -> a -> b
$ forall b a. (b -> (a, Maybe b)) -> b -> DNonEmpty a
unfoldr b -> (a, Maybe b)
f b
z'
{-# 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 forall a. a -> DList a -> DNonEmpty a
:| 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
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
instance Ord a => Ord (DNonEmpty a) where
compare :: DNonEmpty a -> DNonEmpty a -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
instance Read a => Read (DNonEmpty a) where
readPrec :: ReadPrec (DNonEmpty a)
readPrec = forall a. ReadPrec a -> ReadPrec a
Read.parens forall a b. (a -> b) -> a -> b
$
forall a. Int -> ReadPrec a -> ReadPrec a
Read.prec Int
10 forall a b. (a -> b) -> a -> b
$ do
Read.Ident [Char]
"fromNonEmpty" <- ReadPrec Lexeme
Read.lexP
NonEmpty a
dl <- forall a. Read a => ReadPrec a
Read.readPrec
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> DNonEmpty a
fromNonEmpty NonEmpty a
dl
readListPrec :: ReadPrec [DNonEmpty a]
readListPrec = 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 forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
[Char] -> ShowS
showString [Char]
"fromNonEmpty " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows (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 = forall a b. (a -> b) -> DNonEmpty a -> DNonEmpty b
map
instance Applicative.Applicative DNonEmpty where
{-# INLINE pure #-}
pure :: forall a. a -> DNonEmpty a
pure = forall a. a -> DNonEmpty a
singleton
{-# INLINE (<*>) #-}
<*> :: forall a 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 forall a. a -> DList a -> DNonEmpty a
:| forall a. DList a -> DList a -> DList a
DList.append DList b
ys (DList a
xs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. DNonEmpty a -> DList a
toDList 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 = 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 = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
Foldable.fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
{-# INLINE foldMap #-}
foldMap :: forall m a. Monoid m => (a -> m) -> DNonEmpty a -> m
foldMap a -> m
f = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap a -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr a -> b -> b
f b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl b -> a -> b
f b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
{-# INLINE foldr1 #-}
foldr1 :: forall a. (a -> a -> a) -> DNonEmpty a -> a
foldr1 a -> a -> a
f = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldr1 a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
{-# INLINE foldl1 #-}
foldl1 :: forall a. (a -> a -> a) -> DNonEmpty a -> a
foldl1 a -> a -> a
f = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldl1 a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' b -> a -> b
f b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' a -> b -> b
f b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
{-# INLINE toList #-}
toList :: forall a. DNonEmpty a -> [a]
toList = forall a. DNonEmpty a -> [a]
toList
instance NFData a => NFData (DNonEmpty a) where
{-# INLINE rnf #-}
rnf :: DNonEmpty a -> ()
rnf = forall a. NFData a => a -> ()
rnf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. DNonEmpty a -> NonEmpty a
toNonEmpty
instance a ~ Char => IsString (DNonEmpty a) where
{-# INLINE fromString #-}
fromString :: [Char] -> DNonEmpty a
fromString = 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 = forall a. [a] -> DNonEmpty a
fromList
{-# INLINE toList #-}
toList :: DNonEmpty a -> [Item (DNonEmpty a)]
toList = forall a. DNonEmpty a -> [a]
toList
instance Semigroup.Semigroup (DNonEmpty a) where
{-# INLINE (<>) #-}
<> :: DNonEmpty a -> DNonEmpty a -> DNonEmpty a
(<>) = forall a. DNonEmpty a -> DNonEmpty a -> DNonEmpty a
append