author | wenzelm |
Thu, 11 Feb 1999 15:30:10 +0100 | |
changeset 6269 | dbb48b0744d3 |
parent 6089 | 4d2d5556b4f9 |
child 6368 | ba5e97a20b12 |
permissions | -rw-r--r-- |
250 | 1 |
(* Title: Pure/thm.ML |
0 | 2 |
ID: $Id$ |
250 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
4 |
Copyright 1994 University of Cambridge |
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
5 |
|
1160 | 6 |
The core of Isabelle's Meta Logic: certified types and terms, meta |
1529 | 7 |
theorems, meta rules (including resolution and simplification). |
0 | 8 |
*) |
9 |
||
6089 | 10 |
signature BASIC_THM = |
1503 | 11 |
sig |
1160 | 12 |
(*certified types*) |
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
13 |
type ctyp |
1238 | 14 |
val rep_ctyp : ctyp -> {sign: Sign.sg, T: typ} |
15 |
val typ_of : ctyp -> typ |
|
16 |
val ctyp_of : Sign.sg -> typ -> ctyp |
|
17 |
val read_ctyp : Sign.sg -> string -> ctyp |
|
1160 | 18 |
|
19 |
(*certified terms*) |
|
20 |
type cterm |
|
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
21 |
exception CTERM of string |
4270 | 22 |
val rep_cterm : cterm -> {sign: Sign.sg, t: term, T: typ, maxidx: int} |
4288 | 23 |
val crep_cterm : cterm -> {sign: Sign.sg, t: term, T: ctyp, maxidx: int} |
1238 | 24 |
val term_of : cterm -> term |
25 |
val cterm_of : Sign.sg -> term -> cterm |
|
2671 | 26 |
val ctyp_of_term : cterm -> ctyp |
1238 | 27 |
val read_cterm : Sign.sg -> string * typ -> cterm |
28 |
val cterm_fun : (term -> term) -> (cterm -> cterm) |
|
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
29 |
val dest_comb : cterm -> cterm * cterm |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
30 |
val dest_abs : cterm -> cterm * cterm |
1703
e22ad43bab5f
moved dest_cimplies to drule.ML; added adjust_maxidx
clasohm
parents:
1659
diff
changeset
|
31 |
val adjust_maxidx : cterm -> cterm |
1516
96286c4e32de
removed mk_prop; added capply; simplified dest_abs
clasohm
parents:
1503
diff
changeset
|
32 |
val capply : cterm -> cterm -> cterm |
1517 | 33 |
val cabs : cterm -> cterm -> cterm |
1238 | 34 |
val read_def_cterm : |
1160 | 35 |
Sign.sg * (indexname -> typ option) * (indexname -> sort option) -> |
36 |
string list -> bool -> string * typ -> cterm * (indexname * typ) list |
|
4281
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
37 |
val read_def_cterms : |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
38 |
Sign.sg * (indexname -> typ option) * (indexname -> sort option) -> |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
39 |
string list -> bool -> (string * typ)list |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
40 |
-> cterm list * (indexname * typ)list |
1160 | 41 |
|
2671 | 42 |
(*proof terms [must DUPLICATE declaration as a specification]*) |
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
43 |
datatype deriv_kind = MinDeriv | ThmDeriv | FullDeriv; |
6089 | 44 |
type tag (* = string * string list *) |
2386 | 45 |
val keep_derivs : deriv_kind ref |
1529 | 46 |
datatype rule = |
2386 | 47 |
MinProof |
4999 | 48 |
| Oracle of string * Sign.sg * Object.T |
6089 | 49 |
| Axiom of string * tag list |
50 |
| Theorem of string * tag list |
|
2671 | 51 |
| Assume of cterm |
52 |
| Implies_intr of cterm |
|
1529 | 53 |
| Implies_intr_shyps |
54 |
| Implies_intr_hyps |
|
55 |
| Implies_elim |
|
2671 | 56 |
| Forall_intr of cterm |
57 |
| Forall_elim of cterm |
|
58 |
| Reflexive of cterm |
|
1529 | 59 |
| Symmetric |
60 |
| Transitive |
|
2671 | 61 |
| Beta_conversion of cterm |
1529 | 62 |
| Extensional |
2671 | 63 |
| Abstract_rule of string * cterm |
1529 | 64 |
| Combination |
65 |
| Equal_intr |
|
66 |
| Equal_elim |
|
2671 | 67 |
| Trivial of cterm |
68 |
| Lift_rule of cterm * int |
|
69 |
| Assumption of int * Envir.env option |
|
70 |
| Rotate_rule of int * int |
|
71 |
| Instantiate of (indexname * ctyp) list * (cterm * cterm) list |
|
72 |
| Bicompose of bool * bool * int * int * Envir.env |
|
73 |
| Flexflex_rule of Envir.env |
|
4182 | 74 |
| Class_triv of class |
1529 | 75 |
| VarifyT |
76 |
| FreezeT |
|
2671 | 77 |
| RewriteC of cterm |
78 |
| CongC of cterm |
|
79 |
| Rewrite_cterm of cterm |
|
80 |
| Rename_params_rule of string list * int; |
|
6089 | 81 |
type deriv (* = rule mtree *) |
1529 | 82 |
|
1160 | 83 |
(*meta theorems*) |
84 |
type thm |
|
1529 | 85 |
val rep_thm : thm -> {sign: Sign.sg, der: deriv, maxidx: int, |
2386 | 86 |
shyps: sort list, hyps: term list, |
87 |
prop: term} |
|
1529 | 88 |
val crep_thm : thm -> {sign: Sign.sg, der: deriv, maxidx: int, |
2386 | 89 |
shyps: sort list, hyps: cterm list, |
90 |
prop: cterm} |
|
6089 | 91 |
exception THM of string * int * thm list |
92 |
type 'a attribute (* = 'a * thm -> 'a * thm *) |
|
3994 | 93 |
val eq_thm : thm * thm -> bool |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
94 |
val sign_of_thm : thm -> Sign.sg |
4254 | 95 |
val transfer_sg : Sign.sg -> thm -> thm |
3895 | 96 |
val transfer : theory -> thm -> thm |
1238 | 97 |
val tpairs_of : thm -> (term * term) list |
98 |
val prems_of : thm -> term list |
|
99 |
val nprems_of : thm -> int |
|
100 |
val concl_of : thm -> term |
|
101 |
val cprop_of : thm -> cterm |
|
102 |
val extra_shyps : thm -> sort list |
|
3061 | 103 |
val force_strip_shyps : bool ref (* FIXME tmp (since 1995/08/01) *) |
1238 | 104 |
val strip_shyps : thm -> thm |
105 |
val implies_intr_shyps: thm -> thm |
|
3812 | 106 |
val get_axiom : theory -> xstring -> thm |
4847 | 107 |
val get_def : theory -> xstring -> thm |
1238 | 108 |
val axioms_of : theory -> (string * thm) list |
1160 | 109 |
|
110 |
(*meta rules*) |
|
1238 | 111 |
val assume : cterm -> thm |
1416 | 112 |
val compress : thm -> thm |
1238 | 113 |
val implies_intr : cterm -> thm -> thm |
114 |
val implies_elim : thm -> thm -> thm |
|
115 |
val forall_intr : cterm -> thm -> thm |
|
116 |
val forall_elim : cterm -> thm -> thm |
|
117 |
val reflexive : cterm -> thm |
|
118 |
val symmetric : thm -> thm |
|
119 |
val transitive : thm -> thm -> thm |
|
120 |
val beta_conversion : cterm -> thm |
|
121 |
val extensional : thm -> thm |
|
122 |
val abstract_rule : string -> cterm -> thm -> thm |
|
123 |
val combination : thm -> thm -> thm |
|
124 |
val equal_intr : thm -> thm -> thm |
|
125 |
val equal_elim : thm -> thm -> thm |
|
126 |
val implies_intr_hyps : thm -> thm |
|
4270 | 127 |
val flexflex_rule : thm -> thm Seq.seq |
1238 | 128 |
val instantiate : |
1160 | 129 |
(indexname * ctyp) list * (cterm * cterm) list -> thm -> thm |
1238 | 130 |
val trivial : cterm -> thm |
131 |
val class_triv : theory -> class -> thm |
|
132 |
val varifyT : thm -> thm |
|
133 |
val freezeT : thm -> thm |
|
134 |
val dest_state : thm * int -> |
|
1160 | 135 |
(term * term) list * term list * term * term |
1238 | 136 |
val lift_rule : (thm * int) -> thm -> thm |
4270 | 137 |
val assumption : int -> thm -> thm Seq.seq |
1238 | 138 |
val eq_assumption : int -> thm -> thm |
2671 | 139 |
val rotate_rule : int -> int -> thm -> thm |
1160 | 140 |
val rename_params_rule: string list * int -> thm -> thm |
1238 | 141 |
val bicompose : bool -> bool * thm * int -> |
4270 | 142 |
int -> thm -> thm Seq.seq |
1238 | 143 |
val biresolution : bool -> (bool * thm) list -> |
4270 | 144 |
int -> thm -> thm Seq.seq |
1160 | 145 |
|
146 |
(*meta simplification*) |
|
3550 | 147 |
exception SIMPLIFIER of string * thm |
1160 | 148 |
type meta_simpset |
3550 | 149 |
val dest_mss : meta_simpset -> |
150 |
{simps: thm list, congs: thm list, procs: (string * cterm list) list} |
|
1238 | 151 |
val empty_mss : meta_simpset |
3550 | 152 |
val merge_mss : meta_simpset * meta_simpset -> meta_simpset |
1238 | 153 |
val add_simps : meta_simpset * thm list -> meta_simpset |
154 |
val del_simps : meta_simpset * thm list -> meta_simpset |
|
155 |
val mss_of : thm list -> meta_simpset |
|
156 |
val add_congs : meta_simpset * thm list -> meta_simpset |
|
2626 | 157 |
val del_congs : meta_simpset * thm list -> meta_simpset |
2509 | 158 |
val add_simprocs : meta_simpset * |
3577
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
159 |
(string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list |
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
160 |
-> meta_simpset |
2509 | 161 |
val del_simprocs : meta_simpset * |
3577
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
162 |
(string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list |
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
163 |
-> meta_simpset |
1238 | 164 |
val add_prems : meta_simpset * thm list -> meta_simpset |
165 |
val prems_of_mss : meta_simpset -> thm list |
|
166 |
val set_mk_rews : meta_simpset * (thm -> thm list) -> meta_simpset |
|
4679 | 167 |
val set_mk_sym : meta_simpset * (thm -> thm option) -> meta_simpset |
168 |
val set_mk_eq_True : meta_simpset * (thm -> thm option) -> meta_simpset |
|
2509 | 169 |
val set_termless : meta_simpset * (term * term -> bool) -> meta_simpset |
1238 | 170 |
val trace_simp : bool ref |
4713 | 171 |
val rewrite_cterm : bool * bool * bool -> meta_simpset -> |
1529 | 172 |
(meta_simpset -> thm -> thm option) -> cterm -> thm |
1539 | 173 |
|
4999 | 174 |
val invoke_oracle : theory -> xstring -> Sign.sg * Object.T -> thm |
250 | 175 |
end; |
0 | 176 |
|
6089 | 177 |
signature THM = |
178 |
sig |
|
179 |
include BASIC_THM |
|
180 |
val no_attributes : 'a -> 'a * 'b attribute list |
|
181 |
val apply_attributes : ('a * thm) * 'a attribute list -> ('a * thm) |
|
182 |
val applys_attributes : ('a * thm list) * 'a attribute list -> ('a * thm list) |
|
183 |
val get_name_tags : thm -> string * tag list |
|
184 |
val put_name_tags : string * tag list -> thm -> thm |
|
185 |
val name_of_thm : thm -> string |
|
186 |
val tags_of_thm : thm -> tag list |
|
187 |
val name_thm : string * thm -> thm |
|
188 |
end; |
|
189 |
||
3550 | 190 |
structure Thm: THM = |
0 | 191 |
struct |
250 | 192 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
193 |
(*** Certified terms and types ***) |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
194 |
|
250 | 195 |
(** certified types **) |
196 |
||
197 |
(*certified typs under a signature*) |
|
198 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
199 |
datatype ctyp = Ctyp of {sign_ref: Sign.sg_ref, T: typ}; |
250 | 200 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
201 |
fun rep_ctyp (Ctyp {sign_ref, T}) = {sign = Sign.deref sign_ref, T = T}; |
250 | 202 |
fun typ_of (Ctyp {T, ...}) = T; |
203 |
||
204 |
fun ctyp_of sign T = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
205 |
Ctyp {sign_ref = Sign.self_ref sign, T = Sign.certify_typ sign T}; |
250 | 206 |
|
207 |
fun read_ctyp sign s = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
208 |
Ctyp {sign_ref = Sign.self_ref sign, T = Sign.read_typ (sign, K None) s}; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
209 |
|
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
210 |
|
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
211 |
|
250 | 212 |
(** certified terms **) |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
213 |
|
250 | 214 |
(*certified terms under a signature, with checked typ and maxidx of Vars*) |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
215 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
216 |
datatype cterm = Cterm of {sign_ref: Sign.sg_ref, t: term, T: typ, maxidx: int}; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
217 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
218 |
fun rep_cterm (Cterm {sign_ref, t, T, maxidx}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
219 |
{sign = Sign.deref sign_ref, t = t, T = T, maxidx = maxidx}; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
220 |
|
4288 | 221 |
fun crep_cterm (Cterm {sign_ref, t, T, maxidx}) = |
222 |
{sign = Sign.deref sign_ref, t = t, T = Ctyp {sign_ref = sign_ref, T = T}, |
|
223 |
maxidx = maxidx}; |
|
224 |
||
250 | 225 |
fun term_of (Cterm {t, ...}) = t; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
226 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
227 |
fun ctyp_of_term (Cterm {sign_ref, T, ...}) = Ctyp {sign_ref = sign_ref, T = T}; |
2671 | 228 |
|
250 | 229 |
(*create a cterm by checking a "raw" term with respect to a signature*) |
230 |
fun cterm_of sign tm = |
|
231 |
let val (t, T, maxidx) = Sign.certify_term sign tm |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
232 |
in Cterm {sign_ref = Sign.self_ref sign, t = t, T = T, maxidx = maxidx} |
1394
a1d2735f5ade
New function read_cterms is a combination of read_def_cterm and
paulson
parents:
1258
diff
changeset
|
233 |
end; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
234 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
235 |
fun cterm_fun f (Cterm {sign_ref, t, ...}) = cterm_of (Sign.deref sign_ref) (f t); |
250 | 236 |
|
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
237 |
|
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
238 |
exception CTERM of string; |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
239 |
|
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
240 |
(*Destruct application in cterms*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
241 |
fun dest_comb (Cterm {sign_ref, T, maxidx, t = A $ B}) = |
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
242 |
let val typeA = fastype_of A; |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
243 |
val typeB = |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
244 |
case typeA of Type("fun",[S,T]) => S |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
245 |
| _ => error "Function type expected in dest_comb"; |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
246 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
247 |
(Cterm {sign_ref=sign_ref, maxidx=maxidx, t=A, T=typeA}, |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
248 |
Cterm {sign_ref=sign_ref, maxidx=maxidx, t=B, T=typeB}) |
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
249 |
end |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
250 |
| dest_comb _ = raise CTERM "dest_comb"; |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
251 |
|
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
252 |
(*Destruct abstraction in cterms*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
253 |
fun dest_abs (Cterm {sign_ref, T as Type("fun",[_,S]), maxidx, t=Abs(x,ty,M)}) = |
1516
96286c4e32de
removed mk_prop; added capply; simplified dest_abs
clasohm
parents:
1503
diff
changeset
|
254 |
let val (y,N) = variant_abs (x,ty,M) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
255 |
in (Cterm {sign_ref = sign_ref, T = ty, maxidx = 0, t = Free(y,ty)}, |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
256 |
Cterm {sign_ref = sign_ref, T = S, maxidx = maxidx, t = N}) |
1493
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
257 |
end |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
258 |
| dest_abs _ = raise CTERM "dest_abs"; |
e936723cb94d
added dest_comb, dest_abs and mk_prop for manipulating cterms
clasohm
parents:
1460
diff
changeset
|
259 |
|
2147 | 260 |
(*Makes maxidx precise: it is often too big*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
261 |
fun adjust_maxidx (ct as Cterm {sign_ref, T, t, maxidx, ...}) = |
2147 | 262 |
if maxidx = ~1 then ct |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
263 |
else Cterm {sign_ref = sign_ref, T = T, maxidx = maxidx_of_term t, t = t}; |
1703
e22ad43bab5f
moved dest_cimplies to drule.ML; added adjust_maxidx
clasohm
parents:
1659
diff
changeset
|
264 |
|
1516
96286c4e32de
removed mk_prop; added capply; simplified dest_abs
clasohm
parents:
1503
diff
changeset
|
265 |
(*Form cterm out of a function and an argument*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
266 |
fun capply (Cterm {t=f, sign_ref=sign_ref1, T=Type("fun",[dty,rty]), maxidx=maxidx1}) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
267 |
(Cterm {t=x, sign_ref=sign_ref2, T, maxidx=maxidx2}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
268 |
if T = dty then Cterm{t=f$x, sign_ref=Sign.merge_refs(sign_ref1,sign_ref2), T=rty, |
2147 | 269 |
maxidx=Int.max(maxidx1, maxidx2)} |
1516
96286c4e32de
removed mk_prop; added capply; simplified dest_abs
clasohm
parents:
1503
diff
changeset
|
270 |
else raise CTERM "capply: types don't agree" |
96286c4e32de
removed mk_prop; added capply; simplified dest_abs
clasohm
parents:
1503
diff
changeset
|
271 |
| capply _ _ = raise CTERM "capply: first arg is not a function" |
250 | 272 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
273 |
fun cabs (Cterm {t=Free(a,ty), sign_ref=sign_ref1, T=T1, maxidx=maxidx1}) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
274 |
(Cterm {t=t2, sign_ref=sign_ref2, T=T2, maxidx=maxidx2}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
275 |
Cterm {t=absfree(a,ty,t2), sign_ref=Sign.merge_refs(sign_ref1,sign_ref2), |
2147 | 276 |
T = ty --> T2, maxidx=Int.max(maxidx1, maxidx2)} |
1517 | 277 |
| cabs _ _ = raise CTERM "cabs: first arg is not a free variable"; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
278 |
|
2509 | 279 |
|
280 |
||
574 | 281 |
(** read cterms **) (*exception ERROR*) |
250 | 282 |
|
4281
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
283 |
(*read terms, infer types, certify terms*) |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
284 |
fun read_def_cterms (sign, types, sorts) used freeze sTs = |
250 | 285 |
let |
4281
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
286 |
val syn = #syn (Sign.rep_sg sign) |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
287 |
fun read(s,T) = |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
288 |
let val T' = Sign.certify_typ sign T |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
289 |
handle TYPE (msg, _, _) => error msg |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
290 |
in (Syntax.read syn T' s, T') end |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
291 |
val tsTs = map read sTs |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
292 |
val (ts',tye) = Sign.infer_types_simult sign types sorts used freeze tsTs; |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
293 |
val cts = map (cterm_of sign) ts' |
2979 | 294 |
handle TYPE (msg, _, _) => error msg |
2386 | 295 |
| TERM (msg, _) => error msg; |
4281
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
296 |
in (cts, tye) end; |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
297 |
|
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
298 |
(*read term, infer types, certify term*) |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
299 |
fun read_def_cterm args used freeze aT = |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
300 |
let val ([ct],tye) = read_def_cterms args used freeze [aT] |
6c6073b13600
Added read_def_cterms for simultaneous reading/typing of terms under
nipkow
parents:
4270
diff
changeset
|
301 |
in (ct,tye) end; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
302 |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
922
diff
changeset
|
303 |
fun read_cterm sign = #1 o read_def_cterm (sign, K None, K None) [] true; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
304 |
|
250 | 305 |
|
306 |
||
1529 | 307 |
(*** Derivations ***) |
308 |
||
6089 | 309 |
(*tags provide additional comment, apart from the axiom/theorem name*) |
310 |
type tag = string * string list; |
|
311 |
||
1529 | 312 |
(*Names of rules in derivations. Includes logically trivial rules, if |
313 |
executed in ML.*) |
|
314 |
datatype rule = |
|
2386 | 315 |
MinProof (*for building minimal proof terms*) |
4999 | 316 |
| Oracle of string * Sign.sg * Object.T (*oracles*) |
1529 | 317 |
(*Axioms/theorems*) |
6089 | 318 |
| Axiom of string * tag list |
319 |
| Theorem of string * tag list |
|
1529 | 320 |
(*primitive inferences and compound versions of them*) |
2386 | 321 |
| Assume of cterm |
322 |
| Implies_intr of cterm |
|
1529 | 323 |
| Implies_intr_shyps |
324 |
| Implies_intr_hyps |
|
325 |
| Implies_elim |
|
2386 | 326 |
| Forall_intr of cterm |
327 |
| Forall_elim of cterm |
|
328 |
| Reflexive of cterm |
|
1529 | 329 |
| Symmetric |
330 |
| Transitive |
|
2386 | 331 |
| Beta_conversion of cterm |
1529 | 332 |
| Extensional |
2386 | 333 |
| Abstract_rule of string * cterm |
1529 | 334 |
| Combination |
335 |
| Equal_intr |
|
336 |
| Equal_elim |
|
337 |
(*derived rules for tactical proof*) |
|
2386 | 338 |
| Trivial of cterm |
339 |
(*For lift_rule, the proof state is not a premise. |
|
340 |
Use cterm instead of thm to avoid mutual recursion.*) |
|
341 |
| Lift_rule of cterm * int |
|
342 |
| Assumption of int * Envir.env option (*includes eq_assumption*) |
|
2671 | 343 |
| Rotate_rule of int * int |
2386 | 344 |
| Instantiate of (indexname * ctyp) list * (cterm * cterm) list |
345 |
| Bicompose of bool * bool * int * int * Envir.env |
|
346 |
| Flexflex_rule of Envir.env (*identifies unifier chosen*) |
|
1529 | 347 |
(*other derived rules*) |
4182 | 348 |
| Class_triv of class |
1529 | 349 |
| VarifyT |
350 |
| FreezeT |
|
351 |
(*for the simplifier*) |
|
2386 | 352 |
| RewriteC of cterm |
353 |
| CongC of cterm |
|
354 |
| Rewrite_cterm of cterm |
|
1529 | 355 |
(*Logical identities, recorded since they are part of the proof process*) |
2386 | 356 |
| Rename_params_rule of string list * int; |
1529 | 357 |
|
358 |
||
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
359 |
type deriv = rule mtree; |
1529 | 360 |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
361 |
datatype deriv_kind = MinDeriv | ThmDeriv | FullDeriv; |
1529 | 362 |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
363 |
val keep_derivs = ref MinDeriv; |
1529 | 364 |
|
365 |
||
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
366 |
(*Build a minimal derivation. Keep oracles; suppress atomic inferences; |
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
367 |
retain Theorems or their underlying links; keep anything else*) |
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
368 |
fun squash_derivs [] = [] |
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
369 |
| squash_derivs (der::ders) = |
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
370 |
(case der of |
2386 | 371 |
Join (Oracle _, _) => der :: squash_derivs ders |
372 |
| Join (Theorem _, [der']) => if !keep_derivs=ThmDeriv |
|
373 |
then der :: squash_derivs ders |
|
374 |
else squash_derivs (der'::ders) |
|
375 |
| Join (Axiom _, _) => if !keep_derivs=ThmDeriv |
|
376 |
then der :: squash_derivs ders |
|
377 |
else squash_derivs ders |
|
378 |
| Join (_, []) => squash_derivs ders |
|
379 |
| _ => der :: squash_derivs ders); |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
380 |
|
1529 | 381 |
|
382 |
(*Ensure sharing of the most likely derivation, the empty one!*) |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
383 |
val min_infer = Join (MinProof, []); |
1529 | 384 |
|
385 |
(*Make a minimal inference*) |
|
386 |
fun make_min_infer [] = min_infer |
|
387 |
| make_min_infer [der] = der |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
388 |
| make_min_infer ders = Join (MinProof, ders); |
1529 | 389 |
|
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
390 |
fun infer_derivs (rl, []) = Join (rl, []) |
1529 | 391 |
| infer_derivs (rl, ders) = |
1597
54ece585bf62
name_thm no longer takes a theory argument, as the
paulson
parents:
1580
diff
changeset
|
392 |
if !keep_derivs=FullDeriv then Join (rl, ders) |
1529 | 393 |
else make_min_infer (squash_derivs ders); |
394 |
||
395 |
||
2509 | 396 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
397 |
(*** Meta theorems ***) |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
398 |
|
0 | 399 |
datatype thm = Thm of |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
400 |
{sign_ref: Sign.sg_ref, (*mutable reference to signature*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
401 |
der: deriv, (*derivation*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
402 |
maxidx: int, (*maximum index of any Var or TVar*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
403 |
shyps: sort list, (*sort hypotheses*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
404 |
hyps: term list, (*hypotheses*) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
405 |
prop: term}; (*conclusion*) |
0 | 406 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
407 |
fun rep_thm (Thm {sign_ref, der, maxidx, shyps, hyps, prop}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
408 |
{sign = Sign.deref sign_ref, der = der, maxidx = maxidx, |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
409 |
shyps = shyps, hyps = hyps, prop = prop}; |
0 | 410 |
|
1529 | 411 |
(*Version of rep_thm returning cterms instead of terms*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
412 |
fun crep_thm (Thm {sign_ref, der, maxidx, shyps, hyps, prop}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
413 |
let fun ctermf max t = Cterm{sign_ref=sign_ref, t=t, T=propT, maxidx=max}; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
414 |
in {sign = Sign.deref sign_ref, der = der, maxidx = maxidx, shyps = shyps, |
1529 | 415 |
hyps = map (ctermf ~1) hyps, |
416 |
prop = ctermf maxidx prop} |
|
1517 | 417 |
end; |
418 |
||
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
419 |
(*errors involving theorems*) |
0 | 420 |
exception THM of string * int * thm list; |
421 |
||
6089 | 422 |
(*attributes subsume any kind of rules or addXXXs modifiers*) |
423 |
type 'a attribute = 'a * thm -> 'a * thm; |
|
424 |
||
425 |
fun no_attributes x = (x, []); |
|
426 |
fun apply_attributes (x_th, atts) = Library.apply atts x_th; |
|
427 |
fun applys_attributes (x_ths, atts) = foldl_map (Library.apply atts) x_ths; |
|
428 |
||
3994 | 429 |
(*equality of theorems uses equality of signatures and the |
430 |
a-convertible test for terms*) |
|
431 |
fun eq_thm (th1, th2) = |
|
432 |
let |
|
433 |
val {sign = sg1, shyps = shyps1, hyps = hyps1, prop = prop1, ...} = rep_thm th1; |
|
434 |
val {sign = sg2, shyps = shyps2, hyps = hyps2, prop = prop2, ...} = rep_thm th2; |
|
435 |
in |
|
436 |
Sign.eq_sg (sg1, sg2) andalso |
|
437 |
eq_set_sort (shyps1, shyps2) andalso |
|
438 |
aconvs (hyps1, hyps2) andalso |
|
439 |
prop1 aconv prop2 |
|
440 |
end; |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
441 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
442 |
fun sign_of_thm (Thm {sign_ref, ...}) = Sign.deref sign_ref; |
0 | 443 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
444 |
(*merge signatures of two theorems; raise exception if incompatible*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
445 |
fun merge_thm_sgs |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
446 |
(th1 as Thm {sign_ref = sgr1, ...}, th2 as Thm {sign_ref = sgr2, ...}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
447 |
Sign.merge_refs (sgr1, sgr2) handle TERM (msg, _) => raise THM (msg, 0, [th1, th2]); |
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
448 |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
449 |
(*transfer thm to super theory (non-destructive)*) |
4254 | 450 |
fun transfer_sg sign' thm = |
3895 | 451 |
let |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
452 |
val Thm {sign_ref, der, maxidx, shyps, hyps, prop} = thm; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
453 |
val sign = Sign.deref sign_ref; |
3895 | 454 |
in |
4254 | 455 |
if Sign.eq_sg (sign, sign') then thm |
456 |
else if Sign.subsig (sign, sign') then |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
457 |
Thm {sign_ref = Sign.self_ref sign', der = der, maxidx = maxidx, |
3895 | 458 |
shyps = shyps, hyps = hyps, prop = prop} |
459 |
else raise THM ("transfer: not a super theory", 0, [thm]) |
|
460 |
end; |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
461 |
|
4254 | 462 |
val transfer = transfer_sg o sign_of; |
463 |
||
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
464 |
(*maps object-rule to tpairs*) |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
465 |
fun tpairs_of (Thm {prop, ...}) = #1 (Logic.strip_flexpairs prop); |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
466 |
|
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
467 |
(*maps object-rule to premises*) |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
468 |
fun prems_of (Thm {prop, ...}) = |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
469 |
Logic.strip_imp_prems (Logic.skip_flexpairs prop); |
0 | 470 |
|
471 |
(*counts premises in a rule*) |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
472 |
fun nprems_of (Thm {prop, ...}) = |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
473 |
Logic.count_prems (Logic.skip_flexpairs prop, 0); |
0 | 474 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
475 |
(*maps object-rule to conclusion*) |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
476 |
fun concl_of (Thm {prop, ...}) = Logic.strip_imp_concl prop; |
0 | 477 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
478 |
(*the statement of any thm is a cterm*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
479 |
fun cprop_of (Thm {sign_ref, maxidx, prop, ...}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
480 |
Cterm {sign_ref = sign_ref, maxidx = maxidx, T = propT, t = prop}; |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
481 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
482 |
|
0 | 483 |
|
1238 | 484 |
(** sort contexts of theorems **) |
485 |
||
486 |
(* basic utils *) |
|
487 |
||
2163 | 488 |
(*accumulate sorts suppressing duplicates; these are coded low levelly |
1238 | 489 |
to improve efficiency a bit*) |
490 |
||
491 |
fun add_typ_sorts (Type (_, Ts), Ss) = add_typs_sorts (Ts, Ss) |
|
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
492 |
| add_typ_sorts (TFree (_, S), Ss) = ins_sort(S,Ss) |
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
493 |
| add_typ_sorts (TVar (_, S), Ss) = ins_sort(S,Ss) |
1238 | 494 |
and add_typs_sorts ([], Ss) = Ss |
495 |
| add_typs_sorts (T :: Ts, Ss) = add_typs_sorts (Ts, add_typ_sorts (T, Ss)); |
|
496 |
||
497 |
fun add_term_sorts (Const (_, T), Ss) = add_typ_sorts (T, Ss) |
|
498 |
| add_term_sorts (Free (_, T), Ss) = add_typ_sorts (T, Ss) |
|
499 |
| add_term_sorts (Var (_, T), Ss) = add_typ_sorts (T, Ss) |
|
500 |
| add_term_sorts (Bound _, Ss) = Ss |
|
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
501 |
| add_term_sorts (Abs (_,T,t), Ss) = add_term_sorts (t, add_typ_sorts (T,Ss)) |
1238 | 502 |
| add_term_sorts (t $ u, Ss) = add_term_sorts (t, add_term_sorts (u, Ss)); |
503 |
||
504 |
fun add_terms_sorts ([], Ss) = Ss |
|
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
505 |
| add_terms_sorts (t::ts, Ss) = add_terms_sorts (ts, add_term_sorts (t,Ss)); |
1238 | 506 |
|
1258 | 507 |
fun env_codT (Envir.Envir {iTs, ...}) = map snd iTs; |
508 |
||
509 |
fun add_env_sorts (env, Ss) = |
|
510 |
add_terms_sorts (map snd (Envir.alist_of env), |
|
511 |
add_typs_sorts (env_codT env, Ss)); |
|
512 |
||
1238 | 513 |
fun add_thm_sorts (Thm {hyps, prop, ...}, Ss) = |
514 |
add_terms_sorts (hyps, add_term_sorts (prop, Ss)); |
|
515 |
||
516 |
fun add_thms_shyps ([], Ss) = Ss |
|
517 |
| add_thms_shyps (Thm {shyps, ...} :: ths, Ss) = |
|
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
518 |
add_thms_shyps (ths, union_sort(shyps,Ss)); |
1238 | 519 |
|
520 |
||
521 |
(*get 'dangling' sort constraints of a thm*) |
|
522 |
fun extra_shyps (th as Thm {shyps, ...}) = |
|
523 |
shyps \\ add_thm_sorts (th, []); |
|
524 |
||
525 |
||
526 |
(* fix_shyps *) |
|
527 |
||
528 |
(*preserve sort contexts of rule premises and substituted types*) |
|
529 |
fun fix_shyps thms Ts thm = |
|
530 |
let |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
531 |
val Thm {sign_ref, der, maxidx, hyps, prop, ...} = thm; |
1238 | 532 |
val shyps = |
533 |
add_thm_sorts (thm, add_typs_sorts (Ts, add_thms_shyps (thms, []))); |
|
534 |
in |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
535 |
Thm {sign_ref = sign_ref, |
2386 | 536 |
der = der, (*No new derivation, as other rules call this*) |
537 |
maxidx = maxidx, |
|
538 |
shyps = shyps, hyps = hyps, prop = prop} |
|
1238 | 539 |
end; |
540 |
||
541 |
||
542 |
(* strip_shyps *) (* FIXME improve? (e.g. only minimal extra sorts) *) |
|
543 |
||
3061 | 544 |
val force_strip_shyps = ref true; (* FIXME tmp (since 1995/08/01) *) |
1238 | 545 |
|
546 |
(*remove extra sorts that are known to be syntactically non-empty*) |
|
547 |
fun strip_shyps thm = |
|
548 |
let |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
549 |
val Thm {sign_ref, der, maxidx, shyps, hyps, prop} = thm; |
1238 | 550 |
val sorts = add_thm_sorts (thm, []); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
551 |
val maybe_empty = not o Sign.nonempty_sort (Sign.deref sign_ref) sorts; |
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
552 |
val shyps' = filter (fn S => mem_sort(S,sorts) orelse maybe_empty S) shyps; |
1238 | 553 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
554 |
Thm {sign_ref = sign_ref, der = der, maxidx = maxidx, |
2386 | 555 |
shyps = |
556 |
(if eq_set_sort (shyps',sorts) orelse |
|
557 |
not (!force_strip_shyps) then shyps' |
|
3061 | 558 |
else (* FIXME tmp (since 1995/08/01) *) |
2386 | 559 |
(warning ("Removed sort hypotheses: " ^ |
2962 | 560 |
commas (map Sorts.str_of_sort (shyps' \\ sorts))); |
2386 | 561 |
warning "Let's hope these sorts are non-empty!"; |
1238 | 562 |
sorts)), |
1529 | 563 |
hyps = hyps, |
564 |
prop = prop} |
|
1238 | 565 |
end; |
566 |
||
567 |
||
568 |
(* implies_intr_shyps *) |
|
569 |
||
570 |
(*discharge all extra sort hypotheses*) |
|
571 |
fun implies_intr_shyps thm = |
|
572 |
(case extra_shyps thm of |
|
573 |
[] => thm |
|
574 |
| xshyps => |
|
575 |
let |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
576 |
val Thm {sign_ref, der, maxidx, shyps, hyps, prop} = thm; |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2177
diff
changeset
|
577 |
val shyps' = ins_sort (logicS, shyps \\ xshyps); |
1238 | 578 |
val used_names = foldr add_term_tfree_names (prop :: hyps, []); |
579 |
val names = |
|
580 |
tl (variantlist (replicate (length xshyps + 1) "'", used_names)); |
|
581 |
val tfrees = map (TFree o rpair logicS) names; |
|
582 |
||
583 |
fun mk_insort (T, S) = map (Logic.mk_inclass o pair T) S; |
|
2671 | 584 |
val sort_hyps = List.concat (map2 mk_insort (tfrees, xshyps)); |
1238 | 585 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
586 |
Thm {sign_ref = sign_ref, |
2386 | 587 |
der = infer_derivs (Implies_intr_shyps, [der]), |
588 |
maxidx = maxidx, |
|
589 |
shyps = shyps', |
|
590 |
hyps = hyps, |
|
591 |
prop = Logic.list_implies (sort_hyps, prop)} |
|
1238 | 592 |
end); |
593 |
||
594 |
||
1529 | 595 |
(** Axioms **) |
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
596 |
|
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
597 |
(*look up the named axiom in the theory*) |
3812 | 598 |
fun get_axiom theory raw_name = |
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
599 |
let |
4847 | 600 |
val name = Sign.intern (Theory.sign_of theory) Theory.axiomK raw_name; |
601 |
||
602 |
fun get_ax [] = None |
|
1529 | 603 |
| get_ax (thy :: thys) = |
4847 | 604 |
let val {sign, axioms, ...} = Theory.rep_theory thy in |
605 |
(case Symtab.lookup (axioms, name) of |
|
606 |
Some t => |
|
607 |
Some (fix_shyps [] [] |
|
608 |
(Thm {sign_ref = Sign.self_ref sign, |
|
6089 | 609 |
der = infer_derivs (Axiom (name, []), []), |
4847 | 610 |
maxidx = maxidx_of_term t, |
611 |
shyps = [], |
|
612 |
hyps = [], |
|
613 |
prop = t})) |
|
614 |
| None => get_ax thys) |
|
1529 | 615 |
end; |
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
616 |
in |
4847 | 617 |
(case get_ax (theory :: Theory.ancestors_of theory) of |
618 |
Some thm => thm |
|
619 |
| None => raise THEORY ("No axiom " ^ quote name, [theory])) |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
620 |
end; |
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
621 |
|
4847 | 622 |
fun get_def thy name = get_axiom thy (name ^ "_def"); |
623 |
||
1529 | 624 |
|
776
df8f91c0e57c
improved axioms_of: returns thms as the manual says;
wenzelm
parents:
721
diff
changeset
|
625 |
(*return additional axioms of this theory node*) |
df8f91c0e57c
improved axioms_of: returns thms as the manual says;
wenzelm
parents:
721
diff
changeset
|
626 |
fun axioms_of thy = |
df8f91c0e57c
improved axioms_of: returns thms as the manual says;
wenzelm
parents:
721
diff
changeset
|
627 |
map (fn (s, _) => (s, get_axiom thy s)) |
3994 | 628 |
(Symtab.dest (#axioms (rep_theory thy))); |
776
df8f91c0e57c
improved axioms_of: returns thms as the manual says;
wenzelm
parents:
721
diff
changeset
|
629 |
|
6089 | 630 |
|
631 |
(* name and tags -- make proof objects more readable *) |
|
632 |
||
633 |
fun get_name_tags (Thm {der, ...}) = |
|
4018 | 634 |
(case der of |
6089 | 635 |
Join (Theorem x, _) => x |
636 |
| Join (Axiom x, _) => x |
|
637 |
| _ => ("", [])); |
|
4018 | 638 |
|
6089 | 639 |
fun put_name_tags x (Thm {sign_ref, der, maxidx, shyps, hyps, prop}) = |
640 |
let |
|
641 |
val der' = |
|
642 |
(case der of |
|
643 |
Join (Theorem _, ds) => Join (Theorem x, ds) |
|
644 |
| Join (Axiom _, ds) => Join (Axiom x, ds) |
|
645 |
| _ => Join (Theorem x, [der])); |
|
646 |
in |
|
647 |
Thm {sign_ref = sign_ref, der = der', maxidx = maxidx, |
|
648 |
shyps = shyps, hyps = hyps, prop = prop} |
|
649 |
end; |
|
650 |
||
651 |
val name_of_thm = #1 o get_name_tags; |
|
652 |
val tags_of_thm = #2 o get_name_tags; |
|
653 |
||
654 |
fun name_thm (name, thm) = put_name_tags (name, tags_of_thm thm) thm; |
|
0 | 655 |
|
656 |
||
1529 | 657 |
(*Compression of theorems -- a separate rule, not integrated with the others, |
658 |
as it could be slow.*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
659 |
fun compress (Thm {sign_ref, der, maxidx, shyps, hyps, prop}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
660 |
Thm {sign_ref = sign_ref, |
2386 | 661 |
der = der, (*No derivation recorded!*) |
662 |
maxidx = maxidx, |
|
663 |
shyps = shyps, |
|
664 |
hyps = map Term.compress_term hyps, |
|
665 |
prop = Term.compress_term prop}; |
|
564 | 666 |
|
387
69f4356d915d
new datatype theory, supports 'draft theories' and incremental extension:
wenzelm
parents:
309
diff
changeset
|
667 |
|
2509 | 668 |
|
1529 | 669 |
(*** Meta rules ***) |
0 | 670 |
|
2147 | 671 |
(*Check that term does not contain same var with different typing/sorting. |
672 |
If this check must be made, recalculate maxidx in hope of preventing its |
|
673 |
recurrence.*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
674 |
fun nodup_Vars (thm as Thm{sign_ref, der, maxidx, shyps, hyps, prop}) s = |
2147 | 675 |
(Sign.nodup_Vars prop; |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
676 |
Thm {sign_ref = sign_ref, |
2386 | 677 |
der = der, |
678 |
maxidx = maxidx_of_term prop, |
|
679 |
shyps = shyps, |
|
680 |
hyps = hyps, |
|
681 |
prop = prop}) |
|
2147 | 682 |
handle TYPE(msg,Ts,ts) => raise TYPE(s^": "^msg,Ts,ts); |
1495
b8b54847c77f
Added check for duplicate vars with distinct types/sorts (nodup_Vars)
nipkow
parents:
1493
diff
changeset
|
683 |
|
1220 | 684 |
(** 'primitive' rules **) |
685 |
||
686 |
(*discharge all assumptions t from ts*) |
|
0 | 687 |
val disch = gen_rem (op aconv); |
688 |
||
1220 | 689 |
(*The assumption rule A|-A in a theory*) |
5344 | 690 |
fun assume raw_ct : thm = |
691 |
let val ct as Cterm {sign_ref, t=prop, T, maxidx} = adjust_maxidx raw_ct |
|
250 | 692 |
in if T<>propT then |
693 |
raise THM("assume: assumptions must have type prop", 0, []) |
|
0 | 694 |
else if maxidx <> ~1 then |
250 | 695 |
raise THM("assume: assumptions may not contain scheme variables", |
696 |
maxidx, []) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
697 |
else Thm{sign_ref = sign_ref, |
5344 | 698 |
der = infer_derivs (Assume ct, []), |
2386 | 699 |
maxidx = ~1, |
700 |
shyps = add_term_sorts(prop,[]), |
|
701 |
hyps = [prop], |
|
702 |
prop = prop} |
|
0 | 703 |
end; |
704 |
||
1220 | 705 |
(*Implication introduction |
3529 | 706 |
[A] |
707 |
: |
|
708 |
B |
|
1220 | 709 |
------- |
710 |
A ==> B |
|
711 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
712 |
fun implies_intr cA (thB as Thm{sign_ref,der,maxidx,hyps,prop,...}) : thm = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
713 |
let val Cterm {sign_ref=sign_refA, t=A, T, maxidx=maxidxA} = cA |
0 | 714 |
in if T<>propT then |
250 | 715 |
raise THM("implies_intr: assumptions must have type prop", 0, [thB]) |
1238 | 716 |
else fix_shyps [thB] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
717 |
(Thm{sign_ref = Sign.merge_refs (sign_ref,sign_refA), |
2386 | 718 |
der = infer_derivs (Implies_intr cA, [der]), |
719 |
maxidx = Int.max(maxidxA, maxidx), |
|
720 |
shyps = [], |
|
721 |
hyps = disch(hyps,A), |
|
722 |
prop = implies$A$prop}) |
|
0 | 723 |
handle TERM _ => |
724 |
raise THM("implies_intr: incompatible signatures", 0, [thB]) |
|
725 |
end; |
|
726 |
||
1529 | 727 |
|
1220 | 728 |
(*Implication elimination |
729 |
A ==> B A |
|
730 |
------------ |
|
731 |
B |
|
732 |
*) |
|
0 | 733 |
fun implies_elim thAB thA : thm = |
1529 | 734 |
let val Thm{maxidx=maxA, der=derA, hyps=hypsA, prop=propA,...} = thA |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
735 |
and Thm{sign_ref, der, maxidx, hyps, prop,...} = thAB; |
250 | 736 |
fun err(a) = raise THM("implies_elim: "^a, 0, [thAB,thA]) |
0 | 737 |
in case prop of |
250 | 738 |
imp$A$B => |
739 |
if imp=implies andalso A aconv propA |
|
1220 | 740 |
then fix_shyps [thAB, thA] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
741 |
(Thm{sign_ref= merge_thm_sgs(thAB,thA), |
2386 | 742 |
der = infer_derivs (Implies_elim, [der,derA]), |
743 |
maxidx = Int.max(maxA,maxidx), |
|
744 |
shyps = [], |
|
745 |
hyps = union_term(hypsA,hyps), (*dups suppressed*) |
|
746 |
prop = B}) |
|
250 | 747 |
else err("major premise") |
748 |
| _ => err("major premise") |
|
0 | 749 |
end; |
250 | 750 |
|
1220 | 751 |
(*Forall introduction. The Free or Var x must not be free in the hypotheses. |
752 |
A |
|
753 |
----- |
|
754 |
!!x.A |
|
755 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
756 |
fun forall_intr cx (th as Thm{sign_ref,der,maxidx,hyps,prop,...}) = |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
757 |
let val x = term_of cx; |
1238 | 758 |
fun result(a,T) = fix_shyps [th] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
759 |
(Thm{sign_ref = sign_ref, |
2386 | 760 |
der = infer_derivs (Forall_intr cx, [der]), |
761 |
maxidx = maxidx, |
|
762 |
shyps = [], |
|
763 |
hyps = hyps, |
|
764 |
prop = all(T) $ Abs(a, T, abstract_over (x,prop))}) |
|
0 | 765 |
in case x of |
250 | 766 |
Free(a,T) => |
767 |
if exists (apl(x, Logic.occs)) hyps |
|
768 |
then raise THM("forall_intr: variable free in assumptions", 0, [th]) |
|
769 |
else result(a,T) |
|
0 | 770 |
| Var((a,_),T) => result(a,T) |
771 |
| _ => raise THM("forall_intr: not a variable", 0, [th]) |
|
772 |
end; |
|
773 |
||
1220 | 774 |
(*Forall elimination |
775 |
!!x.A |
|
776 |
------ |
|
777 |
A[t/x] |
|
778 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
779 |
fun forall_elim ct (th as Thm{sign_ref,der,maxidx,hyps,prop,...}) : thm = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
780 |
let val Cterm {sign_ref=sign_reft, t, T, maxidx=maxt} = ct |
0 | 781 |
in case prop of |
2386 | 782 |
Const("all",Type("fun",[Type("fun",[qary,_]),_])) $ A => |
783 |
if T<>qary then |
|
784 |
raise THM("forall_elim: type mismatch", 0, [th]) |
|
785 |
else let val thm = fix_shyps [th] [] |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
786 |
(Thm{sign_ref= Sign.merge_refs(sign_ref,sign_reft), |
2386 | 787 |
der = infer_derivs (Forall_elim ct, [der]), |
788 |
maxidx = Int.max(maxidx, maxt), |
|
789 |
shyps = [], |
|
790 |
hyps = hyps, |
|
791 |
prop = betapply(A,t)}) |
|
792 |
in if maxt >= 0 andalso maxidx >= 0 |
|
793 |
then nodup_Vars thm "forall_elim" |
|
794 |
else thm (*no new Vars: no expensive check!*) |
|
795 |
end |
|
2147 | 796 |
| _ => raise THM("forall_elim: not quantified", 0, [th]) |
0 | 797 |
end |
798 |
handle TERM _ => |
|
250 | 799 |
raise THM("forall_elim: incompatible signatures", 0, [th]); |
0 | 800 |
|
801 |
||
1220 | 802 |
(* Equality *) |
0 | 803 |
|
804 |
(*The reflexivity rule: maps t to the theorem t==t *) |
|
250 | 805 |
fun reflexive ct = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
806 |
let val Cterm {sign_ref, t, T, maxidx} = ct |
1238 | 807 |
in fix_shyps [] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
808 |
(Thm{sign_ref= sign_ref, |
2386 | 809 |
der = infer_derivs (Reflexive ct, []), |
810 |
shyps = [], |
|
811 |
hyps = [], |
|
812 |
maxidx = maxidx, |
|
813 |
prop = Logic.mk_equals(t,t)}) |
|
0 | 814 |
end; |
815 |
||
816 |
(*The symmetry rule |
|
1220 | 817 |
t==u |
818 |
---- |
|
819 |
u==t |
|
820 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
821 |
fun symmetric (th as Thm{sign_ref,der,maxidx,shyps,hyps,prop}) = |
0 | 822 |
case prop of |
823 |
(eq as Const("==",_)) $ t $ u => |
|
1238 | 824 |
(*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
825 |
Thm{sign_ref = sign_ref, |
2386 | 826 |
der = infer_derivs (Symmetric, [der]), |
827 |
maxidx = maxidx, |
|
828 |
shyps = shyps, |
|
829 |
hyps = hyps, |
|
830 |
prop = eq$u$t} |
|
0 | 831 |
| _ => raise THM("symmetric", 0, [th]); |
832 |
||
833 |
(*The transitive rule |
|
1220 | 834 |
t1==u u==t2 |
835 |
-------------- |
|
836 |
t1==t2 |
|
837 |
*) |
|
0 | 838 |
fun transitive th1 th2 = |
1529 | 839 |
let val Thm{der=der1, maxidx=max1, hyps=hyps1, prop=prop1,...} = th1 |
840 |
and Thm{der=der2, maxidx=max2, hyps=hyps2, prop=prop2,...} = th2; |
|
0 | 841 |
fun err(msg) = raise THM("transitive: "^msg, 0, [th1,th2]) |
842 |
in case (prop1,prop2) of |
|
843 |
((eq as Const("==",_)) $ t1 $ u, Const("==",_) $ u' $ t2) => |
|
1634 | 844 |
if not (u aconv u') then err"middle term" |
845 |
else let val thm = |
|
1220 | 846 |
fix_shyps [th1, th2] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
847 |
(Thm{sign_ref= merge_thm_sgs(th1,th2), |
2386 | 848 |
der = infer_derivs (Transitive, [der1, der2]), |
2147 | 849 |
maxidx = Int.max(max1,max2), |
2386 | 850 |
shyps = [], |
851 |
hyps = union_term(hyps1,hyps2), |
|
852 |
prop = eq$t1$t2}) |
|
2139
2c59b204b540
Only calls nodup_Vars if really necessary. We get a speedup of nearly 6%
paulson
parents:
2047
diff
changeset
|
853 |
in if max1 >= 0 andalso max2 >= 0 |
2147 | 854 |
then nodup_Vars thm "transitive" |
855 |
else thm (*no new Vars: no expensive check!*) |
|
2139
2c59b204b540
Only calls nodup_Vars if really necessary. We get a speedup of nearly 6%
paulson
parents:
2047
diff
changeset
|
856 |
end |
0 | 857 |
| _ => err"premises" |
858 |
end; |
|
859 |
||
1160 | 860 |
(*Beta-conversion: maps (%x.t)(u) to the theorem (%x.t)(u) == t[u/x] *) |
250 | 861 |
fun beta_conversion ct = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
862 |
let val Cterm {sign_ref, t, T, maxidx} = ct |
0 | 863 |
in case t of |
1238 | 864 |
Abs(_,_,bodt) $ u => fix_shyps [] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
865 |
(Thm{sign_ref = sign_ref, |
2386 | 866 |
der = infer_derivs (Beta_conversion ct, []), |
867 |
maxidx = maxidx, |
|
868 |
shyps = [], |
|
869 |
hyps = [], |
|
870 |
prop = Logic.mk_equals(t, subst_bound (u,bodt))}) |
|
250 | 871 |
| _ => raise THM("beta_conversion: not a redex", 0, []) |
0 | 872 |
end; |
873 |
||
874 |
(*The extensionality rule (proviso: x not free in f, g, or hypotheses) |
|
1220 | 875 |
f(x) == g(x) |
876 |
------------ |
|
877 |
f == g |
|
878 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
879 |
fun extensional (th as Thm{sign_ref, der, maxidx,shyps,hyps,prop}) = |
0 | 880 |
case prop of |
881 |
(Const("==",_)) $ (f$x) $ (g$y) => |
|
250 | 882 |
let fun err(msg) = raise THM("extensional: "^msg, 0, [th]) |
0 | 883 |
in (if x<>y then err"different variables" else |
884 |
case y of |
|
250 | 885 |
Free _ => |
886 |
if exists (apl(y, Logic.occs)) (f::g::hyps) |
|
887 |
then err"variable free in hyps or functions" else () |
|
888 |
| Var _ => |
|
889 |
if Logic.occs(y,f) orelse Logic.occs(y,g) |
|
890 |
then err"variable free in functions" else () |
|
891 |
| _ => err"not a variable"); |
|
1238 | 892 |
(*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
893 |
Thm{sign_ref = sign_ref, |
2386 | 894 |
der = infer_derivs (Extensional, [der]), |
895 |
maxidx = maxidx, |
|
896 |
shyps = shyps, |
|
897 |
hyps = hyps, |
|
1529 | 898 |
prop = Logic.mk_equals(f,g)} |
0 | 899 |
end |
900 |
| _ => raise THM("extensional: premise", 0, [th]); |
|
901 |
||
902 |
(*The abstraction rule. The Free or Var x must not be free in the hypotheses. |
|
903 |
The bound variable will be named "a" (since x will be something like x320) |
|
1220 | 904 |
t == u |
905 |
------------ |
|
906 |
%x.t == %x.u |
|
907 |
*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
908 |
fun abstract_rule a cx (th as Thm{sign_ref,der,maxidx,hyps,prop,...}) = |
229
4002c4cd450c
Pure: MAJOR CHANGE. Moved ML types ctyp and cterm and their associated
lcp
parents:
225
diff
changeset
|
909 |
let val x = term_of cx; |
250 | 910 |
val (t,u) = Logic.dest_equals prop |
911 |
handle TERM _ => |
|
912 |
raise THM("abstract_rule: premise not an equality", 0, [th]) |
|
1238 | 913 |
fun result T = fix_shyps [th] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
914 |
(Thm{sign_ref = sign_ref, |
2386 | 915 |
der = infer_derivs (Abstract_rule (a,cx), [der]), |
916 |
maxidx = maxidx, |
|
917 |
shyps = [], |
|
918 |
hyps = hyps, |
|
919 |
prop = Logic.mk_equals(Abs(a, T, abstract_over (x,t)), |
|
920 |
Abs(a, T, abstract_over (x,u)))}) |
|
0 | 921 |
in case x of |
250 | 922 |
Free(_,T) => |
923 |
if exists (apl(x, Logic.occs)) hyps |
|
924 |
then raise THM("abstract_rule: variable free in assumptions", 0, [th]) |
|
925 |
else result T |
|
0 | 926 |
| Var(_,T) => result T |
927 |
| _ => raise THM("abstract_rule: not a variable", 0, [th]) |
|
928 |
end; |
|
929 |
||
930 |
(*The combination rule |
|
3529 | 931 |
f == g t == u |
932 |
-------------- |
|
933 |
f(t) == g(u) |
|
1220 | 934 |
*) |
0 | 935 |
fun combination th1 th2 = |
1529 | 936 |
let val Thm{der=der1, maxidx=max1, shyps=shyps1, hyps=hyps1, |
2386 | 937 |
prop=prop1,...} = th1 |
1529 | 938 |
and Thm{der=der2, maxidx=max2, shyps=shyps2, hyps=hyps2, |
2386 | 939 |
prop=prop2,...} = th2 |
1836
861e29c7cada
Added type-checking to rule "combination". This corrects a fault
paulson
parents:
1802
diff
changeset
|
940 |
fun chktypes (f,t) = |
2386 | 941 |
(case fastype_of f of |
942 |
Type("fun",[T1,T2]) => |
|
943 |
if T1 <> fastype_of t then |
|
944 |
raise THM("combination: types", 0, [th1,th2]) |
|
945 |
else () |
|
946 |
| _ => raise THM("combination: not function type", 0, |
|
947 |
[th1,th2])) |
|
1495
b8b54847c77f
Added check for duplicate vars with distinct types/sorts (nodup_Vars)
nipkow
parents:
1493
diff
changeset
|
948 |
in case (prop1,prop2) of |
0 | 949 |
(Const("==",_) $ f $ g, Const("==",_) $ t $ u) => |
1836
861e29c7cada
Added type-checking to rule "combination". This corrects a fault
paulson
parents:
1802
diff
changeset
|
950 |
let val _ = chktypes (f,t) |
2386 | 951 |
val thm = (*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
952 |
Thm{sign_ref = merge_thm_sgs(th1,th2), |
2386 | 953 |
der = infer_derivs (Combination, [der1, der2]), |
954 |
maxidx = Int.max(max1,max2), |
|
955 |
shyps = union_sort(shyps1,shyps2), |
|
956 |
hyps = union_term(hyps1,hyps2), |
|
957 |
prop = Logic.mk_equals(f$t, g$u)} |
|
2139
2c59b204b540
Only calls nodup_Vars if really necessary. We get a speedup of nearly 6%
paulson
parents:
2047
diff
changeset
|
958 |
in if max1 >= 0 andalso max2 >= 0 |
2c59b204b540
Only calls nodup_Vars if really necessary. We get a speedup of nearly 6%
paulson
parents:
2047
diff
changeset
|
959 |
then nodup_Vars thm "combination" |
2386 | 960 |
else thm (*no new Vars: no expensive check!*) |
2139
2c59b204b540
Only calls nodup_Vars if really necessary. We get a speedup of nearly 6%
paulson
parents:
2047
diff
changeset
|
961 |
end |
0 | 962 |
| _ => raise THM("combination: premises", 0, [th1,th2]) |
963 |
end; |
|
964 |
||
965 |
||
966 |
(* Equality introduction |
|
3529 | 967 |
A ==> B B ==> A |
968 |
---------------- |
|
969 |
A == B |
|
1220 | 970 |
*) |
0 | 971 |
fun equal_intr th1 th2 = |
1529 | 972 |
let val Thm{der=der1,maxidx=max1, shyps=shyps1, hyps=hyps1, |
2386 | 973 |
prop=prop1,...} = th1 |
1529 | 974 |
and Thm{der=der2, maxidx=max2, shyps=shyps2, hyps=hyps2, |
2386 | 975 |
prop=prop2,...} = th2; |
1529 | 976 |
fun err(msg) = raise THM("equal_intr: "^msg, 0, [th1,th2]) |
977 |
in case (prop1,prop2) of |
|
978 |
(Const("==>",_) $ A $ B, Const("==>",_) $ B' $ A') => |
|
2386 | 979 |
if A aconv A' andalso B aconv B' |
980 |
then |
|
981 |
(*no fix_shyps*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
982 |
Thm{sign_ref = merge_thm_sgs(th1,th2), |
2386 | 983 |
der = infer_derivs (Equal_intr, [der1, der2]), |
984 |
maxidx = Int.max(max1,max2), |
|
985 |
shyps = union_sort(shyps1,shyps2), |
|
986 |
hyps = union_term(hyps1,hyps2), |
|
987 |
prop = Logic.mk_equals(A,B)} |
|
988 |
else err"not equal" |
|
1529 | 989 |
| _ => err"premises" |
990 |
end; |
|
991 |
||
992 |
||
993 |
(*The equal propositions rule |
|
3529 | 994 |
A == B A |
1529 | 995 |
--------- |
996 |
B |
|
997 |
*) |
|
998 |
fun equal_elim th1 th2 = |
|
999 |
let val Thm{der=der1, maxidx=max1, hyps=hyps1, prop=prop1,...} = th1 |
|
1000 |
and Thm{der=der2, maxidx=max2, hyps=hyps2, prop=prop2,...} = th2; |
|
1001 |
fun err(msg) = raise THM("equal_elim: "^msg, 0, [th1,th2]) |
|
1002 |
in case prop1 of |
|
1003 |
Const("==",_) $ A $ B => |
|
1004 |
if not (prop2 aconv A) then err"not equal" else |
|
1005 |
fix_shyps [th1, th2] [] |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1006 |
(Thm{sign_ref= merge_thm_sgs(th1,th2), |
2386 | 1007 |
der = infer_derivs (Equal_elim, [der1, der2]), |
1008 |
maxidx = Int.max(max1,max2), |
|
1009 |
shyps = [], |
|
1010 |
hyps = union_term(hyps1,hyps2), |
|
1011 |
prop = B}) |
|
1529 | 1012 |
| _ => err"major premise" |
1013 |
end; |
|
0 | 1014 |
|
1220 | 1015 |
|
1016 |
||
0 | 1017 |
(**** Derived rules ****) |
1018 |
||
1503 | 1019 |
(*Discharge all hypotheses. Need not verify cterms or call fix_shyps. |
0 | 1020 |
Repeated hypotheses are discharged only once; fold cannot do this*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1021 |
fun implies_intr_hyps (Thm{sign_ref, der, maxidx, shyps, hyps=A::As, prop}) = |
1238 | 1022 |
implies_intr_hyps (*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1023 |
(Thm{sign_ref = sign_ref, |
2386 | 1024 |
der = infer_derivs (Implies_intr_hyps, [der]), |
1025 |
maxidx = maxidx, |
|
1026 |
shyps = shyps, |
|
1529 | 1027 |
hyps = disch(As,A), |
2386 | 1028 |
prop = implies$A$prop}) |
0 | 1029 |
| implies_intr_hyps th = th; |
1030 |
||
1031 |
(*Smash" unifies the list of term pairs leaving no flex-flex pairs. |
|
250 | 1032 |
Instantiates the theorem and deletes trivial tpairs. |
0 | 1033 |
Resulting sequence may contain multiple elements if the tpairs are |
1034 |
not all flex-flex. *) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1035 |
fun flexflex_rule (th as Thm{sign_ref, der, maxidx, hyps, prop,...}) = |
250 | 1036 |
let fun newthm env = |
1529 | 1037 |
if Envir.is_empty env then th |
1038 |
else |
|
250 | 1039 |
let val (tpairs,horn) = |
1040 |
Logic.strip_flexpairs (Envir.norm_term env prop) |
|
1041 |
(*Remove trivial tpairs, of the form t=t*) |
|
1042 |
val distpairs = filter (not o op aconv) tpairs |
|
1043 |
val newprop = Logic.list_flexpairs(distpairs, horn) |
|
1220 | 1044 |
in fix_shyps [th] (env_codT env) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1045 |
(Thm{sign_ref = sign_ref, |
2386 | 1046 |
der = infer_derivs (Flexflex_rule env, [der]), |
1047 |
maxidx = maxidx_of_term newprop, |
|
1048 |
shyps = [], |
|
1049 |
hyps = hyps, |
|
1050 |
prop = newprop}) |
|
250 | 1051 |
end; |
0 | 1052 |
val (tpairs,_) = Logic.strip_flexpairs prop |
4270 | 1053 |
in Seq.map newthm |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1054 |
(Unify.smash_unifiers(Sign.deref sign_ref, Envir.empty maxidx, tpairs)) |
0 | 1055 |
end; |
1056 |
||
1057 |
(*Instantiation of Vars |
|
1220 | 1058 |
A |
1059 |
------------------- |
|
1060 |
A[t1/v1,....,tn/vn] |
|
1061 |
*) |
|
0 | 1062 |
|
1063 |
(*Check that all the terms are Vars and are distinct*) |
|
1064 |
fun instl_ok ts = forall is_Var ts andalso null(findrep ts); |
|
1065 |
||
1066 |
(*For instantiate: process pair of cterms, merge theories*) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1067 |
fun add_ctpair ((ct,cu), (sign_ref,tpairs)) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1068 |
let val Cterm {sign_ref=sign_reft, t=t, T= T, ...} = ct |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1069 |
and Cterm {sign_ref=sign_refu, t=u, T= U, ...} = cu |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1070 |
in |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1071 |
if T=U then |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1072 |
(Sign.merge_refs (sign_ref, Sign.merge_refs (sign_reft, sign_refu)), (t,u)::tpairs) |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1073 |
else raise TYPE("add_ctpair", [T,U], [t,u]) |
0 | 1074 |
end; |
1075 |
||
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1076 |
fun add_ctyp ((v,ctyp), (sign_ref',vTs)) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1077 |
let val Ctyp {T,sign_ref} = ctyp |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1078 |
in (Sign.merge_refs(sign_ref,sign_ref'), (v,T)::vTs) end; |
0 | 1079 |
|
1080 |
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...]. |
|
1081 |
Instantiates distinct Vars by terms of same type. |
|
1082 |
Normalizes the new theorem! *) |
|
1529 | 1083 |
fun instantiate ([], []) th = th |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1084 |
| instantiate (vcTs,ctpairs) (th as Thm{sign_ref,der,maxidx,hyps,prop,...}) = |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1085 |
let val (newsign_ref,tpairs) = foldr add_ctpair (ctpairs, (sign_ref,[])); |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1086 |
val (newsign_ref,vTs) = foldr add_ctyp (vcTs, (newsign_ref,[])); |
250 | 1087 |
val newprop = |
1088 |
Envir.norm_term (Envir.empty 0) |
|
1089 |
(subst_atomic tpairs |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1090 |
(Type.inst_term_tvars(Sign.tsig_of (Sign.deref newsign_ref),vTs) prop)) |
1220 | 1091 |
val newth = |
1092 |
fix_shyps [th] (map snd vTs) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1093 |
(Thm{sign_ref = newsign_ref, |
2386 | 1094 |
der = infer_derivs (Instantiate(vcTs,ctpairs), [der]), |
1095 |
maxidx = maxidx_of_term newprop, |
|
1096 |
shyps = [], |
|
1097 |
hyps = hyps, |
|
1098 |
prop = newprop}) |
|
250 | 1099 |
in if not(instl_ok(map #1 tpairs)) |
193 | 1100 |
then raise THM("instantiate: variables not distinct", 0, [th]) |
1101 |
else if not(null(findrep(map #1 vTs))) |
|
1102 |
then raise THM("instantiate: type variables not distinct", 0, [th]) |
|
2147 | 1103 |
else nodup_Vars newth "instantiate" |
0 | 1104 |
end |
250 | 1105 |
handle TERM _ => |
0 | 1106 |
raise THM("instantiate: incompatible signatures",0,[th]) |
2671 | 1107 |
| TYPE (msg,_,_) => raise THM("instantiate: type conflict: " ^ msg, |
1108 |
0, [th]); |
|
0 | 1109 |
|
1110 |
(*The trivial implication A==>A, justified by assume and forall rules. |
|
1111 |
A can contain Vars, not so for assume! *) |
|
250 | 1112 |
fun trivial ct : thm = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1113 |
let val Cterm {sign_ref, t=A, T, maxidx} = ct |
250 | 1114 |
in if T<>propT then |
1115 |
raise THM("trivial: the term must have type prop", 0, []) |
|
1238 | 1116 |
else fix_shyps [] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1117 |
(Thm{sign_ref = sign_ref, |
2386 | 1118 |
der = infer_derivs (Trivial ct, []), |
1119 |
maxidx = maxidx, |
|
1120 |
shyps = [], |
|
1121 |
hyps = [], |
|
1122 |
prop = implies$A$A}) |
|
0 | 1123 |
end; |
1124 |
||
1503 | 1125 |
(*Axiom-scheme reflecting signature contents: "OFCLASS(?'a::c, c_class)" *) |
399
86cc2b98f9e0
added class_triv: theory -> class -> thm (for axclasses);
wenzelm
parents:
387
diff
changeset
|
1126 |
fun class_triv thy c = |
1529 | 1127 |
let val sign = sign_of thy; |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1128 |
val Cterm {sign_ref, t, maxidx, ...} = |
2386 | 1129 |
cterm_of sign (Logic.mk_inclass (TVar (("'a", 0), [c]), c)) |
1130 |
handle TERM (msg, _) => raise THM ("class_triv: " ^ msg, 0, []); |
|
399
86cc2b98f9e0
added class_triv: theory -> class -> thm (for axclasses);
wenzelm
parents:
387
diff
changeset
|
1131 |
in |
1238 | 1132 |
fix_shyps [] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1133 |
(Thm {sign_ref = sign_ref, |
4182 | 1134 |
der = infer_derivs (Class_triv c, []), |
2386 | 1135 |
maxidx = maxidx, |
1136 |
shyps = [], |
|
1137 |
hyps = [], |
|
1138 |
prop = t}) |
|
399
86cc2b98f9e0
added class_triv: theory -> class -> thm (for axclasses);
wenzelm
parents:
387
diff
changeset
|
1139 |
end; |
86cc2b98f9e0
added class_triv: theory -> class -> thm (for axclasses);
wenzelm
parents:
387
diff
changeset
|
1140 |
|
86cc2b98f9e0
added class_triv: theory -> class -> thm (for axclasses);
wenzelm
parents:
387
diff
changeset
|
1141 |
|
0 | 1142 |
(* Replace all TFrees not in the hyps by new TVars *) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1143 |
fun varifyT(Thm{sign_ref,der,maxidx,shyps,hyps,prop}) = |
0 | 1144 |
let val tfrees = foldr add_term_tfree_names (hyps,[]) |
1634 | 1145 |
in let val thm = (*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1146 |
Thm{sign_ref = sign_ref, |
2386 | 1147 |
der = infer_derivs (VarifyT, [der]), |
1148 |
maxidx = Int.max(0,maxidx), |
|
1149 |
shyps = shyps, |
|
1150 |
hyps = hyps, |
|
1529 | 1151 |
prop = Type.varify(prop,tfrees)} |
2147 | 1152 |
in nodup_Vars thm "varifyT" end |
1634 | 1153 |
(* this nodup_Vars check can be removed if thms are guaranteed not to contain |
1154 |
duplicate TVars with differnt sorts *) |
|
0 | 1155 |
end; |
1156 |
||
1157 |
(* Replace all TVars by new TFrees *) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1158 |
fun freezeT(Thm{sign_ref,der,maxidx,shyps,hyps,prop}) = |
3410 | 1159 |
let val (prop',_) = Type.freeze_thaw prop |
1238 | 1160 |
in (*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1161 |
Thm{sign_ref = sign_ref, |
2386 | 1162 |
der = infer_derivs (FreezeT, [der]), |
1163 |
maxidx = maxidx_of_term prop', |
|
1164 |
shyps = shyps, |
|
1165 |
hyps = hyps, |
|
1529 | 1166 |
prop = prop'} |
1220 | 1167 |
end; |
0 | 1168 |
|
1169 |
||
1170 |
(*** Inference rules for tactics ***) |
|
1171 |
||
1172 |
(*Destruct proof state into constraints, other goals, goal(i), rest *) |
|
1173 |
fun dest_state (state as Thm{prop,...}, i) = |
|
1174 |
let val (tpairs,horn) = Logic.strip_flexpairs prop |
|
1175 |
in case Logic.strip_prems(i, [], horn) of |
|
1176 |
(B::rBs, C) => (tpairs, rev rBs, B, C) |
|
1177 |
| _ => raise THM("dest_state", i, [state]) |
|
1178 |
end |
|
1179 |
handle TERM _ => raise THM("dest_state", i, [state]); |
|
1180 |
||
309 | 1181 |
(*Increment variables and parameters of orule as required for |
0 | 1182 |
resolution with goal i of state. *) |
1183 |
fun lift_rule (state, i) orule = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1184 |
let val Thm{shyps=sshyps, prop=sprop, maxidx=smax, sign_ref=ssign_ref,...} = state |
0 | 1185 |
val (Bi::_, _) = Logic.strip_prems(i, [], Logic.skip_flexpairs sprop) |
1529 | 1186 |
handle TERM _ => raise THM("lift_rule", i, [orule,state]) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1187 |
val ct_Bi = Cterm {sign_ref=ssign_ref, maxidx=smax, T=propT, t=Bi} |
1529 | 1188 |
val (lift_abs,lift_all) = Logic.lift_fns(Bi,smax+1) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1189 |
val (Thm{sign_ref, der, maxidx,shyps,hyps,prop}) = orule |
0 | 1190 |
val (tpairs,As,B) = Logic.strip_horn prop |
1238 | 1191 |
in (*no fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1192 |
Thm{sign_ref = merge_thm_sgs(state,orule), |
2386 | 1193 |
der = infer_derivs (Lift_rule(ct_Bi, i), [der]), |
1194 |
maxidx = maxidx+smax+1, |
|
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
1195 |
shyps=union_sort(sshyps,shyps), |
2386 | 1196 |
hyps=hyps, |
1529 | 1197 |
prop = Logic.rule_of (map (pairself lift_abs) tpairs, |
2386 | 1198 |
map lift_all As, |
1199 |
lift_all B)} |
|
0 | 1200 |
end; |
1201 |
||
1202 |
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. *) |
|
1203 |
fun assumption i state = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1204 |
let val Thm{sign_ref,der,maxidx,hyps,prop,...} = state; |
0 | 1205 |
val (tpairs, Bs, Bi, C) = dest_state(state,i) |
250 | 1206 |
fun newth (env as Envir.Envir{maxidx, ...}, tpairs) = |
1220 | 1207 |
fix_shyps [state] (env_codT env) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1208 |
(Thm{sign_ref = sign_ref, |
2386 | 1209 |
der = infer_derivs (Assumption (i, Some env), [der]), |
1210 |
maxidx = maxidx, |
|
1211 |
shyps = [], |
|
1212 |
hyps = hyps, |
|
1213 |
prop = |
|
1214 |
if Envir.is_empty env then (*avoid wasted normalizations*) |
|
1215 |
Logic.rule_of (tpairs, Bs, C) |
|
1216 |
else (*normalize the new rule fully*) |
|
1217 |
Envir.norm_term env (Logic.rule_of (tpairs, Bs, C))}); |
|
4270 | 1218 |
fun addprfs [] = Seq.empty |
1219 |
| addprfs ((t,u)::apairs) = Seq.make (fn()=> Seq.pull |
|
1220 |
(Seq.mapp newth |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1221 |
(Unify.unifiers(Sign.deref sign_ref,Envir.empty maxidx, (t,u)::tpairs)) |
250 | 1222 |
(addprfs apairs))) |
0 | 1223 |
in addprfs (Logic.assum_pairs Bi) end; |
1224 |
||
250 | 1225 |
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. |
0 | 1226 |
Checks if Bi's conclusion is alpha-convertible to one of its assumptions*) |
1227 |
fun eq_assumption i state = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1228 |
let val Thm{sign_ref,der,maxidx,hyps,prop,...} = state; |
0 | 1229 |
val (tpairs, Bs, Bi, C) = dest_state(state,i) |
1230 |
in if exists (op aconv) (Logic.assum_pairs Bi) |
|
1220 | 1231 |
then fix_shyps [state] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1232 |
(Thm{sign_ref = sign_ref, |
2386 | 1233 |
der = infer_derivs (Assumption (i,None), [der]), |
1234 |
maxidx = maxidx, |
|
1235 |
shyps = [], |
|
1236 |
hyps = hyps, |
|
1237 |
prop = Logic.rule_of(tpairs, Bs, C)}) |
|
0 | 1238 |
else raise THM("eq_assumption", 0, [state]) |
1239 |
end; |
|
1240 |
||
1241 |
||
2671 | 1242 |
(*For rotate_tac: fast rotation of assumptions of subgoal i*) |
1243 |
fun rotate_rule k i state = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1244 |
let val Thm{sign_ref,der,maxidx,hyps,prop,shyps} = state; |
2671 | 1245 |
val (tpairs, Bs, Bi, C) = dest_state(state,i) |
1246 |
val params = Logic.strip_params Bi |
|
1247 |
and asms = Logic.strip_assums_hyp Bi |
|
1248 |
and concl = Logic.strip_assums_concl Bi |
|
1249 |
val n = length asms |
|
1250 |
fun rot m = if 0=m orelse m=n then Bi |
|
1251 |
else if 0<m andalso m<n |
|
1252 |
then list_all |
|
1253 |
(params, |
|
1254 |
Logic.list_implies(List.drop(asms, m) @ |
|
1255 |
List.take(asms, m), |
|
1256 |
concl)) |
|
1257 |
else raise THM("rotate_rule", m, [state]) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1258 |
in Thm{sign_ref = sign_ref, |
2671 | 1259 |
der = infer_derivs (Rotate_rule (k,i), [der]), |
1260 |
maxidx = maxidx, |
|
1261 |
shyps = shyps, |
|
1262 |
hyps = hyps, |
|
1263 |
prop = Logic.rule_of(tpairs, Bs@[rot (if k<0 then n+k else k)], C)} |
|
1264 |
end; |
|
1265 |
||
1266 |
||
0 | 1267 |
(** User renaming of parameters in a subgoal **) |
1268 |
||
1269 |
(*Calls error rather than raising an exception because it is intended |
|
1270 |
for top-level use -- exception handling would not make sense here. |
|
1271 |
The names in cs, if distinct, are used for the innermost parameters; |
|
1272 |
preceding parameters may be renamed to make all params distinct.*) |
|
1273 |
fun rename_params_rule (cs, i) state = |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1274 |
let val Thm{sign_ref,der,maxidx,hyps,...} = state |
0 | 1275 |
val (tpairs, Bs, Bi, C) = dest_state(state,i) |
1276 |
val iparams = map #1 (Logic.strip_params Bi) |
|
1277 |
val short = length iparams - length cs |
|
250 | 1278 |
val newnames = |
1279 |
if short<0 then error"More names than abstractions!" |
|
1280 |
else variantlist(take (short,iparams), cs) @ cs |
|
3037
99ed078e6ae7
rename_params_rule used to check if the new name clashed with a free name in
nipkow
parents:
3012
diff
changeset
|
1281 |
val freenames = map (#1 o dest_Free) (term_frees Bi) |
0 | 1282 |
val newBi = Logic.list_rename_params (newnames, Bi) |
250 | 1283 |
in |
0 | 1284 |
case findrep cs of |
3565
c64410e701fb
Now rename_params_rule merely issues warnings--and does nothing--if the
paulson
parents:
3558
diff
changeset
|
1285 |
c::_ => (warning ("Can't rename. Bound variables not distinct: " ^ c); |
c64410e701fb
Now rename_params_rule merely issues warnings--and does nothing--if the
paulson
parents:
3558
diff
changeset
|
1286 |
state) |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1569
diff
changeset
|
1287 |
| [] => (case cs inter_string freenames of |
3565
c64410e701fb
Now rename_params_rule merely issues warnings--and does nothing--if the
paulson
parents:
3558
diff
changeset
|
1288 |
a::_ => (warning ("Can't rename. Bound/Free variable clash: " ^ a); |
c64410e701fb
Now rename_params_rule merely issues warnings--and does nothing--if the
paulson
parents:
3558
diff
changeset
|
1289 |
state) |
1220 | 1290 |
| [] => fix_shyps [state] [] |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1291 |
(Thm{sign_ref = sign_ref, |
2386 | 1292 |
der = infer_derivs (Rename_params_rule(cs,i), [der]), |
1293 |
maxidx = maxidx, |
|
1294 |
shyps = [], |
|
1295 |
hyps = hyps, |
|
1296 |
prop = Logic.rule_of(tpairs, Bs@[newBi], C)})) |
|
0 | 1297 |
end; |
1298 |
||
1299 |
(*** Preservation of bound variable names ***) |
|
1300 |
||
250 | 1301 |
(*Scan a pair of terms; while they are similar, |
0 | 1302 |
accumulate corresponding bound vars in "al"*) |
1238 | 1303 |
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = |
1195
686e3eb613b9
match_bvs no longer puts a name in the alist if it is null ("")
lcp
parents:
1160
diff
changeset
|
1304 |
match_bvs(s, t, if x="" orelse y="" then al |
1238 | 1305 |
else (x,y)::al) |
0 | 1306 |
| match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al)) |
1307 |
| match_bvs(_,_,al) = al; |
|
1308 |
||
1309 |
(* strip abstractions created by parameters *) |
|
1310 |
fun match_bvars((s,t),al) = match_bvs(strip_abs_body s, strip_abs_body t, al); |
|
1311 |
||
1312 |
||
250 | 1313 |
(* strip_apply f A(,B) strips off all assumptions/parameters from A |
0 | 1314 |
introduced by lifting over B, and applies f to remaining part of A*) |
1315 |
fun strip_apply f = |
|
1316 |
let fun strip(Const("==>",_)$ A1 $ B1, |
|
250 | 1317 |
Const("==>",_)$ _ $ B2) = implies $ A1 $ strip(B1,B2) |
1318 |
| strip((c as Const("all",_)) $ Abs(a,T,t1), |
|
1319 |
Const("all",_) $ Abs(_,_,t2)) = c$Abs(a,T,strip(t1,t2)) |
|
1320 |
| strip(A,_) = f A |
|
0 | 1321 |
in strip end; |
1322 |
||
1323 |
(*Use the alist to rename all bound variables and some unknowns in a term |
|
1324 |
dpairs = current disagreement pairs; tpairs = permanent ones (flexflex); |
|
1325 |
Preserves unknowns in tpairs and on lhs of dpairs. *) |
|
1326 |
fun rename_bvs([],_,_,_) = I |
|
1327 |
| rename_bvs(al,dpairs,tpairs,B) = |
|
250 | 1328 |
let val vars = foldr add_term_vars |
1329 |
(map fst dpairs @ map fst tpairs @ map snd tpairs, []) |
|
1330 |
(*unknowns appearing elsewhere be preserved!*) |
|
1331 |
val vids = map (#1 o #1 o dest_Var) vars; |
|
1332 |
fun rename(t as Var((x,i),T)) = |
|
1333 |
(case assoc(al,x) of |
|
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1569
diff
changeset
|
1334 |
Some(y) => if x mem_string vids orelse y mem_string vids then t |
250 | 1335 |
else Var((y,i),T) |
1336 |
| None=> t) |
|
0 | 1337 |
| rename(Abs(x,T,t)) = |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1569
diff
changeset
|
1338 |
Abs(case assoc_string(al,x) of Some(y) => y | None => x, |
250 | 1339 |
T, rename t) |
0 | 1340 |
| rename(f$t) = rename f $ rename t |
1341 |
| rename(t) = t; |
|
250 | 1342 |
fun strip_ren Ai = strip_apply rename (Ai,B) |
0 | 1343 |
in strip_ren end; |
1344 |
||
1345 |
(*Function to rename bounds/unknowns in the argument, lifted over B*) |
|
1346 |
fun rename_bvars(dpairs, tpairs, B) = |
|
250 | 1347 |
rename_bvs(foldr match_bvars (dpairs,[]), dpairs, tpairs, B); |
0 | 1348 |
|
1349 |
||
1350 |
(*** RESOLUTION ***) |
|
1351 |
||
721
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1352 |
(** Lifting optimizations **) |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1353 |
|
0 | 1354 |
(*strip off pairs of assumptions/parameters in parallel -- they are |
1355 |
identical because of lifting*) |
|
250 | 1356 |
fun strip_assums2 (Const("==>", _) $ _ $ B1, |
1357 |
Const("==>", _) $ _ $ B2) = strip_assums2 (B1,B2) |
|
0 | 1358 |
| strip_assums2 (Const("all",_)$Abs(a,T,t1), |
250 | 1359 |
Const("all",_)$Abs(_,_,t2)) = |
0 | 1360 |
let val (B1,B2) = strip_assums2 (t1,t2) |
1361 |
in (Abs(a,T,B1), Abs(a,T,B2)) end |
|
1362 |
| strip_assums2 BB = BB; |
|
1363 |
||
1364 |
||
721
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1365 |
(*Faster normalization: skip assumptions that were lifted over*) |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1366 |
fun norm_term_skip env 0 t = Envir.norm_term env t |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1367 |
| norm_term_skip env n (Const("all",_)$Abs(a,T,t)) = |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1368 |
let val Envir.Envir{iTs, ...} = env |
1238 | 1369 |
val T' = typ_subst_TVars iTs T |
1370 |
(*Must instantiate types of parameters because they are flattened; |
|
721
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1371 |
this could be a NEW parameter*) |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1372 |
in all T' $ Abs(a, T', norm_term_skip env n t) end |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1373 |
| norm_term_skip env n (Const("==>", _) $ A $ B) = |
1238 | 1374 |
implies $ A $ norm_term_skip env (n-1) B |
721
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1375 |
| norm_term_skip env n t = error"norm_term_skip: too few assumptions??"; |
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1376 |
|
479832ff2d29
Pure/thm/norm_term_skip: new, for skipping normalization of the context
lcp
parents:
678
diff
changeset
|
1377 |
|
0 | 1378 |
(*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C) |
250 | 1379 |
Unifies B with Bi, replacing subgoal i (1 <= i <= n) |
0 | 1380 |
If match then forbid instantiations in proof state |
1381 |
If lifted then shorten the dpair using strip_assums2. |
|
1382 |
If eres_flg then simultaneously proves A1 by assumption. |
|
250 | 1383 |
nsubgoal is the number of new subgoals (written m above). |
0 | 1384 |
Curried so that resolution calls dest_state only once. |
1385 |
*) |
|
4270 | 1386 |
local exception COMPOSE |
0 | 1387 |
in |
250 | 1388 |
fun bicompose_aux match (state, (stpairs, Bs, Bi, C), lifted) |
0 | 1389 |
(eres_flg, orule, nsubgoal) = |
1529 | 1390 |
let val Thm{der=sder, maxidx=smax, shyps=sshyps, hyps=shyps, ...} = state |
1391 |
and Thm{der=rder, maxidx=rmax, shyps=rshyps, hyps=rhyps, |
|
2386 | 1392 |
prop=rprop,...} = orule |
1529 | 1393 |
(*How many hyps to skip over during normalization*) |
1238 | 1394 |
and nlift = Logic.count_prems(strip_all_body Bi, |
1395 |
if eres_flg then ~1 else 0) |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1396 |
val sign_ref = merge_thm_sgs(state,orule); |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1397 |
val sign = Sign.deref sign_ref; |
0 | 1398 |
(** Add new theorem with prop = '[| Bs; As |] ==> C' to thq **) |
250 | 1399 |
fun addth As ((env as Envir.Envir {maxidx, ...}, tpairs), thq) = |
1400 |
let val normt = Envir.norm_term env; |
|
1401 |
(*perform minimal copying here by examining env*) |
|
1402 |
val normp = |
|
1403 |
if Envir.is_empty env then (tpairs, Bs @ As, C) |
|
1404 |
else |
|
1405 |
let val ntps = map (pairself normt) tpairs |
|
2147 | 1406 |
in if Envir.above (smax, env) then |
1238 | 1407 |
(*no assignments in state; normalize the rule only*) |
1408 |
if lifted |
|
1409 |
then (ntps, Bs @ map (norm_term_skip env nlift) As, C) |
|
1410 |
else (ntps, Bs @ map normt As, C) |
|
1529 | 1411 |
else if match then raise COMPOSE |
250 | 1412 |
else (*normalize the new rule fully*) |
1413 |
(ntps, map normt (Bs @ As), normt C) |
|
1414 |
end |
|
1258 | 1415 |
val th = (*tuned fix_shyps*) |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1416 |
Thm{sign_ref = sign_ref, |
2386 | 1417 |
der = infer_derivs (Bicompose(match, eres_flg, |
1418 |
1 + length Bs, nsubgoal, env), |
|
1419 |
[rder,sder]), |
|
1420 |
maxidx = maxidx, |
|
1421 |
shyps = add_env_sorts (env, union_sort(rshyps,sshyps)), |
|
1422 |
hyps = union_term(rhyps,shyps), |
|
1423 |
prop = Logic.rule_of normp} |
|
4270 | 1424 |
in Seq.cons(th, thq) end handle COMPOSE => thq |
0 | 1425 |
val (rtpairs,rhorn) = Logic.strip_flexpairs(rprop); |
1426 |
val (rAs,B) = Logic.strip_prems(nsubgoal, [], rhorn) |
|
1427 |
handle TERM _ => raise THM("bicompose: rule", 0, [orule,state]); |
|
1428 |
(*Modify assumptions, deleting n-th if n>0 for e-resolution*) |
|
1429 |
fun newAs(As0, n, dpairs, tpairs) = |
|
1430 |
let val As1 = if !Logic.auto_rename orelse not lifted then As0 |
|
250 | 1431 |
else map (rename_bvars(dpairs,tpairs,B)) As0 |
0 | 1432 |
in (map (Logic.flatten_params n) As1) |
250 | 1433 |
handle TERM _ => |
1434 |
raise THM("bicompose: 1st premise", 0, [orule]) |
|
0 | 1435 |
end; |
2147 | 1436 |
val env = Envir.empty(Int.max(rmax,smax)); |
0 | 1437 |
val BBi = if lifted then strip_assums2(B,Bi) else (B,Bi); |
1438 |
val dpairs = BBi :: (rtpairs@stpairs); |
|
1439 |
(*elim-resolution: try each assumption in turn. Initially n=1*) |
|
4270 | 1440 |
fun tryasms (_, _, []) = Seq.empty |
0 | 1441 |
| tryasms (As, n, (t,u)::apairs) = |
4270 | 1442 |
(case Seq.pull(Unify.unifiers(sign, env, (t,u)::dpairs)) of |
250 | 1443 |
None => tryasms (As, n+1, apairs) |
1444 |
| cell as Some((_,tpairs),_) => |
|
4270 | 1445 |
Seq.it_right (addth (newAs(As, n, [BBi,(u,t)], tpairs))) |
1446 |
(Seq.make (fn()=> cell), |
|
1447 |
Seq.make (fn()=> Seq.pull (tryasms (As, n+1, apairs))))); |
|
0 | 1448 |
fun eres [] = raise THM("bicompose: no premises", 0, [orule,state]) |
1449 |
| eres (A1::As) = tryasms (As, 1, Logic.assum_pairs A1); |
|
1450 |
(*ordinary resolution*) |
|
4270 | 1451 |
fun res(None) = Seq.empty |
250 | 1452 |
| res(cell as Some((_,tpairs),_)) = |
4270 | 1453 |
Seq.it_right (addth(newAs(rev rAs, 0, [BBi], tpairs))) |
1454 |
(Seq.make (fn()=> cell), Seq.empty) |
|
0 | 1455 |
in if eres_flg then eres(rev rAs) |
4270 | 1456 |
else res(Seq.pull(Unify.unifiers(sign, env, dpairs))) |
0 | 1457 |
end; |
1458 |
end; (*open Sequence*) |
|
1459 |
||
1460 |
||
1461 |
fun bicompose match arg i state = |
|
1462 |
bicompose_aux match (state, dest_state(state,i), false) arg; |
|
1463 |
||
1464 |
(*Quick test whether rule is resolvable with the subgoal with hyps Hs |
|
1465 |
and conclusion B. If eres_flg then checks 1st premise of rule also*) |
|
1466 |
fun could_bires (Hs, B, eres_flg, rule) = |
|
1467 |
let fun could_reshyp (A1::_) = exists (apl(A1,could_unify)) Hs |
|
250 | 1468 |
| could_reshyp [] = false; (*no premise -- illegal*) |
1469 |
in could_unify(concl_of rule, B) andalso |
|
1470 |
(not eres_flg orelse could_reshyp (prems_of rule)) |
|
0 | 1471 |
end; |
1472 |
||
1473 |
(*Bi-resolution of a state with a list of (flag,rule) pairs. |
|
1474 |
Puts the rule above: rule/state. Renames vars in the rules. *) |
|
250 | 1475 |
fun biresolution match brules i state = |
0 | 1476 |
let val lift = lift_rule(state, i); |
250 | 1477 |
val (stpairs, Bs, Bi, C) = dest_state(state,i) |
1478 |
val B = Logic.strip_assums_concl Bi; |
|
1479 |
val Hs = Logic.strip_assums_hyp Bi; |
|
1480 |
val comp = bicompose_aux match (state, (stpairs, Bs, Bi, C), true); |
|
4270 | 1481 |
fun res [] = Seq.empty |
250 | 1482 |
| res ((eres_flg, rule)::brules) = |
1483 |
if could_bires (Hs, B, eres_flg, rule) |
|
4270 | 1484 |
then Seq.make (*delay processing remainder till needed*) |
250 | 1485 |
(fn()=> Some(comp (eres_flg, lift rule, nprems_of rule), |
1486 |
res brules)) |
|
1487 |
else res brules |
|
4270 | 1488 |
in Seq.flat (res brules) end; |
0 | 1489 |
|
1490 |
||
1491 |
||
2509 | 1492 |
(*** Meta Simplification ***) |
0 | 1493 |
|
2509 | 1494 |
(** diagnostics **) |
0 | 1495 |
|
1496 |
exception SIMPLIFIER of string * thm; |
|
1497 |
||
4045 | 1498 |
fun prnt warn a = if warn then warning a else writeln a; |
1499 |
||
1500 |
fun prtm warn a sign t = |
|
1501 |
(prnt warn a; prnt warn (Sign.string_of_term sign t)); |
|
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset
|
1502 |
|
4679 | 1503 |
fun prthm warn a (thm as Thm{sign_ref, prop, ...}) = |
1504 |
(prtm warn a (Sign.deref sign_ref) prop); |
|
1505 |
||
209 | 1506 |
val trace_simp = ref false; |
1507 |
||
4045 | 1508 |
fun trace warn a = if !trace_simp then prnt warn a else (); |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1509 |
|
4045 | 1510 |
fun trace_term warn a sign t = |
1511 |
if !trace_simp then prtm warn a sign t else (); |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1512 |
|
4045 | 1513 |
fun trace_thm warn a (thm as Thm{sign_ref, prop, ...}) = |
1514 |
(trace_term warn a (Sign.deref sign_ref) prop); |
|
209 | 1515 |
|
1516 |
||
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset
|
1517 |
|
2509 | 1518 |
(** meta simp sets **) |
1519 |
||
1520 |
(* basic components *) |
|
1580
e3fd931e6095
Added some functions which allow redirection of Isabelle's output
berghofe
parents:
1576
diff
changeset
|
1521 |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1522 |
type rrule = {thm: thm, lhs: term, elhs: term, fo: bool, perm: bool}; |
2509 | 1523 |
type cong = {thm: thm, lhs: term}; |
3577
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
1524 |
type simproc = |
9715b6e3ec5f
added prems argument to simplification procedures;
wenzelm
parents:
3565
diff
changeset
|
1525 |
{name: string, proc: Sign.sg -> thm list -> term -> thm option, lhs: cterm, id: stamp}; |
288
b00ce6a1fe27
Implemented "ordered rewriting": rules which merely permute variables, such
nipkow
parents:
274
diff
changeset
|
1526 |
|
3550 | 1527 |
fun eq_rrule ({thm = Thm {prop = p1, ...}, ...}: rrule, |
2509 | 1528 |
{thm = Thm {prop = p2, ...}, ...}: rrule) = p1 aconv p2; |
1529 |
||
3550 | 1530 |
fun eq_cong ({thm = Thm {prop = p1, ...}, ...}: cong, |
1531 |
{thm = Thm {prop = p2, ...}, ...}: cong) = p1 aconv p2; |
|
1532 |
||
1533 |
fun eq_prem (Thm {prop = p1, ...}, Thm {prop = p2, ...}) = p1 aconv p2; |
|
1534 |
||
1535 |
fun eq_simproc ({id = s1, ...}:simproc, {id = s2, ...}:simproc) = (s1 = s2); |
|
1536 |
||
1537 |
fun mk_simproc (name, proc, lhs, id) = |
|
1538 |
{name = name, proc = proc, lhs = lhs, id = id}; |
|
2509 | 1539 |
|
1540 |
||
1541 |
(* datatype mss *) |
|
288
b00ce6a1fe27
Implemented "ordered rewriting": rules which merely permute variables, such
nipkow
parents:
274
diff
changeset
|
1542 |
|
2509 | 1543 |
(* |
1544 |
A "mss" contains data needed during conversion: |
|
1545 |
rules: discrimination net of rewrite rules; |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1546 |
congs: association list of congruence rules and |
5624 | 1547 |
a list of `weak' congruence constants. |
1548 |
A congruence is `weak' if it avoids normalization of some argument. |
|
2509 | 1549 |
procs: discrimination net of simplification procedures |
1550 |
(functions that prove rewrite rules on the fly); |
|
1551 |
bounds: names of bound variables already used |
|
1552 |
(for generating new names when rewriting under lambda abstractions); |
|
1553 |
prems: current premises; |
|
4679 | 1554 |
mk_rews: mk: turns simplification thms into rewrite rules; |
1555 |
mk_sym: turns == around; (needs Drule!) |
|
1556 |
mk_eq_True: turns P into P == True - logic specific; |
|
2509 | 1557 |
termless: relation for ordered rewriting; |
1028 | 1558 |
*) |
0 | 1559 |
|
2509 | 1560 |
datatype meta_simpset = |
1561 |
Mss of { |
|
1562 |
rules: rrule Net.net, |
|
5624 | 1563 |
congs: (string * cong) list * string list, |
2509 | 1564 |
procs: simproc Net.net, |
1565 |
bounds: string list, |
|
1566 |
prems: thm list, |
|
4679 | 1567 |
mk_rews: {mk: thm -> thm list, |
1568 |
mk_sym: thm -> thm option, |
|
1569 |
mk_eq_True: thm -> thm option}, |
|
2509 | 1570 |
termless: term * term -> bool}; |
1571 |
||
1572 |
fun mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless) = |
|
1573 |
Mss {rules = rules, congs = congs, procs = procs, bounds = bounds, |
|
4679 | 1574 |
prems=prems, mk_rews=mk_rews, termless=termless}; |
1575 |
||
1576 |
fun upd_rules(Mss{rules,congs,procs,bounds,prems,mk_rews,termless}, rules') = |
|
1577 |
mk_mss(rules',congs,procs,bounds,prems,mk_rews,termless); |
|
2509 | 1578 |
|
1579 |
val empty_mss = |
|
4679 | 1580 |
let val mk_rews = {mk = K [], mk_sym = K None, mk_eq_True = K None} |
5624 | 1581 |
in mk_mss (Net.empty, ([],[]), Net.empty, [], [], mk_rews, Term.termless) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1582 |
end; |
2509 | 1583 |
|
1584 |
||
1585 |
||
1586 |
(** simpset operations **) |
|
1587 |
||
3550 | 1588 |
(* dest_mss *) |
1589 |
||
1590 |
fun dest_mss (Mss {rules, congs, procs, ...}) = |
|
1591 |
{simps = map (fn (_, {thm, ...}) => thm) (Net.dest rules), |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1592 |
congs = map (fn (_, {thm, ...}) => thm) (fst congs), |
3550 | 1593 |
procs = |
1594 |
map (fn (_, {name, lhs, id, ...}) => ((name, lhs), id)) (Net.dest procs) |
|
1595 |
|> partition_eq eq_snd |
|
1596 |
|> map (fn ps => (#1 (#1 (hd ps)), map (#2 o #1) ps))}; |
|
1597 |
||
1598 |
||
1599 |
(* merge_mss *) (*NOTE: ignores mk_rews and termless of 2nd mss*) |
|
1600 |
||
1601 |
fun merge_mss |
|
5624 | 1602 |
(Mss {rules = rules1, congs = (congs1,weak1), procs = procs1, |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1603 |
bounds = bounds1, prems = prems1, mk_rews, termless}, |
5624 | 1604 |
Mss {rules = rules2, congs = (congs2,weak2), procs = procs2, |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1605 |
bounds = bounds2, prems = prems2, ...}) = |
3550 | 1606 |
mk_mss |
1607 |
(Net.merge (rules1, rules2, eq_rrule), |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1608 |
(generic_merge (eq_cong o pairself snd) I I congs1 congs2, |
5624 | 1609 |
merge_lists weak1 weak2), |
3550 | 1610 |
Net.merge (procs1, procs2, eq_simproc), |
1611 |
merge_lists bounds1 bounds2, |
|
1612 |
generic_merge eq_prem I I prems1 prems2, |
|
1613 |
mk_rews, termless); |
|
1614 |
||
4679 | 1615 |
(* add_simps *) |
3550 | 1616 |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1617 |
fun mk_rrule2{thm,lhs,perm} = |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1618 |
let val elhs = Pattern.eta_contract lhs |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1619 |
val fo = Pattern.first_order elhs orelse not(Pattern.pattern elhs) |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1620 |
in {thm=thm,lhs=lhs,elhs=elhs,fo=fo,perm=perm} end |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1621 |
|
4679 | 1622 |
fun insert_rrule(mss as Mss {rules,...}, |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1623 |
rrule as {thm,lhs,perm}) = |
4679 | 1624 |
(trace_thm false "Adding rewrite rule:" thm; |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1625 |
let val rrule2 as {elhs,...} = mk_rrule2 rrule |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1626 |
val rules' = Net.insert_term ((elhs, rrule2), rules, eq_rrule) |
4679 | 1627 |
in upd_rules(mss,rules') end |
1628 |
handle Net.INSERT => |
|
4785 | 1629 |
(prthm true "Ignoring duplicate rewrite rule:" thm; mss)); |
4679 | 1630 |
|
1631 |
fun vperm (Var _, Var _) = true |
|
1632 |
| vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) |
|
1633 |
| vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) |
|
1634 |
| vperm (t, u) = (t = u); |
|
1635 |
||
1636 |
fun var_perm (t, u) = |
|
1637 |
vperm (t, u) andalso eq_set_term (term_vars t, term_vars u); |
|
1638 |
||
1639 |
(* FIXME: it seems that the conditions on extra variables are too liberal if |
|
1640 |
prems are nonempty: does solving the prems really guarantee instantiation of |
|
1641 |
all its Vars? Better: a dynamic check each time a rule is applied. |
|
1642 |
*) |
|
1643 |
fun rewrite_rule_extra_vars prems elhs erhs = |
|
1644 |
not ((term_vars erhs) subset |
|
1645 |
(union_term (term_vars elhs, List.concat(map term_vars prems)))) |
|
1646 |
orelse |
|
1647 |
not ((term_tvars erhs) subset |
|
1648 |
(term_tvars elhs union List.concat(map term_tvars prems))); |
|
2509 | 1649 |
|
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1650 |
(*Simple test for looping rewrite rules and stupid orientations*) |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1651 |
fun reorient sign prems lhs rhs = |
4679 | 1652 |
rewrite_rule_extra_vars prems lhs rhs |
1653 |
orelse |
|
1654 |
is_Var (head_of lhs) |
|
1655 |
orelse |
|
4684 | 1656 |
(exists (apl (lhs, Logic.occs)) (rhs :: prems)) |
4679 | 1657 |
orelse |
1658 |
(null prems andalso |
|
1659 |
Pattern.matches (#tsig (Sign.rep_sg sign)) (lhs, rhs)) |
|
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1660 |
(*the condition "null prems" is necessary because conditional rewrites |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1661 |
with extra variables in the conditions may terminate although |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1662 |
the rhs is an instance of the lhs. Example: ?m < ?n ==> f(?n) == f(?m)*) |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1663 |
orelse |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1664 |
(is_Const lhs andalso not(is_Const rhs)) |
4679 | 1665 |
|
1666 |
fun decomp_simp(thm as Thm {sign_ref, prop, ...}) = |
|
1667 |
let val sign = Sign.deref sign_ref; |
|
1668 |
val prems = Logic.strip_imp_prems prop; |
|
1669 |
val concl = Logic.strip_imp_concl prop; |
|
1670 |
val (lhs, rhs) = Logic.dest_equals concl handle TERM _ => |
|
1671 |
raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm) |
|
1672 |
val elhs = Pattern.eta_contract lhs; |
|
1673 |
val erhs = Pattern.eta_contract rhs; |
|
1674 |
val perm = var_perm (elhs, erhs) andalso not (elhs aconv erhs) |
|
1675 |
andalso not (is_Var elhs) |
|
1676 |
in (sign,prems,lhs,rhs,perm) end; |
|
1677 |
||
1678 |
fun mk_eq_True (Mss{mk_rews={mk_eq_True,...},...}) thm = |
|
4713 | 1679 |
case mk_eq_True thm of |
1680 |
None => [] |
|
1681 |
| Some eq_True => let val (_,_,lhs,_,_) = decomp_simp eq_True |
|
1682 |
in [{thm=eq_True, lhs=lhs, perm=false}] end; |
|
1683 |
||
1684 |
(* create the rewrite rule and possibly also the ==True variant, |
|
1685 |
in case there are extra vars on the rhs *) |
|
1686 |
fun rrule_eq_True(thm,lhs,rhs,mss,thm2) = |
|
1687 |
let val rrule = {thm=thm, lhs=lhs, perm=false} |
|
1688 |
in if (term_vars rhs) subset (term_vars lhs) andalso |
|
1689 |
(term_tvars rhs) subset (term_tvars lhs) |
|
1690 |
then [rrule] |
|
1691 |
else mk_eq_True mss thm2 @ [rrule] |
|
1692 |
end; |
|
4679 | 1693 |
|
1694 |
fun mk_rrule mss thm = |
|
1695 |
let val (_,prems,lhs,rhs,perm) = decomp_simp thm |
|
4713 | 1696 |
in if perm then [{thm=thm, lhs=lhs, perm=true}] else |
4679 | 1697 |
(* weak test for loops: *) |
1698 |
if rewrite_rule_extra_vars prems lhs rhs orelse |
|
1699 |
is_Var (head_of lhs) (* mk_cases may do this! *) |
|
1700 |
then mk_eq_True mss thm |
|
4713 | 1701 |
else rrule_eq_True(thm,lhs,rhs,mss,thm) |
0 | 1702 |
end; |
1703 |
||
4679 | 1704 |
fun orient_rrule mss thm = |
1705 |
let val (sign,prems,lhs,rhs,perm) = decomp_simp thm |
|
4713 | 1706 |
in if perm then [{thm=thm,lhs=lhs,perm=true}] |
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1707 |
else if reorient sign prems lhs rhs |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
1708 |
then if reorient sign prems rhs lhs |
4679 | 1709 |
then mk_eq_True mss thm |
1710 |
else let val Mss{mk_rews={mk_sym,...},...} = mss |
|
4713 | 1711 |
in case mk_sym thm of |
1712 |
None => [] |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1713 |
| Some thm' => |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1714 |
let val (_,_,lhs',rhs',_) = decomp_simp thm' |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1715 |
in rrule_eq_True(thm',lhs',rhs',mss,thm) end |
4679 | 1716 |
end |
4713 | 1717 |
else rrule_eq_True(thm,lhs,rhs,mss,thm) |
4679 | 1718 |
end; |
2509 | 1719 |
|
4679 | 1720 |
fun extract_rews(Mss{mk_rews = {mk,...},...},thms) = flat(map mk thms); |
87 | 1721 |
|
4679 | 1722 |
fun orient_comb_simps comb mk_rrule (mss,thms) = |
1723 |
let val rews = extract_rews(mss,thms) |
|
4713 | 1724 |
val rrules = flat (map mk_rrule rews) |
4679 | 1725 |
in foldl comb (mss,rrules) end |
4667
6328d427a339
Tried to reorganize rewriter a little. More to be done.
nipkow
parents:
4589
diff
changeset
|
1726 |
|
4679 | 1727 |
(* Add rewrite rules explicitly; do not reorient! *) |
1728 |
fun add_simps(mss,thms) = |
|
1729 |
orient_comb_simps insert_rrule (mk_rrule mss) (mss,thms); |
|
0 | 1730 |
|
4679 | 1731 |
fun mss_of thms = |
4713 | 1732 |
foldl insert_rrule (empty_mss, flat(map (mk_rrule empty_mss) thms)); |
2509 | 1733 |
|
4713 | 1734 |
fun extract_safe_rrules(mss,thm) = |
1735 |
flat (map (orient_rrule mss) (extract_rews(mss,[thm]))); |
|
2509 | 1736 |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
1737 |
fun add_safe_simp(mss,thm) = |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
1738 |
foldl insert_rrule (mss, extract_safe_rrules(mss,thm)) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
1739 |
|
2509 | 1740 |
(* del_simps *) |
1741 |
||
4679 | 1742 |
fun del_rrule(mss as Mss {rules,...}, |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1743 |
rrule as {thm, elhs, ...}) = |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1744 |
(upd_rules(mss, Net.delete_term ((elhs, rrule), rules, eq_rrule)) |
4679 | 1745 |
handle Net.DELETE => |
4785 | 1746 |
(prthm true "Rewrite rule not in simpset:" thm; mss)); |
4667
6328d427a339
Tried to reorganize rewriter a little. More to be done.
nipkow
parents:
4589
diff
changeset
|
1747 |
|
4679 | 1748 |
fun del_simps(mss,thms) = |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1749 |
orient_comb_simps del_rrule (map mk_rrule2 o mk_rrule mss) (mss,thms); |
0 | 1750 |
|
2509 | 1751 |
|
2626 | 1752 |
(* add_congs *) |
0 | 1753 |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1754 |
(*FIXME -> term.ML *) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1755 |
fun is_Bound (Bound _) = true |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1756 |
fun is_Bound _ = false; |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1757 |
|
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1758 |
fun is_full_cong_prems [] varpairs = null varpairs |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1759 |
| is_full_cong_prems (p::prems) varpairs = |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1760 |
(case Logic.strip_assums_concl p of |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1761 |
Const("==",_) $ lhs $ rhs => |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1762 |
let val (x,xs) = strip_comb lhs and (y,ys) = strip_comb rhs |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1763 |
in is_Var x andalso forall is_Bound xs andalso |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1764 |
null(findrep(xs)) andalso xs=ys andalso |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1765 |
(x,y) mem varpairs andalso |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1766 |
is_full_cong_prems (p::prems) (varpairs\(x,y)) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1767 |
end |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1768 |
| _ => false); |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1769 |
|
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1770 |
fun is_full_cong (Thm{prop,...}) = |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1771 |
let val prems = Logic.strip_imp_prems prop |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1772 |
and concl = Logic.strip_imp_concl prop |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1773 |
val (lhs,rhs) = Logic.dest_equals concl |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1774 |
val (f,xs) = strip_comb lhs |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1775 |
and (g,ys) = strip_comb rhs |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1776 |
in |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1777 |
f=g andalso null(findrep(xs@ys)) andalso length xs = length ys andalso |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1778 |
is_full_cong_prems prems (xs ~~ ys) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1779 |
end |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1780 |
|
4679 | 1781 |
fun add_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless}, thm) = |
2509 | 1782 |
let |
1783 |
val (lhs, _) = Logic.dest_equals (concl_of thm) handle TERM _ => |
|
1784 |
raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
1785 |
(* val lhs = Pattern.eta_contract lhs; *) |
|
1786 |
val (a, _) = dest_Const (head_of lhs) handle TERM _ => |
|
1787 |
raise SIMPLIFIER ("Congruence must start with a constant", thm); |
|
5624 | 1788 |
val (alist,weak) = congs |
1789 |
val weak2 = if is_full_cong thm then weak else a::weak |
|
2509 | 1790 |
in |
5624 | 1791 |
mk_mss (rules, ((a, {lhs = lhs, thm = thm}) :: alist, weak2), |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1792 |
procs, bounds, prems, mk_rews, termless) |
0 | 1793 |
end; |
1794 |
||
1795 |
val (op add_congs) = foldl add_cong; |
|
1796 |
||
2509 | 1797 |
|
2626 | 1798 |
(* del_congs *) |
1799 |
||
4679 | 1800 |
fun del_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless}, thm) = |
2626 | 1801 |
let |
1802 |
val (lhs, _) = Logic.dest_equals (concl_of thm) handle TERM _ => |
|
1803 |
raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
1804 |
(* val lhs = Pattern.eta_contract lhs; *) |
|
1805 |
val (a, _) = dest_Const (head_of lhs) handle TERM _ => |
|
1806 |
raise SIMPLIFIER ("Congruence must start with a constant", thm); |
|
5624 | 1807 |
val (alist,_) = congs |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1808 |
val alist2 = filter (fn (x,_)=> x<>a) alist |
5624 | 1809 |
val weak2 = mapfilter (fn(a,{thm,...}) => if is_full_cong thm then None |
1810 |
else Some a) |
|
1811 |
alist2 |
|
2626 | 1812 |
in |
5624 | 1813 |
mk_mss (rules, (alist2,weak2), procs, bounds, prems, mk_rews, termless) |
2626 | 1814 |
end; |
1815 |
||
1816 |
val (op del_congs) = foldl del_cong; |
|
1817 |
||
1818 |
||
2509 | 1819 |
(* add_simprocs *) |
1820 |
||
4679 | 1821 |
fun add_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless}, |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1822 |
(name, lhs as Cterm {sign_ref, t, ...}, proc, id)) = |
5494 | 1823 |
(trace_term false ("Adding simplification procedure " ^ quote name ^ " for") |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1824 |
(Sign.deref sign_ref) t; |
2509 | 1825 |
mk_mss (rules, congs, |
3550 | 1826 |
Net.insert_term ((t, mk_simproc (name, proc, lhs, id)), procs, eq_simproc) |
5494 | 1827 |
handle Net.INSERT => |
1828 |
(warning ("Ignoring duplicate simplification procedure \"" |
|
1829 |
^ name ^ "\""); |
|
1830 |
procs), |
|
2509 | 1831 |
bounds, prems, mk_rews, termless)); |
0 | 1832 |
|
3550 | 1833 |
fun add_simproc (mss, (name, lhss, proc, id)) = |
1834 |
foldl add_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss); |
|
1835 |
||
2509 | 1836 |
val add_simprocs = foldl add_simproc; |
1837 |
||
1838 |
||
1839 |
(* del_simprocs *) |
|
0 | 1840 |
|
4679 | 1841 |
fun del_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless}, |
3550 | 1842 |
(name, lhs as Cterm {t, ...}, proc, id)) = |
2509 | 1843 |
mk_mss (rules, congs, |
3550 | 1844 |
Net.delete_term ((t, mk_simproc (name, proc, lhs, id)), procs, eq_simproc) |
5494 | 1845 |
handle Net.DELETE => |
1846 |
(warning ("Simplification procedure \"" ^ name ^ |
|
1847 |
"\" not in simpset"); procs), |
|
3550 | 1848 |
bounds, prems, mk_rews, termless); |
1849 |
||
1850 |
fun del_simproc (mss, (name, lhss, proc, id)) = |
|
1851 |
foldl del_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss); |
|
2509 | 1852 |
|
1853 |
val del_simprocs = foldl del_simproc; |
|
0 | 1854 |
|
1855 |
||
2509 | 1856 |
(* prems *) |
1857 |
||
4679 | 1858 |
fun add_prems (Mss {rules,congs,procs,bounds,prems,mk_rews,termless}, thms) = |
2509 | 1859 |
mk_mss (rules, congs, procs, bounds, thms @ prems, mk_rews, termless); |
1860 |
||
1861 |
fun prems_of_mss (Mss {prems, ...}) = prems; |
|
1862 |
||
1863 |
||
1864 |
(* mk_rews *) |
|
1865 |
||
1866 |
fun set_mk_rews |
|
4679 | 1867 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, termless}, mk) = |
1868 |
mk_mss (rules, congs, procs, bounds, prems, |
|
1869 |
{mk=mk, mk_sym= #mk_sym mk_rews, mk_eq_True= #mk_eq_True mk_rews}, |
|
1870 |
termless); |
|
2509 | 1871 |
|
4679 | 1872 |
fun set_mk_sym |
1873 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, termless}, mk_sym) = |
|
1874 |
mk_mss (rules, congs, procs, bounds, prems, |
|
1875 |
{mk= #mk mk_rews, mk_sym= mk_sym, mk_eq_True= #mk_eq_True mk_rews}, |
|
1876 |
termless); |
|
2509 | 1877 |
|
4679 | 1878 |
fun set_mk_eq_True |
1879 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, termless}, mk_eq_True) = |
|
1880 |
mk_mss (rules, congs, procs, bounds, prems, |
|
1881 |
{mk= #mk mk_rews, mk_sym= #mk_sym mk_rews, mk_eq_True= mk_eq_True}, |
|
1882 |
termless); |
|
2509 | 1883 |
|
1884 |
(* termless *) |
|
1885 |
||
1886 |
fun set_termless |
|
1887 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, termless = _}, termless) = |
|
1888 |
mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless); |
|
1889 |
||
1890 |
||
1891 |
||
1892 |
(** rewriting **) |
|
1893 |
||
1894 |
(* |
|
1895 |
Uses conversions, omitting proofs for efficiency. See: |
|
1896 |
L C Paulson, A higher-order implementation of rewriting, |
|
1897 |
Science of Computer Programming 3 (1983), pages 119-149. |
|
1898 |
*) |
|
0 | 1899 |
|
1900 |
type prover = meta_simpset -> thm -> thm option; |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
1901 |
type termrec = (Sign.sg_ref * term list) * term; |
0 | 1902 |
type conv = meta_simpset -> termrec -> termrec; |
1903 |
||
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1904 |
fun check_conv |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1905 |
(thm as Thm{shyps,hyps,prop,sign_ref,der,...}, prop0, ders) = |
4045 | 1906 |
let fun err() = (trace_thm false "Proved wrong thm (Check subgoaler?)" thm; |
4785 | 1907 |
trace_term false "Should have proved:" (Sign.deref sign_ref) prop0; |
432 | 1908 |
None) |
0 | 1909 |
val (lhs0,_) = Logic.dest_equals(Logic.strip_imp_concl prop0) |
1910 |
in case prop of |
|
1911 |
Const("==",_) $ lhs $ rhs => |
|
1912 |
if (lhs = lhs0) orelse |
|
427
4ce2ce940faf
ordered rewriting applies to conditional rules as well now
nipkow
parents:
421
diff
changeset
|
1913 |
(lhs aconv Envir.norm_term (Envir.empty 0) lhs0) |
4045 | 1914 |
then (trace_thm false "SUCCEEDED" thm; |
4713 | 1915 |
Some(rhs, (shyps, hyps, der::ders))) |
0 | 1916 |
else err() |
1917 |
| _ => err() |
|
1918 |
end; |
|
1919 |
||
659
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1920 |
fun ren_inst(insts,prop,pat,obj) = |
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1921 |
let val ren = match_bvs(pat,obj,[]) |
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1922 |
fun renAbs(Abs(x,T,b)) = |
1576
af8f43f742a0
Added some optimized versions of functions dealing with sets
berghofe
parents:
1569
diff
changeset
|
1923 |
Abs(case assoc_string(ren,x) of None => x | Some(y) => y, T, renAbs(b)) |
659
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1924 |
| renAbs(f$t) = renAbs(f) $ renAbs(t) |
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1925 |
| renAbs(t) = t |
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1926 |
in subst_vars insts (if null(ren) then prop else renAbs(prop)) end; |
678
6151b7f3b606
Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents:
659
diff
changeset
|
1927 |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1928 |
fun incr_insts i (in1:(indexname*typ)list,in2:(indexname*term)list) = |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1929 |
let fun incr ((a,n),x) = ((a,n+i),x) |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1930 |
in (map incr in1, map incr in2) end; |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1931 |
|
1258 | 1932 |
fun add_insts_sorts ((iTs, is), Ss) = |
1933 |
add_typs_sorts (map snd iTs, add_terms_sorts (map snd is, Ss)); |
|
1934 |
||
659
95ed2ccb4438
Prepared the code for preservation of bound var names during rewriting. Still
nipkow
parents:
623
diff
changeset
|
1935 |
|
2509 | 1936 |
(* mk_procrule *) |
1937 |
||
4679 | 1938 |
fun mk_procrule thm = |
1939 |
let val (_,prems,lhs,rhs,_) = decomp_simp thm |
|
1940 |
in if rewrite_rule_extra_vars prems lhs rhs |
|
4785 | 1941 |
then (prthm true "Extra vars on rhs:" thm; []) |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1942 |
else [mk_rrule2{thm = thm, lhs = lhs, perm = false}] |
2509 | 1943 |
end; |
1944 |
||
1945 |
||
1946 |
(* conversion to apply the meta simpset to a term *) |
|
1947 |
||
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1948 |
(* Since the rewriting strategy is bottom-up, we avoid re-normalizing already |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1949 |
normalized terms by carrying around the rhs of the rewrite rule just |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1950 |
applied. This is called the `skeleton'. It is decomposed in parallel |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1951 |
with the term. Once a Var is encountered, the corresponding term is |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1952 |
already in normal form. |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1953 |
skel0 is a dummy skeleton that is to enforce complete normalization. |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1954 |
*) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1955 |
val skel0 = Bound 0; |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1956 |
|
5624 | 1957 |
(* Use rhs as skeleton only if the lhs does not contain unnormalized bits. |
1958 |
The latter may happen iff there are weak congruence rules for constants |
|
1959 |
in the lhs. |
|
1960 |
*) |
|
1961 |
fun uncond_skel((_,weak),(lhs,rhs)) = |
|
1962 |
if null weak then rhs (* optimization *) |
|
1963 |
else if exists_Const (fn (c,_) => c mem weak) lhs then skel0 |
|
1964 |
else rhs; |
|
1965 |
||
1966 |
(* Behaves like unconditional rule if rhs does not contain vars not in the lhs. |
|
1967 |
Otherwise those vars may become instantiated with unnormalized terms |
|
1968 |
while the premises are solved. |
|
1969 |
*) |
|
1970 |
fun cond_skel(args as (congs,(lhs,rhs))) = |
|
1971 |
if term_vars rhs subset term_vars lhs then uncond_skel(args) |
|
1972 |
else skel0; |
|
1973 |
||
2509 | 1974 |
(* |
1975 |
we try in order: |
|
1976 |
(1) beta reduction |
|
1977 |
(2) unconditional rewrite rules |
|
1978 |
(3) conditional rewrite rules |
|
3550 | 1979 |
(4) simplification procedures |
4116 | 1980 |
|
1981 |
IMPORTANT: rewrite rules must not introduce new Vars or TVars! |
|
1982 |
||
2509 | 1983 |
*) |
1984 |
||
4116 | 1985 |
fun rewritec (prover,sign_reft,maxt) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
1986 |
(mss as Mss{rules, procs, termless, prems, congs, ...}) |
4713 | 1987 |
(t:term,etc as (shypst,hypst,ders)) = |
3550 | 1988 |
let |
4713 | 1989 |
val signt = Sign.deref sign_reft; |
1990 |
val tsigt = Sign.tsig_of signt; |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1991 |
fun rew{thm as Thm{sign_ref,der,shyps,hyps,prop,maxidx,...}, |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1992 |
lhs, elhs, fo, perm} = |
4713 | 1993 |
let |
1994 |
val _ = if Sign.subsig (Sign.deref sign_ref, signt) then () |
|
5342
3be51e9b33c8
The warning "Rewrite rule from different theory" is ALWAYS printed, even if
paulson
parents:
4999
diff
changeset
|
1995 |
else (prthm true "Rewrite rule from different theory:" thm; |
4713 | 1996 |
raise Pattern.MATCH); |
1997 |
val rprop = if maxt = ~1 then prop |
|
1998 |
else Logic.incr_indexes([],maxt+1) prop; |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
1999 |
val insts = if fo then Pattern.first_order_match tsigt (elhs,t) |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
2000 |
else Pattern.match tsigt (elhs,t); |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
2001 |
val insts = if maxt = ~1 then insts else incr_insts (maxt+1) insts |
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
2002 |
val prop' = ren_inst(insts,rprop,lhs,t); |
4713 | 2003 |
val hyps' = union_term(hyps,hypst); |
2004 |
val shyps' = add_insts_sorts (insts, union_sort(shyps,shypst)); |
|
2005 |
val unconditional = (Logic.count_prems(prop',0) = 0); |
|
2006 |
val maxidx' = if unconditional then maxt else maxidx+maxt+1 |
|
2007 |
val ct' = Cterm{sign_ref = sign_reft, (*used for deriv only*) |
|
2008 |
t = prop', T = propT, maxidx = maxidx'} |
|
2009 |
val der' = infer_derivs (RewriteC ct', [der]); |
|
2010 |
val thm' = Thm{sign_ref = sign_reft, der = der', shyps = shyps', |
|
2011 |
hyps = hyps', prop = prop', maxidx = maxidx'} |
|
2012 |
val (lhs',rhs') = Logic.dest_equals(Logic.strip_imp_concl prop') |
|
2013 |
in |
|
2014 |
if perm andalso not(termless(rhs',lhs')) then None |
|
5624 | 2015 |
else |
2016 |
(trace_thm false "Applying instance of rewrite rule:" thm; |
|
2017 |
if unconditional |
|
2018 |
then |
|
2019 |
(trace_thm false "Rewriting:" thm'; |
|
2020 |
let val lr = Logic.dest_equals prop |
|
2021 |
val trec' = (rhs', (shyps', hyps', der'::ders)) |
|
2022 |
in Some(trec',uncond_skel(congs,lr)) end) |
|
2023 |
else |
|
2024 |
(trace_thm false "Trying to rewrite:" thm'; |
|
2025 |
case prover mss thm' of |
|
2026 |
None => (trace_thm false "FAILED" thm'; None) |
|
2027 |
| Some(thm2) => |
|
2028 |
(case check_conv(thm2,prop',ders) of |
|
2029 |
None => None | |
|
2030 |
Some trec => |
|
2031 |
let val concl = Logic.strip_imp_concl prop |
|
2032 |
val lr = Logic.dest_equals concl |
|
2033 |
in Some(trec,cond_skel(congs,lr)) end))) |
|
1659 | 2034 |
end |
2509 | 2035 |
|
4713 | 2036 |
fun rews [] = None |
2037 |
| rews (rrule :: rrules) = |
|
2038 |
let val opt = rew rrule handle Pattern.MATCH => None |
|
2039 |
in case opt of None => rews rrules | some => some end; |
|
2040 |
||
2041 |
fun sort_rrules rrs = let |
|
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
2042 |
fun is_simple({thm as Thm{prop,...}, ...}:rrule) = case prop of |
4713 | 2043 |
Const("==",_) $ _ $ _ => true |
2044 |
| _ => false |
|
2045 |
fun sort [] (re1,re2) = re1 @ re2 |
|
2046 |
| sort (rr::rrs) (re1,re2) = if is_simple rr |
|
2047 |
then sort rrs (rr::re1,re2) |
|
2048 |
else sort rrs (re1,rr::re2) |
|
2049 |
in sort rrs ([],[]) end |
|
2050 |
||
2051 |
fun proc_rews _ ([]:simproc list) = None |
|
2052 |
| proc_rews eta_t ({name, proc, lhs = Cterm {t = plhs, ...}, ...} :: ps) = |
|
2053 |
if Pattern.matches tsigt (plhs, t) then |
|
2054 |
(trace_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t; |
|
2055 |
case proc signt prems eta_t of |
|
2056 |
None => (trace false "FAILED"; proc_rews eta_t ps) |
|
2057 |
| Some raw_thm => |
|
4397 | 2058 |
(trace_thm false ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm; |
4713 | 2059 |
(case rews (mk_procrule raw_thm) of |
2060 |
None => (trace false "IGNORED"; proc_rews eta_t ps) |
|
2061 |
| some => some))) |
|
2062 |
else proc_rews eta_t ps; |
|
2063 |
in case t of |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2064 |
Abs (_, _, body) $ u => Some ((subst_bound (u, body), etc),skel0) |
4713 | 2065 |
| _ => (case rews (sort_rrules (Net.match_term rules t)) of |
2066 |
None => proc_rews (Pattern.eta_contract t) |
|
2067 |
(Net.match_term procs t) |
|
2068 |
| some => some) |
|
0 | 2069 |
end; |
2070 |
||
2509 | 2071 |
|
2072 |
(* conversion to apply a congruence rule to a term *) |
|
2073 |
||
4713 | 2074 |
fun congc (prover,sign_reft,maxt) {thm=cong,lhs=lhs} (t,(shypst,hypst,ders)) = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2075 |
let val signt = Sign.deref sign_reft; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2076 |
val tsig = Sign.tsig_of signt; |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2077 |
val Thm{sign_ref,der,shyps,hyps,maxidx,prop,...} = cong |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2078 |
val _ = if Sign.subsig(Sign.deref sign_ref,signt) then () |
208
342f88d2e8ab
optimized simplifier - signature of rewritten term stays constant
nipkow
parents:
200
diff
changeset
|
2079 |
else error("Congruence rule from different theory") |
2147 | 2080 |
val rprop = if maxt = ~1 then prop |
2081 |
else Logic.incr_indexes([],maxt+1) prop; |
|
2082 |
val rlhs = if maxt = ~1 then lhs |
|
1065
8425cb5acb77
Fixed old bug in the simplifier. Term to be simplified now carries around its
nipkow
parents:
1028
diff
changeset
|
2083 |
else fst(Logic.dest_equals(Logic.strip_imp_concl rprop)) |
1569
a89f246dee0e
Non-matching congruence rule in rewriter is simply ignored.
nipkow
parents:
1566
diff
changeset
|
2084 |
val insts = Pattern.match tsig (rlhs,t) |
a89f246dee0e
Non-matching congruence rule in rewriter is simply ignored.
nipkow
parents:
1566
diff
changeset
|
2085 |
(* Pattern.match can raise Pattern.MATCH; |
a89f246dee0e
Non-matching congruence rule in rewriter is simply ignored.
nipkow
parents:
1566
diff
changeset
|
2086 |
is handled when congc is called *) |
1065
8425cb5acb77
Fixed old bug in the simplifier. Term to be simplified now carries around its
nipkow
parents:
1028
diff
changeset
|
2087 |
val prop' = ren_inst(insts,rprop,rlhs,t); |
2177
8b365a3a6ed1
Changed some mem, ins and union calls to be monomorphic
paulson
parents:
2163
diff
changeset
|
2088 |
val shyps' = add_insts_sorts (insts, union_sort(shyps,shypst)) |
1529 | 2089 |
val maxidx' = maxidx_of_term prop' |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2090 |
val ct' = Cterm{sign_ref = sign_reft, (*used for deriv only*) |
2386 | 2091 |
t = prop', |
2092 |
T = propT, |
|
2093 |
maxidx = maxidx'} |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2094 |
val thm' = Thm{sign_ref = sign_reft, |
3550 | 2095 |
der = infer_derivs (CongC ct', [der]), |
2386 | 2096 |
shyps = shyps', |
2097 |
hyps = union_term(hyps,hypst), |
|
1529 | 2098 |
prop = prop', |
2386 | 2099 |
maxidx = maxidx'}; |
4785 | 2100 |
val unit = trace_thm false "Applying congruence rule:" thm'; |
112
009ae5c85ae9
Changed the simplifier: if the subgoaler proves an unexpected thm, chances
nipkow
parents:
87
diff
changeset
|
2101 |
fun err() = error("Failed congruence proof!") |
0 | 2102 |
|
2103 |
in case prover thm' of |
|
112
009ae5c85ae9
Changed the simplifier: if the subgoaler proves an unexpected thm, chances
nipkow
parents:
87
diff
changeset
|
2104 |
None => err() |
1529 | 2105 |
| Some(thm2) => (case check_conv(thm2,prop',ders) of |
405
c97514f63633
Internale optimization of the simplifier: in case a subterm stays unchanged,
nipkow
parents:
399
diff
changeset
|
2106 |
None => err() | some => some) |
0 | 2107 |
end; |
2108 |
||
4713 | 2109 |
fun bottomc ((simprem,useprem,mutsimp),prover,sign_ref,maxidx) = |
2110 |
let |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2111 |
fun botc fail skel mss trec = |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2112 |
if is_Var skel then if fail then None else Some(trec) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2113 |
else |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2114 |
(case subc skel mss trec of |
2386 | 2115 |
some as Some(trec1) => |
4116 | 2116 |
(case rewritec (prover,sign_ref,maxidx) mss trec1 of |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2117 |
Some(trec2,skel2) => botc false skel2 mss trec2 |
2386 | 2118 |
| None => some) |
2119 |
| None => |
|
4116 | 2120 |
(case rewritec (prover,sign_ref,maxidx) mss trec of |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2121 |
Some(trec2,skel2) => botc false skel2 mss trec2 |
2386 | 2122 |
| None => if fail then None else Some(trec))) |
0 | 2123 |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2124 |
and try_botc mss trec = |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2125 |
(case botc true skel0 mss trec of |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2126 |
Some(trec1) => trec1 | None => trec) |
405
c97514f63633
Internale optimization of the simplifier: in case a subterm stays unchanged,
nipkow
parents:
399
diff
changeset
|
2127 |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2128 |
and subc skel |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2129 |
(mss as Mss{rules,congs,procs,bounds,prems,mk_rews,termless}) |
4713 | 2130 |
(trec as (t0:term,etc:sort list*term list * rule mtree list)) = |
1529 | 2131 |
(case t0 of |
2386 | 2132 |
Abs(a,T,t) => |
2133 |
let val b = variant bounds a |
|
2134 |
val v = Free("." ^ b,T) |
|
2509 | 2135 |
val mss' = mk_mss (rules, congs, procs, b :: bounds, prems, mk_rews, termless) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2136 |
val skel' = case skel of Abs(_,_,sk) => sk | _ => skel0 |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2137 |
in case botc true skel' mss' (subst_bound(v,t),etc) of |
4713 | 2138 |
Some(t',etc') => Some(Abs(a, T, abstract_over(v,t')), etc') |
2386 | 2139 |
| None => None |
2140 |
end |
|
2141 |
| t$u => (case t of |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2142 |
Const("==>",_)$s => Some(impc(s,u,mss,etc)) |
2386 | 2143 |
| Abs(_,_,body) => |
4713 | 2144 |
let val trec = (subst_bound(u,body), etc) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2145 |
in case subc skel0 mss trec of |
2386 | 2146 |
None => Some(trec) |
2147 |
| trec => trec |
|
2148 |
end |
|
2149 |
| _ => |
|
2150 |
let fun appc() = |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2151 |
let val (tskel,uskel) = |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2152 |
case skel of tskel$uskel => (tskel,uskel) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2153 |
| _ => (skel0,skel0) |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2154 |
in |
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2155 |
(case botc true tskel mss (t,etc) of |
4713 | 2156 |
Some(t1,etc1) => |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2157 |
(case botc true uskel mss (u,etc1) of |
4713 | 2158 |
Some(u1,etc2) => Some(t1$u1, etc2) |
2159 |
| None => Some(t1$u, etc1)) |
|
2386 | 2160 |
| None => |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2161 |
(case botc true uskel mss (u,etc) of |
4713 | 2162 |
Some(u1,etc1) => Some(t$u1, etc1) |
2386 | 2163 |
| None => None)) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2164 |
end |
2386 | 2165 |
val (h,ts) = strip_comb t |
2166 |
in case h of |
|
2167 |
Const(a,_) => |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2168 |
(case assoc_string(fst congs,a) of |
2386 | 2169 |
None => appc() |
4116 | 2170 |
| Some(cong) => |
2171 |
(congc (prover mss,sign_ref,maxidx) cong trec |
|
2172 |
handle Pattern.MATCH => appc() ) ) |
|
2386 | 2173 |
| _ => appc() |
2174 |
end) |
|
2175 |
| _ => None) |
|
0 | 2176 |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2177 |
and impc args = |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2178 |
if mutsimp |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2179 |
then let val (prem, conc, mss, etc) = args |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2180 |
in snd(mut_impc([], prem, conc, mss, etc)) end |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2181 |
else nonmut_impc args |
4713 | 2182 |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2183 |
and mut_impc (prems, prem, conc, mss, etc) = |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2184 |
let val (prem1,etc1) = try_botc mss (prem,etc) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2185 |
in mut_impc1(prems, prem1, conc, mss, etc1) end |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2186 |
|
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2187 |
and mut_impc1(prems, prem1, conc, mss, etc1 as (_,hyps1,_)) = |
4713 | 2188 |
let |
4820
8f6dbbd8d497
Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents:
4785
diff
changeset
|
2189 |
fun uncond({thm,lhs,perm}) = |
4713 | 2190 |
if nprems_of thm = 0 then Some lhs else None |
2191 |
||
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2192 |
val (lhss1,mss1) = |
4713 | 2193 |
if maxidx_of_term prem1 <> ~1 |
2194 |
then (trace_term true "Cannot add premise as rewrite rule because it contains (type) unknowns:" |
|
2195 |
(Sign.deref sign_ref) prem1; |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2196 |
([],mss)) |
4713 | 2197 |
else let val thm = assume (Cterm{sign_ref=sign_ref, t=prem1, |
2198 |
T=propT, maxidx= ~1}) |
|
2199 |
val rrules1 = extract_safe_rrules(mss,thm) |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2200 |
val lhss1 = mapfilter uncond rrules1 |
4713 | 2201 |
val mss1 = foldl insert_rrule (add_prems(mss,[thm]),rrules1) |
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2202 |
in (lhss1, mss1) end |
4713 | 2203 |
|
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2204 |
fun disch1(conc2,(shyps2,hyps2,ders2)) = |
4713 | 2205 |
let val hyps2' = if gen_mem (op aconv) (prem1, hyps1) |
2206 |
then hyps2 else hyps2\prem1 |
|
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2207 |
in (Logic.mk_implies(prem1,conc2),(shyps2,hyps2',ders2)) end |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2208 |
|
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2209 |
fun rebuild trec2 = |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2210 |
let val trec = disch1 trec2 |
4713 | 2211 |
in case rewritec (prover,sign_ref,maxidx) mss trec of |
2212 |
None => (None,trec) |
|
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2213 |
| Some((Const("==>",_)$prem$conc,etc),_) => |
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2214 |
mut_impc(prems,prem,conc,mss,etc) |
5623
75b513db9a3a
Tuned simplifier not to re-normalized already normalized terms.
nipkow
parents:
5494
diff
changeset
|
2215 |
| Some(trec',_) => (None,trec') |
4713 | 2216 |
end |
2217 |
||
2218 |
fun simpconc() = |
|
2219 |
case conc of |
|
2220 |
Const("==>",_)$s$t => |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2221 |
(case mut_impc(prems@[prem1],s,t,mss1,etc1) of |
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2222 |
(Some(i,prem),trec2) => |
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2223 |
let val trec2' = disch1 trec2 |
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2224 |
in if i=0 then mut_impc1(prems,prem,fst trec2',mss,snd trec2') |
4716
a291e858061c
Asm_full_simp_tac now reorients asm c = t to t = c.
nipkow
parents:
4713
diff
changeset
|
2225 |
else (Some(i-1,prem),trec2') |
4713 | 2226 |
end |
2227 |
| (None,trec) => rebuild(trec)) |
|
2228 |
| _ => rebuild(try_botc mss1 (conc,etc1)) |
|
2229 |
||
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2230 |
in let val sg = Sign.deref sign_ref |
4713 | 2231 |
val tsig = #tsig(Sign.rep_sg sg) |
2232 |
fun reducible t = |
|
2233 |
exists (fn lhs => Pattern.matches_subterm tsig (lhs,t)) |
|
2234 |
lhss1; |
|
2235 |
in case dropwhile (not o reducible) prems of |
|
2236 |
[] => simpconc() |
|
4785 | 2237 |
| red::rest => (trace_term false "Can now reduce premise:" sg |
4713 | 2238 |
red; |
2239 |
(Some(length rest,prem1),(conc,etc1))) |
|
2240 |
end |
|
2241 |
end |
|
0 | 2242 |
|
4740
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2243 |
(* legacy code - only for backwards compatibility *) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2244 |
and nonmut_impc(prem, conc, mss, etc as (_,hyps1,_)) = |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2245 |
let val (prem1,etc1) = if simprem then try_botc mss (prem,etc) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2246 |
else (prem,etc) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2247 |
val maxidx1 = maxidx_of_term prem1 |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2248 |
val mss1 = |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2249 |
if not useprem then mss else |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2250 |
if maxidx1 <> ~1 |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2251 |
then (trace_term true "Cannot add premise as rewrite rule because it contains (type) unknowns:" |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2252 |
(Sign.deref sign_ref) prem1; |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2253 |
mss) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2254 |
else let val thm = assume (Cterm{sign_ref=sign_ref, t=prem1, |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2255 |
T=propT, maxidx= ~1}) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2256 |
in add_safe_simp(add_prems(mss,[thm]), thm) end |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2257 |
val (conc2,(shyps2,hyps2,ders2)) = try_botc mss1 (conc,etc1) |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2258 |
val hyps2' = if prem1 mem hyps1 then hyps2 else hyps2\prem1 |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2259 |
in (Logic.mk_implies(prem1,conc2), (shyps2, hyps2', ders2)) end |
0136b5bbe9fe
Made mutual simplification of prems a special case.
nipkow
parents:
4716
diff
changeset
|
2260 |
|
1529 | 2261 |
in try_botc end; |
0 | 2262 |
|
2263 |
||
2264 |
(*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***) |
|
2509 | 2265 |
|
2266 |
(* |
|
2267 |
Parameters: |
|
4713 | 2268 |
mode = (simplify A, |
2269 |
use A in simplifying B, |
|
2270 |
use prems of B (if B is again a meta-impl.) to simplify A) |
|
2271 |
when simplifying A ==> B |
|
2509 | 2272 |
mss: contains equality theorems of the form [|p1,...|] ==> t==u |
2273 |
prover: how to solve premises in conditional rewrites and congruences |
|
0 | 2274 |
*) |
2509 | 2275 |
|
2276 |
(* FIXME: check that #bounds(mss) does not "occur" in ct alread *) |
|
2277 |
||
214
ed6a3e2b1a33
added new parameter to the simplification tactics which indicates if
nipkow
parents:
209
diff
changeset
|
2278 |
fun rewrite_cterm mode mss prover ct = |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2279 |
let val Cterm {sign_ref, t, T, maxidx} = ct; |
4713 | 2280 |
val (u,(shyps,hyps,ders)) = bottomc (mode,prover, sign_ref, maxidx) mss |
2281 |
(t, (add_term_sorts(t,[]), [], [])); |
|
0 | 2282 |
val prop = Logic.mk_equals(t,u) |
1258 | 2283 |
in |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2284 |
Thm{sign_ref = sign_ref, |
2386 | 2285 |
der = infer_derivs (Rewrite_cterm ct, ders), |
4116 | 2286 |
maxidx = maxidx, |
2386 | 2287 |
shyps = shyps, |
2288 |
hyps = hyps, |
|
1529 | 2289 |
prop = prop} |
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2290 |
end; |
0 | 2291 |
|
1539 | 2292 |
|
2509 | 2293 |
|
2294 |
(*** Oracles ***) |
|
2295 |
||
3812 | 2296 |
fun invoke_oracle thy raw_name = |
2297 |
let |
|
2298 |
val {sign = sg, oracles, ...} = rep_theory thy; |
|
2299 |
val name = Sign.intern sg Theory.oracleK raw_name; |
|
2300 |
val oracle = |
|
2301 |
(case Symtab.lookup (oracles, name) of |
|
2302 |
None => raise THM ("Unknown oracle: " ^ name, 0, []) |
|
2303 |
| Some (f, _) => f); |
|
2304 |
in |
|
2305 |
fn (sign, exn) => |
|
2306 |
let |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2307 |
val sign_ref' = Sign.merge_refs (Sign.self_ref sg, Sign.self_ref sign); |
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2308 |
val sign' = Sign.deref sign_ref'; |
3812 | 2309 |
val (prop, T, maxidx) = Sign.certify_term sign' (oracle (sign', exn)); |
2310 |
in |
|
2311 |
if T <> propT then |
|
2312 |
raise THM ("Oracle's result must have type prop: " ^ name, 0, []) |
|
2313 |
else fix_shyps [] [] |
|
3967
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
wenzelm
parents:
3895
diff
changeset
|
2314 |
(Thm {sign_ref = sign_ref', |
4182 | 2315 |
der = Join (Oracle (name, sign, exn), []), |
3812 | 2316 |
maxidx = maxidx, |
2317 |
shyps = [], |
|
2318 |
hyps = [], |
|
2319 |
prop = prop}) |
|
2320 |
end |
|
2321 |
end; |
|
2322 |
||
1539 | 2323 |
|
0 | 2324 |
end; |
1503 | 2325 |
|
6089 | 2326 |
|
2327 |
structure BasicThm: BASIC_THM = Thm; |
|
2328 |
open BasicThm; |