diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b483b6..15359d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: - Added support for PureScript 0.14 and dropped support for all previous versions (#16) + - Ported `Clown`, `Joker`, and `Product` to `purescript-functors` (#21) New features: diff --git a/src/Data/Bifunctor.purs b/src/Data/Bifunctor.purs index 331f238..245cd3e 100644 --- a/src/Data/Bifunctor.purs +++ b/src/Data/Bifunctor.purs @@ -1,5 +1,11 @@ module Data.Bifunctor where +import Data.Functor (class Functor, map) +import Data.Functor.Clown (Clown(..)) +import Data.Functor.Joker (Joker(..)) +import Data.Functor.Product2 (Product2(..)) +import Control.Biapplicative (class Biapplicative, bipure) +import Control.Biapply (class Biapply, biapply) import Control.Category (identity) -- | A `Bifunctor` is a `Functor` from the pair category `(Type, Type)` to `Type`. @@ -18,6 +24,15 @@ import Control.Category (identity) class Bifunctor f where bimap :: forall a b c d. (a -> b) -> (c -> d) -> f a c -> f b d +instance bifunctorClown :: Functor f => Bifunctor (Clown f) where + bimap f _ (Clown a) = Clown (map f a) + +instance bifunctorJoker :: Functor g => Bifunctor (Joker g) where + bimap _ g (Joker a) = Joker (map g a) + +instance bifunctorProduct2 :: (Bifunctor f, Bifunctor g) => Bifunctor (Product2 f g) where + bimap f g (Product2 x y) = Product2 (bimap f g x) (bimap f g y) + -- | Map a function over the first type argument of a `Bifunctor`. lmap :: forall f a b c. Bifunctor f => (a -> b) -> f a c -> f b c lmap f = bimap f identity diff --git a/src/Data/Bifunctor/Clown.purs b/src/Data/Bifunctor/Clown.purs deleted file mode 100644 index 7a57f59..0000000 --- a/src/Data/Bifunctor/Clown.purs +++ /dev/null @@ -1,34 +0,0 @@ -module Data.Bifunctor.Clown where - -import Prelude - -import Control.Biapplicative (class Biapplicative) -import Control.Biapply (class Biapply) - -import Data.Bifunctor (class Bifunctor) -import Data.Newtype (class Newtype) - --- | Make a `Functor` over the first argument of a `Bifunctor` -newtype Clown :: forall k1 k2. (k1 -> Type) -> k1 -> k2 -> Type -newtype Clown f a b = Clown (f a) - -derive instance newtypeClown :: Newtype (Clown f a b) _ - -derive newtype instance eqClown :: Eq (f a) => Eq (Clown f a b) - -derive newtype instance ordClown :: Ord (f a) => Ord (Clown f a b) - -instance showClown :: Show (f a) => Show (Clown f a b) where - show (Clown x) = "(Clown " <> show x <> ")" - -instance functorClown :: Functor (Clown f a) where - map _ (Clown a) = Clown a - -instance bifunctorClown :: Functor f => Bifunctor (Clown f) where - bimap f _ (Clown a) = Clown (map f a) - -instance biapplyClown :: Apply f => Biapply (Clown f) where - biapply (Clown fg) (Clown xy) = Clown (fg <*> xy) - -instance biapplicativeClown :: Applicative f => Biapplicative (Clown f) where - bipure a _ = Clown (pure a) diff --git a/src/Data/Bifunctor/Joker.purs b/src/Data/Bifunctor/Joker.purs deleted file mode 100644 index a09e522..0000000 --- a/src/Data/Bifunctor/Joker.purs +++ /dev/null @@ -1,34 +0,0 @@ -module Data.Bifunctor.Joker where - -import Prelude - -import Control.Biapplicative (class Biapplicative) -import Control.Biapply (class Biapply) - -import Data.Bifunctor (class Bifunctor) -import Data.Newtype (class Newtype) - --- | Make a `Functor` over the second argument of a `Bifunctor` -newtype Joker :: forall k1 k2. (k2 -> Type) -> k1 -> k2 -> Type -newtype Joker g a b = Joker (g b) - -derive instance newtypeJoker :: Newtype (Joker g a b) _ - -derive newtype instance eqJoker :: Eq (g b) => Eq (Joker g a b) - -derive newtype instance ordJoker :: Ord (g b) => Ord (Joker g a b) - -instance showJoker :: Show (g b) => Show (Joker g a b) where - show (Joker x) = "(Joker " <> show x <> ")" - -instance functorJoker :: Functor g => Functor (Joker g a) where - map g (Joker a) = Joker (map g a) - -instance bifunctorJoker :: Functor g => Bifunctor (Joker g) where - bimap _ g (Joker a) = Joker (map g a) - -instance biapplyJoker :: Apply g => Biapply (Joker g) where - biapply (Joker fg) (Joker xy) = Joker (fg <*> xy) - -instance biapplicativeJoker :: Applicative g => Biapplicative (Joker g) where - bipure _ b = Joker (pure b) diff --git a/src/Data/Bifunctor/Product.purs b/src/Data/Bifunctor/Product.purs deleted file mode 100644 index d8b59d6..0000000 --- a/src/Data/Bifunctor/Product.purs +++ /dev/null @@ -1,28 +0,0 @@ -module Data.Bifunctor.Product where - -import Prelude - -import Control.Biapplicative (class Biapplicative, bipure) -import Control.Biapply (class Biapply, biapply) - -import Data.Bifunctor (class Bifunctor, bimap) - --- | The product of two `Bifunctor`s. -data Product :: forall k1 k2. (k1 -> k2 -> Type) -> (k1 -> k2 -> Type) -> k1 -> k2 -> Type -data Product f g a b = Product (f a b) (g a b) - -derive instance eqProduct :: (Eq (f a b), Eq (g a b)) => Eq (Product f g a b) - -derive instance ordProduct :: (Ord (f a b), Ord (g a b)) => Ord (Product f g a b) - -instance showProduct :: (Show (f a b), Show (g a b)) => Show (Product f g a b) where - show (Product x y) = "(Product " <> show x <> " " <> show y <> ")" - -instance bifunctorProduct :: (Bifunctor f, Bifunctor g) => Bifunctor (Product f g) where - bimap f g (Product x y) = Product (bimap f g x) (bimap f g y) - -instance biapplyProduct :: (Biapply f, Biapply g) => Biapply (Product f g) where - biapply (Product w x) (Product y z) = Product (biapply w y) (biapply x z) - -instance biapplicativeProduct :: (Biapplicative f, Biapplicative g) => Biapplicative (Product f g) where - bipure a b = Product (bipure a b) (bipure a b)