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