src/Pure/library.ML
author wenzelm
Tue Sep 29 11:48:32 2009 +0200 (2009-09-29)
changeset 32737 76fa673eee8b
parent 32352 4839a704939a
child 32966 5b21661fe618
permissions -rw-r--r--
Raw ML references as unsynchronized state variables.
     1 (*  Title:      Pure/library.ML
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Basic library: functions, pairs, booleans, lists, integers,
     6 strings, lists as sets, orders, current directory, misc.
     7 
     8 See also General/basics.ML for the most fundamental concepts.
     9 *)
    10 
    11 infix 2 ?
    12 infix 3 o oo ooo oooo
    13 infix 4 ~~ upto downto
    14 infix orf andf \ \\ mem mem_int mem_string union union_int
    15   union_string inter inter_int inter_string subset subset_int subset_string
    16 
    17 signature BASIC_LIBRARY =
    18 sig
    19   (*functions*)
    20   val undefined: 'a -> 'b
    21   val I: 'a -> 'a
    22   val K: 'a -> 'b -> 'a
    23   val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c
    24   val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c
    25   val ? : bool * ('a -> 'a) -> 'a -> 'a
    26   val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b
    27   val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b
    28   val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b
    29   val funpow: int -> ('a -> 'a) -> 'a -> 'a
    30   val funpow_yield: int -> ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    31 
    32   (*errors*)
    33   exception SYS_ERROR of string
    34   val sys_error: string -> 'a
    35   exception ERROR of string
    36   val error: string -> 'a
    37   val cat_error: string -> string -> 'a
    38   val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
    39 
    40   (*pairs*)
    41   val pair: 'a -> 'b -> 'a * 'b
    42   val rpair: 'a -> 'b -> 'b * 'a
    43   val fst: 'a * 'b -> 'a
    44   val snd: 'a * 'b -> 'b
    45   val eq_fst: ('a * 'c -> bool) -> ('a * 'b) * ('c * 'd) -> bool
    46   val eq_snd: ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
    47   val eq_pair: ('a * 'c -> bool) -> ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
    48   val swap: 'a * 'b -> 'b * 'a
    49   val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c
    50   val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b
    51   val pairself: ('a -> 'b) -> 'a * 'a -> 'b * 'b
    52 
    53   (*booleans*)
    54   val equal: ''a -> ''a -> bool
    55   val not_equal: ''a -> ''a -> bool
    56   val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool
    57   val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool
    58   val exists: ('a -> bool) -> 'a list -> bool
    59   val forall: ('a -> bool) -> 'a list -> bool
    60   val setmp_noncritical: 'a Unsynchronized.ref -> 'a -> ('b -> 'c) -> 'b -> 'c
    61   val setmp: 'a Unsynchronized.ref -> 'a -> ('b -> 'c) -> 'b -> 'c
    62   val setmp_thread_data: 'a Universal.tag -> 'a -> 'a -> ('b -> 'c) -> 'b -> 'c
    63 
    64   (*lists*)
    65   exception UnequalLengths
    66   val single: 'a -> 'a list
    67   val the_single: 'a list -> 'a
    68   val singleton: ('a list -> 'b list) -> 'a -> 'b
    69   val yield_singleton: ('a list -> 'c -> 'b list * 'c) -> 'a -> 'c -> 'b * 'c
    70   val apply: ('a -> 'a) list -> 'a -> 'a
    71   val perhaps_apply: ('a -> 'a option) list -> 'a -> 'a option
    72   val perhaps_loop: ('a -> 'a option) -> 'a -> 'a option
    73   val foldl1: ('a * 'a -> 'a) -> 'a list -> 'a
    74   val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
    75   val flat: 'a list list -> 'a list
    76   val unflat: 'a list list -> 'b list -> 'b list list
    77   val burrow: ('a list -> 'b list) -> 'a list list -> 'b list list
    78   val burrow_options: ('a list -> 'b list) -> 'a option list -> 'b option list
    79   val burrow_fst: ('a list -> 'b list) -> ('a * 'c) list -> ('b * 'c) list
    80   val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd
    81   val maps: ('a -> 'b list) -> 'a list -> 'b list
    82   val filter: ('a -> bool) -> 'a list -> 'a list
    83   val filter_out: ('a -> bool) -> 'a list -> 'a list
    84   val map_filter: ('a -> 'b option) -> 'a list -> 'b list
    85   val chop: int -> 'a list -> 'a list * 'a list
    86   val dropwhile: ('a -> bool) -> 'a list -> 'a list
    87   val nth: 'a list -> int -> 'a
    88   val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list
    89   val nth_drop: int -> 'a list -> 'a list
    90   val nth_list: 'a list list -> int -> 'a list
    91   val map_index: (int * 'a -> 'b) -> 'a list -> 'b list
    92   val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
    93   val split_last: 'a list -> 'a list * 'a
    94   val find_index: ('a -> bool) -> 'a list -> int
    95   val find_first: ('a -> bool) -> 'a list -> 'a option
    96   val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option
    97   val get_first: ('a -> 'b option) -> 'a list -> 'b option
    98   val eq_list: ('a * 'b -> bool) -> 'a list * 'b list -> bool
    99   val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
   100   val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
   101   val zip_options: 'a list -> 'b option list -> ('a * 'b) list
   102   val ~~ : 'a list * 'b list -> ('a * 'b) list
   103   val map_split: ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list
   104   val split_list: ('a * 'b) list -> 'a list * 'b list
   105   val map_product: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
   106   val fold_product: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
   107   val map_transpose: ('a list -> 'b) -> 'a list list -> 'b list
   108   val separate: 'a -> 'a list -> 'a list
   109   val surround: 'a -> 'a list -> 'a list
   110   val replicate: int -> 'a -> 'a list
   111   val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
   112   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   113   val chop_prefix: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list * ('a list * 'b list)
   114   val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
   115   val prefixes1: 'a list -> 'a list list
   116   val prefixes: 'a list -> 'a list list
   117   val suffixes1: 'a list -> 'a list list
   118   val suffixes: 'a list -> 'a list list
   119 
   120   (*integers*)
   121   val upto: int * int -> int list
   122   val downto: int * int -> int list
   123   val radixpand: int * int -> int list
   124   val radixstring: int * string * int -> string
   125   val string_of_int: int -> string
   126   val signed_string_of_int: int -> string
   127   val string_of_indexname: string * int -> string
   128   val read_radix_int: int -> string list -> int * string list
   129   val read_int: string list -> int * string list
   130   val oct_char: string -> string
   131 
   132   (*strings*)
   133   val nth_string: string -> int -> string
   134   val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a
   135   val exists_string: (string -> bool) -> string -> bool
   136   val forall_string: (string -> bool) -> string -> bool
   137   val first_field: string -> string -> (string * string) option
   138   val enclose: string -> string -> string -> string
   139   val unenclose: string -> string
   140   val quote: string -> string
   141   val space_implode: string -> string list -> string
   142   val commas: string list -> string
   143   val commas_quote: string list -> string
   144   val cat_lines: string list -> string
   145   val space_explode: string -> string -> string list
   146   val split_lines: string -> string list
   147   val prefix_lines: string -> string -> string
   148   val prefix: string -> string -> string
   149   val suffix: string -> string -> string
   150   val unprefix: string -> string -> string
   151   val unsuffix: string -> string -> string
   152   val replicate_string: int -> string -> string
   153   val translate_string: (string -> string) -> string -> string
   154   val match_string: string -> string -> bool
   155 
   156   (*lists as sets -- see also Pure/General/ord_list.ML*)
   157   val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool
   158   val insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
   159   val remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
   160   val update: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
   161   val subtract: ('b * 'a -> bool) -> 'b list -> 'a list -> 'a list
   162   val merge: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
   163   val mem: ''a * ''a list -> bool
   164   val mem_int: int * int list -> bool
   165   val mem_string: string * string list -> bool
   166   val union: ''a list * ''a list -> ''a list
   167   val union_int: int list * int list -> int list
   168   val union_string: string list * string list -> string list
   169   val gen_union: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
   170   val gen_inter: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
   171   val inter: ''a list * ''a list -> ''a list
   172   val inter_int: int list * int list -> int list
   173   val inter_string: string list * string list -> string list
   174   val subset: ''a list * ''a list -> bool
   175   val subset_int: int list * int list -> bool
   176   val subset_string: string list * string list -> bool
   177   val eq_set: ''a list * ''a list -> bool
   178   val eq_set_string: string list * string list -> bool
   179   val gen_subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   180   val gen_eq_set: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   181   val \ : ''a list * ''a -> ''a list
   182   val \\ : ''a list * ''a list -> ''a list
   183   val distinct: ('a * 'a -> bool) -> 'a list -> 'a list
   184   val duplicates: ('a * 'a -> bool) -> 'a list -> 'a list
   185   val has_duplicates: ('a * 'a -> bool) -> 'a list -> bool
   186 
   187   (*lists as multisets*)
   188   val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
   189   val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   190 
   191   (*orders*)
   192   val is_equal: order -> bool
   193   val rev_order: order -> order
   194   val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
   195   val bool_ord: bool * bool -> order
   196   val int_ord: int * int -> order
   197   val string_ord: string * string -> order
   198   val fast_string_ord: string * string -> order
   199   val option_ord: ('a * 'b -> order) -> 'a option * 'b option -> order
   200   val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order
   201   val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
   202   val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
   203   val sort: ('a * 'a -> order) -> 'a list -> 'a list
   204   val sort_distinct: ('a * 'a -> order) -> 'a list -> 'a list
   205   val sort_strings: string list -> string list
   206   val sort_wrt: ('a -> string) -> 'a list -> 'a list
   207   val tag_list: int -> 'a list -> (int * 'a) list
   208   val untag_list: (int * 'a) list -> 'a list
   209   val order_list: (int * 'a) list -> 'a list
   210 
   211   (*random numbers*)
   212   exception RANDOM
   213   val random: unit -> real
   214   val random_range: int -> int -> int
   215   val one_of: 'a list -> 'a
   216   val frequency: (int * 'a) list -> 'a
   217 
   218   (*misc*)
   219   val divide_and_conquer: ('a -> 'a list * ('b list -> 'b)) -> 'a -> 'b
   220   val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list
   221   val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list
   222   val gensym: string -> string
   223   type stamp
   224   val stamp: unit -> stamp
   225   type serial
   226   val serial: unit -> serial
   227   val serial_string: unit -> string
   228   structure Object: sig type T end
   229 end;
   230 
   231 signature LIBRARY =
   232 sig
   233   include BASIC_LIBRARY
   234   val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
   235   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   236   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   237   val take: int * 'a list -> 'a list
   238   val drop: int * 'a list -> 'a list
   239   val last_elem: 'a list -> 'a
   240 end;
   241 
   242 structure Library: LIBRARY =
   243 struct
   244 
   245 (* functions *)
   246 
   247 fun undefined _ = raise Match;
   248 
   249 fun I x = x;
   250 fun K x = fn _ => x;
   251 fun curry f x y = f (x, y);
   252 fun uncurry f (x, y) = f x y;
   253 
   254 (*conditional application*)
   255 fun b ? f = fn x => if b then f x else x;
   256 
   257 (*composition with multiple args*)
   258 fun (f oo g) x y = f (g x y);
   259 fun (f ooo g) x y z = f (g x y z);
   260 fun (f oooo g) x y z w = f (g x y z w);
   261 
   262 (*function exponentiation: f (... (f x) ...) with n applications of f*)
   263 fun funpow (0 : int) _ = I
   264   | funpow n f = f #> funpow (n - 1) f;
   265 
   266 fun funpow_yield (0 : int) _ x = ([], x)
   267   | funpow_yield n f x = x |> f ||>> funpow_yield (n - 1) f |>> op ::;
   268 
   269 (* errors *)
   270 
   271 exception SYS_ERROR of string;
   272 fun sys_error msg = raise SYS_ERROR msg;
   273 
   274 exception ERROR of string;
   275 fun error msg = raise ERROR msg;
   276 
   277 fun cat_error "" msg = error msg
   278   | cat_error msg1 msg2 = error (msg1 ^ "\n" ^ msg2);
   279 
   280 fun assert_all pred list msg =
   281   let
   282     fun ass [] = ()
   283       | ass (x :: xs) = if pred x then ass xs else error (msg x);
   284   in ass list end;
   285 
   286 
   287 (* pairs *)
   288 
   289 fun pair x y = (x, y);
   290 fun rpair x y = (y, x);
   291 
   292 fun fst (x, y) = x;
   293 fun snd (x, y) = y;
   294 
   295 fun eq_fst eq ((x1, _), (x2, _)) = eq (x1, x2);
   296 fun eq_snd eq ((_, y1), (_, y2)) = eq (y1, y2);
   297 fun eq_pair eqx eqy ((x1, y1), (x2, y2)) = eqx (x1, x2) andalso eqy (y1, y2);
   298 
   299 fun swap (x, y) = (y, x);
   300 
   301 fun apfst f (x, y) = (f x, y);
   302 fun apsnd f (x, y) = (x, f y);
   303 fun pairself f (x, y) = (f x, f y);
   304 
   305 
   306 (* booleans *)
   307 
   308 (*polymorphic equality*)
   309 fun equal x y = x = y;
   310 fun not_equal x y = x <> y;
   311 
   312 (*combining predicates*)
   313 fun p orf q = fn x => p x orelse q x;
   314 fun p andf q = fn x => p x andalso q x;
   315 
   316 val exists = List.exists;
   317 val forall = List.all;
   318 
   319 
   320 (* flags *)
   321 
   322 (*temporarily set flag during execution*)
   323 fun setmp_noncritical flag value f x =
   324   uninterruptible (fn restore_attributes => fn () =>
   325     let
   326       val orig_value = ! flag;
   327       val _ = flag := value;
   328       val result = Exn.capture (restore_attributes f) x;
   329       val _ = flag := orig_value;
   330     in Exn.release result end) ();
   331 
   332 fun setmp flag value f x =
   333   NAMED_CRITICAL "setmp" (fn () => setmp_noncritical flag value f x);
   334 
   335 fun setmp_thread_data tag orig_data data f x =
   336   uninterruptible (fn restore_attributes => fn () =>
   337     let
   338       val _ = Thread.setLocal (tag, data);
   339       val result = Exn.capture (restore_attributes f) x;
   340       val _ = Thread.setLocal (tag, orig_data);
   341     in Exn.release result end) ();
   342 
   343 
   344 
   345 (** lists **)
   346 
   347 exception UnequalLengths;
   348 
   349 fun single x = [x];
   350 
   351 fun the_single [x] = x
   352   | the_single _ = raise Empty;
   353 
   354 fun singleton f x = the_single (f [x]);
   355 
   356 fun yield_singleton f x = f [x] #>> the_single;
   357 
   358 fun apply [] x = x
   359   | apply (f :: fs) x = apply fs (f x);
   360 
   361 fun perhaps_apply funs arg =
   362   let
   363     fun app [] res = res
   364       | app (f :: fs) (changed, x) =
   365           (case f x of
   366             NONE => app fs (changed, x)
   367           | SOME x' => app fs (true, x'));
   368   in (case app funs (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
   369 
   370 fun perhaps_loop f arg =
   371   let
   372     fun loop (changed, x) =
   373       (case f x of
   374         NONE => (changed, x)
   375       | SOME x' => loop (true, x'));
   376   in (case loop (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
   377 
   378 
   379 (* fold -- old versions *)
   380 
   381 (*the following versions of fold are designed to fit nicely with infixes*)
   382 
   383 (*  (op @) (e, [x1, ..., xn])  ===>  ((e @ x1) @ x2) ... @ xn
   384     for operators that associate to the left (TAIL RECURSIVE)*)
   385 fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
   386   let fun itl (e, [])  = e
   387         | itl (e, a::l) = itl (f(e, a), l)
   388   in  itl end;
   389 
   390 (*  (op @) ([x1, ..., xn], e)  ===>   x1 @ (x2 ... @ (xn @ e))
   391     for operators that associate to the right (not tail recursive)*)
   392 fun foldr f (l, e) =
   393   let fun itr [] = e
   394         | itr (a::l) = f(a, itr l)
   395   in  itr l  end;
   396 
   397 (*  (op @) [x1, ..., xn]  ===>  ((x1 @ x2) @ x3) ... @ xn
   398     for operators that associate to the left (TAIL RECURSIVE)*)
   399 fun foldl1 f [] = raise Empty
   400   | foldl1 f (x :: xs) = foldl f (x, xs);
   401 
   402 (*  (op @) [x1, ..., xn]  ===>   x1 @ (x2 ... @ (x[n-1] @ xn))
   403     for n > 0, operators that associate to the right (not tail recursive)*)
   404 fun foldr1 f [] = raise Empty
   405   | foldr1 f l =
   406       let fun itr [x] = x
   407             | itr (x::l) = f(x, itr l)
   408       in  itr l  end;
   409 
   410 fun foldl_map f =
   411   let
   412     fun fold_aux (x, []) = (x, [])
   413       | fold_aux (x, y :: ys) =
   414           let
   415             val (x', y') = f (x, y);
   416             val (x'', ys') = fold_aux (x', ys);
   417           in (x'', y' :: ys') end;
   418   in fold_aux end;
   419 
   420 
   421 (* basic list functions *)
   422 
   423 fun eq_list eq (list1, list2) =
   424   let
   425     fun eq_lst (x :: xs, y :: ys) = eq (x, y) andalso eq_lst (xs, ys)
   426       | eq_lst _ = true;
   427   in length list1 = length list2 andalso eq_lst (list1, list2) end;
   428 
   429 fun maps f [] = []
   430   | maps f (x :: xs) = f x @ maps f xs;
   431 
   432 val filter = List.filter;
   433 fun filter_out f = filter (not o f);
   434 val map_filter = List.mapPartial;
   435 
   436 fun chop (0: int) xs = ([], xs)
   437   | chop _ [] = ([], [])
   438   | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
   439 
   440 (*take the first n elements from a list*)
   441 fun take (n: int, []) = []
   442   | take (n, x :: xs) =
   443       if n > 0 then x :: take (n - 1, xs) else [];
   444 
   445 (*drop the first n elements from a list*)
   446 fun drop (n: int, []) = []
   447   | drop (n, x :: xs) =
   448       if n > 0 then drop (n - 1, xs) else x :: xs;
   449 
   450 fun dropwhile P [] = []
   451   | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
   452 
   453 (*return nth element of a list, where 0 designates the first element;
   454   raise Subscript if list too short*)
   455 fun nth xs i = List.nth (xs, i);
   456 
   457 fun nth_list xss i = nth xss i handle Subscript => [];
   458 
   459 fun nth_map 0 f (x :: xs) = f x :: xs
   460   | nth_map n f (x :: xs) = x :: nth_map (n - 1) f xs
   461   | nth_map (_: int) _ [] = raise Subscript;
   462 
   463 fun nth_drop n xs =
   464   List.take (xs, n) @ List.drop (xs, n + 1);
   465 
   466 fun map_index f =
   467   let
   468     fun mapp (_: int) [] = []
   469       | mapp i (x :: xs) = f (i, x) :: mapp (i + 1) xs
   470   in mapp 0 end;
   471 
   472 fun fold_index f =
   473   let
   474     fun fold_aux (_: int) [] y = y
   475       | fold_aux i (x :: xs) y = fold_aux (i + 1) xs (f (i, x) y);
   476   in fold_aux 0 end;
   477 
   478 val last_elem = List.last;
   479 
   480 (*rear decomposition*)
   481 fun split_last [] = raise Empty
   482   | split_last [x] = ([], x)
   483   | split_last (x :: xs) = apfst (cons x) (split_last xs);
   484 
   485 (*find position of first element satisfying a predicate*)
   486 fun find_index pred =
   487   let fun find (_: int) [] = ~1
   488         | find n (x :: xs) = if pred x then n else find (n + 1) xs;
   489   in find 0 end;
   490 
   491 (*find first element satisfying predicate*)
   492 val find_first = List.find;
   493 
   494 (*get first element by lookup function*)
   495 fun get_first _ [] = NONE
   496   | get_first f (x :: xs) =
   497       (case f x of
   498         NONE => get_first f xs
   499       | some => some);
   500 
   501 fun get_index f =
   502   let
   503     fun get (_: int) [] = NONE
   504       | get i (x :: xs) =
   505           case f x
   506            of NONE => get (i + 1) xs
   507             | SOME y => SOME (i, y)
   508   in get 0 end;
   509 
   510 val flat = List.concat;
   511 
   512 fun unflat (xs :: xss) ys =
   513       let val (ps, qs) = chop (length xs) ys
   514       in ps :: unflat xss qs end
   515   | unflat [] [] = []
   516   | unflat _ _ = raise UnequalLengths;
   517 
   518 fun burrow f xss = unflat xss (f (flat xss));
   519 
   520 fun burrow_options f os = map (try hd) (burrow f (map the_list os));
   521 
   522 fun fold_burrow f xss s =
   523   apfst (unflat xss) (f (flat xss) s);
   524 
   525 (*separate s [x1, x2, ..., xn]  ===>  [x1, s, x2, s, ..., s, xn]*)
   526 fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs
   527   | separate _ xs = xs;
   528 
   529 fun surround s (x :: xs) = s :: x :: surround s xs
   530   | surround s [] = [s];
   531 
   532 (*make the list [x, x, ..., x] of length n*)
   533 fun replicate (n: int) x =
   534   let fun rep (0, xs) = xs
   535         | rep (n, xs) = rep (n - 1, x :: xs)
   536   in
   537     if n < 0 then raise Subscript
   538     else rep (n, [])
   539   end;
   540 
   541 
   542 (* direct product *)
   543 
   544 fun map_product f _ [] = []
   545   | map_product f [] _ = []
   546   | map_product f (x :: xs) ys = map (f x) ys @ map_product f xs ys;
   547 
   548 fun fold_product f _ [] z = z
   549   | fold_product f [] _ z = z
   550   | fold_product f (x :: xs) ys z = z |> fold (f x) ys |> fold_product f xs ys;
   551 
   552 
   553 (* lists of pairs *)
   554 
   555 exception UnequalLengths;
   556 
   557 fun map2 _ [] [] = []
   558   | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys
   559   | map2 _ _ _ = raise UnequalLengths;
   560 
   561 fun fold2 f [] [] z = z
   562   | fold2 f (x :: xs) (y :: ys) z = fold2 f xs ys (f x y z)
   563   | fold2 f _ _ _ = raise UnequalLengths;
   564 
   565 fun map_split f [] = ([], [])
   566   | map_split f (x :: xs) =
   567       let
   568         val (y, w) = f x;
   569         val (ys, ws) = map_split f xs;
   570       in (y :: ys, w :: ws) end;
   571 
   572 fun zip_options (x :: xs) (SOME y :: ys) = (x, y) :: zip_options xs ys
   573   | zip_options (_ :: xs) (NONE :: ys) = zip_options xs ys
   574   | zip_options _ [] = []
   575   | zip_options [] _ = raise UnequalLengths;
   576 
   577 (*combine two lists forming a list of pairs:
   578   [x1, ..., xn] ~~ [y1, ..., yn]  ===>  [(x1, y1), ..., (xn, yn)]*)
   579 fun [] ~~ [] = []
   580   | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys)
   581   | _ ~~ _ = raise UnequalLengths;
   582 
   583 (*inverse of ~~; the old 'split':
   584   [(x1, y1), ..., (xn, yn)]  ===>  ([x1, ..., xn], [y1, ..., yn])*)
   585 val split_list = ListPair.unzip;
   586 
   587 fun burrow_fst f xs = split_list xs |>> f |> op ~~;
   588 
   589 
   590 (* prefixes, suffixes *)
   591 
   592 fun is_prefix _ [] _ = true
   593   | is_prefix eq (x :: xs) (y :: ys) = eq (x, y) andalso is_prefix eq xs ys
   594   | is_prefix eq _ _ = false;
   595 
   596 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
   597    where xi is the first element that does not satisfy the predicate*)
   598 fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
   599   let fun take (rxs, []) = (rev rxs, [])
   600         | take (rxs, x :: xs) =
   601             if  pred x  then  take(x :: rxs, xs)  else  (rev rxs, x :: xs)
   602   in  take([], xs)  end;
   603 
   604 fun chop_prefix eq ([], ys) = ([], ([], ys))
   605   | chop_prefix eq (xs, []) = ([], (xs, []))
   606   | chop_prefix eq (xs as x :: xs', ys as y :: ys') =
   607       if eq (x, y) then
   608         let val (ps', xys'') = chop_prefix eq (xs', ys')
   609         in (x :: ps', xys'') end
   610       else ([], (xs, ys));
   611 
   612 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., xi], [x(i+1), ..., xn])
   613    where xi is the last element that does not satisfy the predicate*)
   614 fun take_suffix _ [] = ([], [])
   615   | take_suffix pred (x :: xs) =
   616       (case take_suffix pred xs of
   617         ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx)
   618       | (prfx, sffx) => (x :: prfx, sffx));
   619 
   620 fun prefixes1 [] = []
   621   | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
   622 
   623 fun prefixes xs = [] :: prefixes1 xs;
   624 
   625 fun suffixes1 xs = map rev (prefixes1 (rev xs));
   626 fun suffixes xs = [] :: suffixes1 xs;
   627 
   628 
   629 
   630 (** integers **)
   631 
   632 (* lists of integers *)
   633 
   634 (*make the list [from, from + 1, ..., to]*)
   635 fun ((i: int) upto j) =
   636   if i > j then [] else i :: (i + 1 upto j);
   637 
   638 (*make the list [from, from - 1, ..., to]*)
   639 fun ((i: int) downto j) =
   640   if i < j then [] else i :: (i - 1 downto j);
   641 
   642 
   643 (* convert integers to strings *)
   644 
   645 (*expand the number in the given base;
   646   example: radixpand (2, 8) gives [1, 0, 0, 0]*)
   647 fun radixpand (base, num) : int list =
   648   let
   649     fun radix (n, tail) =
   650       if n < base then n :: tail
   651       else radix (n div base, (n mod base) :: tail)
   652   in radix (num, []) end;
   653 
   654 (*expands a number into a string of characters starting from "zerochar";
   655   example: radixstring (2, "0", 8) gives "1000"*)
   656 fun radixstring (base, zerochar, num) =
   657   let val offset = ord zerochar;
   658       fun chrof n = chr (offset + n)
   659   in implode (map chrof (radixpand (base, num))) end;
   660 
   661 
   662 val string_of_int = Int.toString;
   663 
   664 fun signed_string_of_int i =
   665   if i < 0 then "-" ^ string_of_int (~ i) else string_of_int i;
   666 
   667 fun string_of_indexname (a, 0) = a
   668   | string_of_indexname (a, i) = a ^ "_" ^ string_of_int i;
   669 
   670 
   671 (* read integers *)
   672 
   673 fun read_radix_int radix cs =
   674   let
   675     val zero = ord "0";
   676     val limit = zero + radix;
   677     fun scan (num, []) = (num, [])
   678       | scan (num, c :: cs) =
   679         if zero <= ord c andalso ord c < limit then
   680           scan (radix * num + (ord c - zero), cs)
   681         else (num, c :: cs);
   682   in scan (0, cs) end;
   683 
   684 val read_int = read_radix_int 10;
   685 
   686 fun oct_char s = chr (#1 (read_radix_int 8 (explode s)));
   687 
   688 
   689 
   690 (** strings **)
   691 
   692 (* functions tuned for strings, avoiding explode *)
   693 
   694 fun nth_string str i =
   695   (case try String.substring (str, i, 1) of
   696     SOME s => s
   697   | NONE => raise Subscript);
   698 
   699 fun fold_string f str x0 =
   700   let
   701     val n = size str;
   702     fun iter (x, i) =
   703       if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x;
   704   in iter (x0, 0) end;
   705 
   706 fun exists_string pred str =
   707   let
   708     val n = size str;
   709     fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1));
   710   in ex 0 end;
   711 
   712 fun forall_string pred = not o exists_string (not o pred);
   713 
   714 fun first_field sep str =
   715   let
   716     val n = size sep;
   717     val len = size str;
   718     fun find i =
   719       if i + n > len then NONE
   720       else if String.substring (str, i, n) = sep then SOME i
   721       else find (i + 1);
   722   in
   723     (case find 0 of
   724       NONE => NONE
   725     | SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE)))
   726   end;
   727 
   728 (*enclose in brackets*)
   729 fun enclose lpar rpar str = lpar ^ str ^ rpar;
   730 fun unenclose str = String.substring (str, 1, size str - 2);
   731 
   732 (*simple quoting (does not escape special chars)*)
   733 val quote = enclose "\"" "\"";
   734 
   735 (*space_implode "..." (explode "hello") = "h...e...l...l...o"*)
   736 fun space_implode a bs = implode (separate a bs);
   737 
   738 val commas = space_implode ", ";
   739 val commas_quote = commas o map quote;
   740 
   741 (*concatenate messages, one per line, into a string*)
   742 val cat_lines = space_implode "\n";
   743 
   744 (*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*)
   745 fun space_explode _ "" = []
   746   | space_explode sep s = String.fields (fn c => str c = sep) s;
   747 
   748 val split_lines = space_explode "\n";
   749 
   750 fun prefix_lines "" txt = txt
   751   | prefix_lines prfx txt = txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines;
   752 
   753 fun prefix prfx s = prfx ^ s;
   754 fun suffix sffx s = s ^ sffx;
   755 
   756 fun unprefix prfx s =
   757   if String.isPrefix prfx s then String.substring (s, size prfx, size s - size prfx)
   758   else raise Fail "unprefix";
   759 
   760 fun unsuffix sffx s =
   761   if String.isSuffix sffx s then String.substring (s, 0, size s - size sffx)
   762   else raise Fail "unsuffix";
   763 
   764 fun replicate_string (0: int) _ = ""
   765   | replicate_string 1 a = a
   766   | replicate_string k a =
   767       if k mod 2 = 0 then replicate_string (k div 2) (a ^ a)
   768       else replicate_string (k div 2) (a ^ a) ^ a;
   769 
   770 fun translate_string f = String.translate (f o String.str);
   771 
   772 (*crude matching of str against simple glob pat*)
   773 fun match_string pat str =
   774   let
   775     fun match [] _ = true
   776       | match (p :: ps) s =
   777           size p <= size s andalso
   778             (case try (unprefix p) s of
   779               SOME s' => match ps s'
   780             | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
   781   in match (space_explode "*" pat) str end;
   782 
   783 (** lists as sets -- see also Pure/General/ord_list.ML **)
   784 
   785 (* canonical operations *)
   786 
   787 fun member eq list x =
   788   let
   789     fun memb [] = false
   790       | memb (y :: ys) = eq (x, y) orelse memb ys;
   791   in memb list end;
   792 
   793 fun insert eq x xs = if member eq xs x then xs else x :: xs;
   794 fun remove eq x xs = if member eq xs x then filter_out (fn y => eq (x, y)) xs else xs;
   795 fun update eq x xs = cons x (remove eq x xs);
   796 
   797 fun subtract eq = fold (remove eq);
   798 
   799 fun merge eq (xs, ys) =
   800   if pointer_eq (xs, ys) then xs
   801   else if null xs then ys
   802   else fold_rev (insert eq) ys xs;
   803 
   804 
   805 (* old-style infixes *)
   806 
   807 fun x mem xs = member (op =) xs x;
   808 fun (x: int) mem_int xs = member (op =) xs x;
   809 fun (x: string) mem_string xs = member (op =) xs x;
   810 
   811 (*union of sets represented as lists: no repetitions*)
   812 fun xs union [] = xs
   813   | [] union ys = ys
   814   | (x :: xs) union ys = xs union (insert (op =) x ys);
   815 
   816 (*union of sets, optimized version for ints*)
   817 fun (xs:int list) union_int [] = xs
   818   | [] union_int ys = ys
   819   | (x :: xs) union_int ys = xs union_int (insert (op =) x ys);
   820 
   821 (*union of sets, optimized version for strings*)
   822 fun (xs:string list) union_string [] = xs
   823   | [] union_string ys = ys
   824   | (x :: xs) union_string ys = xs union_string (insert (op =) x ys);
   825 
   826 (*generalized union*)
   827 fun gen_union eq (xs, []) = xs
   828   | gen_union eq ([], ys) = ys
   829   | gen_union eq (x :: xs, ys) = gen_union eq (xs, insert eq x ys);
   830 
   831 
   832 (*intersection*)
   833 fun [] inter ys = []
   834   | (x :: xs) inter ys =
   835       if x mem ys then x :: (xs inter ys) else xs inter ys;
   836 
   837 (*intersection, optimized version for ints*)
   838 fun ([]:int list) inter_int ys = []
   839   | (x :: xs) inter_int ys =
   840       if x mem_int ys then x :: (xs inter_int ys) else xs inter_int ys;
   841 
   842 (*intersection, optimized version for strings *)
   843 fun ([]:string list) inter_string ys = []
   844   | (x :: xs) inter_string ys =
   845       if x mem_string ys then x :: (xs inter_string ys) else xs inter_string ys;
   846 
   847 (*generalized intersection*)
   848 fun gen_inter eq ([], ys) = []
   849   | gen_inter eq (x::xs, ys) =
   850       if member eq ys x then x :: gen_inter eq (xs, ys)
   851       else gen_inter eq (xs, ys);
   852 
   853 
   854 (*subset*)
   855 fun [] subset ys = true
   856   | (x :: xs) subset ys = x mem ys andalso xs subset ys;
   857 
   858 (*subset, optimized version for ints*)
   859 fun ([]: int list) subset_int ys = true
   860   | (x :: xs) subset_int ys = x mem_int ys andalso xs subset_int ys;
   861 
   862 (*subset, optimized version for strings*)
   863 fun ([]: string list) subset_string ys = true
   864   | (x :: xs) subset_string ys = x mem_string ys andalso xs subset_string ys;
   865 
   866 (*set equality*)
   867 fun eq_set (xs, ys) =
   868   xs = ys orelse (xs subset ys andalso ys subset xs);
   869 
   870 (*set equality for strings*)
   871 fun eq_set_string ((xs: string list), ys) =
   872   xs = ys orelse (xs subset_string ys andalso ys subset_string xs);
   873 
   874 fun gen_subset eq (xs, ys) = forall (member eq ys) xs;
   875 
   876 fun gen_eq_set eq (xs, ys) =
   877   eq_list eq (xs, ys) orelse
   878     (gen_subset eq (xs, ys) andalso gen_subset (eq o swap) (ys, xs));
   879 
   880 
   881 (*removing an element from a list WITHOUT duplicates*)
   882 fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x)
   883   | [] \ x = [];
   884 fun ys \\ xs = foldl (op \) (ys,xs);
   885 
   886 
   887 (*makes a list of the distinct members of the input; preserves order, takes
   888   first of equal elements*)
   889 fun distinct eq lst =
   890   let
   891     fun dist (rev_seen, []) = rev rev_seen
   892       | dist (rev_seen, x :: xs) =
   893           if member eq rev_seen x then dist (rev_seen, xs)
   894           else dist (x :: rev_seen, xs);
   895   in dist ([], lst) end;
   896 
   897 (*returns a list containing all repeated elements exactly once; preserves
   898   order, takes first of equal elements*)
   899 fun duplicates eq lst =
   900   let
   901     fun dups (rev_dups, []) = rev rev_dups
   902       | dups (rev_dups, x :: xs) =
   903           if member eq rev_dups x orelse not (member eq xs x) then
   904             dups (rev_dups, xs)
   905           else dups (x :: rev_dups, xs);
   906   in dups ([], lst) end;
   907 
   908 fun has_duplicates eq =
   909   let
   910     fun dups [] = false
   911       | dups (x :: xs) = member eq xs x orelse dups xs;
   912   in dups end;
   913 
   914 
   915 (* matrices *)
   916 
   917 fun map_transpose f xss =
   918   let
   919     val n = case distinct (op =) (map length xss)
   920      of [] => 0
   921       | [n] => n
   922       | _ => raise UnequalLengths;
   923   in map (fn m => f (map (fn xs => nth xs m) xss)) (0 upto n - 1) end;
   924 
   925 
   926 
   927 (** lists as multisets **)
   928 
   929 fun remove1 _ _ [] = raise Empty
   930   | remove1 eq y (x::xs) = if eq (y, x) then xs else x :: remove1 eq y xs;
   931 
   932 fun submultiset _ ([], _)  = true
   933   | submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys);
   934 
   935 
   936 
   937 (** orders **)
   938 
   939 fun is_equal EQUAL = true
   940   | is_equal _ = false;
   941 
   942 fun rev_order LESS = GREATER
   943   | rev_order EQUAL = EQUAL
   944   | rev_order GREATER = LESS;
   945 
   946 (*assume rel is a linear strict order*)
   947 fun make_ord rel (x, y) =
   948   if rel (x, y) then LESS
   949   else if rel (y, x) then GREATER
   950   else EQUAL;
   951 
   952 fun bool_ord (false, true) = LESS
   953   | bool_ord (true, false) = GREATER
   954   | bool_ord _ = EQUAL;
   955 
   956 val int_ord = Int.compare;
   957 val string_ord = String.compare;
   958 
   959 fun fast_string_ord (s1, s2) =
   960   (case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2) | ord => ord);
   961 
   962 fun option_ord ord (SOME x, SOME y) = ord (x, y)
   963   | option_ord _ (NONE, NONE) = EQUAL
   964   | option_ord _ (NONE, SOME _) = LESS
   965   | option_ord _ (SOME _, NONE) = GREATER;
   966 
   967 (*lexicographic product*)
   968 fun prod_ord a_ord b_ord ((x, y), (x', y')) =
   969   (case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord);
   970 
   971 (*dictionary order -- in general NOT well-founded!*)
   972 fun dict_ord elem_ord (x :: xs, y :: ys) =
   973       (case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord)
   974   | dict_ord _ ([], []) = EQUAL
   975   | dict_ord _ ([], _ :: _) = LESS
   976   | dict_ord _ (_ :: _, []) = GREATER;
   977 
   978 (*lexicographic product of lists*)
   979 fun list_ord elem_ord (xs, ys) =
   980   (case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord);
   981 
   982 
   983 (* sorting *)
   984 
   985 (*quicksort -- stable, i.e. does not reorder equal elements*)
   986 fun quicksort unique ord =
   987   let
   988     fun qsort [] = []
   989       | qsort (xs as [_]) = xs
   990       | qsort (xs as [x, y]) =
   991           (case ord (x, y) of
   992             LESS => xs
   993           | EQUAL => if unique then [x] else xs
   994           | GREATER => [y, x])
   995       | qsort xs =
   996           let val (lts, eqs, gts) = part (nth xs (length xs div 2)) xs
   997           in qsort lts @ eqs @ qsort gts end
   998     and part _ [] = ([], [], [])
   999       | part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs)
  1000     and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts)
  1001       | add EQUAL x (lts, [], gts) = (lts, [x], gts)
  1002       | add EQUAL x (res as (lts, eqs, gts)) = if unique then res else (lts, x :: eqs, gts)
  1003       | add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts);
  1004   in qsort end;
  1005 
  1006 fun sort ord = quicksort false ord;
  1007 fun sort_distinct ord = quicksort true ord;
  1008 
  1009 val sort_strings = sort string_ord;
  1010 fun sort_wrt sel xs = sort (string_ord o pairself sel) xs;
  1011 
  1012 
  1013 (* items tagged by integer index *)
  1014 
  1015 (*insert tags*)
  1016 fun tag_list k [] = []
  1017   | tag_list k (x :: xs) = (k:int, x) :: tag_list (k + 1) xs;
  1018 
  1019 (*remove tags and suppress duplicates -- list is assumed sorted!*)
  1020 fun untag_list [] = []
  1021   | untag_list [(k: int, x)] = [x]
  1022   | untag_list ((k, x) :: (rest as (k', x') :: _)) =
  1023       if k = k' then untag_list rest
  1024       else x :: untag_list rest;
  1025 
  1026 (*return list elements in original order*)
  1027 fun order_list list = untag_list (sort (int_ord o pairself fst) list);
  1028 
  1029 
  1030 
  1031 (** random numbers **)
  1032 
  1033 exception RANDOM;
  1034 
  1035 fun rmod x y = x - y * Real.realFloor (x / y);
  1036 
  1037 local
  1038   val a = 16807.0;
  1039   val m = 2147483647.0;
  1040   val random_seed = Unsynchronized.ref 1.0;
  1041 in
  1042 
  1043 fun random () = CRITICAL (fn () =>
  1044   let val r = rmod (a * ! random_seed) m
  1045   in (random_seed := r; r) end);
  1046 
  1047 end;
  1048 
  1049 fun random_range l h =
  1050   if h < l orelse l < 0 then raise RANDOM
  1051   else l + Real.floor (rmod (random ()) (real (h - l + 1)));
  1052 
  1053 fun one_of xs = nth xs (random_range 0 (length xs - 1));
  1054 
  1055 fun frequency xs =
  1056   let
  1057     val sum = foldl op + (0, map fst xs);
  1058     fun pick n ((k: int, x) :: xs) =
  1059       if n <= k then x else pick (n - k) xs
  1060   in pick (random_range 1 sum) xs end;
  1061 
  1062 
  1063 
  1064 (** misc **)
  1065 
  1066 fun divide_and_conquer decomp x =
  1067   let val (ys, recomb) = decomp x
  1068   in recomb (map (divide_and_conquer decomp) ys) end;
  1069 
  1070 
  1071 (*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
  1072    putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
  1073 fun partition_list p i j =
  1074   let fun part k xs =
  1075             if k>j then
  1076               (case xs of [] => []
  1077                          | _ => raise Fail "partition_list")
  1078             else
  1079             let val (ns, rest) = List.partition (p k) xs;
  1080             in  ns :: part(k+1)rest  end
  1081   in  part (i: int) end;
  1082 
  1083 fun partition_eq (eq:'a * 'a -> bool) =
  1084   let
  1085     fun part [] = []
  1086       | part (x :: ys) =
  1087           let val (xs, xs') = List.partition (fn y => eq (x, y)) ys
  1088           in (x::xs)::(part xs') end
  1089   in part end;
  1090 
  1091 
  1092 
  1093 (* generating identifiers *)
  1094 
  1095 (** Freshly generated identifiers; supplied prefix MUST start with a letter **)
  1096 local
  1097 (*Maps 0-61 to A-Z, a-z, 0-9; exclude _ or ' to avoid clash with internal/unusual indentifiers*)
  1098 fun gensym_char i =
  1099   if i<26 then chr (ord "A" + i)
  1100   else if i<52 then chr (ord "a" + i - 26)
  1101   else chr (ord "0" + i - 52);
  1102 
  1103 val char_vec = Vector.tabulate (62, gensym_char);
  1104 fun newid n = implode (map (fn i => Vector.sub (char_vec, i)) (radixpand (62, n)));
  1105 
  1106 val gensym_seed = Unsynchronized.ref (0: int);
  1107 
  1108 in
  1109   fun gensym pre =
  1110     pre ^ newid (NAMED_CRITICAL "gensym" (fn () => Unsynchronized.inc gensym_seed));
  1111 end;
  1112 
  1113 
  1114 (* stamps and serial numbers *)
  1115 
  1116 type stamp = unit Unsynchronized.ref;
  1117 val stamp: unit -> stamp = Unsynchronized.ref;
  1118 
  1119 type serial = int;
  1120 val serial = Multithreading.serial;
  1121 val serial_string = string_of_int o serial;
  1122 
  1123 
  1124 (* generic objects *)
  1125 
  1126 (*note that the builtin exception datatype may be extended by new
  1127   constructors at any time*)
  1128 structure Object = struct type T = exn end;
  1129 
  1130 end;
  1131 
  1132 structure Basic_Library: BASIC_LIBRARY = Library;
  1133 open Basic_Library;