src/HOL/Tools/res_atp.ML
author paulson
Tue Sep 20 13:17:55 2005 +0200 (2005-09-20 ago)
changeset 17502 8836793df947
parent 17488 67376a311a2b
child 17525 ae5bb6001afb
permissions -rw-r--r--
further tidying; killing of old Watcher loops
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@15347
    15
end;
paulson@15347
    16
wenzelm@16802
    17
structure ResAtp: RES_ATP =
paulson@15347
    18
struct
paulson@15347
    19
quigley@17150
    20
val call_atp = ref false;
quigley@17150
    21
val hook_count = ref 0;
quigley@17150
    22
paulson@17404
    23
val prover = ref "E";   (* use E as the default prover *)
paulson@17305
    24
val custom_spass =   (*specialized options for SPASS*)
paulson@17317
    25
      ref ["Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub",
paulson@17305
    26
           "-DocProof","-TimeLimit=60"];
paulson@15347
    27
paulson@17484
    28
val destdir = ref "";   (*Empty means write files to /tmp*)
paulson@17484
    29
val problem_name = ref "prob";
paulson@17484
    30
paulson@17484
    31
fun prob_pathname() = 
paulson@17484
    32
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
paulson@17484
    33
  else if File.exists (File.unpack_platform_path (!destdir))
paulson@17484
    34
  then !destdir ^ "/" ^ !problem_name
paulson@17484
    35
  else error ("No such directory: " ^ !destdir);
quigley@15644
    36
wenzelm@16802
    37
paulson@15347
    38
(**** for Isabelle/ML interface  ****)
paulson@15347
    39
paulson@16897
    40
(*Remove unwanted characters such as ? and newline from the textural 
paulson@16897
    41
  representation of a theorem (surely they don't need to be produced in 
paulson@16897
    42
  the first place?) *)
paulson@15608
    43
paulson@16897
    44
fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
paulson@16897
    45
paulson@16897
    46
val proofstring =
paulson@16897
    47
    String.translate (fn c => if is_proof_char c then str c else "");
paulson@15608
    48
paulson@15452
    49
paulson@15347
    50
(**** For running in Isar ****)
paulson@15347
    51
paulson@15608
    52
(* same function as that in res_axioms.ML *)
paulson@15608
    53
fun repeat_RS thm1 thm2 =
paulson@15608
    54
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
    55
    in
wenzelm@16802
    56
        if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
    57
    end;
paulson@15608
    58
paulson@15608
    59
(* a special version of repeat_RS *)
paulson@15608
    60
fun repeat_someI_ex thm = repeat_RS thm someI_ex;
paulson@15608
    61
paulson@16925
    62
paulson@17502
    63
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@17422
    64
fun tptp_inputs_tfrees thms n axclauses =
wenzelm@16802
    65
    let
paulson@16904
    66
      val _ = debug ("in tptp_inputs_tfrees 0")
wenzelm@16802
    67
      val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@16904
    68
      val _ = debug ("in tptp_inputs_tfrees 1")
wenzelm@16802
    69
      val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@16904
    70
      val _ = debug ("in tptp_inputs_tfrees 2")
paulson@17422
    71
      val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
paulson@16904
    72
      val _ = debug ("in tptp_inputs_tfrees 3")
paulson@17484
    73
      val probfile = prob_pathname() ^ "_" ^ string_of_int n
wenzelm@16802
    74
      val out = TextIO.openOut(probfile)
paulson@15608
    75
    in
paulson@17305
    76
      ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
wenzelm@16802
    77
      ResLib.writeln_strs out (tfree_clss @ tptp_clss);
wenzelm@16802
    78
      TextIO.closeOut out;
paulson@16904
    79
      debug probfile
paulson@15608
    80
    end;
paulson@15452
    81
paulson@17502
    82
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@17422
    83
fun dfg_inputs_tfrees thms n axclauses = 
quigley@17150
    84
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@17484
    85
        val probfile = prob_pathname() ^ "_" ^ (string_of_int n)
paulson@17231
    86
        val _ = debug ("about to write out dfg prob file " ^ probfile)
