author | paulson |
Sat, 01 Nov 1997 13:01:57 +0100 | |
changeset 4063 | 0b19014b9155 |
parent 4046 | f89dbf002604 |
child 4102 | f746af27164b |
permissions | -rw-r--r-- |
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
1 |
(* Title: Pure/library.ML |
0 | 2 |
ID: $Id$ |
233 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright 1992 University of Cambridge |
5 |
||
233 | 6 |
Basic library: functions, options, pairs, booleans, lists, integers, |
7 |
strings, lists as sets, association lists, generic tables, balanced trees, |
|
2506 | 8 |
orders, input / output, timing, filenames, misc functions. |
0 | 9 |
*) |
10 |
||
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
11 |
infix |> ~~ \ \\ orelf ins ins_string ins_int orf andf prefix upto downto |
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
12 |
mem mem_int mem_string union union_int union_string |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
13 |
inter inter_int inter_string subset subset_int subset_string subdir_of; |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
14 |
|
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
15 |
|
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
16 |
structure Library = |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
17 |
struct |
0 | 18 |
|
233 | 19 |
(** functions **) |
0 | 20 |
|
233 | 21 |
(*handy combinators*) |
22 |
fun curry f x y = f (x, y); |
|
23 |
fun uncurry f (x, y) = f x y; |
|
24 |
fun I x = x; |
|
25 |
fun K x y = x; |
|
0 | 26 |
|
380 | 27 |
(*reverse apply*) |
410 | 28 |
fun (x |> f) = f x; |
380 | 29 |
|
233 | 30 |
(*combine two functions forming the union of their domains*) |
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
31 |
fun (f orelf g) = fn x => f x handle Match => g x; |
0 | 32 |
|
233 | 33 |
(*application of (infix) operator to its left or right argument*) |
34 |
fun apl (x, f) y = f (x, y); |
|
35 |
fun apr (f, y) x = f (x, y); |
|
0 | 36 |
|
233 | 37 |
(*functional for pairs*) |
38 |
fun pairself f (x, y) = (f x, f y); |
|
0 | 39 |
|
233 | 40 |
(*function exponentiation: f(...(f x)...) with n applications of f*) |
41 |
fun funpow n f x = |
|
42 |
let fun rep (0, x) = x |
|
43 |
| rep (n, x) = rep (n - 1, f x) |
|
44 |
in rep (n, x) end; |
|
160 | 45 |
|
46 |
||
47 |
||
2471 | 48 |
(** stamps **) |
49 |
||
50 |
type stamp = unit ref; |
|
51 |
val stamp: unit -> stamp = ref; |
|
52 |
||
53 |
||
54 |
||
233 | 55 |
(** options **) |
0 | 56 |
|
57 |
datatype 'a option = None | Some of 'a; |
|
58 |
||
59 |
exception OPTION of string; |
|
60 |
||
61 |
fun the (Some x) = x |
|
62 |
| the None = raise OPTION "the"; |
|
63 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
64 |
fun if_none None y = y |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
65 |
| if_none (Some x) _ = x; |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
66 |
|
0 | 67 |
fun is_some (Some _) = true |
68 |
| is_some None = false; |
|
69 |
||
70 |
fun is_none (Some _) = false |
|
71 |
| is_none None = true; |
|
72 |
||
233 | 73 |
fun apsome f (Some x) = Some (f x) |
74 |
| apsome _ None = None; |
|
0 | 75 |
|
4046
f89dbf002604
added merge_opts: ('a * 'a -> 'a) -> 'a option * 'a option -> 'a option;
wenzelm
parents:
3973
diff
changeset
|
76 |
fun merge_opts _ (None, None) = None |
f89dbf002604
added merge_opts: ('a * 'a -> 'a) -> 'a option * 'a option -> 'a option;
wenzelm
parents:
3973
diff
changeset
|
77 |
| merge_opts _ (some as Some _, None) = some |
f89dbf002604
added merge_opts: ('a * 'a -> 'a) -> 'a option * 'a option -> 'a option;
wenzelm
parents:
3973
diff
changeset
|
78 |
| merge_opts _ (None, some as Some _) = some |
f89dbf002604
added merge_opts: ('a * 'a -> 'a) -> 'a option * 'a option -> 'a option;
wenzelm
parents:
3973
diff
changeset
|
79 |
| merge_opts merge (Some x, Some y) = Some (merge (x, y)); |
f89dbf002604
added merge_opts: ('a * 'a -> 'a) -> 'a option * 'a option -> 'a option;
wenzelm
parents:
3973
diff
changeset
|
80 |
|
233 | 81 |
(** pairs **) |
82 |
||
83 |
fun pair x y = (x, y); |
|
84 |
fun rpair x y = (y, x); |
|
85 |
||
86 |
fun fst (x, y) = x; |
|
87 |
fun snd (x, y) = y; |
|
88 |
||
89 |
fun eq_fst ((x1, _), (x2, _)) = x1 = x2; |
|
90 |
fun eq_snd ((_, y1), (_, y2)) = y1 = y2; |
|
91 |
||
92 |
fun swap (x, y) = (y, x); |
|
93 |
||
94 |
(*apply the function to a component of a pair*) |
|
95 |
fun apfst f (x, y) = (f x, y); |
|
96 |
fun apsnd f (x, y) = (x, f y); |
|
97 |
||
98 |
||
99 |
||
100 |
(** booleans **) |
|
101 |
||
102 |
(* equality *) |
|
103 |
||
104 |
fun equal x y = x = y; |
|
105 |
fun not_equal x y = x <> y; |
|
106 |
||
107 |
||
108 |
(* operators for combining predicates *) |
|
109 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
110 |
fun (p orf q) = fn x => p x orelse q x; |
233 | 111 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
112 |
fun (p andf q) = fn x => p x andalso q x; |
233 | 113 |
|
114 |
fun notf p x = not (p x); |
|
0 | 115 |
|
233 | 116 |
|
117 |
(* predicates on lists *) |
|
118 |
||
119 |
fun orl [] = false |
|
120 |
| orl (x :: xs) = x orelse orl xs; |
|
121 |
||
122 |
fun andl [] = true |
|
123 |
| andl (x :: xs) = x andalso andl xs; |
|
124 |
||
3246 | 125 |
(*Several object-logics declare theories named List or Option, hiding the |
126 |
eponymous basis library structures.*) |
|
4063 | 127 |
structure Basis_Library = |
128 |
struct |
|
129 |
structure List = List |
|
130 |
and Option = Option |
|
131 |
end; |
|
132 |
||
2271 | 133 |
|
233 | 134 |
(*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) |
135 |
fun exists (pred: 'a -> bool) : 'a list -> bool = |
|
136 |
let fun boolf [] = false |
|
137 |
| boolf (x :: xs) = pred x orelse boolf xs |
|
138 |
in boolf end; |
|
139 |
||
140 |
(*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*) |
|
141 |
fun forall (pred: 'a -> bool) : 'a list -> bool = |
|
142 |
let fun boolf [] = true |
|
143 |
| boolf (x :: xs) = pred x andalso boolf xs |
|
144 |
in boolf end; |
|
0 | 145 |
|
233 | 146 |
|
380 | 147 |
(* flags *) |
148 |
||
149 |
fun set flag = (flag := true; true); |
|
150 |
fun reset flag = (flag := false; false); |
|
151 |
fun toggle flag = (flag := not (! flag); ! flag); |
|
152 |
||
2978 | 153 |
fun setmp flag value f x = |
2958 | 154 |
let |
155 |
val orig_value = ! flag; |
|
156 |
fun return y = (flag := orig_value; y); |
|
157 |
in |
|
158 |
flag := value; |
|
159 |
return (f x handle exn => (return (); raise exn)) |
|
160 |
end; |
|
161 |
||
380 | 162 |
|
233 | 163 |
|
164 |
(** lists **) |
|
165 |
||
166 |
exception LIST of string; |
|
167 |
||
168 |
fun null [] = true |
|
169 |
| null (_ :: _) = false; |
|
170 |
||
171 |
fun hd [] = raise LIST "hd" |
|
172 |
| hd (x :: _) = x; |
|
173 |
||
174 |
fun tl [] = raise LIST "tl" |
|
175 |
| tl (_ :: xs) = xs; |
|
176 |
||
177 |
fun cons x xs = x :: xs; |
|
178 |
||
179 |
||
180 |
(* fold *) |
|
181 |
||
182 |
(*the following versions of fold are designed to fit nicely with infixes*) |
|
0 | 183 |
|
233 | 184 |
(* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn |
185 |
for operators that associate to the left (TAIL RECURSIVE)*) |
|
186 |
fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a = |
|
187 |
let fun itl (e, []) = e |
|
188 |
| itl (e, a::l) = itl (f(e, a), l) |
|
189 |
in itl end; |
|
190 |
||
191 |
(* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) |
|
192 |
for operators that associate to the right (not tail recursive)*) |
|
193 |
fun foldr f (l, e) = |
|
194 |
let fun itr [] = e |
|
195 |
| itr (a::l) = f(a, itr l) |
|
196 |
in itr l end; |
|
197 |
||
198 |
(* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n-1] @ xn)) |
|
199 |
for n > 0, operators that associate to the right (not tail recursive)*) |
|
200 |
fun foldr1 f l = |
|
201 |
let fun itr [x] = x (* FIXME [] case: elim warn (?) *) |
|
202 |
| itr (x::l) = f(x, itr l) |
|
203 |
in itr l end; |
|
204 |
||
205 |
||
206 |
(* basic list functions *) |
|
207 |
||
208 |
(*length of a list, should unquestionably be a standard function*) |
|
209 |
local fun length1 (n, []) = n (*TAIL RECURSIVE*) |
|
210 |
| length1 (n, x :: xs) = length1 (n + 1, xs) |
|
211 |
in fun length l = length1 (0, l) end; |
|
212 |
||
213 |
(*take the first n elements from a list*) |
|
214 |
fun take (n, []) = [] |
|
215 |
| take (n, x :: xs) = |
|
216 |
if n > 0 then x :: take (n - 1, xs) else []; |
|
217 |
||
218 |
(*drop the first n elements from a list*) |
|
219 |
fun drop (n, []) = [] |
|
220 |
| drop (n, x :: xs) = |
|
221 |
if n > 0 then drop (n - 1, xs) else x :: xs; |
|
0 | 222 |
|
233 | 223 |
(*return nth element of a list, where 0 designates the first element; |
224 |
raise EXCEPTION if list too short*) |
|
225 |
fun nth_elem NL = |
|
226 |
(case drop NL of |
|
227 |
[] => raise LIST "nth_elem" |
|
228 |
| x :: _ => x); |
|
229 |
||
230 |
(*last element of a list*) |
|
231 |
fun last_elem [] = raise LIST "last_elem" |
|
232 |
| last_elem [x] = x |
|
233 |
| last_elem (_ :: xs) = last_elem xs; |
|
234 |
||
3762 | 235 |
(*rear decomposition*) |
236 |
fun split_last [] = raise LIST "split_last" |
|
237 |
| split_last [x] = ([], x) |
|
238 |
| split_last (x :: xs) = apfst (cons x) (split_last xs); |
|
239 |
||
240 |
||
233 | 241 |
(*find the position of an element in a list*) |
242 |
fun find (x, ys) = |
|
243 |
let fun f (y :: ys, i) = if x = y then i else f (ys, i + 1) |
|
244 |
| f (_, _) = raise LIST "find" |
|
245 |
in f (ys, 0) end; |
|
246 |
||
247 |
(*flatten a list of lists to a list*) |
|
248 |
fun flat (ls: 'c list list) : 'c list = foldr (op @) (ls, []); |
|
249 |
||
250 |
||
251 |
(*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates |
|
252 |
(proc x1; ...; proc xn) for side effects*) |
|
253 |
fun seq (proc: 'a -> unit) : 'a list -> unit = |
|
254 |
let fun seqf [] = () |
|
255 |
| seqf (x :: xs) = (proc x; seqf xs) |
|
256 |
in seqf end; |
|
257 |
||
258 |
||
259 |
(*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) |
|
260 |
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs |
|
261 |
| separate _ xs = xs; |
|
262 |
||
263 |
(*make the list [x, x, ..., x] of length n*) |
|
264 |
fun replicate n (x: 'a) : 'a list = |
|
265 |
let fun rep (0, xs) = xs |
|
266 |
| rep (n, xs) = rep (n - 1, x :: xs) |
|
267 |
in |
|
268 |
if n < 0 then raise LIST "replicate" |
|
269 |
else rep (n, []) |
|
270 |
end; |
|
271 |
||
272 |
||
273 |
(* filter *) |
|
274 |
||
275 |
(*copy the list preserving elements that satisfy the predicate*) |
|
276 |
fun filter (pred: 'a->bool) : 'a list -> 'a list = |
|
0 | 277 |
let fun filt [] = [] |
233 | 278 |
| filt (x :: xs) = if pred x then x :: filt xs else filt xs |
279 |
in filt end; |
|
0 | 280 |
|
281 |
fun filter_out f = filter (not o f); |
|
282 |
||
283 |
||
233 | 284 |
fun mapfilter (f: 'a -> 'b option) ([]: 'a list) = [] : 'b list |
285 |
| mapfilter f (x :: xs) = |
|
286 |
(case f x of |
|
287 |
None => mapfilter f xs |
|
288 |
| Some y => y :: mapfilter f xs); |
|
289 |
||
290 |
||
380 | 291 |
fun find_first _ [] = None |
292 |
| find_first pred (x :: xs) = |
|
293 |
if pred x then Some x else find_first pred xs; |
|
294 |
||
295 |
||
233 | 296 |
(* lists of pairs *) |
297 |
||
380 | 298 |
fun map2 _ ([], []) = [] |
299 |
| map2 f (x :: xs, y :: ys) = (f (x, y) :: map2 f (xs, ys)) |
|
300 |
| map2 _ _ = raise LIST "map2"; |
|
301 |
||
302 |
fun exists2 _ ([], []) = false |
|
303 |
| exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys) |
|
304 |
| exists2 _ _ = raise LIST "exists2"; |
|
305 |
||
306 |
fun forall2 _ ([], []) = true |
|
307 |
| forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys) |
|
308 |
| forall2 _ _ = raise LIST "forall2"; |
|
309 |
||
233 | 310 |
(*combine two lists forming a list of pairs: |
311 |
[x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) |
|
312 |
fun [] ~~ [] = [] |
|
313 |
| (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) |
|
314 |
| _ ~~ _ = raise LIST "~~"; |
|
315 |
||
316 |
||
317 |
(*inverse of ~~; the old 'split': |
|
318 |
[(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) |
|
319 |
fun split_list (l: ('a * 'b) list) = (map #1 l, map #2 l); |
|
320 |
||
321 |
||
322 |
(* prefixes, suffixes *) |
|
323 |
||
324 |
fun [] prefix _ = true |
|
325 |
| (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys) |
|
326 |
| _ prefix _ = false; |
|
327 |
||
328 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., x(i-1)], [xi, ..., xn]) |
|
329 |
where xi is the first element that does not satisfy the predicate*) |
|
330 |
fun take_prefix (pred : 'a -> bool) (xs: 'a list) : 'a list * 'a list = |
|
331 |
let fun take (rxs, []) = (rev rxs, []) |
|
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
332 |
| take (rxs, x :: xs) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
333 |
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) |
233 | 334 |
in take([], xs) end; |
335 |
||
336 |
(* [x1, ..., xi, ..., xn] ---> ([x1, ..., xi], [x(i+1), ..., xn]) |
|
337 |
where xi is the last element that does not satisfy the predicate*) |
|
338 |
fun take_suffix _ [] = ([], []) |
|
339 |
| take_suffix pred (x :: xs) = |
|
340 |
(case take_suffix pred xs of |
|
341 |
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) |
|
342 |
| (prfx, sffx) => (x :: prfx, sffx)); |
|
343 |
||
344 |
||
345 |
||
346 |
(** integers **) |
|
347 |
||
2958 | 348 |
fun inc i = (i := ! i + 1; ! i); |
349 |
fun dec i = (i := ! i - 1; ! i); |
|
233 | 350 |
|
351 |
||
352 |
(* lists of integers *) |
|
353 |
||
354 |
(*make the list [from, from + 1, ..., to]*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
355 |
fun (from upto to) = |
233 | 356 |
if from > to then [] else from :: ((from + 1) upto to); |
357 |
||
358 |
(*make the list [from, from - 1, ..., to]*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
359 |
fun (from downto to) = |
233 | 360 |
if from < to then [] else from :: ((from - 1) downto to); |
361 |
||
362 |
(*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*) |
|
363 |
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1) |
|
364 |
| downto0 ([], n) = n = ~1; |
|
365 |
||
366 |
||
367 |
(* convert integers to strings *) |
|
368 |
||
369 |
(*expand the number in the given base; |
|
370 |
example: radixpand (2, 8) gives [1, 0, 0, 0]*) |
|
371 |
fun radixpand (base, num) : int list = |
|
372 |
let |
|
373 |
fun radix (n, tail) = |
|
374 |
if n < base then n :: tail |
|
375 |
else radix (n div base, (n mod base) :: tail) |
|
376 |
in radix (num, []) end; |
|
377 |
||
378 |
(*expands a number into a string of characters starting from "zerochar"; |
|
379 |
example: radixstring (2, "0", 8) gives "1000"*) |
|
380 |
fun radixstring (base, zerochar, num) = |
|
381 |
let val offset = ord zerochar; |
|
382 |
fun chrof n = chr (offset + n) |
|
383 |
in implode (map chrof (radixpand (base, num))) end; |
|
384 |
||
385 |
||
3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
386 |
val string_of_int = Int.toString; |
233 | 387 |
|
3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
388 |
fun string_of_indexname (a,0) = a |
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset
|
389 |
| string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; |
233 | 390 |
|
391 |
||
392 |
(** strings **) |
|
393 |
||
394 |
fun is_letter ch = |
|
395 |
ord "A" <= ord ch andalso ord ch <= ord "Z" orelse |
|
396 |
ord "a" <= ord ch andalso ord ch <= ord "z"; |
|
397 |
||
398 |
fun is_digit ch = |
|
399 |
ord "0" <= ord ch andalso ord ch <= ord "9"; |
|
400 |
||
401 |
(*letter or _ or prime (')*) |
|
402 |
fun is_quasi_letter "_" = true |
|
403 |
| is_quasi_letter "'" = true |
|
404 |
| is_quasi_letter ch = is_letter ch; |
|
405 |
||
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
406 |
(*white space: blanks, tabs, newlines, formfeeds*) |
233 | 407 |
val is_blank : string -> bool = |
3393 | 408 |
fn " " => true | "\t" => true | "\n" => true | "\^L" => true | "\160" => true |
3063 | 409 |
| _ => false; |
233 | 410 |
|
411 |
val is_letdig = is_quasi_letter orf is_digit; |
|
412 |
||
2196 | 413 |
(*printable chars*) |
414 |
fun is_printable c = ord c > ord " " andalso ord c <= ord "~"; |
|
415 |
||
233 | 416 |
|
417 |
(*lower all chars of string*) |
|
418 |
val to_lower = |
|
419 |
let |
|
420 |
fun lower ch = |
|
421 |
if ch >= "A" andalso ch <= "Z" then |
|
422 |
chr (ord ch - ord "A" + ord "a") |
|
423 |
else ch; |
|
424 |
in implode o (map lower) o explode end; |
|
425 |
||
426 |
||
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
427 |
(*enclose in brackets*) |
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
428 |
fun enclose lpar rpar str = lpar ^ str ^ rpar; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
429 |
|
233 | 430 |
(*simple quoting (does not escape special chars)*) |
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset
|
431 |
val quote = enclose "\"" "\""; |
233 | 432 |
|
433 |
(*space_implode "..." (explode "hello"); gives "h...e...l...l...o"*) |
|
434 |
fun space_implode a bs = implode (separate a bs); |
|
435 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
436 |
val commas = space_implode ", "; |
380 | 437 |
val commas_quote = commas o map quote; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
438 |
|
233 | 439 |
(*concatenate messages, one per line, into a string*) |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
440 |
val cat_lines = space_implode "\n"; |
233 | 441 |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
442 |
(*BAD_space_explode "." "h.e..l.lo"; gives ["h", "e", "l", "lo"]*) |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
443 |
fun BAD_space_explode sep s = |
1290 | 444 |
let fun divide [] "" = [] |
445 |
| divide [] part = [part] |
|
446 |
| divide (c::s) part = |
|
447 |
if c = sep then |
|
448 |
(if part = "" then divide s "" else part :: divide s "") |
|
449 |
else divide s (part ^ c) |
|
450 |
in divide (explode s) "" end; |
|
233 | 451 |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
452 |
(*space_explode "." "h.e..l.lo"; gives ["h", "e", "", "l", "lo"]*) |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
453 |
fun space_explode _ "" = [] |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
454 |
| space_explode sep str = |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
455 |
let |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
456 |
fun expl chs = |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
457 |
(case take_prefix (not_equal sep) chs of |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
458 |
(cs, []) => [implode cs] |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
459 |
| (cs, _ :: cs') => implode cs :: expl cs'); |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
460 |
in expl (explode str) end; |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
461 |
|
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
462 |
val split_lines = space_explode "\n"; |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
463 |
|
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
464 |
|
233 | 465 |
|
466 |
(** lists as sets **) |
|
467 |
||
468 |
(*membership in a list*) |
|
469 |
fun x mem [] = false |
|
470 |
| x mem (y :: ys) = x = y orelse x mem ys; |
|
0 | 471 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
472 |
(*membership in a list, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
473 |
fun (x:int) mem_int [] = false |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
474 |
| x mem_int (y :: ys) = x = y orelse x mem_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
475 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
476 |
(*membership in a list, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
477 |
fun (x:string) mem_string [] = false |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
478 |
| x mem_string (y :: ys) = x = y orelse x mem_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
479 |
|
0 | 480 |
(*generalized membership test*) |
233 | 481 |
fun gen_mem eq (x, []) = false |
482 |
| gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); |
|
483 |
||
484 |
||
485 |
(*insertion into list if not already there*) |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
486 |
fun (x ins xs) = if x mem xs then xs else x :: xs; |
0 | 487 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
488 |
(*insertion into list, optimized version for ints*) |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
489 |
fun (x ins_int xs) = if x mem_int xs then xs else x :: xs; |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
490 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
491 |
(*insertion into list, optimized version for strings*) |
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
492 |
fun (x ins_string xs) = if x mem_string xs then xs else x :: xs; |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
493 |
|
0 | 494 |
(*generalized insertion*) |
233 | 495 |
fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; |
496 |
||
497 |
||
498 |
(*union of sets represented as lists: no repetitions*) |
|
499 |
fun xs union [] = xs |
|
500 |
| [] union ys = ys |
|
501 |
| (x :: xs) union ys = xs union (x ins ys); |
|
0 | 502 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
503 |
(*union of sets, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
504 |
fun (xs:int list) union_int [] = xs |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
505 |
| [] union_int ys = ys |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
506 |
| (x :: xs) union_int ys = xs union_int (x ins_int ys); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
507 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
508 |
(*union of sets, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
509 |
fun (xs:string list) union_string [] = xs |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
510 |
| [] union_string ys = ys |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
511 |
| (x :: xs) union_string ys = xs union_string (x ins_string ys); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
512 |
|
0 | 513 |
(*generalized union*) |
233 | 514 |
fun gen_union eq (xs, []) = xs |
515 |
| gen_union eq ([], ys) = ys |
|
516 |
| gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys)); |
|
517 |
||
518 |
||
519 |
(*intersection*) |
|
520 |
fun [] inter ys = [] |
|
521 |
| (x :: xs) inter ys = |
|
522 |
if x mem ys then x :: (xs inter ys) else xs inter ys; |
|
523 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
524 |
(*intersection, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
525 |
fun ([]:int list) inter_int ys = [] |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
526 |
| (x :: xs) inter_int ys = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
527 |
if x mem_int ys then x :: (xs inter_int ys) else xs inter_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
528 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
529 |
(*intersection, optimized version for strings *) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
530 |
fun ([]:string list) inter_string ys = [] |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
531 |
| (x :: xs) inter_string ys = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
532 |
if x mem_string ys then x :: (xs inter_string ys) else xs inter_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
533 |
|
233 | 534 |
|
535 |
(*subset*) |
|
536 |
fun [] subset ys = true |
|
537 |
| (x :: xs) subset ys = x mem ys andalso xs subset ys; |
|
538 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
539 |
(*subset, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
540 |
fun ([]:int list) subset_int ys = true |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
541 |
| (x :: xs) subset_int ys = x mem_int ys andalso xs subset_int ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
542 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
543 |
(*subset, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
544 |
fun ([]:string list) subset_string ys = true |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
545 |
| (x :: xs) subset_string ys = x mem_string ys andalso xs subset_string ys; |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
546 |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
547 |
(*set equality for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
548 |
fun eq_set_string ((xs:string list), ys) = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
549 |
xs = ys orelse (xs subset_string ys andalso ys subset_string xs); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
550 |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
551 |
fun gen_subset eq (xs, ys) = forall (fn x => gen_mem eq (x, ys)) xs; |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
552 |
|
265 | 553 |
|
233 | 554 |
(*removing an element from a list WITHOUT duplicates*) |
555 |
fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x) |
|
556 |
| [] \ x = []; |
|
557 |
||
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
558 |
fun ys \\ xs = foldl (op \) (ys,xs); |
0 | 559 |
|
233 | 560 |
(*removing an element from a list -- possibly WITH duplicates*) |
561 |
fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs; |
|
562 |
||
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
563 |
fun gen_rems eq = foldl (gen_rem eq); |
233 | 564 |
|
565 |
||
566 |
(*makes a list of the distinct members of the input; preserves order, takes |
|
567 |
first of equal elements*) |
|
568 |
fun gen_distinct eq lst = |
|
569 |
let |
|
570 |
val memb = gen_mem eq; |
|
0 | 571 |
|
233 | 572 |
fun dist (rev_seen, []) = rev rev_seen |
573 |
| dist (rev_seen, x :: xs) = |
|
574 |
if memb (x, rev_seen) then dist (rev_seen, xs) |
|
575 |
else dist (x :: rev_seen, xs); |
|
576 |
in |
|
577 |
dist ([], lst) |
|
578 |
end; |
|
579 |
||
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
580 |
fun distinct l = gen_distinct (op =) l; |
233 | 581 |
|
582 |
||
583 |
(*returns the tail beginning with the first repeated element, or []*) |
|
584 |
fun findrep [] = [] |
|
585 |
| findrep (x :: xs) = if x mem xs then x :: xs else findrep xs; |
|
586 |
||
587 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
588 |
(*returns a list containing all repeated elements exactly once; preserves |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
589 |
order, takes first of equal elements*) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
590 |
fun gen_duplicates eq lst = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
591 |
let |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
592 |
val memb = gen_mem eq; |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
593 |
|
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
594 |
fun dups (rev_dups, []) = rev rev_dups |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
595 |
| dups (rev_dups, x :: xs) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
596 |
if memb (x, rev_dups) orelse not (memb (x, xs)) then |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
597 |
dups (rev_dups, xs) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
598 |
else dups (x :: rev_dups, xs); |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
599 |
in |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
600 |
dups ([], lst) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
601 |
end; |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
602 |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
603 |
fun duplicates l = gen_duplicates (op =) l; |
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
604 |
|
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
605 |
|
233 | 606 |
|
607 |
(** association lists **) |
|
0 | 608 |
|
233 | 609 |
(*association list lookup*) |
610 |
fun assoc ([], key) = None |
|
611 |
| assoc ((keyi, xi) :: pairs, key) = |
|
612 |
if key = keyi then Some xi else assoc (pairs, key); |
|
613 |
||
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
614 |
(*association list lookup, optimized version for ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
615 |
fun assoc_int ([], (key:int)) = None |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
616 |
| assoc_int ((keyi, xi) :: pairs, key) = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
617 |
if key = keyi then Some xi else assoc_int (pairs, key); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
618 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
619 |
(*association list lookup, optimized version for strings*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
620 |
fun assoc_string ([], (key:string)) = None |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
621 |
| assoc_string ((keyi, xi) :: pairs, key) = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
622 |
if key = keyi then Some xi else assoc_string (pairs, key); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
623 |
|
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset
|
624 |
(*association list lookup, optimized version for string*ints*) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
625 |
fun assoc_string_int ([], (key:string*int)) = None |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
626 |
| assoc_string_int ((keyi, xi) :: pairs, key) = |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
627 |
if key = keyi then Some xi else assoc_string_int (pairs, key); |
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset
|
628 |
|
233 | 629 |
fun assocs ps x = |
630 |
(case assoc (ps, x) of |
|
631 |
None => [] |
|
632 |
| Some ys => ys); |
|
633 |
||
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
634 |
(*two-fold association list lookup*) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
635 |
fun assoc2 (aal, (key1, key2)) = |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
636 |
(case assoc (aal, key1) of |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
637 |
Some al => assoc (al, key2) |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
638 |
| None => None); |
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset
|
639 |
|
233 | 640 |
(*generalized association list lookup*) |
641 |
fun gen_assoc eq ([], key) = None |
|
642 |
| gen_assoc eq ((keyi, xi) :: pairs, key) = |
|
643 |
if eq (key, keyi) then Some xi else gen_assoc eq (pairs, key); |
|
644 |
||
645 |
(*association list update*) |
|
646 |
fun overwrite (al, p as (key, _)) = |
|
647 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
648 |
if keyi = key then p :: pairs else q :: (over pairs) |
|
649 |
| over [] = [p] |
|
650 |
in over al end; |
|
651 |
||
2522 | 652 |
fun gen_overwrite eq (al, p as (key, _)) = |
653 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
654 |
if eq (keyi, key) then p :: pairs else q :: (over pairs) |
|
655 |
| over [] = [p] |
|
656 |
in over al end; |
|
657 |
||
233 | 658 |
|
659 |
||
660 |
(** generic tables **) |
|
0 | 661 |
|
233 | 662 |
(*Tables are supposed to be 'efficient' encodings of lists of elements distinct |
663 |
wrt. an equality "eq". The extend and merge operations below are optimized |
|
664 |
for long-term space efficiency.*) |
|
665 |
||
666 |
(*append (new) elements to a table*) |
|
667 |
fun generic_extend _ _ _ tab [] = tab |
|
668 |
| generic_extend eq dest_tab mk_tab tab1 lst2 = |
|
669 |
let |
|
670 |
val lst1 = dest_tab tab1; |
|
671 |
val new_lst2 = gen_rems eq (lst2, lst1); |
|
672 |
in |
|
673 |
if null new_lst2 then tab1 |
|
674 |
else mk_tab (lst1 @ new_lst2) |
|
675 |
end; |
|
0 | 676 |
|
233 | 677 |
(*append (new) elements of 2nd table to 1st table*) |
678 |
fun generic_merge eq dest_tab mk_tab tab1 tab2 = |
|
679 |
let |
|
680 |
val lst1 = dest_tab tab1; |
|
681 |
val lst2 = dest_tab tab2; |
|
682 |
val new_lst2 = gen_rems eq (lst2, lst1); |
|
683 |
in |
|
684 |
if null new_lst2 then tab1 |
|
685 |
else if gen_subset eq (lst1, lst2) then tab2 |
|
686 |
else mk_tab (lst1 @ new_lst2) |
|
687 |
end; |
|
0 | 688 |
|
233 | 689 |
|
690 |
(*lists as tables*) |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
691 |
fun extend_list tab = generic_extend (op =) I I tab; |
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
692 |
fun merge_lists tab = generic_merge (op =) I I tab; |
233 | 693 |
|
380 | 694 |
fun merge_rev_lists xs [] = xs |
695 |
| merge_rev_lists [] ys = ys |
|
696 |
| merge_rev_lists xs (y :: ys) = |
|
697 |
(if y mem xs then I else cons y) (merge_rev_lists xs ys); |
|
698 |
||
0 | 699 |
|
700 |
||
233 | 701 |
(** balanced trees **) |
702 |
||
703 |
exception Balance; (*indicates non-positive argument to balancing fun*) |
|
704 |
||
705 |
(*balanced folding; avoids deep nesting*) |
|
706 |
fun fold_bal f [x] = x |
|
707 |
| fold_bal f [] = raise Balance |
|
708 |
| fold_bal f xs = |
|
709 |
let val k = length xs div 2 |
|
710 |
in f (fold_bal f (take(k, xs)), |
|
711 |
fold_bal f (drop(k, xs))) |
|
712 |
end; |
|
713 |
||
714 |
(*construct something of the form f(...g(...(x)...)) for balanced access*) |
|
715 |
fun access_bal (f, g, x) n i = |
|
716 |
let fun acc n i = (*1<=i<=n*) |
|
717 |
if n=1 then x else |
|
718 |
let val n2 = n div 2 |
|
719 |
in if i<=n2 then f (acc n2 i) |
|
720 |
else g (acc (n-n2) (i-n2)) |
|
721 |
end |
|
722 |
in if 1<=i andalso i<=n then acc n i else raise Balance end; |
|
723 |
||
724 |
(*construct ALL such accesses; could try harder to share recursive calls!*) |
|
725 |
fun accesses_bal (f, g, x) n = |
|
726 |
let fun acc n = |
|
727 |
if n=1 then [x] else |
|
728 |
let val n2 = n div 2 |
|
729 |
val acc2 = acc n2 |
|
730 |
in if n-n2=n2 then map f acc2 @ map g acc2 |
|
731 |
else map f acc2 @ map g (acc (n-n2)) end |
|
732 |
in if 1<=n then acc n else raise Balance end; |
|
733 |
||
734 |
||
735 |
||
2506 | 736 |
(** orders **) |
737 |
||
738 |
datatype order = LESS | EQUAL | GREATER; |
|
739 |
||
740 |
fun intord (i, j: int) = |
|
741 |
if i < j then LESS |
|
742 |
else if i = j then EQUAL |
|
743 |
else GREATER; |
|
744 |
||
745 |
fun stringord (a, b: string) = |
|
746 |
if a < b then LESS |
|
747 |
else if a = b then EQUAL |
|
748 |
else GREATER; |
|
749 |
||
750 |
||
751 |
||
3525 | 752 |
(** input / output and diagnostics **) |
233 | 753 |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
754 |
val cd = OS.FileSys.chDir; |
2317 | 755 |
val pwd = OS.FileSys.getDir; |
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
756 |
|
3525 | 757 |
|
758 |
local |
|
759 |
fun out s = |
|
760 |
(TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); |
|
761 |
||
762 |
fun prefix_lines prfx txt = |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
763 |
txt |> split_lines |> map (fn s => prfx ^ s ^ "\n") |> implode; |
3525 | 764 |
in |
765 |
||
766 |
(*hooks for output channels: normal, warning, error*) |
|
767 |
val prs_fn = ref (fn s => out s); |
|
768 |
val warning_fn = ref (fn s => out (prefix_lines "### " s)); |
|
769 |
val error_fn = ref (fn s => out (prefix_lines "*** " s)); |
|
770 |
||
771 |
end; |
|
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset
|
772 |
|
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset
|
773 |
fun prs s = !prs_fn s; |
233 | 774 |
fun writeln s = prs (s ^ "\n"); |
775 |
||
3525 | 776 |
fun warning s = !warning_fn s; |
233 | 777 |
|
778 |
(*print error message and abort to top level*) |
|
779 |
exception ERROR; |
|
3874 | 780 |
fun error_msg s = !error_fn s; (*promise to raise ERROR later!*) |
3553 | 781 |
fun error s = (error_msg s; raise ERROR); |
782 |
fun sys_error msg = (error_msg " !! SYSTEM ERROR !!\n"; error msg); |
|
233 | 783 |
|
784 |
fun assert p msg = if p then () else error msg; |
|
785 |
fun deny p msg = if p then error msg else (); |
|
786 |
||
544
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset
|
787 |
(*Assert pred for every member of l, generating a message if pred fails*) |
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset
|
788 |
fun assert_all pred l msg_fn = |
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset
|
789 |
let fun asl [] = () |
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
790 |
| asl (x::xs) = if pred x then asl xs |
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
791 |
else error (msg_fn x) |
544
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset
|
792 |
in asl l end; |
233 | 793 |
|
3624 | 794 |
|
3699 | 795 |
(* handle errors (capturing messages) *) |
796 |
||
797 |
datatype 'a error = |
|
798 |
Error of string | |
|
799 |
OK of 'a; |
|
800 |
||
801 |
fun handle_error f x = |
|
802 |
let |
|
803 |
val buffer = ref ""; |
|
804 |
fun capture s = buffer := ! buffer ^ s ^ "\n"; |
|
805 |
val result = Some (setmp error_fn capture f x) handle ERROR => None; |
|
806 |
in |
|
807 |
case result of |
|
808 |
None => Error (! buffer) |
|
809 |
| Some y => OK y |
|
810 |
end; |
|
811 |
||
812 |
||
3624 | 813 |
(* read / write files *) |
814 |
||
815 |
fun read_file name = |
|
816 |
let |
|
817 |
val instream = TextIO.openIn name; |
|
818 |
val intext = TextIO.inputAll instream; |
|
819 |
in |
|
820 |
TextIO.closeIn instream; |
|
821 |
intext |
|
822 |
end; |
|
823 |
||
824 |
fun write_file name txt = |
|
3645 | 825 |
let val outstream = TextIO.openOut name in |
826 |
TextIO.output (outstream, txt); |
|
827 |
TextIO.closeOut outstream |
|
828 |
end; |
|
829 |
||
830 |
fun append_file name txt = |
|
831 |
let val outstream = TextIO.openAppend name in |
|
3624 | 832 |
TextIO.output (outstream, txt); |
833 |
TextIO.closeOut outstream |
|
834 |
end; |
|
835 |
||
836 |
||
837 |
(*for the "test" target in IsaMakefiles -- signifies successful termination*) |
|
233 | 838 |
fun maketest msg = |
3624 | 839 |
(writeln msg; write_file "test" "Test examples ran successfully\n"); |
233 | 840 |
|
841 |
||
842 |
(*print a list surrounded by the brackets lpar and rpar, with comma separator |
|
843 |
print nothing for empty list*) |
|
844 |
fun print_list (lpar, rpar, pre: 'a -> unit) (l : 'a list) = |
|
845 |
let fun prec x = (prs ","; pre x) |
|
846 |
in |
|
847 |
(case l of |
|
848 |
[] => () |
|
849 |
| x::l => (prs lpar; pre x; seq prec l; prs rpar)) |
|
850 |
end; |
|
851 |
||
852 |
(*print a list of items separated by newlines*) |
|
853 |
fun print_list_ln (pre: 'a -> unit) : 'a list -> unit = |
|
854 |
seq (fn x => (pre x; writeln "")); |
|
855 |
||
856 |
||
857 |
val print_int = prs o string_of_int; |
|
858 |
||
859 |
||
3525 | 860 |
(* output to LaTeX / xdvi *) |
861 |
fun latex s = |
|
862 |
execute ("( cd /tmp ; echo \"" ^ s ^ |
|
863 |
"\" | isa2latex -s > $$.tex ; latex $$.tex ; xdvi $$.dvi ; rm $$.* ) > /dev/null &"); |
|
864 |
||
233 | 865 |
|
866 |
(** timing **) |
|
867 |
||
868 |
(*unconditional timing function*) |
|
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
869 |
fun timeit x = cond_timeit true x; |
233 | 870 |
|
871 |
(*timed application function*) |
|
872 |
fun timeap f x = timeit (fn () => f x); |
|
873 |
||
874 |
(*timed "use" function, printing filenames*) |
|
875 |
fun time_use fname = timeit (fn () => |
|
876 |
(writeln ("\n**** Starting " ^ fname ^ " ****"); use fname; |
|
877 |
writeln ("\n**** Finished " ^ fname ^ " ****"))); |
|
878 |
||
3624 | 879 |
(*use the file, but exit with error code if errors found.*) |
955
aa0c5f9daf5b
Declares the function exit_use to behave like use but fail if
lcp
parents:
544
diff
changeset
|
880 |
fun exit_use fname = use fname handle _ => exit 1; |
233 | 881 |
|
882 |
||
1407 | 883 |
(** filenames and paths **) |
233 | 884 |
|
1290 | 885 |
(*Convert UNIX filename of the form "path/file" to "path/" and "file"; |
233 | 886 |
if filename contains no slash, then it returns "" and "file"*) |
887 |
val split_filename = |
|
888 |
(pairself implode) o take_suffix (not_equal "/") o explode; |
|
889 |
||
890 |
val base_name = #2 o split_filename; |
|
891 |
||
1290 | 892 |
(*Merge splitted filename (path and file); |
233 | 893 |
if path does not end with one a slash is appended*) |
894 |
fun tack_on "" name = name |
|
895 |
| tack_on path name = |
|
896 |
if last_elem (explode path) = "/" then path ^ name |
|
897 |
else path ^ "/" ^ name; |
|
898 |
||
1290 | 899 |
(*Remove the extension of a filename, i.e. the part after the last '.'*) |
233 | 900 |
val remove_ext = implode o #1 o take_suffix (not_equal ".") o explode; |
901 |
||
1290 | 902 |
(*Make relative path to reach an absolute location from a different one*) |
903 |
fun relative_path cur_path dest_path = |
|
904 |
let (*Remove common beginning of both paths and make relative path*) |
|
905 |
fun mk_relative [] [] = [] |
|
906 |
| mk_relative [] ds = ds |
|
907 |
| mk_relative cs [] = map (fn _ => "..") cs |
|
908 |
| mk_relative (c::cs) (d::ds) = |
|
909 |
if c = d then mk_relative cs ds |
|
910 |
else ".." :: map (fn _ => "..") cs @ (d::ds); |
|
911 |
in if cur_path = "" orelse hd (explode cur_path) <> "/" orelse |
|
912 |
dest_path = "" orelse hd (explode dest_path) <> "/" then |
|
913 |
error "Relative or empty path passed to relative_path" |
|
914 |
else (); |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
915 |
space_implode "/" (mk_relative (BAD_space_explode "/" cur_path) |
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
916 |
(BAD_space_explode "/" dest_path)) |
1290 | 917 |
end; |
233 | 918 |
|
1407 | 919 |
(*Determine if absolute path1 is a subdirectory of absolute path2*) |
920 |
fun path1 subdir_of path2 = |
|
921 |
if hd (explode path1) <> "/" orelse hd (explode path2) <> "/" then |
|
922 |
error "Relative or empty path passed to subdir_of" |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
923 |
else (BAD_space_explode "/" path2) prefix (BAD_space_explode "/" path1); |
1407 | 924 |
|
1456 | 925 |
fun absolute_path cwd file = |
926 |
let fun rm_points [] result = rev result |
|
927 |
| rm_points (".."::ds) result = rm_points ds (tl result) |
|
928 |
| rm_points ("."::ds) result = rm_points ds result |
|
929 |
| rm_points (d::ds) result = rm_points ds (d::result); |
|
930 |
in if file = "" then "" |
|
931 |
else if hd (explode file) = "/" then file |
|
932 |
else "/" ^ space_implode "/" |
|
3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset
|
933 |
(rm_points (BAD_space_explode "/" (tack_on cwd file)) []) |
1456 | 934 |
end; |
935 |
||
3606 | 936 |
fun file_exists file = (file_info file <> ""); |
937 |
||
233 | 938 |
|
939 |
(** misc functions **) |
|
940 |
||
941 |
(*use the keyfun to make a list of (x, key) pairs*) |
|
0 | 942 |
fun make_keylist (keyfun: 'a->'b) : 'a list -> ('a * 'b) list = |
233 | 943 |
let fun keypair x = (x, keyfun x) |
944 |
in map keypair end; |
|
0 | 945 |
|
233 | 946 |
(*given a list of (x, key) pairs and a searchkey |
0 | 947 |
return the list of xs from each pair whose key equals searchkey*) |
948 |
fun keyfilter [] searchkey = [] |
|
233 | 949 |
| keyfilter ((x, key) :: pairs) searchkey = |
950 |
if key = searchkey then x :: keyfilter pairs searchkey |
|
951 |
else keyfilter pairs searchkey; |
|
0 | 952 |
|
953 |
||
954 |
(*Partition list into elements that satisfy predicate and those that don't. |
|
233 | 955 |
Preserves order of elements in both lists.*) |
0 | 956 |
fun partition (pred: 'a->bool) (ys: 'a list) : ('a list * 'a list) = |
957 |
let fun part ([], answer) = answer |
|
233 | 958 |
| part (x::xs, (ys, ns)) = if pred(x) |
959 |
then part (xs, (x::ys, ns)) |
|
960 |
else part (xs, (ys, x::ns)) |
|
961 |
in part (rev ys, ([], [])) end; |
|
0 | 962 |
|
963 |
||
964 |
fun partition_eq (eq:'a * 'a -> bool) = |
|
965 |
let fun part [] = [] |
|
233 | 966 |
| part (x::ys) = let val (xs, xs') = partition (apl(x, eq)) ys |
967 |
in (x::xs)::(part xs') end |
|
0 | 968 |
in part end; |
969 |
||
970 |
||
233 | 971 |
(*Partition a list into buckets [ bi, b(i+1), ..., bj ] |
0 | 972 |
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) |
973 |
fun partition_list p i j = |
|
233 | 974 |
let fun part k xs = |
975 |
if k>j then |
|
0 | 976 |
(case xs of [] => [] |
977 |
| _ => raise LIST "partition_list") |
|
978 |
else |
|
233 | 979 |
let val (ns, rest) = partition (p k) xs; |
980 |
in ns :: part(k+1)rest end |
|
0 | 981 |
in part i end; |
982 |
||
983 |
||
233 | 984 |
(* sorting *) |
985 |
||
986 |
(*insertion sort; stable (does not reorder equal elements) |
|
987 |
'less' is less-than test on type 'a*) |
|
988 |
fun sort (less: 'a*'a -> bool) = |
|
0 | 989 |
let fun insert (x, []) = [x] |
233 | 990 |
| insert (x, y::ys) = |
991 |
if less(y, x) then y :: insert (x, ys) else x::y::ys; |
|
0 | 992 |
fun sort1 [] = [] |
993 |
| sort1 (x::xs) = insert (x, sort1 xs) |
|
994 |
in sort1 end; |
|
995 |
||
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
996 |
(*sort strings*) |
3973 | 997 |
fun sort_wrt sel xs = sort (op <= o pairself (sel: 'a -> string)) xs; |
998 |
val sort_strings = sort_wrt I; |
|
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
999 |
|
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
1000 |
|
233 | 1001 |
(* transitive closure (not Warshall's algorithm) *) |
0 | 1002 |
|
233 | 1003 |
fun transitive_closure [] = [] |
1004 |
| transitive_closure ((x, ys)::ps) = |
|
1005 |
let val qs = transitive_closure ps |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
1006 |
val zs = foldl (fn (zs, y) => assocs qs y union_string zs) (ys, ys) |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset
|
1007 |
fun step(u, us) = (u, if x mem_string us then zs union_string us |
2243
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset
|
1008 |
else us) |
233 | 1009 |
in (x, zs) :: map step qs end; |
0 | 1010 |
|
1011 |
||
233 | 1012 |
(* generating identifiers *) |
0 | 1013 |
|
4063 | 1014 |
(** Freshly generated identifiers; supplied prefix MUST start with a letter **) |
0 | 1015 |
local |
4063 | 1016 |
(*Maps 0-63 to A-Z, a-z, 0-9 or _ or ' for generating random identifiers*) |
1017 |
fun char i = if i<26 then chr (ord "A" + i) |
|
1018 |
else if i<52 then chr (ord "a" + i - 26) |
|
1019 |
else if i<62 then chr (ord"0" + i - 52) |
|
1020 |
else if i=62 then "_" |
|
1021 |
else (*i=63*) "'"; |
|
1022 |
||
1023 |
val charVec = Vector.tabulate (64, char); |
|
1024 |
||
1025 |
fun newid n = |
|
1026 |
let |
|
1027 |
in implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n))) end |
|
2003 | 1028 |
|
2806
772f6bba48a1
gensym no longer generates random identifiers, but just enumerates them
paulson
parents:
2522
diff
changeset
|
1029 |
val seedr = ref 0; |
0 | 1030 |
|
4063 | 1031 |
in |
1032 |
fun init_gensym() = (seedr := 0); |
|
2003 | 1033 |
|
1034 |
fun gensym pre = pre ^ |
|
2806
772f6bba48a1
gensym no longer generates random identifiers, but just enumerates them
paulson
parents:
2522
diff
changeset
|
1035 |
(#1(newid (!seedr), |
772f6bba48a1
gensym no longer generates random identifiers, but just enumerates them
paulson
parents:
2522
diff
changeset
|
1036 |
seedr := 1+ !seedr)) |
4063 | 1037 |
end; |
1038 |
||
1039 |
||
1040 |
local |
|
1041 |
(*Identifies those character codes legal in identifiers. |
|
1042 |
chould use Basis Library character functions if Poly/ML provided characters*) |
|
1043 |
fun idCode k = (ord "a" <= k andalso k < ord "z") orelse |
|
1044 |
(ord "A" <= k andalso k < ord "Z") orelse |
|
1045 |
(ord "0" <= k andalso k < ord "9"); |
|
1046 |
||
1047 |
val idCodeVec = Vector.tabulate (256, idCode); |
|
1048 |
||
1049 |
in |
|
2003 | 1050 |
|
0 | 1051 |
(*Increment a list of letters like a reversed base 26 number. |
233 | 1052 |
If head is "z", bumps chars in tail. |
0 | 1053 |
Digits are incremented as if they were integers. |
1054 |
"_" and "'" are not changed. |
|
233 | 1055 |
For making variants of identifiers.*) |
0 | 1056 |
|
4063 | 1057 |
fun bump_int_list(c::cs) = |
1058 |
if c="9" then "0" :: bump_int_list cs |
|
1059 |
else |
|
1060 |
if "0" <= c andalso c < "9" then chr(ord(c)+1) :: cs |
|
233 | 1061 |
else "1" :: c :: cs |
0 | 1062 |
| bump_int_list([]) = error("bump_int_list: not an identifier"); |
1063 |
||
233 | 1064 |
fun bump_list([], d) = [d] |
1065 |
| bump_list(["'"], d) = [d, "'"] |
|
1066 |
| bump_list("z"::cs, _) = "a" :: bump_list(cs, "a") |
|
1067 |
| bump_list("Z"::cs, _) = "A" :: bump_list(cs, "A") |
|
1068 |
| bump_list("9"::cs, _) = "0" :: bump_int_list cs |
|
4063 | 1069 |
| bump_list(c::cs, _) = |
1070 |
let val k = ord(c) |
|
1071 |
in if Vector.sub(idCodeVec,k) then chr(k+1) :: cs |
|
1072 |
else |
|
1073 |
if c="'" orelse c="_" then c :: bump_list(cs, "") |
|
1074 |
else error("bump_list: not legal in identifier: " ^ |
|
1075 |
implode(rev(c::cs))) |
|
233 | 1076 |
end; |
0 | 1077 |
|
1078 |
end; |
|
1079 |
||
233 | 1080 |
fun bump_string s : string = implode (rev (bump_list(rev(explode s), ""))); |
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
1081 |
|
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset
|
1082 |
|
233 | 1083 |
(* lexical scanning *) |
0 | 1084 |
|
233 | 1085 |
(*scan a list of characters into "words" composed of "letters" (recognized by |
1086 |
is_let) and separated by any number of non-"letters"*) |
|
1087 |
fun scanwords is_let cs = |
|
0 | 1088 |
let fun scan1 [] = [] |
233 | 1089 |
| scan1 cs = |
1090 |
let val (lets, rest) = take_prefix is_let cs |
|
1091 |
in implode lets :: scanwords is_let rest end; |
|
1092 |
in scan1 (#2 (take_prefix (not o is_let) cs)) end; |
|
24
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset
|
1093 |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1094 |
end; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1095 |
|
1592 | 1096 |
(*Variable-branching trees: for proof terms*) |
1097 |
datatype 'a mtree = Join of 'a * 'a mtree list; |
|
1098 |
||
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset
|
1099 |
open Library; |