src/HOL/Import/replay.ML
author obua
Mon Sep 12 15:52:00 2005 +0200 (2005-09-12)
changeset 17322 781abf7011e6
parent 15574 b1d1b5bfc464
child 17440 df77edc4f5d0
permissions -rw-r--r--
Added HOLLight support to importer.
skalberg@14620
     1
(*  Title:      HOL/Import/replay.ML
skalberg@14620
     2
    ID:         $Id$
skalberg@14620
     3
    Author:     Sebastian Skalberg (TU Muenchen)
skalberg@14620
     4
*)
skalberg@14620
     5
skalberg@14516
     6
structure Replay =
skalberg@14516
     7
struct
skalberg@14516
     8
skalberg@14516
     9
structure P = ProofKernel
skalberg@14516
    10
skalberg@14516
    11
open ProofKernel
skalberg@14516
    12
skalberg@14516
    13
exception REPLAY of string * string
skalberg@14516
    14
fun ERR f mesg = REPLAY (f,mesg)
skalberg@14516
    15
fun NY f = raise ERR f "NOT YET!"
skalberg@14516
    16
skalberg@14516
    17
fun replay_proof int_thms thyname thmname prf thy =
skalberg@14516
    18
    let
skalberg@14516
    19
	fun rp (PRefl tm) thy = P.REFL tm thy
skalberg@14516
    20
	  | rp (PInstT(p,lambda)) thy =
skalberg@14516
    21
	    let
