author | paulson |
Fri, 06 Aug 1999 11:07:25 +0200 | |
changeset 7182 | 090723b5024d |
parent 6327 | c6abb5884fed |
child 7410 | 7369a35fb3c2 |
permissions | -rw-r--r-- |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
1 |
(* Title: Pure/Thy/thm_database.ML |
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
2 |
ID: $Id$ |
4023 | 3 |
Author: Carsten Clasohm and Tobias Nipkow and Markus Wenzel, TU Muenchen |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
4 |
|
4023 | 5 |
User level interface to thm database (see also Pure/pure_thy.ML). |
6 |
*) |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
7 |
|
6204 | 8 |
signature BASIC_THM_DATABASE = |
4023 | 9 |
sig |
10 |
val store_thm: string * thm -> thm |
|
11 |
val qed_thm: thm option ref |
|
12 |
val bind_thm: string * thm -> unit |
|
13 |
val qed: string -> unit |
|
14 |
val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit |
|
15 |
val qed_goalw: string -> theory -> thm list -> string -> (thm list -> tactic list) -> unit |
|
16 |
(*these peek at the proof state!*) |
|
4287 | 17 |
val thms_containing: xstring list -> (string * thm) list |
4023 | 18 |
val findI: int -> (string * thm) list |
19 |
val findEs: int -> (string * thm) list |
|
20 |
val findE: int -> int -> (string * thm) list |
|
21 |
end; |
|
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
22 |
|
6204 | 23 |
signature THM_DATABASE = |
24 |
sig |
|
25 |
include BASIC_THM_DATABASE |
|
26 |
val ml_store_thm: string * thm -> unit |
|
27 |
val is_ml_identifier: string -> bool |
|
28 |
end; |
|
29 |
||
3627 | 30 |
structure ThmDatabase: THM_DATABASE = |
1221 | 31 |
struct |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
32 |
|
4023 | 33 |
(** store theorems **) |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
34 |
|
4023 | 35 |
(* store in theory and generate HTML *) |
1136
3910c96551d1
fixed bug in thms_containing; changed error/warning messages;
clasohm
parents:
1134
diff
changeset
|
36 |
|
4023 | 37 |
fun store_thm (name, thm) = |
6204 | 38 |
let val thm' = PureThy.smart_store_thm (name, thm) |
6327 | 39 |
in Present.theorem name thm'; thm' end; |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
40 |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
41 |
|
4023 | 42 |
(* store on ML toplevel *) |
43 |
||
44 |
val qed_thm: thm option ref = ref None; |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
45 |
|
4023 | 46 |
val ml_reserved = |
47 |
["abstype", "and", "andalso", "as", "case", "do", "datatype", "else", |
|
48 |
"end", "exception", "fn", "fun", "handle", "if", "in", "infix", |
|
49 |
"infixr", "let", "local", "nonfix", "of", "op", "open", "orelse", |
|
50 |
"raise", "rec", "then", "type", "val", "with", "withtype", "while", |
|
51 |
"eqtype", "functor", "include", "sharing", "sig", "signature", |
|
52 |
"struct", "structure", "where"]; |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
53 |
|
4023 | 54 |
fun is_ml_identifier name = |
55 |
Syntax.is_identifier name andalso not (name mem ml_reserved); |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
56 |
|
4023 | 57 |
fun ml_store_thm (name, thm) = |
58 |
let val thm' = store_thm (name, thm) in |
|
59 |
if is_ml_identifier name then |
|
60 |
(qed_thm := Some thm'; |
|
5090 | 61 |
use_text true ("val " ^ name ^ " = the (! ThmDatabase.qed_thm);")) |
4023 | 62 |
else warning ("Cannot bind thm " ^ quote name ^ " as ML value") |
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
63 |
end; |
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
64 |
|
4023 | 65 |
fun bind_thm (name, thm) = ml_store_thm (name, standard thm); |
66 |
fun qed name = ml_store_thm (name, result ()); |
|
67 |
fun qed_goal name thy goal tacsf = ml_store_thm (name, prove_goal thy goal tacsf); |
|
68 |
fun qed_goalw name thy rews goal tacsf = ml_store_thm (name, prove_goalw thy rews goal tacsf); |
|
69 |
||
70 |
||
71 |
||
4037 | 72 |
(** retrieve theorems **) |
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
73 |
|
4037 | 74 |
(*get theorems that contain all of given constants*) |
4023 | 75 |
fun thms_containing raw_consts = |
76 |
let |
|
77 |
val sign = sign_of_thm (topthm ()); |
|
78 |
val consts = map (Sign.intern_const sign) raw_consts; |
|
79 |
val thy = ThyInfo.theory_of_sign sign; |
|
7182
090723b5024d
now catches exn THEORY and prints an error message
paulson
parents:
6327
diff
changeset
|
80 |
in PureThy.thms_containing thy consts end |
090723b5024d
now catches exn THEORY and prints an error message
paulson
parents:
6327
diff
changeset
|
81 |
handle THEORY (msg,_) => error msg; |
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
82 |
|
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
83 |
|
4023 | 84 |
(*top_const: main constant, ignoring Trueprop*) |
85 |
fun top_const (_ $ t) = (case head_of t of Const (c, _) => Some c | _ => None) |
|
86 |
| top_const _ = None; |
|
1136
3910c96551d1
fixed bug in thms_containing; changed error/warning messages;
clasohm
parents:
1134
diff
changeset
|
87 |
|
1221 | 88 |
val intro_const = top_const o concl_of; |
89 |
||
4023 | 90 |
fun elim_const thm = case prems_of thm of [] => None | p::_ => top_const p; |
1221 | 91 |
|
92 |
(* In case faster access is necessary, the thm db should provide special |
|
93 |
functions |
|
94 |
||
95 |
index_intros, index_elims: string -> (string * thm) list |
|
96 |
||
97 |
where thm [| A1 ; ...; An |] ==> B is returned by |
|
98 |
- index_intros c if B is of the form c t1 ... tn |
|
99 |
- index_elims c if A1 is of the form c t1 ... tn |
|
100 |
*) |
|
101 |
||
102 |
(* could be provided by thm db *) |
|
103 |
fun index_intros c = |
|
104 |
let fun topc(_,thm) = intro_const thm = Some(c); |
|
105 |
val named_thms = thms_containing [c] |
|
106 |
in filter topc named_thms end; |
|
107 |
||
108 |
(* could be provided by thm db *) |
|
109 |
fun index_elims c = |
|
110 |
let fun topc(_,thm) = elim_const thm = Some(c); |
|
111 |
val named_thms = thms_containing [c] |
|
112 |
in filter topc named_thms end; |
|
113 |
||
114 |
(*assume that parameters have unique names*) |
|
115 |
fun goal_params i = |
|
116 |
let val gi = getgoal i |
|
117 |
val frees = map Free (Logic.strip_params gi) |
|
118 |
in (gi,frees) end; |
|
119 |
||
120 |
fun concl_of_goal i = |
|
121 |
let val (gi,frees) = goal_params i |
|
122 |
val B = Logic.strip_assums_concl gi |
|
123 |
in subst_bounds(frees,B) end; |
|
124 |
||
125 |
fun prems_of_goal i = |
|
126 |
let val (gi,frees) = goal_params i |
|
127 |
val As = Logic.strip_assums_hyp gi |
|
128 |
in map (fn A => subst_bounds(frees,A)) As end; |
|
129 |
||
5155 | 130 |
(*returns all those named_thms whose subterm extracted by extract can be |
131 |
instantiated to obj; the list is sorted according to the number of premises |
|
132 |
and the size of the required substitution.*) |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
133 |
fun select_match(obj, signobj, named_thms, extract) = |
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
134 |
let fun matches(prop, tsig) = |
1221 | 135 |
case extract prop of |
136 |
None => false |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
137 |
| Some pat => Pattern.matches tsig (pat, obj); |
1221 | 138 |
|
1654
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
139 |
fun substsize(prop, tsig) = |
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
140 |
let val Some pat = extract prop |
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
141 |
val (_,subst) = Pattern.match tsig (pat,obj) |
2150 | 142 |
in foldl op+ |
143 |
(0, map (fn (_,t) => size_of_term t) subst) |
|
144 |
end |
|
1654
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
145 |
|
4546 | 146 |
fun thm_ord ((p0,s0,_),(p1,s1,_)) = |
147 |
prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0,s0),(p1,s1)); |
|
1654
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
148 |
|
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
149 |
fun select((p as (_,thm))::named_thms, sels) = |
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
150 |
let val {prop, sign, ...} = rep_thm thm |
1221 | 151 |
in select(named_thms, |
1262
8f40ff1299d8
added removal of theorems if theory is to be reloaded; changed functions for
clasohm
parents:
1245
diff
changeset
|
152 |
if Sign.subsig(sign, signobj) andalso |
1230 | 153 |
matches(prop,#tsig(Sign.rep_sg signobj)) |
1654
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
154 |
then |
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
155 |
(nprems_of thm,substsize(prop,#tsig(Sign.rep_sg signobj)),p)::sels |
faa643c33ee6
select_match now sorts list of matching theorems according to the
berghofe
parents:
1580
diff
changeset
|
156 |
else sels) |
1136
3910c96551d1
fixed bug in thms_containing; changed error/warning messages;
clasohm
parents:
1134
diff
changeset
|
157 |
end |
3910c96551d1
fixed bug in thms_containing; changed error/warning messages;
clasohm
parents:
1134
diff
changeset
|
158 |
| select([],sels) = sels |
1221 | 159 |
|
4546 | 160 |
in map (fn (_,_,t) => t) (sort thm_ord (select(named_thms, []))) end; |
1221 | 161 |
|
162 |
fun find_matching(prop,sign,index,extract) = |
|
163 |
(case top_const prop of |
|
164 |
Some c => select_match(prop,sign,index c,extract) |
|
165 |
| _ => []); |
|
166 |
||
167 |
fun find_intros(prop,sign) = |
|
168 |
find_matching(prop,sign,index_intros,Some o Logic.strip_imp_concl); |
|
1136
3910c96551d1
fixed bug in thms_containing; changed error/warning messages;
clasohm
parents:
1134
diff
changeset
|
169 |
|
1221 | 170 |
fun find_elims sign prop = |
171 |
let fun major prop = case Logic.strip_imp_prems prop of |
|
172 |
[] => None | p::_ => Some p |
|
173 |
in find_matching(prop,sign,index_elims,major) end; |
|
174 |
||
175 |
fun findI i = find_intros(concl_of_goal i,#sign(rep_thm(topthm()))); |
|
176 |
||
177 |
fun findEs i = |
|
1230 | 178 |
let fun eq_nth((n1,th1),(n2,th2)) = n1=n2 andalso eq_thm(th1,th2); |
179 |
val sign = #sign(rep_thm(topthm())) |
|
180 |
val thmss = map (find_elims sign) (prems_of_goal i) |
|
181 |
in foldl (gen_union eq_nth) ([],thmss) end; |
|
1221 | 182 |
|
183 |
fun findE i j = |
|
184 |
let val sign = #sign(rep_thm(topthm())) |
|
185 |
in find_elims sign (nth_elem(j-1, prems_of_goal i)) end; |
|
186 |
||
3601
43c7912aac8d
Moved "qed" - functions etc. from thy_read.ML to thm_database.ML.
berghofe
parents:
2150
diff
changeset
|
187 |
|
1132
dfb29abcf3c2
added theorem database which contains axioms and theorems indexed by the
clasohm
parents:
diff
changeset
|
188 |
end; |
6204 | 189 |
|
190 |
||
191 |
structure BasicThmDatabase: BASIC_THM_DATABASE = ThmDatabase; |
|
192 |
open BasicThmDatabase; |