src/HOL/Tools/Meson/meson_clausify.ML
author wenzelm
Mon Jul 27 17:44:55 2015 +0200 (2015-07-27)
changeset 60801 7664e0916eec
parent 60781 2da59cdf531c
child 61268 abe08fb15a12
permissions -rw-r--r--
tuned signature;
blanchet@39941
     1
(*  Title:      HOL/Tools/Meson/meson_clausify.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     4
blanchet@39941
     5
Transformation of HOL theorems into CNF forms.
paulson@15347
     6
*)
paulson@15347
     7
blanchet@39890
     8
signature MESON_CLAUSIFY =
wenzelm@21505
     9
sig
blanchet@39887
    10
  val new_skolem_var_prefix : string
blanchet@42098
    11
  val new_nonskolem_var_prefix : string
blanchet@42099
    12
  val is_zapped_var_name : string -> bool
blanchet@45511
    13
  val is_quasi_lambda_free : term -> bool
wenzelm@60328
    14
  val introduce_combinators_in_cterm : Proof.context -> cterm -> thm
wenzelm@55236
    15
  val introduce_combinators_in_theorem : Proof.context -> thm -> thm
blanchet@39932
    16
  val cluster_of_zapped_var_name : string -> (int * (int * int)) * bool
wenzelm@51717
    17
  val ss_only : thm list -> Proof.context -> Proof.context
blanchet@39897
    18
  val cnf_axiom :
blanchet@45508
    19
    Proof.context -> bool -> bool -> int -> thm
blanchet@45508
    20
    -> (thm * term) option * thm list
wenzelm@21505
    21
end;
mengj@19196
    22
blanchet@39890
    23
structure Meson_Clausify : MESON_CLAUSIFY =
paulson@15997
    24
struct
paulson@15347
    25
blanchet@39950
    26
open Meson
blanchet@39950
    27
blanchet@42072
    28
(* the extra "Meson" helps prevent clashes (FIXME) *)
blanchet@42072
    29
val new_skolem_var_prefix = "MesonSK"
blanchet@42072
    30
val new_nonskolem_var_prefix = "MesonV"
blanchet@39887
    31
blanchet@42099
    32
fun is_zapped_var_name s =
blanchet@42099
    33
  exists (fn prefix => String.isPrefix prefix s)
blanchet@42099
    34
         [new_skolem_var_prefix, new_nonskolem_var_prefix]
blanchet@42099
    35
paulson@15997
    36
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    37
wenzelm@59632
    38
val cfalse = Thm.cterm_of @{theory_context HOL} @{term False};
wenzelm@59632
    39
val ctp_false = Thm.cterm_of @{theory_context HOL} (HOLogic.mk_Trueprop @{term False});
wenzelm@20461
    40
blanchet@38001
    41
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38001
    42
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@42944
    43
   the conclusion variable to False. (Cf. "transform_elim_prop" in
blanchet@38652
    44
   "Sledgehammer_Util".) *)
blanchet@38001
    45
fun transform_elim_theorem th =
wenzelm@59582
    46
  (case Thm.concl_of th of    (*conclusion variable*)
wenzelm@60642
    47
    @{const Trueprop} $ (Var (v as (_, @{typ bool}))) =>
wenzelm@60642
    48
      Thm.instantiate ([], [(v, cfalse)]) th
wenzelm@60642
    49
  | Var (v as (_, @{typ prop})) =>
wenzelm@60642
    50
      Thm.instantiate ([], [(v, ctp_false)]) th
wenzelm@59582
    51
  | _ => th)
paulson@15997
    52
wenzelm@28544
    53
paulson@16009
    54
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
    55
blanchet@39886
    56
fun mk_old_skolem_term_wrapper t =
blanchet@37436
    57
  let val T = fastype_of t in
blanchet@39962
    58
    Const (@{const_name Meson.skolem}, T --> T) $ t
blanchet@37436
    59
  end
blanchet@37410
    60
blanchet@39931
    61
