src/HOL/Tools/res_atp.ML
author paulson
Wed Sep 28 11:16:27 2005 +0200 (2005-09-28)
changeset 17690 8ba7c3cd24a8
parent 17525 ae5bb6001afb
child 17717 7c6a96cbc966
permissions -rw-r--r--
time limit option; fixed bug concerning first line of ATP output
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@17484
    32
fun prob_pathname() = 
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
wenzelm@16802
    38
paulson@15347
    39
(**** for Isabelle/ML interface  ****)
paulson@15347
    40
paulson@16897
    41
(*Remove unwanted characters such as ? and newline from the textural 
paulson@16897
    42
  representation of a theorem (surely they don't need to be produced in 
paulson@16897
    43
  the first place?) *)
paulson@15608
    44
paulson@16897
    45
fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
paulson@16897
    46
paulson@16897
    47
val proofstring =
paulson@16897
    48
    String.translate (fn c => if is_proof_char c then str c else "");
paulson@15608
    49
paulson@15452
    50
paulson@15347
    51
(**** For running in Isar ****)
paulson@15347
    52
paulson@15608
    53
(* same function as that in res_axioms.ML *)
paulson@15608
    54
fun repeat_RS thm1 thm2 =
paulson@15608
    55
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
    56
    in
wenzelm@16802
    57
        if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
    58
    end;
paulson@15608
    59
paulson@15608
    60
(* a special version of repeat_RS *)
paulson@15608
    61
fun repeat_someI_ex thm = repeat_RS thm someI_ex;
paulson@15608
    62
paulson@16925
    63
paulson@17502
    64
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@17525
    65
fun tptp_inputs_tfrees thms n (axclauses,classrel_clauses,arity_clauses) =
wenzelm@16802
    66
    let
wenzelm@16802
    67
      val clss = map (ResClause.make_conjecture_clause_thm) thms
wenzelm@16802
    68
      val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@17422
    69
      val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
paulson@17525
    70
      val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
paulson@17525
    71
      val arity_cls = map ResClause.tptp_arity_clause arity_clauses
paulson@17525
    72
      val probfile = prob_pathname() ^ "_" ^ Int.toString n
wenzelm@16802
    73
      val out = TextIO.openOut(probfile)
paulson@15608
    74
    in
paulson@17305
    75
      ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
paulson@17525
    76
      ResLib.writeln_strs out (tfree_clss @ tptp_clss @ classrel_cls @ arity_cls);
wenzelm@16802
    77
      TextIO.closeOut out;
paulson@16904
    78
      debug probfile
paulson@15608
    79
    end;
paulson@15452
    80
paulson@17502
    81
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@17525
    82
fun dfg_inputs_tfrees thms n (axclauses,classrel_clauses,arity_clauses) = 
quigley@17150
    83
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@17525
    84
        val probfile = prob_pathname() ^ "_" ^ (Int.toString n)
paulson@17525
    85
        (*FIXME: classrel_clauses and arity_clauses*)
paulson@17231
    86
        val _ = debug ("about to write out dfg prob file " ^ probfile)
paulson@17525
    87
        val probN = ResClause.clauses2dfg clss (!problem_name ^ "_" ^ Int.toString 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@17525
   107
            val probfile = prob_pathname() ^ "_" ^ Int.toString n
paulson@17690
   108
            val time = Int.toString (!time_limit)
paulson@17502
   109
            val _ = debug ("problem file in watcher_call_provers is " ^ probfile)
wenzelm@16802
   110
          in
paulson@17317
   111
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   112
              versions of Unix.execute treat them differently!*)
paulson@17306
   113
            if !prover = "spass"
wenzelm@16802
   114
            then
paulson@17306
   115
              let val optionline = 
paulson@17484
   116
		      if !AtpCommunication.reconstruct 
paulson@17306
   117
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   118
		            inference rules*)
paulson@17690
   119
                      then space_implode "%" (!custom_spass) ^
paulson@17690
   120
                           "%-DocProof%-TimeLimit=" ^ time
paulson@17690
   121
                      else "-DocProof%-SOS%-FullRed=0%-TimeLimit=" ^ time (*Auto mode*)
paulson@16904
   122
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   123
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   124
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   125
              in 
paulson@17422
   126
                  ([("spass", goalstring,
paulson@16897
   127
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   128
                     optionline, probfile)] @ 
paulson@17422
   129
                  (make_atp_list xs (n+1)))
wenzelm@16802
   130
              end
paulson@17306
   131
            else if !prover = "vampire"
quigley@17235
   132
	    then 
paulson@17435
   133
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   134
              in
