Skip to content

Commit

Permalink
moved Biapply from semigroupoids to reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed May 9, 2011
1 parent 849225f commit 340d4ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions Data/Bifoldable.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Data.Bifoldable
, bitraverse_ , bitraverse_
, bifor_ , bifor_
, bimapM_ , bimapM_
, biforM_
, bisequenceA_ , bisequenceA_
, bisequence_ , bisequence_
, biList , biList
Expand Down
62 changes: 62 additions & 0 deletions Data/Bifunctor/Apply.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,62 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.Bifunctor.Apply
-- Copyright : (C) 2011 Edward Kmett,
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : provisional
-- Portability : portable
--
----------------------------------------------------------------------------
module Data.Bifunctor.Apply (
-- * Functors
-- * Applyable bifunctors
Biapply(..)
, (<<$>>)
, (<<..>>)
, bilift2
, bilift3
, module Data.Bifunctor
) where

-- import _everything_
import Data.Bifunctor

infixl 4 <<$>>, <<.>>, <<., .>>, <<..>>

(<<$>>) :: (a -> b) -> a -> b
(<<$>>) = id

-- | A strong lax semi-monoidal endofunctor.
-- This is equivalent to an 'Applicative' without 'pure'.
--
-- Laws:
--
-- > associative composition: (.) <$> u <.> v <.> w = u <.> (v <.> w)
class Bifunctor p => Biapply p where
(<<.>>) :: p (a -> b) (c -> d) -> p a c -> p b d

-- | a .> b = const id <$> a <.> b
(.>>) :: p a b -> p c d -> p c d
a .>> b = bimap (const id) (const id) <<$>> a <<.>> b

-- | a <. b = const <$> a <.> b
(<<.) :: p a b -> p c d -> p a b
a <<. b = bimap const const <<$>> a <<.>> b

(<<..>>) :: Biapply p => p a c -> p (a -> b) (c -> d) -> p b d
(<<..>>) = bilift2 (flip id) (flip id)

-- | Lift a binary function into a comonad with zipping
bilift2 :: Biapply w => (a -> b -> c) -> (d -> e -> f) -> w a d -> w b e -> w c f
bilift2 f g a b = bimap f g <<$>> a <<.>> b
{-# INLINE bilift2 #-}

-- | Lift a ternary function into a comonad with zipping
bilift3 :: Biapply w => (a -> b -> c -> d) -> (e -> f -> g -> h) -> w a e -> w b f -> w c g -> w d h
bilift3 f g a b c = bimap f g <<$>> a <<.>> b <<.>> c
{-# INLINE bilift3 #-}

instance Biapply (,) where
(f, g) <<.>> (a, b) = (f a, g b)
3 changes: 2 additions & 1 deletion bifunctors.cabal
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bifunctors name: bifunctors
category: Data, Functors category: Data, Functors
version: 0.1 version: 0.1.1
license: BSD3 license: BSD3
cabal-version: >= 1.6 cabal-version: >= 1.6
license-file: LICENSE license-file: LICENSE
Expand All @@ -23,6 +23,7 @@ library


exposed-modules: exposed-modules:
Data.Bifunctor Data.Bifunctor
Data.Bifunctor.Apply
Data.Bifoldable Data.Bifoldable
Data.Bitraversable Data.Bitraversable


Expand Down

0 comments on commit 340d4ba

Please sign in to comment.