| author | wenzelm | 
| Tue, 10 Dec 2024 21:06:04 +0100 | |
| changeset 81572 | 693a95492008 | 
| parent 81540 | 58073f3d748a | 
| child 82897 | 9225b889f68a | 
| 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: 
79317diff
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 | |
| 80583 | 37 |   type 'a cache_ops = {apply: key -> 'a, reset: unit -> unit, size: unit -> int};
 | 
| 80579 | 38 | val unsynchronized_cache: (key -> 'a) -> 'a cache_ops | 
| 81540 | 39 | val apply_unsynchronized_cache: (key -> 'a) -> key -> 'a | 
| 74270 | 40 | type set = int table | 
| 41 | val add_set: key -> set -> set | |
| 42 | val make_set: key list -> set | |
| 77878 | 43 | val make1_set: key -> set | 
| 44 | val make2_set: key -> key -> set | |
| 45 | val make3_set: key -> key -> key -> set | |
| 74270 | 46 | val list_set: set -> key list | 
| 47 | val list_set_rev: set -> key list | |
| 48 | val subset: set * set -> bool | |
| 49 | val eq_set: set * set -> bool | |
| 50 | end; | |
| 51 | ||
| 52 | functor Term_Items(Key: KEY): TERM_ITEMS = | |
| 53 | struct | |
| 54 | ||
| 77828 | 55 | (* keys *) | 
| 56 | ||
| 57 | structure Key = Key; | |
| 58 | type key = Key.key; | |
| 59 | ||
| 60 | ||
| 74270 | 61 | (* table with length *) | 
| 62 | ||
| 63 | structure Table = Table(Key); | |
| 79317 | 64 | exception DUP = Table.DUP; | 
| 74270 | 65 | |
| 77828 | 66 | datatype 'a table = Table of 'a Table.table; | 
| 74270 | 67 | |
| 77828 | 68 | val empty = Table Table.empty; | 
| 74270 | 69 | fun build (f: 'a table -> 'a table) = f empty; | 
| 77828 | 70 | fun is_empty (Table tab) = Table.is_empty tab; | 
| 74270 | 71 | |
| 77828 | 72 | fun size (Table tab) = Table.size tab; | 
| 73 | fun dest (Table tab) = Table.dest tab; | |
| 74 | fun keys (Table tab) = Table.keys tab; | |
| 75 | fun exists pred (Table tab) = Table.exists pred tab; | |
| 76 | fun forall pred (Table tab) = Table.forall pred tab; | |
| 77 | fun get_first get (Table tab) = Table.get_first get tab; | |
| 78 | fun lookup (Table tab) = Table.lookup tab; | |
| 79 | fun defined (Table tab) = Table.defined tab; | |
| 74270 | 80 | |
| 79121 | 81 | fun update e (Table tab) = Table (Table.update e tab); | 
| 82 | 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: 
79317diff
changeset | 83 | fun insert eq e (Table tab) = Table (Table.insert eq e tab); | 
| 79121 | 84 | |
| 77828 | 85 | fun add entry (Table tab) = Table (Table.default entry tab); | 
| 77878 | 86 | fun make es = build (fold add es); | 
| 87 | fun make1 e = build (add e); | |
| 88 | fun make2 e1 e2 = build (add e1 #> add e2); | |
| 89 | fun make3 e1 e2 e3 = build (add e1 #> add e2 #> add e3); | |
| 74270 | 90 | |
| 80579 | 91 | type 'a cache_ops = 'a Table.cache_ops; | 
| 79199 | 92 | val unsynchronized_cache = Table.unsynchronized_cache; | 
| 81540 | 93 | val apply_unsynchronized_cache = Table.apply_unsynchronized_cache; | 
| 79199 | 94 | |
| 74270 | 95 | |
| 96 | (* set with order of addition *) | |
| 97 | ||
| 98 | type set = int table; | |
| 99 | ||
| 77828 | 100 | fun add_set x (Table tab) = | 
| 101 | Table (if Table.defined tab x then tab else Table.update_new (x, Table.size tab + 1) tab); | |
| 74270 | 102 | |
| 103 | fun make_set xs = build (fold add_set xs); | |
| 77878 | 104 | fun make1_set e = build (add_set e); | 
| 105 | fun make2_set e1 e2 = build (add_set e1 #> add_set e2); | |
| 106 | fun make3_set e1 e2 e3 = build (add_set e1 #> add_set e2 #> add_set e3); | |
| 74270 | 107 | |
| 108 | fun subset (A: set, B: set) = forall (defined B o #1) A; | |
| 77835 | 109 | fun eq_set (A: set, B: set) = pointer_eq (A, B) orelse size A = size B andalso subset (A, B); | 
| 74270 | 110 | |
| 77828 | 111 | fun list_set_ord ord (Table tab) = tab |> Table.dest |> sort (ord o apply2 #2) |> map #1; | 
| 74270 | 112 | val list_set = list_set_ord int_ord; | 
| 113 | val list_set_rev = list_set_ord (rev_order o int_ord); | |
| 114 | ||
| 77828 | 115 | fun map f (Table tab) = Table (Table.map f tab); | 
| 116 | fun fold f (Table tab) = Table.fold f tab; | |
| 117 | fun fold_rev f (Table tab) = Table.fold_rev f tab; | |
| 74270 | 118 | |
| 119 | end; | |
| 120 | ||
| 121 | ||
| 122 | structure TFrees: | |
| 123 | sig | |
| 124 | include TERM_ITEMS | |
| 125 | val add_tfreesT: typ -> set -> set | |
| 126 | val add_tfrees: term -> set -> set | |
| 74278 | 127 | val add_tfreesT_unless: (string * sort -> bool) -> typ -> set -> set | 
| 128 | val add_tfrees_unless: (string * sort -> bool) -> term -> set -> set | |
| 74270 | 129 | end = | 
| 130 | struct | |
| 131 | ||
| 132 | structure Items = Term_Items | |
| 133 | ( | |
| 134 | type key = string * sort; | |
| 135 | val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.sort_ord); | |
| 136 | ); | |
| 137 | open Items; | |
| 138 | ||
| 139 | val add_tfreesT = fold_atyps (fn TFree v => add_set v | _ => I); | |
| 140 | val add_tfrees = fold_types add_tfreesT; | |
| 141 | ||
| 74278 | 142 | fun add_tfreesT_unless pred = Term.fold_atyps (fn TFree v => not (pred v) ? add_set v | _ => I); | 
| 143 | fun add_tfrees_unless pred = fold_types (add_tfreesT_unless pred); | |
| 144 | ||
| 74270 | 145 | end; | 
| 146 | ||
| 147 | ||
| 148 | structure TVars: | |
| 149 | sig | |
| 150 | include TERM_ITEMS | |
| 151 | val add_tvarsT: typ -> set -> set | |
| 152 | val add_tvars: term -> set -> set | |
| 153 | end = | |
| 154 | struct | |
| 155 | ||
| 156 | structure Term_Items = Term_Items( | |
| 157 | type key = indexname * sort; | |
| 158 | val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.sort_ord); | |
| 159 | ); | |
| 160 | open Term_Items; | |
| 161 | ||
| 162 | val add_tvarsT = fold_atyps (fn TVar v => add_set v | _ => I); | |
| 163 | val add_tvars = fold_types add_tvarsT; | |
| 164 | ||
| 165 | end; | |
| 166 | ||
| 167 | ||
| 168 | structure Frees: | |
| 169 | sig | |
| 170 | include TERM_ITEMS | |
| 171 | val add_frees: term -> set -> set | |
| 172 | end = | |
| 173 | struct | |
| 174 | ||
| 175 | structure Term_Items = Term_Items | |
| 176 | ( | |
| 177 | type key = string * typ; | |
| 178 | val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.typ_ord); | |
| 179 | ); | |
| 180 | open Term_Items; | |
| 181 | ||
| 182 | val add_frees = fold_aterms (fn Free v => add_set v | _ => I); | |
| 183 | ||
| 184 | end; | |
| 185 | ||
| 186 | ||
| 187 | structure Vars: | |
| 188 | sig | |
| 189 | include TERM_ITEMS | |
| 190 | val add_vars: term -> set -> set | |
| 191 | end = | |
| 192 | struct | |
| 193 | ||
| 194 | structure Term_Items = Term_Items | |
| 195 | ( | |
| 196 | type key = indexname * typ; | |
| 79121 | 197 | val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.typ_ord); | 
| 74270 | 198 | ); | 
| 199 | open Term_Items; | |
| 200 | ||
| 201 | val add_vars = fold_aterms (fn Var v => add_set v | _ => I); | |
| 202 | ||
| 203 | end; | |
| 204 | ||
| 205 | ||
| 206 | structure Names: | |
| 207 | sig | |
| 208 | include TERM_ITEMS | |
| 209 | val add_tfree_namesT: typ -> set -> set | |
| 210 | val add_tfree_names: term -> set -> set | |
| 211 | val add_free_names: term -> set -> set | |
| 212 | end = | |
| 213 | struct | |
| 214 | ||
| 215 | structure Term_Items = Term_Items | |
| 216 | ( | |
| 217 | type key = string; | |
| 79121 | 218 | val ord = fast_string_ord; | 
| 74270 | 219 | ); | 
| 220 | open Term_Items; | |
| 221 | ||
| 222 | val add_tfree_namesT = fold_atyps (fn TFree (a, _) => add_set a | _ => I); | |
| 223 | val add_tfree_names = fold_types add_tfree_namesT; | |
| 224 | val add_free_names = fold_aterms (fn Free (x, _) => add_set x | _ => I); | |
| 225 | ||
| 226 | end; | |
| 79380 | 227 | |
| 228 | ||
| 81259 | 229 | structure Indexnames: TERM_ITEMS = Term_Items | 
| 230 | ( | |
| 231 | type key = indexname; | |
| 232 | val ord = Term_Ord.fast_indexname_ord; | |
| 233 | ); | |
| 234 | ||
| 235 | ||
| 79413 | 236 | structure Types: | 
| 237 | sig | |
| 238 | include TERM_ITEMS | |
| 239 | val add_atyps: term -> set -> set | |
| 240 | end = | |
| 241 | struct | |
| 242 | ||
| 243 | structure Term_Items = Term_Items(type key = typ val ord = Term_Ord.typ_ord); | |
| 244 | open Term_Items; | |
| 245 | ||
| 246 | val add_atyps = (fold_types o fold_atyps) add_set; | |
| 247 | ||
| 248 | end; |