src/HOL/Tools/res_atp.ML
author paulson
Tue Mar 07 16:46:54 2006 +0100 (2006-03-07)
changeset 19205 4ec788c69f82
parent 19194 7681c04d8bff
child 19227 d15f2baa7ecc
permissions -rw-r--r--
Tidying. New invoke_atp_ml for top-level debugging. Flag to force FOL mode.
mengj@19194
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory, NICTA
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
mengj@19194
    16
   
mengj@19194
    17
  datatype mode = Auto | Fol | Hol
paulson@19205
    18
  val write_subgoal_file: mode -> Proof.context -> thm list -> thm list -> int -> string
mengj@19194
    19
  val vampire_time: int ref
mengj@19194
    20
  val eprover_time: int ref
mengj@19194
    21
  val run_vampire: int -> unit
mengj@19194
    22
  val run_eprover: int -> unit
mengj@19194
    23
  val vampireLimit: unit -> int
mengj@19194
    24
  val eproverLimit: unit -> int
paulson@19205
    25
  val atp_method: (ProofContext.context -> thm list -> int -> Tactical.tactic) ->
mengj@19194
    26
		  Method.src -> ProofContext.context -> Method.method
mengj@19194
    27
  val cond_rm_tmp: string -> unit
mengj@19194
    28
  val keep_atp_input: bool ref
mengj@19194
    29
  val fol_keep_types: bool ref
mengj@19194
    30
  val hol_full_types: unit -> unit
mengj@19194
    31
  val hol_partial_types: unit -> unit
mengj@19194
    32
  val hol_const_types_only: unit -> unit
mengj@19194
    33
  val hol_no_types: unit -> unit
mengj@19194
    34
  val hol_typ_level: unit -> ResHolClause.type_level
mengj@19194
    35
  val run_relevance_filter: bool ref
paulson@19205
    36
  val invoke_atp_ml : ProofContext.context * thm -> unit
paulson@15347
    37
end;
paulson@15347
    38
paulson@19205
    39
structure ResAtp : RES_ATP =
paulson@15347
    40
struct
paulson@15347
    41
mengj@19194
    42
(********************************************************************)
mengj@19194
    43
(* some settings for both background automatic ATP calling procedure*)
mengj@19194
    44
(* and also explicit ATP invocation methods                         *)
mengj@19194
    45
(********************************************************************)
mengj@19194
    46
mengj@19194
    47
(*** background linkup ***)
mengj@19194
    48
val call_atp = ref false; 
quigley@17150
    49
val hook_count = ref 0;
paulson@18675
    50
val time_limit = ref 30;
paulson@17404
    51
val prover = ref "E";   (* use E as the default prover *)
paulson@17305
    52
val custom_spass =   (*specialized options for SPASS*)
paulson@17690
    53
      ref ["-Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub"];
paulson@17484
    54
val destdir = ref "";   (*Empty means write files to /tmp*)
paulson@17484
    55
val problem_name = ref "prob";
paulson@17484
    56
paulson@17819
    57
(*Return the path to a "helper" like SPASS or tptp2X, first checking that
paulson@17819
    58
  it exists.  FIXME: modify to use Path primitives and move to some central place.*)  
paulson@17819
    59
fun helper_path evar base =
paulson@17819
    60
  case getenv evar of
paulson@17819
    61
      "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
paulson@17819
    62
    | home => 
paulson@17819
    63
        let val path = home ^ "/" ^ base
paulson@17819
    64
        in  if File.exists (File.unpack_platform_path path) then path 
paulson@17819
    65
	    else error ("Could not find the file " ^ path)
paulson@17819
    66
	end;  
paulson@17819
    67
paulson@17717
    68
fun probfile_nosuffix _ = 
paulson@17484
    69
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
paulson@17484
    70
  else if File.exists (File.unpack_platform_path (!destdir))
paulson@17484
    71
  then !destdir ^ "/" ^ !problem_name
paulson@17484
    72
  else error ("No such directory: " ^ !destdir);
quigley@15644
    73
paulson@17717
    74
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
paulson@17717
    75
mengj@19194
    76
mengj@19194
    77
(*** ATP methods ***)
mengj@19194
    78
val vampire_time = ref 60;
mengj@19194
    79
val eprover_time = ref 60;
mengj@19194
    80
fun run_vampire time =  
mengj@19194
    81
    if (time >0) then vampire_time:= time
mengj@19194
    82
    else vampire_time:=60;
mengj@19194
    83
mengj@19194
    84
fun run_eprover time = 
mengj@19194
    85
    if (time > 0) then eprover_time:= time
mengj@19194
    86
    else eprover_time:=60;
mengj@19194
    87
mengj@19194
    88
fun vampireLimit () = !vampire_time;
mengj@19194
    89
fun eproverLimit () = !eprover_time;
mengj@19194
    90
mengj@19194
    91
val keep_atp_input = ref false;
mengj@19194
    92
val fol_keep_types = ResClause.keep_types;
mengj@19194
    93
val hol_full_types = ResHolClause.full_types;
mengj@19194
    94
val hol_partial_types = ResHolClause.partial_types;
mengj@19194
    95
val hol_const_types_only = ResHolClause.const_types_only;
mengj@19194
    96
val hol_no_types = ResHolClause.no_types;
mengj@19194
    97
fun hol_typ_level () = ResHolClause.find_typ_level ();
mengj@19194
    98
fun is_typed_hol () = 
mengj@19194
    99
    let val tp_level = hol_typ_level()
mengj@19194
   100
    in
mengj@19194
   101
	not (tp_level = ResHolClause.T_NONE)
mengj@19194
   102
    end;
mengj@19194
   103
val include_combS = ResHolClause.include_combS;
mengj@19194
   104
val include_min_comb = ResHolClause.include_min_comb;
mengj@19194
   105
mengj@19194
   106
fun atp_input_file () =
mengj@19194
   107
    let val file = !problem_name 
mengj@19194
   108
    in
mengj@19194
   109
	if !destdir = "" then File.platform_path (File.tmp_path (Path.basic file))
mengj@19194
   110
	else if File.exists (File.unpack_platform_path (!destdir))
mengj@19194
   111
	then !destdir ^ "/" ^ file
mengj@19194
   112
	else error ("No such directory: " ^ !destdir)
mengj@19194
   113
    end;
mengj@19194
   114
mengj@19194
   115
val include_simpset = ref false;
mengj@19194
   116
val include_claset = ref false; 
mengj@19194
   117
val include_atpset = ref true;
mengj@19194
   118
val add_simpset = (fn () => include_simpset:=true);
mengj@19194
   119
val add_claset = (fn () => include_claset:=true);
mengj@19194
   120
val add_clasimp = (fn () => include_simpset:=true;include_claset:=true);
mengj@19194
   121
val add_atpset = (fn () => include_atpset:=true);
mengj@19194
   122
val rm_simpset = (fn () => include_simpset:=false);
mengj@19194
   123
val rm_claset = (fn () => include_claset:=false);
mengj@19194
   124
val rm_clasimp = (fn () => include_simpset:=false;include_claset:=false);
mengj@19194
   125
val rm_atpset = (fn () => include_atpset:=false);
mengj@19194
   126
mengj@19194
   127
(*** paths for HOL helper files ***)
mengj@19194
   128
fun full_typed_comb_inclS () =
mengj@19194
   129
    helper_path "E_HOME" "additionals/full_comb_inclS"
mengj@19194
   130
    handle _ => helper_path "VAMPIRE_HOME" "additionals/full_comb_inclS";
mengj@19194
   131
mengj@19194
   132
fun full_typed_comb_noS () =
mengj@19194
   133
    helper_path "E_HOME" "additionals/full_comb_noS"
mengj@19194
   134
    handle _ => helper_path "VAMPIRE_HOME" "additionals/full_comb_noS";
mengj@19194
   135
									      
mengj@19194
   136
fun partial_typed_comb_inclS () =
mengj@19194
   137
    helper_path "E_HOME" "additionals/par_comb_inclS"
mengj@19194
   138
    handle _ => helper_path "VAMPIRE_HOME" "additionals/par_comb_inclS";
mengj@19194
   139
mengj@19194
   140
fun partial_typed_comb_noS () =
mengj@19194
   141
    helper_path "E_HOME" "additionals/par_comb_noS"
mengj@19194
   142
    handle _ => helper_path "VAMPIRE_HOME" "additionals/par_comb_noS";
mengj@19194
   143
mengj@19194
   144
fun const_typed_comb_inclS () =
mengj@19194
   145
    helper_path "E_HOME" "additionals/const_comb_inclS"
mengj@19194
   146
    handle _ => helper_path "VAMPIRE_HOME" "additionals/const_comb_inclS";
