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