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