diff options
author | Herbert Valerio Riedel <hvr@gnu.org> | 2014-09-16 19:19:25 +0200 |
---|---|---|
committer | Herbert Valerio Riedel <hvr@gnu.org> | 2014-09-16 22:43:17 +0200 |
commit | b47521991a7574f4f3554f7c5444a8c60cfe9efd (patch) | |
tree | 55bfb1876f31b841ae9d750aa87489ed6d2dc667 /libraries | |
parent | e7a0f5b66ced8d56d770375e4d35d38c70067559 (diff) | |
download | haskell-b47521991a7574f4f3554f7c5444a8c60cfe9efd.tar.gz |
Move `Maybe`-typedef into GHC.Base
This is preparatory work for reintroducing SPECIALISEs that were lost
in d94de87252d0fe2ae97341d186b03a2fbe136b04
Differential Revision: https://phabricator.haskell.org/D214
Diffstat (limited to 'libraries')
31 files changed, 57 insertions, 78 deletions
diff --git a/libraries/base/Control/Exception.hs b/libraries/base/Control/Exception.hs index 7c019eb5ca..252597d70e 100644 --- a/libraries/base/Control/Exception.hs +++ b/libraries/base/Control/Exception.hs @@ -133,7 +133,6 @@ import Control.Exception.Base import GHC.Base import GHC.IO (unsafeUnmask) -import Data.Maybe -- | You need this when using 'catches'. data Handler a = forall e . Exception e => Handler (e -> IO a) diff --git a/libraries/base/Control/Exception/Base.hs b/libraries/base/Control/Exception/Base.hs index 8df4958cbb..afb715193b 100644 --- a/libraries/base/Control/Exception/Base.hs +++ b/libraries/base/Control/Exception/Base.hs @@ -105,7 +105,6 @@ import GHC.Conc.Sync import Data.Dynamic import Data.Either -import Data.Maybe ----------------------------------------------------------------------------- -- Catching exceptions diff --git a/libraries/base/Data/Maybe.hs b/libraries/base/Data/Maybe.hs index de8eadcd6e..5923ae1061 100644 --- a/libraries/base/Data/Maybe.hs +++ b/libraries/base/Data/Maybe.hs @@ -34,52 +34,6 @@ module Data.Maybe import GHC.Base -- --------------------------------------------------------------------------- --- The Maybe type, and instances - --- | The 'Maybe' type encapsulates an optional value. A value of type --- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@), --- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to --- deal with errors or exceptional cases without resorting to drastic --- measures such as 'error'. --- --- The 'Maybe' type is also a monad. It is a simple kind of error --- monad, where all errors are represented by 'Nothing'. A richer --- error monad can be built using the 'Data.Either.Either' type. - -data Maybe a = Nothing | Just a - deriving (Eq, Ord) - --- | Lift a semigroup into 'Maybe' forming a 'Monoid' according to --- <http://en.wikipedia.org/wiki/Monoid>: \"Any semigroup @S@ may be --- turned into a monoid simply by adjoining an element @e@ not in @S@ --- and defining @e*e = e@ and @e*s = s = s*e@ for all @s ∈ S@.\" Since --- there is no \"Semigroup\" typeclass providing just 'mappend', we --- use 'Monoid' instead. -instance Monoid a => Monoid (Maybe a) where - mempty = Nothing - Nothing `mappend` m = m - m `mappend` Nothing = m - Just m1 `mappend` Just m2 = Just (m1 `mappend` m2) - -instance Functor Maybe where - fmap _ Nothing = Nothing - fmap f (Just a) = Just (f a) - -instance Applicative Maybe where - pure = return - (<*>) = ap - -instance Monad Maybe where - (Just x) >>= k = k x - Nothing >>= _ = Nothing - - (Just _) >> k = k - Nothing >> _ = Nothing - - return = Just - fail _ = Nothing - --- --------------------------------------------------------------------------- -- Functions over Maybe -- | The 'maybe' function takes a default value, a function, and a 'Maybe' diff --git a/libraries/base/Data/Monoid.hs b/libraries/base/Data/Monoid.hs index 6b393b173e..8b8c8e80b7 100644 --- a/libraries/base/Data/Monoid.hs +++ b/libraries/base/Data/Monoid.hs @@ -46,7 +46,6 @@ import GHC.Num import GHC.Read import GHC.Show import GHC.Generics -import Data.Maybe {- -- just for testing diff --git a/libraries/base/Data/OldTypeable/Internal.hs b/libraries/base/Data/OldTypeable/Internal.hs index 2b02930466..d5e39c5688 100644 --- a/libraries/base/Data/OldTypeable/Internal.hs +++ b/libraries/base/Data/OldTypeable/Internal.hs @@ -57,7 +57,6 @@ module Data.OldTypeable.Internal {-# DEPRECATED "Use Data.Typeable.Internal inst import GHC.Base import GHC.Word import GHC.Show -import Data.Maybe import Data.List import GHC.Num import GHC.Real diff --git a/libraries/base/Data/Typeable/Internal.hs b/libraries/base/Data/Typeable/Internal.hs index b67f88c446..1a901792c1 100644 --- a/libraries/base/Data/Typeable/Internal.hs +++ b/libraries/base/Data/Typeable/Internal.hs @@ -54,7 +54,6 @@ import GHC.Base import GHC.Word import GHC.Show import GHC.Read ( Read ) -import Data.Maybe import Data.Proxy import GHC.Num import GHC.Real diff --git a/libraries/base/GHC/Base.lhs b/libraries/base/GHC/Base.lhs index 3267bbfa83..73bfb28d30 100644 --- a/libraries/base/GHC/Base.lhs +++ b/libraries/base/GHC/Base.lhs @@ -183,6 +183,29 @@ otherwise = True build = error "urk" foldr = error "urk" -} + +\end{code} + +%********************************************************* +%* * +\subsection{The Maybe type} +%* * +%********************************************************* +\begin{code} + +-- | The 'Maybe' type encapsulates an optional value. A value of type +-- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@), +-- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to +-- deal with errors or exceptional cases without resorting to drastic +-- measures such as 'error'. +-- +-- The 'Maybe' type is also a monad. It is a simple kind of error +-- monad, where all errors are represented by 'Nothing'. A richer +-- error monad can be built using the 'Data.Either.Either' type. +-- +data Maybe a = Nothing | Just a + deriving (Eq, Ord) + \end{code} %********************************************************* @@ -272,6 +295,18 @@ instance Monoid Ordering where EQ `mappend` y = y GT `mappend` _ = GT +-- | Lift a semigroup into 'Maybe' forming a 'Monoid' according to +-- <http://en.wikipedia.org/wiki/Monoid>: \"Any semigroup @S@ may be +-- turned into a monoid simply by adjoining an element @e@ not in @S@ +-- and defining @e*e = e@ and @e*s = s = s*e@ for all @s ∈ S@.\" Since +-- there is no \"Semigroup\" typeclass providing just 'mappend', we +-- use 'Monoid' instead. +instance Monoid a => Monoid (Maybe a) where + mempty = Nothing + Nothing `mappend` m = m + m `mappend` Nothing = m + Just m1 `mappend` Just m2 = Just (m1 `mappend` m2) + instance Monoid a => Applicative ((,) a) where pure x = (mempty, x) (u, f) <*> (v, x) = (u `mappend` v, f x) @@ -521,6 +556,25 @@ instance Monad ((->) r) where instance Functor ((,) a) where fmap f (x,y) = (x, f y) + +instance Functor Maybe where + fmap _ Nothing = Nothing + fmap f (Just a) = Just (f a) + +instance Applicative Maybe where + pure = return + (<*>) = ap + +instance Monad Maybe where + (Just x) >>= k = k x + Nothing >>= _ = Nothing + + (Just _) >> k = k + Nothing >> _ = Nothing + + return = Just + fail _ = Nothing + \end{code} diff --git a/libraries/base/GHC/Conc/Signal.hs b/libraries/base/GHC/Conc/Signal.hs index 2d70419145..3f5eacb572 100644 --- a/libraries/base/GHC/Conc/Signal.hs +++ b/libraries/base/GHC/Conc/Signal.hs @@ -10,7 +10,6 @@ module GHC.Conc.Signal import Control.Concurrent.MVar (MVar, newMVar, withMVar) import Data.Dynamic (Dynamic) -import Data.Maybe (Maybe(..)) import Foreign.C.Types (CInt) import Foreign.ForeignPtr (ForeignPtr) import Foreign.StablePtr (castPtrToStablePtr, castStablePtrToPtr, diff --git a/libraries/base/GHC/Event/EPoll.hsc b/libraries/base/GHC/Event/EPoll.hsc index 298f450096..f3d635bb5f 100644 --- a/libraries/base/GHC/Event/EPoll.hsc +++ b/libraries/base/GHC/Event/EPoll.hsc @@ -40,7 +40,6 @@ available = False import Control.Monad (when) import Data.Bits (Bits, FiniteBits, (.|.), (.&.)) -import Data.Maybe (Maybe(..)) import Data.Word (Word32) import Foreign.C.Error (eNOENT, getErrno, throwErrno, throwErrnoIfMinus1, throwErrnoIfMinus1_) diff --git a/libraries/base/GHC/Event/Internal.hs b/libraries/base/GHC/Event/Internal.hs index fcd7886a20..f57cf9b88a 100644 --- a/libraries/base/GHC/Event/Internal.hs +++ b/libraries/base/GHC/Event/Internal.hs @@ -24,7 +24,6 @@ module GHC.Event.Internal import Data.Bits ((.|.), (.&.)) import Data.List (foldl', intercalate) -import Data.Maybe (Maybe(..)) import Foreign.C.Error (eINTR, getErrno, throwErrno) import System.Posix.Types (Fd) import GHC.Base diff --git a/libraries/base/GHC/Event/Manager.hs b/libraries/base/GHC/Event/Manager.hs index f07efba311..9f12ecd9dc 100644 --- a/libraries/base/GHC/Event/Manager.hs +++ b/libraries/base/GHC/Event/Manager.hs @@ -56,7 +56,7 @@ import Control.Monad ((=<<), forM_, when, replicateM, void) import Data.Bits ((.&.)) import Data.IORef (IORef, atomicModifyIORef', mkWeakIORef, newIORef, readIORef, writeIORef) -import Data.Maybe (Maybe(..), maybe) +import Data.Maybe (maybe) import GHC.Arr (Array, (!), listArray) import GHC.Base import GHC.Conc.Signal (runHandlers) diff --git a/libraries/base/GHC/Event/PSQ.hs b/libraries/base/GHC/Event/PSQ.hs index 853958bc29..a623625761 100644 --- a/libraries/base/GHC/Event/PSQ.hs +++ b/libraries/base/GHC/Event/PSQ.hs @@ -88,7 +88,6 @@ module GHC.Event.PSQ , atMost ) where -import Data.Maybe (Maybe(..)) import GHC.Base import GHC.Num (Num(..)) import GHC.Show (Show(showsPrec)) diff --git a/libraries/base/GHC/Event/Poll.hsc b/libraries/base/GHC/Event/Poll.hsc index ad2a96f56f..fd05a13799 100644 --- a/libraries/base/GHC/Event/Poll.hsc +++ b/libraries/base/GHC/Event/Poll.hsc @@ -28,7 +28,6 @@ available = False import Control.Concurrent.MVar (MVar, newMVar, swapMVar) import Control.Monad ((=<<), unless) import Data.Bits (Bits, FiniteBits, (.|.), (.&.)) -import Data.Maybe (Maybe(..)) import Data.Word import Foreign.C.Types (CInt(..), CShort(..)) import Foreign.Ptr (Ptr) diff --git a/libraries/base/GHC/Event/Thread.hs b/libraries/base/GHC/Event/Thread.hs index 0a82a548da..3d4dc7d405 100644 --- a/libraries/base/GHC/Event/Thread.hs +++ b/libraries/base/GHC/Event/Thread.hs @@ -19,7 +19,6 @@ import Control.Exception (finally, SomeException, toException) import Control.Monad (forM, forM_, sequence_, zipWithM, when) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.List (zipWith3) -import Data.Maybe (Maybe(..)) import Data.Tuple (snd) import Foreign.C.Error (eBADF, errnoToIOError) import Foreign.C.Types (CInt(..), CUInt(..)) diff --git a/libraries/base/GHC/Event/TimerManager.hs b/libraries/base/GHC/Event/TimerManager.hs index cbfce59434..435693a927 100644 --- a/libraries/base/GHC/Event/TimerManager.hs +++ b/libraries/base/GHC/Event/TimerManager.hs @@ -42,7 +42,6 @@ import Control.Exception (finally) import Control.Monad ((=<<), sequence_, when) import Data.IORef (IORef, atomicModifyIORef', mkWeakIORef, newIORef, readIORef, writeIORef) -import Data.Maybe (Maybe(..)) import GHC.Base import GHC.Conc.Signal (runHandlers) import GHC.Num (Num(..)) diff --git a/libraries/base/GHC/IO.hs b/libraries/base/GHC/IO.hs index 388c81f138..1d86b8d12a 100644 --- a/libraries/base/GHC/IO.hs +++ b/libraries/base/GHC/IO.hs @@ -44,7 +44,6 @@ import GHC.Base import GHC.ST import GHC.Exception import GHC.Show -import Data.Maybe import {-# SOURCE #-} GHC.IO.Exception ( userError ) diff --git a/libraries/base/GHC/IO/BufferedIO.hs b/libraries/base/GHC/IO/BufferedIO.hs index b715c61380..cc98be76d1 100644 --- a/libraries/base/GHC/IO/BufferedIO.hs +++ b/libraries/base/GHC/IO/BufferedIO.hs @@ -25,7 +25,6 @@ import GHC.Base import GHC.Ptr import Data.Word import GHC.Num -import Data.Maybe import GHC.IO.Device as IODevice import GHC.IO.Device as RawIO import GHC.IO.Buffer diff --git a/libraries/base/GHC/IO/Device.hs b/libraries/base/GHC/IO/Device.hs index e20cdf0770..b6c973b7e0 100644 --- a/libraries/base/GHC/IO/Device.hs +++ b/libraries/base/GHC/IO/Device.hs @@ -29,7 +29,6 @@ import GHC.Enum import GHC.Read import GHC.Show import GHC.Ptr -import Data.Maybe import GHC.Num import GHC.IO import {-# SOURCE #-} GHC.IO.Exception ( unsupportedOperation ) diff --git a/libraries/base/GHC/IO/Encoding.hs b/libraries/base/GHC/IO/Encoding.hs index eb4d74ccfc..9d03276c98 100644 --- a/libraries/base/GHC/IO/Encoding.hs +++ b/libraries/base/GHC/IO/Encoding.hs @@ -49,7 +49,6 @@ import GHC.Word import Data.IORef import Data.Char (toUpper) import Data.List -import Data.Maybe import System.IO.Unsafe (unsafePerformIO) -- ----------------------------------------------------------------------------- diff --git a/libraries/base/GHC/IO/Encoding/Failure.hs b/libraries/base/GHC/IO/Encoding/Failure.hs index 4b24d06012..6e06213ae2 100644 --- a/libraries/base/GHC/IO/Encoding/Failure.hs +++ b/libraries/base/GHC/IO/Encoding/Failure.hs @@ -34,9 +34,6 @@ import GHC.Real ( fromIntegral ) --import System.Posix.Internals -import Data.Maybe - - -- | The 'CodingFailureMode' is used to construct 'TextEncoding's, and -- specifies how they handle illegal sequences. data CodingFailureMode diff --git a/libraries/base/GHC/IO/Encoding/UTF16.hs b/libraries/base/GHC/IO/Encoding/UTF16.hs index b0ff992a0d..c6dc7c0a76 100644 --- a/libraries/base/GHC/IO/Encoding/UTF16.hs +++ b/libraries/base/GHC/IO/Encoding/UTF16.hs @@ -47,7 +47,6 @@ import GHC.IO.Encoding.Failure import GHC.IO.Encoding.Types import GHC.Word import Data.Bits -import Data.Maybe import GHC.IORef -- ----------------------------------------------------------------------------- diff --git a/libraries/base/GHC/IO/Encoding/UTF32.hs b/libraries/base/GHC/IO/Encoding/UTF32.hs index eddc4f8693..8d996b14b9 100644 --- a/libraries/base/GHC/IO/Encoding/UTF32.hs +++ b/libraries/base/GHC/IO/Encoding/UTF32.hs @@ -47,7 +47,6 @@ import GHC.IO.Encoding.Failure import GHC.IO.Encoding.Types import GHC.Word import Data.Bits -import Data.Maybe import GHC.IORef -- ----------------------------------------------------------------------------- diff --git a/libraries/base/GHC/IO/Exception.hs b/libraries/base/GHC/IO/Exception.hs index e7e3316ca9..0f351f0382 100644 --- a/libraries/base/GHC/IO/Exception.hs +++ b/libraries/base/GHC/IO/Exception.hs @@ -48,7 +48,6 @@ import GHC.IO import GHC.Show import GHC.Read import GHC.Exception -import Data.Maybe import GHC.IO.Handle.Types import Foreign.C.Types diff --git a/libraries/base/GHC/IO/FD.hs b/libraries/base/GHC/IO/FD.hs index 1134e95f8d..1783125a90 100644 --- a/libraries/base/GHC/IO/FD.hs +++ b/libraries/base/GHC/IO/FD.hs @@ -35,7 +35,6 @@ import GHC.Num import GHC.Real import GHC.Show import GHC.Enum -import Data.Maybe import Control.Monad import Data.Typeable diff --git a/libraries/base/GHC/IO/Handle/Types.hs b/libraries/base/GHC/IO/Handle/Types.hs index defa33bbca..fbedeecdbd 100644 --- a/libraries/base/GHC/IO/Handle/Types.hs +++ b/libraries/base/GHC/IO/Handle/Types.hs @@ -42,7 +42,6 @@ import GHC.IO.Buffer import GHC.IO.BufferedIO import GHC.IO.Encoding.Types import GHC.IORef -import Data.Maybe import GHC.Show import GHC.Read import GHC.Word diff --git a/libraries/base/GHC/MVar.hs b/libraries/base/GHC/MVar.hs index ff138a5ef2..5bdb6ff82a 100644 --- a/libraries/base/GHC/MVar.hs +++ b/libraries/base/GHC/MVar.hs @@ -33,7 +33,6 @@ module GHC.MVar ( ) where import GHC.Base -import Data.Maybe import Data.Typeable data MVar a = MVar (MVar# RealWorld a) deriving( Typeable ) diff --git a/libraries/base/GHC/Show.lhs b/libraries/base/GHC/Show.lhs index 09c3c56cd9..93320eea1b 100644 --- a/libraries/base/GHC/Show.lhs +++ b/libraries/base/GHC/Show.lhs @@ -52,7 +52,6 @@ module GHC.Show import GHC.Base import GHC.Num -import Data.Maybe import GHC.List ((!!), foldr1, break) \end{code} diff --git a/libraries/base/GHC/Weak.lhs b/libraries/base/GHC/Weak.lhs index bffd9f8236..5405a302ed 100644 --- a/libraries/base/GHC/Weak.lhs +++ b/libraries/base/GHC/Weak.lhs @@ -32,7 +32,6 @@ module GHC.Weak ( ) where import GHC.Base -import Data.Maybe import Data.Typeable {-| diff --git a/libraries/base/Numeric.hs b/libraries/base/Numeric.hs index 4a1a5b121a..7cba671309 100644 --- a/libraries/base/Numeric.hs +++ b/libraries/base/Numeric.hs @@ -65,7 +65,6 @@ import GHC.Real import GHC.Float import GHC.Num import GHC.Show -import Data.Maybe import Text.ParserCombinators.ReadP( ReadP, readP_to_S, pfail ) import qualified Text.Read.Lex as L diff --git a/libraries/base/Text/Read.hs b/libraries/base/Text/Read.hs index 6c9d89db76..18c485fea1 100644 --- a/libraries/base/Text/Read.hs +++ b/libraries/base/Text/Read.hs @@ -47,7 +47,6 @@ module Text.Read ( import GHC.Base import GHC.Read import Data.Either -import Data.Maybe import Text.ParserCombinators.ReadP as P import Text.ParserCombinators.ReadPrec import qualified Text.Read.Lex as L diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs index 650410841e..268838e195 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs @@ -512,8 +512,8 @@ trueName = mkNameG DataName "ghc-prim" "GHC.Types" "True" falseName = mkNameG DataName "ghc-prim" "GHC.Types" "False" nothingName, justName :: Name -nothingName = mkNameG DataName "base" "Data.Maybe" "Nothing" -justName = mkNameG DataName "base" "Data.Maybe" "Just" +nothingName = mkNameG DataName "base" "GHC.Base" "Nothing" +justName = mkNameG DataName "base" "GHC.Base" "Just" leftName, rightName :: Name leftName = mkNameG DataName "base" "Data.Either" "Left" |