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