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