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