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