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