You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{-# LANGUAGE QuantifiedConstraints #-}
class (Bifunctorp, foralla.Applicative (pa)) =>Bialternativepwhere
{-# MINIMAL left, ((<<|>>) | liftL2) #-}
left::a->pab(<<|>>)::p (a->b) c->pac->pbc(<<|>>)= liftL2 idliftL2:: (a->b->c) ->pad->pbd->pcd
liftL2 f a b = f `first` a <<|>> b
(|>>)::pac->pbc->pbc
a |>> b = liftL2 (constid) a b
(<<|)::pac->pbc->pac
a <<| b = liftL2 const a b
For example, here's the Bialternative instance of Either.
instanceBialternativeEitherwhereleft::a->Eitherab
left =Left(<<|>>)::Either (a->b) c->Eitherac->EitherbcLeft f <<|>>Left a =Left (f a)
Right c <<|>> _ =Right c
_ <<|>>Right c =Right c
Instances of Bialternative should satisfy the following laws.
Identity
left id <<|>> v = v
Composition
left (.) <<|>> u <<|>> v <<|>> w = u <<|>> (v <<|>> w)
Homomorphism
left f <<|>> left x = left (f x)
Interchange
u <<|>> left y = left ($ y) <<|>> u
Left Catch
pure x <<|>> v = pure x
Right Catch
left x <*> v = left x
The text was updated successfully, but these errors were encountered:
Just like Biapplicative, we can define a generic function to traverse a Traversable container in a Bialternative.
traverseLeft:: (Traversablet, Bialternativep) => (a->pbc) ->ta->p (tb) c
traverseLeft f = go .traverse (One. f)
wherego::Bialternativep=>Mag (pab) ax->pxb
go (Pure t) = left t
go (Map f xs) = first f (go xs)
go (Ap fs xs) = go fs <<|>> go xs
#if MIN_VERSION_base(4,10,0)
go (LiftA2 f xs ys) = liftL2 f (go xs) (go ys)
#endif
go (One p) = p
This uses the same data type Mag that's defined in Data.Biapplicative.
Bialternative
is for bifunctors likeEither
.For example, here's the
Bialternative
instance ofEither
.Instances of
Bialternative
should satisfy the following laws.The text was updated successfully, but these errors were encountered: