src/HOL/Tools/Meson/meson_clausify.ML
author blanchet
Tue Nov 15 22:13:39 2011 +0100 (2011-11-15)
changeset 45511 9b0f8ca4388e
parent 45508 b216dc1b3630
child 45740 132a3e1c0fe5
permissions -rw-r--r--
continued implementation of lambda-lifting in Metis
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
blanchet@38001
    14
  val introduce_combinators_in_cterm : cterm -> thm
blanchet@38028
    15
  val introduce_combinators_in_theorem : thm -> thm
blanchet@39932
    16
  val cluster_of_zapped_var_name : string -> (int * (int * int)) * bool
blanchet@42336
    17
  val ss_only : thm list -> simpset
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@29064
    38
val cfalse = cterm_of @{theory HOL} HOLogic.false_const;
wenzelm@29064
    39
val ctp_false = cterm_of @{theory HOL} (HOLogic.mk_Trueprop HOLogic.false_const);
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 =
paulson@21430
    46
  case concl_of th of    (*conclusion variable*)
blanchet@35963
    47
       @{const Trueprop} $ (v as Var (_, @{typ bool})) =>
wenzelm@29064
    48
           Thm.instantiate ([], [(cterm_of @{theory HOL} v, cfalse)]) th
blanchet@35963
    49
    | v as Var(_, @{typ prop}) =>
wenzelm@29064
    50
           Thm.instantiate ([], [(cterm_of @{theory HOL} v, ctp_false)]) th
blanchet@38001
    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
paulson@20419
    86
  in  dec_sko (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
wenzelm@44121
    97
val [f_B,g_B] = map (cterm_of @{theory}) (Misc_Legacy.term_vars (prop_of @{thm abs_B}));
wenzelm@44121
    98
val [g_C,f_C] = map (cterm_of @{theory}) (Misc_Legacy.term_vars (prop_of @{thm abs_C}));
wenzelm@44121
    99
val [f_S,g_S] = map (cterm_of @{theory}) (Misc_Legacy.term_vars (prop_of @{thm abs_S}));
paulson@20863
   100
blanchet@38282
   101
(* FIXME: Requires more use of cterm constructors. *)
paulson@24827
   102
fun abstract ct =
wenzelm@28544
   103
  let
wenzelm@28544
   104
      val thy = theory_of_cterm ct
paulson@25256
   105
      val Abs(x,_,body) = term_of ct
blanchet@35963
   106
      val Type(@{type_name fun}, [xT,bodyT]) = typ_of (ctyp_of_term ct)
blanchet@38005
   107
      val cxT = ctyp_of thy xT
blanchet@38005
   108
      val cbodyT = ctyp_of thy bodyT
blanchet@38005
   109
      fun makeK () =
blanchet@38005
   110
        instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)]
blanchet@38005
   111
                     @{thm abs_K}
paulson@24827
   112
  in
paulson@24827
   113
      case body of
paulson@24827
   114
          Const _ => makeK()
paulson@24827
   115
        | Free _ => makeK()
paulson@24827
   116
        | Var _ => makeK()  (*though Var isn't expected*)
wenzelm@27184
   117
        | Bound 0 => instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
paulson@24827
   118
        | rator$rand =>
wenzelm@42083
   119
            if Term.is_dependent rator then (*C or S*)
wenzelm@42083
   120
               if Term.is_dependent rand then (*S*)
wenzelm@27179
   121
                 let val crator = cterm_of thy (Abs(x,xT,rator))
wenzelm@27179
   122
                     val crand = cterm_of thy (Abs(x,xT,rand))
wenzelm@27184
   123
                     val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] @{thm abs_S}
wenzelm@27184
   124
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_S')
wenzelm@27179
   125
                 in
wenzelm@27179
   126
                   Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
wenzelm@27179
   127
                 end
wenzelm@27179
   128
               else (*C*)
wenzelm@27179
   129
                 let val crator = cterm_of thy (Abs(x,xT,rator))
wenzelm@27184
   130
                     val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] @{thm abs_C}
wenzelm@27184
   131
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_C')
wenzelm@27179
   132
                 in
wenzelm@27179
   133
                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
wenzelm@27179
   134
                 end
wenzelm@42083
   135
            else if Term.is_dependent rand then (*B or eta*)
wenzelm@36945
   136
               if rand = Bound 0 then Thm.eta_conversion ct
wenzelm@27179
   137
               else (*B*)
wenzelm@27179
   138
                 let val crand = cterm_of thy (Abs(x,xT,rand))
