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