src/HOL/Word/WordDefinition.thy
changeset 24408 058c5613a86f
parent 24397 eaf37b780683
child 24415 640b85390ba0
--- a/src/HOL/Word/WordDefinition.thy	Wed Aug 22 20:59:19 2007 +0200
+++ b/src/HOL/Word/WordDefinition.thy	Wed Aug 22 21:09:21 2007 +0200
@@ -8,12 +8,13 @@
 
 header {* Definition of Word Type *}
 
-theory WordDefinition imports Size BinOperations TdThs begin
+theory WordDefinition
+imports Numeral_Type BinOperations TdThs begin
 
 typedef (open word) 'a word
-  = "{(0::int) ..< 2^len_of TYPE('a::len0)}" by auto
+  = "{(0::int) ..< 2^CARD('a)}" by auto
 
-instance word :: (len0) number ..
+instance word :: (type) number ..
 instance word :: (type) minus ..
 instance word :: (type) plus ..
 instance word :: (type) one ..
@@ -30,17 +31,17 @@
 constdefs
   -- {* representation of words using unsigned or signed bins, 
         only difference in these is the type class *}
-  word_of_int :: "int => 'a :: len0 word"
-  "word_of_int w == Abs_word (bintrunc (len_of TYPE ('a)) w)" 
+  word_of_int :: "int => 'a word"
+  "word_of_int w == Abs_word (bintrunc CARD('a) w)" 
 
   -- {* uint and sint cast a word to an integer,
         uint treats the word as unsigned,
         sint treats the most-significant-bit as a sign bit *}
-  uint :: "'a :: len0 word => int"
+  uint :: "'a word => int"
   "uint w == Rep_word w"
-  sint :: "'a :: len word => int"
-  sint_uint: "sint w == sbintrunc (len_of TYPE ('a) - 1) (uint w)"
-  unat :: "'a :: len0 word => nat"
+  sint :: "'a :: finite word => int"
+  sint_uint: "sint w == sbintrunc (CARD('a) - 1) (uint w)"
+  unat :: "'a word => nat"
   "unat w == nat (uint w)"
 
   -- "the sets of integers representing the words"
@@ -54,11 +55,11 @@
   "norm_sint n w == (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
 
 defs (overloaded)
-  word_size: "size (w :: 'a :: len0 word) == len_of TYPE('a)"
+  word_size: "size (w :: 'a word) == CARD('a)"
   word_number_of_def: "number_of w == word_of_int w"
 
 constdefs
-  word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b"
+  word_int_case :: "(int => 'b) => ('a word) => 'b"
   "word_int_case f w == f (uint w)"
 
 syntax
@@ -70,18 +71,18 @@
 subsection  "Arithmetic operations"
 
 defs (overloaded)
-  word_1_wi: "(1 :: ('a :: len0) word) == word_of_int 1"
-  word_0_wi: "(0 :: ('a :: len0) word) == word_of_int 0"
+  word_1_wi: "(1 :: ('a) word) == word_of_int 1"
+  word_0_wi: "(0 :: ('a) word) == word_of_int 0"
 
 constdefs
-  word_succ :: "'a :: len0 word => 'a word"
+  word_succ :: "'a word => 'a word"
   "word_succ a == word_of_int (Numeral.succ (uint a))"
 
-  word_pred :: "'a :: len0 word => 'a word"
+  word_pred :: "'a word => 'a word"
   "word_pred a == word_of_int (Numeral.pred (uint a))"
 
 consts
-  word_power :: "'a :: len0 word => nat => 'a word"
+  word_power :: "'a word => nat => 'a word"
 primrec
   "word_power a 0 = 1"
   "word_power a (Suc n) = a * word_power a n"
@@ -98,46 +99,46 @@
 
 defs (overloaded)
   word_and_def: 
-  "(a::'a::len0 word) AND b == word_of_int (uint a AND uint b)"
+  "(a::'a word) AND b == word_of_int (uint a AND uint b)"
 
   word_or_def:  
-  "(a::'a::len0 word) OR b == word_of_int (uint a OR uint b)"
+  "(a::'a word) OR b == word_of_int (uint a OR uint b)"
 
   word_xor_def: 
