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