semialign-1: Align and Zip type-classes from the common Semialign ancestor.

Data.Align

Contents

Description

These-based aligning and unaligning of functors with non-uniform shapes.

For a traversals traversal of (bi)foldable (bi)functors through said functors see Data.Crosswalk.

Synopsis

# Documentation

class Functor f => Semialign f where Source #

Functors supporting a zip and align operations that takes the intersection and union of non-uniform shapes.

Minimal definition: either align or alignWith and either zip or zipWith.

## Laws

The laws of align and zip resemble lattice laws. There is a plenty of laws, but they are simply satisfied.

And an addition property if f is Foldable, which tries to enforce align-feel: neither values are duplicated nor lost.

Note: join f x = f x x

Idempotency

join align ≡ fmap (join These)
join zip   ≡ fmap (join (,))


Commutativity

align x y ≡ swap <> align y x zip x y ≡ swap <> zip y x


Associativity

align x (align y z) ≡ assoc <> align (align x y) z zip x (zip y z) ≡ assoc <> zip (zip x y) z


Absorption

fst    <> zip xs (align xs ys) ≡ xs toThis <> align xs (zip xs ys) ≡ This <> xs where toThis (This a) = This a toThis (These a _) = This a toThis (That b) = That b  With alignWith f a b ≡ f <> align a b
zipWith f a b ≡ f <> zip a b  Functoriality align (f <> x) (g <$> y) ≡ bimap f g <$> align x y
zip (f <$> x) (g <$> y) ≡ bimap f g <> zip x y  Zippyness fmap fst (zip x x) ≡ x fmap snd (zip x x) ≡ x zip (fmap fst x) (fmap snd x) ≡ x  Alignedness, if f is Foldable toList x ≡ toListOf (folded . here) (align x y) ≡ mapMaybe justHere (toList (align x y))  Distributivity  align (zip xs ys) zs ≡ undistrThesePair <> zip (align xs zs) (align ys zs)
distrPairThese <> zip (align xs ys) zs ≡ align (zip xs zs) (zip ys zs) zip (align xs ys) zs ≡ undistrPairThese <> align (zip xs zs) (zip ys zs)


Note, the following doesn't hold:

distrThesePair <\$> align (zip xs ys) zs ≢ zip (align xs zs) (align ys zs)


when xs = [] and ys = zs = [0], then the left hand side is "only" [(That 0, That 0)], but the right hand side is [(That 0, These 0 0)].

Minimal complete definition

(align | alignWith), (zip | zipWith)

Methods

align :: f a -> f b -> f (These a b) Source #

Analogous to zip, combines two structures by taking the union of their shapes and using These to hold the elements.

alignWith :: (These a b -> c) -> f a -> f b -> f c Source #

Analogous to zipWith, combines two structures by taking the union of their shapes and combining the elements with the given function.

zip :: f a -> f b -> f (a, b) Source #

Combines to structures by taking the intersection of their shapes and using pair to hold the elements.

zipWith :: (a -> b -> c) -> f a -> f b -> f c Source #

Combines to structures by taking the intersection of their shapes and combining the elements with the given function.

