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