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