src/Pure/library.ML
author wenzelm
Sat Apr 09 11:21:38 2016 +0200 (2016-04-09)
changeset 62918 2fcbd4abc021
parent 62889 99c7f31615c2
child 63304 00a135c0a17f
permissions -rw-r--r--
clarified modules;
wenzelm@41
     1
(*  Title:      Pure/library.ML
wenzelm@233
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@16188
     3
    Author:     Markus Wenzel, TU Muenchen
clasohm@0
     4
haftmann@28732
     5
Basic library: functions, pairs, booleans, lists, integers,
wenzelm@23424
     6
strings, lists as sets, orders, current directory, misc.
wenzelm@21395
     7
wenzelm@21395
     8
See also General/basics.ML for the most fundamental concepts.
clasohm@0
     9
*)
clasohm@0
    10
wenzelm@21395
    11
infix 2 ?
wenzelm@21395
    12
infix 3 o oo ooo oooo
wenzelm@21395
    13
infix 4 ~~ upto downto
haftmann@36692
    14
infix orf andf
wenzelm@5893
    15
wenzelm@15745
    16
signature BASIC_LIBRARY =
wenzelm@4621
    17
sig
wenzelm@4621
    18
  (*functions*)
wenzelm@23860
    19
  val undefined: 'a -> 'b
wenzelm@16842
    20
  val I: 'a -> 'a
wenzelm@16842
    21
  val K: 'a -> 'b -> 'a
wenzelm@4621
    22
  val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c
wenzelm@4621
    23
  val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c
wenzelm@21565
    24
  val ? : bool * ('a -> 'a) -> 'a -> 'a
wenzelm@16721
    25
  val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b
wenzelm@16721
    26
  val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b
wenzelm@16721
    27
  val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b
wenzelm@16842
    28
  val funpow: int -> ('a -> 'a) -> 'a -> 'a
