src/HOL/Tools/res_atp.ML
author paulson
Mon Sep 19 18:30:22 2005 +0200 (2005-09-19)
changeset 17488 67376a311a2b
parent 17484 f6a225f97f0a
child 17502 8836793df947
permissions -rw-r--r--
further simplification of the Isabelle-ATP linkup
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
quigley@15644
    63
(*********************************************************************)
quigley@15644
    64
(* write out a subgoal as tptp clauses to the file "probN"           *)
quigley@15644
    65
(* where N is the number of this subgoal                             *)
quigley@15644
    66
(*********************************************************************)
quigley@15644
    67
paulson@17422
    68
fun tptp_inputs_tfrees thms n axclauses =
wenzelm@16802
    69
    let
paulson@16904
    70
      val _ = debug ("in tptp_inputs_tfrees 0")
wenzelm@16802
    71
      val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@16904
    72
      val _ = debug ("in tptp_inputs_tfrees 1")
wenzelm@16802
    73
      val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@16904
    74
      val _ = debug ("in tptp_inputs_tfrees 2")
paulson@17422
    75
      val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
paulson@16904
    76
      val _ = debug ("in tptp_inputs_tfrees 3")
paulson@17484
    77
      val probfile = prob_pathname() ^ "_" ^ string_of_int n
wenzelm@16802
    78
      val out = TextIO.openOut(probfile)
paulson@15608
    79
    in
paulson@17305
    80
      ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
wenzelm@16802
    81
      ResLib.writeln_strs out (tfree_clss @ tptp_clss);
wenzelm@16802
    82
      TextIO.closeOut out;
paulson@16904
    83
      debug probfile
paulson@15608
    84
    end;
paulson@15452
    85
paulson@15608
    86
quigley@16767
    87
(*********************************************************************)
quigley@16767
    88
(* write out a subgoal as DFG clauses to the file "probN"           *)
quigley@16767
    89
(* where N is the number of this subgoal                             *)
quigley@16767
    90
(*********************************************************************)
quigley@17150
    91
paulson@17422
    92
fun dfg_inputs_tfrees thms n axclauses = 
quigley@17150
    93
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@17484
    94
        val probfile = prob_pathname() ^ "_" ^ (string_of_int n)
paulson@17231
    95
        val _ = debug ("about to write out dfg prob file " ^ probfile)
paulson@17234
    96
        val probN = ResClause.clauses2dfg clss ("prob" ^ (string_of_int n)) 
paulson@17422
    97
                        axclauses [] [] []    
quigley@16767
    98
	val out = TextIO.openOut(probfile)
quigley@16767
    99
    in
paulson@17234
   100
	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
paulson@17231
   101
(* (ResLib.writeln_strs out (tfree_clss @ dfg_clss); *)
quigley@17150
   102
    end;
quigley@17150
   103
quigley@16357
   104
quigley@15644
   105
(*********************************************************************)
paulson@17306
   106
(* call prover with settings and problem file for the current subgoal *)
quigley@15644
   107
(*********************************************************************)
quigley@16357
   108
(* now passing in list of skolemized thms and list of sgterms to go with them *)
paulson@17422
   109
fun watcher_call_provers sign sg_terms (childin, childout,pid) =
wenzelm@16802
   110
  let
paulson@17422
   111
    fun make_atp_list [] n = []
paulson@17422
   112
      | make_atp_list ((sg_term)::xs) n =
wenzelm@16802
   113
          let
paulson@16897
   114
            val goalstring = proofstring (Sign.string_of_term sign sg_term)
paulson@16904
   115
            val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
quigley@16357
   116
paulson@17484
   117
            val probfile = prob_pathname() ^ "_" ^ string_of_int n
paulson@17422
   118
            val _ = debug ("prob file in watcher_call_provers is " ^ probfile)
wenzelm@16802
   119
          in
paulson@17317
   120
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   121
              versions of Unix.execute treat them differently!*)
paulson@17306
   122
            if !prover = "spass"
wenzelm@16802
   123
            then
