src/HOL/Tools/res_atp.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18700 f04a8755d6ca
child 18753 aa82bd41555d
permissions -rw-r--r--
simplified type attribute;
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@18675
    24
val time_limit = ref 30;
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
wenzelm@18680
    94
            Output.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@18700
   100
              let val baseopts = "%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
paulson@18700
   101
              val infopts = 
paulson@17484
   102
		      if !AtpCommunication.reconstruct 
paulson@18700
   103
		          (*Proof reconstruction needs a limited set of inf rules*)
paulson@18700
   104
                      then space_implode "%" (!custom_spass)                           
paulson@18700
   105
                      else "-Auto%-SOS=1"
paulson@18700
   106
                  val spass = helper_path "SPASS_HOME" "SPASS"
paulson@18700
   107
            in 
paulson@18700
   108
                ([("spass", spass, infopts ^ baseopts, probfile)] @ 
paulson@17422
   109
                  (make_atp_list xs (n+1)))
wenzelm@16802
   110
              end
paulson@17306
   111
            else if !prover = "vampire"
quigley@17235
   112
	    then 
paulson@17819
   113
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   114
              in
paulson@17772
   115
                ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@17435
   116
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   117
              end
paulson@17306
   118
      	     else if !prover = "E"
paulson@17306
   119
      	     then
paulson@17819
   120
	       let val Eprover = helper_path "E_HOME" "eproof"
paulson@17306
   121
	       in
paulson@17772
   122
		  ([("E", Eprover, 
paulson@17690
   123
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   124
		     probfile)] @
paulson@17422
   125
		   (make_atp_list xs (n+1)))
paulson@17306
   126
	       end
paulson@17306
   127
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   128
          end
paulson@15452
   129
paulson@17422
   130
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   131
  in
wenzelm@16802
   132
    Watcher.callResProvers(childout,atp_list);
wenzelm@18680
   133
    Output.debug "Sent commands to watcher!"
wenzelm@16802
   134
  end
quigley@16357
   135
paulson@17717
   136
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@17717
   137
  and allows the suppression of the suffix "_1" in problem-generation mode.*)
paulson@17717
   138
fun write_problem_files pf (ctxt,th)  =
paulson@17717
   139
  let val prems = Thm.prems_of th
paulson@17717
   140
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17717
   141
              (*FIXME: hack!! need to consider relevance for all prems*)
wenzelm@18680
   142
      val _ = Output.debug ("claset and simprules total clauses = " ^ 
paulson@17717
   143
                     Int.toString (Array.length clause_arr))
paulson@17717
   144
      val thy = ProofContext.theory_of ctxt
mengj@18270
   145
      val classrel_clauses = if !ResClause.keep_types then ResClause.classrel_clauses_thy thy else []
wenzelm@18680
   146
      val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
mengj@18270
   147
      val arity_clauses = if !ResClause.keep_types then ResClause.arity_clause_thy thy else []
wenzelm@18680
   148
      val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@17717
   149
      val write = if !prover = "spass" then dfg_inputs_tfrees else tptp_inputs_tfrees
paulson@17717
   150
      fun writenext n =
paulson@17772
   151
	if n=0 then []
paulson@17717
   152
	 else
paulson@17717
   153
	   (SELECT_GOAL
paulson@17717
   154
	    (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17717
   155
	      METAHYPS(fn negs => 
paulson@17717
   156
		(write (make_clauses negs) pf n 
paulson@17717
   157
		       (axclauses,classrel_clauses,arity_clauses);
paulson@17717
   158
		 all_tac))]) n th;
paulson@17772
   159
	    pf n :: writenext (n-1))
paulson@17772
   160
      in (writenext (length prems), clause_arr) end;
quigley@15644
   161
paulson@17775
   162
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   163
                                    Posix.Process.pid * string list) option);
paulson@17775
   164
paulson@17775
   165
fun kill_last_watcher () =
paulson@17775
   166
    (case !last_watcher_pid of 
paulson@17775
   167
         NONE => ()
paulson@17775
   168
       | SOME (_, childout, pid, files) => 
wenzelm@18680
   169
	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   170
	   Watcher.killWatcher pid;  
paulson@17775
   171
	   ignore (map (try OS.FileSys.remove) files)))
wenzelm@18680
   172
     handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
paulson@17525
   173
paulson@17525
   174
(*writes out the current clasimpset to a tptp file;
paulson@17525
   175
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   176
val isar_atp = setmp print_mode [] 
paulson@17717
   177
 (fn (ctxt, th) =>
paulson@17717
   178
  if Thm.no_prems th then ()
wenzelm@16802
   179
  else
wenzelm@16802
   180
    let
paulson@17775
   181
      val _ = kill_last_watcher()
paulson@17772
   182
      val (files,clause_arr) = write_problem_files prob_pathname (ctxt,th)
paulson@17717
   183
      val (childin, childout, pid) = Watcher.createWatcher (th, clause_arr)
paulson@15608
   184
    in
paulson@17772
   185
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@18680
   186
      Output.debug ("problem files: " ^ space_implode ", " files); 
wenzelm@18680
   187
      Output.debug ("pid: " ^ string_of_pid pid);
paulson@17717
   188
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   189
    end);
paulson@15608
   190
paulson@17422
   191
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   192
      (fn (ctxt,th) =>
paulson@17717
   193
       if Thm.no_prems th then ()
paulson@17717
   194
       else 
paulson@17717
   195
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   196
         	      else prob_pathname
paulson@17717
   197
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   198
quigley@16357
   199
wenzelm@16802
   200
(** the Isar toplevel hook **)
wenzelm@16802
   201
paulson@17091
   202
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   203
  let
paulson@17091
   204
    val proof = Toplevel.proof_of state
wenzelm@18680
   205
    val (ctxt, (_, goal)) = Proof.get_goal proof;
wenzelm@16802
   206
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   207
  in
wenzelm@18680
   208
    Output.debug ("subgoals in isar_atp:\n" ^ 
paulson@17091
   209
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   210
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@18680
   211
    Output.debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
wenzelm@18680
   212
    Output.debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   213
    hook_count := !hook_count +1;
wenzelm@18680
   214
    Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   215
    ResClause.init thy;
paulson@17690
   216
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   217
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   218
  end);
quigley@16357
   219
paulson@17091
   220
val call_atpP =
paulson@17746
   221
  OuterSyntax.command 
paulson@17091
   222
    "ProofGeneral.call_atp" 
paulson@17091
   223
    "call automatic theorem provers" 
paulson@17091
   224
    OuterKeyword.diag
paulson@17091
   225
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   226
paulson@17091
   227
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   228
paulson@15347
   229
end;