Misplaced Pages

Option type

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by Jarble (talk | contribs) at 03:03, 6 June 2013 (this article relies on just one source). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 03:03, 6 June 2013 by Jarble (talk | contribs) (this article relies on just one source)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
This article relies largely or entirely on a single source. Relevant discussion may be found on the talk page. Please help improve this article by introducing citations to additional sources.
Find sources: "Option type" – news · newspapers · books · scholar · JSTOR
For families of option contracts in finance, see Option style.

In programming languages (especially functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional value; e.g. it is used as the return type of functions which may or may not return a meaningful value when they are applied. It consists of either an empty constructor (called None or Nothing), or a constructor encapsulating the original data type A (written Just A or Some A).

In the Haskell language, the option type (called Maybe) is defined as data Maybe a = Just a | Nothing. In the OCaml language, the option type is defined as type 'a option = None | Some of 'a. In the Scala language, Option is defined as parametrized abstract class '.. Option = if (x == null) None else Some(x)... In the Standard ML language, the option type is defined as datatype 'a option = NONE | SOME of 'a. In the Rust language, it is defined as enum Option<T> { None, Some(T) }.

In type theory, it may be written as: A ? = A + 1 {\displaystyle A^{?}=A+1} .

In languages that have tagged unions, as in most functional programming languages, option types can be expressed as the tagged union of a unit type plus the encapsulated type.

In the Curry-Howard correspondence, option types are related to the annihilation law for ∨: x∨1=1.

An option type can also be seen as a collection containing either a single element or zero elements.

The option monad

The option type is a monad under the following functions:

return : A A ? = a Just a {\displaystyle {\text{return}}\colon A\to A^{?}=a\mapsto {\text{Just}}\,a}
bind : A ? ( A B ? ) B ? = a f { Nothing if   a = Nothing f a if   a = Just a {\displaystyle {\text{bind}}\colon A^{?}\to (A\to B^{?})\to B^{?}=a\mapsto f\mapsto {\begin{cases}{\text{Nothing}}&{\text{if}}\ a={\text{Nothing}}\\f\,a'&{\text{if}}\ a={\text{Just}}\,a'\end{cases}}}

We may also describe the option monad in terms of functions return, fmap and join, where the latter two are given by:

fmap : ( A B ) A ? B ? = f a { Nothing if   a = Nothing Just f a if   a = Just a {\displaystyle {\text{fmap}}\colon (A\to B)\to A^{?}\to B^{?}=f\mapsto a\mapsto {\begin{cases}{\text{Nothing}}&{\text{if}}\ a={\text{Nothing}}\\{\text{Just}}\,f\,a'&{\text{if}}\ a={\text{Just}}\,a'\end{cases}}}
join : A ? ? A ? = a { Nothing if   a = Nothing Nothing if   a = Just Nothing Just a if   a = Just Just a {\displaystyle {\text{join}}\colon {A^{?}}^{?}\to A^{?}=a\mapsto {\begin{cases}{\text{Nothing}}&{\text{if}}\ a={\text{Nothing}}\\{\text{Nothing}}&{\text{if}}\ a={\text{Just}}\,{\text{Nothing}}\\{\text{Just}}\,a'&{\text{if}}\ a={\text{Just}}\,{\text{Just}}\,a'\end{cases}}}

The option monad is an additive monad: it has Nothing as a zero constructor and the following function as a monadic sum:

mplus : A ? A ? A ? = a 1 a 2 { Nothing if   a 1 = Nothing a 2 = Nothing Just a 2 if   a 1 = Nothing a 2 = Just a 2 Just a 1 if   a 1 = Just a 1 {\displaystyle {\text{mplus}}\colon A^{?}\to A^{?}\to A^{?}=a_{1}\mapsto a_{2}\mapsto {\begin{cases}{\text{Nothing}}&{\text{if}}\ a_{1}={\text{Nothing}}\land a_{2}={\text{Nothing}}\\{\text{Just}}\,a'_{2}&{\text{if}}\ a_{1}={\text{Nothing}}\land a_{2}={\text{Just}}\,a'_{2}\\{\text{Just}}\,a'_{1}&{\text{if}}\ a_{1}={\text{Just}}\,a'_{1}\end{cases}}}

In fact, the resulting structure is an idempotent monoid.

Examples

Scala

Scala implements Option as a parameterized type, so a variable can be an Option, accessed as follows:

// Defining variables that are Options of type Int
val res1: Option = Some(42)
val res2: Option = None
// This function uses pattern matching to deconstruct Options
def compute(opt: Option) = opt match {
  case None => "No value"
  case Some(x) => "The value is: " + x
}
System.out.println(compute(res1))  // The value is: 42
System.out.println(compute(res2))  // No value

An Option value is usually used with pattern matching, as in the previous example. In this way, the program is safe as it cannot generate any exception or error (e.g. by trying to obtain the value of an Option variable that is equal to None). Therefore, it essentially works as a type-safe alternative to the null value.

F#

(* This function uses pattern matching to deconstruct Options *)
let compute = function
    None   -> "No value"
  | Some x -> sprintf "The value is: %d" x
printfn "%s" (compute <| Some 42)(* The value is: 42 *)
printfn "%s" (compute None)      (* No value         *)

Haskell

-- Defining variables that are Maybes of type Int
res1, res2 :: Maybe Int
res1 = Just 42
res2 = Nothing
-- This function uses pattern matching to deconstruct Maybes
compute :: Maybe Int -> String
compute may = case may of
    Nothing -> "No value"
    Just x  -> "The value is: " ++ show x
main = do
    print $ compute res1 -- The value is: 42
    print $ compute res2 -- No value

See also

References

  1. Martin Odersky; Lex Spoon; Bill Venners (2008). Programming in Scala. Artima Inc. pp. 282–284. ISBN 978-0-9815316-0-1. Retrieved 6 September 2011.
Data types
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Categories:
Option type Add topic