paulson@17690
   135
                ([("vampire", goalstring, vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@17435
   136
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   137
              end
paulson@17306
   138
      	     else if !prover = "E"
paulson@17306
   139
      	     then
paulson@17306
   140
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   141
	       in
paulson@17422
   142
		  ([("E", goalstring, Eprover, 
paulson@17690
   143
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   144
		     probfile)] @
paulson@17422
   145
		   (make_atp_list xs (n+1)))
paulson@17306
   146
	       end
paulson@17306
   147
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   148
          end
paulson@15452
   149
paulson@17422
   150
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   151
  in
wenzelm@16802
   152
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   153
    debug "Sent commands to watcher!"
wenzelm@16802
   154
  end
quigley@16357
   155
paulson@17525
   156
(*We write out problem files for each subgoal*)
paulson@17525
   157
fun write_problem_files clause thm n =
paulson@17422
   158
    if n=0 then ()
quigley@17150
   159
     else
paulson@17422
   160
       (SELECT_GOAL
paulson@17525
   161
        (EVERY1 [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac, 
paulson@17231
   162
          METAHYPS(fn negs => 
paulson@17306
   163
            (if !prover = "spass" 
paulson@17525
   164
             then dfg_inputs_tfrees (make_clauses negs) n clause
paulson@17525
   165
             else tptp_inputs_tfrees (make_clauses negs) n clause;
paulson@17525
   166
             write_problem_files clause thm (n-1); 
paulson@17422
   167
             all_tac))]) n thm;
paulson@17422
   168
        ());
quigley@15644
   169
paulson@17502
   170
val last_watcher_pid = ref (NONE : Posix.Process.pid option);
paulson@17484
   171
paulson@17525
   172
paulson@17525
   173
(*writes out the current clasimpset to a tptp file;
paulson@17525
   174
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   175
val isar_atp = setmp print_mode [] 
paulson@17502
   176
 (fn (ctxt, thm) =>
wenzelm@16802
   177
  if Thm.no_prems thm then ()
wenzelm@16802
   178
  else
wenzelm@16802
   179
    let
paulson@17525
   180
      val _= debug "in isar_atp"
wenzelm@16802
   181
      val thy = ProofContext.theory_of ctxt
wenzelm@16802
   182
      val prems = Thm.prems_of thm
wenzelm@16802
   183
      val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
wenzelm@16802
   184
paulson@17502
   185
      val _ = (case !last_watcher_pid of NONE => ()
paulson@17502
   186
               | SOME pid => (*FIXME: should kill ATP processes too; at least they time out*)
paulson@17502
   187
                  (debug ("Killing old watcher, pid = " ^ 
paulson@17502
   188
                          Int.toString (ResLib.intOfPid pid));
paulson@17502
   189
                   Watcher.killWatcher pid))
paulson@17502
   190
              handle OS.SysErr _ => debug "Attempt to kill watcher failed";
wenzelm@16802
   191
      (*set up variables for writing out the clasimps to a tptp file*)
paulson@17484
   192
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17484
   193
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17484
   194
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17525
   195
                     Int.toString (Array.length clause_arr))
paulson@17525
   196
      val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
paulson@17525
   197
      val _ = debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
paulson@17525
   198
      val arity_clauses = ResTypesSorts.arity_clause_thy thy
paulson@17525
   199
      val _ = debug ("arity clauses = " ^ Int.toString (length arity_clauses))
paulson@17484
   200
      val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr)
paulson@15608
   201
    in
paulson@17502
   202
      last_watcher_pid := SOME pid;
paulson@16904
   203
      debug ("subgoals: " ^ prems_string);
paulson@17488
   204
      debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
paulson@17525
   205
      write_problem_files (axclauses,classrel_clauses,arity_clauses) thm (length prems);
paulson@17422
   206
      watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
wenzelm@16802
   207
    end);
paulson@15608
   208
paulson@17422
   209
val isar_atp_writeonly = setmp print_mode [] 
paulson@17502
   210
 (fn (ctxt, thm) =>
paulson@17422
   211
  if Thm.no_prems thm then ()
paulson@17422
   212
  else
paulson@17525
   213
    let 
paulson@17525
   214
      val prems = Thm.prems_of thm
paulson@17525
   215
      val (_, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17525
   216
      val thy = ProofContext.theory_of ctxt
paulson@17525
   217
      val classrel_clauses = ResTypesSorts.classrel_clauses_thy thy
paulson@17525
   218
      val arity_clauses = ResTypesSorts.arity_clause_thy thy
paulson@17422
   219
    in
paulson@17525
   220
      write_problem_files (axclauses,classrel_clauses,arity_clauses) thm (length prems)
paulson@17422
   221
    end);
paulson@15452
   222
quigley@16357
   223
wenzelm@16802
   224
(** the Isar toplevel hook **)
wenzelm@16802
   225
paulson@17091
   226
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   227
  let
paulson@17091
   228
    val proof = Toplevel.proof_of state
paulson@17091
   229
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   230
        handle Proof.STATE _ => error "No goal present";
wenzelm@16802
   231
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   232
  in
paulson@17091
   233
    debug ("initial thm in isar_atp: " ^ 
paulson@17091
   234
           Pretty.string_of (ProofContext.pretty_thm ctxt goal));
paulson@17091
   235
    debug ("subgoals in isar_atp: " ^ 
paulson@17091
   236
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   237
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@17525
   238
    debug ("number of subgoals in isar_atp: " ^ Int.toString (Thm.nprems_of goal));
paulson@17484
   239
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   240
    hook_count := !hook_count +1;
paulson@17525
   241
    debug ("in hook for time: " ^(Int.toString (!hook_count)) );
paulson@16925
   242
    ResClause.init thy;
paulson@17690
   243
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   244
    else isar_atp_writeonly (ctxt, goal)
wenzelm@16802
   245
  end);
quigley@16357
   246
paulson@17091
   247
val call_atpP =
paulson@17091
   248
  OuterSyntax.improper_command 
paulson@17091
   249
    "ProofGeneral.call_atp" 
paulson@17091
   250
    "call automatic theorem provers" 
paulson@17091
   251
    OuterKeyword.diag
paulson@17091
   252
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   253
paulson@17091
   254
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   255
paulson@15347
   256
end;