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