17152
|
1 |
(* Title: Pure/General/alist.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Association lists -- lists of (key, value) pairs with unique keys.
|
|
6 |
A light-weight representation of finite mappings;
|
|
7 |
see also Pure/General/table.ML for a more scalable implementation.
|
|
8 |
*)
|
|
9 |
|
|
10 |
signature ALIST =
|
|
11 |
sig
|
17766
|
12 |
exception DUP
|
17152
|
13 |
val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option
|
|
14 |
val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool
|
|
15 |
val update: ('a * 'a -> bool) -> ('a * 'b)
|
|
16 |
-> ('a * 'b) list -> ('a * 'b) list
|
|
17 |
val default: ('a * 'a -> bool) -> ('a * 'b)
|
|
18 |
-> ('a * 'b) list -> ('a * 'b) list
|
|
19 |
val delete: ('a * 'b -> bool) -> 'a
|
|
20 |
-> ('b * 'c) list -> ('b * 'c) list
|
|
21 |
val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c)
|
|
22 |
-> ('b * 'c) list -> ('b * 'c) list
|
17487
|
23 |
val make: ('a -> 'b) -> 'a list -> ('a * 'b) list
|
17766
|
24 |
val merge: ('a * 'a -> bool) -> ('b * 'b -> bool)
|
|
25 |
-> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception DUP*)
|
17497
|
26 |
val find: ('a * 'b -> bool) -> ('c * 'b) list -> 'a -> 'c list
|
17152
|
27 |
end;
|
|
28 |
|
|
29 |
structure AList: ALIST =
|
|
30 |
struct
|
|
31 |
|
17766
|
32 |
fun find_index eq xs key =
|
|
33 |
let
|
|
34 |
fun find [] _ = 0
|
|
35 |
| find ((key', value)::xs) i =
|
|
36 |
if eq (key, key')
|
|
37 |
then i
|
|
38 |
else find xs (i+1);
|
|
39 |
in find xs 1 end;
|
|
40 |
|
17152
|
41 |
fun lookup _ [] _ = NONE
|
|
42 |
| lookup eq ((key, value)::xs) key' =
|
|
43 |
if eq (key', key) then SOME value
|
|
44 |
else lookup eq xs key';
|
|
45 |
|
|
46 |
fun defined _ [] _ = false
|
|
47 |
| defined eq ((key, value)::xs) key' =
|
|
48 |
eq (key', key) orelse defined eq xs key';
|
|
49 |
|
17766
|
50 |
fun update eq (x as (key, value)) xs =
|
17152
|
51 |
let
|
17766
|
52 |
val i = find_index eq xs key;
|
|
53 |
fun upd 1 (_::xs) = (x::xs)
|
|
54 |
| upd i (x::xs) = x :: upd (i-1) xs;
|
|
55 |
in if i = 0 then x::xs else upd i xs end;
|
17152
|
56 |
|
|
57 |
fun default eq (key, value) xs =
|
|
58 |
if defined eq xs key then xs else (key, value)::xs;
|
|
59 |
|
|
60 |
fun delete eq key xs =
|
|
61 |
let
|
17766
|
62 |
val i = find_index eq xs key;
|
|
63 |
fun del 1 (_::xs) = xs
|
|
64 |
| del i (x::xs) = x :: del (i-1) xs;
|
|
65 |
in if i = 0 then xs else del i xs end;
|
|
66 |
|
|
67 |
fun map_entry eq key f xs =
|
|
68 |
let
|
|
69 |
val i = find_index eq xs key;
|
|
70 |
fun mapp 1 ((x as (key, value))::xs) = (key, f value) :: xs
|
|
71 |
| mapp i (x::xs) = x :: mapp (i-1) xs;
|
|
72 |
in if i = 0 then xs else mapp i xs end;
|
17152
|
73 |
|
17766
|
74 |
exception DUP;
|
|
75 |
|
|
76 |
fun merge eq_key eq_val (xs, ys) =
|
17191
|
77 |
let
|
17766
|
78 |
fun add (x as (key, value)) ys =
|
|
79 |
case lookup eq_key ys key
|
|
80 |
of NONE => update eq_key x ys
|
|
81 |
| SOME value' =>
|
|
82 |
if eq_val (value, value')
|
|
83 |
then ys
|
|
84 |
else raise DUP;
|
|
85 |
in
|
|
86 |
fold_rev add xs ys
|
|
87 |
end;
|
17191
|
88 |
|
17487
|
89 |
fun make keyfun =
|
|
90 |
let fun keypair x = (x, keyfun x)
|
|
91 |
in map keypair end;
|
|
92 |
|
17497
|
93 |
fun find eq [] _ = []
|
|
94 |
| find eq ((key, value) :: xs) value' =
|
|
95 |
let
|
17766
|
96 |
val values = find eq xs value';
|
17497
|
97 |
in if eq (value', value) then key :: values else values end;
|
|
98 |
|
17152
|
99 |
end;
|