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