src/HOL/Tools/Meson/meson_clausify.ML
changeset 42335 cb8aa792d138
parent 42333 23bb0784b5d0
child 42336 d63d43e85879
equal deleted inserted replaced
42334:8e58cc1390c7 42335:cb8aa792d138
    11   val new_nonskolem_var_prefix : string
    11   val new_nonskolem_var_prefix : string
    12   val is_zapped_var_name : string -> bool
    12   val is_zapped_var_name : string -> bool
    13   val extensionalize_theorem : thm -> thm
    13   val extensionalize_theorem : thm -> thm
    14   val introduce_combinators_in_cterm : cterm -> thm
    14   val introduce_combinators_in_cterm : cterm -> thm
    15   val introduce_combinators_in_theorem : thm -> thm
    15   val introduce_combinators_in_theorem : thm -> thm
    16   val to_definitional_cnf_with_quantifiers : theory -> thm -> thm
    16   val to_definitional_cnf_with_quantifiers : Proof.context -> thm -> thm
    17   val cluster_of_zapped_var_name : string -> (int * (int * int)) * bool
    17   val cluster_of_zapped_var_name : string -> (int * (int * int)) * bool
    18   val cnf_axiom :
    18   val cnf_axiom :
    19     Proof.context -> bool -> int -> thm -> (thm * term) option * thm list
    19     Proof.context -> bool -> int -> thm -> (thm * term) option * thm list
    20 end;
    20 end;
    21 
    21 
   227     |> forall_intr_list frees
   227     |> forall_intr_list frees
   228     |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   228     |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
   229     |> Thm.varifyT_global
   229     |> Thm.varifyT_global
   230   end
   230   end
   231 
   231 
   232 fun to_definitional_cnf_with_quantifiers thy th =
   232 fun to_definitional_cnf_with_quantifiers ctxt th =
   233   let
   233   let
   234     val eqth = cnf.make_cnfx_thm thy (HOLogic.dest_Trueprop (prop_of th))
   234     val eqth = cnf.make_cnfx_thm ctxt (HOLogic.dest_Trueprop (prop_of th))
   235     val eqth = eqth RS @{thm eq_reflection}
   235     val eqth = eqth RS @{thm eq_reflection}
   236     val eqth = eqth RS @{thm TruepropI}
   236     val eqth = eqth RS @{thm TruepropI}
   237   in Thm.equal_elim eqth th end
   237   in Thm.equal_elim eqth th end
   238 
   238 
   239 fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
   239 fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
   339                 Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> cprop_of
   339                 Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> cprop_of
   340               else
   340               else
   341                 cterm_of thy)
   341                 cterm_of thy)
   342           |> zap true
   342           |> zap true
   343         val fixes =
   343         val fixes =
   344           Term.add_free_names (prop_of zapped_th) []
   344           [] |> Term.add_free_names (prop_of zapped_th)
   345           |> filter is_zapped_var_name
   345              |> filter is_zapped_var_name
   346         val ctxt' = ctxt |> Variable.add_fixes_direct fixes
   346         val ctxt' = ctxt |> Variable.add_fixes_direct fixes
   347         val fully_skolemized_t =
   347         val fully_skolemized_t =
   348           zapped_th |> singleton (Variable.export ctxt' ctxt)
   348           zapped_th |> singleton (Variable.export ctxt' ctxt)
   349                     |> cprop_of |> Thm.dest_equals |> snd |> term_of
   349                     |> cprop_of |> Thm.dest_equals |> snd |> term_of
   350       in
   350       in
   364       end
   364       end
   365     else
   365     else
   366       (NONE, (th, ctxt))
   366       (NONE, (th, ctxt))
   367   end
   367   end
   368 
   368 
       
   369 val all_out_ss =
       
   370   Simplifier.clear_ss HOL_basic_ss addsimps @{thms all_simps[symmetric]}
       
   371 
       
   372 val meta_allI = @{lemma "ALL x. P x ==> (!!x. P x)" by auto}
       
   373 
       
   374 fun repeat f x =
       
   375   case try f x of
       
   376     SOME y => repeat f y
       
   377   | NONE => x
       
   378 
       
   379 fun close_thm thy th =
       
   380   fold (Thm.forall_intr o cterm_of thy o Var) (Term.add_vars (prop_of th) []) th
       
   381 
       
   382 fun cnf_axiom_xxx ctxt0 new_skolemizer ax_no th =
       
   383   let
       
   384     val ctxt0 = Variable.global_thm_context th
       
   385     val thy = ProofContext.theory_of ctxt0
       
   386     val choice_ths = choice_theorems thy
       
   387     val (opt, (nnf_th, ctxt)) =
       
   388       nnf_axiom choice_ths new_skolemizer ax_no th ctxt0
       
   389     val skolem_ths = map (old_skolem_theorem_from_def thy) o old_skolem_defs
       
   390     fun make_cnf th = Meson.make_cnf (skolem_ths th) th
       
   391     val (cnf_ths, ctxt) =
       
   392        make_cnf nnf_th ctxt
       
   393        |> (fn (cnf, _) =>
       
   394               let
       
   395                 val _ = tracing ("nondef CNF: " ^ string_of_int (length cnf) ^ " clause(s)")
       
   396                 val sko_th =
       
   397                   nnf_th |> Simplifier.simplify all_out_ss
       
   398                          |> repeat (fn th => th RS meta_allI)
       
   399                          |> Meson.make_xxx_skolem ctxt (skolem_ths nnf_th)
       
   400                          |> close_thm thy
       
   401                          |> Conv.fconv_rule Object_Logic.atomize
       
   402                          |> to_definitional_cnf_with_quantifiers ctxt
       
   403               in make_cnf sko_th ctxt end
       
   404            | p => p)
       
   405     fun intr_imp ct th =
       
   406       Thm.instantiate ([], map (pairself (cterm_of thy))
       
   407                                [(Var (("i", 0), @{typ nat}),
       
   408                                  HOLogic.mk_nat ax_no)])
       
   409                       (zero_var_indexes @{thm skolem_COMBK_D})
       
   410       RS Thm.implies_intr ct th
       
   411   in
       
   412     (NONE (*###*),
       
   413      cnf_ths |> map (introduce_combinators_in_theorem
       
   414                      #> (case opt of SOME (_, ct) => intr_imp ct | NONE => I))
       
   415              |> Variable.export ctxt ctxt0
       
   416              |> finish_cnf
       
   417              |> map Thm.close_derivation)
       
   418   end
       
   419 
       
   420 
   369 (* Convert a theorem to CNF, with additional premises due to skolemization. *)
   421 (* Convert a theorem to CNF, with additional premises due to skolemization. *)
   370 fun cnf_axiom ctxt0 new_skolemizer ax_no th =
   422 fun cnf_axiom ctxt0 new_skolemizer ax_no th =
   371   let
   423   let
   372     val thy = ProofContext.theory_of ctxt0
   424     val thy = ProofContext.theory_of ctxt0
   373     val choice_ths = choice_theorems thy
   425     val choice_ths = choice_theorems thy
   380                   map (old_skolem_theorem_from_def thy)
   432                   map (old_skolem_theorem_from_def thy)
   381                       (old_skolem_defs th)) th ctxt
   433                       (old_skolem_defs th)) th ctxt
   382     val (cnf_ths, ctxt) =
   434     val (cnf_ths, ctxt) =
   383       clausify nnf_th
   435       clausify nnf_th
   384       |> (fn ([], _) =>
   436       |> (fn ([], _) =>
   385              (* FIXME: This probably doesn't work with the new Skolemizer *)
   437              if new_skolemizer then
   386              clausify (to_definitional_cnf_with_quantifiers thy nnf_th)
   438                let
       
   439 val _ = tracing ("**** NEW CLAUSIFIER, DEF-CNF: " ^ Display.string_of_thm ctxt (to_definitional_cnf_with_quantifiers ctxt nnf_th)) (*###*)
       
   440 val _ = tracing (" *** th: " ^ Display.string_of_thm ctxt th) (*###*)
       
   441                  val (_, (th1, ctxt)) = nnf_axiom choice_ths true ax_no th ctxt0
       
   442 val _ = tracing (" *** th1: " ^ Display.string_of_thm ctxt th1) (*###*)
       
   443                  val th2 = to_definitional_cnf_with_quantifiers ctxt th1
       
   444 val _ = tracing (" *** th2: " ^ Display.string_of_thm ctxt th2) (*###*)
       
   445                  val (ths3, ctxt) = clausify th2
       
   446 val _ = tracing (" *** hd ths3: " ^ Display.string_of_thm ctxt (hd ths3)) (*###*)
       
   447                in (ths3, ctxt) end
       
   448              else
       
   449 let
       
   450 val _ = tracing ("**** OLD CLAUSIFIER, DEF-CNF: " ^ Display.string_of_thm ctxt (to_definitional_cnf_with_quantifiers ctxt nnf_th))
       
   451 (*###*) in
       
   452                clausify (to_definitional_cnf_with_quantifiers ctxt nnf_th)
       
   453 end
   387            | p => p)
   454            | p => p)
   388     fun intr_imp ct th =
   455     fun intr_imp ct th =
   389       Thm.instantiate ([], map (pairself (cterm_of thy))
   456       Thm.instantiate ([], map (pairself (cterm_of thy))
   390                                [(Var (("i", 0), @{typ nat}),
   457                                [(Var (("i", 0), @{typ nat}),
   391                                  HOLogic.mk_nat ax_no)])
   458                                  HOLogic.mk_nat ax_no)])