utilities.dcl 3.49 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
definition module utilities

from StdEnv import Eq, not, Ord, IncDec
import StdMisc, general
5

6
7
import _aconcat
   
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
8
9
10
11
/*
	For Strings
*/

12
13
14
//1.3
from StdString import String
//3.1
15

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
16
17
18
19
20
21
22
23
24
25
26
27
28
stringToCharList	:: !String -> [Char]
charListToString	:: ![Char] -> String
revCharListToString	:: !Int ![Char] -> String

isUpperCaseName :: ! String -> Bool
isLowerCaseName :: ! String -> Bool
isFunnyIdName 	:: ! String -> Bool
isSpecialChar	:: ! Char	-> Bool

/*
	For Lists
*/

Pieter Koopman's avatar
Pieter Koopman committed
29
30
isNotEmpty :: ![a] -> Bool

clean's avatar
clean committed
31
32
//mapSt :: !(.a -> (.st -> (.c,.st))) ![.a] !.st -> (![.c],!.st)

33
mapSt f l s :== map_st l s
clean's avatar
clean committed
34
where
35
	map_st [x : xs] s
clean's avatar
clean committed
36
	 	# (x, s) = f x s
37
		  mapSt_result = map_st xs s
clean's avatar
clean committed
38
39
40
		  (xs, _) = mapSt_result
		#! s = second_of_2_tuple mapSt_result
		= ([x : xs], s)
41
	map_st [] s
clean's avatar
clean committed
42
43
44
45
46
	 	= ([], s)
	
second_of_2_tuple t :== e2
	where
		(_,e2) = t
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur 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
85
86
87

app2St :: !(!.(.a -> .(.st -> (.c,.st))),!.(.e -> .(.st -> (.f,.st)))) !(.a,.e) !.st -> (!(.c,.f),!.st)

mapAppendSt :: !(.a -> .(.b -> (.c,.b))) ![.a] !u:[.c] !.b -> !(!u:[.c],!.b)

strictMap :: !(.a -> .b) ![.a] -> [.b]

strictMapAppend :: !(.a -> .b) ![.a] !u:[.b] -> v:[.b], [u <= v]

mapAppend :: !(.a -> .b) ![.a] !u:[.b] -> u:[.b]

//zip2Append :: [.a] [.b] u:[w:(.a,.b)] -> v:[x:(.a,.b)], [w <= x, u <= v]

eqMerge :: ![a] ![a] -> [a] | Eq a

// foldl2 :: !(.c -> .(.a -> .(.b -> .c))) !.c ![.a] ![.b] -> .c
foldl2 op r l1 l2
	:== foldl2 r l1 l2
where
	foldl2 r [x : xs] [y : ys]
		= foldl2 (op r x y) xs ys
	foldl2 r [] []
		= r
//foldr2 :: !(.a -> .(.b -> .(.c -> .c))) !.c ![.a] ![.b] -> .c

foldr2 op r l1 l2
	:== foldr2 r l1 l2
where
	foldr2 r [x : xs] [y : ys]
		= op x y (foldr2 r xs ys)	
	foldr2 r [] []
		= r

fold2St op l1 l2 st
	:== fold_st2 l1 l2 st
where
	fold_st2 [x : xs] [y : ys] st
		= op x y (fold_st2 xs ys st)	
	fold_st2 [] [] st
		= st
	fold_st2 [] ys st
88
		= abort ("fold_st2: second argument list contains more elements")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
89
	fold_st2 xs [] st
90
		= abort ("fold_st2: first argument list contains more elements")
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
91

92
93
94
95
96
97
98
99
unsafeFold2St op l1 l2 st
	:== ufold_st2 l1 l2 st
where
	ufold_st2 [x : xs] [y : ys] st
		= op x y (ufold_st2 xs ys st)	
	ufold_st2 _ _ st
		= st

Sjaak Smetsers's avatar
Sjaak Smetsers committed
100

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
101
102
103
104
105
106
// foldSt :: !(.a -> .(.st -> .st)) ![.a] !.st -> .st
foldSt op l st :== fold_st l st
	where
		fold_st [] st		= st
		fold_st [a:x] st	= fold_st x (op a st)

107
// iFoldSt :: (Int -> .(.b -> .b)) !Int !Int .b -> .b
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
108
109
110
111
112
113
114
115
116
117
iFoldSt op fr to st :== i_fold_st fr to st
	where
		i_fold_st fr to st
			| fr >= to
				= st
				= i_fold_st (inc fr) to (op fr st)

iterateSt op st :== iterate_st op st
	where
		iterate_st op st
118
			# (continue, st) = op st
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
119
120
121
122
			| continue
				= iterate_st op st
				= st

123
124
125
126
mapFilterYesSt f l st
	:== map_filter_yes_st l st
  where
	map_filter_yes_st [] st
127
		#! st = st
128
129
130
131
		= ([], st)
	map_filter_yes_st [h:t] st
		#! (opt_f_h , st) = f h st
		   (t2, st) = map_filter_yes_st t st
132
		   (f_h_t2, _) = optCons opt_f_h t2
133
134
135
136
137
138
139
		   st = st
		= (f_h_t2, st)
				
iMapFilterYesSt f fr to st 
	:== i_map_filter_yes_st fr to st
  where
	i_map_filter_yes_st fr to st
140
		#! st = st
141
142
143
144
		| fr >= to
			= ([], st)
		#! (opt_f_fr, st) = f fr st
		   (t, st) = i_map_filter_yes_st (inc fr) to st
145
		   (f_fr_t2, _) = optCons opt_f_fr t
146
147
148
		   st = st
		= (f_fr_t2, st)
				
149
optCons :: !(Optional .a) !u:[.a] -> (!v:[.a], !Int) ,[u <= v]
150

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
151
152
153
revAppend	:: ![a] ![a] -> [a]	//	Reverse the list using the second argument as accumulator.
revMap :: !(.a -> .b) ![.a] !u:[.b] -> u:[.b]

154
155
156
157
158
:: Bag x = Empty | Single !x | Pair !(Bag x) !(Bag x)

uniqueBagToList :: !*(Bag x) -> [x] // exploits reuse of unique nodes (if compiled with that option)
bagToList :: !(Bag x) -> [x]
isEmptyBag :: !(Bag x) -> Bool