{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE TypeOperators, NoImplicitPrelude #-}
{-# LANGUAGE CPP, DataKinds, GADTs, KindSignatures, MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms, PolyKinds, RankNTypes, TypeInType #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoStarIsType #-}
module Data.Sized.Peano
(
Sized(), SomeSized, pattern SomeSized, Ordinal,
DomC(),
length, sLength, null,
(!!), (%!!), index, sIndex, head, last,
uncons, uncons', Uncons, pattern Uncons,
unsnoc, unsnoc', Unsnoc, pattern Unsnoc,
tail, init, take, takeAtMost, drop, splitAt, splitAtMost,
empty, singleton, toSomeSized, replicate, replicate', generate, generate',
cons, (<|), snoc, (|>), append, (++), concat,
zip, zipSame, zipWith, zipWithSame, unzip, unzipWith,
map, reverse, intersperse, nub, sort, sortBy, insert, insertBy,
toList, fromList, fromList', unsafeFromList, unsafeFromList',
fromListWithDefault, fromListWithDefault',
unsized,
toSized, toSized', unsafeToSized, unsafeToSized',
toSizedWithDefault, toSizedWithDefault',
Partitioned(), pattern Partitioned,
takeWhile, dropWhile, span, break, partition,
elem, notElem, find, findIndex, sFindIndex,
findIndices, sFindIndices,
elemIndex, sElemIndex, sUnsafeElemIndex, elemIndices, sElemIndices,
viewCons, ConsView,
pattern (:-), pattern NilCV,
viewSnoc, SnocView,
pattern (:-::), pattern NilSV,
pattern Nil, pattern (:<), pattern NilL , pattern (:>), pattern NilR,
) where
import qualified Data.Sized as S
import Data.Sized (DomC)
import Control.Subcategory
import Data.Kind (Type)
import Data.Singletons.Prelude (SingI)
import Data.Singletons.Prelude.Enum (PEnum (..))
import qualified Data.Type.Ordinal as O
import Prelude (Maybe, Ordering, Ord, Eq, Monoid, Bool(..), Int)
import Data.Type.Natural (Two, Nat(..), SNat)
import Data.Singletons.Prelude (POrd((<=)))
import Data.Type.Natural.Class (type (-.), type (<))
import Data.Type.Natural (Min, type (-), type (+), type (*))
import Data.Type.Natural (One)
type Ordinal = (O.Ordinal :: Nat -> Type)
type Sized = (S.Sized :: (Type -> Type) -> Nat -> Type -> Type)
type SomeSized f a = S.SomeSized' f Nat a
pattern SomeSized
:: forall (f :: Type -> Type) a. ()
=> forall (n :: Nat). SNat n
-> Sized f n a -> SomeSized f a
{-# COMPLETE SomeSized #-}
pattern SomeSized n s = S.SomeSized' n s
{-# INLINE length #-}
length :: (Dom f a, SingI n) => Sized f n a -> Int
length = S.length @Nat
{-# INLINE sLength #-}
sLength :: (Dom f a, SingI n) => Sized f n a -> SNat n
sLength = S.sLength @Nat
{-# INLINE null #-}
null :: (Dom f a, CFoldable f) => Sized f n a -> Bool
null = S.null @Nat
{-# INLINE (!!) #-}
(!!) :: (Dom f a, CFoldable f, (One <= m) ~ 'True) => Sized f m a -> Int -> a
(!!) = (S.!!) @Nat
{-# INLINE (%!!) #-}
(%!!) :: (Dom f c, CFoldable f) => Sized f n c -> Ordinal n -> c
(%!!) = (S.%!!) @Nat
{-# INLINE index #-}
index
:: (Dom f a, CFoldable f, (One <= m) ~ 'True)
=> Int -> Sized f m a -> a
index = S.index @Nat
{-# INLINE sIndex #-}
sIndex :: (Dom f c, CFoldable f) => Ordinal n -> Sized f n c -> c
sIndex = S.sIndex @Nat
{-# INLINE head #-}
head :: (Dom f a, CFoldable f, ('Z < n) ~ 'True) => Sized f n a -> a
head = S.head @Nat
{-# INLINE last #-}
last :: (Dom f a, CFoldable f, ('Z < n) ~ 'True) => Sized f n a -> a
last = S.last @Nat
{-# INLINE uncons #-}
uncons
:: (Dom f a, SingI n, CFreeMonoid f, ('Z < n) ~ 'True)
=> Sized f n a -> Uncons f n a
uncons = S.uncons @Nat
{-# INLINE uncons' #-}
uncons'
:: (Dom f a, SingI n, CFreeMonoid f, ('Z < n) ~ 'True)
=> Sized f n a
-> Uncons f n a
uncons' = S.uncons @Nat
{-# INLINE unsnoc #-}
unsnoc
:: (Dom f a, SingI n, CFreeMonoid f, ('Z < n) ~ 'True)
=> Sized f n a -> Unsnoc f n a
unsnoc = S.unsnoc @Nat
{-# INLINE unsnoc' #-}
unsnoc' :: (Dom f a, SingI n, CFreeMonoid f) => proxy n -> Sized f ('S n) a -> Unsnoc f ('S n) a
unsnoc' = S.unsnoc' @Nat
type Uncons f (n :: Nat) a = S.Uncons f n a
pattern Uncons
:: forall (f :: Type -> Type) (n :: Nat) a. ()
=> forall (n1 :: Nat). (n ~ Succ n1, SingI n1)
=> a -> Sized f n1 a -> Uncons f n a
pattern Uncons a as = S.Uncons a as
type Unsnoc f (n :: Nat) a = S.Unsnoc f n a
pattern Unsnoc
:: forall (f :: Type -> Type) (n :: Nat) a. ()
=> forall (n1 :: Nat). (n ~ Succ n1)
=> Sized f n1 a -> a -> Unsnoc f n a
pattern Unsnoc xs x = S.Unsnoc xs x
{-# INLINE tail #-}
tail :: (Dom f a, CFreeMonoid f) => Sized f (One + n) a -> Sized f n a
tail = S.tail @Nat
{-# INLINE init #-}
init :: (Dom f a, CFreeMonoid f) => Sized f (n + One) a -> Sized f n a
init = S.init @Nat
{-# INLINE take #-}
take
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> Sized f n a
take = S.take @Nat
{-# INLINE takeAtMost #-}
takeAtMost
:: (Dom f a, CFreeMonoid f)
=> SNat n -> Sized f m a -> Sized f (Min n m) a
takeAtMost = S.takeAtMost @Nat
{-# INLINE drop #-}
drop
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> Sized f (m - n) a
drop = S.drop @Nat
{-# INLINE splitAt #-}
splitAt
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> (Sized f n a, Sized f (m - n) a)
splitAt = S.splitAt @Nat
{-# INLINE splitAtMost #-}
splitAtMost
:: (Dom f a, CFreeMonoid f)
=> SNat n -> Sized f m a
-> (Sized f (Min n m) a, Sized f (m -. n) a)
splitAtMost = S.splitAtMost @Nat
{-# INLINE empty #-}
empty :: (Dom f a, Monoid (f a)) => Sized f 'Z a
empty = S.empty @Nat
{-# INLINE singleton #-}
singleton :: (Dom f a, CFreeMonoid f) => a -> Sized f One a
singleton = S.singleton @Nat
{-# INLINE toSomeSized #-}
toSomeSized :: (Dom f a, CFoldable f) => f a -> SomeSized f a
toSomeSized = S.toSomeSized @Nat
{-# INLINE replicate #-}
replicate :: (Dom f a, CFreeMonoid f) => SNat n -> a -> Sized f n a
replicate = S.replicate @Nat
{-# INLINE replicate' #-}
replicate' :: (Dom f a, CFreeMonoid f, SingI n) => a -> Sized f n a
replicate' = S.replicate' @Nat
{-# INLINE generate #-}
generate :: (Dom f a, CFreeMonoid f) => SNat n -> (Ordinal n -> a) -> Sized f n a
generate = S.generate @Nat
{-# INLINE generate' #-}
generate' :: forall f n a.
(SingI n, Dom f a, CFreeMonoid f)
=> (Ordinal n -> a) -> Sized f n a
generate' = S.generate' @Nat
{-# INLINE cons #-}
cons :: (Dom f a, CFreeMonoid f) => a -> Sized f n a -> Sized f ('S n) a
cons = S.cons @Nat
{-# INLINE snoc #-}
snoc :: (Dom f a, CFreeMonoid f) => Sized f n a -> a -> Sized f (n + One) a
snoc = S.snoc @Nat
{-# INLINE (<|) #-}
(<|) :: (Dom f a, CFreeMonoid f) => a -> Sized f n a -> Sized f ('S n) a
(<|) = (S.<|) @Nat
{-# INLINE (|>) #-}
(|>) :: (Dom f a, CFreeMonoid f) => Sized f n a -> a -> Sized f (n + One) a
(|>) = (S.|>) @Nat
{-# INLINE (++) #-}
(++) :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f m a -> Sized f (n + m) a
(++) = (S.++) @Nat
{-# INLINE append #-}
append :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f m a -> Sized f (n + m) a
append = S.append @Nat
{-# INLINE concat #-}
concat
:: (Dom f a, Dom f' (f a), Dom f' (Sized f n a),
CFreeMonoid f, CFunctor f', CFoldable f'
) => Sized f' m (Sized f n a) -> Sized f (m * n) a
concat = S.concat @Nat
{-# INLINE zip #-}
zip :: (Dom f a, Dom f b, Dom f (a, b), CZip f)
=> Sized f n a -> Sized f m b -> Sized f (Min n m) (a, b)
zip = S.zip @Nat
{-# INLINE zipSame #-}
zipSame :: (Dom f a, Dom f b, Dom f (a, b), CZip f)
=> Sized f n a -> Sized f n b -> Sized f n (a, b)
zipSame = S.zipSame @Nat
{-# INLINE zipWith #-}
zipWith :: (Dom f a, Dom f b, Dom f c, CZip f, CFreeMonoid f)
=> (a -> b -> c) -> Sized f n a -> Sized f m b -> Sized f (Min n m) c
zipWith = S.zipWith @Nat
{-# INLINE zipWithSame #-}
zipWithSame
:: (Dom f a, Dom f b, Dom f c, CZip f, CFreeMonoid f)
=> (a -> b -> c) -> Sized f n a -> Sized f n b -> Sized f n c
zipWithSame = S.zipWithSame @Nat
{-# INLINE unzip #-}
unzip
:: (Dom f a, Dom f b, Dom f (a, b), CUnzip f)
=> Sized f n (a, b) -> (Sized f n a, Sized f n b)
unzip = S.unzip @Nat
{-# INLINE unzipWith #-}
unzipWith
:: (Dom f a, Dom f b, Dom f c, CUnzip f)
=> (a -> (b, c)) -> Sized f n a -> (Sized f n b, Sized f n c)
unzipWith = S.unzipWith @Nat
{-# INLINE map #-}
map
:: (Dom f a, Dom f b, CFreeMonoid f)
=> (a -> b) -> Sized f n a -> Sized f n b
map = S.map @Nat
{-# INLINE reverse #-}
reverse :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f n a
reverse = S.reverse @Nat
{-# INLINE intersperse #-}
intersperse
:: (Dom f a, CFreeMonoid f)
=> a -> Sized f n a -> Sized f ((Two * n) -. One) a
intersperse = S.intersperse @Nat
{-# INLINE nub #-}
nub :: (Dom f a, Eq a, CFreeMonoid f) => Sized f n a -> SomeSized f a
nub = S.nub @Nat
{-# INLINE sort #-}
sort :: (Dom f a, CFreeMonoid f, Ord a) => Sized f n a -> Sized f n a
sort = S.sort @Nat
{-# INLINE sortBy #-}
sortBy
:: (Dom f a, CFreeMonoid f)
=> (a -> a -> Ordering)
-> Sized f n a -> Sized f n a
sortBy = S.sortBy @Nat
{-# INLINE insert #-}
insert
:: (Dom f a, CFreeMonoid f, Ord a)
=> a -> Sized f n a -> Sized f ('S n) a
insert = S.insert @Nat
{-# INLINE insertBy #-}
insertBy
:: (Dom f a, CFreeMonoid f)
=> (a -> a -> Ordering) -> a -> Sized f n a -> Sized f ('S n) a
insertBy = S.insertBy @Nat
{-# INLINE toList #-}
toList :: (Dom f a, CFoldable f) => Sized f n a -> [a]
toList = S.toList @Nat
{-# INLINE fromList #-}
fromList :: (Dom f a, CFreeMonoid f) => SNat n -> [a] -> Maybe (Sized f n a)
fromList = S.fromList @Nat
{-# INLINE fromList' #-}
fromList' :: (Dom f a, CFreeMonoid f, SingI n) => [a] -> Maybe (Sized f n a)
fromList' = S.fromList' @Nat
{-# INLINE unsafeFromList #-}
unsafeFromList :: (Dom f a, CFreeMonoid f) => SNat n -> [a] -> Sized f n a
unsafeFromList = S.unsafeFromList @Nat
{-# INLINE unsafeFromList' #-}
unsafeFromList' :: (Dom f a, SingI n, CFreeMonoid f) => [a] -> Sized f n a
unsafeFromList' = S.unsafeFromList' @Nat
{-# INLINE fromListWithDefault #-}
fromListWithDefault :: (Dom f a, CFreeMonoid f) => SNat n -> a -> [a] -> Sized f n a
fromListWithDefault = S.fromListWithDefault @Nat
{-# INLINE fromListWithDefault' #-}
fromListWithDefault' :: (Dom f a, SingI n, CFreeMonoid f)
=> a -> [a] -> Sized f n a
fromListWithDefault' = S.fromListWithDefault' @Nat
{-# INLINE unsized #-}
unsized :: Sized f n a -> f a
unsized = S.unsized @Nat
{-# INLINE toSized #-}
toSized :: (Dom f a, CFreeMonoid f) => SNat n -> f a -> Maybe (Sized f n a)
toSized = S.toSized @Nat
{-# INLINE toSized' #-}
toSized' :: (Dom f a, CFreeMonoid f, SingI n) => f a -> Maybe (Sized f n a)
toSized' = S.toSized' @Nat
{-# INLINE unsafeToSized #-}
unsafeToSized :: SNat n -> f a -> Sized f n a
unsafeToSized = S.unsafeToSized @Nat
{-# INLINE unsafeToSized' #-}
unsafeToSized' :: (Dom f a, SingI n) => f a -> Sized f n a
unsafeToSized' = S.unsafeToSized' @Nat
{-# INLINE toSizedWithDefault #-}
toSizedWithDefault :: (Dom f a, CFreeMonoid f) => SNat n -> a -> f a -> Sized f n a
toSizedWithDefault = S.toSizedWithDefault @Nat
{-# INLINE toSizedWithDefault' #-}
toSizedWithDefault' :: (Dom f a, SingI n, CFreeMonoid f)
=> a -> f a -> Sized f n a
toSizedWithDefault' = S.toSizedWithDefault' @Nat
type Partitioned f (n :: Nat) a = S.Partitioned f n a
pattern Partitioned
:: forall (f :: Type -> Type) (n :: Nat) a. ()
=> forall (n1 :: Nat) (m :: Nat). (n ~ (n1 + m), Dom f a)
=> SNat n1 -> Sized f n1 a -> SNat m
-> Sized f m a -> Partitioned f n a
{-# COMPLETE Partitioned #-}
pattern Partitioned ls l rs r = S.Partitioned ls l rs r
{-# INLINE takeWhile #-}
takeWhile :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> SomeSized f a
takeWhile = S.takeWhile @Nat
{-# INLINE dropWhile #-}
dropWhile :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> SomeSized f a
dropWhile = S.dropWhile @Nat
{-# INLINE span #-}
span :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> Partitioned f n a
span = S.span @Nat
{-# INLINE break #-}
break :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> Partitioned f n a
break = S.break @Nat
{-# INLINE partition #-}
partition :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> Partitioned f n a
partition = S.partition @Nat
{-# INLINE elem #-}
elem :: (Dom f a, CFoldable f, Eq a) => a -> Sized f n a -> Bool
elem = S.elem @Nat
{-# INLINE notElem #-}
notElem :: (Dom f a, CFoldable f, Eq a) => a -> Sized f n a -> Bool
notElem = S.notElem @Nat
{-# INLINE find #-}
find :: (Dom f a, CFoldable f) => (a -> Bool) -> Sized f n a -> Maybe a
find = S.find @Nat
{-# INLINE findIndex #-}
findIndex :: (Dom f a, CFoldable f) => (a -> Bool) -> Sized f n a -> Maybe Int
findIndex = S.findIndex @Nat
{-# INLINE sFindIndex #-}
sFindIndex :: (Dom f a, SingI n, CFoldable f) => (a -> Bool) -> Sized f n a -> Maybe (Ordinal n)
sFindIndex = S.sFindIndex @Nat
{-# INLINE findIndices #-}
findIndices :: (Dom f a, CFoldable f) => (a -> Bool) -> Sized f n a -> [Int]
findIndices = S.findIndices @Nat
{-# INLINE sFindIndices #-}
sFindIndices :: (Dom f a, CFoldable f, SingI n) => (a -> Bool) -> Sized f n a -> [Ordinal n]
sFindIndices = S.sFindIndices @Nat
{-# INLINE elemIndex #-}
elemIndex :: (Dom f a, CFoldable f, Eq a) => a -> Sized f n a -> Maybe Int
elemIndex = S.elemIndex @Nat
sElemIndex, sUnsafeElemIndex :: (Dom f a, SingI n, CFoldable f, Eq a) => a -> Sized f n a -> Maybe (Ordinal n)
{-# DEPRECATED sUnsafeElemIndex "Use sElemIndex instead" #-}
sUnsafeElemIndex = S.sElemIndex @Nat
sElemIndex = S.sElemIndex @Nat
{-# INLINE elemIndices #-}
elemIndices :: (Dom f a, CFoldable f, Eq a) => a -> Sized f n a -> [Int]
elemIndices = S.elemIndices @Nat
{-# INLINE sElemIndices #-}
sElemIndices
:: (Dom f a, CFoldable f, SingI n, Eq a)
=> a -> Sized f n a -> [Ordinal n]
sElemIndices = S.sElemIndices @Nat
type ConsView f (n :: Nat) a = S.ConsView f n a
pattern NilCV
:: forall (f :: Type -> Type) n a. ()
=> (n ~ 'Z)
=> ConsView f n a
pattern NilCV = S.NilCV
pattern (:-)
:: forall (f :: Type -> Type) n a. ()
=> forall n1. (n ~ (One + n1), SingI n1)
=> a -> Sized f n1 a -> ConsView f n a
pattern l :- ls = l S.:- ls
infixr 9 :-
{-# COMPLETE NilCV, (:-) #-}
viewCons :: (Dom f a, SingI n, CFreeMonoid f) => Sized f n a -> ConsView f n a
viewCons = S.viewCons @Nat
type SnocView f (n :: Nat) a = S.SnocView f n a
pattern NilSV
:: forall (f :: Type -> Type) n a. ()
=> (n ~ 'Z)
=> SnocView f n a
pattern NilSV = S.NilSV
infixl 9 :-::
pattern (:-::)
:: forall (f :: Type -> Type) n a. ()
=> forall n1. (n ~ (n1 + One), SingI n1)
=> Sized f n1 a -> a -> SnocView f n a
pattern ls :-:: l = ls S.:-:: l
{-# COMPLETE NilSV, (:-::) #-}
viewSnoc :: (Dom f a, SingI n, CFreeMonoid f) => Sized f n a -> ConsView f n a
viewSnoc = S.viewCons @Nat
pattern (:<)
:: forall (f :: Type -> Type) a (n :: Nat).
(Dom f a, SingI n, CFreeMonoid f)
=> forall (n1 :: Nat). (n ~ Succ n1, SingI n1)
=> a -> Sized f n1 a -> Sized f n a
pattern a :< b = a S.:< b
infixr 5 :<
pattern Nil
:: forall (f :: Type -> Type) a n.
(Dom f a, SingI n, CFreeMonoid f)
=> (n ~ 'Z) => Sized f n a
pattern Nil = S.Nil
pattern NilL :: forall f (n :: Nat) a.
(SingI n, CFreeMonoid f, Dom f a)
=> n ~ 'Z => Sized f n a
pattern NilL = Nil
pattern (:>)
:: forall (f :: Type -> Type) a (n :: Nat).
(Dom f a, SingI n, CFreeMonoid f)
=> forall (n1 :: Nat). (n ~ (n1 + One), SingI n1)
=> Sized f n1 a -> a -> Sized f n a
pattern a :> b = a S.:> b
infixl 5 :>
pattern NilR :: forall f (n :: Nat) a.
(CFreeMonoid f, Dom f a, SingI n)
=> n ~ 'Z => Sized f n a
pattern NilR = Nil
{-# COMPLETE (:<), NilL #-}
{-# COMPLETE (:<), NilR #-}
{-# COMPLETE (:<), Nil #-}
{-# COMPLETE (:>), NilL #-}
{-# COMPLETE (:>), NilR #-}
{-# COMPLETE (:>), Nil #-}