src/HOL/Tools/res_atp_setup.ML
author mengj
Fri Oct 21 02:57:22 2005 +0200 (2005-10-21)
changeset 17939 3925ab7b8a18
parent 17907 c20e4bddcb11
child 18001 6ca14bec7cd5
permissions -rw-r--r--
Merged theory ResAtpOracle.thy into ResAtpMethods.thy
     1 (* ID: $Id$ 
     2    Author: Jia Meng, NICTA
     3 
     4 *)
     5 structure ResAtpSetup =
     6 
     7 struct
     8 
     9 val keep_atp_input = ref false;
    10 val debug = ref true;
    11 val filter_ax = ref false;
    12 
    13 
    14 fun writeln_strs _   []      = ()
    15   | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
    16 
    17 
    18 fun warning_thm thm nm = warning (nm ^ " " ^ (string_of_thm thm));
    19 
    20 fun warning_thms_n n thms nm =
    21     let val _ = warning ("total " ^ (string_of_int n) ^ " " ^ nm)
    22 	fun warning_thms_aux [] = warning ("no more " ^ nm)
    23 	  | warning_thms_aux (th::ths) = (warning_thm th nm; warning_thms_aux ths)
    24     in
    25 	warning_thms_aux thms
    26     end;
    27 
    28 
    29 (*******************************************************)
    30 (* set up the output paths                             *)
    31 (*******************************************************)
    32 
    33 val claset_file = File.tmp_path (Path.basic "claset");
    34 val simpset_file = File.tmp_path (Path.basic "simp");
    35 
    36 val prob_file = File.tmp_path (Path.basic "prob");
    37 val hyps_file = File.tmp_path (Path.basic "hyps");
    38 
    39 
    40 
    41 (*******************************************************)
    42 (* operations on Isabelle theorems:                    *)
    43 (* retrieving from ProofContext,                       *)
    44 (* modifying local theorems,                           *)
    45 (* CNF                                                 *)
    46 (*******************************************************)
    47 
    48 fun repeat_RS thm1 thm2 =
    49     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
    50     in
    51         if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
    52     end;
    53 
    54 (* a special version of repeat_RS *)
    55 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    56 
    57 val include_simpset = ref false;
    58 val include_claset = ref false; 
    59 
    60 val add_simpset = (fn () => include_simpset:=true);
    61 val add_claset = (fn () => include_claset:=true);
    62 val add_clasimp = (fn () => include_simpset:=true;include_claset:=true);
    63 val rm_simpset = (fn () => include_simpset:=false);
    64 val rm_claset = (fn () => include_claset:=false);
    65 val rm_clasimp = (fn () => include_simpset:=false;include_claset:=false);
    66 
    67 
    68 
    69 val rules_modifiers =
    70   Simplifier.simp_modifiers @ Splitter.split_modifiers @
    71   Classical.cla_modifiers @ iff_modifiers;
    72 
    73 
    74 
    75 fun get_local_claR ctxt = 
    76     let val cla_rules = rep_cs (Classical.get_local_claset ctxt) 
    77 	val safeEs = #safeEs cla_rules
    78 	val safeIs = #safeIs cla_rules
    79 	val hazEs = #hazEs cla_rules
    80 	val hazIs = #hazIs cla_rules
    81     in
    82 	map ResAxioms.pairname (safeEs @ safeIs @ hazEs @ hazIs)
    83     end;
    84 
    85 
    86 fun get_local_simpR ctxt = 
    87     let val simpset_rules = #rules(fst (rep_ss (Simplifier.get_local_simpset ctxt)))
    88     in
    89 	map (fn r => (#name r, #thm r)) (Net.entries simpset_rules) end;
    90 
    91 
    92 
    93 (***************************************************************)
    94 (* prover-specific output format:                              *)
    95 (* TPTP                                                        *)
    96 (***************************************************************)
    97 
    98 
    99 
   100 
   101 (***************** TPTP format *********************************)
   102 
   103 (* 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 *)
   104 fun tptp_form thms n tfrees =
   105     let val clss = ResClause.make_conjecture_clauses (map prop_of thms)
   106 	val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss)
   107 	val tfree_clss = map ResClause.tfree_clause ((ResClause.union_all tfree_litss) \\ tfrees)
   108 	val probfile = File.platform_path prob_file ^ "_" ^ string_of_int n
   109 	val out = TextIO.openOut(probfile)		 	
   110     in
   111 	writeln_strs out (tfree_clss @ tptp_clss);
   112 	TextIO.closeOut out;
   113 	warning probfile; (* show the location for debugging *)
   114 	probfile (* return the location *)
   115 	
   116     end;
   117 
   118 
   119 fun tptp_hyps [] = ([], [])
   120   | tptp_hyps thms =
   121     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
   122         val prems' = map repeat_someI_ex prems
   123         val prems'' = make_clauses prems'
   124         val prems''' = ResAxioms.rm_Eps [] prems''
   125         val clss = ResClause.make_conjecture_clauses prems'''
   126 	val (tptp_clss,tfree_litss) = ListPair.unzip (map ResClause.clause2tptp clss) 
   127 	val tfree_lits = ResClause.union_all tfree_litss
   128 	val tfree_clss = map ResClause.tfree_clause tfree_lits 
   129         val hypsfile = File.platform_path hyps_file
   130         val out = TextIO.openOut(hypsfile)
   131     in
   132         writeln_strs out (tfree_clss @ tptp_clss);
   133         TextIO.closeOut out; warning hypsfile;
   134         ([hypsfile],tfree_lits)
   135     end;
   136  
   137 
   138 fun subtract_simpset thy ctxt =
   139   let
   140     val rules1 = #rules (#1 (rep_ss (simpset_of thy)));
   141     val rules2 = #rules (#1 (rep_ss (local_simpset_of ctxt)));
   142   in map ResAxioms.pairname (map #thm (Net.subtract MetaSimplifier.eq_rrule rules1 rules2)) end;
   143 
   144 fun subtract_claset thy ctxt =
   145   let
   146     val (netI1, netE1) = #xtra_netpair (rep_cs (claset_of thy));
   147     val (netI2, netE2) = #xtra_netpair (rep_cs (local_claset_of ctxt));
   148     val subtract = map (#2 o #2) oo Net.subtract Tactic.eq_kbrl;
   149   in map ResAxioms.pairname (subtract netI1 netI2 @ subtract netE1 netE2) end;
   150 
   151 local
   152 
   153 fun write_rules [] file = [](* no rules, then no need to write anything, thus no clasimp file *)
   154   | write_rules rules file =
   155     let val out = TextIO.openOut(file)
   156 	val clss = ResClause.union_all(ResAxioms.clausify_rules_pairs rules)
   157 	val (clss',names) = ListPair.unzip clss
   158 	val (tptpclss,_) = ListPair.unzip(map ResClause.clause2tptp clss')
   159     in
   160 	writeln_strs out tptpclss;
   161 	TextIO.closeOut out; warning file;
   162 	[file]
   163     end;
   164 
   165 in
   166 
   167 (* TPTP Isabelle theorems *)
   168 fun tptp_cnf_rules (clasetR,simpsetR) =
   169     let val simpfile = File.platform_path simpset_file
   170 	val clafile =  File.platform_path claset_file
   171     in
   172 	(write_rules clasetR clafile) @ (write_rules simpsetR simpfile)
   173     end;
   174 
   175 end
   176 
   177 fun tptp_cnf_clasimp ctxt (include_claset,include_simpset) =
   178     let val local_claR = if include_claset then get_local_claR ctxt else (subtract_claset (ProofContext.theory_of ctxt) ctxt)
   179 	val local_simpR = if include_simpset then get_local_simpR ctxt else (subtract_simpset (ProofContext.theory_of ctxt) ctxt)
   180 
   181     in
   182 	tptp_cnf_rules (local_claR,local_simpR)
   183     end;
   184 
   185 
   186 (*FIXME: a function that does not perform any filtering now *)
   187 fun find_relevant_ax ctxt = tptp_cnf_clasimp ctxt (true,true);
   188 
   189 
   190 
   191 (***************************************************************)
   192 (* setup ATPs as Isabelle methods                               *)
   193 (***************************************************************)
   194 fun atp_meth' tac prems ctxt = 
   195     let val rules = if !filter_ax then find_relevant_ax ctxt 
   196 		    else tptp_cnf_clasimp ctxt (!include_claset, !include_simpset) 
   197 	val (hyps,tfrees) = tptp_hyps (ProofContext.prems_of ctxt)
   198 	val files = hyps @ rules
   199     in
   200 	Method.METHOD (fn facts =>
   201 			  if !debug then (warning_thms_n (length facts) facts "facts";HEADGOAL (tac ctxt files tfrees)) 
   202 			  else HEADGOAL (tac ctxt files tfrees))
   203     end;
   204 
   205 
   206 fun atp_meth tac prems ctxt =
   207     let val _ = ResClause.init (ProofContext.theory_of ctxt)
   208     in    
   209 	if not(List.null prems) then (warning_thms_n (length prems) prems "prems!!!"; atp_meth' tac prems ctxt)
   210 	else (atp_meth' tac prems ctxt)
   211     end;
   212 	
   213 val atp_method = Method.bang_sectioned_args rules_modifiers o atp_meth;
   214 
   215 
   216 
   217 
   218 (*************Remove tmp files********************************)
   219 fun rm_tmp_files1 [] = ()
   220   | rm_tmp_files1 (f::fs) =
   221     (OS.FileSys.remove f; rm_tmp_files1 fs);
   222 
   223 fun cond_rm_tmp files = 
   224     if !keep_atp_input then warning "ATP input kept..." 
   225     else (warning "deleting ATP inputs..."; rm_tmp_files1 files);
   226 
   227 
   228 end