Move Bifunctor instances

parent c7015bb1
Pipeline #8612 passed with stage
in 1 minute and 32 seconds
definition module Data.Bifunctor
from Data.Either import :: Either
class Bifunctor p where
bifmap :: (a -> b) (c -> d) (p a c) -> p b d
first :: (a -> b) (p a c) -> p b c
second :: (b -> c) (p a b) -> p a c
instance Bifunctor (,)
instance Bifunctor ((,,) x)
instance Bifunctor ((,,,) x y)
instance Bifunctor ((,,,,) x y z)
instance Bifunctor Either
class Bifunctor p
where
bifmap :: (a -> b) (c -> d) (p a c) -> p b d
first :: (a -> b) (p a c) -> p b c
second :: (b -> c) (p a b) -> p a c
implementation module Data.Bifunctor
from StdFunc import o, id
import Data.Either
instance Bifunctor (,) where
bifmap f g t = let (a, b) = t in (f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,) x) where
bifmap f g t = let (x, a, b) = t in (x, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,,) x y) where
bifmap f g t = let (x, y, a, b) = t in (x, y, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,,,) x y z) where
bifmap f g t = let (x, y, z, a, b) = t in (x, y, z, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor Either where
bifmap f _ (Left a) = Left (f a)
bifmap _ g (Right b) = Right (g b)
first f d = bifmap f id d
second g d = bifmap id g d
......@@ -10,6 +10,7 @@ from Control.Monad import class Monad
from Data.Functor import class Functor
from Data.Foldable import class Foldable
from Data.Traversable import class Traversable
from Data.Bifunctor import class Bifunctor
:: Either a b = Left a | Right b
......@@ -27,4 +28,6 @@ instance Monad (Either e)
instance Foldable (Either a)
instance Traversable (Either a)
instance Bifunctor Either
either :: (.a -> .c) (.b -> .c) !(Either .a .b) -> .c
......@@ -10,6 +10,7 @@ import Data.Monoid
from Data.Foldable import class Foldable(foldMap,foldl,foldr)
from Data.Traversable import class Traversable(traverse)
import qualified Data.Traversable as T
import Data.Bifunctor
instance Functor (Either a) where
fmap f (Left l) = Left l
......@@ -66,6 +67,13 @@ where
mapM f x = unwrapMonad (traverse (WrapMonad o f) x)
sequence x = 'T'.mapM id x
instance Bifunctor Either
where
bifmap f _ (Left a) = Left (f a)
bifmap _ g (Right b) = Right (g b)
first f d = bifmap f id d
second g d = bifmap id g d
either :: (.a -> .c) (.b -> .c) !(Either .a .b) -> .c
either f _ (Left x) = f x
either _ g (Right y) = g y
......@@ -4,6 +4,7 @@ from Data.Functor import class Functor
from Data.Monoid import class Semigroup, class Monoid
from Data.Foldable import class Foldable
from Data.Traversable import class Traversable
from Data.Bifunctor import class Bifunctor
tuple :: a b -> (a,b)
tuple3 :: a b c -> (a,b,c)
......@@ -31,3 +32,8 @@ instance Monoid (a, b, c, d, e) | Monoid a & Monoid b & Monoid c & Monoid d & Mo
instance Foldable ((,) a)
instance Traversable ((,) a)
instance Bifunctor (,)
instance Bifunctor ((,,) x)
instance Bifunctor ((,,,) x y)
instance Bifunctor ((,,,,) x y z)
......@@ -2,6 +2,7 @@ implementation module Data.Tuple
from StdFunc import id, o
from StdMisc import abort
import Data.Bifunctor
import Data.Functor
import Data.Maybe
import Data.Monoid
......@@ -95,3 +96,27 @@ where
sequenceA f = traverse id f
mapM f x = unwrapMonad (traverse (WrapMonad o f) x)
sequence x = mapM id x
instance Bifunctor (,)
where
bifmap f g t = let (a, b) = t in (f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,) x)
where
bifmap f g t = let (x, a, b) = t in (x, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,,) x y)
where
bifmap f g t = let (x, y, a, b) = t in (x, y, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
instance Bifunctor ((,,,,) x y z)
where
bifmap f g t = let (x, y, z, a, b) = t in (x, y, z, f a, g b)
first f d = bifmap f id d
second g d = bifmap id g d
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment