src/HOL/Tools/res_atp.ML
author paulson
Tue Jan 31 10:39:13 2006 +0100 (2006-01-31)
changeset 18863 a113b6839df1
parent 18798 ca02a2077955
child 18986 5060ca625e02
permissions -rw-r--r--
reorganization of code to support DFG otuput
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
paulson@17849
    13
  val helper_path: string -> string -> string
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@18675
    23
val time_limit = ref 30;
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@17819
    32
(*Return the path to a "helper" like SPASS or tptp2X, first checking that
paulson@17819
    33
  it exists.  FIXME: modify to use Path primitives and move to some central place.*)  
paulson@17819
    34
fun helper_path evar base =
paulson@17819
    35
  case getenv evar of
paulson@17819
    36
      "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
paulson@17819
    37
    | home => 
paulson@17819
    38
        let val path = home ^ "/" ^ base
paulson@17819
    39
        in  if File.exists (File.unpack_platform_path path) then path 
paulson@17819
    40
	    else error ("Could not find the file " ^ path)
paulson@17819
    41
	end;  
paulson@17819
    42
paulson@17717
    43
fun probfile_nosuffix _ = 
paulson@17484
    44
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
paulson@17484
    45
  else if File.exists (File.unpack_platform_path (!destdir))
paulson@17484
    46
  then !destdir ^ "/" ^ !problem_name
paulson@17484
    47
  else error ("No such directory: " ^ !destdir);
quigley@15644
    48
paulson@17717
    49
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
paulson@17717
    50
paulson@17306
    51
(* call prover with settings and problem file for the current subgoal *)
paulson@17764
    52
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
    53
  let
paulson@17422
    54
    fun make_atp_list [] n = []
paulson@17717
    55
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
    56
          let
paulson@17717
    57
            val probfile = prob_pathname n
paulson@17690
    58
            val time = Int.toString (!time_limit)
wenzelm@16802
    59
          in
wenzelm@18680
    60
            Output.debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17317
    61
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
    62
              versions of Unix.execute treat them differently!*)
paulson@17764
    63
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
    64
            if !prover = "spass"
wenzelm@16802
    65
            then
paulson@18700
    66
              let val baseopts = "%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
paulson@18700
    67
              val infopts = 
paulson@17484
    68
		      if !AtpCommunication.reconstruct 
paulson@18700
    69
		          (*Proof reconstruction needs a limited set of inf rules*)
paulson@18700
    70
                      then space_implode "%" (!custom_spass)                           
paulson@18700
    71
                      else "-Auto%-SOS=1"
paulson@18700
    72
                  val spass = helper_path "SPASS_HOME" "SPASS"
paulson@18700
    73
            in 
paulson@18700
    74
                ([("spass", spass, infopts ^ baseopts, probfile)] @ 
paulson@18798
    75
                  make_atp_list xs (n+1))
wenzelm@16802
    76
              end
paulson@17306
    77
            else if !prover = "vampire"
quigley@17235
    78
	    then 
paulson@17819
    79
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
    80
              in
paulson@17772
    81
                ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@18798
    82
                 make_atp_list xs (n+1))       (*BEWARE! spaces in options!*)
wenzelm@16802
    83
              end
paulson@17306
    84
      	     else if !prover = "E"
paulson@17306
    85
      	     then
paulson@17819
    86
	       let val Eprover = helper_path "E_HOME" "eproof"
paulson@17306
    87
	       in
paulson@17772
    88
		  ([("E", Eprover, 
paulson@17690
    89
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
    90
		     probfile)] @
paulson@18798
    91
		   make_atp_list xs (n+1))
paulson@17306
    92
	       end
paulson@17306
    93
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
    94
          end
paulson@15452
    95
paulson@17422
    96
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
    97
  in
wenzelm@16802
    98
    Watcher.callResProvers(childout,atp_list);
wenzelm@18680
    99
    Output.debug "Sent commands to watcher!"
wenzelm@16802
   100
  end
quigley@16357
   101
paulson@17717
   102
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@17717
   103
  and allows the suppression of the suffix "_1" in problem-generation mode.*)
paulson@17717
   104
