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