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