Safe Haskell | None |
---|---|
Language | Haskell2010 |
Type classes mirroring standard typeclasses, but working with monomorphic containers.
The motivation is that some commonly used data types (i.e., ByteString
and
Text
) do not allow for instances of typeclasses like Functor
and
Foldable
, since they are monomorphic structures. This module allows both
monomorphic and polymorphic data types to be instances of the same
typeclasses.
All of the laws for the polymorphic typeclasses apply to their monomorphic
cousins. Thus, even though a MonoFunctor
instance for Set
could
theoretically be defined, it is omitted since it could violate the functor
law of
.omap
f . omap
g = omap
(f . g)
Note that all typeclasses have been prefixed with Mono
, and functions have
been prefixed with o
. The mnemonic for o
is "only one", or alternatively
"it's mono, but m is overused in Haskell, so we'll use the second letter
instead." (Agreed, it's not a great mangling scheme, input is welcome!)
Synopsis
- type family Element mono
- class MonoFunctor mono where
- replaceElem :: (MonoFunctor mono, Eq (Element mono)) => Element mono -> Element mono -> mono -> mono
- replaceElemStrictText :: Char -> Char -> Text -> Text
- replaceElemLazyText :: Char -> Char -> Text -> Text
- class MonoFoldable mono where
- ofoldMap :: Monoid m => (Element mono -> m) -> mono -> m
- ofoldr :: (Element mono -> b -> b) -> b -> mono -> b
- ofoldl' :: (a -> Element mono -> a) -> a -> mono -> a
- otoList :: mono -> [Element mono]
- oall :: (Element mono -> Bool) -> mono -> Bool
- oany :: (Element mono -> Bool) -> mono -> Bool
- onull :: mono -> Bool
- olength :: mono -> Int
- olength64 :: mono -> Int64
- ocompareLength :: Integral i => mono -> i -> Ordering
- otraverse_ :: Applicative f => (Element mono -> f b) -> mono -> f ()
- ofor_ :: Applicative f => mono -> (Element mono -> f b) -> f ()
- omapM_ :: Applicative m => (Element mono -> m ()) -> mono -> m ()
- oforM_ :: Applicative m => mono -> (Element mono -> m ()) -> m ()
- ofoldlM :: Monad m => (a -> Element mono -> m a) -> a -> mono -> m a
- ofoldMap1Ex :: Semigroup m => (Element mono -> m) -> mono -> m
- ofoldr1Ex :: (Element mono -> Element mono -> Element mono) -> mono -> Element mono
- ofoldl1Ex' :: (Element mono -> Element mono -> Element mono) -> mono -> Element mono
- headEx :: mono -> Element mono
- lastEx :: mono -> Element mono
- unsafeHead :: mono -> Element mono
- unsafeLast :: mono -> Element mono
- maximumByEx :: (Element mono -> Element mono -> Ordering) -> mono -> Element mono
- minimumByEx :: (Element mono -> Element mono -> Ordering) -> mono -> Element mono
- oelem :: Eq (Element mono) => Element mono -> mono -> Bool
- onotElem :: Eq (Element mono) => Element mono -> mono -> Bool
- headMay :: MonoFoldable mono => mono -> Maybe (Element mono)
- lastMay :: MonoFoldable mono => mono -> Maybe (Element mono)
- osum :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono
- oproduct :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono
- oand :: (Element mono ~ Bool, MonoFoldable mono) => mono -> Bool
- oor :: (Element mono ~ Bool, MonoFoldable mono) => mono -> Bool
- oconcatMap :: (MonoFoldable mono, Monoid m) => (Element mono -> m) -> mono -> m
- ofold :: (MonoFoldable mono, Monoid (Element mono)) => mono -> Element mono
- oconcat :: (MonoFoldable mono, Monoid (Element mono)) => mono -> Element mono
- ofoldM :: (MonoFoldable mono, Monad m) => (a -> Element mono -> m a) -> a -> mono -> m a
- osequence_ :: (Applicative m, MonoFoldable mono, Element mono ~ m ()) => mono -> m ()
- maximumEx :: (MonoFoldable mono, Ord (Element mono)) => mono -> Element mono
- minimumEx :: (MonoFoldable mono, Ord (Element mono)) => mono -> Element mono
- maximumMay :: (MonoFoldable mono, Ord (Element mono)) => mono -> Maybe (Element mono)
- maximumByMay :: MonoFoldable mono => (Element mono -> Element mono -> Ordering) -> mono -> Maybe (Element mono)
- minimumMay :: (MonoFoldable mono, Ord (Element mono)) => mono -> Maybe (Element mono)
- minimumByMay :: MonoFoldable mono => (Element mono -> Element mono -> Ordering) -> mono -> Maybe (Element mono)
- class (MonoFunctor mono, MonoFoldable mono) => MonoTraversable mono where
- otraverse :: Applicative f => (Element mono -> f (Element mono)) -> mono -> f mono
- omapM :: Applicative m => (Element mono -> m (Element mono)) -> mono -> m mono
- ofor :: (MonoTraversable mono, Applicative f) => mono -> (Element mono -> f (Element mono)) -> f mono
- oforM :: (MonoTraversable mono, Applicative f) => mono -> (Element mono -> f (Element mono)) -> f mono
- ofoldlUnwrap :: MonoFoldable mono => (x -> Element mono -> x) -> x -> (x -> b) -> mono -> b
- ofoldMUnwrap :: (Monad m, MonoFoldable mono) => (x -> Element mono -> m x) -> m x -> (x -> m b) -> mono -> m b
- class MonoPointed mono where
- class MonoFunctor mono => MonoComonad mono where
- class MonoFoldable mono => GrowingAppend mono
- ointercalate :: (MonoFoldable mono, Monoid (Element mono)) => Element mono -> mono -> Element mono
- newtype WrappedPoly f a = WrappedPoly {
- unwrapPoly :: f a
Documentation
type family Element mono Source #
Type family for getting the type of the elements of a monomorphic container.
Instances
class MonoFunctor mono where Source #
Monomorphic containers that can be mapped over.
Nothing
omap :: (Element mono -> Element mono) -> mono -> mono Source #
Map over a monomorphic container
omap :: (Functor f, Element (f a) ~ a, f a ~ mono) => (Element mono -> Element mono) -> mono -> mono Source #
Map over a monomorphic container
Instances
replaceElem :: (MonoFunctor mono, Eq (Element mono)) => Element mono -> Element mono -> mono -> mono Source #
replaces all replaceElem
old newold
elements with new
.
Since: 1.0.1
class MonoFoldable mono where Source #
Monomorphic containers that can be folded.
Nothing
ofoldMap :: Monoid m => (Element mono -> m) -> mono -> m Source #
Map each element of a monomorphic container to a Monoid
and combine the results.
ofoldMap :: (t a ~ mono, a ~ Element (t a), Foldable t, Monoid m) => (Element mono -> m) -> mono -> m Source #
Map each element of a monomorphic container to a Monoid
and combine the results.
ofoldr :: (Element mono -> b -> b) -> b -> mono -> b Source #
Right-associative fold of a monomorphic container.
ofoldr :: (t a ~ mono, a ~ Element (t a), Foldable t) => (Element mono -> b -> b) -> b -> mono -> b Source #
Right-associative fold of a monomorphic container.
ofoldl' :: (a -> Element mono -> a) -> a -> mono -> a Source #
Strict left-associative fold of a monomorphic container.
ofoldl' :: (t b ~ mono, b ~ Element (t b), Foldable t) => (a -> Element mono -> a) -> a -> mono -> a Source #
Strict left-associative fold of a monomorphic container.
otoList :: mono -> [Element mono] Source #
Convert a monomorphic container to a list.
oall :: (Element mono -> Bool) -> mono -> Bool Source #
Are all of the elements in a monomorphic container
converted to booleans True
?
oany :: (Element mono -> Bool) -> mono -> Bool Source #
Are any of the elements in a monomorphic container
converted to booleans True
?
onull :: mono -> Bool Source #
Is the monomorphic container empty?
olength :: mono -> Int Source #
Length of a monomorphic container, returns a Int
.
olength64 :: mono -> Int64 Source #
Length of a monomorphic container, returns a Int64
.
ocompareLength :: Integral i => mono -> i -> Ordering Source #
Compare the length of a monomorphic container and a given number.
otraverse_ :: Applicative f => (Element mono -> f b) -> mono -> f () Source #
Map each element of a monomorphic container to an action, evaluate these actions from left to right, and ignore the results.
ofor_ :: Applicative f => mono -> (Element mono -> f b) -> f () Source #
ofor_
is otraverse_
with its arguments flipped.
omapM_ :: Applicative m => (Element mono -> m ()) -> mono -> m () Source #
Map each element of a monomorphic container to a monadic action, evaluate these actions from left to right, and ignore the results.
oforM_ :: Applicative m => mono -> (Element mono -> m ()) -> m () Source #
ofoldlM :: Monad m => (a -> Element mono -> m a) -> a -> mono -> m a Source #
Monadic fold over the elements of a monomorphic container, associating to the left.
ofoldMap1Ex :: Semigroup m => (Element mono -> m) -> mono -> m Source #
Map each element of a monomorphic container to a semigroup, and combine the results.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See ofoldMap1
from Data.NonNull for a total version of this function.
ofoldr1Ex :: (Element mono -> Element mono -> Element mono) -> mono -> Element mono Source #
Right-associative fold of a monomorphic container with no base element.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See ofoldr1
from Data.NonNull for a total version of this function.
ofoldr1Ex :: (t a ~ mono, a ~ Element (t a), Foldable t) => (Element mono -> Element mono -> Element mono) -> mono -> Element mono Source #
Right-associative fold of a monomorphic container with no base element.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See ofoldr1
from Data.NonNull for a total version of this function.
ofoldl1Ex' :: (Element mono -> Element mono -> Element mono) -> mono -> Element mono Source #
Strict left-associative fold of a monomorphic container with no base element.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See ofoldl1'
from Data.NonNull for a total version of this function.
ofoldl1Ex' :: (t a ~ mono, a ~ Element (t a), Foldable t) => (Element mono -> Element mono -> Element mono) -> mono -> Element mono Source #
Strict left-associative fold of a monomorphic container with no base element.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See ofoldl1'
from Data.NonNull for a total version of this function.
headEx :: mono -> Element mono Source #
Get the first element of a monomorphic container.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See head
from Data.NonNull for a total version of this function.
lastEx :: mono -> Element mono Source #
Get the last element of a monomorphic container.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See last
from Data.NonNull for a total version of this function.
unsafeHead :: mono -> Element mono Source #
Equivalent to headEx
.
unsafeLast :: mono -> Element mono Source #
Equivalent to lastEx
.
maximumByEx :: (Element mono -> Element mono -> Ordering) -> mono -> Element mono Source #
Get the maximum element of a monomorphic container, using a supplied element ordering function.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See maximiumBy
from Data.NonNull for a total version of this function.
minimumByEx :: (Element mono -> Element mono -> Ordering) -> mono -> Element mono Source #
Get the minimum element of a monomorphic container, using a supplied element ordering function.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See minimumBy
from Data.NonNull for a total version of this function.
oelem :: Eq (Element mono) => Element mono -> mono -> Bool Source #
Checks if the monomorphic container includes the supplied element.
onotElem :: Eq (Element mono) => Element mono -> mono -> Bool Source #
Checks if the monomorphic container does not include the supplied element.
Instances
osum :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono Source #
osum
computes the sum of the numbers of a monomorphic container.
oproduct :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono Source #
oproduct
computes the product of the numbers of a monomorphic container.
oand :: (Element mono ~ Bool, MonoFoldable mono) => mono -> Bool Source #
Are all of the elements True
?
Since: 0.6.0
oor :: (Element mono ~ Bool, MonoFoldable mono) => mono -> Bool Source #
Are any of the elements True
?
Since: 0.6.0
oconcatMap :: (MonoFoldable mono, Monoid m) => (Element mono -> m) -> mono -> m Source #
Synonym for ofoldMap
Since: 1.0.0
ofold :: (MonoFoldable mono, Monoid (Element mono)) => mono -> Element mono Source #
Monoidally combine all values in the container
Since: 1.0.0
oconcat :: (MonoFoldable mono, Monoid (Element mono)) => mono -> Element mono Source #
Synonym for ofold
Since: 1.0.0
ofoldM :: (MonoFoldable mono, Monad m) => (a -> Element mono -> m a) -> a -> mono -> m a Source #
Synonym for ofoldlM
Since: 1.0.0
osequence_ :: (Applicative m, MonoFoldable mono, Element mono ~ m ()) => mono -> m () Source #
Perform all actions in the given container
Since: 1.0.0
maximumEx :: (MonoFoldable mono, Ord (Element mono)) => mono -> Element mono Source #
Get the minimum element of a monomorphic container.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See maximum
from Data.NonNull for a total version of this function.
minimumEx :: (MonoFoldable mono, Ord (Element mono)) => mono -> Element mono Source #
Get the maximum element of a monomorphic container.
Note: this is a partial function. On an empty MonoFoldable
, it will
throw an exception.
See minimum
from Data.NonNull for a total version of this function.
maximumMay :: (MonoFoldable mono, Ord (Element mono)) => mono -> Maybe (Element mono) Source #
maximumByMay :: MonoFoldable mono => (Element mono -> Element mono -> Ordering) -> mono -> Maybe (Element mono) Source #
Safe version of maximumByEx
.
Returns Nothing
instead of throwing an exception when
encountering an empty monomorphic container.
minimumMay :: (MonoFoldable mono, Ord (Element mono)) => mono -> Maybe (Element mono) Source #
minimumByMay :: MonoFoldable mono => (Element mono -> Element mono -> Ordering) -> mono -> Maybe (Element mono) Source #
Safe version of minimumByEx
.
Returns Nothing
instead of throwing an exception when
encountering an empty monomorphic container.
class (MonoFunctor mono, MonoFoldable mono) => MonoTraversable mono where Source #
Monomorphic containers that can be traversed from left to right.
NOTE: Due to limitations with the role system, GHC is yet unable to provide newtype-derivation of
MonoTraversable
. See https://stackoverflow.com/questions/49776924/newtype-deriving-issequence.
Nothing
otraverse :: Applicative f => (Element mono -> f (Element mono)) -> mono -> f mono Source #
Map each element of a monomorphic container to an action, evaluate these actions from left to right, and collect the results.
otraverse :: (Traversable t, mono ~ t a, a ~ Element mono, Applicative f) => (Element mono -> f (Element mono)) -> mono -> f mono Source #
Map each element of a monomorphic container to an action, evaluate these actions from left to right, and collect the results.
omapM :: Applicative m => (Element mono -> m (Element mono)) -> mono -> m mono Source #
Map each element of a monomorphic container to a monadic action, evaluate these actions from left to right, and collect the results.
Instances
ofor :: (MonoTraversable mono, Applicative f) => mono -> (Element mono -> f (Element mono)) -> f mono Source #
oforM :: (MonoTraversable mono, Applicative f) => mono -> (Element mono -> f (Element mono)) -> f mono Source #
ofoldlUnwrap :: MonoFoldable mono => (x -> Element mono -> x) -> x -> (x -> b) -> mono -> b Source #
A strict left fold, together with an unwrap function.
This is convenient when the accumulator value is not the same as the final
expected type. It is provided mainly for integration with the foldl
package, to be used in conjunction with purely
.
Since: 0.3.1
ofoldMUnwrap :: (Monad m, MonoFoldable mono) => (x -> Element mono -> m x) -> m x -> (x -> m b) -> mono -> m b Source #
A monadic strict left fold, together with an unwrap function.
Similar to foldlUnwrap
, but allows monadic actions. To be used with
impurely
from foldl
.
Since: 0.3.1
class MonoPointed mono where Source #
Typeclass for monomorphic containers that an element can be lifted into.
For any MonoFunctor
, the following law holds:
omap
f .opoint
=opoint
. f
Nothing
opoint :: Element mono -> mono Source #
Lift an element into a monomorphic container.
opoint
is the same as pure
for an Applicative
opoint :: (Applicative f, f a ~ mono, Element (f a) ~ a) => Element mono -> mono Source #
Lift an element into a monomorphic container.
opoint
is the same as pure
for an Applicative
Instances
class MonoFunctor mono => MonoComonad mono where Source #
Typeclass for monomorphic containers where it is always okay to
"extract" a value from with oextract
, and where you can extrapolate
any "extracting" function to be a function on the whole part with
oextend
.
oextend
and oextract
should work together following the laws:
oextend
oextract
=id
oextract
.oextend
f = foextend
f .oextend
g =oextend
(f .oextend
g)
As an intuition,
uses oextend
ff
to "build up" a new mono
with
pieces from the old one received by f
.
oextract :: mono -> Element mono Source #
Extract an element from mono
. Can be thought of as a dual
concept to opoint
.
Instances
MonoComonad (ViewL a) Source # | |
MonoComonad (ViewR a) Source # | |
IsSequence mono => MonoComonad (NonNull mono) Source # | |
class MonoFoldable mono => GrowingAppend mono Source #
Containers which, when two values are combined, the combined length is no less than the larger of the two inputs. In code:
olength (x <> y) >= max (olength x) (olength y)
This class has no methods, and is simply used to assert that this law holds, in order to provide guarantees of correctness (see, for instance, Data.NonNull).
This should have a Semigroup
superclass constraint, however, due to
Semigroup
only recently moving to base, some packages do not provide
instances.
Instances
ointercalate :: (MonoFoldable mono, Monoid (Element mono)) => Element mono -> mono -> Element mono Source #
intercalate
seq seqs
inserts seq
in between seqs
and
concatenates the result.
Since: 1.0.0
newtype WrappedPoly f a Source #
Provides a MonoFoldable
, MonoFunctor
or MonoPointed
for an arbitrary
Foldable
, Functor
or Applicative
.
Useful for, e.g., passing a Foldable
type you don't own into a
function that expects a MonoFoldable
.
// package A data MyList a = MyList [a] deriving Foldable // package B process :: MonoFoldable mono => mono -> IO () // package C process (WrappedPoly (MyList []))
Since: 1.0.13.0
WrappedPoly | |
|