Option of a type #
This file develops the basic theory of option types.
If α is a type, then Option α can be understood as the type with one more element than α.
Option α has terms some a, where a : α, and none, which is the added element.
This is useful in multiple ways:
It is the prototype of addition of terms to a type. See for example
WithBot αwhich usesnoneas an element smaller than all others.It can be used to define failsafe partial functions, which return
some the_result_we_expectif we can findthe_result_we_expect, andnoneif there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it returnnone.Optionis a monad. We love monads.
Part is an alternative to Option that can be seen as the type of True/False values
along with a term a : α if the value is True.
Option.map as a function between functions is injective.
Given an element of a : Option α, a default element b : β and a function α → β, apply this
function to a if it comes from α, and return b otherwise.