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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
implementation module basic
// $Id$
/*
Basic definitions
=================
Description
-----------
Basic types and functions.
*/
import StdEnv
/*
Implementation
--------------
*/
//:: Optional t = Absent | Present t
// Now using Optional type from cocl's general module
from general import Optional,No,Yes,--->
instance == (Optional a) | == a
where (==) No No = True
(==) (Yes x1) (Yes x2) = x1==x2
(==) _ _ = False
// Adjust a function for a single argument
adjust :: !arg res (arg->res) !arg -> res | == arg
adjust a r f x
| x==a = r
= f x
// Claim a list of nodes from a heap
claim :: ![.param] u:[.cell] -> ([.cell],v:[.cell]), [u<=v]
claim [] heap = ([],heap)
claim [pnode:pnodes] [snode:heap]
= ([snode:snodes],heap`)
where (snodes,heap`) = claim pnodes heap
claim pnodes emptyheap = abort "claim: out of heap" // Just in case. Should be used with an infinite heap.
/* Depthfirst collects results of a function (called process), applied to a
given list of inputs and other inputs which are generated from the
results recursively, and so on. Duplicates are removed. */
depthfirst :: (res->.[elem]) (elem->res) !.[elem] -> .[res] | == elem
depthfirst generate process xs
= snd (collect xs ([],[]))
where collect [] seenrest = seenrest
collect [x:xs] seenrest
| isMember x seen
= collect xs seenrest
= (seen``,[y:rest``])
where (seen`,rest``) = collect (generate y) ([x:seen],rest`)
(seen``,rest`) = collect xs ( seen`,rest)
y = process x
(seen,rest) = seenrest
// `Disjoint xs ys' checks whether xs and ys are disjoint.
disjoint :: .[elem] !.[elem] -> Bool | == elem
disjoint xs ys = not (or (map (flip isMember xs) ys))
eqlen :: ![.elem1] ![.elem2] -> .Bool
eqlen [x:xs] [y:ys] = eqlen xs ys
eqlen [] [] = True
eqlen xs ys = False
// Extend a function using the elements of a mapping
extendfn :: [(src,dst)] (src->dst) src -> dst | == src
extendfn mapping f x = foldmap id (f x) mapping x
// `Foldlm' is a combination of foldl and map.
foldlm :: ((.collect,.elem) -> (.collect,.elem`)) !(.collect,![.elem]) -> (.collect,[.elem`])
foldlm f (l,[]) = (l,[])
foldlm f (l,[m:ms])
= (l``,[m`:ms`])
where (l`,m`) = f (l,m)
(l``,ms`) = foldlm f (l`,ms)
foldlr :: (.elem -> .((.lrinfo,.rlinfo) -> (.lrinfo,.rlinfo))) !(.lrinfo,.rlinfo) ![.elem] -> (.lrinfo,.rlinfo)
foldlr f lr []
= lr
foldlr f lr [x:xs]
= (l``,r``)
where (l``,r`) = foldlr f (l`,r) xs
(l`,r``) = f x (l,r`)
(l,r) = lr
foldmap :: (x:res -> w:res`) w:res` -> u:(![(arg,x:res)] -> v:(arg -> w:res`)) | == arg, [v u <= w, v <= x]
foldmap f d
= foldr foldmap` (const d)
where foldmap` xy g v
= if (x==v) (f y) (g v)
where (x,y) = xy
foldoptional :: .res .(.t -> .res) !(Optional .t) -> .res
foldoptional no yes No = no
foldoptional no yes (Yes x) = yes x
force :: !.a .b -> .b
force x y = y
forget :: val -> .(![.(val,res)] -> .[(val,res)]) | == val
forget x = filter (neq x o fst)
neq x y = x <> y
inccounter :: a (a->b) a -> b | == a & +,one b
inccounter m f n = if (n==m) (f n+one) (f n)
indent :: .String -> .([.String] -> .[String])
indent first = map2 (+++) [first:repeat (createArray (size first) ' ')]
map2 :: (.a -> .(.b -> .c)) ![.a] [.b] -> [.c]
map2 f [x:xs] [y:ys] = [f x y:map2 f xs ys]
map2 f _ _ = []
// `Identifiers' is the list of all identifiers
identifiers :: [String]
identifiers =: map toString (tl (kleene ['abcdefghijklmnopqrstuvwxyz']))
// `Intersect xs ys' is the intersection of list `ys' with list `xs'.
intersect :: ![elem] [elem] -> .[elem] | == elem
intersect [] _ = []
intersect [y:ys] xs = elim (cons y o intersect ys) (intersect ys xs) y xs
// Elim removes a given element from a list.
// There are two continuations, one for failure and one for success.
elim :: .(v:[elem] -> .res) .res elem u:[elem] -> .res | == elem, [u <= v]
elim found notfound y []
= notfound
elim found notfound y [x:xs]
| x==y
= found xs
= elim (found o cons x) notfound y xs
// Cons prepends an element to a list
cons :: .elem u:[.elem] -> v:[.elem], [u <= v]
cons x xs = [x:xs]
// `Join x xss' is the join of the list of lists `xss', separated by `x'.
join :: a ![.[a]] -> .[a]
join sep [] = []
join sep [x:xs] = x++flatten (map (cons sep) xs)
/* `Kleene xs' determines the kleene closure of the list `xs' of symbols,
i.e. all strings over that list in standard order. The implementation
is designed for maximum sharing.
*/
kleene :: !.[symbol] -> .[[symbol]]
kleene [] = [[]]
kleene symbols
= flatten (iterate prefix [[]])
where prefix strings
= flatten (map appendstrings symbols)
where appendstrings symbol = map (cons symbol) strings
// Lazy variant of the predefined abort function
error :: .String -> .a
error message = abort message
// Determine the string representation of a list
listToString :: [a] -> String | toString a
listToString xs = showlist toString xs
lookup :: u:([(arg,w:res)] -> v:(arg -> w:res)) | == arg, [v u <= w]
lookup = foldmap id (abort "lookup: not found")
pairwith :: .(arg -> .res) arg -> (arg,.res)
pairwith f x = (x,f x)
plookup :: .(arg -> String) ![(arg,.res)] arg -> .res | == arg
plookup showa tbl a = foldmap id (abort (showa a+++": not found")) tbl a
power :: !Int (.t -> .t) -> .(.t -> .t)
power n f
| n <= 0
= id
= f o power (n-1) f
printoptional :: .(.t -> String) !(Optional .t) -> String
printoptional printa No = ""
printoptional printa (Yes a) = printa a
proc :: .((w:elem -> .(.res -> .res)) -> v:(![w:elem] -> u:(.res -> .res))), [u <= v, u <= w]
proc = flip o foldr
mapfst :: v:(.a -> .b) -> u:((.a,.c) -> (.b,.c)), [u <= v]
mapfst f = app2 (f,id)
mapfst3 :: v:(.a -> .b) -> u:((.a,.c,.d) -> (.b,.c,.d)), [u <= v]
mapfst3 f = app3 (f,id,id)
maphd :: .(.a -> .a) !u:[.a] -> v:[.a], [u <= v]
maphd f [] = []
maphd f [x:xs] = [f x:xs]
mapoptional :: .(.a -> .b) !(Optional .a) -> Optional .b
mapoptional f No = No
mapoptional f (Yes x) = Yes (f x)
mappair :: .(.a -> .b) .(.c -> .d) !(.a,.c) -> (.b,.d)
mappair f g (x,y) = (f x,g y)
mapsnd :: v:(.a -> .b) -> u:((.c,.a) -> (.c,.b)), [u <= v]
mapsnd f = app2 (id,f)
mapsnd3 :: v:(.a -> .b) -> u:((.c,.a,.d) -> (.c,.b,.d)), [u <= v]
mapsnd3 f = app3 (id,f,id)
maptl :: .(x:[.a] -> u:[.a]) !w:[.a] -> v:[.a], [u <= v, w <= x]
maptl f [] = []
maptl f [x:xs] = [x:f xs]
maptriple :: x:(.a -> .b) w:(.c -> .d) v:(.e -> .f) -> u:((.a,.c,.e) -> (.b,.d,.f)), [u <= v, u <= w, u <= x]
maptriple f g h = app3 (f,g,h)
// String representation of line terminator
nl :: String
nl =: "\n"
partition :: (a -> b) (a -> .c) -> .(!.[a] -> [(b,[.c])]) | == b
partition f g
= h
where h [] = []
h [x:xs]
= [((r,[g x:ins])):h outs]
where ins = [g x\\x<-xs|f x==r]
outs = [x\\x<-xs|f x<>r]
r = f x
relimg :: ![(a,.b)] a -> [.b] | == a
relimg rel x` = [y\\(x,y)<-rel|x==x`]
remap :: a b [.(a,b)] -> .[(a,b)] | == a
remap x y xys = [(x,y):forget x xys]
// A variant of foldl that is strict in its accumulator
sfoldl :: (.a -> .(.b -> .a)) !.a [.b] -> .a
sfoldl f a xxs
#! a = a
= case xxs
of [] -> a
[x:xs] -> sfoldl f (f a x) xs
shorter :: ![.a] [.b] -> .Bool
shorter [] yys = False
shorter [x:xs] [] = True
shorter [x:xs] [y:ys] = shorter xs ys
showbool :: .(!.Bool -> a) | fromBool a
showbool = fromBool
showlist :: (.elem -> .String) ![.elem] -> String
showlist showa xs
= "["+++inner xs+++"]"
where inner [] = ""
inner [x:xs] = showa x+++continue xs
continue [] = ""
continue [x:xs] = ","+++showa x+++continue xs
showoptional :: .(.a -> .String) !(Optional .a) -> String
showoptional showa No = "No"
showoptional showa (Yes a) = "(Yes "+++showa a+++")"
showpair :: !.(.a -> .String) !.(.b -> .String) !(.a,.b) -> String
showpair showa showb (a,b) = "("+++showa a+++","+++showb b+++")"
showstring :: .(!.String -> a) | fromString a
showstring = fromString
showtriple :: !.(.a -> .String) !.(.b -> .String) !.(.c -> .String) !(.a,.b,.c) -> String
showtriple showa showb showc (a,b,c) = "("+++showa a+++","+++showb b+++","+++showc c+++")"
split :: a -> .(.[a] -> [.[a]]) | == a
split sep
= uncurry cons o spl
where spl [] = ([],[])
spl [x:xs]
| x==sep
= ([],[ys:yss])
= ([x:ys],yss)
where (ys,yss) = spl xs
// `Stub modulename functionname message' aborts with a explanatory message
stub :: .String .String .String -> .a
stub modulename functionname message
= abort (modulename+++": "+++functionname+++": "+++message)
superset :: .[a] -> .(.[a] -> Bool) | == a
superset set = isEmpty o ((--) set)
zipwith :: (.a .b->.c) ![.a] [.b] -> [.c]
zipwith f xs ys = [f x y \\ x<-xs & y<-ys]
// Strict version of --->, which evaluates its lhs first
(<---) infix :: !.a !b -> .a | <<< b
(<---) value message = value ---> message
($) infixr :: !.a .b -> .b
($) x y = y
// List subtraction (lazier than removeMembers)
(--) infixl :: !.[elem] .[elem] -> .[elem] | == elem
(--) [] ys = []
(--) [x:xs] ys = f maybeeqs
where (noteqs,maybeeqs) = span ((<>)x) ys
f [] = [x:xs--noteqs] // x wasn't in ys
f [y:ys] = xs--(noteqs++ys) // x==y
(writeList) infixl :: !*File [a] -> .File | <<< a
(writeList) file [] = file
(writeList) file [x:xs]
= file <<< x <<< nl writeList xs
printlist :: (elem->String) String [elem] *File -> .File
printlist showelem indent [] file
= file
printlist showelem indent [x:xs] file
= printlist showelem indent xs (file <<< indent <<< showelem x <<< nl)
|