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