Instances
 Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: [a] -> [b] -> [These a b] Source #alignWith :: (These a b -> c) -> [a] -> [b] -> [c] Source #zip :: [a] -> [b] -> [(a, b)] Source #zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Maybe a -> Maybe b -> Maybe (These a b) Source #alignWith :: (These a b -> c) -> Maybe a -> Maybe b -> Maybe c Source #zip :: Maybe a -> Maybe b -> Maybe (a, b) Source #zipWith :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c Source # Source # zipWith = liftA2 . Instance detailsDefined in Data.Semialign.Internal Methodsalign :: ZipList a -> ZipList b -> ZipList (These a b) Source #alignWith :: (These a b -> c) -> ZipList a -> ZipList b -> ZipList c Source #zip :: ZipList a -> ZipList b -> ZipList (a, b) Source #zipWith :: (a -> b -> c) -> ZipList a -> ZipList b -> ZipList c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Identity a -> Identity b -> Identity (These a b) Source #alignWith :: (These a b -> c) -> Identity a -> Identity b -> Identity c Source #zip :: Identity a -> Identity b -> Identity (a, b) Source #zipWith :: (a -> b -> c) -> Identity a -> Identity b -> Identity c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: NonEmpty a -> NonEmpty b -> NonEmpty (These a b) Source #alignWith :: (These a b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c Source #zip :: NonEmpty a -> NonEmpty b -> NonEmpty (a, b) Source #zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: IntMap a -> IntMap b -> IntMap (These a b) Source #alignWith :: (These a b -> c) -> IntMap a -> IntMap b -> IntMap c Source #zip :: IntMap a -> IntMap b -> IntMap (a, b) Source #zipWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Tree a -> Tree b -> Tree (These a b) Source #alignWith :: (These a b -> c) -> Tree a -> Tree b -> Tree c Source #zip :: Tree a -> Tree b -> Tree (a, b) Source #zipWith :: (a -> b -> c) -> Tree a -> Tree b -> Tree c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Seq a -> Seq b -> Seq (These a b) Source #alignWith :: (These a b -> c) -> Seq a -> Seq b -> Seq c Source #zip :: Seq a -> Seq b -> Seq (a, b) Source #zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Vector a -> Vector b -> Vector (These a b) Source #alignWith :: (These a b -> c) -> Vector a -> Vector b -> Vector c Source #zip :: Vector a -> Vector b -> Vector (a, b) Source #zipWith :: (a -> b -> c) -> Vector a -> Vector b -> Vector c Source # (Eq k, Hashable k) => Semialign (HashMap k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: HashMap k a -> HashMap k b -> HashMap k (These a b) Source #alignWith :: (These a b -> c) -> HashMap k a -> HashMap k b -> HashMap k c Source #zip :: HashMap k a -> HashMap k b -> HashMap k (a, b) Source #zipWith :: (a -> b -> c) -> HashMap k a -> HashMap k b -> HashMap k c Source # Ord k => Semialign (Map k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Map k a -> Map k b -> Map k (These a b) Source #alignWith :: (These a b -> c) -> Map k a -> Map k b -> Map k c Source #zip :: Map k a -> Map k b -> Map k (a, b) Source #zipWith :: (a -> b -> c) -> Map k a -> Map k b -> Map k c Source # Semialign (Proxy :: Type -> Type) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Proxy a -> Proxy b -> Proxy (These a b) Source #alignWith :: (These a b -> c) -> Proxy a -> Proxy b -> Proxy c Source #zip :: Proxy a -> Proxy b -> Proxy (a, b) Source #zipWith :: (a -> b -> c) -> Proxy a -> Proxy b -> Proxy c Source # Monad m => Semialign (Stream m) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Stream m a -> Stream m b -> Stream m (These a b) Source #alignWith :: (These a b -> c) -> Stream m a -> Stream m b -> Stream m c Source #zip :: Stream m a -> Stream m b -> Stream m (a, b) Source #zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Tagged b a -> Tagged b b0 -> Tagged b (These a b0) Source #alignWith :: (These a b0 -> c) -> Tagged b a -> Tagged b b0 -> Tagged b c Source #zip :: Tagged b a -> Tagged b b0 -> Tagged b (a, b0) Source #zipWith :: (a -> b0 -> c) -> Tagged b a -> Tagged b b0 -> Tagged b c Source # Monad m => Semialign (Bundle m v) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Bundle m v a -> Bundle m v b -> Bundle m v (These a b) Source #alignWith :: (These a b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c Source #zip :: Bundle m v a -> Bundle m v b -> Bundle m v (a, b) Source #zipWith :: (a -> b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c Source # Semialign ((->) e :: Type -> Type) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: (e -> a) -> (e -> b) -> e -> These a b Source #alignWith :: (These a b -> c) -> (e -> a) -> (e -> b) -> e -> c Source #zip :: (e -> a) -> (e -> b) -> e -> (a, b) Source #zipWith :: (a -> b -> c) -> (e -> a) -> (e -> b) -> e -> c Source # (Semialign f, Semialign g) => Semialign (Product f g) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Product f g a -> Product f g b -> Product f g (These a b) Source #alignWith :: (These a b -> c) -> Product f g a -> Product f g b -> Product f g c Source #zip :: Product f g a -> Product f g b -> Product f g (a, b) Source #zipWith :: (a -> b -> c) -> Product f g a -> Product f g b -> Product f g c Source # (Semialign f, Semialign g) => Semialign (Compose f g) Source # Instance detailsDefined in Data.Semialign.Internal Methodsalign :: Compose f g a -> Compose f g b -> Compose f g (These a b) Source #alignWith :: (These a b -> c) -> Compose f g a -> Compose f g b -> Compose f g c Source #zip :: Compose f g a -> Compose f g b -> Compose f g (a, b) Source #zipWith :: (a -> b -> c) -> Compose f g a -> Compose f g b -> Compose f g c Source #

class Semialign f => Align f where Source #

A unit of align.

## Laws

