src/HOL/Tools/res_atp.ML
author paulson
Fri Sep 02 17:55:24 2005 +0200 (2005-09-02)
changeset 17234 12a9393c5d77
parent 17231 f42bc4f7afdf
child 17235 8e55ad29b690
permissions -rw-r--r--
further tidying up of Isabelle-ATP link
     1 (*  Author: Jia Meng, Cambridge University Computer Laboratory
     2     ID: $Id$
     3     Copyright 2004 University of Cambridge
     4 
     5 ATPs with TPTP format input.
     6 *)
     7 
     8 signature RES_ATP =
     9 sig
    10   val axiom_file : Path.T
    11 (*val atp_ax_tac : thm list -> int -> Tactical.tactic*)
    12 (*val atp_tac : int -> Tactical.tactic*)
    13   val full_spass: bool ref
    14 (*val spass: bool ref*)
    15   val vampire: bool ref
    16   val custom_spass: string list ref
    17   val hook_count: int ref
    18 (*  val invoke_atp: Toplevel.transition -> Toplevel.transition*)
    19 end;
    20 
    21 structure ResAtp: RES_ATP =
    22 struct
    23 
    24 
    25 val call_atp = ref false;
    26 val hook_count = ref 0;
    27 
    28 fun debug_tac tac = (debug "testing"; tac);
    29 
    30 val full_spass = ref false;
    31 
    32 (* use spass as default prover *)
    33 (*val spass = ref true;*)
    34 
    35 val custom_spass = ref ["Auto=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub","-DocProof","-TimeLimit=60"];
    36 val vampire = ref false;
    37 
    38 val skolem_tac = skolemize_tac;
    39 
    40 val num_of_clauses = ref 0;
    41 val clause_arr = Array.array (3500, ("empty", 0));
    42 
    43 
    44 val atomize_tac =
    45     SUBGOAL
    46      (fn (prop,_) =>
    47          let val ts = Logic.strip_assums_hyp prop
    48          in EVERY1
    49                 [METAHYPS
    50                      (fn hyps => (cut_facts_tac (map (ObjectLogic.atomize_thm o forall_intr_vars) hyps) 1)),
    51           REPEAT_DETERM_N (length ts) o (etac thin_rl)]
    52      end);
    53 
    54 val axiom_file = File.tmp_path (Path.basic "axioms");
    55 val clasimp_file = File.tmp_path (Path.basic "clasimp");
    56 val hyps_file = File.tmp_path (Path.basic "hyps");
    57 val prob_file = File.tmp_path (Path.basic "prob");
    58 
    59 
    60 (**** for Isabelle/ML interface  ****)
    61 
    62 (*Remove unwanted characters such as ? and newline from the textural 
    63   representation of a theorem (surely they don't need to be produced in 
    64   the first place?) *)
    65 
    66 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
    67 
    68 val proofstring =
    69     String.translate (fn c => if is_proof_char c then str c else "");
    70 
    71 
    72 (**** For running in Isar ****)
    73 
    74 (* same function as that in res_axioms.ML *)
    75 fun repeat_RS thm1 thm2 =
    76     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    77     in
    78         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    79     end;
    80 
    81 (* a special version of repeat_RS *)
    82 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    83 
    84 
    85 (*FIXME: is function isar_atp_h used? If not, delete!*)
    86 (*********************************************************************)
    87 (* convert clauses from "assume" to conjecture. write to file "hyps" *)
    88 (* hypotheses of the goal currently being proved                     *)
    89 (*********************************************************************)
    90 (*perhaps have 2 different versions of this, depending on whether or not SpassComm.spass is set *)
    91 fun isar_atp_h thms =
    92     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
    93         val prems' = map repeat_someI_ex prems
    94         val prems'' = make_clauses prems'
    95         val prems''' = ResAxioms.rm_Eps [] prems''
    96         val clss = map ResClause.make_conjecture_clause prems'''
    97 	val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss) 
    98 	val tfree_lits = ResLib.flat_noDup tfree_litss
    99         (* tfree clause is different in tptp and dfg versions *)
   100 	val tfree_clss = map ResClause.tfree_clause tfree_lits 
   101         val hypsfile = File.platform_path hyps_file
   102         val out = TextIO.openOut(hypsfile)
   103     in
   104         ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   105         TextIO.closeOut out; debug hypsfile;
   106         tfree_lits
   107     end;
   108 
   109 
   110 (*********************************************************************)
   111 (* write out a subgoal as tptp clauses to the file "probN"           *)
   112 (* where N is the number of this subgoal                             *)
   113 (*********************************************************************)
   114 
   115 fun tptp_inputs_tfrees thms n tfrees =
   116     let
   117       val _ = debug ("in tptp_inputs_tfrees 0")
   118       val clss = map (ResClause.make_conjecture_clause_thm) thms
   119       val _ = debug ("in tptp_inputs_tfrees 1")
   120       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
   121       val _ = debug ("in tptp_inputs_tfrees 2")
   122       val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees)
   123       val _ = debug ("in tptp_inputs_tfrees 3")
   124       val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
   125       val out = TextIO.openOut(probfile)
   126     in
   127       ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   128       TextIO.closeOut out;
   129       debug probfile
   130     end;
   131 
   132 
   133 (*********************************************************************)
   134 (* write out a subgoal as DFG clauses to the file "probN"           *)
   135 (* where N is the number of this subgoal                             *)
   136 (*********************************************************************)
   137 
   138 fun dfg_inputs_tfrees thms n tfrees axclauses = 
   139     let val clss = map (ResClause.make_conjecture_clause_thm) thms
   140         val probfile = (File.platform_path prob_file) ^ "_" ^ (string_of_int n)
   141         val _ = debug ("about to write out dfg prob file " ^ probfile)
   142        	(*val (dfg_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2dfg clss)
   143         val tfree_clss = map ResClause.tfree_dfg_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees) *)   
   144         val probN = ResClause.clauses2dfg clss ("prob" ^ (string_of_int n)) 
   145                         axclauses [] [] [] tfrees   
   146 	val out = TextIO.openOut(probfile)
   147     in
   148 	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
   149 (* (ResLib.writeln_strs out (tfree_clss @ dfg_clss); *)
   150     end;
   151 
   152 
   153 (*********************************************************************)
   154 (* call SPASS with settings and problem file for the current subgoal *)
   155 (* should be modified to allow other provers to be called            *)
   156 (*********************************************************************)
   157 (* now passing in list of skolemized thms and list of sgterms to go with them *)
   158 fun call_resolve_tac  (thms: thm list list)  sign (sg_terms:  term list) (childin, childout,pid) n  =
   159   let
   160     val axfile = (File.platform_path axiom_file)
   161 
   162     val hypsfile = (File.platform_path hyps_file)
   163     val clasimpfile = (File.platform_path clasimp_file)
   164 
   165     fun make_atp_list [] sign n = []
   166       | make_atp_list ((sko_thm, sg_term)::xs) sign n =
   167           let
   168             val thmstr = proofstring (Meson.concat_with_and (map string_of_thm sko_thm))
   169             val _ = debug ("thmstring in make_atp_lists is " ^ thmstr)
   170 
   171             val goalstring = proofstring (Sign.string_of_term sign sg_term)
   172             val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
   173 
   174             val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
   175             val _ = debug ("prob file in call_resolve_tac is " ^ probfile)
   176           in
   177             if !SpassComm.spass
   178             then
   179               let val optionline = (*Custom SPASS options, or default?*)
   180 		      if !full_spass (*Auto mode: all SPASS inference rules*)
   181                       then "-DocProof%-TimeLimit=60%-SOS"
   182                       else "-" ^ space_implode "%-" (!custom_spass)
   183                   val _ = debug ("SPASS option string is " ^ optionline)
   184                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   185                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   186               in 
   187                   ([("spass", thmstr, goalstring,
   188                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   189                      optionline, clasimpfile, axfile, hypsfile, probfile)] @ 
   190                   (make_atp_list xs sign (n+1)))
   191               end
   192             else
   193               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vkernel"
   194               in
   195                 ([("vampire", thmstr, goalstring, vampire, "-t 60%-m 100000",
   196                    clasimpfile, axfile, hypsfile, probfile)] @
   197                  (make_atp_list xs sign (n+1)))
   198               end
   199           end
   200 
   201     val atp_list = make_atp_list (ListPair.zip (thms, sg_terms)) sign 1
   202   in
   203     Watcher.callResProvers(childout,atp_list);
   204     debug "Sent commands to watcher!";
   205     all_tac
   206   end
   207 
   208 (**********************************************************)
   209 (* write out the current subgoal as a tptp file, probN,   *)
   210 (* then call all_tac - should be call_res_tac           *)
   211 (**********************************************************)
   212 
   213 
   214 fun get_sko_thms tfrees sign sg_terms (childin, childout,pid) thm n sko_thms axclauses =
   215     if n=0 then 
   216        (call_resolve_tac  (rev sko_thms)
   217         sign  sg_terms (childin, childout, pid) (List.length sg_terms);
   218         all_tac thm)
   219      else
   220 	
   221      ( SELECT_GOAL
   222         (EVERY1 [rtac ccontr,atomize_tac, skolemize_tac, 
   223           METAHYPS(fn negs => 
   224             (if !SpassComm.spass 
   225              then dfg_inputs_tfrees (make_clauses negs) n tfrees axclauses
   226              else tptp_inputs_tfrees (make_clauses negs) n tfrees;
   227              get_sko_thms tfrees sign sg_terms (childin, childout, pid) 
   228                           thm  (n -1) (negs::sko_thms) axclauses; 
   229              all_tac))]) n thm )
   230 
   231 
   232 
   233 (**********************************************)
   234 (* recursively call atp_tac_g on all subgoals *)
   235 (* sg_term is the nth subgoal as a term - used*)
   236 (* in proof reconstruction                    *)
   237 (**********************************************)
   238 
   239 fun isar_atp_goal' thm n tfree_lits (childin, childout, pid)  axclauses =
   240   let
   241     val prems = Thm.prems_of thm
   242     (*val sg_term = get_nth k prems*)
   243     val sign = sign_of_thm thm
   244     val thmstring = string_of_thm thm
   245   in
   246     debug("in isar_atp_goal'");
   247     debug("thmstring in isar_atp_goal': " ^ thmstring);
   248     (* go and call callResProvers with this subgoal *)
   249     (* isar_atp_g tfree_lits  sg_term (childin, childout, pid) k thm; *)
   250     (* recursive call to pick up the remaining subgoals *)
   251     (* isar_atp_goal' thm (k+1) n tfree_lits  (childin, childout, pid) *)
   252     get_sko_thms tfree_lits sign prems (childin, childout, pid) thm n []  axclauses
   253   end;
   254 
   255 
   256 (**************************************************)
   257 (* convert clauses from "assume" to conjecture.   *)
   258 (* i.e. apply make_clauses and then get tptp for  *)
   259 (* any hypotheses in the goal produced by assume  *)
   260 (* statements;                                    *)
   261 (* write to file "hyps"                           *)
   262 (**************************************************)
   263 
   264 fun isar_atp_aux thms thm n_subgoals  (childin, childout, pid) axclauses =
   265   let val tfree_lits = isar_atp_h thms
   266   in
   267     debug ("in isar_atp_aux");
   268     isar_atp_goal' thm n_subgoals tfree_lits (childin, childout, pid)  axclauses
   269   end;
   270 
   271 (******************************************************************)
   272 (* called in Isar automatically                                   *)
   273 (* writes out the current clasimpset to a tptp file               *)
   274 (* passes all subgoals on to isar_atp_aux for further processing  *)
   275 (* turns off xsymbol at start of function, restoring it at end    *)
   276 (******************************************************************)
   277 (*FIX changed to clasimp_file *)
   278 val isar_atp' = setmp print_mode [] (fn (ctxt, thms, thm) =>
   279   if Thm.no_prems thm then ()
   280   else
   281     let
   282       val _= debug ("in isar_atp'")
   283       val thy = ProofContext.theory_of ctxt
   284       val prems = Thm.prems_of thm
   285       val thms_string = Meson.concat_with_and (map string_of_thm thms)
   286       val thm_string = string_of_thm thm
   287       val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
   288 
   289       (*set up variables for writing out the clasimps to a tptp file*)
   290       val (clause_arr, num_of_clauses, axclauses) =
   291         ResClasimp.write_out_clasimp (File.platform_path clasimp_file) thy
   292           (hd prems) (*FIXME: hack!! need to do all prems*)
   293       val _ = debug ("clasimp_file is " ^ File.platform_path clasimp_file ^ " with " ^ (string_of_int num_of_clauses)^ " clauses")
   294       val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr, num_of_clauses)
   295       val pid_string =
   296         string_of_int (Word.toInt (Word.fromLargeWord (Posix.Process.pidToWord pid)))
   297     in
   298       debug ("initial thms: " ^ thms_string);
   299       debug ("initial thm: " ^ thm_string);
   300       debug ("subgoals: " ^ prems_string);
   301       debug ("pid: "^ pid_string);
   302       isar_atp_aux thms thm (length prems) (childin, childout, pid) axclauses;
   303       ()
   304     end);
   305 
   306 
   307 fun get_thms_cs claset =
   308   let val {safeEs, safeIs, hazEs, hazIs, ...} = rep_cs claset
   309   in safeEs @ safeIs @ hazEs @ hazIs end;
   310 
   311 fun append_name name [] _ = []
   312   | append_name name (thm :: thms) k =
   313       Thm.name_thm ((name ^ "_" ^ string_of_int k), thm) :: append_name name thms (k + 1);
   314 
   315 fun append_names (name :: names) (thms :: thmss) =
   316   append_name name thms 0 :: append_names names thmss;
   317 
   318 fun get_thms_ss [] = []
   319   | get_thms_ss thms =
   320       let
   321         val names = map Thm.name_of_thm thms
   322         val thms' = map (mksimps mksimps_pairs) thms
   323         val thms'' = append_names names thms'
   324       in
   325         ResLib.flat_noDup thms''
   326       end;
   327 
   328 
   329 (* convert locally declared rules to axiom clauses *)
   330 
   331 fun subtract_simpset thy ctxt =
   332   let
   333     val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
   334     val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
   335   in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
   336 
   337 fun subtract_claset thy ctxt =
   338   let
   339     val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
   340     val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
   341     val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
   342   in subtract netI1 netI2 @ subtract netE1 netE2 end;
   343 
   344 
   345 
   346 (** the Isar toplevel hook **)
   347 
   348 val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
   349 
   350   let
   351 
   352     val proof = Toplevel.proof_of state
   353     val (ctxt, (_, goal)) = Proof.get_goal proof
   354         handle Proof.STATE _ => error "No goal present";
   355 
   356     val thy = ProofContext.theory_of ctxt;
   357 
   358     (* FIXME presently unused *)
   359     val ss_thms = subtract_simpset thy ctxt;
   360     val cs_thms = subtract_claset thy ctxt;
   361   in
   362     debug ("initial thm in isar_atp: " ^ 
   363            Pretty.string_of (ProofContext.pretty_thm ctxt goal));
   364     debug ("subgoals in isar_atp: " ^ 
   365            Pretty.string_of (ProofContext.pretty_term ctxt
   366              (Logic.mk_conjunction_list (Thm.prems_of goal))));
   367     debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
   368     hook_count := !hook_count +1;
   369     debug ("in hook for time: " ^(string_of_int (!hook_count)) );
   370     ResClause.init thy;
   371     isar_atp' (ctxt, ProofContext.prems_of ctxt, goal)
   372   end);
   373 
   374 val call_atpP =
   375   OuterSyntax.improper_command 
   376     "ProofGeneral.call_atp" 
   377     "call automatic theorem provers" 
   378     OuterKeyword.diag
   379     (Scan.succeed (Toplevel.no_timing o invoke_atp));
   380 
   381 val _ = OuterSyntax.add_parsers [call_atpP];
   382 
   383 end;