fun write_problem_files pf (ctxt,th)  =
paulson@18753
   105
  let val goals = Thm.prems_of th
paulson@18753
   106
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt goals 
wenzelm@18680
   107
      val _ = Output.debug ("claset and simprules total clauses = " ^ 
paulson@17717
   108
                     Int.toString (Array.length clause_arr))
paulson@17717
   109
      val thy = ProofContext.theory_of ctxt
mengj@18270
   110
      val classrel_clauses = if !ResClause.keep_types then ResClause.classrel_clauses_thy thy else []
wenzelm@18680
   111
      val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
mengj@18270
   112
      val arity_clauses = if !ResClause.keep_types then ResClause.arity_clause_thy thy else []
wenzelm@18680
   113
      val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@18863
   114
      val write = if !prover = "spass" then ResClause.dfg_write_file 
paulson@18863
   115
                                       else ResClause.tptp_write_file
paulson@17717
   116
      fun writenext n =
paulson@17772
   117
	if n=0 then []
paulson@17717
   118
	 else
paulson@17717
   119
	   (SELECT_GOAL
paulson@17717
   120
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   121
	      METAHYPS(fn negs => 
paulson@18863
   122
		(write (make_clauses negs) (pf n) 
paulson@17717
   123
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   124
		 all_tac))]) n th;
paulson@17772
   125
	    pf n :: writenext (n-1))
paulson@18753
   126
      in (writenext (length goals), clause_arr) end;
quigley@15644
   127
paulson@17775
   128
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   129
                                    Posix.Process.pid * string list) option);
paulson@17775
   130
paulson@17775
   131
fun kill_last_watcher () =
paulson@17775
   132
    (case !last_watcher_pid of 
paulson@17775
   133
         NONE => ()
paulson@17775
   134
       | SOME (_, childout, pid, files) => 
wenzelm@18680
   135
	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   136
	   Watcher.killWatcher pid;  
paulson@17775
   137
	   ignore (map (try OS.FileSys.remove) files)))
wenzelm@18680
   138
     handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
paulson@17525
   139
paulson@17525
   140
(*writes out the current clasimpset to a tptp file;
paulson@17525
   141
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   142
val isar_atp = setmp print_mode [] 
paulson@17717
   143
 (fn (ctxt, th) =>
paulson@17717
   144
  if Thm.no_prems th then ()
wenzelm@16802
   145
  else
wenzelm@16802
   146
    let
paulson@17775
   147
      val _ = kill_last_watcher()
paulson@17772
   148
      val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   149
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   150
    in
paulson@17772
   151
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@18680
   152
      Output.debug ("problem files: " ^ space_implode ", " files); 
wenzelm@18680
   153
      Output.debug ("pid: " ^ string_of_pid pid);
paulson@17717
   154
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   155
    end);
paulson@15608
   156
paulson@17422
   157
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   158
      (fn (ctxt,th) =>
paulson@17717
   159
       if Thm.no_prems th then ()
paulson@17717
   160
       else 
paulson@17717
   161
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   162
         	      else prob_pathname
paulson@17717
   163
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   164
quigley@16357
   165
wenzelm@16802
   166
(** the Isar toplevel hook **)
wenzelm@16802
   167
paulson@17091
   168
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   169
  let
paulson@17091
   170
    val proof = Toplevel.proof_of state
wenzelm@18680
   171
    val (ctxt, (_, goal)) = Proof.get_goal proof;
wenzelm@16802
   172
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   173
  in
wenzelm@18680
   174
    Output.debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   175
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   176
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@18680
   177
    Output.debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
wenzelm@18680
   178
    Output.debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   179
    hook_count := !hook_count +1;
wenzelm@18680
   180
    Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   181
    ResClause.init thy;
paulson@17690
   182
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   183
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   184
  end);
quigley@16357
   185
paulson@17091
   186
val call_atpP =
paulson@17746
   187
  OuterSyntax.command 
paulson@17091
   188
    "ProofGeneral.call_atp" 
paulson@17091
   189
    "call automatic theorem provers" 
paulson@17091
   190
    OuterKeyword.diag
paulson@17091
   191
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   192
paulson@17091
   193
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   194
paulson@15347
   195
end;