fun beta_eta_in_abs_body (Abs (s, T, t')) = Abs (s, T, beta_eta_in_abs_body t')
blanchet@39931
    62
  | beta_eta_in_abs_body t = Envir.beta_eta_contract t
blanchet@37512
    63
paulson@18141
    64
(*Traverse a theorem, accumulating Skolem function definitions.*)
blanchet@39886
    65
fun old_skolem_defs th =
blanchet@37399
    66
  let
blanchet@39376
    67
    fun dec_sko (Const (@{const_name Ex}, _) $ (body as Abs (_, T, p))) rhss =
blanchet@37399
    68
        (*Existential: declare a Skolem function, then insert into body and continue*)
blanchet@37399
    69
        let
wenzelm@44121
    70
          val args = Misc_Legacy.term_frees body
blanchet@37500
    71
          (* Forms a lambda-abstraction over the formal parameters *)
blanchet@37500
    72
          val rhs =
wenzelm@44241
    73
            fold_rev (absfree o dest_Free) args
wenzelm@44241
    74
              (HOLogic.choice_const T $ beta_eta_in_abs_body body)
blanchet@39886
    75
            |> mk_old_skolem_term_wrapper
blanchet@37518
    76
          val comb = list_comb (rhs, args)
blanchet@37617
    77
        in dec_sko (subst_bound (comb, p)) (rhs :: rhss) end
blanchet@37617
    78
      | dec_sko (Const (@{const_name All},_) $ Abs (a, T, p)) rhss =
blanchet@37399
    79
        (*Universal quant: insert a free variable into body and continue*)
wenzelm@44121
    80
        let val fname = singleton (Name.variant_list (Misc_Legacy.add_term_names (p, []))) a
blanchet@37617
    81
        in dec_sko (subst_bound (Free(fname,T), p)) rhss end
blanchet@39906
    82
      | dec_sko (@{const conj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
blanchet@39906
    83
      | dec_sko (@{const disj} $ p $ q) rhss = rhss |> dec_sko p |> dec_sko q
blanchet@37617
    84
      | dec_sko (@{const Trueprop} $ p) rhss = dec_sko p rhss
blanchet@37617
    85
      | dec_sko _ rhss = rhss
wenzelm@59582
    86
  in  dec_sko (Thm.prop_of th) []  end;
paulson@20419
    87
paulson@20419
    88
paulson@24827
    89
(**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
paulson@20419
    90
blanchet@39962
    91
fun is_quasi_lambda_free (Const (@{const_name Meson.skolem}, _) $ _) = true
blanchet@37416
    92
  | is_quasi_lambda_free (t1 $ t2) =
blanchet@37416
    93
    is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2
blanchet@37416
    94
  | is_quasi_lambda_free (Abs _) = false
blanchet@37416
    95
  | is_quasi_lambda_free _ = true
wenzelm@20461
    96
blanchet@38282
    97
(* FIXME: Requires more use of cterm constructors. *)
wenzelm@60328
    98
fun abstract ctxt ct =
wenzelm@28544
    99
  let
wenzelm@59582
   100
      val Abs(x,_,body) = Thm.term_of ct
wenzelm@59586
   101
      val Type (@{type_name fun}, [xT,bodyT]) = Thm.typ_of_cterm ct
wenzelm@60328
   102
      val cxT = Thm.ctyp_of ctxt xT
wenzelm@60328
   103
      val cbodyT = Thm.ctyp_of ctxt bodyT
blanchet@38005
   104
      fun makeK () =
wenzelm@60801
   105
        Thm.instantiate' [SOME cxT, SOME cbodyT] [SOME (Thm.cterm_of ctxt body)] @{thm abs_K}
paulson@24827
   106
  in
paulson@24827
   107
      case body of
paulson@24827
   108
          Const _ => makeK()
paulson@24827
   109
        | Free _ => makeK()
paulson@24827
   110
        | Var _ => makeK()  (*though Var isn't expected*)
wenzelm@60801
   111
        | Bound 0 => Thm.instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
paulson@24827
   112
        | rator$rand =>
wenzelm@42083
   113
            if Term.is_dependent rator then (*C or S*)
wenzelm@42083
   114
               if Term.is_dependent rand then (*S*)
wenzelm@60328
   115
                 let val crator = Thm.cterm_of ctxt (Abs (x, xT, rator))
wenzelm@60328
   116
                     val crand = Thm.cterm_of ctxt (Abs (x, xT, rand))
wenzelm@60781
   117
                     val abs_S' =
wenzelm@60781
   118
                      infer_instantiate ctxt [(("f", 0), crator), (("g", 0), crand)] @{thm abs_S}
wenzelm@59582
   119
                     val (_,rhs) = Thm.dest_equals (Thm.cprop_of abs_S')
wenzelm@27179
   120
                 in
wenzelm@60328
   121
                   Thm.transitive abs_S' (Conv.binop_conv (abstract ctxt) rhs)
wenzelm@27179
   122
                 end
wenzelm@27179
   123
               else (*C*)
wenzelm@60328
   124
                 let val crator = Thm.cterm_of ctxt (Abs (x, xT, rator))
wenzelm@59582
   125
                     val abs_C' =
wenzelm@60781
   126
                      infer_instantiate ctxt [(("f", 0), crator), (("b", 0), Thm.cterm_of ctxt rand)]
wenzelm@60781
   127
                        @{thm abs_C}
wenzelm@59582
   128
                     val (_,rhs) = Thm.dest_equals (Thm.cprop_of abs_C')
wenzelm@27179
   129
                 in
wenzelm@60328
   130
                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv (abstract ctxt)) rhs)
wenzelm@27179
   131
                 end
wenzelm@42083
   132
            else if Term.is_dependent rand then (*B or eta*)
wenzelm@36945
   133
               if rand = Bound 0 then Thm.eta_conversion ct
wenzelm@27179
   134
               else (*B*)
wenzelm@60328
   135
                 let val crand = Thm.cterm_of ctxt (Abs (x, xT, rand))
wenzelm@60328
   136
                     val crator = Thm.cterm_of ctxt rator
wenzelm@60781
   137
                     val abs_B' =
wenzelm@60781
   138
                      infer_instantiate ctxt [(("a", 0), crator), (("g", 0), crand)] @{thm abs_B}
wenzelm@59582
   139
                     val (_,rhs) = Thm.dest_equals (Thm.cprop_of abs_B')
wenzelm@60328
   140
                 in Thm.transitive abs_B' (Conv.arg_conv (abstract ctxt) rhs) end
wenzelm@60328
   141
            else makeK ()
blanchet@37349
   142
        | _ => raise Fail "abstract: Bad term"
paulson@24827
   143
  end;
paulson@20863
   144
blanchet@37349
   145
(* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
wenzelm@60328
   146
fun introduce_combinators_in_cterm ctxt ct =
wenzelm@59582
   147
  if is_quasi_lambda_free (Thm.term_of ct) then
blanchet@37349
   148
    Thm.reflexive ct
wenzelm@59582
   149
  else case Thm.term_of ct of
blanchet@37349
   150
    Abs _ =>
blanchet@37349
   151
    let
blanchet@37349
   152
      val (cv, cta) = Thm.dest_abs NONE ct
wenzelm@59582
   153
      val (v, _) = dest_Free (Thm.term_of cv)
wenzelm@60328
   154
      val u_th = introduce_combinators_in_cterm ctxt cta
blanchet@37349
   155
      val cu = Thm.rhs_of u_th
wenzelm@60328
   156
      val comb_eq = abstract ctxt (Thm.lambda cv cu)
blanchet@37349
   157
    in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
blanchet@37349
   158
  | _ $ _ =>
blanchet@37349
   159
    let val (ct1, ct2) = Thm.dest_comb ct in
wenzelm@60328
   160
        Thm.combination (introduce_combinators_in_cterm ctxt ct1)
wenzelm@60328
   161
                        (introduce_combinators_in_cterm ctxt ct2)
blanchet@37349
   162
    end
blanchet@37349
   163
wenzelm@55236
   164
fun introduce_combinators_in_theorem ctxt th =
wenzelm@59582
   165
  if is_quasi_lambda_free (Thm.prop_of th) then
blanchet@37349
   166
    th
paulson@24827
   167
  else
blanchet@37349
   168
    let
blanchet@37349
   169
      val th = Drule.eta_contraction_rule th
wenzelm@60328
   170
      val eqth = introduce_combinators_in_cterm ctxt (Thm.cprop_of th)
blanchet@37349
   171
    in Thm.equal_elim eqth th end
blanchet@37349
   172
    handle THM (msg, _, _) =>
blanchet@55523
   173
           (warning ("Error in the combinator translation of " ^ Display.string_of_thm ctxt th ^
blanchet@55523
   174
              "\nException message: " ^ msg);
blanchet@45511
   175
            (* A type variable of sort "{}" will make "abstraction" fail. *)
blanchet@37349
   176
            TrueI)
paulson@16009
   177
paulson@16009
   178
(*cterms are used throughout for efficiency*)
wenzelm@59632
   179
val cTrueprop = Thm.cterm_of @{theory_context HOL} HOLogic.Trueprop;
paulson@16009
   180
paulson@16009
   181
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   182
  ones. Return the body, along with the list of free variables.*)
wenzelm@20461
   183
fun c_variant_abs_multi (ct0, vars) =
paulson@16009
   184
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   185
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   186
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   187
blanchet@37617
   188
(* Given the definition of a Skolem function, return a theorem to replace
blanchet@37617
   189
   an existential formula by a use of that function.
paulson@18141
   190
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
blanchet@57466
   191
fun old_skolem_theorem_of_def ctxt rhs0 =
blanchet@37399
   192
  let
wenzelm@59632
   193
    val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> Thm.cterm_of ctxt
blanchet@37617
   194
    val rhs' = rhs |> Thm.dest_comb |> snd
blanchet@37617
   195
    val (ch, frees) = c_variant_abs_multi (rhs', [])
wenzelm@59582
   196
    val (hilbert, cabs) = ch |> Thm.dest_comb |>> Thm.term_of
blanchet@37617
   197
    val T =
blanchet@37617
   198
      case hilbert of
blanchet@39941
   199
        Const (_, Type (@{type_name fun}, [_, T])) => T
blanchet@57466
   200
      | _ => raise TERM ("old_skolem_theorem_of_def: expected \"Eps\"", [hilbert])
wenzelm@59632
   201
    val cex = Thm.cterm_of ctxt (HOLogic.exists_const T)
wenzelm@46497
   202
    val ex_tm = Thm.apply cTrueprop (Thm.apply cex cabs)
blanchet@37629
   203
    val conc =
blanchet@37617
   204
      Drule.list_comb (rhs, frees)
wenzelm@46497
   205
      |> Drule.beta_conv cabs |> Thm.apply cTrueprop
blanchet@37617
   206
    fun tacf [prem] =
wenzelm@54742
   207
      rewrite_goals_tac ctxt @{thms skolem_def [abs_def]}
wenzelm@59632
   208
      THEN resolve_tac ctxt
wenzelm@59632
   209
        [(prem |> rewrite_rule ctxt @{thms skolem_def [abs_def]})
wenzelm@59632
   210
          RS Global_Theory.get_thm (Proof_Context.theory_of ctxt) "Hilbert_Choice.someI_ex"] 1
blanchet@37617
   211
  in
wenzelm@54883
   212
    Goal.prove_internal ctxt [ex_tm] conc tacf
blanchet@37629
   213
    |> forall_intr_list frees
blanchet@37629
   214
    |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
blanchet@37629
   215
    |> Thm.varifyT_global
blanchet@37617
   216
  end
paulson@24742
   217
blanchet@42335
   218
fun to_definitional_cnf_with_quantifiers ctxt th =
blanchet@39036
   219
  let
wenzelm@59582
   220
    val eqth = CNF.make_cnfx_thm ctxt (HOLogic.dest_Trueprop (Thm.prop_of th))
blanchet@39036
   221
    val eqth = eqth RS @{thm eq_reflection}
blanchet@39036
   222
    val eqth = eqth RS @{thm TruepropI}
blanchet@39036
   223
  in Thm.equal_elim eqth th end
blanchet@39036
   224
blanchet@39932
   225
fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
blanchet@39896
   226
  (if skolem then new_skolem_var_prefix else new_nonskolem_var_prefix) ^
blanchet@39932
   227
  "_" ^ string_of_int ax_no ^ "_" ^ string_of_int cluster_no ^ "_" ^
haftmann@56811
   228
  string_of_int index_no ^ "_" ^ Name.desymbolize (SOME false) s
blanchet@39896
   229
blanchet@39899
   230
fun cluster_of_zapped_var_name s =
blanchet@39932
   231
  let val get_int = the o Int.fromString o nth (space_explode "_" s) in
blanchet@39932
   232
    ((get_int 1, (get_int 2, get_int 3)),
blanchet@39932
   233
     String.isPrefix new_skolem_var_prefix s)
blanchet@39932
   234
  end
blanchet@39897
   235
blanchet@40260
   236
fun rename_bound_vars_to_be_zapped ax_no =
blanchet@40260
   237
  let
blanchet@40260
   238
    fun aux (cluster as (cluster_no, cluster_skolem)) index_no pos t =
blanchet@40260
   239
      case t of
blanchet@40260
   240
        (t1 as Const (s, _)) $ Abs (s', T, t') =>
wenzelm@56245
   241
        if s = @{const_name Pure.all} orelse s = @{const_name All} orelse
blanchet@39906
   242
           s = @{const_name Ex} then
blanchet@39932
   243
          let
blanchet@39932
   244
            val skolem = (pos = (s = @{const_name Ex}))
blanchet@39932
   245
            val (cluster, index_no) =
blanchet@39932
   246
              if skolem = cluster_skolem then (cluster, index_no)
blanchet@39932
   247
              else ((cluster_no ||> cluster_skolem ? Integer.add 1, skolem), 0)
blanchet@40260
   248
            val s' = zapped_var_name cluster index_no s'
blanchet@40260
   249
          in t1 $ Abs (s', T, aux cluster (index_no + 1) pos t') end
blanchet@40260
   250
        else
blanchet@40260
   251
          t
blanchet@40260
   252
      | (t1 as Const (s, _)) $ t2 $ t3 =>
wenzelm@56245
   253
        if s = @{const_name Pure.imp} orelse s = @{const_name HOL.implies} then
blanchet@40260
   254
          t1 $ aux cluster index_no (not pos) t2 $ aux cluster index_no pos t3
blanchet@40260
   255
        else if s = @{const_name HOL.conj} orelse
blanchet@40260
   256
                s = @{const_name HOL.disj} then
blanchet@40260
   257
          t1 $ aux cluster index_no pos t2 $ aux cluster index_no pos t3
blanchet@40260
   258
        else
blanchet@40260
   259
          t
blanchet@40260
   260
      | (t1 as Const (s, _)) $ t2 =>
blanchet@40260
   261
        if s = @{const_name Trueprop} then
blanchet@40260
   262
          t1 $ aux cluster index_no pos t2
blanchet@40260
   263
        else if s = @{const_name Not} then
blanchet@40260
   264
          t1 $ aux cluster index_no (not pos) t2
blanchet@40260
   265
        else
blanchet@40260
   266
          t
blanchet@40260
   267
      | _ => t
blanchet@40260
   268
  in aux ((ax_no, 0), true) 0 true end
blanchet@40260
   269
blanchet@40260
   270
fun zap pos ct =
blanchet@40260
   271
  ct
wenzelm@59582
   272
  |> (case Thm.term_of ct of
blanchet@40260
   273
        Const (s, _) $ Abs (s', _, _) =>
wenzelm@56245
   274
        if s = @{const_name Pure.all} orelse s = @{const_name All} orelse
blanchet@40260
   275
           s = @{const_name Ex} then
blanchet@40260
   276
          Thm.dest_comb #> snd #> Thm.dest_abs (SOME s') #> snd #> zap pos
blanchet@39906
   277
        else
blanchet@39906
   278
          Conv.all_conv
blanchet@39906
   279
      | Const (s, _) $ _ $ _ =>
wenzelm@56245
   280
        if s = @{const_name Pure.imp} orelse s = @{const_name implies} then
blanchet@40260
   281
          Conv.combination_conv (Conv.arg_conv (zap (not pos))) (zap pos)
blanchet@39906
   282
        else if s = @{const_name conj} orelse s = @{const_name disj} then
blanchet@40260
   283
          Conv.combination_conv (Conv.arg_conv (zap pos)) (zap pos)
blanchet@39906
   284
        else
blanchet@39906
   285
          Conv.all_conv
blanchet@39906
   286
      | Const (s, _) $ _ =>
blanchet@40260
   287
        if s = @{const_name Trueprop} then Conv.arg_conv (zap pos)
blanchet@40260
   288
        else if s = @{const_name Not} then Conv.arg_conv (zap (not pos))
blanchet@40260
   289
        else Conv.all_conv
blanchet@39906
   290
      | _ => Conv.all_conv)
blanchet@39887
   291
wenzelm@51717
   292
fun ss_only ths ctxt = clear_simpset (put_simpset HOL_basic_ss ctxt) addsimps ths
blanchet@39901
   293
blanchet@40261
   294
val cheat_choice =
blanchet@39901
   295
  @{prop "ALL x. EX y. Q x y ==> EX f. ALL x. Q x (f x)"}
blanchet@39901
   296
  |> Logic.varify_global
blanchet@39901
   297
  |> Skip_Proof.make_thm @{theory}
blanchet@39887
   298
blanchet@39887
   299
(* Converts an Isabelle theorem into NNF. *)
blanchet@50705
   300
fun nnf_axiom choice_ths new_skolem ax_no th ctxt =
blanchet@39887
   301
  let
wenzelm@42361
   302
    val thy = Proof_Context.theory_of ctxt
blanchet@39887
   303
    val th =
blanchet@39887
   304
      th |> transform_elim_theorem
blanchet@39887
   305
         |> zero_var_indexes
blanchet@50705
   306
         |> new_skolem ? forall_intr_vars
blanchet@39887
   307
    val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single
wenzelm@54742
   308
    val th = th |> Conv.fconv_rule (Object_Logic.atomize ctxt)
wenzelm@59632
   309
                |> cong_extensionalize_thm ctxt
blanchet@47954
   310
                |> abs_extensionalize_thm ctxt
blanchet@47954
   311
                |> make_nnf ctxt
blanchet@39887
   312
  in
blanchet@50705
   313
    if new_skolem then
blanchet@39887
   314
      let
blanchet@39901
   315
        fun skolemize choice_ths =
blanchet@39950
   316
          skolemize_with_choice_theorems ctxt choice_ths
wenzelm@51717
   317
          #> simplify (ss_only @{thms all_simps[symmetric]} ctxt)
blanchet@42347
   318
        val no_choice = null choice_ths
blanchet@39901
   319
        val pull_out =
blanchet@42347
   320
          if no_choice then
wenzelm@51717
   321
            simplify (ss_only @{thms all_simps[symmetric] ex_simps[symmetric]} ctxt)
blanchet@42347
   322
          else
blanchet@42347
   323
            skolemize choice_ths
blanchet@42347
   324
        val discharger_th = th |> pull_out
blanchet@40260
   325
        val discharger_th =
wenzelm@59582
   326
          discharger_th |> has_too_many_clauses ctxt (Thm.concl_of discharger_th)
blanchet@42347
   327
                           ? (to_definitional_cnf_with_quantifiers ctxt
blanchet@42347
   328
                              #> pull_out)
blanchet@42099
   329
        val zapped_th =
wenzelm@59582
   330
          discharger_th |> Thm.prop_of |> rename_bound_vars_to_be_zapped ax_no
blanchet@40263
   331
          |> (if no_choice then
wenzelm@59582
   332
                Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> Thm.cprop_of
blanchet@40263
   333
              else
wenzelm@59632
   334
                Thm.cterm_of ctxt)
blanchet@42099
   335
          |> zap true
blanchet@42099
   336
        val fixes =
wenzelm@59582
   337
          [] |> Term.add_free_names (Thm.prop_of zapped_th)
blanchet@42335
   338
             |> filter is_zapped_var_name
blanchet@42269
   339
        val ctxt' = ctxt |> Variable.add_fixes_direct fixes
blanchet@42099
   340
        val fully_skolemized_t =
blanchet@42333
   341
          zapped_th |> singleton (Variable.export ctxt' ctxt)
wenzelm@59582
   342
                    |> Thm.cprop_of |> Thm.dest_equals |> snd |> Thm.term_of
blanchet@39887
   343
      in
blanchet@39887
   344
        if exists_subterm (fn Var ((s, _), _) =>
blanchet@39887
   345
                              String.isPrefix new_skolem_var_prefix s
blanchet@40260
   346
                            | _ => false) fully_skolemized_t then
blanchet@39887
   347
          let
blanchet@40260
   348
            val (fully_skolemized_ct, ctxt) =
blanchet@40260
   349
              Variable.import_terms true [fully_skolemized_t] ctxt
wenzelm@59632
   350
              |>> the_single |>> Thm.cterm_of ctxt
blanchet@40260
   351
          in
blanchet@40260
   352
            (SOME (discharger_th, fully_skolemized_ct),
blanchet@40262
   353
             (Thm.assume fully_skolemized_ct, ctxt))
blanchet@40260
   354
          end
blanchet@39887
   355
       else
blanchet@40262
   356
         (NONE, (th, ctxt))
blanchet@39887
   357
      end
blanchet@39887
   358
    else
wenzelm@59582
   359
      (NONE, (th |> has_too_many_clauses ctxt (Thm.concl_of th)
blanchet@42347
   360
                    ? to_definitional_cnf_with_quantifiers ctxt, ctxt))
blanchet@39887
   361
  end
blanchet@39887
   362
blanchet@39887
   363
(* Convert a theorem to CNF, with additional premises due to skolemization. *)
blanchet@50705
   364
fun cnf_axiom ctxt0 new_skolem combinators ax_no th =
blanchet@37626
   365
  let
wenzelm@42361
   366
    val thy = Proof_Context.theory_of ctxt0
blanchet@39950
   367
    val choice_ths = choice_theorems thy
blanchet@40262
   368
    val (opt, (nnf_th, ctxt)) =
blanchet@50705
   369
      nnf_axiom choice_ths new_skolem ax_no th ctxt0
blanchet@39894
   370
    fun clausify th =
wenzelm@59165
   371
      make_cnf
wenzelm@59165
   372
       (if new_skolem orelse null choice_ths then []
wenzelm@59165
   373
        else map (old_skolem_theorem_of_def ctxt) (old_skolem_defs th))
wenzelm@59165
   374
       th ctxt
blanchet@42347
   375
    val (cnf_ths, ctxt) = clausify nnf_th
blanchet@39894
   376
    fun intr_imp ct th =
wenzelm@60642
   377
      Thm.instantiate ([], [((("i", 0), @{typ nat}), Thm.cterm_of ctxt (HOLogic.mk_nat ax_no))])
blanchet@39962
   378
                      (zero_var_indexes @{thm skolem_COMBK_D})
blanchet@39894
   379
      RS Thm.implies_intr ct th
blanchet@37626
   380
  in
blanchet@39897
   381
    (opt |> Option.map (I #>> singleton (Variable.export ctxt ctxt0)
wenzelm@59582
   382
                        ##> (Thm.term_of #> HOLogic.dest_Trueprop
blanchet@39897
   383
                             #> singleton (Variable.export_terms ctxt ctxt0))),
wenzelm@55236
   384
     cnf_ths |> map (combinators ? introduce_combinators_in_theorem ctxt
blanchet@39894
   385
                     #> (case opt of SOME (_, ct) => intr_imp ct | NONE => I))
blanchet@39897
   386
             |> Variable.export ctxt ctxt0
blanchet@39950
   387
             |> finish_cnf
blanchet@39887
   388
             |> map Thm.close_derivation)
blanchet@37626
   389
  end
blanchet@39887
   390
  handle THM _ => (NONE, [])
wenzelm@27184
   391
wenzelm@20461
   392
end;