src/HOL/Tools/res_atp.ML
author paulson
Tue Nov 07 16:21:54 2006 +0100 (2006-11-07)
changeset 21224 f86b8463af37
parent 21132 88d1daae0319
child 21243 afffe1f72143
permissions -rw-r--r--
Proper theorem names at last, no fakes!!
Added set_prover function to set various parameters to improve success rate.
Fixed the auto settings for E.
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
paulson@20954
     8
(*Currently unused, during debugging*)
wenzelm@16802
     9
signature RES_ATP =
wenzelm@16802
    10
sig
paulson@17306
    11
  val prover: string ref
wenzelm@16802
    12
  val custom_spass: string list ref
paulson@17484
    13
  val destdir: string ref
paulson@17849
    14
  val helper_path: string -> string -> string
paulson@17484
    15
  val problem_name: string ref
paulson@17690
    16
  val time_limit: int ref
paulson@21224
    17
  val set_prover: string -> unit
mengj@19194
    18
   
mengj@19194
    19
  datatype mode = Auto | Fol | Hol
paulson@19450
    20
  val linkup_logic_mode : mode ref
mengj@19722
    21
  val write_subgoal_file: bool -> mode -> Proof.context -> thm list -> thm list -> int -> string
mengj@19194
    22
  val vampire_time: int ref
mengj@19194
    23
  val eprover_time: int ref
mengj@19722
    24
  val spass_time: int ref
mengj@19194
    25
  val run_vampire: int -> unit
mengj@19194
    26
  val run_eprover: int -> unit
mengj@19722
    27
  val run_spass: int -> unit
mengj@19194
    28
  val vampireLimit: unit -> int
mengj@19194
    29
  val eproverLimit: unit -> int
mengj@19722
    30
  val spassLimit: unit -> int
wenzelm@20289
    31
  val atp_method: (Proof.context -> thm list -> int -> tactic) ->
wenzelm@20289
    32
    Method.src -> Proof.context -> Proof.method
mengj@19194
    33
  val cond_rm_tmp: string -> unit
mengj@19194
    34
  val fol_keep_types: bool ref
mengj@19194
    35
  val hol_full_types: unit -> unit
mengj@19194
    36
  val hol_partial_types: unit -> unit
mengj@19194
    37
  val hol_const_types_only: unit -> unit
mengj@19194
    38
  val hol_no_types: unit -> unit
mengj@19194
    39
  val hol_typ_level: unit -> ResHolClause.type_level
paulson@20246
    40
  val include_all: bool ref
mengj@19194
    41
  val run_relevance_filter: bool ref
mengj@19768
    42
  val run_blacklist_filter: bool ref
paulson@20372
    43
  val blacklist: string list ref
paulson@19894
    44
  val add_all : unit -> unit
mengj@19227
    45
  val add_claset : unit -> unit
mengj@19227
    46
  val add_simpset : unit -> unit
mengj@19227
    47
  val add_clasimp : unit -> unit
mengj@19227
    48
  val add_atpset : unit -> unit
paulson@19894
    49
  val rm_all : unit -> unit
mengj@19227
    50
  val rm_claset : unit -> unit
mengj@19227
    51
  val rm_simpset : unit -> unit
mengj@19227
    52
  val rm_atpset : unit -> unit
mengj@19227
    53
  val rm_clasimp : unit -> unit
paulson@15347
    54
end;
paulson@15347
    55
paulson@20457
    56
structure ResAtp =
paulson@15347
    57
struct
paulson@15347
    58
paulson@21070
    59
fun timestamp s = Output.debug ("At " ^ Time.toString (Time.now()) ^ ": " ^ s);
paulson@21070
    60
mengj@19194
    61
(********************************************************************)
mengj@19194
    62
(* some settings for both background automatic ATP calling procedure*)
mengj@19194
    63
(* and also explicit ATP invocation methods                         *)
mengj@19194
    64
(********************************************************************)
mengj@19194
    65
mengj@19194
    66
(*** background linkup ***)
mengj@19194
    67
val call_atp = ref false; 
quigley@17150
    68
val hook_count = ref 0;
paulson@21224
    69
val time_limit = ref 60;
paulson@21224
    70
val prover = ref "";   
paulson@21224
    71
paulson@21224
    72
fun set_prover atp = 
paulson@21224
    73
  case String.map Char.toLower atp of
paulson@21224
    74
      "e" => 
paulson@21224
    75
          (ReduceAxiomsN.max_new := 80;
paulson@21224
    76
           ReduceAxiomsN.theory_const := false;
paulson@21224
    77
           prover := "E")
paulson@21224
    78
    | "spass" => 
paulson@21224
    79
          (ReduceAxiomsN.max_new := 40;
paulson@21224
    80
           ReduceAxiomsN.theory_const := true;
paulson@21224
    81
           prover := "spass")
paulson@21224
    82
    | "vampire" => 
paulson@21224
    83
          (ReduceAxiomsN.max_new := 60;
paulson@21224
    84
           ReduceAxiomsN.theory_const := false;
paulson@21224
    85
           prover := "vampire")
paulson@21224
    86
    | _ => error ("No such prover: " ^ atp);
paulson@21224
    87
paulson@21224
    88
val _ = set_prover "E"; (* use E as the default prover *)
paulson@21224
    89
paulson@17305
    90
val custom_spass =   (*specialized options for SPASS*)
paulson@17690
    91
      ref ["-Auto=0","-FullRed=0","-IORe","-IOFc","-RTaut","-RFSub","-RBSub"];
paulson@17484
    92
val destdir = ref "";   (*Empty means write files to /tmp*)
paulson@17484
    93
val problem_name = ref "prob";
paulson@17484
    94
paulson@17819
    95
(*Return the path to a "helper" like SPASS or tptp2X, first checking that
paulson@17819
    96
  it exists.  FIXME: modify to use Path primitives and move to some central place.*)  
paulson@17819
    97
fun helper_path evar base =
paulson@17819
    98
  case getenv evar of
paulson@17819
    99
      "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
paulson@17819
   100
    | home => 
paulson@17819
   101
        let val path = home ^ "/" ^ base
paulson@17819
   102
        in  if File.exists (File.unpack_platform_path path) then path 
paulson@17819
   103
	    else error ("Could not find the file " ^ path)
paulson@17819
   104
	end;  
paulson@17819
   105
paulson@17717
   106
fun probfile_nosuffix _ = 
paulson@17484
   107
  if !destdir = "" then File.platform_path (File.tmp_path (Path.basic (!problem_name)))
paulson@17484
   108
  else if File.exists (File.unpack_platform_path (!destdir))
paulson@17484
   109
  then !destdir ^ "/" ^ !problem_name
paulson@17484
   110
  else error ("No such directory: " ^ !destdir);
quigley@15644
   111
paulson@17717
   112
fun prob_pathname n = probfile_nosuffix n ^ "_" ^ Int.toString n;
paulson@17717
   113
mengj@19194
   114
mengj@19194
   115
(*** ATP methods ***)
mengj@19194
   116
val vampire_time = ref 60;
mengj@19194
   117
val eprover_time = ref 60;
mengj@19722
   118
val spass_time = ref 60;
mengj@19722
   119
mengj@19194
   120
fun run_vampire time =  
mengj@19194
   121
    if (time >0) then vampire_time:= time
mengj@19194
   122
    else vampire_time:=60;
mengj@19194
   123
mengj@19194
   124
fun run_eprover time = 
mengj@19194
   125
    if (time > 0) then eprover_time:= time
mengj@19194
   126
    else eprover_time:=60;
mengj@19194
   127
mengj@19722
   128
fun run_spass time = 
mengj@19722
   129
    if (time > 0) then spass_time:=time
mengj@19722
   130
    else spass_time:=60;
mengj@19722
   131
mengj@19722
   132
mengj@19194
   133
fun vampireLimit () = !vampire_time;
mengj@19194
   134