skalberg@14516
    22
		val (thy',th) = rp' p thy
skalberg@14516
    23
	    in
skalberg@14516
    24
		P.INST_TYPE lambda th thy'
skalberg@14516
    25
	    end
skalberg@14516
    26
	  | rp (PSubst(prfs,ctxt,prf)) thy =
skalberg@14516
    27
	    let
skalberg@15574
    28
		val (thy',ths) = foldr (fn (p,(thy,ths)) =>
skalberg@14516
    29
					   let
skalberg@14516
    30
					       val (thy',th) = rp' p thy
skalberg@14516
    31
					   in
skalberg@14516
    32
					       (thy',th::ths)
skalberg@15574
    33
					   end) (thy,[]) prfs
skalberg@14516
    34
		val (thy'',th) = rp' prf thy'
skalberg@14516
    35
	    in
skalberg@14516
    36
		P.SUBST ths ctxt th thy''
skalberg@14516
    37
	    end
skalberg@14516
    38
	  | rp (PAbs(prf,v)) thy =
skalberg@14516
    39
	    let
skalberg@14516
    40
		val (thy',th) = rp' prf thy
skalberg@14516
    41
	    in
skalberg@14516
    42
		P.ABS v th thy'
skalberg@14516
    43
	    end
skalberg@14516
    44
	  | rp (PDisch(prf,tm)) thy =
skalberg@14516
    45
	    let
skalberg@14516
    46
		val (thy',th) = rp' prf thy
skalberg@14516
    47
	    in
skalberg@14516
    48
		P.DISCH tm th thy'
skalberg@14516
    49
	    end
skalberg@14516
    50
	  | rp (PMp(prf1,prf2)) thy =
skalberg@14516
    51
	    let
skalberg@14516
    52
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
    53
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
    54
	    in
skalberg@14516
    55
		P.MP th1 th2 thy2
skalberg@14516
    56
	    end
skalberg@14516
    57
	  | rp (PHyp tm) thy = P.ASSUME tm thy
skalberg@14516
    58
	  | rp (PDef(seg,name,rhs)) thy =
skalberg@14516
    59
	    (case P.get_def seg name rhs thy of
skalberg@15531
    60
		 (thy',SOME res) => (thy',res)
skalberg@15531
    61
	       | (thy',NONE) => 
skalberg@14516
    62
		 if seg = thyname
skalberg@14516
    63
		 then P.new_definition seg name rhs thy'
obua@17322
    64
		 else raise ERR "replay_proof" ("Too late for term definition: "^seg^" != "^thyname))
skalberg@14516
    65
	  | rp (POracle(tg,asl,c)) thy = (*P.mk_oracle_thm tg (map be_contract asl,c)*) NY "ORACLE"
skalberg@14516
    66
	  | rp (PSpec(prf,tm)) thy =
skalberg@14516
    67
	    let
skalberg@14516
    68
		val (thy',th) = rp' prf thy
skalberg@14516
    69
	    in
skalberg@14516
    70
		P.SPEC tm th thy'
skalberg@14516
    71
	    end
skalberg@14516
    72
	  | rp (PInst(prf,theta)) thy =
skalberg@14516
    73
	    let
skalberg@14516
    74
		val (thy',th) = rp' prf thy
skalberg@14516
    75
	    in
skalberg@14516
    76
		P.INST theta th thy'
skalberg@14516
    77
	    end
skalberg@14516
    78
	  | rp (PGen(prf,v)) thy =
skalberg@14516
    79
	    let
skalberg@14516
    80
		val (thy',th) = rp' prf thy
skalberg@14516
    81
	    in
skalberg@14516
    82
		P.GEN v th thy'
skalberg@14516
    83
	    end
skalberg@14516
    84
	  | rp (PGenAbs(prf,opt,vl)) thy =
skalberg@14516
    85
	    let
skalberg@14516
    86
		val (thy',th) = rp' prf thy
skalberg@14516
    87
	    in
skalberg@14516
    88
		P.GEN_ABS opt vl th thy'
skalberg@14516
    89
	    end
skalberg@14516
    90
	  | rp (PImpAS(prf1,prf2)) thy =
skalberg@14516
    91
	    let
skalberg@14516
    92
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
    93
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
    94
	    in
skalberg@14516
    95
		P.IMP_ANTISYM th1 th2 thy2
skalberg@14516
    96
	    end
skalberg@14516
    97
	  | rp (PSym prf) thy =
skalberg@14516
    98
	    let
skalberg@14516
    99
		val (thy1,th) = rp' prf thy
skalberg@14516
   100
	    in
skalberg@14516
   101
		P.SYM th thy1
skalberg@14516
   102
	    end
skalberg@14516
   103
	  | rp (PTrans(prf1,prf2)) thy =
skalberg@14516
   104
	    let
skalberg@14516
   105
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
   106
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   107
	    in
skalberg@14516
   108
		P.TRANS th1 th2 thy2
skalberg@14516
   109
	    end
skalberg@14516
   110
	  | rp (PComb(prf1,prf2)) thy =
skalberg@14516
   111
	    let
skalberg@14516
   112
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
   113
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   114
	    in
skalberg@14516
   115
		P.COMB th1 th2 thy2
skalberg@14516
   116
	    end
skalberg@14516
   117
	  | rp (PEqMp(prf1,prf2)) thy =
skalberg@14516
   118
	    let
skalberg@14516
   119
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
   120
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   121
	    in
skalberg@14516
   122
		P.EQ_MP th1 th2 thy2
skalberg@14516
   123
	    end
skalberg@14516
   124
	  | rp (PEqImp prf) thy =
skalberg@14516
   125
	    let
skalberg@14516
   126
		val (thy',th) = rp' prf thy
skalberg@14516
   127
	    in
skalberg@14516
   128
		P.EQ_IMP_RULE th thy'
skalberg@14516
   129
	    end
skalberg@14516
   130
	  | rp (PExists(prf,ex,wit)) thy =
skalberg@14516
   131
	    let
skalberg@14516
   132
		val (thy',th) = rp' prf thy
skalberg@14516
   133
	    in
skalberg@14516
   134
		P.EXISTS ex wit th thy'
skalberg@14516
   135
	    end
skalberg@14516
   136
	  | rp (PChoose(v,prf1,prf2)) thy =
skalberg@14516
   137
	    let
skalberg@14516
   138
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
   139
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   140
	    in
skalberg@14516
   141
		P.CHOOSE v th1 th2 thy2
skalberg@14516
   142
	    end
skalberg@14516
   143
	  | rp (PConj(prf1,prf2)) thy =
skalberg@14516
   144
	    let
skalberg@14516
   145
		val (thy1,th1) = rp' prf1 thy
skalberg@14516
   146
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   147
	    in
skalberg@14516
   148
		P.CONJ th1 th2 thy2
skalberg@14516
   149
	    end
skalberg@14516
   150
	  | rp (PConjunct1 prf) thy =
skalberg@14516
   151
	    let
skalberg@14516
   152
		val (thy',th) = rp' prf thy
skalberg@14516
   153
	    in
skalberg@14516
   154
		P.CONJUNCT1 th thy'
skalberg@14516
   155
	    end
skalberg@14516
   156
	  | rp (PConjunct2 prf) thy =
skalberg@14516
   157
	    let
skalberg@14516
   158
		val (thy',th) = rp' prf thy
skalberg@14516
   159
	    in
skalberg@14516
   160
		P.CONJUNCT2 th thy'
skalberg@14516
   161
	    end
skalberg@14516
   162
	  | rp (PDisj1(prf,tm)) thy =
skalberg@14516
   163
	    let
skalberg@14516
   164
		val (thy',th) = rp' prf thy
skalberg@14516
   165
	    in
skalberg@14516
   166
		P.DISJ1 th tm thy'
skalberg@14516
   167
	    end
skalberg@14516
   168
	  | rp (PDisj2(prf,tm)) thy =
skalberg@14516
   169
	    let
skalberg@14516
   170
		val (thy',th) = rp' prf thy
skalberg@14516
   171
	    in
skalberg@14516
   172
		P.DISJ2 tm th thy'
skalberg@14516
   173
	    end
skalberg@14516
   174
	  | rp (PDisjCases(prf,prf1,prf2)) thy =
skalberg@14516
   175
	    let
skalberg@14516
   176
		val (thy',th)  = rp' prf  thy
skalberg@14516
   177
		val (thy1,th1) = rp' prf1 thy'
skalberg@14516
   178
		val (thy2,th2) = rp' prf2 thy1
skalberg@14516
   179
	    in
skalberg@14516
   180
		P.DISJ_CASES th th1 th2 thy2
skalberg@14516
   181
	    end
skalberg@14516
   182
	  | rp (PNotI prf) thy =
skalberg@14516
   183
	    let
skalberg@14516
   184
		val (thy',th) = rp' prf thy
skalberg@14516
   185
	    in
skalberg@14516
   186
		P.NOT_INTRO th thy'
skalberg@14516
   187
	    end
skalberg@14516
   188
	  | rp (PNotE prf) thy =
skalberg@14516
   189
	    let
skalberg@14516
   190
		val (thy',th) = rp' prf thy
skalberg@14516
   191
	    in
skalberg@14516
   192
		P.NOT_ELIM th thy'
skalberg@14516
   193
	    end
skalberg@14516
   194
	  | rp (PContr(prf,tm)) thy =
skalberg@14516
   195
	    let
skalberg@14516
   196
		val (thy',th) = rp' prf thy
skalberg@14516
   197
	    in
skalberg@14516
   198
		P.CCONTR tm th thy'
skalberg@14516
   199
	    end
skalberg@14516
   200
	  | rp (PTmSpec _) _ = raise ERR "rp" "Shouldn't reach here (PTmSpec)"
skalberg@14516
   201
	  | rp (PTyDef _) _ = raise ERR "rp" "Shouldn't reach here (PTyDef)"
skalberg@14516
   202
	  | rp (PTyIntro _) _ = raise ERR "rp" "Shouldn't reach here (PTyIntro)"
skalberg@14516
   203
	  | rp PDisk _ = raise ERR "rp" "Shouldn't reach here (PDisk)"
skalberg@14516
   204
	  | rp _ _ = raise ERR "rp" "What the hell is this? Which case did I forget?"
skalberg@14516
   205
	and rp' p thy =
skalberg@14516
   206
	    let
skalberg@14516
   207
		val pc = content_of p
skalberg@14516
   208
	    in
skalberg@14516
   209
		case pc of
skalberg@14516
   210
		    PDisk => (case disk_info_of p of
skalberg@15531
   211
				  SOME(thyname',thmname) =>
skalberg@14516
   212
				  (case Int.fromString thmname of
skalberg@14516
   213
				       SOME i =>
skalberg@14516
   214
				       if thyname' = thyname
skalberg@14516
   215
				       then
skalberg@14516
   216
					   (case Array.sub(int_thms,i-1) of
skalberg@15531
   217
						NONE =>
skalberg@14516
   218
						let
skalberg@14516
   219
						    val (thy',th) = rp' (snd (import_proof thyname' thmname thy) thy) thy
skalberg@15531
   220
						    val _ = Array.update(int_thms,i-1,SOME th)
skalberg@14516
   221
						in
skalberg@14516
   222
						    (thy',th)
skalberg@14516
   223
						end
skalberg@15531
   224
					      | SOME th => (thy,th))
skalberg@14516
   225
				       else raise ERR "replay_proof" ("Library " ^ thyname' ^ " should be built before " ^ thyname ^ " (" ^ thmname ^ ")")
skalberg@14516
   226
				     | NONE => 
skalberg@14516
   227
				       (case P.get_thm thyname' thmname thy of
skalberg@15531
   228
					    (thy',SOME res) => (thy',res)
skalberg@15531
   229
					  | (thy',NONE) => 
skalberg@14516
   230
					    if thyname' = thyname
skalberg@14516
   231
					    then
skalberg@14516
   232
						let
skalberg@14516
   233
						    val _ = writeln ("Found no " ^ thmname ^ " theorem, replaying...")
skalberg@14516
   234
						    val (f_opt,prf) = import_proof thyname' thmname thy'
skalberg@14516
   235
						    val prf = prf thy'
skalberg@14516
   236
						    val (thy',th) = replay_proof int_thms thyname' thmname prf thy'
obua@17322
   237
                                                    val _ = writeln ("Successfully finished replaying "^thmname^" !")
skalberg@14516
   238
						in
skalberg@14516
   239
						    case content_of prf of
skalberg@14516
   240
							PTmSpec _ => (thy',th)
skalberg@14516
   241
						      | PTyDef  _ => (thy',th)
skalberg@14516
   242
						      | PTyIntro _ => (thy',th)
skalberg@14516
   243
						      | _ => P.store_thm thyname' thmname th thy'
skalberg@14516
   244
						end
skalberg@14516
   245
					    else raise ERR "replay_proof" ("Library " ^ thyname' ^ " should be built before " ^ thyname ^ " (" ^ thmname ^ ")")))
skalberg@15531
   246
				| NONE => raise ERR "rp'.PDisk" "Not enough information")
skalberg@14516
   247
		  | PAxm(name,c) =>
skalberg@14516
   248
		    (case P.get_axiom thyname name thy of
skalberg@15531
   249
			    (thy',SOME res) => (thy',res)
skalberg@15531
   250
			  | (thy',NONE) => P.new_axiom name c thy')
skalberg@14516
   251
		  | PTmSpec(seg,names,prf') =>
skalberg@14516
   252
		    let
skalberg@14516
   253
			val (thy',th) = rp' prf' thy
skalberg@14516
   254
		    in
skalberg@14516
   255
			P.new_specification seg thmname names th thy'
skalberg@14516
   256
		    end
skalberg@14516
   257
		  | PTyDef(seg,name,prf') =>
skalberg@14516
   258
		    let
skalberg@14516
   259
			val (thy',th) = rp' prf' thy
skalberg@14516
   260
		    in
skalberg@14516
   261
			P.new_type_definition seg thmname name th thy'
skalberg@14516
   262
		    end
skalberg@14516
   263
		  | PTyIntro(seg,name,abs_name,rep_name,P,t,prf') =>
skalberg@14516
   264
		    let
skalberg@14516
   265
			val (thy',th) = rp' prf' thy
skalberg@14516
   266
		    in
skalberg@14516
   267
			P.type_introduction seg thmname name abs_name rep_name (P,t) th thy'
skalberg@14516
   268
		    end
skalberg@14516
   269
		  | _ => rp pc thy
skalberg@14516
   270
	    end
skalberg@14516
   271
    in
skalberg@14516
   272
	rp' prf thy handle e => (writeln "Exception in replay_proof"; print_exn e)
skalberg@14516
   273
    end
skalberg@14516
   274
skalberg@14516
   275
fun setup_int_thms thyname thy =
skalberg@14516
   276
    let
skalberg@14620
   277
	val fname =
skalberg@14620
   278
	    case P.get_proof_dir thyname thy of
skalberg@15531
   279
		SOME p => OS.Path.joinDirFile {dir=p,file=OS.Path.joinBaseExt{base = "facts",ext=SOME "lst"}}
skalberg@15531
   280
	      | NONE => error "Cannot find proof files"
skalberg@14620
   281
	val is = TextIO.openIn fname
skalberg@14516
   282
	val (num_int_thms,facts) =
skalberg@14516
   283
	    let
skalberg@14516
   284
		fun get_facts facts =
skalberg@14516
   285
		    case TextIO.inputLine is of
skalberg@14516
   286
			"" => (case facts of
skalberg@14516
   287
				   i::facts => (valOf (Int.fromString i),rev facts)
skalberg@14516
   288
				 | _ => raise ERR "replay_thm" "Bad facts.lst file")
skalberg@14516
   289
		      | fact => get_facts ((String.substring(fact,0,String.size fact -1 ))::facts)
skalberg@14516
   290
	    in
skalberg@14516
   291
		get_facts []
skalberg@14516
   292
	    end
skalberg@14516
   293
	val _ = TextIO.closeIn is
skalberg@15531
   294
	val int_thms = Array.array(num_int_thms,NONE:thm option)
skalberg@14516
   295
    in
skalberg@14516
   296
	(int_thms,facts)
skalberg@14516
   297
    end
skalberg@14516
   298
skalberg@14516
   299
fun import_single_thm thyname int_thms thmname thy =
skalberg@14516
   300
    let
skalberg@14516
   301
	fun replay_fact (thmname,thy) =
skalberg@14516
   302
	    let
skalberg@14516
   303
		val _ = writeln ("Replaying " ^ thmname)
skalberg@14516
   304
		val prf = mk_proof PDisk
skalberg@14516
   305
		val _ = set_disk_info_of prf thyname thmname
skalberg@14516
   306
	    in
skalberg@14516
   307
		fst (replay_proof int_thms thyname thmname prf thy)
skalberg@14516
   308
	    end
skalberg@14516
   309
    in
skalberg@14516
   310
	replay_fact (thmname,thy)
skalberg@14516
   311
    end
skalberg@14516
   312
skalberg@14516
   313
fun import_thms thyname int_thms thmnames thy =
skalberg@14516
   314
    let
skalberg@14516
   315
	fun replay_fact (thy,thmname) =
skalberg@14516
   316
	    let
skalberg@14516
   317
		val _ = writeln ("Replaying " ^ thmname)
skalberg@14516
   318
		val prf = mk_proof PDisk
skalberg@14516
   319
		val _ = set_disk_info_of prf thyname thmname
skalberg@14516
   320
	    in
skalberg@14516
   321
		fst (replay_proof int_thms thyname thmname prf thy)
skalberg@14516
   322
	    end
skalberg@15570
   323
	val res_thy = Library.foldl replay_fact (thy,thmnames)
skalberg@14516
   324
    in
skalberg@14516
   325
	res_thy
skalberg@14516
   326
    end
skalberg@14516
   327
skalberg@14516
   328
fun import_thm thyname thmname thy =
skalberg@14516
   329
    let
skalberg@14516
   330
	val int_thms = fst (setup_int_thms thyname thy)
skalberg@14516
   331
	fun replay_fact (thmname,thy) =
skalberg@14516
   332
	    let
skalberg@14516
   333
		val _ = writeln ("Replaying " ^ thmname)
skalberg@14516
   334
		val prf = mk_proof PDisk
skalberg@14516
   335
		val _ = set_disk_info_of prf thyname thmname
skalberg@14516
   336
	    in
skalberg@14516
   337
		fst (replay_proof int_thms thyname thmname prf thy)
skalberg@14516
   338
	    end
skalberg@14516
   339
    in
skalberg@14516
   340
	replay_fact (thmname,thy)
skalberg@14516
   341
    end
skalberg@14516
   342
skalberg@14516
   343
end