src/HOL/Library/bnf_lfp_countable.ML
author blanchet
Wed Sep 03 22:47:05 2014 +0200 (2014-09-03)
changeset 58165 2ec97d9c1e83
parent 58161 deeff89d5b9e
child 58166 86a374caeb82
permissions -rw-r--r--
'prove_sorry' is too dangerous here -- the tactic is sometimes applied to non-theorems
blanchet@58160
     1
(*  Title:      HOL/Library/bnf_lfp_countable.ML
blanchet@58160
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@58160
     3
    Copyright   2014
blanchet@58160
     4
blanchet@58160
     5
Countability tactic for BNF datatypes.
blanchet@58160
     6
*)
blanchet@58160
     7
blanchet@58160
     8
signature BNF_LFP_COUNTABLE =
blanchet@58160
     9
sig
blanchet@58165
    10
  val derive_encode_injectives_thms: Proof.context -> string list -> thm list
blanchet@58161
    11
  val countable_datatype_tac: Proof.context -> tactic
blanchet@58160
    12
end;
blanchet@58160
    13
blanchet@58160
    14
structure BNF_LFP_Countable : BNF_LFP_COUNTABLE =
blanchet@58160
    15
struct
blanchet@58160
    16
blanchet@58160
    17
open BNF_FP_Rec_Sugar_Util
blanchet@58160
    18
open BNF_Def
blanchet@58160
    19
open BNF_Util
blanchet@58160
    20
open BNF_Tactics
blanchet@58160
    21
open BNF_FP_Util
blanchet@58160
    22
open BNF_FP_Def_Sugar
blanchet@58160
    23
blanchet@58160
    24
fun nchotomy_tac nchotomy =
blanchet@58160
    25
  HEADGOAL (rtac (nchotomy RS @{thm all_reg[rotated]}) THEN'
blanchet@58160
    26
    CHANGED_PROP o REPEAT_ALL_NEW (match_tac [allI, impI]) THEN'
blanchet@58160
    27
    CHANGED_PROP o REPEAT_ALL_NEW (ematch_tac [exE, disjE]));
blanchet@58160
    28
blanchet@58160
    29
fun meta_spec_mp_tac 0 = K all_tac
blanchet@58165
    30
  | meta_spec_mp_tac depth =
blanchet@58165
    31
    dtac meta_spec THEN' meta_spec_mp_tac (depth - 1) THEN' dtac meta_mp THEN' atac;
blanchet@58160
    32
blanchet@58160
    33
val use_induction_hypothesis_tac =
blanchet@58161
    34
  DEEPEN (1, 64 (* large number *))
blanchet@58160
    35
    (fn depth => meta_spec_mp_tac depth THEN' etac allE THEN' etac impE THEN' atac THEN' atac) 0;
blanchet@58160
    36
blanchet@58160
    37
val same_ctr_simps =
blanchet@58165
    38
  @{thms sum_encode_eq prod_encode_eq sum.inject prod.inject to_nat_split snd_conv simp_thms};
blanchet@58160
    39
blanchet@58160
    40
