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