universum-0.9.2: Custom prelude used in Serokell

Safe HaskellTrustworthy
LanguageHaskell2010

Container.Class

Contents

Description

Reimagined approach for Foldable type hierarchy. Forbids usages of length function and similar over Maybe and other potentially unsafe data types. It was proposed to use -XTypeApplication for such cases. But this approach is not robust enough because programmers are human and can easily forget to do this. For discussion see this topic: Suggest explicit type application for Foldable length and friends

Synopsis

Foldable-like classes and methods

type family Element t Source #

Type of element for some container. Implemented as a type family because some containers are monomorphic over element type (like Text, IntSet, etc.) so we can't implement nice interface using old higher-kinded types approach.

Instances

class ToList t where Source #

Type class for data types that can be converted to List. Fully compatible with Foldable. Contains very small and safe subset of Foldable functions.

You can define Tolist by just defining toList function. But the following law should be met:

nullnull . toList

Minimal complete definition

toList

Methods

toList :: t -> [Element t] Source #

Convert container to list of elements.

>>> toList (Just True)
[True]
>>> toList @Text "aba"
"aba"
>>> :t toList @Text "aba"
toList @Text "aba" :: [Char]

null :: t -> Bool Source #

Checks whether container is empty.

>>> null @Text ""
True
>>> null @Text "aba"
False

Instances

ToList ByteString Source # 
ToList ByteString Source # 
ToList IntSet Source # 
ToList Text Source # 
ToList Text Source # 
Foldable f => ToList (f a) Source #

This instance makes ToList compatible and overlappable by Foldable.

Methods

toList :: f a -> [Element (f a)] Source #

null :: f a -> Bool Source #

TypeError Constraint (DisallowInstance "tuples") => ToList (a, b) Source # 

Methods

toList :: (a, b) -> [Element (a, b)] Source #

null :: (a, b) -> Bool Source #

ToList (f a) => ToList (WrappedList f a) Source # 

class ToList t => Container t where Source #

A class for ToLists that aren't trivial like Maybe (e.g. can hold more than one value)

Minimal complete definition

foldr, foldl, foldl', length, elem, maximum, minimum

Methods

foldMap :: Monoid m => (Element t -> m) -> t -> m Source #

fold :: Monoid (Element t) => t -> Element t Source #

foldr :: (Element t -> b -> b) -> b -> t -> b Source #

foldr' :: (Element t -> b -> b) -> b -> t -> b Source #

foldl :: (b -> Element t -> b) -> b -> t -> b Source #

foldl' :: (b -> Element t -> b) -> b -> t -> b Source #

foldr1 :: (Element t -> Element t -> Element t) -> t -> Element t Source #

foldl1 :: (Element t -> Element t -> Element t) -> t -> Element t Source #

length :: t -> Int Source #

elem :: Eq (Element t) => Element t -> t -> Bool Source #

notElem :: Eq (Element t) => Element t -> t -> Bool Source #

maximum :: Ord (Element t) => t -> Element t Source #

minimum :: Ord (Element t) => t -> Element t Source #

all :: (Element t -> Bool) -> t -> Bool Source #

any :: (Element t -> Bool) -> t -> Bool Source #

and :: Element t ~ Bool => t -> Bool Source #

or :: Element t ~ Bool => t -> Bool Source #

find :: (Element t -> Bool) -> t -> Maybe (Element t) Source #

head :: t -> Maybe (Element t) Source #

Instances

Container ByteString Source # 
Container ByteString Source # 
Container IntSet Source # 
Container Text Source # 
Container Text Source # 
TypeError Constraint (DisallowInstance "Maybe") => Container (Maybe a) Source # 

Methods

foldMap :: Monoid m => (Element (Maybe a) -> m) -> Maybe a -> m Source #

fold :: Maybe a -> Element (Maybe a) Source #

foldr :: (Element (Maybe a) -> b -> b) -> b -> Maybe a -> b Source #

foldr' :: (Element (Maybe a) -> b -> b) -> b -> Maybe a -> b Source #

