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