src/HOL/Tools/BNF/bnf_lift.ML
author wenzelm
Sun Nov 26 21:08:32 2017 +0100 (17 months ago)
changeset 67091 1393c2340eec
parent 66272 c6714a9562ae
child 69593 3dda49e08b9d
permissions -rw-r--r--
more symbols;
traytel@60918
     1
(*  Title:      HOL/Tools/BNF/bnf_lift.ML
traytel@60918
     2
    Author:     Julian Biendarra, TU Muenchen
traytel@60918
     3
    Author:     Dmitriy Traytel, ETH Zurich
traytel@60918
     4
    Copyright   2015
traytel@60918
     5
traytel@60918
     6
Lifting of BNFs through typedefs.
traytel@60918
     7
*)
traytel@60918
     8
wenzelm@61067
     9
signature BNF_LIFT =
wenzelm@61067
    10
sig
wenzelm@61067
    11
  datatype lift_bnf_option =
wenzelm@61067
    12
    Plugins_Option of Proof.context -> Plugin_Name.filter
wenzelm@61067
    13
  | No_Warn_Wits
traytel@60918
    14
  val copy_bnf:
traytel@60918
    15
    (((lift_bnf_option list * (binding option * (string * sort option)) list) *
traytel@62324
    16
      string) * thm option) * (binding * binding * binding) ->
traytel@60918
    17
      local_theory -> local_theory
traytel@60918
    18
  val copy_bnf_cmd:
traytel@60918
    19
    (((lift_bnf_option list * (binding option * (string * string option)) list) *
traytel@62324
    20
      string) * (Facts.ref * Token.src list) option) * (binding * binding * binding) ->
traytel@60918
    21
      local_theory -> local_theory
traytel@60918
    22
  val lift_bnf:
traytel@62777
    23
    ((((lift_bnf_option list * (binding option * (string * sort option)) list) *
traytel@62777
    24
      string) * term list option) * thm option) * (binding * binding * binding) ->
traytel@60918
    25
      ({context: Proof.context, prems: thm list} -> tactic) list ->
traytel@60918
    26
      local_theory -> local_theory
traytel@60918
    27
  val lift_bnf_cmd:
traytel@60918
    28
     ((((lift_bnf_option list * (binding option * (string * string option)) list) *
traytel@62324
    29
       string) * string list) * (Facts.ref * Token.src list) option) *
traytel@62324
    30
       (binding * binding * binding) -> local_theory -> Proof.state
wenzelm@61067
    31
end
traytel@60918
    32
wenzelm@61067
    33
structure BNF_Lift : BNF_LIFT =
wenzelm@61067
    34
struct
traytel@60918
    35
traytel@60918
    36
open Ctr_Sugar_Tactics
traytel@60918
    37
open BNF_Util
traytel@60918
    38
open BNF_Comp
traytel@60918
    39
open BNF_Def
traytel@60918
    40
wenzelm@61067
    41
wenzelm@61067
    42
(* typedef_bnf *)
wenzelm@61067
    43
wenzelm@61067
    44
datatype lift_bnf_option =
wenzelm@61067
    45
  Plugins_Option of Proof.context -> Plugin_Name.filter
wenzelm@61067
    46
| No_Warn_Wits;
traytel@60918
    47
traytel@62324
    48
fun typedef_bnf thm wits specs map_b rel_b pred_b opts lthy =
traytel@60918
    49
  let
wenzelm@61073
    50
    val plugins =
wenzelm@61073
    51
      get_first (fn Plugins_Option f => SOME (f lthy) | _ => NONE) (rev opts)
traytel@60918
    52
      |> the_default Plugin_Name.default_filter;
wenzelm@61072
    53
    val no_warn_wits = exists (fn No_Warn_Wits => true | _ => false) opts;
traytel@60918
    54
traytel@60918
    55
    (* extract Rep Abs F RepT AbsT *)
wenzelm@61073
    56
    val (_, [Rep_G, Abs_G, F]) = Term.strip_comb (HOLogic.dest_Trueprop (Thm.prop_of thm));
wenzelm@61073
    57
    val typ_Abs_G = dest_funT (fastype_of Abs_G);
traytel@60918
    58
    val RepT = fst typ_Abs_G; (* F *)
traytel@60918
    59
    val AbsT = snd typ_Abs_G; (* G *)
traytel@60918
    60
    val AbsT_name = fst (dest_Type AbsT);
traytel@60918
    61
    val tvs = AbsT |> dest_Type |> snd |> map (fst o dest_TVar);
traytel@60918
    62
    val alpha0s = map (TFree o snd) specs;
