backport bifunctor
This commit is contained in:
@@ -43,6 +43,7 @@ library
|
|||||||
Show
|
Show
|
||||||
Unsafe
|
Unsafe
|
||||||
Functor
|
Functor
|
||||||
|
Bifunctor
|
||||||
|
|
||||||
default-extensions:
|
default-extensions:
|
||||||
NoImplicitPrelude
|
NoImplicitPrelude
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module Applicative (
|
module Applicative (
|
||||||
orAlt,
|
orAlt,
|
||||||
orEmpty,
|
orEmpty,
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
|
module Bifunctor (
|
||||||
|
Bifunctor(..)
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.Function (id, (.))
|
||||||
|
import Data.Either (Either(..))
|
||||||
|
import Control.Applicative ( Const(..) )
|
||||||
|
|
||||||
|
class Bifunctor p where
|
||||||
|
{-# MINIMAL bimap | first, second #-}
|
||||||
|
|
||||||
|
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
|
||||||
|
bimap f g = first f . second g
|
||||||
|
|
||||||
|
first :: (a -> b) -> p a c -> p b c
|
||||||
|
first f = bimap f id
|
||||||
|
|
||||||
|
second :: (b -> c) -> p a b -> p a c
|
||||||
|
second = bimap id
|
||||||
|
|
||||||
|
instance Bifunctor (,) where
|
||||||
|
bimap f g ~(a, b) = (f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor ((,,) x1) where
|
||||||
|
bimap f g ~(x1, a, b) = (x1, f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor ((,,,) x1 x2) where
|
||||||
|
bimap f g ~(x1, x2, a, b) = (x1, x2, f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor ((,,,,) x1 x2 x3) where
|
||||||
|
bimap f g ~(x1, x2, x3, a, b) = (x1, x2, x3, f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor ((,,,,,) x1 x2 x3 x4) where
|
||||||
|
bimap f g ~(x1, x2, x3, x4, a, b) = (x1, x2, x3, x4, f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor ((,,,,,,) x1 x2 x3 x4 x5) where
|
||||||
|
bimap f g ~(x1, x2, x3, x4, x5, a, b) = (x1, x2, x3, x4, x5, f a, g b)
|
||||||
|
|
||||||
|
instance Bifunctor Either where
|
||||||
|
bimap f _ (Left a) = Left (f a)
|
||||||
|
bimap _ g (Right b) = Right (g b)
|
||||||
|
|
||||||
|
instance Bifunctor Const where
|
||||||
|
bimap f _ (Const a) = Const (f a)
|
||||||
+6
-2
@@ -1,3 +1,6 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module Bool (
|
module Bool (
|
||||||
whenM
|
whenM
|
||||||
, unlessM
|
, unlessM
|
||||||
@@ -6,8 +9,9 @@ module Bool (
|
|||||||
, bool
|
, bool
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude
|
import Data.Bool (Bool)
|
||||||
import Control.Monad (MonadPlus, when, unless, guard)
|
import Data.Function (flip)
|
||||||
|
import Control.Monad (Monad, MonadPlus, when, unless, guard, (>>=), (=<<))
|
||||||
|
|
||||||
bool :: a -> a -> Bool -> a
|
bool :: a -> a -> Bool -> a
|
||||||
bool f t p = if p then t else f
|
bool f t p = if p then t else f
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE Unsafe #-}
|
||||||
{-# LANGUAGE NoImplicitPrelude #-}
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module Debug (
|
module Debug (
|
||||||
|
|||||||
+9
-1
@@ -1,3 +1,6 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module Either (
|
module Either (
|
||||||
maybeToLeft
|
maybeToLeft
|
||||||
, maybeToRight
|
, maybeToRight
|
||||||
@@ -6,6 +9,11 @@ module Either (
|
|||||||
, maybeToEither
|
, maybeToEither
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Data.Function (const)
|
||||||
|
import Data.Monoid (Monoid, mempty)
|
||||||
|
import Data.Maybe (Maybe(..), maybe)
|
||||||
|
import Data.Either (Either(..), either)
|
||||||
|
|
||||||
leftToMaybe :: Either l r -> Maybe l
|
leftToMaybe :: Either l r -> Maybe l
|
||||||
leftToMaybe = either Just (const Nothing)
|
leftToMaybe = either Just (const Nothing)
|
||||||
|
|
||||||
@@ -18,5 +26,5 @@ maybeToRight l = maybe (Left l) Right
|
|||||||
maybeToLeft :: r -> Maybe l -> Either l r
|
maybeToLeft :: r -> Maybe l -> Either l r
|
||||||
maybeToLeft r = maybe (Right r) Left
|
maybeToLeft r = maybe (Right r) Left
|
||||||
|
|
||||||
maybeToEither :: Monoid b => Maybe a -> Either b a
|
maybeToEither :: Monoid b => (a -> b) -> Maybe a -> b
|
||||||
maybeToEither = maybe mempty
|
maybeToEither = maybe mempty
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module List (
|
module List (
|
||||||
head,
|
head,
|
||||||
ordNub,
|
ordNub,
|
||||||
|
|||||||
+3
-1
@@ -1,4 +1,5 @@
|
|||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
|
{-# LANGUAGE Safe #-}
|
||||||
{-# LANGUAGE NoImplicitPrelude #-}
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
|
||||||
module Monad (
|
module Monad (
|
||||||
@@ -38,7 +39,8 @@ module Monad (
|
|||||||
, (<$!>)
|
, (<$!>)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude (concat, seq)
|
import Data.List (concat)
|
||||||
|
import Prelude (seq)
|
||||||
|
|
||||||
#if (__GLASGOW_HASKELL__ >= 710)
|
#if (__GLASGOW_HASKELL__ >= 710)
|
||||||
import Control.Monad hiding ((<$!>))
|
import Control.Monad hiding ((<$!>))
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE CPP #-}
|
||||||
{-# LANGUAGE NoImplicitPrelude #-}
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ import qualified Bool as X
|
|||||||
import qualified Debug as X
|
import qualified Debug as X
|
||||||
import qualified Monad as X
|
import qualified Monad as X
|
||||||
import qualified Functor as X
|
import qualified Functor as X
|
||||||
|
import qualified Either as X
|
||||||
import qualified Applicative as X
|
import qualified Applicative as X
|
||||||
|
|
||||||
-- Maybe'ized version of partial functions
|
-- Maybe'ized version of partial functions
|
||||||
@@ -57,6 +59,12 @@ import Data.Foldable as X hiding (
|
|||||||
import Data.Semiring as X
|
import Data.Semiring as X
|
||||||
import Data.Functor.Identity as X
|
import Data.Functor.Identity as X
|
||||||
|
|
||||||
|
#if (__GLASGOW_HASKELL__ >= 710)
|
||||||
|
import Data.Bifunctor as X (Bifunctor(..))
|
||||||
|
#else
|
||||||
|
import Bifunctor as X (Bifunctor(..))
|
||||||
|
#endif
|
||||||
|
|
||||||
-- Deepseq
|
-- Deepseq
|
||||||
import Control.DeepSeq as X (
|
import Control.DeepSeq as X (
|
||||||
NFData(..)
|
NFData(..)
|
||||||
@@ -67,6 +75,7 @@ import Control.DeepSeq as X (
|
|||||||
|
|
||||||
-- Data structures
|
-- Data structures
|
||||||
import Data.Tuple as X
|
import Data.Tuple as X
|
||||||
|
import Data.Semiring as X
|
||||||
import Data.List as X (
|
import Data.List as X (
|
||||||
splitAt
|
splitAt
|
||||||
, break
|
, break
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
{-# LANGUAGE Safe #-}
|
||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
{-# LANGUAGE TypeSynonymInstances #-}
|
{-# LANGUAGE TypeSynonymInstances #-}
|
||||||
|
|
||||||
module Show (
|
module Show (
|
||||||
|
|||||||
Reference in New Issue
Block a user