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