wenzelm@27179
   139
                     val crator = cterm_of thy rator
wenzelm@27184
   140
                     val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
wenzelm@27184
   141
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_B')
blanchet@37349
   142
                 in Thm.transitive abs_B' (Conv.arg_conv abstract rhs) end
wenzelm@27179
   143
            else makeK()
blanchet@37349
   144
        | _ => raise Fail "abstract: Bad term"
paulson@24827
   145
  end;
paulson@20863
   146
blanchet@37349
   147
(* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
blanchet@38001
   148
fun introduce_combinators_in_cterm ct =
blanchet@37416
   149
  if is_quasi_lambda_free (term_of ct) then
blanchet@37349
   150
    Thm.reflexive ct
blanchet@37349
   151
  else case term_of ct of
blanchet@37349
   152
    Abs _ =>
blanchet@37349
   153
    let
blanchet@37349
   154
      val (cv, cta) = Thm.dest_abs NONE ct
blanchet@37349
   155
      val (v, _) = dest_Free (term_of cv)
blanchet@38001
   156
      val u_th = introduce_combinators_in_cterm cta
blanchet@37349
   157
      val cu = Thm.rhs_of u_th
blanchet@37349
   158
      val comb_eq = abstract (Thm.cabs cv cu)
blanchet@37349
   159
    in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
blanchet@37349
   160
  | _ $ _ =>
blanchet@37349
   161
    let val (ct1, ct2) = Thm.dest_comb ct in
blanchet@38001
   162
        Thm.combination (introduce_combinators_in_cterm ct1)
blanchet@38001
   163
                        (introduce_combinators_in_cterm ct2)
blanchet@37349
   164
    end
blanchet@37349
   165
blanchet@38001
   166
fun introduce_combinators_in_theorem th =
blanchet@37416
   167
  if is_quasi_lambda_free (prop_of th) then
blanchet@37349
   168
    th
paulson@24827
   169
  else
blanchet@37349
   170
    let
blanchet@37349
   171
      val th = Drule.eta_contraction_rule th
blanchet@38001
   172
      val eqth = introduce_combinators_in_cterm (cprop_of th)
blanchet@37349
   173
    in Thm.equal_elim eqth th end
blanchet@37349
   174
    handle THM (msg, _, _) =>
blanchet@37349
   175
           (warning ("Error in the combinator translation of " ^
blanchet@37349
   176
                     Display.string_of_thm_without_context th ^
blanchet@37349
   177
                     "\nException message: " ^ msg ^ ".");
blanchet@45511
   178
            (* A type variable of sort "{}" will make "abstraction" fail. *)
blanchet@37349
   179
            TrueI)
paulson@16009
   180
paulson@16009
   181
(*cterms are used throughout for efficiency*)
blanchet@38280
   182
val cTrueprop = cterm_of @{theory HOL} HOLogic.Trueprop;
paulson@16009
   183
paulson@16009
   184
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   185
  ones. Return the body, along with the list of free variables.*)
wenzelm@20461
   186
fun c_variant_abs_multi (ct0, vars) =
paulson@16009
   187
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   188
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   189
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   190
blanchet@39355
   191
val skolem_def_raw = @{thms skolem_def_raw}
blanchet@37617
   192
blanchet@37617
   193
