author  wenzelm 
Tue, 27 Apr 1999 10:42:08 +0200  
changeset 6510  b5ef6d115b45 
parent 6312  d361b0a99e31 
child 6642  732af87c0650 
permissions  rwrr 
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 

44 
val can: ('a > 'b) > 'a > bool 

45 
val try: ('a > 'b) > 'a > 'b option 

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 

110 
val prefix: ''a list * ''a list > bool 

111 
val take_prefix: ('a > bool) > 'a list > 'a list * 'a list 

112 
val take_suffix: ('a > bool) > 'a list > 'a list * 'a list 

113 

114 
(*integers*) 

115 
val inc: int ref > int 

116 
val dec: int ref > int 

117 
val upto: int * int > int list 

118 
val downto: int * int > int list 

119 
val downto0: int list * int > bool 

120 
val radixpand: int * int > int list 

121 
val radixstring: int * string * int > string 

122 
val string_of_int: int > string 

123 
val string_of_indexname: string * int > string 

124 

125 
(*strings*) 

6312  126 
val nth_elem_string: int * string > string 
6282  127 
val foldl_string: ('a * string > 'a) > 'a * string > 'a 
6312  128 
val exists_string: (string > bool) > string > bool 
4621  129 
val enclose: string > string > string > string 
130 
val quote: string > string 

131 
val space_implode: string > string list > string 

132 
val commas: string list > string 

133 
val commas_quote: string list > string 

134 
val cat_lines: string list > string 

135 
val space_explode: string > string > string list 

5942  136 
val std_output: string > unit 
137 
val prefix_lines: string > string > string 

4621  138 
val split_lines: string > string list 
5285  139 
val suffix: string > string > string 
140 
val unsuffix: string > string > string 

4621  141 

142 
(*lists as sets*) 

143 
val mem: ''a * ''a list > bool 

144 
val mem_int: int * int list > bool 

145 
val mem_string: string * string list > bool 

146 
val gen_mem: ('a * 'b > bool) > 'a * 'b list > bool 

147 
val ins: ''a * ''a list > ''a list 

148 
val ins_int: int * int list > int list 

149 
val ins_string: string * string list > string list 

150 
val gen_ins: ('a * 'a > bool) > 'a * 'a list > 'a list 

151 
val union: ''a list * ''a list > ''a list 

152 
val union_int: int list * int list > int list 

153 
val union_string: string list * string list > string list 

154 
val gen_union: ('a * 'a > bool) > 'a list * 'a list > 'a list 

155 
val inter: ''a list * ''a list > ''a list 

156 
val inter_int: int list * int list > int list 

157 
val inter_string: string list * string list > string list 

158 
val subset: ''a list * ''a list > bool 

159 
val subset_int: int list * int list > bool 

160 
val subset_string: string list * string list > bool 

161 
val eq_set: ''a list * ''a list > bool 

162 
val eq_set_string: string list * string list > bool 

163 
val gen_subset: ('a * 'b > bool) > 'a list * 'b list > bool 

164 
val \ : ''a list * ''a > ''a list 

165 
val \\ : ''a list * ''a list > ''a list 

166 
val gen_rem: ('a * 'b > bool) > 'a list * 'b > 'a list 

167 
val gen_rems: ('a * 'b > bool) > 'a list * 'b list > 'a list 

168 
val gen_distinct: ('a * 'a > bool) > 'a list > 'a list 

169 
val distinct: ''a list > ''a list 

170 
val findrep: ''a list > ''a list 

171 
val gen_duplicates: ('a * 'a > bool) > 'a list > 'a list 

172 
val duplicates: ''a list > ''a list 

173 

174 
(*association lists*) 

175 
val assoc: (''a * 'b) list * ''a > 'b option 

176 
val assoc_int: (int * 'a) list * int > 'a option 

177 
val assoc_string: (string * 'a) list * string > 'a option 

178 
val assoc_string_int: ((string * int) * 'a) list * (string * int) > 'a option 

179 
val assocs: (''a * 'b list) list > ''a > 'b list 

180 
val assoc2: (''a * (''b * 'c) list) list * (''a * ''b) > 'c option 

181 
val gen_assoc: ('a * 'b > bool) > ('b * 'c) list * 'a > 'c option 

182 
val overwrite: (''a * 'b) list * (''a * 'b) > (''a * 'b) list 

183 
val gen_overwrite: ('a * 'a > bool) > ('a * 'b) list * ('a * 'b) > ('a * 'b) list 

184 

185 
(*generic tables*) 

186 
val generic_extend: ('a * 'a > bool) 

187 
> ('b > 'a list) > ('a list > 'b) > 'b > 'a list > 'b 

188 
val generic_merge: ('a * 'a > bool) > ('b > 'a list) > ('a list > 'b) > 'b > 'b > 'b 

189 
val extend_list: ''a list > ''a list > ''a list 

190 
val merge_lists: ''a list > ''a list > ''a list 

4692  191 
val merge_alists: (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
4621  192 
val merge_rev_lists: ''a list > ''a list > ''a list 
193 

194 
(*balanced trees*) 

195 
exception Balance 

196 
val fold_bal: ('a * 'a > 'a) > 'a list > 'a 

197 
val access_bal: ('a > 'a) * ('a > 'a) * 'a > int > int > 'a 

198 
val accesses_bal: ('a > 'a) * ('a > 'a) * 'a > int > 'a list 

199 

200 
(*orders*) 

201 
datatype order = EQUAL  GREATER  LESS 

202 
val rev_order: order > order 

203 
val make_ord: ('a * 'a > bool) > 'a * 'a > order 

204 
val int_ord: int * int > order 

205 
val string_ord: string * string > order 

206 
val prod_ord: ('a * 'b > order) > ('c * 'd > order) > ('a * 'c) * ('b * 'd) > order 

207 
val dict_ord: ('a * 'b > order) > 'a list * 'b list > order 

208 
val list_ord: ('a * 'b > order) > 'a list * 'b list > order 

209 
val sort: ('a * 'a > order) > 'a list > 'a list 

210 
val sort_strings: string list > string list 

211 
val sort_wrt: ('a > string) > 'a list > 'a list 

212 

213 
(*I/O and diagnostics*) 

214 
val cd: string > unit 

215 
val pwd: unit > string 

5966
60f80b2a2777
removed prs / prs_fn (broken, because it did not include \n in its
wenzelm
parents:
5949
diff
changeset

216 
val writeln_fn: (string > unit) ref 
4621  217 
val warning_fn: (string > unit) ref 
218 
val error_fn: (string > unit) ref 

219 
val writeln: string > unit 

220 
val warning: string > unit 

221 
exception ERROR 

222 
val error_msg: string > unit 

223 
val error: string > 'a 

224 
val sys_error: string > 'a 

225 
val assert: bool > string > unit 

226 
val deny: bool > string > unit 

227 
val assert_all: ('a > bool) > 'a list > ('a > string) > unit 

228 
datatype 'a error = Error of string  OK of 'a 

229 
val get_error: 'a error > string option 

230 
val get_ok: 'a error > 'a option 

231 
val handle_error: ('a > 'b) > 'a > 'b error 

4923  232 
exception ERROR_MESSAGE of string 
233 
val transform_error: ('a > 'b) > 'a > 'b 

5904  234 
val transform_failure: (exn > exn) > ('a > 'b) > 'a > 'b 
4621  235 

236 
(*timing*) 

237 
val cond_timeit: bool > (unit > 'a) > 'a 

238 
val timeit: (unit > 'a) > 'a 

239 
val timeap: ('a > 'b) > 'a > 'b 

240 

241 
(*misc*) 

242 
val make_keylist: ('a > 'b) > 'a list > ('a * 'b) list 

243 
val keyfilter: ('a * ''b) list > ''b > 'a list 

244 
val partition: ('a > bool) > 'a list > 'a list * 'a list 

245 
val partition_eq: ('a * 'a > bool) > 'a list > 'a list list 

246 
val partition_list: (int > 'a > bool) > int > int > 'a list > 'a list list 

247 
val transitive_closure: (string * string list) list > (string * string list) list 

248 
val init_gensym: unit > unit 

249 
val gensym: string > string 

250 
val bump_int_list: string list > string list 

251 
val bump_list: string list * string > string list 

252 
val bump_string: string > string 

253 
val scanwords: (string > bool) > string list > string list 

254 
datatype 'a mtree = Join of 'a * 'a mtree list 

255 
end; 

256 

257 
structure Library: LIBRARY = 

1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

258 
struct 
0  259 

4995  260 

233  261 
(** functions **) 
0  262 

233  263 
(*handy combinators*) 
264 
fun curry f x y = f (x, y); 

265 
fun uncurry f (x, y) = f x y; 

266 
fun I x = x; 

267 
fun K x y = x; 

0  268 

380  269 
(*reverse apply*) 
410  270 
fun (x > f) = f x; 
380  271 

233  272 
(*application of (infix) operator to its left or right argument*) 
273 
fun apl (x, f) y = f (x, y); 

274 
fun apr (f, y) x = f (x, y); 

0  275 

233  276 
(*function exponentiation: f(...(f x)...) with n applications of f*) 
277 
fun funpow n f x = 

278 
let fun rep (0, x) = x 

279 
 rep (n, x) = rep (n  1, f x) 

280 
in rep (n, x) end; 

160  281 

5893  282 
(*concatenation: 2 and 3 args*) 
283 
fun (f oo g) x y = f (g x y); 

284 
fun (f ooo g) x y z = f (g x y z); 

6510  285 
fun (f oooo g) x y z w = f (g x y z w); 
286 

160  287 

288 

2471  289 
(** stamps **) 
290 

291 
type stamp = unit ref; 

292 
val stamp: unit > stamp = ref; 

293 

294 

295 

233  296 
(** options **) 
0  297 

298 
datatype 'a option = None  Some of 'a; 

299 

4139  300 
exception OPTION; 
0  301 

302 
fun the (Some x) = x 

4139  303 
 the None = raise OPTION; 
0  304 

4212  305 
(*strict!*) 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

306 
fun if_none None y = y 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

307 
 if_none (Some x) _ = x; 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

308 

0  309 
fun is_some (Some _) = true 
310 
 is_some None = false; 

311 

312 
fun is_none (Some _) = false 

313 
 is_none None = true; 

314 

233  315 
fun apsome f (Some x) = Some (f x) 
316 
 apsome _ None = None; 

0  317 

4139  318 
(*handle partial functions*) 
4181  319 
fun can f x = (f x; true) handle _ => false; 
4139  320 
fun try f x = Some (f x) handle _ => None; 
321 

322 

323 

233  324 
(** pairs **) 
325 

326 
fun pair x y = (x, y); 

327 
fun rpair x y = (y, x); 

328 

329 
fun fst (x, y) = x; 

330 
fun snd (x, y) = y; 

331 

332 
fun eq_fst ((x1, _), (x2, _)) = x1 = x2; 

333 
fun eq_snd ((_, y1), (_, y2)) = y1 = y2; 

334 

335 
fun swap (x, y) = (y, x); 

336 

4212  337 
(*apply function to components*) 
233  338 
fun apfst f (x, y) = (f x, y); 
339 
fun apsnd f (x, y) = (x, f y); 

4212  340 
fun pairself f (x, y) = (f x, f y); 
233  341 

342 

343 

344 
(** booleans **) 

345 

346 
(* equality *) 

347 

348 
fun equal x y = x = y; 

349 
fun not_equal x y = x <> y; 

350 

351 

352 
(* operators for combining predicates *) 

353 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

354 
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

355 
fun (p andf q) = fn x => p x andalso q x; 
233  356 

357 

358 
(* predicates on lists *) 

359 

360 
(*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) 

361 
fun exists (pred: 'a > bool) : 'a list > bool = 

362 
let fun boolf [] = false 

363 
 boolf (x :: xs) = pred x orelse boolf xs 

364 
in boolf end; 

365 

366 
(*forall pred [x1, ..., xn] ===> pred x1 andalso ... andalso pred xn*) 

367 
fun forall (pred: 'a > bool) : 'a list > bool = 

368 
let fun boolf [] = true 

369 
 boolf (x :: xs) = pred x andalso boolf xs 

370 
in boolf end; 

0  371 

233  372 

380  373 
(* flags *) 
374 

375 
fun set flag = (flag := true; true); 

376 
fun reset flag = (flag := false; false); 

377 
fun toggle flag = (flag := not (! flag); ! flag); 

378 

4212  379 
(*temporarily set flag, handling errors*) 
2978  380 
fun setmp flag value f x = 
2958  381 
let 
382 
val orig_value = ! flag; 

383 
fun return y = (flag := orig_value; y); 

384 
in 

385 
flag := value; 

386 
return (f x handle exn => (return (); raise exn)) 

387 
end; 

388 

380  389 

233  390 

391 
(** lists **) 

392 

393 
exception LIST of string; 

394 

395 
fun null [] = true 

396 
 null (_ :: _) = false; 

397 

398 
fun hd [] = raise LIST "hd" 

399 
 hd (x :: _) = x; 

400 

401 
fun tl [] = raise LIST "tl" 

402 
 tl (_ :: xs) = xs; 

403 

404 
fun cons x xs = x :: xs; 

5285  405 
fun single x = [x]; 
233  406 

4629  407 
fun append xs ys = xs @ ys; 
408 

5904  409 
fun apply [] x = x 
410 
 apply (f :: fs) x = apply fs (f x); 

411 

233  412 

413 
(* fold *) 

414 

415 
(*the following versions of fold are designed to fit nicely with infixes*) 

0  416 

233  417 
(* (op @) (e, [x1, ..., xn]) ===> ((e @ x1) @ x2) ... @ xn 
418 
for operators that associate to the left (TAIL RECURSIVE)*) 

419 
fun foldl (f: 'a * 'b > 'a) : 'a * 'b list > 'a = 

420 
let fun itl (e, []) = e 

421 
 itl (e, a::l) = itl (f(e, a), l) 

422 
in itl end; 

423 

424 
(* (op @) ([x1, ..., xn], e) ===> x1 @ (x2 ... @ (xn @ e)) 

425 
for operators that associate to the right (not tail recursive)*) 

426 
fun foldr f (l, e) = 

427 
let fun itr [] = e 

428 
 itr (a::l) = f(a, itr l) 

429 
in itr l end; 

430 

431 
(* (op @) [x1, ..., xn] ===> x1 @ (x2 ... @ (x[n1] @ xn)) 

432 
for n > 0, operators that associate to the right (not tail recursive)*) 

433 
fun foldr1 f l = 

4181  434 
let fun itr [x] = x 
233  435 
 itr (x::l) = f(x, itr l) 
436 
in itr l end; 

437 

4956
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

438 
fun foldl_map _ (x, []) = (x, []) 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

439 
 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

440 
let 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

441 
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

442 
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

443 
in (x'', y' :: ys') end; 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

444 

233  445 

446 
(* basic list functions *) 

447 

448 
(*length of a list, should unquestionably be a standard function*) 

449 
local fun length1 (n, []) = n (*TAIL RECURSIVE*) 

450 
 length1 (n, x :: xs) = length1 (n + 1, xs) 

451 
in fun length l = length1 (0, l) end; 

452 

453 
(*take the first n elements from a list*) 

454 
fun take (n, []) = [] 

455 
 take (n, x :: xs) = 

456 
if n > 0 then x :: take (n  1, xs) else []; 

457 

458 
(*drop the first n elements from a list*) 

459 
fun drop (n, []) = [] 

460 
 drop (n, x :: xs) = 

461 
if n > 0 then drop (n  1, xs) else x :: xs; 

0  462 

4713  463 
fun dropwhile P [] = [] 
464 
 dropwhile P (ys as x::xs) = if P x then dropwhile P xs else ys; 

465 

233  466 
(*return nth element of a list, where 0 designates the first element; 
467 
raise EXCEPTION if list too short*) 

468 
fun nth_elem NL = 

469 
(case drop NL of 

470 
[] => raise LIST "nth_elem" 

471 
 x :: _ => x); 

472 

473 
(*last element of a list*) 

474 
fun last_elem [] = raise LIST "last_elem" 

475 
 last_elem [x] = x 

476 
 last_elem (_ :: xs) = last_elem xs; 

477 

3762  478 
(*rear decomposition*) 
479 
fun split_last [] = raise LIST "split_last" 

480 
 split_last [x] = ([], x) 

481 
 split_last (x :: xs) = apfst (cons x) (split_last xs); 

482 

4893  483 
(*update nth element*) 
484 
fun nth_update x (n, xs) = 

485 
let 

486 
val prfx = take (n, xs); 

487 
val sffx = drop (n, xs); 

488 
in 

489 
(case sffx of 

490 
[] => raise LIST "nth_update" 

491 
 _ :: sffx' => prfx @ (x :: sffx')) 

492 
end; 

493 

4212  494 
(*find the position of an element in a list*) 
495 
fun find_index pred = 

496 
let fun find _ [] = ~1 

497 
 find n (x :: xs) = if pred x then n else find (n + 1) xs; 

498 
in find 0 end; 

3762  499 

4224  500 
fun find_index_eq x = find_index (equal x); 
4212  501 

502 
(*find first element satisfying predicate*) 

503 
fun find_first _ [] = None 

504 
 find_first pred (x :: xs) = 

505 
if pred x then Some x else find_first pred xs; 

233  506 

4916
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

507 
(*get first element by lookup function*) 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

508 
fun get_first _ [] = None 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

509 
 get_first f (x :: xs) = 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

510 
(case f x of 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

511 
None => get_first f xs 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

512 
 some => some); 
fe8b0c82691b
get_first: ('a > 'b option) > 'a list > 'b option;
wenzelm
parents:
4893
diff
changeset

513 

233  514 
(*flatten a list of lists to a list*) 
515 
fun flat (ls: 'c list list) : 'c list = foldr (op @) (ls, []); 

516 

517 
(*like Lisp's MAPC  seq proc [x1, ..., xn] evaluates 

518 
(proc x1; ...; proc xn) for side effects*) 

519 
fun seq (proc: 'a > unit) : 'a list > unit = 

520 
let fun seqf [] = () 

521 
 seqf (x :: xs) = (proc x; seqf xs) 

522 
in seqf end; 

523 

524 
(*separate s [x1, x2, ..., xn] ===> [x1, s, x2, s, ..., s, xn]*) 

525 
fun separate s (x :: (xs as _ :: _)) = x :: s :: separate s xs 

526 
 separate _ xs = xs; 

527 

528 
(*make the list [x, x, ..., x] of length n*) 

529 
fun replicate n (x: 'a) : 'a list = 

530 
let fun rep (0, xs) = xs 

531 
 rep (n, xs) = rep (n  1, x :: xs) 

532 
in 

533 
if n < 0 then raise LIST "replicate" 

534 
else rep (n, []) 

535 
end; 

536 

4248
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

537 
(*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

538 
fun multiply ([], _) = [] 
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

539 
 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

540 

233  541 

542 
(* filter *) 

543 

544 
(*copy the list preserving elements that satisfy the predicate*) 

545 
fun filter (pred: 'a>bool) : 'a list > 'a list = 

0  546 
let fun filt [] = [] 
233  547 
 filt (x :: xs) = if pred x then x :: filt xs else filt xs 
548 
in filt end; 

0  549 

550 
fun filter_out f = filter (not o f); 

551 

233  552 
fun mapfilter (f: 'a > 'b option) ([]: 'a list) = [] : 'b list 
553 
 mapfilter f (x :: xs) = 

554 
(case f x of 

555 
None => mapfilter f xs 

556 
 Some y => y :: mapfilter f xs); 

557 

558 

559 
(* lists of pairs *) 

560 

380  561 
fun map2 _ ([], []) = [] 
562 
 map2 f (x :: xs, y :: ys) = (f (x, y) :: map2 f (xs, ys)) 

563 
 map2 _ _ = raise LIST "map2"; 

564 

565 
fun exists2 _ ([], []) = false 

566 
 exists2 pred (x :: xs, y :: ys) = pred (x, y) orelse exists2 pred (xs, ys) 

567 
 exists2 _ _ = raise LIST "exists2"; 

568 

569 
fun forall2 _ ([], []) = true 

570 
 forall2 pred (x :: xs, y :: ys) = pred (x, y) andalso forall2 pred (xs, ys) 

571 
 forall2 _ _ = raise LIST "forall2"; 

572 

4956
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

573 
fun seq2 _ ([], []) = () 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

574 
 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

575 
 seq2 _ _ = raise LIST "seq2"; 
a7538e43896e
added foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list;
wenzelm
parents:
4945
diff
changeset

576 

233  577 
(*combine two lists forming a list of pairs: 
578 
[x1, ..., xn] ~~ [y1, ..., yn] ===> [(x1, y1), ..., (xn, yn)]*) 

579 
fun [] ~~ [] = [] 

580 
 (x :: xs) ~~ (y :: ys) = (x, y) :: (xs ~~ ys) 

581 
 _ ~~ _ = raise LIST "~~"; 

582 

583 
(*inverse of ~~; the old 'split': 

584 
[(x1, y1), ..., (xn, yn)] ===> ([x1, ..., xn], [y1, ..., yn])*) 

585 
fun split_list (l: ('a * 'b) list) = (map #1 l, map #2 l); 

586 

587 

588 
(* prefixes, suffixes *) 

589 

590 
fun [] prefix _ = true 

591 
 (x :: xs) prefix (y :: ys) = x = y andalso (xs prefix ys) 

592 
 _ prefix _ = false; 

593 

594 
(* [x1, ..., xi, ..., xn] > ([x1, ..., x(i1)], [xi, ..., xn]) 

595 
where xi is the first element that does not satisfy the predicate*) 

596 
fun take_prefix (pred : 'a > bool) (xs: 'a list) : 'a list * 'a list = 

597 
let fun take (rxs, []) = (rev rxs, []) 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

598 
 take (rxs, x :: xs) = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

599 
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) 
233  600 
in take([], xs) end; 
601 

602 
(* [x1, ..., xi, ..., xn] > ([x1, ..., xi], [x(i+1), ..., xn]) 

603 
where xi is the last element that does not satisfy the predicate*) 

604 
fun take_suffix _ [] = ([], []) 

605 
 take_suffix pred (x :: xs) = 

606 
(case take_suffix pred xs of 

607 
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) 

608 
 (prfx, sffx) => (x :: prfx, sffx)); 

609 

610 

611 

612 
(** integers **) 

613 

2958  614 
fun inc i = (i := ! i + 1; ! i); 
615 
fun dec i = (i := ! i  1; ! i); 

233  616 

617 

618 
(* lists of integers *) 

619 

620 
(*make the list [from, from + 1, ..., to]*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

621 
fun (from upto to) = 
233  622 
if from > to then [] else from :: ((from + 1) upto to); 
623 

624 
(*make the list [from, from  1, ..., to]*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

625 
fun (from downto to) = 
233  626 
if from < to then [] else from :: ((from  1) downto to); 
627 

628 
(*predicate: downto0 (is, n) <=> is = [n, n  1, ..., 0]*) 

629 
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n  1) 

630 
 downto0 ([], n) = n = ~1; 

631 

632 

633 
(* convert integers to strings *) 

634 

635 
(*expand the number in the given base; 

636 
example: radixpand (2, 8) gives [1, 0, 0, 0]*) 

637 
fun radixpand (base, num) : int list = 

638 
let 

639 
fun radix (n, tail) = 

640 
if n < base then n :: tail 

641 
else radix (n div base, (n mod base) :: tail) 

642 
in radix (num, []) end; 

643 

644 
(*expands a number into a string of characters starting from "zerochar"; 

645 
example: radixstring (2, "0", 8) gives "1000"*) 

646 
fun radixstring (base, zerochar, num) = 

647 
let val offset = ord zerochar; 

648 
fun chrof n = chr (offset + n) 

649 
in implode (map chrof (radixpand (base, num))) end; 

650 

651 

3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset

652 
val string_of_int = Int.toString; 
233  653 

3407
afd288caf573
Removal of radixstring from string_of_int; addition of string_of_indexname
paulson
parents:
3393
diff
changeset

654 
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

655 
 string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; 
233  656 

657 

4212  658 

233  659 
(** strings **) 
660 

6312  661 
(*functions tuned for strings, avoiding explode*) 
662 

663 
fun nth_elem_string (i, str) = 

664 
String.substring (str, i, 1) handle _ => raise LIST "nth_elem_string"; 

665 

6282  666 
fun foldl_string f (x0, str) = 
667 
let 

668 
val n = size str; 

669 
fun fold (x, i) = if i < n then fold (f (x, String.substring (str, i, 1)), i + 1) else x 

670 
in fold (x0, 0) end; 

671 

6312  672 
fun exists_string pred str = foldl_string (fn (b, s) => b orelse pred s) (false, str); 
673 

512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

674 
(*enclose in brackets*) 
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

675 
fun enclose lpar rpar str = lpar ^ str ^ rpar; 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

676 

233  677 
(*simple quoting (does not escape special chars)*) 
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

678 
val quote = enclose "\"" "\""; 
233  679 

4212  680 
(*space_implode "..." (explode "hello") = "h...e...l...l...o"*) 
233  681 
fun space_implode a bs = implode (separate a bs); 
682 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

683 
val commas = space_implode ", "; 
380  684 
val commas_quote = commas o map quote; 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

685 

233  686 
(*concatenate messages, one per line, into a string*) 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

687 
val cat_lines = space_implode "\n"; 
233  688 

4212  689 
(*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

690 
fun space_explode _ "" = [] 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

691 
 space_explode sep str = 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

692 
let 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

693 
fun expl chs = 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

694 
(case take_prefix (not_equal sep) chs of 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

695 
(cs, []) => [implode cs] 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

696 
 (cs, _ :: cs') => implode cs :: expl cs'); 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

697 
in expl (explode str) end; 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

698 

17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

699 
val split_lines = space_explode "\n"; 
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

700 

5285  701 
(*append suffix*) 
702 
fun suffix sfx s = s ^ sfx; 

703 

704 
(*remove suffix*) 

705 
fun unsuffix sfx s = 

706 
let 

707 
val cs = explode s; 

708 
val prfx_len = size s  size sfx; 

709 
in 

710 
if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then 

711 
implode (take (prfx_len, cs)) 

712 
else raise LIST "unsuffix" 

713 
end; 

714 

3832
17a20a2af8f5
fixed space_explode, old one retained as BAD_space_explode;
wenzelm
parents:
3762
diff
changeset

715 

233  716 

717 
(** lists as sets **) 

718 

719 
(*membership in a list*) 

720 
fun x mem [] = false 

721 
 x mem (y :: ys) = x = y orelse x mem ys; 

0  722 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

723 
(*membership in a list, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

724 
fun (x:int) mem_int [] = false 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

725 
 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

726 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

727 
(*membership in a list, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

728 
fun (x:string) mem_string [] = false 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

729 
 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

730 

0  731 
(*generalized membership test*) 
233  732 
fun gen_mem eq (x, []) = false 
733 
 gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); 

734 

735 

736 
(*insertion into list if not already there*) 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

737 
fun (x ins xs) = if x mem xs then xs else x :: xs; 
0  738 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

739 
(*insertion into list, optimized version for ints*) 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

740 
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

741 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

742 
(*insertion into list, optimized version for strings*) 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

743 
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

744 

0  745 
(*generalized insertion*) 
233  746 
fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; 
747 

748 

749 
(*union of sets represented as lists: no repetitions*) 

750 
fun xs union [] = xs 

751 
 [] union ys = ys 

752 
 (x :: xs) union ys = xs union (x ins ys); 

0  753 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

754 
(*union of sets, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

755 
fun (xs:int list) union_int [] = xs 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

756 
 [] union_int ys = ys 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

757 
 (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

758 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

759 
(*union of sets, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

760 
fun (xs:string list) union_string [] = xs 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

761 
 [] union_string ys = ys 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

762 
 (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

763 

0  764 
(*generalized union*) 
233  765 
fun gen_union eq (xs, []) = xs 
766 
 gen_union eq ([], ys) = ys 

767 
 gen_union eq (x :: xs, ys) = gen_union eq (xs, gen_ins eq (x, ys)); 

768 

769 

770 
(*intersection*) 

771 
fun [] inter ys = [] 

772 
 (x :: xs) inter ys = 

773 
if x mem ys then x :: (xs inter ys) else xs inter ys; 

774 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

775 
(*intersection, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

776 
fun ([]:int list) inter_int ys = [] 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

777 
 (x :: xs) inter_int ys = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

778 
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

779 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

780 
(*intersection, optimized version for strings *) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

781 
fun ([]:string list) inter_string ys = [] 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

782 
 (x :: xs) inter_string ys = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

783 
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

784 

233  785 

786 
(*subset*) 

787 
fun [] subset ys = true 

788 
 (x :: xs) subset ys = x mem ys andalso xs subset ys; 

789 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

790 
(*subset, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

791 
fun ([]:int list) subset_int ys = true 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

792 
 (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

793 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

794 
(*subset, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

795 
fun ([]:string list) subset_string ys = true 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

796 
 (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

797 

4363  798 
(*set equality*) 
799 
fun eq_set (xs, ys) = 

800 
xs = ys orelse (xs subset ys andalso ys subset xs); 

801 

2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

802 
(*set equality for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

803 
fun eq_set_string ((xs:string list), ys) = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

804 
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

805 

2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

806 
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

807 

265  808 

233  809 
(*removing an element from a list WITHOUT duplicates*) 
810 
fun (y :: ys) \ x = if x = y then ys else y :: (ys \ x) 

811 
 [] \ x = []; 

812 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

813 
fun ys \\ xs = foldl (op \) (ys,xs); 
0  814 

233  815 
(*removing an element from a list  possibly WITH duplicates*) 
816 
fun gen_rem eq (xs, y) = filter_out (fn x => eq (x, y)) xs; 

817 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

818 
fun gen_rems eq = foldl (gen_rem eq); 
233  819 

820 

821 
(*makes a list of the distinct members of the input; preserves order, takes 

822 
first of equal elements*) 

823 
fun gen_distinct eq lst = 

824 
let 

825 
val memb = gen_mem eq; 

0  826 

233  827 
fun dist (rev_seen, []) = rev rev_seen 
828 
 dist (rev_seen, x :: xs) = 

829 
if memb (x, rev_seen) then dist (rev_seen, xs) 

830 
else dist (x :: rev_seen, xs); 

831 
in 

832 
dist ([], lst) 

833 
end; 

834 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

835 
fun distinct l = gen_distinct (op =) l; 
233  836 

837 

838 
(*returns the tail beginning with the first repeated element, or []*) 

839 
fun findrep [] = [] 

840 
 findrep (x :: xs) = if x mem xs then x :: xs else findrep xs; 

841 

842 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

843 
(*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

844 
order, takes first of equal elements*) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

845 
fun gen_duplicates eq lst = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

846 
let 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

847 
val memb = gen_mem eq; 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

848 

ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

849 
fun dups (rev_dups, []) = rev rev_dups 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

850 
 dups (rev_dups, x :: xs) = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

851 
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

852 
dups (rev_dups, xs) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

853 
else dups (x :: rev_dups, xs); 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

854 
in 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

855 
dups ([], lst) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

856 
end; 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

857 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

858 
fun duplicates l = gen_duplicates (op =) l; 
255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

859 

ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

860 

233  861 

862 
(** association lists **) 

0  863 

233  864 
(*association list lookup*) 
865 
fun assoc ([], key) = None 

866 
 assoc ((keyi, xi) :: pairs, key) = 

867 
if key = keyi then Some xi else assoc (pairs, key); 

868 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

869 
(*association list lookup, optimized version for ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

870 
fun assoc_int ([], (key:int)) = None 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

871 
 assoc_int ((keyi, xi) :: pairs, key) = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

872 
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

873 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

874 
(*association list lookup, optimized version for strings*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

875 
fun assoc_string ([], (key:string)) = None 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

876 
 assoc_string ((keyi, xi) :: pairs, key) = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

877 
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

878 

2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

879 
(*association list lookup, optimized version for string*ints*) 
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

880 
fun assoc_string_int ([], (key:string*int)) = None 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

881 
 assoc_string_int ((keyi, xi) :: pairs, key) = 
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

882 
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

883 

233  884 
fun assocs ps x = 
885 
(case assoc (ps, x) of 

886 
None => [] 

887 
 Some ys => ys); 

888 

255
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

889 
(*twofold association list lookup*) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

890 
fun assoc2 (aal, (key1, key2)) = 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

891 
(case assoc (aal, key1) of 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

892 
Some al => assoc (al, key2) 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

893 
 None => None); 
ee132db91681
added if_none, parents, commas, gen_duplicates, duplicates, assoc2;
wenzelm
parents:
245
diff
changeset

894 

233  895 
(*generalized association list lookup*) 
896 
fun gen_assoc eq ([], key) = None 

897 
 gen_assoc eq ((keyi, xi) :: pairs, key) = 

898 
if eq (key, keyi) then Some xi else gen_assoc eq (pairs, key); 

899 

900 
(*association list update*) 

901 
fun overwrite (al, p as (key, _)) = 

902 
let fun over ((q as (keyi, _)) :: pairs) = 

903 
if keyi = key then p :: pairs else q :: (over pairs) 

904 
 over [] = [p] 

905 
in over al end; 

906 

2522  907 
fun gen_overwrite eq (al, p as (key, _)) = 
908 
let fun over ((q as (keyi, _)) :: pairs) = 

909 
if eq (keyi, key) then p :: pairs else q :: (over pairs) 

910 
 over [] = [p] 

911 
in over al end; 

912 

233  913 

914 

915 
(** generic tables **) 

0  916 

233  917 
(*Tables are supposed to be 'efficient' encodings of lists of elements distinct 
918 
wrt. an equality "eq". The extend and merge operations below are optimized 

919 
for longterm space efficiency.*) 

920 

921 
(*append (new) elements to a table*) 

922 
fun generic_extend _ _ _ tab [] = tab 

923 
 generic_extend eq dest_tab mk_tab tab1 lst2 = 

924 
let 

925 
val lst1 = dest_tab tab1; 

926 
val new_lst2 = gen_rems eq (lst2, lst1); 

927 
in 

928 
if null new_lst2 then tab1 

929 
else mk_tab (lst1 @ new_lst2) 

930 
end; 

0  931 

233  932 
(*append (new) elements of 2nd table to 1st table*) 
933 
fun generic_merge eq dest_tab mk_tab tab1 tab2 = 

934 
let 

935 
val lst1 = dest_tab tab1; 

936 
val lst2 = dest_tab tab2; 

937 
val new_lst2 = gen_rems eq (lst2, lst1); 

938 
in 

939 
if null new_lst2 then tab1 

940 
else if gen_subset eq (lst1, lst2) then tab2 

941 
else mk_tab (lst1 @ new_lst2) 

942 
end; 

0  943 

233  944 

945 
(*lists as tables*) 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

946 
fun extend_list tab = generic_extend (op =) I I tab; 
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

947 
fun merge_lists tab = generic_merge (op =) I I tab; 
4692  948 
fun merge_alists tab = generic_merge eq_fst I I tab; 
233  949 

380  950 
fun merge_rev_lists xs [] = xs 
951 
 merge_rev_lists [] ys = ys 

952 
 merge_rev_lists xs (y :: ys) = 

953 
(if y mem xs then I else cons y) (merge_rev_lists xs ys); 

954 

0  955 

956 

233  957 
(** balanced trees **) 
958 

959 
exception Balance; (*indicates nonpositive argument to balancing fun*) 

960 

961 
(*balanced folding; avoids deep nesting*) 

962 
fun fold_bal f [x] = x 

963 
 fold_bal f [] = raise Balance 

964 
 fold_bal f xs = 

965 
let val k = length xs div 2 

966 
in f (fold_bal f (take(k, xs)), 

967 
fold_bal f (drop(k, xs))) 

968 
end; 

969 

970 
(*construct something of the form f(...g(...(x)...)) for balanced access*) 

971 
fun access_bal (f, g, x) n i = 

972 
let fun acc n i = (*1<=i<=n*) 

973 
if n=1 then x else 

974 
let val n2 = n div 2 

975 
in if i<=n2 then f (acc n2 i) 

976 
else g (acc (nn2) (in2)) 

977 
end 

978 
in if 1<=i andalso i<=n then acc n i else raise Balance end; 

979 

980 
(*construct ALL such accesses; could try harder to share recursive calls!*) 

981 
fun accesses_bal (f, g, x) n = 

982 
let fun acc n = 

983 
if n=1 then [x] else 

984 
let val n2 = n div 2 

985 
val acc2 = acc n2 

986 
in if nn2=n2 then map f acc2 @ map g acc2 

987 
else map f acc2 @ map g (acc (nn2)) end 

988 
in if 1<=n then acc n else raise Balance end; 

989 

990 

991 

2506  992 
(** orders **) 
993 

994 
datatype order = LESS  EQUAL  GREATER; 

995 

4445  996 
fun rev_order LESS = GREATER 
997 
 rev_order EQUAL = EQUAL 

998 
 rev_order GREATER = LESS; 

999 

4479  1000 
(*assume rel is a linear strict order*) 
4445  1001 
fun make_ord rel (x, y) = 
1002 
if rel (x, y) then LESS 

1003 
else if rel (y, x) then GREATER 

1004 
else EQUAL; 

1005 

4343  1006 
fun int_ord (i, j: int) = 
2506  1007 
if i < j then LESS 
1008 
else if i = j then EQUAL 

1009 
else GREATER; 

1010 

4343  1011 
fun string_ord (a, b: string) = 
2506  1012 
if a < b then LESS 
1013 
else if a = b then EQUAL 

1014 
else GREATER; 

1015 

4343  1016 
(*lexicographic product*) 
1017 
fun prod_ord a_ord b_ord ((x, y), (x', y')) = 

1018 
(case a_ord (x, x') of EQUAL => b_ord (y, y')  ord => ord); 

1019 

1020 
(*dictionary order  in general NOT wellfounded!*) 

1021 
fun dict_ord _ ([], []) = EQUAL 

1022 
 dict_ord _ ([], _ :: _) = LESS 

1023 
 dict_ord _ (_ :: _, []) = GREATER 

1024 
 dict_ord elem_ord (x :: xs, y :: ys) = 

1025 
(case elem_ord (x, y) of EQUAL => dict_ord elem_ord (xs, ys)  ord => ord); 

1026 

1027 
(*lexicographic product of lists*) 

1028 
fun list_ord elem_ord (xs, ys) = 

1029 
prod_ord int_ord (dict_ord elem_ord) ((length xs, xs), (length ys, ys)); 

1030 

2506  1031 

4621  1032 
(* sorting *) 
1033 

1034 
(*quicksort (stable, i.e. does not reorder equal elements)*) 

1035 
fun sort ord = 

1036 
let 

1037 
fun qsort xs = 

1038 
let val len = length xs in 

1039 
if len <= 1 then xs 

1040 
else 

1041 
let val (lts, eqs, gts) = part (nth_elem (len div 2, xs)) xs in 

1042 
qsort lts @ eqs @ qsort gts 

1043 
end 

1044 
end 

1045 
and part _ [] = ([], [], []) 

1046 
 part pivot (x :: xs) = add (ord (x, pivot)) x (part pivot xs) 

1047 
and add LESS x (lts, eqs, gts) = (x :: lts, eqs, gts) 

1048 
 add EQUAL x (lts, eqs, gts) = (lts, x :: eqs, gts) 

1049 
 add GREATER x (lts, eqs, gts) = (lts, eqs, x :: gts); 

1050 
in qsort end; 

1051 

1052 
(*sort strings*) 

1053 
val sort_strings = sort string_ord; 

1054 
fun sort_wrt sel xs = sort (string_ord o pairself sel) xs; 

1055 

1056 

2506  1057 

3525  1058 
(** input / output and diagnostics **) 
233  1059 

2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1060 
val cd = OS.FileSys.chDir; 
2317  1061 
val pwd = OS.FileSys.getDir; 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1062 

5942  1063 
fun std_output s = 
1064 
(TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); 

3525  1065 

5942  1066 
fun prefix_lines prfx txt = 
5949  1067 
txt > split_lines > map (fn s => prfx ^ s) > cat_lines; 
3525  1068 

1069 
(*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

1070 
val writeln_fn = ref (std_output o suffix "\n"); 
5949  1071 
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); 
1072 
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

1073 

5966
60f80b2a2777
removed prs / prs_fn (broken, because it did not include \n in its
wenzelm
parents:
5949
diff
changeset

1074 
fun writeln s = ! writeln_fn s; 
5942  1075 
fun warning s = ! warning_fn s; 
233  1076 

1077 
(*print error message and abort to top level*) 

1078 
exception ERROR; 

5949  1079 
fun error_msg s = ! error_fn s; 
3553  1080 
fun error s = (error_msg s; raise ERROR); 
4849  1081 
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg); 
233  1082 

1083 
fun assert p msg = if p then () else error msg; 

1084 
fun deny p msg = if p then error msg else (); 

1085 

544
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset

1086 
(*Assert pred for every member of l, generating a message if pred fails*) 
4212  1087 
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

1088 
let fun asl [] = () 
4212  1089 
 asl (x::xs) = if pred x then asl xs else error (msg_fn x) 
1090 
in asl l end; 

233  1091 

3624  1092 

4212  1093 
(* handle errors capturing messages *) 
3699  1094 

1095 
datatype 'a error = 

1096 
Error of string  

1097 
OK of 'a; 

1098 

4248
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

1099 
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

1100 
 get_error _ = None; 
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

1101 

5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

1102 
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

1103 
 get_ok _ = None; 
5e8a31c41d44
added get_error: 'a error > string option, get_ok: 'a error > 'a option;
wenzelm
parents:
4224
diff
changeset

1104 

5037
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1105 
datatype 'a result = 
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1106 
Result of 'a  
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1107 
Exn of exn; 
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1108 

3699  1109 
fun handle_error f x = 
1110 
let 

4945  1111 
val buffer = ref ([]: string list); 
1112 
fun capture s = buffer := ! buffer @ [s]; 

5037
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1113 
fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else (); 
3699  1114 
in 
5037
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1115 
(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

1116 
Result y => (err_msg (); OK y) 
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1117 
 Exn ERROR => Error (cat_lines (! buffer)) 
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1118 
 Exn exn => (err_msg (); raise exn)) 
3624  1119 
end; 
1120 

1121 

5037
224887671646
handle_error: capture error msgs, even if no exception raised;
wenzelm
parents:
4995
diff
changeset

1122 
(* transform ERROR into ERROR_MESSAGE *) 
4923  1123 

1124 
exception ERROR_MESSAGE of string; 

1125 

1126 
fun transform_error f x = 

1127 
(case handle_error f x of 

1128 
OK y => y 

1129 
 Error msg => raise ERROR_MESSAGE msg); 

1130 

1131 

5904  1132 
(* transform any exception, including ERROR *) 
1133 

1134 
fun transform_failure exn f x = 

1135 
transform_error f x handle e => raise exn e; 

1136 

1137 

233  1138 

1139 
(** timing **) 

1140 

4326  1141 
(*a conditional timing function: applies f to () and, if the flag is true, 
1142 
prints its runtime*) 

1143 
fun cond_timeit flag f = 

1144 
if flag then 

1145 
let val start = startTiming() 

1146 
val result = f () 

1147 
in 

5904  1148 
writeln (endTiming start); result 
4326  1149 
end 
1150 
else f (); 

1151 

233  1152 
(*unconditional timing function*) 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1153 
fun timeit x = cond_timeit true x; 
233  1154 

1155 
(*timed application function*) 

1156 
fun timeap f x = timeit (fn () => f x); 

1157 

3606  1158 

233  1159 

4621  1160 
(** misc **) 
233  1161 

1162 
(*use the keyfun to make a list of (x, key) pairs*) 

0  1163 
fun make_keylist (keyfun: 'a>'b) : 'a list > ('a * 'b) list = 
233  1164 
let fun keypair x = (x, keyfun x) 
1165 
in map keypair end; 

0  1166 

233  1167 
(*given a list of (x, key) pairs and a searchkey 
0  1168 
return the list of xs from each pair whose key equals searchkey*) 
1169 
fun keyfilter [] searchkey = [] 

233  1170 
 keyfilter ((x, key) :: pairs) searchkey = 
1171 
if key = searchkey then x :: keyfilter pairs searchkey 

1172 
else keyfilter pairs searchkey; 

0  1173 

1174 

1175 
(*Partition list into elements that satisfy predicate and those that don't. 

233  1176 
Preserves order of elements in both lists.*) 
0  1177 
fun partition (pred: 'a>bool) (ys: 'a list) : ('a list * 'a list) = 
1178 
let fun part ([], answer) = answer 

233  1179 
 part (x::xs, (ys, ns)) = if pred(x) 
1180 
then part (xs, (x::ys, ns)) 

1181 
else part (xs, (ys, x::ns)) 

1182 
in part (rev ys, ([], [])) end; 

0  1183 

1184 

1185 
fun partition_eq (eq:'a * 'a > bool) = 

1186 
let fun part [] = [] 

233  1187 
 part (x::ys) = let val (xs, xs') = partition (apl(x, eq)) ys 
1188 
in (x::xs)::(part xs') end 

0  1189 
in part end; 
1190 

1191 

233  1192 
(*Partition a list into buckets [ bi, b(i+1), ..., bj ] 
0  1193 
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) 
1194 
fun partition_list p i j = 

233  1195 
let fun part k xs = 
1196 
if k>j then 

0  1197 
(case xs of [] => [] 
1198 
 _ => raise LIST "partition_list") 

1199 
else 

233  1200 
let val (ns, rest) = partition (p k) xs; 
1201 
in ns :: part(k+1)rest end 

0  1202 
in part i end; 
1203 

1204 

233  1205 
(* transitive closure (not Warshall's algorithm) *) 
0  1206 

233  1207 
fun transitive_closure [] = [] 
1208 
 transitive_closure ((x, ys)::ps) = 

1209 
let val qs = transitive_closure ps 

2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

1210 
val zs = foldl (fn (zs, y) => assocs qs y union_string zs) (ys, ys) 
5904  1211 
fun step(u, us) = (u, if x mem_string us then zs union_string us 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

1212 
else us) 
233  1213 
in (x, zs) :: map step qs end; 
0  1214 

1215 

233  1216 
(* generating identifiers *) 
0  1217 

4063  1218 
(** Freshly generated identifiers; supplied prefix MUST start with a letter **) 
0  1219 
local 
4063  1220 
(*Maps 063 to AZ, az, 09 or _ or ' for generating random identifiers*) 
1221 
fun char i = if i<26 then chr (ord "A" + i) 

5904  1222 
else if i<52 then chr (ord "a" + i  26) 
1223 
else if i<62 then chr (ord"0" + i  52) 

1224 
else if i=62 then "_" 

1225 
else (*i=63*) "'"; 

4063  1226 

1227 
val charVec = Vector.tabulate (64, char); 

1228 

5904  1229 
fun newid n = 
1230 
let 

4284  1231 
in implode (map (fn i => Vector.sub(charVec,i)) (radixpand (64,n))) end; 
2003  1232 

4284  1233 
val seedr = ref 0; 
0  1234 

4063  1235 
in 
4284  1236 

4063  1237 
fun init_gensym() = (seedr := 0); 
2003  1238 

4284  1239 
fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr)); 
4063  1240 
end; 
1241 

1242 

1243 
local 

1244 
(*Identifies those character codes legal in identifiers. 

1245 
chould use Basis Library character functions if Poly/ML provided characters*) 

5904  1246 
fun idCode k = (ord "a" <= k andalso k < ord "z") orelse 
4063  1247 
(ord "A" <= k andalso k < ord "Z") orelse 
1248 
(ord "0" <= k andalso k < ord "9"); 

1249 

1250 
val idCodeVec = Vector.tabulate (256, idCode); 

1251 

1252 
in 

2003  1253 

0  1254 
(*Increment a list of letters like a reversed base 26 number. 
233  1255 
If head is "z", bumps chars in tail. 
0  1256 
Digits are incremented as if they were integers. 
1257 
"_" and "'" are not changed. 

233  1258 
For making variants of identifiers.*) 
0  1259 

5904  1260 
fun bump_int_list(c::cs) = 
1261 
if c="9" then "0" :: bump_int_list cs 

1262 
else 

4063  1263 
if "0" <= c andalso c < "9" then chr(ord(c)+1) :: cs 
233  1264 
else "1" :: c :: cs 
0  1265 
 bump_int_list([]) = error("bump_int_list: not an identifier"); 
1266 

233  1267 
fun bump_list([], d) = [d] 
1268 
 bump_list(["'"], d) = [d, "'"] 

1269 
 bump_list("z"::cs, _) = "a" :: bump_list(cs, "a") 

1270 
 bump_list("Z"::cs, _) = "A" :: bump_list(cs, "A") 

1271 
 bump_list("9"::cs, _) = "0" :: bump_int_list cs 

5904  1272 
 bump_list(c::cs, _) = 
4063  1273 
let val k = ord(c) 
5904  1274 
in if Vector.sub(idCodeVec,k) then chr(k+1) :: cs 
1275 
else 

1276 
if c="'" orelse c="_" then c :: bump_list(cs, "") 

1277 
else error("bump_list: not legal in identifier: " ^ 

1278 
implode(rev(c::cs))) 

233  1279 
end; 
0  1280 

1281 
end; 

1282 

233  1283 
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

1284 

97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

1285 

233  1286 
(* lexical scanning *) 
0  1287 

233  1288 
(*scan a list of characters into "words" composed of "letters" (recognized by 
1289 
is_let) and separated by any number of non"letters"*) 

1290 
fun scanwords is_let cs = 

0  1291 
let fun scan1 [] = [] 
233  1292 
 scan1 cs = 
1293 
let val (lets, rest) = take_prefix is_let cs 

1294 
in implode lets :: scanwords is_let rest end; 

1295 
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

1296 

4212  1297 

1298 

1299 
(* Variablebranching trees: for proof terms etc. *) 

1300 
datatype 'a mtree = Join of 'a * 'a mtree list; 

1301 

1302 

1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

1303 
end; 
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

1304 

8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

1305 
open Library; 