fun same_ctr_tac ctxt injects recs map_comps' inj_map_strongs' =
blanchet@58160
    41
  HEADGOAL (asm_full_simp_tac (ss_only (injects @ recs @ map_comps' @ same_ctr_simps) ctxt) THEN'
blanchet@58160
    42
    REPEAT_DETERM o CHANGED_PROP o REPEAT_ALL_NEW (ematch_tac (conjE :: inj_map_strongs'))
blanchet@58160
    43
    THEN_ALL_NEW use_induction_hypothesis_tac);
blanchet@58160
    44
blanchet@58160
    45
fun distinct_ctrs_tac ctxt recs =
blanchet@58160
    46
  HEADGOAL (asm_full_simp_tac (ss_only (recs @
blanchet@58160
    47
    @{thms sum_encode_eq sum.inject sum.distinct simp_thms}) ctxt));
blanchet@58160
    48
blanchet@58160
    49
fun mk_encode_injective_tac ctxt n nchotomy injects recs map_comps' inj_map_strongs' =
blanchet@58160
    50
  let val ks = 1 upto n in
blanchet@58160
    51
    EVERY (maps (fn k => nchotomy_tac nchotomy :: map (fn k' =>
blanchet@58160
    52
      if k = k' then same_ctr_tac ctxt injects recs map_comps' inj_map_strongs'
blanchet@58160
    53
      else distinct_ctrs_tac ctxt recs) ks) ks)
blanchet@58160
    54
  end;
blanchet@58160
    55
blanchet@58160
    56
fun mk_encode_injectives_tac ctxt ns induct nchotomys injectss recss map_comps' inj_map_strongs' =
blanchet@58160
    57
  HEADGOAL (rtac induct) THEN
blanchet@58160
    58
  EVERY (map4 (fn n => fn nchotomy => fn injects => fn recs =>
blanchet@58160
    59
      mk_encode_injective_tac ctxt n nchotomy injects recs map_comps' inj_map_strongs')
blanchet@58160
    60
    ns nchotomys injectss recss);
blanchet@58160
    61
blanchet@58160
    62
fun endgame_tac ctxt encode_injectives =
blanchet@58160
    63
  unfold_thms_tac ctxt @{thms inj_on_def ball_UNIV} THEN
blanchet@58160
    64
  ALLGOALS (rtac exI THEN' rtac allI THEN' resolve_tac encode_injectives);
blanchet@58160
    65
blanchet@58160
    66
fun encode_sumN n k t =
blanchet@58160
    67
  Balanced_Tree.access {init = t,
blanchet@58160
    68
      left = fn t => @{const sum_encode} $ (@{const Inl (nat, nat)} $ t),
blanchet@58160
    69
      right = fn t => @{const sum_encode} $ (@{const Inr (nat, nat)} $ t)}
blanchet@58160
    70
    n k;
blanchet@58160
    71
blanchet@58160
    72
fun encode_tuple [] = @{term "0 :: nat"}
blanchet@58160
    73
  | encode_tuple ts =
blanchet@58160
    74
    Balanced_Tree.make (fn (t, u) => @{const prod_encode} $ (@{const Pair (nat, nat)} $ u $ t)) ts;
blanchet@58160
    75
blanchet@58160
    76
fun mk_to_nat T = Const (@{const_name to_nat}, T --> HOLogic.natT);
blanchet@58160
    77
blanchet@58160
    78
fun mk_encode_funs ctxt fpTs ns ctrss0 recs0 =
blanchet@58160
    79
  let
blanchet@58160
    80
    val thy = Proof_Context.theory_of ctxt;
blanchet@58160
    81
blanchet@58160
    82
    val nn = length ns;
blanchet@58160
    83
    val recs as rec1 :: _ =
blanchet@58160
    84
      map2 (fn fpT => mk_co_rec thy Least_FP fpT (replicate nn HOLogic.natT)) fpTs recs0;
blanchet@58160
    85
    val arg_Ts = binder_fun_types (fastype_of rec1);
blanchet@58160
    86
    val arg_Tss = Library.unflat ctrss0 arg_Ts;
blanchet@58160
    87
blanchet@58160
    88
    fun mk_U (Type (@{type_name prod}, [T1, T2])) =
blanchet@58160
    89
        if member (op =) fpTs T1 then T2 else HOLogic.mk_prodT (mk_U T1, mk_U T2)
blanchet@58160
    90
      | mk_U (Type (s, Ts)) = Type (s, map mk_U Ts)
blanchet@58160
    91
      | mk_U T = T;
blanchet@58160
    92
blanchet@58160
    93
    fun mk_nat (j, T) =
blanchet@58160
    94
      if T = HOLogic.natT then
blanchet@58160
    95
        SOME (Bound j)
blanchet@58160
    96
      else if member (op =) fpTs T then
blanchet@58160
    97
        NONE
blanchet@58160
    98
      else if exists_subtype_in fpTs T then
blanchet@58160
    99
        let val U = mk_U T in
blanchet@58160
   100
          SOME (mk_to_nat U $ (build_map ctxt [] (snd_const o fst) (T, U) $ Bound j))
blanchet@58160
   101
        end
blanchet@58160
   102
      else
blanchet@58160
   103
        SOME (mk_to_nat T $ Bound j);
blanchet@58160
   104
blanchet@58160
   105
    fun mk_arg n (k, arg_T) =
blanchet@58160
   106
      let
blanchet@58160
   107
        val bound_Ts = rev (binder_types arg_T);
blanchet@58160
   108
        val nats = map_filter mk_nat (tag_list 0 bound_Ts);
blanchet@58160
   109
      in
blanchet@58160
   110
        fold (fn T => fn t => Abs (Name.uu, T, t)) bound_Ts (encode_sumN n k (encode_tuple nats))
blanchet@58160
   111
      end;
blanchet@58160
   112
blanchet@58160
   113
    val argss = map2 (map o mk_arg) ns (map (tag_list 1) arg_Tss);
blanchet@58160
   114
  in
blanchet@58160
   115
    map (fn recx => Term.list_comb (recx, flat argss)) recs
blanchet@58160
   116
  end;
blanchet@58160
   117
blanchet@58165
   118
fun derive_encode_injectives_thms _ [] = []
blanchet@58165
   119
  | derive_encode_injectives_thms ctxt fpT_names0 =
blanchet@58160
   120
    let
blanchet@58160
   121
      fun not_datatype s = error (quote s ^ " is not a new-style datatype");
blanchet@58160
   122
      fun not_mutually_recursive ss =
blanchet@58161
   123
        error ("{" ^ commas ss ^ "} are not mutually recursive new-style datatypes");
blanchet@58160
   124
blanchet@58160
   125
      fun lfp_sugar_of s =
blanchet@58160
   126
        (case fp_sugar_of ctxt s of
blanchet@58160
   127
          SOME (fp_sugar as {fp = Least_FP, ...}) => fp_sugar
blanchet@58160
   128
        | _ => not_datatype s);
blanchet@58160
   129
blanchet@58160
   130
      val fpTs0 as Type (_, var_As) :: _ = #Ts (#fp_res (lfp_sugar_of (hd fpT_names0)));
blanchet@58160
   131
      val fpT_names = map (fst o dest_Type) fpTs0;
blanchet@58160
   132
blanchet@58160
   133
      val (As_names, _) = Variable.variant_fixes (map (fn TVar ((s, _), _) => s) var_As) ctxt;
blanchet@58160
   134
      val As =
blanchet@58160
   135
        map2 (fn s => fn TVar (_, S) => TFree (s, union (op =) @{sort countable} S))
blanchet@58160
   136
          As_names var_As;
blanchet@58160
   137
      val fpTs = map (fn s => Type (s, As)) fpT_names;
blanchet@58160
   138
blanchet@58160
   139
      val _ = subset (op =) (fpT_names0, fpT_names) orelse not_mutually_recursive fpT_names0;
blanchet@58160
   140
blanchet@58160
   141
      fun mk_conjunct fpT x encode_fun =
blanchet@58160
   142
        HOLogic.all_const fpT $ Abs (Name.uu, fpT,
blanchet@58160
   143
          HOLogic.mk_imp (HOLogic.mk_eq (encode_fun $ x, encode_fun $ Bound 0),
blanchet@58160
   144
            HOLogic.eq_const fpT $ x $ Bound 0));
blanchet@58160
   145
blanchet@58160
   146
      val fp_sugars as {fp_nesting_bnfs, common_co_inducts = induct :: _, ...} :: _ =
blanchet@58160
   147
        map (the o fp_sugar_of ctxt o fst o dest_Type) fpTs0;
blanchet@58160
   148
      val ctr_sugars = map #ctr_sugar fp_sugars;
blanchet@58160
   149
blanchet@58160
   150
      val ctrss0 = map #ctrs ctr_sugars;
blanchet@58160
   151
      val ns = map length ctrss0;
blanchet@58160
   152
      val recs0 = map #co_rec fp_sugars;
blanchet@58160
   153
      val nchotomys = map #nchotomy ctr_sugars;
blanchet@58160
   154
      val injectss = map #injects ctr_sugars;
blanchet@58160
   155
      val rec_thmss = map #co_rec_thms fp_sugars;
blanchet@58160
   156
      val map_comps' = map (unfold_thms ctxt @{thms comp_def} o map_comp_of_bnf) fp_nesting_bnfs;
blanchet@58160
   157
      val inj_map_strongs' = map (Thm.permute_prems 0 ~1 o inj_map_strong_of_bnf) fp_nesting_bnfs;
blanchet@58160
   158
blanchet@58160
   159
      val (xs, names_ctxt) = ctxt |> mk_Frees "x" fpTs;
blanchet@58160
   160
blanchet@58160
   161
      val conjuncts = map3 mk_conjunct fpTs xs (mk_encode_funs ctxt fpTs ns ctrss0 recs0);
blanchet@58160
   162
      val goal = HOLogic.mk_Trueprop (Balanced_Tree.make HOLogic.mk_conj conjuncts);
blanchet@58160
   163
    in
blanchet@58165
   164
      Goal.prove ctxt [] [] goal (fn {context = ctxt, prems = _} =>
blanchet@58160
   165
        mk_encode_injectives_tac ctxt ns induct nchotomys injectss rec_thmss map_comps'
blanchet@58161
   166
          inj_map_strongs')
blanchet@58160
   167
      |> HOLogic.conj_elims
blanchet@58160
   168
      |> Proof_Context.export names_ctxt ctxt
blanchet@58160
   169
      |> map Thm.close_derivation
blanchet@58160
   170
    end;
blanchet@58160
   171
blanchet@58165
   172
fun get_countable_goal_type_name (@{const Trueprop} $ (Const (@{const_name Ex}, _)
blanchet@58160
   173
    $ Abs (_, Type (_, [Type (s, _), _]), Const (@{const_name inj_on}, _) $ Bound 0
blanchet@58160
   174
        $ Const (@{const_name top}, _)))) = s
blanchet@58165
   175
  | get_countable_goal_type_name _ = error "Wrong goal format for datatype countability tactic";
blanchet@58160
   176
blanchet@58161
   177
fun core_countable_datatype_tac ctxt st =
blanchet@58165
   178
  let val T_names = map get_countable_goal_type_name (Thm.prems_of st) in
blanchet@58165
   179
    endgame_tac ctxt (derive_encode_injectives_thms ctxt T_names) st
blanchet@58165
   180
  end;
blanchet@58160
   181
blanchet@58161
   182
fun countable_datatype_tac ctxt =
blanchet@58161
   183
  TRY (Class.intro_classes_tac []) THEN core_countable_datatype_tac ctxt;
blanchet@58160
   184
blanchet@58160
   185
end;