src/HOL/Tools/res_atp.ML
author paulson
Fri Oct 14 10:19:50 2005 +0200 (2005-10-14)
changeset 17845 1438291d57f0
parent 17819 1241e5d31d5b
child 17849 d7619ccf22e6
permissions -rw-r--r--
deletion of Tools/res_types_sorts; removal of absolute numbering of clauses
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@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
wenzelm@16802
    51
paulson@15347
    52
(**** For running in Isar ****)
paulson@15347
    53
paulson@15608
    54
(* same function as that in res_axioms.ML *)
paulson@15608
    55
fun repeat_RS thm1 thm2 =
paulson@15608
    56
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
    57
    in
wenzelm@16802
    58
        if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
    59
    end;
paulson@15608
    60
paulson@15608
    61
(* a special version of repeat_RS *)
paulson@17717
    62
fun repeat_someI_ex th = repeat_RS th someI_ex;
paulson@15608
    63
paulson@17764
    64
fun writeln_strs _   []      = ()
paulson@17764
    65
  | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
paulson@16925
    66
paulson@17502
    67
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@17717
    68
fun tptp_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) =
paulson@17775
    69
  let
paulson@17845
    70
    val clss = ResClause.make_conjecture_clauses thms
paulson@17775
    71
    val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@17775
    72
    val tfree_clss = map ResClause.tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@17775
    73
    val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
paulson@17775
    74
    val arity_cls = map ResClause.tptp_arity_clause arity_clauses
paulson@17775
    75
    val out = TextIO.openOut(pf n)
paulson@17775
    76
  in
paulson@17775
    77
    writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
paulson@17775
    78
    writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
paulson@17775
    79
    TextIO.closeOut out
paulson@17775
    80
  end;
paulson@15452
    81
paulson@17502
    82
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@17717
    83
fun dfg_inputs_tfrees thms pf n (axclauses,classrel_clauses,arity_clauses) = 
paulson@17845
    84
    let val clss = ResClause.make_conjecture_clauses thms
paulson@17525
    85
        (*FIXME: classrel_clauses and arity_clauses*)
paulson@17525
    86
        val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString n)
paulson@17422
    87
                        axclauses [] [] []    
paulson@17717
    88
	val out = TextIO.openOut(pf n)
quigley@16767
    89
    in
paulson@17764
    90
	writeln_strs out [probN]; TextIO.closeOut out
quigley@17150
    91
    end;
quigley@17150
    92
quigley@16357
    93
quigley@15644
    94
(*********************************************************************)
paulson@17306
    95
(* call prover with settings and problem file for the current subgoal *)
quigley@15644
    96
(*********************************************************************)
quigley@16357
    97
(* now passing in list of skolemized thms and list of sgterms to go with them *)
paulson@17764
    98
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
    99
  let
paulson@17422
   100
    fun make_atp_list [] n = []
paulson@17717
   101
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
   102
          let
paulson@17717
   103
            val probfile = prob_pathname n
paulson@17690
   104
            val time = Int.toString (!time_limit)
wenzelm@16802
   105
          in
paulson@17764
   106
            debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17317
   107
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   108
              versions of Unix.execute treat them differently!*)
paulson@17764
   109
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
   110
            if !prover = "spass"
wenzelm@16802
   111
            then
paulson@17306
   112
              let val optionline = 
paulson@17484
   113
		      if !AtpCommunication.reconstruct 
paulson@17306
   114
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   115
		            inference rules*)
paulson@17690
   116
                      then space_implode "%" (!custom_spass) ^
paulson@17690
   117
                           "%-DocProof%-TimeLimit=" ^ time
paulson@17690
   118
                      else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
paulson@16904
   119
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@17819
   120
                  val _ = helper_path "SPASS_HOME" "SPASS"
paulson@16897
   121
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   122
              in 
paulson@17772
   123
                  ([("spass", 
paulson@16897
   124
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   125
                     optionline, probfile)] @ 
paulson@17422
   126
                  (make_atp_list xs (n+1)))
wenzelm@16802
   127
              end
paulson@17306
   128
            else if !prover = "vampire"
quigley@17235
   129
	    then 
paulson@17819
   130
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   131
              in
paulson@17772
   132
                ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@17435
   133
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   134
              end
paulson@17306
   135
      	     else if !prover = "E"
paulson@17306
   136
      	     then
paulson@17819
   137
	       let val Eprover = helper_path "E_HOME" "eproof"
paulson@17306
   138
	       in
