src/Pure/library.ML
 author wenzelm Wed Jun 01 15:42:25 1994 +0200 (1994-06-01) changeset 410 c8171ee32744 parent 380 daca5b594fb3 child 512 55755ed9fab9 permissions -rw-r--r--
replaced infix also by |>
1 (*  Title:      Pure/library.ML
2     ID:         \$Id\$
3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
4     Copyright   1992  University of Cambridge
6 Basic library: functions, options, pairs, booleans, lists, integers,
7 strings, lists as sets, association lists, generic tables, balanced trees,
8 input / output, timing, filenames, misc functions.
9 *)
12 (** functions **)
14 (*handy combinators*)
15 fun curry f x y = f (x, y);
16 fun uncurry f (x, y) = f x y;
17 fun I x = x;
18 fun K x y = x;
20 (*reverse apply*)
21 infix |>;
22 fun (x |> f) = f x;
24 (*combine two functions forming the union of their domains*)
25 infix orelf;
26 fun f orelf g = fn x => f x handle Match => g x;
28 (*application of (infix) operator to its left or right argument*)
29 fun apl (x, f) y = f (x, y);
30 fun apr (f, y) x = f (x, y);
32 (*functional for pairs*)
33 fun pairself f (x, y) = (f x, f y);
35 (*function exponentiation: f(...(f x)...) with n applications of f*)
36 fun funpow n f x =
37   let fun rep (0, x) = x
38         | rep (n, x) = rep (n - 1, f x)
39   in rep (n, x) end;
43 (** options **)
45 datatype 'a option = None | Some of 'a;
47 exception OPTION of string;
49 fun the (Some x) = x
50   | the None = raise OPTION "the";
52 fun if_none None y = y
53   | if_none (Some x) _ = x;
55 fun is_some (Some _) = true
56   | is_some None = false;
58 fun is_none (Some _) = false
59   | is_none None = true;
61 fun apsome f (Some x) = Some (f x)
62   | apsome _ None = None;
66 (** pairs **)
68 fun pair x y = (x, y);
69 fun rpair x y = (y, x);
71 fun fst (x, y) = x;
72 fun snd (x, y) = y;
74 fun eq_fst ((x1, _), (x2, _)) = x1 = x2;
75 fun eq_snd ((_, y1), (_, y2)) = y1 = y2;
77 fun swap (x, y) = (y, x);
79 (*apply the function to a component of a pair*)
80 fun apfst f (x, y) = (f x, y);
81 fun apsnd f (x, y) = (x, f y);
85 (** booleans **)
87 (* equality *)
89 fun equal x y = x = y;
90 fun not_equal x y = x <> y;
93 (* operators for combining predicates *)
95 infix orf;
96 fun p orf q = fn x => p x orelse q x;
98 infix andf;
99 fun p andf q = fn x => p x andalso q x;
101 fun notf p x = not (p x);
104 (* predicates on lists *)
106 fun orl [] = false
107   | orl (x :: xs) = x orelse orl xs;
109 fun andl [] = true
110   | andl (x :: xs) = x andalso andl xs;
112 (*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*)
113 fun exists (pred: 'a -> bool) : 'a list -> bool =
114   let fun boolf [] = false
115         | boolf (x :: xs) = pred x orelse boolf xs
116   in boolf end;
118 (*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*)
119 fun forall (pred: 'a -> bool) : 'a list -> bool =
120   let fun boolf [] = true
121         | boolf (x :: xs) = pred x andalso boolf xs
122   in boolf end;
125 (* flags *)
127 fun set flag = (flag := true; true);
128 fun reset flag = (flag := false; false);
129 fun toggle flag = (flag := not (! flag); ! flag);
133 (** lists **)
135 exception LIST of string;
137 fun null [] = true
138   | null (_ :: _) = false;
140 fun hd [] = raise LIST "hd"
141   | hd (x :: _) = x;
143 fun tl [] = raise LIST "tl"
144   | tl (_ :: xs) = xs;
146 fun cons x xs = x :: xs;
149 (* fold *)
151 (*the following versions of fold are designed to fit nicely with infixes*)
153 (*  (op @) (e, [x1, ..., xn])  ===>  ((e @ x1) @ x2) ... @ xn
154     for operators that associate to the left (TAIL RECURSIVE)*)
155 fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
156   let fun itl (e, [])  = e
157         | itl (e, a::l) = itl (f(e, a), l)
158   in  itl end;
160 (*  (op @) ([x1, ..., xn], e)  ===>   x1 @ (x2 ... @ (xn @ e))
161     for operators that associate to the right (not tail recursive)*)
162 fun foldr f (l, e) =
163   let fun itr [] = e
164         | itr (a::l) = f(a, itr l)
165   in  itr l  end;
167 (*  (op @) [x1, ..., xn]  ===>   x1 @ (x2 ... @ (x[n-1] @ xn))
168     for n > 0, operators that associate to the right (not tail recursive)*)
169 fun foldr1 f l =
170   let fun itr [x] = x                       (* FIXME [] case: elim warn (?) *)
171         | itr (x::l) = f(x, itr l)
172   in  itr l  end;
175 (* basic list functions *)
177 (*length of a list, should unquestionably be a standard function*)
178 local fun length1 (n, [])  = n   (*TAIL RECURSIVE*)
179         | length1 (n, x :: xs) = length1 (n + 1, xs)
180 in  fun length l = length1 (0, l) end;
182 (*take the first n elements from a list*)
183 fun take (n, []) = []
184   | take (n, x :: xs) =
185       if n > 0 then x :: take (n - 1, xs) else [];
187 (*drop the first n elements from a list*)
188 fun drop (n, []) = []
189   | drop (n, x :: xs) =
190       if n > 0 then drop (n - 1, xs) else x :: xs;
192 (*return nth element of a list, where 0 designates the first element;
193   raise EXCEPTION if list too short*)
194 fun nth_elem NL =
195   (case drop NL of
196     [] => raise LIST "nth_elem"
197   | x :: _ => x);
199 (*last element of a list*)
200 fun last_elem [] = raise LIST "last_elem"
201   | last_elem [x] = x
202   | last_elem (_ :: xs) = last_elem xs;
204 (*find the position of an element in a list*)
205 fun find (x, ys) =
206   let fun f (y :: ys, i) = if x = y then i else f (ys, i + 1)
207         | f (_, _) = raise LIST "find"
208   in f (ys, 0) end;
210 (*flatten a list of lists to a list*)
211 fun flat (ls: 'c list list) : 'c list = foldr (op @) (ls, []);
214 (*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates
215   (proc x1; ...; proc xn) for side effects*)
216 fun seq (proc: 'a -> unit) : 'a list -> unit =
217   let fun seqf [] = ()
218         | seqf (x :: xs) = (proc x; seqf xs)
219   in seqf end;
222 (*separate s [x1, x2, ..., xn]  ===>  [x1, s, x2, s, ..., s, xn]*)
223 fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs
224   | separate _ xs = xs;
226 (*make the list [x, x, ..., x] of length n*)
227 fun replicate n (x: 'a) : 'a list =
228   let fun rep (0, xs) = xs
229         | rep (n, xs) = rep (n - 1, x :: xs)
230   in
231     if n < 0 then raise LIST "replicate"
232     else rep (n, [])
233   end;
236 (* filter *)
238 (*copy the list preserving elements that satisfy the predicate*)
239 fun filter (pred: 'a->bool) : 'a list -> 'a list =
240   let fun filt [] = []
241         | filt (x :: xs) = if pred x then x :: filt xs else filt xs
242   in filt end;
244 fun filter_out f = filter (not o f);
247 fun mapfilter (f: 'a -> 'b option) ([]: 'a list) = [] : 'b list
248   | mapfilter f (x :: xs) =
249       (case f x of
250         None => mapfilter f xs
251       | Some y => y :: mapfilter f xs);
254 fun find_first _ [] = None
255   | find_first pred (x :: xs) =
256       if pred x then Some x else find_first pred xs;
259 (* lists of pairs *)
261 fun map2 _ ([], []) = []
262   | map2 f (x :: xs, y :: ys) = (f (x, y) :: map2 f (xs, ys))
263   | map2 _ _ = raise LIST "map2";
265 fun exists2 _ ([], []) = false
266   | exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys)
267   | exists2 _ _ = raise LIST "exists2";
269 fun forall2 _ ([], []) = true
270   | forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys)
271   | forall2 _ _ = raise LIST "forall2";
273 (*combine two lists forming a list of pairs:
274   [x1, ..., xn] ~~ [y1, ..., yn]  ===>  [(x1, y1), ..., (xn, yn)]*)
275 infix ~~;
276 fun [] ~~ [] = []
277   | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys)
278   | _ ~~ _ = raise LIST "~~";
281 (*inverse of ~~; the old 'split':
282   [(x1, y1), ..., (xn, yn)]  ===>  ([x1, ..., xn], [y1, ..., yn])*)
283 fun split_list (l: ('a * 'b) list) = (map #1 l, map #2 l);
286 (* prefixes, suffixes *)
288 infix prefix;
289 fun [] prefix _ = true
290   | (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys)
291   | _ prefix _ = false;
293 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
294    where xi is the first element that does not satisfy the predicate*)
295 fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
296   let fun take (rxs, []) = (rev rxs, [])
297         | take (rxs, x :: xs) =
298             if  pred x  then  take(x :: rxs, xs)  else  (rev rxs, x :: xs)
299   in  take([], xs)  end;
301 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., xi], [x(i+1), ..., xn])
302    where xi is the last element that does not satisfy the predicate*)
303 fun take_suffix _ [] = ([], [])
304   | take_suffix pred (x :: xs) =
305       (case take_suffix pred xs of
306         ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx)
307       | (prfx, sffx) => (x :: prfx, sffx));
311 (** integers **)
313 fun inc i = i := ! i + 1;
314 fun dec i = i := ! i - 1;
317 (* lists of integers *)
319 (*make the list [from, from + 1, ..., to]*)
320 infix upto;
321 fun from upto to =
322   if from > to then [] else from :: ((from + 1) upto to);
324 (*make the list [from, from - 1, ..., to]*)
325 infix downto;
326 fun from downto to =
327   if from < to then [] else from :: ((from - 1) downto to);
329 (*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*)
330 fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1)
331   | downto0 ([], n) = n = ~1;
334 (* operations on integer lists *)
336 fun sum [] = 0
337   | sum (n :: ns) = n + sum ns;
339 fun max [m:int] = m
340   | max (m :: n :: ns) = if m > n then max (m :: ns) else max (n :: ns)
341   | max [] = raise LIST "max";
343 fun min [m:int] = m
344   | min (m :: n :: ns) = if m < n then min (m :: ns) else min (n :: ns)
345   | min [] = raise LIST "min";
348 (* convert integers to strings *)
350 (*expand the number in the given base;
351   example: radixpand (2, 8) gives [1, 0, 0, 0]*)
352 fun radixpand (base, num) : int list =
353   let
354     fun radix (n, tail) =
355       if n < base then n :: tail
356       else radix (n div base, (n mod base) :: tail)
357   in radix (num, []) end;
359 (*expands a number into a string of characters starting from "zerochar";
360   example: radixstring (2, "0", 8) gives "1000"*)
361 fun radixstring (base, zerochar, num) =
362   let val offset = ord zerochar;
363       fun chrof n = chr (offset + n)
364   in implode (map chrof (radixpand (base, num))) end;
367 fun string_of_int n =
368   if n < 0 then "~" ^ radixstring (10, "0", ~n) else radixstring (10, "0", n);
372 (** strings **)
374 fun is_letter ch =
375   ord "A" <= ord ch andalso ord ch <= ord "Z" orelse
376   ord "a" <= ord ch andalso ord ch <= ord "z";
378 fun is_digit ch =
379   ord "0" <= ord ch andalso ord ch <= ord "9";
381 (*letter or _ or prime (')*)
382 fun is_quasi_letter "_" = true
383   | is_quasi_letter "'" = true
384   | is_quasi_letter ch = is_letter ch;
386 (*white space: blanks, tabs, newlines*)
387 val is_blank : string -> bool =
388   fn " " => true | "\t" => true | "\n" => true | _ => false;
390 val is_letdig = is_quasi_letter orf is_digit;
393 (*lower all chars of string*)
394 val to_lower =
395   let
396     fun lower ch =
397       if ch >= "A" andalso ch <= "Z" then
398         chr (ord ch - ord "A" + ord "a")
399       else ch;
400   in implode o (map lower) o explode end;
403 (*parentesize*)
404 fun parents lpar rpar str = lpar ^ str ^ rpar;
406 (*simple quoting (does not escape special chars)*)
407 val quote = parents "\"" "\"";
409 (*space_implode "..." (explode "hello"); gives "h...e...l...l...o"*)
410 fun space_implode a bs = implode (separate a bs);
412 val commas = space_implode ", ";
413 val commas_quote = commas o map quote;
415 (*concatenate messages, one per line, into a string*)
416 val cat_lines = space_implode "\n";
420 (** lists as sets **)
422 (*membership in a list*)
423 infix mem;
424 fun x mem [] = false
425   | x mem (y :: ys) = x = y orelse x mem ys;
427 (*generalized membership test*)
428 fun gen_mem eq (x, []) = false
429   | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys);
432 (*insertion into list if not already there*)
433 infix ins;
434 fun x ins xs = if x mem xs then xs else x :: xs;
436 (*generalized insertion*)
437 fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs;
440 (*union of sets represented as lists: no repetitions*)
441 infix union;
442 fun xs union [] = xs
443   | [] union ys = ys
444   | (x :: xs) union ys = xs union (x ins ys);
446 (*generalized union*)
447 fun gen_union eq (xs, []) = xs
448   | gen_union eq ([], ys) = ys
449   | gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys));
452 (*intersection*)
453 infix inter;
454 fun [] inter ys = []
455   | (x :: xs) inter ys =
456       if x mem ys then x :: (xs inter ys) else xs inter ys;
459 (*subset*)
460 infix subset;
461 fun [] subset ys = true
462   | (x :: xs) subset ys = x mem ys andalso xs subset ys;
464 fun gen_subset eq (xs, ys) = forall (fn x => gen_mem eq (x, ys)) xs;
467 (*eq_set*)
469 fun eq_set (xs, ys) =
470   xs = ys orelse (xs subset ys andalso ys subset xs);
473 (*removing an element from a list WITHOUT duplicates*)
474 infix \;
475 fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x)
476   | [] \ x = [];
478 infix \\;
479 val op \\ = foldl (op \);
481 (*removing an element from a list -- possibly WITH duplicates*)
482 fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs;
484 val gen_rems = foldl o gen_rem;
487 (*makes a list of the distinct members of the input; preserves order, takes
488   first of equal elements*)
489 fun gen_distinct eq lst =
490   let
491     val memb = gen_mem eq;
493     fun dist (rev_seen, []) = rev rev_seen
494       | dist (rev_seen, x :: xs) =
495           if memb (x, rev_seen) then dist (rev_seen, xs)
496           else dist (x :: rev_seen, xs);
497   in
498     dist ([], lst)
499   end;
501 val distinct = gen_distinct (op =);
504 (*returns the tail beginning with the first repeated element, or []*)
505 fun findrep [] = []
506   | findrep (x :: xs) = if x mem xs then x :: xs else findrep xs;
509 (*returns a list containing all repeated elements exactly once; preserves
510   order, takes first of equal elements*)
511 fun gen_duplicates eq lst =
512   let
513     val memb = gen_mem eq;
515     fun dups (rev_dups, []) = rev rev_dups
516       | dups (rev_dups, x :: xs) =
517           if memb (x, rev_dups) orelse not (memb (x, xs)) then
518             dups (rev_dups, xs)
519           else dups (x :: rev_dups, xs);
520   in
521     dups ([], lst)
522   end;
524 val duplicates = gen_duplicates (op =);
528 (** association lists **)
530 (*association list lookup*)
531 fun assoc ([], key) = None
532   | assoc ((keyi, xi) :: pairs, key) =
533       if key = keyi then Some xi else assoc (pairs, key);
535 fun assocs ps x =
536   (case assoc (ps, x) of
537     None => []
538   | Some ys => ys);
540 (*two-fold association list lookup*)
541 fun assoc2 (aal, (key1, key2)) =
542   (case assoc (aal, key1) of
543     Some al => assoc (al, key2)
544   | None => None);
546 (*generalized association list lookup*)
547 fun gen_assoc eq ([], key) = None
548   | gen_assoc eq ((keyi, xi) :: pairs, key) =
549       if eq (key, keyi) then Some xi else gen_assoc eq (pairs, key);
551 (*association list update*)
552 fun overwrite (al, p as (key, _)) =
553   let fun over ((q as (keyi, _)) :: pairs) =
554             if keyi = key then p :: pairs else q :: (over pairs)
555         | over [] = [p]
556   in over al end;
560 (** generic tables **)
562 (*Tables are supposed to be 'efficient' encodings of lists of elements distinct
563   wrt. an equality "eq". The extend and merge operations below are optimized
564   for long-term space efficiency.*)
566 (*append (new) elements to a table*)
567 fun generic_extend _ _ _ tab [] = tab
568   | generic_extend eq dest_tab mk_tab tab1 lst2 =
569       let
570         val lst1 = dest_tab tab1;
571         val new_lst2 = gen_rems eq (lst2, lst1);
572       in
573         if null new_lst2 then tab1
574         else mk_tab (lst1 @ new_lst2)
575       end;
577 (*append (new) elements of 2nd table to 1st table*)
578 fun generic_merge eq dest_tab mk_tab tab1 tab2 =
579   let
580     val lst1 = dest_tab tab1;
581     val lst2 = dest_tab tab2;
582     val new_lst2 = gen_rems eq (lst2, lst1);
583   in
584     if null new_lst2 then tab1
585     else if gen_subset eq (lst1, lst2) then tab2
586     else mk_tab (lst1 @ new_lst2)
587   end;
590 (*lists as tables*)
591 val extend_list = generic_extend (op =) I I;
592 val merge_lists = generic_merge (op =) I I;
594 fun merge_rev_lists xs [] = xs
595   | merge_rev_lists [] ys = ys
596   | merge_rev_lists xs (y :: ys) =
597       (if y mem xs then I else cons y) (merge_rev_lists xs ys);
601 (** balanced trees **)
603 exception Balance;      (*indicates non-positive argument to balancing fun*)
605 (*balanced folding; avoids deep nesting*)
606 fun fold_bal f [x] = x
607   | fold_bal f [] = raise Balance
608   | fold_bal f xs =
609       let val k = length xs div 2
610       in  f (fold_bal f (take(k, xs)),
611              fold_bal f (drop(k, xs)))
612       end;
614 (*construct something of the form f(...g(...(x)...)) for balanced access*)
615 fun access_bal (f, g, x) n i =
616   let fun acc n i =     (*1<=i<=n*)
617           if n=1 then x else
618           let val n2 = n div 2
619           in  if i<=n2 then f (acc n2 i)
620                        else g (acc (n-n2) (i-n2))
621           end
622   in  if 1<=i andalso i<=n then acc n i else raise Balance  end;
624 (*construct ALL such accesses; could try harder to share recursive calls!*)
625 fun accesses_bal (f, g, x) n =
626   let fun acc n =
627           if n=1 then [x] else
628           let val n2 = n div 2
629               val acc2 = acc n2
630           in  if n-n2=n2 then map f acc2 @ map g acc2
631                          else map f acc2 @ map g (acc (n-n2)) end
632   in  if 1<=n then acc n else raise Balance  end;
636 (** input / output **)
638 fun prs s = output (std_out, s);
639 fun writeln s = prs (s ^ "\n");
642 (*print error message and abort to top level*)
643 exception ERROR;
644 fun error msg = (writeln msg; raise ERROR);
645 fun sys_error msg = (writeln "*** SYSTEM ERROR ***"; error msg);
647 fun assert p msg = if p then () else error msg;
648 fun deny p msg = if p then error msg else ();
651 (* FIXME close file (?) *)
652 (*for the "test" target in Makefiles -- signifies successful termination*)
653 fun maketest msg =
654   (writeln msg; output (open_out "test", "Test examples ran successfully\n"));
657 (*print a list surrounded by the brackets lpar and rpar, with comma separator
658   print nothing for empty list*)
659 fun print_list (lpar, rpar, pre: 'a -> unit) (l : 'a list) =
660   let fun prec x = (prs ","; pre x)
661   in
662     (case l of
663       [] => ()
664     | x::l => (prs lpar; pre x; seq prec l; prs rpar))
665   end;
667 (*print a list of items separated by newlines*)
668 fun print_list_ln (pre: 'a -> unit) : 'a list -> unit =
669   seq (fn x => (pre x; writeln ""));
672 val print_int = prs o string_of_int;
676 (** timing **)
678 (*unconditional timing function*)
679 val timeit = cond_timeit true;
681 (*timed application function*)
682 fun timeap f x = timeit (fn () => f x);
684 (*timed "use" function, printing filenames*)
685 fun time_use fname = timeit (fn () =>
686   (writeln ("\n**** Starting " ^ fname ^ " ****"); use fname;
687    writeln ("\n**** Finished " ^ fname ^ " ****")));
691 (** filenames **)
693 (*convert UNIX filename of the form "path/file" to "path/" and "file";
694   if filename contains no slash, then it returns "" and "file"*)
695 val split_filename =
696   (pairself implode) o take_suffix (not_equal "/") o explode;
698 val base_name = #2 o split_filename;
700 (*merge splitted filename (path and file);
701   if path does not end with one a slash is appended*)
702 fun tack_on "" name = name
703   | tack_on path name =
704       if last_elem (explode path) = "/" then path ^ name
705       else path ^ "/" ^ name;
707 (*remove the extension of a filename, i.e. the part after the last '.'*)
708 val remove_ext = implode o #1 o take_suffix (not_equal ".") o explode;
712 (** misc functions **)
714 (*use the keyfun to make a list of (x, key) pairs*)
715 fun make_keylist (keyfun: 'a->'b) : 'a list -> ('a * 'b) list =
716   let fun keypair x = (x, keyfun x)
717   in map keypair end;
719 (*given a list of (x, key) pairs and a searchkey
720   return the list of xs from each pair whose key equals searchkey*)
721 fun keyfilter [] searchkey = []
722   | keyfilter ((x, key) :: pairs) searchkey =
723       if key = searchkey then x :: keyfilter pairs searchkey
724       else keyfilter pairs searchkey;
727 (*Partition list into elements that satisfy predicate and those that don't.
728   Preserves order of elements in both lists.*)
729 fun partition (pred: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
730     let fun part ([], answer) = answer
731           | part (x::xs, (ys, ns)) = if pred(x)
732             then  part (xs, (x::ys, ns))
733             else  part (xs, (ys, x::ns))
734     in  part (rev ys, ([], []))  end;
737 fun partition_eq (eq:'a * 'a -> bool) =
738     let fun part [] = []
739           | part (x::ys) = let val (xs, xs') = partition (apl(x, eq)) ys
740                            in (x::xs)::(part xs') end
741     in part end;
744 (*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
745    putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
746 fun partition_list p i j =
747   let fun part k xs =
748             if k>j then
749               (case xs of [] => []
750                          | _ => raise LIST "partition_list")
751             else
752             let val (ns, rest) = partition (p k) xs;
753             in  ns :: part(k+1)rest  end
754   in  part i end;
757 (* sorting *)
759 (*insertion sort; stable (does not reorder equal elements)
760   'less' is less-than test on type 'a*)
761 fun sort (less: 'a*'a -> bool) =
762   let fun insert (x, []) = [x]
763         | insert (x, y::ys) =
764               if less(y, x) then y :: insert (x, ys) else x::y::ys;
765       fun sort1 [] = []
766         | sort1 (x::xs) = insert (x, sort1 xs)
767   in  sort1  end;
769 (*sort strings*)
770 val sort_strings = sort (op <= : string * string -> bool);
773 (* transitive closure (not Warshall's algorithm) *)
775 fun transitive_closure [] = []
776   | transitive_closure ((x, ys)::ps) =
777       let val qs = transitive_closure ps
778           val zs = foldl (fn (zs, y) => assocs qs y union zs) (ys, ys)
779           fun step(u, us) = (u, if x mem us then zs union us else us)
780       in (x, zs) :: map step qs end;
783 (* generating identifiers *)
785 local
786   val a = ord "a" and z = ord "z" and A = ord "A" and Z = ord "Z"
787   and k0 = ord "0" and k9 = ord "9"
788 in
790 (*Increment a list of letters like a reversed base 26 number.
791   If head is "z", bumps chars in tail.
792   Digits are incremented as if they were integers.
793   "_" and "'" are not changed.
794   For making variants of identifiers.*)
796 fun bump_int_list(c::cs) = if c="9" then "0" :: bump_int_list cs else
797         if k0 <= ord(c) andalso ord(c) < k9 then chr(ord(c)+1) :: cs
798         else "1" :: c :: cs
799   | bump_int_list([]) = error("bump_int_list: not an identifier");
801 fun bump_list([], d) = [d]
802   | bump_list(["'"], d) = [d, "'"]
803   | bump_list("z"::cs, _) = "a" :: bump_list(cs, "a")
804   | bump_list("Z"::cs, _) = "A" :: bump_list(cs, "A")
805   | bump_list("9"::cs, _) = "0" :: bump_int_list cs
806   | bump_list(c::cs, _) = let val k = ord(c)
807         in if (a <= k andalso k < z) orelse (A <= k andalso k < Z) orelse
808               (k0 <= k andalso k < k9) then chr(k+1) :: cs else
809            if c="'" orelse c="_" then c :: bump_list(cs, "") else
810                 error("bump_list: not legal in identifier: " ^
811                         implode(rev(c::cs)))
812         end;
814 end;
816 fun bump_string s : string = implode (rev (bump_list(rev(explode s), "")));
819 (* lexical scanning *)
821 (*scan a list of characters into "words" composed of "letters" (recognized by
822   is_let) and separated by any number of non-"letters"*)
823 fun scanwords is_let cs =
824   let fun scan1 [] = []
825         | scan1 cs =
826             let val (lets, rest) = take_prefix is_let cs
827             in implode lets :: scanwords is_let rest end;
828   in scan1 (#2 (take_prefix (not o is_let) cs)) end;