src/HOL/Tools/ATP/recon_transfer_proof.ML
author paulson
Thu Sep 29 12:45:16 2005 +0200 (2005-09-29)
changeset 17718 9dab1e491d10
parent 17690 8ba7c3cd24a8
child 17746 af59c748371d
permissions -rw-r--r--
reduction in tracing files
     1 (*  ID:         $Id$
     2     Author:     Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 structure Recon_Transfer =
     7 struct
     8 
     9 open Recon_Parse
    10 
    11 infixr 8 ++; infixr 7 >>; infixr 6 ||;
    12 
    13 val trace_path = Path.basic "transfer_trace";
    14 
    15 fun trace s = if !Output.show_debug_msgs then File.append (File.tmp_path trace_path) s 
    16               else ();
    17 
    18 
    19 (* Versions that include type information *)
    20  
    21 (* FIXME rename to str_of_thm *)
    22 fun string_of_thm thm =
    23   setmp show_sorts true (Pretty.str_of o Display.pretty_thm) thm;
    24 
    25 
    26 (* check separate args in the watcher program for separating strings with a * or ; or something *)
    27 
    28 fun clause_strs_to_string [] str = str
    29 |   clause_strs_to_string (x::xs) str = clause_strs_to_string xs (str^x^"%")
    30 
    31 fun thmvars_to_string [] str = str
    32 |   thmvars_to_string (x::xs) str = thmvars_to_string xs (str^x^"%")
    33 
    34 
    35 fun proofstep_to_string Axiom = "Axiom()"
    36 |   proofstep_to_string  (Binary ((a,b), (c,d)))=
    37       "Binary(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    38 |   proofstep_to_string (Factor (a,b,c)) =
    39       "Factor("^(string_of_int a)^","^(string_of_int b)^","^(string_of_int c)^")"
    40 |   proofstep_to_string  (Para ((a,b), (c,d)))= 
    41       "Para(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    42 |   proofstep_to_string  (MRR ((a,b), (c,d))) =
    43       "MRR(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"
    44 (*|   proofstep_to_string (Rewrite((a,b),(c,d))) =
    45       "Rewrite(("^(string_of_int a)^","^(string_of_int b)^"),("^(string_of_int c)^","^(string_of_int d)^"))"*)
    46 
    47 
    48 fun proof_to_string (num,(step,clause_strs, thmvars)) =
    49  (string_of_int num)^(proofstep_to_string step)^
    50  "["^(clause_strs_to_string clause_strs "")^"]["^(thmvars_to_string thmvars "")^"]"
    51  
    52 
    53 fun proofs_to_string [] str = str
    54 |   proofs_to_string (x::xs) str = let val newstr = proof_to_string x 
    55                                    in
    56                                        proofs_to_string xs (str^newstr)
    57                                    end
    58 
    59 
    60 
    61 fun init_proofstep_to_string (num, step, clause_strs) =
    62  (string_of_int num)^" "^(proofstep_to_string step)^" "^
    63  (clause_strs_to_string clause_strs "")^" "
    64 
    65 fun init_proofsteps_to_string [] str = str
    66 |   init_proofsteps_to_string (x::xs) str = let val newstr = init_proofstep_to_string x 
    67                                    in
    68                                        init_proofsteps_to_string xs (str^newstr)
    69                                    end
    70   
    71 
    72 
    73 (*** get a string representing the Isabelle ordered axioms ***)
    74 
    75 fun origAx_to_string (num,(meta,thmvars)) =
    76     let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
    77     in
    78        (string_of_int num)^"OrigAxiom()["^
    79        (clause_strs_to_string clause_strs "")^"]["^
    80        (thmvars_to_string thmvars "")^"]"
    81     end
    82 
    83 
    84 fun  origAxs_to_string [] str = str
    85 |   origAxs_to_string (x::xs) str = let val newstr = origAx_to_string x 
    86                                    in
    87                                        origAxs_to_string xs (str^newstr)
    88                                    end
    89 
    90 
    91 (*** get a string representing the Isabelle ordered axioms not used in the spass proof***)
    92 
    93 fun extraAx_to_string (num, (meta,thmvars)) =
    94    let val clause_strs = ReconOrderClauses.get_meta_lits_bracket meta
    95    in
    96       (string_of_int num)^"ExtraAxiom()["^
    97       (clause_strs_to_string clause_strs "")^"]"^
    98       "["^(thmvars_to_string thmvars "")^"]"
    99    end;
   100 
   101 fun extraAxs_to_string [] str = str
   102 |   extraAxs_to_string (x::xs) str =
   103       let val newstr = extraAx_to_string x 
   104       in
   105 	  extraAxs_to_string xs (str^newstr)
   106       end;
   107 
   108 fun is_axiom (_,Axiom,str) = true
   109 |   is_axiom (_,_,_) = false
   110 
   111 fun get_step_nums [] nums = nums
   112 |   get_step_nums (( num:int,Axiom, str)::xs) nums = get_step_nums xs (nums@[num])
   113 
   114 exception Noassoc;
   115 
   116 fun assoc_snd a [] = raise Noassoc
   117   | assoc_snd a ((x, y)::t) = if a = y then x else assoc_snd a t;
   118 
   119 (* change to be something using check_order  instead of a = y --> returns true if ASSERTION not raised in checkorder, false otherwise *)
   120 
   121 (*fun get_assoc_snds [] xs assocs= assocs
   122 |   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_snd x ys))])
   123 *)
   124 (*FIX - should this have vars in it? *)
   125 fun there_out_of_order xs ys = (ReconOrderClauses.checkorder xs ys [] ([],[],[]); true) 
   126                                handle _ => false
   127 
   128 fun assoc_out_of_order a [] = raise Noassoc
   129 |   assoc_out_of_order a ((b,c)::t) = if there_out_of_order a c then b else assoc_out_of_order a t;
   130 
   131 fun get_assoc_snds [] xs assocs= assocs
   132 |   get_assoc_snds (x::xs) ys assocs = get_assoc_snds xs ys (assocs@[((assoc_out_of_order x ys))])
   133 
   134 fun add_if_not_inlist [] xs newlist = newlist
   135 |   add_if_not_inlist (y::ys) xs newlist = if (not (y mem xs)) then 
   136                                       add_if_not_inlist ys xs (y::newlist)
   137                                         else add_if_not_inlist ys xs (newlist)
   138 
   139 (*Flattens a list of list of strings to one string*)
   140 fun onestr ls = String.concat (map String.concat ls);
   141 
   142 fun is_clasimp_ax clasimp_num n = n <= clasimp_num 
   143 
   144 fun subone x = x - 1
   145 
   146 fun numstr [] = ""
   147 |   numstr (x::xs) = (string_of_int x)^"%"^(numstr xs)
   148 
   149 
   150 (* retrieve the axioms that were obtained from the clasimpset *)
   151 
   152 fun get_clasimp_cls (clause_arr: (ResClause.clause * thm) array) step_nums = 
   153     let val clasimp_nums = List.filter (is_clasimp_ax (Array.length clause_arr - 1)) 
   154 	                   (map subone step_nums)
   155     in
   156 	map (fn x =>  Array.sub(clause_arr, x)) clasimp_nums
   157     end
   158 
   159 
   160 (*****************************************************)
   161 (* get names of clasimp axioms used                  *)
   162 (*****************************************************)
   163 
   164  fun get_axiom_names step_nums clause_arr =
   165    let 
   166      (* not sure why this is necessary again, but seems to be *)
   167       val _ = (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   168   
   169      (***********************************************)
   170      (* here need to add the clauses from clause_arr*)
   171      (***********************************************)
   172   
   173       val clasimp_names_cls = get_clasimp_cls clause_arr step_nums 
   174       val clasimp_names = map (ResClause.get_axiomName o #1) clasimp_names_cls
   175       val _ = (print_mode := (["xsymbols", "symbols"] @ ! print_mode))
   176    in
   177       clasimp_names
   178    end
   179    
   180 
   181 fun get_axiom_names_spass proofstr clause_arr =
   182   let (* parse spass proof into datatype *)
   183       val _ = trace ("\nStarted parsing:\n" ^ proofstr)
   184       val proof_steps = parse (#1(lex proofstr))
   185       val _ = trace "\nParsing finished!"
   186       (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
   187   in
   188     get_axiom_names (get_step_nums (List.filter is_axiom proof_steps) []) clause_arr
   189   end;
   190     
   191  (*String contains multiple lines.
   192   A list consisting of the first number in each line is returned. *)
   193 fun get_linenums proofstr = 
   194   let val numerics = String.tokens (not o Char.isDigit)
   195       fun firstno [] = NONE
   196         | firstno (x::xs) = Int.fromString x
   197       val lines = String.tokens (fn c => c = #"\n") proofstr
   198   in  List.mapPartial (firstno o numerics) lines  end
   199 
   200 fun get_axiom_names_e proofstr clause_arr  =
   201    get_axiom_names (get_linenums proofstr) clause_arr;
   202     
   203  (*String contains multiple lines. We want those of the form 
   204      "*********** [448, input] ***********".
   205   A list consisting of the first number in each line is returned. *)
   206 fun get_vamp_linenums proofstr = 
   207   let val toks = String.tokens (not o Char.isAlphaNum)
   208       fun inputno [n,"input"] = Int.fromString n
   209         | inputno _ = NONE
   210       val lines = String.tokens (fn c => c = #"\n") proofstr
   211   in  List.mapPartial (inputno o toks) lines  end
   212 
   213 fun get_axiom_names_vamp proofstr clause_arr  =
   214    get_axiom_names (get_vamp_linenums proofstr) clause_arr;
   215     
   216 
   217 (***********************************************)
   218 (* get axioms for reconstruction               *)
   219 (***********************************************)
   220 fun numclstr (vars, []) str = str
   221 |   numclstr ( vars, ((num, thm)::rest)) str =
   222       let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
   223       in
   224         numclstr  (vars,rest) newstr
   225       end
   226 
   227 fun addvars c (a,b)  = (a,b,c)
   228 
   229 fun get_axioms_used proof_steps thms clause_arr  =
   230   let 
   231      val _=  (print_mode := (Library.gen_rems (op =) (! print_mode, ["xsymbols", "symbols"])))
   232      val axioms = (List.filter is_axiom) proof_steps
   233      val step_nums = get_step_nums axioms []
   234 
   235      val clauses = make_clauses thms    (*FIXME: must this be repeated??*)
   236      
   237      val vars = map thm_vars clauses
   238     
   239      val distvars = distinct (fold append vars [])
   240      val clause_terms = map prop_of clauses  
   241      val clause_frees = List.concat (map term_frees clause_terms)
   242 
   243      val frees = map lit_string_with_nums clause_frees;
   244 
   245      val distfrees = distinct frees
   246 
   247      val metas = map Meson.make_meta_clause clauses
   248      val ax_strs = map #3 axioms
   249 
   250      (* literals of -all- axioms, not just those used by spass *)
   251      val meta_strs = map ReconOrderClauses.get_meta_lits metas
   252     
   253      val metas_and_strs = ListPair.zip (metas,meta_strs)
   254      val _ = trace ("\nAxioms: " ^ onestr ax_strs)
   255      val _ = trace ("\nMeta_strs: " ^ onestr meta_strs)
   256 
   257      (* get list of axioms as thms with their variables *)
   258 
   259      val ax_metas = get_assoc_snds ax_strs metas_and_strs []
   260      val ax_vars = map thm_vars ax_metas
   261      val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
   262 
   263      (* get list of extra axioms as thms with their variables *)
   264      val extra_metas = add_if_not_inlist metas ax_metas []
   265      val extra_vars = map thm_vars extra_metas
   266      val extra_with_vars = if (not (extra_metas = []) ) 
   267 			   then ListPair.zip (extra_metas,extra_vars)
   268 			   else []
   269   in
   270      (distfrees,distvars, extra_with_vars,ax_with_vars, ListPair.zip (step_nums,ax_metas))
   271   end;
   272                                             
   273 
   274 (*********************************************************************)
   275 (* Pass in spass string of proof and string version of isabelle goal *)
   276 (* Get out reconstruction steps as a string to be sent to Isabelle   *)
   277 (*********************************************************************)
   278 
   279 fun rules_to_string [] = "NONE"
   280   | rules_to_string xs = "[" ^ space_implode ", " xs ^ "]"
   281 
   282 fun subst_for a b = String.translate (fn c => str (if c=a then b else c));
   283 
   284 val remove_linebreaks = subst_for #"\n" #"\t";
   285 val restore_linebreaks = subst_for #"\t" #"\n";
   286 
   287 
   288 fun prover_lemma_list_aux getax proofstr goalstring toParent ppid clause_arr = 
   289  let val _ = trace
   290                ("\nGetting lemma names. proofstr is " ^ proofstr ^
   291                 "\ngoalstr is " ^ goalstring ^
   292                 "\nnum of clauses is " ^ string_of_int (Array.length clause_arr))
   293      val axiom_names = getax proofstr clause_arr
   294      val ax_str = rules_to_string axiom_names
   295     in 
   296 	 trace ("\nDone. Lemma list is " ^ ax_str);
   297          TextIO.output (toParent, "Success. Lemmas used in automatic proof: " ^
   298                   ax_str ^ "\n");
   299 	 TextIO.output (toParent, "goalstring: "^goalstring^"\n");
   300 	 TextIO.flushOut toParent;
   301 	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2)
   302     end
   303     handle exn => (*FIXME: exn handler is too general!*)
   304      (trace ("\nprover_lemma_list_aux: In exception handler: " ^ 
   305              Toplevel.exn_message exn);
   306       TextIO.output (toParent, "Translation failed for the proof: " ^ 
   307                      remove_linebreaks proofstr ^ "\n");
   308       TextIO.output (toParent, remove_linebreaks goalstring ^ "\n");
   309       TextIO.flushOut toParent;
   310       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2));
   311 
   312 val e_lemma_list = prover_lemma_list_aux get_axiom_names_e;
   313 
   314 val vamp_lemma_list = prover_lemma_list_aux get_axiom_names_vamp;
   315 
   316 val spass_lemma_list = prover_lemma_list_aux get_axiom_names_spass;
   317 
   318 
   319 (**** Full proof reconstruction for SPASS (not really working) ****)
   320 
   321 fun spass_reconstruct proofstr goalstring toParent ppid thms clause_arr = 
   322   let val _ = trace ("\nspass_reconstruct. Proofstr is "^proofstr)
   323       val tokens = #1(lex proofstr)
   324 
   325   (* parse spass proof into datatype *)
   326   (***********************************)
   327       val proof_steps = parse tokens
   328       val _ = trace "\nParsing finished"
   329     
   330   (************************************)
   331   (* recreate original subgoal as thm *)
   332   (************************************)
   333       (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
   334       (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
   335       (* subgoal this is, and turn it into meta_clauses *)
   336       (* should prob add array and table here, so that we can get axioms*)
   337       (* produced from the clasimpset rather than the problem *)
   338       val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms clause_arr
   339       
   340       (*val numcls_string = numclstr ( vars, numcls) ""*)
   341       val _ = trace "\ngot axioms"
   342 	
   343   (************************************)
   344   (* translate proof                  *)
   345   (************************************)
   346       val _ = trace ("\nabout to translate proof, steps: "
   347                        ^ (init_proofsteps_to_string proof_steps ""))
   348       val (newthm,proof) = translate_proof numcls  proof_steps vars
   349       val _ = trace ("translated proof, steps: "^(init_proofsteps_to_string proof_steps ""))
   350   (***************************************************)
   351   (* transfer necessary steps as strings to Isabelle *)
   352   (***************************************************)
   353       (* turn the proof into a string *)
   354       val reconProofStr = proofs_to_string proof ""
   355       (* do the bit for the Isabelle ordered axioms at the top *)
   356       val ax_nums = map #1 numcls
   357       val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
   358       val numcls_strs = ListPair.zip (ax_nums,ax_strs)
   359       val num_cls_vars =  map (addvars vars) numcls_strs;
   360       val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
   361       
   362       val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars))
   363                        else []
   364       val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
   365       val frees_str = "["^(thmvars_to_string frees "")^"]"
   366       val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
   367       val _ = trace ("\nReconstruction:\n" ^ reconstr)
   368   in 
   369        TextIO.output (toParent, reconstr^"\n");
   370        TextIO.output (toParent, goalstring^"\n");
   371        TextIO.flushOut toParent;
   372        Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   373        all_tac
   374   end
   375   handle exn => (*FIXME: exn handler is too general!*)
   376    (trace ("\nspass_reconstruct. In exception handler: " ^ Toplevel.exn_message exn);
   377     TextIO.output (toParent,"Translation failed for the proof:"^
   378          (remove_linebreaks proofstr) ^"\n");
   379     TextIO.output (toParent, goalstring^"\n");
   380     TextIO.flushOut toParent;
   381     Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2); all_tac)
   382 
   383 (**********************************************************************************)
   384 (* At other end, want to turn back into datatype so can apply reconstruct_proof.  *)
   385 (* This will be done by the signal handler                                        *)
   386 (**********************************************************************************)
   387 
   388 (* Parse in the string version of the proof steps for reconstruction *)
   389 (* Isar format: cl1 [BINARY 0 cl2 0];cl1 [PARAMOD 0 cl2 0]; cl1 [DEMOD 0 cl2];cl1 [FACTOR 1 2];*)
   390 
   391 
   392  val term_numstep =
   393         (number ++ (a (Other ",")) ++ number) >> (fn (a, (_, c)) => (a, c))
   394 
   395 val extraaxiomstep = (a (Word "ExtraAxiom"))++ (a (Other "(")) ++(a (Other ")"))
   396             >> (fn (_) => ExtraAxiom)
   397 
   398 
   399 
   400 val origaxiomstep = (a (Word "OrigAxiom"))++ (a (Other "(")) ++(a (Other ")"))
   401             >> (fn (_) => OrigAxiom)
   402 
   403 
   404  val axiomstep = (a (Word "Axiom"))++ (a (Other "(")) ++(a (Other ")"))
   405             >> (fn (_) => Axiom)
   406      
   407 
   408 
   409       
   410  val binarystep = (a (Word "Binary")) ++ (a (Other "(")) ++ (a (Other "(")) 
   411                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   412                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   413             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Binary (c,e))
   414       
   415 
   416  val parastep = (a (Word "Para")) ++ (a (Other "(")) ++ (a (Other "(")) 
   417                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   418                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   419             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Para(c, e))
   420       
   421  val mrrstep = (a (Word "MRR")) ++ (a (Other "(")) ++ (a (Other "(")) 
   422                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   423                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   424             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => MRR(c, e))
   425       
   426 
   427  val factorstep = (a (Word "Factor")) ++ (a (Other "("))
   428                     ++ number ++ (a (Other ","))
   429                        ++ number ++ (a (Other ","))
   430                        ++ number ++  (a (Other ")"))
   431                    
   432             >> (fn (_, (_, (c, (_, (e,(_,(f,_))))))) =>  Factor (c,e,f))
   433 
   434 
   435 (*val rewritestep = (a (Word "Rewrite"))  ++ (a (Other "(")) ++ (a (Other "(")) 
   436                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   437                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   438             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Rewrite (c,e))*)
   439 
   440 val obviousstep = (a (Word "Obvious")) ++ (a (Other "(")) 
   441                    ++ term_numstep  ++ (a (Other ")")) 
   442             >> (fn (_, (_, (c,_))) => Obvious (c))
   443 
   444  val methodstep = extraaxiomstep || origaxiomstep || axiomstep ||binarystep || factorstep|| parastep || mrrstep || (*rewritestep ||*) obviousstep
   445 
   446 
   447  val number_list_step =
   448         ( number ++ many ((a (Other ",") ++ number)>> #2))
   449         >> (fn (a,b) => (a::b))
   450         
   451  val numberlist_step = a (Other "[")  ++ a (Other "]")
   452                         >>(fn (_,_) => ([]:int list))
   453                        || a (Other "[") ++ number_list_step ++ a (Other "]")
   454                         >>(fn (_,(a,_)) => a)
   455                     
   456 
   457 
   458 (** change this to allow P (x U) *)
   459  fun arglist_step input = 
   460    ( word ++ many word >> (fn (a, b) => (a^" "^(space_implode " " b)))
   461     ||word >> (fn (a) => (a)))input
   462                 
   463 
   464 fun literal_step input = (word ++ a (Other "(") ++ arglist_step ++  a (Other ")")
   465                                           >>(fn (a, (b, (c,d))) => (a^" ("^(c)^")"))
   466                         || arglist_step >> (fn (a) => (a)))input
   467                            
   468 
   469 
   470 (* fun term_step input = (a (Other "~") ++ arglist_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
   471                      ||  arglist_step ++ a (Other "%")>> (fn (a,b) => a ))input
   472 *)
   473 
   474 
   475  fun term_step input = (a (Other "~") ++ literal_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
   476                      ||  literal_step ++ a (Other "%")>> (fn (a,b) => a ))input
   477 
   478 
   479          
   480 
   481  val term_list_step =
   482         (  term_step ++ many ( term_step))
   483         >> (fn (a,b) => (a::b))
   484         
   485  
   486 val term_lists_step = a (Other "[")  ++ a (Other "]")
   487                         >>(fn (_,_) => ([]:string list))
   488                        || a (Other "[") ++ term_list_step ++ a (Other "]")
   489                         >>(fn (_,(a,_)) => a)
   490                      
   491 
   492 
   493 
   494 fun anytoken_step input  = (word>> (fn (a) => a)  ) input
   495                        handle NOPARSE_WORD => (number>> (fn (a) => string_of_int a)  ) input
   496                       handle NOPARSE_NUMBER => (other_char >> (fn(a) => a)) input
   497 
   498 
   499 
   500 fun goalstring_step input= (anytoken_step ++ many (anytoken_step )
   501                   >> (fn (a,b) =>  (a^" "^(implode b)))) input
   502 
   503 
   504 
   505  val linestep = number ++ methodstep ++ term_lists_step ++ term_lists_step
   506                 >> (fn (a, (b, (c,d))) => (a,(b,c,d)))
   507     
   508  val lines_step = many linestep
   509 
   510  val alllines_step = (term_lists_step ++ lines_step ) ++ finished >> #1
   511     
   512  val parse_step = #1 o alllines_step
   513 
   514 
   515  (*
   516 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)"
   517 *)
   518 
   519 (************************************************************)
   520 (* Construct an Isar style proof from a list of proof steps *)
   521 (************************************************************)
   522 (* want to assume all axioms, then do haves for the other clauses*)
   523 (* then show for the last step *)
   524 
   525 (* replace ~ by not here *)
   526 val change_nots = String.translate (fn c => if c = #"~" then "\\<not>" else str c);
   527 
   528 fun clstrs_to_string xs = space_implode "; " (map change_nots xs);
   529 
   530 fun thmvars_to_quantstring [] str = str
   531 |   thmvars_to_quantstring (x::[]) str =str^x^". "
   532 |   thmvars_to_quantstring (x::xs) str = thmvars_to_quantstring xs (str^(x^" "))
   533 
   534 
   535 fun clause_strs_to_isar clstrs [] =
   536       "\"\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
   537 |   clause_strs_to_isar clstrs thmvars =
   538       "\"\\<And>"^(thmvars_to_quantstring thmvars "")^
   539       "\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
   540 
   541 fun frees_to_isar_str clstrs = space_implode " " (map change_nots clstrs)
   542 
   543 
   544 (***********************************************************************)
   545 (* functions for producing assumptions for the Isabelle ordered axioms *)
   546 (***********************************************************************)
   547 (*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))[][]";       
   548 num, rule, clausestrs, vars*)
   549 
   550 
   551 (* assume the extra clauses - not used in Spass proof *)
   552 
   553 fun is_extraaxiom_step ( num:int,(ExtraAxiom, str, tstr)) = true
   554 |   is_extraaxiom_step (num, _) = false
   555 
   556 fun get_extraaxioms xs = List.filter (is_extraaxiom_step) ( xs)
   557 
   558 fun assume_isar_extraaxiom [] str  = str
   559 |   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 " )
   560 
   561 
   562 
   563 fun assume_isar_extraaxioms  [] = ""
   564 |assume_isar_extraaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
   565                                          in
   566                                              assume_isar_extraaxiom xs str
   567                                          end
   568 
   569 (* assume the Isabelle ordered clauses *)
   570 
   571 fun is_origaxiom_step ( num:int,(OrigAxiom, str, tstr)) = true
   572 |   is_origaxiom_step (num, _) = false
   573 
   574 fun get_origaxioms xs = List.filter (is_origaxiom_step) ( xs)
   575 
   576 fun assume_isar_origaxiom [] str  = str
   577 |   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 " )
   578 
   579 
   580 
   581 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" 
   582                                          in
   583                                              assume_isar_origaxiom xs str
   584                                          end
   585 
   586 
   587 
   588 fun is_axiom_step ( num:int,(Axiom, str, tstr)) = true
   589 |   is_axiom_step (num, _) = false
   590 
   591 fun get_axioms xs = List.filter  (is_axiom_step) ( xs)
   592 
   593 fun have_isar_axiomline (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
   594 
   595 fun  by_isar_axiomline (numb,(step, clstrs, thmstrs))="by (rule cl"^ (string_of_int numb)^"') \n"
   596 
   597 
   598 fun isar_axiomline (numb, (step, clstrs, thmstrs))  = (have_isar_axiomline (numb,(step,clstrs, thmstrs )))^( by_isar_axiomline(numb,(step,clstrs, thmstrs )) )
   599 
   600 
   601 fun isar_axiomlines [] str = str
   602 |   isar_axiomlines (x::xs) str = isar_axiomlines xs (str^(isar_axiomline x))
   603 
   604 
   605 fun have_isar_line (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
   606 (*FIX: ask Larry to add and mrr attribute *)
   607 
   608 fun by_isar_line ((Binary ((a,b), (c,d)))) = 
   609     "by(rule cl"^
   610 		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
   611 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   612 |by_isar_line ((MRR ((a,b), (c,d)))) = 
   613     "by(rule cl"^
   614 		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
   615 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   616 |   by_isar_line ( (Para ((a,b), (c,d)))) =
   617     "by (rule cl"^
   618 		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
   619 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   620 |   by_isar_line ((Factor ((a,b,c)))) = 
   621     "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
   622 		(string_of_int c)^" ])\n"
   623 (*|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
   624     "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
   625 		(string_of_int c)^" "^(string_of_int d)^" ])\n"*)
   626 |   by_isar_line ( (Obvious ((a,b)))) =
   627     "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
   628 
   629 fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
   630 
   631 
   632 fun isar_lines [] str = str
   633 |   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
   634 
   635 fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
   636 
   637 
   638 fun to_isar_proof (frees, xs, goalstring) =
   639     let val extraaxioms = get_extraaxioms xs
   640 	val extraax_num = length extraaxioms
   641 	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
   642 	
   643 	val origaxioms = get_origaxioms origaxioms_and_steps
   644 	val origax_num = length origaxioms
   645 	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
   646 	val axioms = get_axioms axioms_and_steps
   647 	
   648 	val steps = Library.drop (origax_num, axioms_and_steps)
   649 	val firststeps = ReconOrderClauses.butlast steps
   650 	val laststep = List.last steps
   651 	val goalstring = implode(ReconOrderClauses.butlast(explode goalstring))
   652 	
   653 	val isar_proof = 
   654 		("show \""^goalstring^"\"\n")^
   655 		("proof (rule ccontr,skolemize, make_clauses) \n")^
   656 		("fix "^(frees_to_isar_str frees)^"\n")^
   657 		(assume_isar_extraaxioms extraaxioms)^
   658 		(assume_isar_origaxioms origaxioms)^
   659 		(isar_axiomlines axioms "")^
   660 		(isar_lines firststeps "")^
   661 		(last_isar_line laststep)^
   662 		("qed")
   663 	val _ = trace ("\nto_isar_proof returns " ^ isar_proof)
   664     in
   665 	isar_proof
   666     end;
   667 
   668 (* get fix vars from axioms - all Frees *)
   669 (* check each clause for meta-vars and /\ over them at each step*)
   670 
   671 (*******************************************************)
   672 (* This assumes the thm list  "numcls" is still there  *)
   673 (* In reality, should probably label it with an        *)
   674 (* ID number identifying the subgoal.  This could      *)
   675 (* be passed over to the watcher, e.g.  numcls25       *)
   676 (*******************************************************)
   677 
   678 fun apply_res_thm str goalstring  = 
   679   let val tokens = #1 (lex str);
   680       val _ = trace ("\napply_res_thm. str is: "^str^" goalstr is: "^goalstring^"\n")	
   681       val (frees,recon_steps) = parse_step tokens 
   682   in 
   683       to_isar_proof (frees, recon_steps, goalstring)
   684   end 
   685 
   686 end;