diff options
author | simonmar <unknown> | 2001-06-28 14:15:04 +0000 |
---|---|---|
committer | simonmar <unknown> | 2001-06-28 14:15:04 +0000 |
commit | 4fb94ae5e5d632748fa2e6c35e259eccc5a1a3f4 (patch) | |
tree | dd5bc589e37efe68e84099180c16359a3acdb06b /libraries/base/Data/Maybe.hs | |
download | haskell-4fb94ae5e5d632748fa2e6c35e259eccc5a1a3f4.tar.gz |
[project @ 2001-06-28 14:15:04 by simonmar]
First cut of the Haskell Core Libraries
=======================================
NOTE: it's not meant to be a working snapshot. The code is just here
to look at and so the NHC/Hugs guys can start playing around with it.
There is no build system. For GHC, the libraries tree is intended to
be grafted onto an existing fptools/ tree, and the Makefile in
libraries/core is a quick hack for that setup. This won't work at the
moment without the other changes needed in fptools/ghc, which I
haven't committed because they'll cause breakage. However, with the
changes required these sources build a working Prelude and libraries.
The layout mostly follows the one we agreed on, with one or two minor
changes; in particular the Data/Array layout probably isn't final
(there are several choices here).
The document is in libraries/core/doc as promised.
The cbits stuff is just a copy of ghc/lib/std/cbits and has
GHC-specific stuff in it. We should really separate the
compiler-specific C support from any compiler-independent C support
there might be.
Don't pay too much attention to the portability or stability status
indicated in the header of each source file at the moment - I haven't
gone through to make sure they're all consistent and make sense.
I'm using non-literate source outside of GHC/. Hope that's ok with
everyone.
We need to discuss how the build system is going to work...
Diffstat (limited to 'libraries/base/Data/Maybe.hs')
-rw-r--r-- | libraries/base/Data/Maybe.hs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/libraries/base/Data/Maybe.hs b/libraries/base/Data/Maybe.hs new file mode 100644 index 0000000000..06c7a25398 --- /dev/null +++ b/libraries/base/Data/Maybe.hs @@ -0,0 +1,75 @@ +{-# OPTIONS -fno-implicit-prelude #-} +----------------------------------------------------------------------------- +-- +-- Module : Data.Maybe +-- Copyright : (c) The University of Glasgow 2001 +-- License : BSD-style (see the file libraries/core/LICENSE) +-- +-- Maintainer : libraries@haskell.org +-- Stability : experimental +-- Portability : non-portable +-- +-- $Id: Maybe.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $ +-- +-- The Maybe type, and associated operations. +-- +----------------------------------------------------------------------------- + +module Data.Maybe + ( + Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read, + -- Functor, Monad, MonadPlus + + , maybe -- :: b -> (a -> b) -> Maybe a -> b + + , isJust -- :: Maybe a -> Bool + , isNothing -- :: Maybe a -> Bool + , fromJust -- :: Maybe a -> a + , fromMaybe -- :: a -> Maybe a -> a + , listToMaybe -- :: [a] -> Maybe a + , maybeToList -- :: Maybe a -> [a] + , catMaybes -- :: [Maybe a] -> [a] + , mapMaybe -- :: (a -> Maybe b) -> [a] -> [b] + ) where + +#ifdef __GLASGOW_HASKELL__ +import GHC.Err ( error ) +import GHC.List +import GHC.Maybe +import GHC.Base +#endif + +isJust :: Maybe a -> Bool +isJust Nothing = False +isJust _ = True + +isNothing :: Maybe a -> Bool +isNothing Nothing = True +isNothing _ = False + +fromJust :: Maybe a -> a +fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck +fromJust (Just x) = x + +fromMaybe :: a -> Maybe a -> a +fromMaybe d x = case x of {Nothing -> d;Just v -> v} + +maybeToList :: Maybe a -> [a] +maybeToList Nothing = [] +maybeToList (Just x) = [x] + +listToMaybe :: [a] -> Maybe a +listToMaybe [] = Nothing +listToMaybe (a:_) = Just a + +catMaybes :: [Maybe a] -> [a] +catMaybes ls = [x | Just x <- ls] + +mapMaybe :: (a -> Maybe b) -> [a] -> [b] +mapMaybe _ [] = [] +mapMaybe f (x:xs) = + let rs = mapMaybe f xs in + case f x of + Nothing -> rs + Just r -> r:rs + |