src/HOL/Tools/res_atp.ML
changeset 15452 e2a721567f67
parent 15347 14585bc8fa09
child 15572 9c89b1adf573
equal deleted inserted replaced
15451:c6c8786b9921 15452:e2a721567f67
     2     ID: $Id$
     2     ID: $Id$
     3     Copyright 2004 University of Cambridge
     3     Copyright 2004 University of Cambridge
     4 
     4 
     5 ATPs with TPTP format input.
     5 ATPs with TPTP format input.
     6 *)
     6 *)
       
     7 
       
     8 (*Jia: changed: isar_atp now processes entire proof context.  fetch thms from delta simpset/claset*)
       
     9 
     7 signature RES_ATP = 
    10 signature RES_ATP = 
     8 sig
    11 sig
     9 
    12 
    10 val trace_res : bool ref
    13 val trace_res : bool ref
    11 val axiom_file : Path.T
    14 val axiom_file : Path.T
    12 val hyps_file : Path.T
    15 val hyps_file : Path.T
    13 val isar_atp : Thm.thm list * Thm.thm -> unit
    16 val isar_atp : ProofContext.context * Thm.thm -> unit
    14 val prob_file : Path.T
    17 val prob_file : Path.T
    15 val atp_ax_tac : Thm.thm list -> int -> Tactical.tactic
    18 val atp_ax_tac : Thm.thm list -> int -> Tactical.tactic
    16 val atp_tac : int -> Tactical.tactic
    19 val atp_tac : int -> Tactical.tactic
       
    20 val debug: bool ref
    17 
    21 
    18 end;
    22 end;
    19 
    23 
    20 structure ResAtp : RES_ATP =
    24 structure ResAtp : RES_ATP =
    21 
    25 
    22 struct
    26 struct
    23 
    27 
       
    28 (* used for debug *)
       
    29 val debug = ref false;
       
    30 
       
    31 fun debug_tac tac = (warning "testing";tac);
    24 (* default value is false *)
    32 (* default value is false *)
    25 
    33 
    26 val trace_res = ref false;
    34 val trace_res = ref false;
    27 
    35 
    28 val skolem_tac = skolemize_tac;
    36 val skolem_tac = skolemize_tac;
    44 val hyps_file = File.tmp_path (Path.basic "hyps");
    52 val hyps_file = File.tmp_path (Path.basic "hyps");
    45 val dummy_tac = PRIMITIVE(fn thm => thm );
    53 val dummy_tac = PRIMITIVE(fn thm => thm );
    46 
    54 
    47 
    55 
    48 
    56 
    49 fun tptp_inputs thms = 
    57 fun tptp_inputs thms n = 
    50     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    58     let val clss = map (ResClause.make_conjecture_clause_thm) thms
    51 	val tptp_clss = ResLib.flat_noDup(map ResClause.tptp_clause clss) 
    59 	val tptp_clss = ResLib.flat_noDup(map ResClause.tptp_clause clss) 
    52         val probfile = File.sysify_path prob_file
    60         val probfile = (File.sysify_path prob_file) ^ "_" ^ (string_of_int n)
    53 	val out = TextIO.openOut(probfile)
    61 	val out = TextIO.openOut(probfile)
    54     in
    62     in
    55 	(ResLib.writeln_strs out tptp_clss; TextIO.closeOut out; (if !trace_res then (warning probfile) else ()))
    63 	(ResLib.writeln_strs out tptp_clss; TextIO.closeOut out; (if !trace_res then (warning probfile) else ()))
    56     end;
    64     end;
    57 
    65 
    58 
    66 
    59 (**** for Isabelle/ML interface  ****)
    67 (**** for Isabelle/ML interface  ****)
    60 
    68 
    61 fun call_atp_tac thms = (tptp_inputs thms; dummy_tac);
    69 fun call_atp_tac thms n = (tptp_inputs thms n; dummy_tac);
    62 
    70 
    63 
    71 
    64 val atp_tac = SELECT_GOAL
    72 
    65   (EVERY1 [rtac ccontr, atomize_tac, skolemize_tac, 
    73 
    66   METAHYPS(fn negs => (call_atp_tac(make_clauses negs)))]);
    74 fun atp_tac n = SELECT_GOAL
       
    75   (EVERY1 [rtac ccontr,atomize_tac, skolemize_tac, 
       
    76   METAHYPS(fn negs => (call_atp_tac (make_clauses negs) n))]) n;
    67 
    77 
    68 
    78 
    69 fun atp_ax_tac axioms n = 
    79 fun atp_ax_tac axioms n = 
    70     let val axcls = ResLib.flat_noDup(map ResAxioms.clausify_axiom axioms)
    80     let val axcls = ResLib.flat_noDup(map ResAxioms.clausify_axiom axioms)
    71 	val axcls_str = ResClause.tptp_clauses2str (ResLib.flat_noDup(map ResClause.tptp_clause axcls))
    81 	val axcls_str = ResClause.tptp_clauses2str (ResLib.flat_noDup(map ResClause.tptp_clause axcls))
    95 
   105 
    96 (* a special version of repeat_RS *)
   106 (* a special version of repeat_RS *)
    97 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
   107 fun repeat_someI_ex thm = repeat_RS thm someI_ex;
    98 
   108 
    99 
   109 
   100 
   110 (* convert clauses from "assume" to conjecture. write to file "hyps" *)
   101 fun isar_atp_h thms =
   111 fun isar_atp_h thms =
   102     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
   112     let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
   103 	val prems' = map repeat_someI_ex prems
   113 	val prems' = map repeat_someI_ex prems
   104 	val prems'' = make_clauses prems'
   114 	val prems'' = make_clauses prems'
   105 	val prems''' = ResAxioms.rm_Eps [] prems''
   115 	val prems''' = ResAxioms.rm_Eps [] prems''
   106 	val clss = map ResClause.make_hypothesis_clause prems'''
   116 	val clss = map ResClause.make_conjecture_clause prems'''
   107 	val tptp_clss = ResLib.flat_noDup (map ResClause.tptp_clause clss)
   117 	val tptp_clss = ResLib.flat_noDup (map ResClause.tptp_clause clss)
   108         val hypsfile = File.sysify_path hyps_file
   118         val hypsfile = File.sysify_path hyps_file
   109 	val out = TextIO.openOut(hypsfile)
   119 	val out = TextIO.openOut(hypsfile)
   110     in
   120     in
   111 	(ResLib.writeln_strs out tptp_clss; TextIO.closeOut out; if !trace_res then (warning hypsfile) else ())
   121 	(ResLib.writeln_strs out tptp_clss; TextIO.closeOut out; if !trace_res then (warning hypsfile) else ())
   114 
   124 
   115 
   125 
   116 
   126 
   117 val isar_atp_g = atp_tac;
   127 val isar_atp_g = atp_tac;
   118 
   128 
   119 fun isar_atp_aux thms thm = 
   129 
   120     (isar_atp_h thms; isar_atp_g 1 thm;());
   130 fun isar_atp_goal' thm k n = 
       
   131     if (k > n) then () else (isar_atp_g k thm; isar_atp_goal' thm (k+1) n);
       
   132 
       
   133 
       
   134 fun isar_atp_goal thm n_subgoals = (if (!debug) then warning (string_of_thm thm) else (isar_atp_goal' thm 1 n_subgoals));
       
   135 
       
   136 
       
   137 
       
   138 fun isar_atp_aux thms thm n_subgoals = 
       
   139     (isar_atp_h thms; isar_atp_goal thm n_subgoals); (* convert both to conjecture clauses, but write to different files *)
       
   140 
       
   141 
       
   142 fun isar_atp' (thms, thm) =
       
   143     let val prems = prems_of thm
       
   144     in
       
   145 	case prems of [] => (if (!debug) then warning "entering forward, no goal" else ())
       
   146 		    | _ => (isar_atp_aux thms thm (length prems))
       
   147     end;
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 local
       
   153 
       
   154 fun retr_thms ([]:MetaSimplifier.rrule list) = []
       
   155 	  | retr_thms (r::rs) = (#thm r)::(retr_thms rs);
       
   156 
       
   157 
       
   158 fun snds [] = []
       
   159   |   snds ((x,y)::l) = y::(snds l);
       
   160 
       
   161 
       
   162 
       
   163 
       
   164 fun get_thms_cs claset =
       
   165     let val clsset = rep_cs claset
       
   166 	val safeEs = #safeEs clsset
       
   167 	val safeIs = #safeIs clsset
       
   168 	val hazEs = #hazEs clsset
       
   169 	val hazIs = #hazIs clsset
       
   170     in
       
   171 	safeEs @ safeIs @ hazEs @ hazIs
       
   172     end;
       
   173 
       
   174 
       
   175 
       
   176 fun append_name name [] _ = []
       
   177   | append_name name (thm::thms) k = (Thm.name_thm ((name ^ "_" ^ (string_of_int k)),thm)) :: (append_name name thms (k+1));
       
   178 
       
   179 fun append_names (name::names) (thms::thmss) =
       
   180     let val thms' = append_name name thms 0
       
   181     in
       
   182 	thms'::(append_names names thmss)
       
   183     end;
       
   184 
       
   185 
       
   186 fun get_thms_ss [] = []
       
   187   | get_thms_ss thms =
       
   188     let val names = map Thm.name_of_thm thms 
       
   189         val thms' = map (mksimps mksimps_pairs) thms
       
   190         val thms'' = append_names names thms'
       
   191     in
       
   192 	ResLib.flat_noDup thms''
       
   193     end;
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 in
       
   199 
       
   200 
       
   201 (* convert locally declared rules to axiom clauses *)
       
   202 (* write axiom clauses to ax_file *)
       
   203 fun isar_local_thms (delta_cs, delta_ss_thms) =
       
   204     let val thms_cs = get_thms_cs delta_cs
       
   205 	val thms_ss = get_thms_ss delta_ss_thms
       
   206 	val thms_clauses = ResLib.flat_noDup (map ResAxioms.clausify_axiom (thms_cs @ thms_ss))
       
   207 	val clauses_strs = ResLib.flat_noDup (map ResClause.tptp_clause thms_clauses) (*string list*)
       
   208 	val ax_file = File.sysify_path axiom_file
       
   209 	val out = TextIO.openOut ax_file
       
   210     in
       
   211 	(ResLib.writeln_strs out clauses_strs; TextIO.closeOut out)
       
   212     end;
   121 
   213 
   122 
   214 
   123 
   215 
   124 
   216 
   125 
   217 
   126 (* called in Isar automatically *)
   218 (* called in Isar automatically *)
   127 fun isar_atp (thms, thm) =
   219 fun isar_atp (ctxt,thm) =
   128     let val prems = prems_of thm
   220     let val prems = ProofContext.prems_of ctxt
   129     in
   221       val d_cs = Classical.get_delta_claset ctxt
   130 	case prems of [] => () 
   222       val d_ss_thms = Simplifier.get_delta_simpset ctxt
   131 		    | _ => (isar_atp_aux thms thm)
   223     in
   132     end;
   224 	(isar_local_thms (d_cs,d_ss_thms); isar_atp' (prems, thm))
       
   225     end;
       
   226 
       
   227 end
       
   228 
       
   229 
       
   230 
   133 
   231 
   134 end;
   232 end;
   135 
   233 
   136 Proof.atp_hook := ResAtp.isar_atp;
   234 Proof.atp_hook := ResAtp.isar_atp;
   137 
   235