|
1 (* Auxiliary functions *) |
|
2 |
|
3 exception Assertion of string; |
|
4 |
|
5 val DEBUG = ref true; |
|
6 fun TRACE s = if !DEBUG then print s else (); |
|
7 |
|
8 fun id x = x; |
|
9 fun K x y = x; |
|
10 |
|
11 exception Noassoc; |
|
12 fun assoc a [] = raise Noassoc |
|
13 | assoc a ((x, y)::t) = if a = x then y else assoc a t; |
|
14 fun assoc_exists a [] = false |
|
15 | assoc_exists a ((x, y)::t) = if a = x then true else assoc_exists a t; |
|
16 fun assoc_update (a, b) [] = raise Noassoc |
|
17 | assoc_update (a, b) ((x, y)::t) |
|
18 = if a = x then (a, b)::t else (x, y)::(assoc_update (a, b) t) |
|
19 fun assoc_inv a [] = raise Noassoc |
|
20 | assoc_inv a ((x, y)::t) = if a = y then x else assoc a t; |
|
21 fun assoc_inv_exists a [] = false |
|
22 | assoc_inv_exists a ((x, y)::t) = if a = y then true else assoc_inv_exists a t; |
|
23 |
|
24 fun is_mem x [] = false |
|
25 | is_mem x (h::t) = (x = h) orelse is_mem x t; |
|
26 fun elt 0 (h::t) = h |
|
27 | elt n (h::t) = elt (n - 1) t |
|
28 | elt n l = raise (Assertion "elt: out of range"); |
|
29 fun remove_elt _ [] = raise (Assertion "remove_elt: out of range") |
|
30 | remove_elt 0 (h::t) = t |
|
31 | remove_elt n (h::t) = h::(remove_elt (n - 1) t); |
|
32 fun elt_num x [] = raise (Assertion "elt_num: not in list") |
|
33 | elt_num x (h::t) = if h = x then 0 else 1 + elt_num x t; |
|
34 fun set_add x l = if is_mem x l then l else x::l; |
|
35 |
|
36 fun iter f a [] = a |
|
37 | iter f a (h::t) = f h (iter f a t); |
|
38 |