author  wenzelm 
Mon, 29 Nov 1993 11:08:17 +0100  
changeset 160  80ccb6c354ba 
parent 41  97aae241094b 
child 172  3224c46737ef 
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$ 
3 
Author: Lawrence C Paulson, Cambridge University Computer Laboratory 

4 
Copyright 1992 University of Cambridge 

5 

6 
Basic library: booleans, lists, pairs, input/output, etc. 

7 
*) 

8 

9 

10 
(**** Booleans: operators for combining predicates ****) 

11 

12 
infix orf; 

13 
fun p orf q = fn x => p x orelse q x ; 

14 

15 
infix andf; 

16 
fun p andf q = fn x => p x andalso q x ; 

17 

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

19 

20 
fun orl [] = false 

21 
 orl (x::l) = x orelse orl l; 

22 

23 
fun andl [] = true 

24 
 andl (x::l) = x andalso andl l; 

25 

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

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

28 
let fun boolf [] = false 

29 
 boolf (x::l) = (pred x) orelse boolf l 

30 
in boolf end; 

31 

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

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

34 
let fun boolf [] = true 

35 
 boolf (x::l) = (pred x) andalso (boolf l) 

36 
in boolf end; 

37 

38 

160  39 
(** curried equality **) 
40 

41 
fun equal x y = (x = y); 

42 

43 
fun not_equal x y = x <> y; 

44 

45 

46 

0  47 
(*** Lists ***) 
48 

49 
exception LIST of string; 

50 

51 
(*discriminator and selectors for lists. *) 

52 
fun null [] = true 

53 
 null (_::_) = false; 

54 

55 
fun hd [] = raise LIST "hd" 

56 
 hd (a::_) = a; 

57 

58 
fun tl [] = raise LIST "tl" 

59 
 tl (_::l) = l; 

60 

61 

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

62 
(*curried cons and reverse cons*) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

63 

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

64 
fun cons x xs = x :: xs; 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

65 

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

66 
fun rcons xs x = x :: xs; 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

67 

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

68 

0  69 
(*curried functions for pairing and reversed pairing*) 
70 
fun pair x y = (x,y); 

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

72 

73 
fun fst(x,y) = x and snd(x,y) = y; 

74 

75 
(*Handy combinators*) 

76 
fun curry f x y = f(x,y); 

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

78 
fun I x = x and K x y = x; 

79 

80 
(*Combine two functions forming the union of their domains*) 

81 
infix orelf; 

82 
fun f orelf g = fn x => f x handle Match=> g x; 

83 

84 

85 
(*Application of (infix) operator to its left or right argument*) 

86 
fun apl (x,f) y = f(x,y); 

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

88 

89 

90 
(*functional for pairs*) 

91 
fun pairself f (x,y) = (f x, f y); 

92 

93 
(*Apply the function to a component of a pair*) 

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

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

96 

97 
fun square (n: int) = n*n; 

98 

99 
fun fact 0 = 1 

100 
 fact n = n * fact(n1); 

101 

102 

103 
(*The following versions of fold are designed to fit nicely with infixes.*) 

104 

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

106 
for operators that associate to the left. TAIL RECURSIVE*) 

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

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

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

110 
in itl end; 

111 

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

113 
for operators that associate to the right. Not tail recursive.*) 

114 
fun foldr f (l,e) = 

115 
let fun itr [] = e 

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

117 
in itr l end; 

118 

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

120 
for n>0, operators that associate to the right. Not tail recursive.*) 

121 
fun foldr1 f l = 

122 
let fun itr [x] = x 

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

124 
in itr l end; 

125 

126 

127 
(*Length of a list. Should unquestionably be a standard function*) 

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

129 
 length1 (n, x::l) = length1 (n+1, l) 

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

131 

132 

133 
(*Take the first n elements from l.*) 

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

135 
 take (n, x::xs) = if n>0 then x::take(n1,xs) 

136 
else []; 

137 

138 
(*Drop the first n elements from l.*) 

139 
fun drop (_, []) = [] 

140 
 drop (n, x::xs) = if n>0 then drop (n1, xs) 

141 
else x::xs; 

142 

