src/HOL/Tools/res_axioms.ML
author paulson
Thu May 12 18:24:42 2005 +0200 (2005-05-12)
changeset 15956 0da64b5a9a00
parent 15955 87cf2ce8ede8
child 15997 c71031d7988c
permissions -rw-r--r--
theorem names for caching
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     2
    ID: $Id$
paulson@15347
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
Transformation of axiom rules (elim/intro/etc) into CNF forms.    
paulson@15347
     6
*)
paulson@15347
     7
paulson@15347
     8
paulson@15347
     9
paulson@15347
    10
signature RES_ELIM_RULE =
paulson@15347
    11
sig
paulson@15347
    12
paulson@15347
    13
exception ELIMR2FOL of string
paulson@15956
    14
val elimRule_tac : thm -> Tactical.tactic
paulson@15956
    15
val elimR2Fol : thm -> Term.term
paulson@15956
    16
val transform_elim : thm -> thm
paulson@15347
    17
paulson@15347
    18
end;
paulson@15347
    19
paulson@15347
    20
structure ResElimRule: RES_ELIM_RULE =
paulson@15347
    21
paulson@15347
    22
struct
paulson@15347
    23
paulson@15390
    24
(* a tactic used to prove an elim-rule. *)
paulson@15347
    25
fun elimRule_tac thm =
paulson@15347
    26
    ((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN
paulson@15371
    27
    REPEAT(Fast_tac 1);
paulson@15347
    28
paulson@15347
    29
paulson@15347
    30
(* This following version fails sometimes, need to investigate, do not use it now. *)
paulson@15347
    31
fun elimRule_tac' thm =
paulson@15347
    32
   ((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN
paulson@15347
    33
   REPEAT(SOLVE((etac exI 1) ORELSE (rtac conjI 1) ORELSE (rtac disjI1 1) ORELSE (rtac disjI2 1))); 
paulson@15347
    34
paulson@15347
    35
paulson@15347
    36
exception ELIMR2FOL of string;
paulson@15347
    37
paulson@15390
    38
(* functions used to construct a formula *)
paulson@15390
    39
paulson@15347
    40
fun make_disjs [x] = x
paulson@15956
    41
  | make_disjs (x :: xs) = HOLogic.mk_disj(x, make_disjs xs)
paulson@15347
    42
paulson@15347
    43
fun make_conjs [x] = x
paulson@15956
    44
  | make_conjs (x :: xs) =  HOLogic.mk_conj(x, make_conjs xs)
paulson@15956
    45
paulson@15956
    46
fun add_EX tm [] = tm
paulson@15956
    47
  | add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs;
paulson@15347
    48
paulson@15347
    49
paulson@15347
    50
paulson@15956
    51
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) (Const("Trueprop",_) $ Free(q,_)) = (p = q)
paulson@15371
    52
  | is_neg _ _ = false;
paulson@15371
    53
paulson@15347
    54
paulson@15347
    55
exception STRIP_CONCL;
paulson@15347
    56
paulson@15347
    57
paulson@15371
    58
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) =
paulson@15956
    59
      let val P' = HOLogic.dest_Trueprop P
paulson@15956
    60
  	  val prems' = P'::prems
paulson@15956
    61
      in
paulson@15371
    62
	strip_concl' prems' bvs  Q
paulson@15956
    63
      end
paulson@15371
    64
  | strip_concl' prems bvs P = 
paulson@15956
    65
      let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P)
paulson@15956
    66
      in
paulson@15371
    67
	add_EX (make_conjs (P'::prems)) bvs
paulson@15956
    68
      end;
paulson@15371
    69
paulson@15371
    70
paulson@15371
    71
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body))  = strip_concl prems ((x,xtp)::bvs) concl body
paulson@15371
    72
  | strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) =
