src/HOL/Tools/ATP/recon_transfer_proof.ML
author haftmann
Tue May 09 10:09:37 2006 +0200 (2006-05-09)
changeset 19599 a5c7eb37d14f
parent 19199 b338c218cc6e
child 19617 7cb4b67d4b97
permissions -rw-r--r--
added DatatypeHooks
     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 (names_arr: string array) step_nums = 
   153     let val clasimp_nums = List.filter (is_clasimp_ax (Array.length names_arr - 1)) 
   154 	                   (map subone step_nums)
   155     in
   156 	map (fn x =>  Array.sub(names_arr, x)) clasimp_nums
   157     end
   158 
   159 (* get names of clasimp axioms used*)
   160 fun get_axiom_names step_nums names_arr =
   161   sort_distinct string_ord
   162     (get_clasimp_cls names_arr step_nums);
   163 
   164  (*String contains multiple lines. We want those of the form 
   165      "253[0:Inp] et cetera..."
   166   A list consisting of the first number in each line is returned. *)
   167 fun get_spass_linenums proofstr = 
   168   let val toks = String.tokens (not o Char.isAlphaNum)
   169       fun inputno (ntok::"0"::"Inp"::_) = Int.fromString ntok
   170         | inputno _ = NONE
   171       val lines = String.tokens (fn c => c = #"\n") proofstr
   172   in  List.mapPartial (inputno o toks) lines  end
   173 
   174 fun get_axiom_names_spass proofstr names_arr  =
   175    get_axiom_names (get_spass_linenums proofstr) names_arr;
   176     
   177  (*String contains multiple lines.
   178   A list consisting of the first number in each line is returned. *)
   179 fun get_linenums proofstr = 
   180   let val numerics = String.tokens (not o Char.isDigit)
   181       fun firstno [] = NONE
   182         | firstno (x::xs) = Int.fromString x
   183       val lines = String.tokens (fn c => c = #"\n") proofstr
   184   in  List.mapPartial (firstno o numerics) lines  end
   185 
   186 fun get_axiom_names_e proofstr names_arr  =
   187    get_axiom_names (get_linenums proofstr) names_arr;
   188     
   189  (*String contains multiple lines. We want those of the form 
   190      "*********** [448, input] ***********".
   191   A list consisting of the first number in each line is returned. *)
   192 fun get_vamp_linenums proofstr = 
   193   let val toks = String.tokens (not o Char.isAlphaNum)
   194       fun inputno [ntok,"input"] = Int.fromString ntok
   195         | inputno _ = NONE
   196       val lines = String.tokens (fn c => c = #"\n") proofstr
   197   in  List.mapPartial (inputno o toks) lines  end
   198 
   199 fun get_axiom_names_vamp proofstr names_arr  =
   200    get_axiom_names (get_vamp_linenums proofstr) names_arr;
   201     
   202 
   203 (***********************************************)
   204 (* get axioms for reconstruction               *)
   205 (***********************************************)
   206 fun numclstr (vars, []) str = str
   207 |   numclstr ( vars, ((num, thm)::rest)) str =
   208       let val newstr = str^(string_of_int num)^" "^(string_of_thm thm)^" "
   209       in
   210         numclstr  (vars,rest) newstr
   211       end
   212 
   213 fun addvars c (a,b)  = (a,b,c)
   214 
   215 fun get_axioms_used proof_steps thms names_arr  =
   216  let val axioms = (List.filter is_axiom) proof_steps
   217      val step_nums = get_step_nums axioms []
   218 
   219      val clauses = make_clauses thms    (*FIXME: must this be repeated??*)
   220      
   221      val vars = map thm_vars clauses
   222     
   223      val distvars = distinct (op =) (fold append vars [])
   224      val clause_terms = map prop_of clauses  
   225      val clause_frees = List.concat (map term_frees clause_terms)
   226 
   227      val frees = map lit_string_with_nums clause_frees;
   228 
   229      val distfrees = distinct (op =) frees
   230 
   231      val metas = map Meson.make_meta_clause clauses
   232      val ax_strs = map #3 axioms
   233 
   234      (* literals of -all- axioms, not just those used by spass *)
   235      val meta_strs = map ReconOrderClauses.get_meta_lits metas
   236     
   237      val metas_and_strs = ListPair.zip (metas,meta_strs)
   238      val _ = trace ("\nAxioms: " ^ onestr ax_strs)
   239      val _ = trace ("\nMeta_strs: " ^ onestr meta_strs)
   240 
   241      (* get list of axioms as thms with their variables *)
   242 
   243      val ax_metas = get_assoc_snds ax_strs metas_and_strs []
   244      val ax_vars = map thm_vars ax_metas
   245      val ax_with_vars = ListPair.zip (ax_metas,ax_vars)
   246 
   247      (* get list of extra axioms as thms with their variables *)
   248      val extra_metas = add_if_not_inlist metas ax_metas []
   249      val extra_vars = map thm_vars extra_metas
   250      val extra_with_vars = if (not (extra_metas = []) ) 
   251 			   then ListPair.zip (extra_metas,extra_vars)
   252 			   else []
   253  in
   254     (distfrees,distvars, extra_with_vars,ax_with_vars, ListPair.zip (step_nums,ax_metas))
   255  end;
   256                                             
   257 
   258 (*********************************************************************)
   259 (* Pass in spass string of proof and string version of isabelle goal *)
   260 (* Get out reconstruction steps as a string to be sent to Isabelle   *)
   261 (*********************************************************************)
   262 
   263 fun rules_to_string [] = "NONE"
   264   | rules_to_string xs = space_implode "  " xs
   265 
   266 
   267 (*The signal handler in watcher.ML must be able to read the output of this.*)
   268 fun prover_lemma_list_aux getax proofstr probfile toParent ppid names_arr = 
   269  let val _ = trace
   270                ("\n\nGetting lemma names. proofstr is " ^ proofstr ^
   271                 "\nprobfile is " ^ probfile ^
   272                 "  num of clauses is " ^ string_of_int (Array.length names_arr))
   273      val axiom_names = getax proofstr names_arr
   274      val ax_str = rules_to_string axiom_names
   275     in 
   276 	 trace ("\nDone. Lemma list is " ^ ax_str);
   277          TextIO.output (toParent, "Success. Lemmas used in automatic proof: " ^
   278                   ax_str ^ "\n");
   279 	 TextIO.output (toParent, probfile ^ "\n");
   280 	 TextIO.flushOut toParent;
   281 	 Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2)
   282     end
   283     handle exn => (*FIXME: exn handler is too general!*)
   284      (trace ("\nprover_lemma_list_aux: In exception handler: " ^ 
   285              Toplevel.exn_message exn);
   286       TextIO.output (toParent, "Translation failed for the proof: " ^ 
   287                      String.toString proofstr ^ "\n");
   288       TextIO.output (toParent, probfile);
   289       TextIO.flushOut toParent;
   290       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2));
   291 
   292 val e_lemma_list = prover_lemma_list_aux get_axiom_names_e;
   293 
   294 val vamp_lemma_list = prover_lemma_list_aux get_axiom_names_vamp;
   295 
   296 val spass_lemma_list = prover_lemma_list_aux get_axiom_names_spass;
   297 
   298 
   299 (**** Full proof reconstruction for SPASS (not really working) ****)
   300 
   301 fun spass_reconstruct proofstr probfile toParent ppid thms names_arr = 
   302   let val _ = trace ("\nspass_reconstruct. Proofstr is "^proofstr)
   303       val tokens = #1(lex proofstr)
   304 
   305   (* parse spass proof into datatype *)
   306   (***********************************)
   307       val proof_steps = parse tokens
   308       val _ = trace "\nParsing finished"
   309     
   310   (************************************)
   311   (* recreate original subgoal as thm *)
   312   (************************************)
   313       (* get axioms as correctly numbered clauses w.r.t. the Spass proof *)
   314       (* need to get prems_of thm, then get right one of the prems, relating to whichever*)
   315       (* subgoal this is, and turn it into meta_clauses *)
   316       (* should prob add array and table here, so that we can get axioms*)
   317       (* produced from the clasimpset rather than the problem *)
   318       val (frees,vars,extra_with_vars ,ax_with_vars,numcls) = get_axioms_used proof_steps  thms names_arr
   319       
   320       (*val numcls_string = numclstr ( vars, numcls) ""*)
   321       val _ = trace "\ngot axioms"
   322 	
   323   (************************************)
   324   (* translate proof                  *)
   325   (************************************)
   326       val _ = trace ("\nabout to translate proof, steps: "
   327                        ^ (init_proofsteps_to_string proof_steps ""))
   328       val (newthm,proof) = translate_proof numcls  proof_steps vars
   329       val _ = trace ("translated proof, steps: "^(init_proofsteps_to_string proof_steps ""))
   330   (***************************************************)
   331   (* transfer necessary steps as strings to Isabelle *)
   332   (***************************************************)
   333       (* turn the proof into a string *)
   334       val reconProofStr = proofs_to_string proof ""
   335       (* do the bit for the Isabelle ordered axioms at the top *)
   336       val ax_nums = map #1 numcls
   337       val ax_strs = map ReconOrderClauses.get_meta_lits_bracket (map #2 numcls)
   338       val numcls_strs = ListPair.zip (ax_nums,ax_strs)
   339       val num_cls_vars =  map (addvars vars) numcls_strs;
   340       val reconIsaAxStr = origAxs_to_string (ListPair.zip (ax_nums,ax_with_vars)) ""
   341       
   342       val extra_nums = if (not (extra_with_vars = [])) then (1 upto (length extra_with_vars))
   343                        else []
   344       val reconExtraAxStr = extraAxs_to_string ( ListPair.zip (extra_nums,extra_with_vars)) ""
   345       val frees_str = "["^(thmvars_to_string frees "")^"]"
   346       val reconstr = (frees_str^reconExtraAxStr^reconIsaAxStr^reconProofStr)
   347       val _ = trace ("\nReconstruction:\n" ^ reconstr)
   348   in 
   349        TextIO.output (toParent, reconstr^"\n");
   350        TextIO.output (toParent, probfile ^ "\n");
   351        TextIO.flushOut toParent;
   352        Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   353        all_tac
   354   end
   355   handle exn => (*FIXME: exn handler is too general!*)
   356    (trace ("\nspass_reconstruct. In exception handler: " ^ Toplevel.exn_message exn);
   357     TextIO.output (toParent,"Translation failed for SPASS proof:"^
   358          String.toString proofstr ^"\n");
   359     TextIO.output (toParent, probfile ^ "\n");
   360     TextIO.flushOut toParent;
   361     Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2); all_tac)
   362 
   363 (**********************************************************************************)
   364 (* At other end, want to turn back into datatype so can apply reconstruct_proof.  *)
   365 (* This will be done by the signal handler                                        *)
   366 (**********************************************************************************)
   367 
   368 (* Parse in the string version of the proof steps for reconstruction *)
   369 (* Isar format: cl1 [BINARY 0 cl2 0];cl1 [PARAMOD 0 cl2 0]; cl1 [DEMOD 0 cl2];cl1 [FACTOR 1 2];*)
   370 
   371 
   372  val term_numstep =
   373         (number ++ (a (Other ",")) ++ number) >> (fn (a, (_, c)) => (a, c))
   374 
   375 val extraaxiomstep = (a (Word "ExtraAxiom"))++ (a (Other "(")) ++(a (Other ")"))
   376             >> (fn (_) => ExtraAxiom)
   377 
   378 
   379 
   380 val origaxiomstep = (a (Word "OrigAxiom"))++ (a (Other "(")) ++(a (Other ")"))
   381             >> (fn (_) => OrigAxiom)
   382 
   383 
   384  val axiomstep = (a (Word "Axiom"))++ (a (Other "(")) ++(a (Other ")"))
   385             >> (fn (_) => Axiom)
   386      
   387 
   388 
   389       
   390  val binarystep = (a (Word "Binary")) ++ (a (Other "(")) ++ (a (Other "(")) 
   391                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   392                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   393             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Binary (c,e))
   394       
   395 
   396  val parastep = (a (Word "Para")) ++ (a (Other "(")) ++ (a (Other "(")) 
   397                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   398                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   399             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Para(c, e))
   400       
   401  val mrrstep = (a (Word "MRR")) ++ (a (Other "(")) ++ (a (Other "(")) 
   402                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   403                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   404             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => MRR(c, e))
   405       
   406 
   407  val factorstep = (a (Word "Factor")) ++ (a (Other "("))
   408                     ++ number ++ (a (Other ","))
   409                        ++ number ++ (a (Other ","))
   410                        ++ number ++  (a (Other ")"))
   411                    
   412             >> (fn (_, (_, (c, (_, (e,(_,(f,_))))))) =>  Factor (c,e,f))
   413 
   414 
   415 (*val rewritestep = (a (Word "Rewrite"))  ++ (a (Other "(")) ++ (a (Other "(")) 
   416                    ++ term_numstep  ++ (a (Other ")")) ++ (a (Other ","))
   417                    ++ (a (Other "(")) ++ term_numstep ++ (a (Other ")")) ++ (a (Other ")"))
   418             >> (fn (_, (_, (_, (c, (_,(_,(_, (e,(_,_))))))))) => Rewrite (c,e))*)
   419 
   420 val obviousstep = (a (Word "Obvious")) ++ (a (Other "(")) 
   421                    ++ term_numstep  ++ (a (Other ")")) 
   422             >> (fn (_, (_, (c,_))) => Obvious (c))
   423 
   424  val methodstep = extraaxiomstep || origaxiomstep || axiomstep ||binarystep || factorstep|| parastep || mrrstep || (*rewritestep ||*) obviousstep
   425 
   426 
   427  val number_list_step =
   428         ( number ++ many ((a (Other ",") ++ number)>> #2))
   429         >> (fn (a,b) => (a::b))
   430         
   431  val numberlist_step = a (Other "[")  ++ a (Other "]")
   432                         >>(fn (_,_) => ([]:int list))
   433                        || a (Other "[") ++ number_list_step ++ a (Other "]")
   434                         >>(fn (_,(a,_)) => a)
   435                     
   436 
   437 
   438 (** change this to allow P (x U) *)
   439  fun arglist_step input = 
   440    ( word ++ many word >> (fn (a, b) => (a^" "^(space_implode " " b)))
   441     ||word >> (fn (a) => (a)))input
   442                 
   443 
   444 fun literal_step input = (word ++ a (Other "(") ++ arglist_step ++  a (Other ")")
   445                                           >>(fn (a, (b, (c,d))) => (a^" ("^(c)^")"))
   446                         || arglist_step >> (fn (a) => (a)))input
   447                            
   448 
   449 
   450 (* fun term_step input = (a (Other "~") ++ arglist_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
   451                      ||  arglist_step ++ a (Other "%")>> (fn (a,b) => a ))input
   452 *)
   453 
   454 
   455  fun term_step input = (a (Other "~") ++ literal_step ++ a (Other "%")>> (fn (a,(b,c)) => ("~ "^b))
   456                      ||  literal_step ++ a (Other "%")>> (fn (a,b) => a ))input
   457 
   458 
   459          
   460 
   461  val term_list_step =
   462         (  term_step ++ many ( term_step))
   463         >> (fn (a,b) => (a::b))
   464         
   465  
   466 val term_lists_step = a (Other "[")  ++ a (Other "]")
   467                         >>(fn (_,_) => ([]:string list))
   468                        || a (Other "[") ++ term_list_step ++ a (Other "]")
   469                         >>(fn (_,(a,_)) => a)
   470                      
   471 
   472  val linestep = number ++ methodstep ++ term_lists_step ++ term_lists_step
   473                 >> (fn (a, (b, (c,d))) => (a,(b,c,d)))
   474     
   475  val lines_step = many linestep
   476 
   477  val alllines_step = (term_lists_step ++ lines_step ) ++ finished >> #1
   478     
   479  val parse_step = #1 o alllines_step
   480 
   481 
   482  (*
   483 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)"
   484 *)
   485 
   486 (************************************************************)
   487 (* Construct an Isar style proof from a list of proof steps *)
   488 (************************************************************)
   489 (* want to assume all axioms, then do haves for the other clauses*)
   490 (* then show for the last step *)
   491 
   492 (* replace ~ by not here *)
   493 val change_nots = String.translate (fn c => if c = #"~" then "\\<not>" else str c);
   494 
   495 fun clstrs_to_string xs = space_implode "; " (map change_nots xs);
   496 
   497 fun thmvars_to_quantstring [] str = str
   498 |   thmvars_to_quantstring (x::[]) str =str^x^". "
   499 |   thmvars_to_quantstring (x::xs) str = thmvars_to_quantstring xs (str^(x^" "))
   500 
   501 
   502 fun clause_strs_to_isar clstrs [] =
   503       "\"\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
   504 |   clause_strs_to_isar clstrs thmvars =
   505       "\"\\<And>"^(thmvars_to_quantstring thmvars "")^
   506       "\\<lbrakk>"^(clstrs_to_string clstrs)^"\\<rbrakk> \\<Longrightarrow> False\""
   507 
   508 fun frees_to_isar_str clstrs = space_implode " " (map change_nots clstrs)
   509 
   510 
   511 (***********************************************************************)
   512 (* functions for producing assumptions for the Isabelle ordered axioms *)
   513 (***********************************************************************)
   514 (*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))[][]";       
   515 num, rule, clausestrs, vars*)
   516 
   517 
   518 (* assume the extra clauses - not used in Spass proof *)
   519 
   520 fun is_extraaxiom_step ( num:int,(ExtraAxiom, str, tstr)) = true
   521 |   is_extraaxiom_step (num, _) = false
   522 
   523 fun get_extraaxioms xs = List.filter (is_extraaxiom_step) ( xs)
   524 
   525 fun assume_isar_extraaxiom [] str  = str
   526 |   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 " )
   527 
   528 
   529 
   530 fun assume_isar_extraaxioms  [] = ""
   531 |assume_isar_extraaxioms ((numb,(step, clstrs, thmstrs))::xs) = let val str = "assume cl"^(string_of_int numb)^"': "^(clause_strs_to_isar clstrs thmstrs)^"\n" 
   532                                          in
   533                                              assume_isar_extraaxiom xs str
   534                                          end
   535 
   536 (* assume the Isabelle ordered clauses *)
   537 
   538 fun is_origaxiom_step ( num:int,(OrigAxiom, str, tstr)) = true
   539 |   is_origaxiom_step (num, _) = false
   540 
   541 fun get_origaxioms xs = List.filter (is_origaxiom_step) ( xs)
   542 
   543 fun assume_isar_origaxiom [] str  = str
   544 |   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 " )
   545 
   546 
   547 
   548 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" 
   549                                          in
   550                                              assume_isar_origaxiom xs str
   551                                          end
   552 
   553 
   554 
   555 fun is_axiom_step ( num:int,(Axiom, str, tstr)) = true
   556 |   is_axiom_step (num, _) = false
   557 
   558 fun get_axioms xs = List.filter  (is_axiom_step) ( xs)
   559 
   560 fun have_isar_axiomline (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
   561 
   562 fun  by_isar_axiomline (numb,(step, clstrs, thmstrs))="by (rule cl"^ (string_of_int numb)^"') \n"
   563 
   564 
   565 fun isar_axiomline (numb, (step, clstrs, thmstrs))  = (have_isar_axiomline (numb,(step,clstrs, thmstrs )))^( by_isar_axiomline(numb,(step,clstrs, thmstrs )) )
   566 
   567 
   568 fun isar_axiomlines [] str = str
   569 |   isar_axiomlines (x::xs) str = isar_axiomlines xs (str^(isar_axiomline x))
   570 
   571 
   572 fun have_isar_line (numb,(step, clstrs, thmstrs))="have cl"^(string_of_int numb)^": "^(clause_strs_to_isar clstrs thmstrs)^"\n"
   573 (*FIX: ask Larry to add and mrr attribute *)
   574 
   575 fun by_isar_line ((Binary ((a,b), (c,d)))) = 
   576     "by(rule cl"^
   577 		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
   578 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   579 |by_isar_line ((MRR ((a,b), (c,d)))) = 
   580     "by(rule cl"^
   581 		(string_of_int a)^" [binary "^(string_of_int b)^" cl"^
   582 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   583 |   by_isar_line ( (Para ((a,b), (c,d)))) =
   584     "by (rule cl"^
   585 		(string_of_int a)^" [paramod "^(string_of_int b)^" cl"^
   586 		(string_of_int c)^" "^(string_of_int d)^"])\n"
   587 |   by_isar_line ((Factor ((a,b,c)))) = 
   588     "by (rule cl"^(string_of_int a)^" [factor "^(string_of_int b)^" "^
   589 		(string_of_int c)^" ])\n"
   590 (*|   by_isar_line ( (Rewrite ((a,b),(c,d)))) =
   591     "by (rule cl"^(string_of_int a)^" [demod "^(string_of_int b)^" "^
   592 		(string_of_int c)^" "^(string_of_int d)^" ])\n"*)
   593 |   by_isar_line ( (Obvious ((a,b)))) =
   594     "by (rule cl"^(string_of_int a)^" [obvious "^(string_of_int b)^" ])\n"
   595 
   596 fun isar_line (numb, (step, clstrs, thmstrs))  = (have_isar_line (numb,(step,clstrs, thmstrs )))^( by_isar_line step)
   597 
   598 
   599 fun isar_lines [] str = str
   600 |   isar_lines (x::xs) str = isar_lines xs (str^(isar_line x))
   601 
   602 fun last_isar_line (numb,( step, clstrs,thmstrs)) = "show \"False\"\n"^(by_isar_line step)
   603 
   604 
   605 fun to_isar_proof (frees, xs) =
   606     let val extraaxioms = get_extraaxioms xs
   607 	val extraax_num = length extraaxioms
   608 	val origaxioms_and_steps = Library.drop (extraax_num, xs)  
   609 	
   610 	val origaxioms = get_origaxioms origaxioms_and_steps
   611 	val origax_num = length origaxioms
   612 	val axioms_and_steps = Library.drop (origax_num + extraax_num, xs)  
   613 	val axioms = get_axioms axioms_and_steps
   614 	
   615 	val steps = Library.drop (origax_num, axioms_and_steps)
   616 	val firststeps = ReconOrderClauses.butlast steps
   617 	val laststep = List.last steps
   618 	
   619 	val isar_proof = 
   620 		("show \"[your goal]\"\n")^
   621 		("proof (rule ccontr,skolemize, make_clauses) \n")^
   622 		("fix "^(frees_to_isar_str frees)^"\n")^
   623 		(assume_isar_extraaxioms extraaxioms)^
   624 		(assume_isar_origaxioms origaxioms)^
   625 		(isar_axiomlines axioms "")^
   626 		(isar_lines firststeps "")^
   627 		(last_isar_line laststep)^
   628 		("qed")
   629 	val _ = trace ("\nto_isar_proof returns " ^ isar_proof)
   630     in
   631 	isar_proof
   632     end;
   633 
   634 (* get fix vars from axioms - all Frees *)
   635 (* check each clause for meta-vars and /\ over them at each step*)
   636 
   637 (*******************************************************)
   638 (* This assumes the thm list  "numcls" is still there  *)
   639 (* In reality, should probably label it with an        *)
   640 (* ID number identifying the subgoal.  This could      *)
   641 (* be passed over to the watcher, e.g.  numcls25       *)
   642 (*******************************************************)
   643 
   644 fun apply_res_thm str  = 
   645   let val tokens = #1 (lex str);
   646       val _ = trace ("\napply_res_thm. str is: "^str^"\n")	
   647       val (frees,recon_steps) = parse_step tokens 
   648   in 
   649       to_isar_proof (frees, recon_steps)
   650   end 
   651 
   652 end;