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