author  wenzelm 
Mon, 16 Nov 1998 11:32:28 +0100  
changeset 5893  c755dfd02509 
parent 5285  2d1425492fb3 
child 5904  e077a0e66563 
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 

5893  15 
infix 3 oo ooo; 
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 

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

30 

4621  31 
(*stamps*) 
32 
type stamp 

33 
val stamp: unit > stamp 

34 

35 
(*options*) 

36 
datatype 'a option = None  Some of 'a 

37 
exception OPTION 

38 
val the: 'a option > 'a 

39 
val if_none: 'a option > 'a > 'a 

40 
val is_some: 'a option > bool 

41 
val is_none: 'a option > bool 

42 
val apsome: ('a > 'b) > 'a option > 'b option 

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

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

45 

46 
(*pairs*) 

47 
val pair: 'a > 'b > 'a * 'b 

48 
val rpair: 'a > 'b > 'b * 'a 

49 
val fst: 'a * 'b > 'a 

50 
val snd: 'a * 'b > 'b 

51 
val eq_fst: (''a * 'b) * (''a * 'c) > bool 

52 
val eq_snd: ('a * ''b) * ('c * ''b) > bool 

53 
val swap: 'a * 'b > 'b * 'a 

54 
val apfst: ('a > 'b) > 'a * 'c > 'b * 'c 

55 
val apsnd: ('a > 'b) > 'c * 'a > 'c * 'b 

56 
val pairself: ('a > 'b) > 'a * 'a > 'b * 'b 

57 

58 
(*booleans*) 

59 
val equal: ''a > ''a > bool 

60 
val not_equal: ''a > ''a > bool 

61 
val orf: ('a > bool) * ('a > bool) > 'a > bool 

62 
val andf: ('a > bool) * ('a > bool) > 'a > bool 

