src/HOL/Library/Numeral_Type.thy
author huffman
Fri Feb 20 11:58:00 2009 -0800 (2009-02-20)
changeset 30032 c7f0c1b8001b
parent 30001 dd27e16677b2
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
class instances for num1
     1 (*  Title:      HOL/Library/Numeral_Type.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* Numeral Syntax for Types *}
     6 
     7 theory Numeral_Type
     8 imports Plain "~~/src/HOL/Presburger"
     9 begin
    10 
    11 subsection {* Preliminary lemmas *}
    12 (* These should be moved elsewhere *)
    13 
    14 lemma (in type_definition) univ:
    15   "UNIV = Abs ` A"
    16 proof
    17   show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
    18   show "UNIV \<subseteq> Abs ` A"
    19   proof
    20     fix x :: 'b
    21     have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
    22     moreover have "Rep x \<in> A" by (rule Rep)
    23     ultimately show "x \<in> Abs ` A" by (rule image_eqI)
    24   qed
    25 qed
    26 
    27 lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
    28   by (simp add: univ card_image inj_on_def Abs_inject)
    29 
    30 
    31 subsection {* Cardinalities of types *}
    32 
    33 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
    34 
    35 translations "CARD(t)" => "CONST card (CONST UNIV \<Colon> t set)"
    36 
    37 typed_print_translation {*
    38 let
    39   fun card_univ_tr' show_sorts _ [Const (@{const_name UNIV}, Type(_,[T,_]))] =
    40     Syntax.const "_type_card" $ Syntax.term_of_typ show_sorts T;
    41 in [(@{const_syntax card}, card_univ_tr')]
    42 end
    43 *}
    44 
    45 lemma card_unit [simp]: "CARD(unit) = 1"
    46   unfolding UNIV_unit by simp
    47 
    48 lemma card_bool [simp]: "CARD(bool) = 2"
    49   unfolding UNIV_bool by simp
    50 
    51 lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
    52   unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
    53 
    54 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
    55   unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
    56 
    57 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
    58   unfolding insert_None_conv_UNIV [symmetric]
    59   apply (subgoal_tac "(None::'a option) \<notin> range Some")
    60   apply (simp add: card_image)
    61   apply fast
    62   done
    63 
    64 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    65   unfolding Pow_UNIV [symmetric]
    66   by (simp only: card_Pow finite numeral_2_eq_2)
    67 
    68 lemma card_nat [simp]: "CARD(nat) = 0"
    69   by (simp add: infinite_UNIV_nat card_eq_0_iff)
    70 
    71 
    72 subsection {* Classes with at least 1 and 2  *}
    73 
    74 text {* Class finite already captures "at least 1" *}
    75 
    76 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
    77   unfolding neq0_conv [symmetric] by simp
    78 
    79 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
    80   by (simp add: less_Suc_eq_le [symmetric])
    81 
    82 text {* Class for cardinality "at least 2" *}
    83 
    84 class card2 = finite + 
    85   assumes two_le_card: "2 \<le> CARD('a)"
    86 
    87 lemma one_less_card: "Suc 0 < CARD('a::card2)"
    88   using two_le_card [where 'a='a] by simp
    89 
    90 lemma one_less_int_card: "1 < int CARD('a::card2)"
    91   using one_less_card [where 'a='a] by simp
    92 
    93 
    94 subsection {* Numeral Types *}
    95 
    96 typedef (open) num0 = "UNIV :: nat set" ..
    97 typedef (open) num1 = "UNIV :: unit set" ..
    98 
    99 typedef (open) 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
   100 proof
   101   show "0 \<in> {0 ..< 2 * int CARD('a)}"
   102     by simp
   103 qed
   104 
   105 typedef (open) 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
   106 proof
   107   show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
   108     by simp
   109 qed
   110 
   111 lemma card_num0 [simp]: "CARD (num0) = 0"
   112   unfolding type_definition.card [OF type_definition_num0]
   113   by simp
   114 
   115 lemma card_num1 [simp]: "CARD(num1) = 1"
   116   unfolding type_definition.card [OF type_definition_num1]
   117   by (simp only: card_unit)
   118 
   119 lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)"
   120   unfolding type_definition.card [OF type_definition_bit0]
   121   by simp
   122 
   123 lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))"
   124   unfolding type_definition.card [OF type_definition_bit1]
   125   by simp
   126 
   127 instance num1 :: finite
   128 proof
   129   show "finite (UNIV::num1 set)"
   130     unfolding type_definition.univ [OF type_definition_num1]
   131     using finite by (rule finite_imageI)
   132 qed
   133 
   134 instance bit0 :: (finite) card2
   135 proof
   136   show "finite (UNIV::'a bit0 set)"
   137     unfolding type_definition.univ [OF type_definition_bit0]
   138     by simp
   139   show "2 \<le> CARD('a bit0)"
   140     by simp
   141 qed
   142 
   143 instance bit1 :: (finite) card2
   144 proof
   145   show "finite (UNIV::'a bit1 set)"
   146     unfolding type_definition.univ [OF type_definition_bit1]
   147     by simp
   148   show "2 \<le> CARD('a bit1)"
   149     by simp
   150 qed
   151 
   152 
   153 subsection {* Locale for modular arithmetic subtypes *}
   154 
   155 locale mod_type =
   156   fixes n :: int
   157   and Rep :: "'a::{zero,one,plus,times,uminus,minus,power} \<Rightarrow> int"
   158   and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus,power}"
   159   assumes type: "type_definition Rep Abs {0..<n}"
   160   and size1: "1 < n"
   161   and zero_def: "0 = Abs 0"
   162   and one_def:  "1 = Abs 1"
   163   and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
   164   and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
   165   and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
   166   and minus_def: "- x = Abs ((- Rep x) mod n)"
   167   and power_def: "x ^ k = Abs (Rep x ^ k mod n)"
   168 begin
   169 
   170 lemma size0: "0 < n"
   171 by (cut_tac size1, simp)
   172 
   173 lemmas definitions =
   174   zero_def one_def add_def mult_def minus_def diff_def power_def
   175 
   176 lemma Rep_less_n: "Rep x < n"
   177 by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
   178 
   179 lemma Rep_le_n: "Rep x \<le> n"
   180 by (rule Rep_less_n [THEN order_less_imp_le])
   181 
   182 lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
   183 by (rule type_definition.Rep_inject [OF type, symmetric])
   184 
   185 lemma Rep_inverse: "Abs (Rep x) = x"
   186 by (rule type_definition.Rep_inverse [OF type])
   187 
   188 lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
   189 by (rule type_definition.Abs_inverse [OF type])
   190 
   191 lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
   192 by (simp add: Abs_inverse IntDiv.pos_mod_conj [OF size0])
   193 
   194 lemma Rep_Abs_0: "Rep (Abs 0) = 0"
   195 by (simp add: Abs_inverse size0)
   196 
   197 lemma Rep_0: "Rep 0 = 0"
   198 by (simp add: zero_def Rep_Abs_0)
   199 
   200 lemma Rep_Abs_1: "Rep (Abs 1) = 1"
   201 by (simp add: Abs_inverse size1)
   202 
   203 lemma Rep_1: "Rep 1 = 1"
   204 by (simp add: one_def Rep_Abs_1)
   205 
   206 lemma Rep_mod: "Rep x mod n = Rep x"
   207 apply (rule_tac x=x in type_definition.Abs_cases [OF type])
   208 apply (simp add: type_definition.Abs_inverse [OF type])
   209 apply (simp add: mod_pos_pos_trivial)
   210 done
   211 
   212 lemmas Rep_simps =
   213   Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
   214 
   215 lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
   216 apply (intro_classes, unfold definitions)
   217 apply (simp_all add: Rep_simps zmod_simps ring_simps)
   218 done
   219 
   220 lemma recpower: "OFCLASS('a, recpower_class)"
   221 apply (intro_classes, unfold definitions)
   222 apply (simp_all add: Rep_simps zmod_simps add_ac mult_assoc
   223                      mod_pos_pos_trivial size1)
   224 done
   225 
   226 end
   227 
   228 locale mod_ring = mod_type +
   229   constrains n :: int
   230   and Rep :: "'a::{number_ring,power} \<Rightarrow> int"
   231   and Abs :: "int \<Rightarrow> 'a::{number_ring,power}"
   232 begin
   233 
   234 lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
   235 apply (induct k)
   236 apply (simp add: zero_def)
   237 apply (simp add: Rep_simps add_def one_def zmod_simps add_ac)
   238 done
   239 
   240 lemma of_int_eq: "of_int z = Abs (z mod n)"
   241 apply (cases z rule: int_diff_cases)
   242 apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
   243 done
   244 
   245 lemma Rep_number_of:
   246   "Rep (number_of w) = number_of w mod n"
   247 by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
   248 
   249 lemma iszero_number_of:
   250   "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
   251 by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
   252 
   253 lemma cases:
   254   assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
   255   shows "P"
   256 apply (cases x rule: type_definition.Abs_cases [OF type])
   257 apply (rule_tac z="y" in 1)
   258 apply (simp_all add: of_int_eq mod_pos_pos_trivial)
   259 done
   260 
   261 lemma induct:
   262   "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
   263 by (cases x rule: cases) simp
   264 
   265 end
   266 
   267 
   268 subsection {* Number ring instances *}
   269 
   270 text {*
   271   Unfortunately a number ring instance is not possible for
   272   @{typ num1}, since 0 and 1 are not distinct.
   273 *}
   274 
   275 instantiation num1 :: "{comm_ring,comm_monoid_mult,number,recpower}"
   276 begin
   277 
   278 lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
   279   by (induct x, induct y) simp
   280 
   281 instance proof
   282 qed (simp_all add: num1_eq_iff)
   283 
   284 end
   285 
   286 instantiation
   287   bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus,power}"
   288 begin
   289 
   290 definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
   291   "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
   292 
   293 definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
   294   "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
   295 
   296 definition "0 = Abs_bit0 0"
   297 definition "1 = Abs_bit0 1"
   298 definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
   299 definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
   300 definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
   301 definition "- x = Abs_bit0' (- Rep_bit0 x)"
   302 definition "x ^ k = Abs_bit0' (Rep_bit0 x ^ k)"
   303 
   304 definition "0 = Abs_bit1 0"
   305 definition "1 = Abs_bit1 1"
   306 definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
   307 definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
   308 definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
   309 definition "- x = Abs_bit1' (- Rep_bit1 x)"
   310 definition "x ^ k = Abs_bit1' (Rep_bit1 x ^ k)"
   311 
   312 instance ..
   313 
   314 end
   315 
   316 interpretation bit0!:
   317   mod_type "int CARD('a::finite bit0)"
   318            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   319            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   320 apply (rule mod_type.intro)
   321 apply (simp add: int_mult type_definition_bit0)
   322 apply (rule one_less_int_card)
   323 apply (rule zero_bit0_def)
   324 apply (rule one_bit0_def)
   325 apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
   326 apply (rule times_bit0_def [unfolded Abs_bit0'_def])
   327 apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
   328 apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
   329 apply (rule power_bit0_def [unfolded Abs_bit0'_def])
   330 done
   331 
   332 interpretation bit1!:
   333   mod_type "int CARD('a::finite bit1)"
   334            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   335            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   336 apply (rule mod_type.intro)
   337 apply (simp add: int_mult type_definition_bit1)
   338 apply (rule one_less_int_card)
   339 apply (rule zero_bit1_def)
   340 apply (rule one_bit1_def)
   341 apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
   342 apply (rule times_bit1_def [unfolded Abs_bit1'_def])
   343 apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
   344 apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
   345 apply (rule power_bit1_def [unfolded Abs_bit1'_def])
   346 done
   347 
   348 instance bit0 :: (finite) "{comm_ring_1,recpower}"
   349   by (rule bit0.comm_ring_1 bit0.recpower)+
   350 
   351 instance bit1 :: (finite) "{comm_ring_1,recpower}"
   352   by (rule bit1.comm_ring_1 bit1.recpower)+
   353 
   354 instantiation bit0 and bit1 :: (finite) number_ring
   355 begin
   356 
   357 definition "(number_of w :: _ bit0) = of_int w"
   358 
   359 definition "(number_of w :: _ bit1) = of_int w"
   360 
   361 instance proof
   362 qed (rule number_of_bit0_def number_of_bit1_def)+
   363 
   364 end
   365 
   366 interpretation bit0!:
   367   mod_ring "int CARD('a::finite bit0)"
   368            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   369            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   370   ..
   371 
   372 interpretation bit1!:
   373   mod_ring "int CARD('a::finite bit1)"
   374            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   375            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   376   ..
   377 
   378 text {* Set up cases, induction, and arithmetic *}
   379 
   380 lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
   381 lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
   382 
   383 lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
   384 lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
   385 
   386 lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
   387 lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
   388 
   389 declare power_Suc [where ?'a="'a::finite bit0", standard, simp]
   390 declare power_Suc [where ?'a="'a::finite bit1", standard, simp]
   391 
   392 
   393 subsection {* Syntax *}
   394 
   395 syntax
   396   "_NumeralType" :: "num_const => type"  ("_")
   397   "_NumeralType0" :: type ("0")
   398   "_NumeralType1" :: type ("1")
   399 
   400 translations
   401   "_NumeralType1" == (type) "num1"
   402   "_NumeralType0" == (type) "num0"
   403 
   404 parse_translation {*
   405 let
   406 
   407 val num1_const = Syntax.const "Numeral_Type.num1";
   408 val num0_const = Syntax.const "Numeral_Type.num0";
   409 val B0_const = Syntax.const "Numeral_Type.bit0";
   410 val B1_const = Syntax.const "Numeral_Type.bit1";
   411 
   412 fun mk_bintype n =
   413   let
   414     fun mk_bit n = if n = 0 then B0_const else B1_const;
   415     fun bin_of n =
   416       if n = 1 then num1_const
   417       else if n = 0 then num0_const
   418       else if n = ~1 then raise TERM ("negative type numeral", [])
   419       else
   420         let val (q, r) = Integer.div_mod n 2;
   421         in mk_bit r $ bin_of q end;
   422   in bin_of n end;
   423 
   424 fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
   425       mk_bintype (valOf (Int.fromString str))
   426   | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
   427 
   428 in [("_NumeralType", numeral_tr)] end;
   429 *}
   430 
   431 print_translation {*
   432 let
   433 fun int_of [] = 0
   434   | int_of (b :: bs) = b + 2 * int_of bs;
   435 
   436 fun bin_of (Const ("num0", _)) = []
   437   | bin_of (Const ("num1", _)) = [1]
   438   | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
   439   | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
   440   | bin_of t = raise TERM("bin_of", [t]);
   441 
   442 fun bit_tr' b [t] =
   443   let
   444     val rev_digs = b :: bin_of t handle TERM _ => raise Match
   445     val i = int_of rev_digs;
   446     val num = string_of_int (abs i);
   447   in
   448     Syntax.const "_NumeralType" $ Syntax.free num
   449   end
   450   | bit_tr' b _ = raise Match;
   451 
   452 in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
   453 *}
   454 
   455 subsection {* Examples *}
   456 
   457 lemma "CARD(0) = 0" by simp
   458 lemma "CARD(17) = 17" by simp
   459 lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
   460 
   461 end