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