src/HOL/Tools/res_atp.ML
author paulson
Wed Jul 20 17:00:28 2005 +0200 (2005-07-20)
changeset 16897 7e5319d0f418
parent 16802 6eeee59dac4c
child 16904 6fb188ca5f91
permissions -rw-r--r--
code streamlining
     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 call_atp: bool ref
    11   val trace_res : bool ref
    12   val traceflag : bool ref
    13   val axiom_file : Path.T
    14   val hyps_file : Path.T
    15   val prob_file : Path.T;
    16 (*val atp_ax_tac : thm list -> int -> Tactical.tactic*)
    17 (*val atp_tac : int -> Tactical.tactic*)
    18   val debug: bool ref
    19   val full_spass: bool ref
    20 (*val spass: bool ref*)
    21   val vampire: bool ref
    22   val custom_spass: string list ref
    23 end;
    24 
    25 structure ResAtp: RES_ATP =
    26 struct
    27 
    28 val call_atp = ref false;
    29 
    30 val traceflag = ref true;
    31 val debug = ref false;
    32 
    33 fun debug_tac tac = (warning "testing"; tac);
    34 
    35 val full_spass = ref false;
    36 
    37 (* use spass as default prover *)
    38 (*val spass = ref true;*)
    39 
    40 val custom_spass = ref ["Auto=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub","-DocProof","-TimeLimit=60"];
    41 val vampire = ref false;
    42 
    43 val trace_res = ref false;
    44 
    45 val skolem_tac = skolemize_tac;
    46 
    47 val num_of_clauses = ref 0;
    48 val clause_arr = Array.array (3500, ("empty", 0));
    49 
    50 
    51 val atomize_tac =
    52     SUBGOAL
    53      (fn (prop,_) =>
    54          let val ts = Logic.strip_assums_hyp prop
    55          in EVERY1
    56                 [METAHYPS
    57                      (fn hyps => (cut_facts_tac (map (ObjectLogic.atomize_thm o forall_intr_vars) hyps) 1)),
    58           REPEAT_DETERM_N (length ts) o (etac thin_rl)]
    59      end);
    60 
    61 (* temporarily use these files, which will be loaded by Vampire *)
    62 val file_id_num = ref 0;
    63 fun new_prob_file () = "prob" ^ string_of_int (inc file_id_num);
    64 
    65 val axiom_file = File.tmp_path (Path.basic "axioms");
    66 val clasimp_file = File.tmp_path (Path.basic "clasimp");
    67 val hyps_file = File.tmp_path (Path.basic "hyps");
    68 val prob_file = File.tmp_path (Path.basic "prob");
    69 val dummy_tac = all_tac;
    70 
    71 
    72 (**** for Isabelle/ML interface  ****)
    73 
    74 (*Remove unwanted characters such as ? and newline from the textural 
    75   representation of a theorem (surely they don't need to be produced in 
    76   the first place?) *)
    77 
    78 fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
    79 
    80 val proofstring =
    81     String.translate (fn c => if is_proof_char c then str c else "");
    82 
    83 
    84 (*
    85 fun call_atp_tac thms n = (tptp_inputs thms ; dummy_tac);
    86 
    87 *)
    88 
    89 (**** For running in Isar ****)
    90 
    91 (* same function as that in res_axioms.ML *)
    92 fun repeat_RS thm1 thm2 =
    93     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    94     in
    95         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    96     end;
    97 
    98 (* a special version of repeat_RS *)
    99 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
   100 
   101 (*********************************************************************)
   102 (* convert clauses from "assume" to conjecture. write to file "hyps" *)
   103 (* hypotheses of the goal currently being proved                     *)
   104 (*********************************************************************)
   105 (*perhaps have 2 different versions of this, depending on whether or not SpassComm.spass is set *)
   106 fun isar_atp_h thms =
   107     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
   108         val prems' = map repeat_someI_ex prems
   109         val prems'' = make_clauses prems'
   110         val prems''' = ResAxioms.rm_Eps [] prems''
   111         val clss = map ResClause.make_conjecture_clause prems'''
   112 	val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss) 
   113 	val tfree_lits = ResLib.flat_noDup tfree_litss
   114         (* tfree clause is different in tptp and dfg versions *)
   115 	val tfree_clss = map ResClause.tfree_clause tfree_lits 
   116         val hypsfile = File.platform_path hyps_file
   117         val out = TextIO.openOut(hypsfile)
   118     in
   119       (ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   120         TextIO.closeOut out; if !trace_res then (warning hypsfile) else ());
   121       tfree_lits
   122     end;
   123 
   124 
   125 (*********************************************************************)
   126 (* write out a subgoal as tptp clauses to the file "probN"           *)
   127 (* where N is the number of this subgoal                             *)
   128 (*********************************************************************)
   129 
   130 fun tptp_inputs_tfrees thms n tfrees =
   131     let
   132       val _ = warning ("in tptp_inputs_tfrees 0")
   133       val clss = map (ResClause.make_conjecture_clause_thm) thms
   134       val _ = warning ("in tptp_inputs_tfrees 1")
   135       val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
   136       val _ = warning ("in tptp_inputs_tfrees 2")
   137       val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees)
   138       val _ = warning ("in tptp_inputs_tfrees 3")
   139       val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
   140       val out = TextIO.openOut(probfile)
   141     in
   142       ResLib.writeln_strs out (tfree_clss @ tptp_clss);
   143       TextIO.closeOut out;
   144       if !trace_res then (warning probfile) else ()
   145     end;
   146 
   147 
   148 (*********************************************************************)
   149 (* write out a subgoal as DFG clauses to the file "probN"           *)
   150 (* where N is the number of this subgoal                             *)
   151 (*********************************************************************)
   152 (*
   153 fun dfg_inputs_tfrees thms n tfrees = 
   154     let val _ = (warning ("in dfg_inputs_tfrees 0"))
   155         val clss = map (ResClause.make_conjecture_clause_thm) thms
   156          val _ = (warning ("in dfg_inputs_tfrees 1"))
   157 	val (dfg_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2dfg clss)
   158         val _ = (warning ("in dfg_inputs_tfrees 2"))
   159 	val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees) 
   160          val _ = (warning ("in dfg_inputs_tfrees 3"))
   161         val probfile = (File.platform_path prob_file) ^ "_" ^ (string_of_int n)
   162 	val out = TextIO.openOut(probfile)
   163     in
   164 	(ResLib.writeln_strs out (tfree_clss @ dfg_clss); TextIO.closeOut out; (if !trace_res then (warning probfile) else ()))
   165     end;*)
   166 
   167 (*********************************************************************)
   168 (* call SPASS with settings and problem file for the current subgoal *)
   169 (* should be modified to allow other provers to be called            *)
   170 (*********************************************************************)
   171 (* now passing in list of skolemized thms and list of sgterms to go with them *)
   172 fun call_resolve_tac  (thms: thm list list)  sign (sg_terms:  term list) (childin, childout,pid) n  =
   173   let
   174     val axfile = (File.platform_path axiom_file)
   175 
   176     val hypsfile = (File.platform_path hyps_file)
   177     val clasimpfile = (File.platform_path clasimp_file)
   178 
   179     fun make_atp_list [] sign n = []
   180       | make_atp_list ((sko_thm, sg_term)::xs) sign n =
   181           let
   182             val thmstr = proofstring (Meson.concat_with_and (map string_of_thm sko_thm))
   183             val _ = warning ("thmstring in make_atp_lists is " ^ thmstr)
   184 
   185             val goalstring = proofstring (Sign.string_of_term sign sg_term)
   186             val _ = warning ("goalstring in make_atp_lists is " ^ goalstring)
   187 
   188             val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
   189             val _ = warning ("prob file in call_resolve_tac is " ^ probfile)
   190           in
   191             if !SpassComm.spass
   192             then
   193               let val optionline = (*Custom SPASS options, or default?*)
   194 		      if !full_spass (*Auto mode: all SPASS inference rules*)
   195                       then "-DocProof%-TimeLimit=60%-SOS"
   196                       else "-" ^ space_implode "%-" (!custom_spass)
   197                   val _ = warning ("SPASS option string is " ^ optionline)
   198                   val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
   199                     (*We've checked that SPASS is there for ATP/spassshell to run.*)
   200               in 
   201                   ([("spass", thmstr, goalstring,
   202                      getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
   203                      optionline, clasimpfile, axfile, hypsfile, probfile)] @ 
   204                   (make_atp_list xs sign (n+1)))
   205               end
   206             else
   207               let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vkernel"
   208               in
   209                 ([("vampire", thmstr, goalstring, vampire, "-t 60%-m 100000",
   210                    clasimpfile, axfile, hypsfile, probfile)] @
   211                  (make_atp_list xs sign (n+1)))
   212               end
   213           end
   214 
   215     val atp_list = make_atp_list (ListPair.zip (thms, sg_terms)) sign 1
   216   in
   217     Watcher.callResProvers(childout,atp_list);
   218     warning "Sent commands to watcher!";
   219     dummy_tac
   220   end
   221 
   222 (**********************************************************)
   223 (* write out the current subgoal as a tptp file, probN,   *)
   224 (* then call dummy_tac - should be call_res_tac           *)
   225 (**********************************************************)
   226 
   227 fun get_sko_thms tfrees sign sg_terms (childin, childout, pid) thm n sko_thms =
   228   if n = 0 then
   229     (call_resolve_tac (rev sko_thms)
   230       sign sg_terms (childin, childout, pid) (List.length sg_terms);
   231      dummy_tac thm)
   232   else
   233     SELECT_GOAL
   234       (EVERY1 [rtac ccontr, atomize_tac, skolemize_tac,
   235         METAHYPS (fn negs =>
   236           (tptp_inputs_tfrees (make_clauses negs) n tfrees;
   237            get_sko_thms tfrees sign sg_terms (childin, childout, pid) thm (n - 1)
   238              (negs::sko_thms); dummy_tac))]) n thm;
   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) =
   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     warning("in isar_atp_goal'");
   255     warning("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 []
   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) =
   273   let val tfree_lits = isar_atp_h thms
   274   in
   275     warning ("in isar_atp_aux");
   276     isar_atp_goal' thm n_subgoals tfree_lits (childin, childout, pid)
   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 _= warning ("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) =
   299         ResClasimp.write_out_clasimp (File.platform_path clasimp_file) thy
   300           (hd prems) (*FIXME: hack!! need to do all prems*)
   301       val _ = warning ("clasimp_file is " ^ File.platform_path clasimp_file)
   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       warning ("initial thms: " ^ thms_string);
   307       warning ("initial thm: " ^ thm_string);
   308       warning ("subgoals: " ^ prems_string);
   309       warning ("pid: "^ pid_string);
   310       isar_atp_aux thms thm (length prems) (childin, childout, pid);
   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 (* write axiom clauses to ax_file *)
   339 (* what about clasimpset - it should already be in the ax file - perhaps append to ax file rather than just *)
   340 (* write out ? Or keep as a separate file and then cat them all together in the watcher, like we do with the *)
   341 (*claset file and prob file*)
   342 (* FIX*)
   343 (*fun isar_local_thms (delta_cs, delta_ss_thms) =
   344     let val thms_cs = get_thms_cs delta_cs
   345         val thms_ss = get_thms_ss delta_ss_thms
   346         val thms_clauses = ResLib.flat_noDup (map ResAxioms.clausify_axiom (thms_cs @ thms_ss))
   347         val clauses_strs = ResLib.flat_noDup (map ResClause.tptp_clause thms_clauses) (*string list*)
   348         val ax_file = File.platform_path axiom_file
   349         val out = TextIO.openOut ax_file
   350     in
   351         (ResLib.writeln_strs out clauses_strs; (warning ("axiom file is "^ax_file));TextIO.closeOut out)
   352     end;
   353 *)
   354 
   355 
   356 fun subtract_simpset thy ctxt =
   357   let
   358     val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
   359     val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
   360   in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
   361 
   362 fun subtract_claset thy ctxt =
   363   let
   364     val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
   365     val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
   366     val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
   367   in subtract netI1 netI2 @ subtract netE1 netE2 end;
   368 
   369 
   370 
   371 (** the Isar toplevel hook **)
   372 
   373 val _ = Toplevel.print_state_hook (fn _ => fn state =>
   374   let
   375     val _ = if ! call_atp then () else raise Toplevel.UNDEF;
   376     val prf =
   377       (case Toplevel.node_of state of Toplevel.Proof prf => prf | _ => raise Toplevel.UNDEF);
   378     val _ = Proof.assert_backward (the (ProofHistory.previous prf));
   379     val proof = Proof.assert_forward (ProofHistory.current prf);
   380     val (ctxt, (_, goal)) = Proof.get_goal proof;
   381 
   382     val thy = ProofContext.theory_of ctxt;
   383     val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) (Thm.prems_of goal));
   384 
   385     (* FIXME presently unused *)
   386     val ss_thms = subtract_simpset thy ctxt;
   387     val cs_thms = subtract_claset thy ctxt;
   388   in
   389     warning ("initial thm in isar_atp: " ^ string_of_thm goal);
   390     warning ("subgoals in isar_atp: " ^ prems_string);
   391     warning ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
   392     (*isar_local_thms (d_cs,d_ss_thms);*)
   393     isar_atp' (ctxt, ProofContext.prems_of ctxt, goal)
   394   end);
   395 
   396 end;