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