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