src/HOL/Tools/res_atp.ML
author paulson
Wed Oct 05 11:18:06 2005 +0200 (2005-10-05)
changeset 17764 fde495b9e24b
parent 17746 af59c748371d
child 17772 818cec5f82a4
permissions -rw-r--r--
improved process handling. tidied
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@17746
    92
            val goalstring = Sign.string_of_term sign sg_term
paulson@17717
    93
            val probfile = prob_pathname n
paulson@17690
    94
            val time = Int.toString (!time_limit)
wenzelm@16802
    95
          in
paulson@17764
    96
            debug ("goalstring in make_atp_lists is\n" ^ goalstring);
paulson@17764
    97
            debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17317
    98
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
    99
              versions of Unix.execute treat them differently!*)
paulson@17764
   100
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
   101
            if !prover = "spass"
wenzelm@16802
   102
            then
paulson@17306
   103
              let val optionline = 
paulson@17484
   104
		      if !AtpCommunication.reconstruct 
paulson@17306
   105
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   106
		            inference rules*)
paulson@17690
   107
                      then space_implode "%" (!custom_spass) ^
paulson@17690
   108
                           "%-DocProof%-TimeLimit=" ^ time
paulson@17690
   109
                      else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
paulson@16904
   110
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   111
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   112
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   113
              in 
paulson@17422
   114
                  ([("spass", goalstring,
paulson@16897
   115
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   116
                     optionline, probfile)] @ 
paulson@17422
   117
                  (make_atp_list xs (n+1)))
wenzelm@16802
   118
              end
paulson@17306
   119
            else if !prover = "vampire"
quigley@17235
   120
	    then 
paulson@17435
   121
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   122
              in
paulson@17690
   123
                ([("vampire", goalstring, vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@17435
   124
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   125
              end
paulson@17306
   126
      	     else if !prover = "E"
paulson@17306
   127
      	     then
paulson@17306
   128
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   129
	       in
paulson@17422
   130
		  ([("E", goalstring, Eprover, 
paulson@17690
   131
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   132
		     probfile)] @
paulson@17422
   133
		   (make_atp_list xs (n+1)))
paulson@17306
   134
	       end
paulson@17306
   135
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   136
          end
paulson@15452
   137
paulson@17422
   138
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   139
  in
wenzelm@16802
   140
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   141
    debug "Sent commands to watcher!"
wenzelm@16802
   142
  end
quigley@16357
   143
paulson@17717
   144
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@17717
   145
  and allows the suppression of the suffix "_1" in problem-generation mode.*)
paulson@17717
   146
fun write_problem_files pf (ctxt,th)  =
paulson@17717
   147
  let val prems = Thm.prems_of th
paulson@17717
   148
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17717
   149
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17717
   150
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17717
   151
                     Int.toString (Array.length clause_arr))
paulson@17717
   152
      val thy = ProofContext.theory_of ctxt
paulson@17717
   153
      val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
paulson@17717
   154
      val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@17717
   155
      val arity_clauses = ResTypesSorts.arity_clause_thy thy
paulson@17717
   156
      val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@17717
   157
      val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
paulson@17717
   158
      fun writenext n =
paulson@17717
   159
	if n=0 then ()
paulson@17717
   160
	 else
paulson@17717
   161
	   (SELECT_GOAL
paulson@17717
   162
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   163
	      METAHYPS(fn negs => 
paulson@17717
   164
		(write (make_clauses negs) pf n 
paulson@17717
   165
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   166
		 writenext (n-1); 
paulson@17717
   167
		 all_tac))]) n th;
paulson@17717
   168
	    ())
paulson@17717
   169
      in writenext (length prems); clause_arr end;
quigley@15644
   170
paulson@17764
   171
val last_watcher_pid =
paulson@17764
   172
  ref (NONE : (TextIO.instream * TextIO.outstream * Posix.Process.pid) option);
paulson@17484
   173
paulson@17525
   174
paulson@17525
   175
(*writes out the current clasimpset to a tptp file;
paulson@17525
   176
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   177
val isar_atp = setmp print_mode [] 
paulson@17717
   178
 (fn (ctxt, th) =>
paulson@17717
   179
  if Thm.no_prems th then ()
wenzelm@16802
   180
  else
wenzelm@16802
   181
    let
paulson@17764
   182
      val _ = (*Set up signal handler to tidy away dead processes*)
paulson@17764
   183
	      IsaSignal.signal (IsaSignal.chld, 
paulson@17764
   184
	        IsaSignal.SIG_HANDLE (fn _ =>
paulson@17764
   185
		  (Posix.Process.waitpid_nh(Posix.Process.W_ANY_CHILD, []);
paulson@17764
   186
		   debug "Child signal received\n")));  
paulson@17502
   187
      val _ = (case !last_watcher_pid of NONE => ()
paulson@17764
   188
               | SOME (_, childout, pid) => 
paulson@17502
   189
                  (debug ("Killing old watcher, pid = " ^ 
paulson@17502
   190
                          Int.toString (ResLib.intOfPid pid));
paulson@17502
   191
                   Watcher.killWatcher pid))
paulson@17502
   192
              handle OS.SysErr _ => debug "Attempt to kill watcher failed";
paulson@17717
   193
      val clause_arr = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   194
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   195
    in
paulson@17764
   196
      last_watcher_pid := SOME (childin, childout, pid);
paulson@17717
   197
      debug ("proof state: " ^ string_of_thm th);
paulson@17488
   198
      debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
paulson@17717
   199
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   200
    end);
paulson@15608
   201
paulson@17422
   202
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   203
      (fn (ctxt,th) =>
paulson@17717
   204
       if Thm.no_prems th then ()
paulson@17717
   205
       else 
paulson@17717
   206
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   207
         	      else prob_pathname
paulson@17717
   208
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   209
quigley@16357
   210
wenzelm@16802
   211
(** the Isar toplevel hook **)
wenzelm@16802
   212
paulson@17091
   213
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   214
  let
paulson@17091
   215
    val proof = Toplevel.proof_of state
paulson@17091
   216
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   217
        handle Proof.STATE _ => error "No goal present";
wenzelm@16802
   218
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   219
  in
paulson@17764
   220
    debug ("initial thm in isar_atp:\n" ^ 
paulson@17091
   221
           Pretty.string_of (ProofContext.pretty_thm ctxt goal));
paulson@17764
   222
    debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   223
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   224
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@17525
   225
    debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
paulson@17484
   226
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   227
    hook_count := !hook_count +1;
paulson@17717
   228
    debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   229
    ResClause.init thy;
paulson@17690
   230
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   231
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   232
  end);
quigley@16357
   233
paulson@17091
   234
val call_atpP =
paulson@17746
   235
  OuterSyntax.command 
paulson@17091
   236
    "ProofGeneral.call_atp" 
paulson@17091
   237
    "call automatic theorem provers" 
paulson@17091
   238
    OuterKeyword.diag
paulson@17091
   239
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   240
paulson@17091
   241
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   242
paulson@15347
   243
end;