src/HOL/Library/Numeral_Type.thy
author wenzelm
Sat Nov 10 18:36:06 2007 +0100 (2007-11-10)
changeset 25378 dca691610489
parent 24630 351a308ab58d
child 25459 d1dce7d0731c
permissions -rw-r--r--
tuned document;
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
wenzelm@25378
    88
kleing@24332
    89
subsection {* Numeral Types *}
kleing@24332
    90
huffman@24406
    91
typedef (open) num0 = "UNIV :: nat set" ..
kleing@24332
    92
typedef (open) num1 = "UNIV :: unit set" ..
kleing@24332
    93
typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
kleing@24332
    94
typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
kleing@24332
    95
kleing@24332
    96
instance num1 :: finite
kleing@24332
    97
proof
kleing@24332
    98
  show "finite (UNIV::num1 set)"
kleing@24332
    99
    unfolding type_definition.univ [OF type_definition_num1]
kleing@24332
   100
    using finite by (rule finite_imageI)
kleing@24332
   101
qed
kleing@24332
   102
kleing@24332
   103
instance bit0 :: (finite) finite
kleing@24332
   104
proof
kleing@24332
   105
  show "finite (UNIV::'a bit0 set)"
kleing@24332
   106
    unfolding type_definition.univ [OF type_definition_bit0]
kleing@24332
   107
    using finite by (rule finite_imageI)
kleing@24332
   108
qed
kleing@24332
   109
kleing@24332
   110
instance bit1 :: (finite) finite
kleing@24332
   111
proof
kleing@24332
   112
  show "finite (UNIV::'a bit1 set)"
kleing@24332
   113
    unfolding type_definition.univ [OF type_definition_bit1]
kleing@24332
   114
    using finite by (rule finite_imageI)
kleing@24332
   115
qed
kleing@24332
   116
kleing@24332
   117
lemma card_num1: "CARD(num1) = 1"
kleing@24332
   118
  unfolding type_definition.card [OF type_definition_num1]
kleing@24332
   119
  by (simp only: card_unit)
kleing@24332
   120
kleing@24332
   121
lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
kleing@24332
   122
  unfolding type_definition.card [OF type_definition_bit0]
kleing@24332
   123
  by (simp only: card_prod card_bool)
kleing@24332
   124
kleing@24332
   125
lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
kleing@24332
   126
  unfolding type_definition.card [OF type_definition_bit1]
kleing@24332
   127
  by (simp only: card_prod card_option card_bool)
kleing@24332
   128
huffman@24406
   129
lemma card_num0: "CARD (num0) = 0"
huffman@24406
   130
  by (simp add: type_definition.card [OF type_definition_num0])
kleing@24332
   131
kleing@24332
   132
lemmas card_univ_simps [simp] =
kleing@24332
   133
  card_unit
kleing@24332
   134
  card_bool
kleing@24332
   135
  card_prod
kleing@24332
   136
  card_sum
kleing@24332
   137
  card_option
kleing@24332
   138
  card_set
kleing@24332
   139
  card_num1
kleing@24332
   140
  card_bit0
kleing@24332
   141
  card_bit1
huffman@24406
   142
  card_num0
kleing@24332
   143
wenzelm@25378
   144
kleing@24332
   145
subsection {* Syntax *}
kleing@24332
   146
kleing@24332
   147
syntax
kleing@24332
   148
  "_NumeralType" :: "num_const => type"  ("_")
kleing@24332
   149
  "_NumeralType0" :: type ("0")
kleing@24332
   150
  "_NumeralType1" :: type ("1")
kleing@24332
   151
kleing@24332
   152
translations
kleing@24332
   153
  "_NumeralType1" == (type) "num1"
huffman@24406
   154
  "_NumeralType0" == (type) "num0"
kleing@24332
   155
kleing@24332
   156
