src/Pure/library.ML
author haftmann
Thu Sep 08 16:08:50 2005 +0200 (2005-09-08)
changeset 17313 7d97f60293ae
parent 17257 0ab67cb765da
child 17341 ca0e5105c4b1
permissions -rw-r--r--
added the_list, the_default
     1 (*  Title:      Pure/library.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Author:     Markus Wenzel, TU Muenchen
     5 
     6 Basic library: functions, options, pairs, booleans, lists, integers,
     7 rational numbers, strings, lists as sets, association lists, generic
     8 tables, balanced trees, orders, current directory, misc.
     9 *)
    10 
    11 infix 1 |> |-> ||> ||>> |>> |>>> #> #->;
    12 infix 2 ?;
    13 infix 3 o oo ooo oooo;
    14 
    15 infix 4 ~~ upto downto;
    16 infix orf andf prefix \ \\ ins ins_string ins_int mem mem_int mem_string union union_int
    17   union_string inter inter_int inter_string subset subset_int subset_string;
    18 
    19 signature BASIC_LIBRARY =
    20 sig
    21   (*functions*)
    22   val I: 'a -> 'a
    23   val K: 'a -> 'b -> 'a
    24   val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c
    25   val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c
    26   val |> : 'a * ('a -> 'b) -> 'b
    27   val |-> : ('c * 'a) * ('c -> 'a -> 'b) -> 'b
    28   val ||> : ('c * 'a) * ('a -> 'b) -> 'c * 'b
    29   val ||>> : ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b
    30   val |>> : ('a * 'c) * ('a -> 'b) -> 'b * 'c
    31   val |>>> : ('a * 'c) * ('a -> 'b * 'd) -> 'b * ('c * 'd)
    32   val #> : ('a -> 'b) * ('b -> 'c) -> 'a -> 'c
    33   val #-> : ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd
    34   val ? : ('a -> bool) * ('a -> 'a) -> 'a -> 'a
    35   val ` : ('b -> 'a) -> 'b -> 'a * 'b
    36   val tap: ('b -> 'a) -> 'b -> 'b
    37   val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b
    38   val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b
    39   val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b
    40   val funpow: int -> ('a -> 'a) -> 'a -> 'a
    41   val apl: 'a * ('a * 'b -> 'c) -> 'b -> 'c
    42   val apr: ('a * 'b -> 'c) * 'b -> 'a -> 'c
    43 
    44   (*old options -- invalidated*)
    45   datatype invalid = None of invalid
    46   exception OPTION of invalid
    47 
    48   (*options*)
    49   val the: 'a option -> 'a
    50   val these: 'a list option -> 'a list
    51   val the_default: 'a -> 'a option -> 'a
    52   val the_list: 'a option -> 'a list
    53   val if_none: 'a option -> 'a -> 'a
    54   val is_some: 'a option -> bool
    55   val is_none: 'a option -> bool
    56 
    57   exception ERROR
    58   val try: ('a -> 'b) -> 'a -> 'b option
    59   val can: ('a -> 'b) -> 'a -> bool
    60   datatype 'a result = Result of 'a | Exn of exn
    61   val capture: ('a -> 'b) -> 'a -> 'b result
    62   val release: 'a result -> 'a
    63   val get_result: 'a result -> 'a option
    64   val get_exn: 'a result -> exn option
    65 
    66   (*pairs*)
    67   val pair: 'a -> 'b -> 'a * 'b
    68   val rpair: 'a -> 'b -> 'b * 'a
    69   val fst: 'a * 'b -> 'a
    70   val snd: 'a * 'b -> 'b
    71   val eq_fst: (''a * 'b) * (''a * 'c) -> bool
    72   val eq_snd: ('a * ''b) * ('c * ''b) -> bool
    73   val swap: 'a * 'b -> 'b * 'a
    74   val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c
    75   val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b
    76   val pairself: ('a -> 'b) -> 'a * 'a -> 'b * 'b
    77 
    78   (*booleans*)
    79   val equal: ''a -> ''a -> bool
    80   val not_equal: ''a -> ''a -> bool
    81   val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool
    82   val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool
    83   val exists: ('a -> bool) -> 'a list -> bool
    84   val forall: ('a -> bool) -> 'a list -> bool
    85   val set: bool ref -> bool
    86   val reset: bool ref -> bool
    87   val toggle: bool ref -> bool
    88   val change: 'a ref -> ('a -> 'a) -> unit
    89   val setmp: 'a ref -> 'a -> ('b -> 'c) -> 'b -> 'c
    90   val conditional: bool -> (unit -> unit) -> unit
    91 
    92   (*lists*)
    93   exception UnequalLengths
    94   val cons: 'a -> 'a list -> 'a list
    95   val single: 'a -> 'a list
    96   val append: 'a list -> 'a list -> 'a list
    97   val apply: ('a -> 'a) list -> 'a -> 'a
    98   val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
    99   val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
   100   val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
   101   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
   102   val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
   103   val foldln: ('a * int -> 'b -> 'b) -> 'a list -> 'b -> 'b
   104   val unflat: 'a list list -> 'b list -> 'b list list
   105   val splitAt: int * 'a list -> 'a list * 'a list
   106   val dropwhile: ('a -> bool) -> 'a list -> 'a list
   107   val map_nth_elem: int -> ('a -> 'a) -> 'a list -> 'a list
   108   val split_last: 'a list -> 'a list * 'a
   109   val nth_update: 'a -> int * 'a list -> 'a list
   110   val find_index: ('a -> bool) -> 'a list -> int
   111   val find_index_eq: ''a -> ''a list -> int
   112   val find_first: ('a -> bool) -> 'a list -> 'a option
   113   val get_first: ('a -> 'b option) -> 'a list -> 'b option
   114   val separate: 'a -> 'a list -> 'a list
   115   val replicate: int -> 'a -> 'a list
   116   val multiply: 'a list * 'a list list -> 'a list list
   117   val product: 'a list -> 'b list -> ('a * 'b) list
   118   val filter: ('a -> bool) -> 'a list -> 'a list
   119   val filter_out: ('a -> bool) -> 'a list -> 'a list
   120   val map2: ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list
   121   val exists2: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   122   val forall2: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   123   val seq2: ('a * 'b -> unit) -> 'a list * 'b list -> unit
   124   val ~~ : 'a list * 'b list -> ('a * 'b) list
   125   val split_list: ('a * 'b) list -> 'a list * 'b list
   126   val equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   127   val prefix: ''a list * ''a list -> bool
   128   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
   129   val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
   130   val prefixes1: 'a list -> 'a list list
   131   val suffixes1: 'a list -> 'a list list
   132 
   133   (*integers*)
   134   val gcd: IntInf.int * IntInf.int -> IntInf.int
   135   val lcm: IntInf.int * IntInf.int -> IntInf.int
   136   val inc: int ref -> int
   137   val dec: int ref -> int
   138   val upto: int * int -> int list
   139   val downto: int * int -> int list
   140   val downto0: int list * int -> bool
   141   val radixpand: int * int -> int list
   142   val radixstring: int * string * int -> string
   143   val string_of_int: int -> string
   144   val string_of_indexname: string * int -> string
   145   val read_radixint: int * string list -> int * string list
   146   val read_int: string list -> int * string list
   147   val oct_char: string -> string
   148 
   149   (*rational numbers*)
   150   type rat
   151   exception RAT of string
   152   val rep_rat: rat -> IntInf.int * IntInf.int
   153   val ratge0: rat -> bool
   154   val ratgt0: rat -> bool
   155   val ratle: rat * rat -> bool
   156   val ratlt: rat * rat -> bool
   157   val ratadd: rat * rat -> rat
   158   val ratmul: rat * rat -> rat
   159   val ratinv: rat -> rat
   160   val int_ratdiv: IntInf.int * IntInf.int -> rat
   161   val ratneg: rat -> rat
   162   val rat_of_int: int -> rat
   163   val rat_of_intinf: IntInf.int -> rat
   164   val rat0: rat
   165 
   166   (*strings*)
   167   val nth_elem_string: int * string -> string
   168   val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a
   169   val exists_string: (string -> bool) -> string -> bool
   170   val forall_string: (string -> bool) -> string -> bool
   171   val enclose: string -> string -> string -> string
   172   val unenclose: string -> string
   173   val quote: string -> string
   174   val space_implode: string -> string list -> string
   175   val commas: string list -> string
   176   val commas_quote: string list -> string
   177   val cat_lines: string list -> string
   178   val space_explode: string -> string -> string list
   179   val split_lines: string -> string list
   180   val prefix_lines: string -> string -> string
   181   val untabify: string list -> string list
   182   val suffix: string -> string -> string
   183   val unsuffix: string -> string -> string
   184   val unprefix: string -> string -> string
   185   val replicate_string: int -> string -> string
   186   val translate_string: (string -> string) -> string -> string
   187 
   188   (*lists as sets -- see also Pure/General/ord_list.ML*)
   189   val mem: ''a * ''a list -> bool
   190   val mem_int: int * int list -> bool
   191   val mem_string: string * string list -> bool
   192   val gen_mem: ('a * 'b -> bool) -> 'a * 'b list -> bool
   193   val ins: ''a * ''a list -> ''a list
   194   val ins_int: int * int list -> int list
   195   val ins_string: string * string list -> string list
   196   val gen_ins: ('a * 'a -> bool) -> 'a * 'a list -> 'a list
   197   val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool
   198   val insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
   199   val remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
   200   val union: ''a list * ''a list -> ''a list
   201   val union_int: int list * int list -> int list
   202   val union_string: string list * string list -> string list
   203   val gen_union: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
   204   val gen_inter: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
   205   val inter: ''a list * ''a list -> ''a list
   206   val inter_int: int list * int list -> int list
   207   val inter_string: string list * string list -> string list
   208   val subset: ''a list * ''a list -> bool
   209   val subset_int: int list * int list -> bool
   210   val subset_string: string list * string list -> bool
   211   val eq_set: ''a list * ''a list -> bool
   212   val eq_set_string: string list * string list -> bool
   213   val gen_subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
   214   val \ : ''a list * ''a -> ''a list
   215   val \\ : ''a list * ''a list -> ''a list
   216   val gen_rem: ('a * 'b -> bool) -> 'a list * 'b -> 'a list
   217   val gen_rems: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
   218   val gen_distinct: ('a * 'a -> bool) -> 'a list -> 'a list
   219   val distinct: ''a list -> ''a list
   220   val findrep: ''a list -> ''a list
   221   val gen_duplicates: ('a * 'a -> bool) -> 'a list -> 'a list
   222   val duplicates: ''a list -> ''a list
   223   val has_duplicates: ('a * 'a -> bool) -> 'a list -> bool
   224 
   225   (*association lists -- see also Pure/General/alist.ML*)
   226   val assoc: (''a * 'b) list * ''a -> 'b option
   227   val assoc_int: (int * 'a) list * int -> 'a option
   228   val assoc_string: (string * 'a) list * string -> 'a option
   229   val assoc_string_int: ((string * int) * 'a) list * (string * int) -> 'a option
   230   val assocs: (''a * 'b list) list -> ''a -> 'b list
   231   val gen_assoc: ('a * 'b -> bool) -> ('b * 'c) list * 'a -> 'c option
   232   val overwrite: (''a * 'b) list * (''a * 'b) -> (''a * 'b) list
   233   val gen_overwrite: ('a * 'a -> bool) -> ('a * 'b) list * ('a * 'b) -> ('a * 'b) list
   234 
   235   (*lists as tables*)
   236   val gen_merge_lists: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
   237   val gen_merge_lists': ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
   238   val merge_lists: ''a list -> ''a list -> ''a list
   239   val merge_lists': ''a list -> ''a list -> ''a list
   240   val merge_alists: (''a * 'b) list -> (''a * 'b) list -> (''a * 'b) list
   241   val merge_alists': (''a * 'b) list -> (''a * 'b) list -> (''a * 'b) list
   242 
   243   (*balanced trees*)
   244   exception Balance
   245   val fold_bal: ('a * 'a -> 'a) -> 'a list -> 'a
   246   val access_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> int -> 'a
   247   val accesses_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> 'a list
   248 
   249   (*orders*)
   250   val rev_order: order -> order
   251   val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
   252   val int_ord: int * int -> order
   253   val string_ord: string * string -> order
   254   val fast_string_ord: string * string -> order
   255   val option_ord: ('a * 'b -> order) -> 'a option * 'b option -> order
   256   val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order
   257   val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
   258   val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
   259   val sort: ('a * 'a -> order) -> 'a list -> 'a list
   260   val sort_strings: string list -> string list
   261   val sort_wrt: ('a -> string) -> 'a list -> 'a list
   262   val unique_strings: string list -> string list
   263 
   264   (*random numbers*)
   265   exception RANDOM
   266   val random: unit -> real
   267   val random_range: int -> int -> int
   268   val one_of: 'a list -> 'a
   269   val frequency: (int * 'a) list -> 'a
   270 
   271   (*current directory*)
   272   val cd: string -> unit
   273   val pwd: unit -> string
   274 
   275   (*misc*)
   276   val make_keylist: ('a -> 'b) -> 'a list -> ('a * 'b) list
   277   val keyfilter: ('a * ''b) list -> ''b -> 'a list
   278   val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list
   279   val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list
   280   val gensym: string -> string
   281   val scanwords: (string -> bool) -> string list -> string list
   282   type stamp
   283   val stamp: unit -> stamp
   284   type serial
   285   val serial: unit -> serial
   286   structure Object: sig type T end
   287 end;
   288 
   289 signature LIBRARY =
   290 sig
   291   include BASIC_LIBRARY
   292   val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
   293   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
   294   val take: int * 'a list -> 'a list
   295   val drop: int * 'a list -> 'a list
   296   val nth_elem: int * 'a list -> 'a
   297   val last_elem: 'a list -> 'a
   298   val flat: 'a list list -> 'a list
   299   val seq: ('a -> unit) -> 'a list -> unit
   300   val partition: ('a -> bool) -> 'a list -> 'a list * 'a list
   301   val mapfilter: ('a -> 'b option) -> 'a list -> 'b list
   302 end;
   303 
   304 structure Library: LIBRARY =
   305 struct
   306 
   307 
   308 (** functions **)
   309 
   310 fun I x = x;
   311 fun K x = fn _ => x;
   312 fun curry f x y = f (x, y);
   313 fun uncurry f (x, y) = f x y;
   314 
   315 (*reverse application and structured results*)
   316 fun x |> f = f x;
   317 fun (x, y) |-> f = f x y;
   318 fun (x, y) |>> f = (f x, y);
   319 fun (x, y) ||> f = (x, f y);
   320 fun (x, y) |>>> f = let val (x', z) = f x in (x', (y, z)) end;
   321 fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end;
   322 
   323 (*reverse composition*)
   324 fun f #> g = g o f;
   325 fun f #-> g = uncurry g o f;
   326 
   327 (*conditional application*)
   328 fun b ? f = fn x => if b x then f x else x
   329 
   330 (*view results*)
   331 fun `f = fn x => (f x, x);
   332 fun tap f x = (f x; x);
   333 
   334 (*composition with multiple args*)
   335 fun (f oo g) x y = f (g x y);
   336 fun (f ooo g) x y z = f (g x y z);
   337 fun (f oooo g) x y z w = f (g x y z w);
   338 
   339 (*function exponentiation: f(...(f x)...) with n applications of f*)
   340 fun funpow n f x =
   341   let fun rep (0, x) = x
   342         | rep (n, x) = rep (n - 1, f x)
   343   in rep (n, x) end;
   344 
   345 (*application of (infix) operator to its left or right argument*)
   346 fun apl (x, f) y = f (x, y);
   347 fun apr (f, y) x = f (x, y);
   348 
   349 
   350 (** options **)
   351 
   352 (*invalidate former constructors to prevent accidental use as match-all patterns!*)
   353 datatype invalid = None of invalid;
   354 exception OPTION of invalid;
   355 
   356 val the = Option.valOf;
   357 fun these (SOME x) = x
   358   | these _ = [];
   359 fun the_default x (SOME y) = y
   360   | the_default x _ = x;
   361 fun the_list (SOME x) = [x]
   362   | the_list _ = []
   363 
   364 (*strict!*)
   365 fun if_none NONE y = y
   366   | if_none (SOME x) _ = x;
   367 
   368 fun is_some (SOME _) = true
   369   | is_some NONE = false;
   370 
   371 fun is_none (SOME _) = false
   372   | is_none NONE = true;
   373 
   374 
   375 (* exception handling *)
   376 
   377 exception ERROR;
   378 
   379 fun try f x = SOME (f x)
   380   handle Interrupt => raise Interrupt | ERROR => raise ERROR | _ => NONE;
   381 
   382 fun can f x = is_some (try f x);
   383 
   384 
   385 datatype 'a result =
   386   Result of 'a |
   387   Exn of exn;
   388 
   389 fun capture f x = Result (f x) handle e => Exn e;
   390 
   391 fun release (Result y) = y
   392   | release (Exn e) = raise e;
   393 
   394 fun get_result (Result x) = SOME x
   395   | get_result _ = NONE;
   396 
   397 fun get_exn (Exn exn) = SOME exn
   398   | get_exn _ = NONE;
   399 
   400 
   401 
   402 (** pairs **)
   403 
   404 fun pair x y = (x, y);
   405 fun rpair x y = (y, x);
   406 
   407 fun fst (x, y) = x;
   408 fun snd (x, y) = y;
   409 
   410 fun eq_fst ((x1, _), (x2, _)) = x1 = x2;
   411 fun eq_snd ((_, y1), (_, y2)) = y1 = y2;
   412 
   413 fun swap (x, y) = (y, x);
   414 
   415 (*apply function to components*)
   416 fun apfst f (x, y) = (f x, y);
   417 fun apsnd f (x, y) = (x, f y);
   418 fun pairself f (x, y) = (f x, f y);
   419 
   420 
   421 
   422 (** booleans **)
   423 
   424 (* equality *)
   425 
   426 fun equal x y = x = y;
   427 fun not_equal x y = x <> y;
   428 
   429 
   430 (* operators for combining predicates *)
   431 
   432 fun p orf q = fn x => p x orelse q x;
   433 fun p andf q = fn x => p x andalso q x;
   434 
   435 
   436 (* predicates on lists *)
   437 
   438 (*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*)
   439 fun exists (pred: 'a -> bool) : 'a list -> bool =
   440   let fun boolf [] = false
   441         | boolf (x :: xs) = pred x orelse boolf xs
   442   in boolf end;
   443 
   444 (*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*)
   445 fun forall (pred: 'a -> bool) : 'a list -> bool =
   446   let fun boolf [] = true
   447         | boolf (x :: xs) = pred x andalso boolf xs
   448   in boolf end;
   449 
   450 
   451 (* flags *)
   452 
   453 fun set flag = (flag := true; true);
   454 fun reset flag = (flag := false; false);
   455 fun toggle flag = (flag := not (! flag); ! flag);
   456 
   457 fun change r f = r := f (! r);
   458 
   459 (*temporarily set flag, handling exceptions*)
   460 fun setmp flag value f x =
   461   let
   462     val orig_value = ! flag;
   463     fun return y = (flag := orig_value; y);
   464   in
   465     flag := value;
   466     return (f x handle exn => (return (); raise exn))
   467   end;
   468 
   469 
   470 (* conditional execution *)
   471 
   472 fun conditional b f = if b then f () else ();
   473 
   474 
   475 
   476 (** lists **)
   477 
   478 exception UnequalLengths;
   479 
   480 fun cons x xs = x :: xs;
   481 fun single x = [x];
   482 
   483 fun append xs ys = xs @ ys;
   484 
   485 fun apply [] x = x
   486   | apply (f :: fs) x = apply fs (f x);
   487 
   488 
   489 (* fold *)
   490 
   491 fun fold f =
   492   let
   493     fun fold_aux [] y = y
   494       | fold_aux (x :: xs) y = fold_aux xs (f x y);
   495   in fold_aux end;
   496 
   497 fun fold_rev f =
   498   let
   499     fun fold_aux [] y = y
   500       | fold_aux (x :: xs) y = f x (fold_aux xs y);
   501   in fold_aux end;
   502 
   503 fun fold_map f =
   504   let
   505     fun fold_aux [] y = ([], y)
   506       | fold_aux (x :: xs) y =
   507           let
   508             val (x', y') = f x y;
   509             val (xs', y'') = fold_aux xs y';
   510           in (x' :: xs', y'') end;
   511   in fold_aux end;
   512 
   513 (*the following versions of fold are designed to fit nicely with infixes*)
   514 
   515 (*  (op @) (e, [x1, ..., xn])  ===>  ((e @ x1) @ x2) ... @ xn
   516     for operators that associate to the left (TAIL RECURSIVE)*)
   517 fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
   518   let fun itl (e, [])  = e
   519         | itl (e, a::l) = itl (f(e, a), l)
   520   in  itl end;
   521 
   522 (*  (op @) ([x1, ..., xn], e)  ===>   x1 @ (x2 ... @ (xn @ e))
   523     for operators that associate to the right (not tail recursive)*)
   524 fun foldr f (l, e) =
   525   let fun itr [] = e
   526         | itr (a::l) = f(a, itr l)
   527   in  itr l  end;
   528 
   529 (*  (op @) [x1, ..., xn]  ===>   x1 @ (x2 ... @ (x[n-1] @ xn))
   530     for n > 0, operators that associate to the right (not tail recursive)*)
   531 fun foldr1 f l =
   532   let fun itr [x] = x
   533         | itr (x::l) = f(x, itr l)
   534   in  itr l  end;
   535 
   536 fun foldln f xs e = fst (foldl (fn ((e,i), x) => (f (x,i) e, i+1)) ((e,1),xs));
   537 
   538 fun foldl_map f =
   539   let
   540     fun fold_aux (x, []) = (x, [])
   541       | fold_aux (x, y :: ys) =
   542           let
   543             val (x', y') = f (x, y);
   544             val (x'', ys') = fold_aux (x', ys);
   545           in (x'', y' :: ys') end;
   546   in fold_aux end;
   547 
   548 
   549 (* basic list functions *)
   550 
   551 (*take the first n elements from a list*)
   552 fun take (n, []) = []
   553   | take (n, x :: xs) =
   554       if n > 0 then x :: take (n - 1, xs) else [];
   555 
   556 (*drop the first n elements from a list*)
   557 fun drop (n, []) = []
   558   | drop (n, x :: xs) =
   559       if n > 0 then drop (n - 1, xs) else x :: xs;
   560 
   561 fun splitAt(n,[]) = ([],[])
   562   | splitAt(n,xs as x::ys) =
   563       if n>0 then let val (ps,qs) = splitAt(n-1,ys) in (x::ps,qs) end
   564       else ([],xs)
   565 
   566 fun dropwhile P [] = []
   567   | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys;
   568 
   569 (*return nth element of a list, where 0 designates the first element;
   570   raise EXCEPTION if list too short*)
   571 fun nth_elem (i,xs) = List.nth(xs,i);
   572 
   573 fun map_nth_elem 0 f (x :: xs) = f x :: xs
   574   | map_nth_elem n f (x :: xs) = x :: map_nth_elem (n - 1) f xs
   575   | map_nth_elem _ _ [] = raise Subscript;
   576 
   577 (*last element of a list*)
   578 val last_elem = List.last;
   579 
   580 (*rear decomposition*)
   581 fun split_last [] = raise Empty
   582   | split_last [x] = ([], x)
   583   | split_last (x :: xs) = apfst (cons x) (split_last xs);
   584 
   585 (*update nth element*)
   586 fun nth_update x n_xs =
   587     (case splitAt n_xs of
   588       (_,[]) => raise Subscript
   589     | (prfx, _ :: sffx') => prfx @ (x :: sffx'))
   590 
   591 (*find the position of an element in a list*)
   592 fun find_index pred =
   593   let fun find _ [] = ~1
   594         | find n (x :: xs) = if pred x then n else find (n + 1) xs;
   595   in find 0 end;
   596 
   597 fun find_index_eq x = find_index (equal x);
   598 
   599 (*find first element satisfying predicate*)
   600 fun find_first _ [] = NONE
   601   | find_first pred (x :: xs) =
   602       if pred x then SOME x else find_first pred xs;
   603 
   604 (*get first element by lookup function*)
   605 fun get_first _ [] = NONE
   606   | get_first f (x :: xs) =
   607       (case f x of
   608         NONE => get_first f xs
   609       | some => some);
   610 
   611 (*flatten a list of lists to a list*)
   612 val flat = List.concat;
   613 
   614 fun unflat (xs :: xss) ys =
   615       let val (ps,qs) = splitAt(length xs,ys)
   616       in ps :: unflat xss qs end
   617   | unflat [] [] = []
   618   | unflat _ _ = raise UnequalLengths;
   619 
   620 (*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates
   621   (proc x1; ...; proc xn) for side effects*)
   622 val seq = List.app;
   623 
   624 (*separate s [x1, x2, ..., xn]  ===>  [x1, s, x2, s, ..., s, xn]*)
   625 fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs
   626   | separate _ xs = xs;
   627 
   628 (*make the list [x, x, ..., x] of length n*)
   629 fun replicate n (x: 'a) : 'a list =
   630   let fun rep (0, xs) = xs
   631         | rep (n, xs) = rep (n - 1, x :: xs)
   632   in
   633     if n < 0 then raise Subscript
   634     else rep (n, [])
   635   end;
   636 
   637 fun translate_string f = String.translate (f o String.str);
   638 
   639 (*multiply [a, b, c, ...] * [xs, ys, zs, ...]*)
   640 fun multiply ([], _) = []
   641   | multiply (x :: xs, yss) = map (cons x) yss @ multiply (xs, yss);
   642 
   643 (*direct product*)
   644 fun product _ [] = []
   645   | product [] _ = []
   646   | product (x :: xs) ys = map (pair x) ys @ product xs ys;
   647 
   648 
   649 (* filter *)
   650 
   651 (*copy the list preserving elements that satisfy the predicate*)
   652 val filter = List.filter;
   653 fun filter_out f = filter (not o f);
   654 val mapfilter = List.mapPartial;
   655 
   656 
   657 (* lists of pairs *)
   658 
   659 exception UnequalLengths;
   660 
   661 fun map2 _ ([], []) = []
   662   | map2 f (x :: xs, y :: ys) = f (x, y) :: map2 f (xs, ys)
   663   | map2 _ _ = raise UnequalLengths;
   664 
   665 fun exists2 _ ([], []) = false
   666   | exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys)
   667   | exists2 _ _ = raise UnequalLengths;
   668 
   669 fun forall2 _ ([], []) = true
   670   | forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys)
   671   | forall2 _ _ = raise UnequalLengths;
   672 
   673 fun seq2 _ ([], []) = ()
   674   | seq2 f (x :: xs, y :: ys) = (f (x, y); seq2 f (xs, ys))
   675   | seq2 _ _ = raise UnequalLengths;
   676 
   677 (*combine two lists forming a list of pairs:
   678   [x1, ..., xn] ~~ [y1, ..., yn]  ===>  [(x1, y1), ..., (xn, yn)]*)
   679 fun [] ~~ [] = []
   680   | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys)
   681   | _ ~~ _ = raise UnequalLengths;
   682 
   683 (*inverse of ~~; the old 'split':
   684   [(x1, y1), ..., (xn, yn)]  ===>  ([x1, ..., xn], [y1, ..., yn])*)
   685 val split_list = ListPair.unzip;
   686 
   687 fun equal_lists eq (xs, ys) = length xs = length ys andalso forall2 eq (xs, ys);
   688 
   689 
   690 (* prefixes, suffixes *)
   691 
   692 fun [] prefix _ = true
   693   | (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys)
   694   | _ prefix _ = false;
   695 
   696 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
   697    where xi is the first element that does not satisfy the predicate*)
   698 fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
   699   let fun take (rxs, []) = (rev rxs, [])
   700         | take (rxs, x :: xs) =
   701             if  pred x  then  take(x :: rxs, xs)  else  (rev rxs, x :: xs)
   702   in  take([], xs)  end;
   703 
   704 (* [x1, ..., xi, ..., xn]  --->  ([x1, ..., xi], [x(i+1), ..., xn])
   705    where xi is the last element that does not satisfy the predicate*)
   706 fun take_suffix _ [] = ([], [])
   707   | take_suffix pred (x :: xs) =
   708       (case take_suffix pred xs of
   709         ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx)
   710       | (prfx, sffx) => (x :: prfx, sffx));
   711 
   712 fun prefixes1 [] = []
   713   | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
   714 
   715 fun suffixes1 xs = map rev (prefixes1 (rev xs));
   716 
   717 
   718 
   719 (** integers **)
   720 
   721 fun gcd (x, y) =
   722   let fun gxd x y : IntInf.int =
   723     if y = 0 then x else gxd y (x mod y)
   724   in if x < y then gxd y x else gxd x y end;
   725 
   726 fun lcm (x, y) = (x * y) div gcd (x, y);
   727 
   728 fun inc i = (i := ! i + 1; ! i);
   729 fun dec i = (i := ! i - 1; ! i);
   730 
   731 
   732 (* lists of integers *)
   733 
   734 (*make the list [from, from + 1, ..., to]*)
   735 fun (from upto to) =
   736   if from > to then [] else from :: ((from + 1) upto to);
   737 
   738 (*make the list [from, from - 1, ..., to]*)
   739 fun (from downto to) =
   740   if from < to then [] else from :: ((from - 1) downto to);
   741 
   742 (*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*)
   743 fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1)
   744   | downto0 ([], n) = n = ~1;
   745 
   746 
   747 (* convert integers to strings *)
   748 
   749 (*expand the number in the given base;
   750   example: radixpand (2, 8) gives [1, 0, 0, 0]*)
   751 fun radixpand (base, num) : int list =
   752   let
   753     fun radix (n, tail) =
   754       if n < base then n :: tail
   755       else radix (n div base, (n mod base) :: tail)
   756   in radix (num, []) end;
   757 
   758 (*expands a number into a string of characters starting from "zerochar";
   759   example: radixstring (2, "0", 8) gives "1000"*)
   760 fun radixstring (base, zerochar, num) =
   761   let val offset = ord zerochar;
   762       fun chrof n = chr (offset + n)
   763   in implode (map chrof (radixpand (base, num))) end;
   764 
   765 
   766 val string_of_int = Int.toString;
   767 
   768 fun string_of_indexname (a,0) = a
   769   | string_of_indexname (a,i) = a ^ "_" ^ Int.toString i;
   770 
   771 
   772 (* read integers *)
   773 
   774 fun read_radixint (radix: int, cs) : int * string list =
   775   let val zero = ord"0"
   776       val limit = zero+radix
   777       fun scan (num,[]) = (num,[])
   778         | scan (num, c::cs) =
   779               if  zero <= ord c  andalso  ord c < limit
   780               then scan(radix*num + ord c - zero, cs)
   781               else (num, c::cs)
   782   in  scan(0,cs)  end;
   783 
   784 fun read_int cs = read_radixint (10, cs);
   785 
   786 fun oct_char s = chr (#1 (read_radixint (8, explode s)));
   787 
   788 
   789 
   790 (** strings **)
   791 
   792 (* functions tuned for strings, avoiding explode *)
   793 
   794 fun nth_elem_string (i, str) =
   795   (case try String.substring (str, i, 1) of
   796     SOME s => s
   797   | NONE => raise Subscript);
   798 
   799 fun fold_string f str x0 =
   800   let
   801     val n = size str;
   802     fun iter (x, i) =
   803       if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x;
   804   in iter (x0, 0) end;
   805 
   806 fun exists_string pred str =
   807   let
   808     val n = size str;
   809     fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1));
   810   in ex 0 end;
   811 
   812 fun forall_string pred = not o exists_string (not o pred);
   813 
   814 (*enclose in brackets*)
   815 fun enclose lpar rpar str = lpar ^ str ^ rpar;
   816 fun unenclose str = String.substring (str, 1, size str - 2);
   817 
   818 (*simple quoting (does not escape special chars)*)
   819 val quote = enclose "\"" "\"";
   820 
   821 (*space_implode "..." (explode "hello") = "h...e...l...l...o"*)
   822 fun space_implode a bs = implode (separate a bs);
   823 
   824 val commas = space_implode ", ";
   825 val commas_quote = commas o map quote;
   826 
   827 (*concatenate messages, one per line, into a string*)
   828 val cat_lines = space_implode "\n";
   829 
   830 (*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*)
   831 fun space_explode _ "" = []
   832   | space_explode sep str =
   833       let
   834         fun expl chs =
   835           (case take_prefix (not_equal sep) chs of
   836             (cs, []) => [implode cs]
   837           | (cs, _ :: cs') => implode cs :: expl cs');
   838       in expl (explode str) end;
   839 
   840 val split_lines = space_explode "\n";
   841 
   842 fun prefix_lines "" txt = txt
   843   | prefix_lines prfx txt = txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines;
   844 
   845 fun untabify chs =
   846   let
   847     val tab_width = 8;
   848 
   849     fun untab (_, "\n") = (0, ["\n"])
   850       | untab (pos, "\t") =
   851           let val d = tab_width - (pos mod tab_width) in (pos + d, replicate d " ") end
   852       | untab (pos, c) = (pos + 1, [c]);
   853   in
   854     if not (exists (equal "\t") chs) then chs
   855     else flat (#2 (foldl_map untab (0, chs)))
   856   end;
   857 
   858 fun suffix sffx s = s ^ sffx;
   859 
   860 fun unsuffix sffx s =
   861   if String.isSuffix sffx s then String.substring (s, 0, size s - size sffx)
   862   else raise Fail "unsuffix";
   863 
   864 fun unprefix prfx s =
   865   if String.isPrefix prfx s then String.substring (s, size prfx, size s - size prfx)
   866   else raise Fail "unprefix";
   867 
   868 fun replicate_string 0 _ = ""
   869   | replicate_string 1 a = a
   870   | replicate_string k a =
   871       if k mod 2 = 0 then replicate_string (k div 2) (a ^ a)
   872       else replicate_string (k div 2) (a ^ a) ^ a;
   873 
   874 
   875 
   876 (** lists as sets -- see also Pure/General/ord_list.ML **)
   877 
   878 (*membership in a list*)
   879 fun x mem [] = false
   880   | x mem (y :: ys) = x = y orelse x mem ys;
   881 
   882 (*membership in a list, optimized version for ints*)
   883 fun (x:int) mem_int [] = false
   884   | x mem_int (y :: ys) = x = y orelse x mem_int ys;
   885 
   886 (*membership in a list, optimized version for strings*)
   887 fun (x:string) mem_string [] = false
   888   | x mem_string (y :: ys) = x = y orelse x mem_string ys;
   889 
   890 (*generalized membership test*)
   891 fun gen_mem eq (x, []) = false
   892   | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys);
   893 
   894 (*member, insert, and remove -- with canonical argument order*)
   895 fun member eq xs x = gen_mem eq (x, xs);
   896 fun insert eq x xs = if gen_mem eq (x, xs) then xs else x :: xs;
   897 fun remove eq x xs = if gen_mem eq (x, xs) then filter_out (fn y => eq (x, y)) xs else xs;
   898 
   899 (*insertion into list if not already there*)
   900 fun (x ins xs) = if x mem xs then xs else x :: xs;
   901 
   902 (*insertion into list, optimized version for ints*)
   903 fun (x ins_int xs) = if x mem_int xs then xs else x :: xs;
   904 
   905 (*insertion into list, optimized version for strings*)
   906 fun (x ins_string xs) = if x mem_string xs then xs else x :: xs;
   907 
   908 (*generalized insertion*)
   909 fun gen_ins eq (x, xs) = insert eq x xs;
   910 
   911 (*union of sets represented as lists: no repetitions*)
   912 fun xs union [] = xs
   913   | [] union ys = ys
   914   | (x :: xs) union ys = xs union (x ins ys);
   915 
   916 (*union of sets, optimized version for ints*)
   917 fun (xs:int list) union_int [] = xs
   918   | [] union_int ys = ys
   919   | (x :: xs) union_int ys = xs union_int (x ins_int ys);
   920 
   921 (*union of sets, optimized version for strings*)
   922 fun (xs:string list) union_string [] = xs
   923   | [] union_string ys = ys
   924   | (x :: xs) union_string ys = xs union_string (x ins_string ys);
   925 
   926 (*generalized union*)
   927 fun gen_union eq (xs, []) = xs
   928   | gen_union eq ([], ys) = ys
   929   | gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys));
   930 
   931 
   932 (*intersection*)
   933 fun [] inter ys = []
   934   | (x :: xs) inter ys =
   935       if x mem ys then x :: (xs inter ys) else xs inter ys;
   936 
   937 (*intersection, optimized version for ints*)
   938 fun ([]:int list) inter_int ys = []
   939   | (x :: xs) inter_int ys =
   940       if x mem_int ys then x :: (xs inter_int ys) else xs inter_int ys;
   941 
   942 (*intersection, optimized version for strings *)
   943 fun ([]:string list) inter_string ys = []
   944   | (x :: xs) inter_string ys =
   945       if x mem_string ys then x :: (xs inter_string ys) else xs inter_string ys;
   946 
   947 (*generalized intersection*)
   948 fun gen_inter eq ([], ys) = []
   949   | gen_inter eq (x::xs, ys) =
   950       if gen_mem eq (x,ys) then x :: gen_inter eq (xs, ys)
   951                            else      gen_inter eq (xs, ys);
   952 
   953 
   954 (*subset*)
   955 fun [] subset ys = true
   956   | (x :: xs) subset ys = x mem ys andalso xs subset ys;
   957 
   958 (*subset, optimized version for ints*)
   959 fun ([]: int list) subset_int ys = true
   960   | (x :: xs) subset_int ys = x mem_int ys andalso xs subset_int ys;
   961 
   962 (*subset, optimized version for strings*)
   963 fun ([]: string list) subset_string ys = true
   964   | (x :: xs) subset_string ys = x mem_string ys andalso xs subset_string ys;
   965 
   966 (*set equality*)
   967 fun eq_set (xs, ys) =
   968   xs = ys orelse (xs subset ys andalso ys subset xs);
   969 
   970 (*set equality for strings*)
   971 fun eq_set_string ((xs: string list), ys) =
   972   xs = ys orelse (xs subset_string ys andalso ys subset_string xs);
   973 
   974 fun gen_subset eq (xs, ys) = forall (fn x => gen_mem eq (x, ys)) xs;
   975 
   976 
   977 (*removing an element from a list WITHOUT duplicates*)
   978 fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x)
   979   | [] \ x = [];
   980 
   981 fun ys \\ xs = foldl (op \) (ys,xs);
   982 
   983 (*removing an element from a list -- possibly WITH duplicates*)
   984 fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs;
   985 fun gen_rems eq (xs, ys) = filter_out (fn x => gen_mem eq (x, ys)) xs;
   986 
   987 (*makes a list of the distinct members of the input; preserves order, takes
   988   first of equal elements*)
   989 fun gen_distinct eq lst =
   990   let
   991     val memb = gen_mem eq;
   992 
   993     fun dist (rev_seen, []) = rev rev_seen
   994       | dist (rev_seen, x :: xs) =
   995           if memb (x, rev_seen) then dist (rev_seen, xs)
   996           else dist (x :: rev_seen, xs);
   997   in
   998     dist ([], lst)
   999   end;
  1000 
  1001 fun distinct l = gen_distinct (op =) l;
  1002 
  1003 (*returns the tail beginning with the first repeated element, or []*)
  1004 fun findrep [] = []
  1005   | findrep (x :: xs) = if x mem xs then x :: xs else findrep xs;
  1006 
  1007 
  1008 (*returns a list containing all repeated elements exactly once; preserves
  1009   order, takes first of equal elements*)
  1010 fun gen_duplicates eq lst =
  1011   let
  1012     val memb = gen_mem eq;
  1013 
  1014     fun dups (rev_dups, []) = rev rev_dups
  1015       | dups (rev_dups, x :: xs) =
  1016           if memb (x, rev_dups) orelse not (memb (x, xs)) then
  1017             dups (rev_dups, xs)
  1018           else dups (x :: rev_dups, xs);
  1019   in
  1020     dups ([], lst)
  1021   end;
  1022 
  1023 fun duplicates l = gen_duplicates (op =) l;
  1024 
  1025 fun has_duplicates eq =
  1026   let
  1027     fun dups [] = false
  1028       | dups (x :: xs) = member eq xs x orelse dups xs;
  1029   in dups end;
  1030 
  1031 
  1032 
  1033 (** association lists **)
  1034 
  1035 (*association list lookup*)
  1036 fun assoc ([], key) = NONE
  1037   | assoc ((keyi, xi) :: pairs, key) =
  1038       if key = keyi then SOME xi else assoc (pairs, key);
  1039 
  1040 (*association list lookup, optimized version for ints*)
  1041 fun assoc_int ([], (key:int)) = NONE
  1042   | assoc_int ((keyi, xi) :: pairs, key) =
  1043       if key = keyi then SOME xi else assoc_int (pairs, key);
  1044 
  1045 (*association list lookup, optimized version for strings*)
  1046 fun assoc_string ([], (key:string)) = NONE
  1047   | assoc_string ((keyi, xi) :: pairs, key) =
  1048       if key = keyi then SOME xi else assoc_string (pairs, key);
  1049 
  1050 (*association list lookup, optimized version for string*ints*)
  1051 fun assoc_string_int ([], (key:string*int)) = NONE
  1052   | assoc_string_int ((keyi, xi) :: pairs, key) =
  1053       if key = keyi then SOME xi else assoc_string_int (pairs, key);
  1054 
  1055 fun assocs z = curry (these o assoc) z
  1056 
  1057 (*generalized association list lookup*)
  1058 fun gen_assoc eq ([], key) = NONE
  1059   | gen_assoc eq ((keyi, xi) :: pairs, key) =
  1060       if eq (key, keyi) then SOME xi else gen_assoc eq (pairs, key);
  1061 
  1062 (*association list update*)
  1063 fun overwrite (al, p as (key, _)) =
  1064   let fun over ((q as (keyi, _)) :: pairs) =
  1065             if keyi = key then p :: pairs else q :: (over pairs)
  1066         | over [] = [p]
  1067   in over al end;
  1068 
  1069 fun gen_overwrite eq (al, p as (key, _)) =
  1070   let fun over ((q as (keyi, _)) :: pairs) =
  1071             if eq (keyi, key) then p :: pairs else q :: (over pairs)
  1072         | over [] = [p]
  1073   in over al end;
  1074 
  1075 
  1076 (* lists as tables *)
  1077 
  1078 fun gen_merge_lists _ xs [] = xs
  1079   | gen_merge_lists _ [] ys = ys
  1080   | gen_merge_lists eq xs ys = xs @ gen_rems eq (ys, xs);
  1081 
  1082 fun gen_merge_lists' _ xs [] = xs
  1083   | gen_merge_lists' _ [] ys = ys
  1084   | gen_merge_lists' eq xs ys = gen_rems eq (ys, xs) @ xs;
  1085 
  1086 fun merge_lists xs ys = gen_merge_lists (op =) xs ys;
  1087 fun merge_lists' xs ys = gen_merge_lists' (op =) xs ys;
  1088 fun merge_alists al = gen_merge_lists eq_fst al;
  1089 fun merge_alists' al = gen_merge_lists' eq_fst al;
  1090 
  1091 
  1092 
  1093 (** balanced trees **)
  1094 
  1095 exception Balance;      (*indicates non-positive argument to balancing fun*)
  1096 
  1097 (*balanced folding; avoids deep nesting*)
  1098 fun fold_bal f [x] = x
  1099   | fold_bal f [] = raise Balance
  1100   | fold_bal f xs =
  1101       let val (ps,qs) = splitAt(length xs div 2, xs)
  1102       in  f (fold_bal f ps, fold_bal f qs)  end;
  1103 
  1104 (*construct something of the form f(...g(...(x)...)) for balanced access*)
  1105 fun access_bal (f, g, x) n i =
  1106   let fun acc n i =     (*1<=i<=n*)
  1107           if n=1 then x else
  1108           let val n2 = n div 2
  1109           in  if i<=n2 then f (acc n2 i)
  1110                        else g (acc (n-n2) (i-n2))
  1111           end
  1112   in  if 1<=i andalso i<=n then acc n i else raise Balance  end;
  1113 
  1114 (*construct ALL such accesses; could try harder to share recursive calls!*)
  1115 fun accesses_bal (f, g, x) n =
  1116   let fun acc n =
  1117           if n=1 then [x] else
  1118           let val n2 = n div 2
  1119               val acc2 = acc n2
  1120           in  if n-n2=n2 then map f acc2 @ map g acc2
  1121                          else map f acc2 @ map g (acc (n-n2)) end
  1122   in  if 1<=n then acc n else raise Balance  end;
  1123 
  1124 
  1125 
  1126 (** orders **)
  1127 
  1128 fun rev_order LESS = GREATER
  1129   | rev_order EQUAL = EQUAL
  1130   | rev_order GREATER = LESS;
  1131 
  1132 (*assume rel is a linear strict order*)
  1133 fun make_ord rel (x, y) =
  1134   if rel (x, y) then LESS
  1135   else if rel (y, x) then GREATER
  1136   else EQUAL;
  1137 
  1138 val int_ord = Int.compare;
  1139 val string_ord = String.compare;
  1140 
  1141 fun fast_string_ord (s1, s2) =
  1142   (case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2) | ord => ord);
  1143 
  1144 fun option_ord ord (SOME x, SOME y) = ord (x, y)
  1145   | option_ord _ (NONE, NONE) = EQUAL
  1146   | option_ord _ (NONE, SOME _) = LESS
  1147   | option_ord _ (SOME _, NONE) = GREATER;
  1148 
  1149 (*lexicographic product*)
  1150 fun prod_ord a_ord b_ord ((x, y), (x', y')) =
  1151   (case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord);
  1152 
  1153 (*dictionary order -- in general NOT well-founded!*)
  1154 fun dict_ord elem_ord (x :: xs, y :: ys) =
  1155       (case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord)
  1156   | dict_ord _ ([], []) = EQUAL
  1157   | dict_ord _ ([], _ :: _) = LESS
  1158   | dict_ord _ (_ :: _, []) = GREATER;
  1159 
  1160 (*lexicographic product of lists*)
  1161 fun list_ord elem_ord (xs, ys) =
  1162   (case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord);
  1163 
  1164 
  1165 (* sorting *)
  1166 
  1167 (*quicksort (stable, i.e. does not reorder equal elements)*)
  1168 fun sort ord =
  1169   let
  1170     fun qsort [] = []
  1171       | qsort (xs as [_]) = xs
  1172       | qsort (xs as [x, y]) = if ord (x, y) = GREATER then [y, x] else xs
  1173       | qsort xs =
  1174           let val (lts, eqs, gts) = part (nth_elem (length xs div 2, xs)) xs
  1175           in qsort lts @ eqs @ qsort gts end
  1176     and part _ [] = ([], [], [])
  1177       | part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs)
  1178     and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts)
  1179       | add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts)
  1180       | add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts);
  1181   in qsort end;
  1182 
  1183 (*sort strings*)
  1184 val sort_strings = sort string_ord;
  1185 fun sort_wrt sel xs = sort (string_ord o pairself sel) xs;
  1186 
  1187 fun unique_strings ([]: string list) = []
  1188   | unique_strings [x] = [x]
  1189   | unique_strings (x :: y :: ys) =
  1190       if x = y then unique_strings (y :: ys)
  1191       else x :: unique_strings (y :: ys);
  1192 
  1193 
  1194 (** random numbers **)
  1195 
  1196 exception RANDOM;
  1197 
  1198 fun rmod x y = x - y * Real.realFloor (x / y);
  1199 
  1200 local
  1201   val a = 16807.0;
  1202   val m = 2147483647.0;
  1203   val random_seed = ref 1.0;
  1204 in
  1205 
  1206 fun random () =
  1207   let val r = rmod (a * !random_seed) m
  1208   in (random_seed := r; r) end;
  1209 
  1210 end;
  1211 
  1212 fun random_range l h =
  1213   if h < l orelse l < 0 then raise RANDOM
  1214   else l + Real.floor (rmod (random ()) (real (h - l + 1)));
  1215 
  1216 fun one_of xs = nth_elem (random_range 0 (length xs - 1), xs);
  1217 
  1218 fun frequency xs =
  1219   let
  1220     val sum = foldl op + (0, map fst xs);
  1221     fun pick n ((k, x) :: xs) =
  1222       if n <= k then x else pick (n - k) xs
  1223   in pick (random_range 1 sum) xs end;
  1224 
  1225 
  1226 (** current directory **)
  1227 
  1228 val cd = OS.FileSys.chDir;
  1229 val pwd = OS.FileSys.getDir;
  1230 
  1231 
  1232 
  1233 (** rational numbers **)
  1234 (* Keep them normalized! *)
  1235 
  1236 datatype rat = Rat of bool * IntInf.int * IntInf.int
  1237 
  1238 exception RAT of string;
  1239 
  1240 fun rep_rat(Rat(a,p,q)) = (if a then p else ~p,q)
  1241 
  1242 fun ratnorm(a,p,q) = if p=0 then Rat(a,0,1) else
  1243   let val absp = abs p
  1244       val m = gcd(absp,q)
  1245   in Rat(a = (0 <= p), absp div m, q div m) end;
  1246 
  1247 fun ratcommon(p,q,r,s) =
  1248   let val den = lcm(q,s)
  1249   in (p*(den div q), r*(den div s), den) end
  1250 
  1251 fun ratge0(Rat(a,p,q)) = a;
  1252 fun ratgt0(Rat(a,p,q)) = a andalso p > 0;
  1253 
  1254 fun ratle(Rat(a,p,q),Rat(b,r,s)) =
  1255   not a andalso b orelse
  1256   a = b andalso
  1257     let val (p,r,_) = ratcommon(p,q,r,s)
  1258     in if a then p <= r else r <= p end
  1259 
  1260 fun ratlt(Rat(a,p,q),Rat(b,r,s)) =
  1261   not a andalso b orelse
  1262   a = b andalso
  1263     let val (p,r,_) = ratcommon(p,q,r,s)
  1264     in if a then p < r else r < p end
  1265 
  1266 fun ratadd(Rat(a,p,q),Rat(b,r,s)) =
  1267   let val (p,r,den) = ratcommon(p,q,r,s)
  1268       val num = (if a then p else ~p) + (if b then r else ~r)
  1269   in ratnorm(true,num,den) end;
  1270 
  1271 fun ratmul(Rat(a,p,q),Rat(b,r,s)) = ratnorm(a=b,p*r,q*s)
  1272 
  1273 fun ratinv(Rat(a,p,q)) = if p=0 then raise RAT "ratinv" else Rat(a,q,p)
  1274 
  1275 fun int_ratdiv(p,q) =
  1276   if q=0 then raise RAT "int_ratdiv" else ratnorm(0<=q, p, abs q)
  1277 
  1278 fun ratneg(Rat(b,p,q)) = Rat(not b,p,q);
  1279 
  1280 fun rat_of_intinf i = if i < 0 then Rat(false,abs i,1) else Rat(true,i,1);
  1281 
  1282 fun rat_of_int i = rat_of_intinf (IntInf.fromInt i);
  1283 
  1284 val rat0 = rat_of_int 0; 
  1285 
  1286 (** misc **)
  1287 
  1288 (*use the keyfun to make a list of (x, key) pairs*)
  1289 fun make_keylist (keyfun: 'a->'b) : 'a list -> ('a * 'b) list =
  1290   let fun keypair x = (x, keyfun x)
  1291   in map keypair end;
  1292 
  1293 (*given a list of (x, key) pairs and a searchkey
  1294   return the list of xs from each pair whose key equals searchkey*)
  1295 fun keyfilter [] searchkey = []
  1296   | keyfilter ((x, key) :: pairs) searchkey =
  1297       if key = searchkey then x :: keyfilter pairs searchkey
  1298       else keyfilter pairs searchkey;
  1299 
  1300 
  1301 (*Partition list into elements that satisfy predicate and those that don't.
  1302   Preserves order of elements in both lists.*)
  1303 val partition = List.partition;
  1304 
  1305 fun partition_eq (eq:'a * 'a -> bool) =
  1306   let
  1307     fun part [] = []
  1308       | part (x :: ys) =
  1309           let val (xs, xs') = partition (fn y => eq (x, y)) ys
  1310           in (x::xs)::(part xs') end
  1311   in part end;
  1312 
  1313 
  1314 (*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
  1315    putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
  1316 fun partition_list p i j =
  1317   let fun part k xs =
  1318             if k>j then
  1319               (case xs of [] => []
  1320                          | _ => raise Fail "partition_list")
  1321             else
  1322             let val (ns, rest) = partition (p k) xs;
  1323             in  ns :: part(k+1)rest  end
  1324   in  part i end;
  1325 
  1326 
  1327 (* generating identifiers *)
  1328 
  1329 (** Freshly generated identifiers; supplied prefix MUST start with a letter **)
  1330 local
  1331 (*Maps 0-63 to A-Z, a-z, 0-9 or _ or ' for generating random identifiers*)
  1332 fun char i =      if i<26 then chr (ord "A" + i)
  1333              else if i<52 then chr (ord "a" + i - 26)
  1334              else if i<62 then chr (ord"0" + i - 52)
  1335              else if i=62 then "_"
  1336              else  (*i=63*)    "'";
  1337 
  1338 val charVec = Vector.tabulate (64, char);
  1339 
  1340 fun newid n =
  1341   let
  1342   in  implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n)))  end;
  1343 
  1344 val seedr = ref 0;
  1345 
  1346 in
  1347 
  1348 fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr));
  1349 
  1350 end;
  1351 
  1352 
  1353 (* lexical scanning *)
  1354 
  1355 (*scan a list of characters into "words" composed of "letters" (recognized by
  1356   is_let) and separated by any number of non-"letters"*)
  1357 fun scanwords is_let cs =
  1358   let fun scan1 [] = []
  1359         | scan1 cs =
  1360             let val (lets, rest) = take_prefix is_let cs
  1361             in implode lets :: scanwords is_let rest end;
  1362   in scan1 (#2 (take_prefix (not o is_let) cs)) end;
  1363 
  1364 
  1365 (* stamps and serial numbers *)
  1366 
  1367 type stamp = unit ref;
  1368 val stamp: unit -> stamp = ref;
  1369 
  1370 type serial = int;
  1371 local val count = ref 0
  1372 in fun serial () = inc count end;
  1373 
  1374 
  1375 (* generic objects *)
  1376 
  1377 (*note that the builtin exception datatype may be extended by new
  1378   constructors at any time*)
  1379 structure Object = struct type T = exn end;
  1380 
  1381 end;
  1382 
  1383 structure BasicLibrary: BASIC_LIBRARY = Library;
  1384 open BasicLibrary;