src/HOL/Tools/Meson/meson_clausify.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59586 ddf6deaadfe8
child 59632 5980e75a204e
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
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
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@59621
    38
val cfalse = Thm.global_cterm_of @{theory HOL} @{term False};
wenzelm@59621
    39
val ctp_false = Thm.global_cterm_of @{theory 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@59582
    47
    @{const Trueprop} $ (v as Var (_, @{typ bool})) =>
wenzelm@59621
    48
      Thm.instantiate ([], [(Thm.global_cterm_of @{theory HOL} v, cfalse)]) th
wenzelm@59582
    49
  | v as Var(_, @{typ prop}) =>
wenzelm@59621
    50
      Thm.instantiate ([], [(Thm.global_cterm_of @{theory HOL} 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
wenzelm@59621
    97
val [f_B,g_B] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_B}));
wenzelm@59621
    98
val [g_C,f_C] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_C}));
wenzelm@59621
    99
val [f_S,g_S] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.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@59582
   104
      val thy = Thm.theory_of_cterm ct
wenzelm@59582
   105
      val Abs(x,_,body) = Thm.term_of ct
wenzelm@59586
   106
      val Type (@{type_name fun}, [xT,bodyT]) = Thm.typ_of_cterm ct
wenzelm@59621
   107
      val cxT = Thm.global_ctyp_of thy xT
wenzelm@59621
   108
      val cbodyT = Thm.global_ctyp_of thy bodyT
blanchet@38005
   109
      fun makeK () =
wenzelm@59621
   110
        instantiate' [SOME cxT, SOME cbodyT] [SOME (Thm.global_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@59621
   121
                 let val crator = Thm.global_cterm_of thy (Abs(x,xT,rator))
