{-# 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 :: 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
{-# INLINE toNonEmpty #-}
toNonEmpty :: DNonEmpty a -> NonEmpty a
toNonEmpty :: 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
toDList :: DNonEmpty a -> DList a
toDList :: 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
{-# INLINE toList #-}
toList :: DNonEmpty a -> [a]
toList :: 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
fromList :: [a] -> DNonEmpty a
fromList :: [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"
{-# INLINE singleton #-}
singleton :: a -> DNonEmpty a
singleton :: 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
infixr 9 `cons`
{-# INLINE cons #-}
cons :: a -> DNonEmpty a -> DNonEmpty a
cons :: 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`
{-# INLINE snoc #-}
snoc :: DNonEmpty a -> a -> DNonEmpty a
snoc :: 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
{-# INLINE append #-}
append :: DNonEmpty a -> DNonEmpty a -> DNonEmpty a
append :: 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)
{-# INLINE head #-}
head :: DNonEmpty a -> a
head :: DNonEmpty a -> a
head ~(a
x :| DList a
_) = a
x
{-# INLINE tail #-}
tail :: DNonEmpty a -> DList a
tail :: DNonEmpty a -> DList a
tail ~(a
_ :| DList a
xs) = DList a
xs
unfoldr :: (b -> (a, Maybe b)) -> b -> DNonEmpty a
unfoldr :: (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'
{-# INLINE map #-}
map :: (a -> b) -> DNonEmpty a -> DNonEmpty b
map :: (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 (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 :: (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 :: a -> DNonEmpty a
pure = a -> DNonEmpty a
forall a. a -> DNonEmpty a
singleton
{-# INLINE (<*>) #-}
<*> :: 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) >>= :: 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 (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 :: a -> DNonEmpty a
return = a -> DNonEmpty a
forall (f :: * -> *) a. Applicative f => a -> f a
Applicative.pure
instance Foldable.Foldable DNonEmpty where
{-# INLINE fold #-}
fold :: DNonEmpty m -> m
fold = 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 :: (a -> m) -> DNonEmpty a -> m
foldMap a -> m
f = (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 :: (a -> b -> b) -> b -> DNonEmpty a -> b
foldr a -> b -> b
f b
x = (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 :: (b -> a -> b) -> b -> DNonEmpty a -> b
foldl b -> a -> b
f b
x = (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 :: (a -> a -> a) -> DNonEmpty a -> a
foldr1 a -> a -> a
f = (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 :: (a -> a -> a) -> DNonEmpty a -> a
foldl1 a -> a -> a
f = (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' :: (b -> a -> b) -> b -> DNonEmpty a -> b
foldl' b -> a -> b
f b
x = (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' :: (a -> b -> b) -> b -> DNonEmpty a -> b
foldr' a -> b -> b
f b
x = (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 :: 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
instance a ~ Char => IsString (DNonEmpty a) where
{-# INLINE fromString #-}
fromString :: [Char] -> DNonEmpty a
fromString = [Char] -> DNonEmpty a
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 = [Item (DNonEmpty a)] -> DNonEmpty a
forall a. [a] -> DNonEmpty a
fromList
{-# INLINE toList #-}
toList :: DNonEmpty a -> [Item (DNonEmpty a)]
toList = 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