X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FMaybe.hs;h=36e684e1ea62cfd6de0714a4b9294f7d85f68832;hb=9fa9bc17072a58c0bae2cce4764d38677e96ac29;hp=de5d121ee502bc25b585b690b36ad4744945edf9;hpb=5545727d5a6a1fc6d5d00f32a92a8fdf0fb7ca77;p=ghc-base.git diff --git a/Data/Maybe.hs b/Data/Maybe.hs index de5d121..36e684e 100644 --- a/Data/Maybe.hs +++ b/Data/Maybe.hs @@ -1,6 +1,6 @@ {-# OPTIONS -fno-implicit-prelude #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Data.Maybe -- Copyright : (c) The University of Glasgow 2001 -- License : BSD-style (see the file libraries/core/LICENSE) @@ -9,7 +9,7 @@ -- Stability : experimental -- Portability : portable -- --- $Id: Maybe.hs,v 1.2 2001/07/03 11:37:50 simonmar Exp $ +-- $Id: Maybe.hs,v 1.4 2002/04/24 16:31:39 simonmar Exp $ -- -- The Maybe type, and associated operations. -- @@ -33,12 +33,36 @@ module Data.Maybe ) where #ifdef __GLASGOW_HASKELL__ -import GHC.Err ( error ) -import GHC.List -import GHC.Maybe +import {-# SOURCE #-} GHC.Err ( error ) import GHC.Base #endif +-- --------------------------------------------------------------------------- +-- The Maybe type, and instances + +data Maybe a = Nothing | Just a deriving (Eq, Ord) + +instance Functor Maybe where + fmap _ Nothing = Nothing + fmap f (Just a) = Just (f a) + +instance Monad Maybe where + (Just x) >>= k = k x + Nothing >>= _ = Nothing + + (Just _) >> k = k + Nothing >> _ = Nothing + + return = Just + fail _ = Nothing + +-- --------------------------------------------------------------------------- +-- Functions over Maybe + +maybe :: b -> (a -> b) -> Maybe a -> b +maybe n _ Nothing = n +maybe _ f (Just x) = f x + isJust :: Maybe a -> Bool isJust Nothing = False isJust _ = True