mengj@19194
   147
mengj@19194
   148
fun const_typed_comb_noS () =
mengj@19194
   149
    helper_path "E_HOME" "additionals/const_comb_noS"
mengj@19194
   150
    handle _ => helper_path "VAMPIRE_HOME" "additionals/const_comb_noS";
mengj@19194
   151
mengj@19194
   152
fun untyped_comb_inclS () =
mengj@19194
   153
    helper_path "E_HOME" "additionals/u_comb_inclS"
mengj@19194
   154
    handle _ => helper_path "VAMPIRE_HOME" "additionals/u_comb_inclS";
mengj@19194
   155
mengj@19194
   156
fun untyped_comb_noS () =
mengj@19194
   157
    helper_path "E_HOME" "additionals/u_comb_noS"
mengj@19194
   158
    handle _ => helper_path "VAMPIRE_HOME" "additionals/u_comb_noS";
mengj@19194
   159
mengj@19194
   160
fun full_typed_HOL_helper1 () =
mengj@19194
   161
    helper_path "E_HOME" "additionals/full_helper1"
mengj@19194
   162
    handle _ => helper_path "VAMPIRE_HOME" "additionals/full_helper1";
mengj@19194
   163
mengj@19194
   164
fun partial_typed_HOL_helper1 () = 
mengj@19194
   165
    helper_path "E_HOME" "additionals/par_helper1"
mengj@19194
   166
    handle _ => helper_path "VAMPIRE_HOME" "additionals/par_helper1";
mengj@19194
   167
mengj@19194
   168
fun const_typed_HOL_helper1 () = 
mengj@19194
   169
    helper_path "E_HOME" "additionals/const_helper1"
mengj@19194
   170
    handle _ => helper_path "VAMPIRE_HOME" "additionals/const_helper1";
mengj@19194
   171
mengj@19194
   172
fun untyped_HOL_helper1 () = 
mengj@19194
   173
    helper_path "E_HOME" "additionals/u_helper1"
mengj@19194
   174
    handle _ => helper_path "VAMPIRE_HOME" "additionals/u_helper1";
mengj@19194
   175
mengj@19194
   176
fun get_full_typed_helpers () =
mengj@19194
   177
    (full_typed_HOL_helper1 (), full_typed_comb_noS (), full_typed_comb_inclS ());
mengj@19194
   178
mengj@19194
   179
fun get_partial_typed_helpers () =
mengj@19194
   180
    (partial_typed_HOL_helper1 (), partial_typed_comb_noS (), partial_typed_comb_inclS ());
mengj@19194
   181
mengj@19194
   182
fun get_const_typed_helpers () =
mengj@19194
   183
    (const_typed_HOL_helper1 (), const_typed_comb_noS (), const_typed_comb_inclS ());
mengj@19194
   184
mengj@19194
   185
fun get_untyped_helpers () =
mengj@19194
   186
    (untyped_HOL_helper1 (), untyped_comb_noS (), untyped_comb_inclS ());
mengj@19194
   187
mengj@19194
   188
fun get_all_helpers () =
mengj@19194
   189
    (get_full_typed_helpers (), get_partial_typed_helpers (), get_const_typed_helpers (), get_untyped_helpers ());
mengj@19194
   190
mengj@19194
   191
mengj@19194
   192
(**** relevance filter ****)
mengj@19194
   193
val run_relevance_filter = ref true;
mengj@19194
   194
mengj@19194
   195
(******************************************************************)
mengj@19194
   196
(* detect whether a given problem (clauses) is FOL/HOL/HOLC/HOLCS *)
mengj@19194
   197
(******************************************************************)
mengj@19194
   198
mengj@19194
   199
datatype logic = FOL | HOL | HOLC | HOLCS;
mengj@19194
   200
mengj@19194
   201
fun string_of_logic FOL = "FOL"
mengj@19194
   202
  | string_of_logic HOL = "HOL"
mengj@19194
   203
  | string_of_logic HOLC = "HOLC"
mengj@19194
   204
  | string_of_logic HOLCS = "HOLCS";
mengj@19194
   205
mengj@19194
   206
mengj@19194
   207
fun is_fol_logic FOL = true
mengj@19194
   208
  | is_fol_logic  _ = false
mengj@19194
   209
mengj@19194
   210
mengj@19194
   211
(*HOLCS will not occur here*)
mengj@19194
   212
