sized-0.6.0.0: Sized sequence data-types

Safe HaskellNone
LanguageHaskell2010

Data.Sized

Contents

Description

This module provides the functionality to make length-parametrized types from existing ListLike and Functor sequential types.

Most of the complexity of operations for Sized f n a are the same as original operations for f. For example, !! is O(1) for Sized Vector n a but O(i) for Sized [] n a.

This module also provides powerful view types and pattern synonyms to inspect the sized sequence. See Views and Patterns for more detail.

Synopsis

Main Data-types

data Sized (f :: Type -> Type) (n :: nat) a Source #

Sized wraps a sequential type f and makes length-parametrized version.

Here, f must be the instance of Functor and ListLike (f a) a for all a. This constraint is expressed by ListLikeF. Folding and traversing function such as all and foldl' is available via Foldable or Traversable class, if f is the instance of them.

Since 0.2.0.0

Instances
(Integral i, FunctorWithIndex i f, HasOrdinal nat, SingI n) => FunctorWithIndex (Ordinal n) (Sized f n) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

Methods

imap :: (Ordinal n -> a -> b) -> Sized f n a -> Sized f n b #

imapped :: IndexedSetter (Ordinal n) (Sized f n a) (Sized f n b) a b #

(Integral i, FoldableWithIndex i f, HasOrdinal nat, SingI n) => FoldableWithIndex (Ordinal n) (Sized f n) Source #

Since 0.4.0.0

Instance details

Defined in Data.Sized.Internal

Methods

ifoldMap :: Monoid m => (Ordinal n -> a -> m) -> Sized f n a -> m #

ifolded :: IndexedFold (Ordinal n) (Sized f n a) a #

ifoldr :: (Ordinal n -> a -> b -> b) -> b -> Sized f n a -> b #

ifoldl :: (Ordinal n -> b -> a -> b) -> b -> Sized f n a -> b #

ifoldr' :: (Ordinal n -> a -> b -> b) -> b -> Sized f n a -> b #

ifoldl' :: (Ordinal n -> b -> a -> b) -> b -> Sized f n a -> b #

(Integral i, TraversableWithIndex i f, HasOrdinal nat, SingI n) => TraversableWithIndex (Ordinal n) (Sized f n) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

Methods

itraverse :: Applicative f0 => (Ordinal n -> a -> f0 b) -> Sized f n a -> f0 (Sized f n b) #

itraversed :: IndexedTraversal (Ordinal n) (Sized f n a) (Sized f n b) a b #

Functor f => Functor (Sized f n) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

fmap :: (a -> b) -> Sized f n a -> Sized f n b #

(<$) :: a -> Sized f n b -> Sized f n a #

(Functor f, HasOrdinal nat, SingI n, ListLikeF f) => Applicative (Sized f n) Source #

Applicative instance, generalizing ZipList.

Instance details

Defined in Data.Sized

Methods

pure :: a -> Sized f n a #

(<*>) :: Sized f n (a -> b) -> Sized f n a -> Sized f n b #

liftA2 :: (a -> b -> c) -> Sized f n a -> Sized f n b -> Sized f n c #

(*>) :: Sized f n a -> Sized f n b -> Sized f n b #

(<*) :: Sized f n a -> Sized f n b -> Sized f n a #

Foldable f => Foldable (Sized f n) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

fold :: Monoid m => Sized f n m -> m #

foldMap :: Monoid m => (a -> m) -> Sized f n a -> m #

foldr :: (a -> b -> b) -> b -> Sized f n a -> b #

foldr' :: (a -> b -> b) -> b -> Sized f n a -> b #

foldl :: (b -> a -> b) -> b -> Sized f n a -> b #

foldl' :: (b -> a -> b) -> b -> Sized f n a -> b #

foldr1 :: (a -> a -> a) -> Sized f n a -> a #

foldl1 :: (a -> a -> a) -> Sized f n a -> a #

toList :: Sized f n a -> [a] #

null :: Sized f n a -> Bool #

length :: Sized f n a -> Int #

elem :: Eq a => a -> Sized f n a -> Bool #

maximum :: Ord a => Sized f n a -> a #

minimum :: Ord a => Sized f n a -> a #

sum :: Num a => Sized f n a -> a #

product :: Num a => Sized f n a -> a #

Traversable f => Traversable (Sized f n) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

traverse :: Applicative f0 => (a -> f0 b) -> Sized f n a -> f0 (Sized f n b) #