wenzelm@59621
   122
                     val crand = Thm.global_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@59582
   124
                     val (_,rhs) = Thm.dest_equals (Thm.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@59621
   129
                 let val crator = Thm.global_cterm_of thy (Abs(x,xT,rator))
wenzelm@59582
   130
                     val abs_C' =
wenzelm@59621
   131
                      cterm_instantiate [(f_C,crator),(g_C,Thm.global_cterm_of thy rand)] @{thm abs_C}
wenzelm@59582
   132
                     val (_,rhs) = Thm.dest_equals (Thm.cprop_of abs_C')
wenzelm@27179
   133
                 in
wenzelm@27179
   134
                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
wenzelm@27179
   135
                 end
wenzelm@42083
   136
            else if Term.is_dependent rand then (*B or eta*)
wenzelm@36945
   137
               if rand = Bound 0 then Thm.eta_conversion ct
wenzelm@27179
   138
               else (*B*)
wenzelm@59621
   139
                 let val crand = Thm.global_cterm_of thy (Abs(x,xT,rand))
wenzelm@59621
   140
                     val crator = Thm.global_cterm_of thy rator
wenzelm@27184
   141
                     val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
wenzelm@59582
   142
                     val (_,rhs) = Thm.dest_equals (Thm.cprop_of abs_B')
blanchet@37349
   143
                 in Thm.transitive abs_B' (Conv.arg_conv abstract rhs) end
wenzelm@27179
   144
            else makeK()
blanchet@37349
   145
        | _ => raise Fail "abstract: Bad term"
paulson@24827
   146
  end;
paulson@20863
   147
blanchet@37349
   148
(* Traverse a theorem, remplacing lambda-abstractions with combinators. *)
blanchet@38001
   149
fun introduce_combinators_in_cterm ct =
wenzelm@59582
   150
  if is_quasi_lambda_free (Thm.term_of ct) then
blanchet@37349
   151
    Thm.reflexive ct
wenzelm@59582
   152
  else case Thm.term_of ct of
blanchet@37349
   153
    Abs _ =>
blanchet@37349
   154
    let
blanchet@37349
   155
      val (cv, cta) = Thm.dest_abs NONE ct
wenzelm@59582
   156
      val (v, _) = dest_Free (Thm.term_of cv)
blanchet@38001
   157
      val u_th = introduce_combinators_in_cterm cta
blanchet@37349
   158
      val cu = Thm.rhs_of u_th
wenzelm@46497
   159
      val comb_eq = abstract (Thm.lambda cv cu)
blanchet@37349
   160
    in Thm.transitive (Thm.abstract_rule v cv u_th) comb_eq end
blanchet@37349
   161
  | _ $ _ =>
blanchet@37349
   162
    let val (ct1, ct2) = Thm.dest_comb ct in
blanchet@38001
   163
        Thm.combination (introduce_combinators_in_cterm ct1)
blanchet@38001
   164
                        (introduce_combinators_in_cterm ct2)
blanchet@37349
   165
    end
blanchet@37349
   166
wenzelm@55236
   167
fun introduce_combinators_in_theorem ctxt th =
wenzelm@59582
   168
  if is_quasi_lambda_free (Thm.prop_of th) then
blanchet@37349
   169
    th
paulson@24827
   170
  else
blanchet@37349
   171
    let
blanchet@37349
   172
      val th = Drule.eta_contraction_rule th
wenzelm@59582
   173
      val eqth = introduce_combinators_in_cterm (Thm.cprop_of th)
blanchet@37349
   174
    in Thm.equal_elim eqth th end
blanchet@37349
   175
    handle THM (msg, _, _) =>
blanchet@55523
   176
           (warning ("Error in the combinator translation of " ^ Display.string_of_thm ctxt th ^
blanchet@55523
   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*)
wenzelm@59621
   182
val cTrueprop = Thm.global_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@37617
   191
(* Given the definition of a Skolem function, return a theorem to replace
blanchet@37617
   192
   an existential formula by a use of that function.
paulson@18141
   193
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
blanchet@57466
   194
fun old_skolem_theorem_of_def ctxt rhs0 =
blanchet@37399
   195
  let
blanchet@57466
   196
    val thy = Proof_Context.theory_of ctxt
wenzelm@59621
   197
    val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> Thm.global_cterm_of thy
blanchet@37617
   198
    val rhs' = rhs |> Thm.dest_comb |> snd
blanchet@37617
   199
    val (ch, frees) = c_variant_abs_multi (rhs', [])
wenzelm@59582
   200
    val (hilbert, cabs) = ch |> Thm.dest_comb |>> Thm.term_of
blanchet@37617
   201
    val T =
blanchet@37617
   202
      case hilbert of
blanchet@39941
   203
        Const (_, Type (@{type_name fun}, [_, T])) => T
blanchet@57466
   204
      | _ => raise TERM ("old_skolem_theorem_of_def: expected \"Eps\"", [hilbert])
wenzelm@59621
   205
    val cex = Thm.global_cterm_of thy (HOLogic.exists_const T)
wenzelm@46497
   206
    val ex_tm = Thm.apply cTrueprop (Thm.apply cex cabs)
blanchet@37629
   207
    val conc =
blanchet@37617
   208
      Drule.list_comb (rhs, frees)
wenzelm@46497
   209
      |> Drule.beta_conv cabs |> Thm.apply cTrueprop
blanchet@37617
   210
    fun tacf [prem] =
wenzelm@54742
   211
      rewrite_goals_tac ctxt @{thms skolem_def [abs_def]}
wenzelm@59498
   212
      THEN resolve_tac ctxt [(prem |> rewrite_rule ctxt @{thms skolem_def [abs_def]})
wenzelm@58839
   213
                 RS Global_Theory.get_thm thy "Hilbert_Choice.someI_ex"] 1
blanchet@37617
   214
  in
wenzelm@54883
   215
    Goal.prove_internal ctxt [ex_tm] conc tacf
blanchet@37629
   216
    |> forall_intr_list frees
blanchet@37629
   217
    |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
blanchet@37629
   218
    |> Thm.varifyT_global
blanchet@37617
   219
  end
paulson@24742
   220
blanchet@42335
   221
fun to_definitional_cnf_with_quantifiers ctxt th =
blanchet@39036
   222
  let
wenzelm@59582
   223
    val eqth = CNF.make_cnfx_thm ctxt (HOLogic.dest_Trueprop (Thm.prop_of th))
blanchet@39036
   224
    val eqth = eqth RS @{thm eq_reflection}
blanchet@39036
   225
    val eqth = eqth RS @{thm TruepropI}
blanchet@39036
   226
  in Thm.equal_elim eqth th end
blanchet@39036
   227
blanchet@39932
   228
fun zapped_var_name ((ax_no, cluster_no), skolem) index_no s =
blanchet@39896
   229
  (if skolem then new_skolem_var_prefix else new_nonskolem_var_prefix) ^
blanchet@39932
   230
  "_" ^ string_of_int ax_no ^ "_" ^ string_of_int cluster_no ^ "_" ^
haftmann@56811
   231
  string_of_int index_no ^ "_" ^ Name.desymbolize (SOME false) s
blanchet@39896
   232
blanchet@39899
   233
fun cluster_of_zapped_var_name s =
blanchet@39932
   234
  let val get_int = the o Int.fromString o nth (space_explode "_" s) in
blanchet@39932
   235
    ((get_int 1, (get_int 2, get_int 3)),
blanchet@39932
   236
     String.isPrefix new_skolem_var_prefix s)
blanchet@39932
   237
  end
blanchet@39897
   238
blanchet@40260
   239
fun rename_bound_vars_to_be_zapped ax_no =
blanchet@40260
   240
  let
blanchet@40260
   241
    fun aux (cluster as (cluster_no, cluster_skolem)) index_no pos t =
blanchet@40260
   242
      case t of
blanchet@40260
   243
        (t1 as Const (s, _)) $ Abs (s', T, t') =>
wenzelm@56245
   244
        if s = @{const_name Pure.all} orelse s = @{const_name All} orelse
blanchet@39906
   245
           s = @{const_name Ex} then
blanchet@39932
   246
          let
blanchet@39932
   247
            val skolem = (pos = (s = @{const_name Ex}))
blanchet@39932
   248
            val (cluster, index_no) =
blanchet@39932
   249
              if skolem = cluster_skolem then (cluster, index_no)
blanchet@39932
   250
              else ((cluster_no ||> cluster_skolem ? Integer.add 1, skolem), 0)
blanchet@40260
   251
            val s' = zapped_var_name cluster index_no s'
blanchet@40260
   252
          in t1 $ Abs (s', T, aux cluster (index_no + 1) pos t') end
blanchet@40260
   253
        else
blanchet@40260
   254
          t
blanchet@40260
   255
      | (t1 as Const (s, _)) $ t2 $ t3 =>
wenzelm@56245
   256
        if s = @{const_name Pure.imp} orelse s = @{const_name HOL.implies} then
blanchet@40260
   257
          t1 $ aux cluster index_no (not pos) t2 $ aux cluster index_no pos t3
blanchet@40260
   258
        else if s = @{const_name HOL.conj} orelse
blanchet@40260
   259
                s = @{const_name HOL.disj} then
blanchet@40260
   260
          t1 $ aux cluster index_no pos t2 $ aux cluster index_no pos t3
blanchet@40260
   261
        else
blanchet@40260
   262
          t
blanchet@40260
   263
      | (t1 as Const (s, _)) $ t2 =>
blanchet@40260
   264
        if s = @{const_name Trueprop} then
blanchet@40260
   265
          t1 $ aux cluster index_no pos t2
blanchet@40260
   266
        else if s = @{const_name Not} then
blanchet@40260
   267
          t1 $ aux cluster index_no (not pos) t2
blanchet@40260
   268
        else
blanchet@40260
   269
          t
blanchet@40260
   270
      | _ => t
blanchet@40260
   271
  in aux ((ax_no, 0), true) 0 true end
blanchet@40260
   272
blanchet@40260
   273
fun zap pos ct =
blanchet@40260
   274
  ct
wenzelm@59582
   275
  |> (case Thm.term_of ct of
blanchet@40260
   276
        Const (s, _) $ Abs (s', _, _) =>
wenzelm@56245
   277
        if s = @{const_name Pure.all} orelse s = @{const_name All} orelse
blanchet@40260
   278
           s = @{const_name Ex} then
blanchet@40260
   279
          Thm.dest_comb #> snd #> Thm.dest_abs (SOME s') #> snd #> zap pos
blanchet@39906
   280
        else
blanchet@39906
   281
          Conv.all_conv
blanchet@39906
   282
      | Const (s, _) $ _ $ _ =>
wenzelm@56245
   283
        if s = @{const_name Pure.imp} orelse s = @{const_name implies} then
blanchet@40260
   284
          Conv.combination_conv (Conv.arg_conv (zap (not pos))) (zap pos)
blanchet@39906
   285
        else if s = @{const_name conj} orelse s = @{const_name disj} then
blanchet@40260
   286
          Conv.combination_conv (Conv.arg_conv (zap pos)) (zap pos)
blanchet@39906
   287
        else
blanchet@39906
   288
          Conv.all_conv
blanchet@39906
   289
      | Const (s, _) $ _ =>
blanchet@40260
   290
        if s = @{const_name Trueprop} then Conv.arg_conv (zap pos)
blanchet@40260
   291
        else if s = @{const_name Not} then Conv.arg_conv (zap (not pos))
blanchet@40260
   292
        else Conv.all_conv
blanchet@39906
   293
      | _ => Conv.all_conv)
blanchet@39887
   294
wenzelm@51717
   295
fun ss_only ths ctxt = clear_simpset (put_simpset HOL_basic_ss ctxt) addsimps ths
blanchet@39901
   296
blanchet@40261
   297
val cheat_choice =
blanchet@39901
   298
  @{prop "ALL x. EX y. Q x y ==> EX f. ALL x. Q x (f x)"}
blanchet@39901
   299
  |> Logic.varify_global
blanchet@39901
   300
  |> Skip_Proof.make_thm @{theory}
blanchet@39887
   301
blanchet@39887
   302
(* Converts an Isabelle theorem into NNF. *)
blanchet@50705
   303
fun nnf_axiom choice_ths new_skolem ax_no th ctxt =
blanchet@39887
   304
  let
wenzelm@42361
   305
    val thy = Proof_Context.theory_of ctxt
blanchet@39887
   306
    val th =
blanchet@39887
   307
      th |> transform_elim_theorem
blanchet@39887
   308
         |> zero_var_indexes
blanchet@50705
   309
         |> new_skolem ? forall_intr_vars
blanchet@39887
   310
    val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single
wenzelm@54742
   311
    val th = th |> Conv.fconv_rule (Object_Logic.atomize ctxt)
blanchet@47954
   312
                |> cong_extensionalize_thm thy
blanchet@47954
   313
                |> abs_extensionalize_thm ctxt
blanchet@47954
   314
                |> make_nnf ctxt
blanchet@39887
   315
  in
blanchet@50705
   316
    if new_skolem then
blanchet@39887
   317
      let
blanchet@39901
   318
        fun skolemize choice_ths =
blanchet@39950
   319
          skolemize_with_choice_theorems ctxt choice_ths
wenzelm@51717
   320
          #> simplify (ss_only @{thms all_simps[symmetric]} ctxt)
blanchet@42347
   321
        val no_choice = null choice_ths
blanchet@39901
   322
        val pull_out =
blanchet@42347
   323
          if no_choice then
wenzelm@51717
   324
            simplify (ss_only @{thms all_simps[symmetric] ex_simps[symmetric]} ctxt)
blanchet@42347
   325
          else
blanchet@42347
   326
            skolemize choice_ths
blanchet@42347
   327
        val discharger_th = th |> pull_out
blanchet@40260
   328
        val discharger_th =
wenzelm@59582
   329
          discharger_th |> has_too_many_clauses ctxt (Thm.concl_of discharger_th)
blanchet@42347
   330
                           ? (to_definitional_cnf_with_quantifiers ctxt
blanchet@42347
   331
                              #> pull_out)
blanchet@42099
   332
        val zapped_th =
wenzelm@59582
   333
          discharger_th |> Thm.prop_of |> rename_bound_vars_to_be_zapped ax_no
blanchet@40263
   334
          |> (if no_choice then
wenzelm@59582
   335
                Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> Thm.cprop_of
blanchet@40263
   336
              else
wenzelm@59621
   337
                Thm.global_cterm_of thy)
blanchet@42099
   338
          |> zap true
blanchet@42099
   339
        val fixes =
wenzelm@59582
   340
          [] |> Term.add_free_names (Thm.prop_of zapped_th)
blanchet@42335
   341
             |> filter is_zapped_var_name
blanchet@42269
   342
        val ctxt' = ctxt |> Variable.add_fixes_direct fixes
blanchet@42099
   343
        val fully_skolemized_t =
blanchet@42333
   344
          zapped_th |> singleton (Variable.export ctxt' ctxt)
wenzelm@59582
   345
                    |> Thm.cprop_of |> Thm.dest_equals |> snd |> Thm.term_of
blanchet@39887
   346
      in
blanchet@39887
   347
        if exists_subterm (fn Var ((s, _), _) =>
blanchet@39887
   348
                              String.isPrefix new_skolem_var_prefix s
blanchet@40260
   349
                            | _ => false) fully_skolemized_t then
blanchet@39887
   350
          let
blanchet@40260
   351
            val (fully_skolemized_ct, ctxt) =
blanchet@40260
   352
              Variable.import_terms true [fully_skolemized_t] ctxt
wenzelm@59621
   353
              |>> the_single |>> Thm.global_cterm_of thy
blanchet@40260
   354
          in
blanchet@40260
   355
            (SOME (discharger_th, fully_skolemized_ct),
blanchet@40262
   356
             (Thm.assume fully_skolemized_ct, ctxt))
blanchet@40260
   357
          end
blanchet@39887
   358
       else
blanchet@40262
   359
         (NONE, (th, ctxt))
blanchet@39887
   360
      end
blanchet@39887
   361
    else
wenzelm@59582
   362
      (NONE, (th |> has_too_many_clauses ctxt (Thm.concl_of th)
blanchet@42347
   363
                    ? to_definitional_cnf_with_quantifiers ctxt, ctxt))
blanchet@39887
   364
  end
blanchet@39887
   365
blanchet@39887
   366
(* Convert a theorem to CNF, with additional premises due to skolemization. *)
blanchet@50705
   367
fun cnf_axiom ctxt0 new_skolem combinators ax_no th =
blanchet@37626
   368
  let
wenzelm@42361
   369
    val thy = Proof_Context.theory_of ctxt0
blanchet@39950
   370
    val choice_ths = choice_theorems thy
blanchet@40262
   371
    val (opt, (nnf_th, ctxt)) =
blanchet@50705
   372
      nnf_axiom choice_ths new_skolem ax_no th ctxt0
blanchet@39894
   373
    fun clausify th =
wenzelm@59165
   374
      make_cnf
wenzelm@59165
   375
       (if new_skolem orelse null choice_ths then []
wenzelm@59165
   376
        else map (old_skolem_theorem_of_def ctxt) (old_skolem_defs th))
wenzelm@59165
   377
       th ctxt
blanchet@42347
   378
    val (cnf_ths, ctxt) = clausify nnf_th
blanchet@39894
   379
    fun intr_imp ct th =
wenzelm@59621
   380
      Thm.instantiate ([], map (apply2 (Thm.global_cterm_of thy))
blanchet@39962
   381
                               [(Var (("i", 0), @{typ nat}),
blanchet@39902
   382
                                 HOLogic.mk_nat ax_no)])
blanchet@39962
   383
                      (zero_var_indexes @{thm skolem_COMBK_D})
blanchet@39894
   384
      RS Thm.implies_intr ct th
blanchet@37626
   385
  in
blanchet@39897
   386
    (opt |> Option.map (I #>> singleton (Variable.export ctxt ctxt0)
wenzelm@59582
   387
                        ##> (Thm.term_of #> HOLogic.dest_Trueprop
blanchet@39897
   388
                             #> singleton (Variable.export_terms ctxt ctxt0))),
wenzelm@55236
   389
     cnf_ths |> map (combinators ? introduce_combinators_in_theorem ctxt
blanchet@39894
   390
                     #> (case opt of SOME (_, ct) => intr_imp ct | NONE => I))
blanchet@39897
   391
             |> Variable.export ctxt ctxt0
blanchet@39950
   392
             |> finish_cnf
blanchet@39887
   393
             |> map Thm.close_derivation)
blanchet@37626
   394
  end
blanchet@39887
   395
  handle THM _ => (NONE, [])
wenzelm@27184
   396
wenzelm@20461
   397
end;