src/HOL/Tools/ATP/recon_transfer_proof.ML
author paulson
Thu Sep 08 13:24:19 2005 +0200 (2005-09-08)
changeset 17312 159783c74f75
parent 17306 5cde710a8a23
child 17315 5bf0e0aacc24
permissions -rw-r--r--
yet more tidying of Isabelle-ATP link
paulson@15789
     1
(*  ID:         $Id$
wenzelm@16259
     2
    Author:     Claire Quigley
wenzelm@16259
     3
    Copyright   2004  University of Cambridge
paulson@15789
     4
*)
paulson@15789
     5
paulson@15684
     6
structure Recon_Transfer =
paulson@15684
     7
struct
wenzelm@16803
     8
paulson@15684
     9
open Recon_Parse
wenzelm@16803
    10
paulson@15684
    11
infixr 8 ++; infixr 7 >>; infixr 6 ||;
paulson@15684
    12
quigley@15642
    13
quigley@15642
    14
(* Versions that include type information *)
quigley@15642
    15
 
wenzelm@16803
    16
(* FIXME rename to str_of_thm *)
paulson@16157
    17
fun string_of_thm thm =
wenzelm@16803
    18
  setmp show_sorts true (Pretty.str_of o Display.pretty_thm) thm;
quigley@15642
    19
quigley@15642
    20
quigley@15642
    21
(* check separate args in the watcher program for separating strings with a * or ; or something *)
quigley@15642
    22
quigley@15642
    23
fun clause_strs_to_string [] str = str
quigley@15642
    24
|   clause_strs_to_string (x::xs) str = clause_strs_to_string xs (str^x^"%")
quigley@15642
    25
quigley@15642
    26
fun thmvars_to_string [] str = str
quigley@15642
    27
|   thmvars_to_string (x::xs) str = thmvars_to_string xs (str^x^"%")
quigley@15642
    28
quigley@15642
    29
quigley@15642
    30
fun proofstep_to_string Axiom = "Axiom()"
paulson@16091
    31
|   proofstep_to_string  (Binary ((a,b), (c,d)))=
paulson@16091
    32
      "Binary(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    33
|   proofstep_to_string (Factor (a,b,c)) =
paulson@16091
    34
      "Factor("^(string_of_int a)^","^(string_of_int b)^","^(string_of_int c)^")"
paulson@16091
    35
|   proofstep_to_string  (Para ((a,b), (c,d)))= 
paulson@16091
    36
      "Para(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
paulson@16091
    37
|   proofstep_to_string  (MRR ((a,b), (c,d))) =
paulson@16091
    38
      "MRR(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
quigley@16548
    39
(*|   proofstep_to_string (Rewrite((a,b),(c,d))) =
quigley@16548
    40
      "Rewrite(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"*)
quigley@15642
    41
quigley@15642
    42
fun list_to_string [] liststr = liststr
quigley@15642
    43
|   list_to_string (x::[]) liststr = liststr^(string_of_int x)
quigley@15642
    44
|   list_to_string (x::xs) liststr = list_to_string xs (liststr^(string_of_int x)^",")
quigley@15642
    45
quigley@15642
    46
paulson@16091
    47
fun proof_to_string (num,(step,clause_strs, thmvars)) = (string_of_int num)^(proofstep_to_string step)^"["^(clause_strs_to_string clause_strs "")^"]["^(thmvars_to_string thmvars "")^"]"
quigley@15642
    48
 
quigley@15642
    49
quigley@15642
    50
fun proofs_to_string [] str = str
quigley@15642
    51
|   proofs_to_string (x::xs) str = let val newstr = proof_to_string x 
quigley@15642
    52
                                   in
quigley@15642
    53
                                       proofs_to_string xs (str^newstr)
quigley@15642
    54
                                   end
quigley@15642
    55
quigley@15642
    56
quigley@15642
    57
quigley@15642
    58
fun init_proofstep_to_string (num, step, clause_strs) = (string_of_int num)^" "^(proofstep_to_string step)^" "^(clause_strs_to_string clause_strs "")^" "
quigley@15642
    59
quigley@15642
    60
fun init_proofsteps_to_string [] str = str
quigley@15642
    61
|   init_proofsteps_to_string (x::xs) str = let val newstr = init_proofstep_to_string x 
quigley@15642
    62
                                   in
quigley@15642
    63
                                       init_proofsteps_to_string xs (str^newstr)
quigley@15642
    64
                                   end
quigley@15642
    65
  
quigley@15642
    66
quigley@15642
    67
quigley@15642
    68
(*** get a string representing the Isabelle ordered axioms ***)
quigley@15642
    69
paulson@16061
    70
fun origAx_to_string (num,(meta,thmvars)) =
paulson@16061
    71
    let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
    72
    in
paulson@16091
    73
       (string_of_int num)^"OrigAxiom()["^
paulson@16091
    74
       (clause_strs_to_string clause_strs "")^"]["^
paulson@16061
    75
       (thmvars_to_string thmvars "")^"]"
paulson@16061
    76
    end
quigley@15642
    77
quigley@15642
    78
quigley@15642
    79
fun  origAxs_to_string [] str = str
quigley@15642
    80
|   origAxs_to_string (x::xs) str = let val newstr = origAx_to_string x 
quigley@15642
    81
                                   in
quigley@15642
    82
                                       origAxs_to_string xs (str^newstr)
quigley@15642
    83
                                   end
quigley@15642
    84
quigley@15642
    85
quigley@15642
    86
(*** get a string representing the Isabelle ordered axioms not used in the spass proof***)
quigley@15642
    87
paulson@16061
    88
fun extraAx_to_string (num, (meta,thmvars)) =
paulson@16061
    89
   let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
paulson@16061
    90
   in
paulson@16091
    91
      (string_of_int num)^"ExtraAxiom()["^
paulson@16061
    92
      (clause_strs_to_string clause_strs "")^"]"^
paulson@16061
    93
      "["^(thmvars_to_string thmvars "")^"]"
paulson@16061
    94
   end;
quigley@15642
    95
paulson@16061
    96
fun extraAxs_to_string [] str = str
paulson@16061
    97
|   extraAxs_to_string (x::xs) str =
paulson@16061
    98
      let val newstr = extraAx_to_string x 
paulson@16061
    99
      in
paulson@16061
   100
	  extraAxs_to_string xs (str^newstr)
paulson@16061
   101
      end;
quigley@15642
   102
paulson@17312
   103
fun is_axiom (_,Axiom,str) = true
paulson@17312
   104
|   is_axiom (_,_,_) = false
quigley@15642
   105
quigley@15642
   106
fun get_step_nums [] nums = nums
quigley@15642
   107
|   get_step_nums (( num:int,Axiom, str)::xs) nums = get_step_nums xs (nums@[num])
quigley@15642
   108