-  "(a::'a::len0 word) XOR b == word_of_int (uint a XOR uint b)"
+  "(a::'a word) XOR b == word_of_int (uint a XOR uint b)"
 
   word_not_def: 
-  "NOT (a::'a::len0 word) == word_of_int (NOT (uint a))"
+  "NOT (a::'a word) == word_of_int (NOT (uint a))"
 
   word_test_bit_def: 
-  "test_bit (a::'a::len0 word) == bin_nth (uint a)"
+  "test_bit (a::'a word) == bin_nth (uint a)"
 
   word_set_bit_def: 
-  "set_bit (a::'a::len0 word) n x == 
+  "set_bit (a::'a word) n x == 
    word_of_int (bin_sc n (If x bit.B1 bit.B0) (uint a))"
 
   word_lsb_def: 
-  "lsb (a::'a::len0 word) == bin_last (uint a) = bit.B1"
+  "lsb (a::'a word) == bin_last (uint a) = bit.B1"
 
   word_msb_def: 
-  "msb (a::'a::len word) == bin_sign (sint a) = Numeral.Min"
+  "msb (a::'a::finite word) == bin_sign (sint a) = Numeral.Min"
 
 
 constdefs
-  setBit :: "'a :: len0 word => nat => 'a word" 
+  setBit :: "'a word => nat => 'a word" 
   "setBit w n == set_bit w n True"
 
-  clearBit :: "'a :: len0 word => nat => 'a word" 
+  clearBit :: "'a word => nat => 'a word" 
   "clearBit w n == set_bit w n False"
 
 
 constdefs
   -- "Largest representable machine integer."
-  max_word :: "'a::len word"
-  "max_word \<equiv> word_of_int (2^len_of TYPE('a) - 1)"
+  max_word :: "'a::finite word"
+  "max_word \<equiv> word_of_int (2^CARD('a) - 1)"
 
 consts 
-  of_bool :: "bool \<Rightarrow> 'a::len word"
+  of_bool :: "bool \<Rightarrow> 'a::finite word"
 primrec
   "of_bool False = 0"
   "of_bool True = 1"
@@ -145,8 +146,8 @@
 
 
 lemmas word_size_gt_0 [iff] = 
-  xtr1 [OF word_size [THEN meta_eq_to_obj_eq] len_gt_0, standard]
-lemmas lens_gt_0 = word_size_gt_0 len_gt_0
+  xtr1 [OF word_size [THEN meta_eq_to_obj_eq] zero_less_card_finite, standard]
+lemmas lens_gt_0 = word_size_gt_0 zero_less_card_finite
 lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
 
 lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
@@ -163,16 +164,16 @@
 
 lemma 
   Rep_word_0:"0 <= Rep_word x" and 
-  Rep_word_lt: "Rep_word (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
+  Rep_word_lt: "Rep_word (x::'a word) < 2 ^ CARD('a)"
   by (auto simp: Rep_word [simplified])
 
 lemma Rep_word_mod_same:
-  "Rep_word x mod 2 ^ len_of TYPE('a) = Rep_word (x::'a::len0 word)"
+  "Rep_word x mod 2 ^ CARD('a) = Rep_word (x::'a word)"
   by (simp add: int_mod_eq Rep_word_lt Rep_word_0)
 
 lemma td_ext_uint: 
