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