| author | paulson | 
| Wed, 28 Jun 2000 10:56:01 +0200 | |
| changeset 9172 | 2dbb80d4fdb7 | 
| parent 9118 | 62367f8fef02 | 
| child 9721 | 7e51c9f3d5a0 | 
| permissions | -rw-r--r-- | 
| 41 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
 wenzelm parents: 
24diff
changeset | 1 | (* Title: Pure/library.ML | 
| 0 | 2 | ID: $Id$ | 
| 233 | 3 | Author: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 8806 | 4 | Author: Markus Wenzel, TU Munich | 
| 5 | License: GPL (GNU GENERAL PUBLIC LICENSE) | |
| 0 | 6 | |
| 233 | 7 | Basic library: functions, options, pairs, booleans, lists, integers, | 
| 4212 | 8 | strings, lists as sets, association lists, generic tables, balanced | 
| 4621 | 9 | trees, orders, I/O and diagnostics, timing, misc. | 
| 0 | 10 | *) | 
| 11 | ||
| 8418 | 12 | infix |> |>> |>>> ~~ \ \\ ins ins_string ins_int orf andf prefix upto downto | 
| 4212 | 13 | mem mem_int mem_string union union_int union_string inter inter_int | 
| 14 | inter_string subset subset_int subset_string; | |
| 1364 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 15 | |
| 6510 | 16 | infix 3 oo ooo oooo; | 
| 5893 | 17 | |
| 4621 | 18 | signature LIBRARY = | 
| 19 | sig | |
| 20 | (*functions*) | |
| 21 |   val curry: ('a * 'b -> 'c) -> 'a -> 'b -> 'c
 | |
| 22 |   val uncurry: ('a -> 'b -> 'c) -> 'a * 'b -> 'c
 | |
| 23 | val I: 'a -> 'a | |
| 24 | val K: 'a -> 'b -> 'a | |
| 25 |   val |> : 'a * ('a -> 'b) -> 'b
 | |
| 8418 | 26 |   val |>> : ('a * 'b) * ('a -> 'c) -> 'c * 'b
 | 
| 27 |   val |>>> : ('a * 'b) * ('a -> 'c * 'd) -> 'c * ('b * 'd)
 | |
| 4621 | 28 |   val apl: 'a * ('a * 'b -> 'c) -> 'b -> 'c
 | 
| 29 |   val apr: ('a * 'b -> 'c) * 'b -> 'a -> 'c
 | |
| 30 |   val funpow: int -> ('a -> 'a) -> 'a -> 'a
 | |
| 5893 | 31 |   val oo: ('a -> 'b) * ('c -> 'd -> 'a) -> 'c -> 'd -> 'b
 | 
| 32 |   val ooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'a) -> 'c -> 'd -> 'e -> 'b
 | |
| 6510 | 33 |   val oooo: ('a -> 'b) * ('c -> 'd -> 'e -> 'f -> 'a) -> 'c -> 'd -> 'e -> 'f -> 'b
 | 
| 1364 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 34 | |
| 4621 | 35 | (*stamps*) | 
| 36 | type stamp | |
| 37 | val stamp: unit -> stamp | |
| 38 | ||
| 39 | (*options*) | |
| 40 | datatype 'a option = None | Some of 'a | |
| 41 | exception OPTION | |
| 42 | val the: 'a option -> 'a | |
| 43 | val if_none: 'a option -> 'a -> 'a | |
| 44 | val is_some: 'a option -> bool | |
| 45 | val is_none: 'a option -> bool | |
| 46 |   val apsome: ('a -> 'b) -> 'a option -> 'b option
 | |
| 6959 | 47 |   val try: ('a -> 'b) -> 'a -> 'b option
 | 
| 4621 | 48 |   val can: ('a -> 'b) -> 'a -> bool
 | 
| 49 | ||
| 50 | (*pairs*) | |
| 51 | val pair: 'a -> 'b -> 'a * 'b | |
| 52 | val rpair: 'a -> 'b -> 'b * 'a | |
| 53 | val fst: 'a * 'b -> 'a | |
| 54 | val snd: 'a * 'b -> 'b | |
| 55 |   val eq_fst: (''a * 'b) * (''a * 'c) -> bool
 | |
| 56 |   val eq_snd: ('a * ''b) * ('c * ''b) -> bool
 | |
| 57 | val swap: 'a * 'b -> 'b * 'a | |
| 58 |   val apfst: ('a -> 'b) -> 'a * 'c -> 'b * 'c
 | |
| 59 |   val apsnd: ('a -> 'b) -> 'c * 'a -> 'c * 'b
 | |
| 60 |   val pairself: ('a -> 'b) -> 'a * 'a -> 'b * 'b
 | |
| 61 | ||
| 62 | (*booleans*) | |
| 63 | val equal: ''a -> ''a -> bool | |
| 64 | val not_equal: ''a -> ''a -> bool | |
| 65 |   val orf: ('a -> bool) * ('a -> bool) -> 'a -> bool
 | |
| 66 |   val andf: ('a -> bool) * ('a -> bool) -> 'a -> bool
 | |
| 67 |   val exists: ('a -> bool) -> 'a list -> bool
 | |
| 68 |   val forall: ('a -> bool) -> 'a list -> bool
 | |
| 69 | val set: bool ref -> bool | |
| 70 | val reset: bool ref -> bool | |
| 71 | val toggle: bool ref -> bool | |
| 9118 | 72 |   val change: 'a ref -> ('a -> 'a) -> unit
 | 
| 4621 | 73 |   val setmp: 'a ref -> 'a -> ('b -> 'c) -> 'b -> 'c
 | 
| 74 | ||
| 75 | (*lists*) | |
| 76 | exception LIST of string | |
| 77 | val null: 'a list -> bool | |
| 78 | val hd: 'a list -> 'a | |
| 79 | val tl: 'a list -> 'a list | |
| 80 | val cons: 'a -> 'a list -> 'a list | |
| 5285 | 81 | val single: 'a -> 'a list | 
| 4629 | 82 | val append: 'a list -> 'a list -> 'a list | 
| 5904 | 83 |   val apply: ('a -> 'a) list -> 'a -> 'a
 | 
| 4621 | 84 |   val foldl: ('a * 'b -> 'a) -> 'a * 'b list -> 'a
 | 
| 85 |   val foldr: ('a * 'b -> 'b) -> 'a list * 'b -> 'b
 | |
| 86 |   val foldr1: ('a * 'a -> 'a) -> 'a list -> 'a
 | |
| 4956 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 87 |   val foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list
 | 
| 4621 | 88 | val length: 'a list -> int | 
| 89 | val take: int * 'a list -> 'a list | |
| 90 | val drop: int * 'a list -> 'a list | |
| 4713 | 91 |   val dropwhile: ('a -> bool) -> 'a list -> 'a list
 | 
| 4621 | 92 | val nth_elem: int * 'a list -> 'a | 
| 93 | val last_elem: 'a list -> 'a | |
| 94 | val split_last: 'a list -> 'a list * 'a | |
| 4893 | 95 | val nth_update: 'a -> int * 'a list -> 'a list | 
| 4621 | 96 |   val find_index: ('a -> bool) -> 'a list -> int
 | 
| 97 | val find_index_eq: ''a -> ''a list -> int | |
| 98 |   val find_first: ('a -> bool) -> 'a list -> 'a option
 | |
| 4916 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 99 |   val get_first: ('a -> 'b option) -> 'a list -> 'b option
 | 
| 4621 | 100 | val flat: 'a list list -> 'a list | 
| 101 |   val seq: ('a -> unit) -> 'a list -> unit
 | |
| 102 | val separate: 'a -> 'a list -> 'a list | |
| 103 | val replicate: int -> 'a -> 'a list | |
| 104 | val multiply: 'a list * 'a list list -> 'a list list | |
| 105 |   val filter: ('a -> bool) -> 'a list -> 'a list
 | |
| 106 |   val filter_out: ('a -> bool) -> 'a list -> 'a list
 | |
| 107 |   val mapfilter: ('a -> 'b option) -> 'a list -> 'b list
 | |
| 108 |   val map2: ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list
 | |
| 109 |   val exists2: ('a * 'b -> bool) -> 'a list * 'b list -> bool
 | |
| 110 |   val forall2: ('a * 'b -> bool) -> 'a list * 'b list -> bool
 | |
| 4956 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 111 |   val seq2: ('a * 'b -> unit) -> 'a list * 'b list -> unit
 | 
| 4621 | 112 |   val ~~ : 'a list * 'b list -> ('a * 'b) list
 | 
| 113 |   val split_list: ('a * 'b) list -> 'a list * 'b list
 | |
| 7468 
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
 wenzelm parents: 
7090diff
changeset | 114 |   val equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool
 | 
| 4621 | 115 | val prefix: ''a list * ''a list -> bool | 
| 116 |   val take_prefix: ('a -> bool) -> 'a list -> 'a list * 'a list
 | |
| 117 |   val take_suffix: ('a -> bool) -> 'a list -> 'a list * 'a list
 | |
| 118 | ||
| 119 | (*integers*) | |
| 120 | val inc: int ref -> int | |
| 121 | val dec: int ref -> int | |
| 122 | val upto: int * int -> int list | |
| 123 | val downto: int * int -> int list | |
| 124 | val downto0: int list * int -> bool | |
| 125 | val radixpand: int * int -> int list | |
| 126 | val radixstring: int * string * int -> string | |
| 127 | val string_of_int: int -> string | |
| 128 | val string_of_indexname: string * int -> string | |
| 129 | ||
| 130 | (*strings*) | |
| 6312 | 131 | val nth_elem_string: int * string -> string | 
| 6282 | 132 |   val foldl_string: ('a * string -> 'a) -> 'a * string -> 'a
 | 
| 6312 | 133 | val exists_string: (string -> bool) -> string -> bool | 
| 4621 | 134 | val enclose: string -> string -> string -> string | 
| 6642 | 135 | val unenclose: string -> string | 
| 4621 | 136 | val quote: string -> string | 
| 137 | val space_implode: string -> string list -> string | |
| 138 | val commas: string list -> string | |
| 139 | val commas_quote: string list -> string | |
| 140 | val cat_lines: string list -> string | |
| 141 | val space_explode: string -> string -> string list | |
| 5942 | 142 | val std_output: string -> unit | 
| 143 | val prefix_lines: string -> string -> string | |
| 4621 | 144 | val split_lines: string -> string list | 
| 7712 | 145 | val untabify: string list -> string list | 
| 5285 | 146 | val suffix: string -> string -> string | 
| 147 | val unsuffix: string -> string -> string | |
| 4621 | 148 | |
| 149 | (*lists as sets*) | |
| 150 | val mem: ''a * ''a list -> bool | |
| 151 | val mem_int: int * int list -> bool | |
| 152 | val mem_string: string * string list -> bool | |
| 153 |   val gen_mem: ('a * 'b -> bool) -> 'a * 'b list -> bool
 | |
| 154 | val ins: ''a * ''a list -> ''a list | |
| 155 | val ins_int: int * int list -> int list | |
| 156 | val ins_string: string * string list -> string list | |
| 157 |   val gen_ins: ('a * 'a -> bool) -> 'a * 'a list -> 'a list
 | |
| 158 | val union: ''a list * ''a list -> ''a list | |
| 159 | val union_int: int list * int list -> int list | |
| 160 | val union_string: string list * string list -> string list | |
| 161 |   val gen_union: ('a * 'a -> bool) -> 'a list * 'a list -> 'a list
 | |
| 7090 | 162 |   val gen_inter: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
 | 
| 4621 | 163 | val inter: ''a list * ''a list -> ''a list | 
| 164 | val inter_int: int list * int list -> int list | |
| 165 | val inter_string: string list * string list -> string list | |
| 166 | val subset: ''a list * ''a list -> bool | |
| 167 | val subset_int: int list * int list -> bool | |
| 168 | val subset_string: string list * string list -> bool | |
| 169 | val eq_set: ''a list * ''a list -> bool | |
| 170 | val eq_set_string: string list * string list -> bool | |
| 171 |   val gen_subset: ('a * 'b -> bool) -> 'a list * 'b list -> bool
 | |
| 172 | val \ : ''a list * ''a -> ''a list | |
| 173 | val \\ : ''a list * ''a list -> ''a list | |
| 174 |   val gen_rem: ('a * 'b -> bool) -> 'a list * 'b -> 'a list
 | |
| 175 |   val gen_rems: ('a * 'b -> bool) -> 'a list * 'b list -> 'a list
 | |
| 176 |   val gen_distinct: ('a * 'a -> bool) -> 'a list -> 'a list
 | |
| 177 | val distinct: ''a list -> ''a list | |
| 178 | val findrep: ''a list -> ''a list | |
| 179 |   val gen_duplicates: ('a * 'a -> bool) -> 'a list -> 'a list
 | |
| 180 | val duplicates: ''a list -> ''a list | |
| 181 | ||
| 182 | (*association lists*) | |
| 183 |   val assoc: (''a * 'b) list * ''a -> 'b option
 | |
| 184 | val assoc_int: (int * 'a) list * int -> 'a option | |
| 185 | val assoc_string: (string * 'a) list * string -> 'a option | |
| 186 | val assoc_string_int: ((string * int) * 'a) list * (string * int) -> 'a option | |
| 187 |   val assocs: (''a * 'b list) list -> ''a -> 'b list
 | |
| 188 |   val assoc2: (''a * (''b * 'c) list) list * (''a * ''b) -> 'c option
 | |
| 189 |   val gen_assoc: ('a * 'b -> bool) -> ('b * 'c) list * 'a -> 'c option
 | |
| 190 |   val overwrite: (''a * 'b) list * (''a * 'b) -> (''a * 'b) list
 | |
| 191 |   val gen_overwrite: ('a * 'a -> bool) -> ('a * 'b) list * ('a * 'b) -> ('a * 'b) list
 | |
| 192 | ||
| 193 | (*generic tables*) | |
| 194 |   val generic_extend: ('a * 'a -> bool)
 | |
| 195 |     -> ('b -> 'a list) -> ('a list -> 'b) -> 'b -> 'a list -> 'b
 | |
| 196 |   val generic_merge: ('a * 'a -> bool) -> ('b -> 'a list) -> ('a list -> 'b) -> 'b -> 'b -> 'b
 | |
| 197 | val extend_list: ''a list -> ''a list -> ''a list | |
| 198 | val merge_lists: ''a list -> ''a list -> ''a list | |
| 4692 | 199 |   val merge_alists: (''a * 'b) list -> (''a * 'b) list -> (''a * 'b) list
 | 
| 4621 | 200 | val merge_rev_lists: ''a list -> ''a list -> ''a list | 
| 201 | ||
| 202 | (*balanced trees*) | |
| 203 | exception Balance | |
| 204 |   val fold_bal: ('a * 'a -> 'a) -> 'a list -> 'a
 | |
| 205 |   val access_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> int -> 'a
 | |
| 206 |   val accesses_bal: ('a -> 'a) * ('a -> 'a) * 'a -> int -> 'a list
 | |
| 207 | ||
| 208 | (*orders*) | |
| 8043 
0e4434d55df9
fixed the discrepancy in the ordering of the constructors LESS EQUAL GREATER
 paulson parents: 
7712diff
changeset | 209 | datatype order = LESS | EQUAL | GREATER | 
| 4621 | 210 | val rev_order: order -> order | 
| 211 |   val make_ord: ('a * 'a -> bool) -> 'a * 'a -> order
 | |
| 212 | val int_ord: int * int -> order | |
| 213 | val string_ord: string * string -> order | |
| 214 |   val prod_ord: ('a * 'b -> order) -> ('c * 'd -> order) -> ('a * 'c) * ('b * 'd) -> order
 | |
| 215 |   val dict_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
 | |
| 216 |   val list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order
 | |
| 217 |   val sort: ('a * 'a -> order) -> 'a list -> 'a list
 | |
| 218 | val sort_strings: string list -> string list | |
| 219 |   val sort_wrt: ('a -> string) -> 'a list -> 'a list
 | |
| 220 | ||
| 221 | (*I/O and diagnostics*) | |
| 222 | val cd: string -> unit | |
| 223 | val pwd: unit -> string | |
| 5966 
60f80b2a2777
removed prs / prs_fn (broken, because it did not include \n in its
 wenzelm parents: 
5949diff
changeset | 224 | val writeln_fn: (string -> unit) ref | 
| 4621 | 225 | val warning_fn: (string -> unit) ref | 
| 226 | val error_fn: (string -> unit) ref | |
| 227 | val writeln: string -> unit | |
| 228 | val warning: string -> unit | |
| 229 | exception ERROR | |
| 230 | val error_msg: string -> unit | |
| 231 | val error: string -> 'a | |
| 232 | val sys_error: string -> 'a | |
| 233 | val assert: bool -> string -> unit | |
| 234 | val deny: bool -> string -> unit | |
| 235 |   val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
 | |
| 236 | datatype 'a error = Error of string | OK of 'a | |
| 237 | val get_error: 'a error -> string option | |
| 238 | val get_ok: 'a error -> 'a option | |
| 239 |   val handle_error: ('a -> 'b) -> 'a -> 'b error
 | |
| 4923 | 240 | exception ERROR_MESSAGE of string | 
| 241 |   val transform_error: ('a -> 'b) -> 'a -> 'b
 | |
| 5904 | 242 |   val transform_failure: (exn -> exn) -> ('a -> 'b) -> 'a -> 'b
 | 
| 4621 | 243 | |
| 244 | (*timing*) | |
| 245 | val cond_timeit: bool -> (unit -> 'a) -> 'a | |
| 246 | val timeit: (unit -> 'a) -> 'a | |
| 247 |   val timeap: ('a -> 'b) -> 'a -> 'b
 | |
| 8999 | 248 | val timing: bool ref | 
| 4621 | 249 | |
| 250 | (*misc*) | |
| 251 |   val make_keylist: ('a -> 'b) -> 'a list -> ('a * 'b) list
 | |
| 252 |   val keyfilter: ('a * ''b) list -> ''b -> 'a list
 | |
| 253 |   val partition: ('a -> bool) -> 'a list -> 'a list * 'a list
 | |
| 254 |   val partition_eq: ('a * 'a -> bool) -> 'a list -> 'a list list
 | |
| 255 | val partition_list: (int -> 'a -> bool) -> int -> int -> 'a list -> 'a list list | |
| 256 | val transitive_closure: (string * string list) list -> (string * string list) list | |
| 257 | val init_gensym: unit -> unit | |
| 258 | val gensym: string -> string | |
| 259 | val bump_int_list: string list -> string list | |
| 260 | val bump_list: string list * string -> string list | |
| 261 | val bump_string: string -> string | |
| 262 | val scanwords: (string -> bool) -> string list -> string list | |
| 263 | datatype 'a mtree = Join of 'a * 'a mtree list | |
| 264 | end; | |
| 265 | ||
| 266 | structure Library: LIBRARY = | |
| 1364 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 267 | struct | 
| 0 | 268 | |
| 4995 | 269 | |
| 233 | 270 | (** functions **) | 
| 0 | 271 | |
| 233 | 272 | (*handy combinators*) | 
| 273 | fun curry f x y = f (x, y); | |
| 274 | fun uncurry f (x, y) = f x y; | |
| 275 | fun I x = x; | |
| 276 | fun K x y = x; | |
| 0 | 277 | |
| 380 | 278 | (*reverse apply*) | 
| 410 | 279 | fun (x |> f) = f x; | 
| 8418 | 280 | fun ((x, y) |>> f) = (f x, y); | 
| 281 | fun ((x, y) |>>> f) = let val (x', z) = f x in (x', (y, z)) end; | |
| 380 | 282 | |
| 233 | 283 | (*application of (infix) operator to its left or right argument*) | 
| 284 | fun apl (x, f) y = f (x, y); | |
| 285 | fun apr (f, y) x = f (x, y); | |
| 0 | 286 | |
| 233 | 287 | (*function exponentiation: f(...(f x)...) with n applications of f*) | 
| 288 | fun funpow n f x = | |
| 289 | let fun rep (0, x) = x | |
| 290 | | rep (n, x) = rep (n - 1, f x) | |
| 291 | in rep (n, x) end; | |
| 160 | 292 | |
| 5893 | 293 | (*concatenation: 2 and 3 args*) | 
| 294 | fun (f oo g) x y = f (g x y); | |
| 295 | fun (f ooo g) x y z = f (g x y z); | |
| 6510 | 296 | fun (f oooo g) x y z w = f (g x y z w); | 
| 297 | ||
| 160 | 298 | |
| 299 | ||
| 2471 | 300 | (** stamps **) | 
| 301 | ||
| 302 | type stamp = unit ref; | |
| 303 | val stamp: unit -> stamp = ref; | |
| 304 | ||
| 305 | ||
| 306 | ||
| 233 | 307 | (** options **) | 
| 0 | 308 | |
| 309 | datatype 'a option = None | Some of 'a; | |
| 310 | ||
| 4139 | 311 | exception OPTION; | 
| 0 | 312 | |
| 313 | fun the (Some x) = x | |
| 4139 | 314 | | the None = raise OPTION; | 
| 0 | 315 | |
| 4212 | 316 | (*strict!*) | 
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 317 | fun if_none None y = y | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 318 | | if_none (Some x) _ = x; | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 319 | |
| 0 | 320 | fun is_some (Some _) = true | 
| 321 | | is_some None = false; | |
| 322 | ||
| 323 | fun is_none (Some _) = false | |
| 324 | | is_none None = true; | |
| 325 | ||
| 233 | 326 | fun apsome f (Some x) = Some (f x) | 
| 327 | | apsome _ None = None; | |
| 0 | 328 | |
| 6959 | 329 | |
| 330 | (* exception handling *) | |
| 331 | ||
| 332 | exception ERROR; | |
| 333 | ||
| 334 | fun try f x = Some (f x) | |
| 335 | handle Interrupt => raise Interrupt | ERROR => raise ERROR | _ => None; | |
| 336 | ||
| 337 | fun can f x = is_some (try f x); | |
| 4139 | 338 | |
| 339 | ||
| 340 | ||
| 233 | 341 | (** pairs **) | 
| 342 | ||
| 343 | fun pair x y = (x, y); | |
| 344 | fun rpair x y = (y, x); | |
| 345 | ||
| 346 | fun fst (x, y) = x; | |
| 347 | fun snd (x, y) = y; | |
| 348 | ||
| 349 | fun eq_fst ((x1, _), (x2, _)) = x1 = x2; | |
| 350 | fun eq_snd ((_, y1), (_, y2)) = y1 = y2; | |
| 351 | ||
| 352 | fun swap (x, y) = (y, x); | |
| 353 | ||
| 4212 | 354 | (*apply function to components*) | 
| 233 | 355 | fun apfst f (x, y) = (f x, y); | 
| 356 | fun apsnd f (x, y) = (x, f y); | |
| 4212 | 357 | fun pairself f (x, y) = (f x, f y); | 
| 233 | 358 | |
| 359 | ||
| 360 | ||
| 361 | (** booleans **) | |
| 362 | ||
| 363 | (* equality *) | |
| 364 | ||
| 365 | fun equal x y = x = y; | |
| 366 | fun not_equal x y = x <> y; | |
| 367 | ||
| 368 | ||
| 369 | (* operators for combining predicates *) | |
| 370 | ||
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 371 | fun (p orf q) = fn x => p x orelse q x; | 
| 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 372 | fun (p andf q) = fn x => p x andalso q x; | 
| 233 | 373 | |
| 374 | ||
| 375 | (* predicates on lists *) | |
| 376 | ||
| 377 | (*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) | |
| 378 | fun exists (pred: 'a -> bool) : 'a list -> bool = | |
| 379 | let fun boolf [] = false | |
| 380 | | boolf (x :: xs) = pred x orelse boolf xs | |
| 381 | in boolf end; | |
| 382 | ||
| 383 | (*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*) | |
| 384 | fun forall (pred: 'a -> bool) : 'a list -> bool = | |
| 385 | let fun boolf [] = true | |
| 386 | | boolf (x :: xs) = pred x andalso boolf xs | |
| 387 | in boolf end; | |
| 0 | 388 | |
| 233 | 389 | |
| 380 | 390 | (* flags *) | 
| 391 | ||
| 392 | fun set flag = (flag := true; true); | |
| 393 | fun reset flag = (flag := false; false); | |
| 394 | fun toggle flag = (flag := not (! flag); ! flag); | |
| 395 | ||
| 9118 | 396 | fun change r f = r := f (! r); | 
| 397 | ||
| 4212 | 398 | (*temporarily set flag, handling errors*) | 
| 2978 | 399 | fun setmp flag value f x = | 
| 2958 | 400 | let | 
| 401 | val orig_value = ! flag; | |
| 402 | fun return y = (flag := orig_value; y); | |
| 403 | in | |
| 404 | flag := value; | |
| 405 | return (f x handle exn => (return (); raise exn)) | |
| 406 | end; | |
| 407 | ||
| 380 | 408 | |
| 233 | 409 | |
| 410 | (** lists **) | |
| 411 | ||
| 412 | exception LIST of string; | |
| 413 | ||
| 414 | fun null [] = true | |
| 415 | | null (_ :: _) = false; | |
| 416 | ||
| 417 | fun hd [] = raise LIST "hd" | |
| 418 | | hd (x :: _) = x; | |
| 419 | ||
| 420 | fun tl [] = raise LIST "tl" | |
| 421 | | tl (_ :: xs) = xs; | |
| 422 | ||
| 423 | fun cons x xs = x :: xs; | |
| 5285 | 424 | fun single x = [x]; | 
| 233 | 425 | |
| 4629 | 426 | fun append xs ys = xs @ ys; | 
| 427 | ||
| 5904 | 428 | fun apply [] x = x | 
| 429 | | apply (f :: fs) x = apply fs (f x); | |
| 430 | ||
| 233 | 431 | |
| 432 | (* fold *) | |
| 433 | ||
| 434 | (*the following versions of fold are designed to fit nicely with infixes*) | |
| 0 | 435 | |
| 233 | 436 | (* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn | 
| 437 | for operators that associate to the left (TAIL RECURSIVE)*) | |
| 438 | fun foldl (f: 'a * 'b -> 'a) : 'a * 'b list -> 'a = | |
| 439 | let fun itl (e, []) = e | |
| 440 | | itl (e, a::l) = itl (f(e, a), l) | |
| 441 | in itl end; | |
| 442 | ||
| 443 | (* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) | |
| 444 | for operators that associate to the right (not tail recursive)*) | |
| 445 | fun foldr f (l, e) = | |
| 446 | let fun itr [] = e | |
| 447 | | itr (a::l) = f(a, itr l) | |
| 448 | in itr l end; | |
| 449 | ||
| 450 | (* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n-1] @ xn)) | |
| 451 | for n > 0, operators that associate to the right (not tail recursive)*) | |
| 452 | fun foldr1 f l = | |
| 4181 | 453 | let fun itr [x] = x | 
| 233 | 454 | | itr (x::l) = f(x, itr l) | 
| 455 | in itr l end; | |
| 456 | ||
| 4956 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 457 | fun foldl_map _ (x, []) = (x, []) | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 458 | | foldl_map f (x, y :: ys) = | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 459 | let | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 460 | val (x', y') = f (x, y); | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 461 | val (x'', ys') = foldl_map f (x', ys); | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 462 | in (x'', y' :: ys') end; | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 463 | |
| 233 | 464 | |
| 465 | (* basic list functions *) | |
| 466 | ||
| 467 | (*length of a list, should unquestionably be a standard function*) | |
| 468 | local fun length1 (n, []) = n (*TAIL RECURSIVE*) | |
| 469 | | length1 (n, x :: xs) = length1 (n + 1, xs) | |
| 470 | in fun length l = length1 (0, l) end; | |
| 471 | ||
| 472 | (*take the first n elements from a list*) | |
| 473 | fun take (n, []) = [] | |
| 474 | | take (n, x :: xs) = | |
| 475 | if n > 0 then x :: take (n - 1, xs) else []; | |
| 476 | ||
| 477 | (*drop the first n elements from a list*) | |
| 478 | fun drop (n, []) = [] | |
| 479 | | drop (n, x :: xs) = | |
| 480 | if n > 0 then drop (n - 1, xs) else x :: xs; | |
| 0 | 481 | |
| 4713 | 482 | fun dropwhile P [] = [] | 
| 483 | | dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; | |
| 484 | ||
| 233 | 485 | (*return nth element of a list, where 0 designates the first element; | 
| 486 | raise EXCEPTION if list too short*) | |
| 487 | fun nth_elem NL = | |
| 488 | (case drop NL of | |
| 489 | [] => raise LIST "nth_elem" | |
| 490 | | x :: _ => x); | |
| 491 | ||
| 492 | (*last element of a list*) | |
| 493 | fun last_elem [] = raise LIST "last_elem" | |
| 494 | | last_elem [x] = x | |
| 495 | | last_elem (_ :: xs) = last_elem xs; | |
| 496 | ||
| 3762 | 497 | (*rear decomposition*) | 
| 498 | fun split_last [] = raise LIST "split_last" | |
| 499 | | split_last [x] = ([], x) | |
| 500 | | split_last (x :: xs) = apfst (cons x) (split_last xs); | |
| 501 | ||
| 4893 | 502 | (*update nth element*) | 
| 503 | fun nth_update x (n, xs) = | |
| 504 | let | |
| 505 | val prfx = take (n, xs); | |
| 506 | val sffx = drop (n, xs); | |
| 507 | in | |
| 508 | (case sffx of | |
| 509 | [] => raise LIST "nth_update" | |
| 510 | | _ :: sffx' => prfx @ (x :: sffx')) | |
| 511 | end; | |
| 512 | ||
| 4212 | 513 | (*find the position of an element in a list*) | 
| 514 | fun find_index pred = | |
| 515 | let fun find _ [] = ~1 | |
| 516 | | find n (x :: xs) = if pred x then n else find (n + 1) xs; | |
| 517 | in find 0 end; | |
| 3762 | 518 | |
| 4224 | 519 | fun find_index_eq x = find_index (equal x); | 
| 4212 | 520 | |
| 521 | (*find first element satisfying predicate*) | |
| 522 | fun find_first _ [] = None | |
| 523 | | find_first pred (x :: xs) = | |
| 524 | if pred x then Some x else find_first pred xs; | |
| 233 | 525 | |
| 4916 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 526 | (*get first element by lookup function*) | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 527 | fun get_first _ [] = None | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 528 | | get_first f (x :: xs) = | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 529 | (case f x of | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 530 | None => get_first f xs | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 531 | | some => some); | 
| 
fe8b0c82691b
get_first: ('a -> 'b option) -> 'a list -> 'b option;
 wenzelm parents: 
4893diff
changeset | 532 | |
| 233 | 533 | (*flatten a list of lists to a list*) | 
| 534 | fun flat (ls: 'c list list) : 'c list = foldr (op @) (ls, []); | |
| 535 | ||
| 536 | (*like Lisp's MAPC -- seq proc [x1, ..., xn] evaluates | |
| 537 | (proc x1; ...; proc xn) for side effects*) | |
| 538 | fun seq (proc: 'a -> unit) : 'a list -> unit = | |
| 539 | let fun seqf [] = () | |
| 540 | | seqf (x :: xs) = (proc x; seqf xs) | |
| 541 | in seqf end; | |
| 542 | ||
| 543 | (*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) | |
| 544 | fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs | |
| 545 | | separate _ xs = xs; | |
| 546 | ||
| 547 | (*make the list [x, x, ..., x] of length n*) | |
| 548 | fun replicate n (x: 'a) : 'a list = | |
| 549 | let fun rep (0, xs) = xs | |
| 550 | | rep (n, xs) = rep (n - 1, x :: xs) | |
| 551 | in | |
| 552 | if n < 0 then raise LIST "replicate" | |
| 553 | else rep (n, []) | |
| 554 | end; | |
| 555 | ||
| 4248 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 556 | (*multiply [a, b, c, ...] * [xs, ys, zs, ...]*) | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 557 | fun multiply ([], _) = [] | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 558 | | multiply (x :: xs, yss) = map (cons x) yss @ multiply (xs, yss); | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 559 | |
| 233 | 560 | |
| 561 | (* filter *) | |
| 562 | ||
| 563 | (*copy the list preserving elements that satisfy the predicate*) | |
| 564 | fun filter (pred: 'a->bool) : 'a list -> 'a list = | |
| 0 | 565 | let fun filt [] = [] | 
| 233 | 566 | | filt (x :: xs) = if pred x then x :: filt xs else filt xs | 
| 567 | in filt end; | |
| 0 | 568 | |
| 569 | fun filter_out f = filter (not o f); | |
| 570 | ||
| 233 | 571 | fun mapfilter (f: 'a -> 'b option) ([]: 'a list) = [] : 'b list | 
| 572 | | mapfilter f (x :: xs) = | |
| 573 | (case f x of | |
| 574 | None => mapfilter f xs | |
| 575 | | Some y => y :: mapfilter f xs); | |
| 576 | ||
| 577 | ||
| 578 | (* lists of pairs *) | |
| 579 | ||
| 380 | 580 | fun map2 _ ([], []) = [] | 
| 581 | | map2 f (x :: xs, y :: ys) = (f (x, y) :: map2 f (xs, ys)) | |
| 582 | | map2 _ _ = raise LIST "map2"; | |
| 583 | ||
| 584 | fun exists2 _ ([], []) = false | |
| 585 | | exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys) | |
| 586 | | exists2 _ _ = raise LIST "exists2"; | |
| 587 | ||
| 588 | fun forall2 _ ([], []) = true | |
| 589 | | forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys) | |
| 590 | | forall2 _ _ = raise LIST "forall2"; | |
| 591 | ||
| 4956 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 592 | fun seq2 _ ([], []) = () | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 593 | | seq2 f (x :: xs, y :: ys) = (f (x, y); seq2 f (xs, ys)) | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 594 | | seq2 _ _ = raise LIST "seq2"; | 
| 
a7538e43896e
added foldl_map: ('a * 'b -> 'a * 'c) -> 'a * 'b list -> 'a * 'c list;
 wenzelm parents: 
4945diff
changeset | 595 | |
| 233 | 596 | (*combine two lists forming a list of pairs: | 
| 597 | [x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) | |
| 598 | fun [] ~~ [] = [] | |
| 599 | | (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) | |
| 600 | | _ ~~ _ = raise LIST "~~"; | |
| 601 | ||
| 602 | (*inverse of ~~; the old 'split': | |
| 603 | [(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) | |
| 604 | fun split_list (l: ('a * 'b) list) = (map #1 l, map #2 l);
 | |
| 605 | ||
| 7468 
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
 wenzelm parents: 
7090diff
changeset | 606 | fun equal_lists eq (xs, ys) = length xs = length ys andalso forall2 eq (xs, ys); | 
| 
6ce03d2f7d91
equal_lists: ('a * 'b -> bool) -> 'a list * 'b list -> bool;
 wenzelm parents: 
7090diff
changeset | 607 | |
| 233 | 608 | |
| 609 | (* prefixes, suffixes *) | |
| 610 | ||
| 611 | fun [] prefix _ = true | |
| 612 | | (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys) | |
| 613 | | _ prefix _ = false; | |
| 614 | ||
| 615 | (* [x1, ..., xi, ..., xn] ---> ([x1, ..., x(i-1)], [xi, ..., xn]) | |
| 616 | where xi is the first element that does not satisfy the predicate*) | |
| 617 | fun take_prefix (pred : 'a -> bool) (xs: 'a list) : 'a list * 'a list = | |
| 618 | let fun take (rxs, []) = (rev rxs, []) | |
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 619 | | take (rxs, x :: xs) = | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 620 | if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) | 
| 233 | 621 | in take([], xs) end; | 
| 622 | ||
| 623 | (* [x1, ..., xi, ..., xn] ---> ([x1, ..., xi], [x(i+1), ..., xn]) | |
| 624 | where xi is the last element that does not satisfy the predicate*) | |
| 625 | fun take_suffix _ [] = ([], []) | |
| 626 | | take_suffix pred (x :: xs) = | |
| 627 | (case take_suffix pred xs of | |
| 628 | ([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) | |
| 629 | | (prfx, sffx) => (x :: prfx, sffx)); | |
| 630 | ||
| 631 | ||
| 632 | ||
| 633 | (** integers **) | |
| 634 | ||
| 2958 | 635 | fun inc i = (i := ! i + 1; ! i); | 
| 636 | fun dec i = (i := ! i - 1; ! i); | |
| 233 | 637 | |
| 638 | ||
| 639 | (* lists of integers *) | |
| 640 | ||
| 641 | (*make the list [from, from + 1, ..., to]*) | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 642 | fun (from upto to) = | 
| 233 | 643 | if from > to then [] else from :: ((from + 1) upto to); | 
| 644 | ||
| 645 | (*make the list [from, from - 1, ..., to]*) | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 646 | fun (from downto to) = | 
| 233 | 647 | if from < to then [] else from :: ((from - 1) downto to); | 
| 648 | ||
| 649 | (*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*) | |
| 650 | fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1) | |
| 651 | | downto0 ([], n) = n = ~1; | |
| 652 | ||
| 653 | ||
| 654 | (* convert integers to strings *) | |
| 655 | ||
| 656 | (*expand the number in the given base; | |
| 657 | example: radixpand (2, 8) gives [1, 0, 0, 0]*) | |
| 658 | fun radixpand (base, num) : int list = | |
| 659 | let | |
| 660 | fun radix (n, tail) = | |
| 661 | if n < base then n :: tail | |
| 662 | else radix (n div base, (n mod base) :: tail) | |
| 663 | in radix (num, []) end; | |
| 664 | ||
| 665 | (*expands a number into a string of characters starting from "zerochar"; | |
| 666 | example: radixstring (2, "0", 8) gives "1000"*) | |
| 667 | fun radixstring (base, zerochar, num) = | |
| 668 | let val offset = ord zerochar; | |
| 669 | fun chrof n = chr (offset + n) | |
| 670 | in implode (map chrof (radixpand (base, num))) end; | |
| 671 | ||
| 672 | ||
| 3407 
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
 paulson parents: 
3393diff
changeset | 673 | val string_of_int = Int.toString; | 
| 233 | 674 | |
| 3407 
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
 paulson parents: 
3393diff
changeset | 675 | fun string_of_indexname (a,0) = a | 
| 
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
 paulson parents: 
3393diff
changeset | 676 | | string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; | 
| 233 | 677 | |
| 678 | ||
| 4212 | 679 | |
| 233 | 680 | (** strings **) | 
| 681 | ||
| 6312 | 682 | (*functions tuned for strings, avoiding explode*) | 
| 683 | ||
| 684 | fun nth_elem_string (i, str) = | |
| 6959 | 685 | (case try String.substring (str, i, 1) of | 
| 686 | Some s => s | |
| 687 | | None => raise LIST "nth_elem_string"); | |
| 6312 | 688 | |
| 6282 | 689 | fun foldl_string f (x0, str) = | 
| 690 | let | |
| 691 | val n = size str; | |
| 692 | fun fold (x, i) = if i < n then fold (f (x, String.substring (str, i, 1)), i + 1) else x | |
| 693 | in fold (x0, 0) end; | |
| 694 | ||
| 6312 | 695 | fun exists_string pred str = foldl_string (fn (b, s) => b orelse pred s) (false, str); | 
| 696 | ||
| 512 
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
 lcp parents: 
410diff
changeset | 697 | (*enclose in brackets*) | 
| 
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
 lcp parents: 
410diff
changeset | 698 | fun enclose lpar rpar str = lpar ^ str ^ rpar; | 
| 6642 | 699 | fun unenclose str = String.substring (str, 1, size str - 2); | 
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 700 | |
| 233 | 701 | (*simple quoting (does not escape special chars)*) | 
| 512 
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
 lcp parents: 
410diff
changeset | 702 | val quote = enclose "\"" "\""; | 
| 233 | 703 | |
| 4212 | 704 | (*space_implode "..." (explode "hello") = "h...e...l...l...o"*) | 
| 233 | 705 | fun space_implode a bs = implode (separate a bs); | 
| 706 | ||
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 707 | val commas = space_implode ", "; | 
| 380 | 708 | val commas_quote = commas o map quote; | 
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 709 | |
| 233 | 710 | (*concatenate messages, one per line, into a string*) | 
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 711 | val cat_lines = space_implode "\n"; | 
| 233 | 712 | |
| 4212 | 713 | (*space_explode "." "h.e..l.lo" = ["h", "e", "", "l", "lo"]*) | 
| 3832 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 714 | fun space_explode _ "" = [] | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 715 | | space_explode sep str = | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 716 | let | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 717 | fun expl chs = | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 718 | (case take_prefix (not_equal sep) chs of | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 719 | (cs, []) => [implode cs] | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 720 | | (cs, _ :: cs') => implode cs :: expl cs'); | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 721 | in expl (explode str) end; | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 722 | |
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 723 | val split_lines = space_explode "\n"; | 
| 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 724 | |
| 7712 | 725 | (*untabify*) | 
| 726 | fun untabify chs = | |
| 727 | let | |
| 728 | val tab_width = 8; | |
| 729 | ||
| 730 | fun untab (_, "\n") = (0, ["\n"]) | |
| 9118 | 731 | | untab (pos, "\t") = | 
| 732 | let val d = tab_width - (pos mod tab_width) in (pos + d, replicate d " ") end | |
| 7712 | 733 | | untab (pos, c) = (pos + 1, [c]); | 
| 734 | in | |
| 735 | if not (exists (equal "\t") chs) then chs | |
| 736 | else flat (#2 (foldl_map untab (0, chs))) | |
| 737 | end; | |
| 738 | ||
| 5285 | 739 | (*append suffix*) | 
| 740 | fun suffix sfx s = s ^ sfx; | |
| 741 | ||
| 742 | (*remove suffix*) | |
| 743 | fun unsuffix sfx s = | |
| 744 | let | |
| 745 | val cs = explode s; | |
| 746 | val prfx_len = size s - size sfx; | |
| 747 | in | |
| 748 | if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then | |
| 749 | implode (take (prfx_len, cs)) | |
| 750 | else raise LIST "unsuffix" | |
| 751 | end; | |
| 752 | ||
| 3832 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
 wenzelm parents: 
3762diff
changeset | 753 | |
| 233 | 754 | |
| 755 | (** lists as sets **) | |
| 756 | ||
| 757 | (*membership in a list*) | |
| 758 | fun x mem [] = false | |
| 759 | | x mem (y :: ys) = x = y orelse x mem ys; | |
| 0 | 760 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 761 | (*membership in a list, optimized version for ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 762 | fun (x:int) mem_int [] = false | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 763 | | x mem_int (y :: ys) = x = y orelse x mem_int ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 764 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 765 | (*membership in a list, optimized version for strings*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 766 | fun (x:string) mem_string [] = false | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 767 | | x mem_string (y :: ys) = x = y orelse x mem_string ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 768 | |
| 0 | 769 | (*generalized membership test*) | 
| 233 | 770 | fun gen_mem eq (x, []) = false | 
| 771 | | gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); | |
| 772 | ||
| 773 | ||
| 774 | (*insertion into list if not already there*) | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 775 | fun (x ins xs) = if x mem xs then xs else x :: xs; | 
| 0 | 776 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 777 | (*insertion into list, optimized version for ints*) | 
| 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 778 | fun (x ins_int xs) = if x mem_int xs then xs else x :: xs; | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 779 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 780 | (*insertion into list, optimized version for strings*) | 
| 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 781 | fun (x ins_string xs) = if x mem_string xs then xs else x :: xs; | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 782 | |
| 0 | 783 | (*generalized insertion*) | 
| 233 | 784 | fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; | 
| 785 | ||
| 786 | ||
| 787 | (*union of sets represented as lists: no repetitions*) | |
| 788 | fun xs union [] = xs | |
| 789 | | [] union ys = ys | |
| 790 | | (x :: xs) union ys = xs union (x ins ys); | |
| 0 | 791 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 792 | (*union of sets, optimized version for ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 793 | fun (xs:int list) union_int [] = xs | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 794 | | [] union_int ys = ys | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 795 | | (x :: xs) union_int ys = xs union_int (x ins_int ys); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 796 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 797 | (*union of sets, optimized version for strings*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 798 | fun (xs:string list) union_string [] = xs | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 799 | | [] union_string ys = ys | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 800 | | (x :: xs) union_string ys = xs union_string (x ins_string ys); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 801 | |
| 0 | 802 | (*generalized union*) | 
| 233 | 803 | fun gen_union eq (xs, []) = xs | 
| 804 | | gen_union eq ([], ys) = ys | |
| 805 | | gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys)); | |
| 806 | ||
| 807 | ||
| 808 | (*intersection*) | |
| 809 | fun [] inter ys = [] | |
| 810 | | (x :: xs) inter ys = | |
| 811 | if x mem ys then x :: (xs inter ys) else xs inter ys; | |
| 812 | ||
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 813 | (*intersection, optimized version for ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 814 | fun ([]:int list) inter_int ys = [] | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 815 | | (x :: xs) inter_int ys = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 816 | if x mem_int ys then x :: (xs inter_int ys) else xs inter_int ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 817 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 818 | (*intersection, optimized version for strings *) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 819 | fun ([]:string list) inter_string ys = [] | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 820 | | (x :: xs) inter_string ys = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 821 | if x mem_string ys then x :: (xs inter_string ys) else xs inter_string ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 822 | |
| 7090 | 823 | (*generalized intersection*) | 
| 824 | fun gen_inter eq ([], ys) = [] | |
| 825 | | gen_inter eq (x::xs, ys) = | |
| 826 | if gen_mem eq (x,ys) then x :: gen_inter eq (xs, ys) | |
| 827 | else gen_inter eq (xs, ys); | |
| 828 | ||
| 233 | 829 | |
| 830 | (*subset*) | |
| 831 | fun [] subset ys = true | |
| 832 | | (x :: xs) subset ys = x mem ys andalso xs subset ys; | |
| 833 | ||
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 834 | (*subset, optimized version for ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 835 | fun ([]:int list) subset_int ys = true | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 836 | | (x :: xs) subset_int ys = x mem_int ys andalso xs subset_int ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 837 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 838 | (*subset, optimized version for strings*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 839 | fun ([]:string list) subset_string ys = true | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 840 | | (x :: xs) subset_string ys = x mem_string ys andalso xs subset_string ys; | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 841 | |
| 4363 | 842 | (*set equality*) | 
| 843 | fun eq_set (xs, ys) = | |
| 844 | xs = ys orelse (xs subset ys andalso ys subset xs); | |
| 845 | ||
| 2182 
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2175diff
changeset | 846 | (*set equality for strings*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 847 | fun eq_set_string ((xs:string list), ys) = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 848 | xs = ys orelse (xs subset_string ys andalso ys subset_string xs); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 849 | |
| 2182 
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2175diff
changeset | 850 | fun gen_subset eq (xs, ys) = forall (fn x => gen_mem eq (x, ys)) xs; | 
| 
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2175diff
changeset | 851 | |
| 265 | 852 | |
| 233 | 853 | (*removing an element from a list WITHOUT duplicates*) | 
| 854 | fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x) | |
| 855 | | [] \ x = []; | |
| 856 | ||
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 857 | fun ys \\ xs = foldl (op \) (ys,xs); | 
| 0 | 858 | |
| 233 | 859 | (*removing an element from a list -- possibly WITH duplicates*) | 
| 860 | fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs; | |
| 861 | ||
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 862 | fun gen_rems eq = foldl (gen_rem eq); | 
| 233 | 863 | |
| 864 | ||
| 865 | (*makes a list of the distinct members of the input; preserves order, takes | |
| 866 | first of equal elements*) | |
| 867 | fun gen_distinct eq lst = | |
| 868 | let | |
| 869 | val memb = gen_mem eq; | |
| 0 | 870 | |
| 233 | 871 | fun dist (rev_seen, []) = rev rev_seen | 
| 872 | | dist (rev_seen, x :: xs) = | |
| 873 | if memb (x, rev_seen) then dist (rev_seen, xs) | |
| 874 | else dist (x :: rev_seen, xs); | |
| 875 | in | |
| 876 | dist ([], lst) | |
| 877 | end; | |
| 878 | ||
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 879 | fun distinct l = gen_distinct (op =) l; | 
| 233 | 880 | |
| 881 | ||
| 882 | (*returns the tail beginning with the first repeated element, or []*) | |
| 883 | fun findrep [] = [] | |
| 884 | | findrep (x :: xs) = if x mem xs then x :: xs else findrep xs; | |
| 885 | ||
| 886 | ||
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 887 | (*returns a list containing all repeated elements exactly once; preserves | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 888 | order, takes first of equal elements*) | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 889 | fun gen_duplicates eq lst = | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 890 | let | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 891 | val memb = gen_mem eq; | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 892 | |
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 893 | fun dups (rev_dups, []) = rev rev_dups | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 894 | | dups (rev_dups, x :: xs) = | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 895 | if memb (x, rev_dups) orelse not (memb (x, xs)) then | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 896 | dups (rev_dups, xs) | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 897 | else dups (x :: rev_dups, xs); | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 898 | in | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 899 | dups ([], lst) | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 900 | end; | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 901 | |
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 902 | fun duplicates l = gen_duplicates (op =) l; | 
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 903 | |
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 904 | |
| 233 | 905 | |
| 906 | (** association lists **) | |
| 0 | 907 | |
| 233 | 908 | (*association list lookup*) | 
| 909 | fun assoc ([], key) = None | |
| 910 | | assoc ((keyi, xi) :: pairs, key) = | |
| 911 | if key = keyi then Some xi else assoc (pairs, key); | |
| 912 | ||
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 913 | (*association list lookup, optimized version for ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 914 | fun assoc_int ([], (key:int)) = None | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 915 | | assoc_int ((keyi, xi) :: pairs, key) = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 916 | if key = keyi then Some xi else assoc_int (pairs, key); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 917 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 918 | (*association list lookup, optimized version for strings*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 919 | fun assoc_string ([], (key:string)) = None | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 920 | | assoc_string ((keyi, xi) :: pairs, key) = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 921 | if key = keyi then Some xi else assoc_string (pairs, key); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 922 | |
| 2175 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
 paulson parents: 
2157diff
changeset | 923 | (*association list lookup, optimized version for string*ints*) | 
| 1576 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 924 | fun assoc_string_int ([], (key:string*int)) = None | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 925 | | assoc_string_int ((keyi, xi) :: pairs, key) = | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 926 | if key = keyi then Some xi else assoc_string_int (pairs, key); | 
| 
af8f43f742a0
Added some optimized versions of functions dealing with sets
 berghofe parents: 
1460diff
changeset | 927 | |
| 233 | 928 | fun assocs ps x = | 
| 929 | (case assoc (ps, x) of | |
| 930 | None => [] | |
| 931 | | Some ys => ys); | |
| 932 | ||
| 255 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 933 | (*two-fold association list lookup*) | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 934 | fun assoc2 (aal, (key1, key2)) = | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 935 | (case assoc (aal, key1) of | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 936 | Some al => assoc (al, key2) | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 937 | | None => None); | 
| 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
 wenzelm parents: 
245diff
changeset | 938 | |
| 233 | 939 | (*generalized association list lookup*) | 
| 940 | fun gen_assoc eq ([], key) = None | |
| 941 | | gen_assoc eq ((keyi, xi) :: pairs, key) = | |
| 942 | if eq (key, keyi) then Some xi else gen_assoc eq (pairs, key); | |
| 943 | ||
| 944 | (*association list update*) | |
| 945 | fun overwrite (al, p as (key, _)) = | |
| 946 | let fun over ((q as (keyi, _)) :: pairs) = | |
| 947 | if keyi = key then p :: pairs else q :: (over pairs) | |
| 948 | | over [] = [p] | |
| 949 | in over al end; | |
| 950 | ||
| 2522 | 951 | fun gen_overwrite eq (al, p as (key, _)) = | 
| 952 | let fun over ((q as (keyi, _)) :: pairs) = | |
| 953 | if eq (keyi, key) then p :: pairs else q :: (over pairs) | |
| 954 | | over [] = [p] | |
| 955 | in over al end; | |
| 956 | ||
| 233 | 957 | |
| 958 | ||
| 959 | (** generic tables **) | |
| 0 | 960 | |
| 233 | 961 | (*Tables are supposed to be 'efficient' encodings of lists of elements distinct | 
| 962 | wrt. an equality "eq". The extend and merge operations below are optimized | |
| 963 | for long-term space efficiency.*) | |
| 964 | ||
| 965 | (*append (new) elements to a table*) | |
| 966 | fun generic_extend _ _ _ tab [] = tab | |
| 967 | | generic_extend eq dest_tab mk_tab tab1 lst2 = | |
| 968 | let | |
| 969 | val lst1 = dest_tab tab1; | |
| 970 | val new_lst2 = gen_rems eq (lst2, lst1); | |
| 971 | in | |
| 972 | if null new_lst2 then tab1 | |
| 973 | else mk_tab (lst1 @ new_lst2) | |
| 974 | end; | |
| 0 | 975 | |
| 233 | 976 | (*append (new) elements of 2nd table to 1st table*) | 
| 977 | fun generic_merge eq dest_tab mk_tab tab1 tab2 = | |
| 978 | let | |
| 979 | val lst1 = dest_tab tab1; | |
| 980 | val lst2 = dest_tab tab2; | |
| 981 | val new_lst2 = gen_rems eq (lst2, lst1); | |
| 982 | in | |
| 983 | if null new_lst2 then tab1 | |
| 984 | else if gen_subset eq (lst1, lst2) then tab2 | |
| 985 | else mk_tab (lst1 @ new_lst2) | |
| 986 | end; | |
| 0 | 987 | |
| 233 | 988 | |
| 989 | (*lists as tables*) | |
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 990 | fun extend_list tab = generic_extend (op =) I I tab; | 
| 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 991 | fun merge_lists tab = generic_merge (op =) I I tab; | 
| 4692 | 992 | fun merge_alists tab = generic_merge eq_fst I I tab; | 
| 233 | 993 | |
| 380 | 994 | fun merge_rev_lists xs [] = xs | 
| 995 | | merge_rev_lists [] ys = ys | |
| 996 | | merge_rev_lists xs (y :: ys) = | |
| 997 | (if y mem xs then I else cons y) (merge_rev_lists xs ys); | |
| 998 | ||
| 0 | 999 | |
| 1000 | ||
| 233 | 1001 | (** balanced trees **) | 
| 1002 | ||
| 1003 | exception Balance; (*indicates non-positive argument to balancing fun*) | |
| 1004 | ||
| 1005 | (*balanced folding; avoids deep nesting*) | |
| 1006 | fun fold_bal f [x] = x | |
| 1007 | | fold_bal f [] = raise Balance | |
| 1008 | | fold_bal f xs = | |
| 1009 | let val k = length xs div 2 | |
| 1010 | in f (fold_bal f (take(k, xs)), | |
| 1011 | fold_bal f (drop(k, xs))) | |
| 1012 | end; | |
| 1013 | ||
| 1014 | (*construct something of the form f(...g(...(x)...)) for balanced access*) | |
| 1015 | fun access_bal (f, g, x) n i = | |
| 1016 | let fun acc n i = (*1<=i<=n*) | |
| 1017 | if n=1 then x else | |
| 1018 | let val n2 = n div 2 | |
| 1019 | in if i<=n2 then f (acc n2 i) | |
| 1020 | else g (acc (n-n2) (i-n2)) | |
| 1021 | end | |
| 1022 | in if 1<=i andalso i<=n then acc n i else raise Balance end; | |
| 1023 | ||
| 1024 | (*construct ALL such accesses; could try harder to share recursive calls!*) | |
| 1025 | fun accesses_bal (f, g, x) n = | |
| 1026 | let fun acc n = | |
| 1027 | if n=1 then [x] else | |
| 1028 | let val n2 = n div 2 | |
| 1029 | val acc2 = acc n2 | |
| 1030 | in if n-n2=n2 then map f acc2 @ map g acc2 | |
| 1031 | else map f acc2 @ map g (acc (n-n2)) end | |
| 1032 | in if 1<=n then acc n else raise Balance end; | |
| 1033 | ||
| 1034 | ||
| 1035 | ||
| 2506 | 1036 | (** orders **) | 
| 1037 | ||
| 1038 | datatype order = LESS | EQUAL | GREATER; | |
| 1039 | ||
| 4445 | 1040 | fun rev_order LESS = GREATER | 
| 1041 | | rev_order EQUAL = EQUAL | |
| 1042 | | rev_order GREATER = LESS; | |
| 1043 | ||
| 4479 | 1044 | (*assume rel is a linear strict order*) | 
| 4445 | 1045 | fun make_ord rel (x, y) = | 
| 1046 | if rel (x, y) then LESS | |
| 1047 | else if rel (y, x) then GREATER | |
| 1048 | else EQUAL; | |
| 1049 | ||
| 4343 | 1050 | fun int_ord (i, j: int) = | 
| 2506 | 1051 | if i < j then LESS | 
| 1052 | else if i = j then EQUAL | |
| 1053 | else GREATER; | |
| 1054 | ||
| 4343 | 1055 | fun string_ord (a, b: string) = | 
| 2506 | 1056 | if a < b then LESS | 
| 1057 | else if a = b then EQUAL | |
| 1058 | else GREATER; | |
| 1059 | ||
| 4343 | 1060 | (*lexicographic product*) | 
| 1061 | fun prod_ord a_ord b_ord ((x, y), (x', y')) = | |
| 1062 | (case a_ord (x, x') of EQUAL => b_ord (y, y') | ord => ord); | |
| 1063 | ||
| 1064 | (*dictionary order -- in general NOT well-founded!*) | |
| 1065 | fun dict_ord _ ([], []) = EQUAL | |
| 1066 | | dict_ord _ ([], _ :: _) = LESS | |
| 1067 | | dict_ord _ (_ :: _, []) = GREATER | |
| 1068 | | dict_ord elem_ord (x :: xs, y :: ys) = | |
| 1069 | (case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys) | ord => ord); | |
| 1070 | ||
| 1071 | (*lexicographic product of lists*) | |
| 1072 | fun list_ord elem_ord (xs, ys) = | |
| 1073 | prod_ord int_ord (dict_ord elem_ord) ((length xs, xs), (length ys, ys)); | |
| 1074 | ||
| 2506 | 1075 | |
| 4621 | 1076 | (* sorting *) | 
| 1077 | ||
| 1078 | (*quicksort (stable, i.e. does not reorder equal elements)*) | |
| 1079 | fun sort ord = | |
| 1080 | let | |
| 1081 | fun qsort xs = | |
| 1082 | let val len = length xs in | |
| 1083 | if len <= 1 then xs | |
| 1084 | else | |
| 1085 | let val (lts, eqs, gts) = part (nth_elem (len div 2, xs)) xs in | |
| 1086 | qsort lts @ eqs @ qsort gts | |
| 1087 | end | |
| 1088 | end | |
| 1089 | and part _ [] = ([], [], []) | |
| 1090 | | part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) | |
| 1091 | and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) | |
| 1092 | | add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) | |
| 1093 | | add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts); | |
| 1094 | in qsort end; | |
| 1095 | ||
| 1096 | (*sort strings*) | |
| 1097 | val sort_strings = sort string_ord; | |
| 1098 | fun sort_wrt sel xs = sort (string_ord o pairself sel) xs; | |
| 1099 | ||
| 1100 | ||
| 2506 | 1101 | |
| 3525 | 1102 | (** input / output and diagnostics **) | 
| 233 | 1103 | |
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 1104 | val cd = OS.FileSys.chDir; | 
| 2317 | 1105 | val pwd = OS.FileSys.getDir; | 
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 1106 | |
| 5942 | 1107 | fun std_output s = | 
| 1108 | (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); | |
| 3525 | 1109 | |
| 5942 | 1110 | fun prefix_lines prfx txt = | 
| 5949 | 1111 | txt |> split_lines |> map (fn s => prfx ^ s) |> cat_lines; | 
| 3525 | 1112 | |
| 1113 | (*hooks for output channels: normal, warning, error*) | |
| 5966 
60f80b2a2777
removed prs / prs_fn (broken, because it did not include \n in its
 wenzelm parents: 
5949diff
changeset | 1114 | val writeln_fn = ref (std_output o suffix "\n"); | 
| 5949 | 1115 | val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); | 
| 1116 | val error_fn = ref (std_output o suffix "\n" o prefix_lines "*** "); | |
| 1580 
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
 berghofe parents: 
1576diff
changeset | 1117 | |
| 5966 
60f80b2a2777
removed prs / prs_fn (broken, because it did not include \n in its
 wenzelm parents: 
5949diff
changeset | 1118 | fun writeln s = ! writeln_fn s; | 
| 5942 | 1119 | fun warning s = ! warning_fn s; | 
| 233 | 1120 | |
| 1121 | (*print error message and abort to top level*) | |
| 6959 | 1122 | |
| 5949 | 1123 | fun error_msg s = ! error_fn s; | 
| 3553 | 1124 | fun error s = (error_msg s; raise ERROR); | 
| 4849 | 1125 | fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg);
 | 
| 233 | 1126 | |
| 1127 | fun assert p msg = if p then () else error msg; | |
| 1128 | fun deny p msg = if p then error msg else (); | |
| 1129 | ||
| 544 
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
 lcp parents: 
512diff
changeset | 1130 | (*Assert pred for every member of l, generating a message if pred fails*) | 
| 4212 | 1131 | fun assert_all pred l msg_fn = | 
| 544 
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
 lcp parents: 
512diff
changeset | 1132 | let fun asl [] = () | 
| 4212 | 1133 | | asl (x::xs) = if pred x then asl xs else error (msg_fn x) | 
| 1134 | in asl l end; | |
| 233 | 1135 | |
| 3624 | 1136 | |
| 4212 | 1137 | (* handle errors capturing messages *) | 
| 3699 | 1138 | |
| 1139 | datatype 'a error = | |
| 1140 | Error of string | | |
| 1141 | OK of 'a; | |
| 1142 | ||
| 4248 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1143 | fun get_error (Error msg) = Some msg | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1144 | | get_error _ = None; | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1145 | |
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1146 | fun get_ok (OK x) = Some x | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1147 | | get_ok _ = None; | 
| 
5e8a31c41d44
added get_error: 'a error -> string option, get_ok: 'a error -> 'a option;
 wenzelm parents: 
4224diff
changeset | 1148 | |
| 5037 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1149 | datatype 'a result = | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1150 | Result of 'a | | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1151 | Exn of exn; | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1152 | |
| 3699 | 1153 | fun handle_error f x = | 
| 1154 | let | |
| 4945 | 1155 | val buffer = ref ([]: string list); | 
| 1156 | fun capture s = buffer := ! buffer @ [s]; | |
| 5037 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1157 | fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else (); | 
| 3699 | 1158 | in | 
| 5037 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1159 | (case Result (setmp error_fn capture f x) handle exn => Exn exn of | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1160 | Result y => (err_msg (); OK y) | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1161 | | Exn ERROR => Error (cat_lines (! buffer)) | 
| 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1162 | | Exn exn => (err_msg (); raise exn)) | 
| 3624 | 1163 | end; | 
| 1164 | ||
| 1165 | ||
| 5037 
224887671646
handle_error: capture error msgs, even if no exception raised;
 wenzelm parents: 
4995diff
changeset | 1166 | (* transform ERROR into ERROR_MESSAGE *) | 
| 4923 | 1167 | |
| 1168 | exception ERROR_MESSAGE of string; | |
| 1169 | ||
| 1170 | fun transform_error f x = | |
| 1171 | (case handle_error f x of | |
| 1172 | OK y => y | |
| 1173 | | Error msg => raise ERROR_MESSAGE msg); | |
| 1174 | ||
| 1175 | ||
| 5904 | 1176 | (* transform any exception, including ERROR *) | 
| 1177 | ||
| 1178 | fun transform_failure exn f x = | |
| 1179 | transform_error f x handle e => raise exn e; | |
| 1180 | ||
| 1181 | ||
| 233 | 1182 | |
| 1183 | (** timing **) | |
| 1184 | ||
| 4326 | 1185 | (*a conditional timing function: applies f to () and, if the flag is true, | 
| 8999 | 1186 | prints its runtime on warning channel*) | 
| 4326 | 1187 | fun cond_timeit flag f = | 
| 1188 | if flag then | |
| 1189 | let val start = startTiming() | |
| 1190 | val result = f () | |
| 8999 | 1191 | in warning (endTiming start); result end | 
| 4326 | 1192 | else f (); | 
| 1193 | ||
| 233 | 1194 | (*unconditional timing function*) | 
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 1195 | fun timeit x = cond_timeit true x; | 
| 233 | 1196 | |
| 1197 | (*timed application function*) | |
| 1198 | fun timeap f x = timeit (fn () => f x); | |
| 1199 | ||
| 8999 | 1200 | (*global timing mode*) | 
| 1201 | val timing = ref false; | |
| 1202 | ||
| 3606 | 1203 | |
| 233 | 1204 | |
| 4621 | 1205 | (** misc **) | 
| 233 | 1206 | |
| 1207 | (*use the keyfun to make a list of (x, key) pairs*) | |
| 0 | 1208 | fun make_keylist (keyfun: 'a->'b) : 'a list -> ('a * 'b) list =
 | 
| 233 | 1209 | let fun keypair x = (x, keyfun x) | 
| 1210 | in map keypair end; | |
| 0 | 1211 | |
| 233 | 1212 | (*given a list of (x, key) pairs and a searchkey | 
| 0 | 1213 | return the list of xs from each pair whose key equals searchkey*) | 
| 1214 | fun keyfilter [] searchkey = [] | |
| 233 | 1215 | | keyfilter ((x, key) :: pairs) searchkey = | 
| 1216 | if key = searchkey then x :: keyfilter pairs searchkey | |
| 1217 | else keyfilter pairs searchkey; | |
| 0 | 1218 | |
| 1219 | ||
| 1220 | (*Partition list into elements that satisfy predicate and those that don't. | |
| 233 | 1221 | Preserves order of elements in both lists.*) | 
| 0 | 1222 | fun partition (pred: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
 | 
| 1223 | let fun part ([], answer) = answer | |
| 233 | 1224 | | part (x::xs, (ys, ns)) = if pred(x) | 
| 1225 | then part (xs, (x::ys, ns)) | |
| 1226 | else part (xs, (ys, x::ns)) | |
| 1227 | in part (rev ys, ([], [])) end; | |
| 0 | 1228 | |
| 1229 | ||
| 1230 | fun partition_eq (eq:'a * 'a -> bool) = | |
| 1231 | let fun part [] = [] | |
| 233 | 1232 | | part (x::ys) = let val (xs, xs') = partition (apl(x, eq)) ys | 
| 1233 | in (x::xs)::(part xs') end | |
| 0 | 1234 | in part end; | 
| 1235 | ||
| 1236 | ||
| 233 | 1237 | (*Partition a list into buckets [ bi, b(i+1), ..., bj ] | 
| 0 | 1238 | putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) | 
| 1239 | fun partition_list p i j = | |
| 233 | 1240 | let fun part k xs = | 
| 1241 | if k>j then | |
| 0 | 1242 | (case xs of [] => [] | 
| 1243 | | _ => raise LIST "partition_list") | |
| 1244 | else | |
| 233 | 1245 | let val (ns, rest) = partition (p k) xs; | 
| 1246 | in ns :: part(k+1)rest end | |
| 0 | 1247 | in part i end; | 
| 1248 | ||
| 1249 | ||
| 233 | 1250 | (* transitive closure (not Warshall's algorithm) *) | 
| 0 | 1251 | |
| 233 | 1252 | fun transitive_closure [] = [] | 
| 1253 | | transitive_closure ((x, ys)::ps) = | |
| 1254 | let val qs = transitive_closure ps | |
| 2182 
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2175diff
changeset | 1255 | val zs = foldl (fn (zs, y) => assocs qs y union_string zs) (ys, ys) | 
| 5904 | 1256 | fun step(u, us) = (u, if x mem_string us then zs union_string us | 
| 2243 
3ebeaaacfbd1
Eta-expanded some declarations that are illegal under value polymorphism
 paulson parents: 
2196diff
changeset | 1257 | else us) | 
| 233 | 1258 | in (x, zs) :: map step qs end; | 
| 0 | 1259 | |
| 1260 | ||
| 233 | 1261 | (* generating identifiers *) | 
| 0 | 1262 | |
| 4063 | 1263 | (** Freshly generated identifiers; supplied prefix MUST start with a letter **) | 
| 0 | 1264 | local | 
| 4063 | 1265 | (*Maps 0-63 to A-Z, a-z, 0-9 or _ or ' for generating random identifiers*) | 
| 1266 | fun char i = if i<26 then chr (ord "A" + i) | |
| 5904 | 1267 | else if i<52 then chr (ord "a" + i - 26) | 
| 1268 | else if i<62 then chr (ord"0" + i - 52) | |
| 1269 | else if i=62 then "_" | |
| 1270 | else (*i=63*) "'"; | |
| 4063 | 1271 | |
| 1272 | val charVec = Vector.tabulate (64, char); | |
| 1273 | ||
| 5904 | 1274 | fun newid n = | 
| 1275 | let | |
| 4284 | 1276 | in implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n))) end; | 
| 2003 | 1277 | |
| 4284 | 1278 | val seedr = ref 0; | 
| 0 | 1279 | |
| 4063 | 1280 | in | 
| 4284 | 1281 | |
| 4063 | 1282 | fun init_gensym() = (seedr := 0); | 
| 2003 | 1283 | |
| 4284 | 1284 | fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr)); | 
| 4063 | 1285 | end; | 
| 1286 | ||
| 1287 | ||
| 1288 | local | |
| 1289 | (*Identifies those character codes legal in identifiers. | |
| 1290 | chould use Basis Library character functions if Poly/ML provided characters*) | |
| 5904 | 1291 | fun idCode k = (ord "a" <= k andalso k < ord "z") orelse | 
| 4063 | 1292 | (ord "A" <= k andalso k < ord "Z") orelse | 
| 1293 | (ord "0" <= k andalso k < ord "9"); | |
| 1294 | ||
| 1295 | val idCodeVec = Vector.tabulate (256, idCode); | |
| 1296 | ||
| 1297 | in | |
| 2003 | 1298 | |
| 0 | 1299 | (*Increment a list of letters like a reversed base 26 number. | 
| 233 | 1300 | If head is "z", bumps chars in tail. | 
| 0 | 1301 | Digits are incremented as if they were integers. | 
| 1302 | "_" and "'" are not changed. | |
| 233 | 1303 | For making variants of identifiers.*) | 
| 0 | 1304 | |
| 5904 | 1305 | fun bump_int_list(c::cs) = | 
| 1306 | if c="9" then "0" :: bump_int_list cs | |
| 1307 | else | |
| 4063 | 1308 | if "0" <= c andalso c < "9" then chr(ord(c)+1) :: cs | 
| 233 | 1309 | else "1" :: c :: cs | 
| 0 | 1310 |   | bump_int_list([]) = error("bump_int_list: not an identifier");
 | 
| 1311 | ||
| 233 | 1312 | fun bump_list([], d) = [d] | 
| 1313 | | bump_list(["'"], d) = [d, "'"] | |
| 1314 |   | bump_list("z"::cs, _) = "a" :: bump_list(cs, "a")
 | |
| 1315 |   | bump_list("Z"::cs, _) = "A" :: bump_list(cs, "A")
 | |
| 1316 |   | bump_list("9"::cs, _) = "0" :: bump_int_list cs
 | |
| 5904 | 1317 | | bump_list(c::cs, _) = | 
| 4063 | 1318 | let val k = ord(c) | 
| 5904 | 1319 | in if Vector.sub(idCodeVec,k) then chr(k+1) :: cs | 
| 1320 | else | |
| 1321 | if c="'" orelse c="_" then c :: bump_list(cs, "") | |
| 1322 |            else error("bump_list: not legal in identifier: " ^
 | |
| 1323 | implode(rev(c::cs))) | |
| 233 | 1324 | end; | 
| 0 | 1325 | |
| 1326 | end; | |
| 1327 | ||
| 233 | 1328 | fun bump_string s : string = implode (rev (bump_list(rev(explode s), ""))); | 
| 41 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
 wenzelm parents: 
24diff
changeset | 1329 | |
| 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
 wenzelm parents: 
24diff
changeset | 1330 | |
| 233 | 1331 | (* lexical scanning *) | 
| 0 | 1332 | |
| 233 | 1333 | (*scan a list of characters into "words" composed of "letters" (recognized by | 
| 1334 | is_let) and separated by any number of non-"letters"*) | |
| 1335 | fun scanwords is_let cs = | |
| 0 | 1336 | let fun scan1 [] = [] | 
| 233 | 1337 | | scan1 cs = | 
| 1338 | let val (lets, rest) = take_prefix is_let cs | |
| 1339 | in implode lets :: scanwords is_let rest end; | |
| 1340 | in scan1 (#2 (take_prefix (not o is_let) cs)) end; | |
| 24 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
 clasohm parents: 
0diff
changeset | 1341 | |
| 4212 | 1342 | |
| 1343 | ||
| 1344 | (* Variable-branching trees: for proof terms etc. *) | |
| 1345 | datatype 'a mtree = Join of 'a * 'a mtree list; | |
| 1346 | ||
| 1347 | ||
| 1364 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 1348 | end; | 
| 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 1349 | |
| 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
 clasohm parents: 
1290diff
changeset | 1350 | open Library; |