src/HOL/Tools/res_atp.ML
author paulson
Mon Mar 14 17:04:10 2005 +0100 (2005-03-14)
changeset 15608 f161fa6f8fd5
parent 15603 27a706e3a53d
child 15644 f2ef8c258fa4
permissions -rw-r--r--
bug fixes involving typechecking clauses
paulson@15608
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
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@15452
     8
(*Jia: changed: isar_atp now processes entire proof context.  fetch thms from delta simpset/claset*)
paulson@15452
     9
paulson@15347
    10
signature RES_ATP = 
paulson@15347
    11
sig
paulson@15347
    12
paulson@15608
    13
val trace_res : bool ref
paulson@15608
    14
val axiom_file : Path.T
paulson@15608
    15
val hyps_file : Path.T
paulson@15608
    16
val isar_atp : ProofContext.context * Thm.thm -> unit
paulson@15608
    17
val prob_file : Path.T
paulson@15608
    18
val atp_ax_tac : Thm.thm list -> int -> Tactical.tactic
paulson@15608
    19
val atp_tac : int -> Tactical.tactic
paulson@15608
    20
val debug: bool ref
paulson@15347
    21
paulson@15347
    22
end;
paulson@15347
    23
paulson@15347
    24
structure ResAtp : RES_ATP =
paulson@15608
    25
paulson@15347
    26
struct
paulson@15347
    27
paulson@15608
    28
(* used for debug *)
paulson@15608
    29
val debug = ref false;
paulson@15452
    30
paulson@15608
    31
fun debug_tac tac = (warning "testing";tac);
paulson@15608
    32
(* default value is false *)
paulson@15347
    33
paulson@15608
    34
val trace_res = ref false;
paulson@15347
    35
paulson@15608
    36
val skolem_tac = skolemize_tac;
paulson@15608
    37
paulson@15347
    38
paulson@15608
    39
val atomize_tac =
paulson@15608
    40
    SUBGOAL
paulson@15608
    41
     (fn (prop,_) =>
paulson@15608
    42
	 let val ts = Logic.strip_assums_hyp prop
paulson@15608
    43
	 in EVERY1 
paulson@15608
    44
		[METAHYPS
paulson@15608
    45
		     (fn hyps => (cut_facts_tac (map (ObjectLogic.atomize_thm o forall_intr_vars) hyps) 1)),
paulson@15608
    46
	  REPEAT_DETERM_N (length ts) o (etac thin_rl)]
paulson@15608
    47
     end);
paulson@15347
    48
paulson@15608
    49
(* temporarily use these files, which will be loaded by Vampire *)
paulson@15608
    50
val prob_file = File.tmp_path (Path.basic "prob");
paulson@15608
    51
val axiom_file = File.tmp_path (Path.basic "axioms");
paulson@15608
    52
val hyps_file = File.tmp_path (Path.basic "hyps");
paulson@15608
    53
val dummy_tac = PRIMITIVE(fn thm => thm );
paulson@15608
    54
paulson@15608
    55
paulson@15347
    56
paulson@15608
    57
fun tptp_inputs thms n = 
paulson@15608
    58
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@15608
    59
	val tptp_clss = ResLib.flat_noDup(map ResClause.tptp_clause clss) 
paulson@15608
    60
        val probfile = (File.sysify_path prob_file) ^ "_" ^ (string_of_int n)
paulson@15608
    61
	val out = TextIO.openOut(probfile)
paulson@15608
    62
    in
paulson@15608
    63
	(ResLib.writeln_strs out tptp_clss; TextIO.closeOut out; (if !trace_res then (warning probfile) else ()))
paulson@15608
    64
    end;
paulson@15347
    65
paulson@15347
    66
paulson@15347
    67
(**** for Isabelle/ML interface  ****)
paulson@15347
    68
paulson@15608
    69
fun call_atp_tac thms n = (tptp_inputs thms n; dummy_tac);
paulson@15608
    70
paulson@15608
    71
paulson@15608
    72
paulson@15452
    73
paulson@15608
    74
fun atp_tac n = SELECT_GOAL
paulson@15608
    75
  (EVERY1 [rtac ccontr,atomize_tac, skolemize_tac, 
paulson@15608
    76
  METAHYPS(fn negs => (call_atp_tac (make_clauses negs) n))]) n ;
paulson@15608
    77
paulson@15347
    78
paulson@15608
    79
fun atp_ax_tac axioms n = 
paulson@15608
    80
    let val axcls = ResLib.flat_noDup(map ResAxioms.clausify_axiom axioms)
paulson@15608
    81
	val axcls_str = ResClause.tptp_clauses2str (ResLib.flat_noDup(map ResClause.tptp_clause axcls))
paulson@15608
    82
	val axiomfile = File.sysify_path axiom_file
paulson@15608
    83
	val out = TextIO.openOut (axiomfile)
