| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
) |
| 6 |
|
| 7 |
*) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
let cmp = compare |
| 16 |
|
| 17 |
let ( =? ) x y = cmp x y = 0 |
| 18 |
let ( <? ) x y = cmp x y < 0 |
| 19 |
let ( <=? ) x y = cmp x y <= 0 |
| 20 |
let ( >? ) x y = cmp x y > 0 |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
let o f g x = f (g x) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
let rec end_itlist f l = |
| 33 |
match l with |
| 34 |
| [] -> failwith "end_itlist" |
| 35 |
| [x] -> x |
| 36 |
| h :: t -> f h (end_itlist f t) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
let rec allpairs f l1 l2 = |
| 43 |
match l1 with |
| 44 |
| h1 :: t1 -> List.fold_right (fun x a -> f h1 x :: a) l2 (allpairs f t1 l2) |
| 45 |
| [] -> [] |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
let implode l = List.fold_right ( ^ ) l "" |
| 52 |
|
| 53 |
let explode s = |
| 54 |
let rec exap n l = |
| 55 |
if n < 0 then l else exap (n - 1) (String.sub s n 1 :: l) |
| 56 |
in |
| 57 |
exap (String.length s - 1) [] |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
let rec funpow n f x = if n < 1 then x else funpow (n - 1) f (f x) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
let rec ( -- ) m n = if m > n then [] else m :: (m + 1 -- n) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
let rec tryfind f l = |
| 76 |
match l with |
| 77 |
| [] -> failwith "tryfind" |
| 78 |
| h :: t -> ( try f h with Failure _ -> tryfind f t ) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
let rec mem x lis = match lis with [] -> false | h :: t -> x =? h || mem x t |
| 85 |
let insert x l = if mem x l then l else x :: l |
| 86 |
let union l1 l2 = List.fold_right insert l1 l2 |
| 87 |
let subtract l1 l2 = List.filter (fun x -> not (mem x l2)) l1 |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
let increasing f x y = f x <? f y |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
let rec do_list f l = match l with [] -> () | h :: t -> f h; do_list f t |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
let rec sort cmp lis = |
| 106 |
match lis with |
| 107 |
| [] -> [] |
| 108 |
| piv :: rest -> |
| 109 |
let r, l = List.partition (cmp piv) rest in |
| 110 |
sort cmp l @ (piv :: sort cmp r) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
let rec uniq l = |
| 117 |
match l with |
| 118 |
| x :: (y :: _ as t) -> |
| 119 |
let t' = uniq t in |
| 120 |
if x =? y then t' else if t' == t then l else x :: t' |
| 121 |
| _ -> l |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
let setify s = uniq (sort ( <=? ) s) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
type ('a, 'b) func = |
| 139 |
| Empty |
| 140 |
| Leaf of int * ('a * 'b) list |
| 141 |
| Branch of int * int * ('a, 'b) func * ('a, 'b) func |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
let undefined = Empty |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
let is_undefined f = match f with Empty -> true | _ -> false |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
let mapf = |
| 160 |
let rec map_list f l = |
| 161 |
match l with [] -> [] | (x, y) :: t -> (x, f y) :: map_list f t |
| 162 |
in |
| 163 |
let rec mapf f t = |
| 164 |
match t with |
| 165 |
| Empty -> Empty |
| 166 |
| Leaf (h, l) -> Leaf (h, map_list f l) |
| 167 |
| Branch (p, b, l, r) -> Branch (p, b, mapf f l, mapf f r) |
| 168 |
in |
| 169 |
mapf |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let foldl = |
| 176 |
let rec foldl_list f a l = |
| 177 |
match l with [] -> a | (x, y) :: t -> foldl_list f (f a x y) t |
| 178 |
in |
| 179 |
let rec foldl f a t = |
| 180 |
match t with |
| 181 |
| Empty -> a |
| 182 |
| Leaf (h, l) -> foldl_list f a l |
| 183 |
| Branch (p, b, l, r) -> foldl f (foldl f a l) r |
| 184 |
in |
| 185 |
foldl |
| 186 |
|
| 187 |
let foldr = |
| 188 |
let rec foldr_list f l a = |
| 189 |
match l with [] -> a | (x, y) :: t -> f x y (foldr_list f t a) |
| 190 |
in |
| 191 |
let rec foldr f t a = |
| 192 |
match t with |
| 193 |
| Empty -> a |
| 194 |
| Leaf (h, l) -> foldr_list f l a |
| 195 |
| Branch (p, b, l, r) -> foldr f l (foldr f r a) |
| 196 |
in |
| 197 |
foldr |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
let ( |-> ), combine = |
| 204 |
let ldb x y = |
| 205 |
let z = x lxor y in |
| 206 |
z land -z |
| 207 |
in |
| 208 |
let newbranch p1 t1 p2 t2 = |
| 209 |
let b = ldb p1 p2 in |
| 210 |
let p = p1 land (b - 1) in |
| 211 |
if p1 land b = 0 then Branch (p, b, t1, t2) else Branch (p, b, t2, t1) |
| 212 |
in |
| 213 |
let rec define_list ((x, y) as xy) l = |
| 214 |
match l with |
| 215 |
| ((a, b) as ab) :: t -> |
| 216 |
if x =? a then xy :: t |
| 217 |
else if x <? a then xy :: l |
| 218 |
else ab :: define_list xy t |
| 219 |
| [] -> [xy] |
| 220 |
and combine_list op z l1 l2 = |
| 221 |
match (l1, l2) with |
| 222 |
| [], _ -> l2 |
| 223 |
| _, [] -> l1 |
| 224 |
| ((x1, y1) as xy1) :: t1, ((x2, y2) as xy2) :: t2 -> |
| 225 |
if x1 <? x2 then xy1 :: combine_list op z t1 l2 |
| 226 |
else if x2 <? x1 then xy2 :: combine_list op z l1 t2 |
| 227 |
else |
| 228 |
let y = op y1 y2 and l = combine_list op z t1 t2 in |
| 229 |
if z y then l else (x1, y) :: l |
| 230 |
in |
| 231 |
let ( |-> ) x y = |
| 232 |
let k = Hashtbl.hash x in |
| 233 |
let rec upd t = |
| 234 |
match t with |
| 235 |
| Empty -> Leaf (k, [(x, y)]) |
| 236 |
| Leaf (h, l) -> |
| 237 |
if h = k then Leaf (h, define_list (x, y) l) |
| 238 |
else newbranch h t k (Leaf (k, [(x, y)])) |
| 239 |
| Branch (p, b, l, r) -> |
| 240 |
if k land (b - 1) <> p then newbranch p t k (Leaf (k, [(x, y)])) |
| 241 |
else if k land b = 0 then Branch (p, b, upd l, r) |
| 242 |
else Branch (p, b, l, upd r) |
| 243 |
in |
| 244 |
upd |
| 245 |
in |
| 246 |
let rec combine op z t1 t2 = |
| 247 |
match (t1, t2) with |
| 248 |
| Empty, _ -> t2 |
| 249 |
| _, Empty -> t1 |
| 250 |
| Leaf (h1, l1), Leaf (h2, l2) -> |
| 251 |
if h1 = h2 then |
| 252 |
let l = combine_list op z l1 l2 in |
| 253 |
if l = [] then Empty else Leaf (h1, l) |
| 254 |
else newbranch h1 t1 h2 t2 |
| 255 |
| (Leaf (k, lis) as lf), (Branch (p, b, l, r) as br) |
| 256 |
|(Branch (p, b, l, r) as br), (Leaf (k, lis) as lf) -> |
| 257 |
if k land (b - 1) = p then |
| 258 |
if k land b = 0 then |
| 259 |
let l' = combine op z lf l in |
| 260 |
if is_undefined l' then r else Branch (p, b, l', r) |
| 261 |
else |
| 262 |
let r' = combine op z lf r in |
| 263 |
if is_undefined r' then l else Branch (p, b, l, r') |
| 264 |
else newbranch k lf p br |
| 265 |
| Branch (p1, b1, l1, r1), Branch (p2, b2, l2, r2) -> |
| 266 |
if b1 < b2 then |
| 267 |
if p2 land (b1 - 1) <> p1 then newbranch p1 t1 p2 t2 |
| 268 |
else if p2 land b1 = 0 then |
| 269 |
let l = combine op z l1 t2 in |
| 270 |
if is_undefined l then r1 else Branch (p1, b1, l, r1) |
| 271 |
else |
| 272 |
let r = combine op z r1 t2 in |
| 273 |
if is_undefined r then l1 else Branch (p1, b1, l1, r) |
| 274 |
else if b2 < b1 then |
| 275 |
if p1 land (b2 - 1) <> p2 then newbranch p1 t1 p2 t2 |
| 276 |
else if p1 land b2 = 0 then |
| 277 |
let l = combine op z t1 l2 in |
| 278 |
if is_undefined l then r2 else Branch (p2, b2, l, r2) |
| 279 |
else |
| 280 |
let r = combine op z t1 r2 in |
| 281 |
if is_undefined r then l2 else Branch (p2, b2, l2, r) |
| 282 |
else if p1 = p2 then |
| 283 |
let l = combine op z l1 l2 and r = combine op z r1 r2 in |
| 284 |
if is_undefined l then r |
| 285 |
else if is_undefined r then l |
| 286 |
else Branch (p1, b1, l, r) |
| 287 |
else newbranch p1 t1 p2 t2 |
| 288 |
in |
| 289 |
(( |-> ), combine) |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
let ( |=> ) x y = (x |-> y) undefined |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
let rec choose t = |
| 302 |
match t with |
| 303 |
| Empty -> failwith "choose: completely undefined function" |
| 304 |
| Leaf (h, l) -> List.hd l |
| 305 |
| Branch (b, p, t1, t2) -> choose t1 |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
let applyd = |
| 312 |
let rec apply_listd l d x = |
| 313 |
match l with |
| 314 |
| (a, b) :: t -> |
| 315 |
if x =? a then b else if x >? a then apply_listd t d x else d x |
| 316 |
| [] -> d x |
| 317 |
in |
| 318 |
fun f d x -> |
| 319 |
let k = Hashtbl.hash x in |
| 320 |
let rec look t = |
| 321 |
match t with |
| 322 |
| Leaf (h, l) when h = k -> apply_listd l d x |
| 323 |
| Branch (p, b, l, r) -> look (if k land b = 0 then l else r) |
| 324 |
| _ -> d x |
| 325 |
in |
| 326 |
look f |
| 327 |
|
| 328 |
let apply f = applyd f (fun x -> failwith "apply") |
| 329 |
let tryapplyd f a d = applyd f (fun x -> d) a |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
let undefine = |
| 336 |
let rec undefine_list x l = |
| 337 |
match l with |
| 338 |
| ((a, b) as ab) :: t -> |
| 339 |
if x =? a then t |
| 340 |
else if x <? a then l |
| 341 |
else |
| 342 |
let t' = undefine_list x t in |
| 343 |
if t' == t then l else ab :: t' |
| 344 |
| [] -> [] |
| 345 |
in |
| 346 |
fun x -> |
| 347 |
let k = Hashtbl.hash x in |
| 348 |
let rec und t = |
| 349 |
match t with |
| 350 |
| Leaf (h, l) when h = k -> |
| 351 |
let l' = undefine_list x l in |
| 352 |
if l' == l then t else if l' = [] then Empty else Leaf (h, l') |
| 353 |
| Branch (p, b, l, r) when k land (b - 1) = p -> |
| 354 |
if k land b = 0 then |
| 355 |
let l' = und l in |
| 356 |
if l' == l then t |
| 357 |
else if is_undefined l' then r |
| 358 |
else Branch (p, b, l', r) |
| 359 |
else |
| 360 |
let r' = und r in |
| 361 |
if r' == r then t |
| 362 |
else if is_undefined r' then l |
| 363 |
else Branch (p, b, l, r') |
| 364 |
| _ -> t |
| 365 |
in |
| 366 |
und |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
let graph f = setify (foldl (fun a x y -> (x, y) :: a) [] f) |
| 373 |
let dom f = setify (foldl (fun a x y -> x :: a) [] f) |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
exception Noparse |
| 380 |
|
| 381 |
let isspace, isnum = |
| 382 |
let charcode s = Char.code s.[0] in |
| 383 |
let spaces = " \t\n\r" |
| 384 |
and separators = ",;" |
| 385 |
and brackets = "()[]{}" |
| 386 |
and symbs = "\\!@#$%^&*-+|\\<=>/?~.:" |
| 387 |
and alphas = "'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 388 |
and nums = "0123456789" in |
| 389 |
let allchars = spaces ^ separators ^ brackets ^ symbs ^ alphas ^ nums in |
| 390 |
let csetsize = List.fold_right (o max charcode) (explode allchars) 256 in |
| 391 |
let ctable = Array.make csetsize 0 in |
| 392 |
do_list (fun c -> ctable.(charcode c) <- 1) (explode spaces); |
| 393 |
do_list (fun c -> ctable.(charcode c) <- 2) (explode separators); |
| 394 |
do_list (fun c -> ctable.(charcode c) <- 4) (explode brackets); |
| 395 |
do_list (fun c -> ctable.(charcode c) <- 8) (explode symbs); |
| 396 |
do_list (fun c -> ctable.(charcode c) <- 16) (explode alphas); |
| 397 |
do_list (fun c -> ctable.(charcode c) <- 32) (explode nums); |
| 398 |
let isspace c = ctable.(charcode c) = 1 |
| 399 |
and isnum c = ctable.(charcode c) = 32 in |
| 400 |
(isspace, isnum) |
| 401 |
|
| 402 |
let parser_or parser1 parser2 input = |
| 403 |
try parser1 input with Noparse -> parser2 input |
| 404 |
|
| 405 |
let ( ++ ) parser1 parser2 input = |
| 406 |
let result1, rest1 = parser1 input in |
| 407 |
let result2, rest2 = parser2 rest1 in |
| 408 |
((result1, result2), rest2) |
| 409 |
|
| 410 |
let rec many prs input = |
| 411 |
try |
| 412 |
let result, next = prs input in |
| 413 |
let results, rest = many prs next in |
| 414 |
(result :: results, rest) |
| 415 |
with Noparse -> ([], input) |
| 416 |
|
| 417 |
let ( >> ) prs treatment input = |
| 418 |
let result, rest = prs input in |
| 419 |
(treatment result, rest) |
| 420 |
|
| 421 |
let fix err prs input = |
| 422 |
try prs input with Noparse -> failwith (err ^ " expected") |
| 423 |
|
| 424 |
let listof prs sep err = |
| 425 |
prs ++ many (sep ++ fix err prs >> snd) >> fun (h, t) -> h :: t |
| 426 |
|
| 427 |
let possibly prs input = |
| 428 |
try |
| 429 |
let x, rest = prs input in |
| 430 |
([x], rest) |
| 431 |
with Noparse -> ([], input) |
| 432 |
|
| 433 |
let some p = function |
| 434 |
| [] -> raise Noparse |
| 435 |
| h :: t -> if p h then (h, t) else raise Noparse |
| 436 |
|
| 437 |
let a tok = some (fun item -> item = tok) |
| 438 |
|
| 439 |
let rec atleast n prs i = |
| 440 |
( if n <= 0 then many prs |
| 441 |
else prs ++ atleast (n - 1) prs >> fun (h, t) -> h :: t ) |
| 442 |
i |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
let temp_path = Filename.get_temp_dir_name () |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
let strings_of_file filename = |
| 453 |
let fd = |
| 454 |
try open_in filename |
| 455 |
with Sys_error _ -> failwith ("strings_of_file: can't open " ^ filename) |
| 456 |
in |
| 457 |
let rec suck_lines acc = |
| 458 |
try |
| 459 |
let l = input_line fd in |
| 460 |
suck_lines (l :: acc) |
| 461 |
with End_of_file -> List.rev acc |
| 462 |
in |
| 463 |
let data = suck_lines [] in |
| 464 |
close_in fd; data |
| 465 |
|
| 466 |
let string_of_file filename = String.concat "\n" (strings_of_file filename) |
| 467 |
|
| 468 |
let file_of_string filename s = |
| 469 |
let fd = open_out filename in |
| 470 |
output_string fd s; close_out fd |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
let rec deepen f n = |
| 477 |
try |
| 478 |
|
| 479 |
|
| 480 |
f n |
| 481 |
with Failure _ -> deepen f (n + 1) |
| 482 |
|
| 483 |
exception TooDeep |
| 484 |
|
| 485 |
let deepen_until limit f n = |
| 486 |
match compare limit 0 with |
| 487 |
| 0 -> raise TooDeep |
| 488 |
| -1 -> deepen f n |
| 489 |
| _ -> |
| 490 |
let rec d_until f n = |
| 491 |
try |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
f n |
| 496 |
with Failure x -> |
| 497 |
|
| 498 |
if n = limit then raise TooDeep else d_until f (n + 1) |
| 499 |
in |
| 500 |
d_until f n |