StdFunc.dcl 2.74 KB
Newer Older
1 2 3 4 5 6 7
definition module StdFunc

// ****************************************************************************************
//	Concurrent Clean Standard Library Module Version 2.0
//	Copyright 1998 University of Nijmegen
// ****************************************************************************************

Camil Staps's avatar
Camil Staps committed
8 9 10 11 12 13 14
/**
 * The identity function.
 *
 * @param A value
 * @result The same value
 */
id    :: !.a -> .a
15

Camil Staps's avatar
Camil Staps committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * The constant function.
 *
 * @param A value
 * @param Another value
 * @result The first value
 */
const :: !.a .b -> .a

/**
 * Flip the arguments of a function. This is useful in function compositions.
 *
 * @type !.(.a -> .(.b -> .c)) .b .a -> .c
 * @param A function
 * @param The second argument to the function
 * @param The first argument to the function
 * @result The function, applied to its arguments
 */
34 35
flip f a b :== f b a

Camil Staps's avatar
Camil Staps committed
36 37 38 39 40 41 42 43 44
/**
 * Function composition.
 *
 * @type u:(.a -> .b) u:(.c -> .a) -> u:(.c -> .b)
 * @param A function f
 * @param A function g
 * @result The function f o g, that is, f applied after g
 */
(o) infixr  9
45 46
(o) f g :== \ x -> f (g x)

Camil Staps's avatar
Camil Staps committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/**
 * Apply a function twice.
 *
 * @param A function f
 * @param A value x
 * @result f (f x)
 */
twice			:: !(.a -> .a)   .a             -> .a

/**
 * Apply a function as long as a property holds.
 *
 * @param A property p
 * @param A function f
 * @param An initial value x
 * @result x if not p x, otherwise while p f (f x)
 */
while			:: !(a -> .Bool) (a -> a) 	 a 	->  a

/**
 * Apply a function until a property holds.
 *
 * @param A property p
 * @param A function f
 * @param An initial value x
 * @result x if p x, otherwise until p f (f x)
 */
until			:: !(a -> .Bool) (a -> a) 	 a 	->  a

/**
 * Apply a function a number of times.
 *
 * @param The number of iterations n
 * @param The function
 * @param The initial value
 * @result The result of applying the function n times to the initial value
 */
iter			:: !Int 		 (.a -> .a) .a	-> .a
85 86 87

//	Some handy functions for transforming unique states:

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 * Iterate a list of state functions.
 *
 * @param The functions
 * @param The initial state
 * @result The final state
 */
seq				:: ![.(.s -> .s)] .s -> .s

/**
 * Iterate a list of state functions with result
 *
 * @param The functions
 * @param The initial state
 * @result A list of results from the state function and the final state
 */
seqList			:: ![St .s .a] .s -> ([.a],.s)
105

106 107 108
/**
 * A function that updates a state and produces a result.
 */
109 110 111 112
:: St s a :== s -> *(a,s)

// monadic style:

113 114 115 116 117
/**
 * Monadic bind for the {{`St`}} type.
 * @type w:(St .s .a) v:(.a -> .(St .s .b)) -> u:(St .s .b), [u <= v, u <= w]
 */
(`bind`) infix 0
118 119
(`bind`) f g :== \st0 -> let (r,st1) = f st0 in g r st1

120 121 122 123
/**
 * Monadic return for the {{`St`}} type.
 * @type u:a -> u:(St .s u:a)
 */
124
return r :== \s -> (r,s)