paulson@17306
   124
              let val optionline = 
paulson@17484
   125
		      if !AtpCommunication.reconstruct 
paulson@17306
   126
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   127
		            inference rules*)
paulson@17306
   128
                      then "-" ^ space_implode "%-" (!custom_spass)
paulson@17317
   129
                      else "-DocProof%-TimeLimit=60%-SOS%-FullRed=0" (*Auto mode*)
paulson@16904
   130
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   131
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   132
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   133
              in 
paulson@17422
   134
                  ([("spass", goalstring,
paulson@16897
   135
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   136
                     optionline, probfile)] @ 
paulson@17422
   137
                  (make_atp_list xs (n+1)))
wenzelm@16802
   138
              end
paulson@17306
   139
            else if !prover = "vampire"
quigley@17235
   140
	    then 
paulson@17435
   141
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   142
              in
paulson@17435
   143
                ([("vampire", goalstring, vampire, "-t 60%-m 100000", probfile)] @
paulson@17435
   144
                 (make_atp_list xs (n+1)))       (*BEWARE! spaces in options!*)
wenzelm@16802
   145
              end
paulson@17306
   146
      	     else if !prover = "E"
paulson@17306
   147
      	     then
paulson@17306
   148
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   149
	       in
paulson@17422
   150
		  ([("E", goalstring, Eprover, 
paulson@17306
   151
		     "--tptp-in%-l5%-xAuto%-tAuto%--soft-cpu-limit=60",
paulson@17422
   152
		     probfile)] @
paulson@17422
   153
		   (make_atp_list xs (n+1)))
paulson@17306
   154
	       end
paulson@17306
   155
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   156
          end
paulson@15452
   157
paulson@17422
   158
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   159
  in
wenzelm@16802
   160
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   161
    debug "Sent commands to watcher!"
wenzelm@16802
   162
  end
quigley@16357
   163
paulson@17422
   164
(*We write out problem files for each subgoal, but work is repeated (skolemize)*)
paulson@17422
   165
fun write_problem_files axclauses thm n =
paulson@17422
   166
    if n=0 then ()
quigley@17150
   167
     else
paulson@17422
   168
       (SELECT_GOAL
paulson@17305
   169
        (EVERY1 [rtac ccontr, ResLib.atomize_tac, skolemize_tac, 
paulson@17231
   170
          METAHYPS(fn negs => 
paulson@17306
   171
            (if !prover = "spass" 
paulson@17422
   172
             then dfg_inputs_tfrees (make_clauses negs) n axclauses
paulson@17422
   173
             else tptp_inputs_tfrees (make_clauses negs) n axclauses;
paulson@17422
   174
             write_problem_files axclauses thm (n-1); 
paulson@17422
   175
             all_tac))]) n thm;
paulson@17422
   176
        ());
quigley@15644
   177
quigley@15644
   178
paulson@17484
   179
(*FIXME: WHAT IS THMS FOR????*)
paulson@17484
   180
quigley@15644
   181
(******************************************************************)
quigley@15644
   182
(* called in Isar automatically                                   *)
quigley@15644
   183
(* writes out the current clasimpset to a tptp file               *)
quigley@15644
   184
(* turns off xsymbol at start of function, restoring it at end    *)
quigley@15644
   185
(******************************************************************)
quigley@15779
   186
(*FIX changed to clasimp_file *)
paulson@17484
   187