63 
val exists: ('a > bool) > 'a list > bool 

64 
val forall: ('a > bool) > 'a list > bool 

65 
val set: bool ref > bool 

66 
val reset: bool ref > bool 

67 
val toggle: bool ref > bool 

68 
val setmp: 'a ref > 'a > ('b > 'c) > 'b > 'c 

69 

70 
(*lists*) 

71 
exception LIST of string 

72 
val null: 'a list > bool 

73 
val hd: 'a list > 'a 

74 
val tl: 'a list > 'a list 

75 
val cons: 'a > 'a list > 'a list 

5285  76 
val single: 'a > 'a list 
4629  77 
val append: 'a list > 'a list > 'a list 
4621  78 
val foldl: ('a * 'b > 'a) > 'a * 'b list > 'a 
79 
val foldr: ('a * 'b > 'b) > 'a list * 'b > 'b 

80 
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

81 
val foldl_map: ('a * 'b > 'a * 'c) > 'a * 'b list > 'a * 'c list 
4621  82 
val length: 'a list > int 
83 
val take: int * 'a list > 'a list 

84 
val drop: int * 'a list > 'a list 

4713  85 
val dropwhile: ('a > bool) > 'a list > 'a list 
4621  86 
val nth_elem: int * 'a list > 'a 
87 
val last_elem: 'a list > 'a 

88 
val split_last: 'a list > 'a list * 'a 

4893  89 
val nth_update: 'a > int * 'a list > 'a list 
4621  90 
val find_index: ('a > bool) > 'a list > int 
91 
val find_index_eq: ''a > ''a list > int 

92 
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

93 
val get_first: ('a > 'b option) > 'a list > 'b option 
4621  94 
val flat: 'a list list > 'a list 
95 
val seq: ('a > unit) > 'a list > unit 

96 
val separate: 'a > 'a list > 'a list 

97 
val replicate: int > 'a > 'a list 

98 
val multiply: 'a list * 'a list list > 'a list list 

99 
val filter: ('a > bool) > 'a list > 'a list 

100 
val filter_out: ('a > bool) > 'a list > 'a list 

101 
val mapfilter: ('a > 'b option) > 'a list > 'b list 

102 
val map2: ('a * 'b > 'c) > 'a list * 'b list > 'c list 

103 
val exists2: ('a * 'b > bool) > 'a list * 'b list > bool 

104 
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

105 
val seq2: ('a * 'b > unit) > 'a list * 'b list > unit 
4621  106 
val ~~ : 'a list * 'b list > ('a * 'b) list 
107 
val split_list: ('a * 'b) list > 'a list * 'b list 

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

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

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

111 

112 
(*integers*) 

113 
val inc: int ref > int 

114 
val dec: int ref > int 

115 
val upto: int * int > int list 

116 
val downto: int * int > int list 

117 
val downto0: int list * int > bool 

118 
val radixpand: int * int > int list 

119 
val radixstring: int * string * int > string 

120 
val string_of_int: int > string 

121 
val string_of_indexname: string * int > string 

122 

123 
(*strings*) 

124 
val enclose: string > string > string > string 

125 
val quote: string > string 

126 
val space_implode: string > string list > string 

127 
val commas: string list > string 

128 
val commas_quote: string list > string 

129 
val cat_lines: string list > string 

130 
val space_explode: string > string > string list 

131 
val split_lines: string > string list 

5285  132 
val suffix: string > string > string 
133 
val unsuffix: string > string > string 

4621  134 

135 
(*lists as sets*) 

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

137 
val mem_int: int * int list > bool 

138 
val mem_string: string * string list > bool 

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

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

141 
val ins_int: int * int list > int list 

142 
val ins_string: string * string list > string list 

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

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

145 
val union_int: int list * int list > int list 

146 
val union_string: string list * string list > string list 

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

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

149 
val inter_int: int list * int list > int list 

150 
val inter_string: string list * string list > string list 

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

152 
val subset_int: int list * int list > bool 

153 
val subset_string: string list * string list > bool 

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

155 
val eq_set_string: string list * string list > bool 

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

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

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

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

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

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

162 
val distinct: ''a list > ''a list 

163 
val findrep: ''a list > ''a list 

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

165 
val duplicates: ''a list > ''a list 

166 

167 
(*association lists*) 

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

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

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

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

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

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

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

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

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

177 

178 
(*generic tables*) 

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

180 
> ('b > 'a list) > ('a list > 'b) > 'b > 'a list > 'b 

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

182 
val extend_list: ''a list > ''a list > ''a list 

183 
val merge_lists: ''a list > ''a list > ''a list 

4692  184 
val merge_alists: (''a * 'b) list > (''a * 'b) list > (''a * 'b) list 
4621  185 
val merge_rev_lists: ''a list > ''a list > ''a list 
186 

187 
(*balanced trees*) 

188 
exception Balance 

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

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

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

192 

193 
(*orders*) 

194 
datatype order = EQUAL  GREATER  LESS 

195 
val rev_order: order > order 

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

197 
val int_ord: int * int > order 

198 
val string_ord: string * string > order 

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

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

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

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

203 
val sort_strings: string list > string list 

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

205 

206 
(*I/O and diagnostics*) 

207 
val cd: string > unit 

208 
val pwd: unit > string 

209 
val prs_fn: (string > unit) ref 

210 
val warning_fn: (string > unit) ref 

211 
val error_fn: (string > unit) ref 

212 
val prs: string > unit 

213 
val writeln: string > unit 

214 
val warning: string > unit 

215 
exception ERROR 

216 
val error_msg: string > unit 

217 
val error: string > 'a 

218 
val sys_error: string > 'a 

219 
val assert: bool > string > unit 

220 
val deny: bool > string > unit 

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

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

223 
val get_error: 'a error > string option 

224 
val get_ok: 'a error > 'a option 

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

4923  226 
exception ERROR_MESSAGE of string 
227 
val transform_error: ('a > 'b) > 'a > 'b 

4621  228 

229 
(*timing*) 

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

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

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

233 

234 
(*misc*) 

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

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

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

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

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

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

241 
val init_gensym: unit > unit 

242 
val gensym: string > string 

243 
val bump_int_list: string list > string list 

244 
val bump_list: string list * string > string list 

245 
val bump_string: string > string 

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

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

248 
end; 

249 

250 
structure Library: LIBRARY = 

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

251 
struct 
0  252 

4995  253 

233  254 
(** functions **) 
0  255 

233  256 
(*handy combinators*) 
257 
fun curry f x y = f (x, y); 

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

259 
fun I x = x; 

260 
fun K x y = x; 

0  261 

380  262 
(*reverse apply*) 
410  263 
fun (x > f) = f x; 
380  264 

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

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

0  268 

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

271 
let fun rep (0, x) = x 

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

273 
in rep (n, x) end; 

160  274 

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

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

160  278 

279 

2471  280 
(** stamps **) 
281 

282 
type stamp = unit ref; 

283 
val stamp: unit > stamp = ref; 

284 

285 

286 

233  287 
(** options **) 
0  288 

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

290 

4139  291 
exception OPTION; 
0  292 

293 
fun the (Some x) = x 

4139  294 
 the None = raise OPTION; 
0  295 

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

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

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

299 

0  300 
fun is_some (Some _) = true 
301 
 is_some None = false; 

302 

303 
fun is_none (Some _) = false 

304 
 is_none None = true; 

305 

233  306 
fun apsome f (Some x) = Some (f x) 
307 
 apsome _ None = None; 

0  308 

4139  309 
(*handle partial functions*) 
4181  310 
fun can f x = (f x; true) handle _ => false; 
4139  311 
fun try f x = Some (f x) handle _ => None; 
312 

313 

314 

233  315 
(** pairs **) 
316 

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

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

319 

320 
fun fst (x, y) = x; 

321 
fun snd (x, y) = y; 

322 

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

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

325 

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

327 

4212  328 
(*apply function to components*) 
233  329 
fun apfst f (x, y) = (f x, y); 
330 
fun apsnd f (x, y) = (x, f y); 

4212  331 
fun pairself f (x, y) = (f x, f y); 
233  332 

333 

334 

335 
(** booleans **) 

336 

337 
(* equality *) 

338 

339 
fun equal x y = x = y; 

340 
fun not_equal x y = x <> y; 

341 

342 

343 
(* operators for combining predicates *) 

344 

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

345 
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

346 
fun (p andf q) = fn x => p x andalso q x; 
233  347 

348 

349 
(* predicates on lists *) 

350 

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

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

353 
let fun boolf [] = false 

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

355 
in boolf end; 

356 

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

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

359 
let fun boolf [] = true 

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

361 
in boolf end; 

0  362 

233  363 

380  364 
(* flags *) 
365 

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

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

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

369 

4212  370 
(*temporarily set flag, handling errors*) 
2978  371 
fun setmp flag value f x = 
2958  372 
let 
373 
val orig_value = ! flag; 

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

375 
in 

376 
flag := value; 

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

378 
end; 

379 

380  380 

233  381 

382 
(** lists **) 

383 

384 
exception LIST of string; 

385 

386 
fun null [] = true 

387 
 null (_ :: _) = false; 

388 

389 
fun hd [] = raise LIST "hd" 

390 
 hd (x :: _) = x; 

391 

392 
fun tl [] = raise LIST "tl" 

393 
 tl (_ :: xs) = xs; 

394 

395 
fun cons x xs = x :: xs; 

5285  396 
fun single x = [x]; 
233  397 

4629  398 
fun append xs ys = xs @ ys; 
399 

233  400 

401 
(* fold *) 

402 

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

0  404 

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

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

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

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

410 
in itl end; 

411 

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

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

414 
fun foldr f (l, e) = 

415 
let fun itr [] = e 

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

417 
in itr l end; 

418 

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

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

421 
fun foldr1 f l = 

4181  422 
let fun itr [x] = x 
233  423 
 itr (x::l) = f(x, itr l) 
424 
in itr l end; 

425 

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

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

427 
 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

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

429 
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

430 
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

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

432 

233  433 

434 
(* basic list functions *) 

435 

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

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

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

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

440 

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

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

443 
 take (n, x :: xs) = 

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

445 

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

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

448 
 drop (n, x :: xs) = 

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

0  450 

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

453 

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

456 
fun nth_elem NL = 

457 
(case drop NL of 

458 
[] => raise LIST "nth_elem" 

459 
 x :: _ => x); 

460 

461 
(*last element of a list*) 

462 
fun last_elem [] = raise LIST "last_elem" 

463 
 last_elem [x] = x 

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

465 

3762  466 
(*rear decomposition*) 
467 
fun split_last [] = raise LIST "split_last" 

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

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

470 

4893  471 
(*update nth element*) 
472 
fun nth_update x (n, xs) = 

473 
let 

474 
val prfx = take (n, xs); 

475 
val sffx = drop (n, xs); 

476 
in 

477 
(case sffx of 

478 
[] => raise LIST "nth_update" 

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

480 
end; 

481 

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

484 
let fun find _ [] = ~1 

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

486 
in find 0 end; 

3762  487 

4224  488 
fun find_index_eq x = find_index (equal x); 
4212  489 

490 
(*find first element satisfying predicate*) 

491 
fun find_first _ [] = None 

492 
 find_first pred (x :: xs) = 

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

233  494 

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

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

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

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

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

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

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

501 

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

504 

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

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

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

508 
let fun seqf [] = () 

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

510 
in seqf end; 

511 

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

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

514 
 separate _ xs = xs; 

515 

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

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

518 
let fun rep (0, xs) = xs 

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

520 
in 

521 
if n < 0 then raise LIST "replicate" 

522 
else rep (n, []) 

523 
end; 

524 

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

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

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

527 
 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

528 

233  529 

530 
(* filter *) 

531 

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

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

0  534 
let fun filt [] = [] 
233  535 
 filt (x :: xs) = if pred x then x :: filt xs else filt xs 
536 
in filt end; 

0  537 

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

539 

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

542 
(case f x of 

543 
None => mapfilter f xs 

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

545 

546 

547 
(* lists of pairs *) 

548 

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

551 
 map2 _ _ = raise LIST "map2"; 

552 

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

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

555 
 exists2 _ _ = raise LIST "exists2"; 

556 

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

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

559 
 forall2 _ _ = raise LIST "forall2"; 

560 

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

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

562 
 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

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

564 

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

567 
fun [] ~~ [] = [] 

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

569 
 _ ~~ _ = raise LIST "~~"; 

570 

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

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

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

574 

575 

576 
(* prefixes, suffixes *) 

577 

578 
fun [] prefix _ = true 

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

580 
 _ prefix _ = false; 

581 

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

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

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

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

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

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

587 
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) 
233  588 
in take([], xs) end; 
589 

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

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

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

593 
 take_suffix pred (x :: xs) = 

594 
(case take_suffix pred xs of 

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

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

597 

598 

599 

600 
(** integers **) 

601 

2958  602 
fun inc i = (i := ! i + 1; ! i); 
603 
fun dec i = (i := ! i  1; ! i); 

233  604 

605 

606 
(* lists of integers *) 

607 

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

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

609 
fun (from upto to) = 
233  610 
if from > to then [] else from :: ((from + 1) upto to); 
611 

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

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

613 
fun (from downto to) = 
233  614 
if from < to then [] else from :: ((from  1) downto to); 
615 

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

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

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

619 

620 

621 
(* convert integers to strings *) 

622 

623 
(*expand the number in the given base; 

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

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

626 
let 

627 
fun radix (n, tail) = 

628 
if n < base then n :: tail 

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

630 
in radix (num, []) end; 

631 

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

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

634 
fun radixstring (base, zerochar, num) = 

635 
let val offset = ord zerochar; 

636 
fun chrof n = chr (offset + n) 

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

638 

639 

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

640 
val string_of_int = Int.toString; 
233  641 

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

642 
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

643 
 string_of_indexname (a,i) = a ^ "_" ^ Int.toString i; 
233  644 

645 

4212  646 

233  647 
(** strings **) 
648 

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

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

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

651 

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

653 
val quote = enclose "\"" "\""; 
233  654 

4212  655 
(*space_implode "..." (explode "hello") = "h...e...l...l...o"*) 
233  656 
fun space_implode a bs = implode (separate a bs); 
657 

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

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

660 

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

662 
val cat_lines = space_implode "\n"; 
233  663 

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

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

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

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

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

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

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

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

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

673 

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

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

675 

5285  676 
(*append suffix*) 
677 
fun suffix sfx s = s ^ sfx; 

678 

679 
(*remove suffix*) 

680 
fun unsuffix sfx s = 

681 
let 

682 
val cs = explode s; 

683 
val prfx_len = size s  size sfx; 

684 
in 

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

686 
implode (take (prfx_len, cs)) 

687 
else raise LIST "unsuffix" 

688 
end; 

689 

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

690 

233  691 

692 
(** lists as sets **) 

693 

694 
(*membership in a list*) 

695 
fun x mem [] = false 

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

0  697 

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

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

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

700 
 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

701 

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

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

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

704 
 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

705 

0  706 
(*generalized membership test*) 
233  707 
fun gen_mem eq (x, []) = false 
708 
 gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); 

709 

710 

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

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

712 
fun (x ins xs) = if x mem xs then xs else x :: xs; 
0  713 

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

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

715 
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

716 

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

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

718 
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

719 

0  720 
(*generalized insertion*) 
233  721 
fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; 
722 

723 

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

725 
fun xs union [] = xs 

726 
 [] union ys = ys 

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

0  728 

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

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

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

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

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

733 

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

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

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

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

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

738 

0  739 
(*generalized union*) 
233  740 
fun gen_union eq (xs, []) = xs 
741 
 gen_union eq ([], ys) = ys 

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

743 

744 

745 
(*intersection*) 

746 
fun [] inter ys = [] 

747 
 (x :: xs) inter ys = 

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

749 

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

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

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

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

753 
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

754 

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

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

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

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

758 
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

759 

233  760 

761 
(*subset*) 

762 
fun [] subset ys = true 

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

764 

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

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

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

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

768 

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

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

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

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

772 

4363  773 
(*set equality*) 
774 
fun eq_set (xs, ys) = 

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

776 

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

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

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

779 
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

780 

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

781 
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

782 

265  783 

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

786 
 [] \ x = []; 

787 

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

788 
fun ys \\ xs = foldl (op \) (ys,xs); 
0  789 

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

792 

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

793 
fun gen_rems eq = foldl (gen_rem eq); 
233  794 

795 

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

797 
first of equal elements*) 

798 
fun gen_distinct eq lst = 

799 
let 

800 
val memb = gen_mem eq; 

0  801 

233  802 
fun dist (rev_seen, []) = rev rev_seen 
803 
 dist (rev_seen, x :: xs) = 

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

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

806 
in 

807 
dist ([], lst) 

808 
end; 

809 

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

810 
fun distinct l = gen_distinct (op =) l; 
233  811 

812 

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

814 
fun findrep [] = [] 

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

816 

817 

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

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

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

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

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

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

823 

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

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

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

826 
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

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

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

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

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

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

832 

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

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

834 

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

835 

233  836 

837 
(** association lists **) 

0  838 

233  839 
(*association list lookup*) 
840 
fun assoc ([], key) = None 

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

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

843 

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

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

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

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

847 
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

848 

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

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

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

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

852 
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

853 

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

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

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

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

857 
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

858 

233  859 
fun assocs ps x = 
860 
(case assoc (ps, x) of 

861 
None => [] 

862 
 Some ys => ys); 

863 

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

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

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

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

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

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

869 

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

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

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

874 

875 
(*association list update*) 

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

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

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

879 
 over [] = [p] 

880 
in over al end; 

881 

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

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

885 
 over [] = [p] 

886 
in over al end; 

887 

233  888 

889 

890 
(** generic tables **) 

0  891 

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

894 
for longterm space efficiency.*) 

895 

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

897 
fun generic_extend _ _ _ tab [] = tab 

898 
 generic_extend eq dest_tab mk_tab tab1 lst2 = 

899 
let 

900 
val lst1 = dest_tab tab1; 

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

902 
in 

903 
if null new_lst2 then tab1 

904 
else mk_tab (lst1 @ new_lst2) 

905 
end; 

0  906 

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

909 
let 

910 
val lst1 = dest_tab tab1; 

911 
val lst2 = dest_tab tab2; 

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

913 
in 

914 
if null new_lst2 then tab1 

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

916 
else mk_tab (lst1 @ new_lst2) 

917 
end; 

0  918 

233  919 

920 
(*lists as tables*) 

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

921 
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

922 
fun merge_lists tab = generic_merge (op =) I I tab; 
4692  923 
fun merge_alists tab = generic_merge eq_fst I I tab; 
233  924 

380  925 
fun merge_rev_lists xs [] = xs 
926 
 merge_rev_lists [] ys = ys 

927 
 merge_rev_lists xs (y :: ys) = 

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

929 

0  930 

931 

233  932 
(** balanced trees **) 
933 

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

935 

936 
(*balanced folding; avoids deep nesting*) 

937 
fun fold_bal f [x] = x 

938 
 fold_bal f [] = raise Balance 

939 
 fold_bal f xs = 

940 
let val k = length xs div 2 

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

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

943 
end; 

944 

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

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

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

948 
if n=1 then x else 

949 
let val n2 = n div 2 

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

951 
else g (acc (nn2) (in2)) 

952 
end 

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

954 

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

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

957 
let fun acc n = 

958 
if n=1 then [x] else 

959 
let val n2 = n div 2 

960 
val acc2 = acc n2 

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

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

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

964 

965 

966 

2506  967 
(** orders **) 
968 

969 
datatype order = LESS  EQUAL  GREATER; 

970 

4445  971 
fun rev_order LESS = GREATER 
972 
 rev_order EQUAL = EQUAL 

973 
 rev_order GREATER = LESS; 

974 

4479  975 
(*assume rel is a linear strict order*) 
4445  976 
fun make_ord rel (x, y) = 
977 
if rel (x, y) then LESS 

978 
else if rel (y, x) then GREATER 

979 
else EQUAL; 

980 

4343  981 
fun int_ord (i, j: int) = 
2506  982 
if i < j then LESS 
983 
else if i = j then EQUAL 

984 
else GREATER; 

985 

4343  986 
fun string_ord (a, b: string) = 
2506  987 
if a < b then LESS 
988 
else if a = b then EQUAL 

989 
else GREATER; 

990 

4343  991 
(*lexicographic product*) 
992 
fun prod_ord a_ord b_ord ((x, y), (x', y')) = 

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

994 

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

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

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

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

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

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

1001 

1002 
(*lexicographic product of lists*) 

1003 
fun list_ord elem_ord (xs, ys) = 

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

1005 

2506  1006 

4621  1007 
(* sorting *) 
1008 

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

1010 
fun sort ord = 

1011 
let 

1012 
fun qsort xs = 

1013 
let val len = length xs in 

1014 
if len <= 1 then xs 

1015 
else 

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

1017 
qsort lts @ eqs @ qsort gts 

1018 
end 

1019 
end 

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

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

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

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

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

1025 
in qsort end; 

1026 

1027 
(*sort strings*) 

1028 
val sort_strings = sort string_ord; 

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

1030 

1031 

2506  1032 

3525  1033 
(** input / output and diagnostics **) 
233  1034 

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

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

1037 

3525  1038 

1039 
local 

1040 
fun out s = 

1041 
(TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); 

1042 

1043 
fun prefix_lines prfx txt = 

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

1044 
txt > split_lines > map (fn s => prfx ^ s ^ "\n") > implode; 
3525  1045 
in 
1046 

1047 
(*hooks for output channels: normal, warning, error*) 

1048 
val prs_fn = ref (fn s => out s); 

1049 
val warning_fn = ref (fn s => out (prefix_lines "### " s)); 

1050 
val error_fn = ref (fn s => out (prefix_lines "*** " s)); 

1051 

1052 
end; 

1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

1053 

e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

1054 
fun prs s = !prs_fn s; 
233  1055 
fun writeln s = prs (s ^ "\n"); 
1056 

3525  1057 
fun warning s = !warning_fn s; 
233  1058 

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

1060 
exception ERROR; 

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

1061 
fun error_msg s = !error_fn s; 
3553  1062 
fun error s = (error_msg s; raise ERROR); 
4849  1063 
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg); 
233  1064 

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

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

1067 

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

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

1070 
let fun asl [] = () 
4212  1071 
 asl (x::xs) = if pred x then asl xs else error (msg_fn x) 
1072 
in asl l end; 

233  1073 

3624  1074 

4212  1075 
(* handle errors capturing messages *) 
3699  1076 

1077 
datatype 'a error = 

1078 
Error of string  

1079 
OK of 'a; 

1080 

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

1081 
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

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

1083 

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

1084 
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

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

1086 

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

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

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

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

1090 

3699  1091 
fun handle_error f x = 
1092 
let 

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

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

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

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

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

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

1100 
 Exn exn => (err_msg (); raise exn)) 
3624  1101 
end; 
1102 

1103 

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

1104 
(* transform ERROR into ERROR_MESSAGE *) 
4923  1105 

1106 
exception ERROR_MESSAGE of string; 

1107 

1108 
fun transform_error f x = 

1109 
(case handle_error f x of 

1110 
OK y => y 

1111 
 Error msg => raise ERROR_MESSAGE msg); 

1112 

1113 

233  1114 

1115 
(** timing **) 

1116 

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

1119 
fun cond_timeit flag f = 

1120 
if flag then 

1121 
let val start = startTiming() 

1122 
val result = f () 

1123 
in 

1124 
writeln (endTiming start); result 

1125 
end 

1126 
else f (); 

1127 

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

1129 
fun timeit x = cond_timeit true x; 
233  1130 

1131 
(*timed application function*) 

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

1133 

3606  1134 

233  1135 

4621  1136 
(** misc **) 
233  1137 

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

0  1139 
fun make_keylist (keyfun: 'a>'b) : 'a list > ('a * 'b) list = 
233  1140 
let fun keypair x = (x, keyfun x) 
1141 
in map keypair end; 

0  1142 

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

233  1146 
 keyfilter ((x, key) :: pairs) searchkey = 
1147 
if key = searchkey then x :: keyfilter pairs searchkey 

1148 
else keyfilter pairs searchkey; 

0  1149 

1150 

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

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

233  1155 
 part (x::xs, (ys, ns)) = if pred(x) 
1156 
then part (xs, (x::ys, ns)) 

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

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

0  1159 

1160 

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

1162 
let fun part [] = [] 

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

0  1165 
in part end; 
1166 

1167 

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

233  1171 
let fun part k xs = 
1172 
if k>j then 

0  1173 
(case xs of [] => [] 
1174 
 _ => raise LIST "partition_list") 

1175 
else 

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

0  1178 
in part i end; 
1179 

1180 

233  1181 
(* transitive closure (not Warshall's algorithm) *) 
0  1182 

233  1183 
fun transitive_closure [] = [] 
1184 
 transitive_closure ((x, ys)::ps) = 

1185 
let val qs = transitive_closure ps 

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

1186 
val zs = foldl (fn (zs, y) => assocs qs y union_string zs) (ys, ys) 
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2175
diff
changeset

1187 
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

1188 
else us) 
233  1189 
in (x, zs) :: map step qs end; 
0  1190 

1191 

233  1192 
(* generating identifiers *) 
0  1193 

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

1198 
else if i<52 then chr (ord "a" + i  26) 

1199 
else if i<62 then chr (ord"0" + i  52) 

1200 
else if i=62 then "_" 

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

1202 

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

1204 

1205 
fun newid n = 

1206 
let 

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

4284  1209 
val seedr = ref 0; 
0  1210 

4063  1211 
in 
4284  1212 

4063  1213 
fun init_gensym() = (seedr := 0); 
2003  1214 

4284  1215 
fun gensym pre = pre ^ (#1(newid (!seedr), inc seedr)); 
4063  1216 
end; 
1217 

1218 

1219 
local 

1220 
(*Identifies those character codes legal in identifiers. 

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

1222 
fun idCode k = (ord "a" <= k andalso k < ord "z") orelse 

1223 
(ord "A" <= k andalso k < ord "Z") orelse 

1224 
(ord "0" <= k andalso k < ord "9"); 

1225 

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

1227 

1228 
in 

2003  1229 

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

233  1234 
For making variants of identifiers.*) 
0  1235 

4063  1236 
fun bump_int_list(c::cs) = 
1237 
if c="9" then "0" :: bump_int_list cs 

1238 
else 

1239 
if "0" <= c andalso c < "9" then chr(ord(c)+1) :: cs 

233  1240 
else "1" :: c :: cs 
0  1241 
 bump_int_list([]) = error("bump_int_list: not an identifier"); 
1242 

233  1243 
fun bump_list([], d) = [d] 
1244 
 bump_list(["'"], d) = [d, "'"] 

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

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

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

4063  1248 
 bump_list(c::cs, _) = 
1249 
let val k = ord(c) 

1250 
in if Vector.sub(idCodeVec,k) then chr(k+1) :: cs 

1251 
else 

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

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

1254 
implode(rev(c::cs))) 

233  1255 
end; 
0  1256 

1257 
end; 

1258 

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

1260 

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

1261 

233  1262 
(* lexical scanning *) 
0  1263 

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

1266 
fun scanwords is_let cs = 

0  1267 
let fun scan1 [] = [] 
233  1268 
 scan1 cs = 
1269 
let val (lets, rest) = take_prefix is_let cs 

1270 
in implode lets :: scanwords is_let rest end; 

1271 
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

1272 

4212  1273 

1274 

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

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

1277 

1278 

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

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

1280 

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

1281 
open Library; 