StdTuple.icl 1.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
implementation module StdTuple

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

// Standard Functions on Tuples:

import StdClass, StdBool

// fst		:: !(!.a,.b) -> .a									// t1 of (t1,t2)
fst tuple :== t1 where (t1, _) = tuple
// snd		:: !(.a,!.b) -> .b									// t2 of (t1,t2)
snd tuple :== t2 where (_, t2) = tuple

// fst3	:: !(!.a,.b,.c) -> .a								// t1 of (t1,t2,t3)
fst3 tuple :== t1 where (t1, _, _) = tuple
// snd3	:: !(.a,!.b,.c) -> .b								// t2 of (t1,t2,t3)
snd3 tuple :== t2 where (_, t2, _) = tuple
// thd3	:: !(.a,.b,!.c) -> .c								// t3 of (t1,t2,t3)
thd3 tuple :== t3 where (_, _, t3) = tuple

instance ==	(a,b) |	Eq a & Eq b
	where
26
	(==) ::!(!a,b) !(!a,b) -> Bool	|	Eq a & Eq b
27 28 29 30
	(==) (x1,y1) (x2,y2) = x1==x2 && y1==y2
	
instance == (a,b,c)	| Eq a & Eq b & Eq c
	where
31
	(==) ::!(!a,b,c) !(!a,b,c) -> Bool	|	Eq a & Eq b & Eq c
32 33 34 35
	(==) (x1,y1,z1) (x2,y2,z2) = x1==x2 && y1==y2 && z1==z2
	
instance <	(a,b) |	Ord a & Ord b
	where
36
	(<) ::!(!a,b) !(!a,b) -> Bool	|	Ord a & Ord b
37 38 39 40 41 42 43 44 45 46
	(<) (x1,y1) (x2,y2)
		|	x1<x2
			=	True
		|	x1>x2
			=	False
		//	otherwise
			=	y1<y2
	
instance <	(a,b,c) | Ord a & Ord b & Ord c
	where
47
	(<) ::!(!a,b,c) !(!a,b,c) -> Bool	|	Ord a & Ord b & Ord c
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	(<) (x1,y1,z1) (x2,y2,z2)
		|	x1<x2
			=	True
		|	x1>x2	
			=	False
		//	otherwise
			=	(y1,z1) < (y2,z2)
	
curry::!.((.a,.b) -> .c) .a .b -> .c
curry f x y     =  f (x,y)

uncurry :: !.(.a -> .(.b -> .c)) !(.a,.b) -> .c;
uncurry f (x,y) = f x y

app2 :: !(.(.a -> .b),.(.c -> .d)) !(.a,.c) -> (.b,.d)
app2 (f1, f2) (x1,x2) = (f1 x1,f2 x2)

app3 :: !(.(.a -> .b),.(.c -> .d),.(.e -> .f)) !(.a,.c,.e) -> (.b,.d,.f)
app3 (f1, f2, f3) (x1,x2,x3) = (f1 x1,f2 x2,f3 x3)