fun upgrade_lg HOLC _ = HOLC
mengj@19194
   213
  | upgrade_lg HOL HOLC = HOLC
mengj@19194
   214
  | upgrade_lg HOL _ = HOL
mengj@19194
   215
  | upgrade_lg FOL lg = lg; 
mengj@19194
   216
mengj@19194
   217
(* check types *)
mengj@19194
   218
fun has_bool (Type("bool",_)) = true
mengj@19194
   219
  | has_bool (Type(_, Ts)) = exists has_bool Ts
mengj@19194
   220
  | has_bool _ = false;
mengj@19194
   221
mengj@19194
   222
fun has_bool_arg tp = 
mengj@19194
   223
    let val (targs,tr) = strip_type tp
mengj@19194
   224
    in
mengj@19194
   225
	exists has_bool targs
mengj@19194
   226
    end;
mengj@19194
   227
mengj@19194
   228
fun is_fn_tp (Type("fun",_)) = true
mengj@19194
   229
  | is_fn_tp _ = false;
mengj@19194
   230
mengj@19194
   231
mengj@19194
   232
exception FN_LG of term;
mengj@19194
   233
mengj@19194
   234
fun fn_lg (t as Const(f,tp)) (lg,seen) = 
mengj@19194
   235
    if has_bool tp then (upgrade_lg HOL lg, t ins seen) else (lg, t ins seen) 
mengj@19194
   236
  | fn_lg (t as Free(f,tp)) (lg,seen) = 
mengj@19194
   237
    if has_bool tp then (upgrade_lg HOL lg, t ins seen) else (lg, t ins seen) 
mengj@19194
   238
  | fn_lg (t as Var(f,tp)) (lg,seen) =
mengj@19194
   239
    if is_fn_tp tp orelse has_bool tp then (upgrade_lg HOL lg,t ins seen)
mengj@19194
   240
    else (lg,t ins seen)
mengj@19194
   241
  | fn_lg (t as Abs(_,_,_)) (lg,seen) = (upgrade_lg HOLC lg,t ins seen)
mengj@19194
   242
  | fn_lg f _ = raise FN_LG(f); 
mengj@19194
   243
mengj@19194
   244
mengj@19194
   245
fun term_lg [] (lg,seen) = (lg,seen)
mengj@19194
   246
  | term_lg (tm::tms) (FOL,seen) =
mengj@19194
   247
    let val (f,args) = strip_comb tm
mengj@19194
   248
	val (lg',seen') = if f mem seen then (FOL,seen) 
mengj@19194
   249
			  else fn_lg f (FOL,seen)
mengj@19194
   250
mengj@19194
   251
	 in
mengj@19194
   252
	     term_lg (args@tms) (lg',seen')
mengj@19194
   253
    end
mengj@19194
   254
  | term_lg _ (lg,seen) = (lg,seen)
mengj@19194
   255
mengj@19194
   256
exception PRED_LG of term;
mengj@19194
   257
mengj@19194
   258
fun pred_lg (t as Const(P,tp)) (lg,seen)= 
mengj@19194
   259
    if has_bool_arg tp then (upgrade_lg HOL lg, t ins seen) else (lg,t ins seen) 
mengj@19194
   260
  | pred_lg (t as Free(P,tp)) (lg,seen) =
mengj@19194
   261
    if has_bool_arg tp then (upgrade_lg HOL lg, t ins seen) else (lg,t ins seen)
mengj@19194
   262
  | pred_lg (t as Var(_,_)) (lg,seen) = (upgrade_lg HOL lg, t ins seen)
mengj@19194
   263
  | pred_lg P _ = raise PRED_LG(P);
mengj@19194
   264
mengj@19194
   265
mengj@19194
   266
fun lit_lg (Const("Not",_) $ P) (lg,seen) = lit_lg P (lg,seen)
mengj@19194
   267
  | lit_lg P (lg,seen) =
mengj@19194
   268
    let val (pred,args) = strip_comb P
mengj@19194
   269
	val (lg',seen') = if pred mem seen then (lg,seen) 
mengj@19194
   270
			  else pred_lg pred (lg,seen)
mengj@19194
   271
    in
mengj@19194
   272
	term_lg args (lg',seen')
mengj@19194
   273
    end;
mengj@19194
   274
mengj@19194
   275
fun lits_lg [] (lg,seen) = (lg,seen)
mengj@19194
   276
  | lits_lg (lit::lits) (FOL,seen) =