paulson@15371
    73
    if (is_neg P concl) then (strip_concl' prems bvs Q)
paulson@15371
    74
    else
paulson@15956
    75
	(let val P' = HOLogic.dest_Trueprop P
paulson@15371
    76
	     val prems' = P'::prems
paulson@15371
    77
	 in
paulson@15371
    78
	     strip_concl prems' bvs  concl Q
paulson@15371
    79
	 end)
paulson@15371
    80
  | strip_concl prems bvs concl _ = add_EX (make_conjs prems) bvs;
paulson@15347
    81
 
paulson@15347
    82
paulson@15347
    83
paulson@15371
    84
fun trans_elim (main,others,concl) =
paulson@15371
    85
    let val others' = map (strip_concl [] [] concl) others
paulson@15347
    86
	val disjs = make_disjs others'
paulson@15347
    87
    in
paulson@15956
    88
	HOLogic.mk_imp (HOLogic.dest_Trueprop main, disjs)
paulson@15347
    89
    end;
paulson@15347
    90
paulson@15347
    91
paulson@15390
    92
(* aux function of elim2Fol, take away predicate variable. *)
paulson@15371
    93
fun elimR2Fol_aux prems concl = 
paulson@15347
    94
    let val nprems = length prems
paulson@15347
    95
	val main = hd prems
paulson@15347
    96
    in
paulson@15956
    97
	if (nprems = 1) then HOLogic.Not $ (HOLogic.dest_Trueprop main)
paulson@15371
    98
        else trans_elim (main, tl prems, concl)
paulson@15347
    99
    end;
paulson@15347
   100
paulson@15956
   101
    
paulson@15390
   102
(* convert an elim rule into an equivalent formula, of type Term.term. *)
paulson@15347
   103
fun elimR2Fol elimR = 
paulson@15347
   104
    let val elimR' = Drule.freeze_all elimR
paulson@15347
   105
	val (prems,concl) = (prems_of elimR', concl_of elimR')
paulson@15347
   106
    in
paulson@15347
   107
	case concl of Const("Trueprop",_) $ Free(_,Type("bool",[])) 
paulson@15956
   108
		      => HOLogic.mk_Trueprop (elimR2Fol_aux prems concl)
paulson@15956
   109
                    | Free(x,Type("prop",[])) => HOLogic.mk_Trueprop(elimR2Fol_aux prems concl) 
paulson@15347
   110
		    | _ => raise ELIMR2FOL("Not an elimination rule!")
paulson@15347
   111
    end;
paulson@15347
   112
paulson@15347
   113
paulson@15347
   114
paulson@15347
   115
(**** use prove_goalw_cterm to prove ****)
paulson@15347
   116
paulson@15390
   117
(* convert an elim-rule into an equivalent theorem that does not have the predicate variable. *) 
paulson@15347
   118
fun transform_elim thm =
paulson@15347
   119
    let val tm = elimR2Fol thm
paulson@15347
   120
	val ctm = cterm_of (sign_of_thm thm) tm	
paulson@15347
   121
    in
paulson@15347
   122
	prove_goalw_cterm [] ctm (fn prems => [elimRule_tac thm])
paulson@15347
   123
    end;	
paulson@15347
   124
paulson@15347
   125
paulson@15347
   126
end;
paulson@15347
   127
paulson@15347
   128
paulson@15347
   129
paulson@15347
   130
signature RES_AXIOMS =
paulson@15347
   131
sig
paulson@15347
   132
paulson@15956
   133
val clausify_axiom : thm -> ResClause.clause list
paulson@15956
   134
val cnf_axiom : (string * thm) -> thm list
paulson@15956
   135
val meta_cnf_axiom : thm -> thm list
paulson@15956
   136
val cnf_elim : thm -> thm list
paulson@15956
   137
val cnf_rule : thm -> thm list
paulson@15956
   138
val cnf_classical_rules_thy : theory -> thm list list * thm list
paulson@15956
   139
val clausify_classical_rules_thy : theory -> ResClause.clause list list * thm list
paulson@15956
   140
val cnf_simpset_rules_thy : theory -> thm list list * thm list
paulson@15956
   141
val clausify_simpset_rules_thy : theory -> ResClause.clause list list * thm list
paulson@15347
   142
val rm_Eps 
paulson@15956
   143
: (Term.term * Term.term) list -> thm list -> Term.term list
paulson@15956
   144
val claset_rules_of_thy : theory -> (string * thm) list
paulson@15956
   145
val simpset_rules_of_thy : theory -> (string * thm) list
paulson@15956
   146
val clausify_rules : thm list -> thm list -> ResClause.clause list list * thm list
paulson@15684
   147
paulson@15347
   148
end;
paulson@15347
   149
paulson@15347
   150
structure ResAxioms : RES_AXIOMS =
paulson@15347
   151
 
paulson@15347
   152
struct
paulson@15347
   153
paulson@15347
   154
open ResElimRule;
paulson@15347
   155
paulson@15347
   156
(* to be fixed: cnf_intro, cnf_rule, is_introR *)
paulson@15347
   157
paulson@15390
   158
(* check if a rule is an elim rule *)
paulson@15347
   159
fun is_elimR thm = 
paulson@15347
   160
    case (concl_of thm) of (Const ("Trueprop", _) $ Var (idx,_)) => true
paulson@15347
   161
			 | Var(indx,Type("prop",[])) => true
paulson@15347
   162
			 | _ => false;
paulson@15347
   163
paulson@15347
   164
paulson@15390
   165
(* repeated resolution *)
paulson@15347
   166
fun repeat_RS thm1 thm2 =
paulson@15347
   167
    let val thm1' =  thm1 RS thm2 handle THM _ => thm1
paulson@15347
   168
    in
paulson@15347
   169
	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
paulson@15347
   170
    end;
paulson@15347
   171
paulson@15347
   172
paulson@15390
   173
(* convert a theorem into NNF and also skolemize it. *)
paulson@15347
   174
fun skolem_axiom thm = 
paulson@15872
   175
  if Term.is_first_order (prop_of thm) then
paulson@15872
   176
    let val thm' = (skolemize o make_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) thm
paulson@15347
   177
    in 
paulson@15347
   178
	repeat_RS thm' someI_ex
paulson@15872
   179
    end
paulson@15872
   180
  else raise THM ("skolem_axiom: not first-order", 0, [thm]);
paulson@15347
   181
paulson@15347
   182
paulson@15872
   183
fun cnf_rule thm = make_clauses [skolem_axiom thm]
paulson@15347
   184
paulson@15872
   185
fun cnf_elim thm = cnf_rule (transform_elim thm);
paulson@15347
   186
paulson@15347
   187
paulson@15370
   188
(*Transfer a theorem in to theory Reconstruction.thy if it is not already
paulson@15359
   189
  inside that theory -- because it's needed for Skolemization *)
paulson@15359
   190
paulson@15370
   191
val recon_thy = ThyInfo.get_theory"Reconstruction";
paulson@15359
   192
paulson@15370
   193
fun transfer_to_Reconstruction thm =
paulson@15370
   194
    transfer recon_thy thm handle THM _ => thm;
paulson@15347
   195
paulson@15955
   196
fun is_taut th =
paulson@15955
   197
      case (prop_of th) of
paulson@15955
   198
           (Const ("Trueprop", _) $ Const ("True", _)) => true
paulson@15955
   199
         | _ => false;
paulson@15955
   200
paulson@15955
   201
(* remove tautologous clauses *)
paulson@15955
   202
val rm_redundant_cls = List.filter (not o is_taut);
paulson@15347
   203
paulson@15347
   204
(* transform an Isabelle thm into CNF *)
paulson@15955
   205
fun cnf_axiom_aux thm =
paulson@15370
   206
    let val thm' = transfer_to_Reconstruction thm
paulson@15499
   207
	val thm'' = if (is_elimR thm') then (cnf_elim thm')  else cnf_rule thm'
paulson@15347
   208
    in
paulson@15955
   209
	map (zero_var_indexes o Thm.varifyT) (rm_redundant_cls thm'')
paulson@15347
   210
    end;
paulson@15955
   211
    
paulson@15955
   212
(*Cache for clauses: could be a hash table if we provided them.*)
paulson@15955
   213
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
paulson@15955
   214
paulson@15956
   215
fun cnf_axiom (name,th) =
paulson@15956
   216
    case name of
paulson@15955
   217
	  "" => cnf_axiom_aux th (*no name, so can't cache*)
paulson@15955
   218
	| s  => case Symtab.lookup (!clause_cache,s) of
paulson@15955
   219
	  	  NONE => 
paulson@15955
   220
		    let val cls = cnf_axiom_aux th
paulson@15955
   221
		    in  clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls
paulson@15955
   222
		    end
paulson@15955
   223
	        | SOME(th',cls) =>
paulson@15955
   224
		    if eq_thm(th,th') then cls
paulson@15955
   225
		    else (*New theorem stored under the same name? Possible??*)
paulson@15955
   226
		      let val cls = cnf_axiom_aux th
paulson@15955
   227
		      in  clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls
paulson@15955
   228
		      end;
paulson@15347
   229
paulson@15956
   230
fun pairname th = (Thm.name_of_thm th, th);
paulson@15956
   231
paulson@15956
   232
fun meta_cnf_axiom th = 
paulson@15956
   233
    map Meson.make_meta_clause (cnf_axiom (pairname th));
paulson@15499
   234
paulson@15347
   235
paulson@15347
   236
(* changed: with one extra case added *)
paulson@15956
   237
fun univ_vars_of_aux (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,body)) vars =    
paulson@15956
   238
      univ_vars_of_aux body vars
paulson@15956
   239
  | univ_vars_of_aux (Const ("Ex",_) $ Abs(_,_,body)) vars = 
paulson@15956
   240
      univ_vars_of_aux body vars (* EX x. body *)
paulson@15347
   241
  | univ_vars_of_aux (P $ Q) vars =
paulson@15956
   242
      univ_vars_of_aux Q (univ_vars_of_aux P vars)
paulson@15347
   243
  | univ_vars_of_aux (t as Var(_,_)) vars = 
paulson@15956
   244
      if (t mem vars) then vars else (t::vars)
paulson@15347
   245
  | univ_vars_of_aux _ vars = vars;
paulson@15347
   246
  
paulson@15347
   247
fun univ_vars_of t = univ_vars_of_aux t [];
paulson@15347
   248
paulson@15347
   249
paulson@15347
   250
fun get_new_skolem epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,tp,_)))  = 
paulson@15347
   251
    let val all_vars = univ_vars_of t
paulson@15347
   252
	val sk_term = ResSkolemFunction.gen_skolem all_vars tp
paulson@15347
   253
    in
paulson@15347
   254
	(sk_term,(t,sk_term)::epss)
paulson@15347
   255
    end;
paulson@15347
   256
paulson@15347
   257
skalberg@15531
   258
fun sk_lookup [] t = NONE
skalberg@15531
   259
  | sk_lookup ((tm,sk_tm)::tms) t = if (t = tm) then SOME (sk_tm) else (sk_lookup tms t);
paulson@15347
   260
paulson@15347
   261
paulson@15390
   262
paulson@15390
   263
(* get the proper skolem term to replace epsilon term *)
paulson@15347
   264
fun get_skolem epss t = 
paulson@15956
   265
    case (sk_lookup epss t) of NONE => get_new_skolem epss t
paulson@15956
   266
		             | SOME sk => (sk,epss);
paulson@15347
   267
paulson@15347
   268
paulson@15347
   269
fun rm_Eps_cls_aux epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,_))) = get_skolem epss t
paulson@15347
   270
  | rm_Eps_cls_aux epss (P $ Q) =
