author  wenzelm 
Fri, 17 Jan 1997 18:20:22 +0100  
changeset 2522  a1a18530c4ac 
parent 2506  965127966331 
child 2806  772f6bba48a1 
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, 
7 
strings, lists as sets, association lists, generic tables, balanced trees, 

2506  8 
orders, input / output, timing, filenames, misc functions. 
0  9 
*) 
10 

1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1460
diff
changeset

11 
infix > ~~ \ \\ orelf ins ins_string ins_int orf andf prefix upto downto 
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

12 
mem mem_int mem_string union union_int union_string 
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

13 
inter inter_int inter_string subset subset_int subset_string subdir_of; 
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1290
diff
changeset

14 

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

15 

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

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

17 
struct 
0  18 

233  19 
(** functions **) 
0  20 

233  21 
(*handy combinators*) 
22 
fun curry f x y = f (x, y); 

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

24 
fun I x = x; 

25 
fun K x y = x; 

0  26 

380  27 
(*reverse apply*) 
410  28 
fun (x > f) = f x; 
380  29 

233  30 
(*combine two functions forming the union of their domains*) 
2175
21fde76bc742
Updated syntax; shortened comments; put in monomorphic versions of ins
paulson
parents:
2157
diff
changeset

31 
fun (f orelf g) = fn x => f x handle Match => g x; 
0  32 

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

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

0  36 

233  37 
(*functional for pairs*) 
38 
fun pairself f (x, y) = (f x, f y); 

0  39 

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

42 
let fun rep (0, x) = x 

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

44 
in rep (n, x) end; 

160  45 

46 

47 

2471  48 
(** stamps **) 
49 

50 
type stamp = unit ref; 

51 
val stamp: unit > stamp = ref; 

52 

53 

54 

233  55 
(** options **) 
0  56 

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

58 

59 
exception OPTION of string; 

60 

61 
fun the (Some x) = x 

62 
 the None = raise OPTION "the"; 

63 

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

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

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

66 

0  67 
fun is_some (Some _) = true 
68 
 is_some None = false; 

69 

70 
fun is_none (Some _) = false 

71 
 is_none None = true; 

72 

233  73 
fun apsome f (Some x) = Some (f x) 
74 
 apsome _ None = None; 

0  75 

233  76 

77 

78 
(** pairs **) 

79 

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

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

82 

83 
fun fst (x, y) = x; 

84 
fun snd (x, y) = y; 

85 

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

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

88 

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

90 

91 
(*apply the function to a component of a pair*) 

92 
fun apfst f (x, y) = (f x, y); 

93 
fun apsnd f (x, y) = (x, f y); 

94 

95 

96 

97 
(** booleans **) 

98 

99 
(* equality *) 

100 

101 
fun equal x y = x = y; 

102 
fun not_equal x y = x <> y; 

103 

104 

105 
(* operators for combining predicates *) 

106 

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

107 
fun (p orf q) = fn x => p x orelse q x; 
233  108 

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

109 
fun (p andf q) = fn x => p x andalso q x; 
233  110 

111 
fun notf p x = not (p x); 

0  112 

233  113 

114 
(* predicates on lists *) 

115 

116 
fun orl [] = false 

117 
 orl (x :: xs) = x orelse orl xs; 

118 

119 
fun andl [] = true 

120 
 andl (x :: xs) = x andalso andl xs; 

121 

2271  122 
(*Needed because several objectlogics declare the theory, therefore structure, 
123 
List.*) 

124 
structure List_ = List; 

125 