paulson@15774
   109
exception Noassoc;
paulson@15774
   110
paulson@15774
   111
fun assoc_snd a [] = raise Noassoc
quigley@15642
   112
  | assoc_snd a ((x, y)::t) = if a = y then x else assoc_snd a t;
quigley@15642
   113
quigley@15642
   114
(* change to be something using check_order  instead of a = y --> returns true if ASSERTION not raised in checkorder, false otherwise *)
quigley@15642
   115
quigley@15642
   116
(*fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   117
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_snd x ys))])
quigley@15642
   118
*)
quigley@15642
   119
(*FIX - should this have vars in it? *)
paulson@16061
   120
fun there_out_of_order xs ys = (ReconOrderClauses.checkorder xs ys [] ([],[],[]); true) 
paulson@15774
   121
                               handle _ => false
quigley@15642
   122
paulson@15774
   123
fun assoc_out_of_order a [] = raise Noassoc
quigley@15642
   124
|   assoc_out_of_order a ((b,c)::t) = if there_out_of_order a c then b else assoc_out_of_order a t;
quigley@15642
   125
quigley@15642
   126
fun get_assoc_snds [] xs assocs= assocs
quigley@15642
   127
|   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_out_of_order x ys))])
quigley@15642
   128
quigley@15642
   129
fun add_if_not_inlist [] xs newlist = newlist
paulson@16157
   130
|   add_if_not_inlist (y::ys) xs newlist = if (not (y mem xs)) then 
quigley@15642
   131
                                      add_if_not_inlist ys xs (y::newlist)
quigley@15642
   132
                                        else add_if_not_inlist ys xs (newlist)
quigley@15642
   133
paulson@15700
   134
(*Flattens a list of list of strings to one string*)
paulson@15700
   135
fun onestr ls = String.concat (map String.concat ls);
quigley@15642
   136
quigley@15642
   137
fun thmstrings [] str = str
quigley@15642
   138
|   thmstrings (x::xs) str = thmstrings xs (str^(string_of_thm x))
quigley@15642
   139
quigley@15919
   140
fun is_clasimp_ax clasimp_num n = n <=clasimp_num 
quigley@15642
   141
quigley@15919
   142
quigley@15919
   143
quigley@15919
   144
fun retrieve_axioms clause_arr  [] = []
quigley@15919
   145
|   retrieve_axioms clause_arr  (x::xs) =  [Array.sub(clause_arr, x)]@
quigley@15919
   146
 						     (retrieve_axioms clause_arr  xs)
quigley@15919
   147
quigley@16156
   148
fun subone x = x - 1
quigley@16156
   149
quigley@16156
   150
fun numstr [] = ""
quigley@16156
   151
|   numstr (x::xs) = (string_of_int x)^"%"^(numstr xs)
quigley@16156
   152
quigley@15919
   153
quigley@15919
   154
(* retrieve the axioms that were obtained from the clasimpset *)
quigley@15919
   155
paulson@17312
   156
fun get_clasimp_cls (clause_arr: (ResClause.clause * thm) array) clasimp_num step_nums = 
paulson@16157
   157
    let val realnums = map subone step_nums	
paulson@16157
   158
	val clasimp_nums = List.filter (is_clasimp_ax (clasimp_num - 1)) realnums
quigley@16478
   159
(*	val axnums = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "axnums")))     
paulson@16157
   160
	val _ = TextIO.output(axnums,(numstr clasimp_nums))
quigley@16478
   161
	val _ = TextIO.closeOut(axnums)*)
paulson@16157
   162
    in
paulson@17312
   163
	retrieve_axioms clause_arr clasimp_nums
paulson@16157
   164
    end
quigley@15919
   165
quigley@16156
   166
quigley@16357
   167
(*****************************************************)
quigley@16357
   168
(* get names of clasimp axioms used                  *)
quigley@16357
   169
(*****************************************************)
quigley@16156
   170
paulson@17312
   171
 fun get_axiom_names step_nums thms clause_arr num_of_clauses =
paulson@16157
   172
   let 
paulson@16157
   173
     (* not sure why this is necessary again, but seems to be *)