paulson@17502
    87
        val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ string_of_int n)
paulson@17422
    88
                        axclauses [] [] []    
quigley@16767
    89
	val out = TextIO.openOut(probfile)
quigley@16767
    90
    in
paulson@17234
    91
	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
quigley@17150
    92
    end;
quigley@17150
    93
quigley@16357
    94
quigley@15644
    95
(*********************************************************************)
paulson@17306
    96
(* call prover with settings and problem file for the current subgoal *)
quigley@15644
    97
(*********************************************************************)
quigley@16357
    98
(* now passing in list of skolemized thms and list of sgterms to go with them *)
paulson@17422
    99
fun watcher_call_provers sign sg_terms (childin, childout,pid) =
wenzelm@16802
   100
  let
paulson@17422
   101
    fun make_atp_list [] n = []
paulson@17422
   102
      | make_atp_list ((sg_term)::xs) n =
wenzelm@16802
   103
          let
paulson@16897
   104
            val goalstring = proofstring (Sign.string_of_term sign sg_term)
paulson@16904
   105
            val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
quigley@16357
   106
paulson@17484
   107
            val probfile = prob_pathname() ^ "_" ^ string_of_int n
paulson@17502
   108
            val _ = debug ("problem file in watcher_call_provers is " ^ probfile)
wenzelm@16802
   109
          in
paulson@17317
   110
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   111
              versions of Unix.execute treat them differently!*)
paulson@17306
   112
            if !prover = "spass"
wenzelm@16802
   113
            then
paulson@17306
   114
              let val optionline = 
paulson@17484
   115
		      if !AtpCommunication.reconstruct 
paulson@17306
   116
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   117
		            inference rules*)
paulson@17306
   118
                      then "-" ^ space_implode "%-" (!custom_spass)
paulson@17317
   119
                      else "-DocProof%-TimeLimit=60%-SOS%-FullRed=0" (*Auto mode*)
paulson@16904
   120
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   121
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   122
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   123
              in 
paulson@17422
   124
                  ([("spass", goalstring,
paulson@16897
   125
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   126
                     optionline, probfile)] @ 
paulson@17422
   127
                  (make_atp_list xs (n+1)))
wenzelm@16802
   128
              end
paulson@17306
   129
            else if !prover = "vampire"
quigley@17235
   130
	    then 
paulson@17435
   131
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   132
              in
