src/HOL/Tools/res_axioms.ML
changeset 18198 95330fc0ea8d
parent 18144 4edcb5fdc3b0
child 18274 bbca1d2da0e9
equal deleted inserted replaced
18197:082a2bd6f655 18198:95330fc0ea8d
    13   val elimR2Fol : thm -> term
    13   val elimR2Fol : thm -> term
    14   val transform_elim : thm -> thm
    14   val transform_elim : thm -> thm
    15   val clausify_axiom_pairs : (string*thm) -> (ResClause.clause*thm) list
    15   val clausify_axiom_pairs : (string*thm) -> (ResClause.clause*thm) list
    16   val clausify_axiom_pairsH : (string*thm) -> (ResHolClause.clause*thm) list
    16   val clausify_axiom_pairsH : (string*thm) -> (ResHolClause.clause*thm) list
    17   val cnf_axiom : (string * thm) -> thm list
    17   val cnf_axiom : (string * thm) -> thm list
    18   val cnf_axiomH : (string * thm) -> thm list
       
    19   val meta_cnf_axiom : thm -> thm list
    18   val meta_cnf_axiom : thm -> thm list
    20   val meta_cnf_axiomH : thm -> thm list
       
    21   val claset_rules_of_thy : theory -> (string * thm) list
    19   val claset_rules_of_thy : theory -> (string * thm) list
    22   val simpset_rules_of_thy : theory -> (string * thm) list
    20   val simpset_rules_of_thy : theory -> (string * thm) list
    23   val claset_rules_of_ctxt: Proof.context -> (string * thm) list
    21   val claset_rules_of_ctxt: Proof.context -> (string * thm) list
    24   val simpset_rules_of_ctxt : Proof.context -> (string * thm) list
    22   val simpset_rules_of_ctxt : Proof.context -> (string * thm) list
    25   val clausify_rules_pairs : (string*thm) list -> (ResClause.clause*thm) list list
    23   val clausify_rules_pairs : (string*thm) list -> (ResClause.clause*thm) list list
    26   val clausify_rules_pairsH : (string*thm) list -> (ResHolClause.clause*thm) list list
    24   val clausify_rules_pairsH : (string*thm) list -> (ResHolClause.clause*thm) list list
    27   val clause_setup : (theory -> theory) list
    25   val clause_setup : (theory -> theory) list
    28   val meson_method_setup : (theory -> theory) list
    26   val meson_method_setup : (theory -> theory) list
    29   val pairname : thm -> (string * thm)
    27   val pairname : thm -> (string * thm)
    30   val repeat_RS : thm -> thm -> thm
    28   val repeat_RS : thm -> thm -> thm
       
    29   val cnf_axiom_aux : thm -> thm list
    31 
    30 
    32   end;
    31   end;
    33 
    32 
    34 structure ResAxioms (*: RES_AXIOMS*) =
    33 structure ResAxioms : RES_AXIOMS =
    35  
    34  
    36 struct
    35 struct
    37 
    36 
    38 
    37 
    39 val tagging_enabled = false (*compile_time option*)
    38 val tagging_enabled = false (*compile_time option*)
   140 fun repeat_RS thm1 thm2 =
   139 fun repeat_RS thm1 thm2 =
   141     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
   140     let val thm1' =  thm1 RS thm2 handle THM _ => thm1
   142     in
   141     in
   143 	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
   142 	if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2)
   144     end;
   143     end;
   145 
       
   146 
       
   147 (*Convert a theorem into NNF and also skolemize it. Original version, using
       
   148   Hilbert's epsilon in the resulting clauses.   FIXME DELETE*)
       
   149 fun skolem_axiom_g mk_nnf th = 
       
   150   let val th' = (skolemize o mk_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) th
       
   151   in  repeat_RS th' someI_ex
       
   152   end;
       
   153 
   144 
   154 
   145 
   155 (*Transfer a theorem into theory Reconstruction.thy if it is not already
   146 (*Transfer a theorem into theory Reconstruction.thy if it is not already
   156   inside that theory -- because it's needed for Skolemization *)
   147   inside that theory -- because it's needed for Skolemization *)
   157 
   148 
   264        |> forall_intr_list frees
   255        |> forall_intr_list frees
   265        |> forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   256        |> forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   266        |> Thm.varifyT
   257        |> Thm.varifyT
   267   end;
   258   end;
   268 
   259 
   269 (*Converts an Isabelle theorem (intro, elim or simp format) into nnf.
   260 (*Converts an Isabelle theorem (intro, elim or simp format) into nnf.*)
   270   FIXME: generalize so that it works for HOL too!!*)
   261 (*It now works for HOL too. *)
   271 fun to_nnf th = 
   262 fun to_nnf th = 
   272     th |> transfer_to_Reconstruction
   263     th |> transfer_to_Reconstruction
   273        |> transform_elim |> Drule.freeze_all
   264        |> transform_elim |> Drule.freeze_all
   274        |> ObjectLogic.atomize_thm |> make_nnf;
   265        |> ObjectLogic.atomize_thm |> make_nnf;
   275 
   266 
       
   267 
       
   268 
       
   269 
   276 (*The cache prevents repeated clausification of a theorem, 
   270 (*The cache prevents repeated clausification of a theorem, 
   277   and also repeated declaration of Skolem functions*)  (* FIXME better use Termtab!? *)
   271   and also repeated declaration of Skolem functions*)  (* FIXME better use Termtab!? *)
   278 val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
   272 val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table)
   279 
   273 
   280 
   274 
   281 (*Generate Skolem functions for a theorem supplied in nnf*)
   275 (*Generate Skolem functions for a theorem supplied in nnf*)
   282 fun skolem_of_nnf th =
   276 fun skolem_of_nnf th =
   283   map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns th);
   277   map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns th);
   284 
   278 
   285 (*Skolemize a named theorem, returning a modified theory. NONE can occur if the
   279 (*Skolemize a named theorem, returning a modified theory.*)
   286   theorem is not first-order.*)
   280 (*also works for HOL*) 
   287 fun skolem_thm th = 
   281 fun skolem_thm th = 
   288   Option.map (fn nnfth => Meson.make_cnf (skolem_of_nnf nnfth) nnfth)
   282   Option.map (fn nnfth => Meson.make_cnf (skolem_of_nnf nnfth) nnfth)
   289 	 (SOME (to_nnf th)  handle THM _ => NONE);
   283 	 (SOME (to_nnf th)  handle THM _ => NONE);
   290 
   284 
       
   285 
   291 (*Declare Skolem functions for a theorem, supplied in nnf and with its name*)
   286 (*Declare Skolem functions for a theorem, supplied in nnf and with its name*)
       
   287 (*in case skolemization fails, the input theory is not changed*)
   292 fun skolem thy (name,th) =
   288 fun skolem thy (name,th) =
   293   let val cname = (case name of "" => gensym "sko" | s => Sign.base_name s)
   289   let val cname = (case name of "" => gensym "sko" | s => Sign.base_name s)
   294   in Option.map 
   290   in Option.map 
   295         (fn nnfth => 
   291         (fn nnfth => 
   296           let val (thy',defs) = declare_skofuns cname nnfth thy
   292           let val (thy',defs) = declare_skofuns cname nnfth thy
   297               val skoths = map skolem_of_def defs
   293               val skoths = map skolem_of_def defs
   298           in (thy', Meson.make_cnf skoths nnfth) end)
   294           in (thy', Meson.make_cnf skoths nnfth) end)
   299       (SOME (to_nnf th)  handle THM _ => NONE)
   295       (SOME (to_nnf th)  handle THM _ => NONE) 
   300   end;
   296   end;
   301 
   297 
   302 (*Populate the clause cache using the supplied theorems*)
   298 (*Populate the clause cache using the supplied theorems*)
   303 fun skolem_cache ((name,th), thy) =
   299 fun skolem_cache ((name,th), thy) =
   304   case Symtab.lookup (!clause_cache) name of
   300   case Symtab.lookup (!clause_cache) name of
   330 		        warning (string_of_thm th');
   326 		        warning (string_of_thm th');
   331 		        cls);
   327 		        cls);
   332 
   328 
   333 fun pairname th = (Thm.name_of_thm th, th);
   329 fun pairname th = (Thm.name_of_thm th, th);
   334 
   330 
   335 val skolem_axiomH = skolem_axiom_g make_nnf1;
   331 
   336 
   332 (*no first-order check, so works for HOL too.*)
   337 fun cnf_ruleH th = make_clauses [skolem_axiomH (transform_elim th)];
       
   338 
       
   339 (* transform an Isabelle theorem into CNF *)
       
   340 fun cnf_axiom_aux_g cnf_rule th =
       
   341     map zero_var_indexes
       
   342         (rm_redundant_cls (cnf_rule (transfer_to_Reconstruction th)));
       
   343 
       
   344 val cnf_axiom_auxH = cnf_axiom_aux_g cnf_ruleH;
       
   345 
       
   346 (*NONE can occur if th fails the first-order check.*)
       
   347 fun cnf_axiom_aux th = Option.getOpt (skolem_thm th, []);
   333 fun cnf_axiom_aux th = Option.getOpt (skolem_thm th, []);
   348 
   334 
       
   335 
       
   336 
   349 val cnf_axiom = cnf_axiom_g cnf_axiom_aux;
   337 val cnf_axiom = cnf_axiom_g cnf_axiom_aux;
   350 
       
   351 val cnf_axiomH = cnf_axiom_g cnf_axiom_auxH;
       
   352 
   338 
   353 
   339 
   354 fun meta_cnf_axiom th = 
   340 fun meta_cnf_axiom th = 
   355     map Meson.make_meta_clause (cnf_axiom (pairname th));
   341     map Meson.make_meta_clause (cnf_axiom (pairname th));
   356 
       
   357 fun meta_cnf_axiomH th = 
       
   358     map Meson.make_meta_clause (cnf_axiomH (pairname th));
       
   359 
   342 
   360 
   343 
   361 
   344 
   362 (**** Extract and Clausify theorems from a theory's claset and simpset ****)
   345 (**** Extract and Clausify theorems from a theory's claset and simpset ****)
   363 
   346 
   406   | cnf_rules_g cnf_axiom ((name,th) :: ths) err_list = 
   389   | cnf_rules_g cnf_axiom ((name,th) :: ths) err_list = 
   407       let val (ts,es) = cnf_rules_g cnf_axiom ths err_list
   390       let val (ts,es) = cnf_rules_g cnf_axiom ths err_list
   408       in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;  
   391       in  (cnf_axiom (name,th) :: ts,es) handle  _ => (ts, (th::es))  end;  
   409 
   392 
   410 
   393 
       
   394 (*works for both FOL and HOL*)
   411 val cnf_rules = cnf_rules_g cnf_axiom;
   395 val cnf_rules = cnf_rules_g cnf_axiom;
   412 val cnf_rulesH = cnf_rules_g cnf_axiomH;
   396 
   413 
   397 
   414 
   398 (**** Convert all theorems of a claset/simpset into clauses (ResClause.clause, or ResHolClause.clause) ****)
   415 (**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****)
       
   416 
   399 
   417 fun make_axiom_clauses _ _ [] = []
   400 fun make_axiom_clauses _ _ [] = []
   418   | make_axiom_clauses name i (cls::clss) =
   401   | make_axiom_clauses name i (cls::clss) =
   419       (ResClause.make_axiom_clause (prop_of cls) (name,i), cls) ::
   402       (ResClause.make_axiom_clause (prop_of cls) (name,i), cls) ::
   420       (make_axiom_clauses name (i+1) clss)
   403       (make_axiom_clauses name (i+1) clss)
   429       (ResHolClause.make_axiom_clause (prop_of cls) (name,i), cls) ::
   412       (ResHolClause.make_axiom_clause (prop_of cls) (name,i), cls) ::
   430       (make_axiom_clausesH name (i+1) clss)
   413       (make_axiom_clausesH name (i+1) clss)
   431 
   414 
   432 fun clausify_axiom_pairsH (name,th) = 
   415 fun clausify_axiom_pairsH (name,th) = 
   433     filter (fn (c,th) => not (ResHolClause.isTaut c))
   416     filter (fn (c,th) => not (ResHolClause.isTaut c))
   434            (make_axiom_clausesH name 0 (cnf_axiomH (name,th)));
   417            (make_axiom_clausesH name 0 (cnf_axiom (name,th)));
   435 
   418 
   436 
   419 
   437 fun clausify_rules_pairs_aux result [] = result
   420 fun clausify_rules_pairs_aux result [] = result
   438   | clausify_rules_pairs_aux result ((name,th)::ths) =
   421   | clausify_rules_pairs_aux result ((name,th)::ths) =
   439       clausify_rules_pairs_aux (clausify_axiom_pairs (name,th) :: result) ths
   422       clausify_rules_pairs_aux (clausify_axiom_pairs (name,th) :: result) ths