foldl :: (b -> Element (Maybe a) -> b) -> b -> Maybe a -> b Source #

foldl' :: (b -> Element (Maybe a) -> b) -> b -> Maybe a -> b Source #

foldr1 :: (Element (Maybe a) -> Element (Maybe a) -> Element (Maybe a)) -> Maybe a -> Element (Maybe a) Source #

foldl1 :: (Element (Maybe a) -> Element (Maybe a) -> Element (Maybe a)) -> Maybe a -> Element (Maybe a) Source #

length :: Maybe a -> Int Source #

elem :: Element (Maybe a) -> Maybe a -> Bool Source #

notElem :: Element (Maybe a) -> Maybe a -> Bool Source #

maximum :: Maybe a -> Element (Maybe a) Source #

minimum :: Maybe a -> Element (Maybe a) Source #

all :: (Element (Maybe a) -> Bool) -> Maybe a -> Bool Source #

any :: (Element (Maybe a) -> Bool) -> Maybe a -> Bool Source #

and :: Maybe a -> Bool Source #

or :: Maybe a -> Bool Source #

find :: (Element (Maybe a) -> Bool) -> Maybe a -> Maybe (Element (Maybe a)) Source #

head :: Maybe a -> Maybe (Element (Maybe a)) Source #

Foldable f => Container (f a) Source # 

Methods

foldMap :: Monoid m => (Element (f a) -> m) -> f a -> m Source #

fold :: f a -> Element (f a) Source #

foldr :: (Element (f a) -> b -> b) -> b -> f a -> b Source #

foldr' :: (Element (f a) -> b -> b) -> b -> f a -> b Source #

foldl :: (b -> Element (f a) -> b) -> b -> f a -> b Source #

foldl' :: (b -> Element (f a) -> b) -> b -> f a -> b Source #

foldr1 :: (Element (f a) -> Element (f a) -> Element (f a)) -> f a -> Element (f a) Source #

foldl1 :: (Element (f a) -> Element (f a) -> Element (f a)) -> f a -> Element (f a) Source #

length :: f a -> Int Source #

elem :: Element (f a) -> f a -> Bool Source #

notElem :: Element (f a) -> f a -> Bool Source #

maximum :: f a -> Element (f a) Source #

minimum :: f a -> Element (f a) Source #

all :: (Element (f a) -> Bool) -> f a -> Bool Source #

any :: (Element (f a) -> Bool) -> f a -> Bool Source #

and :: f a -> Bool Source #

or :: f a -> Bool Source #

find :: (Element (f a) -> Bool) -> f a -> Maybe (Element (f a)) Source #

head :: f a -> Maybe (Element (f a)) Source #

TypeError Constraint (DisallowInstance "Identity") => Container (Identity a) Source # 

Methods

foldMap :: Monoid m => (Element (Identity a) -> m) -> Identity a -> m Source #

fold :: Identity a -> Element (Identity a) Source #

foldr :: (Element (Identity a) -> b -> b) -> b -> Identity a -> b Source #

foldr' :: (Element (Identity a) -> b -> b) -> b -> Identity a -> b Source #

foldl :: (b -> Element (Identity a) -> b) -> b -> Identity a -> b Source #

foldl' :: (b -> Element (Identity a) -> b) -> b -> Identity a -> b Source #

foldr1 :: (Element (Identity a) -> Element (Identity a) -> Element (Identity a)) -> Identity a -> Element (Identity a) Source #

foldl1 :: (Element (Identity a) -> Element (Identity a) -> Element (Identity a)) -> Identity a -> Element (Identity a) Source #

length :: Identity a -> Int Source #

elem :: Element (Identity a) -> Identity a -> Bool Source #

notElem :: Element (Identity a) -> Identity a -> Bool Source #

maximum :: Identity a -> Element (Identity a) Source #

minimum :: Identity a -> Element (Identity a) Source #

all :: (Element (Identity a) -> Bool) -> Identity a -> Bool Source #

any :: (Element (Identity a) -> Bool) -> Identity a -> Bool Source #

