{-# OPTIONS_GHC -Wunused-imports #-}

-- | Lists of length at least 2.
--
--   Import as:
--   @
--      import Agda.Utils.List2 (List2(List2))
--      import qualified Agda.Utils.List2 as List2
--   @

module Agda.Utils.List2
  ( module Agda.Utils.List2
  , module Reexport
  ) where

import Control.DeepSeq
import Control.Monad                   ( (<=<) )

import qualified Data.List as List

import GHC.Exts                        ( IsList(..) )
import qualified GHC.Exts  as Reexport ( toList )

import Agda.Utils.List1                ( List1, pattern (:|) )
import qualified Agda.Utils.List1 as List1

import Agda.Utils.Impossible

-- | Lists of length ≥2.
data List2 a = List2 a a [a]
  deriving (List2 a -> List2 a -> Bool
(List2 a -> List2 a -> Bool)
-> (List2 a -> List2 a -> Bool) -> Eq (List2 a)
forall a. Eq a => List2 a -> List2 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => List2 a -> List2 a -> Bool
== :: List2 a -> List2 a -> Bool
$c/= :: forall a. Eq a => List2 a -> List2 a -> Bool
/= :: List2 a -> List2 a -> Bool
Eq, Eq (List2 a)
Eq (List2 a) =>
(List2 a -> List2 a -> Ordering)
-> (List2 a -> List2 a -> Bool)
-> (List2 a -> List2 a -> Bool)
-> (List2 a -> List2 a -> Bool)
-> (List2 a -> List2 a -> Bool)
-> (List2 a -> List2 a -> List2 a)
-> (List2 a -> List2 a -> List2 a)
-> Ord (List2 a)
List2 a -> List2 a -> Bool
List2 a -> List2 a -> Ordering
List2 a -> List2 a -> List2 a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (List2 a)
forall a. Ord a => List2 a -> List2 a -> Bool
forall a. Ord a => List2 a -> List2 a -> Ordering
forall a. Ord a => List2 a -> List2 a -> List2 a
$ccompare :: forall a. Ord a => List2 a -> List2 a -> Ordering
compare :: List2 a -> List2 a -> Ordering
$c< :: forall a. Ord a => List2 a -> List2 a -> Bool
< :: List2 a -> List2 a -> Bool
$c<= :: forall a. Ord a => List2 a -> List2 a -> Bool
<= :: List2 a -> List2 a -> Bool
$c> :: forall a. Ord a => List2 a -> List2 a -> Bool
> :: List2 a -> List2 a -> Bool
$c>= :: forall a. Ord a => List2 a -> List2 a -> Bool
>= :: List2 a -> List2 a -> Bool
$cmax :: forall a. Ord a => List2 a -> List2 a -> List2 a
max :: List2 a -> List2 a -> List2 a
$cmin :: forall a. Ord a => List2 a -> List2 a -> List2 a
min :: List2 a -> List2 a -> List2 a
Ord, Int -> List2 a -> ShowS
[List2 a] -> ShowS
List2 a -> String
(Int -> List2 a -> ShowS)
-> (List2 a -> String) -> ([List2 a] -> ShowS) -> Show (List2 a)
forall a. Show a => Int -> List2 a -> ShowS
forall a. Show a => [List2 a] -> ShowS
forall a. Show a => List2 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> List2 a -> ShowS
showsPrec :: Int -> List2 a -> ShowS
$cshow :: forall a. Show a => List2 a -> String
show :: List2 a -> String
$cshowList :: forall a. Show a => [List2 a] -> ShowS
showList :: [List2 a] -> ShowS
Show, (forall a b. (a -> b) -> List2 a -> List2 b)
-> (forall a b. a -> List2 b -> List2 a) -> Functor List2
forall a b. a -> List2 b -> List2 a
forall a b. (a -> b) -> List2 a -> List2 b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> List2 a -> List2 b
fmap :: forall a b. (a -> b) -> List2 a -> List2 b
$c<$ :: forall a b. a -> List2 b -> List2 a
<$ :: forall a b. a -> List2 b -> List2 a
Functor, (forall m. Monoid m => List2 m -> m)
-> (forall m a. Monoid m => (a -> m) -> List2 a -> m)
-> (forall m a. Monoid m => (a -> m) -> List2 a -> m)
-> (forall a b. (a -> b -> b) -> b -> List2 a -> b)
-> (forall a b. (a -> b -> b) -> b -> List2 a -> b)
-> (forall b a. (b -> a -> b) -> b -> List2 a -> b)
-> (forall b a. (b -> a -> b) -> b -> List2 a -> b)
-> (forall a. (a -> a -> a) -> List2 a -> a)
-> (forall a. (a -> a -> a) -> List2 a -> a)
-> (forall a. List2 a -> [a])
-> (forall a. List2 a -> Bool)
-> (forall a. List2 a -> Int)
-> (forall a. Eq a => a -> List2 a -> Bool)
-> (forall a. Ord a => List2 a -> a)
-> (forall a. Ord a => List2 a -> a)
-> (forall a. Num a => List2 a -> a)
-> (forall a. Num a => List2 a -> a)
-> Foldable List2
forall a. Eq a => a -> List2 a -> Bool
forall a. Num a => List2 a -> a
forall a. Ord a => List2 a -> a
forall m. Monoid m => List2 m -> m
forall a. List2 a -> Bool
forall a. List2 a -> Int
forall a. List2 a -> [a]
forall a. (a -> a -> a) -> List2 a -> a
forall m a. Monoid m => (a -> m) -> List2 a -> m
forall b a. (b -> a -> b) -> b -> List2 a -> b
forall a b. (a -> b -> b) -> b -> List2 a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => List2 m -> m
fold :: forall m. Monoid m => List2 m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> List2 a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> List2 a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> List2 a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> List2 a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> List2 a -> b
foldr :: forall a b. (a -> b -> b) -> b -> List2 a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> List2 a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> List2 a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> List2 a -> b
foldl :: forall b a. (b -> a -> b) -> b -> List2 a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> List2 a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> List2 a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> List2 a -> a
foldr1 :: forall a. (a -> a -> a) -> List2 a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> List2 a -> a
foldl1 :: forall a. (a -> a -> a) -> List2 a -> a
$ctoList :: forall a. List2 a -> [a]
toList :: forall a. List2 a -> [a]
$cnull :: forall a. List2 a -> Bool
null :: forall a. List2 a -> Bool
$clength :: forall a. List2 a -> Int
length :: forall a. List2 a -> Int
$celem :: forall a. Eq a => a -> List2 a -> Bool
elem :: forall a. Eq a => a -> List2 a -> Bool
$cmaximum :: forall a. Ord a => List2 a -> a
maximum :: forall a. Ord a => List2 a -> a
$cminimum :: forall a. Ord a => List2 a -> a
minimum :: forall a. Ord a => List2 a -> a
$csum :: forall a. Num a => List2 a -> a
sum :: forall a. Num a => List2 a -> a
$cproduct :: forall a. Num a => List2 a -> a
product :: forall a. Num a => List2 a -> a
Foldable, Functor List2
Foldable List2
(Functor List2, Foldable List2) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> List2 a -> f (List2 b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    List2 (f a) -> f (List2 a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> List2 a -> m (List2 b))
-> (forall (m :: * -> *) a. Monad m => List2 (m a) -> m (List2 a))
-> Traversable List2
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => List2 (m a) -> m (List2 a)
forall (f :: * -> *) a. Applicative f => List2 (f a) -> f (List2 a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> List2 a -> m (List2 b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> List2 a -> f (List2 b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> List2 a -> f (List2 b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> List2 a -> f (List2 b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => List2 (f a) -> f (List2 a)
sequenceA :: forall (f :: * -> *) a. Applicative f => List2 (f a) -> f (List2 a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> List2 a -> m (List2 b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> List2 a -> m (List2 b)
$csequence :: forall (m :: * -> *) a. Monad m => List2 (m a) -> m (List2 a)
sequence :: forall (m :: * -> *) a. Monad m => List2 (m a) -> m (List2 a)
Traversable)

-- * Conversion from and to other list types.

-- | 'fromList' is unsafe.
instance IsList (List2 a) where
  type Item (List2 a) = a

  -- Unsafe! O(1).
  fromList :: [a] -> List2 a
  fromList :: [a] -> List2 a
fromList (a
a : a
b : [a]
cs) = a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
a a
b [a]
cs
  fromList [a]
_            = List2 a
forall a. HasCallStack => a
__IMPOSSIBLE__

  toList :: List2 a -> [a]
  toList :: List2 a -> [a]
toList (List2 a
a a
b [a]
cs) = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
cs

-- | Unsafe! O(1).
fromList1 :: List1 a -> List2 a
fromList1 :: forall a. List1 a -> List2 a
fromList1 (a
a :| a
b : [a]
cs) = a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
a a
b [a]
cs
fromList1 NonEmpty a
_             = List2 a
forall a. HasCallStack => a
__IMPOSSIBLE__

-- | Safe. O(1).
toList1 :: List2 a -> List1 a
toList1 :: forall a. List2 a -> List1 a
toList1 (List2 a
a a
b [a]
cs) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
cs

-- | Safe. O(1).
fromListMaybe :: [a] -> Maybe (List2 a)
fromListMaybe :: forall a. [a] -> Maybe (List2 a)
fromListMaybe = List1 a -> Maybe (List2 a)
forall a. List1 a -> Maybe (List2 a)
fromList1Maybe (List1 a -> Maybe (List2 a))
-> ([a] -> Maybe (List1 a)) -> [a] -> Maybe (List2 a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< [a] -> Maybe (List1 a)
forall a. [a] -> Maybe (NonEmpty a)
List1.nonEmpty

-- | Safe. O(1).
fromList1Maybe :: List1 a -> Maybe (List2 a)
fromList1Maybe :: forall a. List1 a -> Maybe (List2 a)
fromList1Maybe = \case
  (a
a :| a
b : [a]
cs) -> List2 a -> Maybe (List2 a)
forall a. a -> Maybe a
Just (a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
a a
b [a]
cs)
  List1 a
_ -> Maybe (List2 a)
forall a. Maybe a
Nothing

-- | Any 'List1' is either a singleton or a 'List2'. O(1).
fromList1Either :: List1 a -> Either a (List2 a)
fromList1Either :: forall a. List1 a -> Either a (List2 a)
fromList1Either (a
a :| [a]
as) = case [a]
as of
  []   -> a -> Either a (List2 a)
forall a b. a -> Either a b
Left a
a
  a
b:[a]
bs -> List2 a -> Either a (List2 a)
forall a b. b -> Either a b
Right (a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
a a
b [a]
bs)

-- | Inverse of 'fromList1Either'. O(1).
toList1Either :: Either a (List2 a) -> List1 a
toList1Either :: forall a. Either a (List2 a) -> List1 a
toList1Either = \case
  Left  a
a              -> a
a a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| []
  Right (List2 a
a a
b [a]
bs) -> a
a a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
bs

-- * Construction

-- | O(1).
cons :: a -> List1 a -> List2 a
cons :: forall a. a -> List1 a -> List2 a
cons a
x (a
y :| [a]
ys) = a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
x a
y [a]
ys

-- | O(length first list).
append :: List1 a -> List1 a -> List2 a
append :: forall a. List1 a -> List1 a -> List2 a
append (a
x :| [a]
xs) NonEmpty a
ys = a -> NonEmpty a -> List2 a
forall a. a -> List1 a -> List2 a
cons a
x (NonEmpty a -> List2 a) -> NonEmpty a -> List2 a
forall a b. (a -> b) -> a -> b
$ [a] -> NonEmpty a -> NonEmpty a
forall a. [a] -> NonEmpty a -> NonEmpty a
List1.prependList [a]
xs NonEmpty a
ys

-- | O(length first list).
appendList :: List2 a -> [a] -> List2 a
appendList :: forall a. List2 a -> [a] -> List2 a
appendList (List2 a
x a
y [a]
ys) [a]
zs = a -> a -> [a] -> List2 a
forall a. a -> a -> [a] -> List2 a
List2 a
x a
y ([a] -> List2 a) -> [a] -> List2 a
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> [a]
forall a. Monoid a => a -> a -> a
mappend [a]
ys [a]
zs

-- * Destruction

-- | Safe. O(1).
head :: List2 a -> a
head :: forall a. List2 a -> a
head (List2 a
a a
_ [a]
_) = a
a

-- | Safe. O(1).
tail :: List2 a -> List1 a
tail :: forall a. List2 a -> List1 a
tail (List2 a
a a
b [a]
cs) = a
b a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
cs

-- | Safe. O(n).
init :: List2 a -> List1 a
init :: forall a. List2 a -> List1 a
init (List2 a
a a
b [a]
cs) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
List1.init (a
b a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
cs)

-- * Partition

break :: (a -> Bool) -> List2 a -> ([a],[a])
break :: forall a. (a -> Bool) -> List2 a -> ([a], [a])
break a -> Bool
p = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.break a -> Bool
p ([a] -> ([a], [a])) -> (List2 a -> [a]) -> List2 a -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. List2 a -> [a]
List2 a -> [Item (List2 a)]
forall l. IsList l => l -> [Item l]
toList

instance NFData a => NFData (List2 a) where
  rnf :: List2 a -> ()
rnf (List2 a
a a
b [a]
cs) = a -> ()
forall a. NFData a => a -> ()
rnf a
a () -> () -> ()
forall a b. a -> b -> b
`seq` a -> ()
forall a. NFData a => a -> ()
rnf a
b () -> () -> ()
forall a b. a -> b -> b
`seq` [a] -> ()
forall a. NFData a => a -> ()
rnf [a]
cs