src/HOL/Library/Numeral_Type.thy
author huffman
Wed Aug 22 18:53:54 2007 +0200 (2007-08-22)
changeset 24406 d96eb21fc1bc
parent 24332 e3a2b75b1cf9
child 24407 61b10ffb2549
permissions -rw-r--r--
rename type pls to num0
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
kleing@24332
    57
lemma card_unit: "CARD(unit) = 1"
kleing@24332
    58
  unfolding univ_unit by simp
kleing@24332
    59
kleing@24332
    60
lemma card_bool: "CARD(bool) = 2"
kleing@24332
    61
  unfolding univ_bool by simp
kleing@24332
    62
kleing@24332
    63
lemma card_prod: "CARD('a::finite \<times> 'b::finite) = CARD('a) * CARD('b)"
kleing@24332
    64
  unfolding univ_prod by (simp only: card_cartesian_product)
kleing@24332
    65
kleing@24332
    66
lemma card_sum: "CARD('a::finite + 'b::finite) = CARD('a) + CARD('b)"
kleing@24332
    67
  unfolding univ_sum by (simp only: finite card_Plus)
kleing@24332
    68
kleing@24332
    69
lemma card_option: "CARD('a::finite option) = Suc CARD('a)"
kleing@24332
    70
  unfolding univ_option
kleing@24332
    71
  apply (subgoal_tac "(None::'a option) \<notin> range Some")
kleing@24332
    72
  apply (simp add: finite card_image)
kleing@24332
    73
  apply fast
kleing@24332
    74
  done
kleing@24332
    75
kleing@24332
    76
lemma card_set: "CARD('a::finite set) = 2 ^ CARD('a)"
kleing@24332
    77
  unfolding univ_set
kleing@24332
    78
  by (simp only: card_Pow finite numeral_2_eq_2)
kleing@24332
    79
kleing@24332
    80
subsection {* Numeral Types *}
kleing@24332
    81
huffman@24406
    82
typedef (open) num0 = "UNIV :: nat set" ..
kleing@24332
    83
typedef (open) num1 = "UNIV :: unit set" ..
kleing@24332
    84
typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
kleing@24332
    85
typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
kleing@24332
    86
kleing@24332
    87
instance num1 :: finite
kleing@24332
    88
proof
kleing@24332
    89
  show "finite (UNIV::num1 set)"
kleing@24332
    90
    unfolding type_definition.univ [OF type_definition_num1]
kleing@24332
    91
    using finite by (rule finite_imageI)
kleing@24332
    92
qed
kleing@24332
    93
kleing@24332
    94
instance bit0 :: (finite) finite
kleing@24332
    95
proof
kleing@24332
    96
  show "finite (UNIV::'a bit0 set)"
kleing@24332
    97
    unfolding type_definition.univ [OF type_definition_bit0]
kleing@24332
    98
    using finite by (rule finite_imageI)
kleing@24332
    99
qed
kleing@24332
   100
kleing@24332
   101
instance bit1 :: (finite) finite
kleing@24332
   102
proof
kleing@24332
   103
  show "finite (UNIV::'a bit1 set)"
kleing@24332
   104
    unfolding type_definition.univ [OF type_definition_bit1]
kleing@24332
   105
    using finite by (rule finite_imageI)
kleing@24332
   106
qed
kleing@24332
   107
kleing@24332
   108
lemma card_num1: "CARD(num1) = 1"
kleing@24332
   109
  unfolding type_definition.card [OF type_definition_num1]
kleing@24332
   110
  by (simp only: card_unit)
kleing@24332
   111
kleing@24332
   112
lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
kleing@24332
   113
  unfolding type_definition.card [OF type_definition_bit0]
kleing@24332
   114
  by (simp only: card_prod card_bool)
kleing@24332
   115
kleing@24332
   116
lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
kleing@24332
   117
  unfolding type_definition.card [OF type_definition_bit1]
kleing@24332
   118
  by (simp only: card_prod card_option card_bool)
kleing@24332
   119
huffman@24406
   120
lemma card_num0: "CARD (num0) = 0"
huffman@24406
   121
  by (simp add: type_definition.card [OF type_definition_num0])
kleing@24332
   122
kleing@24332
   123
lemmas card_univ_simps [simp] =
kleing@24332
   124
  card_unit
kleing@24332
   125
  card_bool
kleing@24332
   126
  card_prod
kleing@24332
   127
  card_sum
kleing@24332
   128
  card_option
kleing@24332
   129
  card_set
kleing@24332
   130
  card_num1
kleing@24332
   131
  card_bit0
kleing@24332
   132
  card_bit1
huffman@24406
   133
  card_num0
kleing@24332
   134
kleing@24332
   135
