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