author | wenzelm |
Wed, 20 Dec 2023 14:26:18 +0100 | |
changeset 79320 | bbac3e8a5070 |
parent 79317 | 788921b906e1 |
child 79380 | b9d80d5aca8e |
permissions | -rw-r--r-- |
74270 | 1 |
(* Title: Pure/term_items.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Scalable collections of term items: |
|
5 |
- table: e.g. for instantiation |
|
6 |
- set with order of addition, e.g. occurrences within term |
|
7 |
*) |
|
8 |
||
9 |
signature TERM_ITEMS = |
|
10 |
sig |
|
77828 | 11 |
structure Key: KEY |
74270 | 12 |
type key |
79317 | 13 |
exception DUP of key |
74270 | 14 |
type 'a table |
15 |
val empty: 'a table |
|
16 |
val build: ('a table -> 'a table) -> 'a table |
|
17 |
val size: 'a table -> int |
|
18 |
val is_empty: 'a table -> bool |
|
19 |
val map: (key -> 'a -> 'b) -> 'a table -> 'b table |
|
20 |
val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
|
21 |
val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
|
22 |
val dest: 'a table -> (key * 'a) list |
|
23 |
val keys: 'a table -> key list |
|
24 |
val exists: (key * 'a -> bool) -> 'a table -> bool |
|
25 |
val forall: (key * 'a -> bool) -> 'a table -> bool |
|
26 |
val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option |
|
27 |
val lookup: 'a table -> key -> 'a option |
|
28 |
val defined: 'a table -> key -> bool |
|
79121 | 29 |
val update: key * 'a -> 'a table -> 'a table |
30 |
val remove: key -> 'a table -> 'a table |
|
79320
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents:
79317
diff
changeset
|
31 |
val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table (*exception DUP*) |
74270 | 32 |
val add: key * 'a -> 'a table -> 'a table |
33 |
val make: (key * 'a) list -> 'a table |
|
77878 | 34 |
val make1: key * 'a -> 'a table |
35 |
val make2: key * 'a -> key * 'a -> 'a table |
|
36 |
val make3: key * 'a -> key * 'a -> key * 'a -> 'a table |
|
79199 | 37 |
val unsynchronized_cache: (key -> 'a) -> key -> 'a |
74270 | 38 |
type set = int table |
39 |
val add_set: key -> set -> set |
|
40 |
val make_set: key list -> set |
|
77878 | 41 |
val make1_set: key -> set |
42 |
val make2_set: key -> key -> set |
|
43 |
val make3_set: key -> key -> key -> set |
|
74270 | 44 |
val list_set: set -> key list |
45 |
val list_set_rev: set -> key list |
|
46 |
val subset: set * set -> bool |
|
47 |
val eq_set: set * set -> bool |
|
48 |
end; |
|
49 |
||
50 |
functor Term_Items(Key: KEY): TERM_ITEMS = |
|
51 |
struct |
|
52 |
||
77828 | 53 |
(* keys *) |
54 |
||
55 |
structure Key = Key; |
|
56 |
type key = Key.key; |
|
57 |
||
58 |
||
74270 | 59 |
(* table with length *) |
60 |
||
61 |
structure Table = Table(Key); |
|
79317 | 62 |
exception DUP = Table.DUP; |
74270 | 63 |
|
77828 | 64 |
datatype 'a table = Table of 'a Table.table; |
74270 | 65 |
|
77828 | 66 |
val empty = Table Table.empty; |
74270 | 67 |
fun build (f: 'a table -> 'a table) = f empty; |
77828 | 68 |
fun is_empty (Table tab) = Table.is_empty tab; |
74270 | 69 |
|
77828 | 70 |
fun size (Table tab) = Table.size tab; |
71 |
fun dest (Table tab) = Table.dest tab; |
|
72 |
fun keys (Table tab) = Table.keys tab; |
|
73 |
fun exists pred (Table tab) = Table.exists pred tab; |
|
74 |
fun forall pred (Table tab) = Table.forall pred tab; |
|
75 |
fun get_first get (Table tab) = Table.get_first get tab; |
|
76 |
fun lookup (Table tab) = Table.lookup tab; |
|
77 |
fun defined (Table tab) = Table.defined tab; |
|
74270 | 78 |
|
79121 | 79 |
fun update e (Table tab) = Table (Table.update e tab); |
80 |
fun remove x (Table tab) = Table (Table.delete_safe x tab); |
|
79320
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents:
79317
diff
changeset
|
81 |
fun insert eq e (Table tab) = Table (Table.insert eq e tab); |
79121 | 82 |
|
77828 | 83 |
fun add entry (Table tab) = Table (Table.default entry tab); |
77878 | 84 |
fun make es = build (fold add es); |
85 |
fun make1 e = build (add e); |
|
86 |
fun make2 e1 e2 = build (add e1 #> add e2); |
|
87 |
fun make3 e1 e2 e3 = build (add e1 #> add e2 #> add e3); |
|
74270 | 88 |
|
79199 | 89 |
val unsynchronized_cache = Table.unsynchronized_cache; |
90 |
||
74270 | 91 |
|
92 |
(* set with order of addition *) |
|
93 |
||
94 |
type set = int table; |
|
95 |
||
77828 | 96 |
fun add_set x (Table tab) = |
97 |
Table (if Table.defined tab x then tab else Table.update_new (x, Table.size tab + 1) tab); |
|
74270 | 98 |
|
99 |
fun make_set xs = build (fold add_set xs); |
|
77878 | 100 |
fun make1_set e = build (add_set e); |
101 |
fun make2_set e1 e2 = build (add_set e1 #> add_set e2); |
|
102 |
fun make3_set e1 e2 e3 = build (add_set e1 #> add_set e2 #> add_set e3); |
|
74270 | 103 |
|
104 |
fun subset (A: set, B: set) = forall (defined B o #1) A; |
|
77835 | 105 |
fun eq_set (A: set, B: set) = pointer_eq (A, B) orelse size A = size B andalso subset (A, B); |
74270 | 106 |
|
77828 | 107 |
fun list_set_ord ord (Table tab) = tab |> Table.dest |> sort (ord o apply2 #2) |> map #1; |
74270 | 108 |
val list_set = list_set_ord int_ord; |
109 |
val list_set_rev = list_set_ord (rev_order o int_ord); |
|
110 |
||
77828 | 111 |
fun map f (Table tab) = Table (Table.map f tab); |
112 |
fun fold f (Table tab) = Table.fold f tab; |
|
113 |
fun fold_rev f (Table tab) = Table.fold_rev f tab; |
|
74270 | 114 |
|
115 |
end; |
|
116 |
||
117 |
||
118 |
structure TFrees: |
|
119 |
sig |
|
120 |
include TERM_ITEMS |
|
121 |
val add_tfreesT: typ -> set -> set |
|
122 |
val add_tfrees: term -> set -> set |
|
74278 | 123 |
val add_tfreesT_unless: (string * sort -> bool) -> typ -> set -> set |
124 |
val add_tfrees_unless: (string * sort -> bool) -> term -> set -> set |
|
74270 | 125 |
end = |
126 |
struct |
|
127 |
||
128 |
structure Items = Term_Items |
|
129 |
( |
|
130 |
type key = string * sort; |
|
131 |
val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.sort_ord); |
|
132 |
); |
|
133 |
open Items; |
|
134 |
||
135 |
val add_tfreesT = fold_atyps (fn TFree v => add_set v | _ => I); |
|
136 |
val add_tfrees = fold_types add_tfreesT; |
|
137 |
||
74278 | 138 |
fun add_tfreesT_unless pred = Term.fold_atyps (fn TFree v => not (pred v) ? add_set v | _ => I); |
139 |
fun add_tfrees_unless pred = fold_types (add_tfreesT_unless pred); |
|
140 |
||
74270 | 141 |
end; |
142 |
||
143 |
||
144 |
structure TVars: |
|
145 |
sig |
|
146 |
include TERM_ITEMS |
|
147 |
val add_tvarsT: typ -> set -> set |
|
148 |
val add_tvars: term -> set -> set |
|
149 |
end = |
|
150 |
struct |
|
151 |
||
152 |
structure Term_Items = Term_Items( |
|
153 |
type key = indexname * sort; |
|
154 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.sort_ord); |
|
155 |
); |
|
156 |
open Term_Items; |
|
157 |
||
158 |
val add_tvarsT = fold_atyps (fn TVar v => add_set v | _ => I); |
|
159 |
val add_tvars = fold_types add_tvarsT; |
|
160 |
||
161 |
end; |
|
162 |
||
163 |
||
164 |
structure Frees: |
|
165 |
sig |
|
166 |
include TERM_ITEMS |
|
167 |
val add_frees: term -> set -> set |
|
168 |
end = |
|
169 |
struct |
|
170 |
||
171 |
structure Term_Items = Term_Items |
|
172 |
( |
|
173 |
type key = string * typ; |
|
174 |
val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.typ_ord); |
|
175 |
); |
|
176 |
open Term_Items; |
|
177 |
||
178 |
val add_frees = fold_aterms (fn Free v => add_set v | _ => I); |
|
179 |
||
180 |
end; |
|
181 |
||
182 |
||
183 |
structure Vars: |
|
184 |
sig |
|
185 |
include TERM_ITEMS |
|
186 |
val add_vars: term -> set -> set |
|
187 |
end = |
|
188 |
struct |
|
189 |
||
190 |
structure Term_Items = Term_Items |
|
191 |
( |
|
192 |
type key = indexname * typ; |
|
79121 | 193 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.typ_ord); |
74270 | 194 |
); |
195 |
open Term_Items; |
|
196 |
||
197 |
val add_vars = fold_aterms (fn Var v => add_set v | _ => I); |
|
198 |
||
199 |
end; |
|
200 |
||
201 |
||
202 |
structure Names: |
|
203 |
sig |
|
204 |
include TERM_ITEMS |
|
205 |
val add_tfree_namesT: typ -> set -> set |
|
206 |
val add_tfree_names: term -> set -> set |
|
207 |
val add_free_names: term -> set -> set |
|
208 |
end = |
|
209 |
struct |
|
210 |
||
211 |
structure Term_Items = Term_Items |
|
212 |
( |
|
213 |
type key = string; |
|
79121 | 214 |
val ord = fast_string_ord; |
74270 | 215 |
); |
216 |
open Term_Items; |
|
217 |
||
218 |
val add_tfree_namesT = fold_atyps (fn TFree (a, _) => add_set a | _ => I); |
|
219 |
val add_tfree_names = fold_types add_tfree_namesT; |
|
220 |
val add_free_names = fold_aterms (fn Free (x, _) => add_set x | _ => I); |
|
221 |
||
222 |
end; |