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