subsection {* Syntax *}
kleing@24332
   136
kleing@24332
   137
kleing@24332
   138
syntax
kleing@24332
   139
  "_NumeralType" :: "num_const => type"  ("_")
kleing@24332
   140
  "_NumeralType0" :: type ("0")
kleing@24332
   141
  "_NumeralType1" :: type ("1")
kleing@24332
   142
kleing@24332
   143
translations
kleing@24332
   144
  "_NumeralType1" == (type) "num1"
huffman@24406
   145
  "_NumeralType0" == (type) "num0"
kleing@24332
   146
kleing@24332
   147
parse_translation {*
kleing@24332
   148
let
kleing@24332
   149
kleing@24332
   150
val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406
   151
val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332
   152
val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332
   153
val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332
   154
kleing@24332
   155
fun mk_bintype n =
kleing@24332
   156
  let
kleing@24332
   157
    fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332
   158
    fun bin_of n =
kleing@24332
   159
      if n = 1 then num1_const
huffman@24406
   160
      else if n = 0 then num0_const
kleing@24332
   161
      else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332
   162
      else
kleing@24332
   163
        let val (q, r) = IntInf.divMod (n, 2);
kleing@24332
   164
        in mk_bit r $ bin_of q end;
kleing@24332
   165
  in bin_of n end;
kleing@24332
   166
kleing@24332
   167
fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
kleing@24332
   168
      mk_bintype (valOf (IntInf.fromString str))
kleing@24332
   169
  | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332
   170
kleing@24332
   171
in [("_NumeralType", numeral_tr)] end;
kleing@24332
   172
*}
kleing@24332
   173
kleing@24332
   174
print_translation {*
kleing@24332
   175
let
kleing@24332
   176
fun int_of [] = 0
kleing@24332
   177
  | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);
kleing@24332
   178
huffman@24406
   179
fun bin_of (Const ("num0", _)) = []
kleing@24332
   180
  | bin_of (Const ("num1", _)) = [1]
kleing@24332
   181
  | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332
   182
  | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332
   183
  | bin_of t = raise TERM("bin_of", [t]);
kleing@24332
   184
kleing@24332
   185
fun bit_tr' b [t] =
kleing@24332
   186
  let
kleing@24332
   187
    val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332
   188
    val i = int_of rev_digs;
kleing@24332
   189
    val num = IntInf.toString (IntInf.abs i);
kleing@24332
   190
  in
kleing@24332
   191
    Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332
   192
  end
kleing@24332
   193
  | bit_tr' b _ = raise Match;
kleing@24332
   194
kleing@24332
   195
in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332
   196
*}
kleing@24332
   197
kleing@24332
   198
kleing@24332
   199
subsection {* Classes with at values least 1 and 2  *}
kleing@24332
   200
kleing@24332
   201
text {* Class finite already captures "at least 1" *}
kleing@24332
   202
kleing@24332
   203
lemma zero_less_card_finite:
kleing@24332
   204
  "0 < CARD('a::finite)"
kleing@24332
   205
proof (cases "CARD('a::finite) = 0")
kleing@24332
   206
  case False thus ?thesis by (simp del: card_0_eq)
kleing@24332
   207
next
kleing@24332
   208
  case True
kleing@24332
   209
  thus ?thesis by (simp add: finite)
kleing@24332
   210
qed
kleing@24332
   211
kleing@24332
   212
lemma one_le_card_finite:
kleing@24332
   213
  "Suc 0 <= CARD('a::finite)"
kleing@24332
   214
  by (simp add: less_Suc_eq_le [symmetric] zero_less_card_finite)
kleing@24332
   215
kleing@24332
   216
kleing@24332
   217
text {* Class for cardinality "at least 2" *}
kleing@24332
   218
kleing@24332
   219
class card2 = finite + 
kleing@24332
   220
  assumes two_le_card: "2 <= CARD('a)"
kleing@24332
   221
kleing@24332
   222
lemma one_less_card: "Suc 0 < CARD('a::card2)"
kleing@24332
   223
  using two_le_card [where 'a='a] by simp
kleing@24332
   224
kleing@24332
   225
instance bit0 :: (finite) card2
kleing@24332
   226
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   227
kleing@24332
   228
instance bit1 :: (finite) card2
kleing@24332
   229
  by intro_classes (simp add: one_le_card_finite)
kleing@24332
   230
kleing@24332
   231
subsection {* Examples *}
kleing@24332
   232
kleing@24332
   233
term "TYPE(10)"
kleing@24332
   234
kleing@24332
   235
lemma "CARD(0) = 0" by simp
kleing@24332
   236
lemma "CARD(17) = 17" by simp
kleing@24332
   237
  
kleing@24332
   238
end