fun eproverLimit () = !eprover_time;
mengj@19722
   135
fun spassLimit () = !spass_time;
mengj@19194
   136
mengj@19194
   137
val fol_keep_types = ResClause.keep_types;
mengj@19194
   138
val hol_full_types = ResHolClause.full_types;
mengj@19194
   139
val hol_partial_types = ResHolClause.partial_types;
mengj@19194
   140
val hol_const_types_only = ResHolClause.const_types_only;
mengj@19194
   141
val hol_no_types = ResHolClause.no_types;
mengj@19194
   142
fun hol_typ_level () = ResHolClause.find_typ_level ();
mengj@19194
   143
fun is_typed_hol () = 
mengj@19194
   144
    let val tp_level = hol_typ_level()
mengj@19194
   145
    in
mengj@19194
   146
	not (tp_level = ResHolClause.T_NONE)
mengj@19194
   147
    end;
mengj@19194
   148
mengj@19194
   149
fun atp_input_file () =
mengj@19194
   150
    let val file = !problem_name 
mengj@19194
   151
    in
mengj@19194
   152
	if !destdir = "" then File.platform_path (File.tmp_path (Path.basic file))
mengj@19194
   153
	else if File.exists (File.unpack_platform_path (!destdir))
mengj@19194
   154
	then !destdir ^ "/" ^ file
mengj@19194
   155
	else error ("No such directory: " ^ !destdir)
mengj@19194
   156
    end;
mengj@19194
   157
paulson@20868
   158
val include_all = ref true;
mengj@19194
   159
val include_simpset = ref false;
mengj@19194
   160
val include_claset = ref false; 
mengj@19194
   161
val include_atpset = ref true;
paulson@20246
   162
paulson@20246
   163
(*Tests show that follow_defs gives VERY poor results with "include_all"*)
paulson@20246
   164
fun add_all() = (include_all:=true; ReduceAxiomsN.follow_defs := false);
paulson@20246
   165
fun rm_all() = include_all:=false;
paulson@20246
   166
paulson@20246
   167
fun add_simpset() = include_simpset:=true;
paulson@20246
   168
fun rm_simpset() = include_simpset:=false;
paulson@20246
   169
paulson@20246
   170
fun add_claset() = include_claset:=true;
paulson@20246
   171
fun rm_claset() = include_claset:=false;
paulson@20246
   172
paulson@20246
   173
fun add_clasimp() = (include_simpset:=true;include_claset:=true);
paulson@20246
   174
fun rm_clasimp() = (include_simpset:=false;include_claset:=false);
paulson@20246
   175
paulson@20246
   176
fun add_atpset() = include_atpset:=true;
paulson@20246
   177
fun rm_atpset() = include_atpset:=false;
mengj@19194
   178
mengj@19194
   179
mengj@19194
   180
(**** relevance filter ****)
paulson@20457
   181
val run_relevance_filter = ReduceAxiomsN.run_relevance_filter;
mengj@19768
   182
val run_blacklist_filter = ref true;
mengj@19194
   183
mengj@19194
   184
(******************************************************************)
mengj@19194
   185
(* detect whether a given problem (clauses) is FOL/HOL/HOLC/HOLCS *)
mengj@19194
   186
(******************************************************************)
mengj@19194
   187
mengj@19194
   188
datatype logic = FOL | HOL | HOLC | HOLCS;
mengj@19194
   189
mengj@19194
   190
fun string_of_logic FOL = "FOL"
mengj@19194
   191
  | string_of_logic HOL = "HOL"
mengj@19194
   192
  | string_of_logic HOLC = "HOLC"
mengj@19194
   193
  | string_of_logic HOLCS = "HOLCS";
mengj@19194
   194
mengj@19194
   195
fun is_fol_logic FOL = true
mengj@19194
   196
  | is_fol_logic  _ = false
mengj@19194
   197
mengj@19194
   198
(*HOLCS will not occur here*)
mengj@19194
   199
fun upgrade_lg HOLC _ = HOLC
mengj@19194
   200
  | upgrade_lg HOL HOLC = HOLC
mengj@19194
   201
  | upgrade_lg HOL _ = HOL
mengj@19194
   202
  | upgrade_lg FOL lg = lg; 
mengj@19194
   203
mengj@19194
   204
(* check types *)
mengj@19451
   205
fun has_bool_hfn (Type("bool",_)) = true
mengj@19451
   206
  | has_bool_hfn (Type("fun",_)) = true
mengj@19451
   207
  | has_bool_hfn (Type(_, Ts)) = exists has_bool_hfn Ts
mengj@19451
   208
  | has_bool_hfn _ = false;
mengj@19194
   209
mengj@19451
   210
fun is_hol_fn tp =
mengj@19194
   211
    let val (targs,tr) = strip_type tp
mengj@19194
   212
    in
mengj@19451
   213
	exists (has_bool_hfn) (tr::targs)
mengj@19194
   214
    end;
mengj@19194
   215
mengj@19451
   216
fun is_hol_pred tp =
mengj@19451
   217
    let val (targs,tr) = strip_type tp
mengj@19451
   218
    in
mengj@19451
   219
	exists (has_bool_hfn) targs
mengj@19451
   220
    end;
mengj@19194
   221
mengj@19194
   222
exception FN_LG of term;
mengj@19194
   223
mengj@19194
   224
fun fn_lg (t as Const(f,tp)) (lg,seen) = 
haftmann@20854
   225
    if is_hol_fn tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg, insert (op =) t seen) 
mengj@19194
   226
  | fn_lg (t as Free(f,tp)) (lg,seen) = 
haftmann@20854
   227
    if is_hol_fn tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg, insert (op =) t seen) 
mengj@19194
   228
  | fn_lg (t as Var(f,tp)) (lg,seen) =
haftmann@20854
   229
    if is_hol_fn tp then (upgrade_lg HOL lg,insert (op =) t seen) else (lg,insert (op =) t seen)
haftmann@20854
   230
  | fn_lg (t as Abs(_,_,_)) (lg,seen) = (upgrade_lg HOLC lg,insert (op =) t seen)
mengj@19194
   231
  | fn_lg f _ = raise FN_LG(f); 
mengj@19194
   232
mengj@19194
   233
mengj@19194
   234
fun term_lg [] (lg,seen) = (lg,seen)
mengj@19194
   235
  | term_lg (tm::tms) (FOL,seen) =
paulson@20457
   236
      let val (f,args) = strip_comb tm
paulson@20457
   237
	  val (lg',seen') = if f mem seen then (FOL,seen) 
paulson@20457
   238
			    else fn_lg f (FOL,seen)
paulson@20457
   239
      in
paulson@20457
   240
	if is_fol_logic lg' then ()
paulson@20457
   241
        else Output.debug ("Found a HOL term: " ^ Display.raw_string_of_term f);
paulson@20457
   242
        term_lg (args@tms) (lg',seen')
paulson@20457
   243
      end
mengj@19194
   244
  | term_lg _ (lg,seen) = (lg,seen)
mengj@19194
   245
mengj@19194
   246
exception PRED_LG of term;
mengj@19194
   247
mengj@19194
   248
fun pred_lg (t as Const(P,tp)) (lg,seen)= 
haftmann@20854
   249
      if is_hol_pred tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg,insert (op =) t seen) 
mengj@19194
   250
  | pred_lg (t as Free(P,tp)) (lg,seen) =
haftmann@20854
   251
      if is_hol_pred tp then (upgrade_lg HOL lg, insert (op =) t seen) else (lg,insert (op =) t seen)
haftmann@20854
   252
  | pred_lg (t as Var(_,_)) (lg,seen) = (upgrade_lg HOL lg, insert (op =) t seen)
mengj@19194
   253
  | pred_lg P _ = raise PRED_LG(P);
mengj@19194
   254
mengj@19194
   255
mengj@19194
   256