143 
(*Return nth element of l, where 0 designates the first element; 

144 
raise EXCEPTION if list too short.*) 

145 
fun nth_elem NL = case (drop NL) of 

146 
[] => raise LIST "nth_elem" 

147 
 x::l => x; 

148 

149 

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

150 
(*Last element of a list*) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

151 
fun last_elem [] = raise LIST "last_elem" 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

152 
 last_elem [x] = x 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

153 
 last_elem (_ :: xs) = last_elem xs; 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

154 

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

155 

0  156 
(*make the list [from, from+1, ..., to]*) 
157 
infix upto; 

158 
fun from upto to = 

159 
if from>to then [] else from :: ((from+1) upto to); 

160 

161 
(*make the list [from, from1, ..., to]*) 

162 
infix downto; 

163 
fun from downto to = 

164 
if from<to then [] else from :: ((from1) downto to); 

165 

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

167 
fun downto0(i::is,n) = i=n andalso downto0(is,n1) 

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

169 

170 
(*Like Lisp's MAPC  seq proc [x1,...,xn] evaluates 

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

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

173 
let fun seqf [] = () 

174 
 seqf (x::l) = (proc x; seqf l) 

175 
in seqf end; 

176 

177 

178 
(*** Balanced folding; access to balanced trees ***) 

179 

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

181 

182 
(*Balanced folding; avoids deep nesting*) 

183 
fun fold_bal f [x] = x 

184 
 fold_bal f [] = raise Balance 

185 
 fold_bal f xs = 

186 
let val k = length xs div 2 

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

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

189 
end; 

190 

191 
(*Construct something of the form f(...g(...(x)...)) for balanced access*) 

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

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

194 
if n=1 then x else 

195 
let val n2 = n div 2 

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

197 
else g (acc (nn2) (in2)) 

198 
end 

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

200 

201 
(*Construct ALL such accesses; could try harder to share recursive calls!*) 

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

203 
let fun acc n = 

204 
if n=1 then [x] else 

205 
let val n2 = n div 2 

206 
val acc2 = acc n2 

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

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

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

210 

211 

212 
(*** Input/Output ***) 

213 

214 
fun prs s = output(std_out,s); 

215 
fun writeln s = prs (s ^ "\n"); 

216 

217 
(*Print error message and abort to top level*) 

218 
exception ERROR; 

219 
fun error (msg) = (writeln msg; raise ERROR); 

220 

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

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

223 

224 
(*For the "test" target in Makefiles  signifies successful termination*) 

225 
fun maketest msg = 

226 
(writeln msg; 

227 
output(open_out "test", "Test examples ran successfully\n")); 

228 

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

230 
print nothing for empty list*) 

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

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

233 
in case l of 

234 
[] => () 

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

236 
end; 

237 

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

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

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

241 

242 
fun is_letter ch = 

243 
(ord"A" <= ord ch) andalso (ord ch <= ord"Z") orelse 

244 
(ord"a" <= ord ch) andalso (ord ch <= ord"z"); 

245 

246 
fun is_digit ch = 

247 
(ord"0" <= ord ch) andalso (ord ch <= ord"9"); 

248 

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

250 
fun is_quasi_letter "_" = true 

251 
 is_quasi_letter "'" = true 

252 
 is_quasi_letter ch = is_letter ch; 

253 

254 
(*white space: blanks, tabs, newlines*) 

255 
val is_blank : string > bool = fn 

256 
" " => true  "\t" => true  "\n" => true  _ => false; 

257 

258 
val is_letdig = is_quasi_letter orf is_digit; 

259 

260 
val to_lower = 

261 
let 

262 
fun lower ch = 

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

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

265 
else ch; 

266 
in 

267 
implode o (map lower) o explode 

268 
end; 

269 

270 

271 
(*** Timing ***) 

272 

273 
(*Unconditional timing function*) 

274 
val timeit = cond_timeit true; 

275 

276 
(*Timed application function*) 

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

278 

279 
(*Timed "use" function, printing filenames*) 

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

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

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

283 

284 

285 
(*** Misc functions ***) 

286 

287 
(*Function exponentiation: f(...(f x)...) with n applications of f *) 

288 
fun funpow n f x = 

289 
let fun rep (0,x) = x 