mengj@19194
   277
    lits_lg lits (lit_lg lit (FOL,seen))
mengj@19194
   278
  | lits_lg lits (lg,seen) = (lg,seen);
mengj@19194
   279
mengj@19194
   280
mengj@19194
   281
fun logic_of_clause tm (lg,seen) =
mengj@19194
   282
    let val tm' = HOLogic.dest_Trueprop tm
mengj@19194
   283
	val disjs = HOLogic.dest_disj tm'
mengj@19194
   284
    in
mengj@19194
   285
	lits_lg disjs (lg,seen)
mengj@19194
   286
    end;
mengj@19194
   287
mengj@19194
   288
fun lits_lg [] (lg,seen) = (lg,seen)
mengj@19194
   289
  | lits_lg (lit::lits) (FOL,seen) =
mengj@19194
   290
    lits_lg lits (lit_lg lit (FOL,seen))
mengj@19194
   291
  | lits_lg lits (lg,seen) = (lg,seen);
mengj@19194
   292
mengj@19194
   293
mengj@19194
   294
fun logic_of_clause tm (lg,seen) =
mengj@19194
   295
    let val tm' = HOLogic.dest_Trueprop tm
mengj@19194
   296
	val disjs = HOLogic.dest_disj tm'
mengj@19194
   297
    in
mengj@19194
   298
	lits_lg disjs (lg,seen)
mengj@19194
   299
    end;
mengj@19194
   300
mengj@19194
   301
fun logic_of_clauses [] (lg,seen) = (lg,seen)
mengj@19194
   302
  | logic_of_clauses (cls::clss) (FOL,seen) =
mengj@19194
   303
    logic_of_clauses clss (logic_of_clause cls (FOL,seen))
mengj@19194
   304
  | logic_of_clauses (cls::clss) (lg,seen) = (lg,seen);
mengj@19194
   305
mengj@19194
   306
fun logic_of_nclauses [] (lg,seen) = (lg,seen)
mengj@19194
   307
  | logic_of_nclauses (cls::clss) (FOL,seen) =
mengj@19194
   308
    logic_of_nclauses clss (logic_of_clauses (snd cls) (FOL,seen))
mengj@19194
   309
  | logic_of_nclauses clss (lg,seen) = (lg,seen);
mengj@19194
   310
mengj@19194
   311
fun problem_logic (goal_cls,rules_cls) =
mengj@19194
   312
    let val (lg1,seen1) = logic_of_clauses goal_cls (FOL,[])
mengj@19194
   313
	val (lg2,seen2) = logic_of_nclauses rules_cls (lg1,seen1)
mengj@19194
   314
    in
mengj@19194
   315
	lg2
mengj@19194
   316
    end;
mengj@19194
   317
mengj@19194
   318
fun problem_logic_goals_aux [] (lg,seen) = lg
mengj@19194
   319
  | problem_logic_goals_aux (subgoal::subgoals) (lg,seen) = 
mengj@19194
   320
    problem_logic_goals_aux subgoals (logic_of_clauses subgoal (lg,seen));
mengj@19194
   321
    
mengj@19194
   322
fun problem_logic_goals subgoals = problem_logic_goals_aux subgoals (FOL,[]);
mengj@19194
   323
mengj@19194
   324
mengj@19194
   325
(***************************************************************)
mengj@19194
   326
(* ATP invocation methods setup                                *)
mengj@19194
   327
(***************************************************************)
mengj@19194
   328
mengj@19194
   329
mengj@19194
   330
(**** prover-specific format: TPTP ****)
mengj@19194
   331
mengj@19194
   332
mengj@19194
   333
fun cnf_hyps_thms ctxt = 
mengj@19194
   334
    let val ths = ProofContext.prems_of ctxt
mengj@19194
   335
    in
mengj@19194
   336
	ResClause.union_all (map ResAxioms.skolem_thm ths)
mengj@19194
   337
    end;
mengj@19194
   338
mengj@19194
   339
mengj@19194
   340
(**** write to files ****)
mengj@19194
   341
mengj@19194
   342
datatype mode = Auto | Fol | Hol;
mengj@19194
   343
paulson@19205
   344
val linkup_logic_mode = ref Fol;
paulson@19205
   345
mengj@19194
   346
fun tptp_writer logic goals filename (axioms,classrels,arities) =
mengj@19194
   347
    if is_fol_logic logic then ResClause.tptp_write_file goals filename (axioms, classrels, arities)
