1 (* Title: Pure/library.ML
2 Author: Lawrence C Paulson, Cambridge University Computer Laboratory
3 Author: Markus Wenzel, TU Muenchen
5 Basic library: functions, pairs, booleans, lists, integers,
6 strings, lists as sets, orders, current directory, misc.
8 See also General/basics.ML for the most fundamental concepts.
13 infix 4 ~~ upto downto
16 signature BASIC_LIBRARY =
19 val undefined: 'a -> 'b
22 val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c
23 val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c
24 val ? : bool * ('a -> 'a) -> 'a -> 'a
25 val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b
26 val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b
27 val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b
28 val funpow: int -> ('a -> 'a) -> 'a -> 'a
29 val funpow_yield: int -> ('a -> 'b * 'a) -> 'a -> 'b list * 'a
32 exception ERROR of string
33 val error: string -> 'a
34 val cat_error: string -> string -> 'a
35 val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
38 val pair: 'a -> 'b -> 'a * 'b
39 val rpair: 'a -> 'b -> 'b * 'a
40 val fst: 'a * 'b -> 'a
41 val snd: 'a * 'b -> 'b
42 val eq_fst: ('a * 'c -> bool) -> ('a * 'b) * ('c * 'd) -> bool
43 val eq_snd: ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
44 val eq_pair: ('a * 'c -> bool) -> ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
45 val swap: 'a * 'b -> 'b * 'a
46 val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c
47 val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b
48 val pairself: ('a -> 'b) -> 'a * 'a -> 'b * 'b
51 val equal: ''a -> ''a -> bool
52 val not_equal: ''a -> ''a -> bool
53 val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool
54 val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool
55 val exists: ('a -> bool) -> 'a list -> bool
56 val forall: ('a -> bool) -> 'a list -> bool
57 val setmp_CRITICAL: 'a Unsynchronized.ref -> 'a -> ('b -> 'c) -> 'b -> 'c
58 val setmp_thread_data: 'a Universal.tag -> 'a -> 'a -> ('b -> 'c) -> 'b -> 'c
61 val single: 'a -> 'a list
62 val the_single: 'a list -> 'a
63 val singleton: ('a list -> 'b list) -> 'a -> 'b
64 val yield_singleton: ('a list -> 'c -> 'b list * 'c) -> 'a -> 'c -> 'b * 'c
65 val perhaps_apply: ('a -> 'a option) list -> 'a -> 'a option
66 val perhaps_loop: ('a -> 'a option) -> 'a -> 'a option
67 val foldl1: ('a * 'a -> 'a) -> 'a list -> 'a
68 val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
69 val eq_list: ('a * 'a -> bool) -> 'a list * 'a list -> bool
70 val maps: ('a -> 'b list) -> 'a list -> 'b list
71 val filter: ('a -> bool) -> 'a list -> 'a list
72 val filter_out: ('a -> bool) -> 'a list -> 'a list
73 val map_filter: ('a -> 'b option) -> 'a list -> 'b list
74 val take: int -> 'a list -> 'a list
75 val drop: int -> 'a list -> 'a list
76 val chop: int -> 'a list -> 'a list * 'a list
77 val chop_while: ('a -> bool) -> 'a list -> 'a list * 'a list
78 val chop_groups: int -> 'a list -> 'a list list
79 val nth: 'a list -> int -> 'a
80 val nth_list: 'a list list -> int -> 'a list
81 val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list
82 val nth_drop: int -> 'a list -> 'a list
83 val map_index: (int * 'a -> 'b) -> 'a list -> 'b list
84 val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
85 val map_range: (int -> 'a) -> int -> 'a list
86 val split_last: 'a list -> 'a list * 'a
87 val find_first: ('a -> bool) -> 'a list -> 'a option
88 val find_index: ('a -> bool) -> 'a list -> int
89 val get_first: ('a -> 'b option) -> 'a list -> 'b option
90 val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option
91 val flat: 'a list list -> 'a list
92 val unflat: 'a list list -> 'b list -> 'b list list
93 val grouped: int -> (('a list -> 'b list) -> 'c list list -> 'd list list) ->
94 ('a -> 'b) -> 'c list -> 'd list
95 val burrow: ('a list -> 'b list) -> 'a list list -> 'b list list
96 val burrow_options: ('a list -> 'b list) -> 'a option list -> 'b option list
97 val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd
98 val separate: 'a -> 'a list -> 'a list
99 val surround: 'a -> 'a list -> 'a list
100 val replicate: int -> 'a -> 'a list
101 val map_product: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
102 val fold_product: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
103 val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
104 val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
105 val fold_rev2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
106 val forall2: ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
107 val map_split: ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list
108 val zip_options: 'a list -> 'b option list -> ('a * 'b) list
109 val ~~ : 'a list * 'b list -> ('a * 'b) list
110 val split_list: ('a * 'b) list -> 'a list * 'b list
111 val burrow_fst: ('a list -> 'b list) -> ('a * 'c) list -> ('b * 'c) list
112 val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
113 val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
114 val chop_prefix: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list * ('a list * 'b list)
115 val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
116 val prefixes1: 'a list -> 'a list list
117 val prefixes: 'a list -> 'a list list
118 val suffixes1: 'a list -> 'a list list
119 val suffixes: 'a list -> 'a list list
122 val upto: int * int -> int list
123 val downto: int * int -> int list
124 val radixpand: int * int -> int list
125 val radixstring: int * string * int -> string
126 val string_of_int: int -> string
127 val signed_string_of_int: int -> string
128 val string_of_indexname: string * int -> string
129 val read_radix_int: int -> string list -> int * string list
130 val read_int: string list -> int * string list
131 val oct_char: string -> string
134 val nth_string: string -> int -> string
135 val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a
136 val exists_string: (string -> bool) -> string -> bool
137 val forall_string: (string -> bool) -> string -> bool
138 val first_field: string -> string -> (string * string) option
139 val enclose: string -> string -> string -> string
140 val unenclose: string -> string
141 val quote: string -> string
142 val space_implode: string -> string list -> string
143 val commas: string list -> string
144 val commas_quote: string list -> string
145 val cat_lines: string list -> string
146 val space_explode: string -> string -> string list
147 val split_lines: string -> string list
148 val prefix_lines: string -> string -> string
149 val prefix: string -> string -> string
150 val suffix: string -> string -> string
151 val unprefix: string -> string -> string
152 val unsuffix: string -> string -> string
153 val trim_line: string -> string
154 val replicate_string: int -> string -> string
155 val translate_string: (string -> string) -> string -> string
156 val match_string: string -> string -> bool
159 val string_of_real: real -> string
160 val signed_string_of_real: real -> string
161 val smart_string_of_real: real -> string
163 (*lists as sets -- see also Pure/General/ord_list.ML*)
164 val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool
165 val insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
166 val remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
167 val update: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
168 val union: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
169 val subtract: ('b * 'a -> bool) -> 'b list -> 'a list -> 'a list
170 val inter: ('a * 'b -> bool) -> 'b list -> 'a list -> 'a list
171 val merge: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
172 val subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
173 val eq_set: ('a * 'a -> bool) -> 'a list * 'a list -> bool
174 val distinct: ('a * 'a -> bool) -> 'a list -> 'a list
175 val duplicates: ('a * 'a -> bool) -> 'a list -> 'a list
176 val has_duplicates: ('a * 'a -> bool) -> 'a list -> bool
177 val map_transpose: ('a list -> 'b) -> 'a list list -> 'b list
179 (*lists as multisets*)
180 val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
181 val combine: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
182 val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
185 val is_equal: order -> bool
186 val rev_order: order -> order
187 val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
188 val bool_ord: bool * bool -> order
189 val int_ord: int * int -> order
190 val string_ord: string * string -> order
191 val fast_string_ord: string * string -> order
192 val option_ord: ('a * 'b -> order) -> 'a option * 'b option -> order
193 val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order
194 val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
195 val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
196 val sort: ('a * 'a -> order) -> 'a list -> 'a list
197 val sort_distinct: ('a * 'a -> order) -> 'a list -> 'a list
198 val sort_strings: string list -> string list
199 val sort_wrt: ('a -> string) -> 'a list -> 'a list
200 val tag_list: int -> 'a list -> (int * 'a) list
201 val untag_list: (int * 'a) list -> 'a list
202 val order_list: (int * 'a) list -> 'a list
206 val random: unit -> real
207 val random_range: int -> int -> int
208 val one_of: 'a list -> 'a
209 val frequency: (int * 'a) list -> 'a
212 val divide_and_conquer: ('a -> 'a list * ('b list -> 'b)) -> 'a -> 'b
213 val divide_and_conquer': ('a -> 'b -> ('a list * ('c list * 'b -> 'c * 'b)) * 'b) ->
215 val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list
216 val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list
218 val serial: unit -> serial
219 val serial_string: unit -> string
221 val stamp: unit -> stamp
222 structure Object: sig type T = exn end
223 val cd: string -> unit
224 val pwd: unit -> string
225 val getenv: string -> string
226 val getenv_strict: string -> string
231 include BASIC_LIBRARY
232 val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
233 val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
236 structure Library: LIBRARY =
241 fun undefined _ = raise Match;
245 fun curry f x y = f (x, y);
246 fun uncurry f (x, y) = f x y;
248 (*conditional application*)
249 fun b ? f = fn x => if b then f x else x;
251 (*composition with multiple args*)
252 fun (f oo g) x y = f (g x y);
253 fun (f ooo g) x y z = f (g x y z);
254 fun (f oooo g) x y z w = f (g x y z w);
256 (*function exponentiation: f (... (f x) ...) with n applications of f*)
257 fun funpow (0 : int) _ = I
258 | funpow n f = f #> funpow (n - 1) f;
260 fun funpow_yield (0 : int) _ x = ([], x)
261 | funpow_yield n f x = x |> f ||>> funpow_yield (n - 1) f |>> op ::;
266 exception ERROR of string;
267 fun error msg = raise ERROR msg;
269 fun cat_error "" msg = error msg
270 | cat_error msg1 msg2 = error (msg1 ^ "\n" ^ msg2);
272 fun assert_all pred list msg =
275 | ass (x :: xs) = if pred x then ass xs else error (msg x);
281 fun pair x y = (x, y);
282 fun rpair x y = (y, x);
287 fun eq_fst eq ((x1, _), (x2, _)) = eq (x1, x2);
288 fun eq_snd eq ((_, y1), (_, y2)) = eq (y1, y2);
289 fun eq_pair eqx eqy ((x1, y1), (x2, y2)) = eqx (x1, x2) andalso eqy (y1, y2);
291 fun swap (x, y) = (y, x);
293 fun apfst f (x, y) = (f x, y);
294 fun apsnd f (x, y) = (x, f y);
295 fun pairself f (x, y) = (f x, f y);
300 (*polymorphic equality*)
301 fun equal x y = x = y;
302 fun not_equal x y = x <> y;
304 (*combining predicates*)
305 fun p orf q = fn x => p x orelse q x;
306 fun p andf q = fn x => p x andalso q x;
308 val exists = List.exists;
309 val forall = List.all;
314 fun setmp_CRITICAL flag value f x =
315 NAMED_CRITICAL "setmp" (fn () => Unsynchronized.setmp flag value f x);
317 fun setmp_thread_data tag orig_data data f x =
318 uninterruptible (fn restore_attributes => fn () =>
320 val _ = Thread.setLocal (tag, data);
321 val result = Exn.capture (restore_attributes f) x;
322 val _ = Thread.setLocal (tag, orig_data);
323 in Exn.release result end) ();
331 fun the_single [x] = x
332 | the_single _ = raise List.Empty;
334 fun singleton f x = the_single (f [x]);
336 fun yield_singleton f x = f [x] #>> the_single;
338 fun perhaps_apply funs arg =
341 | app (f :: fs) (changed, x) =
343 NONE => app fs (changed, x)
344 | SOME x' => app fs (true, x'));
345 in (case app funs (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
347 fun perhaps_loop f arg =
349 fun loop (changed, x) =
352 | SOME x' => loop (true, x'));
353 in (case loop (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
356 (* fold -- old versions *)
358 (*the following versions of fold are designed to fit nicely with infixes*)
360 (* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn
361 for operators that associate to the left (TAIL RECURSIVE)*)
362 fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
363 let fun itl (e, []) = e
364 | itl (e, a::l) = itl (f(e, a), l)
367 (* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e))
368 for operators that associate to the right (not tail recursive)*)
371 | itr (a::l) = f(a, itr l)
374 (* (op @) [x1, ..., xn] ===> ((x1 @ x2) @ x3) ... @ xn
375 for operators that associate to the left (TAIL RECURSIVE)*)
376 fun foldl1 f [] = raise List.Empty
377 | foldl1 f (x :: xs) = foldl f (x, xs);
379 (* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n-1] @ xn))
380 for n > 0, operators that associate to the right (not tail recursive)*)
381 fun foldr1 f [] = raise List.Empty
384 | itr (x::l) = f(x, itr l)
388 (* basic list functions *)
390 fun eq_list eq (list1, list2) =
391 pointer_eq (list1, list2) orelse
393 fun eq_lst (x :: xs, y :: ys) = eq (x, y) andalso eq_lst (xs, ys)
395 in length list1 = length list2 andalso eq_lst (list1, list2) end;
398 | maps f (x :: xs) = f x @ maps f xs;
400 val filter = List.filter;
401 fun filter_out f = filter (not o f);
402 val map_filter = List.mapPartial;
404 fun take (0: int) xs = []
406 | take n (x :: xs) = x :: take (n - 1) xs;
408 fun drop (0: int) xs = xs
410 | drop n (x :: xs) = drop (n - 1) xs;
412 fun chop (0: int) xs = ([], xs)
413 | chop _ [] = ([], [])
414 | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
416 fun chop_while P [] = ([], [])
417 | chop_while P (ys as x :: xs) =
418 if P x then chop_while P xs |>> cons x else ([], ys);
420 fun chop_groups n list =
421 (case chop (Int.max (n, 1)) list of
423 | (g, rest) => g :: chop_groups n rest);
426 (*return nth element of a list, where 0 designates the first element;
427 raise Subscript if list too short*)
428 fun nth xs i = List.nth (xs, i);
430 fun nth_list xss i = nth xss i handle General.Subscript => [];
432 fun nth_map 0 f (x :: xs) = f x :: xs
433 | nth_map n f (x :: xs) = x :: nth_map (n - 1) f xs
434 | nth_map (_: int) _ [] = raise Subscript;
437 List.take (xs, n) @ List.drop (xs, n + 1);
441 fun mapp (_: int) [] = []
442 | mapp i (x :: xs) = f (i, x) :: mapp (i + 1) xs
447 fun fold_aux (_: int) [] y = y
448 | fold_aux i (x :: xs) y = fold_aux (i + 1) xs (f (i, x) y);
454 if k < i then f k :: mapp (k + 1) else [];
457 (*rear decomposition*)
458 fun split_last [] = raise List.Empty
459 | split_last [x] = ([], x)
460 | split_last (x :: xs) = apfst (cons x) (split_last xs);
462 (*find first element satisfying predicate*)
463 val find_first = List.find;
465 (*find position of first element satisfying a predicate*)
466 fun find_index pred =
467 let fun find (_: int) [] = ~1
468 | find n (x :: xs) = if pred x then n else find (n + 1) xs;
471 (*get first element by lookup function*)
472 fun get_first _ [] = NONE
473 | get_first f (x :: xs) =
475 NONE => get_first f xs
480 fun get (_: int) [] = NONE
483 NONE => get (i + 1) xs
484 | SOME y => SOME (i, y))
487 val flat = List.concat;
489 fun unflat (xs :: xss) ys =
490 let val (ps, qs) = chop (length xs) ys
491 in ps :: unflat xss qs end
493 | unflat _ _ = raise ListPair.UnequalLengths;
495 fun grouped n comb f = chop_groups n #> comb (map f) #> flat;
497 fun burrow f xss = unflat xss (f (flat xss));
499 fun burrow_options f os = map (try hd) (burrow f (map the_list os));
501 fun fold_burrow f xss s =
502 apfst (unflat xss) (f (flat xss) s);
504 (*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*)
505 fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs
506 | separate _ xs = xs;
508 fun surround s (x :: xs) = s :: x :: surround s xs
509 | surround s [] = [s];
511 (*make the list [x, x, ..., x] of length n*)
512 fun replicate (n: int) x =
513 let fun rep (0, xs) = xs
514 | rep (n, xs) = rep (n - 1, x :: xs)
516 if n < 0 then raise Subscript
523 fun map_product f _ [] = []
524 | map_product f [] _ = []
525 | map_product f (x :: xs) ys = map (f x) ys @ map_product f xs ys;
527 fun fold_product f _ [] z = z
528 | fold_product f [] _ z = z
529 | fold_product f (x :: xs) ys z = z |> fold (f x) ys |> fold_product f xs ys;
534 fun map2 _ [] [] = []
535 | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys
536 | map2 _ _ _ = raise ListPair.UnequalLengths;
538 fun fold2 f [] [] z = z
539 | fold2 f (x :: xs) (y :: ys) z = fold2 f xs ys (f x y z)
540 | fold2 f _ _ _ = raise ListPair.UnequalLengths;
542 fun fold_rev2 f [] [] z = z
543 | fold_rev2 f (x :: xs) (y :: ys) z = f x y (fold_rev2 f xs ys z)
544 | fold_rev2 f _ _ _ = raise ListPair.UnequalLengths;
546 fun forall2 P [] [] = true
547 | forall2 P (x :: xs) (y :: ys) = P x y andalso forall2 P xs ys
548 | forall2 P _ _ = raise ListPair.UnequalLengths;
550 fun map_split f [] = ([], [])
551 | map_split f (x :: xs) =
554 val (ys, ws) = map_split f xs;
555 in (y :: ys, w :: ws) end;
557 fun zip_options (x :: xs) (SOME y :: ys) = (x, y) :: zip_options xs ys
558 | zip_options (_ :: xs) (NONE :: ys) = zip_options xs ys
559 | zip_options _ [] = []
560 | zip_options [] _ = raise ListPair.UnequalLengths;
562 (*combine two lists forming a list of pairs:
563 [x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*)
565 | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys)
566 | _ ~~ _ = raise ListPair.UnequalLengths;
568 (*inverse of ~~; the old 'split':
569 [(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*)
570 val split_list = ListPair.unzip;
572 fun burrow_fst f xs = split_list xs |>> f |> op ~~;
575 (* prefixes, suffixes *)
577 fun is_prefix _ [] _ = true
578 | is_prefix eq (x :: xs) (y :: ys) = eq (x, y) andalso is_prefix eq xs ys
579 | is_prefix eq _ _ = false;
581 (* [x1, ..., xi, ..., xn] ---> ([x1, ..., x(i-1)], [xi, ..., xn])
582 where xi is the first element that does not satisfy the predicate*)
583 fun take_prefix (pred : 'a -> bool) (xs: 'a list) : 'a list * 'a list =
584 let fun take (rxs, []) = (rev rxs, [])
585 | take (rxs, x :: xs) =
586 if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs)
589 fun chop_prefix eq ([], ys) = ([], ([], ys))
590 | chop_prefix eq (xs, []) = ([], (xs, []))
591 | chop_prefix eq (xs as x :: xs', ys as y :: ys') =
593 let val (ps', xys'') = chop_prefix eq (xs', ys')
594 in (x :: ps', xys'') end
597 (* [x1, ..., xi, ..., xn] ---> ([x1, ..., xi], [x(i+1), ..., xn])
598 where xi is the last element that does not satisfy the predicate*)
599 fun take_suffix _ [] = ([], [])
600 | take_suffix pred (x :: xs) =
601 (case take_suffix pred xs of
602 ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx)
603 | (prfx, sffx) => (x :: prfx, sffx));
605 fun prefixes1 [] = []
606 | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
608 fun prefixes xs = [] :: prefixes1 xs;
610 fun suffixes1 xs = map rev (prefixes1 (rev xs));
611 fun suffixes xs = [] :: suffixes1 xs;
617 (* lists of integers *)
619 (*make the list [from, from + 1, ..., to]*)
620 fun ((i: int) upto j) =
621 if i > j then [] else i :: (i + 1 upto j);
623 (*make the list [from, from - 1, ..., to]*)
624 fun ((i: int) downto j) =
625 if i < j then [] else i :: (i - 1 downto j);
628 (* convert integers to strings *)
630 (*expand the number in the given base;
631 example: radixpand (2, 8) gives [1, 0, 0, 0]*)
632 fun radixpand (base, num) : int list =
634 fun radix (n, tail) =
635 if n < base then n :: tail
636 else radix (n div base, (n mod base) :: tail)
637 in radix (num, []) end;
639 (*expands a number into a string of characters starting from "zerochar";
640 example: radixstring (2, "0", 8) gives "1000"*)
641 fun radixstring (base, zerochar, num) =
642 let val offset = ord zerochar;
643 fun chrof n = chr (offset + n)
644 in implode (map chrof (radixpand (base, num))) end;
649 val small = 10000: int;
650 val small_table = Vector.tabulate (small, Int.toString);
653 fun string_of_int i =
654 if i < 0 then Int.toString i
655 else if i < 10 then chr (zero + i)
656 else if i < small then Vector.sub (small_table, i)
661 fun signed_string_of_int i =
662 if i < 0 then "-" ^ string_of_int (~ i) else string_of_int i;
664 fun string_of_indexname (a, 0) = a
665 | string_of_indexname (a, i) = a ^ "_" ^ string_of_int i;
670 fun read_radix_int radix cs =
673 val limit = zero + radix;
674 fun scan (num, []) = (num, [])
675 | scan (num, c :: cs) =
676 if zero <= ord c andalso ord c < limit then
677 scan (radix * num + (ord c - zero), cs)
681 val read_int = read_radix_int 10;
683 fun oct_char s = chr (#1 (read_radix_int 8 (raw_explode s)));
689 (* functions tuned for strings, avoiding explode *)
691 fun nth_string str i =
692 (case try String.substring (str, i, 1) of
694 | NONE => raise Subscript);
696 fun fold_string f str x0 =
700 if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x;
703 fun exists_string pred str =
706 fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1));
709 fun forall_string pred = not o exists_string (not o pred);
711 fun first_field sep str =
716 if i + n > len then NONE
717 else if String.substring (str, i, n) = sep then SOME i
722 | SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE)))
725 (*enclose in brackets*)
726 fun enclose lpar rpar str = lpar ^ str ^ rpar;
727 fun unenclose str = String.substring (str, 1, size str - 2);
729 (*simple quoting (does not escape special chars)*)
730 val quote = enclose "\"" "\"";
732 fun space_implode a bs = implode (separate a bs);
734 val commas = space_implode ", ";
735 val commas_quote = commas o map quote;
737 val cat_lines = space_implode "\n";
739 (*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*)
740 fun space_explode _ "" = []
741 | space_explode sep s = String.fields (fn c => str c = sep) s;
743 val split_lines = space_explode "\n";
745 fun prefix_lines "" txt = txt
746 | prefix_lines prfx txt = txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines;
748 fun prefix prfx s = prfx ^ s;
749 fun suffix sffx s = s ^ sffx;
751 fun unprefix prfx s =
752 if String.isPrefix prfx s then String.substring (s, size prfx, size s - size prfx)
753 else raise Fail "unprefix";
755 fun unsuffix sffx s =
756 if String.isSuffix sffx s then String.substring (s, 0, size s - size sffx)
757 else raise Fail "unsuffix";
759 val trim_line = perhaps (try (unsuffix "\n"));
761 fun replicate_string (0: int) _ = ""
762 | replicate_string 1 a = a
763 | replicate_string k a =
764 if k mod 2 = 0 then replicate_string (k div 2) (a ^ a)
765 else replicate_string (k div 2) (a ^ a) ^ a;
767 fun translate_string f = String.translate (f o String.str);
769 (*crude matching of str against simple glob pat*)
770 fun match_string pat str =
772 fun match [] _ = true
773 | match (p :: ps) s =
774 size p <= size s andalso
775 (case try (unprefix p) s of
776 SOME s' => match ps s'
777 | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
778 in match (space_explode "*" pat) str end;
783 val string_of_real = Real.fmt (StringCvt.GEN NONE);
785 fun signed_string_of_real x =
786 if x < 0.0 then "-" ^ string_of_real (~ x) else string_of_real x;
788 fun smart_string_of_real x =
790 val s = signed_string_of_real x;
792 (case space_explode "." s of
793 [a, b] => if forall_string (fn c => c = "0") b then a else s
799 (** lists as sets -- see also Pure/General/ord_list.ML **)
801 (* canonical operations *)
803 fun member eq list x =
806 | memb (y :: ys) = eq (x, y) orelse memb ys;
809 fun insert eq x xs = if member eq xs x then xs else x :: xs;
810 fun remove eq x xs = if member eq xs x then filter_out (fn y => eq (x, y)) xs else xs;
811 fun update eq x xs = cons x (remove eq x xs);
813 fun inter eq xs = filter (member eq xs);
815 fun union eq = fold (insert eq);
816 fun subtract eq = fold (remove eq);
818 fun merge eq (xs, ys) =
819 if pointer_eq (xs, ys) then xs
820 else if null xs then ys
821 else fold_rev (insert eq) ys xs;
824 (* subset and set equality *)
826 fun subset eq (xs, ys) = forall (member eq ys) xs;
828 fun eq_set eq (xs, ys) =
829 eq_list eq (xs, ys) orelse
830 (subset eq (xs, ys) andalso subset (eq o swap) (ys, xs));
833 (*makes a list of the distinct members of the input; preserves order, takes
834 first of equal elements*)
835 fun distinct eq lst =
837 fun dist (rev_seen, []) = rev rev_seen
838 | dist (rev_seen, x :: xs) =
839 if member eq rev_seen x then dist (rev_seen, xs)
840 else dist (x :: rev_seen, xs);
841 in dist ([], lst) end;
843 (*returns a list containing all repeated elements exactly once; preserves
844 order, takes first of equal elements*)
845 fun duplicates eq lst =
847 fun dups (rev_dups, []) = rev rev_dups
848 | dups (rev_dups, x :: xs) =
849 if member eq rev_dups x orelse not (member eq xs x) then
851 else dups (x :: rev_dups, xs);
852 in dups ([], lst) end;
854 fun has_duplicates eq =
857 | dups (x :: xs) = member eq xs x orelse dups xs;
863 fun map_transpose f xss =
866 (case distinct (op =) (map length xss) of
869 | _ => raise ListPair.UnequalLengths);
870 in map_range (fn m => f (map (fn xs => nth xs m) xss)) n end;
874 (** lists as multisets **)
876 fun remove1 eq x [] = []
877 | remove1 eq x (y :: ys) = if eq (x, y) then ys else y :: remove1 eq x ys;
879 fun combine eq xs ys = fold (remove1 eq) ys xs @ ys;
881 fun submultiset _ ([], _) = true
882 | submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys);
888 fun is_equal EQUAL = true
889 | is_equal _ = false;
891 fun rev_order LESS = GREATER
892 | rev_order EQUAL = EQUAL
893 | rev_order GREATER = LESS;
895 (*assume rel is a linear strict order*)
896 fun make_ord rel (x, y) =
897 if rel (x, y) then LESS
898 else if rel (y, x) then GREATER
901 fun bool_ord (false, true) = LESS
902 | bool_ord (true, false) = GREATER
903 | bool_ord _ = EQUAL;
905 val int_ord = Int.compare;
906 val string_ord = String.compare;
908 fun fast_string_ord (s1, s2) =
909 if pointer_eq (s1, s2) then EQUAL
910 else (case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2) | ord => ord);
912 fun option_ord ord (SOME x, SOME y) = ord (x, y)
913 | option_ord _ (NONE, NONE) = EQUAL
914 | option_ord _ (NONE, SOME _) = LESS
915 | option_ord _ (SOME _, NONE) = GREATER;
917 (*lexicographic product*)
918 fun prod_ord a_ord b_ord ((x, y), (x', y')) =
919 (case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord);
921 (*dictionary order -- in general NOT well-founded!*)
922 fun dict_ord elem_ord (x :: xs, y :: ys) =
923 (case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord)
924 | dict_ord _ ([], []) = EQUAL
925 | dict_ord _ ([], _ :: _) = LESS
926 | dict_ord _ (_ :: _, []) = GREATER;
928 (*lexicographic product of lists*)
929 fun list_ord elem_ord (xs, ys) =
930 (case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord);
935 (*stable mergesort -- preserves order of equal elements*)
936 fun mergesort unique ord =
938 fun merge (xs as x :: xs') (ys as y :: ys') =
940 LESS => x :: merge xs' ys
942 if unique then merge xs ys'
943 else x :: merge xs' ys
944 | GREATER => y :: merge xs ys')
948 fun merge_all [xs] = xs
949 | merge_all xss = merge_all (merge_pairs xss)
950 and merge_pairs (xs :: ys :: xss) = merge xs ys :: merge_pairs xss
951 | merge_pairs xss = xss;
953 fun runs (x :: y :: xs) =
955 LESS => ascending y [x] xs
957 if unique then runs (x :: xs)
958 else ascending y [x] xs
959 | GREATER => descending y [x] xs)
962 and ascending x xs (zs as y :: ys) =
964 LESS => ascending y (x :: xs) ys
966 if unique then ascending x xs ys
967 else ascending y (x :: xs) ys
968 | GREATER => rev (x :: xs) :: runs zs)
969 | ascending x xs [] = [rev (x :: xs)]
971 and descending x xs (zs as y :: ys) =
973 GREATER => descending y (x :: xs) ys
975 if unique then descending x xs ys
976 else (x :: xs) :: runs zs
977 | LESS => (x :: xs) :: runs zs)
978 | descending x xs [] = [x :: xs];
980 in merge_all o runs end;
982 fun sort ord = mergesort false ord;
983 fun sort_distinct ord = mergesort true ord;
985 val sort_strings = sort string_ord;
986 fun sort_wrt key xs = sort (string_ord o pairself key) xs;
989 (* items tagged by integer index *)
992 fun tag_list k [] = []
993 | tag_list k (x :: xs) = (k:int, x) :: tag_list (k + 1) xs;
995 (*remove tags and suppress duplicates -- list is assumed sorted!*)
996 fun untag_list [] = []
997 | untag_list [(k: int, x)] = [x]
998 | untag_list ((k, x) :: (rest as (k', x') :: _)) =
999 if k = k' then untag_list rest
1000 else x :: untag_list rest;
1002 (*return list elements in original order*)
1003 fun order_list list = untag_list (sort (int_ord o pairself fst) list);
1007 (** random numbers **)
1011 fun rmod x y = x - y * Real.realFloor (x / y);
1015 val m = 2147483647.0;
1016 val random_seed = Unsynchronized.ref 1.0;
1019 fun random () = CRITICAL (fn () =>
1020 let val r = rmod (a * ! random_seed) m
1021 in (random_seed := r; r) end);
1025 fun random_range l h =
1026 if h < l orelse l < 0 then raise RANDOM
1027 else l + Real.floor (rmod (random ()) (real (h - l + 1)));
1029 fun one_of xs = nth xs (random_range 0 (length xs - 1));
1033 val sum = foldl op + (0, map fst xs);
1034 fun pick n ((k: int, x) :: xs) =
1035 if n <= k then x else pick (n - k) xs
1036 in pick (random_range 1 sum) xs end;
1042 fun divide_and_conquer decomp x =
1043 let val (ys, recomb) = decomp x
1044 in recomb (map (divide_and_conquer decomp) ys) end;
1046 fun divide_and_conquer' decomp x s =
1047 let val ((ys, recomb), s') = decomp x s
1048 in recomb (fold_map (divide_and_conquer' decomp) ys s') end;
1051 (*Partition a list into buckets [ bi, b(i+1), ..., bj ]
1052 putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*)
1053 fun partition_list p i j =
1055 fun part (k: int) xs =
1059 | _ => raise Fail "partition_list")
1061 let val (ns, rest) = List.partition (p k) xs
1062 in ns :: part (k + 1) rest end;
1063 in part (i: int) end;
1065 fun partition_eq (eq: 'a * 'a -> bool) =
1069 let val (xs, xs') = List.partition (fn y => eq (x, y)) ys
1070 in (x :: xs) :: part xs' end;
1074 (* serial numbers and abstract stamps *)
1077 val serial = Multithreading.serial;
1078 val serial_string = string_of_int o serial;
1080 datatype stamp = Stamp of serial;
1081 fun stamp () = Stamp (serial ());
1084 (* generic objects *)
1086 (*note that the builtin exception datatype may be extended by new
1087 constructors at any time*)
1088 structure Object = struct type T = exn end;
1091 (* current directory *)
1093 val cd = OS.FileSys.chDir;
1094 val pwd = OS.FileSys.getDir;
1100 (case OS.Process.getEnv x of
1104 fun getenv_strict x =
1106 "" => error ("Undefined Isabelle environment variable: " ^ quote x)
1111 structure Basic_Library: BASIC_LIBRARY = Library;