paulson@17435
   133
                ([("vampire", goalstring, vampire, "-t 60%-m 100000", probfile)] @
paulson@17435
   134
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   135
              end
paulson@17306
   136
      	     else if !prover = "E"
paulson@17306
   137
      	     then
paulson@17306
   138
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   139
	       in
paulson@17422
   140
		  ([("E", goalstring, Eprover, 
paulson@17306
   141
		     "--tptp-in%-l5%-xAuto%-tAuto%--soft-cpu-limit=60",
paulson@17422
   142
		     probfile)] @
paulson@17422
   143
		   (make_atp_list xs (n+1)))
paulson@17306
   144
	       end
paulson@17306
   145
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   146
          end
paulson@15452
   147
paulson@17422
   148
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   149
  in
wenzelm@16802
   150
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   151
    debug "Sent commands to watcher!"
wenzelm@16802
   152
  end
quigley@16357
   153
paulson@17422
   154
(*We write out problem files for each subgoal, but work is repeated (skolemize)*)
paulson@17422
   155
fun write_problem_files axclauses thm n =
paulson@17422
   156
    if n=0 then ()
quigley@17150
   157
     else
paulson@17422
   158
       (SELECT_GOAL
paulson@17305
   159
        (EVERY1 [rtac ccontr, ResLib.atomize_tac, skolemize_tac, 
paulson@17231
   160
          METAHYPS(fn negs => 
paulson@17306
   161
            (if !prover = "spass" 
paulson@17422
   162
             then dfg_inputs_tfrees (make_clauses negs) n axclauses
paulson@17422
   163
             else tptp_inputs_tfrees (make_clauses negs) n axclauses;
paulson@17422
   164
             write_problem_files axclauses thm (n-1); 
paulson@17422
   165
             all_tac))]) n thm;
paulson@17422
   166
        ());
quigley@15644
   167
paulson@17502
   168
val last_watcher_pid = ref (NONE : Posix.Process.pid option);
paulson@17484
   169
quigley@15644
   170
(******************************************************************)
quigley@15644
   171
(* called in Isar automatically                                   *)
quigley@15644
   172
(* writes out the current clasimpset to a tptp file               *)
quigley@15644
   173
(* turns off xsymbol at start of function, restoring it at end    *)
quigley@15644
   174
(******************************************************************)
quigley@15779
   175
(*FIX changed to clasimp_file *)
paulson@17484
   176
val isar_atp = setmp print_mode [] 
paulson@17502
   177
 (fn (ctxt, thm) =>
wenzelm@16802
   178
  if Thm.no_prems thm then ()
wenzelm@16802
   179
  else
wenzelm@16802
   180
    let
paulson@17484
   181
      val _= debug ("in isar_atp")
wenzelm@16802
   182
      val thy = ProofContext.theory_of ctxt
wenzelm@16802
   183
      val prems = Thm.prems_of thm
wenzelm@16802
   184
      val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
wenzelm@16802
   185
paulson@17502
   186
      val _ = (case !last_watcher_pid of NONE => ()
paulson@17502
   187
               | SOME pid => (*FIXME: should kill ATP processes too; at least they time out*)
paulson@17502
   188
                  (debug ("Killing old watcher, pid = " ^ 
paulson@17502
   189
                          Int.toString (ResLib.intOfPid pid));
paulson@17502
   190
                   Watcher.killWatcher pid))
paulson@17502
   191
              handle OS.SysErr _ => debug "Attempt to kill watcher failed";
wenzelm@16802
   192
      (*set up variables for writing out the clasimps to a tptp file*)
paulson@17484
   193
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17484
   194
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17484
   195
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17484
   196
                     string_of_int (Array.length clause_arr))
paulson@17484
   197
      val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr)
paulson@15608
   198
    in
paulson@17502
   199
      last_watcher_pid := SOME pid;
paulson@16904
   200
      debug ("subgoals: " ^ prems_string);
paulson@17488
   201
      debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
paulson@17422
   202
      write_problem_files axclauses thm (length prems);
paulson@17422
   203
      watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
wenzelm@16802
   204
    end);
paulson@15608
   205
paulson@17422
   206
val isar_atp_writeonly = setmp print_mode [] 
paulson@17502
   207
 (fn (ctxt, thm) =>
paulson@17422
   208
  if Thm.no_prems thm then ()
paulson@17422
   209
  else
paulson@17484
   210
    let val prems = Thm.prems_of thm
paulson@17484
   211
        val (_, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17422
   212
    in
paulson@17422
   213
      write_problem_files axclauses thm (length prems)
paulson@17422
   214
    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@17091
   226
    debug ("initial thm in isar_atp: " ^ 
paulson@17091
   227
           Pretty.string_of (ProofContext.pretty_thm ctxt goal));
paulson@17091
   228
    debug ("subgoals in isar_atp: " ^ 
paulson@17091
   229
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   230
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@16904
   231
    debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
paulson@17484
   232
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   233
    hook_count := !hook_count +1;
quigley@17150
   234
    debug ("in hook for time: " ^(string_of_int (!hook_count)) );
paulson@16925
   235
    ResClause.init thy;
paulson@17502
   236
    if !destdir = "" then isar_atp (ctxt, goal)
paulson@17502
   237
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   238
  end);
quigley@16357
   239
paulson@17091
   240
val call_atpP =
paulson@17091
   241
  OuterSyntax.improper_command 
paulson@17091
   242
    "ProofGeneral.call_atp" 
paulson@17091
   243
    "call automatic theorem provers" 
paulson@17091
   244
    OuterKeyword.diag
paulson@17091
   245
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   246
paulson@17091
   247
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   248
paulson@15347
   249
end;