233  126 
(*exists pred [x1, ..., xn] ===> pred x1 orelse ... orelse pred xn*) 
127 
fun exists (pred: 'a > bool) : 'a list > bool = 

128 
let fun boolf [] = false 

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

130 
in boolf end; 

131 

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

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

134 
let fun boolf [] = true 

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

136 
in boolf end; 

0  137 

233  138 

380  139 
(* flags *) 
140 

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

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

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

144 

145 

233  146 

147 
(** lists **) 

148 

149 
exception LIST of string; 

150 

151 
fun null [] = true 

152 
 null (_ :: _) = false; 

153 

154 
fun hd [] = raise LIST "hd" 

155 
 hd (x :: _) = x; 

156 

157 
fun tl [] = raise LIST "tl" 

158 
 tl (_ :: xs) = xs; 

159 

160 
fun cons x xs = x :: xs; 

161 

162 

163 
(* fold *) 

164 

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

0  166 

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

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

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

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

172 
in itl end; 

173 

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

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

176 
fun foldr f (l, e) = 

177 
let fun itr [] = e 

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

179 
in itr l end; 

180 

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

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

183 
fun foldr1 f l = 

184 
let fun itr [x] = x (* FIXME [] case: elim warn (?) *) 

185 
 itr (x::l) = f(x, itr l) 

186 
in itr l end; 

187 

188 

189 
(* basic list functions *) 

190 

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

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

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

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

195 

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

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

198 
 take (n, x :: xs) = 

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

200 

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

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

203 
 drop (n, x :: xs) = 

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

0  205 

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

208 
fun nth_elem NL = 

209 
(case drop NL of 

210 
[] => raise LIST "nth_elem" 

211 
 x :: _ => x); 

212 

213 
(*last element of a list*) 

214 
fun last_elem [] = raise LIST "last_elem" 

215 
 last_elem [x] = x 

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

217 

218 
(*find the position of an element in a list*) 

219 
fun find (x, ys) = 

220 
let fun f (y :: ys, i) = if x = y then i else f (ys, i + 1) 

221 
 f (_, _) = raise LIST "find" 

222 
in f (ys, 0) end; 

223 

224 
(*flatten a list of lists to a list*) 

225 
fun flat (ls: 'c list list) : 'c list = foldr (op @) (ls, []); 

226 

227 

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

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

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

231 
let fun seqf [] = () 

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

233 
in seqf end; 

234 

235 

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

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

238 
 separate _ xs = xs; 

239 

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

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

242 
let fun rep (0, xs) = xs 

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

244 
in 

245 
if n < 0 then raise LIST "replicate" 

246 
else rep (n, []) 

247 
end; 

248 

249 

250 
(* filter *) 

251 

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

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

0  254 
let fun filt [] = [] 
233  255 
 filt (x :: xs) = if pred x then x :: filt xs else filt xs 
256 
in filt end; 

0  257 

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

259 

260 

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

263 
(case f x of 

264 
None => mapfilter f xs 

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

266 

267 

380  268 
fun find_first _ [] = None 
269 
 find_first pred (x :: xs) = 

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

271 

272 

233  273 
(* lists of pairs *) 
274 

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

277 
 map2 _ _ = raise LIST "map2"; 

278 

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

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

281 
 exists2 _ _ = raise LIST "exists2"; 

282 

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

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

285 
 forall2 _ _ = raise LIST "forall2"; 

286 

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

289 
fun [] ~~ [] = [] 

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

291 
 _ ~~ _ = raise LIST "~~"; 

292 

293 

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

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

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

297 

298 

299 
(* prefixes, suffixes *) 

300 

301 
fun [] prefix _ = true 

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

303 
 _ prefix _ = false; 

304 

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

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

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

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

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

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

310 
if pred x then take(x :: rxs, xs) else (rev rxs, x :: xs) 
233  311 
in take([], xs) end; 
312 

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

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

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

316 
 take_suffix pred (x :: xs) = 

317 
(case take_suffix pred xs of 

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

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

320 

321 

322 

323 
(** integers **) 

324 

325 
fun inc i = i := ! i + 1; 

326 
fun dec i = i := ! i  1; 

327 

328 

329 
(* lists of integers *) 

330 

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

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

332 
fun (from upto to) = 
233  333 
if from > to then [] else from :: ((from + 1) upto to); 
334 

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

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

336 
fun (from downto to) = 
233  337 
if from < to then [] else from :: ((from  1) downto to); 
338 

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

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

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

342 

343 

344 
(* convert integers to strings *) 

345 

346 
(*expand the number in the given base; 

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

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

349 
let 

350 
fun radix (n, tail) = 

351 
if n < base then n :: tail 

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

353 
in radix (num, []) end; 

354 

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

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

357 
fun radixstring (base, zerochar, num) = 

358 
let val offset = ord zerochar; 

359 
fun chrof n = chr (offset + n) 

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

361 

362 

363 
fun string_of_int n = 

364 
if n < 0 then "~" ^ radixstring (10, "0", ~n) else radixstring (10, "0", n); 

365 

366 

367 

368 
(** strings **) 

369 

370 
fun is_letter ch = 

371 
ord "A" <= ord ch andalso ord ch <= ord "Z" orelse 

372 
ord "a" <= ord ch andalso ord ch <= ord "z"; 

373 

374 
fun is_digit ch = 

375 
ord "0" <= ord ch andalso ord ch <= ord "9"; 

376 

377 
(*letter or _ or prime (')*) 

378 
fun is_quasi_letter "_" = true 

379 
 is_quasi_letter "'" = true 

380 
 is_quasi_letter ch = is_letter ch; 

381 

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

382 
(*white space: blanks, tabs, newlines, formfeeds*) 
233  383 
val is_blank : string > bool = 
512
55755ed9fab9
Pure/library/enclose, Pure/Syntax/pretty/enclose: renamed from parents
lcp
parents:
410
diff
changeset

384 
fn " " => true  "\t" => true  "\n" => true  "\^L" => true  _ => false; 
233  385 

386 
val is_letdig = is_quasi_letter orf is_digit; 

387 

2196  388 
(*printable chars*) 
389 
fun is_printable c = ord c > ord " " andalso ord c <= ord "~"; 

390 

233  391 

392 
(*lower all chars of string*) 

393 
val to_lower = 

394 
let 

395 
fun lower ch = 

396 
if ch >= "A" andalso ch <= "Z" then 

397 
chr (ord ch  ord "A" + ord "a") 

398 
else ch; 

399 
in implode o (map lower) o explode end; 

400 

401 

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

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

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

404 

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

406 
val quote = enclose "\"" "\""; 
233  407 

408 
(*space_implode "..." (explode "hello"); gives "h...e...l...l...o"*) 

409 
fun space_implode a bs = implode (separate a bs); 

410 

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

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

413 

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

415 
val cat_lines = space_implode "\n"; 
233  416 

1290  417 
(*space_explode "." "h.e..l.lo"; gives ["h", "e", "l", "lo"]*) 
418 
fun space_explode sep s = 

419 
let fun divide [] "" = [] 

420 
 divide [] part = [part] 

421 
 divide (c::s) part = 

422 
if c = sep then 

423 
(if part = "" then divide s "" else part :: divide s "") 

424 
else divide s (part ^ c) 

425 
in divide (explode s) "" end; 

233  426 

427 

428 
(** lists as sets **) 

429 

430 
(*membership in a list*) 

431 
fun x mem [] = false 

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

0  433 

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

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

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

436 
 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

437 

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

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

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

440 
 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

441 

0  442 
(*generalized membership test*) 
233  443 
fun gen_mem eq (x, []) = false 
444 
 gen_mem eq (x, y :: ys) = eq (x, y) orelse gen_mem eq (x, ys); 

445 

446 

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

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

448 
fun (x ins xs) = if x mem xs then xs else x :: xs; 
0  449 

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

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

451 
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

452 

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

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

454 
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

455 

0  456 
(*generalized insertion*) 
233  457 
fun gen_ins eq (x, xs) = if gen_mem eq (x, xs) then xs else x :: xs; 
458 

459 

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

461 
fun xs union [] = xs 

462 
 [] union ys = ys 

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

0  464 

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

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

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

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

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

469 

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

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

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

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

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

474 

0  475 
(*generalized union*) 
233  476 
fun gen_union eq (xs, []) = xs 
477 
 gen_union eq ([], ys) = ys 

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

479 

480 

481 
(*intersection*) 

482 
fun [] inter ys = [] 

483 
 (x :: xs) inter ys = 

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

485 

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

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

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

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

489 
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

490 

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

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

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

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

494 
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

495 

233  496 

497 
(*subset*) 

498 
fun [] subset ys = true 

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

500 

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

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

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

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

504 

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

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

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

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

508 

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

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

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

511 
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

512 

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

513 
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

514 

265  515 

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

518 
 [] \ x = []; 

519 

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

520 
fun ys \\ xs = foldl (op \) (ys,xs); 
0  521 

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

524 

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

525 
fun gen_rems eq = foldl (gen_rem eq); 
233  526 

527 

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

529 
first of equal elements*) 

530 
fun gen_distinct eq lst = 

531 
let 

532 
val memb = gen_mem eq; 

0  533 

233  534 
fun dist (rev_seen, []) = rev rev_seen 
535 
 dist (rev_seen, x :: xs) = 

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

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

538 
in 

539 
dist ([], lst) 

540 
end; 

541 

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

542 
fun distinct l = gen_distinct (op =) l; 
233  543 

544 

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

546 
fun findrep [] = [] 

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

548 

549 

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

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

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

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

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

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

555 

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

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

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

558 
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

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

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

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

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

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

564 

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

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

566 

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

567 

233  568 

569 
(** association lists **) 

0  570 

233  571 
(*association list lookup*) 
572 
fun assoc ([], key) = None 

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

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

575 

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

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

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

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

579 
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

580 

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

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

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

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

584 
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

585 

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

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

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

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

589 
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

590 

233  591 
fun assocs ps x = 
592 
(case assoc (ps, x) of 

593 
None => [] 

594 
 Some ys => ys); 

595 

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

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

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

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

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

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

601 

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

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

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

606 

607 
(*association list update*) 

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

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

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

611 
 over [] = [p] 

612 
in over al end; 

613 

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

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

617 
 over [] = [p] 

618 
in over al end; 

619 

233  620 

621 

622 
(** generic tables **) 

0  623 

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

626 
for longterm space efficiency.*) 

627 

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

629 
fun generic_extend _ _ _ tab [] = tab 

630 
 generic_extend eq dest_tab mk_tab tab1 lst2 = 

631 
let 

632 
val lst1 = dest_tab tab1; 

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

634 
in 

635 
if null new_lst2 then tab1 

636 
else mk_tab (lst1 @ new_lst2) 

637 
end; 

0  638 

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

641 
let 

642 
val lst1 = dest_tab tab1; 

643 
val lst2 = dest_tab tab2; 

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

645 
in 

646 
if null new_lst2 then tab1 

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

648 
else mk_tab (lst1 @ new_lst2) 

649 
end; 

0  650 

233  651 

652 
(*lists as tables*) 

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

653 
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

654 
fun merge_lists tab = generic_merge (op =) I I tab; 
233  655 

380  656 
fun merge_rev_lists xs [] = xs 
657 
 merge_rev_lists [] ys = ys 

658 
 merge_rev_lists xs (y :: ys) = 

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

660 

0  661 

662 

233  663 
(** balanced trees **) 
664 

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

666 

667 
(*balanced folding; avoids deep nesting*) 

668 
fun fold_bal f [x] = x 

669 
 fold_bal f [] = raise Balance 

670 
 fold_bal f xs = 

671 
let val k = length xs div 2 

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

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

674 
end; 

675 

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

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

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

679 
if n=1 then x else 

680 
let val n2 = n div 2 

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

682 
else g (acc (nn2) (in2)) 

683 
end 

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

685 

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

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

688 
let fun acc n = 

689 
if n=1 then [x] else 

690 
let val n2 = n div 2 

691 
val acc2 = acc n2 

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

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

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

695 

696 

697 

2506  698 
(** orders **) 
699 

700 
datatype order = LESS  EQUAL  GREATER; 

701 

702 
fun intord (i, j: int) = 

703 
if i < j then LESS 

704 
else if i = j then EQUAL 

705 
else GREATER; 

706 

707 
fun stringord (a, b: string) = 

708 
if a < b then LESS 

709 
else if a = b then EQUAL 

710 
else GREATER; 

711 

712 

713 

233  714 
(** input / output **) 
715 

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

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

718 

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

719 
val prs_fn = ref(fn s => TextIO.output (TextIO.stdOut, s)); 
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

720 

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

721 
fun prs s = !prs_fn s; 
233  722 
fun writeln s = prs (s ^ "\n"); 
723 

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

724 
(* TextIO.output to LaTeX / xdvi *) 
1628
60136fdd80c4
Added functions pr_latex and printgoal_latex which
berghofe
parents:
1592
diff
changeset

725 
fun latex s = 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

726 
execute ( "( cd /tmp ; echo \"" ^ s ^ 
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

727 
"\"  isa2latex s > $$.tex ; latex $$.tex ; xdvi $$.dvi ; rm $$.* ) > /dev/null &" ) ; 
1628
60136fdd80c4
Added functions pr_latex and printgoal_latex which
berghofe
parents:
1592
diff
changeset

728 

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

729 
(*print warning*) 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

730 
val warning_fn = ref(fn s => TextIO.output (TextIO.stdOut, s ^ "\n")); 
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

731 
fun warning s = !warning_fn ("Warning: " ^ s); 
233  732 

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

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

734 

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

735 
val error_fn = ref(fn s => TextIO.output (TextIO.stdOut, s ^ "\n")); 
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

736 

233  737 
exception ERROR; 
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

738 
fun error msg = (!error_fn msg; raise ERROR); 
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset

739 
fun sys_error msg = (!error_fn "*** SYSTEM ERROR ***"; error msg); 
233  740 

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

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

743 

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

744 
(*Assert pred for every member of l, generating a message if pred fails*) 
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset

745 
fun assert_all pred l msg_fn = 
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset

746 
let fun asl [] = () 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

747 
 asl (x::xs) = if pred x then asl xs 
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

748 
else error (msg_fn x) 
544
c53386a5bcf1
Pure/library/assert_all: new, moved from ZF/ind_syntax.ML
lcp
parents:
512
diff
changeset

749 
in asl l end; 
233  750 

751 
(*for the "test" target in Makefiles  signifies successful termination*) 

752 
fun maketest msg = 

1592  753 
(writeln msg; 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

754 
let val os = TextIO.openOut "test" 
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

755 
in TextIO.output (os, "Test examples ran successfully\n"); 
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

756 
TextIO.closeOut os 
1592  757 
end); 
233  758 

759 

760 
(*print a list surrounded by the brackets lpar and rpar, with comma separator 

761 
print nothing for empty list*) 

762 
fun print_list (lpar, rpar, pre: 'a > unit) (l : 'a list) = 

763 
let fun prec x = (prs ","; pre x) 

764 
in 

765 
(case l of 

766 
[] => () 

767 
 x::l => (prs lpar; pre x; seq prec l; prs rpar)) 

768 
end; 

769 

770 
(*print a list of items separated by newlines*) 

771 
fun print_list_ln (pre: 'a > unit) : 'a list > unit = 

772 
seq (fn x => (pre x; writeln "")); 

773 

774 

775 
val print_int = prs o string_of_int; 

776 

777 

778 

779 
(** timing **) 

780 

781 
(*unconditional timing function*) 

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

782 
fun timeit x = cond_timeit true x; 
233  783 

784 
(*timed application function*) 

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

786 

787 
(*timed "use" function, printing filenames*) 

788 
fun time_use fname = timeit (fn () => 

789 
(writeln ("\n**** Starting " ^ fname ^ " ****"); use fname; 

790 
writeln ("\n**** Finished " ^ fname ^ " ****"))); 

791 

955
aa0c5f9daf5b
Declares the function exit_use to behave like use but fail if
lcp
parents:
544
diff
changeset

792 
(*For Makefiles: use the file, but exit with error code if errors found.*) 
aa0c5f9daf5b
Declares the function exit_use to behave like use but fail if
lcp
parents:
544
diff
changeset

793 
fun exit_use fname = use fname handle _ => exit 1; 
233  794 

795 

1407  796 
(** filenames and paths **) 
233  797 

1290  798 
(*Convert UNIX filename of the form "path/file" to "path/" and "file"; 
233  799 
if filename contains no slash, then it returns "" and "file"*) 
800 
val split_filename = 

801 
(pairself implode) o take_suffix (not_equal "/") o explode; 

802 

803 
val base_name = #2 o split_filename; 

804 

1290  805 
(*Merge splitted filename (path and file); 
233  806 
if path does not end with one a slash is appended*) 
807 
fun tack_on "" name = name 

808 
 tack_on path name = 

809 
if last_elem (explode path) = "/" then path ^ name 

810 
else path ^ "/" ^ name; 

811 

1290  812 
(*Remove the extension of a filename, i.e. the part after the last '.'*) 
233  813 
val remove_ext = implode o #1 o take_suffix (not_equal ".") o explode; 
814 

1290  815 
(*Make relative path to reach an absolute location from a different one*) 
816 
fun relative_path cur_path dest_path = 

817 
let (*Remove common beginning of both paths and make relative path*) 

818 
fun mk_relative [] [] = [] 

819 
 mk_relative [] ds = ds 

820 
 mk_relative cs [] = map (fn _ => "..") cs 

821 
 mk_relative (c::cs) (d::ds) = 

822 
if c = d then mk_relative cs ds 

823 
else ".." :: map (fn _ => "..") cs @ (d::ds); 

824 
in if cur_path = "" orelse hd (explode cur_path) <> "/" orelse 

825 
dest_path = "" orelse hd (explode dest_path) <> "/" then 

826 
error "Relative or empty path passed to relative_path" 

827 
else (); 

828 
space_implode "/" (mk_relative (space_explode "/" cur_path) 

829 
(space_explode "/" dest_path)) 

830 
end; 

233  831 

1407  832 
(*Determine if absolute path1 is a subdirectory of absolute path2*) 
833 
fun path1 subdir_of path2 = 

834 
if hd (explode path1) <> "/" orelse hd (explode path2) <> "/" then 

835 
error "Relative or empty path passed to subdir_of" 

836 
else (space_explode "/" path2) prefix (space_explode "/" path1); 

837 

1456  838 
fun absolute_path cwd file = 
839 
let fun rm_points [] result = rev result 

840 
 rm_points (".."::ds) result = rm_points ds (tl result) 

841 
 rm_points ("."::ds) result = rm_points ds result 

842 
 rm_points (d::ds) result = rm_points ds (d::result); 

843 
in if file = "" then "" 

844 
else if hd (explode file) = "/" then file 

845 
else "/" ^ space_implode "/" 

846 
(rm_points (space_explode "/" (tack_on cwd file)) []) 

847 
end; 

848 

233  849 

850 
(** misc functions **) 

851 

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

0  853 
fun make_keylist (keyfun: 'a>'b) : 'a list > ('a * 'b) list = 
233  854 
let fun keypair x = (x, keyfun x) 
855 
in map keypair end; 

0  856 

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

233  860 
 keyfilter ((x, key) :: pairs) searchkey = 
861 
if key = searchkey then x :: keyfilter pairs searchkey 

862 
else keyfilter pairs searchkey; 

0  863 

864 

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

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

233  869 
 part (x::xs, (ys, ns)) = if pred(x) 
870 
then part (xs, (x::ys, ns)) 

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

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

0  873 

874 

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

876 
let fun part [] = [] 

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

0  879 
in part end; 
880 

881 

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

233  885 
let fun part k xs = 
886 
if k>j then 

0  887 
(case xs of [] => [] 
888 
 _ => raise LIST "partition_list") 

889 
else 

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

0  892 
in part i end; 
893 

894 

233  895 
(* sorting *) 
896 

897 
(*insertion sort; stable (does not reorder equal elements) 

898 
'less' is lessthan test on type 'a*) 

899 
fun sort (less: 'a*'a > bool) = 

0  900 
let fun insert (x, []) = [x] 
233  901 
 insert (x, y::ys) = 
902 
if less(y, x) then y :: insert (x, ys) else x::y::ys; 

0  903 
fun sort1 [] = [] 
904 
 sort1 (x::xs) = insert (x, sort1 xs) 

905 
in sort1 end; 

906 

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

907 
(*sort strings*) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

908 
val sort_strings = sort (op <= : string * string > bool); 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

909 

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

910 

233  911 
(* transitive closure (not Warshall's algorithm) *) 
0  912 

233  913 
fun transitive_closure [] = [] 
914 
 transitive_closure ((x, ys)::ps) = 

915 
let val qs = transitive_closure ps 

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

916 
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

917 
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

918 
else us) 
233  919 
in (x, zs) :: map step qs end; 
0  920 

921 

2303
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

922 
(** Simple random number generator; not guaranteed to be good, because modulus 
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

923 
has been reduced from 2^311 to 2^291 to prevent integer overflows 
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

924 
**) 
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

925 
local val a = 16807.0 and m = 536870911.0 (* 2^29  1 *) 
2003  926 
in fun nextrandom seed = 
927 
let val t = a*seed 

928 
in t  m * real(floor(t/m)) end 

929 
end; 

930 

233  931 
(* generating identifiers *) 
0  932 

933 
local 

233  934 
val a = ord "a" and z = ord "z" and A = ord "A" and Z = ord "Z" 
935 
and k0 = ord "0" and k9 = ord "9" 

2003  936 

937 
val seedr = ref 10000.0; 

0  938 
in 
939 

2003  940 
(*Maps 063 to AZ, az, 09 or _ or ' for generating random identifiers*) 
941 
fun newid n = 

942 
let fun char i = 

943 
if i<26 then chr (A+i) 

944 
else if i<52 then chr (a+i26) 

945 
else if i<62 then chr (k0+i52) 

946 
else if i=62 then "_" 

947 
else (*i=63*) "'" 

948 
in implode (map char (radixpand (64,n))) end; 

949 

2303
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

950 
(*Randomly generated identifiers with given prefix; MUST start with a letter*) 
2003  951 
fun gensym pre = pre ^ 
2303
84ed9e0d7c50
Random number generated "downgraded" to generate numbers below 2^29  1,
paulson
parents:
2271
diff
changeset

952 
(#1(newid (floor (!seedr)), 
2243
3ebeaaacfbd1
Etaexpanded some declarations that are illegal under value polymorphism
paulson
parents:
2196
diff
changeset

953 
seedr := nextrandom (!seedr))) 
2003  954 

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

233  959 
For making variants of identifiers.*) 
0  960 

961 
fun bump_int_list(c::cs) = if c="9" then "0" :: bump_int_list cs else 

233  962 
if k0 <= ord(c) andalso ord(c) < k9 then chr(ord(c)+1) :: cs 
963 
else "1" :: c :: cs 

0  964 
 bump_int_list([]) = error("bump_int_list: not an identifier"); 
965 

233  966 
fun bump_list([], d) = [d] 
967 
 bump_list(["'"], d) = [d, "'"] 

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

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

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

971 
 bump_list(c::cs, _) = let val k = ord(c) 

972 
in if (a <= k andalso k < z) orelse (A <= k andalso k < Z) orelse 

973 
(k0 <= k andalso k < k9) then chr(k+1) :: cs else 

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

975 
error("bump_list: not legal in identifier: " ^ 

976 
implode(rev(c::cs))) 

977 
end; 

0  978 

979 
end; 

980 

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

982 

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

983 

233  984 
(* lexical scanning *) 
0  985 

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

988 
fun scanwords is_let cs = 

0  989 
let fun scan1 [] = [] 
233  990 
 scan1 cs = 
991 
let val (lets, rest) = take_prefix is_let cs 

992 
in implode lets :: scanwords is_let rest end; 

993 
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

994 

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

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

996 

1592  997 
(*Variablebranching trees: for proof terms*) 
998 
datatype 'a mtree = Join of 'a * 'a mtree list; 

999 

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

1000 
open Library; 