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