src/HOL/Tools/res_atp.ML
author paulson
Thu Feb 09 12:20:31 2006 +0100 (2006-02-09)
changeset 18986 5060ca625e02
parent 18863 a113b6839df1
child 19194 7681c04d8bff
permissions -rw-r--r--
tidying
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@18986
   103
  and allows the suppression of the suffix "_1" in problem-generation mode.
paulson@18986
   104
  FIXME: does not cope with &&, and it isn't easy because one could have multiple
paulson@18986
   105
  subgoals, each involving &&.*)
paulson@17717
   106
fun write_problem_files pf (ctxt,th)  =
paulson@18753
   107
  let val goals = Thm.prems_of th
paulson@18986
   108
      val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals));
paulson@18753
   109
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt goals 
wenzelm@18680
   110
      val _ = Output.debug ("claset and simprules total clauses = " ^ 
paulson@17717
   111
                     Int.toString (Array.length clause_arr))
paulson@17717
   112
      val thy = ProofContext.theory_of ctxt
paulson@18986
   113
      val classrel_clauses = 
paulson@18986
   114
          if !ResClause.keep_types then ResClause.classrel_clauses_thy thy else []
wenzelm@18680
   115
      val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@18986
   116
      val arity_clauses = 
paulson@18986
   117
          if !ResClause.keep_types then ResClause.arity_clause_thy thy else []
wenzelm@18680
   118
      val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@18863
   119
      val write = if !prover = "spass" then ResClause.dfg_write_file 
paulson@18863
   120
                                       else ResClause.tptp_write_file
paulson@17717
   121
      fun writenext n =
paulson@17772
   122
	if n=0 then []
paulson@17717
   123
	 else
paulson@17717
   124
	   (SELECT_GOAL
paulson@17717
   125
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   126
	      METAHYPS(fn negs => 
paulson@18863
   127
		(write (make_clauses negs) (pf n) 
paulson@17717
   128
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   129
		 all_tac))]) n th;
paulson@17772
   130
	    pf n :: writenext (n-1))
paulson@18753
   131
      in (writenext (length goals), clause_arr) end;
quigley@15644
   132
paulson@17775
   133
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   134
                                    Posix.Process.pid * string list) option);
paulson@17775
   135
paulson@17775
   136
fun kill_last_watcher () =
paulson@17775
   137
    (case !last_watcher_pid of 
paulson@17775
   138
         NONE => ()
paulson@17775
   139
       | SOME (_, childout, pid, files) => 
wenzelm@18680
   140
	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   141
	   Watcher.killWatcher pid;  
paulson@17775
   142
	   ignore (map (try OS.FileSys.remove) files)))
wenzelm@18680
   143
     handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
paulson@17525
   144
paulson@17525
   145
(*writes out the current clasimpset to a tptp file;
paulson@17525
   146
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   147
val isar_atp = setmp print_mode [] 
paulson@17717
   148
 (fn (ctxt, th) =>
paulson@17717
   149
  if Thm.no_prems th then ()
wenzelm@16802
   150
  else
wenzelm@16802
   151
    let
paulson@17775
   152
      val _ = kill_last_watcher()
paulson@17772
   153
      val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   154
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   155
    in
paulson@17772
   156
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@18680
   157
      Output.debug ("problem files: " ^ space_implode ", " files); 
wenzelm@18680
   158
      Output.debug ("pid: " ^ string_of_pid pid);
paulson@17717
   159
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   160
    end);
paulson@15608
   161
paulson@17422
   162
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   163
      (fn (ctxt,th) =>
paulson@17717
   164
       if Thm.no_prems th then ()
paulson@17717
   165
       else 
paulson@17717
   166
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   167
         	      else prob_pathname
paulson@17717
   168
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   169
quigley@16357
   170
wenzelm@16802
   171
(** the Isar toplevel hook **)
wenzelm@16802
   172
paulson@17091
   173
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   174
  let
paulson@17091
   175
    val proof = Toplevel.proof_of state
wenzelm@18680
   176
    val (ctxt, (_, goal)) = Proof.get_goal proof;
wenzelm@16802
   177
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   178
  in
wenzelm@18680
   179
    Output.debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   180
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   181
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@18680
   182
    Output.debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   183
    hook_count := !hook_count +1;
wenzelm@18680
   184
    Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   185
    ResClause.init thy;
paulson@17690
   186
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   187
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   188
  end);
quigley@16357
   189
paulson@17091
   190
val call_atpP =
paulson@17746
   191
  OuterSyntax.command 
paulson@17091
   192
    "ProofGeneral.call_atp" 
paulson@17091
   193
    "call automatic theorem provers" 
paulson@17091
   194
    OuterKeyword.diag
paulson@17091
   195
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   196
paulson@17091
   197
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   198
paulson@15347
   199
end;