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