13270
|
1 |
(* Title: Pure/fact_index.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
16020
|
5 |
Facts indexed by consts or frees.
|
13270
|
6 |
*)
|
|
7 |
|
|
8 |
signature FACT_INDEX =
|
|
9 |
sig
|
16020
|
10 |
type spec
|
13270
|
11 |
type T
|
16020
|
12 |
val facts: T -> (string * thm list) list
|
18026
|
13 |
val could_unify: T -> term -> thm list
|
13270
|
14 |
val empty: T
|
18026
|
15 |
val add_global: (string * thm list) -> T -> T
|
19141
|
16 |
val add_local: bool -> (string -> bool) -> (string * thm list) -> T -> T
|
16020
|
17 |
val find: T -> spec -> (string * thm list) list
|
13270
|
18 |
end;
|
|
19 |
|
|
20 |
structure FactIndex: FACT_INDEX =
|
|
21 |
struct
|
|
22 |
|
16020
|
23 |
|
13270
|
24 |
(* indexing items *)
|
|
25 |
|
18026
|
26 |
type spec = string list * string list;
|
|
27 |
|
16874
|
28 |
val add_consts = fold_aterms
|
|
29 |
(fn Const (c, _) => if c = Term.dummy_patternN then I else insert (op =) c | _ => I);
|
13270
|
30 |
|
18026
|
31 |
fun add_frees known = fold_aterms
|
|
32 |
(fn Free (x, _) => if known x then insert (op =) x else I | _ => I);
|
16874
|
33 |
|
18026
|
34 |
fun index_term known t (cs, xs) = (add_consts t cs, add_frees known t xs);
|
13270
|
35 |
|
18026
|
36 |
fun index_thm known th idx =
|
16020
|
37 |
let val {tpairs, hyps, prop, ...} = Thm.rep_thm th in
|
|
38 |
idx
|
18026
|
39 |
|> index_term known prop
|
|
40 |
|> fold (index_term known) hyps
|
|
41 |
|> fold (fn (t, u) => index_term known t #> index_term known u) tpairs
|
16020
|
42 |
end;
|
13270
|
43 |
|
|
44 |
|
|
45 |
(* build index *)
|
|
46 |
|
18026
|
47 |
type fact = string * thm list;
|
|
48 |
|
|
49 |
datatype T = Index of
|
|
50 |
{facts: fact list,
|
|
51 |
consts: (serial * fact) list Symtab.table,
|
|
52 |
frees: (serial * fact) list Symtab.table,
|
|
53 |
props: thm Net.net};
|
13270
|
54 |
|
16020
|
55 |
fun facts (Index {facts, ...}) = facts;
|
18026
|
56 |
fun could_unify (Index {props, ...}) = Net.unify_term props;
|
16020
|
57 |
|
13270
|
58 |
val empty =
|
18026
|
59 |
Index {facts = [], consts = Symtab.empty, frees = Symtab.empty, props = Net.empty};
|
13270
|
60 |
|
19141
|
61 |
fun add do_props do_index known (fact as (_, ths)) (Index {facts, consts, frees, props}) =
|
13270
|
62 |
let
|
18026
|
63 |
val entry = (serial (), fact);
|
|
64 |
val (cs, xs) = fold (index_thm known) ths ([], []);
|
|
65 |
|
19141
|
66 |
val (facts', consts', frees') =
|
|
67 |
if do_index then
|
|
68 |
(fact :: facts,
|
|
69 |
consts |> fold (fn c => Symtab.update_list (c, entry)) cs,
|
|
70 |
frees |> fold (fn x => Symtab.update_list (x, entry)) xs)
|
|
71 |
else (facts, consts, frees);
|
|
72 |
val props' =
|
|
73 |
if do_props then props |> fold (fn th => Net.insert_term (K false) (Thm.prop_of th, th)) ths
|
|
74 |
else props;
|
18026
|
75 |
in Index {facts = facts', consts = consts', frees = frees', props = props'} end;
|
|
76 |
|
19141
|
77 |
val add_global = add false true (K false);
|
18026
|
78 |
val add_local = add true;
|
13270
|
79 |
|
|
80 |
|
18026
|
81 |
(* find by consts/frees *)
|
|
82 |
|
|
83 |
local
|
13270
|
84 |
|
16491
|
85 |
fun fact_ord ((i, _), (j, _)) = int_ord (j, i);
|
13270
|
86 |
|
|
87 |
fun intersects [xs] = xs
|
16491
|
88 |
| intersects xss =
|
|
89 |
if exists null xss then []
|
|
90 |
else fold (OrdList.inter fact_ord) (tl xss) (hd xss);
|
13270
|
91 |
|
18026
|
92 |
in
|
|
93 |
|
16020
|
94 |
fun find idx ([], []) = facts idx
|
|
95 |
| find (Index {consts, frees, ...}) (cs, xs) =
|
18931
|
96 |
(map (Symtab.lookup_list consts) cs @
|
|
97 |
map (Symtab.lookup_list frees) xs) |> intersects |> map #2;
|
13270
|
98 |
|
18026
|
99 |
end
|
|
100 |
|
13270
|
101 |
end;
|