290 
 rep (n,x) = rep (n1, f x) 

291 
in rep (n,x) end; 

292 

293 
(*Combine two lists forming a list of pairs: 

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

295 
infix ~~; 

296 
fun [] ~~ [] = [] 

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

298 
 _ ~~ _ = raise LIST "~~"; 

299 

300 
(*Inverse of ~~; the old 'split'. 

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

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

303 

304 
(*make the list [x; x; ...; x] of length n*) 

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

306 
let fun rep (0,xs) = xs 

307 
 rep (n,xs) = rep(n1, x::xs) 

308 
in if n<0 then raise LIST "replicate" 

309 
else rep (n,[]) 

310 
end; 

311 

312 
(*Flatten a list of lists to a list.*) 

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

314 

315 

316 
(*** polymorphic set operations ***) 

317 

318 
(*membership in a list*) 

319 
infix mem; 

320 
fun x mem [] = false 

321 
 x mem (y::l) = (x=y) orelse (x mem l); 

322 

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

324 
infix ins; 

325 
fun x ins xs = if x mem xs then xs else x::xs; 

326 

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

328 
infix union; 

329 
fun xs union [] = xs 

330 
 [] union ys = ys 

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

332 

333 
infix inter; 

334 
fun [] inter ys = [] 

335 
 (x::xs) inter ys = if x mem ys then x::(xs inter ys) 

336 
else xs inter ys; 

337 

338 
infix subset; 

339 
fun [] subset ys = true 

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

341 

342 
(*removing an element from a list WITHOUT duplicates*) 

343 
infix \; 

344 
fun (y::ys) \ x = if x=y then ys else y::(ys \ x) 

345 
 [] \ x = []; 

346 

347 
infix \\; 

348 
val op \\ = foldl (op \); 

349 

350 
(*** option stuff ***) 

351 

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

353 

354 
exception OPTION of string; 

355 

356 
fun the (Some x) = x 

357 
 the None = raise OPTION "the"; 

358 

359 
fun is_some (Some _) = true 

360 
 is_some None = false; 

361 

362 
fun is_none (Some _) = false 

363 
 is_none None = true; 

364 

365 

366 
(*** Association lists ***) 

367 

368 
(*Association list lookup*) 

369 
fun assoc ([], key) = None 

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

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

372 

373 
fun assocs ps x = case assoc(ps,x) of None => []  Some(ys) => ys; 

374 

375 
(*Association list update*) 

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

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

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

379 
 over[] = [p] 

380 
in over al end; 

381 

382 
(*Copy the list preserving elements that satisfy the predicate*) 

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

384 
let fun filt [] = [] 

385 
 filt (x::xs) = if pred(x) then x :: filt xs else filt xs 

386 
in filt end; 

387 

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

389 

390 

391 
(*** List operations, generalized to an arbitrary equality function "eq" 

392 
 so what good are equality types?? ***) 

393 

394 
(*removing an element from a list  possibly WITH duplicates*) 

395 
fun gen_rem eq (xs,y) = filter_out (fn x => eq(x,y)) xs; 

396 

397 
(*generalized membership test*) 

398 
fun gen_mem eq (x, []) = false 

399 
 gen_mem eq (x, y::ys) = eq(x,y) orelse gen_mem eq (x,ys); 

400 

401 
(*generalized insertion*) 

402 
fun gen_ins eq (x,xs) = if gen_mem eq (x,xs) then xs else x::xs; 

403 

404 
(*generalized union*) 

405 
fun gen_union eq (xs,[]) = xs 

406 
 gen_union eq ([],ys) = ys 

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

408 

409 
(*Generalized association list lookup*) 

410 
fun gen_assoc eq ([], key) = None 

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

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

413 

414 
(** Finding list elements and duplicates **) 

415 

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

417 
fun find(x,ys) = 

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

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

420 
in f(ys,0) end; 

421 

422 
(*Returns the tail beginning with the first repeated element, or []. *) 

423 
fun findrep [] = [] 

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

425 

426 
fun distinct1 (seen, []) = rev seen 

427 
 distinct1 (seen, x::xs) = 

428 
if x mem seen then distinct1 (seen, xs) 

429 
else distinct1 (x::seen, xs); 

430 

431 
(*Makes a list of the distinct members of the input*) 

432 
fun distinct xs = distinct1([],xs); 

433 

434 

435 
(*Use the keyfun to make a list of (x,key) pairs.*) 

436 
fun make_keylist (keyfun: 'a>'b) : 'a list > ('a * 'b) list = 

437 
let fun keypair x = (x, keyfun x) 

438 
in map keypair end; 

439 

440 
(*Given a list of (x,key) pairs and a searchkey 

441 
return the list of xs from each pair whose key equals searchkey*) 

442 
fun keyfilter [] searchkey = [] 

443 
 keyfilter ((x,key)::pairs) searchkey = 

444 
if key=searchkey then x :: keyfilter pairs searchkey 

445 
else keyfilter pairs searchkey; 

446 

447 
fun mapfilter (f: 'a > 'b option) ([]: 'a list) = [] : 'b list 

448 
 mapfilter f (x::xs) = 

449 
case (f x) of 

450 
None => mapfilter f xs 

451 
 Some y => y :: mapfilter f xs; 

452 

453 

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

455 
Preserves order of elements in both lists. *) 

456 
fun partition (pred: 'a>bool) (ys: 'a list) : ('a list * 'a list) = 

457 
let fun part ([], answer) = answer 

458 
 part (x::xs, (ys, ns)) = if pred(x) 

459 
then part (xs, (x::ys, ns)) 

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

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

462 

463 

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

465 
let fun part [] = [] 

466 
 part (x::ys) = let val (xs,xs') = partition (apl(x,eq)) ys 

467 
in (x::xs)::(part xs') end 

468 
in part end; 

469 

470 

471 
(*Partition a list into buckets [ bi, b(i+1),...,bj ] 

472 
putting x in bk if p(k)(x) holds. Preserve order of elements if possible.*) 

473 
fun partition_list p i j = 

474 
let fun part k xs = 

475 
if k>j then 

476 
(case xs of [] => [] 

477 
 _ => raise LIST "partition_list") 

478 
else 

479 
let val (ns,rest) = partition (p k) xs; 

480 
in ns :: part(k+1)rest end 

481 
in part i end; 

482 

483 

484 
(*Insertion sort. Stable (does not reorder equal elements) 

485 
'less' is lessthan test on type 'a. *) 

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

487 
let fun insert (x, []) = [x] 

488 
 insert (x, y::ys) = 

489 
if less(y,x) then y :: insert (x,ys) else x::y::ys; 

490 
fun sort1 [] = [] 

491 
 sort1 (x::xs) = insert (x, sort1 xs) 

492 
in sort1 end; 

493 

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

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

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

496 

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

497 

0  498 
(*Transitive Closure. Not Warshall's algorithm*) 
499 
fun transitive_closure [] = [] 

500 
 transitive_closure ((x,ys)::ps) = 

501 
let val qs = transitive_closure ps 

502 
val zs = foldl (fn (zs,y) => assocs qs y union zs) (ys,ys) 

503 
fun step(u,us) = (u, if x mem us then zs union us else us) 

504 
in (x,zs) :: map step qs end; 

505 

506 
(*** Converting integers to strings, generating identifiers, etc. ***) 

507 

508 
(*Expand the number in the given base 

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

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

511 
let fun radix (n,tail) = 

512 
if n<base then n :: tail 

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

514 
in radix (num,[]) end; 

515 

516 
(*Expands a number into a string of characters starting from "zerochar" 

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

518 
fun radixstring (base,zerochar,num) = 

519 
let val offset = ord(zerochar); 

520 
fun chrof n = chr(offset+n) 

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

522 

523 
fun string_of_int n = 

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

525 

526 
val print_int = prs o string_of_int; 

527 

528 
local 

529 
val a = ord("a") and z = ord("z") and A = ord("A") and Z = ord("Z") 

530 
and k0 = ord("0") and k9 = ord("9") 

531 
in 

532 

533 
(*Increment a list of letters like a reversed base 26 number. 

534 
If head is "z", bumps chars in tail. 

535 
Digits are incremented as if they were integers. 

536 
"_" and "'" are not changed. 

537 
For making variants of identifiers. *) 

538 

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

540 
if k0 <= ord(c) andalso ord(c) < k9 then chr(ord(c)+1) :: cs 

541 
else "1" :: c :: cs 

542 
 bump_int_list([]) = error("bump_int_list: not an identifier"); 

543 

544 
fun bump_list([],d) = [d] 

545 
 bump_list(["'"],d) = [d,"'"] 

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

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

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

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

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

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

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

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

554 
implode(rev(c::cs))) 

555 
end; 

556 

557 
end; 

558 

559 
fun bump_string s : string = implode (rev (bump_list(rev(explode s),""))); 

560 

561 

562 
(*** Operations on integer lists ***) 

563 

564 
fun sum [] = 0 

565 
 sum (n::ns) = n + sum ns; 

566 

567 
fun max[m : int] = m 

568 
 max(m::n::ns) = if m>n then max(m::ns) else max(n::ns) 

569 
 max [] = raise LIST "max"; 

570 

571 
fun min[m : int] = m 

572 
 min(m::n::ns) = if m<n then min(m::ns) else min(n::ns) 

573 
 min [] = raise LIST "min"; 

574 

575 

576 
(*** Lexical scanning ***) 

577 

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

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

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

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

582 
 take (rxs, x::xs) = 

583 
if pred x then take(x::rxs, xs) else (rev rxs, x::xs) 

584 
in take([],xs) end; 

585 

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

586 
(* [x1,...,xi,...,xn] > ([x1,...,xi], [x(i+1),..., xn]) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

587 
where xi is the last element that does not satisfy the predicate*) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

588 
fun take_suffix _ [] = ([], []) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

589 
 take_suffix pred (x :: xs) = 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

590 
(case take_suffix pred xs of 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

591 
([], sffx) => if pred x then ([], x :: sffx) else ([x], sffx) 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

592 
 (prfx, sffx) => (x :: prfx, sffx)); 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

593 

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

594 

0  595 
infix prefix; 
596 
fun [] prefix _ = true 

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

598 
 _ prefix _ = false; 

599 

600 
(* [x1, x2, ..., xn] > [x1, s, x2, s, ..., s, xn] *) 

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

602 
 separate _ xs = xs; 

603 

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

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

606 

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

607 
(*simple quoting (does not escape special chars) *) 
0  608 
fun quote s = "\"" ^ s ^ "\""; 
609 

610 
(*Concatenate messages, one per line, into a string*) 

611 
val cat_lines = implode o (map (apr(op^,"\n"))); 

612 

613 
(*Scan a list of characters into "words" composed of "letters" (recognized 

614 
by is_let) and separated by any number of non"letters".*) 

615 
fun scanwords is_let cs = 

616 
let fun scan1 [] = [] 

617 
 scan1 cs = 

618 
let val (lets, rest) = take_prefix is_let cs 

619 
in implode lets :: scanwords is_let rest end; 

620 
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

621 

f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

622 

f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

623 
(*** Operations on filenames ***) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

624 

f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

625 
(*Convert Unix filename of the form path/file to "path/" and "file" ; 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

626 
if filename contains no slash, then it returns "" and "file" *) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

627 
fun split_filename name = 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

628 
let val (file,path) = take_prefix (apr(op<>,"/")) (rev (explode name)) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

629 
in (implode(rev path), implode(rev file)) end; 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

630 

f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

631 
(*Merge splitted filename (path and file); 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

632 
if path does not end with one a slash is appended *) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

633 
fun tack_on "" name = name 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

634 
 tack_on path name = 
41
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

635 
if last_elem (explode path) = "/" then path ^ name 
97aae241094b
added cons, rcons, last_elem, sort_strings, take_suffix;
wenzelm
parents:
24
diff
changeset

636 
else path ^ "/" ^ name; 
24
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

637 

f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

638 
(*Remove the extension of a filename, i.e. the part after the last '.' *) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

639 
fun remove_ext name = 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

640 
let val (file,_) = take_prefix (apr(op<>,".")) (rev (explode name)) 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

641 
in implode (rev file) end; 
f3d4ff75d9f2
added functions that operate on filenames: split_filename (originally located
clasohm
parents:
0
diff
changeset

642 