-  "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
-    (%w::int. w mod 2 ^ len_of TYPE('a))"
+  "td_ext (uint :: 'a word => int) word_of_int (uints CARD('a)) 
+    (%w::int. w mod 2 ^ CARD('a))"
   apply (unfold td_ext_def')
   apply (simp add: uints_num uint_def word_of_int_def bintrunc_mod2p)
   apply (simp add: Rep_word_mod_same Rep_word_0 Rep_word_lt
@@ -182,33 +183,34 @@
 lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard]
 
 interpretation word_uint: 
-  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
+  td_ext ["uint::'a word \<Rightarrow> int" 
           word_of_int 
-          "uints (len_of TYPE('a::len0))"
-          "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"]
+          "uints CARD('a)"
+          "\<lambda>w. w mod 2 ^ CARD('a)"]
   by (rule td_ext_uint)
   
 lemmas td_uint = word_uint.td_thm
 
 lemmas td_ext_ubin = td_ext_uint 
-  [simplified len_gt_0 no_bintr_alt1 [symmetric]]
+  [simplified zero_less_card_finite no_bintr_alt1 [symmetric]]
 
 interpretation word_ubin:
-  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
+  td_ext ["uint::'a word \<Rightarrow> int" 
           word_of_int 
-          "uints (len_of TYPE('a::len0))"
-          "bintrunc (len_of TYPE('a::len0))"]
+          "uints CARD('a)"
+          "bintrunc CARD('a)"]
   by (rule td_ext_ubin)
 
 lemma sint_sbintrunc': 
   "sint (word_of_int bin :: 'a word) = 
-    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
+    (sbintrunc (CARD('a :: finite) - 1) bin)"
   unfolding sint_uint 
   by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
 
 lemma uint_sint: 
-  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
+  "uint w = bintrunc CARD('a) (sint (w :: 'a :: finite word))"
   unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
+  
 
 lemma bintr_uint': 
   "n >= size w ==> bintrunc n (uint w) = uint w"
@@ -228,11 +230,11 @@
 lemmas wi_bintr = wi_bintr' [unfolded word_size]
 
 lemma td_ext_sbin: 
-  "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 
-    (sbintrunc (len_of TYPE('a) - 1))"
+  "td_ext (sint :: 'a word => int) word_of_int (sints CARD('a::finite)) 
+    (sbintrunc (CARD('a) - 1))"
   apply (unfold td_ext_def' sint_uint)
   apply (simp add : word_ubin.eq_norm)
-  apply (cases "len_of TYPE('a)")
+  apply (cases "CARD('a)")
    apply (auto simp add : sints_def)
   apply (rule sym [THEN trans])
   apply (rule word_ubin.Abs_norm)
@@ -242,25 +244,25 @@
   done
 
 lemmas td_ext_sint = td_ext_sbin 
-  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
+  [simplified zero_less_card_finite no_sbintr_alt2 Suc_pred' [symmetric]]
 
 (* We do sint before sbin, before sint is the user version
    and interpretations do not produce thm duplicates. I.e. 
    we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
    because the latter is the same thm as the former *)
 interpretation word_sint:
-  td_ext ["sint ::'a::len word => int" 
+  td_ext ["sint ::'a::finite word => int" 
           word_of_int 
-          "sints (len_of TYPE('a::len))"
-          "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
-               2 ^ (len_of TYPE('a::len) - 1)"]
+          "sints CARD('a::finite)"
+          "%w. (w + 2^(CARD('a::finite) - 1)) mod 2^CARD('a::finite) -
+               2 ^ (CARD('a::finite) - 1)"]
   by (rule td_ext_sint)
 
 interpretation word_sbin:
-  td_ext ["sint ::'a::len word => int" 
+  td_ext ["sint ::'a::finite word => int" 
           word_of_int 
-          "sints (len_of TYPE('a::len))"
-          "sbintrunc (len_of TYPE('a::len) - 1)"]
+          "sints CARD('a::finite)"
+          "sbintrunc (CARD('a::finite) - 1)"]
   by (rule td_ext_sbin)
 
 lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard]
@@ -276,18 +278,18 @@
 lemmas uints_mod = uints_def [unfolded no_bintr_alt1]
 
 lemma uint_bintrunc: "uint (number_of bin :: 'a word) = 
-    number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
+    number_of (bintrunc CARD('a) bin)"
   unfolding word_number_of_def number_of_eq
   by (auto intro: word_ubin.eq_norm) 
 
 lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = 
-    number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
+    number_of (sbintrunc (CARD('a :: finite) - 1) bin)" 
   unfolding word_number_of_def number_of_eq
   by (auto intro!: word_sbin.eq_norm simp del: one_is_Suc_zero)
 
 lemma unat_bintrunc: 
-  "unat (number_of bin :: 'a :: len0 word) =
-    number_of (bintrunc (len_of TYPE('a)) bin)"
+  "unat (number_of bin :: 'a word) =
+    number_of (bintrunc CARD('a) bin)"
   unfolding unat_def nat_number_of_def 
   by (simp only: uint_bintrunc)
 
@@ -297,7 +299,7 @@
   sint_sbintrunc [simp] 
   unat_bintrunc [simp]
 
-lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w"
+lemma size_0_eq: "size (w :: 'a word) = 0 ==> v = w"
   apply (unfold word_size)
   apply (rule word_uint.Rep_eqD)
   apply (rule box_equals)
@@ -322,7 +324,7 @@
   iffD2 [OF linorder_not_le uint_m2p_neg, standard]
 
 lemma lt2p_lem:
-  "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n"
+  "CARD('a) <= n ==> uint (w :: 'a word) < 2 ^ n"
   by (rule xtr8 [OF _ uint_lt2p]) simp
 
 lemmas uint_le_0_iff [simp] = 
@@ -332,13 +334,13 @@
   unfolding unat_def by auto
 
 lemma uint_number_of:
-  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
+  "uint (number_of b :: 'a word) = number_of b mod 2 ^ CARD('a)"
   unfolding word_number_of_alt
   by (simp only: int_word_uint)
 
 lemma unat_number_of: 
   "bin_sign b = Numeral.Pls ==> 
-  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
+  unat (number_of b::'a word) = number_of b mod 2 ^ CARD('a)"
   apply (unfold unat_def)
   apply (clarsimp simp only: uint_number_of)
   apply (rule nat_mod_distrib [THEN trans])
@@ -346,31 +348,31 @@
    apply (simp_all add: nat_power_eq)
   done
 
-lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
-    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
-    2 ^ (len_of TYPE('a) - 1)"
+lemma sint_number_of: "sint (number_of b :: 'a :: finite word) = (number_of b + 
+    2 ^ (CARD('a) - 1)) mod 2 ^ CARD('a) -
+    2 ^ (CARD('a) - 1)"
   unfolding word_number_of_alt by (rule int_word_sint)
 
 lemma word_of_int_bin [simp] : 
-  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
+  "(word_of_int (number_of bin) :: 'a word) = (number_of bin)"
   unfolding word_number_of_alt by auto
 
 lemma word_int_case_wi: 
   "word_int_case f (word_of_int i :: 'b word) = 
-    f (i mod 2 ^ len_of TYPE('b::len0))"
+    f (i mod 2 ^ CARD('b))"
   unfolding word_int_case_def by (simp add: word_uint.eq_norm)
 
 lemma word_int_split: 
   "P (word_int_case f x) = 
-    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
-      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
+    (ALL i. x = (word_of_int i :: 'b word) & 
+      0 <= i & i < 2 ^ CARD('b) --> P (f i))"
   unfolding word_int_case_def
   by (auto simp: word_uint.eq_norm int_mod_eq')
 
 lemma word_int_split_asm: 
   "P (word_int_case f x) = 
-    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
-      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
+    (~ (EX n. x = (word_of_int n :: 'b word) &
+      0 <= n & n < 2 ^ CARD('b) & ~ P (f n)))"
   unfolding word_int_case_def
   by (auto simp: word_uint.eq_norm int_mod_eq')
   
@@ -392,10 +394,10 @@
 lemmas sint_below_size = sint_range_size
   [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard]
 
-lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)"
+lemma test_bit_eq_iff: "(test_bit (u::'a word) = test_bit v) = (u = v)"
   unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
 
-lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
+lemma test_bit_size [rule_format] : "(w::'a word) !! n --> n < size w"
   apply (unfold word_test_bit_def)
   apply (subst word_ubin.norm_Rep [symmetric])
   apply (simp only: nth_bintr word_size)
@@ -403,7 +405,7 @@
   done
 
 lemma word_eqI [rule_format] : 
-  fixes u :: "'a::len0 word"
+  fixes u :: "'a word"
   shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v"
   apply (rule test_bit_eq_iff [THEN iffD1])
   apply (rule ext)
@@ -475,14 +477,14 @@
   may want these in reverse, but loop as simp rules, so use following *)
 
 lemma num_of_bintr':
-  "bintrunc (len_of TYPE('a :: len0)) a = b ==> 
+  "bintrunc CARD('a) a = b ==> 
     number_of a = (number_of b :: 'a word)"
   apply safe
   apply (rule_tac num_of_bintr [symmetric])
   done
 
 lemma num_of_sbintr':
-  "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> 
+  "sbintrunc (CARD('a :: finite) - 1) a = b ==> 
     number_of a = (number_of b :: 'a word)"
   apply safe
   apply (rule_tac num_of_sbintr [symmetric])
@@ -503,19 +505,19 @@
 
 constdefs
   -- "cast a word to a different length"
-  scast :: "'a :: len word => 'b :: len word"
+  scast :: "'a :: finite word => 'b :: finite word"
   "scast w == word_of_int (sint w)"
-  ucast :: "'a :: len0 word => 'b :: len0 word"
+  ucast :: "'a word => 'b word"
   "ucast w == word_of_int (uint w)"
 
   -- "whether a cast (or other) function is to a longer or shorter length"
-  source_size :: "('a :: len0 word => 'b) => nat"
+  source_size :: "('a word => 'b) => nat"
   "source_size c == let arb = arbitrary ; x = c arb in size arb"  
-  target_size :: "('a => 'b :: len0 word) => nat"
+  target_size :: "('a => 'b word) => nat"
   "target_size c == size (c arbitrary)"
-  is_up :: "('a :: len0 word => 'b :: len0 word) => bool"
+  is_up :: "('a word => 'b word) => bool"
   "is_up c == source_size c <= target_size c"
-  is_down :: "('a :: len0 word => 'b :: len0 word) => bool"
+  is_down :: "('a word => 'b word) => bool"
   "is_down c == target_size c <= source_size c"
 
 (** cast - note, no arg for new length, as it's determined by type of result,
@@ -528,7 +530,7 @@
   unfolding scast_def by auto
 
 lemma nth_ucast: 
-  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
+  "(ucast w::'a word) !! n = (w !! n & n < CARD('a))"
   apply (unfold ucast_def test_bit_bin)
   apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
   apply (fast elim!: bin_nth_uint_imp)
@@ -537,13 +539,13 @@
 (* for literal u(s)cast *)
 
 lemma ucast_bintr [simp]: 
-  "ucast (number_of w ::'a::len0 word) = 
-   number_of (bintrunc (len_of TYPE('a)) w)"
+  "ucast (number_of w ::'a word) = 
+   number_of (bintrunc CARD('a) w)"
   unfolding ucast_def by simp
 
 lemma scast_sbintr [simp]: 
-  "scast (number_of w ::'a::len word) = 
-   number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
+  "scast (number_of w ::'a::finite word) = 
+   number_of (sbintrunc (CARD('a) - Suc 0) w)"
   unfolding scast_def by simp
 
 lemmas source_size = source_size_def [unfolded Let_def word_size]
@@ -616,22 +618,22 @@
 lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
 
 lemma up_ucast_surj:
-  "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> 
+  "is_up (ucast :: 'b word => 'a word) ==> 
    surj (ucast :: 'a word => 'b word)"
   by (rule surjI, erule ucast_up_ucast_id)
 
 lemma up_scast_surj:
-  "is_up (scast :: 'b::len word => 'a::len word) ==> 
+  "is_up (scast :: 'b::finite word => 'a::finite word) ==> 
    surj (scast :: 'a word => 'b word)"
   by (rule surjI, erule scast_up_scast_id)
 
 lemma down_scast_inj:
-  "is_down (scast :: 'b::len word => 'a::len word) ==> 
+  "is_down (scast :: 'b::finite word => 'a::finite word) ==> 
    inj_on (ucast :: 'a word => 'b word) A"
   by (rule inj_on_inverseI, erule scast_down_scast_id)
 
 lemma down_ucast_inj:
-  "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> 
+  "is_down (ucast :: 'b word => 'a word) ==> 
    inj_on (ucast :: 'a word => 'b word) A"
   by (rule inj_on_inverseI, erule ucast_down_ucast_id)