StdTuple.dcl 2.37 KB
Newer Older
1 2
definition module StdTuple

Camil Staps's avatar
Camil Staps committed
3 4 5 6
/**
 * Class instances and basic functions for tuples.
 */

7 8 9 10 11 12 13
// ****************************************************************************************
//	Concurrent Clean Standard Library Module Version 2.0
//	Copyright 1998 University of Nijmegen
// ****************************************************************************************

import StdClass

Camil Staps's avatar
Camil Staps committed
14 15 16 17 18 19 20
/**
 * The first element of a two-tuple.
 *
 * @type :: !(!.a,.b) -> .a
 * @param The tuple
 * @result The first element
 */
21
fst tuple :== t1 where (t1, _) = tuple
Camil Staps's avatar
Camil Staps committed
22 23 24 25 26 27 28 29

/**
 * The second element of a two-tuple.
 *
 * @type :: !(.a,!.b) -> .b
 * @param The tuple
 * @result The second element
 */
30 31
snd tuple :== t2 where (_, t2) = tuple

Camil Staps's avatar
Camil Staps committed
32 33 34 35 36 37 38
/**
 * The first element of a three-tuple.
 *
 * @type :: !(!.a,.b,.c) -> .a
 * @param The tuple
 * @result The first element
 */
39
fst3 tuple :== t1 where (t1, _, _) = tuple
Camil Staps's avatar
Camil Staps committed
40 41 42 43 44 45 46 47

/**
 * The second element of a three-tuple.
 *
 * @type :: !(.a,!.b,.c) -> .b
 * @param The tuple
 * @result The second element
 */
48
snd3 tuple :== t2 where (_, t2, _) = tuple
Camil Staps's avatar
Camil Staps committed
49 50 51 52 53 54 55 56

/**
 * The third element of a three-tuple.
 *
 * @type :: !(.a,.b,!.c) -> .c
 * @param The tuple
 * @result The third element
 */
57 58 59 60 61 62 63 64
thd3 tuple :== t3 where (_, _, t3) = tuple

instance == (a,b)   | Eq a & Eq b
instance == (a,b,c) | Eq a & Eq b & Eq c

instance <  (a,b)   | Ord a & Ord b
instance <  (a,b,c) | Ord a & Ord b & Ord c

Camil Staps's avatar
Camil Staps committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * Apply functions to both elements of a two-tuple.
 *
 * @param A tuple of functions (f,g)
 * @param A tuple of values (x,y)
 * @result The tuple of the result of the applications (f x,g y)
 */
app2 	:: !(.(.a -> .b),.(.c -> .d)) !(.a,.c) -> (.b,.d)

/**
 * Apply functions to all elements of a three-tuple.
 *
 * @param A tuple of functions (f,g,h)
 * @param A tuple of values (x,y,z)
 * @result The tuple of the result of the applications (f x,g y,h z)
 */
81 82
app3 	:: !(.(.a -> .b),.(.c -> .d),.(.e -> .f)) !(.a,.c,.e) -> (.b,.d,.f)

Camil Staps's avatar
Camil Staps committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**
 * Apply a function that takes a two-tuple to two separate values.
 *
 * @param A function f
 * @param The first element of the parameter tuple x
 * @param The second element of the parameter tuple y
 * @result f applied to (x,y)
 */
curry	:: !.((.a,.b) -> .c) .a .b -> .c

/**
 * Apply a function for two separate values to a two-tuple.
 *
 * @param A function f
 * @param A tuple of parameters (x,y)
 * @result The application f x y.
 */
uncurry :: !.(.a -> .(.b -> .c)) !(.a,.b) -> .c