paulson@15608
    84
    in
paulson@15608
    85
	(TextIO.output(out,axcls_str);TextIO.closeOut out; if !trace_res then (warning axiomfile) else (); atp_tac n)
paulson@15608
    86
    end;
paulson@15347
    87
paulson@15608
    88
paulson@15608
    89
fun atp thm =
paulson@15608
    90
    let val prems = prems_of thm
paulson@15608
    91
    in
paulson@15608
    92
	case prems of [] => () 
paulson@15608
    93
		    | _ => (atp_tac 1 thm; ())
paulson@15608
    94
    end;
paulson@15347
    95
paulson@15347
    96
paulson@15347
    97
(**** For running in Isar ****)
paulson@15347
    98
paulson@15608
    99
(* same function as that in res_axioms.ML *)
paulson@15608
   100
fun repeat_RS thm1 thm2 =
paulson@15608
   101
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15608
   102
    in
paulson@15608
   103
	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15608
   104
    end;
paulson@15608
   105
paulson@15608
   106
(* a special version of repeat_RS *)
paulson@15608
   107
fun repeat_someI_ex thm = repeat_RS thm someI_ex;
paulson@15608
   108
paulson@15347
   109
paulson@15608
   110
(* convert clauses from "assume" to conjecture. write to file "hyps" *)
paulson@15608
   111
fun isar_atp_h thms =
paulson@15608
   112
    let val prems = map (skolemize o make_nnf o ObjectLogic.atomize_thm) thms
paulson@15608
   113
	val prems' = map repeat_someI_ex prems
paulson@15608
   114
	val prems'' = make_clauses prems'
paulson@15608
   115
	val prems''' = ResAxioms.rm_Eps [] prems''
paulson@15608
   116
	val clss = map ResClause.make_conjecture_clause prems'''
paulson@15608
   117
	val (tptp_clss,tfree_litss) = ResLib.unzip (map ResClause.clause2tptp clss) 
paulson@15608
   118
	val tfree_lits = ResLib.flat_noDup tfree_litss
paulson@15608
   119
	val tfree_clss = map ResClause.tfree_clause tfree_lits 
paulson@15608
   120
        val hypsfile = File.sysify_path hyps_file
paulson@15608
   121
	val out = TextIO.openOut(hypsfile)
paulson@15608
   122
    in
paulson@15608
   123
	((ResLib.writeln_strs out (tfree_clss @ tptp_clss); TextIO.closeOut out; if !trace_res then (warning hypsfile) else ());tfree_lits) 
paulson@15608
   124
    end;
paulson@15347
   125
paulson@15608
   126
fun tptp_inputs_tfrees thms n tfrees = 
paulson@15608
   127
    let val clss = map (ResClause.make_conjecture_clause_thm) thms
paulson@15608
   128
	val (tptp_clss,tfree_litss) = ResLib.unzip (map ResClause.clause2tptp clss)
paulson@15608
   129
	val tfree_clss = map ResClause.tfree_clause ((ResLib.flat_noDup tfree_litss) \\ tfrees) 
paulson@15608
   130
        val probfile = (File.sysify_path prob_file) ^ "_" ^ (string_of_int n)
paulson@15608
   131
	val out = TextIO.openOut(probfile)
paulson@15608
   132
    in
paulson@15608
   133
	(ResLib.writeln_strs out (tfree_clss @ tptp_clss); TextIO.closeOut out; (if !trace_res then (warning probfile) else ()))
paulson@15608
   134
    end;
paulson@15452
   135
paulson@15608
   136
paulson@15608
   137
fun call_atp_tac_tfrees thms n tfrees = (tptp_inputs_tfrees thms n tfrees; dummy_tac);
paulson@15608
   138
paulson@15608
   139
paulson@15608
   140
fun atp_tac_tfrees tfrees n  = SELECT_GOAL
paulson@15608
   141
  (EVERY1 [rtac ccontr,atomize_tac, skolemize_tac, 
paulson@15608
   142
  METAHYPS(fn negs => (call_atp_tac_tfrees (make_clauses negs) n tfrees))]) n;
paulson@15608
   143
paulson@15608
   144
paulson@15608
   145
fun isar_atp_g tfrees = atp_tac_tfrees tfrees;
paulson@15452
   146
paulson@15608
   147