and :: Identity a -> Bool Source #

or :: Identity a -> Bool Source #

find :: (Element (Identity a) -> Bool) -> Identity a -> Maybe (Element (Identity a)) Source #

head :: Identity a -> Maybe (Element (Identity a)) Source #

TypeError Constraint (DisallowInstance "Either") => Container (Either a b) Source # 

Methods

foldMap :: Monoid m => (Element (Either a b) -> m) -> Either a b -> m Source #

fold :: Either a b -> Element (Either a b) Source #

foldr :: (Element (Either a b) -> b -> b) -> b -> Either a b -> b Source #

foldr' :: (Element (Either a b) -> b -> b) -> b -> Either a b -> b Source #

foldl :: (b -> Element (Either a b) -> b) -> b -> Either a b -> b Source #

foldl' :: (b -> Element (Either a b) -> b) -> b -> Either a b -> b Source #

foldr1 :: (Element (Either a b) -> Element (Either a b) -> Element (Either a b)) -> Either a b -> Element (Either a b) Source #

foldl1 :: (Element (Either a b) -> Element (Either a b) -> Element (Either a b)) -> Either a b -> Element (Either a b) Source #

length :: Either a b -> Int Source #

elem :: Element (Either a b) -> Either a b -> Bool Source #

notElem :: Element (Either a b) -> Either a b -> Bool Source #

maximum :: Either a b -> Element (Either a b) Source #

minimum :: Either a b -> Element (Either a b) Source #

all :: (Element (Either a b) -> Bool) -> Either a b -> Bool Source #

any :: (Element (Either a b) -> Bool) -> Either a b -> Bool Source #

and :: Either a b -> Bool Source #

or :: Either a b -> Bool Source #

find :: (Element (Either a b) -> Bool) -> Either a b -> Maybe (Element (Either a b)) Source #

head :: Either a b -> Maybe (Element (Either a b)) Source #

TypeError Constraint (DisallowInstance "tuples") => Container (a, b) Source # 

Methods

foldMap :: Monoid m => (Element (a, b) -> m) -> (a, b) -> m Source #

fold :: (a, b) -> Element (a, b) Source #

foldr :: (Element (a, b) -> b -> b) -> b -> (a, b) -> b Source #

foldr' :: (Element (a, b) -> b -> b) -> b -> (a, b) -> b Source #

foldl :: (b -> Element (a, b) -> b) -> b -> (a, b) -> b Source #

foldl' :: (b -> Element (a, b) -> b) -> b -> (a, b) -> b Source #

foldr1 :: (Element (a, b) -> Element (a, b) -> Element (a, b)) -> (a, b) -> Element (a, b) Source #

foldl1 :: (Element (a, b) -> Element (a, b) -> Element (a, b)) -> (a, b) -> Element (a, b) Source #

length :: (a, b) -> Int Source #

elem :: Element (a, b) -> (a, b) -> Bool Source #

notElem :: Element (a, b) -> (a, b) -> Bool Source #

maximum :: (a, b) -> Element (a, b) Source #

minimum :: (a, b) -> Element (a, b) Source #

all :: (Element (a, b) -> Bool) -> (a, b) -> Bool Source #

any :: (Element (a, b) -> Bool) -> (a, b) -> Bool Source #

and :: (a, b) -> Bool Source #

or :: (a, b) -> Bool Source #

find :: (Element (a, b) -> Bool) -> (a, b) -> Maybe (Element (a, b)) Source #

head :: (a, b) -> Maybe (Element (a, b)) Source #

ToList (f a) => Container (WrappedList f a) Source # 

Methods

foldMap :: Monoid m => (Element (WrappedList f a) -> m) -> WrappedList f a -> m Source #

fold :: WrappedList f a -> Element (WrappedList f a) Source #

foldr :: (Element (WrappedList f a) -> b -> b) -> b -> WrappedList f a -> b Source #

foldr' :: (Element (WrappedList f a) -> b -> b) -> b -> WrappedList f a -> b Source #

foldl :: (b -> Element (WrappedList f a) -> b) -> b -> WrappedList f a -> b Source #

foldl' :: (b -> Element (WrappedList f a) -> b) -> b -> WrappedList f a -> b Source #

foldr1 :: (Element (WrappedList f a) -> Element (WrappedList f a) -> Element (WrappedList f a)) -> WrappedList f a -> Element (WrappedList f a) Source #

foldl1 :: (Element (WrappedList f a) -> Element (WrappedList f a) -> Element (WrappedList f a)) -> WrappedList f a -> Element (WrappedList f a) Source #

length :: WrappedList f a -> Int Source #

elem :: Element (WrappedList f a) -> WrappedList f a -> Bool Source #

notElem :: Element (WrappedList f a) -> WrappedList f a -> Bool Source #

maximum :: WrappedList f a -> Element (WrappedList f a) Source #

minimum :: WrappedList f a -> Element (WrappedList f a) Source #

all :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Bool Source #

any :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Bool Source #

and :: WrappedList f a -> Bool Source #

or :: WrappedList f a -> Bool Source #

find :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Maybe (Element (WrappedList f a)) Source #

head :: WrappedList f a -> Maybe (Element (WrappedList f a)) Source #

type NontrivialContainer t = Container t Source #

To save backwards compatibility with previous naming.

newtype WrappedList f a Source #

This can be useful if you want to use Container methods for your data type but you don't want to implement all methods of this type class for that.

Constructors

WrappedList (f a) 

Instances

ToList (f a) => Container (WrappedList f a) Source # 

Methods

foldMap :: Monoid m => (Element (WrappedList f a) -> m) -> WrappedList f a -> m Source #

fold :: WrappedList f a -> Element (WrappedList f a) Source #

foldr :: (Element (WrappedList f a) -> b -> b) -> b -> WrappedList f a -> b Source #

foldr' :: (Element (WrappedList f a) -> b -> b) -> b -> WrappedList f a -> b Source #

foldl :: (b -> Element (WrappedList f a) -> b) -> b -> WrappedList f a -> b Source #

foldl' :: (b -> Element (WrappedList f a) -> b) -> b -> WrappedList f a -> b Source #

foldr1 :: (Element (WrappedList f a) -> Element (WrappedList f a) -> Element (WrappedList f a)) -> WrappedList f a -> Element (WrappedList f a) Source #

foldl1 :: (Element (WrappedList f a) -> Element (WrappedList f a) -> Element (WrappedList f a)) -> WrappedList f a -> Element (WrappedList f a) Source #

length :: WrappedList f a -> Int Source #

elem :: Element (WrappedList f a) -> WrappedList f a -> Bool Source #

notElem :: Element (WrappedList f a) -> WrappedList f a -> Bool Source #

maximum :: WrappedList f a -> Element (WrappedList f a) Source #

minimum :: WrappedList f a -> Element (WrappedList f a) Source #

all :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Bool Source #

any :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Bool Source #

and :: WrappedList f a -> Bool Source #

or :: WrappedList f a -> Bool Source #

find :: (Element (WrappedList f a) -> Bool) -> WrappedList f a -> Maybe (Element (WrappedList f a)) Source #

head :: WrappedList f a -> Maybe (Element (WrappedList f a)) Source #

ToList (f a) => ToList (WrappedList f a) Source # 
type Element (WrappedList f a) Source # 
type Element (WrappedList f a) = a

sum :: (Container t, Num (Element t)) => t -> Element t Source #

Stricter version of sum.

>>> sum [1..10]
55
>>> sum (Just 3)
<interactive>:43:1: error:
    • Do not use 'Foldable' methods on Maybe
    • In the expression: sum (Just 3)
      In an equation for ‘it’: it = sum (Just 3)

product :: (Container t, Num (Element t)) => t -> Element t Source #

Stricter version of product.

>>> product [1..10]
3628800
>>> product (Right 3)
<interactive>:45:1: error:
    • Do not use 'Foldable' methods on Either
    • In the expression: product (Right 3)
      In an equation for ‘it’: it = product (Right 3)