haftmann@31250
    29
  val funpow_yield: int -> ('a -> 'b * 'a) -> 'a -> 'b list * 'a
clasohm@1364
    30
wenzelm@4621
    31
  (*pairs*)
wenzelm@4621
    32
  val pair: 'a -> 'b -> 'a * 'b
wenzelm@4621
    33
  val rpair: 'a -> 'b -> 'b * 'a
wenzelm@4621
    34
  val fst: 'a * 'b -> 'a
wenzelm@4621
    35
  val snd: 'a * 'b -> 'b
haftmann@17498
    36
  val eq_fst: ('a * 'c -> bool) -> ('a * 'b) * ('c * 'd) -> bool
haftmann@17498
    37
  val eq_snd: ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
haftmann@19454
    38
  val eq_pair: ('a * 'c -> bool) -> ('b * 'd -> bool) -> ('a * 'b) * ('c * 'd) -> bool
wenzelm@4621
    39
  val swap: 'a * 'b -> 'b * 'a
wenzelm@4621
    40
  val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c
wenzelm@4621
    41
  val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b
wenzelm@59058
    42
  val apply2: ('a -> 'b) -> 'a * 'a -> 'b * 'b
wenzelm@4621
    43
wenzelm@4621
    44
  (*booleans*)
wenzelm@4621
    45
  val equal: ''a -> ''a -> bool
wenzelm@4621
    46
  val not_equal: ''a -> ''a -> bool
wenzelm@4621
    47
  val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool
wenzelm@4621
    48
  val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool
wenzelm@4621
    49
  val exists: ('a -> bool) -> 'a list -> bool
wenzelm@4621
    50
  val forall: ('a -> bool) -> 'a list -> bool
wenzelm@4621
    51
wenzelm@4621
    52
  (*lists*)
wenzelm@5285
    53
  val single: 'a -> 'a list
wenzelm@20882
    54
  val the_single: 'a list -> 'a
wenzelm@19273
    55
  val singleton: ('a list -> 'b list) -> 'a -> 'b
haftmann@25061
    56
  val yield_singleton: ('a list -> 'c -> 'b list * 'c) -> 'a -> 'c -> 'b * 'c
wenzelm@25058
    57
  val perhaps_apply: ('a -> 'a option) list -> 'a -> 'a option
wenzelm@25058
    58
  val perhaps_loop: ('a -> 'a option) -> 'a -> 'a option
berghofe@25681
    59
  val foldl1: ('a * 'a -> 'a) -> 'a list -> 'a
wenzelm@15760
    60
  val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
wenzelm@46891
    61
  val eq_list: ('a * 'a -> bool) -> 'a list * 'a list -> bool
wenzelm@21395
    62
  val maps: ('a -> 'b list) -> 'a list -> 'b list
wenzelm@25549
    63
  val filter: ('a -> bool) -> 'a list -> 'a list
wenzelm@25549
    64
  val filter_out: ('a -> bool) -> 'a list -> 'a list
wenzelm@25549
    65
  val map_filter: ('a -> 'b option) -> 'a list -> 'b list
haftmann@33955
    66
  val take: int -> 'a list -> 'a list
haftmann@33955
    67
  val drop: int -> 'a list -> 'a list
wenzelm@19011
    68
  val chop: int -> 'a list -> 'a list * 'a list
wenzelm@46891
    69
  val chop_groups: int -> 'a list -> 'a list list
haftmann@18278
    70
  val nth: 'a list -> int -> 'a
wenzelm@46891
    71
  val nth_list: 'a list list -> int -> 'a list
haftmann@18011
    72
  val nth_map: int -> ('a -> 'a) -> 'a list -> 'a list
haftmann@24846
    73
  val nth_drop: int -> 'a list -> 'a list
haftmann@18514
    74
  val map_index: (int * 'a -> 'b) -> 'a list -> 'b list
haftmann@18514
    75
  val fold_index: (int * 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b
haftmann@33063
    76
  val map_range: (int -> 'a) -> int -> 'a list
haftmann@52271
    77
  val fold_range: (int -> 'a -> 'a) -> int -> 'a -> 'a
wenzelm@4621
    78
  val split_last: 'a list -> 'a list * 'a
wenzelm@46891
    79
  val find_first: ('a -> bool) -> 'a list -> 'a option
wenzelm@4621
    80
  val find_index: ('a -> bool) -> 'a list -> int
wenzelm@46891
    81
  val get_first: ('a -> 'b option) -> 'a list -> 'b option
haftmann@19233
    82
  val get_index: ('a -> 'b option) -> 'a list -> (int * 'b) option
wenzelm@46891
    83
  val flat: 'a list list -> 'a list
wenzelm@46891
    84
  val unflat: 'a list list -> 'b list -> 'b list list
wenzelm@46891
    85
  val grouped: int -> (('a list -> 'b list) -> 'c list list -> 'd list list) ->
wenzelm@46891
    86
    ('a -> 'b) -> 'c list -> 'd list
wenzelm@46891
    87
  val burrow: ('a list -> 'b list) -> 'a list list -> 'b list list
wenzelm@46891
    88
  val burrow_options: ('a list -> 'b list) -> 'a option list -> 'b option list
wenzelm@46891
    89
  val fold_burrow: ('a list -> 'c -> 'b list * 'd) -> 'a list list -> 'c -> 'b list list * 'd
wenzelm@46891
    90
  val separate: 'a -> 'a list -> 'a list
wenzelm@46891
    91
  val surround: 'a -> 'a list -> 'a list
wenzelm@46891
    92
  val replicate: int -> 'a -> 'a list
wenzelm@46891
    93
  val map_product: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
wenzelm@46891
    94
  val fold_product: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
haftmann@18330
    95
  val map2: ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
haftmann@18330
    96
  val fold2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
wenzelm@46891
    97
  val map_split: ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list
wenzelm@19799
    98
  val zip_options: 'a list -> 'b option list -> ('a * 'b) list
haftmann@18330
    99
  val ~~ : 'a list * 'b list -> ('a * 'b) list
haftmann@18330
   100
  val split_list: ('a * 'b) list -> 'a list * 'b list
wenzelm@46891
   101
  val burrow_fst: ('a list -> 'b list) -> ('a * 'c) list -> ('b * 'c) list
haftmann@18441
   102
  val is_prefix: ('a * 'a -> bool) -> 'a list -> 'a list -> bool
wenzelm@4621
   103
  val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
haftmann@20108
   104
  val chop_prefix: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list * ('a list * 'b list)
wenzelm@4621
   105
  val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
wenzelm@12249
   106
  val prefixes1: 'a list -> 'a list list
wenzelm@19011
   107
  val prefixes: 'a list -> 'a list list
wenzelm@12249
   108
  val suffixes1: 'a list -> 'a list list
wenzelm@19011
   109
  val suffixes: 'a list -> 'a list list
wenzelm@61707
   110
  val trim: ('a -> bool) -> 'a list -> 'a list
wenzelm@4621
   111
wenzelm@4621
   112
  (*integers*)
wenzelm@4621
   113
  val upto: int * int -> int list
wenzelm@4621
   114
  val downto: int * int -> int list
wenzelm@4621
   115
  val radixpand: int * int -> int list
wenzelm@4621
   116
  val radixstring: int * string * int -> string
wenzelm@4621
   117
  val string_of_int: int -> string
wenzelm@21942
   118
  val signed_string_of_int: int -> string
wenzelm@4621
   119
  val string_of_indexname: string * int -> string
wenzelm@24630
   120
  val read_radix_int: int -> string list -> int * string list
wenzelm@14826
   121
  val read_int: string list -> int * string list
wenzelm@14826
   122
  val oct_char: string -> string
wenzelm@4621
   123
wenzelm@4621
   124
  (*strings*)
haftmann@18011
   125
  val nth_string: string -> int -> string
wenzelm@16188
   126
  val fold_string: (string -> 'a -> 'a) -> string -> 'a -> 'a
wenzelm@6312
   127
  val exists_string: (string -> bool) -> string -> bool
wenzelm@16188
   128
  val forall_string: (string -> bool) -> string -> bool
wenzelm@28025
   129
  val first_field: string -> string -> (string * string) option
wenzelm@4621
   130
  val enclose: string -> string -> string -> string
wenzelm@6642
   131
  val unenclose: string -> string
wenzelm@4621
   132
  val quote: string -> string
wenzelm@55033
   133
  val cartouche: string -> string
wenzelm@4621
   134
  val space_implode: string -> string list -> string
wenzelm@4621
   135
  val commas: string list -> string
wenzelm@4621
   136
  val commas_quote: string list -> string
wenzelm@4621
   137
  val cat_lines: string list -> string
wenzelm@4621
   138
  val space_explode: string -> string -> string list
wenzelm@14826
   139
  val split_lines: string -> string list
wenzelm@56038
   140
  val plain_words: string -> string
wenzelm@5942
   141
  val prefix_lines: string -> string -> string
wenzelm@18681
   142
  val prefix: string -> string -> string
wenzelm@5285
   143
  val suffix: string -> string -> string
wenzelm@18681
   144
  val unprefix: string -> string -> string
wenzelm@5285
   145
  val unsuffix: string -> string -> string
wenzelm@47499
   146
  val trim_line: string -> string
wenzelm@10951
   147
  val replicate_string: int -> string -> string
wenzelm@14926
   148
  val translate_string: (string -> string) -> string -> string
kleing@29882
   149
  val match_string: string -> string -> bool
wenzelm@4621
   150
wenzelm@41516
   151
  (*reals*)
wenzelm@41516
   152
  val string_of_real: real -> string
wenzelm@41516
   153
  val signed_string_of_real: real -> string
wenzelm@41516
   154
wenzelm@16492
   155
  (*lists as sets -- see also Pure/General/ord_list.ML*)
wenzelm@18923
   156
  val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool
wenzelm@18923
   157
  val insert: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
wenzelm@18923
   158
  val remove: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
wenzelm@24049
   159
  val update: ('a * 'a -> bool) -> 'a -> 'a list -> 'a list
haftmann@33042
   160
  val union: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
wenzelm@19301
   161
  val subtract: ('b * 'a -> bool) -> 'b list -> 'a list -> 'a list
haftmann@33049
   162
  val inter: ('a * 'b -> bool) -> 'b list -> 'a list -> 'a list
wenzelm@18923
   163
  val merge: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
haftmann@33038
   164
  val subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
wenzelm@42403
   165
  val eq_set: ('a * 'a -> bool) -> 'a list * 'a list -> bool
wenzelm@19046
   166
  val distinct: ('a * 'a -> bool) -> 'a list -> 'a list
wenzelm@18966
   167
  val duplicates: ('a * 'a -> bool) -> 'a list -> 'a list
wenzelm@16878
   168
  val has_duplicates: ('a * 'a -> bool) -> 'a list -> bool
wenzelm@46891
   169
  val map_transpose: ('a list -> 'b) -> 'a list list -> 'b list
wenzelm@4621
   170
wenzelm@23220
   171
  (*lists as multisets*)
nipkow@22142
   172
  val remove1: ('b * 'a -> bool) -> 'b -> 'a list -> 'a list
haftmann@33078
   173
  val combine: ('a * 'a -> bool) -> 'a list -> 'a list -> 'a list
haftmann@33079
   174
  val submultiset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
wenzelm@4621
   175
wenzelm@4621
   176
  (*orders*)
wenzelm@18966
   177
  val is_equal: order -> bool
wenzelm@4621
   178
  val rev_order: order -> order
wenzelm@4621
   179
  val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
wenzelm@25224
   180
  val bool_ord: bool * bool -> order
wenzelm@4621
   181
  val int_ord: int * int -> order
wenzelm@4621
   182
  val string_ord: string * string -> order
wenzelm@16676
   183
  val fast_string_ord: string * string -> order
wenzelm@16492
   184
  val option_ord: ('a * 'b -> order) -> 'a option * 'b option -> order
wenzelm@4621
   185
  val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order
wenzelm@4621
   186
  val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
wenzelm@4621
   187
  val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
wenzelm@4621
   188
  val sort: ('a * 'a -> order) -> 'a list -> 'a list
wenzelm@18427
   189
  val sort_distinct: ('a * 'a -> order) -> 'a list -> 'a list
wenzelm@4621
   190
  val sort_strings: string list -> string list
wenzelm@60924
   191
  val sort_by: ('a -> string) -> 'a list -> 'a list
wenzelm@30558
   192
  val tag_list: int -> 'a list -> (int * 'a) list
wenzelm@30558
   193
  val untag_list: (int * 'a) list -> 'a list
wenzelm@30558
   194
  val order_list: (int * 'a) list -> 'a list
wenzelm@4621
   195
wenzelm@4621
   196
  (*misc*)
wenzelm@19644
   197
  val divide_and_conquer: ('a -> 'a list * ('b list -> 'b)) -> 'a -> 'b
wenzelm@32978
   198
  val divide_and_conquer': ('a -> 'b -> ('a list * ('c list * 'b -> 'c * 'b)) * 'b) ->
wenzelm@32978
   199
    'a -> 'b -> 'c * 'b
wenzelm@4621
   200
  val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list
wenzelm@4621
   201
  val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list
wenzelm@40509
   202
  type serial = int
wenzelm@16439
   203
  val serial: unit -> serial
wenzelm@19512
   204
  val serial_string: unit -> string
wenzelm@45626
   205
  eqtype stamp
wenzelm@45626
   206
  val stamp: unit -> stamp
wenzelm@51368
   207
  structure Any: sig type T = exn end
wenzelm@43603
   208
  val getenv: string -> string
wenzelm@43603
   209
  val getenv_strict: string -> string
wenzelm@4621
   210
end;
wenzelm@4621
   211
wenzelm@15745
   212
signature LIBRARY =
skalberg@15570
   213
sig
wenzelm@15745
   214
  include BASIC_LIBRARY
skalberg@15570
   215
  val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
skalberg@15570
   216
  val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
skalberg@15570
   217
end;
skalberg@15570
   218
wenzelm@15745
   219
structure Library: LIBRARY =
clasohm@1364
   220
struct
clasohm@0
   221
wenzelm@21395
   222
(* functions *)
clasohm@0
   223
wenzelm@23860
   224
fun undefined _ = raise Match;
wenzelm@23860
   225
wenzelm@16842
   226
fun I x = x;
wenzelm@16842
   227
fun K x = fn _ => x;
wenzelm@233
   228
fun curry f x y = f (x, y);
wenzelm@233
   229
fun uncurry f (x, y) = f x y;
clasohm@0
   230
haftmann@17141
   231
(*conditional application*)
wenzelm@21565
   232
fun b ? f = fn x => if b then f x else x;
haftmann@17141
   233
wenzelm@16721
   234
(*composition with multiple args*)
wenzelm@16721
   235
fun (f oo g) x y = f (g x y);
wenzelm@16721
   236
fun (f ooo g) x y z = f (g x y z);
wenzelm@16721
   237
fun (f oooo g) x y z w = f (g x y z w);
wenzelm@16721
   238
haftmann@31250
   239
(*function exponentiation: f (... (f x) ...) with n applications of f*)
haftmann@31250
   240
fun funpow (0 : int) _ = I
haftmann@31250
   241
  | funpow n f = f #> funpow (n - 1) f;
wenzelm@160
   242
haftmann@31250
   243
fun funpow_yield (0 : int) _ x = ([], x)
haftmann@31250
   244
  | funpow_yield n f x = x |> f ||>> funpow_yield (n - 1) f |>> op ::;
wenzelm@160
   245
wenzelm@40318
   246
wenzelm@21395
   247
(* pairs *)
wenzelm@233
   248
wenzelm@233
   249
fun pair x y = (x, y);
wenzelm@233
   250
fun rpair x y = (y, x);
wenzelm@233
   251
wenzelm@233
   252
fun fst (x, y) = x;
wenzelm@233
   253
fun snd (x, y) = y;
wenzelm@233
   254
haftmann@17498
   255
fun eq_fst eq ((x1, _), (x2, _)) = eq (x1, x2);
haftmann@17498
   256
fun eq_snd eq ((_, y1), (_, y2)) = eq (y1, y2);
haftmann@19454
   257
fun eq_pair eqx eqy ((x1, y1), (x2, y2)) = eqx (x1, x2) andalso eqy (y1, y2);
wenzelm@233
   258
wenzelm@233
   259
fun swap (x, y) = (y, x);
wenzelm@233
   260
wenzelm@233
   261
fun apfst f (x, y) = (f x, y);
wenzelm@233
   262
fun apsnd f (x, y) = (x, f y);
wenzelm@59058
   263
fun apply2 f (x, y) = (f x, f y);
wenzelm@233
   264
wenzelm@233
   265
wenzelm@21395
   266
(* booleans *)
wenzelm@233
   267
wenzelm@21395
   268
(*polymorphic equality*)
wenzelm@233
   269
fun equal x y = x = y;
wenzelm@233
   270
fun not_equal x y = x <> y;
wenzelm@233
   271
wenzelm@21395
   272
(*combining predicates*)
wenzelm@16721
   273
fun p orf q = fn x => p x orelse q x;
wenzelm@16721
   274
fun p andf q = fn x => p x andalso q x;
wenzelm@233
   275
wenzelm@25752
   276
val exists = List.exists;
wenzelm@25752
   277
val forall = List.all;
clasohm@0
   278
wenzelm@19644
   279
wenzelm@21395
   280
wenzelm@233
   281
(** lists **)
wenzelm@233
   282
wenzelm@5285
   283
fun single x = [x];
wenzelm@233
   284
wenzelm@20882
   285
fun the_single [x] = x
wenzelm@47060
   286
  | the_single _ = raise List.Empty;
wenzelm@20882
   287
wenzelm@20882
   288
fun singleton f x = the_single (f [x]);
wenzelm@19273
   289
haftmann@25061
   290
fun yield_singleton f x = f [x] #>> the_single;
haftmann@25061
   291
wenzelm@25058
   292
fun perhaps_apply funs arg =
wenzelm@25058
   293
  let
wenzelm@25058
   294
    fun app [] res = res
wenzelm@25058
   295
      | app (f :: fs) (changed, x) =
wenzelm@25058
   296
          (case f x of
wenzelm@25058
   297
            NONE => app fs (changed, x)
wenzelm@25058
   298
          | SOME x' => app fs (true, x'));
wenzelm@25058
   299
  in (case app funs (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
wenzelm@25058
   300
wenzelm@25058
   301
fun perhaps_loop f arg =
wenzelm@25058
   302
  let
wenzelm@25058
   303
    fun loop (changed, x) =
wenzelm@25058
   304
      (case f x of
wenzelm@25058
   305
        NONE => (changed, x)
wenzelm@25058
   306
      | SOME x' => loop (true, x'));
wenzelm@25058
   307
  in (case loop (false, arg) of (false, _) => NONE | (true, arg') => SOME arg') end;
wenzelm@25058
   308
wenzelm@233
   309
wenzelm@21395
   310
(* fold -- old versions *)
haftmann@16691
   311
wenzelm@233
   312
(*the following versions of fold are designed to fit nicely with infixes*)
clasohm@0
   313
wenzelm@233
   314
(*  (op @) (e, [x1, ..., xn])  ===>  ((e @ x1) @ x2) ... @ xn
wenzelm@233
   315
    for operators that associate to the left (TAIL RECURSIVE)*)
wenzelm@233
   316
fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a =
wenzelm@233
   317
  let fun itl (e, [])  = e
wenzelm@233
   318
        | itl (e, a::l) = itl (f(e, a), l)
wenzelm@233
   319
  in  itl end;
wenzelm@233
   320
wenzelm@233
   321
(*  (op @) ([x1, ..., xn], e)  ===>   x1 @ (x2 ... @ (xn @ e))
wenzelm@233
   322
    for operators that associate to the right (not tail recursive)*)
wenzelm@233
   323
fun foldr f (l, e) =
wenzelm@233
   324
  let fun itr [] = e
wenzelm@233
   325
        | itr (a::l) = f(a, itr l)
wenzelm@233
   326
  in  itr l  end;
wenzelm@233
   327
berghofe@25681
   328
(*  (op @) [x1, ..., xn]  ===>  ((x1 @ x2) @ x3) ... @ xn
berghofe@25681
   329
    for operators that associate to the left (TAIL RECURSIVE)*)
wenzelm@47060
   330
fun foldl1 f [] = raise List.Empty
berghofe@25681
   331
  | foldl1 f (x :: xs) = foldl f (x, xs);
berghofe@25681
   332
wenzelm@233
   333
(*  (op @) [x1, ..., xn]  ===>   x1 @ (x2 ... @ (x[n-1] @ xn))
wenzelm@233
   334
    for n > 0, operators that associate to the right (not tail recursive)*)
wenzelm@47060
   335
fun foldr1 f [] = raise List.Empty
wenzelm@20510
   336
  | foldr1 f l =
paulson@20443
   337
      let fun itr [x] = x
wenzelm@20510
   338
            | itr (x::l) = f(x, itr l)
paulson@20443
   339
      in  itr l  end;
wenzelm@233
   340
wenzelm@233
   341
wenzelm@233
   342
(* basic list functions *)
wenzelm@233
   343
wenzelm@20510
   344
fun eq_list eq (list1, list2) =
wenzelm@42403
   345
  pointer_eq (list1, list2) orelse
wenzelm@42403
   346
    let
wenzelm@42403
   347
      fun eq_lst (x :: xs, y :: ys) = eq (x, y) andalso eq_lst (xs, ys)
wenzelm@42403
   348
        | eq_lst _ = true;
wenzelm@42403
   349
    in length list1 = length list2 andalso eq_lst (list1, list2) end;
haftmann@20348
   350
wenzelm@19483
   351
fun maps f [] = []
wenzelm@19483
   352
  | maps f (x :: xs) = f x @ maps f xs;
wenzelm@19483
   353
haftmann@25538
   354
val filter = List.filter;
haftmann@25538
   355
fun filter_out f = filter (not o f);
haftmann@25538
   356
val map_filter = List.mapPartial;
haftmann@25538
   357
haftmann@33955
   358
fun take (0: int) xs = []
haftmann@33955
   359
  | take _ [] = []
haftmann@34059
   360
  | take n (x :: xs) = x :: take (n - 1) xs;
haftmann@33955
   361
haftmann@33955
   362
fun drop (0: int) xs = xs
haftmann@33955
   363
  | drop _ [] = []
haftmann@34059
   364
  | drop n (x :: xs) = drop (n - 1) xs;
haftmann@33955
   365
wenzelm@24593
   366
fun chop (0: int) xs = ([], xs)
wenzelm@23220
   367
  | chop _ [] = ([], [])
wenzelm@23220
   368
  | chop n (x :: xs) = chop (n - 1) xs |>> cons x;
wenzelm@19011
   369
wenzelm@46891
   370
fun chop_groups n list =
wenzelm@46891
   371
  (case chop (Int.max (n, 1)) list of
wenzelm@46891
   372
    ([], _) => []
wenzelm@46891
   373
  | (g, rest) => g :: chop_groups n rest);
wenzelm@46891
   374
wenzelm@46891
   375
wenzelm@233
   376
(*return nth element of a list, where 0 designates the first element;
wenzelm@18461
   377
  raise Subscript if list too short*)
haftmann@18011
   378
fun nth xs i = List.nth (xs, i);
wenzelm@233
   379
wenzelm@43278
   380
fun nth_list xss i = nth xss i handle General.Subscript => [];
wenzelm@18461
   381
haftmann@18011
   382
fun nth_map 0 f (x :: xs) = f x :: xs
haftmann@18011
   383
  | nth_map n f (x :: xs) = x :: nth_map (n - 1) f xs
wenzelm@24593
   384
  | nth_map (_: int) _ [] = raise Subscript;
wenzelm@11773
   385
haftmann@24846
   386
fun nth_drop n xs =
haftmann@24846
   387
  List.take (xs, n) @ List.drop (xs, n + 1);
haftmann@24846
   388
haftmann@18514
   389
fun map_index f =
haftmann@18514
   390
  let
haftmann@52271
   391
    fun map_aux (_: int) [] = []
haftmann@52271
   392
      | map_aux i (x :: xs) = f (i, x) :: map_aux (i + 1) xs
haftmann@52271
   393
  in map_aux 0 end;
haftmann@18514
   394
haftmann@21118
   395
fun fold_index f =
haftmann@21118
   396
  let
wenzelm@24593
   397
    fun fold_aux (_: int) [] y = y
haftmann@52271
   398
      | fold_aux i (x :: xs) y = fold_aux (i + 1) xs (f (i, x) y)
haftmann@21118
   399
  in fold_aux 0 end;
haftmann@21118
   400
haftmann@33063
   401
fun map_range f i =
haftmann@33063
   402
  let
haftmann@52271
   403
    fun map_aux (k: int) =
haftmann@52271
   404
      if k < i then f k :: map_aux (k + 1) else []
haftmann@52271
   405
  in map_aux 0 end;
haftmann@52271
   406
haftmann@52271
   407
fun fold_range f i =
haftmann@52271
   408
  let
haftmann@52271
   409
    fun fold_aux (k: int) y =
haftmann@52271
   410
      if k < i then fold_aux (k + 1) (f k y) else y
haftmann@52271
   411
  in fold_aux 0 end;
haftmann@52271
   412
haftmann@33063
   413
wenzelm@3762
   414
(*rear decomposition*)
wenzelm@47060
   415
fun split_last [] = raise List.Empty
wenzelm@3762
   416
  | split_last [x] = ([], x)
wenzelm@3762
   417
  | split_last (x :: xs) = apfst (cons x) (split_last xs);
wenzelm@3762
   418
wenzelm@46891
   419
(*find first element satisfying predicate*)
wenzelm@46891
   420
val find_first = List.find;
wenzelm@46891
   421
ballarin@29209
   422
(*find position of first element satisfying a predicate*)
wenzelm@4212
   423
fun find_index pred =
wenzelm@24593
   424
  let fun find (_: int) [] = ~1
wenzelm@4212
   425
        | find n (x :: xs) = if pred x then n else find (n + 1) xs;
wenzelm@4212
   426
  in find 0 end;
wenzelm@3762
   427
wenzelm@4916
   428
(*get first element by lookup function*)
skalberg@15531
   429
fun get_first _ [] = NONE
wenzelm@4916
   430
  | get_first f (x :: xs) =
wenzelm@4916
   431
      (case f x of
skalberg@15531
   432
        NONE => get_first f xs
wenzelm@4916
   433
      | some => some);
wenzelm@4916
   434
haftmann@19233
   435
fun get_index f =
haftmann@19233
   436
  let
wenzelm@24593
   437
    fun get (_: int) [] = NONE
wenzelm@19461
   438
      | get i (x :: xs) =
wenzelm@46838
   439
          (case f x of
wenzelm@46838
   440
            NONE => get (i + 1) xs
wenzelm@46838
   441
          | SOME y => SOME (i, y))
haftmann@19233
   442
  in get 0 end;
haftmann@19233
   443
skalberg@15531
   444
val flat = List.concat;
wenzelm@233
   445
wenzelm@12136
   446
fun unflat (xs :: xss) ys =
wenzelm@19424
   447
      let val (ps, qs) = chop (length xs) ys
nipkow@13629
   448
      in ps :: unflat xss qs end
wenzelm@12136
   449
  | unflat [] [] = []
wenzelm@40722
   450
  | unflat _ _ = raise ListPair.UnequalLengths;
wenzelm@12136
   451
wenzelm@46891
   452
fun grouped n comb f = chop_groups n #> comb (map f) #> flat;
wenzelm@46891
   453
wenzelm@21479
   454
fun burrow f xss = unflat xss (f (flat xss));
haftmann@18359
   455
wenzelm@24864
   456
fun burrow_options f os = map (try hd) (burrow f (map the_list os));
wenzelm@24864
   457
haftmann@18549
   458
fun fold_burrow f xss s =
haftmann@18549
   459
  apfst (unflat xss) (f (flat xss) s);
haftmann@18359
   460
wenzelm@233
   461
(*separate s [x1, x2, ..., xn]  ===>  [x1, s, x2, s, ..., s, xn]*)
wenzelm@233
   462
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs
wenzelm@233
   463
  | separate _ xs = xs;
wenzelm@233
   464
wenzelm@25980
   465
fun surround s (x :: xs) = s :: x :: surround s xs
wenzelm@25980
   466
  | surround s [] = [s];
wenzelm@25980
   467
wenzelm@233
   468
(*make the list [x, x, ..., x] of length n*)
wenzelm@24593
   469
fun replicate (n: int) x =
wenzelm@233
   470
  let fun rep (0, xs) = xs
wenzelm@233
   471
        | rep (n, xs) = rep (n - 1, x :: xs)
wenzelm@233
   472
  in
skalberg@15570
   473
    if n < 0 then raise Subscript
wenzelm@233
   474
    else rep (n, [])
wenzelm@233
   475
  end;
wenzelm@233
   476
wenzelm@4248
   477
wenzelm@25549
   478
(* direct product *)
wenzelm@25549
   479
haftmann@25538
   480
fun map_product f _ [] = []
haftmann@25538
   481
  | map_product f [] _ = []
haftmann@25538
   482
  | map_product f (x :: xs) ys = map (f x) ys @ map_product f xs ys;
wenzelm@233
   483
haftmann@25538
   484
fun fold_product f _ [] z = z
haftmann@25538
   485
  | fold_product f [] _ z = z
haftmann@25538
   486
  | fold_product f (x :: xs) ys z = z |> fold (f x) ys |> fold_product f xs ys;
wenzelm@233
   487
wenzelm@25549
   488
wenzelm@25549
   489
(* lists of pairs *)
wenzelm@233
   490
haftmann@18330
   491
fun map2 _ [] [] = []
haftmann@18330
   492
  | map2 f (x :: xs) (y :: ys) = f x y :: map2 f xs ys
wenzelm@40722
   493
  | map2 _ _ _ = raise ListPair.UnequalLengths;
wenzelm@380
   494
wenzelm@58633
   495
fun fold2 _ [] [] z = z
wenzelm@23220
   496
  | fold2 f (x :: xs) (y :: ys) z = fold2 f xs ys (f x y z)
wenzelm@58633
   497
  | fold2 _ _ _ _ = raise ListPair.UnequalLengths;
wenzelm@380
   498
wenzelm@58633
   499
fun map_split _ [] = ([], [])
haftmann@25943
   500
  | map_split f (x :: xs) =
haftmann@25943
   501
      let
haftmann@25943
   502
        val (y, w) = f x;
haftmann@25943
   503
        val (ys, ws) = map_split f xs;
haftmann@25943
   504
      in (y :: ys, w :: ws) end;
haftmann@25943
   505
wenzelm@19799
   506
fun zip_options (x :: xs) (SOME y :: ys) = (x, y) :: zip_options xs ys
wenzelm@19799
   507
  | zip_options (_ :: xs) (NONE :: ys) = zip_options xs ys
wenzelm@19799
   508
  | zip_options _ [] = []
wenzelm@40722
   509
  | zip_options [] _ = raise ListPair.UnequalLengths;
wenzelm@4956
   510
wenzelm@233
   511
(*combine two lists forming a list of pairs:
wenzelm@233
   512
  [x1, ..., xn] ~~ [y1, ..., yn]  ===>  [(x1, y1), ..., (xn, yn)]*)
wenzelm@233
   513
fun [] ~~ [] = []
wenzelm@233
   514
  | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys)
wenzelm@40722
   515
  | _ ~~ _ = raise ListPair.UnequalLengths;
wenzelm@233
   516
wenzelm@233
   517
(*inverse of ~~; the old 'split':
wenzelm@233
   518
  [(x1, y1), ..., (xn, yn)]  ===>  ([x1, ..., xn], [y1, ..., yn])*)
skalberg@15570
   519
val split_list = ListPair.unzip;
wenzelm@233
   520
haftmann@28347
   521
fun burrow_fst f xs = split_list xs |>> f |> op ~~;
haftmann@28347
   522
wenzelm@233
   523
wenzelm@233
   524
(* prefixes, suffixes *)
wenzelm@233
   525
haftmann@18441
   526
fun is_prefix _ [] _ = true
haftmann@18441
   527
  | is_prefix eq (x :: xs) (y :: ys) = eq (x, y) andalso is_prefix eq xs ys
haftmann@18441
   528
  | is_prefix eq _ _ = false;
wenzelm@233
   529
wenzelm@233
   530
(* [x1, ..., xi, ..., xn]  --->  ([x1, ..., x(i-1)], [xi, ..., xn])
wenzelm@233
   531
   where xi is the first element that does not satisfy the predicate*)
wenzelm@233
   532
fun take_prefix (pred : 'a -> bool)  (xs: 'a list) : 'a list * 'a list =
wenzelm@233
   533
  let fun take (rxs, []) = (rev rxs, [])
wenzelm@255
   534
        | take (rxs, x :: xs) =
wenzelm@255
   535
            if  pred x  then  take(x :: rxs, xs)  else  (rev rxs, x :: xs)
wenzelm@233
   536
  in  take([], xs)  end;
wenzelm@233
   537
haftmann@20108
   538
fun chop_prefix eq ([], ys) = ([], ([], ys))
haftmann@20108
   539
  | chop_prefix eq (xs, []) = ([], (xs, []))
wenzelm@23220
   540
  | chop_prefix eq (xs as x :: xs', ys as y :: ys') =
haftmann@20108
   541
      if eq (x, y) then
haftmann@20108
   542
        let val (ps', xys'') = chop_prefix eq (xs', ys')
wenzelm@23220
   543
        in (x :: ps', xys'') end
haftmann@20108
   544
      else ([], (xs, ys));
haftmann@20108
   545
wenzelm@233
   546
(* [x1, ..., xi, ..., xn]  --->  ([x1, ..., xi], [x(i+1), ..., xn])
wenzelm@233
   547
   where xi is the last element that does not satisfy the predicate*)
wenzelm@233
   548
fun take_suffix _ [] = ([], [])
wenzelm@233
   549
  | take_suffix pred (x :: xs) =
wenzelm@233
   550
      (case take_suffix pred xs of
wenzelm@233
   551
        ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx)
wenzelm@233
   552
      | (prfx, sffx) => (x :: prfx, sffx));
wenzelm@233
   553
wenzelm@12249
   554
fun prefixes1 [] = []
wenzelm@12249
   555
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
wenzelm@12249
   556
wenzelm@19011
   557
fun prefixes xs = [] :: prefixes1 xs;
wenzelm@19011
   558
wenzelm@12249
   559
fun suffixes1 xs = map rev (prefixes1 (rev xs));
wenzelm@19011
   560
fun suffixes xs = [] :: suffixes1 xs;
wenzelm@233
   561
wenzelm@61707
   562
fun trim pred = take_prefix pred #> #2 #> take_suffix pred #> #1;
wenzelm@23220
   563
wenzelm@23220
   564
wenzelm@233
   565
(** integers **)
wenzelm@233
   566
wenzelm@233
   567
(* lists of integers *)
wenzelm@233
   568
wenzelm@233
   569
(*make the list [from, from + 1, ..., to]*)
wenzelm@24593
   570
fun ((i: int) upto j) =
wenzelm@21859
   571
  if i > j then [] else i :: (i + 1 upto j);
wenzelm@233
   572
wenzelm@233
   573
(*make the list [from, from - 1, ..., to]*)
wenzelm@24593
   574
fun ((i: int) downto j) =
wenzelm@21859
   575
  if i < j then [] else i :: (i - 1 downto j);
wenzelm@233
   576
wenzelm@233
   577
wenzelm@233
   578
(* convert integers to strings *)
wenzelm@233
   579
wenzelm@233
   580
(*expand the number in the given base;
wenzelm@233
   581
  example: radixpand (2, 8) gives [1, 0, 0, 0]*)
wenzelm@233
   582
fun radixpand (base, num) : int list =
wenzelm@233
   583
  let
wenzelm@233
   584
    fun radix (n, tail) =
wenzelm@233
   585
      if n < base then n :: tail
wenzelm@233
   586
      else radix (n div base, (n mod base) :: tail)
wenzelm@233
   587
  in radix (num, []) end;
wenzelm@233
   588
wenzelm@233
   589
(*expands a number into a string of characters starting from "zerochar";
wenzelm@233
   590
  example: radixstring (2, "0", 8) gives "1000"*)
wenzelm@233
   591
fun radixstring (base, zerochar, num) =
wenzelm@233
   592
  let val offset = ord zerochar;
wenzelm@233
   593
      fun chrof n = chr (offset + n)
wenzelm@233
   594
  in implode (map chrof (radixpand (base, num))) end;
wenzelm@233
   595
wenzelm@233
   596
wenzelm@41492
   597
local
wenzelm@41492
   598
  val zero = ord "0";
wenzelm@57909
   599
  val small_int = 10000: int;
wenzelm@57909
   600
  val small_int_table = Vector.tabulate (small_int, Int.toString);
wenzelm@41492
   601
in
wenzelm@41492
   602
wenzelm@41492
   603
fun string_of_int i =
wenzelm@41492
   604
  if i < 0 then Int.toString i
wenzelm@41492
   605
  else if i < 10 then chr (zero + i)
wenzelm@57909
   606
  else if i < small_int then Vector.sub (small_int_table, i)
wenzelm@41492
   607
  else Int.toString i;
wenzelm@41492
   608
wenzelm@41492
   609
end;
wenzelm@233
   610
wenzelm@21942
   611
fun signed_string_of_int i =
wenzelm@21942
   612
  if i < 0 then "-" ^ string_of_int (~ i) else string_of_int i;
wenzelm@21942
   613
wenzelm@23220
   614
fun string_of_indexname (a, 0) = a
wenzelm@23220
   615
  | string_of_indexname (a, i) = a ^ "_" ^ string_of_int i;
wenzelm@233
   616
wenzelm@233
   617
wenzelm@14826
   618
(* read integers *)
wenzelm@14826
   619
wenzelm@24630
   620
fun read_radix_int radix cs =
wenzelm@20095
   621
  let
wenzelm@20095
   622
    val zero = ord "0";
wenzelm@20095
   623
    val limit = zero + radix;
wenzelm@20095
   624
    fun scan (num, []) = (num, [])
wenzelm@20095
   625
      | scan (num, c :: cs) =
wenzelm@50637
   626
          if zero <= ord c andalso ord c < limit then
wenzelm@50637
   627
            scan (radix * num + (ord c - zero), cs)
wenzelm@50637
   628
          else (num, c :: cs);
wenzelm@24630
   629
  in scan (0, cs) end;
wenzelm@14826
   630
wenzelm@24630
   631
val read_int = read_radix_int 10;
wenzelm@14826
   632
wenzelm@40627
   633
fun oct_char s = chr (#1 (read_radix_int 8 (raw_explode s)));
wenzelm@14826
   634
wenzelm@14826
   635
wenzelm@14826
   636
wenzelm@233
   637
(** strings **)
wenzelm@233
   638
wenzelm@16188
   639
(* functions tuned for strings, avoiding explode *)
wenzelm@6312
   640
haftmann@18011
   641
fun nth_string str i =
wenzelm@6959
   642
  (case try String.substring (str, i, 1) of
skalberg@15531
   643
    SOME s => s
skalberg@15570
   644
  | NONE => raise Subscript);
wenzelm@6312
   645
wenzelm@16188
   646
fun fold_string f str x0 =
wenzelm@6282
   647
  let
wenzelm@6282
   648
    val n = size str;
wenzelm@16188
   649
    fun iter (x, i) =
wenzelm@16188
   650
      if i < n then iter (f (String.substring (str, i, 1)) x, i + 1) else x;
wenzelm@16188
   651
  in iter (x0, 0) end;
wenzelm@6282
   652
wenzelm@14968
   653
fun exists_string pred str =
wenzelm@14968
   654
  let
wenzelm@14968
   655
    val n = size str;
wenzelm@14968
   656
    fun ex i = i < n andalso (pred (String.substring (str, i, 1)) orelse ex (i + 1));
wenzelm@14968
   657
  in ex 0 end;
wenzelm@6312
   658
wenzelm@16188
   659
fun forall_string pred = not o exists_string (not o pred);
wenzelm@16188
   660
wenzelm@28025
   661
fun first_field sep str =
wenzelm@28022
   662
  let
wenzelm@28025
   663
    val n = size sep;
wenzelm@28022
   664
    val len = size str;
wenzelm@28022
   665
    fun find i =
wenzelm@28022
   666
      if i + n > len then NONE
wenzelm@28025
   667
      else if String.substring (str, i, n) = sep then SOME i
wenzelm@28022
   668
      else find (i + 1);
wenzelm@28025
   669
  in
wenzelm@28025
   670
    (case find 0 of
wenzelm@28025
   671
      NONE => NONE
wenzelm@28025
   672
    | SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE)))
wenzelm@28025
   673
  end;
wenzelm@28022
   674
lcp@512
   675
(*enclose in brackets*)
lcp@512
   676
fun enclose lpar rpar str = lpar ^ str ^ rpar;
wenzelm@6642
   677
fun unenclose str = String.substring (str, 1, size str - 2);
wenzelm@255
   678
wenzelm@233
   679
(*simple quoting (does not escape special chars)*)
lcp@512
   680
val quote = enclose "\"" "\"";
wenzelm@233
   681
wenzelm@62529
   682
val cartouche = enclose "\<open>" "\<close>";
wenzelm@55033
   683
wenzelm@59469
   684
val space_implode = String.concatWith;
wenzelm@233
   685
wenzelm@255
   686
val commas = space_implode ", ";
wenzelm@380
   687
val commas_quote = commas o map quote;
wenzelm@255
   688
wenzelm@255
   689
val cat_lines = space_implode "\n";
wenzelm@233
   690
wenzelm@4212
   691
(*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*)
wenzelm@3832
   692
fun space_explode _ "" = []
paulson@21899
   693
  | space_explode sep s = String.fields (fn c => str c = sep) s;
wenzelm@3832
   694
wenzelm@3832
   695
val split_lines = space_explode "\n";
wenzelm@3832
   696
wenzelm@56038
   697
fun plain_words s = space_explode "_" s |> space_implode " ";
wenzelm@56038
   698
wenzelm@14826
   699
fun prefix_lines "" txt = txt
wenzelm@14826
   700
  | prefix_lines prfx txt = txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines;
wenzelm@14826
   701
wenzelm@18681
   702
fun prefix prfx s = prfx ^ s;
wenzelm@16188
   703
fun suffix sffx s = s ^ sffx;
wenzelm@5285
   704
wenzelm@18681
   705
fun unprefix prfx s =
wenzelm@18681
   706
  if String.isPrefix prfx s then String.substring (s, size prfx, size s - size prfx)
wenzelm@18681
   707
  else raise Fail "unprefix";
wenzelm@18681
   708
wenzelm@16188
   709
fun unsuffix sffx s =
wenzelm@17061
   710
  if String.isSuffix sffx s then String.substring (s, 0, size s - size sffx)
wenzelm@17061
   711
  else raise Fail "unsuffix";
wenzelm@5285
   712
wenzelm@47499
   713
val trim_line = perhaps (try (unsuffix "\n"));
wenzelm@47499
   714
wenzelm@24593
   715
fun replicate_string (0: int) _ = ""
wenzelm@10951
   716
  | replicate_string 1 a = a
wenzelm@10951
   717
  | replicate_string k a =
wenzelm@10951
   718
      if k mod 2 = 0 then replicate_string (k div 2) (a ^ a)
wenzelm@10951
   719
      else replicate_string (k div 2) (a ^ a) ^ a;
wenzelm@10951
   720
haftmann@31250
   721
fun translate_string f = String.translate (f o String.str);
haftmann@31250
   722
kleing@29882
   723
(*crude matching of str against simple glob pat*)
kleing@29882
   724
fun match_string pat str =
kleing@29882
   725
  let
kleing@29882
   726
    fun match [] _ = true
kleing@29882
   727
      | match (p :: ps) s =
kleing@29882
   728
          size p <= size s andalso
kleing@29882
   729
            (case try (unprefix p) s of
kleing@29882
   730
              SOME s' => match ps s'
kleing@29882
   731
            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
kleing@29882
   732
  in match (space_explode "*" pat) str end;
wenzelm@23220
   733
wenzelm@35976
   734
wenzelm@41516
   735
(** reals **)
wenzelm@41516
   736
wenzelm@41516
   737
val string_of_real = Real.fmt (StringCvt.GEN NONE);
wenzelm@41516
   738
wenzelm@41516
   739
fun signed_string_of_real x =
wenzelm@41516
   740
  if x < 0.0 then "-" ^ string_of_real (~ x) else string_of_real x;
wenzelm@41516
   741
wenzelm@41516
   742
wenzelm@35976
   743
wenzelm@16492
   744
(** lists as sets -- see also Pure/General/ord_list.ML **)
wenzelm@233
   745
wenzelm@26439
   746
(* canonical operations *)
wenzelm@26439
   747
wenzelm@18923
   748
fun member eq list x =
wenzelm@18923
   749
  let
wenzelm@18923
   750
    fun memb [] = false
wenzelm@18923
   751
      | memb (y :: ys) = eq (x, y) orelse memb ys;
wenzelm@18923
   752
  in memb list end;
berghofe@1576
   753
wenzelm@18923
   754
fun insert eq x xs = if member eq xs x then xs else x :: xs;
wenzelm@18923
   755
fun remove eq x xs = if member eq xs x then filter_out (fn y => eq (x, y)) xs else xs;
wenzelm@24049
   756
fun update eq x xs = cons x (remove eq x xs);
wenzelm@233
   757
haftmann@33049
   758
fun inter eq xs = filter (member eq xs);
haftmann@33049
   759
haftmann@33042
   760
fun union eq = fold (insert eq);
wenzelm@19301
   761
fun subtract eq = fold (remove eq);
wenzelm@19301
   762
wenzelm@30572
   763
fun merge eq (xs, ys) =
wenzelm@30572
   764
  if pointer_eq (xs, ys) then xs
wenzelm@30572
   765
  else if null xs then ys
wenzelm@30572
   766
  else fold_rev (insert eq) ys xs;
clasohm@0
   767
wenzelm@26439
   768
haftmann@33050
   769
(* subset and set equality *)
haftmann@33050
   770
haftmann@33038
   771
fun subset eq (xs, ys) = forall (member eq ys) xs;
berghofe@1576
   772
haftmann@33038
   773
fun eq_set eq (xs, ys) =
haftmann@20348
   774
  eq_list eq (xs, ys) orelse
haftmann@33038
   775
    (subset eq (xs, ys) andalso subset (eq o swap) (ys, xs));
wenzelm@19301
   776
wenzelm@265
   777
wenzelm@233
   778
(*makes a list of the distinct members of the input; preserves order, takes
wenzelm@233
   779
  first of equal elements*)
wenzelm@19046
   780
fun distinct eq lst =
wenzelm@233
   781
  let
wenzelm@233
   782
    fun dist (rev_seen, []) = rev rev_seen
wenzelm@233
   783
      | dist (rev_seen, x :: xs) =
wenzelm@18923
   784
          if member eq rev_seen x then dist (rev_seen, xs)
wenzelm@233
   785
          else dist (x :: rev_seen, xs);
wenzelm@19046
   786
  in dist ([], lst) end;
wenzelm@233
   787
wenzelm@255
   788
(*returns a list containing all repeated elements exactly once; preserves
wenzelm@255
   789
  order, takes first of equal elements*)
wenzelm@18966
   790
fun duplicates eq lst =
wenzelm@255
   791
  let
wenzelm@255
   792
    fun dups (rev_dups, []) = rev rev_dups
wenzelm@255
   793
      | dups (rev_dups, x :: xs) =
wenzelm@18923
   794
          if member eq rev_dups x orelse not (member eq xs x) then
wenzelm@255
   795
            dups (rev_dups, xs)
wenzelm@255
   796
          else dups (x :: rev_dups, xs);
wenzelm@18966
   797
  in dups ([], lst) end;
wenzelm@255
   798
wenzelm@16878
   799
fun has_duplicates eq =
wenzelm@16878
   800
  let
wenzelm@16878
   801
    fun dups [] = false
wenzelm@16878
   802
      | dups (x :: xs) = member eq xs x orelse dups xs;
wenzelm@16878
   803
  in dups end;
wenzelm@16878
   804
wenzelm@255
   805
haftmann@32352
   806
(* matrices *)
haftmann@32352
   807
haftmann@32352
   808
fun map_transpose f xss =
haftmann@32352
   809
  let
wenzelm@40722
   810
    val n =
wenzelm@40722
   811
      (case distinct (op =) (map length xss) of
wenzelm@40722
   812
        [] => 0
haftmann@32352
   813
      | [n] => n
wenzelm@40722
   814
      | _ => raise ListPair.UnequalLengths);
haftmann@33206
   815
  in map_range (fn m => f (map (fn xs => nth xs m) xss)) n end;
haftmann@32352
   816
haftmann@32352
   817
wenzelm@23220
   818
nipkow@22142
   819
(** lists as multisets **)
nipkow@22142
   820
haftmann@33078
   821
fun remove1 eq x [] = []
haftmann@33078
   822
  | remove1 eq x (y :: ys) = if eq (x, y) then ys else y :: remove1 eq x ys;
nipkow@22142
   823
haftmann@33078
   824
fun combine eq xs ys = fold (remove1 eq) ys xs @ ys;
wenzelm@233
   825
haftmann@33079
   826
fun submultiset _ ([], _)  = true
haftmann@33079
   827
  | submultiset eq (x :: xs, ys) = member eq ys x andalso submultiset eq (xs, remove1 eq x ys);
haftmann@33079
   828
clasohm@0
   829
clasohm@0
   830
wenzelm@2506
   831
(** orders **)
wenzelm@2506
   832
wenzelm@18966
   833
fun is_equal EQUAL = true
wenzelm@18966
   834
  | is_equal _ = false;
wenzelm@18966
   835
wenzelm@4445
   836
fun rev_order LESS = GREATER
wenzelm@4445
   837
  | rev_order EQUAL = EQUAL
wenzelm@4445
   838
  | rev_order GREATER = LESS;
wenzelm@4445
   839
wenzelm@4479
   840
(*assume rel is a linear strict order*)
wenzelm@4445
   841
fun make_ord rel (x, y) =
wenzelm@4445
   842
  if rel (x, y) then LESS
wenzelm@4445
   843
  else if rel (y, x) then GREATER
wenzelm@4445
   844
  else EQUAL;
wenzelm@4445
   845
wenzelm@25224
   846
fun bool_ord (false, true) = LESS
wenzelm@25224
   847
  | bool_ord (true, false) = GREATER
wenzelm@25224
   848
  | bool_ord _ = EQUAL;
wenzelm@25224
   849
wenzelm@15051
   850
val int_ord = Int.compare;
wenzelm@15051
   851
val string_ord = String.compare;
wenzelm@2506
   852
wenzelm@16676
   853
fun fast_string_ord (s1, s2) =
wenzelm@43793
   854
  if pointer_eq (s1, s2) then EQUAL
wenzelm@43793
   855
  else (case int_ord (size s1, size s2) of EQUAL => string_ord (s1, s2) | ord => ord);
wenzelm@16676
   856
wenzelm@16492
   857
fun option_ord ord (SOME x, SOME y) = ord (x, y)
wenzelm@16492
   858
  | option_ord _ (NONE, NONE) = EQUAL
wenzelm@16492
   859
  | option_ord _ (NONE, SOME _) = LESS
wenzelm@16492
   860
  | option_ord _ (SOME _, NONE) = GREATER;
wenzelm@16492
   861
wenzelm@4343
   862
(*lexicographic product*)
wenzelm@4343
   863
fun prod_ord a_ord b_ord ((x, y), (x', y')) =
wenzelm@4343
   864
  (case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord);
wenzelm@4343
   865
wenzelm@4343
   866
(*dictionary order -- in general NOT well-founded!*)
wenzelm@16984
   867
fun dict_ord elem_ord (x :: xs, y :: ys) =
wenzelm@16984
   868
      (case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord)
wenzelm@16984
   869
  | dict_ord _ ([], []) = EQUAL
wenzelm@4343
   870
  | dict_ord _ ([], _ :: _) = LESS
wenzelm@16984
   871
  | dict_ord _ (_ :: _, []) = GREATER;
wenzelm@4343
   872
wenzelm@4343
   873
(*lexicographic product of lists*)
wenzelm@4343
   874
fun list_ord elem_ord (xs, ys) =
wenzelm@16676
   875
  (case int_ord (length xs, length ys) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord);
wenzelm@4343
   876
wenzelm@2506
   877
wenzelm@4621
   878
(* sorting *)
wenzelm@4621
   879
wenzelm@48271
   880
(*stable mergesort -- preserves order of equal elements*)
wenzelm@48271
   881
fun mergesort unique ord =
wenzelm@4621
   882
  let
wenzelm@48271
   883
    fun merge (xs as x :: xs') (ys as y :: ys') =
wenzelm@48271
   884
          (case ord (x, y) of
wenzelm@48271
   885
            LESS => x :: merge xs' ys
wenzelm@48271
   886
          | EQUAL =>
wenzelm@48271
   887
              if unique then merge xs ys'
wenzelm@48271
   888
              else x :: merge xs' ys
wenzelm@48271
   889
          | GREATER => y :: merge xs ys')
wenzelm@48271
   890
      | merge [] ys = ys
wenzelm@48271
   891
      | merge xs [] = xs;
wenzelm@48271
   892
wenzelm@48271
   893
    fun merge_all [xs] = xs
wenzelm@48271
   894
      | merge_all xss = merge_all (merge_pairs xss)
wenzelm@48271
   895
    and merge_pairs (xs :: ys :: xss) = merge xs ys :: merge_pairs xss
wenzelm@48271
   896
      | merge_pairs xss = xss;
wenzelm@48271
   897
wenzelm@48271
   898
    fun runs (x :: y :: xs) =
wenzelm@18427
   899
          (case ord (x, y) of
wenzelm@48271
   900
             LESS => ascending y [x] xs
wenzelm@48271
   901
           | EQUAL =>
wenzelm@48271
   902
               if unique then runs (x :: xs)
wenzelm@48271
   903
               else ascending y [x] xs
wenzelm@48271
   904
           | GREATER => descending y [x] xs)
wenzelm@48271
   905
      | runs xs = [xs]
wenzelm@4621
   906
wenzelm@48271
   907
    and ascending x xs (zs as y :: ys) =
wenzelm@48271
   908
          (case ord (x, y) of
wenzelm@48271
   909
             LESS => ascending y (x :: xs) ys
wenzelm@48271
   910
           | EQUAL =>
wenzelm@48271
   911
               if unique then ascending x xs ys
wenzelm@48271
   912
               else ascending y (x :: xs) ys
wenzelm@48271
   913
           | GREATER => rev (x :: xs) :: runs zs)
wenzelm@48271
   914
      | ascending x xs [] = [rev (x :: xs)]
wenzelm@48271
   915
wenzelm@48271
   916
    and descending x xs (zs as y :: ys) =
wenzelm@48271
   917
          (case ord (x, y) of
wenzelm@48271
   918
             GREATER => descending y (x :: xs) ys
wenzelm@48271
   919
           | EQUAL =>
wenzelm@48271
   920
               if unique then descending x xs ys
wenzelm@48271
   921
               else (x :: xs) :: runs zs
wenzelm@48271
   922
           | LESS => (x :: xs) :: runs zs)
wenzelm@48271
   923
      | descending x xs [] = [x :: xs];
wenzelm@48271
   924
wenzelm@48271
   925
  in merge_all o runs end;
wenzelm@48271
   926
wenzelm@48271
   927
fun sort ord = mergesort false ord;
wenzelm@48271
   928
fun sort_distinct ord = mergesort true ord;
wenzelm@18427
   929
wenzelm@4621
   930
val sort_strings = sort string_ord;
wenzelm@60924
   931
fun sort_by key xs = sort (string_ord o apply2 key) xs;
wenzelm@4621
   932
wenzelm@4621
   933
wenzelm@30558
   934
(* items tagged by integer index *)
wenzelm@30558
   935
wenzelm@30558
   936
(*insert tags*)
wenzelm@30558
   937
fun tag_list k [] = []
haftmann@30570
   938
  | tag_list k (x :: xs) = (k:int, x) :: tag_list (k + 1) xs;
wenzelm@30558
   939
wenzelm@30558
   940
(*remove tags and suppress duplicates -- list is assumed sorted!*)
wenzelm@30558
   941
fun untag_list [] = []
wenzelm@30558
   942
  | untag_list [(k: int, x)] = [x]
wenzelm@30558
   943
  | untag_list ((k, x) :: (rest as (k', x') :: _)) =
wenzelm@30558
   944
      if k = k' then untag_list rest
wenzelm@30558
   945
      else x :: untag_list rest;
wenzelm@30558
   946
wenzelm@30558
   947
(*return list elements in original order*)
wenzelm@59058
   948
fun order_list list = untag_list (sort (int_ord o apply2 fst) list);
wenzelm@30558
   949
wenzelm@30558
   950
wenzelm@2506
   951
wenzelm@4621
   952
(** misc **)
wenzelm@233
   953
wenzelm@19644
   954
fun divide_and_conquer decomp x =
wenzelm@19644
   955
  let val (ys, recomb) = decomp x
wenzelm@19644
   956
  in recomb (map (divide_and_conquer decomp) ys) end;
wenzelm@19644
   957
wenzelm@32978
   958
fun divide_and_conquer' decomp x s =
wenzelm@32978
   959
  let val ((ys, recomb), s') = decomp x s
wenzelm@32978
   960
  in recomb (fold_map (divide_and_conquer' decomp) ys s') end;
wenzelm@32978
   961
clasohm@0
   962
wenzelm@233
   963
(*Partition a list into buckets  [ bi, b(i+1), ..., bj ]
clasohm@0
   964
   putting x in bk if p(k)(x) holds.  Preserve order of elements if possible.*)
clasohm@0
   965
fun partition_list p i j =
wenzelm@37851
   966
  let
wenzelm@37851
   967
    fun part (k: int) xs =
wenzelm@37851
   968
      if k > j then
wenzelm@37851
   969
        (case xs of
wenzelm@37851
   970
          [] => []
wenzelm@37851
   971
        | _ => raise Fail "partition_list")
wenzelm@37851
   972
      else
wenzelm@37851
   973
        let val (ns, rest) = List.partition (p k) xs
wenzelm@37851
   974
        in ns :: part (k + 1) rest end;
wenzelm@37851
   975
  in part (i: int) end;
clasohm@0
   976
wenzelm@37851
   977
fun partition_eq (eq: 'a * 'a -> bool) =
wenzelm@19691
   978
  let
wenzelm@19691
   979
    fun part [] = []
wenzelm@19691
   980
      | part (x :: ys) =
wenzelm@19691
   981
          let val (xs, xs') = List.partition (fn y => eq (x, y)) ys
wenzelm@37851
   982
          in (x :: xs) :: part xs' end;
wenzelm@19691
   983
  in part end;
wenzelm@19691
   984
wenzelm@19691
   985
wenzelm@45626
   986
(* serial numbers and abstract stamps *)
wenzelm@16439
   987
wenzelm@16439
   988
type serial = int;
wenzelm@62918
   989
val serial = Counter.make ();
wenzelm@19512
   990
val serial_string = string_of_int o serial;
wenzelm@19512
   991
wenzelm@45626
   992
datatype stamp = Stamp of serial;
wenzelm@45626
   993
fun stamp () = Stamp (serial ());
wenzelm@45626
   994
wenzelm@16535
   995
wenzelm@51368
   996
(* values of any type *)
wenzelm@16535
   997
wenzelm@16535
   998
(*note that the builtin exception datatype may be extended by new
wenzelm@16535
   999
  constructors at any time*)
wenzelm@51368
  1000
structure Any = struct type T = exn end;
wenzelm@16535
  1001
wenzelm@43603
  1002
wenzelm@43603
  1003
(* getenv *)
wenzelm@43603
  1004
wenzelm@43603
  1005
fun getenv x =
wenzelm@43603
  1006
  (case OS.Process.getEnv x of
wenzelm@43603
  1007
    NONE => ""
wenzelm@43603
  1008
  | SOME y => y);
wenzelm@43603
  1009
wenzelm@43603
  1010
fun getenv_strict x =
wenzelm@43603
  1011
  (case getenv x of
wenzelm@43603
  1012
    "" => error ("Undefined Isabelle environment variable: " ^ quote x)
wenzelm@43603
  1013
  | y => y);
wenzelm@43603
  1014
clasohm@1364
  1015
end;
clasohm@1364
  1016
wenzelm@32188
  1017
structure Basic_Library: BASIC_LIBRARY = Library;
wenzelm@32188
  1018
open Basic_Library;
wenzelm@40722
  1019