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