StdMaybe.dcl 1.29 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
//* `case @1 of ({{Just}} _) -> True; _ -> False`
isJust		:: !(Maybe .x) -> Bool
19

Camil Staps's avatar
Camil Staps committed
20 21 22 23 24 25 26
//* `{{not}} {{o}} {{isJust}}`
isNothing	:: !(Maybe .x) -> Bool

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

//* {{`isJust`}} for a possibly unique value.
27
isJustU :: !u:(Maybe .x) -> (!Bool, !u:Maybe .x)
Camil Staps's avatar
Camil Staps committed
28 29

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

Camil Staps's avatar
Camil Staps committed
32
//* Apply a function on the value boxed in {{`Just`}}, or pass on {{`Nothing`}}.
33 34 35 36
mapMaybe	:: .(.x -> .y) !(Maybe .x) -> Maybe .y

instance == (Maybe x) | == x

Camil Staps's avatar
Camil Staps committed
37 38 39
/**
 * @result A list with no or one element.
 */
40 41
maybeToList :: !(Maybe .a) -> [.a];

Camil Staps's avatar
Camil Staps committed
42 43 44
/**
 * @result {{`Just`}} the head of the list or {{`Nothing`}} if it is empty.
 */
45 46
listToMaybe :: ![.a] -> .Maybe .a;

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