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