src/HOL/Tools/res_atp_setup.ML
author mengj
Thu Nov 03 04:31:12 2005 +0100 (2005-11-03)
changeset 18086 051b7f323b4c
parent 18014 8bedb073e628
child 18141 89e2e8bed08f
permissions -rw-r--r--
Changed the way additional lemmas are passed to ATP methods for proof of a goal: now only list them after the methods' names.
Also removed some functions that are not used any more.
mengj@17907
     1
(* ID: $Id$ 
mengj@17907
     2
   Author: Jia Meng, NICTA
mengj@17905
     3
mengj@17905
     4
*)
mengj@17905
     5
structure ResAtpSetup =
mengj@17905
     6
mengj@17905
     7
struct
mengj@17905
     8
mengj@17905
     9
val keep_atp_input = ref false;
mengj@17905
    10
val debug = ref true;
mengj@17905
    11
val filter_ax = ref false;
mengj@17905
    12
mengj@17905
    13
(*******************************************************)
mengj@17905
    14
(* set up the output paths                             *)
mengj@17905
    15
(*******************************************************)
mengj@17905
    16
mengj@17905
    17
val claset_file = File.tmp_path (Path.basic "claset");
mengj@17905
    18
val simpset_file = File.tmp_path (Path.basic "simp");
mengj@18086
    19
val local_lemma_file = File.tmp_path (Path.basic "locallemmas");
mengj@17905
    20
mengj@17905
    21
val prob_file = File.tmp_path (Path.basic "prob");
mengj@17905
    22
val hyps_file = File.tmp_path (Path.basic "hyps");
mengj@17905
    23
mengj@18001
    24
val types_sorts_file =  File.tmp_path (Path.basic "typsorts");
mengj@17905
    25
mengj@17905
    26
(*******************************************************)
mengj@17905
    27
(* operations on Isabelle theorems:                    *)
mengj@17905
    28
(* retrieving from ProofContext,                       *)
mengj@17905
    29
(* modifying local theorems,                           *)
mengj@17905
    30
(* CNF                                                 *)
mengj@17905
    31
(*******************************************************)
mengj@17905
    32
mengj@17905
    33
(* a special version of repeat_RS *)
mengj@18001
    34
fun repeat_someI_ex thm = ResAxioms.repeat_RS thm someI_ex;
mengj@17905
    35
mengj@17905
    36
val include_simpset = ref false;
mengj@17905
    37
val include_claset = ref false; 
mengj@17905
    38
mengj@17905
    39
val add_simpset = (fn () => include_simpset:=true);
mengj@17905
    40
val add_claset = (fn () => include_claset:=true);
mengj@17905
    41
val add_clasimp = (fn () => include_simpset:=true;include_claset:=true);
mengj@17905
    42
val rm_simpset = (fn () => include_simpset:=false);
mengj@17905
    43
val rm_claset = (fn () => include_claset:=false);
mengj@17905
    44
val rm_clasimp = (fn () => include_simpset:=false;include_claset:=false);
mengj@17905
    45
mengj@17905
    46
mengj@17905
    47
fun get_local_claR ctxt = 
mengj@17905
    48
    let val cla_rules = rep_cs (Classical.get_local_claset ctxt) 
mengj@17905
    49
	val safeEs = #safeEs cla_rules
mengj@17905
    50
	val safeIs = #safeIs cla_rules
mengj@17905
    51
	val hazEs = #hazEs cla_rules
mengj@17905
    52
	val hazIs = #hazIs cla_rules
mengj@17905
    53
    in
mengj@17905
    54
	map ResAxioms.pairname (safeEs @ safeIs @ hazEs @ hazIs)
mengj@17905
    55
    end;
mengj@17905
    56
mengj@17905
    57
mengj@17905
    58
fun get_local_simpR ctxt = 
mengj@17905
    59
    let val simpset_rules = #rules(fst (rep_ss (Simplifier.get_local_simpset ctxt)))
mengj@17905
    60
    in