fun isar_atp_goal' thm k n tfree_lits = 
paulson@15608
   148
    if (k > n) then () else (isar_atp_g tfree_lits k thm; isar_atp_goal' thm (k+1) n tfree_lits);
paulson@15608
   149
paulson@15452
   150
paulson@15608
   151
fun isar_atp_goal thm n_subgoals tfree_lits = (if (!debug) then warning (string_of_thm thm) else (isar_atp_goal' thm 1 n_subgoals tfree_lits));
webertj@15603
   152
paulson@15608
   153
paulson@15452
   154
paulson@15608
   155
fun isar_atp_aux thms thm n_subgoals = 
paulson@15608
   156
    let val tfree_lits = isar_atp_h thms 
paulson@15608
   157
    in
paulson@15608
   158
	isar_atp_goal thm n_subgoals tfree_lits
paulson@15608
   159
    end;
paulson@15608
   160
   
paulson@15452
   161
paulson@15608
   162
fun isar_atp' (thms, thm) =
paulson@15608
   163
    let val prems = prems_of thm
paulson@15608
   164
    in
paulson@15608
   165
	case prems of [] => (if (!debug) then warning "entering forward, no goal" else ())
paulson@15608
   166
		    | _ => (isar_atp_aux thms thm (length prems))
paulson@15608
   167
    end;
paulson@15608
   168
paulson@15452
   169
paulson@15608
   170
paulson@15608
   171
paulson@15608
   172
local
paulson@15608
   173
paulson@15608
   174
fun get_thms_cs claset =
paulson@15608
   175
    let val clsset = rep_cs claset
paulson@15608
   176
	val safeEs = #safeEs clsset
paulson@15608
   177
	val safeIs = #safeIs clsset
paulson@15608
   178
	val hazEs = #hazEs clsset
paulson@15608
   179
	val hazIs = #hazIs clsset
paulson@15608
   180
    in
paulson@15608
   181
	safeEs @ safeIs @ hazEs @ hazIs
paulson@15608
   182
    end;
paulson@15608
   183
paulson@15608
   184
paulson@15452
   185
paulson@15608
   186
fun append_name name [] _ = []
paulson@15608
   187
  | append_name name (thm::thms) k = (Thm.name_thm ((name ^ "_" ^ (string_of_int k)),thm)) :: (append_name name thms (k+1));
paulson@15608
   188
paulson@15608
   189
fun append_names (name::names) (thms::thmss) =
paulson@15608
   190
    let val thms' = append_name name thms 0
paulson@15608
   191
    in
paulson@15608
   192
	thms'::(append_names names thmss)
paulson@15608
   193
    end;
paulson@15608
   194
paulson@15452
   195
paulson@15608
   196
fun get_thms_ss [] = []
paulson@15608
   197
  | get_thms_ss thms =
paulson@15608
   198
    let val names = map Thm.name_of_thm thms 
paulson@15608
   199
        val thms' = map (mksimps mksimps_pairs) thms
paulson@15608
   200
        val thms'' = append_names names thms'
paulson@15608
   201
    in
paulson@15608
   202
	ResLib.flat_noDup thms''
paulson@15608
   203
    end;
paulson@15608
   204
paulson@15452
   205
paulson@15608
   206
paulson@15608
   207
paulson@15608
   208
in
paulson@15608
   209
paulson@15452
   210
paulson@15608
   211
(* convert locally declared rules to axiom clauses *)
paulson@15608
   212
(* write axiom clauses to ax_file *)
paulson@15608
   213
fun isar_local_thms (delta_cs, delta_ss_thms) =
paulson@15608
   214
    let val thms_cs = get_thms_cs delta_cs
paulson@15608
   215
	val thms_ss = get_thms_ss delta_ss_thms
paulson@15608
   216
	val thms_clauses = ResLib.flat_noDup (map ResAxioms.clausify_axiom (thms_cs @ thms_ss))
paulson@15608
   217
	val clauses_strs = ResLib.flat_noDup (map ResClause.tptp_clause thms_clauses) (*string list*)
paulson@15608
   218
	val ax_file = File.sysify_path axiom_file
paulson@15608
   219
	val out = TextIO.openOut ax_file
paulson@15608
   220
    in
paulson@15608
   221
	(ResLib.writeln_strs out clauses_strs; TextIO.closeOut out)
paulson@15608
   222
    end;
paulson@15347
   223
paulson@15608
   224
paulson@15608
   225
paulson@15608
   226
paulson@15347
   227
paulson@15608
   228
(* called in Isar automatically *)
paulson@15608
   229
fun isar_atp (ctxt,thm) =
paulson@15608
   230
    let val prems = ProofContext.prems_of ctxt
paulson@15608
   231
      val d_cs = Classical.get_delta_claset ctxt
paulson@15608
   232
      val d_ss_thms = Simplifier.get_delta_simpset ctxt
paulson@15608
   233
    in
paulson@15608
   234
	(isar_local_thms (d_cs,d_ss_thms); isar_atp' (prems, thm))
paulson@15608
   235
    end;
paulson@15347
   236
paulson@15608
   237
end
paulson@15608
   238
paulson@15608
   239
paulson@15608
   240
paulson@15452
   241
paulson@15347
   242
end;
paulson@15347
   243
paulson@15347
   244
Proof.atp_hook := ResAtp.isar_atp;
paulson@15608
   245
paulson@15608
   246