src/HOL/Tools/res_atp.ML
author paulson
Thu Oct 06 10:14:22 2005 +0200 (2005-10-06)
changeset 17772 818cec5f82a4
parent 17764 fde495b9e24b
child 17773 a7258e1020b7
permissions -rw-r--r--
major simplification: removal of the goalstring argument
paulson@15608
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15608
     2
    ID: $Id$
paulson@15608
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
ATPs with TPTP format input.
paulson@15347
     6
*)
paulson@15452
     7
wenzelm@16802
     8
signature RES_ATP =
wenzelm@16802
     9
sig
paulson@17306
    10
  val prover: string ref
wenzelm@16802
    11
  val custom_spass: string list ref
paulson@17484
    12
  val destdir: string ref
quigley@17150
    13
  val hook_count: int ref
paulson@17484
    14
  val problem_name: string ref
paulson@17690
    15
  val time_limit: int ref
paulson@15347
    16
end;
paulson@15347
    17
wenzelm@16802
    18
structure ResAtp: RES_ATP =
paulson@15347
    19
struct
paulson@15347
    20
quigley@17150
    21
val call_atp = ref false;
quigley@17150
    22
val hook_count = ref 0;
paulson@17690
    23
val time_limit = ref 60;
quigley@17150
    24
paulson@17404
    25
val prover = ref "E";   (* use E as the default prover *)
paulson@17305
    26
val custom_spass =   (*specialized options for SPASS*)
paulson@17690
    27
      ref ["-Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub"];
paulson@15347
    28
paulson@17484
    29
val destdir = ref "";   (*Empty means write files to /tmp*)
paulson@17484
    30
val problem_name = ref "prob";
paulson@17484
    31
paulson@17717
    32
fun probfile_nosuffix _ = 
paulson@17484
    33
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
paulson@17484
    34
  else if File.exists (File.unpack_platform_path (!destdir))
paulson@17484
    35
  then !destdir ^ "/" ^ !problem_name
paulson@17484
    36
  else error ("No such directory: " ^ !destdir);
quigley@15644
    37
paulson@17717
    38
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
paulson@17717
    39
wenzelm@16802
    40
paulson@15347
    41
(**** For running in Isar ****)
paulson@15347
    42
paulson@15608
    43
(* same function as that in res_axioms.ML *)
paulson@15608
    44
fun repeat_RS thm1 thm2 =
paulson@15608
    45
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
    46
    in
wenzelm@16802
    47
        if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
    48
    end;
paulson@15608
    49
paulson@15608
    50
(* a special version of repeat_RS *)
paulson@17717
    51
fun repeat_someI_ex th = repeat_RS th someI_ex;
paulson@15608
    52
paulson@17764
    53
fun writeln_strs _   []      = ()
paulson@17764
    54
  | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
paulson@16925
    55
paulson@17502
    56
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@17717
    57
fun tptp_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) =
wenzelm@16802
    58
    let
wenzelm@16802
    59
      val clss = map (ResClause.make_conjecture_clause_thm) thms
wenzelm@16802
    60
      val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@17422
    61
      val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
paulson@17525
    62
      val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
paulson@17525
    63
      val arity_cls = map ResClause.tptp_arity_clause arity_clauses
paulson@17717
    64
      val out = TextIO.openOut(pf n)
paulson@15608
    65
    in
paulson@17764
    66
      writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
paulson@17764
    67
      writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
paulson@17717
    68
      TextIO.closeOut out
paulson@15608
    69
    end;
paulson@15452
    70
paulson@17502
    71
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@17717
    72
fun dfg_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
quigley@17150
    73
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@17525
    74
        (*FIXME: classrel_clauses and arity_clauses*)
paulson@17525
    75
        val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
paulson@17422
    76
                        axclauses [] [] []    
paulson@17717
    77
	val out = TextIO.openOut(pf n)
quigley@16767
    78
    in
paulson@17764
    79
	writeln_strs out [probN]; TextIO.closeOut out
quigley@17150
    80
    end;
quigley@17150
    81
quigley@16357
    82
quigley@15644
    83
(*********************************************************************)
paulson@17306
    84
(* call prover with settings and problem file for the current subgoal *)
quigley@15644
    85
(*********************************************************************)
quigley@16357
    86
(* now passing in list of skolemized thms and list of sgterms to go with them *)
paulson@17764
    87
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
    88
  let
paulson@17422
    89
    fun make_atp_list [] n = []
paulson@17717
    90
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
    91
          let
paulson@17717
    92
            val probfile = prob_pathname n
paulson@17690
    93
            val time = Int.toString (!time_limit)
wenzelm@16802
    94
          in
paulson@17764
    95
            debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17317
    96
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
    97
              versions of Unix.execute treat them differently!*)
paulson@17764
    98
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
    99
            if !prover = "spass"
wenzelm@16802
   100
            then
paulson@17306
   101
              let val optionline = 
paulson@17484
   102
		      if !AtpCommunication.reconstruct 
paulson@17306
   103
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   104
		            inference rules*)
paulson@17690
   105
                      then space_implode "%" (!custom_spass) ^
paulson@17690
   106
                           "%-DocProof%-TimeLimit=" ^ time
paulson@17690
   107
                      else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
paulson@16904
   108
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   109
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   110
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   111
              in 
paulson@17772
   112
                  ([("spass", 
paulson@16897
   113
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   114
                     optionline, probfile)] @ 
paulson@17422
   115
                  (make_atp_list xs (n+1)))
