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