wenzelm@61067
    63
blanchet@62690
    64
    val _ = length tvs = length alpha0s orelse
blanchet@62690
    65
      error ("Expected " ^ string_of_int (length tvs) ^ " type argument(s) to " ^ quote AbsT_name);
blanchet@62690
    66
traytel@60918
    67
    (* instantiate the new type variables newtvs to oldtvs *)
traytel@60918
    68
    val subst = subst_TVars (tvs ~~ alpha0s);
traytel@60918
    69
    val typ_subst = typ_subst_TVars (tvs ~~ alpha0s);
traytel@60918
    70
traytel@60918
    71
    val Rep_G = subst Rep_G;
traytel@60918
    72
    val Abs_G = subst Abs_G;
traytel@60918
    73
    val F = subst F;
traytel@60918
    74
    val RepT = typ_subst RepT;
traytel@60918
    75
    val AbsT = typ_subst AbsT;
traytel@60918
    76
wenzelm@61073
    77
    fun flatten_tyargs Ass =
wenzelm@61073
    78
      map dest_TFree alpha0s
wenzelm@61073
    79
      |> filter (fn T => exists (fn Ts => member (op =) Ts T) Ass);
traytel@60918
    80
traytel@60918
    81
    val Ds0 = filter (is_none o fst) specs |> map snd;
traytel@60918
    82
traytel@60918
    83
    (* get the bnf for RepT *)
traytel@60918
    84
    val ((bnf, (deads, alphas)),((_, unfolds), lthy)) =
blanchet@62621
    85
      bnf_of_typ true Dont_Inline (Binding.qualify true AbsT_name) flatten_tyargs []
traytel@60918
    86
        Ds0 RepT ((empty_comp_cache, empty_unfolds), lthy);
traytel@60918
    87
wenzelm@61073
    88
    val set_bs =
wenzelm@61073
    89
      map (fn T => find_index (fn U => T = U) alpha0s) alphas
traytel@60918
    90
      |> map (the_default Binding.empty o fst o nth specs);
traytel@60918
    91
wenzelm@61067
    92
    val _ = (case alphas of [] => error "No live variables" | _ => ());
traytel@60918
    93
traytel@60918
    94
    val defs = #map_unfolds unfolds @ flat (#set_unfoldss unfolds) @ #rel_unfolds unfolds;
traytel@60918
    95
traytel@60918
    96
    (* number of live variables *)
traytel@60918
    97
    val lives = length alphas;
traytel@60918
    98
traytel@60918
    99
    (* state the three required properties *)
traytel@60918
   100
    val sorts = map Type.sort_of_atyp alphas;
traytel@60918
   101
    val names_lthy = fold Variable.declare_typ (alphas @ deads) lthy;
