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