| author | bulwahn | 
| Fri, 11 Feb 2011 11:47:43 +0100 | |
| changeset 41754 | aa94a003dcdf | 
| parent 39502 | cffceed8e7fa | 
| child 42102 | fcfd07f122d4 | 
| permissions | -rw-r--r-- | 
| 39348 | 1 | (* ========================================================================= *) | 
| 2 | (* ML UTILITY FUNCTIONS *) | |
| 39502 | 3 | (* Copyright (c) 2001 Joe Hurd, distributed under the BSD License *) | 
| 39348 | 4 | (* ========================================================================= *) | 
| 5 | ||
| 6 | structure Useful :> Useful = | |
| 7 | struct | |
| 8 | ||
| 9 | (* ------------------------------------------------------------------------- *) | |
| 10 | (* Exceptions. *) | |
| 11 | (* ------------------------------------------------------------------------- *) | |
| 12 | ||
| 13 | exception Error of string; | |
| 14 | ||
| 15 | exception Bug of string; | |
| 16 | ||
| 17 | fun errorToStringOption err = | |
| 18 | case err of | |
| 19 |       Error message => SOME ("Error: " ^ message)
 | |
| 20 | | _ => NONE; | |
| 21 | ||
| 22 | (*mlton | |
| 23 | val () = MLton.Exn.addExnMessager errorToStringOption; | |
| 24 | *) | |
| 25 | ||
| 26 | fun errorToString err = | |
| 27 | case errorToStringOption err of | |
| 28 | SOME s => "\n" ^ s ^ "\n" | |
| 29 | | NONE => raise Bug "errorToString: not an Error exception"; | |
| 30 | ||
| 31 | fun bugToStringOption err = | |
| 32 | case err of | |
| 33 |       Bug message => SOME ("Bug: " ^ message)
 | |