paulson@15347
   271
    let val (P',epss') = rm_Eps_cls_aux epss P
paulson@15347
   272
	val (Q',epss'') = rm_Eps_cls_aux epss' Q
paulson@15347
   273
    in
paulson@15347
   274
	(P' $ Q',epss'')
paulson@15347
   275
    end
paulson@15347
   276
  | rm_Eps_cls_aux epss t = (t,epss);
paulson@15347
   277
paulson@15347
   278
paulson@15956
   279
fun rm_Eps_cls epss thm = rm_Eps_cls_aux epss (prop_of thm);
paulson@15347
   280
paulson@15347
   281
paulson@15390
   282
(* remove the epsilon terms in a formula, by skolem terms. *)
paulson@15347
   283
fun rm_Eps _ [] = []
paulson@15347
   284
  | rm_Eps epss (thm::thms) = 
paulson@15956
   285
      let val (thm',epss') = rm_Eps_cls epss thm
paulson@15956
   286
      in
paulson@15347
   287
	thm' :: (rm_Eps epss' thms)
paulson@15956
   288
      end;
paulson@15347
   289
paulson@15347
   290
paulson@15390
   291
(* convert a theorem into CNF and then into Clause.clause format. *)
paulson@15347
   292
fun clausify_axiom thm =
paulson@15956
   293
    let val name = Thm.name_of_thm thm
