definition module StdFunc // **************************************************************************************** // Concurrent Clean Standard Library Module Version 2.0 // Copyright 1998 University of Nijmegen // **************************************************************************************** /** * The identity function. * * @param A value * @result The same value */ id :: !.a -> .a /** * 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 */ flip f a b :== f b a /** * 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 (o) f g :== \ x -> f (g x) /** * 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 // Some handy functions for transforming unique states: /** * 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) /** * A function that updates a state and produces a result. */ :: St s a :== s -> *(a,s) // monadic style: /** * 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 (`bind`) f g :== \st0 -> let (r,st1) = f st0 in g r st1 /** * Monadic return for the {{`St`}} type. * @type u:a -> u:(St .s u:a) */ return r :== \s -> (r,s)