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