traytel@60918
   102
    val (alphas', names_lthy) = mk_TFrees' sorts names_lthy;
traytel@60918
   103
    val (betas, names_lthy) = mk_TFrees' sorts names_lthy;
traytel@60918
   104
traytel@60918
   105
    val map_F = mk_map_of_bnf deads alphas betas bnf;
traytel@60918
   106
traytel@60918
   107
    val (typ_fs, typ_aF) = fastype_of map_F |> strip_typeN lives ||> domain_type;
traytel@60918
   108
    val typ_pairs = map HOLogic.mk_prodT (alphas ~~ alphas');
traytel@60918
   109
    val typ_subst_pair = typ_subst_atomic (alphas ~~ typ_pairs);
traytel@60918
   110
    val typ_pair = typ_subst_pair RepT;
traytel@60918
   111
traytel@60918
   112
    val subst_b = subst_atomic_types (alphas ~~ betas);
traytel@60918
   113
    val subst_a' = subst_atomic_types (alphas ~~ alphas');
traytel@60918
   114
    val subst_pair = subst_atomic_types (alphas ~~ typ_pairs);
traytel@60918
   115
    val aF_set = F;
traytel@60918
   116
    val bF_set = subst_b F;
traytel@60918
   117
    val aF_set' = subst_a' F;
traytel@60918
   118
    val pairF_set = subst_pair F;
traytel@60918
   119
    val map_F_fst = mk_map_of_bnf deads typ_pairs alphas bnf;
traytel@60918
   120
    val map_F_snd = mk_map_of_bnf deads typ_pairs alphas' bnf;
traytel@60918
   121
    val wits_F = mk_wits_of_bnf
traytel@60918
   122
      (replicate (nwits_of_bnf bnf) deads)
traytel@60918
   123
      (replicate (nwits_of_bnf bnf) alphas) bnf;
traytel@60918
   124
traytel@60918
   125
    (* val map_closed_F = @{term "\<And>f x. x \<in> F \<Longrightarrow> map_F f x \<in> F"}; *)
traytel@60918
   126
    val (var_fs, names_lthy) = mk_Frees "f" typ_fs names_lthy;
traytel@60918
   127
    val (var_x, names_lthy) = mk_Frees "x" [typ_aF] names_lthy |>> the_single;
wenzelm@61073
   128
    val mem_x = HOLogic.mk_Trueprop (HOLogic.mk_mem (var_x, aF_set));
traytel@60918
   129
    val map_f = list_comb (map_F, var_fs);
wenzelm@61073
   130
    val mem_map = HOLogic.mk_Trueprop (HOLogic.mk_mem (map_f $ var_x, bF_set));
traytel@60918
   131
    val imp_map = Logic.mk_implies (mem_x, mem_map);
wenzelm@61073
   132
    val map_closed_F = fold_rev Logic.all var_fs (Logic.all var_x imp_map);
traytel@60918
   133
traytel@60918
   134
    (* val zip_closed_F = @{term "\<And>z. map_F fst z \<in> F \<Longrightarrow> map_F snd z \<in> F \<Longrightarrow> z \<in> F"}; *)
traytel@60918
   135
    val (var_zs, names_lthy) = mk_Frees "z" [typ_pair] names_lthy;
traytel@60918
   136
    val (pairs, names_lthy) = mk_Frees "tmp" typ_pairs names_lthy;
traytel@60918
   137
    val var_z = hd var_zs;
traytel@60918
   138
    val fsts = map (fst o Term.strip_comb o HOLogic.mk_fst) pairs;
traytel@60918
   139
    val snds = map (fst o Term.strip_comb o HOLogic.mk_snd) pairs;
traytel@60918
   140
    val map_fst = list_comb (list_comb (map_F_fst, fsts), var_zs);
wenzelm@61073
   141
    val mem_map_fst = HOLogic.mk_Trueprop (HOLogic.mk_mem (map_fst, aF_set));
traytel@60918
   142
    val map_snd = list_comb (list_comb (map_F_snd, snds), var_zs);
wenzelm@61073
   143
    val mem_map_snd = HOLogic.mk_Trueprop (HOLogic.mk_mem (map_snd, aF_set'));
wenzelm@61073
   144
    val mem_z = HOLogic.mk_Trueprop (HOLogic.mk_mem (var_z, pairF_set));
traytel@60918
   145
    val imp_zip = Logic.mk_implies (mem_map_fst, Logic.mk_implies (mem_map_snd, mem_z));
traytel@60918
   146
    val zip_closed_F = Logic.all var_z imp_zip;
traytel@60918
   147
traytel@60918
   148
    (* val wit_closed_F = @{term "wit_F a \<in> F"}; *)
traytel@60918
   149
    val (var_as, names_lthy) = mk_Frees "a" alphas names_lthy;
traytel@60918
   150
    val (var_bs, _) = mk_Frees "a" alphas names_lthy;
traytel@62137
   151
    fun binder_types_until_eq V T =
traytel@62137
   152
      let
traytel@62137
   153
        fun strip (TU as Type ("fun", [T, U])) = if V = TU then [] else T :: strip U
traytel@62137
   154
          | strip T = if V = T then [] else
traytel@62137
   155
              error ("Bad type for witness: " ^ quote (Syntax.string_of_typ lthy T));
traytel@62137
   156
      in strip T end;
traytel@60918
   157
    val Iwits = the_default wits_F (Option.map (map (`(map (fn T =>
traytel@62137
   158
      find_index (fn U => T = U) alphas) o binder_types_until_eq RepT o fastype_of))) wits);
traytel@60918
   159
    val wit_closed_Fs =
wenzelm@61067
   160
      Iwits |> map (fn (I, wit_F) =>
traytel@60918
   161
        let
traytel@60918
   162
          val vars = map (nth var_as) I;
traytel@60918
   163
          val wit_a = list_comb (wit_F, vars);
wenzelm@61073
   164
        in fold_rev Logic.all vars (HOLogic.mk_Trueprop (HOLogic.mk_mem (wit_a, aF_set))) end);
traytel@60918
   165
traytel@60918
   166
    val mk_wit_goals = mk_wit_goals var_as var_bs
traytel@60918
   167
      (mk_sets_of_bnf (replicate lives deads)  (replicate lives alphas) bnf);
traytel@60918
   168
traytel@60918
   169
    val goals = [map_closed_F, zip_closed_F] @ wit_closed_Fs @
traytel@60918
   170
      (case wits of NONE => [] | _ => maps mk_wit_goals Iwits);
traytel@60918
   171
traytel@60918
   172
    val lost_wits = filter_out (fn (J, _) => exists (fn (I, _) => I = J) Iwits) wits_F;
wenzelm@61067
   173
    val _ =
wenzelm@61067
   174
      if null lost_wits orelse no_warn_wits then ()
wenzelm@61067
   175
      else
wenzelm@61067
   176
        lost_wits
wenzelm@61067
   177
        |> map (Syntax.pretty_typ lthy o fastype_of o snd)
wenzelm@61067
   178
        |> Pretty.big_list
wenzelm@61067
   179
          "The following types of nonemptiness witnesses of the raw type's BNF were lost:"
wenzelm@61067
   180
        |> (fn pt => Pretty.chunks [pt,
wenzelm@61067
   181
          Pretty.para "You can specify a liftable witness (e.g., a term of one of the above types\
wenzelm@61067
   182
            \ that satisfies the typedef's invariant)\
wenzelm@61067
   183
            \ using the annotation [wits: <term>]."])
wenzelm@61067
   184
        |> Pretty.string_of
wenzelm@61067
   185
        |> warning;
traytel@60918
   186
traytel@60918
   187
    fun after_qed ([map_closed_thm] :: [zip_closed_thm] :: wit_thmss) lthy =
wenzelm@61067
   188
          let
wenzelm@61067
   189
            val (wit_closed_thms, wit_thms) =
wenzelm@61067
   190
              (case wits of
wenzelm@61067
   191
                NONE => (map the_single wit_thmss, wit_thms_of_bnf bnf)
wenzelm@61067
   192
              | _ => chop (length wit_closed_Fs) (map the_single wit_thmss))
traytel@60918
   193
wenzelm@61067
   194
            (*  construct map set bd rel wit *)
wenzelm@61067
   195
            (* val map_G = @{term "\<lambda>f. Abs_G o map_F f o Rep_G"}; *)
wenzelm@61067
   196
            val Abs_Gb = subst_b Abs_G;
wenzelm@61073
   197
            val map_G =
wenzelm@61073
   198
              fold_rev HOLogic.tupled_lambda var_fs
wenzelm@61073
   199
                (HOLogic.mk_comp (HOLogic.mk_comp (Abs_Gb, map_f), Rep_G));
traytel@60918
   200
wenzelm@61067
   201
            (* val sets_G = [@{term "set_F o Rep_G"}]; *)
wenzelm@61067
   202
            val sets_F = mk_sets_of_bnf (replicate lives deads) (replicate lives alphas) bnf;
wenzelm@61067
   203
            val sets_G = map (fn set_F => HOLogic.mk_comp (set_F, Rep_G)) sets_F;
traytel@60918
   204
wenzelm@61067
   205
            (* val bd_G = @{term "bd_F"}; *)
wenzelm@61067
   206
            val bd_F = mk_bd_of_bnf deads alphas bnf;
wenzelm@61067
   207
            val bd_G = bd_F;
wenzelm@61067
   208
wenzelm@61067
   209
            (* val rel_G = @{term "\<lambda>R. BNF_Def.vimage2p Rep_G Rep_G (rel_F R)"}; *)
wenzelm@61067
   210
            val rel_F = mk_rel_of_bnf deads alphas betas bnf;
wenzelm@61073
   211
            val (typ_Rs, _) = strip_typeN lives (fastype_of rel_F);
traytel@60918
   212
wenzelm@61067
   213
            val (var_Rs, names_lthy) = mk_Frees "R" typ_Rs lthy;
wenzelm@61067
   214
            val Rep_Gb = subst_b Rep_G;
wenzelm@61067
   215
            val rel_G = fold_rev absfree (map dest_Free var_Rs)
wenzelm@61067
   216
              (mk_vimage2p Rep_G Rep_Gb $ list_comb (rel_F, var_Rs));
traytel@60918
   217
traytel@62324
   218
            (* val pred_G = @{term "\<lambda>P. pred_F P o Rep_G"}; *)
traytel@62324
   219
            val pred_F = mk_pred_of_bnf deads alphas bnf;
traytel@62324
   220
            val (typ_Ps, _) = strip_typeN lives (fastype_of pred_F);
traytel@62324
   221
traytel@62324
   222
            val (var_Ps, names_lthy) = mk_Frees "P" typ_Ps names_lthy;
traytel@62324
   223
            val pred_G = fold_rev absfree (map dest_Free var_Ps)
traytel@62324
   224
              (HOLogic.mk_comp (list_comb (pred_F, var_Ps), Rep_G));
traytel@62324
   225
wenzelm@61067
   226
            (* val wits_G = [@{term "Abs_G o wit_F"}]; *)
wenzelm@61067
   227
            val (var_as, _) = mk_Frees "a" alphas names_lthy;
wenzelm@61067
   228
            val wits_G =
wenzelm@61067
   229
              map (fn (I, wit_F) =>
wenzelm@61067
   230
                let
wenzelm@61067
   231
                  val vs = map (nth var_as) I;
wenzelm@61067
   232
                in fold_rev absfree (map dest_Free vs) (Abs_G $ (list_comb (wit_F, vs))) end)
wenzelm@61067
   233
              Iwits;
traytel@60918
   234
wenzelm@61067
   235
            (* tactics *)
wenzelm@61067
   236
            val Rep_thm = thm RS @{thm type_definition.Rep};
wenzelm@61067
   237
            val Abs_inverse_thm = thm RS @{thm type_definition.Abs_inverse};
wenzelm@61067
   238
            val Abs_inject_thm = thm RS @{thm type_definition.Abs_inject};
wenzelm@61067
   239
            val Rep_cases_thm = thm RS @{thm type_definition.Rep_cases};
wenzelm@61067
   240
            val Rep_inverse_thm = thm RS @{thm type_definition.Rep_inverse};
traytel@60918
   241
wenzelm@61067
   242
            fun map_id0_tac ctxt =
wenzelm@61067
   243
              HEADGOAL (EVERY' [rtac ctxt ext,
wenzelm@61067
   244
                SELECT_GOAL (unfold_thms_tac ctxt [map_id0_of_bnf bnf, id_apply, o_apply,
wenzelm@61067
   245
                  Rep_inverse_thm]),
wenzelm@61067
   246
                rtac ctxt refl]);
traytel@60918
   247
wenzelm@61067
   248
            fun map_comp0_tac ctxt =
traytel@60918
   249
              HEADGOAL (EVERY' [rtac ctxt ext,
wenzelm@61067
   250
                SELECT_GOAL (unfold_thms_tac ctxt [map_comp0_of_bnf bnf, o_apply,
traytel@60918
   251
                  Rep_thm RS (map_closed_thm RS Abs_inverse_thm)]),
wenzelm@61067
   252
                rtac ctxt refl]);
traytel@60918
   253
wenzelm@61067
   254
            fun map_cong0_tac ctxt =
wenzelm@61067
   255
              HEADGOAL (EVERY' ([SELECT_GOAL (unfold_thms_tac ctxt [o_apply]),
wenzelm@61067
   256
                rtac ctxt (([Rep_thm RS map_closed_thm, Rep_thm RS map_closed_thm] MRS
wenzelm@61067
   257
                  Abs_inject_thm) RS iffD2),
wenzelm@61067
   258
                rtac ctxt (map_cong0_of_bnf bnf)] @ replicate lives (Goal.assume_rule_tac ctxt)));
traytel@60918
   259
wenzelm@61067
   260
            val set_map0s_tac =
wenzelm@61067
   261
              map (fn set_map => fn ctxt =>
wenzelm@61067
   262
                HEADGOAL (EVERY' [rtac ctxt ext,
wenzelm@61067
   263
                  SELECT_GOAL (unfold_thms_tac ctxt [set_map, o_apply,
wenzelm@61067
   264
                    Rep_thm RS (map_closed_thm RS Abs_inverse_thm)]),
wenzelm@61067
   265
                  rtac ctxt refl]))
wenzelm@61067
   266
             (set_map_of_bnf bnf);
wenzelm@61067
   267
wenzelm@61067
   268
            fun card_order_bd_tac ctxt = HEADGOAL (rtac ctxt (bd_card_order_of_bnf bnf));
traytel@60918
   269
wenzelm@61067
   270
            fun cinfinite_bd_tac ctxt = HEADGOAL (rtac ctxt (bd_cinfinite_of_bnf bnf));
wenzelm@61067
   271
wenzelm@61067
   272
            val set_bds_tac =
wenzelm@61067
   273
              map (fn set_bd => fn ctxt =>
wenzelm@61067
   274
                HEADGOAL (EVERY' [SELECT_GOAL (unfold_thms_tac ctxt [o_apply]), rtac ctxt set_bd]))
wenzelm@61067
   275
              (set_bd_of_bnf bnf);
wenzelm@61067
   276
wenzelm@61067
   277
            fun le_rel_OO_tac ctxt =
wenzelm@61067
   278
              HEADGOAL (EVERY' [rtac ctxt @{thm vimage2p_relcompp_mono},
wenzelm@61067
   279
                rtac ctxt ((rel_OO_of_bnf bnf RS sym) RS @{thm ord_eq_le_trans}),
wenzelm@61067
   280
                rtac ctxt @{thm order_refl}]);
traytel@60918
   281
wenzelm@61067
   282
            fun rel_OO_Grp_tac ctxt =
wenzelm@61067
   283
              HEADGOAL (EVERY' ([SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt ext))),
wenzelm@61067
   284
                SELECT_GOAL (unfold_thms_tac ctxt [@{thm OO_Grp_alt}, mem_Collect_eq,
wenzelm@61067
   285
                  o_apply, @{thm vimage2p_def}, in_rel_of_bnf bnf, Bex_def, mem_Collect_eq]),
wenzelm@61067
   286
                rtac ctxt iffI,
wenzelm@61067
   287
                SELECT_GOAL (REPEAT_DETERM (HEADGOAL (eresolve0_tac [exE,conjE]))),
wenzelm@61067
   288
                rtac ctxt (zip_closed_thm OF (replicate 2 (Rep_thm RSN (2, @{thm ssubst_mem}))) RS
wenzelm@61067
   289
                  Rep_cases_thm),
wenzelm@61067
   290
                assume_tac ctxt,
wenzelm@61067
   291
                assume_tac ctxt,
wenzelm@61067
   292
                hyp_subst_tac ctxt,
wenzelm@61067
   293
                SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt exI))),
wenzelm@61067
   294
                rtac ctxt conjI] @
wenzelm@61067
   295
                replicate (lives - 1) (rtac ctxt conjI THEN' assume_tac ctxt) @
wenzelm@61067
   296
                [assume_tac ctxt,
wenzelm@61067
   297
                SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt conjI))),
wenzelm@61067
   298
                REPEAT_DETERM_N 2 o
wenzelm@61067
   299
                  etac ctxt (trans OF [iffD2 OF [Abs_inject_thm OF
wenzelm@61067
   300
                    [map_closed_thm OF [Rep_thm], Rep_thm]], Rep_inverse_thm]),
wenzelm@61067
   301
                SELECT_GOAL (REPEAT_DETERM (HEADGOAL (eresolve0_tac [exE,conjE]))),
wenzelm@61067
   302
                rtac ctxt exI,
wenzelm@61067
   303
                rtac ctxt conjI] @
wenzelm@61067
   304
                replicate (lives - 1) (rtac ctxt conjI THEN' assume_tac ctxt) @
wenzelm@61067
   305
                [assume_tac ctxt,
wenzelm@61067
   306
                rtac ctxt conjI,
wenzelm@61067
   307
                REPEAT_DETERM_N 2 o EVERY'
wenzelm@61067
   308
                  [rtac ctxt (iffD1 OF [Abs_inject_thm OF [map_closed_thm OF [Rep_thm], Rep_thm]]),
wenzelm@61067
   309
                  etac ctxt (Rep_inverse_thm RS sym RSN (2, trans))]]));
traytel@60918
   310
traytel@62324
   311
            fun pred_set_tac ctxt =
traytel@62324
   312
              HEADGOAL (EVERY'
wenzelm@67091
   313
                [rtac ctxt (pred_set_of_bnf bnf RS @{thm arg_cong[of _ _ "\<lambda>f. f \<circ> _"]} RS trans),
traytel@62324
   314
                SELECT_GOAL (unfold_thms_tac ctxt (@{thms Ball_comp_iff conj_comp_iff})),
traytel@62324
   315
                rtac ctxt refl]);
traytel@62324
   316
wenzelm@61067
   317
            fun wit_tac ctxt =
wenzelm@61067
   318
              HEADGOAL (EVERY'
wenzelm@61067
   319
                (map (fn thm => (EVERY'
wenzelm@61067
   320
                  [SELECT_GOAL (unfold_thms_tac ctxt (o_apply ::
wenzelm@61067
   321
                    (wit_closed_thms RL [Abs_inverse_thm]))),
wenzelm@61067
   322
                  dtac ctxt thm, assume_tac ctxt]))
wenzelm@61067
   323
                wit_thms));
traytel@60918
   324
wenzelm@61067
   325
            val tactics = [map_id0_tac, map_comp0_tac, map_cong0_tac] @ set_map0s_tac @
traytel@62324
   326
              [card_order_bd_tac, cinfinite_bd_tac] @ set_bds_tac @
traytel@62324
   327
              [le_rel_OO_tac, rel_OO_Grp_tac, pred_set_tac];
traytel@60918
   328
traytel@66272
   329
            val (bnf, lthy) = bnf_def Dont_Inline (user_policy Note_Some) true I
traytel@62324
   330
              tactics wit_tac NONE map_b rel_b pred_b set_bs
traytel@62324
   331
              (((((((Binding.empty, AbsT), map_G), sets_G), bd_G), wits_G), SOME rel_G), SOME pred_G)
wenzelm@61067
   332
              lthy;
traytel@60928
   333
traytel@66272
   334
            val (bnf, lthy) =
traytel@66272
   335
              morph_bnf_defs (Morphism.thm_morphism "BNF" (unfold_thms lthy defs)) bnf
traytel@66272
   336
              |> (fn bnf => note_bnf_defs bnf lthy);
wenzelm@61067
   337
          in
wenzelm@61067
   338
            lthy |> BNF_Def.register_bnf plugins AbsT_name bnf
wenzelm@61067
   339
          end
wenzelm@61067
   340
      | after_qed _ _ = raise Match;
traytel@60918
   341
  in
traytel@60918
   342
    (goals, after_qed, defs, lthy)
traytel@60918
   343
  end;
traytel@60918
   344
wenzelm@61067
   345
wenzelm@61067
   346
(* main commands *)
wenzelm@61067
   347
wenzelm@61067
   348
local
wenzelm@61067
   349
traytel@60918
   350
fun prepare_common prepare_name prepare_sort prepare_term prepare_thm
traytel@62324
   351
    (((((plugins, raw_specs), raw_Tname), raw_wits), xthm_opt), (map_b, rel_b, pred_b)) lthy =
traytel@60918
   352
  let
traytel@60918
   353
    val Tname = prepare_name lthy raw_Tname;
traytel@60918
   354
    val input_thm =
traytel@60918
   355
      (case xthm_opt of
traytel@60918
   356
        SOME xthm => prepare_thm lthy xthm
traytel@60918
   357
      | NONE => Typedef.get_info lthy Tname |> hd |> snd |> #type_definition);
wenzelm@61067
   358
    val wits = (Option.map o map) (prepare_term lthy) raw_wits;
wenzelm@61067
   359
    val specs =
wenzelm@61067
   360
      map (apsnd (apsnd (the_default @{sort type} o Option.map (prepare_sort lthy)))) raw_specs;
traytel@60918
   361
wenzelm@61067
   362
    val _ =
wenzelm@61067
   363
      (case HOLogic.dest_Trueprop (Thm.prop_of input_thm) of
wenzelm@61067
   364
        Const (@{const_name type_definition}, _) $ _ $ _ $ _ => ()
wenzelm@61067
   365
      | _ => error "Unsupported type of a theorem: only type_definition is supported");
traytel@60918
   366
  in
traytel@62324
   367
    typedef_bnf input_thm wits specs map_b rel_b pred_b plugins lthy
traytel@60918
   368
  end;
traytel@60918
   369
traytel@60918
   370
fun prepare_lift_bnf prepare_name prepare_sort prepare_term prepare_thm =
traytel@60918
   371
  (fn (goals, after_qed, definitions, lthy) =>
traytel@60918
   372
    lthy
traytel@60918
   373
    |> Proof.theorem NONE after_qed (map (single o rpair []) goals)
wenzelm@61841
   374
    |> Proof.refine_singleton
wenzelm@61841
   375
        (Method.Basic (fn ctxt => SIMPLE_METHOD (unfold_thms_tac ctxt definitions)))
wenzelm@61841
   376
    |> Proof.refine_singleton (Method.primitive_text (K I))) oo
traytel@60918
   377
  prepare_common prepare_name prepare_sort prepare_term prepare_thm o apfst (apfst (apsnd SOME));
traytel@60918
   378
traytel@60918
   379
fun prepare_solve prepare_name prepare_typ prepare_sort prepare_thm tacs =
traytel@63023
   380
  (fn (goals, after_qed, definitions, lthy) =>
traytel@60918
   381
    lthy
traytel@63023
   382
    |> after_qed (map2 (fn goal => fn tac => [Goal.prove lthy [] [] goal
traytel@63023
   383
        (fn (ctxtprems as {context = ctxt, prems = _}) =>
traytel@63023
   384
          unfold_thms_tac ctxt definitions THEN tac ctxtprems)])
traytel@63023
   385
      goals (tacs (length goals)))) oo
traytel@62777
   386
  prepare_common prepare_name prepare_typ prepare_sort prepare_thm;
traytel@60918
   387
wenzelm@61067
   388
in
wenzelm@61067
   389
wenzelm@61067
   390
val lift_bnf_cmd =
wenzelm@61067
   391
  prepare_lift_bnf
wenzelm@61067
   392
    (fst o dest_Type oo Proof_Context.read_type_name {proper = true, strict = false})
wenzelm@61067
   393
    Syntax.read_sort Syntax.read_term (singleton o Attrib.eval_thms);
wenzelm@61067
   394
traytel@62777
   395
fun lift_bnf args tacs =
traytel@62777
   396
  prepare_solve (K I) (K I) (K I) (K I) (K tacs) args;
wenzelm@61067
   397
wenzelm@61067
   398
val copy_bnf =
traytel@62777
   399
  apfst (apfst (rpair NONE))
traytel@62777
   400
  #> prepare_solve (K I) (K I) (K I) (K I)
wenzelm@61067
   401
    (fn n => replicate n (fn {context = ctxt, prems = _} => rtac ctxt UNIV_I 1));
wenzelm@61067
   402
wenzelm@61067
   403
val copy_bnf_cmd =
traytel@62777
   404
  apfst (apfst (rpair NONE))
traytel@62777
   405
  #> prepare_solve
wenzelm@61067
   406
    (fst o dest_Type oo Proof_Context.read_type_name {proper = true, strict = false})
wenzelm@61067
   407
    Syntax.read_sort Syntax.read_term (singleton o Attrib.eval_thms)
wenzelm@61067
   408
    (fn n => replicate n (fn {context = ctxt, prems = _} => rtac ctxt UNIV_I 1));
wenzelm@61067
   409
wenzelm@61067
   410
end;
wenzelm@61067
   411
wenzelm@61067
   412
wenzelm@61067
   413
(* outer syntax *)
wenzelm@61067
   414
wenzelm@61067
   415
local
traytel@60918
   416
traytel@60918
   417
val parse_wits =
traytel@60918
   418
  @{keyword "["} |-- (Parse.name --| @{keyword ":"} -- Scan.repeat Parse.term >>
traytel@60918
   419
    (fn ("wits", Ts) => Ts
traytel@60918
   420
      | (s, _) => error ("Unknown label " ^ quote s ^ " (expected \"wits\")"))) --|
traytel@60918
   421
  @{keyword "]"} || Scan.succeed [];
traytel@60918
   422
traytel@60918
   423
val parse_options =
traytel@60918
   424
  Scan.optional (@{keyword "("} |--
traytel@60918
   425
    Parse.list1 (Parse.group (K "option")
traytel@60918
   426
      (Plugin_Name.parse_filter >> Plugins_Option
traytel@60918
   427
      || Parse.reserved "no_warn_wits" >> K No_Warn_Wits))
traytel@60918
   428
    --| @{keyword ")"}) [];
traytel@60918
   429
traytel@60918
   430
val parse_plugins =
traytel@60918
   431
  Scan.optional (@{keyword "("} |-- Plugin_Name.parse_filter --| @{keyword ")"})
traytel@60918
   432
    (K Plugin_Name.default_filter) >> Plugins_Option >> single;
traytel@60918
   433
wenzelm@62969
   434
val parse_typedef_thm = Scan.option (Parse.reserved "via" |-- Parse.thm);
traytel@60918
   435
wenzelm@61067
   436
in
wenzelm@61067
   437
traytel@60918
   438
val _ =
traytel@60918
   439
  Outer_Syntax.local_theory_to_proof @{command_keyword lift_bnf}
traytel@60918
   440
    "register a subtype of a bounded natural functor (BNF) as a BNF"
traytel@60918
   441
    ((parse_options -- parse_type_args_named_constrained -- Parse.type_const -- parse_wits --
traytel@62324
   442
      parse_typedef_thm -- parse_map_rel_pred_bindings) >> lift_bnf_cmd);
traytel@60918
   443
traytel@60918
   444
val _ =
traytel@60918
   445
  Outer_Syntax.local_theory @{command_keyword copy_bnf}
traytel@60918
   446
    "register a type copy of a bounded natural functor (BNF) as a BNF"
traytel@60918
   447
    ((parse_plugins -- parse_type_args_named_constrained -- Parse.type_const --
traytel@62324
   448
      parse_typedef_thm -- parse_map_rel_pred_bindings) >> copy_bnf_cmd);
traytel@60918
   449
wenzelm@61067
   450
end;
wenzelm@61067
   451
wenzelm@61067
   452
end;