X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FMaybe.hs;h=4b7b67e305445c9c4f3201fd7282d78a393b5ce0;hb=f7a485978f04e84b086f1974b88887cc72d832d0;hp=06c7a25398d39db56e13dddf52d3b1830261b498;hpb=7f1f4e7a695c402ddd3a1dc2cc7114e649a78ebc;p=ghc-base.git diff --git a/Data/Maybe.hs b/Data/Maybe.hs index 06c7a25..4b7b67e 100644 --- a/Data/Maybe.hs +++ b/Data/Maybe.hs @@ -1,15 +1,13 @@ {-# OPTIONS -fno-implicit-prelude #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Data.Maybe -- Copyright : (c) The University of Glasgow 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : experimental --- Portability : non-portable --- --- $Id: Maybe.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $ +-- Portability : portable -- -- The Maybe type, and associated operations. -- @@ -33,12 +31,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