module Mit.Seq1
  ( Seq1,
    fromSeq,
    unsafeFromSeq,
    toSeq,
    toList,
    Mit.Seq1.length,
    dropEnd,
  )
where

import Data.Coerce
import Data.Foldable qualified
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq)
import Data.Sequence qualified as Seq
import GHC.Stack (HasCallStack)
import Prelude

newtype Seq1 a = Seq1 (Seq a)
  deriving newtype (forall a. Eq a => a -> Seq1 a -> Bool
forall a. Num a => Seq1 a -> a
forall a. Ord a => Seq1 a -> a
forall m. Monoid m => Seq1 m -> m
forall a. Seq1 a -> Bool
forall a. Seq1 a -> Int
forall a. Seq1 a -> [a]
forall a. (a -> a -> a) -> Seq1 a -> a
forall m a. Monoid m => (a -> m) -> Seq1 a -> m
forall b a. (b -> a -> b) -> b -> Seq1 a -> b
forall a b. (a -> b -> b) -> b -> Seq1 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
product :: forall a. Num a => Seq1 a -> a
$cproduct :: forall a. Num a => Seq1 a -> a
sum :: forall a. Num a => Seq1 a -> a
$csum :: forall a. Num a => Seq1 a -> a
minimum :: forall a. Ord a => Seq1 a -> a
$cminimum :: forall a. Ord a => Seq1 a -> a
maximum :: forall a. Ord a => Seq1 a -> a
$cmaximum :: forall a. Ord a => Seq1 a -> a
elem :: forall a. Eq a => a -> Seq1 a -> Bool
$celem :: forall a. Eq a => a -> Seq1 a -> Bool
length :: forall a. Seq1 a -> Int
$clength :: forall a. Seq1 a -> Int
null :: forall a. Seq1 a -> Bool
$cnull :: forall a. Seq1 a -> Bool
toList :: forall a. Seq1 a -> [a]
$ctoList :: forall a. Seq1 a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Seq1 a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Seq1 a -> a
foldr1 :: forall a. (a -> a -> a) -> Seq1 a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Seq1 a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Seq1 a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Seq1 a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Seq1 a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Seq1 a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Seq1 a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Seq1 a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Seq1 a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Seq1 a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Seq1 a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Seq1 a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Seq1 a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Seq1 a -> m
fold :: forall m. Monoid m => Seq1 m -> m
$cfold :: forall m. Monoid m => Seq1 m -> m
Foldable)

fromSeq :: Seq a -> Maybe (Seq1 a)
fromSeq :: forall a. Seq a -> Maybe (Seq1 a)
fromSeq = \case
  Seq a
Seq.Empty -> forall a. Maybe a
Nothing
  Seq a
xs -> forall a. a -> Maybe a
Just (forall a. Seq a -> Seq1 a
Seq1 Seq a
xs)

unsafeFromSeq :: HasCallStack => Seq a -> Seq1 a
unsafeFromSeq :: forall a. HasCallStack => Seq a -> Seq1 a
unsafeFromSeq =
  forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"unsafeFromSeq: empty sequence") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Seq a -> Maybe (Seq1 a)
fromSeq

toSeq :: Seq1 a -> Seq a
toSeq :: forall a. Seq1 a -> Seq a
toSeq =
  coerce :: forall a b. Coercible a b => a -> b
coerce

toList :: forall a. Seq1 a -> [a]
toList :: forall a. Seq1 a -> [a]
toList =
  coerce :: forall a b. Coercible a b => a -> b
coerce (forall (t :: * -> *) a. Foldable t => t a -> [a]
Data.Foldable.toList @Seq @a)

length :: forall a. Seq1 a -> Int
length :: forall a. Seq1 a -> Int
length =
  coerce :: forall a b. Coercible a b => a -> b
coerce (forall a. Seq a -> Int
Seq.length @a)

dropEnd :: Int -> Seq1 a -> Seq a
dropEnd :: forall a. Int -> Seq1 a -> Seq a
dropEnd =
  let loop :: Int -> Seq a -> Seq a
      loop :: forall a. Int -> Seq a -> Seq a
loop Int
n =
        case Int
n of
          Int
0 -> forall a. a -> a
id
          Int
_ -> \case
            Seq a
Seq.Empty -> forall a. Seq a
Seq.Empty
            Seq a
ys Seq.:|> a
_ -> forall a. Int -> Seq a -> Seq a
loop (Int
n forall a. Num a => a -> a -> a
- Int
1) Seq a
ys
   in \Int
n (Seq1 Seq a
xs) -> forall a. Int -> Seq a -> Seq a
loop Int
n Seq a
xs