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