Commit b4752199 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺
Browse files

Move `Maybe`-typedef into GHC.Base

This is preparatory work for reintroducing SPECIALISEs that were lost
in d94de872

Differential Revision: https://phabricator.haskell.org/D214
parent e7a0f5b6
......@@ -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)
......
......@@ -105,7 +105,6 @@ import GHC.Conc.Sync
import Data.Dynamic
import Data.Either
import Data.Maybe
-----------------------------------------------------------------------------
-- Catching exceptions
......
......@@ -33,52 +33,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
......
......@@ -46,7 +46,6 @@ import GHC.Num
import GHC.Read
import GHC.Show
import GHC.Generics
import Data.Maybe
{-
-- just for testing
......
......@@ -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
......
......@@ -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
......
......@@ -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}
......
......@@ -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,
......
......@@ -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_)
......
......@@ -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
......
......@@ -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)
......
......@@ -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))
......
......@@ -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)
......
......@@ -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(..))
......
......@@ -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(..))
......
......@@ -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 )
......
......@@ -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
......
......@@ -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 )
......
......@@ -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)
-- -----------------------------------------------------------------------------
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment