diff --git a/protolude.cabal b/protolude.cabal index 7c0a233da..18d006df8 100644 --- a/protolude.cabal +++ b/protolude.cabal @@ -43,6 +43,7 @@ library Show Unsafe Functor + Bifunctor default-extensions: NoImplicitPrelude diff --git a/src/Applicative.hs b/src/Applicative.hs index 596437654..e58d8eac0 100644 --- a/src/Applicative.hs +++ b/src/Applicative.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE Safe #-} +{-# LANGUAGE NoImplicitPrelude #-} + module Applicative ( orAlt, orEmpty, diff --git a/src/Bifunctor.hs b/src/Bifunctor.hs new file mode 100644 index 000000000..62c03de6a --- /dev/null +++ b/src/Bifunctor.hs @@ -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) diff --git a/src/Bool.hs b/src/Bool.hs index 797f96c88..3b0a3a419 100644 --- a/src/Bool.hs +++ b/src/Bool.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE Safe #-} +{-# LANGUAGE NoImplicitPrelude #-} + module Bool ( whenM , unlessM @@ -6,8 +9,9 @@ module Bool ( , bool ) where -import Prelude -import Control.Monad (MonadPlus, when, unless, guard) +import Data.Bool (Bool) +import Data.Function (flip) +import Control.Monad (Monad, MonadPlus, when, unless, guard, (>>=), (=<<)) bool :: a -> a -> Bool -> a bool f t p = if p then t else f diff --git a/src/Debug.hs b/src/Debug.hs index 7880373dc..5fd3dcd63 100644 --- a/src/Debug.hs +++ b/src/Debug.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE Unsafe #-} {-# LANGUAGE NoImplicitPrelude #-} module Debug ( diff --git a/src/Either.hs b/src/Either.hs index 416c372d2..5a18c4f5a 100644 --- a/src/Either.hs +++ b/src/Either.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE Safe #-} +{-# LANGUAGE NoImplicitPrelude #-} + module Either ( maybeToLeft , maybeToRight @@ -6,6 +9,11 @@ module Either ( , maybeToEither ) 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 Just (const Nothing) @@ -18,5 +26,5 @@ maybeToRight l = maybe (Left l) Right maybeToLeft :: r -> Maybe l -> Either l r 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 diff --git a/src/List.hs b/src/List.hs index d3555ae34..efde12ce5 100644 --- a/src/List.hs +++ b/src/List.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE Safe #-} +{-# LANGUAGE NoImplicitPrelude #-} + module List ( head, ordNub, diff --git a/src/Monad.hs b/src/Monad.hs index cc31e98cb..106bf493d 100644 --- a/src/Monad.hs +++ b/src/Monad.hs @@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE Safe #-} {-# LANGUAGE NoImplicitPrelude #-} module Monad ( @@ -38,7 +39,8 @@ module Monad ( , (<$!>) ) where -import Prelude (concat, seq) +import Data.List (concat) +import Prelude (seq) #if (__GLASGOW_HASKELL__ >= 710) import Control.Monad hiding ((<$!>)) diff --git a/src/Protolude.hs b/src/Protolude.hs index 98f4d390b..b14542211 100644 --- a/src/Protolude.hs +++ b/src/Protolude.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE NoImplicitPrelude #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} @@ -23,6 +24,7 @@ import qualified Bool as X import qualified Debug as X import qualified Monad as X import qualified Functor as X +import qualified Either as X import qualified Applicative as X -- Maybe'ized version of partial functions @@ -57,6 +59,12 @@ import Data.Foldable as X hiding ( import Data.Semiring 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 import Control.DeepSeq as X ( NFData(..) @@ -67,6 +75,7 @@ import Control.DeepSeq as X ( -- Data structures import Data.Tuple as X +import Data.Semiring as X import Data.List as X ( splitAt , break diff --git a/src/Show.hs b/src/Show.hs index 1c3d26716..321aadb5a 100644 --- a/src/Show.hs +++ b/src/Show.hs @@ -1,5 +1,7 @@ +{-# LANGUAGE Safe #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE TypeSynonymInstances #-} module Show (