src/HOL/Tools/ATP/recon_transfer_proof.ML
author paulson
Fri Jul 22 17:43:03 2005 +0200 (2005-07-22)
changeset 16905 fa26952fa7b6
parent 16803 014090d1e64b
child 17122 278eb6251dc0
permissions -rw-r--r--
reformatting and tidying
     1 (*  ID:         $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 (******************)
     7 (* complete later *)
     8 (******************)
     9 
    10 structure Recon_Transfer =
    11 struct
    12 
    13 open Recon_Parse
    14 
    15 infixr 8 ++; infixr 7 >>; infixr 6 ||;
    16 
    17 
    18 (* Versions that include type information *)
    19  
    20 (* FIXME rename to str_of_thm *)
    21 fun string_of_thm thm =
    22   setmp show_sorts true (Pretty.str_of o Display.pretty_thm) thm;
    23 
    24 
    25 (* check separate args in the watcher program for separating strings with a * or ; or something *)
    26 
    27 fun clause_strs_to_string [] str = str
    28 |   clause_strs_to_string (x::xs) str = clause_strs_to_string xs (str^x^"%")
    29 
    30 fun thmvars_to_string [] str = str
    31 |   thmvars_to_string (x::xs) str = thmvars_to_string xs (str^x^"%")
    32 
    33 
    34 fun proofstep_to_string Axiom = "Axiom()"
    35 |   proofstep_to_string  (Binary ((a,b), (c,d)))=
    36       "Binary(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    37 |   proofstep_to_string (Factor (a,b,c)) =
    38       "Factor("^(string_of_int a)^","^(string_of_int b)^","^(string_of_int c)^")"
    39 |   proofstep_to_string  (Para ((a,b), (c,d)))= 
    40       "Para(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    41 |   proofstep_to_string  (MRR ((a,b), (c,d))) =
    42       "MRR(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    43 (*|   proofstep_to_string (Rewrite((a,b),(c,d))) =
    44       "Rewrite(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"*)
    45 
    46 fun list_to_string [] liststr = liststr
    47 |   list_to_string (x::[]) liststr = liststr^(string_of_int x)
    48 |   list_to_string (x::xs) liststr = list_to_string xs (liststr^(string_of_int x)^",")
    49 
    50 
    51 fun proof_to_string (num,(step,clause_strs, thmvars)) = (string_of_int num)^(proofstep_to_string step)^"["^(clause_strs_to_string clause_strs "")^"]["^(thmvars_to_string thmvars "")^"]"
    52  
    53 
    54 fun proofs_to_string [] str = str
    55 |   proofs_to_string (x::xs) str = let val newstr = proof_to_string x 
    56                                    in
    57                                        proofs_to_string xs (str^newstr)
    58                                    end
    59 
    60 
    61 
    62 fun init_proofstep_to_string (num, step, clause_strs) = (string_of_int num)^" "^(proofstep_to_string step)^" "^(clause_strs_to_string clause_strs "")^" "
    63 
    64 fun init_proofsteps_to_string [] str = str
    65 |   init_proofsteps_to_string (x::xs) str = let val newstr = init_proofstep_to_string x 
    66                                    in
    67                                        init_proofsteps_to_string xs (str^newstr)
    68                                    end
    69   
    70 
    71 
    72 (*** get a string representing the Isabelle ordered axioms ***)
    73 
    74 fun origAx_to_string (num,(meta,thmvars)) =
    75     let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
    76     in
    77        (string_of_int num)^"OrigAxiom()["^
    78        (clause_strs_to_string clause_strs "")^"]["^
    79        (thmvars_to_string thmvars "")^"]"
    80     end
    81 
    82 
    83 fun  origAxs_to_string [] str = str
    84 |   origAxs_to_string (x::xs) str = let val newstr = origAx_to_string x 
    85                                    in
    86                                        origAxs_to_string xs (str^newstr)
    87                                    end
    88 
    89 
    90 (*** get a string representing the Isabelle ordered axioms not used in the spass proof***)
    91 
    92 fun extraAx_to_string (num, (meta,thmvars)) =
    93    let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
    94    in
    95       (string_of_int num)^"ExtraAxiom()["^
    96       (clause_strs_to_string clause_strs "")^"]"^
    97       "["^(thmvars_to_string thmvars "")^"]"
    98    end;
    99 
   100 fun extraAxs_to_string [] str = str
   101 |   extraAxs_to_string (x::xs) str =
   102       let val newstr = extraAx_to_string x 
   103       in
   104 	  extraAxs_to_string xs (str^newstr)
   105       end;
   106 
   107 fun is_axiom ( num:int,Axiom, str) = true
   108 |   is_axiom (num, _,_) = false
   109 
   110 fun get_init_axioms xs = List.filter (is_axiom) ( xs)
   111 
   112 fun get_step_nums [] nums = nums
   113 |   get_step_nums (( num:int,Axiom, str)::xs) nums = get_step_nums xs (nums@[num])
   114 
   115 exception Noassoc;
   116 
   117 fun assoc_snd a [] = raise Noassoc
   118   | assoc_snd a ((x, y)::t) = if a = y then x else assoc_snd a t;
   119 
   120 (* change to be something using check_order  instead of a = y --> returns true if ASSERTION not raised in checkorder, false otherwise *)
   121 
   122 (*fun get_assoc_snds [] xs assocs= assocs
   123 |   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_snd x ys))])
   124 *)
   125 (*FIX - should this have vars in it? *)
   126 fun there_out_of_order xs ys = (ReconOrderClauses.checkorder xs ys [] ([],[],[]); true) 
   127                                handle _ => false
   128 
   129 fun assoc_out_of_order a [] = raise Noassoc
   130 |   assoc_out_of_order a ((b,c)::t) = if there_out_of_order a c then b else assoc_out_of_order a t;
   131 
   132 fun get_assoc_snds [] xs assocs= assocs
   133 |   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_out_of_order x ys))])
   134 
   135 fun add_if_not_inlist [] xs newlist = newlist
   136 |   add_if_not_inlist (y::ys) xs newlist = if (not (y mem xs)) then 
   137                                       add_if_not_inlist ys xs (y::newlist)
   138                                         else add_if_not_inlist ys xs (newlist)
   139 
   140 (*Flattens a list of list of strings to one string*)
   141 fun onestr ls = String.concat (map String.concat ls);
   142 
   143 fun thmstrings [] str = str
   144 |   thmstrings (x::xs) str = thmstrings xs (str^(string_of_thm x))
   145 
   146 fun is_clasimp_ax clasimp_num n = n <=clasimp_num 
   147 
   148 
   149 
   150 fun retrieve_axioms clause_arr  [] = []
   151 |   retrieve_axioms clause_arr  (x::xs) =  [Array.sub(clause_arr, x)]@
   152  						     (retrieve_axioms clause_arr  xs)
   153 
   154 fun subone x = x - 1
   155 
   156 fun numstr [] = ""
   157 |   numstr (x::xs) = (string_of_int x)^"%"^(numstr xs)
   158 
   159 
   160 (* retrieve the axioms that were obtained from the clasimpset *)
   161 
   162 fun get_clasimp_cls clause_arr clasimp_num step_nums = 
   163     let val realnums = map subone step_nums	
   164 	val clasimp_nums = List.filter (is_clasimp_ax (clasimp_num - 1)) realnums
   165 (*	val axnums = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "axnums")))     
   166 	val _ = TextIO.output(axnums,(numstr clasimp_nums))
   167 	val _ = TextIO.closeOut(axnums)*)
   168     in
   169 	retrieve_axioms clause_arr  clasimp_nums
   170     end
   171 
   172 
   173 
   174 (*****************************************************)
   175 (* get names of clasimp axioms used                  *)
   176 (*****************************************************)
   177 
   178  fun get_axiom_names proof_steps thms (clause_arr:(ResClause.clause * thm) Array.array) num_of_clauses  =
   179    let 
   180      (* not sure why this is necessary again, but seems to be *)
   181       val _ = (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   182       val axioms = get_init_axioms proof_steps
   183       val step_nums = get_step_nums axioms []
   184   
   185      (***********************************************)
   186      (* here need to add the clauses from clause_arr*)
   187      (***********************************************)
   188   
   189       val (clasimp_names_cls) = get_clasimp_cls clause_arr num_of_clauses step_nums 
   190       val clasimp_names = map (#1 o ResClause.clause_info o #1) clasimp_names_cls
   191   
   192       val _ = File.write (File.tmp_path (Path.basic "clasimp_names"))                                                               
   193                          (concat clasimp_names)
   194       val _ = (print_mode := (["xsymbols", "symbols"] @ ! print_mode))
   195    in
   196       clasimp_names
   197    end
   198     
   199  fun get_axiom_names_vamp proofstr thms (clause_arr:(ResClause.clause * thm) Array.array) num_of_clauses  =
   200    let 
   201      (* not sure why this is necessary again, but seems to be *)
   202       val _ = (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   203       val step_nums =get_linenums proofstr
   204   
   205      (***********************************************)
   206      (* here need to add the clauses from clause_arr*)
   207      (***********************************************)
   208   
   209       val (clasimp_names_cls) = get_clasimp_cls clause_arr num_of_clauses step_nums 
   210       val clasimp_names = map (#1 o ResClause.clause_info o #1) clasimp_names_cls
   211   
   212       val _ = File.write (File.tmp_path (Path.basic "clasimp_names"))                                                               
   213                          (concat clasimp_names)
   214       val _ = (print_mode := (["xsymbols", "symbols"] @ ! print_mode))
   215    in
   216       clasimp_names
   217    end
   218     
   219 
   220 (***********************************************)
   221 (* get axioms for reconstruction               *)
   222 (***********************************************)
   223 fun numclstr (vars, []) str = str
   224 |   numclstr ( vars, ((num, thm)::rest)) str =
   225       let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
   226       in
   227         numclstr  (vars,rest) newstr
   228       end
   229 
   230 fun addvars c (a,b)  = (a,b,c)
   231 
   232  fun get_axioms_used proof_steps thms clause_arr num_of_clauses  =
   233      let 
   234 	val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   235 	val axioms = get_init_axioms proof_steps
   236 	val step_nums = get_step_nums axioms []
   237 
   238        (***********************************************)
   239        (* here need to add the clauses from clause_arr*)
   240        (***********************************************)
   241 
   242        (* val clasimp_names_cls = get_clasimp_cls clause_arr   num_of_clauses step_nums   
   243 	val clasimp_names = map #1 clasimp_names_cls
   244 	val clasimp_cls = map #2 clasimp_names_cls
   245 	val _ = TextIO.openAppend(File.platform_path(File.tmp_path (Path.basic "clasimp_names")))                                                            val clasimp_namestr = concat clasimp_names                            
   246 	 val _ = TextIO.output (outfile,clasimp_namestr)
   247 	 
   248 	 val _ =  TextIO.closeOut outfile
   249 *)
   250 
   251 	val clauses =(*(clasimp_cls)@*)( make_clauses thms)
   252 	
   253 	val vars = map thm_vars clauses
   254        
   255 	val distvars = distinct (fold append vars [])
   256 	val clause_terms = map prop_of clauses  
   257 	val clause_frees = List.concat (map term_frees clause_terms)
   258 
   259 	val frees = map lit_string_with_nums clause_frees;
   260 
   261 	val distfrees = distinct frees
   262 
   263 	val metas = map Meson.make_meta_clause clauses
   264 	val ax_strs = map #3 axioms
   265 
   266 	(* literals of -all- axioms, not just those used by spass *)
   267 	val meta_strs = map ReconOrderClauses.get_meta_lits metas
   268        
   269 	val metas_and_strs = ListPair.zip (metas,meta_strs)
   270 	val _ = File.write(File.tmp_path (Path.basic "foo_clauses")) (onestr ax_strs)
   271 	val _ = File.write(File.tmp_path (Path.basic "foo_metastrs")) (onestr meta_strs)
   272 
   273 	(* get list of axioms as thms with their variables *)
   274 
   275 	val ax_metas = get_assoc_snds ax_strs metas_and_strs []
   276 	val ax_vars = map thm_vars ax_metas
   277 	val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
   278 
   279 	(* get list of extra axioms as thms with their variables *)
   280 	val extra_metas = add_if_not_inlist metas ax_metas []
   281 	val extra_vars = map thm_vars extra_metas
   282 	val extra_with_vars = if (not (extra_metas = []) ) 
   283 			      then ListPair.zip (extra_metas,extra_vars)
   284 			      else []
   285        (* val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   286        val _ = TextIO.openOut(File.platform_path(File.tmp_path (Path.basic "foo_metas")))
   287 
   288        val _ = TextIO.output (outfile, ((thmstrings ax_metas "")))
   289        val _ =  TextIO.closeOut outfile
   290       val foo_metas =  File.platform_path(File.tmp_path (Path.basic "foo_metas"))
   291       val foo_metas2 =   File.platform_path(File.tmp_path (Path.basic "foo_metas2"))
   292 	val execperl = Unix.execute("/usr/bin/perl", ["remchars.pl", "  <", foo_metas, "  >", foo_metas2])
   293      val infile = TextIO.openIn(File.platform_path(File.tmp_path (Path.basic "foo_metas2")))
   294     val ax_metas_str = TextIO.inputLine (infile)
   295     val _ = TextIO.closeIn infile
   296        val _= (print_mode := (["xsymbols", "symbols"] @ ! print_mode))*)
   297      in
   298 	(distfrees,distvars, extra_with_vars,ax_with_vars, (*clasimp_names*)(ListPair.zip (step_nums,ax_metas)))
   299      end;
   300                                             
   301 
   302 (*********************************************************************)
   303 (* Pass in spass string of proof and string version of isabelle goal *)
   304 (* Get out reconstruction steps as a string to be sent to Isabelle   *)
   305 (*********************************************************************)
   306 
   307 
   308 fun rules_to_string [] str = str
   309 |   rules_to_string [x] str = str^x
   310 |   rules_to_string (x::xs) str = rules_to_string xs (str^x^"   ")
   311                                   
   312 
   313 val dummy_tac = PRIMITIVE(fn thm => thm );
   314 
   315 
   316 (*val  proofstr = "1242[0:Inp] ||  -> equal(const_List_Orev(tconst_fun(tconst_List_Olist(U),tconst_List_Olist(U)),const_List_Olist_ONil),const_List_Olist_ONil)**.\
   317 \1279[0:Inp] ||  -> equal(const_Nat_Osize(tconst_fun(tconst_List_Olist(U),tconst_nat),const_List_Olist_ONil),const_0)**.\
   318 \1430[0:Inp] || equal(const_Nat_Osize(tconst_fun(tconst_List_Olist(typ__Ha),tconst_nat),const_List_Orev(tconst_fun(tconst_List_Olist(typ__Ha),tconst_List_Olist(typ__Ha)),const_List_Olist_ONil)),const_Nat_Osize(tconst_fun(tconst_List_Olist(typ__Ha),tconst_nat),const_List_Olist_ONil))** -> .\
   319 \1453[0:Rew:1279.0,1430.0,1242.0,1430.0,1279.0,1430.0] || equal(const_0,const_0)* -> .\
   320 \1454[0:Obv:1453.0] ||  -> .";*)
   321 
   322 fun subst_for a b = String.translate (fn c => str (if c=a then b else c));
   323 
   324 val remove_linebreaks = subst_for #"\n" #"\t";
   325 val restore_linebreaks = subst_for #"\t" #"\n";
   326 
   327 fun spassString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
   328   let val _ = File.append(File.tmp_path (Path.basic"thmstringfile"))
   329                   ("thmstring is: "^thmstring^"\n proofstr is: "^proofstr^"\n goalstr is: "^goalstring^" \n  num of clauses is: "^(string_of_int num_of_clauses))
   330 
   331      (***********************************)
   332      (* parse spass proof into datatype *)
   333      (***********************************)
   334       val tokens = #1(lex proofstr)
   335       val proof_steps1 = parse tokens
   336       val proof_steps = just_change_space proof_steps1
   337       val _ = File.write (File.tmp_path (Path.basic "foo_parse")) ("Did parsing on "^proofstr)
   338       val _ = File.write(File.tmp_path (Path.basic "foo_thmstring_at_parse"))
   339                         ("Parsing for thmstring: "^thmstring)
   340       (************************************)
   341       (* recreate original subgoal as thm *)
   342       (************************************)
   343     
   344       (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
   345       (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
   346       (* subgoal this is, and turn it into meta_clauses *)
   347       (* should prob add array and table here, so that we can get axioms*)
   348       (* produced from the clasimpset rather than the problem *)
   349 
   350       val (axiom_names) = get_axiom_names proof_steps  thms clause_arr  num_of_clauses
   351       val ax_str = "Rules from clasimpset used in proof found by SPASS: "^(rules_to_string axiom_names "")
   352       val _ = File.append(File.tmp_path (Path.basic "reconstrfile"))
   353                          ("reconstring is: "^ax_str^"  "^goalstring)       
   354   in 
   355        TextIO.output (toParent, ax_str^"\n");
   356        TextIO.flushOut toParent;
   357        TextIO.output (toParent, "goalstring: "^goalstring^"\n");
   358        TextIO.flushOut toParent;
   359        TextIO.output (toParent, "thmstring: "^thmstring^"\n");
   360        TextIO.flushOut toParent;
   361 
   362        Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   363       (* Attempt to prevent several signals from turning up simultaneously *)
   364        Posix.Process.sleep(Time.fromSeconds 1) ; dummy_tac
   365   end
   366   handle _ => 
   367   let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
   368   in 
   369      TextIO.output (toParent,"Proof found but parsing failed for resolution proof: "^(remove_linebreaks proofstr)^"\n" );
   370       TextIO.flushOut toParent;
   371        TextIO.output (toParent, (remove_linebreaks thmstring)^"\n");
   372        TextIO.flushOut toParent;
   373        TextIO.output (toParent, ( (remove_linebreaks goalstring)^"\n"));
   374        TextIO.flushOut toParent;
   375       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   376       (* Attempt to prevent several signals from turning up simultaneously *)
   377       Posix.Process.sleep(Time.fromSeconds 1) ;dummy_tac
   378   end
   379 
   380 
   381 fun vampString_to_lemmaString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
   382  let val _ = File.append(File.tmp_path (Path.basic "thmstringfile"))
   383                (" thmstring is: "^thmstring^"proofstr is: "^proofstr^
   384                 "goalstr is: "^goalstring^" num of clauses is: "^
   385                 (string_of_int num_of_clauses))
   386 
   387     (***********************************)
   388     (* get vampire axiom names         *)
   389     (***********************************)
   390 
   391      val (axiom_names) = get_axiom_names_vamp proofstr  thms clause_arr  num_of_clauses
   392      val ax_str = "Rules from clasimpset used in proof found by Vampire: "^(rules_to_string axiom_names "")
   393     in 
   394 	 File.append(File.tmp_path (Path.basic "reconstrfile")) ("reconstring is: "^ax_str^"  "^goalstring);
   395          TextIO.output (toParent, ax_str^"\n");
   396 	 TextIO.flushOut toParent;
   397 	 TextIO.output (toParent, "goalstring: "^goalstring^"\n");
   398 	 TextIO.flushOut toParent;
   399 	 TextIO.output (toParent, "thmstring: "^thmstring^"\n");
   400 	 TextIO.flushOut toParent;
   401 
   402 	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   403 	(* Attempt to prevent several signals from turning up simultaneously *)
   404 	 Posix.Process.sleep(Time.fromSeconds 1) ; dummy_tac
   405     end
   406     handle _ => 
   407     let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
   408     in 
   409 	TextIO.output (toParent,"Proof found by Vampire but translation failed for resolution proof: "^(remove_linebreaks proofstr) );
   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 (* val proofstr = "1582[0:Inp] || -> v_P*.\
   422                  \1583[0:Inp] || v_P* -> .\
   423 		 \1584[0:MRR:1583.0,1582.0] || -> ."; *)
   424 
   425 fun spassString_to_reconString proofstr thmstring goalstring toParent ppid thms clause_arr  num_of_clauses  = 
   426   let val _ = File.write(File.tmp_path (Path.basic "thmstringfile")) 
   427                  (" thmstring is: "^thmstring^"proofstr is: "^proofstr)
   428       val tokens = #1(lex proofstr)
   429 
   430   (***********************************)
   431   (* parse spass proof into datatype *)
   432   (***********************************)
   433       val proof_steps1 = parse tokens
   434       val proof_steps = just_change_space proof_steps1
   435 
   436       val _ = File.write (File.tmp_path (Path.basic "foo_parse"))
   437                       ("Did parsing on "^proofstr)
   438     
   439       val _ = File.write (File.tmp_path (Path.basic "foo_thmstring_at_parse"))
   440                                 ("Parsing for thmstring: "^thmstring)
   441   (************************************)
   442   (* recreate original subgoal as thm *)
   443   (************************************)
   444       (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
   445       (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
   446       (* subgoal this is, and turn it into meta_clauses *)
   447       (* should prob add array and table here, so that we can get axioms*)
   448       (* produced from the clasimpset rather than the problem *)
   449       val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms clause_arr  num_of_clauses
   450       
   451       (*val numcls_string = numclstr ( vars, numcls) ""*)
   452       val _ = File.write (File.tmp_path (Path.basic "foo_axiom")) "got axioms"
   453 	
   454   (************************************)
   455   (* translate proof                  *)
   456   (************************************)
   457       val _ = File.write (File.tmp_path (Path.basic "foo_steps"))                                                                           
   458                        ("about to translate proof, steps: "
   459                        ^(init_proofsteps_to_string proof_steps ""))
   460       val (newthm,proof) = translate_proof numcls  proof_steps vars
   461       val _ = File.write (File.tmp_path (Path.basic "foo_steps2"))                                                                       
   462                        ("translated proof, steps: "^(init_proofsteps_to_string proof_steps ""))
   463   (***************************************************)
   464   (* transfer necessary steps as strings to Isabelle *)
   465   (***************************************************)
   466       (* turn the proof into a string *)
   467       val reconProofStr = proofs_to_string proof ""
   468       (* do the bit for the Isabelle ordered axioms at the top *)
   469       val ax_nums = map #1 numcls
   470       val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
   471       val numcls_strs = ListPair.zip (ax_nums,ax_strs)
   472       val num_cls_vars =  map (addvars vars) numcls_strs;
   473       val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
   474       
   475       val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars))
   476                        else []
   477       val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
   478       val frees_str = "["^(thmvars_to_string frees "")^"]"
   479       val _ = File.write (File.tmp_path (Path.basic "reconstringfile"))
   480                           (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
   481       val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
   482   in 
   483        TextIO.output (toParent, reconstr^"\n");
   484        TextIO.flushOut toParent;
   485        TextIO.output (toParent, thmstring^"\n");
   486        TextIO.flushOut toParent;
   487        TextIO.output (toParent, goalstring^"\n");
   488        TextIO.flushOut toParent;
   489 
   490        Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   491       (* Attempt to prevent several signals from turning up simultaneously *)
   492        Posix.Process.sleep(Time.fromSeconds 1) ; dummy_tac
   493   end
   494   handle _ => 
   495   let val _ = File.write(File.tmp_path (Path.basic "foo_handler")) "In exception handler"
   496   in 
   497        TextIO.output (toParent,"Proof found but translation failed for resolution proof:"^(remove_linebreaks proofstr) ^"\n");
   498       TextIO.flushOut toParent;
   499     TextIO.output (toParent, thmstring^"\n");
   500        TextIO.flushOut toParent;
   501        TextIO.output (toParent, goalstring^"\n");
   502        TextIO.flushOut toParent;
   503       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   504       (* Attempt to prevent several signals from turning up simultaneously *)
   505       Posix.Process.sleep(Time.fromSeconds 1) ;dummy_tac
   506   end
   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 (*FIX: ask Larry to add and mrr attribute *)
   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 ((MRR ((a,b), (c,d)))) = 
   753     "by(rule cl"^
   754 		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
   755 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   756 |   by_isar_line ( (Para ((a,b), (c,d)))) =
   757     "by (rule cl"^
   758 		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
   759 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   760 |   by_isar_line ((Factor ((a,b,c)))) = 
   761     "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
   762 		(string_of_int c)^" ])\n"
   763 (*|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
   764     "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
   765 		(string_of_int c)^" "^(string_of_int d)^" ])\n"*)
   766 |   by_isar_line ( (Obvious ((a,b)))) =
   767     "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
   768 
   769 fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
   770 
   771 
   772 fun isar_lines [] str = str
   773 |   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
   774 
   775 fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
   776 
   777 
   778 fun to_isar_proof (frees, xs, goalstring) =
   779     let val extraaxioms = get_extraaxioms xs
   780 	val extraax_num = length extraaxioms
   781 	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
   782 	
   783 	val origaxioms = get_origaxioms origaxioms_and_steps
   784 	val origax_num = length origaxioms
   785 	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
   786 	val axioms = get_axioms axioms_and_steps
   787 	
   788 	val steps = Library.drop (origax_num, axioms_and_steps)
   789 	val firststeps = ReconOrderClauses.butlast steps
   790 	val laststep = ReconOrderClauses.last steps
   791 	val goalstring = implode(ReconOrderClauses.butlast(explode goalstring))
   792 	
   793 	val isar_proof = 
   794 		("show \""^goalstring^"\"\n")^
   795 		("proof (rule ccontr,skolemize, make_clauses) \n")^
   796 		("fix "^(frees_to_isar_str frees)^"\n")^
   797 		(assume_isar_extraaxioms extraaxioms)^
   798 		(assume_isar_origaxioms origaxioms)^
   799 		(isar_axiomlines axioms "")^
   800 		(isar_lines firststeps "")^
   801 		(last_isar_line laststep)^
   802 		("qed")
   803 	val _ = File.write (File.tmp_path (Path.basic "isar_proof_file")) isar_proof
   804     in
   805 	isar_proof
   806     end;
   807 
   808 (* get fix vars from axioms - all Frees *)
   809 (* check each clause for meta-vars and /\ over them at each step*)
   810 
   811 (*******************************************************)
   812 (* This assumes the thm list  "numcls" is still there  *)
   813 (* In reality, should probably label it with an        *)
   814 (* ID number identifying the subgoal.  This could      *)
   815 (* be passed over to the watcher, e.g.  numcls25       *)
   816 (*******************************************************)
   817 
   818 (* 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))[][]";
   819 
   820 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))[][]";
   821 
   822 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)";
   823 
   824 
   825 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))[][]";
   826 
   827 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)))";
   828 *)
   829 
   830 fun apply_res_thm str goalstring  = let val tokens = #1 (lex str);
   831 				val _ = File.append (File.tmp_path (Path.basic "applyres")) 
   832 			           ("str is: "^str^" goalstr is: "^goalstring^"\n")	
   833                                    val (frees,recon_steps) = parse_step tokens 
   834                                    val isar_str = to_isar_proof (frees, recon_steps, goalstring)
   835                                    val foo =   TextIO.openOut (File.platform_path(File.tmp_path (Path.basic "foobar")));
   836                                in 
   837                                   TextIO.output(foo,(isar_str));TextIO.closeOut foo;Pretty.writeln(Pretty.str  isar_str); () 
   838                                end 
   839 
   840 
   841 (* val reconstr = reconstr^"[P%]51Axiom()[~P%]52Axiom[P%][]53MRR((52,0),(51,0))[][]";
   842 *)
   843 end;