mengj@17905
    61
	map (fn r => (#name r, #thm r)) (Net.entries simpset_rules) end;
mengj@17905
    62
mengj@17905
    63
mengj@17905
    64
mengj@17905
    65
(***************************************************************)
mengj@17905
    66
(* prover-specific output format:                              *)
mengj@17905
    67
(* TPTP                                                        *)
mengj@17905
    68
(***************************************************************)
mengj@17905
    69
mengj@17905
    70
mengj@17905
    71
mengj@17905
    72
mengj@17905
    73
(***************** TPTP format *********************************)
mengj@17905
    74
mengj@17905
    75
(* convert each (sub)goal clause (Thm.thm) into one or more TPTP clauses.  The additional TPTP clauses are tfree_lits.  Write the output TPTP clauses to a problem file *)
mengj@18001
    76
mengj@18001
    77
fun mk_conjectures is_fol terms =
mengj@18001
    78
    if is_fol then
mengj@18001
    79
	ListPair.unzip (map ResClause.clause2tptp (ResClause.make_conjecture_clauses terms))
mengj@18001
    80
    else
mengj@18001
    81
	ListPair.unzip (map ResHolClause.clause2tptp (ResHolClause.make_conjecture_clauses terms));
mengj@18001
    82
mengj@18001
    83
mengj@18001
    84
fun tptp_form_g is_fol thms n tfrees =
mengj@18001
    85
    let val terms = map prop_of thms
mengj@18001
    86
	val (tptp_clss,tfree_litss) = mk_conjectures is_fol terms
mengj@17939
    87
	val tfree_clss = map ResClause.tfree_clause ((ResClause.union_all tfree_litss) \\ tfrees)
mengj@17905
    88
	val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
mengj@17905
    89
	val out = TextIO.openOut(probfile)		 	
mengj@17905
    90
    in
mengj@18001
    91
	ResAtp.writeln_strs out (tfree_clss @ tptp_clss);
mengj@17905
    92
	TextIO.closeOut out;
mengj@17905
    93
	warning probfile; (* show the location for debugging *)
mengj@17905
    94
	probfile (* return the location *)
mengj@17905
    95
	
mengj@17905
    96
    end;
mengj@17905
    97
mengj@17905
    98
mengj@18001
    99
val tptp_form = tptp_form_g true;
mengj@18001
   100
val tptp_formH = tptp_form_g false;
mengj@18001
   101
mengj@18001
   102
fun tptp_hyps_g _ [] = ([], [])
mengj@18001
   103
  | tptp_hyps_g is_fol thms =
mengj@18001
   104
    let val mk_nnf = if is_fol then make_nnf else make_nnf1
mengj@18001
   105
	val prems = map (skolemize o mk_nnf o ObjectLogic.atomize_thm) thms
mengj@17905
   106
        val prems' = map repeat_someI_ex prems
mengj@17905
   107
        val prems'' = make_clauses prems'
mengj@17905
   108
        val prems''' = ResAxioms.rm_Eps [] prems''
mengj@18001
   109
	val (tptp_clss,tfree_litss) = mk_conjectures is_fol prems'''
mengj@17939
   110
	val tfree_lits = ResClause.union_all tfree_litss
mengj@17905
   111
	val tfree_clss = map ResClause.tfree_clause tfree_lits 
mengj@17905
   112
        val hypsfile = File.platform_path hyps_file
mengj@17905
   113
        val out = TextIO.openOut(hypsfile)
mengj@17905
   114
    in
mengj@18001
   115
        ResAtp.writeln_strs out (tfree_clss @ tptp_clss);
mengj@17905
   116
        TextIO.closeOut out; warning hypsfile;
mengj@17905
   117
        ([hypsfile],tfree_lits)
mengj@17905
   118
    end;
mengj@17905
   119
 
mengj@17905
   120
mengj@18001
   121
val tptp_hyps = tptp_hyps_g true;
mengj@18001
   122
val tptp_hypsH = tptp_hyps_g false;
mengj@18001
   123
mengj@18001
   124
fun mk_axioms is_fol rules =
mengj@18001
   125
    if is_fol then 
mengj@18001
   126
	(let val  clss = ResClause.union_all(ResAxioms.clausify_rules_pairs rules)
mengj@18001
   127
	     val (clss',names) = ListPair.unzip clss
mengj@18001
   128
	     val (tptpclss,_) = ListPair.unzip(map ResClause.clause2tptp clss')
mengj@18001
   129
	 in tptpclss end)
mengj@18001
   130
    else
mengj@18001
   131
	(let val  clss = ResClause.union_all(ResAxioms.clausify_rules_pairsH rules)
mengj@18001
   132
	     val (clss',names) = ListPair.unzip clss
mengj@18001
   133
	     val (tptpclss,_) = ListPair.unzip(map ResHolClause.clause2tptp clss')
mengj@18001
   134
	 in tptpclss end)
mengj@18001
   135
mengj@18001
   136
mengj@17905
   137
mengj@18001
   138
fun write_rules_g is_fol [] file = [](* no rules, then no need to write anything, thus no clasimp file *)
mengj@18001
   139
  | write_rules_g is_fol rules file =
mengj@17905
   140
    let val out = TextIO.openOut(file)
mengj@18001
   141
	val tptpclss = mk_axioms is_fol rules
mengj@17905
   142
    in
mengj@18001
   143
	ResAtp.writeln_strs out tptpclss;
mengj@17905
   144
	TextIO.closeOut out; warning file;
mengj@17905
   145
	[file]
mengj@17905
   146
    end;
mengj@17905
   147
mengj@18001
   148
mengj@18001
   149
val write_rules = write_rules_g true;
mengj@18001
   150
val write_rulesH = write_rules_g false;
mengj@18001
   151
mengj@18086
   152
mengj@17905
   153
mengj@17905
   154
(* TPTP Isabelle theorems *)
mengj@18086
   155
fun tptp_cnf_rules_g write_rules ths (clasetR,simpsetR) =
mengj@17905
   156
    let val simpfile = File.platform_path simpset_file
mengj@17905
   157
	val clafile =  File.platform_path claset_file
mengj@18086
   158
	val local_lemfile = File.platform_path local_lemma_file
mengj@17905
   159
    in
mengj@18086
   160
	(write_rules clasetR clafile) @ (write_rules simpsetR simpfile) @ (write_rules ths local_lemfile)
mengj@17905
   161
    end;
mengj@17905
   162
mengj@18001
   163
val tptp_cnf_rules = tptp_cnf_rules_g write_rules;
mengj@18001
   164
val tptp_cnf_rulesH = tptp_cnf_rules_g write_rulesH;
mengj@18001
   165
mengj@17905
   166
mengj@18086
   167
fun tptp_cnf_clasimp_g tptp_cnf_rules ths ctxt (include_claset,include_simpset) =
mengj@18086
   168
    let val local_claR = if include_claset then get_local_claR ctxt else []
mengj@18086
   169
	val local_simpR = if include_simpset then get_local_simpR ctxt else []
mengj@18086
   170
	val ths_names = map ResAxioms.pairname ths
mengj@17905
   171
    in
mengj@18086
   172
	tptp_cnf_rules ths_names (local_claR,local_simpR)
mengj@17905
   173
    end;
mengj@17905
   174
mengj@17905
   175
mengj@18086
   176
val tptp_cnf_clasimp  = tptp_cnf_clasimp_g tptp_cnf_rules;
mengj@18001
   177
val tptp_cnf_clasimpH = tptp_cnf_clasimp_g tptp_cnf_rulesH;
mengj@18001
   178
mengj@18001
   179
mengj@18001
   180
fun tptp_types_sorts thy = 
mengj@18001
   181
    let val classrel_clauses = ResClause.classrel_clauses_thy thy
mengj@18001
   182
	val arity_clauses = ResClause.arity_clause_thy thy
mengj@18001
   183
	val classrel_cls = map ResClause.tptp_classrelClause classrel_clauses
mengj@18001
   184
	val arity_cls = map ResClause.tptp_arity_clause arity_clauses
mengj@18001
   185
	fun write_ts () =
mengj@18001
   186
	    let val tsfile = File.platform_path types_sorts_file
mengj@18001
   187
		val out = TextIO.openOut(tsfile)
mengj@18001
   188
	    in
mengj@18001
   189
		ResAtp.writeln_strs out (classrel_cls @ arity_cls);
mengj@18001
   190
		TextIO.closeOut out;
mengj@18001
   191
		[tsfile]
mengj@18001
   192
	    end
mengj@18001
   193
    in
mengj@18001
   194
	if (List.null arity_cls andalso List.null classrel_cls) then []
mengj@18001
   195
	else
mengj@18001
   196
	    write_ts ()
mengj@18001
   197
    end;
mengj@18001
   198
mengj@18001
   199
mengj@18001
   200
mengj@17905
   201
(*FIXME: a function that does not perform any filtering now *)
mengj@18086
   202
fun find_relevant_ax tptp_cnf_clasimp ths ctxt = tptp_cnf_clasimp ths ctxt (true,true);
mengj@17905
   203
mengj@17905
   204
mengj@17905
   205
(***************************************************************)
mengj@18086
   206
(* setup ATPs as Isabelle methods                              *)
mengj@17905
   207
(***************************************************************)
mengj@18086
   208
fun atp_meth_g tptp_hyps tptp_cnf_clasimp tac ths ctxt = 
mengj@18086
   209
    let val rules = if !filter_ax then find_relevant_ax tptp_cnf_clasimp ths ctxt 
mengj@18086
   210
		    else tptp_cnf_clasimp ths ctxt (!include_claset, !include_simpset) 
mengj@17905
   211
	val (hyps,tfrees) = tptp_hyps (ProofContext.prems_of ctxt)
mengj@18001
   212
	val thy = ProofContext.theory_of ctxt
mengj@18001
   213
	val tsfile = tptp_types_sorts thy
mengj@18001
   214
	val files = hyps @ rules @ tsfile
mengj@17905
   215
    in
mengj@18086
   216
	Method.SIMPLE_METHOD' HEADGOAL
mengj@18086
   217
	(tac ctxt files tfrees)
mengj@17905
   218
    end;
mengj@18086
   219
fun atp_meth_f tac ths ctxt = atp_meth_g tptp_hyps tptp_cnf_clasimp tac ths ctxt;
mengj@18086
   220
fun atp_meth_h tac ths ctxt = atp_meth_g tptp_hypsH tptp_cnf_clasimpH tac ths ctxt;
mengj@17905
   221
mengj@17905
   222
mengj@18086
   223
fun atp_meth_G atp_meth tac ths ctxt =
mengj@18086
   224
    let val _  = ResClause.init (ProofContext.theory_of ctxt)
mengj@18086
   225
    in
mengj@18086
   226
	atp_meth tac ths ctxt
mengj@18086
   227
    end;
mengj@18086
   228
fun atp_meth_H tac ths ctxt = atp_meth_G atp_meth_h tac ths ctxt;
mengj@18086
   229
fun atp_meth_F tac ths ctxt = atp_meth_G atp_meth_f tac ths ctxt;
mengj@18001
   230
mengj@18086
   231
mengj@18086
   232
fun atp_method_H tac = Method.thms_ctxt_args (atp_meth_H tac);
mengj@18086
   233
fun atp_method_F tac = Method.thms_ctxt_args (atp_meth_F tac);
mengj@18001
   234
mengj@17905
   235
mengj@17905
   236
mengj@17905
   237
(*************Remove tmp files********************************)
mengj@17905
   238
fun rm_tmp_files1 [] = ()
mengj@17905
   239
  | rm_tmp_files1 (f::fs) =
mengj@17905
   240
    (OS.FileSys.remove f; rm_tmp_files1 fs);
mengj@17905
   241
mengj@17939
   242
fun cond_rm_tmp files = 
mengj@17939
   243
    if !keep_atp_input then warning "ATP input kept..." 
mengj@17939
   244
    else (warning "deleting ATP inputs..."; rm_tmp_files1 files);
mengj@17905
   245
mengj@17905
   246
mengj@17907
   247
end