wenzelm@16802
   116
              end
paulson@17306
   117
            else if !prover = "vampire"
quigley@17235
   118
	    then 
paulson@17435
   119
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   120
              in
paulson@17772
   121
                ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@17435
   122
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   123
              end
paulson@17306
   124
      	     else if !prover = "E"
paulson@17306
   125
      	     then
paulson@17306
   126
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   127
	       in
paulson@17772
   128
		  ([("E", Eprover, 
paulson@17690
   129
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   130
		     probfile)] @
paulson@17422
   131
		   (make_atp_list xs (n+1)))
paulson@17306
   132
	       end
paulson@17306
   133
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   134
          end
paulson@15452
   135
paulson@17422
   136
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   137
  in
wenzelm@16802
   138
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   139
    debug "Sent commands to watcher!"
wenzelm@16802
   140
  end
quigley@16357
   141
paulson@17717
   142
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@17717
   143
  and allows the suppression of the suffix "_1" in problem-generation mode.*)
paulson@17717
   144
fun write_problem_files pf (ctxt,th)  =
paulson@17717
   145
  let val prems = Thm.prems_of th
paulson@17717
   146
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17717
   147
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17717
   148
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17717
   149
                     Int.toString (Array.length clause_arr))
paulson@17717
   150
      val thy = ProofContext.theory_of ctxt
paulson@17717
   151
      val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
paulson@17717
   152
      val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@17717
   153
      val arity_clauses = ResTypesSorts.arity_clause_thy thy
paulson@17717
   154
      val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@17717
   155
      val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
paulson@17717
   156
      fun writenext n =
paulson@17772
   157
	if n=0 then []
paulson@17717
   158
	 else
paulson@17717
   159
	   (SELECT_GOAL
paulson@17717
   160
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   161
	      METAHYPS(fn negs => 
paulson@17717
   162
		(write (make_clauses negs) pf n 
paulson@17717
   163
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   164
		 all_tac))]) n th;
paulson@17772
   165
	    pf n :: writenext (n-1))
paulson@17772
   166
      in (writenext (length prems), clause_arr) end;
quigley@15644
   167
paulson@17764
   168
val last_watcher_pid =
paulson@17772
   169
  ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17772
   170
               Posix.Process.pid * string list) option);
paulson@17525
   171
paulson@17525
   172
(*writes out the current clasimpset to a tptp file;
paulson@17525
   173
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   174
val isar_atp = setmp print_mode [] 
paulson@17717
   175
 (fn (ctxt, th) =>
paulson@17717
   176
  if Thm.no_prems th then ()
wenzelm@16802
   177
  else
wenzelm@16802
   178
    let
paulson@17772
   179
      fun reap s = (*Signal handler to tidy away dead processes*)
paulson@17772
   180
	   (case Posix.Process.waitpid_nh(Posix.Process.W_ANY_CHILD, []) of
paulson@17772
   181
		SOME _ => reap s | NONE => ()) 
paulson@17772
   182
           handle OS.SysErr _ => ()
paulson@17772
   183
      val _ = 
paulson@17772
   184
	      IsaSignal.signal (IsaSignal.chld, IsaSignal.SIG_HANDLE reap)
paulson@17502
   185
      val _ = (case !last_watcher_pid of NONE => ()
paulson@17772
   186
               | SOME (_, childout, pid, files) => 
paulson@17502
   187
                  (debug ("Killing old watcher, pid = " ^ 
paulson@17502
   188
                          Int.toString (ResLib.intOfPid pid));
paulson@17772
   189
                   Watcher.killWatcher pid;
paulson@17772
   190
                   ignore (map (try OS.FileSys.remove) files)))
paulson@17502
   191
              handle OS.SysErr _ => debug "Attempt to kill watcher failed";
paulson@17772
   192
      val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   193
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   194
    in
paulson@17772
   195
      last_watcher_pid := SOME (childin, childout, pid, files);
paulson@17772
   196
      debug ("problem files: " ^ space_implode ", " files); 
paulson@17488
   197
      debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
paulson@17717
   198
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   199
    end);
paulson@15608
   200
paulson@17422
   201
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   202
      (fn (ctxt,th) =>
paulson@17717
   203
       if Thm.no_prems th then ()
paulson@17717
   204
       else 
paulson@17717
   205
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   206
         	      else prob_pathname
paulson@17717
   207
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   208
quigley@16357
   209
wenzelm@16802
   210
(** the Isar toplevel hook **)
wenzelm@16802
   211
paulson@17091
   212
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   213
  let
paulson@17091
   214
    val proof = Toplevel.proof_of state
paulson@17091
   215
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   216
        handle Proof.STATE _ => error "No goal present";
wenzelm@16802
   217
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   218
  in
paulson@17764
   219
    debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   220
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   221
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@17525
   222
    debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
paulson@17484
   223
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   224
    hook_count := !hook_count +1;
paulson@17717
   225
    debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   226
    ResClause.init thy;
paulson@17690
   227
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   228
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   229
  end);
quigley@16357
   230
paulson@17091
   231
val call_atpP =
paulson@17746
   232
  OuterSyntax.command 
paulson@17091
   233
    "ProofGeneral.call_atp" 
paulson@17091
   234
    "call automatic theorem provers" 
paulson@17091
   235
    OuterKeyword.diag
paulson@17091
   236
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   237
paulson@17091
   238
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   239
paulson@15347
   240
end;