HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-9.10.3GHC2021

GHC.Data.Maybe

  • 3 types
  • 20 values
  • Packageghc-9.10.3
  • Exports23
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceMaybe.hs
datadata Maybe a
#

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 Either type.

Constructors

Instances50Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
valuefromMaybe :: a -> Maybe a -> a
#

The fromMaybe function takes a default value and a Maybe value. If the Maybe is Nothing, it returns the default value; otherwise, it returns the value contained in the Maybe.

Examples

Basic usage:

Example1 expression
fromMaybe "" (Just "Hello, World!")"Hello, World!"
Example1 expression
fromMaybe "" Nothing""

Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:

Example3 expressions
import GHC.Internal.Text.Read ( readMaybe )fromMaybe 0 (readMaybe "5")5fromMaybe 0 (readMaybe "")0
valuemaybe :: b -> (a -> b) -> Maybe a -> b
#

The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

Examples

Basic usage:

Example1 expression
maybe False odd (Just 3)True
Example1 expression
maybe False odd NothingFalse

Read an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:

Example3 expressions
import GHC.Internal.Text.Read ( readMaybe )maybe 0 (*2) (readMaybe "5")10maybe 0 (*2) (readMaybe "")0

Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":

Example2 expressions
maybe "" show (Just 5)"5"maybe "" show Nothing""
valuecatMaybes :: [Maybe a] -> [a]
#

The catMaybes function takes a list of Maybes and returns a list of all the Just values.

Examples

Basic usage:

Example1 expression
catMaybes [Just 1, Nothing, Just 3][1,3]

When constructing a list of Maybe values, catMaybes can be used to return all of the "success" results (if the list is the result of a map, then mapMaybe would be more appropriate):

Example3 expressions
import GHC.Internal.Text.Read ( readMaybe )[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][Just 1,Nothing,Just 3]catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][1,3]
valuefromJust :: HasCallStack => Maybe a -> a
#

The fromJust function extracts the element out of a Just and throws an error if its argument is Nothing.

Examples

Basic usage:

Example1 expression
fromJust (Just 1)1
Example1 expression
2 * (fromJust (Just 10))20
Example1 expression
2 * (fromJust Nothing)*** Exception: Maybe.fromJust: Nothing...

WARNING: This function is partial. You can use case-matching instead.

valueisJust :: Maybe a -> Bool
#

The isJust function returns True iff its argument is of the form Just _.

Examples

Basic usage:

Example1 expression
isJust (Just 3)True
Example1 expression
isJust (Just ())True
Example1 expression
isJust NothingFalse

Only the outer constructor is taken into consideration:

Example1 expression
isJust (Just Nothing)True
valueisNothing :: Maybe a -> Bool
#

The isNothing function returns True iff its argument is Nothing.

Examples

Basic usage:

Example1 expression
isNothing (Just 3)False
Example1 expression
isNothing (Just ())False
Example1 expression
isNothing NothingTrue

Only the outer constructor is taken into consideration:

Example1 expression
isNothing (Just Nothing)False
valuelistToMaybe :: [a] -> Maybe a
#

The listToMaybe function returns Nothing on an empty list or Just a where a is the first element of the list.

Examples

Basic usage:

Example1 expression
listToMaybe []Nothing
Example1 expression
listToMaybe [9]Just 9
Example1 expression
listToMaybe [1,2,3]Just 1

Composing maybeToList with listToMaybe should be the identity on singleton/empty lists:

Example2 expressions
maybeToList $ listToMaybe [5][5]maybeToList $ listToMaybe [][]

But not on lists with more than one element:

Example1 expression
maybeToList $ listToMaybe [1,2,3][1]
valuemapMaybe :: (a -> Maybe b) -> [a] -> [b]
#

The mapMaybe function is a version of map which can throw out elements. In particular, the functional argument returns something of type Maybe b. If this is Nothing, no element is added on to the result list. If it is Just b, then b is included in the result list.

Examples

Using mapMaybe f x is a shortcut for catMaybes $ map f x in most cases:

Example4 expressions
import GHC.Internal.Text.Read ( readMaybe )let readMaybeInt = readMaybe :: String -> Maybe IntmapMaybe readMaybeInt ["1", "Foo", "3"][1,3]catMaybes $ map readMaybeInt ["1", "Foo", "3"][1,3]

If we map the Just constructor, the entire list should be returned:

Example1 expression
mapMaybe Just [1,2,3][1,2,3]
valuemaybeToList :: Maybe a -> [a]
#

The maybeToList function returns an empty list when given Nothing or a singleton list when given Just.

Examples

Basic usage:

Example1 expression
maybeToList (Just 7)[7]
Example1 expression
maybeToList Nothing[]

One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:

Example3 expressions
import GHC.Internal.Text.Read ( readMaybe )sum $ maybeToList (readMaybe "3")3sum $ maybeToList (readMaybe "")0
valueorElse :: Maybe a -> a -> a
#

Flipped version of fromMaybe, useful for chaining.

valuefirstJusts :: Foldable f => f (Maybe a) -> Maybe a
#

Takes a list of Maybes and returns the first Just if there is one, or Nothing otherwise.

valuefirstJustsM :: (Monad m, Foldable f) => f (m (Maybe a)) -> m (Maybe a)
#

Takes computations returnings Maybes; tries each one in order. The first one to return a Just wins. Returns Nothing if all computations return Nothing.

MaybeT

3 declarations
newtypenewtype MaybeT (m :: Type -> Type) a
#

The parameterizable maybe monad, obtained by composing an arbitrary monad with the Maybe monad.

Computations are actions that may produce a value or exit.

The return function yields a computation that produces that value, while >>= sequences two subcomputations, exiting if either computation does.

Constructors

Instances37MonadTrans, MonadRWS, Generic1, MonadAccum, MonadError, MonadReader, …