{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE TypeOperators, NoImplicitPrelude #-}
{-# LANGUAGE CPP, DataKinds, GADTs, KindSignatures, MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms, PolyKinds, RankNTypes, TypeInType #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoStarIsType #-}
module Data.Sized.Builtin
(
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 GHC.TypeNats (KnownNat, Nat)
import Prelude (Maybe, Ordering, Ord, Eq, Monoid, Bool(..), Int)
import Data.Singletons.TypeLits (SNat)
import Data.Singletons.Prelude (POrd((<=)))
import Data.Type.Natural.Class (type (-.), type (<))
import Data.Type.Natural (Min, type (-), type (+), type (*))
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, KnownNat n) => Sized f n a -> Int
length = S.length @Nat
sLength :: (Dom f a, KnownNat n) => Sized f n a -> SNat n
{-# INLINE sLength #-}
sLength = S.sLength @Nat
null :: (Dom f a, CFoldable f) => Sized f n a -> Bool
{-# INLINE null #-}
null = S.null @Nat
(!!) :: (Dom f a, CFoldable f, (1 <= m) ~ 'True) => Sized f m a -> Int -> a
{-# INLINE (!!) #-}
(!!) = (S.!!) @Nat
(%!!) :: (Dom f c, CFoldable f) => Sized f n c -> Ordinal n -> c
{-# INLINE (%!!) #-}
(%!!) = (S.%!!) @Nat
index
:: (Dom f a, CFoldable f, (1 <= m) ~ 'True)
=> Int -> Sized f m a -> a
{-# INLINE index #-}
index = S.index @Nat
sIndex :: (Dom f c, CFoldable f) => Ordinal n -> Sized f n c -> c
{-# INLINE sIndex #-}
sIndex = S.sIndex @Nat
head :: (Dom f a, CFoldable f, (0 < n) ~ 'True) => Sized f n a -> a
{-# INLINE head #-}
head = S.head @Nat
last :: (Dom f a, CFoldable f, (0 < n) ~ 'True) => Sized f n a -> a
{-# INLINE last #-}
last = S.last @Nat
uncons
:: (Dom f a, KnownNat n, CFreeMonoid f, (0 < n) ~ 'True)
=> Sized f n a -> Uncons f n a
{-# INLINE uncons #-}
uncons = S.uncons @Nat
uncons'
:: (Dom f a, KnownNat n, CFreeMonoid f, (0 < n) ~ 'True)
=> Sized f n a
-> Uncons f n a
{-# INLINE uncons' #-}
uncons' = S.uncons @Nat
unsnoc
:: (Dom f a, KnownNat n, CFreeMonoid f, (0 < n) ~ 'True)
=> Sized f n a -> Unsnoc f n a
{-# INLINE unsnoc #-}
unsnoc = S.unsnoc @Nat
unsnoc' :: (Dom f a, KnownNat n, CFreeMonoid f) => proxy n -> Sized f (n + 1) a -> Unsnoc f (n + 1) a
{-# INLINE unsnoc' #-}
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 ~ (1 + 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
tail :: (Dom f a, CFreeMonoid f) => Sized f (1 + n) a -> Sized f n a
{-# INLINE tail #-}
tail = S.tail @Nat
init :: (Dom f a, CFreeMonoid f) => Sized f (n + 1) a -> Sized f n a
{-# INLINE init #-}
init = S.init @Nat
take
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> Sized f n a
{-# INLINE take #-}
take = S.take @Nat
takeAtMost
:: (Dom f a, CFreeMonoid f)
=> SNat n -> Sized f m a -> Sized f (Min n m) a
{-# INLINE takeAtMost #-}
takeAtMost = S.takeAtMost @Nat
drop
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> Sized f (m - n) a
{-# INLINE drop #-}
drop = S.drop @Nat
splitAt
:: (Dom f a, CFreeMonoid f, (n <= m) ~ 'True)
=> SNat n -> Sized f m a -> (Sized f n a, Sized f (m - n) a)
{-# INLINE splitAt #-}
splitAt = S.splitAt @Nat
splitAtMost
:: (Dom f a, CFreeMonoid f)
=> SNat n -> Sized f m a
-> (Sized f (Min n m) a, Sized f (m -. n) a)
{-# INLINE splitAtMost #-}
splitAtMost = S.splitAtMost @Nat
empty :: (Dom f a, Monoid (f a)) => Sized f 0 a
{-# INLINE empty #-}
empty = S.empty @Nat
singleton :: (Dom f a, CFreeMonoid f) => a -> Sized f 1 a
{-# INLINE singleton #-}
singleton = S.singleton @Nat
toSomeSized :: (Dom f a, CFoldable f) => f a -> SomeSized f a
{-# INLINE toSomeSized #-}
toSomeSized = S.toSomeSized @Nat
replicate :: (Dom f a, CFreeMonoid f) => SNat n -> a -> Sized f n a
{-# INLINE replicate #-}
replicate = S.replicate @Nat
replicate' :: (Dom f a, KnownNat n, CFreeMonoid f) => a -> Sized f n a
{-# INLINE replicate' #-}
replicate' = S.replicate' @Nat
generate :: (Dom f a, CFreeMonoid f) => SNat n -> (Ordinal n -> a) -> Sized f n a
{-# INLINE generate #-}
generate = S.generate @Nat
generate'
:: forall f n a. (KnownNat n, Dom f a, CFreeMonoid f) => (Ordinal n -> a) -> Sized f n a
{-# INLINE generate' #-}
generate' = S.generate' @Nat
cons :: (Dom f a, CFreeMonoid f) => a -> Sized f n a -> Sized f (1 + n) a
{-# INLINE cons #-}
cons = S.cons @Nat
snoc :: (Dom f a, CFreeMonoid f) => Sized f n a -> a -> Sized f (n + 1) a
{-# INLINE snoc #-}
snoc = S.snoc @Nat
(<|) :: (Dom f a, CFreeMonoid f) => a -> Sized f n a -> Sized f (1 + n) a
{-# INLINE (<|) #-}
(<|) = (S.<|) @Nat
(|>) :: (Dom f a, CFreeMonoid f) => Sized f n a -> a -> Sized f (n + 1) a
{-# INLINE (|>) #-}
(|>) = (S.|>) @Nat
(++) :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f m a -> Sized f (n + m) a
{-# INLINE (++) #-}
(++) = (S.++) @Nat
append :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f m a -> Sized f (n + m) a
{-# INLINE append #-}
append = S.append @Nat
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
{-# INLINE concat #-}
concat = S.concat @Nat
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)
{-# INLINE zip #-}
zip = S.zip @Nat
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)
{-# INLINE zipSame #-}
zipSame = S.zipSame @Nat
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
{-# INLINE zipWith #-}
zipWith = S.zipWith @Nat
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
{-# INLINE zipWithSame #-}
zipWithSame = S.zipWithSame @Nat
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)
{-# INLINE unzip #-}
unzip = S.unzip @Nat
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)
{-# INLINE unzipWith #-}
unzipWith = S.unzipWith @Nat
map
:: (Dom f a, Dom f b, CFreeMonoid f)
=> (a -> b) -> Sized f n a -> Sized f n b
{-# INLINE map #-}
map = S.map @Nat
reverse :: (Dom f a, CFreeMonoid f) => Sized f n a -> Sized f n a
{-# INLINE reverse #-}
reverse = S.reverse @Nat
intersperse
:: (Dom f a, CFreeMonoid f)
=> a -> Sized f n a -> Sized f ((2 * n) -. 1) a
{-# INLINE intersperse #-}
intersperse = S.intersperse @Nat
nub :: (Dom f a, Eq a, CFreeMonoid f) => Sized f n a -> SomeSized f a
{-# INLINE nub #-}
nub = S.nub @Nat
sort :: (Dom f a, CFreeMonoid f, Ord a) => Sized f n a -> Sized f n a
{-# INLINE sort #-}
sort = S.sort @Nat
sortBy
:: (Dom f a, CFreeMonoid f)
=> (a -> a -> Ordering)
-> Sized f n a -> Sized f n a
{-# INLINE sortBy #-}
sortBy = S.sortBy @Nat
insert
:: (Dom f a, CFreeMonoid f, Ord a)
=> a -> Sized f n a -> Sized f (n + 1) a
{-# INLINE insert #-}
insert = S.insert @Nat
{-# INLINE insertBy #-}
insertBy
:: (Dom f a, CFreeMonoid f)
=> (a -> a -> Ordering) -> a -> Sized f n a -> Sized f (n + 1) 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, KnownNat 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, KnownNat 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, KnownNat 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, KnownNat 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, KnownNat 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, KnownNat 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
span :: (Dom f a, CFreeMonoid f) => (a -> Bool) -> Sized f n a -> Partitioned f n a
{-# INLINE span #-}
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, KnownNat 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, KnownNat 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, KnownNat 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, KnownNat n, Eq a)
=> a -> Sized f n a -> [Ordinal n]
sElemIndices = S.sElemIndices @Nat
type ConsView =
(S.ConsView :: (Type -> Type) -> Nat -> Type -> Type)
pattern NilCV
:: forall (f :: Type -> Type) n a. ()
=> (n ~ 0)
=> ConsView f n a
pattern NilCV = S.NilCV
pattern (:-)
:: forall (f :: Type -> Type) n a. ()
=> forall n1. (n ~ (1 + 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, KnownNat n, CFreeMonoid f) => Sized f n a -> ConsView f n a
viewCons = S.viewCons @Nat
type SnocView =
(S.SnocView :: (Type -> Type) -> Nat -> Type -> Type)
pattern NilSV
:: forall (f :: Type -> Type) n a. ()
=> (n ~ 0)
=> SnocView f n a
pattern NilSV = S.NilSV
infixl 9 :-::
pattern (:-::)
:: forall (f :: Type -> Type) n a. ()
=> forall n1. (n ~ (n1 + 1), SingI n1)
=> Sized f n1 a -> a -> SnocView f n a
pattern ls :-:: l = ls S.:-:: l
{-# COMPLETE NilSV, (:-::) #-}
viewSnoc :: (Dom f a, KnownNat n, CFreeMonoid f) => Sized f n a -> SnocView f n a
viewSnoc = S.viewSnoc @Nat
pattern (:<)
:: forall (f :: Type -> Type) a (n :: Nat).
(Dom f a, KnownNat n, CFreeMonoid f)
=> forall (n1 :: Nat). (n ~ (1 + 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, KnownNat n, CFreeMonoid f)
=> (n ~ 0) => Sized f n a
pattern Nil = S.Nil
{-# DEPRECATED NilL "Use Nil instead" #-}
pattern NilL :: forall f (n :: Nat) a.
(KnownNat n, CFreeMonoid f, Dom f a)
=> n ~ 0 => Sized f n a
pattern NilL = Nil
pattern (:>)
:: forall (f :: Type -> Type) a (n :: Nat).
(Dom f a, KnownNat n, CFreeMonoid f)
=> forall (n1 :: Nat). (n ~ (n1 + 1), SingI n1)
=> Sized f n1 a -> a -> Sized f n a
pattern a :> b = a S.:> b
infixl 5 :>
{-# DEPRECATED NilR "Use Nil instead" #-}
pattern NilR :: forall f (n :: Nat) a.
(CFreeMonoid f, Dom f a, KnownNat n)
=> n ~ 0 => Sized f n a
pattern NilR = Nil
{-# COMPLETE (:<), NilL #-}
{-# COMPLETE (:<), NilR #-}
{-# COMPLETE (:<), Nil #-}
{-# COMPLETE (:>), NilL #-}
{-# COMPLETE (:>), NilR #-}
{-# COMPLETE (:>), Nil #-}