matchable-0.1.2.1: A type class for Matchable Functors.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Bimatchable

Synopsis

Documentation

class (Eq2 t, Bifunctor t) => Bimatchable t where Source #

Containers that allows exact structural matching of two containers.

Bimatchable is Bifunctor-version of Matchable. It can compare and zip containers with two parameters.

Minimal complete definition

bizipMatchWith

Methods

bizipMatch :: t a b -> t a' b' -> Maybe (t (a, a') (b, b')) Source #

bizipMatch is to zipMatch what bimap is to fmap.

Decides if two structures match exactly. If they match, return zipped version of them.

Law

Forall x :: t a b, y :: t a' b', z :: t (a,a') (b,b'),

bizipMatch x y = Just z

holds if and only if both of

x = bimap fst fst z
y = bimap snd snd z

holds. Otherwise, bizipMatch x y = Nothing.

Example

>>> bizipMatch (Left 1) (Left 'a')
Just (Left (1,'a'))
>>> bizipMatch (Right 1) (Right False)
Just (Right (1,False))
>>> bizipMatch (Left 1) (Right False)
Nothing

bizipMatchWith :: (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> t a b -> t a' b' -> Maybe (t a'' b'') Source #

bizipMatchWith is to zipMatchWith what bimap is to fmap.

Match two structures. If they match, zip them with given functions (a -> a' -> Maybe a'') and (b -> b -> Maybe b''). Passed functions can make whole match failby returning Nothing.

Law

For any

x :: t a b
y :: t a' b'
f :: a -> a' -> Maybe a''
g :: b -> b' -> Maybe b''

bizipMatchWith must satisfy the following.

  • If there is a pair (z :: t (a,a') (b,b'), w :: t a'' b'') such that fulfills all of the following three conditions, then bizipMatchWith f g x y = Just w.

    1. x = bimap fst fst z
    2. y = bimap snd snd z
    3. bimap (uncurry f) (uncurry g) z = bimap Just Just w
  • If there are no such pair, bizipMatchWith f g x y = Nothing.

Instances

Instances details
Bimatchable Either Source # 
Instance details

Defined in Data.Bimatchable

Methods

bizipMatch :: Either a b -> Either a' b' -> Maybe (Either (a, a') (b, b')) Source #

bizipMatchWith :: (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> Either a b -> Either a' b' -> Maybe (Either a'' b'') Source #

Bimatchable (,) Source # 
Instance details

Defined in Data.Bimatchable

Methods

bizipMatch :: (a, b) -> (a', b') -> Maybe ((a, a'), (b, b')) Source #

bizipMatchWith :: (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> (a, b) -> (a', b') -> Maybe (a'', b'') Source #

Bimatchable (Const :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Bimatchable

Methods

bizipMatch :: Const a b -> Const a' b' -> Maybe (Const (a, a') (b, b')) Source #

bizipMatchWith :: (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> Const a b -> Const a' b' -> Maybe (Const a'' b'') Source #

Bimatchable (Tagged :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Bimatchable

Methods

bizipMatch :: Tagged a b -> Tagged a' b' -> Maybe (Tagged (a, a') (b, b')) Source #

bizipMatchWith :: (a -> a' -> Maybe a'') -> (b -> b' -> Maybe b'') -> Tagged a b -> Tagged a' b' -> Maybe (Tagged a'' b'') Source #

bimapRecovered :: Bimatchable t => (a -> a') -> (b -> b') -> t a b -> t a' b' Source #

eq2Default :: (Bimatchable t, Eq a, Eq b) => t a b -> t a b -> Bool Source #

liftEq2Default :: Bimatchable t => (a -> a' -> Bool) -> (b -> b' -> Bool) -> t a b -> t a' b' -> Bool Source #