src/HOL/Library/Numeral_Type.thy
author huffman
Wed Aug 22 20:59:19 2007 +0200 (2007-08-22)
changeset 24407 61b10ffb2549
parent 24406 d96eb21fc1bc
child 24630 351a308ab58d
permissions -rw-r--r--
typed print translation for CARD('a);
declare zero_less_card_finite [simp]
kleing@24332
     1
(*
kleing@24332
     2
  ID:     $Id$
kleing@24332
     3
  Author: Brian Huffman
kleing@24332
     4
kleing@24332
     5
  Numeral Syntax for Types
kleing@24332
     6
*)
kleing@24332
     7
kleing@24332
     8
header "Numeral Syntax for Types"
kleing@24332
     9
kleing@24332
    10
theory Numeral_Type
kleing@24332
    11
  imports Infinite_Set
kleing@24332
    12
begin
kleing@24332
    13
kleing@24332
    14
subsection {* Preliminary lemmas *}
kleing@24332
    15
(* These should be moved elsewhere *)
kleing@24332
    16
kleing@24332
    17
lemma inj_Inl [simp]: "inj_on Inl A"
kleing@24332
    18
  by (rule inj_onI, simp)
kleing@24332
    19
kleing@24332
    20
lemma inj_Inr [simp]: "inj_on Inr A"
kleing@24332
    21
  by (rule inj_onI, simp)
kleing@24332
    22
kleing@24332
    23
lemma inj_Some [simp]: "inj_on Some A"
kleing@24332
    24
  by (rule inj_onI, simp)
kleing@24332
    25
kleing@24332
    26
lemma card_Plus:
kleing@24332
    27
  "[| finite A; finite B |] ==> card (A <+> B) = card A + card B"
kleing@24332
    28
  unfolding Plus_def
kleing@24332
    29
  apply (subgoal_tac "Inl ` A \<inter> Inr ` B = {}")
kleing@24332
    30
  apply (simp add: card_Un_disjoint card_image)
kleing@24332
    31
  apply fast
kleing@24332
    32
  done
kleing@24332
    33
kleing@24332
    34
lemma (in type_definition) univ:
kleing@24332
    35
  "UNIV = Abs ` A"
kleing@24332
    36
proof
kleing@24332
    37
  show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
kleing@24332
    38
  show "UNIV \<subseteq> Abs ` A"
kleing@24332
    39
  proof
kleing@24332
    40
    fix x :: 'b
kleing@24332
    41
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
kleing@24332
    42
    moreover have "Rep x \<in> A" by (rule Rep)
kleing@24332
    43
    ultimately show "x \<in> Abs ` A" by (rule image_eqI)
kleing@24332
    44
  qed
kleing@24332
    45
qed
kleing@24332
    46
kleing@24332
    47
lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
kleing@24332
    48
  by (simp add: univ card_image inj_on_def Abs_inject)
kleing@24332
    49
kleing@24332
    50
kleing@24332
    51
subsection {* Cardinalities of types *}
kleing@24332
    52
kleing@24332
    53
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
kleing@24332
    54
kleing@24332
    55
translations "CARD(t)" => "card (UNIV::t set)"
kleing@24332
    56
huffman@24407
    57
typed_print_translation {*
huffman@24407
    58
let
huffman@24407
    59
  fun card_univ_tr' show_sorts _ [Const (@{const_name UNIV}, Type(_,[T]))] =
huffman@24407
    60
    Syntax.const "_type_card" $ Syntax.term_of_typ show_sorts T;
huffman@24407
    61
in [("card", card_univ_tr')]
huffman@24407
    62
end
huffman@24407
    63
*}
huffman@24407
    64
kleing@24332
    65
lemma card_unit: "CARD(unit) = 1"
kleing@24332
    66
  unfolding univ_unit by simp
kleing@24332
    67
kleing@24332
    68
lemma card_bool: "CARD(bool) = 2"
kleing@24332
    69
  unfolding univ_bool by simp
kleing@24332
    70
kleing@24332
    71
lemma card_prod: "CARD('a::finite \<times> 'b::finite) = CARD('a) * CARD('b)"
kleing@24332
    72
  unfolding univ_prod by (simp only: card_cartesian_product)
kleing@24332
    73
kleing@24332
    74
lemma card_sum: "CARD('a::finite + 'b::finite) = CARD('a) + CARD('b)"
kleing@24332
    75
  unfolding univ_sum by (simp only: finite card_Plus)
kleing@24332
    76
kleing@24332
    77
lemma card_option: "CARD('a::finite option) = Suc CARD('a)"
kleing@24332
    78
  unfolding univ_option
kleing@24332
    79
  apply (subgoal_tac "(None::'a option) \<notin> range Some")
kleing@24332
    80
  apply (simp add: finite card_image)
kleing@24332
    81
  apply fast
kleing@24332
    82
  done
kleing@24332
    83
kleing@24332
    84
lemma card_set: "CARD('a::finite set) = 2 ^ CARD('a)"
kleing@24332
    85
  unfolding univ_set
kleing@24332
    86
  by (simp only: card_Pow finite numeral_2_eq_2)
kleing@24332
    87
kleing@24332
    88
subsection {* Numeral Types *}
kleing@24332
    89
huffman@24406
    90
typedef (open) num0 = "UNIV :: nat set" ..
kleing@24332
    91
typedef (open) num1 = "UNIV :: unit set" ..
kleing@24332
    92
typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
kleing@24332
    93
typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
kleing@24332
    94
kleing@24332
    95
instance num1 :: finite
kleing@24332
    96
proof
kleing@24332
    97
  show "finite (UNIV::num1 set)"
kleing@24332
    98
    unfolding type_definition.univ [OF type_definition_num1]
kleing@24332
    99
    using finite by (rule finite_imageI)
kleing@24332
   100
qed
kleing@24332
   101
kleing@24332
   102
instance bit0 :: (finite) finite
kleing@24332
   103
proof
kleing@24332
   104
  show "finite (UNIV::'a bit0 set)"
kleing@24332
   105
    unfolding type_definition.univ [OF type_definition_bit0]
kleing@24332
   106
    using finite by (rule finite_imageI)
kleing@24332
   107
qed
kleing@24332
   108
kleing@24332
   109
instance bit1 :: (finite) finite
kleing@24332
   110
proof
kleing@24332
   111
  show "finite (UNIV::'a bit1 set)"
kleing@24332
   112
    unfolding type_definition.univ [OF type_definition_bit1]
kleing@24332
   113
    using finite by (rule finite_imageI)
kleing@24332
   114
qed
kleing@24332
   115
kleing@24332
   116
lemma card_num1: "CARD(num1) = 1"
kleing@24332
   117
  unfolding type_definition.card [OF type_definition_num1]
kleing@24332
   118
  by (simp only: card_unit)
kleing@24332
   119
kleing@24332
   120
lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
kleing@24332
   121
  unfolding type_definition.card [OF type_definition_bit0]
kleing@24332
   122
  by (simp only: card_prod card_bool)
kleing@24332
   123
kleing@24332
   124
lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
kleing@24332
   125
  unfolding type_definition.card [OF type_definition_bit1]
kleing@24332
   126
  by (simp only: card_prod card_option card_bool)
kleing@24332
   127
huffman@24406
   128
lemma card_num0: "CARD (num0) = 0"
huffman@24406
   129
  by (simp add: type_definition.card [OF type_definition_num0])
kleing@24332
   130
kleing@24332
   131
lemmas card_univ_simps [simp] =
kleing@24332
   132
  card_unit
kleing@24332
   133
  card_bool
kleing@24332
   134
  card_prod
kleing@24332
   135
  card_sum
kleing@24332
   136
  card_option
kleing@24332
   137
  card_set
kleing@24332
   138
  card_num1
kleing@24332
   139
  card_bit0
kleing@24332
   140
  card_bit1
huffman@24406
   141
  card_num0
kleing@24332
   142
kleing@24332
   143
subsection {* Syntax *}
kleing@24332
   144
kleing@24332
   145
kleing@24332
   146
syntax
kleing@24332
   147
  "_NumeralType" :: "num_const => type"  ("_")
kleing@24332
   148
  "_NumeralType0" :: type ("0")
kleing@24332
   149
  "_NumeralType1" :: type ("1")
kleing@24332
   150
kleing@24332
   151
translations
kleing@24332
   152
  "_NumeralType1" == (type) "num1"
huffman@24406
   153
  "_NumeralType0" == (type) "num0"
kleing@24332
   154
kleing@24332
   155
parse_translation {*
kleing@24332
   156
let
kleing@24332
   157
kleing@24332
   158
val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406
   159
val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332
   160
val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332
   161
val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332
   162
kleing@24332
   163
fun mk_bintype n =
kleing@24332
   164
  let
kleing@24332
   165
    fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332
   166
    fun bin_of n =
kleing@24332
   167
      if n = 1 then num1_const
huffman@24406
   168
      else if n = 0 then num0_const
kleing@24332
   169
      else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332
   170
      else
kleing@24332
   171
        let val (q, r) = IntInf.divMod (n, 2);
kleing@24332
   172
        in mk_bit r $ bin_of q end;
kleing@24332
   173
  in bin_of n end;
kleing@24332
   174
kleing@24332
   175
fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
kleing@24332
   176
      mk_bintype (valOf (IntInf.fromString str))
kleing@24332
   177
  | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332
   178
kleing@24332
   179
in [("_NumeralType", numeral_tr)] end;
kleing@24332
   180
*}
kleing@24332
   181
kleing@24332
   182
print_translation {*
kleing@24332
   183
let
kleing@24332
   184
fun int_of [] = 0
kleing@24332
   185
  | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);
kleing@24332
   186
huffman@24406
   187
fun bin_of (Const ("num0", _)) = []
kleing@24332
   188
  | bin_of (Const ("num1", _)) = [1]
kleing@24332
   189
  | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332
   190
  | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332
   191
  | bin_of t = raise TERM("bin_of", [t]);
kleing@24332
   192
kleing@24332
   193
fun bit_tr' b [t] =
kleing@24332
   194
  let
kleing@24332
   195
    val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332
   196
    val i = int_of rev_digs;
kleing@24332
   197
    val num = IntInf.toString (IntInf.abs i);
kleing@24332
   198
  in
kleing@24332
   199
    Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332
   200
  end
kleing@24332
   201
  | bit_tr' b _ = raise Match;
kleing@24332
   202
kleing@24332
   203
in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332
   204
*}
kleing@24332
   205
kleing@24332
   206
kleing@24332
   207
subsection {* Classes with at values least 1 and 2  *}
kleing@24332
   208
kleing@24332
   209
text {* Class finite already captures "at least 1" *}
kleing@24332
   210
huffman@24407
   211
lemma zero_less_card_finite [simp]:
kleing@24332
   212
  "0 < CARD('a::finite)"
kleing@24332
   213
proof (cases "CARD('a::finite) = 0")
kleing@24332
   214
  case False thus ?thesis by (simp del: card_0_eq)
kleing@24332
   215
next
kleing@24332
   216
  case True
kleing@24332
   217
  thus ?thesis by (simp add: finite)
kleing@24332
   218
qed
kleing@24332
   219
huffman@24407
   220
lemma one_le_card_finite [simp]:
kleing@24332
   221
  "Suc 0 <= CARD('a::finite)"
kleing@24332
   222
  by (simp add: less_Suc_eq_le [symmetric] zero_less_card_finite)
kleing@24332
   223
kleing@24332
   224
kleing@24332
   225
text {* Class for cardinality "at least 2" *}
kleing@24332
   226
kleing@24332
   227
class card2 = finite + 
kleing@24332
   228
  assumes two_le_card: "2 <= CARD('a)"
kleing@24332
   229
kleing@24332
   230
lemma one_less_card: "Suc 0 < CARD('a::card2)"
kleing@24332
   231
  using two_le_card [where 'a='a] by simp
kleing@24332
   232
kleing@24332
   233
instance bit0 :: (finite) card2
kleing@24332
   234
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   235
kleing@24332
   236
instance bit1 :: (finite) card2
kleing@24332
   237
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   238
kleing@24332
   239
subsection {* Examples *}
kleing@24332
   240
kleing@24332
   241
term "TYPE(10)"
kleing@24332
   242
kleing@24332
   243
lemma "CARD(0) = 0" by simp
kleing@24332
   244
lemma "CARD(17) = 17" by simp
kleing@24332
   245
  
kleing@24332
   246
end