Copyright | (c) 2014 Patrick Bahr |
---|---|
License | BSD3 |
Maintainer | Patrick Bahr <paba@diku.dk> |
Stability | experimental |
Portability | non-portable (GHC Extensions) |
Safe Haskell | None |
Language | Haskell2010 |
This module provides functionality to construct mappings from positions in a functorial value.
Synopsis
- data Numbered a = Numbered Int a
- unNumbered :: Numbered a -> a
- number :: Traversable f => f a -> f (Numbered a)
- class (Functor t, Foldable t) => Traversable (t :: Type -> Type)
- class Functor m => Mapping m k | m -> k where
- (&) :: m v -> m v -> m v
- (|->) :: k -> v -> m v
- empty :: m v
- prodMapWith :: (v1 -> v2 -> v) -> v1 -> v2 -> m v1 -> m v2 -> m v
- findWithDefault :: a -> k -> m a -> a
- prodMap :: Mapping m k => v1 -> v2 -> m v1 -> m v2 -> m (v1, v2)
- lookupNumMap :: a -> Int -> NumMap t a -> a
- lookupNumMap' :: Int -> NumMap t a -> Maybe a
- data NumMap k v
Documentation
This type is used for numbering components of a functorial value.
unNumbered :: Numbered a -> a Source #
number :: Traversable f => f a -> f (Numbered a) Source #
This function numbers the components of the given functorial value with consecutive integers starting at 0.
class (Functor t, Foldable t) => Traversable (t :: Type -> Type) #
Functors representing data structures that can be traversed from left to right.
A definition of traverse
must satisfy the following laws:
- Naturality
t .
for every applicative transformationtraverse
f =traverse
(t . f)t
- Identity
traverse
Identity
=Identity
- Composition
traverse
(Compose
.fmap
g . f) =Compose
.fmap
(traverse
g) .traverse
f
A definition of sequenceA
must satisfy the following laws:
- Naturality
t .
for every applicative transformationsequenceA
=sequenceA
.fmap
tt
- Identity
sequenceA
.fmap
Identity
=Identity
- Composition
sequenceA
.fmap
Compose
=Compose
.fmap
sequenceA
.sequenceA
where an applicative transformation is a function
t :: (Applicative f, Applicative g) => f a -> g a
preserving the Applicative
operations, i.e.
t (pure
x) =pure
x t (f<*>
x) = t f<*>
t x
and the identity functor Identity
and composition functors
Compose
are from Data.Functor.Identity and
Data.Functor.Compose.
A result of the naturality law is a purity law for traverse
traverse
pure
=pure
(The naturality law is implied by parametricity and thus so is the purity law [1, p15].)
Instances are similar to Functor
, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
This is suitable even for abstract types, as the laws for <*>
imply a form of associativity.
The superclass instances should satisfy the following:
- In the
Functor
instance,fmap
should be equivalent to traversal with the identity applicative functor (fmapDefault
). - In the
Foldable
instance,foldMap
should be equivalent to traversal with a constant applicative functor (foldMapDefault
).
References: [1] The Essence of the Iterator Pattern, Jeremy Gibbons and Bruno C. d. S. Oliveira
Instances
class Functor m => Mapping m k | m -> k where Source #
(&) :: m v -> m v -> m v infixr 0 Source #
left-biased union of two mappings.
(|->) :: k -> v -> m v infix 1 Source #
This operator constructs a singleton mapping.
This is the empty mapping.
prodMapWith :: (v1 -> v2 -> v) -> v1 -> v2 -> m v1 -> m v2 -> m v Source #
This function constructs the pointwise product of two maps each with a default value.
findWithDefault :: a -> k -> m a -> a Source #
Returns the value at the given key or returns the given default when the key is not an element of the map.
prodMap :: Mapping m k => v1 -> v2 -> m v1 -> m v2 -> m (v1, v2) Source #
This function constructs the pointwise product of two maps each with a default value.
lookupNumMap :: a -> Int -> NumMap t a -> a Source #
Instances
Functor (NumMap k) Source # | |
Foldable (NumMap k) Source # | |
Defined in Data.Comp.Mapping fold :: Monoid m => NumMap k m -> m # foldMap :: Monoid m => (a -> m) -> NumMap k a -> m # foldMap' :: Monoid m => (a -> m) -> NumMap k a -> m # foldr :: (a -> b -> b) -> b -> NumMap k a -> b # foldr' :: (a -> b -> b) -> b -> NumMap k a -> b # foldl :: (b -> a -> b) -> b -> NumMap k a -> b # foldl' :: (b -> a -> b) -> b -> NumMap k a -> b # foldr1 :: (a -> a -> a) -> NumMap k a -> a # foldl1 :: (a -> a -> a) -> NumMap k a -> a # elem :: Eq a => a -> NumMap k a -> Bool # maximum :: Ord a => NumMap k a -> a # minimum :: Ord a => NumMap k a -> a # | |
Traversable (NumMap k) Source # | |
Mapping (NumMap k) (Numbered k) Source # | |
Defined in Data.Comp.Mapping |