StdMaybe.dcl 1.34 KB
Newer Older
1 2 3 4 5 6 7 8
definition module StdMaybe

//	********************************************************************************
//	Clean StdLib library module, version 1.0
//	********************************************************************************

from StdOverloaded import class ==(..);

Camil Staps's avatar
Camil Staps committed
9 10 11 12
/**
 * The Maybe type represents an optional value by providing a constructor 
 * for no value (`Nothing`) and a constructor for just a value (`Just`).
 */
13 14 15 16
::	Maybe x
	=	Just x
	|	Nothing

Camil Staps's avatar
Camil Staps committed
17 18 19 20
/**
 * `case @1 of ({{Just}} _) -> True; _ -> False`
 */
isJust		:: !(Maybe .x) -> Bool
21

Camil Staps's avatar
Camil Staps committed
22 23 24 25 26 27 28 29 30 31 32 33 34
/**
 * `{{not}} {{o}} {{isJust}}`
 */
isNothing	:: !(Maybe .x) -> Bool

/**
 * `\({{Just}} x) -> x`
 */
fromJust	:: !(Maybe .x) -> .x

/**
 * {{`isJust`}} for a possibly unique value.
 */
35
isJustU :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
Camil Staps's avatar
Camil Staps committed
36 37 38 39

/**
 * {{`isNothing`}} for a possibly unique value.
 */
40 41
isNothingU :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)

Camil Staps's avatar
Camil Staps committed
42 43 44
/**
 * Apply a function on the value boxed in {{`Just`}}, or pass on {{`Nothing`}}.
 */
45 46 47 48
mapMaybe	:: .(.x -> .y) !(Maybe .x) -> Maybe .y

instance == (Maybe x) | == x

Camil Staps's avatar
Camil Staps committed
49 50 51
/**
 * @result A list with no or one element.
 */
52 53
maybeToList :: !(Maybe .a) -> [.a];

Camil Staps's avatar
Camil Staps committed
54 55 56
/**
 * @result {{`Just`}} the head of the list or {{`Nothing`}} if it is empty.
 */
57 58
listToMaybe :: ![.a] -> .Maybe .a;

Camil Staps's avatar
Camil Staps committed
59 60 61
/**
 * `catMaybes ms = [m \\ {{Just}} m <- ms]`
 */
62
catMaybes :: ![Maybe .a] -> .[.a];