StdTuple.dcl 2.55 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
/**
 * The first element of a two-tuple.
 *
Camil Staps's avatar
Camil Staps committed
17
 * @type !(!.a,.b) -> .a
Camil Staps's avatar
Camil Staps committed
18 19 20
 * @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

/**
 * The second element of a two-tuple.
 *
Camil Staps's avatar
Camil Staps committed
26
 * @type !(.a,!.b) -> .b
Camil Staps's avatar
Camil Staps committed
27 28 29
 * @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
/**
 * The first element of a three-tuple.
 *
Camil Staps's avatar
Camil Staps committed
35
 * @type !(!.a,.b,.c) -> .a
Camil Staps's avatar
Camil Staps committed
36 37 38
 * @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

/**
 * The second element of a three-tuple.
 *
Camil Staps's avatar
Camil Staps committed
44
 * @type !(.a,!.b,.c) -> .b
Camil Staps's avatar
Camil Staps committed
45 46 47
 * @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

/**
 * The third element of a three-tuple.
 *
Camil Staps's avatar
Camil Staps committed
53
 * @type !(.a,.b,!.c) -> .c
Camil Staps's avatar
Camil Staps committed
54 55 56
 * @param The tuple
 * @result The third element
 */
57 58
thd3 tuple :== t3 where (_, _, t3) = tuple

59 60
instance == (a,b)   | Eq a & Eq b			:: !(!a,b) !(!a,b) -> Bool | Eq a & Eq b
instance == (a,b,c) | Eq a & Eq b & Eq c	:: !(!a,b,c) !(!a,b,c) -> Bool | Eq a & Eq b & Eq c
61

62 63
instance <  (a,b)   | Ord a & Ord b			:: !(!a,b) !(!a,b) -> Bool | Ord a & Ord b
instance <  (a,b,c) | Ord a & Ord b & Ord c	:: !(!a,b,c) !(!a,b,c) -> Bool | Ord a & Ord b & Ord c
64

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