mengj@19194
   348
    else
mengj@19194
   349
	ResHolClause.tptp_write_file goals filename (axioms, classrels, arities) (get_all_helpers());
mengj@19194
   350
mengj@19194
   351
mengj@19194
   352
fun write_subgoal_file mode ctxt conjectures user_thms n =
mengj@19194
   353
    let val conj_cls = map prop_of (make_clauses conjectures) 
mengj@19194
   354
	val hyp_cls = map prop_of (cnf_hyps_thms ctxt)
mengj@19194
   355
	val goal_cls = conj_cls@hyp_cls
mengj@19194
   356
	val user_rules = map ResAxioms.pairname user_thms
mengj@19194
   357
	val (names_arr,axclauses_as_tms) = ResClasimp.get_clasimp_atp_lemmas ctxt (goal_cls) user_rules (!include_claset,!include_simpset,!include_atpset) (!run_relevance_filter)  
mengj@19194
   358
	val thy = ProofContext.theory_of ctxt
mengj@19194
   359
	val prob_logic = case mode of Auto => problem_logic_goals [goal_cls]
mengj@19194
   360
				    | Fol => FOL
mengj@19194
   361
				    | Hol => HOL
mengj@19194
   362
	val keep_types = if is_fol_logic prob_logic then !fol_keep_types else is_typed_hol ()
mengj@19194
   363
	val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy else []
mengj@19194
   364
	val arity_clauses = if keep_types then ResClause.arity_clause_thy thy else []
mengj@19194
   365
	val writer = tptp_writer
mengj@19194
   366
	val file = atp_input_file()
mengj@19194
   367
    in
mengj@19194
   368
	(writer prob_logic goal_cls file (axclauses_as_tms,classrel_clauses,arity_clauses);
mengj@19194
   369
	 warning ("Writing to " ^ file);
mengj@19194
   370
	 file)
mengj@19194
   371
    end;
mengj@19194
   372
mengj@19194
   373
mengj@19194
   374
(**** remove tmp files ****)
mengj@19194
   375
fun cond_rm_tmp file = 
mengj@19194
   376
    if !keep_atp_input then warning "ATP input kept..." 
mengj@19194
   377
    else if !destdir <> "" then warning ("ATP input kept in directory " ^ (!destdir))
mengj@19194
   378
    else (warning "deleting ATP inputs..."; OS.FileSys.remove file);
mengj@19194
   379
mengj@19194
   380
mengj@19194
   381
(****** setup ATPs as Isabelle methods ******)
mengj@19194
   382
fun atp_meth' tac ths ctxt = 
mengj@19194
   383
    Method.SIMPLE_METHOD' HEADGOAL
mengj@19194
   384
    (tac ctxt ths);
mengj@19194
   385
mengj@19194
   386
fun atp_meth tac ths ctxt = 
mengj@19194
   387
    let val thy = ProofContext.theory_of ctxt
mengj@19194
   388
	val _ = ResClause.init thy
mengj@19194
   389
	val _ = ResHolClause.init thy
mengj@19194
   390
    in
mengj@19194
   391
	atp_meth' tac ths ctxt
mengj@19194
   392
    end;
mengj@19194
   393
mengj@19194
   394
fun atp_method tac = Method.thms_ctxt_args (atp_meth tac);
mengj@19194
   395
mengj@19194
   396
(***************************************************************)
mengj@19194
   397
(* automatic ATP invocation                                    *)
mengj@19194
   398
(***************************************************************)
mengj@19194
   399
paulson@17306
   400
(* call prover with settings and problem file for the current subgoal *)
paulson@17764
   401
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
   402
  let
paulson@17422
   403
    fun make_atp_list [] n = []
paulson@17717
   404
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
   405
          let
paulson@17717
   406
            val probfile = prob_pathname n
paulson@17690
   407
            val time = Int.toString (!time_limit)
wenzelm@16802
   408
          in
wenzelm@18680
   409
            Output.debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17317
   410
            (*Avoid command arguments containing spaces: Poly/ML and SML/NJ
paulson@17317
   411
              versions of Unix.execute treat them differently!*)
paulson@17764
   412
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
   413
            if !prover = "spass"
wenzelm@16802
   414
            then
paulson@18700
   415
              let val baseopts = "%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
paulson@18700
   416
              val infopts = 
