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