MAYBE

MAYBE TYPE

The type (also called ) is the disjoint union of the unit type and a given type . It represents computations that may fail or return no value () or succeed with a value ().

Formation

def Maybe (A : U) : U := + 𝟏 A

Constructors

has two constructors: (the failure case) and (the success case).

def nothing (A : U) : Maybe A := inl 𝟏 A β˜… def just (A : U) (a : A) : Maybe A := inr 𝟏 A a

Eliminators

The induction principle for is derived from the induction principle of the disjoint union .

def maybe-ind (A : U) (P: Maybe A β†’ U) (n: P (nothing A)) (j: Ξ  (x: A), P (just A x)) : Ξ  (a: Maybe A), P a := +-ind 𝟏 A P (ind₁ (Ξ» (_ : 𝟏), P (nothing A)) n) j

Non-dependent versions:

def maybe-rec (A P: U) (n: P) (j: A β†’ P) : Maybe A β†’ P := maybe-ind A (Ξ» (_ : Maybe A), P) n j

Theorems

Standard computational rules follow from the corresponding rules of .

(See $+$-Ξ² rules in the Either section for the computational behavior.)