paulson@16157
   174
      val _ = (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
paulson@16157
   175
  
paulson@16157
   176
     (***********************************************)
paulson@16157
   177
     (* here need to add the clauses from clause_arr*)
paulson@16157
   178
     (***********************************************)
paulson@16157
   179
  
paulson@17312
   180
      val clasimp_names_cls = get_clasimp_cls clause_arr num_of_clauses step_nums 
paulson@16157
   181
      val clasimp_names = map (#1 o ResClause.clause_info o #1) clasimp_names_cls
paulson@16157
   182
  
paulson@16157
   183
      val _ = File.write (File.tmp_path (Path.basic "clasimp_names"))                                                               
paulson@16157
   184
                         (concat clasimp_names)
paulson@16157
   185
      val _ = (print_mode := (["xsymbols", "symbols"] @ ! print_mode))
paulson@16157
   186
   in
paulson@16157
   187
      clasimp_names
paulson@16157
   188
   end
paulson@17306
   189
   
paulson@17306
   190
 fun get_axiom_names_spass proof_steps thms clause_arr num_of_clauses  =
paulson@17312
   191
   get_axiom_names (get_step_nums (List.filter is_axiom proof_steps) []) 
paulson@17306
   192
                       thms clause_arr num_of_clauses;
quigley@16357
   193
    
paulson@17306
   194
 fun get_axiom_names_vamp_E proofstr thms clause_arr num_of_clauses  =
paulson@17306
   195
   get_axiom_names (get_linenums proofstr) thms clause_arr num_of_clauses;
paulson@17306
   196
    
quigley@17235
   197
quigley@16357
   198
(***********************************************)
quigley@16357
   199
(* get axioms for reconstruction               *)
quigley@16357
   200
(***********************************************)
quigley@16156
   201
fun numclstr (vars, []) str = str
paulson@16157
   202
|   numclstr ( vars, ((num, thm)::rest)) str =
paulson@16157
   203
      let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
paulson@16157
   204
      in
paulson@16157
   205
        numclstr  (vars,rest) newstr
paulson@16157
   206
      end
quigley@15919
   207
paulson@16157
   208
fun addvars c (a,b)  = (a,b,c)
quigley@15919
   209
quigley@15919
   210
 fun get_axioms_used proof_steps thms clause_arr num_of_clauses  =
paulson@16061
   211
     let 
paulson@16061
   212
	val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
paulson@17312
   213
	val axioms = (List.filter is_axiom) proof_steps
paulson@16061
   214
	val step_nums = get_step_nums axioms []
quigley@15919
   215
paulson@16061
   216
	val clauses =(*(clasimp_cls)@*)( make_clauses thms)
paulson@16061
   217
	
paulson@16061
   218
	val vars = map thm_vars clauses
paulson@16061
   219
       
paulson@16061
   220
	val distvars = distinct (fold append vars [])
paulson@16061
   221
	val clause_terms = map prop_of clauses  
paulson@16061
   222
	val clause_frees = List.concat (map term_frees clause_terms)
quigley@15642
   223
paulson@16061
   224
	val frees = map lit_string_with_nums clause_frees;
quigley@15642
   225
paulson@16061
   226
	val distfrees = distinct frees
quigley@15642
   227
paulson@16061
   228
	val metas = map Meson.make_meta_clause clauses
paulson@16061
   229
	val ax_strs = map #3 axioms
quigley@15642
   230
paulson@16061
   231
	(* literals of -all- axioms, not just those used by spass *)
paulson@16061
   232
	val meta_strs = map ReconOrderClauses.get_meta_lits metas
paulson@16061
   233
       
paulson@16061
   234
	val metas_and_strs = ListPair.zip (metas,meta_strs)
paulson@16905
   235
	val _ = File.write(File.tmp_path (Path.basic "foo_clauses")) (onestr ax_strs)
paulson@16905
   236
	val _ = File.write(File.tmp_path (Path.basic "foo_metastrs")) (onestr meta_strs)
quigley@15642
   237
paulson@16061
   238
	(* get list of axioms as thms with their variables *)
quigley@15642
   239
paulson@16061
   240
	val ax_metas = get_assoc_snds ax_strs metas_and_strs []
paulson@16061
   241
	val ax_vars = map thm_vars ax_metas
paulson@16061
   242
	val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
quigley@15642
   243
paulson@16061
   244
	(* get list of extra axioms as thms with their variables *)
paulson@16061
   245
	val extra_metas = add_if_not_inlist metas ax_metas []
paulson@16061
   246
	val extra_vars = map thm_vars extra_metas
paulson@16061
   247
	val extra_with_vars = if (not (extra_metas = []) ) 
paulson@16905
   248
			      then ListPair.zip (extra_metas,extra_vars)
paulson@16905
   249
			      else []
paulson@16061
   250
     in
paulson@17306
   251
	(distfrees,distvars, extra_with_vars,ax_with_vars, (ListPair.zip (step_nums,ax_metas)))
paulson@16905
   252
     end;
paulson@16905
   253
                                            
quigley@15642
   254
quigley@15642
   255
(*********************************************************************)
quigley@15642
   256
(* Pass in spass string of proof and string version of isabelle goal *)
quigley@15642
   257
(* Get out reconstruction steps as a string to be sent to Isabelle   *)
quigley@15642
   258
(*********************************************************************)
quigley@15642
   259
quigley@15642
   260
paulson@17122
   261
fun rules_to_string [] = "NONE"
paulson@17122
   262
  | rules_to_string xs = "[" ^ space_implode ", " xs ^ "]"
quigley@16156
   263
                                  
quigley@15642
   264
quigley@16357
   265
(*val  proofstr = "1242[0:Inp] ||  -> equal(const_List_Orev(tconst_fun(tconst_List_Olist(U),tconst_List_Olist(U)),const_List_Olist_ONil),const_List_Olist_ONil)**.\
quigley@16357
   266
\1279[0:Inp] ||  -> equal(const_Nat_Osize(tconst_fun(tconst_List_Olist(U),tconst_nat),const_List_Olist_ONil),const_0)**.\
quigley@16357
   267
\1430[0:Inp] || equal(const_Nat_Osize(tconst_fun(tconst_List_Olist(typ__Ha),tconst_nat),const_List_Orev(tconst_fun(tconst_List_Olist(typ__Ha),tconst_List_Olist(typ__Ha)),const_List_Olist_ONil)),const_Nat_Osize(tconst_fun(tconst_List_Olist(typ__Ha),tconst_nat),const_List_Olist_ONil))** -> .\
quigley@16357
   268
\1453[0:Rew:1279.0,1430.0,1242.0,1430.0,1279.0,1430.0] || equal(const_0,const_0)* -> .\
quigley@16357
   269
\1454[0:Obv:1453.0] ||  -> .";*)
quigley@16357
   270
paulson@16905
   271
fun subst_for a b = String.translate (fn c => str (if c=a then b else c));
quigley@16357
   272
paulson@16905
   273
val remove_linebreaks = subst_for #"\n" #"\t";
paulson@16905
   274
val restore_linebreaks = subst_for #"\t" #"\n";
quigley@16357
   275
quigley@16357
   276
fun spassString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
paulson@16905
   277
  let val _ = File.append(File.tmp_path (Path.basic"thmstringfile"))
paulson@16905
   278
                  ("thmstring is: "^thmstring^"\n proofstr is: "^proofstr^"\n goalstr is: "^goalstring^" \n  num of clauses is: "^(string_of_int num_of_clauses))
quigley@16357
   279
paulson@16905
   280
     (***********************************)
paulson@16905
   281
     (* parse spass proof into datatype *)
paulson@16905
   282
     (***********************************)
paulson@16905
   283
      val tokens = #1(lex proofstr)
paulson@17306
   284
      val proof_steps = parse tokens
paulson@17306
   285
      val _ = File.write (File.tmp_path (Path.basic "parsing_done")) 
paulson@17306
   286
                         ("Did parsing on "^proofstr)
paulson@17306
   287
      val _ = File.write(File.tmp_path (Path.basic "parsing_thmstring"))
paulson@16905
   288
                        ("Parsing for thmstring: "^thmstring)
paulson@16905
   289
      (************************************)
paulson@16905
   290
      (* recreate original subgoal as thm *)
paulson@16905
   291
      (************************************)
paulson@16905
   292
    
paulson@16905
   293
      (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
paulson@16905
   294
      (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
paulson@16905
   295
      (* subgoal this is, and turn it into meta_clauses *)
paulson@16905
   296
      (* should prob add array and table here, so that we can get axioms*)
paulson@16905
   297
      (* produced from the clasimpset rather than the problem *)
quigley@16156
   298
paulson@17306
   299
      val axiom_names = get_axiom_names_spass proof_steps thms clause_arr num_of_clauses
paulson@17122
   300
      val ax_str = "Rules from clasimpset used in proof found by SPASS: " ^ 
paulson@17122
   301
                   rules_to_string axiom_names 
paulson@16905
   302
      val _ = File.append(File.tmp_path (Path.basic "reconstrfile"))
paulson@16905
   303
                         ("reconstring is: "^ax_str^"  "^goalstring)       
paulson@16905
   304
  in 
paulson@16905
   305
       TextIO.output (toParent, ax_str^"\n");
paulson@16905
   306
       TextIO.output (toParent, "goalstring: "^goalstring^"\n");
paulson@16905
   307
       TextIO.output (toParent, "thmstring: "^thmstring^"\n");
paulson@16905
   308
       TextIO.flushOut toParent;
quigley@16156
   309
paulson@16905
   310
       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16905
   311
      (* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   312
       Posix.Process.sleep(Time.fromSeconds 1) ; all_tac
paulson@16905
   313
  end
paulson@16905
   314
  handle _ => 
paulson@16905
   315
  let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
paulson@16905
   316
  in 
paulson@16905
   317
     TextIO.output (toParent,"Proof found but parsing failed for resolution proof: "^(remove_linebreaks proofstr)^"\n" );
paulson@16905
   318
       TextIO.output (toParent, (remove_linebreaks thmstring)^"\n");
paulson@16905
   319
       TextIO.output (toParent, ( (remove_linebreaks goalstring)^"\n"));
paulson@16905
   320
       TextIO.flushOut toParent;
paulson@16905
   321
      Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16905
   322
      (* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   323
      Posix.Process.sleep(Time.fromSeconds 1); all_tac
paulson@16905
   324
  end
quigley@16478
   325
quigley@16478
   326
quigley@16478
   327
fun vampString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
paulson@16905
   328
 let val _ = File.append(File.tmp_path (Path.basic "thmstringfile"))
paulson@16905
   329
               (" thmstring is: "^thmstring^"proofstr is: "^proofstr^
paulson@16905
   330
                "goalstr is: "^goalstring^" num of clauses is: "^
paulson@16905
   331
                (string_of_int num_of_clauses))
paulson@16905
   332
paulson@16905
   333
    (***********************************)
paulson@16905
   334
    (* get vampire axiom names         *)
paulson@16905
   335
    (***********************************)
quigley@16478
   336
paulson@17312
   337
     val axiom_names = get_axiom_names_vamp_E proofstr thms clause_arr num_of_clauses
paulson@17122
   338
     val ax_str = "Rules from clasimpset used in proof found by Vampire: " ^
paulson@17122
   339
                  rules_to_string axiom_names
paulson@16905
   340
    in 
paulson@17312
   341
	 File.append(File.tmp_path (Path.basic "reconstrfile")) 
paulson@17312
   342
	            ("reconstring is: "^ax_str^"  "^goalstring);
paulson@16905
   343
         TextIO.output (toParent, ax_str^"\n");
paulson@16905
   344
	 TextIO.output (toParent, "goalstring: "^goalstring^"\n");
paulson@16905
   345
	 TextIO.output (toParent, "thmstring: "^thmstring^"\n");
paulson@16905
   346
	 TextIO.flushOut toParent;
quigley@16478
   347
paulson@16905
   348
	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16905
   349
	(* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   350
	 Posix.Process.sleep(Time.fromSeconds 1) ; all_tac
paulson@16905
   351
    end
paulson@16905
   352
    handle _ => 
paulson@16905
   353
    let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
paulson@16905
   354
    in 
paulson@16905
   355
	TextIO.output (toParent,"Proof found by Vampire but translation failed for resolution proof: "^(remove_linebreaks proofstr) );
paulson@16905
   356
	 TextIO.output (toParent, thmstring^"\n");
paulson@16905
   357
	 TextIO.output (toParent, goalstring^"\n");
paulson@16905
   358
	 TextIO.flushOut toParent;
paulson@16905
   359
	Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@17306
   360
	Posix.Process.sleep(Time.fromSeconds 1); all_tac
paulson@16905
   361
    end;
quigley@16357
   362
quigley@16357
   363
quigley@17235
   364
fun EString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
quigley@17235
   365
 let val _ = File.append(File.tmp_path (Path.basic "thmstringfile"))
quigley@17235
   366
               (" thmstring is: "^thmstring^"proofstr is: "^proofstr^
quigley@17235
   367
                "goalstr is: "^goalstring^" num of clauses is: "^
quigley@17235
   368
                (string_of_int num_of_clauses))
quigley@17235
   369
quigley@17235
   370
    (***********************************)
paulson@17306
   371
    (* get E axiom names         *)
quigley@17235
   372
    (***********************************)
quigley@17235
   373
paulson@17306
   374
     val axiom_names = get_axiom_names_vamp_E proofstr  thms clause_arr  num_of_clauses
quigley@17235
   375
     val ax_str = "Rules from clasimpset used in proof found by E: " ^
quigley@17235
   376
                  rules_to_string axiom_names
quigley@17235
   377
    in 
paulson@17312
   378
	 File.append(File.tmp_path (Path.basic "reconstrfile"))
paulson@17312
   379
	            ("reconstring is: "^ax_str^"  "^goalstring);
quigley@17235
   380
         TextIO.output (toParent, ax_str^"\n");
paulson@17312
   381
	 TextIO.output (toParent, "goalstring: "^goalstring^"\n");
quigley@17235
   382
	 TextIO.output (toParent, "thmstring: "^thmstring^"\n");
quigley@17235
   383
	 TextIO.flushOut toParent;
quigley@17235
   384
quigley@17235
   385
	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
quigley@17235
   386
	(* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   387
	 Posix.Process.sleep(Time.fromSeconds 1) ; all_tac
quigley@17235
   388
    end
quigley@17235
   389
    handle _ => 
quigley@17235
   390
    let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
quigley@17235
   391
    in 
paulson@17306
   392
	TextIO.output (toParent,"Proof found by E but translation failed for resolution proof: "^(remove_linebreaks proofstr) );
quigley@17235
   393
	 TextIO.output (toParent, thmstring^"\n");
quigley@17235
   394
	 TextIO.output (toParent, goalstring^"\n");
quigley@17235
   395
	 TextIO.flushOut toParent;
quigley@17235
   396
	Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
quigley@17235
   397
	(* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   398
	Posix.Process.sleep(Time.fromSeconds 1); all_tac
quigley@17235
   399
    end;
quigley@17235
   400
quigley@15642
   401
quigley@15919
   402
fun spassString_to_reconString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
paulson@16905
   403
  let val _ = File.write(File.tmp_path (Path.basic "thmstringfile")) 
paulson@16905
   404
                 (" thmstring is: "^thmstring^"proofstr is: "^proofstr)
paulson@16905
   405
      val tokens = #1(lex proofstr)
quigley@15782
   406
paulson@16905
   407
  (***********************************)
paulson@16905
   408
  (* parse spass proof into datatype *)
paulson@16905
   409
  (***********************************)
paulson@17306
   410
      val proof_steps = parse tokens
quigley@15919
   411
paulson@16905
   412
      val _ = File.write (File.tmp_path (Path.basic "foo_parse"))
paulson@16905
   413
                      ("Did parsing on "^proofstr)
paulson@16905
   414
    
paulson@16905
   415
      val _ = File.write (File.tmp_path (Path.basic "foo_thmstring_at_parse"))
paulson@16905
   416
                                ("Parsing for thmstring: "^thmstring)
paulson@16905
   417
  (************************************)
paulson@16905
   418
  (* recreate original subgoal as thm *)
paulson@16905
   419
  (************************************)
paulson@16905
   420
      (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
paulson@16905
   421
      (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
paulson@16905
   422
      (* subgoal this is, and turn it into meta_clauses *)
paulson@16905
   423
      (* should prob add array and table here, so that we can get axioms*)
paulson@16905
   424
      (* produced from the clasimpset rather than the problem *)
paulson@16905
   425
      val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms clause_arr  num_of_clauses
paulson@16905
   426
      
paulson@16905
   427
      (*val numcls_string = numclstr ( vars, numcls) ""*)
paulson@16905
   428
      val _ = File.write (File.tmp_path (Path.basic "foo_axiom")) "got axioms"
paulson@16061
   429
	
paulson@16905
   430
  (************************************)
paulson@16905
   431
  (* translate proof                  *)
paulson@16905
   432
  (************************************)
paulson@16905
   433
      val _ = File.write (File.tmp_path (Path.basic "foo_steps"))                                                                           
paulson@16905
   434
                       ("about to translate proof, steps: "
paulson@16905
   435
                       ^(init_proofsteps_to_string proof_steps ""))
paulson@16905
   436
      val (newthm,proof) = translate_proof numcls  proof_steps vars
paulson@16905
   437
      val _ = File.write (File.tmp_path (Path.basic "foo_steps2"))                                                                       
paulson@16905
   438
                       ("translated proof, steps: "^(init_proofsteps_to_string proof_steps ""))
paulson@16905
   439
  (***************************************************)
paulson@16905
   440
  (* transfer necessary steps as strings to Isabelle *)
paulson@16905
   441
  (***************************************************)
paulson@16905
   442
      (* turn the proof into a string *)
paulson@16905
   443
      val reconProofStr = proofs_to_string proof ""
paulson@16905
   444
      (* do the bit for the Isabelle ordered axioms at the top *)
paulson@16905
   445
      val ax_nums = map #1 numcls
paulson@16905
   446
      val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
paulson@16905
   447
      val numcls_strs = ListPair.zip (ax_nums,ax_strs)
paulson@16905
   448
      val num_cls_vars =  map (addvars vars) numcls_strs;
paulson@16905
   449
      val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
paulson@16905
   450
      
paulson@16905
   451
      val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars))
paulson@16905
   452
                       else []
paulson@16905
   453
      val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
paulson@16905
   454
      val frees_str = "["^(thmvars_to_string frees "")^"]"
paulson@16905
   455
      val _ = File.write (File.tmp_path (Path.basic "reconstringfile"))
paulson@16905
   456
                          (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
paulson@16905
   457
      val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
paulson@16905
   458
  in 
paulson@16905
   459
       TextIO.output (toParent, reconstr^"\n");
paulson@16905
   460
       TextIO.flushOut toParent;
paulson@16905
   461
       TextIO.output (toParent, thmstring^"\n");
paulson@16905
   462
       TextIO.flushOut toParent;
paulson@16905
   463
       TextIO.output (toParent, goalstring^"\n");
paulson@16905
   464
       TextIO.flushOut toParent;
quigley@15642
   465
paulson@16905
   466
       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16905
   467
      (* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   468
       Posix.Process.sleep(Time.fromSeconds 1) ; all_tac
paulson@16905
   469
  end
paulson@16905
   470
  handle _ => 
paulson@16905
   471
  let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
paulson@16905
   472
  in 
paulson@16905
   473
       TextIO.output (toParent,"Proof found but translation failed for resolution proof:"^(remove_linebreaks proofstr) ^"\n");
paulson@16905
   474
      TextIO.flushOut toParent;
paulson@16905
   475
    TextIO.output (toParent, thmstring^"\n");
paulson@16905
   476
       TextIO.flushOut toParent;
paulson@16905
   477
       TextIO.output (toParent, goalstring^"\n");
paulson@16905
   478
       TextIO.flushOut toParent;
paulson@16905
   479
      Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
paulson@16905
   480
      (* Attempt to prevent several signals from turning up simultaneously *)
paulson@17306
   481
      Posix.Process.sleep(Time.fromSeconds 1); all_tac
paulson@16905
   482
  end
quigley@15642
   483
quigley@15642
   484
quigley@15642
   485
(**********************************************************************************)
quigley@15642
   486
(* At other end, want to turn back into datatype so can apply reconstruct_proof.  *)
quigley@15642
   487
(* This will be done by the signal handler                                        *)
quigley@15642
   488
(**********************************************************************************)
quigley@15642
   489
quigley@15642
   490
(* Parse in the string version of the proof steps for reconstruction *)
quigley@15642
   491
(* Isar format: cl1 [BINARY 0 cl2 0];cl1 [PARAMOD 0 cl2 0]; cl1 [DEMOD 0 cl2];cl1 [FACTOR 1 2];*)
quigley@15642
   492
quigley@15642
   493
quigley@15642
   494
 val term_numstep =
quigley@15642
   495
        (number ++ (a (Other ",")) ++ number) >> (fn (a, (_, c)) => (a, c))
quigley@15642
   496
quigley@15642
   497
val extraaxiomstep = (a (Word "ExtraAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   498
            >> (fn (_) => ExtraAxiom)
quigley@15642
   499
quigley@15642
   500
quigley@15642
   501
quigley@15642
   502
val origaxiomstep = (a (Word "OrigAxiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   503
            >> (fn (_) => OrigAxiom)
quigley@15642
   504
quigley@15642
   505
quigley@15642
   506
 val axiomstep = (a (Word "Axiom"))++ (a (Other "(")) ++(a (Other ")"))
quigley@15642
   507
            >> (fn (_) => Axiom)
quigley@15642
   508
     
quigley@15642
   509
quigley@15642
   510
quigley@15642
   511
      
quigley@15642
   512
 val binarystep = (a (Word "Binary")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   513
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   514
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   515
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Binary (c,e))
quigley@15642
   516
      
quigley@15642
   517
quigley@15642
   518
 val parastep = (a (Word "Para")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   519
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   520
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   521
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Para(c, e))
quigley@15642
   522
      
quigley@15642
   523
 val mrrstep = (a (Word "MRR")) ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   524
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   525
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@15642
   526
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => MRR(c, e))
quigley@15642
   527
      
quigley@15642
   528
quigley@15642
   529
 val factorstep = (a (Word "Factor")) ++ (a (Other "("))
quigley@15642
   530
                    ++ number ++ (a (Other ","))
quigley@15642
   531
                       ++ number ++ (a (Other ","))
quigley@15642
   532
                       ++ number ++  (a (Other ")"))
quigley@15642
   533
                   
quigley@15642
   534
            >> (fn (_, (_, (c, (_, (e,(_,(f,_))))))) =>  Factor (c,e,f))
quigley@15642
   535
quigley@15642
   536
quigley@16548
   537
(*val rewritestep = (a (Word "Rewrite"))  ++ (a (Other "(")) ++ (a (Other "(")) 
quigley@15642
   538
                   ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
quigley@15642
   539
                   ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
quigley@16548
   540
            >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Rewrite (c,e))*)
quigley@15642
   541
quigley@15642
   542
val obviousstep = (a (Word "Obvious")) ++ (a (Other "(")) 
quigley@15642
   543
                   ++ term_numstep  ++ (a (Other ")")) 
quigley@15642
   544
            >> (fn (_, (_, (c,_))) => Obvious (c))
quigley@15642
   545
quigley@16548
   546
 val methodstep = extraaxiomstep || origaxiomstep || axiomstep ||binarystep || factorstep|| parastep || mrrstep || (*rewritestep ||*) obviousstep
quigley@15642
   547
quigley@15642
   548
quigley@15642
   549
 val number_list_step =
paulson@15739
   550
        ( number ++ many ((a (Other ",") ++ number)>> #2))
quigley@15642
   551
        >> (fn (a,b) => (a::b))
quigley@15642
   552
        
quigley@15642
   553
 val numberlist_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   554
                        >>(fn (_,_) => ([]:int list))
quigley@15642
   555
                       || a (Other "[") ++ number_list_step ++ a (Other "]")
quigley@15642
   556
                        >>(fn (_,(a,_)) => a)
quigley@15642
   557
                    
quigley@15642
   558
quigley@15642
   559
quigley@15642
   560
(** change this to allow P (x U) *)
paulson@17306
   561
 fun arglist_step input = 
paulson@17306
   562
   ( word ++ many word >> (fn (a, b) => (a^" "^(space_implode " " b)))
paulson@17306
   563
    ||word >> (fn (a) => (a)))input
quigley@15642
   564
                
quigley@15642
   565
quigley@15642
   566
fun literal_step input = (word ++ a (Other "(") ++ arglist_step ++  a (Other ")")
quigley@15642
   567
                                          >>(fn (a, (b, (c,d))) => (a^" ("^(c)^")"))
quigley@15642
   568
                        || arglist_step >> (fn (a) => (a)))input
quigley@15642
   569
                           
quigley@15642
   570
quigley@15642
   571
quigley@15642
   572
(* fun term_step input = (a (Other "~") ++ arglist_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   573
                     ||  arglist_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   574
*)
quigley@15642
   575
quigley@15642
   576
quigley@15642
   577
 fun term_step input = (a (Other "~") ++ literal_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
quigley@15642
   578
                     ||  literal_step ++ a (Other "%")>> (fn (a,b) => a ))input
quigley@15642
   579
quigley@15642
   580
quigley@15642
   581
         
quigley@15642
   582
quigley@15642
   583
 val term_list_step =
quigley@15642
   584
        (  term_step ++ many ( term_step))
quigley@15642
   585
        >> (fn (a,b) => (a::b))
quigley@15642
   586
        
quigley@15642
   587
 
quigley@15642
   588
val term_lists_step = a (Other "[")  ++ a (Other "]")
quigley@15642
   589
                        >>(fn (_,_) => ([]:string list))
quigley@15642
   590
                       || a (Other "[") ++ term_list_step ++ a (Other "]")
quigley@15642
   591
                        >>(fn (_,(a,_)) => a)
quigley@15642
   592
                     
quigley@15642
   593
quigley@15642
   594
quigley@15642
   595
quigley@15642
   596
fun anytoken_step input  = (word>> (fn (a) => a)  ) input
quigley@15642
   597
                       handle NOPARSE_WORD => (number>> (fn (a) => string_of_int a)  ) input
quigley@15642
   598
                      handle NOPARSE_NUMBER => (other_char >> (fn(a) => a)) input
quigley@15642
   599
quigley@15642
   600
quigley@15642
   601
quigley@15642
   602
fun goalstring_step input= (anytoken_step ++ many (anytoken_step )
quigley@15642
   603
                  >> (fn (a,b) =>  (a^" "^(implode b)))) input
quigley@15642
   604
quigley@15642
   605
quigley@15642
   606
quigley@15642
   607
 val linestep = number ++ methodstep ++ term_lists_step ++ term_lists_step
quigley@15642
   608
                >> (fn (a, (b, (c,d))) => (a,(b,c,d)))
quigley@15642
   609
    
quigley@15642
   610
 val lines_step = many linestep
quigley@15642
   611
paulson@15739
   612
 val alllines_step = (term_lists_step ++ lines_step ) ++ finished >> #1
quigley@15642
   613
    
paulson@15739
   614
 val parse_step = #1 o alllines_step
quigley@15642
   615
quigley@15642
   616
quigley@15642
   617
 (*
quigley@15642
   618
val reconstr ="[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]%(EX x::'a::type. ALL y::'a::type. (P::'a::type => bool) x = P y) -->(EX x::'a::type. P x) = (ALL y::'a::type. P y)"
quigley@15642
   619
*)
quigley@15642
   620
quigley@15642
   621
(************************************************************)
quigley@15642
   622
(* Construct an Isar style proof from a list of proof steps *)
quigley@15642
   623
(************************************************************)
quigley@15642
   624
(* want to assume all axioms, then do haves for the other clauses*)
quigley@15642
   625
(* then show for the last step *)
quigley@15642
   626
quigley@15642
   627
(* replace ~ by not here *)
quigley@15642
   628
fun change_nots [] = []
quigley@15642
   629
|   change_nots (x::xs) = if x = "~" 
quigley@15642
   630
                          then 
quigley@15642
   631
                             ["\\", "<", "n", "o", "t", ">"]@(change_nots xs)
quigley@15642
   632
                          else (x::(change_nots xs))
quigley@15642
   633
quigley@15642
   634
(*
quigley@15642
   635
fun clstrs_to_string [] str = str
quigley@15642
   636
|   clstrs_to_string (x::[]) str = str^x
quigley@15642
   637
|   clstrs_to_string (x::xs) str = clstrs_to_string xs (str^(x^"; "))
quigley@15642
   638
*)
quigley@15642
   639
fun clstrs_to_string [] str = implode (change_nots (explode str))
quigley@15642
   640
|   clstrs_to_string (x::[]) str = implode (change_nots (explode (str^x)))
quigley@15642
   641
|   clstrs_to_string (x::xs) str = implode (change_nots (explode (clstrs_to_string xs (str^(x^"; ")))))
quigley@15642
   642
quigley@15642
   643
quigley@15642
   644
quigley@15642
   645
fun thmvars_to_quantstring [] str = str
quigley@15642
   646
|   thmvars_to_quantstring (x::[]) str =str^x^". "
quigley@15642
   647
|   thmvars_to_quantstring (x::xs) str = thmvars_to_quantstring xs (str^(x^" "))
quigley@15642
   648
quigley@15642
   649
paulson@16091
   650
fun clause_strs_to_isar clstrs [] =  "\"\\<lbrakk>"^(clstrs_to_string clstrs "")^"\\<rbrakk> \\<Longrightarrow> False\""
paulson@16091
   651
|   clause_strs_to_isar clstrs thmvars = "\"\\<And>"^(thmvars_to_quantstring thmvars "")^"\\<lbrakk>"^(clstrs_to_string clstrs "")^"\\<rbrakk> \\<Longrightarrow> False\""
quigley@15642
   652
quigley@15642
   653
fun frees_to_string [] str = implode (change_nots (explode str))
quigley@15642
   654
|   frees_to_string (x::[]) str = implode (change_nots (explode (str^x)))
quigley@15642
   655
|   frees_to_string  (x::xs) str = implode (change_nots (explode (frees_to_string xs (str^(x^" ")))))
quigley@15642
   656
quigley@15642
   657
fun frees_to_isar_str [] =  ""
quigley@15642
   658
|   frees_to_isar_str  clstrs = (frees_to_string clstrs "")
quigley@15642
   659
quigley@15642
   660
quigley@15642
   661
(***********************************************************************)
quigley@15642
   662
(* functions for producing assumptions for the Isabelle ordered axioms *)
quigley@15642
   663
(***********************************************************************)
quigley@15642
   664
(*val str = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";       
quigley@15642
   665
num, rule, clausestrs, vars*)
quigley@15642
   666
quigley@15642
   667
quigley@15642
   668
(* assume the extra clauses - not used in Spass proof *)
quigley@15642
   669
quigley@15642
   670
fun is_extraaxiom_step ( num:int,(ExtraAxiom, str, tstr)) = true
quigley@15642
   671
|   is_extraaxiom_step (num, _) = false
quigley@15642
   672
quigley@15642
   673
fun get_extraaxioms xs = List.filter (is_extraaxiom_step) ( xs)
quigley@15642
   674
quigley@15642
   675
fun assume_isar_extraaxiom [] str  = str
quigley@15642
   676
|   assume_isar_extraaxiom ((numb,(step, clstr, thmvars))::xs) str  = assume_isar_extraaxiom xs (str^"and cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstr thmvars)^"\n " )
quigley@15642
   677
quigley@15642
   678
quigley@15642
   679
quigley@15642
   680
fun assume_isar_extraaxioms  [] = ""
quigley@15642
   681
|assume_isar_extraaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
quigley@15642
   682
                                         in
quigley@15642
   683
                                             assume_isar_extraaxiom xs str
quigley@15642
   684
                                         end
quigley@15642
   685
quigley@15642
   686
(* assume the Isabelle ordered clauses *)
quigley@15642
   687
quigley@15642
   688
fun is_origaxiom_step ( num:int,(OrigAxiom, str, tstr)) = true
quigley@15642
   689
|   is_origaxiom_step (num, _) = false
quigley@15642
   690
quigley@15642
   691
fun get_origaxioms xs = List.filter (is_origaxiom_step) ( xs)
quigley@15642
   692
quigley@15642
   693
fun assume_isar_origaxiom [] str  = str
quigley@15642
   694
|   assume_isar_origaxiom ((numb,(step, clstr, thmvars))::xs) str  = assume_isar_origaxiom xs (str^"and cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstr thmvars)^"\n " )
quigley@15642
   695
quigley@15642
   696
quigley@15642
   697
quigley@15642
   698
fun assume_isar_origaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
quigley@15642
   699
                                         in
quigley@15642
   700
                                             assume_isar_origaxiom xs str
quigley@15642
   701
                                         end
quigley@15642
   702
quigley@15642
   703
quigley@15642
   704
quigley@15642
   705
fun is_axiom_step ( num:int,(Axiom, str, tstr)) = true
quigley@15642
   706
|   is_axiom_step (num, _) = false
quigley@15642
   707
quigley@15642
   708
fun get_axioms xs = List.filter  (is_axiom_step) ( xs)
quigley@15642
   709
quigley@15642
   710
fun have_isar_axiomline (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@15642
   711
quigley@15642
   712
fun  by_isar_axiomline (numb,(step, clstrs, thmstrs))="by (rule cl"^ (string_of_int numb)^"') \n"
quigley@15642
   713
quigley@15642
   714
quigley@15642
   715
fun isar_axiomline (numb, (step, clstrs, thmstrs))  = (have_isar_axiomline (numb,(step,clstrs, thmstrs )))^( by_isar_axiomline(numb,(step,clstrs, thmstrs )) )
quigley@15642
   716
quigley@15642
   717
quigley@15642
   718
fun isar_axiomlines [] str = str
quigley@15642
   719
|   isar_axiomlines (x::xs) str = isar_axiomlines xs (str^(isar_axiomline x))
quigley@15642
   720
quigley@15642
   721
quigley@15642
   722
fun have_isar_line (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
quigley@16357
   723
(*FIX: ask Larry to add and mrr attribute *)
quigley@15642
   724
paulson@16091
   725
fun by_isar_line ((Binary ((a,b), (c,d)))) = 
paulson@16091
   726
    "by(rule cl"^
paulson@16091
   727
		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
paulson@16091
   728
		(string_of_int c)^" "^(string_of_int d)^"])\n"
quigley@16357
   729
|by_isar_line ((MRR ((a,b), (c,d)))) = 
quigley@16357
   730
    "by(rule cl"^
quigley@16357
   731
		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
quigley@16357
   732
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   733
|   by_isar_line ( (Para ((a,b), (c,d)))) =
paulson@16091
   734
    "by (rule cl"^
paulson@16091
   735
		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
paulson@16091
   736
		(string_of_int c)^" "^(string_of_int d)^"])\n"
paulson@16091
   737
|   by_isar_line ((Factor ((a,b,c)))) = 
paulson@16091
   738
    "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
paulson@16091
   739
		(string_of_int c)^" ])\n"
quigley@16548
   740
(*|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
paulson@16091
   741
    "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
quigley@16548
   742
		(string_of_int c)^" "^(string_of_int d)^" ])\n"*)
paulson@16091
   743
|   by_isar_line ( (Obvious ((a,b)))) =
paulson@16091
   744
    "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
quigley@15642
   745
quigley@15642
   746
fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
quigley@15642
   747
quigley@15642
   748
quigley@15642
   749
fun isar_lines [] str = str
quigley@15642
   750
|   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
quigley@15642
   751
quigley@15642
   752
fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
quigley@15642
   753
quigley@15642
   754
paulson@16091
   755
fun to_isar_proof (frees, xs, goalstring) =
paulson@16091
   756
    let val extraaxioms = get_extraaxioms xs
paulson@16091
   757
	val extraax_num = length extraaxioms
paulson@16091
   758
	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
paulson@16091
   759
	
paulson@16091
   760
	val origaxioms = get_origaxioms origaxioms_and_steps
paulson@16091
   761
	val origax_num = length origaxioms
paulson@16091
   762
	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
paulson@16091
   763
	val axioms = get_axioms axioms_and_steps
paulson@16091
   764
	
paulson@16091
   765
	val steps = Library.drop (origax_num, axioms_and_steps)
paulson@16091
   766
	val firststeps = ReconOrderClauses.butlast steps
paulson@17312
   767
	val laststep = List.last steps
paulson@16091
   768
	val goalstring = implode(ReconOrderClauses.butlast(explode goalstring))
paulson@16091
   769
	
paulson@16091
   770
	val isar_proof = 
paulson@16091
   771
		("show \""^goalstring^"\"\n")^
paulson@16091
   772
		("proof (rule ccontr,skolemize, make_clauses) \n")^
paulson@16091
   773
		("fix "^(frees_to_isar_str frees)^"\n")^
paulson@16091
   774
		(assume_isar_extraaxioms extraaxioms)^
paulson@16091
   775
		(assume_isar_origaxioms origaxioms)^
paulson@16091
   776
		(isar_axiomlines axioms "")^
paulson@16091
   777
		(isar_lines firststeps "")^
paulson@16091
   778
		(last_isar_line laststep)^
paulson@16091
   779
		("qed")
paulson@16905
   780
	val _ = File.write (File.tmp_path (Path.basic "isar_proof_file")) isar_proof
paulson@16091
   781
    in
paulson@16091
   782
	isar_proof
paulson@16091
   783
    end;
quigley@15642
   784
quigley@15642
   785
(* get fix vars from axioms - all Frees *)
quigley@15642
   786
(* check each clause for meta-vars and /\ over them at each step*)
quigley@15642
   787
quigley@15642
   788
(*******************************************************)
quigley@15642
   789
(* This assumes the thm list  "numcls" is still there  *)
quigley@15642
   790
(* In reality, should probably label it with an        *)
quigley@15642
   791
(* ID number identifying the subgoal.  This could      *)
quigley@15642
   792
(* be passed over to the watcher, e.g.  numcls25       *)
quigley@15642
   793
(*******************************************************)
quigley@15642
   794
quigley@15642
   795
(* val str = "[S%x%P%R%Q%]1ExtraAxiom()[~ Q U%~ R U%][U%]2ExtraAxiom()[~ Q U%~ P U%][U%]3ExtraAxiom()[Q U%R U%][U%]1OrigAxiom()[S x%][]2OrigAxiom()[P U%R U%][U%]6OrigAxiom()[~ S U%~ P U%][U%]7OrigAxiom()[~ S U%~ R U%][U%]1Axiom()[S x%][]2Axiom()[R U%P U%][U%]6Axiom()[~ P U%~ S U%][U%]7Axiom()[~ R U%~ S U%][U%]8Binary((6,1),(1,0))[~ P x%][]9Binary((7,1),(1,0))[~ R x%][]19Binary((9,0),(2,0))[P x%][]25Binary((8,0),(19,0))[][]";
quigley@15642
   796
quigley@15642
   797
val str = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";
quigley@15642
   798
quigley@15642
   799
val reconstr = "[P%Q%x%xa%]1OrigAxiom()[~ P U%][U%]3OrigAxiom()[Q U%][U%]5OrigAxiom()[P (x U)%~ Q (xa U)%][U%]9Binary((7,0),(3,0))[][]7Binary((1,0),(5,0))[~ Q (xa U)%][U%]5Axiom()[P (x U)%~ Q (xa U)%][U%]3Axiom()[Q U%][U%]1Axiom()[~ P U%][U%](ALL xb::'a::type.   (~ (P::'a::type => bool) ((x::'a::type => 'a::type) xb) |    (Q::'a::type => bool) ((xa::'a::type => 'a::type) xb)) &   P xb & ~ Q xb)";
quigley@15642
   800
quigley@15642
   801
quigley@15642
   802
val reconstr = "[P%x%xa%xb%]1OrigAxiom()[P x%~ P U%][U%]3OrigAxiom()[P U%~ P x%][U%]5OrigAxiom()[~ P xa%~ P U%][U%]7OrigAxiom()[P U%P xb%][U%]1Axiom()[P x%~ P U%][U%]3Axiom()[P U%~ P x%][U%]5Axiom()[~ P U%~ P xa%][U%]7Axiom()[P U%P xb%][U%]9Factor(5,0,1)[~ P xa%][]10Binary((9,0),(3,0))[~ P x%][]11Binary((10,0),(1,0))[~ P U%][U%]12Factor(7,0,1)[P xb%][]14Binary((11,0),(12,0))[][]";
quigley@15642
   803
quigley@15642
   804
val thmstring = " (ALL xa::'a::type.   (~ (P::'a::type => bool) (x::'a::type) | P xa) & (~ P xa | P x)) & (((P::'a::type => bool) (xa::'a::type) | (ALL x::'a::type. P x)) &((ALL x::'a::type. ~ P x) | ~ P (xb::'a::type)))";
quigley@15642
   805
*)
quigley@15642
   806
paulson@15739
   807
fun apply_res_thm str goalstring  = let val tokens = #1 (lex str);
quigley@16357
   808
				val _ = File.append (File.tmp_path (Path.basic "applyres")) 
quigley@16357
   809
			           ("str is: "^str^" goalstr is: "^goalstring^"\n")	
quigley@15642
   810
                                   val (frees,recon_steps) = parse_step tokens 
quigley@15642
   811
                                   val isar_str = to_isar_proof (frees, recon_steps, goalstring)
wenzelm@16259
   812
                                   val foo =   TextIO.openOut (File.platform_path(File.tmp_path (Path.basic "foobar")));
quigley@15642
   813
                               in 
quigley@15642
   814
                                  TextIO.output(foo,(isar_str));TextIO.closeOut foo;Pretty.writeln(Pretty.str  isar_str); () 
quigley@15642
   815
                               end 
quigley@15642
   816
quigley@15642
   817
quigley@16357
   818
(* val reconstr = reconstr^"[P%]51Axiom()[~P%]52Axiom[P%][]53MRR((52,0),(51,0))[][]";
quigley@16357
   819
*)
paulson@15684
   820
end;