parse_translation {*
kleing@24332
   157
let
kleing@24332
   158
kleing@24332
   159
val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406
   160
val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332
   161
val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332
   162
val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332
   163
kleing@24332
   164
fun mk_bintype n =
kleing@24332
   165
  let
kleing@24332
   166
    fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332
   167
    fun bin_of n =
kleing@24332
   168
      if n = 1 then num1_const
huffman@24406
   169
      else if n = 0 then num0_const
kleing@24332
   170
      else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332
   171
      else
wenzelm@24630
   172
        let val (q, r) = Integer.div_mod n 2;
kleing@24332
   173
        in mk_bit r $ bin_of q end;
kleing@24332
   174
  in bin_of n end;
kleing@24332
   175
kleing@24332
   176
fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
wenzelm@24630
   177
      mk_bintype (valOf (Int.fromString str))
kleing@24332
   178
  | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332
   179
kleing@24332
   180
in [("_NumeralType", numeral_tr)] end;
kleing@24332
   181
*}
kleing@24332
   182
kleing@24332
   183
print_translation {*
kleing@24332
   184
let
kleing@24332
   185
fun int_of [] = 0
wenzelm@24630
   186
  | int_of (b :: bs) = b + 2 * int_of bs;
kleing@24332
   187
huffman@24406
   188
fun bin_of (Const ("num0", _)) = []
kleing@24332
   189
  | bin_of (Const ("num1", _)) = [1]
kleing@24332
   190
  | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332
   191
  | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332
   192
  | bin_of t = raise TERM("bin_of", [t]);
kleing@24332
   193
kleing@24332
   194
fun bit_tr' b [t] =
kleing@24332
   195
  let
kleing@24332
   196
    val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332
   197
    val i = int_of rev_digs;
wenzelm@24630
   198
    val num = string_of_int (abs i);
kleing@24332
   199
  in
kleing@24332
   200
    Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332
   201
  end
kleing@24332
   202
  | bit_tr' b _ = raise Match;
kleing@24332
   203
kleing@24332
   204
in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332
   205
*}
kleing@24332
   206
kleing@24332
   207
wenzelm@25378
   208
subsection {* Classes with at least 1 and 2  *}
kleing@24332
   209
kleing@24332
   210
text {* Class finite already captures "at least 1" *}
kleing@24332
   211
huffman@24407
   212
lemma zero_less_card_finite [simp]:
kleing@24332
   213
  "0 < CARD('a::finite)"
kleing@24332
   214
proof (cases "CARD('a::finite) = 0")
kleing@24332
   215
  case False thus ?thesis by (simp del: card_0_eq)
kleing@24332
   216
next
kleing@24332
   217
  case True
kleing@24332
   218
  thus ?thesis by (simp add: finite)
kleing@24332
   219
qed
kleing@24332
   220
huffman@24407
   221
lemma one_le_card_finite [simp]:
kleing@24332
   222
  "Suc 0 <= CARD('a::finite)"
kleing@24332
   223
  by (simp add: less_Suc_eq_le [symmetric] zero_less_card_finite)
kleing@24332
   224
kleing@24332
   225
kleing@24332
   226
text {* Class for cardinality "at least 2" *}
kleing@24332
   227
kleing@24332
   228
class card2 = finite + 
kleing@24332
   229
  assumes two_le_card: "2 <= CARD('a)"
kleing@24332
   230
kleing@24332
   231
lemma one_less_card: "Suc 0 < CARD('a::card2)"
kleing@24332
   232
  using two_le_card [where 'a='a] by simp
kleing@24332
   233
kleing@24332
   234
instance bit0 :: (finite) card2
kleing@24332
   235
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   236
kleing@24332
   237
instance bit1 :: (finite) card2
kleing@24332
   238
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   239
kleing@24332
   240
subsection {* Examples *}
kleing@24332
   241
kleing@24332
   242
term "TYPE(10)"
kleing@24332
   243
kleing@24332
   244
lemma "CARD(0) = 0" by simp
kleing@24332
   245
lemma "CARD(17) = 17" by simp
kleing@24332
   246
  
kleing@24332
   247
end