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