paulson@15956
   294
	val isa_clauses = cnf_axiom (name, thm)
paulson@15956
   295
	      (*"isa_clauses" are already "standard"ed. *)
paulson@15347
   296
        val isa_clauses' = rm_Eps [] isa_clauses
paulson@15956
   297
        val clauses_n = length isa_clauses
paulson@15347
   298
	fun make_axiom_clauses _ [] = []
paulson@15956
   299
	  | make_axiom_clauses i (cls::clss) = (ResClause.make_axiom_clause cls (name,i)) :: make_axiom_clauses (i+1) clss 
paulson@15347
   300
    in
paulson@15872
   301
	make_axiom_clauses 0 isa_clauses'		
paulson@15347
   302
    end;
paulson@15347
   303
  
paulson@15347
   304
paulson@15872
   305
(**** Extract and Clausify theorems from a theory's claset and simpset ****)
paulson@15347
   306
paulson@15347
   307
fun claset_rules_of_thy thy =
paulson@15347
   308
    let val clsset = rep_cs (claset_of thy)
paulson@15347
   309
	val safeEs = #safeEs clsset
paulson@15347
   310
	val safeIs = #safeIs clsset
paulson@15347
   311
	val hazEs = #hazEs clsset
paulson@15347
   312
	val hazIs = #hazIs clsset
paulson@15347
   313
    in
paulson@15956
   314
	map pairname (safeEs @ safeIs @ hazEs @ hazIs)
paulson@15347
   315
    end;
paulson@15347
   316
paulson@15347
   317
fun simpset_rules_of_thy thy =
paulson@15872
   318
    let val rules = #rules(fst (rep_ss (simpset_of thy)))
paulson@15347
   319
    in
paulson@15872
   320
	map (fn (_,r) => (#name r, #thm r)) (Net.dest rules)
paulson@15347
   321
    end;
paulson@15347
   322
paulson@15347
   323
paulson@15872
   324
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm")  ****)
paulson@15347
   325
paulson@15347
   326
(* classical rules *)
paulson@15872
   327
fun cnf_rules [] err_list = ([],err_list)
paulson@15956
   328
  | cnf_rules ((name,thm) :: thms) err_list = 
paulson@15872
   329
      let val (ts,es) = cnf_rules thms err_list
paulson@15956
   330
      in  (cnf_axiom (name,thm) :: ts,es) handle  _ => (ts, (thm::es))  end;
paulson@15347
   331
paulson@15347
   332
(* CNF all rules from a given theory's classical reasoner *)
paulson@15347
   333
fun cnf_classical_rules_thy thy = 
paulson@15872
   334
    cnf_rules (claset_rules_of_thy thy) [];
paulson@15347
   335
paulson@15347
   336
(* CNF all simplifier rules from a given theory's simpset *)
paulson@15347
   337
fun cnf_simpset_rules_thy thy =
paulson@15956
   338
    cnf_rules (simpset_rules_of_thy thy) [];
paulson@15347
   339
paulson@15347
   340
paulson@15872
   341
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****)
paulson@15347
   342
paulson@15347
   343
(* classical rules *)
paulson@15872
   344
fun clausify_rules [] err_list = ([],err_list)
paulson@15872
   345
  | clausify_rules (thm::thms) err_list =
paulson@15872
   346
    let val (ts,es) = clausify_rules thms err_list
paulson@15347
   347
    in
paulson@15347
   348
	((clausify_axiom thm)::ts,es) handle  _ => (ts,(thm::es))
paulson@15347
   349
    end;
paulson@15347
   350
paulson@15390
   351
paulson@15736
   352
(* convert all classical rules from a given theory into Clause.clause format. *)
paulson@15347
   353
fun clausify_classical_rules_thy thy =
paulson@15956
   354
    clausify_rules (map #2 (claset_rules_of_thy thy)) [];
paulson@15347
   355
paulson@15736
   356
(* convert all simplifier rules from a given theory into Clause.clause format. *)
paulson@15347
   357
fun clausify_simpset_rules_thy thy =
paulson@15872
   358
    clausify_rules (map #2 (simpset_rules_of_thy thy)) [];
paulson@15347
   359
paulson@15347
   360
paulson@15347
   361
end;