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