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