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