paulson@17484
   417
		      if !AtpCommunication.reconstruct 
paulson@18700
   418
		          (*Proof reconstruction needs a limited set of inf rules*)
paulson@18700
   419
                      then space_implode "%" (!custom_spass)                           
paulson@18700
   420
                      else "-Auto%-SOS=1"
paulson@18700
   421
                  val spass = helper_path "SPASS_HOME" "SPASS"
paulson@18700
   422
            in 
paulson@18700
   423
                ([("spass", spass, infopts ^ baseopts, probfile)] @ 
paulson@18798
   424
                  make_atp_list xs (n+1))
wenzelm@16802
   425
              end
paulson@17306
   426
            else if !prover = "vampire"
quigley@17235
   427
	    then 
paulson@17819
   428
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
wenzelm@16802
   429
              in
paulson@17772
   430
                ([("vampire", vampire, "-m 100000%-t " ^ time, probfile)] @
paulson@18798
   431
                 make_atp_list xs (n+1))       (*BEWARE! spaces in options!*)
wenzelm@16802
   432
              end
paulson@17306
   433
      	     else if !prover = "E"
paulson@17306
   434
      	     then
paulson@17819
   435
	       let val Eprover = helper_path "E_HOME" "eproof"
paulson@17306
   436
	       in
paulson@17772
   437
		  ([("E", Eprover, 
paulson@17690
   438
		     "--tptp-in%-l5%-xAuto%-tAuto%--cpu-limit=" ^ time,
paulson@17422
   439
		     probfile)] @
paulson@18798
   440
		   make_atp_list xs (n+1))
paulson@17306
   441
	       end
paulson@17306
   442
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   443
          end
paulson@15452
   444
paulson@17422
   445
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   446
  in
wenzelm@16802
   447
    Watcher.callResProvers(childout,atp_list);
wenzelm@18680
   448
    Output.debug "Sent commands to watcher!"
wenzelm@16802
   449
  end
quigley@16357
   450
paulson@17717
   451
(*We write out problem files for each subgoal. Argument pf generates filenames,
paulson@18986
   452
  and allows the suppression of the suffix "_1" in problem-generation mode.
paulson@18986
   453
  FIXME: does not cope with &&, and it isn't easy because one could have multiple
paulson@18986
   454
  subgoals, each involving &&.*)
paulson@17717
   455
fun write_problem_files pf (ctxt,th)  =
paulson@18753
   456
  let val goals = Thm.prems_of th
mengj@19194
   457
      val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals))
mengj@19194
   458
      val (names_arr, axclauses_as_terms) = ResClasimp.get_clasimp_atp_lemmas ctxt goals [] (true,true,true) (!run_relevance_filter) (* no user supplied rules here, because no user invocation *)
mengj@19194
   459
      val _ = Output.debug ("claset, simprules and atprules total clauses = " ^ 
mengj@19194
   460
                     Int.toString (Array.length names_arr))
paulson@17717
   461
      val thy = ProofContext.theory_of ctxt
mengj@19194
   462
      fun get_neg_subgoals n =
mengj@19194
   463
	  if n=0 then []
mengj@19194
   464
	  else
mengj@19194
   465
	      let val st = Seq.hd (EVERY'
mengj@19194
   466
				       [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac] n th)
mengj@19194
   467
		  val negs = Option.valOf (metahyps_thms n st)
mengj@19194
   468
		  val negs_clauses = map prop_of (make_clauses negs)
mengj@19194
   469
	      in
mengj@19194
   470
		  negs_clauses::(get_neg_subgoals (n - 1))
mengj@19194
   471
	      end
mengj@19194
   472
      val neg_subgoals = get_neg_subgoals (length goals) 
paulson@19205
   473
      val goals_logic = case !linkup_logic_mode of Auto => problem_logic_goals neg_subgoals
paulson@19205
   474
						 | Fol => FOL
paulson@19205
   475
						 | Hol => HOL
mengj@19194
   476
      val keep_types = if is_fol_logic goals_logic then !ResClause.keep_types else is_typed_hol ()
mengj@19194
   477
      val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy else []
wenzelm@18680
   478
      val _ = Output.debug ("classrel clauses = " ^ Int.toString (length classrel_clauses))
mengj@19194
   479
      val arity_clauses = if keep_types then ResClause.arity_clause_thy thy else []
wenzelm@18680
   480
      val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
mengj@19194
   481
      val writer = (*if !prover ~= "spass" then *)tptp_writer 
