src/HOL/Library/Countable.thy
author wenzelm
Sat Jul 18 20:47:08 2015 +0200 (2015-07-18)
changeset 60752 b48830b670a1
parent 60500 903bb1495239
child 60801 7664e0916eec
permissions -rw-r--r--
prefer tactics with explicit context;
     1 (*  Title:      HOL/Library/Countable.thy
     2     Author:     Alexander Krauss, TU Muenchen
     3     Author:     Brian Huffman, Portland State University
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 *)
     6 
     7 section \<open>Encoding (almost) everything into natural numbers\<close>
     8 
     9 theory Countable
    10 imports Old_Datatype Rat Nat_Bijection
    11 begin
    12 
    13 subsection \<open>The class of countable types\<close>
    14 
    15 class countable =
    16   assumes ex_inj: "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat"
    17 
    18 lemma countable_classI:
    19   fixes f :: "'a \<Rightarrow> nat"
    20   assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
    21   shows "OFCLASS('a, countable_class)"
    22 proof (intro_classes, rule exI)
    23   show "inj f"
    24     by (rule injI [OF assms]) assumption
    25 qed
    26 
    27 
    28 subsection \<open>Conversion functions\<close>
    29 
    30 definition to_nat :: "'a\<Colon>countable \<Rightarrow> nat" where
    31   "to_nat = (SOME f. inj f)"
    32 
    33 definition from_nat :: "nat \<Rightarrow> 'a\<Colon>countable" where
    34   "from_nat = inv (to_nat \<Colon> 'a \<Rightarrow> nat)"
    35 
    36 lemma inj_to_nat [simp]: "inj to_nat"
    37   by (rule exE_some [OF ex_inj]) (simp add: to_nat_def)
    38 
    39 lemma inj_on_to_nat[simp, intro]: "inj_on to_nat S"
    40   using inj_to_nat by (auto simp: inj_on_def)
    41 
    42 lemma surj_from_nat [simp]: "surj from_nat"
    43   unfolding from_nat_def by (simp add: inj_imp_surj_inv)
    44 
    45 lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y"
    46   using injD [OF inj_to_nat] by auto
    47 
    48 lemma from_nat_to_nat [simp]:
    49   "from_nat (to_nat x) = x"
    50   by (simp add: from_nat_def)
    51 
    52 
    53 subsection \<open>Finite types are countable\<close>
    54 
    55 subclass (in finite) countable
    56 proof
    57   have "finite (UNIV\<Colon>'a set)" by (rule finite_UNIV)
    58   with finite_conv_nat_seg_image [of "UNIV::'a set"]
    59   obtain n and f :: "nat \<Rightarrow> 'a" 
    60     where "UNIV = f ` {i. i < n}" by auto
    61   then have "surj f" unfolding surj_def by auto
    62   then have "inj (inv f)" by (rule surj_imp_inj_inv)
    63   then show "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" by (rule exI[of inj])
    64 qed
    65 
    66 
    67 subsection \<open>Automatically proving countability of old-style datatypes\<close>
    68 
    69 inductive finite_item :: "'a Old_Datatype.item \<Rightarrow> bool" where
    70   undefined: "finite_item undefined"
    71 | In0: "finite_item x \<Longrightarrow> finite_item (Old_Datatype.In0 x)"
    72 | In1: "finite_item x \<Longrightarrow> finite_item (Old_Datatype.In1 x)"
    73 | Leaf: "finite_item (Old_Datatype.Leaf a)"
    74 | Scons: "\<lbrakk>finite_item x; finite_item y\<rbrakk> \<Longrightarrow> finite_item (Old_Datatype.Scons x y)"
    75 
    76 function
    77   nth_item :: "nat \<Rightarrow> ('a::countable) Old_Datatype.item"
    78 where
    79   "nth_item 0 = undefined"
    80 | "nth_item (Suc n) =
    81   (case sum_decode n of
    82     Inl i \<Rightarrow>
    83     (case sum_decode i of
    84       Inl j \<Rightarrow> Old_Datatype.In0 (nth_item j)
    85     | Inr j \<Rightarrow> Old_Datatype.In1 (nth_item j))
    86   | Inr i \<Rightarrow>
    87     (case sum_decode i of
    88       Inl j \<Rightarrow> Old_Datatype.Leaf (from_nat j)
    89     | Inr j \<Rightarrow>
    90       (case prod_decode j of
    91         (a, b) \<Rightarrow> Old_Datatype.Scons (nth_item a) (nth_item b))))"
    92 by pat_completeness auto
    93 
    94 lemma le_sum_encode_Inl: "x \<le> y \<Longrightarrow> x \<le> sum_encode (Inl y)"
    95 unfolding sum_encode_def by simp
    96 
    97 lemma le_sum_encode_Inr: "x \<le> y \<Longrightarrow> x \<le> sum_encode (Inr y)"
    98 unfolding sum_encode_def by simp
    99 
   100 termination
   101 by (relation "measure id")
   102   (auto simp add: sum_encode_eq [symmetric] prod_encode_eq [symmetric]
   103     le_imp_less_Suc le_sum_encode_Inl le_sum_encode_Inr
   104     le_prod_encode_1 le_prod_encode_2)
   105 
   106 lemma nth_item_covers: "finite_item x \<Longrightarrow> \<exists>n. nth_item n = x"
   107 proof (induct set: finite_item)
   108   case undefined
   109   have "nth_item 0 = undefined" by simp
   110   thus ?case ..
   111 next
   112   case (In0 x)
   113   then obtain n where "nth_item n = x" by fast
   114   hence "nth_item (Suc (sum_encode (Inl (sum_encode (Inl n))))) = Old_Datatype.In0 x" by simp
   115   thus ?case ..
   116 next
   117   case (In1 x)
   118   then obtain n where "nth_item n = x" by fast
   119   hence "nth_item (Suc (sum_encode (Inl (sum_encode (Inr n))))) = Old_Datatype.In1 x" by simp
   120   thus ?case ..
   121 next
   122   case (Leaf a)
   123   have "nth_item (Suc (sum_encode (Inr (sum_encode (Inl (to_nat a)))))) = Old_Datatype.Leaf a"
   124     by simp
   125   thus ?case ..
   126 next
   127   case (Scons x y)
   128   then obtain i j where "nth_item i = x" and "nth_item j = y" by fast
   129   hence "nth_item
   130     (Suc (sum_encode (Inr (sum_encode (Inr (prod_encode (i, j))))))) = Old_Datatype.Scons x y"
   131     by simp
   132   thus ?case ..
   133 qed
   134 
   135 theorem countable_datatype:
   136   fixes Rep :: "'b \<Rightarrow> ('a::countable) Old_Datatype.item"
   137   fixes Abs :: "('a::countable) Old_Datatype.item \<Rightarrow> 'b"
   138   fixes rep_set :: "('a::countable) Old_Datatype.item \<Rightarrow> bool"
   139   assumes type: "type_definition Rep Abs (Collect rep_set)"
   140   assumes finite_item: "\<And>x. rep_set x \<Longrightarrow> finite_item x"
   141   shows "OFCLASS('b, countable_class)"
   142 proof
   143   def f \<equiv> "\<lambda>y. LEAST n. nth_item n = Rep y"
   144   {
   145     fix y :: 'b
   146     have "rep_set (Rep y)"
   147       using type_definition.Rep [OF type] by simp
   148     hence "finite_item (Rep y)"
   149       by (rule finite_item)
   150     hence "\<exists>n. nth_item n = Rep y"
   151       by (rule nth_item_covers)
   152     hence "nth_item (f y) = Rep y"
   153       unfolding f_def by (rule LeastI_ex)
   154     hence "Abs (nth_item (f y)) = y"
   155       using type_definition.Rep_inverse [OF type] by simp
   156   }
   157   hence "inj f"
   158     by (rule inj_on_inverseI)
   159   thus "\<exists>f::'b \<Rightarrow> nat. inj f"
   160     by - (rule exI)
   161 qed
   162 
   163 ML \<open>
   164   fun old_countable_datatype_tac ctxt =
   165     SUBGOAL (fn (goal, _) =>
   166       let
   167         val ty_name =
   168           (case goal of
   169             (_ $ Const (@{const_name Pure.type}, Type (@{type_name itself}, [Type (n, _)]))) => n
   170           | _ => raise Match)
   171         val typedef_info = hd (Typedef.get_info ctxt ty_name)
   172         val typedef_thm = #type_definition (snd typedef_info)
   173         val pred_name =
   174           (case HOLogic.dest_Trueprop (Thm.concl_of typedef_thm) of
   175             (_ $ _ $ _ $ (_ $ Const (n, _))) => n
   176           | _ => raise Match)
   177         val induct_info = Inductive.the_inductive ctxt pred_name
   178         val pred_names = #names (fst induct_info)
   179         val induct_thms = #inducts (snd induct_info)
   180         val alist = pred_names ~~ induct_thms
   181         val induct_thm = the (AList.lookup (op =) alist pred_name)
   182         val vars = rev (Term.add_vars (Thm.prop_of induct_thm) [])
   183         val insts = vars |> map (fn (_, T) => try (Thm.cterm_of ctxt)
   184           (Const (@{const_name Countable.finite_item}, T)))
   185         val induct_thm' = Drule.instantiate' [] insts induct_thm
   186         val rules = @{thms finite_item.intros}
   187       in
   188         SOLVED' (fn i => EVERY
   189           [resolve_tac ctxt @{thms countable_datatype} i,
   190            resolve_tac ctxt [typedef_thm] i,
   191            eresolve_tac ctxt [induct_thm'] i,
   192            REPEAT (resolve_tac ctxt rules i ORELSE assume_tac ctxt i)]) 1
   193       end)
   194 \<close>
   195 
   196 hide_const (open) finite_item nth_item
   197 
   198 
   199 subsection \<open>Automatically proving countability of datatypes\<close>
   200 
   201 ML_file "bnf_lfp_countable.ML"
   202 
   203 ML \<open>
   204 fun countable_datatype_tac ctxt st =
   205   (case try (fn () => HEADGOAL (old_countable_datatype_tac ctxt) st) () of
   206     SOME res => res
   207   | NONE => BNF_LFP_Countable.countable_datatype_tac ctxt st);
   208 
   209 (* compatibility *)
   210 fun countable_tac ctxt =
   211   SELECT_GOAL (countable_datatype_tac ctxt);
   212 \<close>
   213 
   214 method_setup countable_datatype = \<open>
   215   Scan.succeed (SIMPLE_METHOD o countable_datatype_tac)
   216 \<close> "prove countable class instances for datatypes"
   217 
   218 
   219 subsection \<open>More Countable types\<close>
   220 
   221 text \<open>Naturals\<close>
   222 
   223 instance nat :: countable
   224   by (rule countable_classI [of "id"]) simp
   225 
   226 text \<open>Pairs\<close>
   227 
   228 instance prod :: (countable, countable) countable
   229   by (rule countable_classI [of "\<lambda>(x, y). prod_encode (to_nat x, to_nat y)"])
   230     (auto simp add: prod_encode_eq)
   231 
   232 text \<open>Sums\<close>
   233 
   234 instance sum :: (countable, countable) countable
   235   by (rule countable_classI [of "(\<lambda>x. case x of Inl a \<Rightarrow> to_nat (False, to_nat a)
   236                                      | Inr b \<Rightarrow> to_nat (True, to_nat b))"])
   237     (simp split: sum.split_asm)
   238 
   239 text \<open>Integers\<close>
   240 
   241 instance int :: countable
   242   by (rule countable_classI [of int_encode]) (simp add: int_encode_eq)
   243 
   244 text \<open>Options\<close>
   245 
   246 instance option :: (countable) countable
   247   by countable_datatype
   248 
   249 text \<open>Lists\<close>
   250 
   251 instance list :: (countable) countable
   252   by countable_datatype
   253 
   254 text \<open>String literals\<close>
   255 
   256 instance String.literal :: countable
   257   by (rule countable_classI [of "to_nat \<circ> String.explode"]) (auto simp add: explode_inject)
   258 
   259 text \<open>Functions\<close>
   260 
   261 instance "fun" :: (finite, countable) countable
   262 proof
   263   obtain xs :: "'a list" where xs: "set xs = UNIV"
   264     using finite_list [OF finite_UNIV] ..
   265   show "\<exists>to_nat::('a \<Rightarrow> 'b) \<Rightarrow> nat. inj to_nat"
   266   proof
   267     show "inj (\<lambda>f. to_nat (map f xs))"
   268       by (rule injI, simp add: xs fun_eq_iff)
   269   qed
   270 qed
   271 
   272 text \<open>Typereps\<close>
   273 
   274 instance typerep :: countable
   275   by countable_datatype
   276 
   277 
   278 subsection \<open>The rationals are countably infinite\<close>
   279 
   280 definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where
   281   "nat_to_rat_surj n = (let (a, b) = prod_decode n in Fract (int_decode a) (int_decode b))"
   282 
   283 lemma surj_nat_to_rat_surj: "surj nat_to_rat_surj"
   284 unfolding surj_def
   285 proof
   286   fix r::rat
   287   show "\<exists>n. r = nat_to_rat_surj n"
   288   proof (cases r)
   289     fix i j assume [simp]: "r = Fract i j" and "j > 0"
   290     have "r = (let m = int_encode i; n = int_encode j in nat_to_rat_surj (prod_encode (m, n)))"
   291       by (simp add: Let_def nat_to_rat_surj_def)
   292     thus "\<exists>n. r = nat_to_rat_surj n" by(auto simp: Let_def)
   293   qed
   294 qed
   295 
   296 lemma Rats_eq_range_nat_to_rat_surj: "\<rat> = range nat_to_rat_surj"
   297   by (simp add: Rats_def surj_nat_to_rat_surj)
   298 
   299 context field_char_0
   300 begin
   301 
   302 lemma Rats_eq_range_of_rat_o_nat_to_rat_surj:
   303   "\<rat> = range (of_rat \<circ> nat_to_rat_surj)"
   304   using surj_nat_to_rat_surj
   305   by (auto simp: Rats_def image_def surj_def) (blast intro: arg_cong[where f = of_rat])
   306 
   307 lemma surj_of_rat_nat_to_rat_surj:
   308   "r \<in> \<rat> \<Longrightarrow> \<exists>n. r = of_rat (nat_to_rat_surj n)"
   309   by (simp add: Rats_eq_range_of_rat_o_nat_to_rat_surj image_def)
   310 
   311 end
   312 
   313 instance rat :: countable
   314 proof
   315   show "\<exists>to_nat::rat \<Rightarrow> nat. inj to_nat"
   316   proof
   317     have "surj nat_to_rat_surj"
   318       by (rule surj_nat_to_rat_surj)
   319     then show "inj (inv nat_to_rat_surj)"
   320       by (rule surj_imp_inj_inv)
   321   qed
   322 qed
   323 
   324 end