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

Moduleoptional-args-1.0.2Haskell2010

Data.Optional

Use the Optional type for optional function arguments. For example:

import Data.Optional

greet :: Optional String -> String
greet (Specific name) = "Hello, " ++ name
greet  Default        = "Hello"
Example2 expressions
greet (Specific "John")"Hello, John"greet Default"Hello"

The Optional type overloads as many Haskell literals as possible so that you do not need to wrap values in Specific. For example, if you enable the OverloadedStrings extension you can use a naked string literal instead:

Example2 expressions
:set -XOverloadedStringsgreet "John""Hello, John"

The Optional type also implements Num and Fractional, so you can use numeric literals in place of Optional values:

birthday :: Optional Int -> String
birthday (Specific age) = "You are " ++ show age ++ " years old!"
birthday  Default       = "You are one year older!"
Example2 expressions
birthday 20"You are 20 years old!"birthday Default"You are one year older!"

The IsString, Num, and Fractional instances are recursive, so you can wrap your types in a more descriptive newtype and derive IsString, Num or Fractional:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Data.Optional
import Data.String (IsString)

newtype Name = Name { getName :: String } deriving (IsString)

greet :: Optional Name -> String
greet (Specific name) = "Hello, " ++ getName name
greet  Default        = "Hello"

newtype Age = Age { getAge :: Int } deriving (Num)

birthday :: Optional Age -> String
birthday (Specific age) = "You are " ++ show (getAge age) ++ " years old!"
birthday  Default       = "You are one year older!"

... and you would still be able to provide naked numeric or string literals:

Example3 expressions
:set -XOverloadedStringsgreet "John""Hello, John"birthday 20"You are 20 years old!"

You can use empty as a short-hand for a Default argument:

Example2 expressions
greet empty"Hello"birthday empty"You are one year older!"

You can also use pure as a short-hand for a Specific argument:

Example2 expressions
greet (pure "John")"Hello, John"birthday (pure 20)"You are 20 years old!"
  • 1 type
  • 3 values

Optional

4 declarations
datadata Optional a
#

A function argument that has a Default value

Constructors

Instances14Monad, Functor, Applicative, Foldable, Traversable, Alternative, …
valueoptional :: b -> (a -> b) -> Optional a -> b
#

The optional function takes a default value, a function, and an Optional value. If the Optional value is Default, the function returns the default value. Otherwise, it applies the function to the value inside the Optional and returns the result.

Re-exports

2 declarations
methodempty :: f a
#

The identity of <|>

empty <|> a     == a
a     <|> empty == a
methodpure :: a -> f a
#

Lift a value into the Structure.

Examples
Example1 expression
pure 1 :: Maybe IntJust 1
Example1 expression
pure 'z' :: [Char]"z"
Example1 expression
pure (pure ":D") :: Maybe [String]Just [":D"]