src/HOL/Tools/res_atp.ML
author paulson
Thu Sep 15 17:46:00 2005 +0200 (2005-09-15)
changeset 17422 3b237822985d
parent 17404 d16c3a62c396
child 17435 0eed5a1c00c1
permissions -rw-r--r--
massive tidy-up and simplification
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
quigley@17150
    12
  val hook_count: int ref
paulson@15347
    13
end;
paulson@15347
    14
wenzelm@16802
    15
structure ResAtp: RES_ATP =
paulson@15347
    16
struct
paulson@15347
    17
quigley@17150
    18
quigley@17150
    19
val call_atp = ref false;
quigley@17150
    20
val hook_count = ref 0;
quigley@17150
    21
paulson@16904
    22
fun debug_tac tac = (debug "testing"; tac);
quigley@16478
    23
paulson@17404
    24
val prover = ref "E";   (* use E as the default prover *)
paulson@17305
    25
val custom_spass =   (*specialized options for SPASS*)
paulson@17317
    26
      ref ["Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub",
paulson@17305
    27
           "-DocProof","-TimeLimit=60"];
paulson@15347
    28
quigley@15644
    29
val prob_file = File.tmp_path (Path.basic "prob");
quigley@15644
    30
wenzelm@16802
    31
paulson@15347
    32
(**** for Isabelle/ML interface  ****)
paulson@15347
    33
paulson@16897
    34
(*Remove unwanted characters such as ? and newline from the textural 
paulson@16897
    35
  representation of a theorem (surely they don't need to be produced in 
paulson@16897
    36
  the first place?) *)
paulson@15608
    37
paulson@16897
    38