(align nil) ≡ fmap This
(nil align) ≡ fmap That


Methods

nil :: f a Source #

An empty structure. aligning with nil will produce a structure with the same shape and elements as the other input, modulo This or That.

Instances
 Align [] Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: [a] Source # Source # Instance detailsDefined in Data.Semialign.Internal Methods Source # Instance detailsDefined in Data.Semialign.Internal Methods Source # Instance detailsDefined in Data.Semialign.Internal Methods Source # Instance detailsDefined in Data.Semialign.Internal Methods Source # Instance detailsDefined in Data.Semialign.Internal Methods (Eq k, Hashable k) => Align (HashMap k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: HashMap k a Source # Ord k => Align (Map k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: Map k a Source # Align (Proxy :: Type -> Type) Source # Instance detailsDefined in Data.Semialign.Internal Methods Monad m => Align (Stream m) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: Stream m a Source # Monad m => Align (Bundle m v) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: Bundle m v a Source # (Align f, Align g) => Align (Product f g) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: Product f g a Source # (Align f, Semialign g) => Align (Compose f g) Source # Instance detailsDefined in Data.Semialign.Internal Methodsnil :: Compose f g a Source #

class Semialign f => Unalign f where Source #

Alignable functors supporting an "inverse" to align: splitting a union shape into its component parts.

## Laws

uncurry align (unalign xs) ≡ xs
unalign (align xs ys) ≡ (xs, ys)


## Compatibility note

In version 1 unalign was changed to return (f a, f b) pair, instead of (f (Just a), f (Just b)). Old behaviour can be achieved with if ever needed.

>>> unzipWith (unalign . Just) [This 'a', That 'b', These 'c' 'd']
([Just 'a',Nothing,Just 'c'],[Nothing,Just 'b',Just 'd'])


Minimal complete definition

Methods

unalign :: f (These a b) -> (f a, f b) Source #

unalignWith :: (c -> These a b) -> f c -> (f a, f b) Source #

Instances
 Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: Maybe (These a b) -> (Maybe a, Maybe b) Source #unalignWith :: (c -> These a b) -> Maybe c -> (Maybe a, Maybe b) Source # Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: IntMap (These a b) -> (IntMap a, IntMap b) Source #unalignWith :: (c -> These a b) -> IntMap c -> (IntMap a, IntMap b) Source # (Eq k, Hashable k) => Unalign (HashMap k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: HashMap k (These a b) -> (HashMap k a, HashMap k b) Source #unalignWith :: (c -> These a b) -> HashMap k c -> (HashMap k a, HashMap k b) Source # Ord k => Unalign (Map k) Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: Map k (These a b) -> (Map k a, Map k b) Source #unalignWith :: (c -> These a b) -> Map k c -> (Map k a, Map k b) Source # Unalign (Proxy :: Type -> Type) Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: Proxy (These a b) -> (Proxy a, Proxy b) Source #unalignWith :: (c -> These a b) -> Proxy c -> (Proxy a, Proxy b) Source # (Unalign f, Unalign g) => Unalign (Product f g) Source # Instance detailsDefined in Data.Semialign.Internal Methodsunalign :: Product f g (These a b) -> (Product f g a, Product f g b) Source #unalignWith :: (c -> These a b) -> Product f g c -> (Product f g a, Product f g b) Source #

# Specialized aligns

malign :: (Semialign f, Monoid a) => f a -> f a -> f a Source #

Align two structures and combine with mappend.

See salign. malign will be deprecated after Semigroup becomes a super class of Monoid

salign :: (Semialign f, Semigroup a) => f a -> f a -> f a Source #

Align two structures and combine with <>.

padZip :: Semialign f => f a -> f b -> f (Maybe a, Maybe b) Source #

Align two structures as in zip, but filling in blanks with Nothing.

padZipWith :: Semialign f => (Maybe a -> Maybe b -> c) -> f a -> f b -> f c Source #

Align two structures as in zipWith, but filling in blanks with Nothing.

lpadZip :: [a] -> [b] -> [(Maybe a, b)] Source #

Left-padded zip.

lpadZipWith :: (Maybe a -> b -> c) -> [a] -> [b] -> [c] Source #

Left-padded zipWith.

rpadZip :: [a] -> [b] -> [(a, Maybe b)] Source #

Right-padded zip.

rpadZipWith :: (a -> Maybe b -> c) -> [a] -> [b] -> [c] Source #

Right-padded zipWith.

alignVectorWith :: (Vector v a, Vector v b, Vector v c) => (These a b -> c) -> v a -> v b -> v c Source #