fun lit_lg (Const("Not",_) $ P) (lg,seen) = lit_lg P (lg,seen)
mengj@19194
   257
  | lit_lg P (lg,seen) =
paulson@20457
   258
      let val (pred,args) = strip_comb P
paulson@20457
   259
	  val (lg',seen') = if pred mem seen then (lg,seen) 
paulson@20457
   260
			    else pred_lg pred (lg,seen)
paulson@20457
   261
      in
paulson@20457
   262
	if is_fol_logic lg' then ()
paulson@20457
   263
	else Output.debug ("Found a HOL predicate: " ^ Display.raw_string_of_term pred);
mengj@19194
   264
	term_lg args (lg',seen')
paulson@20457
   265
      end;
mengj@19194
   266
mengj@19194
   267
fun lits_lg [] (lg,seen) = (lg,seen)
mengj@19194
   268
  | lits_lg (lit::lits) (FOL,seen) =
paulson@20457
   269
      let val (lg,seen') = lit_lg lit (FOL,seen)
paulson@20457
   270
      in
paulson@20457
   271
	if is_fol_logic lg then ()
paulson@20457
   272
	else Output.debug ("Found a HOL literal: " ^ Display.raw_string_of_term lit);
mengj@19227
   273
	lits_lg lits (lg,seen')
paulson@20457
   274
      end
mengj@19194
   275
  | lits_lg lits (lg,seen) = (lg,seen);
mengj@19194
   276
mengj@19227
   277
fun dest_disj_aux (Const ("op |", _) $ t $ t') disjs = 
mengj@19227
   278
    dest_disj_aux t (dest_disj_aux t' disjs)
mengj@19227
   279
  | dest_disj_aux t disjs = t::disjs;
mengj@19227
   280
mengj@19227
   281
fun dest_disj t = dest_disj_aux t [];
mengj@19227
   282
mengj@19194
   283
fun logic_of_clause tm (lg,seen) =
mengj@19194
   284
    let val tm' = HOLogic.dest_Trueprop tm
mengj@19227
   285
	val disjs = dest_disj tm'
mengj@19194
   286
    in
mengj@19194
   287
	lits_lg disjs (lg,seen)
mengj@19194
   288
    end;
mengj@19194
   289
mengj@19194
   290
fun logic_of_clauses [] (lg,seen) = (lg,seen)
mengj@19194
   291
  | logic_of_clauses (cls::clss) (FOL,seen) =
mengj@19227
   292
    let val (lg,seen') = logic_of_clause cls (FOL,seen)
wenzelm@19641
   293
	val _ =
wenzelm@19641
   294
          if is_fol_logic lg then ()
paulson@19746
   295
          else Output.debug ("Found a HOL clause: " ^ Display.raw_string_of_term cls)
mengj@19227
   296
    in
mengj@19227
   297
	logic_of_clauses clss (lg,seen')
mengj@19227
   298
    end
mengj@19194
   299
  | logic_of_clauses (cls::clss) (lg,seen) = (lg,seen);
mengj@19194
   300
mengj@19194
   301
fun problem_logic_goals_aux [] (lg,seen) = lg
mengj@19194
   302
  | problem_logic_goals_aux (subgoal::subgoals) (lg,seen) = 
mengj@19194
   303
    problem_logic_goals_aux subgoals (logic_of_clauses subgoal (lg,seen));
mengj@19194
   304
    
mengj@19194
   305
fun problem_logic_goals subgoals = problem_logic_goals_aux subgoals (FOL,[]);
mengj@19194
   306
mengj@19768
   307
(***************************************************************)
mengj@19768
   308
(* Retrieving and filtering lemmas                             *)
mengj@19768
   309
(***************************************************************)
mengj@19768
   310
mengj@19768
   311
(*** white list and black list of lemmas ***)
mengj@19768
   312
mengj@19768
   313
(*The rule subsetI is frequently omitted by the relevance filter.*)
mengj@19768
   314
val whitelist = ref [subsetI]; 
mengj@19768
   315
paulson@20444
   316
(*Names of theorems and theorem lists to be banned. The final numeric suffix of
paulson@20444
   317
  theorem lists is first removed.
paulson@20444
   318
paulson@20444
   319
  These theorems typically produce clauses that are prolific (match too many equality or
mengj@19768
   320
  membership literals) and relate to seldom-used facts. Some duplicate other rules.
mengj@19768
   321
  FIXME: this blacklist needs to be maintained using theory data and added to using
mengj@19768
   322
  an attribute.*)
mengj@19768
   323
val blacklist = ref
paulson@20526
   324
  ["Datatype.prod.size",
paulson@21132
   325
   "Datatype.unit.exhaust", (*"unit" thms cause unsound proofs; unit.nchotomy is caught automatically*)
paulson@21224
   326
   "Datatype.unit.induct", 
paulson@20372
   327
   "Datatype.unit.inducts",
paulson@20526
   328
   "Datatype.unit.split_asm", 
paulson@20372
   329
   "Datatype.unit.split",
paulson@20444
   330
   "Datatype.unit.splits",
mengj@19768
   331
   "Divides.dvd_0_left_iff",
mengj@19768
   332
   "Finite_Set.card_0_eq",
mengj@19768
   333
   "Finite_Set.card_infinite",
mengj@19768
   334
   "Finite_Set.Max_ge",
mengj@19768
   335
   "Finite_Set.Max_in",
mengj@19768
   336
   "Finite_Set.Max_le_iff",
mengj@19768
   337
   "Finite_Set.Max_less_iff",
mengj@19768
   338
   "Finite_Set.max.f_below_strict_below.below_f_conv", (*duplicates in Orderings.*)
mengj@19768
   339
   "Finite_Set.max.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
mengj@19768
   340
   "Finite_Set.Min_ge_iff",
mengj@19768
   341
   "Finite_Set.Min_gr_iff",
mengj@19768
   342
   "Finite_Set.Min_in",
mengj@19768
   343
   "Finite_Set.Min_le",
mengj@19768
   344
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.inf_Sup_absorb", 
mengj@19768
   345
   "Finite_Set.min_max.below_inf_sup_Inf_Sup.sup_Inf_absorb", 
mengj@19768
   346
   "Finite_Set.min.f_below_strict_below.below_f_conv",        (*duplicates in Orderings.*)
mengj@19768
   347
   "Finite_Set.min.f_below_strict_below.strict_below_f_conv", (*duplicates in Orderings.*)
paulson@20526
   348
   "Fun.vimage_image_eq",   (*involves an existential quantifier*)
paulson@20526
   349
   "HOL.split_if_asm",     (*splitting theorem*)
paulson@20457
   350
   "HOL.split_if",         (*splitting theorem*)
paulson@20526
   351
   "IntDef.abs_split",
mengj@19768
   352
   "IntDef.Integ.Abs_Integ_inject",
mengj@19768
   353
   "IntDef.Integ.Abs_Integ_inverse",
mengj@19768
   354
   "IntDiv.zdvd_0_left",
mengj@19768
   355
   "List.append_eq_append_conv",
mengj@19768
   356
   "List.hd_Cons_tl",   (*Says everything is [] or Cons. Probably prolific.*)
mengj@19768
   357
   "List.in_listsD",
mengj@19768
   358
   "List.in_listsI",
mengj@19768
   359
   "List.lists.Cons",
mengj@19768
   360
   "List.listsE",
mengj@19768
   361
   "Nat.less_one", (*not directional? obscure*)
mengj@19768
   362
   "Nat.not_gr0",
mengj@19768
   363
   "Nat.one_eq_mult_iff", (*duplicate by symmetry*)
mengj@19768
   364
   "NatArith.of_nat_0_eq_iff",
mengj@19768
   365
   "NatArith.of_nat_eq_0_iff",
mengj@19768
   366
   "NatArith.of_nat_le_0_iff",
mengj@19768
   367
   "NatSimprocs.divide_le_0_iff_number_of",  (*too many clauses*)
mengj@19768
   368
   "NatSimprocs.divide_less_0_iff_number_of",
mengj@19768
   369
   "NatSimprocs.equation_minus_iff_1",  (*not directional*)
mengj@19768
   370
   "NatSimprocs.equation_minus_iff_number_of", (*not directional*)
mengj@19768
   371
   "NatSimprocs.le_minus_iff_1", (*not directional*)
mengj@19768
   372
   "NatSimprocs.le_minus_iff_number_of",  (*not directional*)
mengj@19768
   373
   "NatSimprocs.less_minus_iff_1", (*not directional*)
mengj@19768
   374
   "NatSimprocs.less_minus_iff_number_of", (*not directional*)
mengj@19768
   375
   "NatSimprocs.minus_equation_iff_number_of", (*not directional*)
mengj@19768
   376
   "NatSimprocs.minus_le_iff_1", (*not directional*)
mengj@19768
   377
   "NatSimprocs.minus_le_iff_number_of", (*not directional*)
mengj@19768
   378
   "NatSimprocs.minus_less_iff_1", (*not directional*)
mengj@19768
   379
   "NatSimprocs.mult_le_cancel_left_number_of", (*excessive case analysis*)
mengj@19768
   380
   "NatSimprocs.mult_le_cancel_right_number_of", (*excessive case analysis*)
mengj@19768
   381
   "NatSimprocs.mult_less_cancel_left_number_of", (*excessive case analysis*)
mengj@19768
   382
   "NatSimprocs.mult_less_cancel_right_number_of", (*excessive case analysis*)
mengj@19768
   383
   "NatSimprocs.zero_le_divide_iff_number_of", (*excessive case analysis*)
mengj@19768
   384
   "NatSimprocs.zero_less_divide_iff_number_of",
mengj@19768
   385
   "OrderedGroup.abs_0_eq", (*duplicate by symmetry*)
mengj@19768
   386
   "OrderedGroup.diff_eq_0_iff_eq", (*prolific?*)
mengj@19768
   387
   "OrderedGroup.join_0_eq_0",
mengj@19768
   388
   "OrderedGroup.meet_0_eq_0",
mengj@19768
   389
   "OrderedGroup.pprt_eq_0",   (*obscure*)
mengj@19768
   390
   "OrderedGroup.pprt_eq_id",   (*obscure*)
mengj@19768
   391
   "OrderedGroup.pprt_mono",   (*obscure*)
paulson@20526
   392
   "Orderings.split_max",      (*splitting theorem*)
paulson@20457
   393
   "Orderings.split_min",      (*splitting theorem*)
mengj@19768
   394
   "Parity.even_nat_power",   (*obscure, somewhat prolilfic*)
mengj@19768
   395
   "Parity.power_eq_0_iff_number_of",
mengj@19768
   396
   "Parity.power_le_zero_eq_number_of",   (*obscure and prolific*)
mengj@19768
   397
   "Parity.power_less_zero_eq_number_of",
mengj@19768
   398
   "Parity.zero_le_power_eq_number_of",   (*obscure and prolific*)
mengj@19768
   399
   "Parity.zero_less_power_eq_number_of",   (*obscure and prolific*)
mengj@19768
   400
   "Power.zero_less_power_abs_iff",
paulson@20526
   401
   "Product_Type.split_eta_SetCompr",   (*involves an existential quantifier*)
paulson@20457
   402
   "Product_Type.split_paired_Ball_Sigma",     (*splitting theorem*)
paulson@20457
   403
   "Product_Type.split_paired_Bex_Sigma",      (*splitting theorem*)
paulson@20526
   404
   "Product_Type.split_split_asm",             (*splitting theorem*)
paulson@20457
   405
   "Product_Type.split_split",                 (*splitting theorem*)
paulson@20526
   406
   "Product_Type.unit_abs_eta_conv",
paulson@20526
   407
   "Product_Type.unit_induct",
mengj@19768
   408
   "Relation.diagI",
paulson@20526
   409
   "Relation.Domain_def",   (*involves an existential quantifier*)
paulson@20526
   410
   "Relation.Image_def",   (*involves an existential quantifier*)
mengj@19768
   411
   "Relation.ImageI",
mengj@19768
   412
   "Ring_and_Field.divide_cancel_left", (*fields are seldom used & often prolific*)
mengj@19768
   413
   "Ring_and_Field.divide_cancel_right",
mengj@19768
   414
   "Ring_and_Field.divide_divide_eq_left",
mengj@19768
   415
   "Ring_and_Field.divide_divide_eq_right",
mengj@19768
   416
   "Ring_and_Field.divide_eq_0_iff",
mengj@19768
   417
   "Ring_and_Field.divide_eq_1_iff",
mengj@19768
   418
   "Ring_and_Field.divide_eq_eq_1",
mengj@19768
   419
   "Ring_and_Field.divide_le_0_1_iff",
mengj@19768
   420
   "Ring_and_Field.divide_le_eq_1_neg",  (*obscure and prolific*)
mengj@19768
   421
   "Ring_and_Field.divide_le_eq_1_pos",  (*obscure and prolific*)
mengj@19768
   422
   "Ring_and_Field.divide_less_0_1_iff",
mengj@19768
   423
   "Ring_and_Field.divide_less_eq_1_neg",  (*obscure and prolific*)
mengj@19768
   424
   "Ring_and_Field.divide_less_eq_1_pos",  (*obscure and prolific*)
mengj@19768
   425
   "Ring_and_Field.eq_divide_eq_1", (*duplicate by symmetry*)
mengj@19768
   426
   "Ring_and_Field.field_mult_cancel_left",
mengj@19768
   427
   "Ring_and_Field.field_mult_cancel_right",
mengj@19768
   428
   "Ring_and_Field.inverse_le_iff_le_neg",
mengj@19768
   429
   "Ring_and_Field.inverse_le_iff_le",
mengj@19768
   430
   "Ring_and_Field.inverse_less_iff_less_neg",
mengj@19768
   431
   "Ring_and_Field.inverse_less_iff_less",
mengj@19768
   432
   "Ring_and_Field.le_divide_eq_1_neg", (*obscure and prolific*)
mengj@19768
   433
   "Ring_and_Field.le_divide_eq_1_pos", (*obscure and prolific*)
mengj@19768
   434
   "Ring_and_Field.less_divide_eq_1_neg", (*obscure and prolific*)
mengj@19768
   435
   "Ring_and_Field.less_divide_eq_1_pos", (*obscure and prolific*)
mengj@19768
   436
   "Ring_and_Field.one_eq_divide_iff",  (*duplicate by symmetry*)
paulson@20444
   437
   "Set.ball_simps", "Set.bex_simps",   (*quantifier rewriting: useless*)
paulson@20526
   438
   "Set.Collect_bex_eq",   (*involves an existential quantifier*)
paulson@20526
   439
   "Set.Collect_ex_eq",   (*involves an existential quantifier*)
mengj@19768
   440
   "Set.Diff_eq_empty_iff", (*redundant with paramodulation*)
mengj@19768
   441
   "Set.Diff_insert0",
paulson@20526
   442
   "Set.disjoint_insert",
paulson@20526
   443
   "Set.empty_Union_conv",   (*redundant with paramodulation*)
paulson@20526
   444
   "Set.full_SetCompr_eq",   (*involves an existential quantifier*)
paulson@20526
   445
   "Set.image_Collect",      (*involves an existential quantifier*)
paulson@20526
   446
   "Set.image_def",          (*involves an existential quantifier*)
paulson@20526
   447
   "Set.insert_disjoint",
paulson@20526
   448
   "Set.Int_UNIV",  (*redundant with paramodulation*)
paulson@20526
   449
   "Set.Inter_iff", (*We already have InterI, InterE*)
paulson@20526
   450
   "Set.Inter_UNIV_conv",
mengj@19768
   451
   "Set.psubsetE",    (*too prolific and obscure*)
mengj@19768
   452
   "Set.psubsetI",
mengj@19768
   453
   "Set.singleton_insert_inj_eq'",
mengj@19768
   454
   "Set.singleton_insert_inj_eq",
mengj@19768
   455
   "Set.singletonD",  (*these two duplicate some "insert" lemmas*)
mengj@19768
   456
   "Set.singletonI",
mengj@19768
   457
   "Set.Un_empty", (*redundant with paramodulation*)
paulson@20526
   458
   "Set.UNION_def",   (*involves an existential quantifier*)
mengj@19768
   459
   "Set.Union_empty_conv", (*redundant with paramodulation*)
mengj@19768
   460
   "Set.Union_iff",              (*We already have UnionI, UnionE*)
mengj@19768
   461
   "SetInterval.atLeastAtMost_iff", (*obscure and prolific*)
mengj@19768
   462
   "SetInterval.atLeastLessThan_iff", (*obscure and prolific*)
mengj@19768
   463
   "SetInterval.greaterThanAtMost_iff", (*obscure and prolific*)
mengj@19768
   464
   "SetInterval.greaterThanLessThan_iff", (*obscure and prolific*)
mengj@19768
   465
   "SetInterval.ivl_subset"];  (*excessive case analysis*)
mengj@19768
   466
mengj@19768
   467
mengj@19768
   468
(*These might be prolific but are probably OK, and min and max are basic.
mengj@19768
   469
   "Orderings.max_less_iff_conj", 
mengj@19768
   470
   "Orderings.min_less_iff_conj",
mengj@19768
   471
   "Orderings.min_max.below_inf.below_inf_conv",
mengj@19768
   472
   "Orderings.min_max.below_sup.above_sup_conv",
mengj@19768
   473
Very prolific and somewhat obscure:
mengj@19768
   474
   "Set.InterD",
mengj@19768
   475
   "Set.UnionI",
mengj@19768
   476
*)
mengj@19768
   477
mengj@19768
   478
(*** retrieve lemmas from clasimpset and atpset, may filter them ***)
mengj@19768
   479
mengj@19768
   480
(*Hashing to detect duplicate and variant clauses, e.g. from the [iff] attribute*)
mengj@19768
   481
mengj@19768
   482
exception HASH_CLAUSE and HASH_STRING;
mengj@19768
   483
mengj@19768
   484
(*Catches (for deletion) theorems automatically generated from other theorems*)
mengj@19768
   485
fun insert_suffixed_names ht x = 
mengj@19768
   486
     (Polyhash.insert ht (x^"_iff1", ()); 
mengj@19768
   487
      Polyhash.insert ht (x^"_iff2", ()); 
mengj@19768
   488
      Polyhash.insert ht (x^"_dest", ())); 
mengj@19768
   489
paulson@20444
   490
(*Are all characters in this string digits?*)
paulson@20444
   491
fun all_numeric s = null (String.tokens Char.isDigit s);
paulson@20444
   492
paulson@20444
   493
(*Delete a suffix of the form _\d+*)
paulson@20444
   494
fun delete_numeric_suffix s =
paulson@20444
   495
  case rev (String.fields (fn c => c = #"_") s) of
paulson@20444
   496
      last::rest => 
paulson@20444
   497
          if all_numeric last 
webertj@20446
   498
          then [s, space_implode "_" (rev rest)]
paulson@20444
   499
          else [s]
paulson@20444
   500
    | [] => [s];
paulson@20444
   501
paulson@20457
   502
fun banned_thmlist s =
paulson@20457
   503
  (Sign.base_name s) mem_string ["induct","inducts","split","splits","split_asm"];
paulson@20457
   504
paulson@20757
   505
(*Reject theorems with names like "List.filter.filter_list_def" or
paulson@20823
   506
  "Accessible_Part.acc.defs", as these are definitions arising from packages.
paulson@20823
   507
  FIXME: this will also block definitions within locales*)
paulson@20757
   508
fun is_package_def a =
paulson@20823
   509
   length (NameSpace.unpack a) > 2 andalso 
paulson@20823
   510
   String.isSuffix "_def" a  orelse  String.isSuffix "_defs" a;
paulson@20757
   511
mengj@19768
   512
fun make_banned_test xs = 
mengj@19768
   513
  let val ht = Polyhash.mkTable (Polyhash.hash_string, op =)
mengj@19768
   514
                                (6000, HASH_STRING)
paulson@20757
   515
      fun banned_aux s = 
paulson@20757
   516
            isSome (Polyhash.peek ht s) orelse banned_thmlist s orelse is_package_def s
paulson@20457
   517
      fun banned s = exists banned_aux (delete_numeric_suffix s)
mengj@19768
   518
  in  app (fn x => Polyhash.insert ht (x,())) (!blacklist);
mengj@19768
   519
      app (insert_suffixed_names ht) (!blacklist @ xs); 
mengj@19768
   520
      banned
mengj@19768
   521
  end;
mengj@19768
   522
mengj@19768
   523
(** a hash function from Term.term to int, and also a hash table **)
mengj@19768
   524
val xor_words = List.foldl Word.xorb 0w0;
mengj@19768
   525
mengj@19768
   526
fun hashw_term ((Const(c,_)), w) = Polyhash.hashw_string (c,w)
paulson@20661
   527
  | hashw_term ((Free(a,_)), w) = Polyhash.hashw_string (a,w)
mengj@19768
   528
  | hashw_term ((Var(_,_)), w) = w
paulson@20661
   529
  | hashw_term ((Bound i), w) = Polyhash.hashw_int(i,w)
mengj@19768
   530
  | hashw_term ((Abs(_,_,t)), w) = hashw_term (t, w)
mengj@19768
   531
  | hashw_term ((P$Q), w) = hashw_term (Q, (hashw_term (P, w)));
mengj@19768
   532
paulson@21070
   533
fun hash_literal (Const("Not",_)$P) = Word.notb(hashw_term(P,0w0))
paulson@21070
   534
  | hash_literal P = hashw_term(P,0w0);
mengj@19768
   535
mengj@19768
   536
mengj@19768
   537
fun get_literals (Const("Trueprop",_)$P) lits = get_literals P lits
mengj@19768
   538
  | get_literals (Const("op |",_)$P$Q) lits = get_literals Q (get_literals P lits)
mengj@19768
   539
  | get_literals lit lits = (lit::lits);
mengj@19768
   540
paulson@20457
   541
fun hash_term t = Word.toIntX (xor_words (map hash_literal (get_literals t [])));
mengj@19768
   542
mengj@19768
   543
fun equal_thm (thm1,thm2) = Term.aconv(prop_of thm1, prop_of thm2);
paulson@20457
   544
mengj@19768
   545
(*Create a hash table for clauses, of the given size*)
mengj@19768
   546
fun mk_clause_table n =
paulson@20457
   547
      Polyhash.mkTable (hash_term o prop_of, equal_thm)
mengj@19768
   548
                       (n, HASH_CLAUSE);
mengj@19768
   549
paulson@20457
   550
(*Use a hash table to eliminate duplicates from xs. Argument is a list of
paulson@20868
   551
  (thm * (string * int)) tuples. The theorems are hashed into the table. *)
paulson@20457
   552
fun make_unique xs = 
paulson@20868
   553
  let val ht = mk_clause_table 7000
paulson@20457
   554
  in
paulson@21070
   555
      Output.debug("make_unique gets " ^ Int.toString (length xs) ^ " clauses");
paulson@20868
   556
      app (ignore o Polyhash.peekInsert ht) xs;  
paulson@20868
   557
      Polyhash.listItems ht
paulson@20457
   558
  end;
mengj@19768
   559
paulson@20868
   560
(*Remove existing axiom clauses from the conjecture clauses, as this can dramatically
paulson@20868
   561
  boost an ATP's performance (for some reason)*)
paulson@20868
   562
fun subtract_cls c_clauses ax_clauses = 
paulson@20868
   563
  let val ht = mk_clause_table 2200
paulson@20868
   564
      fun known x = isSome (Polyhash.peek ht x)
paulson@20868
   565
  in
paulson@20868
   566
      app (ignore o Polyhash.peekInsert ht) ax_clauses;  
paulson@20868
   567
      filter (not o known) c_clauses 
paulson@20868
   568
  end;
mengj@19768
   569
paulson@20868
   570
(*Filter axiom clauses, but keep supplied clauses and clauses in whitelist. 
paulson@20868
   571
  Duplicates are removed later.*)
paulson@20457
   572
fun get_relevant_clauses thy cls_thms white_cls goals =
paulson@20868
   573
  white_cls @ (ReduceAxiomsN.relevance_filter thy cls_thms goals);
paulson@20457
   574
mengj@19768
   575
fun display_thms [] = ()
mengj@19768
   576
  | display_thms ((name,thm)::nthms) = 
mengj@19768
   577
      let val nthm = name ^ ": " ^ (string_of_thm thm)
mengj@19768
   578
      in Output.debug nthm; display_thms nthms  end;
mengj@19768
   579
 
paulson@21224
   580
fun all_valid_thms ctxt =
paulson@21224
   581
  PureThy.thms_containing (ProofContext.theory_of ctxt) ([], []) @
paulson@21224
   582
  filter (ProofContext.valid_thms ctxt)
paulson@21224
   583
    (FactIndex.find (ProofContext.fact_index_of ctxt) ([], []));
paulson@21224
   584
paulson@21224
   585
fun multi_name a (th, (n,pairs)) = 
paulson@21224
   586
  (n+1, (a ^ "(" ^ Int.toString n ^ ")", th) :: pairs)
paulson@21224
   587
paulson@21224
   588
fun multi_names ((a, []), pairs) = pairs
paulson@21224
   589
  | multi_names ((a, [th]), pairs) = (a,th)::pairs
paulson@21224
   590
  | multi_names ((a, ths), pairs) = #2 (foldl (multi_name a) (1,pairs) ths);
paulson@21224
   591
paulson@21224
   592
fun name_thm_pairs ctxt = foldl multi_names [] (all_valid_thms ctxt);
paulson@21224
   593
paulson@21224
   594
fun check_named ("",th) = (warning ("No name for theorem " ^ string_of_thm th); false)
paulson@21224
   595
  | check_named (_,th) = true;
paulson@19894
   596
mengj@19768
   597
(* get lemmas from claset, simpset, atpset and extra supplied rules *)
mengj@19768
   598
fun get_clasimp_atp_lemmas ctxt user_thms = 
paulson@19894
   599
  let val included_thms =
paulson@19894
   600
	if !include_all 
paulson@20457
   601
	then (tap (fn ths => Output.debug
paulson@20457
   602
	             ("Including all " ^ Int.toString (length ths) ^ " theorems")) 
paulson@21224
   603
	          (name_thm_pairs ctxt))
paulson@19894
   604
	else 
paulson@19894
   605
	let val claset_thms =
paulson@19894
   606
		if !include_claset then ResAxioms.claset_rules_of_ctxt ctxt
paulson@19894
   607
		else []
paulson@19894
   608
	    val simpset_thms = 
paulson@19894
   609
		if !include_simpset then ResAxioms.simpset_rules_of_ctxt ctxt
paulson@19894
   610
		else []
paulson@19894
   611
	    val atpset_thms =
paulson@19894
   612
		if !include_atpset then ResAxioms.atpset_rules_of_ctxt ctxt
paulson@19894
   613
		else []
paulson@19894
   614
	    val _ = if !Output.show_debug_msgs 
paulson@19894
   615
		    then (Output.debug "ATP theorems: "; display_thms atpset_thms) 
paulson@19894
   616
		    else ()		 
paulson@19894
   617
	in  claset_thms @ simpset_thms @ atpset_thms  end
paulson@21224
   618
      val user_rules = filter check_named 
paulson@21224
   619
                         (map (ResAxioms.pairname)
paulson@21224
   620
			   (if null user_thms then !whitelist else user_thms))
paulson@19894
   621
  in
paulson@21224
   622
      (filter check_named included_thms, user_rules)
paulson@19894
   623
  end;
mengj@19768
   624
paulson@20457
   625
(*Remove lemmas that are banned from the backlist. Also remove duplicates. *)
mengj@19768
   626
fun blacklist_filter thms = 
paulson@19894
   627
  if !run_blacklist_filter then 
paulson@21070
   628
      let val _ = Output.debug("blacklist filter gets " ^ Int.toString (length thms) ^ " theorems")
paulson@21070
   629
          val banned = make_banned_test (map #1 thms)
paulson@19894
   630
	  fun ok (a,_) = not (banned a)
paulson@21070
   631
	  val okthms = filter ok thms
paulson@21070
   632
          val _ = Output.debug("...and returns " ^ Int.toString (length okthms))
paulson@21070
   633
      in  okthms end
paulson@19894
   634
  else thms;
mengj@19768
   635
paulson@20526
   636
mengj@19194
   637
(***************************************************************)
mengj@19194
   638
(* ATP invocation methods setup                                *)
mengj@19194
   639
(***************************************************************)
mengj@19194
   640
mengj@19194
   641
fun cnf_hyps_thms ctxt = 
wenzelm@20224
   642
    let val ths = Assumption.prems_of ctxt
wenzelm@19617
   643
    in fold (fold (insert Thm.eq_thm) o ResAxioms.skolem_thm) ths [] end;
mengj@19194
   644
paulson@20526
   645
(*Translation mode can be auto-detected, or forced to be first-order or higher-order*)
mengj@19194
   646
datatype mode = Auto | Fol | Hol;
mengj@19194
   647
paulson@19450
   648
val linkup_logic_mode = ref Auto;
paulson@19205
   649
paulson@20526
   650
(*Ensures that no higher-order theorems "leak out"*)
paulson@20526
   651
fun restrict_to_logic logic cls =
wenzelm@20532
   652
  if is_fol_logic logic then filter (Meson.is_fol_term o prop_of o fst) cls 
paulson@20526
   653
	                else cls;
paulson@20526
   654
mengj@20131
   655
fun tptp_writer logic goals filename (axioms,classrels,arities) user_lemmas =
paulson@19445
   656
    if is_fol_logic logic 
paulson@19445
   657
    then ResClause.tptp_write_file goals filename (axioms, classrels, arities)
mengj@20131
   658
    else ResHolClause.tptp_write_file goals filename (axioms, classrels, arities) user_lemmas;
paulson@19445
   659
mengj@20131
   660
fun dfg_writer logic goals filename (axioms,classrels,arities) user_lemmas =
paulson@19445
   661
    if is_fol_logic logic 
paulson@19445
   662
    then ResClause.dfg_write_file goals filename (axioms, classrels, arities)
mengj@20131
   663
    else ResHolClause.dfg_write_file goals filename (axioms, classrels, arities) user_lemmas;
mengj@19194
   664
paulson@20022
   665
(*Called by the oracle-based methods declared in res_atp_methods.ML*)
mengj@19722
   666
fun write_subgoal_file dfg mode ctxt conjectures user_thms n =
mengj@19442
   667
    let val conj_cls = make_clauses conjectures 
paulson@20444
   668
                         |> ResAxioms.assume_abstract_list |> Meson.finish_cnf
mengj@19442
   669
	val hyp_cls = cnf_hyps_thms ctxt
mengj@19194
   670
	val goal_cls = conj_cls@hyp_cls
paulson@20526
   671
	val logic = case mode of 
paulson@20526
   672
                            Auto => problem_logic_goals [map prop_of goal_cls]
paulson@20526
   673
			  | Fol => FOL
paulson@20526
   674
			  | Hol => HOL
paulson@19894
   675
	val (included_thms,user_rules) = get_clasimp_atp_lemmas ctxt user_thms
paulson@20457
   676
	val cla_simp_atp_clauses = included_thms |> blacklist_filter
paulson@20868
   677
	                             |> ResAxioms.cnf_rules_pairs |> make_unique 
paulson@20526
   678
                                     |> restrict_to_logic logic 
mengj@19768
   679
	val user_cls = ResAxioms.cnf_rules_pairs user_rules
paulson@20457
   680
	val thy = ProofContext.theory_of ctxt
paulson@20457
   681
	val axclauses = get_relevant_clauses thy cla_simp_atp_clauses
paulson@20868
   682
	                            user_cls (map prop_of goal_cls) |> make_unique
paulson@20526
   683
	val keep_types = if is_fol_logic logic then !fol_keep_types else is_typed_hol ()
mengj@19194
   684
	val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy else []
mengj@19194
   685
	val arity_clauses = if keep_types then ResClause.arity_clause_thy thy else []
mengj@19722
   686
        val writer = if dfg then dfg_writer else tptp_writer 
paulson@20757
   687
	and file = atp_input_file()
paulson@20757
   688
	and user_lemmas_names = map #1 user_rules
mengj@19194
   689
    in
paulson@20757
   690
	writer logic goal_cls file (axclauses,classrel_clauses,arity_clauses) user_lemmas_names;
paulson@20757
   691
	Output.debug ("Writing to " ^ file);
paulson@20757
   692
	file
mengj@19194
   693
    end;
mengj@19194
   694
mengj@19194
   695
mengj@19194
   696
(**** remove tmp files ****)
mengj@19194
   697
fun cond_rm_tmp file = 
paulson@20870
   698
    if !Output.show_debug_msgs orelse !destdir <> "" then Output.debug "ATP input kept..." 
paulson@20870
   699
    else OS.FileSys.remove file;
mengj@19194
   700
mengj@19194
   701
mengj@19194
   702
(****** setup ATPs as Isabelle methods ******)
mengj@19194
   703
fun atp_meth' tac ths ctxt = 
mengj@19194
   704
    Method.SIMPLE_METHOD' HEADGOAL
mengj@19194
   705
    (tac ctxt ths);
mengj@19194
   706
mengj@19194
   707
fun atp_meth tac ths ctxt = 
mengj@19194
   708
    let val thy = ProofContext.theory_of ctxt
mengj@19194
   709
	val _ = ResClause.init thy
mengj@19194
   710
	val _ = ResHolClause.init thy
mengj@19194
   711
    in
mengj@19194
   712
	atp_meth' tac ths ctxt
mengj@19194
   713
    end;
mengj@19194
   714
mengj@19194
   715
fun atp_method tac = Method.thms_ctxt_args (atp_meth tac);
mengj@19194
   716
mengj@19194
   717
(***************************************************************)
mengj@19194
   718
(* automatic ATP invocation                                    *)
mengj@19194
   719
(***************************************************************)
mengj@19194
   720
paulson@17306
   721
(* call prover with settings and problem file for the current subgoal *)
paulson@17764
   722
fun watcher_call_provers sign sg_terms (childin, childout, pid) =
wenzelm@16802
   723
  let
paulson@17422
   724
    fun make_atp_list [] n = []
paulson@17717
   725
      | make_atp_list (sg_term::xs) n =
wenzelm@16802
   726
          let
paulson@17717
   727
            val probfile = prob_pathname n
paulson@17690
   728
            val time = Int.toString (!time_limit)
wenzelm@16802
   729
          in
wenzelm@18680
   730
            Output.debug ("problem file in watcher_call_provers is " ^ probfile);
paulson@17764
   731
            (*options are separated by Watcher.setting_sep, currently #"%"*)
paulson@17306
   732
            if !prover = "spass"
wenzelm@16802
   733
            then
paulson@19445
   734
              let val spass = helper_path "SPASS_HOME" "SPASS"
paulson@19445
   735
                  val sopts =
paulson@19445
   736
   "-Auto%-SOS=1%-PGiven=0%-PProblem=0%-Splits=0%-FullRed=0%-DocProof%-TimeLimit=" ^ time
paulson@19445
   737
              in 
paulson@19445
   738
                  ("spass", spass, sopts, probfile) :: make_atp_list xs (n+1)
wenzelm@16802
   739
              end
paulson@17306
   740
            else if !prover = "vampire"
quigley@17235
   741
	    then 
paulson@17819
   742
              let val vampire = helper_path "VAMPIRE_HOME" "vampire"
paulson@21132
   743
                  val vopts = "--mode casc%-t " ^ time  (*what about -m 100000?*)
wenzelm@16802
   744
              in
paulson@19445
   745
                  ("vampire", vampire, vopts, probfile) :: make_atp_list xs (n+1)
wenzelm@16802
   746
              end
paulson@17306
   747
      	     else if !prover = "E"
paulson@17306
   748
      	     then
paulson@17819
   749
	       let val Eprover = helper_path "E_HOME" "eproof"
paulson@17306
   750
	       in
paulson@19445
   751
		  ("E", Eprover, 
paulson@21224
   752
		     "--tptp-in%-l5%-xAutoDev%-tAutoDev%--silent%--cpu-limit=" ^ time, probfile) ::
paulson@19445
   753
		   make_atp_list xs (n+1)
paulson@17306
   754
	       end
paulson@17306
   755
	     else error ("Invalid prover name: " ^ !prover)
wenzelm@16802
   756
          end
paulson@15452
   757
paulson@17422
   758
    val atp_list = make_atp_list sg_terms 1
wenzelm@16802
   759
  in
wenzelm@16802
   760
    Watcher.callResProvers(childout,atp_list);
wenzelm@18680
   761
    Output.debug "Sent commands to watcher!"
wenzelm@16802
   762
  end
paulson@20022
   763
  
paulson@20022
   764
fun trace_array fname =
paulson@20995
   765
  let val path = File.unpack_platform_path fname
paulson@20022
   766
  in  Array.app (File.append path o (fn s => s ^ "\n"))  end;
quigley@16357
   767
paulson@20457
   768
(*Converting a subgoal into negated conjecture clauses*)
paulson@20457
   769
fun neg_clauses th n =
paulson@20457
   770
  let val tacs = [rtac ccontr, ObjectLogic.atomize_tac, skolemize_tac]
paulson@20457
   771
      val st = Seq.hd (EVERY' tacs n th)
paulson@20457
   772
      val negs = Option.valOf (metahyps_thms n st)
paulson@20457
   773
  in make_clauses negs |> ResAxioms.assume_abstract_list |> Meson.finish_cnf end;
paulson@20457
   774
		                       
paulson@20022
   775
(*We write out problem files for each subgoal. Argument probfile generates filenames,
paulson@18986
   776
  and allows the suppression of the suffix "_1" in problem-generation mode.
paulson@18986
   777
  FIXME: does not cope with &&, and it isn't easy because one could have multiple
paulson@18986
   778
  subgoals, each involving &&.*)
paulson@20022
   779
fun write_problem_files probfile (ctxt,th)  =
paulson@18753
   780
  let val goals = Thm.prems_of th
mengj@19194
   781
      val _ = Output.debug ("number of subgoals = " ^ Int.toString (length goals))
paulson@17717
   782
      val thy = ProofContext.theory_of ctxt
paulson@20457
   783
      fun get_neg_subgoals [] _ = []
paulson@20526
   784
        | get_neg_subgoals (gl::gls) n = neg_clauses th n :: get_neg_subgoals gls (n+1)
paulson@20526
   785
      val goal_cls = get_neg_subgoals goals 1
paulson@20457
   786
      val logic = case !linkup_logic_mode of
paulson@20526
   787
		Auto => problem_logic_goals (map ((map prop_of)) goal_cls)
paulson@20457
   788
	      | Fol => FOL
paulson@20457
   789
	      | Hol => HOL
paulson@20526
   790
      val (included_thms,white_thms) = get_clasimp_atp_lemmas ctxt []
paulson@20526
   791
      val included_cls = included_thms |> blacklist_filter
paulson@20868
   792
                                       |> ResAxioms.cnf_rules_pairs |> make_unique 
paulson@20526
   793
                                       |> restrict_to_logic logic 
paulson@21070
   794
      val _ = Output.debug ("clauses = " ^ Int.toString(length included_cls))
paulson@20526
   795
      val white_cls = ResAxioms.cnf_rules_pairs white_thms
paulson@20526
   796
      (*clauses relevant to goal gl*)
paulson@20526
   797
      val axcls_list = map (fn gl => get_relevant_clauses thy included_cls white_cls [gl])
paulson@20526
   798
                           goals
paulson@21070
   799
      val _ = app (fn axcls => Output.debug ("filtered clauses = " ^ Int.toString(length axcls)))
paulson@21070
   800
                  axcls_list
paulson@20457
   801
      val keep_types = if is_fol_logic logic then !ResClause.keep_types 
paulson@20457
   802
                       else is_typed_hol ()
paulson@20457
   803
      val classrel_clauses = if keep_types then ResClause.classrel_clauses_thy thy
paulson@20457
   804
                             else []
paulson@20457
   805
      val _ = Output.debug ("classrel clauses = " ^ 
paulson@20457
   806
                            Int.toString (length classrel_clauses))
paulson@20457
   807
      val arity_clauses = if keep_types then ResClause.arity_clause_thy thy 
paulson@20457
   808
                          else []
wenzelm@18680
   809
      val _ = Output.debug ("arity clauses = " ^ Int.toString (length arity_clauses))
mengj@19718
   810
      val writer = if !prover = "spass" then dfg_writer else tptp_writer 
paulson@20526
   811
      fun write_all [] [] _ = []
paulson@20526
   812
	| write_all (ccls::ccls_list) (axcls::axcls_list) k =
paulson@20868
   813
            let val fname = probfile k
paulson@20868
   814
                val axcls = make_unique axcls
paulson@20868
   815
                val ccls = subtract_cls ccls axcls
paulson@20868
   816
                val clnames = writer logic ccls fname (axcls,classrel_clauses,arity_clauses) []
paulson@20870
   817
                val thm_names = Array.fromList clnames
paulson@20870
   818
                val _ = if !Output.show_debug_msgs 
paulson@20870
   819
                        then trace_array (fname ^ "_thm_names") thm_names else ()
paulson@20870
   820
            in  (thm_names,fname) :: write_all ccls_list axcls_list (k+1)  end
paulson@20870
   821
      val (thm_names_list, filenames) = ListPair.unzip (write_all goal_cls axcls_list 1)
mengj@19194
   822
  in
paulson@20870
   823
      (filenames, thm_names_list)
mengj@19194
   824
  end;
quigley@15644
   825
paulson@17775
   826
val last_watcher_pid = ref (NONE : (TextIO.instream * TextIO.outstream * 
paulson@17775
   827
                                    Posix.Process.pid * string list) option);
paulson@17775
   828
paulson@17775
   829
fun kill_last_watcher () =
paulson@17775
   830
    (case !last_watcher_pid of 
paulson@17775
   831
         NONE => ()
paulson@19445
   832
       | SOME (_, _, pid, files) => 
wenzelm@18680
   833
	  (Output.debug ("Killing old watcher, pid = " ^ string_of_pid pid);
paulson@17775
   834
	   Watcher.killWatcher pid;  
paulson@20870
   835
	   ignore (map (try cond_rm_tmp) files)))
wenzelm@18680
   836
     handle OS.SysErr _ => Output.debug "Attempt to kill watcher failed";
paulson@17525
   837
paulson@17525
   838
(*writes out the current clasimpset to a tptp file;
paulson@17525
   839
  turns off xsymbol at start of function, restoring it at end    *)
paulson@20954
   840
fun isar_atp_body (ctxt, th) =
paulson@17717
   841
  if Thm.no_prems th then ()
wenzelm@16802
   842
  else
wenzelm@16802
   843
    let
paulson@17775
   844
      val _ = kill_last_watcher()
paulson@20870
   845
      val (files,thm_names_list) = write_problem_files prob_pathname (ctxt,th)
paulson@20870
   846
      val (childin, childout, pid) = Watcher.createWatcher (th, thm_names_list)
paulson@15608
   847
    in
paulson@17772
   848
      last_watcher_pid := SOME (childin, childout, pid, files);
wenzelm@18680
   849
      Output.debug ("problem files: " ^ space_implode ", " files); 
wenzelm@18680
   850
      Output.debug ("pid: " ^ string_of_pid pid);
paulson@17717
   851
      watcher_call_provers (sign_of_thm th) (Thm.prems_of th) (childin, childout, pid)
paulson@20954
   852
    end;
paulson@20954
   853
paulson@20954
   854
val isar_atp = setmp print_mode [] isar_atp_body;
paulson@20954
   855
paulson@20954
   856
(*For ML scripts, and primarily, for debugging*)
paulson@20954
   857
fun callatp () = 
paulson@20954
   858
  let val th = topthm()
paulson@20954
   859
      val ctxt = ProofContext.init (theory_of_thm th)
paulson@20954
   860
  in  isar_atp_body (ctxt, th)  end;
paulson@15608
   861
paulson@17422
   862
val isar_atp_writeonly = setmp print_mode [] 
paulson@17717
   863
      (fn (ctxt,th) =>
paulson@17717
   864
       if Thm.no_prems th then ()
paulson@17717
   865
       else 
paulson@20022
   866
         let val probfile = if Thm.nprems_of th = 1 then probfile_nosuffix 
paulson@20022
   867
          	            else prob_pathname
paulson@20022
   868
         in ignore (write_problem_files probfile (ctxt,th)) end);
paulson@15452
   869
quigley@16357
   870
wenzelm@16802
   871
(** the Isar toplevel hook **)
wenzelm@16802
   872
paulson@19205
   873
fun invoke_atp_ml (ctxt, goal) =
paulson@19205
   874
  let val thy = ProofContext.theory_of ctxt;
wenzelm@16802
   875
  in
wenzelm@18680
   876
    Output.debug ("subgoals in isar_atp:\n" ^ 
paulson@19205
   877
		  Pretty.string_of (ProofContext.pretty_term ctxt
paulson@19205
   878
		    (Logic.mk_conjunction_list (Thm.prems_of goal))));
wenzelm@18680
   879
    Output.debug ("current theory: " ^ Context.theory_name thy);
paulson@20419
   880
    inc hook_count;
wenzelm@18680
   881
    Output.debug ("in hook for time: " ^ Int.toString (!hook_count));
paulson@16925
   882
    ResClause.init thy;
mengj@19194
   883
    ResHolClause.init thy;
paulson@21132
   884
    if !time_limit > 0 then isar_atp (ctxt, goal)
paulson@21132
   885
    else (warning "Writing problem file only"; isar_atp_writeonly (ctxt, goal))
paulson@19205
   886
  end;
paulson@19205
   887
paulson@19205
   888
val invoke_atp = Toplevel.no_timing o Toplevel.unknown_proof o Toplevel.keep
paulson@19205
   889
 (fn state =>
paulson@19205
   890
  let val (ctxt, (_, goal)) = Proof.get_goal (Toplevel.proof_of state)
paulson@19205
   891
  in  invoke_atp_ml (ctxt, goal)  end);
quigley@16357
   892
paulson@17091
   893
val call_atpP =
paulson@17746
   894
  OuterSyntax.command 
paulson@17091
   895
    "ProofGeneral.call_atp" 
paulson@17091
   896
    "call automatic theorem provers" 
paulson@17091
   897
    OuterKeyword.diag
paulson@19205
   898
    (Scan.succeed invoke_atp);
paulson@17091
   899
paulson@17091
   900
val _ = OuterSyntax.add_parsers [call_atpP];
paulson@17091
   901
paulson@15347
   902
end;