fun is_proof_char ch = (#" " <= ch andalso ch <= #"~" andalso ch <> #"?");
paulson@16897
    39
paulson@16897
    40
val proofstring =
paulson@16897
    41
    String.translate (fn c => if is_proof_char c then str c else "");
paulson@15608
    42
paulson@15452
    43
paulson@15347
    44
(**** For running in Isar ****)
paulson@15347
    45
paulson@15608
    46
(* same function as that in res_axioms.ML *)
paulson@15608
    47
fun repeat_RS thm1 thm2 =
paulson@15608
    48
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
    49
    in
wenzelm@16802
    50
        if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
    51
    end;
paulson@15608
    52
paulson@15608
    53
(* a special version of repeat_RS *)
paulson@15608
    54
fun repeat_someI_ex thm = repeat_RS thm someI_ex;
paulson@15608
    55
paulson@16925
    56
quigley@15644
    57
(*********************************************************************)
quigley@15644
    58
(* write out a subgoal as tptp clauses to the file "probN"           *)
quigley@15644
    59
(* where N is the number of this subgoal                             *)
quigley@15644
    60
(*********************************************************************)
quigley@15644
    61
paulson@17422
    62
fun tptp_inputs_tfrees thms n axclauses =
wenzelm@16802
    63
    let
paulson@16904
    64
      val _ = debug ("in tptp_inputs_tfrees 0")
wenzelm@16802
    65
      val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@16904
    66
      val _ = debug ("in tptp_inputs_tfrees 1")
wenzelm@16802
    67
      val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
paulson@16904
    68
      val _ = debug ("in tptp_inputs_tfrees 2")
paulson@17422
    69
      val tfree_clss = map ResClause.tfree_clause (ResLib.flat_noDup tfree_litss)
paulson@16904
    70
      val _ = debug ("in tptp_inputs_tfrees 3")
wenzelm@16802
    71
      val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
wenzelm@16802
    72
      val out = TextIO.openOut(probfile)
paulson@15608
    73
    in
paulson@17305
    74
      ResLib.writeln_strs out (List.concat (map ResClause.tptp_clause axclauses));
wenzelm@16802
    75
      ResLib.writeln_strs out (tfree_clss @ tptp_clss);
wenzelm@16802
    76
      TextIO.closeOut out;
paulson@16904
    77
      debug probfile
paulson@15608
    78
    end;
paulson@15452
    79
paulson@15608
    80
quigley@16767
    81
(*********************************************************************)
quigley@16767
    82
(* write out a subgoal as DFG clauses to the file "probN"           *)
quigley@16767
    83
(* where N is the number of this subgoal                             *)
quigley@16767
    84
(*********************************************************************)
quigley@17150
    85
paulson@17422
    86
fun dfg_inputs_tfrees thms n axclauses = 
quigley@17150
    87
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
quigley@16767
    88
        val probfile = (File.platform_path prob_file) ^ "_" ^ (string_of_int n)
paulson@17231
    89
        val _ = debug ("about to write out dfg prob file " ^ probfile)
paulson@17234
    90
        val probN = ResClause.clauses2dfg clss ("prob" ^ (string_of_int n)) 
paulson@17422
    91
                        axclauses [] [] []    
quigley@16767
    92
	val out = TextIO.openOut(probfile)
quigley@16767
    93
    in
paulson@17234
    94
	(ResLib.writeln_strs out [probN]; TextIO.closeOut out; debug probfile )
paulson@17231
    95
(* (ResLib.writeln_strs out (tfree_clss @ dfg_clss); *)
quigley@17150
    96
    end;
quigley@17150
    97
quigley@16357
    98
quigley@15644
    99
(*********************************************************************)
paulson@17306
   100
(* call prover with settings and problem file for the current subgoal *)
quigley@15644
   101
(*********************************************************************)
quigley@16357
   102
(* now passing in list of skolemized thms and list of sgterms to go with them *)
paulson@17422
   103
fun watcher_call_provers sign sg_terms (childin, childout,pid) =
wenzelm@16802
   104
  let
paulson@17422
   105
    fun make_atp_list [] n = []
paulson@17422
   106
      | make_atp_list ((sg_term)::xs) n =
wenzelm@16802
   107
          let
paulson@16897
   108
            val goalstring = proofstring (Sign.string_of_term sign sg_term)
paulson@16904
   109
            val _ = debug ("goalstring in make_atp_lists is " ^ goalstring)
quigley@16357
   110
paulson@16897
   111
            val probfile = File.platform_path prob_file ^ "_" ^ (string_of_int n)
paulson@17422
   112
            val _ = debug ("prob file in watcher_call_provers is " ^ probfile)
wenzelm@16802
   113
          in
paulson@17317
   114
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   115
              versions of Unix.execute treat them differently!*)
paulson@17306
   116
            if !prover = "spass"
wenzelm@16802
   117
            then
paulson@17306
   118
              let val optionline = 
paulson@17306
   119
		      if !SpassComm.reconstruct 
paulson@17306
   120
		          (*Proof reconstruction works for only a limited set of 
paulson@17306
   121
		            inference rules*)
paulson@17306
   122
                      then "-" ^ space_implode "%-" (!custom_spass)
paulson@17317
   123
                      else "-DocProof%-TimeLimit=60%-SOS%-FullRed=0" (*Auto mode*)
paulson@16904
   124
                  val _ = debug ("SPASS option string is " ^ optionline)
paulson@16897
   125
                  val _ = ResLib.helper_path "SPASS_HOME" "SPASS"
paulson@16897
   126
                    (*We've checked that SPASS is there for ATP/spassshell to run.*)
paulson@16897
   127
              in 
paulson@17422
   128
                  ([("spass", goalstring,
paulson@16897
   129
                     getenv "ISABELLE_HOME" ^ "/src/HOL/Tools/ATP/spassshell",
paulson@17422
   130
                     optionline, probfile)] @ 
paulson@17422
   131
                  (make_atp_list xs (n+1)))
wenzelm@16802
   132
              end
paulson@17306
   133
            else if !prover = "vampire"
quigley@17235
   134
	    then 
wenzelm@16802
   135
              let val vampire = ResLib.helper_path "VAMPIRE_HOME" "vkernel"
wenzelm@16802
   136
              in
paulson@17422
   137
                ([("vampire", goalstring, vampire, "-t60%-m100000",
paulson@17422
   138
                   probfile)] @
paulson@17422
   139
                 (make_atp_list xs (n+1)))
wenzelm@16802
   140
              end
paulson@17306
   141
      	     else if !prover = "E"
paulson@17306
   142
      	     then
paulson@17306
   143
	       let val Eprover = ResLib.helper_path "E_HOME" "eproof"
paulson@17306
   144
	       in
paulson@17422
   145
		  ([("E", goalstring, Eprover, 
paulson@17306
   146
		     "--tptp-in%-l5%-xAuto%-tAuto%--soft-cpu-limit=60",
paulson@17422
   147
		     probfile)] @
paulson@17422
   148
		   (make_atp_list xs (n+1)))
paulson@17306
   149
	       end
paulson@17306
   150
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   151
          end
paulson@15452
   152
paulson@17422
   153
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   154
  in
wenzelm@16802
   155
    Watcher.callResProvers(childout,atp_list);
paulson@17422
   156
    debug "Sent commands to watcher!"
wenzelm@16802
   157
  end
quigley@16357
   158
paulson@17422
   159
(*We write out problem files for each subgoal, but work is repeated (skolemize)*)
paulson@17422
   160
fun write_problem_files axclauses thm n =
paulson@17422
   161
    if n=0 then ()
quigley@17150
   162
     else
paulson@17422
   163
       (SELECT_GOAL
paulson@17305
   164
        (EVERY1 [rtac ccontr, ResLib.atomize_tac, skolemize_tac, 
paulson@17231
   165
          METAHYPS(fn negs => 
paulson@17306
   166
            (if !prover = "spass" 
paulson@17422
   167
             then dfg_inputs_tfrees (make_clauses negs) n axclauses
paulson@17422
   168
             else tptp_inputs_tfrees (make_clauses negs) n axclauses;
paulson@17422
   169
             write_problem_files axclauses thm (n-1); 
paulson@17422
   170
             all_tac))]) n thm;
paulson@17422
   171
        ());
quigley@15644
   172
quigley@15644
   173
quigley@15644
   174
(******************************************************************)
quigley@15644
   175
(* called in Isar automatically                                   *)
quigley@15644
   176
(* writes out the current clasimpset to a tptp file               *)
quigley@15644
   177
(* turns off xsymbol at start of function, restoring it at end    *)
quigley@15644
   178
(******************************************************************)
quigley@15779
   179
(*FIX changed to clasimp_file *)
paulson@17422
   180
val isar_atp' = setmp print_mode [] 
paulson@17422
   181
 (fn (ctxt, thms, thm) =>
wenzelm@16802
   182
  if Thm.no_prems thm then ()
wenzelm@16802
   183
  else
wenzelm@16802
   184
    let
paulson@16904
   185
      val _= debug ("in isar_atp'")
wenzelm@16802
   186
      val thy = ProofContext.theory_of ctxt
wenzelm@16802
   187
      val prems = Thm.prems_of thm
wenzelm@16802
   188
      val thms_string = Meson.concat_with_and (map string_of_thm thms)
wenzelm@16802
   189
      val prems_string = Meson.concat_with_and (map (Sign.string_of_term thy) prems)
wenzelm@16802
   190
wenzelm@16802
   191
      (*set up variables for writing out the clasimps to a tptp file*)
quigley@17150
   192
      val (clause_arr, num_of_clauses, axclauses) =
paulson@17305
   193
        ResClasimp.get_clasimp_lemmas thy (hd prems) (*FIXME: hack!! need to do all prems*)
paulson@17422
   194
      val _ = debug ("claset and simprules total " ^ (string_of_int num_of_clauses)^
paulson@17422
   195
                  " clauses")
paulson@17422
   196
      val (childin, childout, pid) = 
paulson@17422
   197
          Watcher.createWatcher (thm, clause_arr, num_of_clauses)
wenzelm@16802
   198
      val pid_string =
wenzelm@16802
   199
        string_of_int (Word.toInt (Word.fromLargeWord (Posix.Process.pidToWord pid)))
paulson@15608
   200
    in
paulson@16904
   201
      debug ("initial thms: " ^ thms_string);
paulson@16904
   202
      debug ("subgoals: " ^ prems_string);
paulson@16904
   203
      debug ("pid: "^ pid_string);
paulson@17422
   204
      write_problem_files axclauses thm (length prems);
paulson@17422
   205
      watcher_call_provers (sign_of_thm thm) (Thm.prems_of thm) (childin, childout, pid)
wenzelm@16802
   206
    end);
paulson@15608
   207
paulson@17422
   208
val isar_atp_writeonly = setmp print_mode [] 
paulson@17422
   209
 (fn (ctxt, thms, thm) =>
paulson@17422
   210
  if Thm.no_prems thm then ()
paulson@17422
   211
  else
paulson@17422
   212
    let
paulson@17422
   213
      val thy = ProofContext.theory_of ctxt
paulson@17422
   214
      val prems = Thm.prems_of thm
paulson@17422
   215
paulson@17422
   216
      (*set up variables for writing out the clasimps to a tptp file*)
paulson@17422
   217
      val (clause_arr, num_of_clauses, axclauses) =
paulson@17422
   218
        ResClasimp.get_clasimp_lemmas thy (hd prems) (*FIXME: hack!! need to do all prems*)
paulson@17422
   219
    in
paulson@17422
   220
      write_problem_files axclauses thm (length prems)
paulson@17422
   221
    end);
paulson@15452
   222
paulson@15608
   223
fun get_thms_cs claset =
wenzelm@16802
   224
  let val {safeEs, safeIs, hazEs, hazIs, ...} = rep_cs claset
wenzelm@16802
   225
  in safeEs @ safeIs @ hazEs @ hazIs end;
quigley@16357
   226
paulson@15608
   227
fun append_name name [] _ = []
wenzelm@16802
   228
  | append_name name (thm :: thms) k =
wenzelm@16802
   229
      Thm.name_thm ((name ^ "_" ^ string_of_int k), thm) :: append_name name thms (k + 1);
paulson@15608
   230
wenzelm@16802
   231
fun append_names (name :: names) (thms :: thmss) =
wenzelm@16802
   232
  append_name name thms 0 :: append_names names thmss;
quigley@16357
   233
paulson@15608
   234
fun get_thms_ss [] = []
paulson@15608
   235
  | get_thms_ss thms =
wenzelm@16802
   236
      let
wenzelm@16802
   237
        val names = map Thm.name_of_thm thms
paulson@15608
   238
        val thms' = map (mksimps mksimps_pairs) thms
paulson@15608
   239
        val thms'' = append_names names thms'
wenzelm@16802
   240
      in
wenzelm@16802
   241
        ResLib.flat_noDup thms''
wenzelm@16802
   242
      end;
paulson@15608
   243
paulson@15452
   244
paulson@15608
   245
(* convert locally declared rules to axiom clauses *)
paulson@15608
   246
wenzelm@16802
   247
fun subtract_simpset thy ctxt =
wenzelm@16802
   248
  let
wenzelm@16802
   249
    val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
wenzelm@16802
   250
    val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
wenzelm@16802
   251
  in map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2) end;
quigley@15679
   252
wenzelm@16802
   253
fun subtract_claset thy ctxt =
wenzelm@16802
   254
  let
wenzelm@16802
   255
    val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
wenzelm@16802
   256
    val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
wenzelm@16802
   257
    val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
wenzelm@16802
   258
  in subtract netI1 netI2 @ subtract netE1 netE2 end;
paulson@15608
   259
paulson@15608
   260
quigley@16357
   261
wenzelm@16802
   262
(** the Isar toplevel hook **)
wenzelm@16802
   263
paulson@17091
   264
val invoke_atp = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@16802
   265
  let
paulson@17091
   266
    val proof = Toplevel.proof_of state
paulson@17091
   267
    val (ctxt, (_, goal)) = Proof.get_goal proof
paulson@17091
   268
        handle Proof.STATE _ => error "No goal present";
quigley@17150
   269
wenzelm@16802
   270
    val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   271
wenzelm@16802
   272
    (* FIXME presently unused *)
wenzelm@16802
   273
    val ss_thms = subtract_simpset thy ctxt;
wenzelm@16802
   274
    val cs_thms = subtract_claset thy ctxt;
wenzelm@16802
   275
  in
paulson@17091
   276
    debug ("initial thm in isar_atp: " ^ 
paulson@17091
   277
           Pretty.string_of (ProofContext.pretty_thm ctxt goal));
paulson@17091
   278
    debug ("subgoals in isar_atp: " ^ 
paulson@17091
   279
           Pretty.string_of (ProofContext.pretty_term ctxt
paulson@17091
   280
             (Logic.mk_conjunction_list (Thm.prems_of goal))));
paulson@16904
   281
    debug ("number of subgoals in isar_atp: " ^ string_of_int (Thm.nprems_of goal));
quigley@17150
   282
    hook_count := !hook_count +1;
quigley@17150
   283
    debug ("in hook for time: " ^(string_of_int (!hook_count)) );
paulson@16925
   284
    ResClause.init thy;
wenzelm@16802
   285
    isar_atp' (ctxt, ProofContext.prems_of ctxt, goal)
wenzelm@16802
   286
  end);
quigley@16357
   287
paulson@17091
   288
val call_atpP =
paulson@17091
   289
  OuterSyntax.improper_command 
paulson@17091
   290
    "ProofGeneral.call_atp" 
paulson@17091
   291
    "call automatic theorem provers" 
paulson@17091
   292
    OuterKeyword.diag
paulson@17091
   293
    (Scan.succeed (Toplevel.no_timing o invoke_atp));
paulson@17091
   294
paulson@17091
   295
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   296
paulson@15347
   297
end;