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