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