sequenceA :: Applicative f0 => Sized f n (f0 a) -> f0 (Sized f n a) #

mapM :: Monad m => (a -> m b) -> Sized f n a -> m (Sized f n b) #

sequence :: Monad m => Sized f n (m a) -> m (Sized f n a) #

Eq (f a) => Eq (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

(==) :: Sized f n a -> Sized f n a -> Bool #

(/=) :: Sized f n a -> Sized f n a -> Bool #

Ord (f a) => Ord (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

compare :: Sized f n a -> Sized f n a -> Ordering #

(<) :: Sized f n a -> Sized f n a -> Bool #

(<=) :: Sized f n a -> Sized f n a -> Bool #

(>) :: Sized f n a -> Sized f n a -> Bool #

(>=) :: Sized f n a -> Sized f n a -> Bool #

max :: Sized f n a -> Sized f n a -> Sized f n a #

min :: Sized f n a -> Sized f n a -> Sized f n a #

Show (f a) => Show (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

showsPrec :: Int -> Sized f n a -> ShowS #

show :: Sized f n a -> String #

showList :: [Sized f n a] -> ShowS #

NFData (f a) => NFData (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

rnf :: Sized f n a -> () #

Hashable (f a) => Hashable (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

hashWithSalt :: Int -> Sized f n a -> Int #

hash :: Sized f n a -> Int #

(Integral (Index (f a)), Ixed (f a), HasOrdinal nat) => Ixed (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

Methods

ix :: Index (Sized f n a) -> Traversal' (Sized f n a) (IxValue (Sized f n a)) #

MonoFunctor (f a) => MonoFunctor (Sized f n a) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

Methods

omap :: (Element (Sized f n a) -> Element (Sized f n a)) -> Sized f n a -> Sized f n a #

MonoFoldable (f a) => MonoFoldable (Sized f n a) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

Methods

ofoldMap :: Monoid m => (Element (Sized f n a) -> m) -> Sized f n a -> m #

ofoldr :: (Element (Sized f n a) -> b -> b) -> b -> Sized f n a -> b #

ofoldl' :: (a0 -> Element (Sized f n a) -> a0) -> a0 -> Sized f n a -> a0 #

otoList :: Sized f n a -> [Element (Sized f n a)] #

oall :: (Element (Sized f n a) -> Bool) -> Sized f n a -> Bool #

oany :: (Element (Sized f n a) -> Bool) -> Sized f n a -> Bool #

onull :: Sized f n a -> Bool #

olength :: Sized f n a -> Int #

olength64 :: Sized f n a -> Int64 #

ocompareLength :: Integral i => Sized f n a -> i -> Ordering #

otraverse_ :: Applicative f0 => (Element (Sized f n a) -> f0 b) -> Sized f n a -> f0 () #

ofor_ :: Applicative f0 => Sized f n a -> (Element (Sized f n a) -> f0 b) -> f0 () #

omapM_ :: Applicative m => (Element (Sized f n a) -> m ()) -> Sized f n a -> m () #

oforM_ :: Applicative m => Sized f n a -> (Element (Sized f n a) -> m ()) -> m () #

ofoldlM :: Monad m => (a0 -> Element (Sized f n a) -> m a0) -> a0 -> Sized f n a -> m a0 #

ofoldMap1Ex :: Semigroup m => (Element (Sized f n a) -> m) -> Sized f n a -> m #

ofoldr1Ex :: (Element (Sized f n a) -> Element (Sized f n a) -> Element (Sized f n a)) -> Sized f n a -> Element (Sized f n a) #

ofoldl1Ex' :: (Element (Sized f n a) -> Element (Sized f n a) -> Element (Sized f n a)) -> Sized f n a -> Element (Sized f n a) #

headEx :: Sized f n a -> Element (Sized f n a) #

lastEx :: Sized f n a -> Element (Sized f n a) #

unsafeHead :: Sized f n a -> Element (Sized f n a) #

unsafeLast :: Sized f n a -> Element (Sized f n a) #

maximumByEx :: (Element (Sized f n a) -> Element (Sized f n a) -> Ordering) -> Sized f n a -> Element (Sized f n a) #

minimumByEx :: (Element (Sized f n a) -> Element (Sized f n a) -> Ordering) -> Sized f n a -> Element (Sized f n a) #

oelem :: Element (Sized f n a) -> Sized f n a -> Bool #

onotElem :: Element (Sized f n a) -> Sized f n a -> Bool #

Unbox a => MonoTraversable (Sized Vector n a) Source #

Since 0.6.0.0

Instance details

Defined in Data.Sized.Internal

Methods

otraverse :: Applicative f => (Element (Sized Vector n a) -> f (Element (Sized Vector n a))) -> Sized Vector n a -> f (Sized Vector n a) #

omapM :: Applicative m => (Element (Sized Vector n a) -> m (Element (Sized Vector n a))) -> Sized Vector n a -> m (Sized Vector n a) #

Storable a => MonoTraversable (Sized Vector n a) Source #

Since 0.6.0.0

Instance details

Defined in Data.Sized.Internal

Methods

otraverse :: Applicative f => (Element (Sized Vector n a) -> f (Element (Sized Vector n a))) -> Sized Vector n a -> f (Sized Vector n a) #

omapM :: Applicative m => (Element (Sized Vector n a) -> m (Element (Sized Vector n a))) -> Sized Vector n a -> m (Sized Vector n a) #

MonoTraversable (f a) => MonoTraversable (Sized f n a) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

Methods

otraverse :: Applicative f0 => (Element (Sized f n a) -> f0 (Element (Sized f n a))) -> Sized f n a -> f0 (Sized f n a) #

omapM :: Applicative m => (Element (Sized f n a) -> m (Element (Sized f n a))) -> Sized f n a -> m (Sized f n a) #

type Index (Sized f n a) Source #

Since 0.2.0.0

Instance details

Defined in Data.Sized.Internal

type Index (Sized f n a) = Ordinal n
type IxValue (Sized f n a) Source #

Since 0.3.0.0

Instance details

Defined in Data.Sized.Internal

type IxValue (Sized f n a) = IxValue (f a)
type Element (Sized f n a) Source # 
Instance details

Defined in Data.Sized.Internal

type Element (Sized f n a) = Element (f a)

data SomeSized f nat a where Source #

Sized vector with the length is existentially quantified. This type is used mostly when the return type's length cannot be statically determined beforehand.

SomeSized sn xs :: SomeSized f a stands for the Sized sequence xs of element type a and length sn.

Since 0.1.0.0

Constructors

SomeSized :: ListLike (f a) a => Sing n -> Sized f (n :: nat) a -> SomeSized f nat a 
Instances
Eq (f a) => Eq (SomeSized f nat a) Source # 
Instance details

Defined in Data.Sized

Methods

(==) :: SomeSized f nat a -> SomeSized f nat a -> Bool #

(/=) :: SomeSized f nat a -> SomeSized f nat a -> Bool #

Show (f a) => Show (SomeSized f nat a) Source # 
Instance details

Defined in Data.Sized

Methods

showsPrec :: Int -> SomeSized f nat a -> ShowS #

show :: SomeSized f nat a -> String #

showList :: [SomeSized f nat a] -> ShowS #

instLL :: forall f a. ListLikeF f :- ListLike (f a) a Source #

type ListLikeF f = (Functor f, Forall (LLF f)) Source #

Functor f such that there is instance ListLike (f a) a for any a.

Since 0.1.0.0

withListLikeF :: forall pxy f a b. ListLikeF f => pxy (f a) -> ((Functor f, ListLike (f a) a) => b) -> b Source #

withListLikeF' :: ListLikeF f => f a -> ((Functor f, ListLike (f a) a) => b) -> b Source #

Accessors

Length information

length :: ListLike (f a) a => Sized f n a -> Int Source #

Returns the length of wrapped containers. If you use unsafeFromList or similar unsafe functions, this function may return different value from type-parameterized length.

Since 0.1.0.0

sLength :: forall f nat (n :: nat) a. (HasOrdinal nat, ListLike (f a) a) => Sized f n a -> Sing n Source #

Sing version of length.

Since 0.5.0.0 (type changed)

null :: ListLike (f a) a => Sized f n a -> Bool Source #

Test if the sequence is empty or not.

Since 0.1.0.0

Indexing

(!!) :: ListLike (f a) a => Sized f (Succ m) a -> Int -> a Source #

(Unsafe) indexing with Ints. If you want to check boundary statically, use %!! or sIndex.

Since 0.1.0.0

(%!!) :: (HasOrdinal nat, ListLike (f c) c) => Sized f n c -> Ordinal (n :: nat) -> c Source #

Safe indexing with Ordinals.

Since 0.1.0.0

index :: ListLike (f a) a => Int -> Sized f (Succ m) a -> a Source #

Flipped version of !!.

Since 0.1.0.0

sIndex :: (HasOrdinal nat, ListLike (f c) c) => Ordinal (n :: nat) -> Sized f n c -> c Source #

Flipped version of %!!.

Since 0.1.0.0

head :: (HasOrdinal nat, ListLike (f a) b, (Zero nat < n) ~ True) => Sized f n a -> b Source #

Take the first element of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

last :: (HasOrdinal nat, (Zero nat < n) ~ True, ListLike (f a) b) => Sized f n a -> b Source #

Take the last element of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

uncons :: ListLike (f a) b => Sized f (Succ n) a -> (b, Sized f n a) Source #

Take the head and tail of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

uncons' :: ListLike (f a) b => proxy n -> Sized f (Succ n) a -> (b, Sized f n a) Source #

unsnoc :: ListLike (f a) b => Sized f (Succ n) a -> (Sized f n a, b) Source #

Take the init and last of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

unsnoc' :: ListLike (f a) b => proxy n -> Sized f (Succ n) a -> (Sized f n a, b) Source #

Slicing

tail :: (HasOrdinal nat, ListLike (f a) a) => Sized f (Succ n) a -> Sized f (n :: nat) a Source #

Take the tail of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

init :: ListLike (f a) a => Sized f (Succ n) a -> Sized f n a Source #

Take the initial segment of non-empty sequence. If you want to make case-analysis for general sequence, see Views and Patterns section.

Since 0.1.0.0

take :: (ListLike (f a) a, (n <= m) ~ True, HasOrdinal nat) => Sing (n :: nat) -> Sized f m a -> Sized f n a Source #

take k xs takes first k element of xs where the length of xs should be larger than k. It is really sad, that this function takes at least O(k) regardless of base container.

Since 0.1.0.0

takeAtMost :: (ListLike (f a) a, HasOrdinal nat) => Sing (n :: nat) -> Sized f m a -> Sized f (Min n m) a Source #

take k xs takes first k element of xs at most. It is really sad, that this function takes at least O(k) regardless of base container.

Since 0.1.0.0

drop :: (HasOrdinal nat, ListLike (f a) a, (n <= m) ~ True) => Sing (n :: nat) -> Sized f m a -> Sized f (m - n) a Source #

drop k xs drops first k element of xs and returns the rest of sequence, where the length of xs should be larger than k. It is really sad, that this function takes at least O(k) regardless of base container.

Since 0.1.0.0

splitAt :: (ListLike (f a) a, (n <= m) ~ True, HasOrdinal nat) => Sing (n :: nat) -> Sized f m a -> (Sized f n a, Sized f (m -. n) a) Source #

splitAt k xs split xs at k, where the length of xs should be less than or equal to k. It is really sad, that this function takes at least O(k) regardless of base container.

Since 0.1.0.0

splitAtMost :: (HasOrdinal nat, ListLike (f a) a) => Sing (n :: nat) -> Sized f m a -> (Sized f (Min n m) a, Sized f (m -. n) a) Source #

splitAtMost k xs split xs at k. If k exceeds the length of xs, then the second result value become empty. It is really sad, that this function takes at least O(k) regardless of base container.

Since 0.1.0.0

Construction

Initialisation

empty :: forall f nat a. (HasOrdinal nat, ListLike (f a) a) => Sized f (Zero nat :: nat) a Source #

Empty sequence.

Since 0.5.0.0 (type changed)

singleton :: forall f a. ListLike (f a) a => a -> Sized f 1 a Source #

Sequence with one element.

Since 0.1.0.0

toSomeSized :: forall nat f a. (HasOrdinal nat, ListLike (f a) a) => f a -> SomeSized f nat a Source #

Consruct the Sized sequence from base type, but the length parameter is dynamically determined and existentially quantified; see also SomeSized.

Since 0.1.0.0

replicate :: forall f nat (n :: nat) a. (HasOrdinal nat, ListLike (f a) a) => Sing n -> a -> Sized f n a Source #

Replicates the same value.

Since 0.1.0.0

replicate' :: (HasOrdinal nat, SingI (n :: nat), ListLike (f a) a) => a -> Sized f n a Source #

replicate with the length inferred.

Since 0.1.0.0

generate :: forall (nat :: Type) (n :: nat) (a :: Type) f. (ListLike (f a) a, HasOrdinal nat) => Sing n -> (Ordinal n -> a) -> Sized f n a Source #

Concatenation

cons :: ListLike (f a) b => b -> Sized f n a -> Sized f (Succ n) a Source #

Append an element to the head of sequence.

Since 0.1.0.0

(<|) :: ListLike (f a) b => b -> Sized f n a -> Sized f (Succ n) a infixr 5 Source #

Infix version of cons.

Since 0.1.0.0

snoc :: ListLike (f a) b => Sized f n a -> b -> Sized f (Succ n) a Source #

Append an element to the tail of sequence.

Since 0.1.0.0

(|>) :: ListLike (f a) b => Sized f n a -> b -> Sized f (Succ n) a infixl 5 Source #

Infix version of snoc.

Since 0.1.0.0

append :: ListLike (f a) a => Sized f n a -> Sized f m a -> Sized f (n + m) a Source #

Append two lists.

Since 0.1.0.0

(++) :: ListLike (f a) a => Sized f n a -> Sized f m a -> Sized f (n + m) a infixr 5 Source #

Infix version of append.

Since 0.1.0.0

concat :: forall f f' m n a. (Functor f', Foldable f', ListLike (f a) a) => Sized f' m (Sized f n a) -> Sized f (m * n) a Source #

Concatenates multiple sequences into one.

Since 0.1.0.0

Zips

zip :: (ListLike (f a) a, ListLike (f b) b, ListLike (f (a, b)) (a, b)) => Sized f n a -> Sized f m b -> Sized f (Min n m) (a, b) Source #

Zipping two sequences. Length is adjusted to shorter one.

Since 0.1.0.0

zipSame :: (ListLike (f a) a, ListLike (f b) b, ListLike (f (a, b)) (a, b)) => Sized f n a -> Sized f n b -> Sized f n (a, b) Source #

zip for the sequences of the same length.

Since 0.1.0.0

zipWith :: (ListLike (f a) a, ListLike (f b) b, ListLike (f c) c) => (a -> b -> c) -> Sized f n a -> Sized f m b -> Sized f (Min n m) c Source #

Zipping two sequences with funtion. Length is adjusted to shorter one.

Since 0.1.0.0

zipWithSame :: (ListLike (f a) a, ListLike (f b) b, ListLike (f c) c) => (a -> b -> c) -> Sized f n a -> Sized f n b -> Sized f n c Source #

zipWith for the sequences of the same length.

Since 0.1.0.0

unzip :: (ListLike (f a) a, ListLike (f b) b, ListLike (f (a, b)) (a, b)) => Sized f n (a, b) -> (Sized f n a, Sized f n b) Source #

Unzipping the sequence of tuples.

Since 0.1.0.0

Transformation

map :: (ListLike (f a) a, ListLike (f b) b) => (a -> b) -> Sized f n a -> Sized f n b Source #

Map function.

Since 0.1.0.0

fmap :: forall f n a b. Functor f => (a -> b) -> Sized f n a -> Sized f n b Source #

reverse :: ListLike (f a) a => Sized f n a -> Sized f n a Source #

Reverse function.

Since 0.1.0.0

intersperse :: ListLike (f a) a => a -> Sized f n a -> Sized f ((FromInteger 2 * n) -. 1) a Source #

Intersperces.

Since 0.1.0.0

nub :: (HasOrdinal nat, ListLike (f a) a, Eq a) => Sized f n a -> SomeSized f nat a Source #

Remove all duplicates.

Since 0.1.0.0

sort :: (ListLike (f a) a, Ord a) => Sized f n a -> Sized f n a Source #

Sorting sequence by ascending order.

Since 0.1.0.0

sortBy :: ListLike (f a) a => (a -> a -> Ordering) -> Sized f n a -> Sized f n a Source #

Generalized version of sort.

Since 0.1.0.0

insert :: (ListLike (f a) a, Ord a) => a -> Sized f n a -> Sized f (Succ n) a Source #

Insert new element into the presorted sequence.

Since 0.1.0.0

insertBy :: ListLike (f a) a => (a -> a -> Ordering) -> a -> Sized f n a -> Sized f (Succ n) a Source #

Generalized version of insert.

Since 0.1.0.0

Conversion

List

toList :: ListLike (f a) a => Sized f n a -> [a] Source #

Convert to list.

Since 0.1.0.0

fromList :: forall f nat (n :: nat) a. (HasOrdinal nat, ListLike (f a) a) => Sing n -> [a] -> Maybe (Sized f n a) Source #

If the given list is shorter than n, then returns Nothing Otherwise returns Sized f n a consisting of initial n element of given list.

Since 0.5.0.0 (type changed)

fromList' :: (ListLike (f a) a, SingI (n :: Nat)) => [a] -> Maybe (Sized f n a) Source #

fromList with the result length inferred.

Since 0.1.0.0

unsafeFromList :: forall (nat :: Type) f (n :: nat) a. ListLike (f a) a => Sing n -> [a] -> Sized f n a Source #

Unsafe version of fromList. If the length of the given list does not equal to n, then something unusual happens.

Since 0.1.0.0

unsafeFromList' :: (SingI (n :: Nat), ListLike (f a) a) => [a] -> Sized f n a Source #

unsafeFromList with the result length inferred.

Since 0.1.0.0

fromListWithDefault :: forall f nat (n :: nat) a. (HasOrdinal nat, ListLike (f a) a) => Sing n -> a -> [a] -> Sized f n a Source #

Construct a Sized f n a by padding default value if the given list is short.

Since 0.5.0.0 (type changed)

fromListWithDefault' :: (SingI (n :: Nat), ListLike (f a) a) => a -> [a] -> Sized f n a Source #

fromListWithDefault with the result length inferred.

Since 0.1.0.0

Base container

unsized :: Sized f n a -> f a Source #

Forget the length and obtain the wrapped base container.

Since 0.1.0.0

toSized :: (HasOrdinal nat, ListLike (f a) a) => Sing (n :: nat) -> f a -> Maybe (Sized f n a) Source #

If the length of the input is shorter than n, then returns Nothing. Otherwise returns Sized f n a consisting of initial n element of the input.

Since 0.1.0.0

toSized' :: (ListLike (f a) a, SingI (n :: Nat)) => f a -> Maybe (Sized f n a) Source #

toSized with the result length inferred.

Since 0.1.0.0

unsafeToSized :: Sing n -> f a -> Sized f n a Source #

Unsafe version of toSized. If the length of the given list does not equal to n, then something unusual happens.

Since 0.1.0.0

unsafeToSized' :: (SingI (n :: Nat), ListLike (f a) a) => f a -> Sized f n a Source #

unsafeToSized with the result length inferred.

Since 0.1.0.0

toSizedWithDefault :: (HasOrdinal nat, ListLike (f a) a) => Sing (n :: nat) -> a -> f a -> Sized f n a Source #

Construct a Sized f n a by padding default value if the given list is short.

Since 0.1.0.0

toSizedWithDefault' :: (SingI (n :: Nat), ListLike (f a) a) => a -> f a -> Sized f n a Source #

toSizedWithDefault with the result length inferred.

Since 0.1.0.0

Querying

Partitioning

data Partitioned f n a where Source #

The type Partitioned f n a represents partitioned sequence of length n. Value Partitioned lenL ls lenR rs stands for:

  • Entire sequence is divided into ls and rs, and their length are lenL and lenR resp.
  • lenL + lenR = n

Since 0.1.0.0

Constructors

Partitioned :: ListLike (f a) a => Sing n -> Sized f (n :: Nat) a -> Sing m -> Sized f (m :: Nat) a -> Partitioned f (n + m) a 

takeWhile :: (HasOrdinal nat, ListLike (f a) a) => (a -> Bool) -> Sized f n a -> SomeSized f nat a Source #

Take the initial segment as long as elements satisfys the predicate.

Since 0.1.0.0

dropWhile :: (HasOrdinal nat, ListLike (f a) a) => (a -> Bool) -> Sized f n a -> SomeSized f nat a Source #

Drop the initial segment as long as elements satisfys the predicate.

Since 0.1.0.0

span :: ListLike (f a) a => (a -> Bool) -> Sized f n a -> Partitioned f n a Source #

Invariant: ListLike (f a) a instance must be implemented to satisfy the following property: length (fst (span p xs)) + length (snd (span p xs)) == length xs Otherwise, this function introduces severe contradiction.

Since 0.1.0.0

break :: ListLike (f a) a => (a -> Bool) -> Sized f n a -> Partitioned f n a Source #

Invariant: ListLike (f a) a instance must be implemented to satisfy the following property: length (fst (break p xs)) + length (snd (break p xs)) == length xs Otherwise, this function introduces severe contradiction.

Since 0.1.0.0

partition :: ListLike (f a) a => (a -> Bool) -> Sized f n a -> Partitioned f n a Source #

Invariant: ListLike (f a) a instance must be implemented to satisfy the following property: length (fst (partition p xs)) + length (snd (partition p xs)) == length xs Otherwise, this function introduces severe contradiction.

Since 0.1.0.0

Searching

elem :: (ListLike (f a) a, Eq a) => a -> Sized f n a -> Bool Source #

Membership test; see also notElem.

Since 0.1.0.0

notElem :: (ListLike (f a) a, Eq a) => a -> Sized f n a -> Bool Source #

Negation of elem.

Since 0.1.0.0

find :: Foldable f => (a -> Bool) -> Sized f n a -> Maybe a Source #

Find the element satisfying the predicate.

Since 0.1.0.0

findF :: Foldable f => (a -> Bool) -> Sized f n a -> Maybe a Source #

Foldable version of find.

findIndex :: ListLike (f a) a => (a -> Bool) -> Sized f n a -> Maybe Int Source #

findIndex p xs find the element satisfying p and returns its index if exists.

Since 0.1.0.0

findIndexIF :: FoldableWithIndex i f => (a -> Bool) -> Sized f n a -> Maybe i Source #

findIndex implemented in terms of FoldableWithIndex

sFindIndex :: (SingI (n :: nat), ListLike (f a) a, HasOrdinal nat) => (a -> Bool) -> Sized f n a -> Maybe (Ordinal n) Source #

Ordinal version of findIndex.

Since 0.1.0.0

sFindIndexIF :: (FoldableWithIndex i f, Integral i, HasOrdinal nat, SingI n) => (a -> Bool) -> Sized f (n :: nat) a -> Maybe (Ordinal n) Source #

sFindIndex implemented in terms of FoldableWithIndex

findIndices :: ListLike (f a) a => (a -> Bool) -> Sized f n a -> [Int] Source #

findIndices p xs find all elements satisfying p and returns their indices.

Since 0.1.0.0

findIndicesIF :: FoldableWithIndex i f => (a -> Bool) -> Sized f n a -> [i] Source #

findIndices implemented in terms of FoldableWithIndex

sFindIndices :: (HasOrdinal nat, SingI (n :: nat), ListLike (f a) a) => (a -> Bool) -> Sized f n a -> [Ordinal n] Source #

Ordinal version of findIndices.

Since 0.1.0.0

sFindIndicesIF :: (FoldableWithIndex i f, Integral i, HasOrdinal nat, SingI n) => (a -> Bool) -> Sized f (n :: nat) a -> [Ordinal n] Source #

elemIndex :: (Eq a, ListLike (f a) a) => a -> Sized f n a -> Maybe Int Source #

Returns the index of the given element in the list, if exists.

Since 0.1.0.0

sElemIndex :: forall nat (n :: nat) f a. (SingI n, ListLike (f a) a, Eq a, HasOrdinal nat) => a -> Sized f n a -> Maybe (Ordinal n) Source #

Ordinal version of elemIndex It statically checks boundary invariants. If you don't internal structure on Sized, then sUnsafeElemIndex is much faster and also safe for most cases.

Since 0.5.0.0 (type changed)

sUnsafeElemIndex :: forall nat (n :: nat) f a. (SingI n, ListLike (f a) a, Eq a, HasOrdinal nat) => a -> Sized f n a -> Maybe (Ordinal n) Source #

Since 0.5.0.0 (type changed)

elemIndices :: (ListLike (f a) a, Eq a) => a -> Sized f n a -> [Int] Source #

Returns all indices of the given element in the list.

Since 0.1.0.0

sElemIndices :: (HasOrdinal nat, SingI (n :: nat), ListLike (f a) a, Eq a) => a -> Sized f n a -> [Ordinal n] Source #

Ordinal version of elemIndices

Since 0.1.0.0

Views and Patterns

With GHC's ViewPatterns and PatternSynonym extensions, we can pattern-match on arbitrary Sized f n a if f is list-like functor. Curretnly, there are two direction view and patterns: Cons and Snoc. Assuming underlying sequence type f has O(1) implementation for null, head (resp. last) and tail (resp. init), We can view and pattern-match on cons (resp. snoc) of Sized f n a in O(1).

Views

With ViewPatterns extension, we can pattern-match on Sized value as follows:

slen :: (SingI n, 'ListLike (f a) a' f) => Sized f n a -> Sing n
slen (viewCons -> NilCV)    = SZ
slen (viewCons -> _ :- as) = SS (slen as)
slen _                          = error "impossible"

The constraint (SingI n, 'ListLike (f a) a' f) is needed for view function. In the above, we have extra wildcard pattern (_) at the last. Code compiles if we removed it, but current GHC warns for incomplete pattern, although we know first two patterns exhausts all the case.

Equivalently, we can use snoc-style pattern-matching:

slen :: (SingI n, 'ListLike (f a) a' f) => Sized f n a -> Sing n
slen (viewSnoc -> NilSV)     = SZ
slen (viewSnoc -> as -:: _) = SS (slen as)

Patterns

So we can pattern match on both end of sequence via views, but it is rather clumsy to nest it. For example:

nextToHead :: ('ListLike (f a) a' f, SingI n) => Sized f (S (S n)) a -> a
nextToHead (viewCons -> _ :- (viewCons -> a :- _)) = a

In such a case, with PatternSynonyms extension we can write as follows:

nextToHead :: ('ListLike (f a) a' f, SingI n) => Sized f (S (S n)) a -> a
nextToHead (_ :< a :< _) = a

Of course, we can also rewrite above slen example using PatternSynonyms:

slen :: (SingI n, 'ListLike (f a) a' f) => Sized f n a -> Sing n
slen NilL      = SZ
slen (_ :< as) = SS (slen as)
slen _           = error "impossible"

So, we can use :< and NilL (resp. :> and NilR) to pattern-match directly on cons-side (resp. snoc-side) as we usually do for lists. :<, NilL, :> and NilR are neither functions nor data constructors, but pattern synonyms so we cannot use them in expression contexts. For more detail on pattern synonyms, see GHC Users Guide and HaskellWiki.

Definitions

viewCons :: forall f a nat (n :: nat). (HasOrdinal nat, ListLike (f a) a) => Sized f n a -> ConsView f n a Source #

Case analysis for the cons-side of sequence.

Since 0.5.0.0 (type changed)

data ConsView f n a where Source #

View of the left end of sequence (cons-side).

Since 0.1.0.0

Constructors

NilCV :: ConsView f (Zero nat) a 
(:-) :: SingI n => a -> Sized f n a -> ConsView f (Succ n) a infixr 5 

viewSnoc :: forall f nat (n :: nat) a. (HasOrdinal nat, ListLike (f a) a) => Sized f n a -> SnocView f n a Source #

Case analysis for the snoc-side of sequence.

Since 0.5.0.0 (type changed)

data SnocView f n a where Source #

View of the left end of sequence (snoc-side).

Since 0.1.0.0

Constructors

NilSV :: SnocView f (Zero nat) a 
(:-::) :: SingI n => Sized f n a -> a -> SnocView f (Succ n) a infixl 5 

pattern (:<) :: forall nat f (n :: nat) a. (ListLike (f a) a, HasOrdinal nat) => forall (n1 :: nat). (n ~ Succ n1, SingI n1) => a -> Sized f n1 a -> Sized f n a infixr 5 Source #

Pattern synonym for cons-side uncons.

pattern NilL :: forall nat f (n :: nat) a. (ListLike (f a) a, HasOrdinal nat) => n ~ Zero nat => Sized f n a Source #

pattern (:>) :: forall nat f (n :: nat) a. (ListLike (f a) a, HasOrdinal nat) => forall (n1 :: nat). (n ~ Succ n1, SingI n1) => Sized f n1 a -> a -> Sized f n a infixl 5 Source #

pattern NilR :: forall nat f (n :: nat) a. (ListLike (f a) a, HasOrdinal nat) => n ~ Zero nat => Sized f n a Source #

Orphan instances

(Functor f, HasOrdinal nat, SingI n, ListLikeF f) => Applicative (Sized f n) Source #

Applicative instance, generalizing ZipList.

Instance details

Methods

pure :: a -> Sized f n a #

(<*>) :: Sized f n (a -> b) -> Sized f n a -> Sized f n b #

liftA2 :: (a -> b -> c) -> Sized f n a -> Sized f n b -> Sized f n c #

(*>) :: Sized f n a -> Sized f n b -> Sized f n b #

(<*) :: Sized f n a -> Sized f n b -> Sized f n a #