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