mengj@19194
   482
      fun write_all [] _ = []
mengj@19194
   483
	| write_all (subgoal::subgoals) k =
mengj@19194
   484
	  (writer goals_logic subgoal (pf k) (axclauses_as_terms,classrel_clauses,arity_clauses); pf k):: (write_all subgoals (k - 1))
mengj@19194
   485
  in
mengj@19194
   486
      (write_all neg_subgoals (length goals), names_arr)
mengj@19194
   487
  end;
quigley@15644
   488
paulson@17775
   489
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   490
                                    Posix.Process.pid * string list) option);
paulson@17775
   491
paulson@17775
   492
fun kill_last_watcher () =
paulson@17775
   493
    (case !last_watcher_pid of 
paulson@17775
   494
         NONE => ()
paulson@17775
   495
       | SOME (_, childout, pid, files) => 
wenzelm@18680
   496
	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   497
	   Watcher.killWatcher pid;  
paulson@17775
   498
	   ignore (map (try OS.FileSys.remove) files)))
wenzelm@18680
   499
     handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
paulson@17525
   500
paulson@17525
   501
(*writes out the current clasimpset to a tptp file;
paulson@17525
   502
  turns off xsymbol at start of function, restoring it at end    *)
paulson@17484
   503
val isar_atp = setmp print_mode [] 
paulson@17717
   504
 (fn (ctxt, th) =>
paulson@17717
   505
  if Thm.no_prems th then ()
wenzelm@16802
   506
  else
wenzelm@16802
   507
    let
paulson@17775
   508
      val _ = kill_last_watcher()
mengj@19194
   509
      val (files,names_arr) = write_problem_files prob_pathname (ctxt,th)
mengj@19194
   510
      val (childin, childout, pid) = Watcher.createWatcher (th, names_arr)
paulson@15608
   511
    in
paulson@17772
   512
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@18680
   513
      Output.debug ("problem files: " ^ space_implode ", " files); 
wenzelm@18680
   514
      Output.debug ("pid: " ^ string_of_pid pid);
paulson@17717
   515
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
wenzelm@16802
   516
    end);
paulson@15608
   517
paulson@17422
   518
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   519
      (fn (ctxt,th) =>
paulson@17717
   520
       if Thm.no_prems th then ()
paulson@17717
   521
       else 
paulson@17717
   522
         let val pf = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@17717
   523
         	      else prob_pathname
paulson@17717
   524
         in ignore (write_problem_files pf (ctxt,th)) end);
paulson@15452
   525
quigley@16357
   526
wenzelm@16802
   527
(** the Isar toplevel hook **)
wenzelm@16802
   528
paulson@19205
   529
fun invoke_atp_ml (ctxt, goal) =
paulson@19205
   530
  let val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   531
  in
wenzelm@18680
   532
    Output.debug ("subgoals in isar_atp:\n" ^ 
paulson@19205
   533
		  Pretty.string_of (ProofContext.pretty_term ctxt
paulson@19205
   534
		    (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@18680
   535
    Output.debug ("current theory: " ^ Context.theory_name thy);
quigley@17150
   536
    hook_count := !hook_count +1;
wenzelm@18680
   537
    Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   538
    ResClause.init thy;
mengj@19194
   539
    ResHolClause.init thy;
paulson@17690
   540
    if !destdir = "" andalso !time_limit > 0 then isar_atp (ctxt, goal)
paulson@17502
   541
    else isar_atp_writeonly (ctxt, goal)
paulson@19205
   542
  end;
paulson@19205
   543
paulson@19205
   544
val invoke_atp = Toplevel.no_timing o Toplevel.unknown_proof o Toplevel.keep
paulson@19205
   545
 (fn state =>
paulson@19205
   546
  let val (ctxt, (_, goal)) = Proof.get_goal (Toplevel.proof_of state)
paulson@19205
   547
  in  invoke_atp_ml (ctxt, goal)  end);
quigley@16357
   548
paulson@17091
   549
val call_atpP =
paulson@17746
   550
  OuterSyntax.command 
paulson@17091
   551
    "ProofGeneral.call_atp" 
paulson@17091
   552
    "call automatic theorem provers" 
paulson@17091
   553
    OuterKeyword.diag
paulson@19205
   554
    (Scan.succeed invoke_atp);
paulson@17091
   555
paulson@17091
   556
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   557
paulson@15347
   558
end;