basic.icl 7.81 KB
Newer Older
1
2
implementation module basic

3
4
// $Id$

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*

Basic definitions
=================

Description
-----------

Basic types and functions.

*/

import StdEnv

/*

Implementation
--------------

*/

26
27
28
//:: Optional t = Absent | Present t
// Now using Optional type from cocl's general module
from general import Optional,No,Yes
29

30
31
32
33
34
instance == (Optional a) | == a
 where (==) No No = True
       (==) (Yes x1) (Yes x2) = x1==x2
       (==) _ _ = False

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

// 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] (seen,rest)
          | isMember x seen
          =   collect xs (seen,rest)
          =   (seen``,[y:rest``])
              where (seen`,rest``) = collect (generate y) ([x:seen],rest`)
                    (seen``,rest`) = collect xs           (   seen`,rest)
                    y = process x

// `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

79
80
81
82
// 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

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// `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 (l,r) []
    = (l,r)
foldlr f (l,r) [x:xs]
    = (l``,r``)
      where (l``,r`) = foldlr f (l`,r) xs
            (l`,r``) = f x (l,r`)

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` (x,y) g v = if (x==v) (f y) (g v)

foldoptional :: .res .(.t -> .res) !(Optional .t) -> .res
105
106
foldoptional no yes No = no
foldoptional no yes (Yes x) = yes x
107
108
109
110
111

forget :: val -> .(![.(val,res)] -> .[(val,res)]) | == val
forget x = filter (neq x o fst)
neq x y = x <> y

112
113
114
inccounter :: a (a->b) a -> b | == a & +,one b
inccounter m f n = if (n==m) (f n+one) (f n)

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
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

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
178
179
printoptional printa  No     = ""
printoptional printa (Yes a) = printa a
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

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
195
196
mapoptional f No      = No
mapoptional f (Yes x) = Yes (f x)
197
198
199
200
201
202
203

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)

Vincent Zweije's avatar
Vincent Zweije committed
204
205
206
mapsnd3 :: v:(.a -> .b) -> u:((.c,.a,.d) -> (.c,.b,.d)), [u <= v]
mapsnd3 f = app3 (id,f,id)

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
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)

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]

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

showoptional :: .(.a -> .String) !(Optional .a) -> String
239
240
showoptional showa No      = "No"
showoptional showa (Yes a) = "(Yes "+++showa a+++")"
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

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

261
262
263
264
265
// `Stub modulename functionname message' aborts with a explanatory message
stub :: .String .String .String -> a
stub modulename functionname message
= abort (modulename+++": "+++functionname+++": "+++message)

266
267
superset :: .[a] -> .(.[a] -> Bool) | == a
superset set = isEmpty o (removeMembers set)