val isar_atp = setmp print_mode [] 
paulson@17422
   188
 (fn (ctxt, thms, thm) =>
wenzelm@16802
   189
  if Thm.no_prems thm then ()
wenzelm@16802
   190
  else
wenzelm@16802
   191
    let
paulson@17484
   192
      val _= debug ("in isar_atp")
wenzelm@16802
   193
      val thy = ProofContext.theory_of ctxt
wenzelm@16802
   194
      val prems = Thm.prems_of thm
wenzelm@16802
   195
      val thms_string = Meson.concat_with_and (map string_of_thm thms)
wenzelm@16802
   196
      val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
wenzelm@16802
   197
wenzelm@16802
   198
      (*set up variables for writing out the clasimps to a tptp file*)
paulson@17484
   199
      val (clause_arr, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17484
   200
              (*FIXME: hack!! need to consider relevance for all prems*)
paulson@17484
   201
      val _ = debug ("claset and simprules total clauses = " ^ 
paulson@17484
   202
                     string_of_int (Array.length clause_arr))
paulson@17484
   203
      val (childin, childout, pid) = Watcher.createWatcher (thm, clause_arr)
paulson@15608
   204
    in
paulson@16904
   205
      debug ("initial thms: " ^ thms_string);
paulson@16904
   206
      debug ("subgoals: " ^ prems_string);
paulson@17488
   207
      debug ("pid: " ^ Int.toString (ResLib.intOfPid pid));
paulson@17422
   208
      write_problem_files axclauses thm (length prems);
paulson@17422
   209
      watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
wenzelm@16802
   210
    end);
paulson@15608
   211
paulson@17422
   212
val isar_atp_writeonly = setmp print_mode [] 
paulson@17484
   213
 (fn (ctxt, thms: thm list, thm) =>
paulson@17422
   214
  if Thm.no_prems thm then ()
paulson@17422
   215
  else
paulson@17484
   216
    let val prems = Thm.prems_of thm
paulson@17484
   217
        val (_, axclauses) = ResClasimp.get_clasimp_lemmas ctxt (hd prems) 
paulson@17422
   218
    in
paulson@17422
   219
      write_problem_files axclauses thm (length prems)
paulson@17422
   220
    end);
paulson@15452
   221
quigley@16357
   222
paulson@17484
   223
(* convert locally declared rules to axiom clauses: UNUSED *)
paulson@15608
   224
wenzelm@16802
   225
fun subtract_simpset thy ctxt =
wenzelm@16802
   226
  let
wenzelm@16802
   227
    val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
wenzelm@16802
   228
    val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
wenzelm@16802
   229
  in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
quigley@15679
   230
wenzelm@16802
   231
fun subtract_claset thy ctxt =
wenzelm@16802
   232
  let
wenzelm@16802
   233
    val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
wenzelm@16802
   234
    val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
wenzelm@16802
   235
    val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
wenzelm@16802
   236
  in subtract netI1 netI2 @ subtract netE1 netE2 end;
paulson@15608
   237
paulson@15608
   238
quigley@16357
   239
wenzelm@16802
   240
(** the Isar toplevel hook **)
wenzelm@16802
   241
paulson@17091
   242
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   243
  let
paulson@17091
   244
    val proof = Toplevel.proof_of state
paulson@17091
   245
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   246
        handle Proof.STATE _ => error "No goal present";
wenzelm@16802
   247
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   248
  in
paulson@17091
   249
    debug ("initial thm in isar_atp: " ^ 
paulson@17091
   250
           Pretty.string_of (ProofContext.pretty_thm ctxt goal));
paulson@17091
   251
    debug ("subgoals in isar_atp: " ^ 
paulson@17091
   252
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   253
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@16904
   254
    debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
paulson@17484
   255
    debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   256
    hook_count := !hook_count +1;
quigley@17150
   257
    debug ("in hook for time: " ^(string_of_int (!hook_count)) );
paulson@16925
   258
    ResClause.init thy;
paulson@17484
   259
    if !destdir = "" then isar_atp (ctxt, ProofContext.prems_of ctxt, goal)
paulson@17484
   260
    else isar_atp_writeonly (ctxt, ProofContext.prems_of ctxt, goal)
wenzelm@16802
   261
  end);
quigley@16357
   262
paulson@17091
   263
val call_atpP =
paulson@17091
   264
  OuterSyntax.improper_command 
paulson@17091
   265
    "ProofGeneral.call_atp" 
paulson@17091
   266
    "call automatic theorem provers" 
paulson@17091
   267
    OuterKeyword.diag
paulson@17091
   268
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   269
paulson@17091
   270
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   271
paulson@15347
   272
end;