author | skalberg |
Sat, 17 Apr 2004 23:53:35 +0200 | |
changeset 14620 | 1be590fd2422 |
parent 14518 | c3019a66180f |
child 14673 | 3d760a971fde |
permissions | -rw-r--r-- |
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
1 |
(* Title: HOL/Import/proof_kernel.ML |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
2 |
ID: $Id$ |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
3 |
Author: Sebastian Skalberg (TU Muenchen) |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
4 |
*) |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
5 |
|
14516 | 6 |
signature ProofKernel = |
7 |
sig |
|
8 |
type hol_type |
|
9 |
type tag |
|
10 |
type term |
|
11 |
type thm |
|
12 |
type ('a,'b) subst |
|
13 |
||
14 |
type proof_info |
|
15 |
datatype proof = Proof of proof_info * proof_content |
|
16 |
and proof_content |
|
17 |
= PRefl of term |
|
18 |
| PInstT of proof * (hol_type,hol_type) subst |
|
19 |
| PSubst of proof list * term * proof |
|
20 |
| PAbs of proof * term |
|
21 |
| PDisch of proof * term |
|
22 |
| PMp of proof * proof |
|
23 |
| PHyp of term |
|
24 |
| PAxm of string * term |
|
25 |
| PDef of string * string * term |
|
26 |
| PTmSpec of string * string list * proof |
|
27 |
| PTyDef of string * string * proof |
|
28 |
| PTyIntro of string * string * string * string * term * term * proof |
|
29 |
| POracle of tag * term list * term |
|
30 |
| PDisk |
|
31 |
| PSpec of proof * term |
|
32 |
| PInst of proof * (term,term) subst |
|
33 |
| PGen of proof * term |
|
34 |
| PGenAbs of proof * term option * term list |
|
35 |
| PImpAS of proof * proof |
|
36 |
| PSym of proof |
|
37 |
| PTrans of proof * proof |
|
38 |
| PComb of proof * proof |
|
39 |
| PEqMp of proof * proof |
|
40 |
| PEqImp of proof |
|
41 |
| PExists of proof * term * term |
|
42 |
| PChoose of term * proof * proof |
|
43 |
| PConj of proof * proof |
|
44 |
| PConjunct1 of proof |
|
45 |
| PConjunct2 of proof |
|
46 |
| PDisj1 of proof * term |
|
47 |
| PDisj2 of proof * term |
|
48 |
| PDisjCases of proof * proof * proof |
|
49 |
| PNotI of proof |
|
50 |
| PNotE of proof |
|
51 |
| PContr of proof * term |
|
52 |
||
53 |
exception PK of string * string |
|
54 |
||
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
55 |
val get_proof_dir: string -> theory -> string option |
14516 | 56 |
val debug : bool ref |
57 |
val disk_info_of : proof -> (string * string) option |
|
58 |
val set_disk_info_of : proof -> string -> string -> unit |
|
59 |
val mk_proof : proof_content -> proof |
|
60 |
val content_of : proof -> proof_content |
|
61 |
val import_proof : string -> string -> theory -> (theory -> term) option * (theory -> proof) |
|
62 |
||
63 |
val rewrite_hol4_term: Term.term -> theory -> Thm.thm |
|
64 |
||
65 |
val type_of : term -> hol_type |
|
66 |
||
67 |
val get_thm : string -> string -> theory -> (theory * thm option) |
|
68 |
val get_def : string -> string -> term -> theory -> (theory * thm option) |
|
69 |
val get_axiom: string -> string -> theory -> (theory * thm option) |
|
70 |
||
71 |
val store_thm : string -> string -> thm -> theory -> theory * thm |
|
72 |
||
73 |
val to_isa_thm : thm -> (term * term) list * Thm.thm |
|
74 |
val to_isa_term: term -> Term.term |
|
75 |
||
76 |
val REFL : term -> theory -> theory * thm |
|
77 |
val ASSUME : term -> theory -> theory * thm |
|
78 |
val INST_TYPE : (hol_type,hol_type) subst -> thm -> theory -> theory * thm |
|
79 |
val INST : (term,term)subst -> thm -> theory -> theory * thm |
|
80 |
val EQ_MP : thm -> thm -> theory -> theory * thm |
|
81 |
val EQ_IMP_RULE : thm -> theory -> theory * thm |
|
82 |
val SUBST : thm list -> term -> thm -> theory -> theory * thm |
|
83 |
val DISJ_CASES : thm -> thm -> thm -> theory -> theory * thm |
|
84 |
val DISJ1: thm -> term -> theory -> theory * thm |
|
85 |
val DISJ2: term -> thm -> theory -> theory * thm |
|
86 |
val IMP_ANTISYM: thm -> thm -> theory -> theory * thm |
|
87 |
val SYM : thm -> theory -> theory * thm |
|
88 |
val MP : thm -> thm -> theory -> theory * thm |
|
89 |
val GEN : term -> thm -> theory -> theory * thm |
|
90 |
val CHOOSE : term -> thm -> thm -> theory -> theory * thm |
|
91 |
val EXISTS : term -> term -> thm -> theory -> theory * thm |
|
92 |
val ABS : term -> thm -> theory -> theory * thm |
|
93 |
val GEN_ABS : term option -> term list -> thm -> theory -> theory * thm |
|
94 |
val TRANS : thm -> thm -> theory -> theory * thm |
|
95 |
val CCONTR : term -> thm -> theory -> theory * thm |
|
96 |
val CONJ : thm -> thm -> theory -> theory * thm |
|
97 |
val CONJUNCT1: thm -> theory -> theory * thm |
|
98 |
val CONJUNCT2: thm -> theory -> theory * thm |
|
99 |
val NOT_INTRO: thm -> theory -> theory * thm |
|
100 |
val NOT_ELIM : thm -> theory -> theory * thm |
|
101 |
val SPEC : term -> thm -> theory -> theory * thm |
|
102 |
val COMB : thm -> thm -> theory -> theory * thm |
|
103 |
val DISCH: term -> thm -> theory -> theory * thm |
|
104 |
||
105 |
val type_introduction: string -> string -> string -> string -> string -> term * term -> thm -> theory -> theory * thm |
|
106 |
||
107 |
val new_definition : string -> string -> term -> theory -> theory * thm |
|
108 |
val new_specification : string -> string -> string list -> thm -> theory -> theory * thm |
|
109 |
val new_type_definition : string -> string -> string -> thm -> theory -> theory * thm |
|
110 |
val new_axiom : string -> term -> theory -> theory * thm |
|
111 |
||
112 |
end |
|
113 |
||
114 |
structure ProofKernel :> ProofKernel = |
|
115 |
struct |
|
116 |
type hol_type = Term.typ |
|
117 |
type term = Term.term |
|
118 |
datatype tag = Tag of string list |
|
119 |
type ('a,'b) subst = ('a * 'b) list |
|
120 |
datatype thm = HOLThm of (Term.term * Term.term) list * Thm.thm |
|
121 |
||
122 |
datatype proof_info |
|
123 |
= Info of {disk_info: (string * string) option ref} |
|
124 |
||
125 |
datatype proof = Proof of proof_info * proof_content |
|
126 |
and proof_content |
|
127 |
= PRefl of term |
|
128 |
| PInstT of proof * (hol_type,hol_type) subst |
|
129 |
| PSubst of proof list * term * proof |
|
130 |
| PAbs of proof * term |
|
131 |
| PDisch of proof * term |
|
132 |
| PMp of proof * proof |
|
133 |
| PHyp of term |
|
134 |
| PAxm of string * term |
|
135 |
| PDef of string * string * term |
|
136 |
| PTmSpec of string * string list * proof |
|
137 |
| PTyDef of string * string * proof |
|
138 |
| PTyIntro of string * string * string * string * term * term * proof |
|
139 |
| POracle of tag * term list * term |
|
140 |
| PDisk |
|
141 |
| PSpec of proof * term |
|
142 |
| PInst of proof * (term,term) subst |
|
143 |
| PGen of proof * term |
|
144 |
| PGenAbs of proof * term option * term list |
|
145 |
| PImpAS of proof * proof |
|
146 |
| PSym of proof |
|
147 |
| PTrans of proof * proof |
|
148 |
| PComb of proof * proof |
|
149 |
| PEqMp of proof * proof |
|
150 |
| PEqImp of proof |
|
151 |
| PExists of proof * term * term |
|
152 |
| PChoose of term * proof * proof |
|
153 |
| PConj of proof * proof |
|
154 |
| PConjunct1 of proof |
|
155 |
| PConjunct2 of proof |
|
156 |
| PDisj1 of proof * term |
|
157 |
| PDisj2 of proof * term |
|
158 |
| PDisjCases of proof * proof * proof |
|
159 |
| PNotI of proof |
|
160 |
| PNotE of proof |
|
161 |
| PContr of proof * term |
|
162 |
||
163 |
exception PK of string * string |
|
164 |
fun ERR f mesg = PK (f,mesg) |
|
165 |
||
166 |
fun print_exn e = |
|
167 |
case e of |
|
168 |
PK (m,s) => (writeln ("PK (" ^ m ^ "): " ^ s); raise e) |
|
169 |
| _ => Goals.print_exn e |
|
170 |
||
171 |
(* Compatibility. *) |
|
172 |
||
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
173 |
fun mk_syn c = if Symbol.is_identifier c then NoSyn else Syntax.literal c |
14516 | 174 |
|
175 |
val keywords = ["open"] |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
176 |
fun quotename c = if Symbol.is_identifier c andalso not (c mem keywords) then c else quote c |
14516 | 177 |
|
178 |
fun smart_string_of_cterm ct = |
|
179 |
let |
|
180 |
val {sign,t,T,...} = rep_cterm ct |
|
181 |
(* Hack to avoid parse errors with Trueprop *) |
|
182 |
val ct = (cterm_of sign (HOLogic.dest_Trueprop t) |
|
183 |
handle TERM _ => ct) |
|
184 |
fun match cu = t aconv (term_of cu) |
|
185 |
fun G 0 = I |
|
186 |
| G 1 = Library.setmp show_types true |
|
187 |
| G 2 = Library.setmp show_all_types true |
|
188 |
| G _ = error ("ProofKernel.smart_string_of_cterm internal error: " ^ (G 2 string_of_cterm ct)) |
|
189 |
fun F sh_br n = |
|
190 |
let |
|
191 |
val str = Library.setmp show_brackets sh_br (G n string_of_cterm) ct |
|
192 |
val cu = transform_error (read_cterm sign) (str,T) |
|
193 |
in |
|
194 |
if match cu |
|
195 |
then quote str |
|
196 |
else F false (n+1) |
|
197 |
end |
|
198 |
handle ERROR_MESSAGE mesg => |
|
199 |
if String.isPrefix "Ambiguous" mesg andalso |
|
200 |
not sh_br |
|
201 |
then F true n |
|
202 |
else F false (n+1) |
|
203 |
in |
|
204 |
transform_error (Library.setmp Syntax.ambiguity_is_error true (F false)) 0 |
|
205 |
end |
|
206 |
handle ERROR_MESSAGE mesg => |
|
207 |
(writeln "Exception in smart_string_of_cterm!"; |
|
208 |
writeln mesg; |
|
209 |
quote (string_of_cterm ct)) |
|
210 |
||
211 |
val smart_string_of_thm = smart_string_of_cterm o cprop_of |
|
212 |
||
213 |
fun prth th = writeln ((Library.setmp print_mode [] string_of_thm) th) |
|
214 |
fun prc ct = writeln ((Library.setmp print_mode [] string_of_cterm) ct) |
|
215 |
val prin = Library.setmp print_mode [] prin |
|
216 |
fun pth (HOLThm(ren,thm)) = |
|
217 |
let |
|
218 |
val _ = writeln "Renaming:" |
|
219 |
val _ = app (fn(v,w) => (prin v; writeln " -->"; prin w)) ren |
|
220 |
val _ = prth thm |
|
221 |
in |
|
222 |
() |
|
223 |
end |
|
224 |
||
225 |
fun disk_info_of (Proof(Info{disk_info,...},_)) = !disk_info |
|
226 |
fun mk_proof p = Proof(Info{disk_info = ref None},p) |
|
227 |
fun content_of (Proof(_,p)) = p |
|
228 |
||
229 |
fun set_disk_info_of (Proof(Info{disk_info,...},_)) thyname thmname = |
|
230 |
disk_info := Some(thyname,thmname) |
|
231 |
||
232 |
structure Lib = |
|
233 |
struct |
|
234 |
fun wrap b e s = String.concat[b,s,e] |
|
235 |
||
236 |
fun assoc x = |
|
237 |
let |
|
238 |
fun F [] = raise PK("Lib.assoc","Not found") |
|
239 |
| F ((x',y)::rest) = if x = x' |
|
240 |
then y |
|
241 |
else F rest |
|
242 |
in |
|
243 |
F |
|
244 |
end |
|
245 |
fun i mem L = |
|
246 |
let fun itr [] = false |
|
247 |
| itr (a::rst) = i=a orelse itr rst |
|
248 |
in itr L end; |
|
249 |
||
250 |
fun insert i L = if i mem L then L else i::L |
|
251 |
||
252 |
fun mk_set [] = [] |
|
253 |
| mk_set (a::rst) = insert a (mk_set rst) |
|
254 |
||
255 |
fun [] union S = S |
|
256 |
| S union [] = S |
|
257 |
| (a::rst) union S2 = rst union (insert a S2) |
|
258 |
||
259 |
fun implode_subst [] = [] |
|
260 |
| implode_subst (x::r::rest) = ((x,r)::(implode_subst rest)) |
|
261 |
| implode_subst _ = raise ERR "implode_subst" "malformed substitution list" |
|
262 |
||
263 |
fun apboth f (x,y) = (f x,f y) |
|
264 |
end |
|
265 |
open Lib |
|
266 |
||
267 |
structure Tag = |
|
268 |
struct |
|
269 |
val empty_tag = Tag [] |
|
270 |
fun read name = Tag [name] |
|
271 |
fun merge (Tag tag1) (Tag tag2) = Tag (Lib.union(tag1,tag2)) |
|
272 |
end |
|
273 |
||
274 |
(* Acutal code. *) |
|
275 |
||
276 |
fun get_segment thyname l = (Lib.assoc "s" l |
|
277 |
handle PK _ => thyname) |
|
14518
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
278 |
val get_name : (string * string) list -> string = Lib.assoc "n" |
14516 | 279 |
|
280 |
local |
|
281 |
open LazyScan |
|
282 |
infix 7 |-- --| |
|
283 |
infix 5 :-- -- ^^ |
|
284 |
infix 3 >> |
|
285 |
infix 0 || |
|
286 |
in |
|
287 |
exception XML of string |
|
288 |
||
289 |
datatype xml = Elem of string * (string * string) list * xml list |
|
290 |
datatype XMLtype = XMLty of xml | FullType of hol_type |
|
291 |
datatype XMLterm = XMLtm of xml | FullTerm of term |
|
292 |
||
293 |
fun pair x y = (x,y) |
|
294 |
||
295 |
fun scan_id toks = |
|
296 |
let |
|
297 |
val (x,toks2) = one Char.isAlpha toks |
|
298 |
val (xs,toks3) = any Char.isAlphaNum toks2 |
|
299 |
in |
|
300 |
(String.implode (x::xs),toks3) |
|
301 |
end |
|
302 |
||
303 |
fun scan_string str c = |
|
304 |
let |
|
305 |
fun F [] toks = (c,toks) |
|
306 |
| F (c::cs) toks = |
|
307 |
case LazySeq.getItem toks of |
|
308 |
Some(c',toks') => |
|
309 |
if c = c' |
|
310 |
then F cs toks' |
|
311 |
else raise SyntaxError |
|
312 |
| None => raise SyntaxError |
|
313 |
in |
|
314 |
F (String.explode str) |
|
315 |
end |
|
316 |
||
317 |
local |
|
318 |
val scan_entity = |
|
319 |
(scan_string "amp;" #"&") |
|
320 |
|| scan_string "quot;" #"\"" |
|
321 |
|| scan_string "gt;" #">" |
|
322 |
|| scan_string "lt;" #"<" |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
323 |
|| scan_string "apos;" #"'" |
14516 | 324 |
in |
325 |
fun scan_nonquote toks = |
|
326 |
case LazySeq.getItem toks of |
|
327 |
Some (c,toks') => |
|
328 |
(case c of |
|
329 |
#"\"" => raise SyntaxError |
|
330 |
| #"&" => scan_entity toks' |
|
331 |
| c => (c,toks')) |
|
332 |
| None => raise SyntaxError |
|
333 |
end |
|
334 |
||
335 |
val scan_string = $$ #"\"" |-- repeat scan_nonquote --| $$ #"\"" >> |
|
336 |
String.implode |
|
337 |
||
338 |
val scan_attribute = scan_id -- $$ #"=" |-- scan_string |
|
339 |
||
340 |
val scan_start_of_tag = $$ #"<" |-- scan_id -- |
|
341 |
repeat ($$ #" " |-- scan_attribute) |
|
342 |
||
14518
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
343 |
(* The evaluation delay introduced through the 'toks' argument is needed |
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
344 |
for the sake of the SML/NJ (110.9.1) compiler. Either that or an explicit |
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
345 |
type :-( *) |
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
346 |
fun scan_end_of_tag toks = ($$ #"/" |-- $$ #">" |-- succeed []) toks |
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
347 |
|
14516 | 348 |
val scan_end_tag = $$ #"<" |-- $$ #"/" |-- scan_id --| $$ #">" |
349 |
||
350 |
fun scan_children id = $$ #">" |-- repeat scan_tag -- scan_end_tag >> |
|
351 |
(fn (chldr,id') => if id = id' |
|
352 |
then chldr |
|
353 |
else raise XML "Tag mismatch") |
|
354 |
and scan_tag toks = |
|
355 |
let |
|
356 |
val ((id,atts),toks2) = scan_start_of_tag toks |
|
357 |
val (chldr,toks3) = (scan_children id || scan_end_of_tag) toks2 |
|
358 |
in |
|
359 |
(Elem (id,atts,chldr),toks3) |
|
360 |
end |
|
361 |
end |
|
362 |
||
363 |
val type_of = Term.type_of |
|
364 |
||
365 |
val boolT = Type("bool",[]) |
|
366 |
val propT = Type("prop",[]) |
|
367 |
||
368 |
fun mk_defeq name rhs thy = |
|
369 |
let |
|
370 |
val ty = type_of rhs |
|
371 |
in |
|
372 |
Logic.mk_equals (Const(Sign.intern_const (sign_of thy) name,ty),rhs) |
|
373 |
end |
|
374 |
||
375 |
fun mk_teq name rhs thy = |
|
376 |
let |
|
377 |
val ty = type_of rhs |
|
378 |
in |
|
379 |
HOLogic.mk_eq (Const(Sign.intern_const (sign_of thy) name,ty),rhs) |
|
380 |
end |
|
381 |
||
382 |
fun intern_const_name thyname const thy = |
|
383 |
case get_hol4_const_mapping thyname const thy of |
|
384 |
Some (_,cname,_) => cname |
|
385 |
| None => (case get_hol4_const_renaming thyname const thy of |
|
386 |
Some cname => Sign.intern_const (sign_of thy) (thyname ^ "." ^ cname) |
|
387 |
| None => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const)) |
|
388 |
||
389 |
fun intern_type_name thyname const thy = |
|
390 |
case get_hol4_type_mapping thyname const thy of |
|
391 |
Some (_,cname) => cname |
|
392 |
| None => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const) |
|
393 |
||
394 |
fun mk_vartype name = TFree(name,["HOL.type"]) |
|
395 |
fun mk_thy_type thy Thy Tyop Args = Type(intern_type_name Thy Tyop thy,Args) |
|
396 |
||
397 |
val mk_var = Free |
|
398 |
||
399 |
fun dom_rng (Type("fun",[dom,rng])) = (dom,rng) |
|
400 |
| dom_rng _ = raise ERR "dom_rng" "Not a functional type" |
|
401 |
||
402 |
fun mk_thy_const thy Thy Name Ty = Const(intern_const_name Thy Name thy,Ty) |
|
403 |
||
404 |
local |
|
405 |
fun get_type sg thyname name = |
|
406 |
case Sign.const_type sg name of |
|
407 |
Some ty => ty |
|
408 |
| None => raise ERR "get_type" (name ^ ": No such constant") |
|
409 |
in |
|
410 |
fun prim_mk_const thy Thy Name = |
|
411 |
let |
|
412 |
val name = intern_const_name Thy Name thy |
|
413 |
val cmaps = HOL4ConstMaps.get thy |
|
414 |
in |
|
415 |
case StringPair.lookup(cmaps,(Thy,Name)) of |
|
416 |
Some(_,_,Some ty) => Const(name,ty) |
|
417 |
| _ => Const(name,get_type (sign_of thy) Thy name) |
|
418 |
end |
|
419 |
end |
|
420 |
||
421 |
fun mk_comb(f,a) = f $ a |
|
422 |
fun mk_abs(x,a) = Term.lambda x a |
|
423 |
||
424 |
(* Needed for HOL Light *) |
|
425 |
fun protect_tyvarname s = |
|
426 |
let |
|
427 |
fun no_quest s = |
|
428 |
if Char.contains s #"?" |
|
429 |
then String.translate (fn #"?" => "q_" | c => Char.toString c) s |
|
430 |
else s |
|
431 |
fun beg_prime s = |
|
432 |
if String.isPrefix "'" s |
|
433 |
then s |
|
434 |
else "'" ^ s |
|
435 |
in |
|
436 |
s |> no_quest |> beg_prime |
|
437 |
end |
|
438 |
fun protect_varname s = |
|
439 |
let |
|
440 |
fun no_beg_underscore s = |
|
441 |
if String.isPrefix "_" s |
|
442 |
then "dummy" ^ s |
|
443 |
else s |
|
444 |
in |
|
445 |
s |> no_beg_underscore |
|
446 |
end |
|
447 |
||
448 |
structure TypeNet = |
|
449 |
struct |
|
450 |
fun get_type_from_index thy thyname types is = |
|
451 |
case Int.fromString is of |
|
452 |
SOME i => (case Array.sub(types,i) of |
|
453 |
FullType ty => ty |
|
454 |
| XMLty xty => |
|
455 |
let |
|
456 |
val ty = get_type_from_xml thy thyname types xty |
|
457 |
val _ = Array.update(types,i,FullType ty) |
|
458 |
in |
|
459 |
ty |
|
460 |
end) |
|
461 |
| NONE => raise ERR "get_type_from_index" "Bad index" |
|
462 |
and get_type_from_xml thy thyname types = |
|
463 |
let |
|
464 |
fun gtfx (Elem("tyi",[("i",iS)],[])) = |
|
465 |
get_type_from_index thy thyname types iS |
|
466 |
| gtfx (Elem("tyc",atts,[])) = |
|
467 |
mk_thy_type thy |
|
468 |
(get_segment thyname atts) |
|
469 |
(get_name atts) |
|
470 |
[] |
|
471 |
| gtfx (Elem("tyv",[("n",s)],[])) = mk_vartype (protect_tyvarname s) |
|
472 |
| gtfx (Elem("tya",[],(Elem("tyc",atts,[]))::tys)) = |
|
473 |
mk_thy_type thy |
|
474 |
(get_segment thyname atts) |
|
475 |
(get_name atts) |
|
476 |
(map gtfx tys) |
|
477 |
| gtfx _ = raise ERR "get_type" "Bad type" |
|
478 |
in |
|
479 |
gtfx |
|
480 |
end |
|
481 |
||
482 |
fun input_types thyname (Elem("tylist",[("i",i)],xtys)) = |
|
483 |
let |
|
484 |
val types = Array.array(valOf (Int.fromString i),XMLty (Elem("",[],[]))) |
|
485 |
fun IT _ [] = () |
|
486 |
| IT n (xty::xtys) = |
|
487 |
(Array.update(types,n,XMLty xty); |
|
488 |
IT (n+1) xtys) |
|
489 |
val _ = IT 0 xtys |
|
490 |
in |
|
491 |
types |
|
492 |
end |
|
493 |
| input_types _ _ = raise ERR "input_types" "Bad type list" |
|
494 |
end |
|
495 |
||
496 |
structure TermNet = |
|
497 |
struct |
|
498 |
fun get_term_from_index thy thyname types terms is = |
|
499 |
case Int.fromString is of |
|
500 |
SOME i => (case Array.sub(terms,i) of |
|
501 |
FullTerm tm => tm |
|
502 |
| XMLtm xtm => |
|
503 |
let |
|
504 |
val tm = get_term_from_xml thy thyname types terms xtm |
|
505 |
val _ = Array.update(terms,i,FullTerm tm) |
|
506 |
in |
|
507 |
tm |
|
508 |
end) |
|
509 |
| NONE => raise ERR "get_term_from_index" "Bad index" |
|
510 |
and get_term_from_xml thy thyname types terms = |
|
511 |
let |
|
512 |
fun get_type [] = None |
|
513 |
| get_type [ty] = Some (TypeNet.get_type_from_xml thy thyname types ty) |
|
514 |
| get_type _ = raise ERR "get_term" "Bad type" |
|
515 |
||
516 |
fun gtfx (Elem("tmv",[("n",name),("t",tyi)],[])) = |
|
517 |
mk_var(protect_varname name,TypeNet.get_type_from_index thy thyname types tyi) |
|
518 |
| gtfx (Elem("tmc",atts,[])) = |
|
519 |
let |
|
520 |
val segment = get_segment thyname atts |
|
521 |
val name = get_name atts |
|
522 |
in |
|
523 |
mk_thy_const thy segment name (TypeNet.get_type_from_index thy thyname types (Lib.assoc "t" atts)) |
|
524 |
handle PK _ => prim_mk_const thy segment name |
|
525 |
end |
|
526 |
| gtfx (Elem("tma",[("f",tmf),("a",tma)],[])) = |
|
527 |
let |
|
528 |
val f = get_term_from_index thy thyname types terms tmf |
|
529 |
val a = get_term_from_index thy thyname types terms tma |
|
530 |
in |
|
531 |
mk_comb(f,a) |
|
532 |
end |
|
533 |
| gtfx (Elem("tml",[("x",tmx),("a",tma)],[])) = |
|
534 |
let |
|
535 |
val x = get_term_from_index thy thyname types terms tmx |
|
536 |
val a = get_term_from_index thy thyname types terms tma |
|
537 |
in |
|
538 |
mk_abs(x,a) |
|
539 |
end |
|
540 |
| gtfx (Elem("tmi",[("i",iS)],[])) = |
|
541 |
get_term_from_index thy thyname types terms iS |
|
542 |
| gtfx (Elem(tag,_,_)) = |
|
543 |
raise ERR "get_term" ("Not a term: "^tag) |
|
544 |
in |
|
545 |
gtfx |
|
546 |
end |
|
547 |
||
548 |
fun input_terms thyname types (Elem("tmlist",[("i",i)],xtms)) = |
|
549 |
let |
|
550 |
val terms = Array.array(valOf(Int.fromString i),XMLtm (Elem("",[],[]))) |
|
551 |
||
552 |
fun IT _ [] = () |
|
553 |
| IT n (xtm::xtms) = |
|
554 |
(Array.update(terms,n,XMLtm xtm); |
|
555 |
IT (n+1) xtms) |
|
556 |
val _ = IT 0 xtms |
|
557 |
in |
|
558 |
terms |
|
559 |
end |
|
560 |
| input_terms _ _ _ = raise ERR "input_terms" "Bad term list" |
|
561 |
end |
|
562 |
||
563 |
fun get_proof_dir (thyname:string) thy = |
|
564 |
let |
|
565 |
val import_segment = |
|
566 |
case get_segment2 thyname thy of |
|
567 |
Some seg => seg |
|
568 |
| None => get_import_segment thy |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
569 |
val defpath = [OS.Path.joinDirFile {dir=getenv "ISABELLE_HOME_USER",file="proofs"}] |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
570 |
val path = space_explode ":" (getenv "PROOF_DIRS") @ defpath |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
571 |
fun find [] = None |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
572 |
| find (p::ps) = |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
573 |
(let |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
574 |
val dir = OS.Path.joinDirFile {dir = p,file=import_segment} |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
575 |
in |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
576 |
if OS.FileSys.isDir dir |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
577 |
then Some dir |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
578 |
else find ps |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
579 |
end) handle OS.SysErr _ => find ps |
14516 | 580 |
in |
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
581 |
apsome (fn p => OS.Path.joinDirFile {dir = p, file = thyname}) (find path) |
14516 | 582 |
end |
583 |
||
584 |
fun proof_file_name thyname thmname thy = |
|
585 |
let |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
586 |
val path = case get_proof_dir thyname thy of |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
587 |
Some p => p |
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
588 |
| None => error "Cannot find proof files" |
14516 | 589 |
val _ = OS.FileSys.mkDir path handle OS.SysErr _ => () |
590 |
in |
|
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
591 |
OS.Path.joinDirFile {dir = path, file = OS.Path.joinBaseExt {base = thmname, ext = SOME "prf"}} |
14516 | 592 |
end |
593 |
||
594 |
fun xml_to_proof thyname types terms prf thy = |
|
595 |
let |
|
596 |
val xml_to_hol_type = TypeNet.get_type_from_xml thy thyname types |
|
597 |
val xml_to_term = TermNet.get_term_from_xml thy thyname types terms |
|
598 |
||
599 |
fun index_to_term is = |
|
600 |
TermNet.get_term_from_index thy thyname types terms is |
|
601 |
||
602 |
fun x2p (Elem("prefl",[("i",is)],[])) = mk_proof (PRefl (index_to_term is)) |
|
603 |
| x2p (Elem("pinstt",[],p::lambda)) = |
|
604 |
let |
|
605 |
val p = x2p p |
|
606 |
val lambda = implode_subst (map xml_to_hol_type lambda) |
|
607 |
in |
|
608 |
mk_proof (PInstT(p,lambda)) |
|
609 |
end |
|
610 |
| x2p (Elem("psubst",[("i",is)],prf::prfs)) = |
|
611 |
let |
|
612 |
val tm = index_to_term is |
|
613 |
val prf = x2p prf |
|
614 |
val prfs = map x2p prfs |
|
615 |
in |
|
616 |
mk_proof (PSubst(prfs,tm,prf)) |
|
617 |
end |
|
618 |
| x2p (Elem("pabs",[("i",is)],[prf])) = |
|
619 |
let |
|
620 |
val p = x2p prf |
|
621 |
val t = index_to_term is |
|
622 |
in |
|
623 |
mk_proof (PAbs (p,t)) |
|
624 |
end |
|
625 |
| x2p (Elem("pdisch",[("i",is)],[prf])) = |
|
626 |
let |
|
627 |
val p = x2p prf |
|
628 |
val t = index_to_term is |
|
629 |
in |
|
630 |
mk_proof (PDisch (p,t)) |
|
631 |
end |
|
632 |
| x2p (Elem("pmp",[],[prf1,prf2])) = |
|
633 |
let |
|
634 |
val p1 = x2p prf1 |
|
635 |
val p2 = x2p prf2 |
|
636 |
in |
|
637 |
mk_proof (PMp(p1,p2)) |
|
638 |
end |
|
639 |
| x2p (Elem("phyp",[("i",is)],[])) = mk_proof (PHyp (index_to_term is)) |
|
640 |
| x2p (Elem("paxiom",[("n",n),("i",is)],[])) = |
|
641 |
mk_proof (PAxm(n,index_to_term is)) |
|
642 |
| x2p (Elem("pfact",atts,[])) = |
|
643 |
let |
|
644 |
val thyname = get_segment thyname atts |
|
645 |
val thmname = get_name atts |
|
646 |
val p = mk_proof PDisk |
|
647 |
val _ = set_disk_info_of p thyname thmname |
|
648 |
in |
|
649 |
p |
|
650 |
end |
|
651 |
| x2p (Elem("pdef",[("s",seg),("n",name),("i",is)],[])) = |
|
652 |
mk_proof (PDef(seg,name,index_to_term is)) |
|
653 |
| x2p (Elem("ptmspec",[("s",seg)],p::names)) = |
|
654 |
let |
|
655 |
val names = map (fn Elem("name",[("n",name)],[]) => name |
|
656 |
| _ => raise ERR "x2p" "Bad proof (ptmspec)") names |
|
657 |
in |
|
658 |
mk_proof (PTmSpec(seg,names,x2p p)) |
|
659 |
end |
|
660 |
| x2p (Elem("ptyintro",[("s",seg),("n",name),("a",abs_name),("r",rep_name)],[xP,xt,p])) = |
|
661 |
let |
|
662 |
val P = xml_to_term xP |
|
663 |
val t = xml_to_term xt |
|
664 |
in |
|
665 |
mk_proof (PTyIntro(seg,name,abs_name,rep_name,P,t,x2p p)) |
|
666 |
end |
|
667 |
| x2p (Elem("ptydef",[("s",seg),("n",name)],[p])) = |
|
668 |
mk_proof (PTyDef(seg,name,x2p p)) |
|
669 |
| x2p (xml as Elem("poracle",[],chldr)) = |
|
670 |
let |
|
671 |
val (oracles,terms) = Library.partition (fn (Elem("oracle",_,_)) => true | _ => false) chldr |
|
672 |
val ors = map (fn (Elem("oracle",[("n",name)],[])) => name | xml => raise ERR "x2p" "bad oracle") oracles |
|
673 |
val (c,asl) = case terms of |
|
674 |
[] => raise ERR "x2p" "Bad oracle description" |
|
675 |
| (hd::tl) => (hd,tl) |
|
676 |
val tg = foldr (fn (oracle,tg) => Tag.merge (Tag.read oracle) tg) (ors,Tag.empty_tag) |
|
677 |
in |
|
678 |
mk_proof (POracle(tg,map xml_to_term asl,xml_to_term c)) |
|
679 |
end |
|
680 |
| x2p (Elem("pspec",[("i",is)],[prf])) = |
|
681 |
let |
|
682 |
val p = x2p prf |
|
683 |
val tm = index_to_term is |
|
684 |
in |
|
685 |
mk_proof (PSpec(p,tm)) |
|
686 |
end |
|
687 |
| x2p (Elem("pinst",[],p::theta)) = |
|
688 |
let |
|
689 |
val p = x2p p |
|
690 |
val theta = implode_subst (map xml_to_term theta) |
|
691 |
in |
|
692 |
mk_proof (PInst(p,theta)) |
|
693 |
end |
|
694 |
| x2p (Elem("pgen",[("i",is)],[prf])) = |
|
695 |
let |
|
696 |
val p = x2p prf |
|
697 |
val tm = index_to_term is |
|
698 |
in |
|
699 |
mk_proof (PGen(p,tm)) |
|
700 |
end |
|
701 |
| x2p (Elem("pgenabs",[],prf::tms)) = |
|
702 |
let |
|
703 |
val p = x2p prf |
|
704 |
val tml = map xml_to_term tms |
|
705 |
in |
|
706 |
mk_proof (PGenAbs(p,None,tml)) |
|
707 |
end |
|
708 |
| x2p (Elem("pgenabs",[("i",is)],prf::tms)) = |
|
709 |
let |
|
710 |
val p = x2p prf |
|
711 |
val tml = map xml_to_term tms |
|
712 |
in |
|
713 |
mk_proof (PGenAbs(p,Some (index_to_term is),tml)) |
|
714 |
end |
|
715 |
| x2p (Elem("pimpas",[],[prf1,prf2])) = |
|
716 |
let |
|
717 |
val p1 = x2p prf1 |
|
718 |
val p2 = x2p prf2 |
|
719 |
in |
|
720 |
mk_proof (PImpAS(p1,p2)) |
|
721 |
end |
|
722 |
| x2p (Elem("psym",[],[prf])) = |
|
723 |
let |
|
724 |
val p = x2p prf |
|
725 |
in |
|
726 |
mk_proof (PSym p) |
|
727 |
end |
|
728 |
| x2p (Elem("ptrans",[],[prf1,prf2])) = |
|
729 |
let |
|
730 |
val p1 = x2p prf1 |
|
731 |
val p2 = x2p prf2 |
|
732 |
in |
|
733 |
mk_proof (PTrans(p1,p2)) |
|
734 |
end |
|
735 |
| x2p (Elem("pcomb",[],[prf1,prf2])) = |
|
736 |
let |
|
737 |
val p1 = x2p prf1 |
|
738 |
val p2 = x2p prf2 |
|
739 |
in |
|
740 |
mk_proof (PComb(p1,p2)) |
|
741 |
end |
|
742 |
| x2p (Elem("peqmp",[],[prf1,prf2])) = |
|
743 |
let |
|
744 |
val p1 = x2p prf1 |
|
745 |
val p2 = x2p prf2 |
|
746 |
in |
|
747 |
mk_proof (PEqMp(p1,p2)) |
|
748 |
end |
|
749 |
| x2p (Elem("peqimp",[],[prf])) = |
|
750 |
let |
|
751 |
val p = x2p prf |
|
752 |
in |
|
753 |
mk_proof (PEqImp p) |
|
754 |
end |
|
755 |
| x2p (Elem("pexists",[("e",ise),("w",isw)],[prf])) = |
|
756 |
let |
|
757 |
val p = x2p prf |
|
758 |
val ex = index_to_term ise |
|
759 |
val w = index_to_term isw |
|
760 |
in |
|
761 |
mk_proof (PExists(p,ex,w)) |
|
762 |
end |
|
763 |
| x2p (Elem("pchoose",[("i",is)],[prf1,prf2])) = |
|
764 |
let |
|
765 |
val v = index_to_term is |
|
766 |
val p1 = x2p prf1 |
|
767 |
val p2 = x2p prf2 |
|
768 |
in |
|
769 |
mk_proof (PChoose(v,p1,p2)) |
|
770 |
end |
|
771 |
| x2p (Elem("pconj",[],[prf1,prf2])) = |
|
772 |
let |
|
773 |
val p1 = x2p prf1 |
|
774 |
val p2 = x2p prf2 |
|
775 |
in |
|
776 |
mk_proof (PConj(p1,p2)) |
|
777 |
end |
|
778 |
| x2p (Elem("pconjunct1",[],[prf])) = |
|
779 |
let |
|
780 |
val p = x2p prf |
|
781 |
in |
|
782 |
mk_proof (PConjunct1 p) |
|
783 |
end |
|
784 |
| x2p (Elem("pconjunct2",[],[prf])) = |
|
785 |
let |
|
786 |
val p = x2p prf |
|
787 |
in |
|
788 |
mk_proof (PConjunct2 p) |
|
789 |
end |
|
790 |
| x2p (Elem("pdisj1",[("i",is)],[prf])) = |
|
791 |
let |
|
792 |
val p = x2p prf |
|
793 |
val t = index_to_term is |
|
794 |
in |
|
795 |
mk_proof (PDisj1 (p,t)) |
|
796 |
end |
|
797 |
| x2p (Elem("pdisj2",[("i",is)],[prf])) = |
|
798 |
let |
|
799 |
val p = x2p prf |
|
800 |
val t = index_to_term is |
|
801 |
in |
|
802 |
mk_proof (PDisj2 (p,t)) |
|
803 |
end |
|
804 |
| x2p (Elem("pdisjcases",[],[prf1,prf2,prf3])) = |
|
805 |
let |
|
806 |
val p1 = x2p prf1 |
|
807 |
val p2 = x2p prf2 |
|
808 |
val p3 = x2p prf3 |
|
809 |
in |
|
810 |
mk_proof (PDisjCases(p1,p2,p3)) |
|
811 |
end |
|
812 |
| x2p (Elem("pnoti",[],[prf])) = |
|
813 |
let |
|
814 |
val p = x2p prf |
|
815 |
in |
|
816 |
mk_proof (PNotI p) |
|
817 |
end |
|
818 |
| x2p (Elem("pnote",[],[prf])) = |
|
819 |
let |
|
820 |
val p = x2p prf |
|
821 |
in |
|
822 |
mk_proof (PNotE p) |
|
823 |
end |
|
824 |
| x2p (Elem("pcontr",[("i",is)],[prf])) = |
|
825 |
let |
|
826 |
val p = x2p prf |
|
827 |
val t = index_to_term is |
|
828 |
in |
|
829 |
mk_proof (PContr (p,t)) |
|
830 |
end |
|
831 |
| x2p xml = raise ERR "x2p" "Bad proof" |
|
832 |
in |
|
833 |
x2p prf |
|
834 |
end |
|
835 |
||
836 |
fun import_proof thyname thmname thy = |
|
837 |
let |
|
838 |
val is = TextIO.openIn(proof_file_name thyname thmname thy) |
|
839 |
val (proof_xml,_) = scan_tag (LazySeq.of_instream is) |
|
840 |
val _ = TextIO.closeIn is |
|
841 |
in |
|
842 |
case proof_xml of |
|
843 |
Elem("proof",[],xtypes::xterms::prf::rest) => |
|
844 |
let |
|
845 |
val types = TypeNet.input_types thyname xtypes |
|
846 |
val terms = TermNet.input_terms thyname types xterms |
|
847 |
in |
|
848 |
(case rest of |
|
849 |
[] => None |
|
850 |
| [xtm] => Some (fn thy => TermNet.get_term_from_xml thy thyname types terms xtm) |
|
851 |
| _ => raise ERR "import_proof" "Bad argument list", |
|
852 |
xml_to_proof thyname types terms prf) |
|
853 |
end |
|
854 |
| _ => raise ERR "import_proof" "Bad proof" |
|
855 |
end |
|
856 |
||
857 |
fun uniq_compose m th i st = |
|
858 |
let |
|
859 |
val res = bicompose false (false,th,m) i st |
|
860 |
in |
|
861 |
case Seq.pull res of |
|
862 |
Some (th,rest) => (case Seq.pull rest of |
|
863 |
Some _ => raise ERR "uniq_compose" "Not unique!" |
|
864 |
| None => th) |
|
865 |
| None => raise ERR "uniq_compose" "No result" |
|
866 |
end |
|
867 |
||
868 |
val reflexivity_thm = thm "refl" |
|
869 |
val substitution_thm = thm "subst" |
|
870 |
val mp_thm = thm "mp" |
|
871 |
val imp_antisym_thm = thm "light_imp_as" |
|
872 |
val disch_thm = thm "impI" |
|
873 |
val ccontr_thm = thm "ccontr" |
|
874 |
||
875 |
val meta_eq_to_obj_eq_thm = thm "meta_eq_to_obj_eq" |
|
876 |
||
877 |
val gen_thm = thm "HOLallI" |
|
878 |
val choose_thm = thm "exE" |
|
879 |
val exists_thm = thm "exI" |
|
880 |
val conj_thm = thm "conjI" |
|
881 |
val conjunct1_thm = thm "conjunct1" |
|
882 |
val conjunct2_thm = thm "conjunct2" |
|
883 |
val spec_thm = thm "spec" |
|
884 |
val disj_cases_thm = thm "disjE" |
|
885 |
val disj1_thm = thm "disjI1" |
|
886 |
val disj2_thm = thm "disjI2" |
|
887 |
||
888 |
local |
|
889 |
val th = thm "not_def" |
|
890 |
val sg = sign_of_thm th |
|
891 |
val pp = reflexive (cterm_of sg (Const("Trueprop",boolT-->propT))) |
|
892 |
in |
|
893 |
val not_elim_thm = combination pp th |
|
894 |
end |
|
895 |
||
896 |
val not_intro_thm = symmetric not_elim_thm |
|
897 |
val abs_thm = thm "ext" |
|
898 |
val trans_thm = thm "trans" |
|
899 |
val symmetry_thm = thm "sym" |
|
900 |
val transitivity_thm = thm "trans" |
|
901 |
val eqmp_thm = thm "iffD1" |
|
902 |
val eqimp_thm = thm "HOL4Setup.eq_imp" |
|
903 |
val comb_thm = thm "cong" |
|
904 |
||
905 |
(* Beta-eta normalizes a theorem (only the conclusion, not the * |
|
906 |
hypotheses!) *) |
|
907 |
||
908 |
fun beta_eta_thm th = |
|
909 |
let |
|
910 |
val th1 = Thm.equal_elim (Thm.beta_conversion true (cprop_of th)) th |
|
911 |
val th2 = Thm.equal_elim (Thm.eta_conversion (cprop_of th1)) th1 |
|
912 |
in |
|
913 |
th2 |
|
914 |
end |
|
915 |
||
916 |
fun implies_elim_all th = |
|
917 |
foldl (fn (th,p) => implies_elim th (assume p)) (th,cprems_of th) |
|
918 |
||
919 |
fun norm_hyps th = |
|
920 |
th |> beta_eta_thm |
|
921 |
|> implies_elim_all |
|
922 |
|> implies_intr_hyps |
|
923 |
||
924 |
fun mk_GEN v th sg = |
|
925 |
let |
|
926 |
val c = HOLogic.dest_Trueprop (concl_of th) |
|
927 |
val cv = cterm_of sg v |
|
928 |
val lc = Term.lambda v c |
|
929 |
val clc = Thm.cterm_of sg lc |
|
930 |
val cvty = ctyp_of_term cv |
|
931 |
val th1 = implies_elim_all th |
|
932 |
val th2 = beta_eta_thm (forall_intr cv th1) |
|
933 |
val th3 = th2 COMP (beta_eta_thm (Drule.instantiate' [Some cvty] [Some clc] gen_thm)) |
|
934 |
val c = prop_of th3 |
|
935 |
val vname = fst(dest_Free v) |
|
936 |
val (cold,cnew) = case c of |
|
937 |
tpc $ (Const("All",allT) $ Abs(oldname,ty,body)) => |
|
938 |
(Abs(oldname,dummyT,Bound 0),Abs(vname,dummyT,Bound 0)) |
|
939 |
| tpc $ (Const("All",allT) $ rest) => (tpc,tpc) |
|
940 |
| _ => raise ERR "mk_GEN" "Unknown conclusion" |
|
941 |
val th4 = Thm.rename_boundvars cold cnew th3 |
|
942 |
val res = implies_intr_hyps th4 |
|
943 |
in |
|
944 |
res |
|
945 |
end |
|
946 |
||
947 |
(* rotate left k places, leaving the first j and last l premises alone |
|
948 |
*) |
|
949 |
||
950 |
fun permute_prems j k 0 th = Thm.permute_prems j k th |
|
951 |
| permute_prems j k l th = |
|
952 |
th |> Thm.permute_prems 0 (~l) |
|
953 |
|> Thm.permute_prems (j+l) k |
|
954 |
|> Thm.permute_prems 0 l |
|
955 |
||
956 |
fun rearrange sg tm th = |
|
957 |
let |
|
958 |
val tm' = Pattern.beta_eta_contract tm |
|
959 |
fun find [] n = permute_prems 0 1 0 (implies_intr (Thm.cterm_of sg tm) th) |
|
960 |
| find (p::ps) n = if tm' aconv (Pattern.beta_eta_contract p) |
|
961 |
then permute_prems n 1 0 th |
|
962 |
else find ps (n+1) |
|
963 |
in |
|
964 |
find (prems_of th) 0 |
|
965 |
end |
|
966 |
||
967 |
fun zip (x::xs) (y::ys) = (x,y)::(zip xs ys) |
|
968 |
| zip [] [] = [] |
|
969 |
| zip _ _ = raise ERR "zip" "arguments not of same length" |
|
970 |
||
971 |
fun mk_INST dom rng th = |
|
972 |
th |> forall_intr_list dom |
|
973 |
|> forall_elim_list rng |
|
974 |
||
975 |
fun apply_tyinst_typ tyinst = |
|
976 |
let |
|
977 |
fun G (ty as TFree _) = |
|
978 |
(case try (Lib.assoc ty) tyinst of |
|
979 |
Some ty' => ty' |
|
980 |
| None => ty) |
|
981 |
| G (Type(tyname,tys)) = Type(tyname,map G tys) |
|
982 |
| G (TVar _) = raise ERR "apply_tyinst_typ" "Scematic variable found" |
|
983 |
in |
|
984 |
G |
|
985 |
end |
|
986 |
||
987 |
fun apply_tyinst_term tyinst = |
|
988 |
let |
|
989 |
val G = apply_tyinst_typ tyinst |
|
990 |
fun F (tm as Bound _) = tm |
|
991 |
| F (tm as Free(vname,ty)) = Free(vname,G ty) |
|
992 |
| F (tm as Const(vname,ty)) = Const(vname,G ty) |
|
993 |
| F (tm1 $ tm2) = (F tm1) $ (F tm2) |
|
994 |
| F (Abs(vname,ty,body)) = Abs(vname,G ty,F body) |
|
995 |
| F (Var _) = raise ERR "apply_tyinst_term" "Schematic variable found" |
|
996 |
in |
|
997 |
F |
|
998 |
end |
|
999 |
||
1000 |
fun apply_inst_term tminst = |
|
1001 |
let |
|
1002 |
fun F (tm as Bound _) = tm |
|
1003 |
| F (tm as Free _) = |
|
1004 |
(case try (Lib.assoc tm) tminst of |
|
1005 |
Some tm' => tm' |
|
1006 |
| None => tm) |
|
1007 |
| F (tm as Const _) = tm |
|
1008 |
| F (tm1 $ tm2) = (F tm1) $ (F tm2) |
|
1009 |
| F (Abs(vname,ty,body)) = Abs(vname,ty,F body) |
|
1010 |
| F (Var _) = raise ERR "apply_inst_term" "Schematic variable found" |
|
1011 |
in |
|
1012 |
F |
|
1013 |
end |
|
1014 |
||
1015 |
val collect_vars = |
|
1016 |
let |
|
1017 |
fun F vars (Bound _) = vars |
|
1018 |
| F vars (tm as Free _) = |
|
1019 |
if tm mem vars |
|
1020 |
then vars |
|
1021 |
else (tm::vars) |
|
1022 |
| F vars (Const _) = vars |
|
1023 |
| F vars (tm1 $ tm2) = F (F vars tm1) tm2 |
|
1024 |
| F vars (Abs(_,_,body)) = F vars body |
|
1025 |
| F vars (Var _) = raise ERR "collect_vars" "Schematic variable found" |
|
1026 |
in |
|
1027 |
F [] |
|
1028 |
end |
|
1029 |
||
1030 |
(* Code for disambiguating variablenames (wrt. types) *) |
|
1031 |
||
1032 |
val disamb_info_empty = {vars=[],rens=[]} |
|
1033 |
||
1034 |
fun rens_of {vars,rens} = rens |
|
1035 |
||
1036 |
fun name_of_var (Free(vname,_)) = vname |
|
1037 |
| name_of_var _ = raise ERR "name_of_var" "Not a variable" |
|
1038 |
||
1039 |
fun disamb_helper {vars,rens} tm = |
|
1040 |
let |
|
1041 |
val varstm = collect_vars tm |
|
1042 |
fun process (v as Free(vname,ty),(vars,rens,inst)) = |
|
1043 |
if v mem vars |
|
1044 |
then (vars,rens,inst) |
|
1045 |
else (case try (Lib.assoc v) rens of |
|
1046 |
Some vnew => (vars,rens,(v,vnew)::inst) |
|
1047 |
| None => if exists (fn Free(vname',_) => vname = vname' | _ => raise ERR "disamb_helper" "Bad varlist") vars |
|
1048 |
then |
|
1049 |
let |
|
1050 |
val tmnames = map name_of_var varstm |
|
1051 |
val varnames = map name_of_var vars |
|
1052 |
val (dom,rng) = ListPair.unzip rens |
|
1053 |
val rensnames = (map name_of_var dom) @ (map name_of_var rng) |
|
1054 |
val instnames = map name_of_var (snd (ListPair.unzip inst)) |
|
1055 |
val allnames = tmnames @ varnames @ rensnames @ instnames |
|
1056 |
val vnewname = Term.variant allnames vname |
|
1057 |
val vnew = Free(vnewname,ty) |
|
1058 |
in |
|
1059 |
(vars,(v,vnew)::rens,(v,vnew)::inst) |
|
1060 |
end |
|
1061 |
else (v::vars,rens,inst)) |
|
1062 |
| process _ = raise ERR "disamb_helper" "Internal error" |
|
1063 |
||
1064 |
val (vars',rens',inst) = |
|
1065 |
foldr process (varstm,(vars,rens,[])) |
|
1066 |
in |
|
1067 |
({vars=vars',rens=rens'},inst) |
|
1068 |
end |
|
1069 |
||
1070 |
fun disamb_term_from info tm = |
|
1071 |
let |
|
1072 |
val (info',inst) = disamb_helper info tm |
|
1073 |
in |
|
1074 |
(info',apply_inst_term inst tm) |
|
1075 |
end |
|
1076 |
||
1077 |
fun swap (x,y) = (y,x) |
|
1078 |
||
1079 |
fun has_ren (HOLThm([],_)) = false |
|
1080 |
| has_ren _ = true |
|
1081 |
||
1082 |
fun prinfo {vars,rens} = (writeln "Vars:"; |
|
1083 |
app prin vars; |
|
1084 |
writeln "Renaming:"; |
|
1085 |
app (fn(x,y)=>(prin x; writeln " -->"; prin y)) rens) |
|
1086 |
||
1087 |
fun disamb_thm_from info (hth as HOLThm(rens,thm)) = |
|
1088 |
let |
|
1089 |
val inv_rens = map swap rens |
|
1090 |
val orig_thm = apply_inst_term inv_rens (prop_of thm) |
|
1091 |
val (info',inst) = disamb_helper info orig_thm |
|
1092 |
val rens' = map (apsnd (apply_inst_term inst)) inv_rens |
|
1093 |
val (dom,rng) = ListPair.unzip (rens' @ inst) |
|
1094 |
val sg = sign_of_thm thm |
|
1095 |
val thm' = mk_INST (map (cterm_of sg) dom) (map (cterm_of sg) rng) thm |
|
1096 |
in |
|
1097 |
(info',thm') |
|
1098 |
end |
|
1099 |
||
1100 |
fun disamb_terms_from info tms = |
|
1101 |
foldr (fn (tm,(info,tms)) => |
|
1102 |
let |
|
1103 |
val (info',tm') = disamb_term_from info tm |
|
1104 |
in |
|
1105 |
(info',tm'::tms) |
|
1106 |
end) |
|
1107 |
(tms,(info,[])) |
|
1108 |
||
1109 |
fun disamb_thms_from info hthms = |
|
1110 |
foldr (fn (hthm,(info,thms)) => |
|
1111 |
let |
|
1112 |
val (info',tm') = disamb_thm_from info hthm |
|
1113 |
in |
|
1114 |
(info',tm'::thms) |
|
1115 |
end) |
|
1116 |
(hthms,(info,[])) |
|
1117 |
||
1118 |
fun disamb_term tm = disamb_term_from disamb_info_empty tm |
|
1119 |
fun disamb_terms tms = disamb_terms_from disamb_info_empty tms |
|
1120 |
fun disamb_thm thm = disamb_thm_from disamb_info_empty thm |
|
1121 |
fun disamb_thms thms = disamb_thms_from disamb_info_empty thms |
|
1122 |
||
1123 |
fun norm_hthm sg (hth as HOLThm([],_)) = hth |
|
1124 |
| norm_hthm sg (hth as HOLThm(rens,th)) = |
|
1125 |
let |
|
1126 |
val vars = collect_vars (prop_of th) |
|
1127 |
val (rens',inst,_) = |
|
1128 |
foldr (fn((ren as (vold as Free(vname,_),vnew)), |
|
1129 |
(rens,inst,vars)) => |
|
1130 |
(case Library.find_first (fn Free(v,_) => v = vname | _ => false) vars of |
|
1131 |
Some v' => if v' = vold |
|
1132 |
then (rens,(vnew,vold)::inst,vold::vars) |
|
1133 |
else (ren::rens,(vold,vnew)::inst,vnew::vars) |
|
1134 |
| None => (rens,(vnew,vold)::inst,vold::vars)) |
|
1135 |
| _ => raise ERR "norm_hthm" "Internal error") |
|
1136 |
(rens,([],[],vars)) |
|
1137 |
val (dom,rng) = ListPair.unzip inst |
|
1138 |
val th' = mk_INST (map (cterm_of sg) dom) (map (cterm_of sg) rng) th |
|
1139 |
val nvars = collect_vars (prop_of th') |
|
1140 |
val rens' = filter (fn(_,v) => v mem nvars) rens |
|
1141 |
val res = HOLThm(rens',th') |
|
1142 |
in |
|
1143 |
res |
|
1144 |
end |
|
1145 |
||
1146 |
(* End of disambiguating code *) |
|
1147 |
||
1148 |
val debug = ref false |
|
1149 |
||
1150 |
fun if_debug f x = if !debug then f x else () |
|
1151 |
val message = if_debug writeln |
|
1152 |
||
1153 |
val conjE_helper = Thm.permute_prems 0 1 conjE |
|
1154 |
||
1155 |
fun get_hol4_thm thyname thmname thy = |
|
1156 |
case get_hol4_theorem thyname thmname thy of |
|
1157 |
Some hth => Some (HOLThm hth) |
|
1158 |
| None => |
|
1159 |
let |
|
1160 |
val pending = HOL4Pending.get thy |
|
1161 |
in |
|
1162 |
case StringPair.lookup (pending,(thyname,thmname)) of |
|
1163 |
Some hth => Some (HOLThm hth) |
|
1164 |
| None => None |
|
1165 |
end |
|
1166 |
||
1167 |
fun non_trivial_term_consts tm = |
|
1168 |
filter (fn c => not (c = "Trueprop" orelse |
|
1169 |
c = "All" orelse |
|
1170 |
c = "op -->" orelse |
|
1171 |
c = "op &" orelse |
|
1172 |
c = "op =")) (Term.term_consts tm) |
|
1173 |
||
1174 |
fun match_consts t (* th *) = |
|
1175 |
let |
|
1176 |
fun add_consts (Const (c, _), cs) = |
|
1177 |
(case c of |
|
1178 |
"op =" => "==" ins_string cs |
|
1179 |
| "op -->" => "==>" ins_string cs |
|
1180 |
| "All" => cs |
|
1181 |
| "all" => cs |
|
1182 |
| "op &" => cs |
|
1183 |
| "Trueprop" => cs |
|
1184 |
| _ => c ins_string cs) |
|
1185 |
| add_consts (t $ u, cs) = add_consts (t, add_consts (u, cs)) |
|
1186 |
| add_consts (Abs (_, _, t), cs) = add_consts (t, cs) |
|
1187 |
| add_consts (_, cs) = cs |
|
1188 |
val t_consts = add_consts(t,[]) |
|
1189 |
in |
|
1190 |
fn th => eq_set(t_consts,add_consts(prop_of th,[])) |
|
1191 |
end |
|
1192 |
||
1193 |
fun split_name str = |
|
1194 |
let |
|
1195 |
val sub = Substring.all str |
|
1196 |
val (f,idx) = apsnd Substring.string (Substring.splitr Char.isDigit sub) |
|
1197 |
val (newstr,u) = apboth Substring.string (Substring.splitr (fn c => c = #"_") f) |
|
1198 |
in |
|
1199 |
if not (idx = "") andalso u = "_" |
|
1200 |
then Some (newstr,valOf(Int.fromString idx)) |
|
1201 |
else None |
|
1202 |
end |
|
1203 |
handle _ => None |
|
1204 |
||
1205 |
fun rewrite_hol4_term t thy = |
|
1206 |
let |
|
1207 |
val sg = sign_of thy |
|
1208 |
||
1209 |
val hol4rews1 = map (transfer_sg sg) (HOL4Rewrites.get thy) |
|
1210 |
val hol4ss = empty_ss setmksimps single addsimps hol4rews1 |
|
1211 |
in |
|
1212 |
transfer_sg sg (Simplifier.full_rewrite hol4ss (cterm_of sg t)) |
|
1213 |
end |
|
1214 |
||
1215 |
||
1216 |
fun get_isabelle_thm thyname thmname hol4conc thy = |
|
1217 |
let |
|
1218 |
val sg = sign_of thy |
|
1219 |
||
1220 |
val (info,hol4conc') = disamb_term hol4conc |
|
1221 |
val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy) |
|
1222 |
val isaconc = |
|
1223 |
case concl_of i2h_conc of |
|
1224 |
Const("==",_) $ lhs $ _ => lhs |
|
1225 |
| _ => error "get_isabelle_thm" "Bad rewrite rule" |
|
1226 |
val _ = (message "Original conclusion:"; |
|
1227 |
if_debug prin hol4conc'; |
|
1228 |
message "Modified conclusion:"; |
|
1229 |
if_debug prin isaconc) |
|
1230 |
||
1231 |
fun mk_res th = HOLThm(rens_of info,equal_elim i2h_conc th) |
|
1232 |
in |
|
1233 |
case get_hol4_mapping thyname thmname thy of |
|
1234 |
Some (Some thmname) => |
|
1235 |
let |
|
1236 |
val _ = message ("Looking for " ^ thmname) |
|
1237 |
val th1 = (Some (transform_error (PureThy.get_thm thy) thmname) |
|
1238 |
handle ERROR_MESSAGE _ => |
|
1239 |
(case split_name thmname of |
|
1240 |
Some (listname,idx) => (Some (nth_elem(idx-1,PureThy.get_thms thy listname)) |
|
1241 |
handle _ => None) |
|
1242 |
| None => None)) |
|
1243 |
in |
|
1244 |
case th1 of |
|
1245 |
Some th2 => |
|
1246 |
(case Shuffler.set_prop thy isaconc [(thmname,th2)] of |
|
1247 |
Some (_,th) => (message "YES";(thy, Some (mk_res th))) |
|
1248 |
| None => (message "NO2";error "get_isabelle_thm" "Bad mapping")) |
|
1249 |
| None => (message "NO1";error "get_isabelle_thm" "Bad mapping") |
|
1250 |
end |
|
1251 |
| Some None => error ("Trying to access ignored theorem " ^ thmname) |
|
1252 |
| None => |
|
1253 |
let |
|
1254 |
val _ = (message "Looking for conclusion:"; |
|
1255 |
if_debug prin isaconc) |
|
1256 |
val cs = non_trivial_term_consts isaconc |
|
1257 |
val _ = (message "Looking for consts:"; |
|
1258 |
message (String.concat cs)) |
|
1259 |
val pot_thms = Shuffler.find_potential thy isaconc |
|
1260 |
val _ = message ((Int.toString (length pot_thms)) ^ " potential theorems") |
|
1261 |
in |
|
1262 |
case Shuffler.set_prop thy isaconc pot_thms of |
|
1263 |
Some (isaname,th) => |
|
1264 |
let |
|
1265 |
val hth as HOLThm args = mk_res th |
|
1266 |
val thy' = thy |> add_hol4_theorem thyname thmname args |
|
1267 |
|> add_hol4_mapping thyname thmname isaname |
|
1268 |
in |
|
1269 |
(thy',Some hth) |
|
1270 |
end |
|
1271 |
| None => (thy,None) |
|
1272 |
end |
|
1273 |
end |
|
1274 |
handle _ => (thy,None) |
|
1275 |
||
1276 |
fun get_thm thyname thmname thy = |
|
1277 |
case get_hol4_thm thyname thmname thy of |
|
1278 |
Some hth => (thy,Some hth) |
|
1279 |
| None => ((case fst (import_proof thyname thmname thy) of |
|
1280 |
Some f => get_isabelle_thm thyname thmname (f thy) thy |
|
1281 |
| None => (thy,None)) |
|
1282 |
handle e as IO.Io _ => (thy,None) |
|
1283 |
| e as PK _ => (thy,None)) |
|
1284 |
||
1285 |
fun rename_const thyname thy name = |
|
1286 |
case get_hol4_const_renaming thyname name thy of |
|
1287 |
Some cname => cname |
|
1288 |
| None => name |
|
1289 |
||
1290 |
fun get_def thyname constname rhs thy = |
|
1291 |
let |
|
1292 |
val constname = rename_const thyname thy constname |
|
1293 |
val (thmname,thy') = get_defname thyname constname thy |
|
1294 |
val _ = message ("Looking for definition " ^ thyname ^ "." ^ thmname) |
|
1295 |
in |
|
1296 |
get_isabelle_thm thyname thmname (mk_teq (thyname ^ "." ^ constname) rhs thy') thy' |
|
1297 |
end |
|
1298 |
||
1299 |
fun get_axiom thyname axname thy = |
|
1300 |
case get_thm thyname axname thy of |
|
1301 |
arg as (_,Some _) => arg |
|
1302 |
| _ => raise ERR "get_axiom" ("Trying to retrieve axiom (" ^ axname ^ ")") |
|
1303 |
||
1304 |
fun intern_store_thm gen_output thyname thmname hth thy = |
|
1305 |
let |
|
1306 |
val sg = sign_of thy |
|
1307 |
val (hth' as HOLThm (args as (_,th))) = norm_hthm sg hth |
|
1308 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating") |
|
1309 |
else () |
|
1310 |
val rew = rewrite_hol4_term (concl_of th) thy |
|
1311 |
val th = equal_elim rew th |
|
1312 |
val thy' = add_hol4_pending thyname thmname args thy |
|
1313 |
val thy2 = if gen_output |
|
1314 |
then add_dump ("lemma " ^ thmname ^ ": " ^ (smart_string_of_thm th) ^ "\n by (import " ^ thyname ^ " " ^ thmname ^ ")") thy' |
|
1315 |
else thy' |
|
1316 |
in |
|
1317 |
(thy2,hth') |
|
1318 |
end |
|
1319 |
||
1320 |
val store_thm = intern_store_thm true |
|
1321 |
||
1322 |
fun mk_REFL ctm = |
|
1323 |
let |
|
1324 |
val cty = Thm.ctyp_of_term ctm |
|
1325 |
in |
|
1326 |
Drule.instantiate' [Some cty] [Some ctm] reflexivity_thm |
|
1327 |
end |
|
1328 |
||
1329 |
fun REFL tm thy = |
|
1330 |
let |
|
1331 |
val _ = message "REFL:" |
|
1332 |
val (info,tm') = disamb_term tm |
|
1333 |
val sg = sign_of thy |
|
1334 |
val ctm = Thm.cterm_of sg tm' |
|
1335 |
val res = HOLThm(rens_of info,mk_REFL ctm) |
|
1336 |
val _ = if_debug pth res |
|
1337 |
in |
|
1338 |
(thy,res) |
|
1339 |
end |
|
1340 |
||
1341 |
fun ASSUME tm thy = |
|
1342 |
let |
|
1343 |
val _ = message "ASSUME:" |
|
1344 |
val (info,tm') = disamb_term tm |
|
1345 |
val sg = sign_of thy |
|
1346 |
val ctm = Thm.cterm_of sg (HOLogic.mk_Trueprop tm') |
|
1347 |
val th = Thm.trivial ctm |
|
1348 |
val res = HOLThm(rens_of info,th) |
|
1349 |
val _ = if_debug pth res |
|
1350 |
in |
|
1351 |
(thy,res) |
|
1352 |
end |
|
1353 |
||
1354 |
fun INST_TYPE lambda (hth as HOLThm(rens,th)) thy = |
|
1355 |
let |
|
1356 |
val _ = message "INST_TYPE:" |
|
1357 |
val _ = if_debug pth hth |
|
1358 |
val sg = sign_of thy |
|
1359 |
val tys_before = add_term_tfrees (prop_of th,[]) |
|
1360 |
val th1 = varifyT th |
|
1361 |
val tys_after = add_term_tvars (prop_of th1,[]) |
|
1362 |
val tyinst = map (fn (bef,(i,_)) => |
|
1363 |
(case try (Lib.assoc (TFree bef)) lambda of |
|
1364 |
Some ty => (i,ctyp_of sg ty) |
|
1365 |
| None => (i,ctyp_of sg (TFree bef)) |
|
1366 |
)) |
|
1367 |
(zip tys_before tys_after) |
|
1368 |
val res = Drule.instantiate (tyinst,[]) th1 |
|
1369 |
val appty = apboth (apply_tyinst_term lambda) |
|
1370 |
val hth = HOLThm(map appty rens,res) |
|
1371 |
val res = norm_hthm sg hth |
|
1372 |
val _ = message "RESULT:" |
|
1373 |
val _ = if_debug pth res |
|
1374 |
in |
|
1375 |
(thy,res) |
|
1376 |
end |
|
1377 |
||
1378 |
fun INST sigma hth thy = |
|
1379 |
let |
|
1380 |
val _ = message "INST:" |
|
1381 |
val _ = if_debug (app (fn (x,y) => (prin x; prin y))) sigma |
|
1382 |
val _ = if_debug pth hth |
|
1383 |
val sg = sign_of thy |
|
1384 |
val (sdom,srng) = ListPair.unzip sigma |
|
1385 |
val (info,th) = disamb_thm hth |
|
1386 |
val (info',srng') = disamb_terms_from info srng |
|
1387 |
val rens = rens_of info' |
|
1388 |
val sdom' = map (apply_inst_term rens) sdom |
|
1389 |
val th1 = mk_INST (map (cterm_of sg) sdom') (map (cterm_of sg) srng') th |
|
1390 |
val res = HOLThm(rens,th1) |
|
1391 |
val _ = message "RESULT:" |
|
1392 |
val _ = if_debug pth res |
|
1393 |
in |
|
1394 |
(thy,res) |
|
1395 |
end |
|
1396 |
||
1397 |
fun EQ_IMP_RULE (hth as HOLThm(rens,th)) thy = |
|
1398 |
let |
|
1399 |
val _ = message "EQ_IMP_RULE:" |
|
1400 |
val _ = if_debug pth hth |
|
1401 |
val res = HOLThm(rens,th RS eqimp_thm) |
|
1402 |
val _ = message "RESULT:" |
|
1403 |
val _ = if_debug pth res |
|
1404 |
in |
|
1405 |
(thy,res) |
|
1406 |
end |
|
1407 |
||
1408 |
fun mk_EQ_MP th1 th2 = [beta_eta_thm th1,beta_eta_thm th2] MRS eqmp_thm |
|
1409 |
||
1410 |
fun EQ_MP hth1 hth2 thy = |
|
1411 |
let |
|
1412 |
val _ = message "EQ_MP:" |
|
1413 |
val _ = if_debug pth hth1 |
|
1414 |
val _ = if_debug pth hth2 |
|
1415 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1416 |
val res = HOLThm(rens_of info,mk_EQ_MP th1 th2) |
|
1417 |
val _ = message "RESULT:" |
|
1418 |
val _ = if_debug pth res |
|
1419 |
in |
|
1420 |
(thy,res) |
|
1421 |
end |
|
1422 |
||
1423 |
fun mk_COMB th1 th2 sg = |
|
1424 |
let |
|
1425 |
val (f,g) = case concl_of th1 of |
|
1426 |
_ $ (Const("op =",_) $ f $ g) => (f,g) |
|
1427 |
| _ => raise ERR "mk_COMB" "First theorem not an equality" |
|
1428 |
val (x,y) = case concl_of th2 of |
|
1429 |
_ $ (Const("op =",_) $ x $ y) => (x,y) |
|
1430 |
| _ => raise ERR "mk_COMB" "Second theorem not an equality" |
|
1431 |
val fty = type_of f |
|
1432 |
val (fd,fr) = dom_rng fty |
|
1433 |
val comb_thm' = Drule.instantiate' |
|
1434 |
[Some (ctyp_of sg fd),Some (ctyp_of sg fr)] |
|
1435 |
[Some (cterm_of sg f),Some (cterm_of sg g), |
|
1436 |
Some (cterm_of sg x),Some (cterm_of sg y)] comb_thm |
|
1437 |
in |
|
1438 |
[th1,th2] MRS comb_thm' |
|
1439 |
end |
|
1440 |
||
1441 |
fun SUBST rews ctxt hth thy = |
|
1442 |
let |
|
1443 |
val _ = message "SUBST:" |
|
1444 |
val _ = if_debug (app pth) rews |
|
1445 |
val _ = if_debug prin ctxt |
|
1446 |
val _ = if_debug pth hth |
|
1447 |
val (info,th) = disamb_thm hth |
|
1448 |
val (info1,ctxt') = disamb_term_from info ctxt |
|
1449 |
val (info2,rews') = disamb_thms_from info1 rews |
|
1450 |
||
1451 |
val sg = sign_of thy |
|
1452 |
val cctxt = cterm_of sg ctxt' |
|
1453 |
fun subst th [] = th |
|
1454 |
| subst th (rew::rews) = subst (mk_COMB th rew sg) rews |
|
1455 |
val res = HOLThm(rens_of info2,mk_EQ_MP (subst (mk_REFL cctxt) rews') th) |
|
1456 |
val _ = message "RESULT:" |
|
1457 |
val _ = if_debug pth res |
|
1458 |
in |
|
1459 |
(thy,res) |
|
1460 |
end |
|
1461 |
||
1462 |
fun DISJ_CASES hth hth1 hth2 thy = |
|
1463 |
let |
|
1464 |
val _ = message "DISJ_CASES:" |
|
1465 |
val _ = if_debug (app pth) [hth,hth1,hth2] |
|
1466 |
val (info,th) = disamb_thm hth |
|
1467 |
val (info1,th1) = disamb_thm_from info hth1 |
|
1468 |
val (info2,th2) = disamb_thm_from info1 hth2 |
|
1469 |
val sg = sign_of thy |
|
1470 |
val th1 = norm_hyps th1 |
|
1471 |
val th2 = norm_hyps th2 |
|
1472 |
val (l,r) = case concl_of th of |
|
1473 |
_ $ (Const("op |",_) $ l $ r) => (l,r) |
|
1474 |
| _ => raise ERR "DISJ_CASES" "Conclusion not a disjunction" |
|
1475 |
val th1' = rearrange sg (HOLogic.mk_Trueprop l) th1 |
|
1476 |
val th2' = rearrange sg (HOLogic.mk_Trueprop r) th2 |
|
1477 |
val res1 = th RS disj_cases_thm |
|
1478 |
val res2 = uniq_compose ((nprems_of th1')-1) th1' ((nprems_of th)+1) res1 |
|
1479 |
val res3 = uniq_compose ((nprems_of th2')-1) th2' (nprems_of res2) res2 |
|
1480 |
val res = HOLThm(rens_of info2,res3) |
|
1481 |
val _ = message "RESULT:" |
|
1482 |
val _ = if_debug pth res |
|
1483 |
in |
|
1484 |
(thy,res) |
|
1485 |
end |
|
1486 |
||
1487 |
fun DISJ1 hth tm thy = |
|
1488 |
let |
|
1489 |
val _ = message "DISJ1:" |
|
1490 |
val _ = if_debug pth hth |
|
1491 |
val _ = if_debug prin tm |
|
1492 |
val (info,th) = disamb_thm hth |
|
1493 |
val (info',tm') = disamb_term_from info tm |
|
1494 |
val sg = sign_of thy |
|
1495 |
val ct = Thm.cterm_of sg tm' |
|
1496 |
val disj1_thm' = Drule.instantiate' [] [None,Some ct] disj1_thm |
|
1497 |
val res = HOLThm(rens_of info',th RS disj1_thm') |
|
1498 |
val _ = message "RESULT:" |
|
1499 |
val _ = if_debug pth res |
|
1500 |
in |
|
1501 |
(thy,res) |
|
1502 |
end |
|
1503 |
||
1504 |
fun DISJ2 tm hth thy = |
|
1505 |
let |
|
1506 |
val _ = message "DISJ1:" |
|
1507 |
val _ = if_debug prin tm |
|
1508 |
val _ = if_debug pth hth |
|
1509 |
val (info,th) = disamb_thm hth |
|
1510 |
val (info',tm') = disamb_term_from info tm |
|
1511 |
val sg = sign_of thy |
|
1512 |
val ct = Thm.cterm_of sg tm' |
|
1513 |
val disj2_thm' = Drule.instantiate' [] [None,Some ct] disj2_thm |
|
1514 |
val res = HOLThm(rens_of info',th RS disj2_thm') |
|
1515 |
val _ = message "RESULT:" |
|
1516 |
val _ = if_debug pth res |
|
1517 |
in |
|
1518 |
(thy,res) |
|
1519 |
end |
|
1520 |
||
1521 |
fun IMP_ANTISYM hth1 hth2 thy = |
|
1522 |
let |
|
1523 |
val _ = message "IMP_ANTISYM:" |
|
1524 |
val _ = if_debug pth hth1 |
|
1525 |
val _ = if_debug pth hth2 |
|
1526 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1527 |
val th = [beta_eta_thm th1,beta_eta_thm th2] MRS imp_antisym_thm |
|
1528 |
val res = HOLThm(rens_of info,th) |
|
1529 |
val _ = message "RESULT:" |
|
1530 |
val _ = if_debug pth res |
|
1531 |
in |
|
1532 |
(thy,res) |
|
1533 |
end |
|
1534 |
||
1535 |
fun SYM (hth as HOLThm(rens,th)) thy = |
|
1536 |
let |
|
1537 |
val _ = message "SYM:" |
|
1538 |
val _ = if_debug pth hth |
|
1539 |
val th = th RS symmetry_thm |
|
1540 |
val res = HOLThm(rens,th) |
|
1541 |
val _ = message "RESULT:" |
|
1542 |
val _ = if_debug pth res |
|
1543 |
in |
|
1544 |
(thy,res) |
|
1545 |
end |
|
1546 |
||
1547 |
fun MP hth1 hth2 thy = |
|
1548 |
let |
|
1549 |
val _ = message "MP:" |
|
1550 |
val _ = if_debug pth hth1 |
|
1551 |
val _ = if_debug pth hth2 |
|
1552 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1553 |
val th = [beta_eta_thm th1,beta_eta_thm th2] MRS mp_thm |
|
1554 |
val res = HOLThm(rens_of info,th) |
|
1555 |
val _ = message "RESULT:" |
|
1556 |
val _ = if_debug pth res |
|
1557 |
in |
|
1558 |
(thy,res) |
|
1559 |
end |
|
1560 |
||
1561 |
fun CONJ hth1 hth2 thy = |
|
1562 |
let |
|
1563 |
val _ = message "CONJ:" |
|
1564 |
val _ = if_debug pth hth1 |
|
1565 |
val _ = if_debug pth hth2 |
|
1566 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1567 |
val th = [th1,th2] MRS conj_thm |
|
1568 |
val res = HOLThm(rens_of info,th) |
|
1569 |
val _ = message "RESULT:" |
|
1570 |
val _ = if_debug pth res |
|
1571 |
in |
|
1572 |
(thy,res) |
|
1573 |
end |
|
1574 |
||
1575 |
fun CONJUNCT1 (hth as HOLThm(rens,th)) thy = |
|
1576 |
let |
|
1577 |
val _ = message "CONJUNCT1:" |
|
1578 |
val _ = if_debug pth hth |
|
1579 |
val res = HOLThm(rens,th RS conjunct1_thm) |
|
1580 |
val _ = message "RESULT:" |
|
1581 |
val _ = if_debug pth res |
|
1582 |
in |
|
1583 |
(thy,res) |
|
1584 |
end |
|
1585 |
||
1586 |
fun CONJUNCT2 (hth as HOLThm(rens,th)) thy = |
|
1587 |
let |
|
1588 |
val _ = message "CONJUNCT1:" |
|
1589 |
val _ = if_debug pth hth |
|
1590 |
val res = HOLThm(rens,th RS conjunct2_thm) |
|
1591 |
val _ = message "RESULT:" |
|
1592 |
val _ = if_debug pth res |
|
1593 |
in |
|
1594 |
(thy,res) |
|
1595 |
end |
|
1596 |
||
1597 |
fun EXISTS ex wit hth thy = |
|
1598 |
let |
|
1599 |
val _ = message "EXISTS:" |
|
1600 |
val _ = if_debug prin ex |
|
1601 |
val _ = if_debug prin wit |
|
1602 |
val _ = if_debug pth hth |
|
1603 |
val (info,th) = disamb_thm hth |
|
1604 |
val (info',[ex',wit']) = disamb_terms_from info [ex,wit] |
|
1605 |
val sg = sign_of thy |
|
1606 |
val cwit = cterm_of sg wit' |
|
1607 |
val cty = ctyp_of_term cwit |
|
1608 |
val a = case ex' of |
|
1609 |
(Const("Ex",_) $ a) => a |
|
1610 |
| _ => raise ERR "EXISTS" "Argument not existential" |
|
1611 |
val ca = cterm_of sg a |
|
1612 |
val exists_thm' = beta_eta_thm (Drule.instantiate' [Some cty] [Some ca,Some cwit] exists_thm) |
|
1613 |
val th1 = beta_eta_thm th |
|
1614 |
val th2 = implies_elim_all th1 |
|
1615 |
val th3 = th2 COMP exists_thm' |
|
1616 |
val th = implies_intr_hyps th3 |
|
1617 |
val res = HOLThm(rens_of info',th) |
|
1618 |
val _ = message "RESULT:" |
|
1619 |
val _ = if_debug pth res |
|
1620 |
in |
|
1621 |
(thy,res) |
|
1622 |
end |
|
1623 |
||
1624 |
fun CHOOSE v hth1 hth2 thy = |
|
1625 |
let |
|
1626 |
val _ = message "CHOOSE:" |
|
1627 |
val _ = if_debug prin v |
|
1628 |
val _ = if_debug pth hth1 |
|
1629 |
val _ = if_debug pth hth2 |
|
1630 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1631 |
val (info',v') = disamb_term_from info v |
|
1632 |
fun strip 0 _ th = th |
|
1633 |
| strip n (p::ps) th = |
|
1634 |
strip (n-1) ps (implies_elim th (assume p)) |
|
1635 |
| strip _ _ _ = raise ERR "CHOOSE" "strip error" |
|
1636 |
val sg = sign_of thy |
|
1637 |
val cv = cterm_of sg v' |
|
1638 |
val th2 = norm_hyps th2 |
|
1639 |
val cvty = ctyp_of_term cv |
|
14518
c3019a66180f
Added a number of explicit type casts and delayed evaluations (all seemingly
skalberg
parents:
14516
diff
changeset
|
1640 |
val c = HOLogic.dest_Trueprop (concl_of th2) |
14516 | 1641 |
val cc = cterm_of sg c |
1642 |
val a = case concl_of th1 of |
|
1643 |
_ $ (Const("Ex",_) $ a) => a |
|
1644 |
| _ => raise ERR "CHOOSE" "Conclusion not existential" |
|
1645 |
val ca = cterm_of (sign_of_thm th1) a |
|
1646 |
val choose_thm' = beta_eta_thm (Drule.instantiate' [Some cvty] [Some ca,Some cc] choose_thm) |
|
1647 |
val th21 = rearrange sg (HOLogic.mk_Trueprop (a $ v')) th2 |
|
1648 |
val th22 = strip ((nprems_of th21)-1) (cprems_of th21) th21 |
|
1649 |
val th23 = beta_eta_thm (forall_intr cv th22) |
|
1650 |
val th11 = implies_elim_all (beta_eta_thm th1) |
|
1651 |
val th' = th23 COMP (th11 COMP choose_thm') |
|
1652 |
val th = implies_intr_hyps th' |
|
1653 |
val res = HOLThm(rens_of info',th) |
|
1654 |
val _ = message "RESULT:" |
|
1655 |
val _ = if_debug pth res |
|
1656 |
in |
|
1657 |
(thy,res) |
|
1658 |
end |
|
1659 |
||
1660 |
fun GEN v hth thy = |
|
1661 |
let |
|
1662 |
val _ = message "GEN:" |
|
1663 |
val _ = if_debug prin v |
|
1664 |
val _ = if_debug pth hth |
|
1665 |
val (info,th) = disamb_thm hth |
|
1666 |
val (info',v') = disamb_term_from info v |
|
1667 |
val res = HOLThm(rens_of info',mk_GEN v' th (sign_of thy)) |
|
1668 |
val _ = message "RESULT:" |
|
1669 |
val _ = if_debug pth res |
|
1670 |
in |
|
1671 |
(thy,res) |
|
1672 |
end |
|
1673 |
||
1674 |
fun SPEC tm hth thy = |
|
1675 |
let |
|
1676 |
val _ = message "SPEC:" |
|
1677 |
val _ = if_debug prin tm |
|
1678 |
val _ = if_debug pth hth |
|
1679 |
val (info,th) = disamb_thm hth |
|
1680 |
val (info',tm') = disamb_term_from info tm |
|
1681 |
val sg = sign_of thy |
|
1682 |
val ctm = Thm.cterm_of sg tm' |
|
1683 |
val cty = Thm.ctyp_of_term ctm |
|
1684 |
val spec' = Drule.instantiate' [Some cty] [None,Some ctm] spec_thm |
|
1685 |
val th = th RS spec' |
|
1686 |
val res = HOLThm(rens_of info',th) |
|
1687 |
val _ = message "RESULT:" |
|
1688 |
val _ = if_debug pth res |
|
1689 |
in |
|
1690 |
(thy,res) |
|
1691 |
end |
|
1692 |
||
1693 |
fun COMB hth1 hth2 thy = |
|
1694 |
let |
|
1695 |
val _ = message "COMB:" |
|
1696 |
val _ = if_debug pth hth1 |
|
1697 |
val _ = if_debug pth hth2 |
|
1698 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1699 |
val sg = sign_of thy |
|
1700 |
val res = HOLThm(rens_of info,mk_COMB th1 th2 sg) |
|
1701 |
val _ = message "RESULT:" |
|
1702 |
val _ = if_debug pth res |
|
1703 |
in |
|
1704 |
(thy,res) |
|
1705 |
end |
|
1706 |
||
1707 |
fun TRANS hth1 hth2 thy = |
|
1708 |
let |
|
1709 |
val _ = message "TRANS:" |
|
1710 |
val _ = if_debug pth hth1 |
|
1711 |
val _ = if_debug pth hth2 |
|
1712 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2] |
|
1713 |
val th = [th1,th2] MRS trans_thm |
|
1714 |
val res = HOLThm(rens_of info,th) |
|
1715 |
val _ = message "RESULT:" |
|
1716 |
val _ = if_debug pth res |
|
1717 |
in |
|
1718 |
(thy,res) |
|
1719 |
end |
|
1720 |
||
1721 |
||
1722 |
fun CCONTR tm hth thy = |
|
1723 |
let |
|
1724 |
val _ = message "SPEC:" |
|
1725 |
val _ = if_debug prin tm |
|
1726 |
val _ = if_debug pth hth |
|
1727 |
val (info,th) = disamb_thm hth |
|
1728 |
val (info',tm') = disamb_term_from info tm |
|
1729 |
val th = norm_hyps th |
|
1730 |
val sg = sign_of thy |
|
1731 |
val ct = cterm_of sg tm' |
|
1732 |
val th1 = rearrange sg (HOLogic.mk_Trueprop (Const("Not",boolT-->boolT) $ tm')) th |
|
1733 |
val ccontr_thm' = Drule.instantiate' [] [Some ct] ccontr_thm |
|
1734 |
val res1 = uniq_compose ((nprems_of th1) - 1) th1 1 ccontr_thm' |
|
1735 |
val res = HOLThm(rens_of info',res1) |
|
1736 |
val _ = message "RESULT:" |
|
1737 |
val _ = if_debug pth res |
|
1738 |
in |
|
1739 |
(thy,res) |
|
1740 |
end |
|
1741 |
||
1742 |
fun mk_ABS v th sg = |
|
1743 |
let |
|
1744 |
val cv = cterm_of sg v |
|
1745 |
val th1 = implies_elim_all (beta_eta_thm th) |
|
1746 |
val (f,g) = case concl_of th1 of |
|
1747 |
_ $ (Const("op =",_) $ f $ g) => (Term.lambda v f,Term.lambda v g) |
|
1748 |
| _ => raise ERR "mk_ABS" "Bad conclusion" |
|
1749 |
val (fd,fr) = dom_rng (type_of f) |
|
1750 |
val abs_thm' = Drule.instantiate' [Some (ctyp_of sg fd), Some (ctyp_of sg fr)] [Some (cterm_of sg f), Some (cterm_of sg g)] abs_thm |
|
1751 |
val th2 = forall_intr cv th1 |
|
1752 |
val th3 = th2 COMP abs_thm' |
|
1753 |
val res = implies_intr_hyps th3 |
|
1754 |
in |
|
1755 |
res |
|
1756 |
end |
|
1757 |
||
1758 |
fun ABS v hth thy = |
|
1759 |
let |
|
1760 |
val _ = message "ABS:" |
|
1761 |
val _ = if_debug prin v |
|
1762 |
val _ = if_debug pth hth |
|
1763 |
val (info,th) = disamb_thm hth |
|
1764 |
val (info',v') = disamb_term_from info v |
|
1765 |
val sg = sign_of thy |
|
1766 |
val res = HOLThm(rens_of info',mk_ABS v' th sg) |
|
1767 |
val _ = message "RESULT:" |
|
1768 |
val _ = if_debug pth res |
|
1769 |
in |
|
1770 |
(thy,res) |
|
1771 |
end |
|
1772 |
||
1773 |
fun GEN_ABS copt vlist hth thy = |
|
1774 |
let |
|
1775 |
val _ = message "GEN_ABS:" |
|
1776 |
val _ = case copt of |
|
1777 |
Some c => if_debug prin c |
|
1778 |
| None => () |
|
1779 |
val _ = if_debug (app prin) vlist |
|
1780 |
val _ = if_debug pth hth |
|
1781 |
val (info,th) = disamb_thm hth |
|
1782 |
val (info',vlist') = disamb_terms_from info vlist |
|
1783 |
val sg = sign_of thy |
|
1784 |
val th1 = |
|
1785 |
case copt of |
|
1786 |
Some (c as Const(cname,cty)) => |
|
1787 |
let |
|
1788 |
fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!" |
|
1789 |
| inst_type ty1 ty2 (ty as TFree _) = if ty1 = ty |
|
1790 |
then ty2 |
|
1791 |
else ty |
|
1792 |
| inst_type ty1 ty2 (ty as Type(name,tys)) = |
|
1793 |
Type(name,map (inst_type ty1 ty2) tys) |
|
1794 |
in |
|
1795 |
foldr (fn (v,th) => |
|
1796 |
let |
|
1797 |
val cdom = fst (dom_rng (fst (dom_rng cty))) |
|
1798 |
val vty = type_of v |
|
1799 |
val newcty = inst_type cdom vty cty |
|
1800 |
val cc = cterm_of sg (Const(cname,newcty)) |
|
1801 |
in |
|
1802 |
mk_COMB (mk_REFL cc) (mk_ABS v th sg) sg |
|
1803 |
end) (vlist',th) |
|
1804 |
end |
|
1805 |
| Some _ => raise ERR "GEN_ABS" "Bad constant" |
|
1806 |
| None => |
|
1807 |
foldr (fn (v,th) => mk_ABS v th sg) (vlist',th) |
|
1808 |
val res = HOLThm(rens_of info',th1) |
|
1809 |
val _ = message "RESULT:" |
|
1810 |
val _ = if_debug pth res |
|
1811 |
in |
|
1812 |
(thy,res) |
|
1813 |
end |
|
1814 |
||
1815 |
fun NOT_INTRO (hth as HOLThm(rens,th)) thy = |
|
1816 |
let |
|
1817 |
val _ = message "NOT_INTRO:" |
|
1818 |
val _ = if_debug pth hth |
|
1819 |
val sg = sign_of thy |
|
1820 |
val th1 = implies_elim_all (beta_eta_thm th) |
|
1821 |
val a = case concl_of th1 of |
|
1822 |
_ $ (Const("op -->",_) $ a $ Const("False",_)) => a |
|
1823 |
| _ => raise ERR "NOT_INTRO" "Conclusion of bad form" |
|
1824 |
val ca = cterm_of sg a |
|
1825 |
val th2 = equal_elim (Drule.instantiate' [] [Some ca] not_intro_thm) th1 |
|
1826 |
val res = HOLThm(rens,implies_intr_hyps th2) |
|
1827 |
val _ = message "RESULT:" |
|
1828 |
val _ = if_debug pth res |
|
1829 |
in |
|
1830 |
(thy,res) |
|
1831 |
end |
|
1832 |
||
1833 |
fun NOT_ELIM (hth as HOLThm(rens,th)) thy = |
|
1834 |
let |
|
1835 |
val _ = message "NOT_INTRO:" |
|
1836 |
val _ = if_debug pth hth |
|
1837 |
val sg = sign_of thy |
|
1838 |
val th1 = implies_elim_all (beta_eta_thm th) |
|
1839 |
val a = case concl_of th1 of |
|
1840 |
_ $ (Const("Not",_) $ a) => a |
|
1841 |
| _ => raise ERR "NOT_ELIM" "Conclusion of bad form" |
|
1842 |
val ca = cterm_of sg a |
|
1843 |
val th2 = equal_elim (Drule.instantiate' [] [Some ca] not_elim_thm) th1 |
|
1844 |
val res = HOLThm(rens,implies_intr_hyps th2) |
|
1845 |
val _ = message "RESULT:" |
|
1846 |
val _ = if_debug pth res |
|
1847 |
in |
|
1848 |
(thy,res) |
|
1849 |
end |
|
1850 |
||
1851 |
fun DISCH tm hth thy = |
|
1852 |
let |
|
1853 |
val _ = message "DISCH:" |
|
1854 |
val _ = if_debug prin tm |
|
1855 |
val _ = if_debug pth hth |
|
1856 |
val (info,th) = disamb_thm hth |
|
1857 |
val (info',tm') = disamb_term_from info tm |
|
1858 |
val prems = prems_of th |
|
1859 |
val sg = sign_of thy |
|
1860 |
val th1 = beta_eta_thm th |
|
1861 |
val th2 = implies_elim_all th1 |
|
1862 |
val th3 = implies_intr (cterm_of sg (HOLogic.mk_Trueprop tm')) th2 |
|
1863 |
val th4 = th3 COMP disch_thm |
|
1864 |
val res = HOLThm(rens_of info',implies_intr_hyps th4) |
|
1865 |
val _ = message "RESULT:" |
|
1866 |
val _ = if_debug pth res |
|
1867 |
in |
|
1868 |
(thy,res) |
|
1869 |
end |
|
1870 |
||
1871 |
val spaces = String.concat o separate " " |
|
1872 |
||
1873 |
fun new_definition thyname constname rhs thy = |
|
1874 |
let |
|
1875 |
val constname = rename_const thyname thy constname |
|
1876 |
val _ = warning ("Introducing constant " ^ constname) |
|
1877 |
val (thmname,thy) = get_defname thyname constname thy |
|
1878 |
val (info,rhs') = disamb_term rhs |
|
1879 |
val ctype = type_of rhs' |
|
1880 |
val csyn = mk_syn constname |
|
1881 |
val thy1 = case HOL4DefThy.get thy of |
|
1882 |
Replaying _ => thy |
|
1883 |
| _ => Theory.add_consts_i [(constname,ctype,csyn)] thy |
|
1884 |
val eq = mk_defeq constname rhs' thy1 |
|
1885 |
val (thy2,thms) = PureThy.add_defs_i false [((thmname,eq),[])] thy1 |
|
1886 |
val def_thm = hd thms |
|
1887 |
val thm' = def_thm RS meta_eq_to_obj_eq_thm |
|
1888 |
val (thy',th) = (thy2, thm') |
|
1889 |
val fullcname = Sign.intern_const (sign_of thy') constname |
|
1890 |
val thy'' = add_hol4_const_mapping thyname constname true fullcname thy' |
|
1891 |
val (linfo,tm24) = disamb_term (mk_teq constname rhs' thy'') |
|
1892 |
val sg = sign_of thy'' |
|
1893 |
val rew = rewrite_hol4_term eq thy'' |
|
1894 |
val crhs = cterm_of sg (#2 (Logic.dest_equals (prop_of rew))) |
|
1895 |
val thy22 = if (def_name constname) = thmname |
|
1896 |
then |
|
1897 |
add_dump ("constdefs\n " ^ (quotename constname) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg ctype)) ^ "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n " ^ (smart_string_of_cterm crhs)) thy'' |
|
1898 |
else |
|
1899 |
add_dump ("consts\n " ^ (quotename constname) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg ctype)) ^ |
|
1900 |
"\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n\ndefs\n " ^ (quotename thmname) ^ ": " ^ (smart_string_of_cterm crhs)) |
|
1901 |
thy'' |
|
1902 |
||
1903 |
val hth = case Shuffler.set_prop thy22 (HOLogic.mk_Trueprop tm24) [("",th)] of |
|
1904 |
Some (_,res) => HOLThm(rens_of linfo,res) |
|
1905 |
| None => raise ERR "new_definition" "Bad conclusion" |
|
1906 |
val fullname = Sign.full_name sg thmname |
|
1907 |
val thy22' = case opt_get_output_thy thy22 of |
|
1908 |
"" => add_hol4_mapping thyname thmname fullname thy22 |
|
1909 |
| output_thy => |
|
1910 |
let |
|
1911 |
val moved_thmname = output_thy ^ "." ^ thyname ^ "." ^ thmname |
|
1912 |
in |
|
1913 |
thy22 |> add_hol4_move fullname moved_thmname |
|
1914 |
|> add_hol4_mapping thyname thmname moved_thmname |
|
1915 |
end |
|
1916 |
val _ = message "new_definition:" |
|
1917 |
val _ = if_debug pth hth |
|
1918 |
in |
|
1919 |
(thy22',hth) |
|
1920 |
end |
|
1921 |
handle e => (message "exception in new_definition"; print_exn e) |
|
1922 |
||
1923 |
val commafy = String.concat o separate ", " |
|
1924 |
||
1925 |
local |
|
1926 |
val helper = thm "termspec_help" |
|
1927 |
in |
|
1928 |
fun new_specification thyname thmname names hth thy = |
|
1929 |
case HOL4DefThy.get thy of |
|
1930 |
Replaying _ => (thy,hth) |
|
1931 |
| _ => |
|
1932 |
let |
|
1933 |
val _ = message "NEW_SPEC:" |
|
1934 |
val _ = if_debug pth hth |
|
1935 |
val names = map (rename_const thyname thy) names |
|
1936 |
val _ = warning ("Introducing constants " ^ (commafy names)) |
|
1937 |
val (HOLThm(rens,th)) = norm_hthm (sign_of thy) hth |
|
1938 |
val thy1 = case HOL4DefThy.get thy of |
|
1939 |
Replaying _ => thy |
|
1940 |
| _ => |
|
1941 |
let |
|
1942 |
fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body) |
|
1943 |
| dest_eta_abs body = |
|
1944 |
let |
|
1945 |
val (dT,rT) = dom_rng (type_of body) |
|
1946 |
in |
|
1947 |
("x",dT,body $ Bound 0) |
|
1948 |
end |
|
1949 |
handle TYPE _ => raise ERR "new_specification" "not an abstraction type" |
|
1950 |
fun dest_exists (Const("Ex",_) $ abody) = |
|
1951 |
dest_eta_abs abody |
|
1952 |
| dest_exists tm = |
|
1953 |
raise ERR "new_specification" "Bad existential formula" |
|
1954 |
||
1955 |
val (consts,_) = foldl (fn ((cs,ex),cname) => |
|
1956 |
let |
|
1957 |
val (_,cT,p) = dest_exists ex |
|
1958 |
in |
|
1959 |
((cname,cT,mk_syn cname)::cs,p) |
|
1960 |
end) (([],HOLogic.dest_Trueprop (concl_of th)),names) |
|
1961 |
val sg = sign_of thy |
|
1962 |
val str = foldl (fn (acc,(c,T,csyn)) => |
|
1963 |
acc ^ "\n " ^ (quotename c) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg T)) ^ "\" " ^ (Syntax.string_of_mixfix csyn)) ("consts",consts) |
|
1964 |
val thy' = add_dump str thy |
|
1965 |
in |
|
1966 |
Theory.add_consts_i consts thy' |
|
1967 |
end |
|
1968 |
||
1969 |
val thy1 = foldr (fn(name,thy)=> |
|
1970 |
snd (get_defname thyname name thy)) (names,thy1) |
|
1971 |
fun new_name name = fst (get_defname thyname name thy1) |
|
1972 |
val (thy',res) = SpecificationPackage.add_specification_i None |
|
1973 |
(map (fn name => (new_name name,name,false)) names) |
|
1974 |
(thy1,th) |
|
1975 |
val res' = Drule.freeze_all res |
|
1976 |
val hth = HOLThm(rens,res') |
|
1977 |
val rew = rewrite_hol4_term (concl_of res') thy' |
|
1978 |
val th = equal_elim rew res' |
|
1979 |
fun handle_const (name,thy) = |
|
1980 |
let |
|
1981 |
val defname = def_name name |
|
1982 |
val (newname,thy') = get_defname thyname name thy |
|
1983 |
in |
|
1984 |
(if defname = newname |
|
1985 |
then quotename name |
|
1986 |
else (quotename newname) ^ ": " ^ (quotename name),thy') |
|
1987 |
end |
|
1988 |
val (new_names,thy') = foldr (fn(name,(names,thy)) => |
|
1989 |
let |
|
1990 |
val (name',thy') = handle_const (name,thy) |
|
1991 |
in |
|
1992 |
(name'::names,thy') |
|
1993 |
end) (names,([],thy')) |
|
1994 |
val thy'' = add_dump ("specification (" ^ (spaces new_names) ^ ") " ^ thmname ^ ": " ^ (smart_string_of_thm th) ^ |
|
1995 |
"\n by (import " ^ thyname ^ " " ^ thmname ^ ")") |
|
1996 |
thy' |
|
1997 |
val _ = message "RESULT:" |
|
1998 |
val _ = if_debug pth hth |
|
1999 |
in |
|
2000 |
intern_store_thm false thyname thmname hth thy'' |
|
2001 |
end |
|
2002 |
handle e => (message "exception in new_specification"; print_exn e) |
|
2003 |
||
2004 |
end |
|
2005 |
||
2006 |
fun new_axiom name tm thy = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")") |
|
2007 |
||
2008 |
fun to_isa_thm (hth as HOLThm(_,th)) = |
|
2009 |
let |
|
2010 |
val (HOLThm args) = norm_hthm (sign_of_thm th) hth |
|
2011 |
in |
|
2012 |
apsnd strip_shyps args |
|
2013 |
end |
|
2014 |
||
2015 |
fun to_isa_term tm = tm |
|
2016 |
||
2017 |
local |
|
2018 |
val light_nonempty = thm "light_ex_imp_nonempty" |
|
2019 |
val ex_imp_nonempty = thm "ex_imp_nonempty" |
|
2020 |
val typedef_hol2hol4 = thm "typedef_hol2hol4" |
|
2021 |
val typedef_hol2hollight = thm "typedef_hol2hollight" |
|
2022 |
in |
|
2023 |
fun new_type_definition thyname thmname tycname hth thy = |
|
2024 |
case HOL4DefThy.get thy of |
|
2025 |
Replaying _ => (thy,hth) |
|
2026 |
| _ => |
|
2027 |
let |
|
2028 |
val _ = message "TYPE_DEF:" |
|
2029 |
val _ = if_debug pth hth |
|
2030 |
val _ = warning ("Introducing type " ^ tycname) |
|
2031 |
val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth |
|
2032 |
val th2 = beta_eta_thm (td_th RS ex_imp_nonempty) |
|
2033 |
val c = case concl_of th2 of |
|
2034 |
_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c |
|
2035 |
| _ => raise ERR "new_type_definition" "Bad type definition theorem" |
|
2036 |
val tfrees = term_tfrees c |
|
2037 |
val tnames = map fst tfrees |
|
2038 |
val tsyn = mk_syn tycname |
|
2039 |
val typ = (tycname,tnames,tsyn) |
|
2040 |
val (thy',typedef_info) = TypedefPackage.add_typedef_i false (Some thmname) typ c None (rtac th2 1) thy |
|
2041 |
||
2042 |
val th3 = (#type_definition typedef_info) RS typedef_hol2hol4 |
|
2043 |
||
2044 |
val fulltyname = Sign.intern_tycon (sign_of thy') tycname |
|
2045 |
val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy' |
|
2046 |
||
2047 |
val sg = sign_of thy'' |
|
2048 |
val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th3)) |
|
2049 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating") |
|
2050 |
else () |
|
2051 |
val thy4 = add_hol4_pending thyname thmname args thy'' |
|
2052 |
||
2053 |
val sg = sign_of thy4 |
|
2054 |
val rew = rewrite_hol4_term (concl_of td_th) thy4 |
|
2055 |
val th = equal_elim rew (transfer_sg sg td_th) |
|
2056 |
val c = case HOLogic.dest_Trueprop (prop_of th) of |
|
2057 |
Const("Ex",exT) $ P => |
|
2058 |
let |
|
2059 |
val PT = domain_type exT |
|
2060 |
in |
|
2061 |
Const("Collect",PT-->HOLogic.mk_setT (domain_type PT)) $ P |
|
2062 |
end |
|
2063 |
| _ => error "Internal error in ProofKernel.new_typedefinition" |
|
2064 |
val tnames_string = if null tnames |
|
2065 |
then "" |
|
2066 |
else "(" ^ (commafy tnames) ^ ") " |
|
2067 |
val proc_prop = if null tnames |
|
2068 |
then smart_string_of_cterm |
|
2069 |
else Library.setmp show_all_types true smart_string_of_cterm |
|
2070 |
val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of sg c)) ^ " " ^ (Syntax.string_of_mixfix tsyn) ^ "\n by (rule typedef_helper,import " ^ thyname ^ " " ^ thmname ^ ")") thy4 |
|
2071 |
val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hol4 [OF type_definition_" ^ tycname ^ "]") |
|
2072 |
thy5 |
|
2073 |
val _ = message "RESULT:" |
|
2074 |
val _ = if_debug pth hth' |
|
2075 |
in |
|
2076 |
(thy6,hth') |
|
2077 |
end |
|
2078 |
handle e => (message "exception in new_type_definition"; print_exn e) |
|
2079 |
||
2080 |
fun type_introduction thyname thmname tycname abs_name rep_name (P,t) hth thy = |
|
2081 |
case HOL4DefThy.get thy of |
|
2082 |
Replaying _ => (thy,hth) |
|
2083 |
| _ => |
|
2084 |
let |
|
2085 |
val _ = message "TYPE_INTRO:" |
|
2086 |
val _ = if_debug pth hth |
|
2087 |
val _ = warning ("Introducing type " ^ tycname ^ " (with morphisms " ^ abs_name ^ " and " ^ rep_name ^ ")") |
|
2088 |
val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth |
|
2089 |
val sg = sign_of thy |
|
2090 |
val tT = type_of t |
|
2091 |
val light_nonempty' = |
|
2092 |
Drule.instantiate' [Some (ctyp_of sg tT)] |
|
2093 |
[Some (cterm_of sg P), |
|
2094 |
Some (cterm_of sg t)] light_nonempty |
|
2095 |
val th2 = beta_eta_thm (td_th RS (beta_eta_thm light_nonempty')) |
|
2096 |
val c = case concl_of th2 of |
|
2097 |
_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c |
|
2098 |
| _ => raise ERR "type_introduction" "Bad type definition theorem" |
|
2099 |
val tfrees = term_tfrees c |
|
2100 |
val tnames = map fst tfrees |
|
2101 |
val tsyn = mk_syn tycname |
|
2102 |
val typ = (tycname,tnames,tsyn) |
|
2103 |
val (thy',typedef_info) = TypedefPackage.add_typedef_i false (Some thmname) typ c (Some(rep_name,abs_name)) (rtac th2 1) thy |
|
2104 |
||
2105 |
val th3 = (#type_definition typedef_info) RS typedef_hol2hollight |
|
2106 |
||
2107 |
val th4 = Drule.freeze_all th3 |
|
2108 |
val fulltyname = Sign.intern_tycon (sign_of thy') tycname |
|
2109 |
val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy' |
|
2110 |
||
2111 |
val sg = sign_of thy'' |
|
2112 |
val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th4)) |
|
2113 |
val _ = if #maxidx (rep_thm th4) <> ~1 |
|
2114 |
then (Library.setmp show_types true pth hth' ; error "SCHEME!") |
|
2115 |
else () |
|
2116 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating") |
|
2117 |
else () |
|
2118 |
val thy4 = add_hol4_pending thyname thmname args thy'' |
|
2119 |
||
2120 |
val sg = sign_of thy4 |
|
2121 |
val P' = #2 (Logic.dest_equals (concl_of (rewrite_hol4_term P thy4))) |
|
2122 |
val c = |
|
2123 |
let |
|
2124 |
val PT = type_of P' |
|
2125 |
in |
|
2126 |
Const("Collect",PT-->HOLogic.mk_setT (domain_type PT)) $ P' |
|
2127 |
end |
|
2128 |
||
2129 |
val tnames_string = if null tnames |
|
2130 |
then "" |
|
2131 |
else "(" ^ (commafy tnames) ^ ") " |
|
2132 |
val proc_prop = if null tnames |
|
2133 |
then smart_string_of_cterm |
|
2134 |
else Library.setmp show_all_types true smart_string_of_cterm |
|
2135 |
val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of sg c)) ^ " " ^ (Syntax.string_of_mixfix tsyn) ^ "\n by (rule light_ex_imp_nonempty,import " ^ thyname ^ " " ^ thmname ^ ")") thy4 |
|
2136 |
val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hollight [OF type_definition_" ^ tycname ^ "]") |
|
2137 |
thy5 |
|
2138 |
val _ = message "RESULT:" |
|
2139 |
val _ = if_debug pth hth' |
|
2140 |
in |
|
2141 |
(thy6,hth') |
|
2142 |
end |
|
2143 |
handle e => (message "exception in type_introduction"; print_exn e) |
|
2144 |
end |
|
2145 |
||
2146 |
end |