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