| 34 | | _ => NONE; | |
| 35 | ||
| 36 | (*mlton | |
| 37 | val () = MLton.Exn.addExnMessager bugToStringOption; | |
| 38 | *) | |
| 39 | ||
| 40 | fun bugToString err = | |
| 41 | case bugToStringOption err of | |
| 42 | SOME s => "\n" ^ s ^ "\n" | |
| 43 | | NONE => raise Bug "bugToString: not a Bug exception"; | |
| 44 | ||
| 45 | fun total f x = SOME (f x) handle Error _ => NONE; | |
| 46 | ||
| 47 | fun can f = Option.isSome o total f; | |
| 48 | ||
| 49 | (* ------------------------------------------------------------------------- *) | |
| 50 | (* Tracing. *) | |
| 51 | (* ------------------------------------------------------------------------- *) | |
| 52 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 53 | val tracePrint = ref TextIO.print; | 
| 39348 | 54 | |
| 55 | fun trace mesg = !tracePrint mesg; | |
| 56 | ||
| 57 | (* ------------------------------------------------------------------------- *) | |
| 58 | (* Combinators. *) | |
| 59 | (* ------------------------------------------------------------------------- *) | |
| 60 | ||
| 61 | fun C f x y = f y x; | |
| 62 | ||
| 63 | fun I x = x; | |
| 64 | ||
| 65 | fun K x y = x; | |
| 66 | ||
| 67 | fun S f g x = f x (g x); | |
| 68 | ||
| 69 | fun W f x = f x x; | |
| 70 | ||
| 71 | fun funpow 0 _ x = x | |
| 72 | | funpow n f x = funpow (n - 1) f (f x); | |
| 73 | ||
| 74 | fun exp m = | |
| 75 | let | |
| 76 | fun f _ 0 z = z | |
| 77 | | f x y z = f (m (x,x)) (y div 2) (if y mod 2 = 0 then z else m (z,x)) | |
| 78 | in | |
| 79 | f | |
| 80 | end; | |
| 81 | ||
| 82 | (* ------------------------------------------------------------------------- *) | |
| 83 | (* Pairs. *) | |
| 84 | (* ------------------------------------------------------------------------- *) | |
| 85 | ||
| 86 | fun fst (x,_) = x; | |
| 87 | ||
| 88 | fun snd (_,y) = y; | |
| 89 | ||
| 90 | fun pair x y = (x,y); | |
| 91 | ||
| 92 | fun swap (x,y) = (y,x); | |
| 93 | ||
| 94 | fun curry f x y = f (x,y); | |
| 95 | ||
| 96 | fun uncurry f (x,y) = f x y; | |
| 97 | ||
| 98 | val op## = fn (f,g) => fn (x,y) => (f x, g y); | |
| 99 | ||
| 100 | (* ------------------------------------------------------------------------- *) | |
| 101 | (* State transformers. *) | |
| 102 | (* ------------------------------------------------------------------------- *) | |
| 103 | ||
| 104 | val unit : 'a -> 's -> 'a * 's = pair; | |
| 105 | ||
| 106 | fun bind f (g : 'a -> 's -> 'b * 's) = uncurry g o f; | |
| 107 | ||
| 108 | fun mmap f (m : 's -> 'a * 's) = bind m (unit o f); | |
| 109 | ||
| 110 | fun mjoin (f : 's -> ('s -> 'a * 's) * 's) = bind f I;
 | |
| 111 | ||
| 112 | fun mwhile c b = let fun f a = if c a then bind (b a) f else unit a in f end; | |
| 113 | ||
| 114 | (* ------------------------------------------------------------------------- *) | |
| 115 | (* Equality. *) | |
| 116 | (* ------------------------------------------------------------------------- *) | |
| 117 | ||
| 118 | val equal = fn x => fn y => x = y; | |
| 119 | ||
| 120 | val notEqual = fn x => fn y => x <> y; | |
| 121 | ||
| 122 | fun listEqual xEq = | |
| 123 | let | |
| 124 | fun xsEq [] [] = true | |
| 125 | | xsEq (x1 :: xs1) (x2 :: xs2) = xEq x1 x2 andalso xsEq xs1 xs2 | |
| 126 | | xsEq _ _ = false | |
| 127 | in | |
| 128 | xsEq | |
| 129 | end; | |
| 130 | ||
| 131 | (* ------------------------------------------------------------------------- *) | |
| 132 | (* Comparisons. *) | |
| 133 | (* ------------------------------------------------------------------------- *) | |
| 134 | ||
| 135 | fun mapCompare f cmp (a,b) = cmp (f a, f b); | |
| 136 | ||
| 137 | fun revCompare cmp x_y = | |
| 138 | case cmp x_y of LESS => GREATER | EQUAL => EQUAL | GREATER => LESS; | |
| 139 | ||
| 140 | fun prodCompare xCmp yCmp ((x1,y1),(x2,y2)) = | |
| 141 | case xCmp (x1,x2) of | |
| 142 | LESS => LESS | |
| 143 | | EQUAL => yCmp (y1,y2) | |
| 144 | | GREATER => GREATER; | |
| 145 | ||
| 146 | fun lexCompare cmp = | |
| 147 | let | |
| 148 | fun lex ([],[]) = EQUAL | |
| 149 | | lex ([], _ :: _) = LESS | |
| 150 | | lex (_ :: _, []) = GREATER | |
| 151 | | lex (x :: xs, y :: ys) = | |
| 152 | case cmp (x,y) of | |
| 153 | LESS => LESS | |
| 154 | | EQUAL => lex (xs,ys) | |
| 155 | | GREATER => GREATER | |
| 156 | in | |
| 157 | lex | |
| 158 | end; | |
| 159 | ||
| 160 | fun optionCompare _ (NONE,NONE) = EQUAL | |
| 161 | | optionCompare _ (NONE,_) = LESS | |
| 162 | | optionCompare _ (_,NONE) = GREATER | |
| 163 | | optionCompare cmp (SOME x, SOME y) = cmp (x,y); | |
| 164 | ||
| 165 | fun boolCompare (false,true) = LESS | |
| 166 | | boolCompare (true,false) = GREATER | |
| 167 | | boolCompare _ = EQUAL; | |
| 168 | ||
| 169 | (* ------------------------------------------------------------------------- *) | |
| 170 | (* Lists. *) | |
| 171 | (* ------------------------------------------------------------------------- *) | |
| 172 | ||
| 173 | fun cons x y = x :: y; | |
| 174 | ||
| 175 | fun hdTl l = (hd l, tl l); | |
| 176 | ||
| 177 | fun append xs ys = xs @ ys; | |
| 178 | ||
| 179 | fun singleton a = [a]; | |
| 180 | ||
| 181 | fun first f [] = NONE | |
| 182 | | first f (x :: xs) = (case f x of NONE => first f xs | s => s); | |
| 183 | ||
| 184 | fun maps (_ : 'a -> 's -> 'b * 's) [] = unit [] | |
| 185 | | maps f (x :: xs) = | |
| 186 | bind (f x) (fn y => bind (maps f xs) (fn ys => unit (y :: ys))); | |
| 187 | ||
| 188 | fun mapsPartial (_ : 'a -> 's -> 'b option * 's) [] = unit [] | |
| 189 | | mapsPartial f (x :: xs) = | |
| 190 | bind | |
| 191 | (f x) | |
| 192 | (fn yo => | |
| 193 | bind | |
| 194 | (mapsPartial f xs) | |
| 195 | (fn ys => unit (case yo of NONE => ys | SOME y => y :: ys))); | |
| 196 | ||
| 197 | fun zipWith f = | |
| 198 | let | |
| 199 | fun z l [] [] = l | |
| 200 | | z l (x :: xs) (y :: ys) = z (f x y :: l) xs ys | |
| 201 | | z _ _ _ = raise Error "zipWith: lists different lengths"; | |
| 202 | in | |
| 203 | fn xs => fn ys => rev (z [] xs ys) | |
| 204 | end; | |
| 205 | ||
| 206 | fun zip xs ys = zipWith pair xs ys; | |
| 207 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 208 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 209 | fun inc ((x,y),(xs,ys)) = (x :: xs, y :: ys); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 210 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 211 | fun unzip ab = List.foldl inc ([],[]) (rev ab); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 212 | end; | 
| 39348 | 213 | |
| 214 | fun cartwith f = | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 215 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 216 | fun aux _ res _ [] = res | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 217 | | aux xsCopy res [] (y :: yt) = aux xsCopy res xsCopy yt | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 218 | | aux xsCopy res (x :: xt) (ys as y :: _) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 219 | aux xsCopy (f x y :: res) xt ys | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 220 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 221 | fn xs => fn ys => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 222 | let val xs' = rev xs in aux xs' [] xs' (rev ys) end | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 223 | end; | 
| 39348 | 224 | |
| 225 | fun cart xs ys = cartwith pair xs ys; | |
| 226 | ||
| 227 | fun takeWhile p = | |
| 228 | let | |
| 229 | fun f acc [] = rev acc | |
| 230 | | f acc (x :: xs) = if p x then f (x :: acc) xs else rev acc | |
| 231 | in | |
| 232 | f [] | |
| 233 | end; | |
| 234 | ||
| 235 | fun dropWhile p = | |
| 236 | let | |
| 237 | fun f [] = [] | |
| 238 | | f (l as x :: xs) = if p x then f xs else l | |
| 239 | in | |
| 240 | f | |
| 241 | end; | |
| 242 | ||
| 243 | fun divideWhile p = | |
| 244 | let | |
| 245 | fun f acc [] = (rev acc, []) | |
| 246 | | f acc (l as x :: xs) = if p x then f (x :: acc) xs else (rev acc, l) | |
| 247 | in | |
| 248 | f [] | |
| 249 | end; | |
| 250 | ||
| 251 | fun groups f = | |
| 252 | let | |
| 253 | fun group acc row x l = | |
| 254 | case l of | |
| 255 | [] => | |
| 256 | let | |
| 257 | val acc = if null row then acc else rev row :: acc | |
| 258 | in | |
| 259 | rev acc | |
| 260 | end | |
| 261 | | h :: t => | |
| 262 | let | |
| 263 | val (eor,x) = f (h,x) | |
| 264 | in | |
| 265 | if eor then group (rev row :: acc) [h] x t | |
| 266 | else group acc (h :: row) x t | |
| 267 | end | |
| 268 | in | |
| 269 | group [] [] | |
| 270 | end; | |
| 271 | ||
| 272 | fun groupsBy eq = | |
| 273 | let | |
| 274 | fun f (x_y as (x,_)) = (not (eq x_y), x) | |
| 275 | in | |
| 276 | fn [] => [] | |
| 277 | | h :: t => | |
| 278 | case groups f h t of | |
| 279 | [] => [[h]] | |
| 280 | | hs :: ts => (h :: hs) :: ts | |
| 281 | end; | |
| 282 | ||
| 283 | local | |
| 284 | fun fstEq ((x,_),(y,_)) = x = y; | |
| 285 | ||
| 286 | fun collapse l = (fst (hd l), map snd l); | |
| 287 | in | |
| 288 | fun groupsByFst l = map collapse (groupsBy fstEq l); | |
| 289 | end; | |
| 290 | ||
| 291 | fun groupsOf n = | |
| 292 | let | |
| 293 | fun f (_,i) = if i = 1 then (true,n) else (false, i - 1) | |
| 294 | in | |
| 295 | groups f (n + 1) | |
| 296 | end; | |
| 297 | ||
| 298 | fun index p = | |
| 299 | let | |
| 300 | fun idx _ [] = NONE | |
| 301 | | idx n (x :: xs) = if p x then SOME n else idx (n + 1) xs | |
| 302 | in | |
| 303 | idx 0 | |
| 304 | end; | |
| 305 | ||
| 306 | fun enumerate l = fst (maps (fn x => fn m => ((m, x), m + 1)) l 0); | |
| 307 | ||
| 308 | local | |
| 309 | fun revDiv acc l 0 = (acc,l) | |
| 310 | | revDiv _ [] _ = raise Subscript | |
| 311 | | revDiv acc (h :: t) n = revDiv (h :: acc) t (n - 1); | |
| 312 | in | |
| 313 | fun revDivide l = revDiv [] l; | |
| 314 | end; | |
| 315 | ||
| 316 | fun divide l n = let val (a,b) = revDivide l n in (rev a, b) end; | |
| 317 | ||
| 318 | fun updateNth (n,x) l = | |
| 319 | let | |
| 320 | val (a,b) = revDivide l n | |
| 321 | in | |
| 322 | case b of [] => raise Subscript | _ :: t => List.revAppend (a, x :: t) | |
| 323 | end; | |
| 324 | ||
| 325 | fun deleteNth n l = | |
| 326 | let | |
| 327 | val (a,b) = revDivide l n | |
| 328 | in | |
| 329 | case b of [] => raise Subscript | _ :: t => List.revAppend (a,t) | |
| 330 | end; | |
| 331 | ||
| 332 | (* ------------------------------------------------------------------------- *) | |
| 333 | (* Sets implemented with lists. *) | |
| 334 | (* ------------------------------------------------------------------------- *) | |
| 335 | ||
| 336 | fun mem x = List.exists (equal x); | |
| 337 | ||
| 338 | fun insert x s = if mem x s then s else x :: s; | |
| 339 | ||
| 340 | fun delete x s = List.filter (not o equal x) s; | |
| 341 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 342 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 343 | fun inc (v,x) = if mem v x then x else v :: x; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 344 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 345 | fun setify s = rev (List.foldl inc [] s); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 346 | end; | 
| 39348 | 347 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 348 | fun union s t = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 349 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 350 | fun inc (v,x) = if mem v t then x else v :: x | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 351 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 352 | List.foldl inc t (rev s) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 353 | end; | 
| 39348 | 354 | |
| 355 | fun intersect s t = | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 356 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 357 | fun inc (v,x) = if mem v t then v :: x else x | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 358 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 359 | List.foldl inc [] (rev s) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 360 | end; | 
| 39348 | 361 | |
| 362 | fun difference s t = | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 363 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 364 | fun inc (v,x) = if mem v t then x else v :: x | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 365 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 366 | List.foldl inc [] (rev s) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 367 | end; | 
| 39348 | 368 | |
| 369 | fun subset s t = List.all (fn x => mem x t) s; | |
| 370 | ||
| 371 | fun distinct [] = true | |
| 372 | | distinct (x :: rest) = not (mem x rest) andalso distinct rest; | |
| 373 | ||
| 374 | (* ------------------------------------------------------------------------- *) | |
| 375 | (* Sorting and searching. *) | |
| 376 | (* ------------------------------------------------------------------------- *) | |
| 377 | ||
| 378 | (* Finding the minimum and maximum element of a list, wrt some order. *) | |
| 379 | ||
| 380 | fun minimum cmp = | |
| 381 | let | |
| 382 | fun min (l,m,r) _ [] = (m, List.revAppend (l,r)) | |
| 383 | | min (best as (_,m,_)) l (x :: r) = | |
| 384 | min (case cmp (x,m) of LESS => (l,x,r) | _ => best) (x :: l) r | |
| 385 | in | |
| 386 | fn [] => raise Empty | |
| 387 | | h :: t => min ([],h,t) [h] t | |
| 388 | end; | |
| 389 | ||
| 390 | fun maximum cmp = minimum (revCompare cmp); | |
| 391 | ||
| 392 | (* Merge (for the following merge-sort, but generally useful too). *) | |
| 393 | ||
| 394 | fun merge cmp = | |
| 395 | let | |
| 396 | fun mrg acc [] ys = List.revAppend (acc,ys) | |
| 397 | | mrg acc xs [] = List.revAppend (acc,xs) | |
| 398 | | mrg acc (xs as x :: xt) (ys as y :: yt) = | |
| 399 | (case cmp (x,y) of | |
| 400 | GREATER => mrg (y :: acc) xs yt | |
| 401 | | _ => mrg (x :: acc) xt ys) | |
| 402 | in | |
| 403 | mrg [] | |
| 404 | end; | |
| 405 | ||
| 406 | (* Merge sort (stable). *) | |
| 407 | ||
| 408 | fun sort cmp = | |
| 409 | let | |
| 410 | fun findRuns acc r rs [] = rev (rev (r :: rs) :: acc) | |
| 411 | | findRuns acc r rs (x :: xs) = | |
| 412 | case cmp (r,x) of | |
| 413 | GREATER => findRuns (rev (r :: rs) :: acc) x [] xs | |
| 414 | | _ => findRuns acc x (r :: rs) xs | |
| 415 | ||
| 416 | fun mergeAdj acc [] = rev acc | |
| 417 | | mergeAdj acc (xs as [_]) = List.revAppend (acc,xs) | |
| 418 | | mergeAdj acc (x :: y :: xs) = mergeAdj (merge cmp x y :: acc) xs | |
| 419 | ||
| 420 | fun mergePairs [xs] = xs | |
| 421 | | mergePairs l = mergePairs (mergeAdj [] l) | |
| 422 | in | |
| 423 | fn [] => [] | |
| 424 | | l as [_] => l | |
| 425 | | h :: t => mergePairs (findRuns [] h [] t) | |
| 426 | end; | |
| 427 | ||
| 428 | fun sortMap _ _ [] = [] | |
| 429 | | sortMap _ _ (l as [_]) = l | |
| 430 | | sortMap f cmp xs = | |
| 431 | let | |
| 432 | fun ncmp ((m,_),(n,_)) = cmp (m,n) | |
| 433 | val nxs = map (fn x => (f x, x)) xs | |
| 434 | val nys = sort ncmp nxs | |
| 435 | in | |
| 436 | map snd nys | |
| 437 | end; | |
| 438 | ||
| 439 | (* ------------------------------------------------------------------------- *) | |
| 440 | (* Integers. *) | |
| 441 | (* ------------------------------------------------------------------------- *) | |
| 442 | ||
| 443 | fun interval m 0 = [] | |
| 444 | | interval m len = m :: interval (m + 1) (len - 1); | |
| 445 | ||
| 446 | fun divides _ 0 = true | |
| 447 | | divides 0 _ = false | |
| 448 | | divides a b = b mod (Int.abs a) = 0; | |
| 449 | ||
| 450 | local | |
| 451 | fun hcf 0 n = n | |
| 452 | | hcf 1 _ = 1 | |
| 453 | | hcf m n = hcf (n mod m) m; | |
| 454 | in | |
| 455 | fun gcd m n = | |
| 456 | let | |
| 457 | val m = Int.abs m | |
| 458 | and n = Int.abs n | |
| 459 | in | |
| 460 | if m < n then hcf m n else hcf n m | |
| 461 | end; | |
| 462 | end; | |
| 463 | ||
| 464 | local | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 465 | fun calcPrimes mode ps i n = | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 466 | if n = 0 then ps | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 467 | else if List.exists (fn p => divides p i) ps then | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 468 | let | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 469 | val i = i + 1 | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 470 | and n = if mode then n else n - 1 | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 471 | in | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 472 | calcPrimes mode ps i n | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 473 | end | 
| 39348 | 474 | else | 
| 475 | let | |
| 476 | val ps = ps @ [i] | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 477 | and i = i + 1 | 
| 39348 | 478 | and n = n - 1 | 
| 479 | in | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 480 | calcPrimes mode ps i n | 
| 39348 | 481 | end; | 
| 482 | in | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 483 | fun primes n = | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 484 | if n <= 0 then [] | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 485 | else calcPrimes true [2] 3 (n - 1); | 
| 39348 | 486 | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 487 | fun primesUpTo n = | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 488 | if n < 2 then [] | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 489 | else calcPrimes false [2] 3 (n - 2); | 
| 39348 | 490 | end; | 
| 491 | ||
| 492 | (* ------------------------------------------------------------------------- *) | |
| 493 | (* Strings. *) | |
| 494 | (* ------------------------------------------------------------------------- *) | |
| 495 | ||
| 496 | local | |
| 497 | fun len l = (length l, l) | |
| 498 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 499 | val upper = len (String.explode "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | 
| 39348 | 500 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 501 | val lower = len (String.explode "abcdefghijklmnopqrstuvwxyz"); | 
| 39348 | 502 | |
| 503 | fun rotate (n,l) c k = | |
| 504 | List.nth (l, (k + Option.valOf (index (equal c) l)) mod n); | |
| 505 | in | |
| 506 | fun rot k c = | |
| 507 | if Char.isLower c then rotate lower c k | |
| 508 | else if Char.isUpper c then rotate upper c k | |
| 509 | else c; | |
| 510 | end; | |
| 511 | ||
| 512 | fun charToInt #"0" = SOME 0 | |
| 513 | | charToInt #"1" = SOME 1 | |
| 514 | | charToInt #"2" = SOME 2 | |
| 515 | | charToInt #"3" = SOME 3 | |
| 516 | | charToInt #"4" = SOME 4 | |
| 517 | | charToInt #"5" = SOME 5 | |
| 518 | | charToInt #"6" = SOME 6 | |
| 519 | | charToInt #"7" = SOME 7 | |
| 520 | | charToInt #"8" = SOME 8 | |
| 521 | | charToInt #"9" = SOME 9 | |
| 522 | | charToInt _ = NONE; | |
| 523 | ||
| 524 | fun charFromInt 0 = SOME #"0" | |
| 525 | | charFromInt 1 = SOME #"1" | |
| 526 | | charFromInt 2 = SOME #"2" | |
| 527 | | charFromInt 3 = SOME #"3" | |
| 528 | | charFromInt 4 = SOME #"4" | |
| 529 | | charFromInt 5 = SOME #"5" | |
| 530 | | charFromInt 6 = SOME #"6" | |
| 531 | | charFromInt 7 = SOME #"7" | |
| 532 | | charFromInt 8 = SOME #"8" | |
| 533 | | charFromInt 9 = SOME #"9" | |
| 534 | | charFromInt _ = NONE; | |
| 535 | ||
| 536 | fun nChars x = | |
| 537 | let | |
| 538 | fun dup 0 l = l | dup n l = dup (n - 1) (x :: l) | |
| 539 | in | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 540 | fn n => String.implode (dup n []) | 
| 39348 | 541 | end; | 
| 542 | ||
| 543 | fun chomp s = | |
| 544 | let | |
| 545 | val n = size s | |
| 546 | in | |
| 547 | if n = 0 orelse String.sub (s, n - 1) <> #"\n" then s | |
| 548 | else String.substring (s, 0, n - 1) | |
| 549 | end; | |
| 550 | ||
| 551 | local | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 552 | fun chop l = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 553 | case l of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 554 | [] => [] | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 555 | | h :: t => if Char.isSpace h then chop t else l; | 
| 39348 | 556 | in | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 557 | val trim = String.implode o chop o rev o chop o rev o String.explode; | 
| 39348 | 558 | end; | 
| 559 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 560 | val join = String.concatWith; | 
| 39348 | 561 | |
| 562 | local | |
| 563 | fun match [] l = SOME l | |
| 564 | | match _ [] = NONE | |
| 565 | | match (x :: xs) (y :: ys) = if x = y then match xs ys else NONE; | |
| 566 | ||
| 567 | fun stringify acc [] = acc | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 568 | | stringify acc (h :: t) = stringify (String.implode h :: acc) t; | 
| 39348 | 569 | in | 
| 570 | fun split sep = | |
| 571 | let | |
| 572 | val pat = String.explode sep | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 573 | |
| 39348 | 574 | fun div1 prev recent [] = stringify [] (rev recent :: prev) | 
| 575 | | div1 prev recent (l as h :: t) = | |
| 576 | case match pat l of | |
| 577 | NONE => div1 prev (h :: recent) t | |
| 578 | | SOME rest => div1 (rev recent :: prev) [] rest | |
| 579 | in | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 580 | fn s => div1 [] [] (String.explode s) | 
| 39348 | 581 | end; | 
| 582 | end; | |
| 583 | ||
| 584 | fun capitalize s = | |
| 585 | if s = "" then s | |
| 586 | else str (Char.toUpper (String.sub (s,0))) ^ String.extract (s,1,NONE); | |
| 587 | ||
| 588 | fun mkPrefix p s = p ^ s; | |
| 589 | ||
| 590 | fun destPrefix p = | |
| 591 | let | |
| 592 | fun check s = | |
| 593 | if String.isPrefix p s then () | |
| 594 | else raise Error "destPrefix" | |
| 595 | ||
| 596 | val sizeP = size p | |
| 597 | in | |
| 598 | fn s => | |
| 599 | let | |
| 600 | val () = check s | |
| 601 | in | |
| 602 | String.extract (s,sizeP,NONE) | |
| 603 | end | |
| 604 | end; | |
| 605 | ||
| 606 | fun isPrefix p = can (destPrefix p); | |
| 607 | ||
| 608 | fun stripPrefix pred s = | |
| 609 | Substring.string (Substring.dropl pred (Substring.full s)); | |
| 610 | ||
| 611 | fun mkSuffix p s = s ^ p; | |
| 612 | ||
| 613 | fun destSuffix p = | |
| 614 | let | |
| 615 | fun check s = | |
| 616 | if String.isSuffix p s then () | |
| 617 | else raise Error "destSuffix" | |
| 618 | ||
| 619 | val sizeP = size p | |
| 620 | in | |
| 621 | fn s => | |
| 622 | let | |
| 623 | val () = check s | |
| 624 | ||
| 625 | val sizeS = size s | |
| 626 | in | |
| 627 | String.substring (s, 0, sizeS - sizeP) | |
| 628 | end | |
| 629 | end; | |
| 630 | ||
| 631 | fun isSuffix p = can (destSuffix p); | |
| 632 | ||
| 633 | fun stripSuffix pred s = | |
| 634 | Substring.string (Substring.dropr pred (Substring.full s)); | |
| 635 | ||
| 636 | (* ------------------------------------------------------------------------- *) | |
| 637 | (* Tables. *) | |
| 638 | (* ------------------------------------------------------------------------- *) | |
| 639 | ||
| 640 | type columnAlignment = {leftAlign : bool, padChar : char}
 | |
| 641 | ||
| 642 | fun alignColumn {leftAlign,padChar} column =
 | |
| 643 | let | |
| 644 | val (n,_) = maximum Int.compare (map size column) | |
| 645 | ||
| 646 | fun pad entry row = | |
| 647 | let | |
| 648 | val padding = nChars padChar (n - size entry) | |
| 649 | in | |
| 650 | if leftAlign then entry ^ padding ^ row | |
| 651 | else padding ^ entry ^ row | |
| 652 | end | |
| 653 | in | |
| 654 | zipWith pad column | |
| 655 | end; | |
| 656 | ||
| 657 | local | |
| 658 | fun alignTab aligns rows = | |
| 659 | case aligns of | |
| 660 | [] => map (K "") rows | |
| 661 |       | [{leftAlign = true, padChar = #" "}] => map hd rows
 | |
| 662 | | align :: aligns => | |
| 663 | alignColumn align (map hd rows) (alignTab aligns (map tl rows)); | |
| 664 | in | |
| 665 | fun alignTable aligns rows = | |
| 666 | if null rows then [] else alignTab aligns rows; | |
| 667 | end; | |
| 668 | ||
| 669 | (* ------------------------------------------------------------------------- *) | |
| 670 | (* Reals. *) | |
| 671 | (* ------------------------------------------------------------------------- *) | |
| 672 | ||
| 673 | val realToString = Real.toString; | |
| 674 | ||
| 675 | fun percentToString x = Int.toString (Real.round (100.0 * x)) ^ "%"; | |
| 676 | ||
| 677 | fun pos r = Real.max (r,0.0); | |
| 678 | ||
| 679 | local | |
| 680 | val invLn2 = 1.0 / Math.ln 2.0; | |
| 681 | in | |
| 682 | fun log2 x = invLn2 * Math.ln x; | |
| 683 | end; | |
| 684 | ||
| 685 | (* ------------------------------------------------------------------------- *) | |
| 686 | (* Sums. *) | |
| 687 | (* ------------------------------------------------------------------------- *) | |
| 688 | ||
| 689 | datatype ('a,'b) sum = Left of 'a | Right of 'b
 | |
| 690 | ||
| 691 | fun destLeft (Left l) = l | |
| 692 | | destLeft _ = raise Error "destLeft"; | |
| 693 | ||
| 694 | fun isLeft (Left _) = true | |
| 695 | | isLeft (Right _) = false; | |
| 696 | ||
| 697 | fun destRight (Right r) = r | |
| 698 | | destRight _ = raise Error "destRight"; | |
| 699 | ||
| 700 | fun isRight (Left _) = false | |
| 701 | | isRight (Right _) = true; | |
| 702 | ||
| 703 | (* ------------------------------------------------------------------------- *) | |
| 704 | (* Useful impure features. *) | |
| 705 | (* ------------------------------------------------------------------------- *) | |
| 706 | ||
| 707 | local | |
| 708 | val generator = ref 0 | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 709 | |
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 710 | fun newIntThunk () = | 
| 39348 | 711 | let | 
| 712 | val n = !generator | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 713 | |
| 39348 | 714 | val () = generator := n + 1 | 
| 715 | in | |
| 716 | n | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 717 | end; | 
| 39348 | 718 | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 719 | fun newIntsThunk k () = | 
| 39348 | 720 | let | 
| 721 | val n = !generator | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 722 | |
| 39348 | 723 | val () = generator := n + k | 
| 724 | in | |
| 725 | interval n k | |
| 39501 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 726 | end; | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 727 | in | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 728 | fun newInt () = Portable.critical newIntThunk (); | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 729 | |
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 730 | fun newInts k = | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 731 | if k <= 0 then [] | 
| 
aaa7078fff55
updated source files with Metis 2.3 (timestamp: 16 Sept. 2010)
 blanchet parents: 
39445diff
changeset | 732 | else Portable.critical (newIntsThunk k) (); | 
| 39348 | 733 | end; | 
| 734 | ||
| 735 | fun withRef (r,new) f x = | |
| 736 | let | |
| 737 | val old = !r | |
| 738 | val () = r := new | |
| 739 | val y = f x handle e => (r := old; raise e) | |
| 740 | val () = r := old | |
| 741 | in | |
| 742 | y | |
| 743 | end; | |
| 744 | ||
| 745 | fun cloneArray a = | |
| 746 | let | |
| 747 | fun index i = Array.sub (a,i) | |
| 748 | in | |
| 749 | Array.tabulate (Array.length a, index) | |
| 750 | end; | |
| 751 | ||
| 752 | (* ------------------------------------------------------------------------- *) | |
| 753 | (* Environment. *) | |
| 754 | (* ------------------------------------------------------------------------- *) | |
| 755 | ||
| 756 | fun host () = Option.getOpt (OS.Process.getEnv "HOSTNAME", "unknown"); | |
| 757 | ||
| 758 | fun time () = Date.fmt "%H:%M:%S" (Date.fromTimeLocal (Time.now ())); | |
| 759 | ||
| 760 | fun date () = Date.fmt "%d/%m/%Y" (Date.fromTimeLocal (Time.now ())); | |
| 761 | ||
| 762 | fun readDirectory {directory = dir} =
 | |
| 763 | let | |
| 764 | val dirStrm = OS.FileSys.openDir dir | |
| 765 | ||
| 766 | fun readAll acc = | |
| 767 | case OS.FileSys.readDir dirStrm of | |
| 768 | NONE => acc | |
| 769 | | SOME file => | |
| 770 | let | |
| 771 |               val filename = OS.Path.joinDirFile {dir = dir, file = file}
 | |
| 772 | ||
| 773 |               val acc = {filename = filename} :: acc
 | |
| 774 | in | |
| 775 | readAll acc | |
| 776 | end | |
| 777 | ||
| 778 | val filenames = readAll [] | |
| 779 | ||
| 780 | val () = OS.FileSys.closeDir dirStrm | |
| 781 | in | |
| 782 | rev filenames | |
| 783 | end; | |
| 784 | ||
| 785 | fun readTextFile {filename} =
 | |
| 786 | let | |
| 787 | open TextIO | |
| 788 | ||
| 789 | val h = openIn filename | |
| 790 | ||
| 791 | val contents = inputAll h | |
| 792 | ||
| 793 | val () = closeIn h | |
| 794 | in | |
| 795 | contents | |
| 796 | end; | |
| 797 | ||
| 798 | fun writeTextFile {contents,filename} =
 | |
| 799 | let | |
| 800 | open TextIO | |
| 801 | val h = openOut filename | |
| 802 | val () = output (h,contents) | |
| 803 | val () = closeOut h | |
| 804 | in | |
| 805 | () | |
| 806 | end; | |
| 807 | ||
| 808 | (* ------------------------------------------------------------------------- *) | |
| 809 | (* Profiling and error reporting. *) | |
| 810 | (* ------------------------------------------------------------------------- *) | |
| 811 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 812 | fun chat s = TextIO.output (TextIO.stdOut, s ^ "\n"); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 813 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 814 | fun chide s = TextIO.output (TextIO.stdErr, s ^ "\n"); | 
| 39348 | 815 | |
| 816 | local | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39414diff
changeset | 817 | fun err x s = chide (x ^ ": " ^ s); | 
| 39348 | 818 | in | 
| 819 | fun try f x = f x | |
| 820 | handle e as Error _ => (err "try" (errorToString e); raise e) | |
| 821 | | e as Bug _ => (err "try" (bugToString e); raise e) | |
| 822 | | e => (err "try" "strange exception raised"; raise e); | |
| 823 | ||
| 824 | val warn = err "WARNING"; | |
| 825 | ||
| 826 | fun die s = (err "\nFATAL ERROR" s; OS.Process.exit OS.Process.failure); | |
| 827 | end; | |
| 828 | ||
| 829 | fun timed f a = | |
| 830 | let | |
| 831 | val tmr = Timer.startCPUTimer () | |
| 832 | val res = f a | |
| 833 |     val {usr,sys,...} = Timer.checkCPUTimer tmr
 | |
| 834 | in | |
| 835 | (Time.toReal usr + Time.toReal sys, res) | |
| 836 | end; | |
| 837 | ||
| 838 | local | |
| 839 | val MIN = 1.0; | |
| 840 | ||
| 841 | fun several n t f a = | |
| 842 | let | |
| 843 | val (t',res) = timed f a | |
| 844 | val t = t + t' | |
| 845 | val n = n + 1 | |
| 846 | in | |
| 847 | if t > MIN then (t / Real.fromInt n, res) else several n t f a | |
| 848 | end; | |
| 849 | in | |
| 850 | fun timedMany f a = several 0 0.0 f a | |
| 851 | end; | |
| 852 | ||
| 853 | val executionTime = | |
| 854 | let | |
| 855 | val startTime = Time.toReal (Time.now ()) | |
| 856 | in | |
| 857 | fn () => Time.toReal (Time.now ()) - startTime | |
| 858 | end; | |
| 859 | ||
| 860 | end |