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