paulson@17772
   139
		  ([("E", Eprover, 
paulson@17690
   140
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   141
		     probfile)] @
paulson@17422
   142
		   (make_atp_list xs (n+1)))
paulson@17306
   143
	       end
paulson@17306
   144
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   145
          end
paulson@15452
   146
paulson@17422
   147
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   148
  in
wenzelm@16802
   149
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   150
    debug "Sent commands to watcher!"
wenzelm@16802
   151
  end
quigley@16357
   152
paulson@17717
   153
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@17717
   154
  and allows the suppression of the suffix "_1" in problem-generation mode.*)
paulson@17717
   155
fun write_problem_files pf (ctxt,th)  =
paulson@17717
   156
  let val prems = Thm.prems_of th
paulson@17717
   157
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17717
   158
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17717
   159
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17717
   160
                     Int.toString (Array.length clause_arr))
paulson@17717
   161
      val thy = ProofContext.theory_of ctxt
paulson@17845
   162
      val classrel_clauses = ResClause.classrel_clauses_thy thy
paulson@17717
   163
      val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@17845
   164
      val arity_clauses = ResClause.arity_clause_thy thy
paulson@17717
   165
      val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@17717
   166
      val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
paulson@17717
   167
      fun writenext n =
paulson@17772
   168
	if n=0 then []
paulson@17717
   169
	 else
paulson@17717
   170
	   (SELECT_GOAL
paulson@17717
   171
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   172
	      METAHYPS(fn negs => 
paulson@17717
   173
		(write (make_clauses negs) pf n 
paulson@17717
   174
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   175
		 all_tac))]) n th;
paulson@17772
   176
	    pf n :: writenext (n-1))
paulson@17772
   177
      in (writenext (length prems), clause_arr) end;
quigley@15644
   178
paulson@17775
   179
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   180
                                    Posix.Process.pid * string list) option);
paulson@17775
   181
paulson@17775
   182
fun kill_last_watcher () =
paulson@17775
   183
    (case !last_watcher_pid of 
paulson@17775
   184
         NONE => ()
paulson@17775
   185
       | SOME (_, childout, pid, files) => 
paulson@17819
   186
	  (debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   187
	   Watcher.killWatcher pid;  
paulson@17775
   188
	   ignore (map (try OS.FileSys.remove) files)))
paulson@17775
   189
     handle OS.SysErr _ => debug "Attempt to kill watcher failed";
paulson@17525
   190
paulson@17525
   191
(*writes out the current clasimpset to a tptp file;
paulson@17525
   192
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   193
val isar_atp = setmp print_mode [] 
paulson@17717
   194
 (fn (ctxt, th) =>
paulson@17717
   195
  if Thm.no_prems th then ()
wenzelm@16802
   196
  else
wenzelm@16802
   197
    let
paulson@17775
   198
      val _ = kill_last_watcher()
paulson@17772
   199
      val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   200
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   201
    in
paulson@17772
   202
      last_watcher_pid := SOME (childin, childout, pid, files);
paulson@17772
   203
      debug ("problem files: " ^ space_implode ", " files); 
paulson@17819
   204
      debug ("pid: " ^ string_of_pid pid);
paulson@17717
   205
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   206
    end);
paulson@15608
   207
paulson@17422
   208
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   209
      (fn (ctxt,th) =>
paulson@17717
   210
       if Thm.no_prems th then ()
paulson@17717
   211
       else 
paulson@17717
   212
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   213
         	      else prob_pathname
paulson@17717
   214
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   215
quigley@16357
   216
wenzelm@16802
   217
(** the Isar toplevel hook **)
wenzelm@16802
   218
paulson@17091
   219
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   220
  let
paulson@17091
   221
    val proof = Toplevel.proof_of state
paulson@17091
   222
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   223
        handle Proof.STATE _ => error "No goal present";
wenzelm@16802
   224
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   225
  in
paulson@17764
   226
    debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   227
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   228
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@17525
   229
    debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
paulson@17484
   230
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   231
    hook_count := !hook_count +1;
paulson@17717
   232
    debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   233
    ResClause.init thy;
paulson@17690
   234
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   235
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   236
  end);
quigley@16357
   237
paulson@17091
   238
val call_atpP =
paulson@17746
   239
  OuterSyntax.command 
paulson@17091
   240
    "ProofGeneral.call_atp" 
paulson@17091
   241
    "call automatic theorem provers" 
paulson@17091
   242
    OuterKeyword.diag
paulson@17091
   243
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   244
paulson@17091
   245
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   246
paulson@15347
   247
end;