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