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