mapM_ :: (Container t, Monad m) => (Element t -> m b) -> t -> m () Source #

Constrained to Container version of mapM_.

forM_ :: (Container t, Monad m) => t -> (Element t -> m b) -> m () Source #

Constrained to Container version of forM_.

traverse_ :: (Container t, Applicative f) => (Element t -> f b) -> t -> f () Source #

Constrained to Container version of traverse_.

for_ :: (Container t, Applicative f) => t -> (Element t -> f b) -> f () Source #

Constrained to Container version of for_.

sequenceA_ :: (Container t, Applicative f, Element t ~ f a) => t -> f () Source #

Constrained to Container version of sequenceA_.

sequence_ :: (Container t, Monad m, Element t ~ m a) => t -> m () Source #

Constrained to Container version of sequence_.

asum :: (Container t, Alternative f, Element t ~ f a) => t -> f a Source #

Constrained to Container version of asum.

Others

class One x where Source #

Type class for types that can be created from one element. singleton is lone name for this function. Also constructions of different type differ: :[] for lists, two arguments for Maps. Also some data types are monomorphic.

>>> one True :: [Bool]
[True]
>>> one 'a' :: Text
"a"
>>> one (3, "hello") :: HashMap Int String
fromList [(3,"hello")]

Minimal complete definition

one

Associated Types

type OneItem x Source #

Methods

one :: OneItem x -> x Source #

Create a list, map, Text, etc from a single element.

Instances

One ByteString Source # 

Associated Types

type OneItem ByteString :: * Source #

One ByteString Source # 

Associated Types

type OneItem ByteString :: * Source #

One IntSet Source # 

Associated Types

type OneItem IntSet :: * Source #

One Text Source # 

Associated Types

type OneItem Text :: * Source #

Methods

one :: OneItem Text -> Text Source #

One Text Source # 

Associated Types

type OneItem Text :: * Source #

Methods

one :: OneItem Text -> Text Source #

One [a] Source # 

Associated Types

type OneItem [a] :: * Source #

Methods

one :: OneItem [a] -> [a] Source #

One (NonEmpty a) Source # 

Associated Types

type OneItem (NonEmpty a) :: * Source #

Methods

one :: OneItem (NonEmpty a) -> NonEmpty a Source #

One (IntMap v) Source # 

Associated Types

type OneItem (IntMap v) :: * Source #

Methods

one :: OneItem (IntMap v) -> IntMap v Source #

One (Seq a) Source # 

Associated Types

type OneItem (Seq a) :: * Source #

Methods

one :: OneItem (Seq a) -> Seq a Source #

One (Set v) Source # 

Associated Types

type OneItem (Set v) :: * Source #

Methods

one :: OneItem (Set v) -> Set v Source #

Hashable v => One (HashSet v) Source # 

Associated Types

type OneItem (HashSet v) :: * Source #

Methods

one :: OneItem (HashSet v) -> HashSet v Source #

Unbox a => One (Vector a) Source # 

Associated Types

type OneItem (Vector a) :: * Source #

Methods

one :: OneItem (Vector a) -> Vector a Source #

Storable a => One (Vector a) Source # 

Associated Types

type OneItem (Vector a) :: * Source #

Methods

one :: OneItem (Vector a) -> Vector a Source #

Prim a => One (Vector a) Source # 

Associated Types

type OneItem (Vector a) :: * Source #

Methods

one :: OneItem (Vector a) -> Vector a Source #

One (Vector a) Source # 

Associated Types

type OneItem (Vector a) :: * Source #

Methods

one :: OneItem (Vector a) -> Vector a Source #

One (Map k v) Source # 

Associated Types

type OneItem (Map k v) :: * Source #

Methods

one :: OneItem (Map k v) -> Map k v Source #

Hashable k => One (HashMap k v) Source # 

Associated Types

type OneItem (HashMap k v) :: * Source #

Methods

one :: OneItem (HashMap k v) -> HashMap k v Source #