(* Given the definition of a Skolem function, return a theorem to replace
blanchet@37617
   194
   an existential formula by a use of that function.
paulson@18141
   195
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
blanchet@39886
   196
fun old_skolem_theorem_from_def thy rhs0 =
blanchet@37399
   197
  let
blanchet@38280
   198
    val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> cterm_of thy
blanchet@37617
   199
    val rhs' = rhs |> Thm.dest_comb |> snd
blanchet@37617
   200
    val (ch, frees) = c_variant_abs_multi (rhs', [])
blanchet@37617
   201
    val (hilbert, cabs) = ch |> Thm.dest_comb |>> term_of
blanchet@37617
   202
    val T =
blanchet@37617
   203
      case hilbert of
blanchet@39941
   204
        Const (_, Type (@{type_name fun}, [_, T])) => T
blanchet@39886
   205
      | _ => raise TERM ("old_skolem_theorem_from_def: expected \"Eps\"",
blanchet@39886
   206
                         [hilbert])
blanchet@38280
   207
    val cex = cterm_of thy (HOLogic.exists_const T)
blanchet@37617
   208
    val ex_tm = Thm.capply cTrueprop (Thm.capply cex cabs)
blanchet@37629
   209
    val conc =
blanchet@37617
   210
      Drule.list_comb (rhs, frees)
blanchet@37617
   211
      |> Drule.beta_conv cabs |> Thm.capply cTrueprop
blanchet@37617
   212
    fun tacf [prem] =
blanchet@39355
   213
      rewrite_goals_tac skolem_def_raw
blanchet@39941
   214
      THEN rtac ((prem |> rewrite_rule skolem_def_raw)
blanchet@39949
   215
                 RS Global_Theory.get_thm thy "Hilbert_Choice.someI_ex") 1
blanchet@37617
   216
  in
blanchet@37629
   217
    Goal.prove_internal [ex_tm] conc tacf
blanchet@37629
   218
    |> forall_intr_list frees
blanchet@37629
   219
    |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
blanchet@37629
   220
    |> Thm.varifyT_global
blanchet@37617
   221
  end
paulson@24742
   222
blanchet@42335
   223
fun to_definitional_cnf_with_quantifiers ctxt th =
blanchet@39036
   224
  let
blanchet@42335
   225
    val eqth = cnf.make_cnfx_thm ctxt (HOLogic.dest_Trueprop (prop_of th))
blanchet@39036
   226
    val eqth = eqth RS @{thm eq_reflection}
blanchet@39036
   227
    val eqth = eqth RS @{thm TruepropI}
blanchet@39036
   228
  in Thm.equal_elim eqth th end
blanchet@39036
   229
blanchet@39932
   230
fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
blanchet@39896
   231
  (if skolem then new_skolem_var_prefix else new_nonskolem_var_prefix) ^
blanchet@39932
   232
  "_" ^ string_of_int ax_no ^ "_" ^ string_of_int cluster_no ^ "_" ^
blanchet@40261
   233
  string_of_int index_no ^ "_" ^ Name.desymbolize false s
blanchet@39896
   234
blanchet@39899
   235
fun cluster_of_zapped_var_name s =
blanchet@39932
   236
  let val get_int = the o Int.fromString o nth (space_explode "_" s) in
blanchet@39932
   237
    ((get_int 1, (get_int 2, get_int 3)),
blanchet@39932
   238
     String.isPrefix new_skolem_var_prefix s)
blanchet@39932
   239
  end
blanchet@39897
   240
blanchet@40260
   241
fun rename_bound_vars_to_be_zapped ax_no =
blanchet@40260
   242
  let
blanchet@40260
   243
    fun aux (cluster as (cluster_no, cluster_skolem)) index_no pos t =
blanchet@40260
   244
      case t of
blanchet@40260
   245
        (t1 as Const (s, _)) $ Abs (s', T, t') =>
blanchet@39906
   246
        if s = @{const_name all} orelse s = @{const_name All} orelse
blanchet@39906
   247
           s = @{const_name Ex} then
blanchet@39932
   248
          let
blanchet@39932
   249
            val skolem = (pos = (s = @{const_name Ex}))
blanchet@39932
   250
            val (cluster, index_no) =
blanchet@39932
   251
              if skolem = cluster_skolem then (cluster, index_no)
blanchet@39932
   252
              else ((cluster_no ||> cluster_skolem ? Integer.add 1, skolem), 0)
blanchet@40260
   253
            val s' = zapped_var_name cluster index_no s'
blanchet@40260
   254
          in t1 $ Abs (s', T, aux cluster (index_no + 1) pos t') end
blanchet@40260
   255
        else
blanchet@40260
   256
          t
blanchet@40260
   257
      | (t1 as Const (s, _)) $ t2 $ t3 =>
blanchet@40260
   258
        if s = @{const_name "==>"} orelse s = @{const_name HOL.implies} then
blanchet@40260
   259
          t1 $ aux cluster index_no (not pos) t2 $ aux cluster index_no pos t3
blanchet@40260
   260
        else if s = @{const_name HOL.conj} orelse
blanchet@40260
   261
                s = @{const_name HOL.disj} then
blanchet@40260
   262
          t1 $ aux cluster index_no pos t2 $ aux cluster index_no pos t3
blanchet@40260
   263
        else
blanchet@40260
   264
          t
blanchet@40260
   265
      | (t1 as Const (s, _)) $ t2 =>
blanchet@40260
   266
        if s = @{const_name Trueprop} then
blanchet@40260
   267
          t1 $ aux cluster index_no pos t2
blanchet@40260
   268
        else if s = @{const_name Not} then
blanchet@40260
   269
          t1 $ aux cluster index_no (not pos) t2
blanchet@40260
   270
        else
blanchet@40260
   271
          t
blanchet@40260
   272
      | _ => t
blanchet@40260
   273
  in aux ((ax_no, 0), true) 0 true end
blanchet@40260
   274
blanchet@40260
   275
fun zap pos ct =
blanchet@40260
   276
  ct
blanchet@40260
   277
  |> (case term_of ct of
blanchet@40260
   278
        Const (s, _) $ Abs (s', _, _) =>
blanchet@40260
   279
        if s = @{const_name all} orelse s = @{const_name All} orelse
blanchet@40260
   280
           s = @{const_name Ex} then
blanchet@40260
   281
          Thm.dest_comb #> snd #> Thm.dest_abs (SOME s') #> snd #> zap pos
blanchet@39906
   282
        else
blanchet@39906
   283
          Conv.all_conv
blanchet@39906
   284
      | Const (s, _) $ _ $ _ =>
blanchet@39906
   285
        if s = @{const_name "==>"} orelse s = @{const_name implies} then
blanchet@40260
   286
          Conv.combination_conv (Conv.arg_conv (zap (not pos))) (zap pos)
blanchet@39906
   287
        else if s = @{const_name conj} orelse s = @{const_name disj} then
blanchet@40260
   288
          Conv.combination_conv (Conv.arg_conv (zap pos)) (zap pos)
blanchet@39906
   289
        else
blanchet@39906
   290
          Conv.all_conv
blanchet@39906
   291
      | Const (s, _) $ _ =>
blanchet@40260
   292
        if s = @{const_name Trueprop} then Conv.arg_conv (zap pos)
blanchet@40260
   293
        else if s = @{const_name Not} then Conv.arg_conv (zap (not pos))
blanchet@40260
   294
        else Conv.all_conv
blanchet@39906
   295
      | _ => Conv.all_conv)
blanchet@39887
   296
wenzelm@41225
   297
fun ss_only ths = Simplifier.clear_ss HOL_basic_ss addsimps ths
blanchet@39901
   298
blanchet@40261
   299
val cheat_choice =
blanchet@39901
   300
  @{prop "ALL x. EX y. Q x y ==> EX f. ALL x. Q x (f x)"}
blanchet@39901
   301
  |> Logic.varify_global
blanchet@39901
   302
  |> Skip_Proof.make_thm @{theory}
blanchet@39887
   303
blanchet@39887
   304
(* Converts an Isabelle theorem into NNF. *)
blanchet@39901
   305
fun nnf_axiom choice_ths new_skolemizer ax_no th ctxt =
blanchet@39887
   306
  let
wenzelm@42361
   307
    val thy = Proof_Context.theory_of ctxt
blanchet@39887
   308
    val th =
blanchet@39887
   309
      th |> transform_elim_theorem
blanchet@39887
   310
         |> zero_var_indexes
blanchet@39887
   311
         |> new_skolemizer ? forall_intr_vars
blanchet@39887
   312
    val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single
blanchet@39887
   313
    val th = th |> Conv.fconv_rule Object_Logic.atomize
blanchet@42739
   314
                |> Raw_Simplifier.rewrite_rule (unfold_set_const_simps ctxt)
blanchet@42747
   315
                |> extensionalize_theorem ctxt
blanchet@39950
   316
                |> make_nnf ctxt
blanchet@39887
   317
  in
blanchet@39887
   318
    if new_skolemizer then
blanchet@39887
   319
      let
blanchet@39901
   320
        fun skolemize choice_ths =
blanchet@39950
   321
          skolemize_with_choice_theorems ctxt choice_ths
blanchet@39901
   322
          #> simplify (ss_only @{thms all_simps[symmetric]})
blanchet@42347
   323
        val no_choice = null choice_ths
blanchet@39901
   324
        val pull_out =
blanchet@42347
   325
          if no_choice then
blanchet@42347
   326
            simplify (ss_only @{thms all_simps[symmetric] ex_simps[symmetric]})
blanchet@42347
   327
          else
blanchet@42347
   328
            skolemize choice_ths
blanchet@42347
   329
        val discharger_th = th |> pull_out
blanchet@40260
   330
        val discharger_th =
blanchet@42347
   331
          discharger_th |> has_too_many_clauses ctxt (concl_of discharger_th)
blanchet@42347
   332
                           ? (to_definitional_cnf_with_quantifiers ctxt
blanchet@42347
   333
                              #> pull_out)
blanchet@42099
   334
        val zapped_th =
blanchet@40263
   335
          discharger_th |> prop_of |> rename_bound_vars_to_be_zapped ax_no
blanchet@40263
   336
          |> (if no_choice then
blanchet@40263
   337
                Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> cprop_of
blanchet@40263
   338
              else
blanchet@40263
   339
                cterm_of thy)
blanchet@42099
   340
          |> zap true
blanchet@42099
   341
        val fixes =
blanchet@42335
   342
          [] |> Term.add_free_names (prop_of zapped_th)
blanchet@42335
   343
             |> filter is_zapped_var_name
blanchet@42269
   344
        val ctxt' = ctxt |> Variable.add_fixes_direct fixes
blanchet@42099
   345
        val fully_skolemized_t =
blanchet@42333
   346
          zapped_th |> singleton (Variable.export ctxt' ctxt)
blanchet@42333
   347
                    |> cprop_of |> Thm.dest_equals |> snd |> term_of
blanchet@39887
   348
      in
blanchet@39887
   349
        if exists_subterm (fn Var ((s, _), _) =>
blanchet@39887
   350
                              String.isPrefix new_skolem_var_prefix s
blanchet@40260
   351
                            | _ => false) fully_skolemized_t then
blanchet@39887
   352
          let
blanchet@40260
   353
            val (fully_skolemized_ct, ctxt) =
blanchet@40260
   354
              Variable.import_terms true [fully_skolemized_t] ctxt
blanchet@39887
   355
              |>> the_single |>> cterm_of thy
blanchet@40260
   356
          in
blanchet@40260
   357
            (SOME (discharger_th, fully_skolemized_ct),
blanchet@40262
   358
             (Thm.assume fully_skolemized_ct, ctxt))
blanchet@40260
   359
          end
blanchet@39887
   360
       else
blanchet@40262
   361
         (NONE, (th, ctxt))
blanchet@39887
   362
      end
blanchet@39887
   363
    else
blanchet@42347
   364
      (NONE, (th |> has_too_many_clauses ctxt (concl_of th)
blanchet@42347
   365
                    ? to_definitional_cnf_with_quantifiers ctxt, ctxt))
blanchet@39887
   366
  end
blanchet@39887
   367
blanchet@39887
   368
(* Convert a theorem to CNF, with additional premises due to skolemization. *)
blanchet@45508
   369
fun cnf_axiom ctxt0 new_skolemizer combinators ax_no th =
blanchet@37626
   370
  let
wenzelm@42361
   371
    val thy = Proof_Context.theory_of ctxt0
blanchet@39950
   372
    val choice_ths = choice_theorems thy
blanchet@40262
   373
    val (opt, (nnf_th, ctxt)) =
blanchet@40262
   374
      nnf_axiom choice_ths new_skolemizer ax_no th ctxt0
blanchet@39894
   375
    fun clausify th =
blanchet@42347
   376
      make_cnf (if new_skolemizer orelse null choice_ths then []
blanchet@42347
   377
                else map (old_skolem_theorem_from_def thy) (old_skolem_defs th))
blanchet@43964
   378
               th ctxt ctxt
blanchet@42347
   379
    val (cnf_ths, ctxt) = clausify nnf_th
blanchet@39894
   380
    fun intr_imp ct th =
blanchet@39950
   381
      Thm.instantiate ([], map (pairself (cterm_of thy))
blanchet@39962
   382
                               [(Var (("i", 0), @{typ nat}),
blanchet@39902
   383
                                 HOLogic.mk_nat ax_no)])
blanchet@39962
   384
                      (zero_var_indexes @{thm skolem_COMBK_D})
blanchet@39894
   385
      RS Thm.implies_intr ct th
blanchet@37626
   386
  in
blanchet@39897
   387
    (opt |> Option.map (I #>> singleton (Variable.export ctxt ctxt0)
blanchet@39897
   388
                        ##> (term_of #> HOLogic.dest_Trueprop
blanchet@39897
   389
                             #> singleton (Variable.export_terms ctxt ctxt0))),
blanchet@45508
   390
     cnf_ths |> map (combinators ? introduce_combinators_in_theorem
blanchet@39894
   391
                     #> (case opt of SOME (_, ct) => intr_imp ct | NONE => I))
blanchet@39897
   392
             |> Variable.export ctxt ctxt0
blanchet@39950
   393
             |> finish_cnf
blanchet@39887
   394
             |> map Thm.close_derivation)
blanchet@37626
   395
  end
blanchet@39887
   396
  handle THM _ => (NONE, [])
wenzelm@27184
   397
wenzelm@20461
   398
end;