src/HOL/String.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58310 91ea607a34d8
child 58822 90a5e981af3e
permissions -rw-r--r--
specialized specification: avoid trivial instances
haftmann@31051
     1
(* Author: Tobias Nipkow, Florian Haftmann, TU Muenchen *)
haftmann@31051
     2
haftmann@31051
     3
header {* Character and string types *}
haftmann@31051
     4
haftmann@31051
     5
theory String
blanchet@55969
     6
imports Enum
haftmann@31051
     7
begin
haftmann@31051
     8
haftmann@49972
     9
subsection {* Characters and strings *}
haftmann@31051
    10
blanchet@58310
    11
datatype nibble =
haftmann@31051
    12
    Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
haftmann@31051
    13
  | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
haftmann@31051
    14
haftmann@31051
    15
lemma UNIV_nibble:
haftmann@31051
    16
  "UNIV = {Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@31051
    17
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF}" (is "_ = ?A")
haftmann@31051
    18
proof (rule UNIV_eq_I)
haftmann@31051
    19
  fix x show "x \<in> ?A" by (cases x) simp_all
haftmann@31051
    20
qed
haftmann@31051
    21
haftmann@49972
    22
lemma size_nibble [code, simp]:
blanchet@56846
    23
  "size_nibble (x::nibble) = 0"
blanchet@56846
    24
  "size (x::nibble) = 0"
blanchet@56846
    25
  by (cases x, simp_all)+
haftmann@49972
    26
haftmann@49972
    27
instantiation nibble :: enum
haftmann@49972
    28
begin
haftmann@49972
    29
haftmann@49972
    30
definition
haftmann@49972
    31
  "Enum.enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@49972
    32
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@49972
    33
haftmann@49972
    34
definition
haftmann@49972
    35
  "Enum.enum_all P \<longleftrightarrow> P Nibble0 \<and> P Nibble1 \<and> P Nibble2 \<and> P Nibble3 \<and> P Nibble4 \<and> P Nibble5 \<and> P Nibble6 \<and> P Nibble7
haftmann@49972
    36
     \<and> P Nibble8 \<and> P Nibble9 \<and> P NibbleA \<and> P NibbleB \<and> P NibbleC \<and> P NibbleD \<and> P NibbleE \<and> P NibbleF"
haftmann@49972
    37
haftmann@49972
    38
definition
haftmann@49972
    39
  "Enum.enum_ex P \<longleftrightarrow> P Nibble0 \<or> P Nibble1 \<or> P Nibble2 \<or> P Nibble3 \<or> P Nibble4 \<or> P Nibble5 \<or> P Nibble6 \<or> P Nibble7
haftmann@49972
    40
     \<or> P Nibble8 \<or> P Nibble9 \<or> P NibbleA \<or> P NibbleB \<or> P NibbleC \<or> P NibbleD \<or> P NibbleE \<or> P NibbleF"
haftmann@49972
    41
haftmann@49972
    42
instance proof
haftmann@49972
    43
qed (simp_all only: enum_nibble_def enum_all_nibble_def enum_ex_nibble_def UNIV_nibble, simp_all)
haftmann@49972
    44
haftmann@49972
    45
end
haftmann@49972
    46
haftmann@49972
    47
lemma card_UNIV_nibble:
haftmann@49972
    48
  "card (UNIV :: nibble set) = 16"
haftmann@49972
    49
  by (simp add: card_UNIV_length_enum enum_nibble_def)
haftmann@31051
    50
haftmann@51160
    51
primrec nat_of_nibble :: "nibble \<Rightarrow> nat"
haftmann@51160
    52
where
haftmann@51160
    53
  "nat_of_nibble Nibble0 = 0"
haftmann@51160
    54
| "nat_of_nibble Nibble1 = 1"
haftmann@51160
    55
| "nat_of_nibble Nibble2 = 2"
haftmann@51160
    56
| "nat_of_nibble Nibble3 = 3"
haftmann@51160
    57
| "nat_of_nibble Nibble4 = 4"
haftmann@51160
    58
| "nat_of_nibble Nibble5 = 5"
haftmann@51160
    59
| "nat_of_nibble Nibble6 = 6"
haftmann@51160
    60
| "nat_of_nibble Nibble7 = 7"
haftmann@51160
    61
| "nat_of_nibble Nibble8 = 8"
haftmann@51160
    62
| "nat_of_nibble Nibble9 = 9"
haftmann@51160
    63
| "nat_of_nibble NibbleA = 10"
haftmann@51160
    64
| "nat_of_nibble NibbleB = 11"
haftmann@51160
    65
| "nat_of_nibble NibbleC = 12"
haftmann@51160
    66
| "nat_of_nibble NibbleD = 13"
haftmann@51160
    67
| "nat_of_nibble NibbleE = 14"
haftmann@51160
    68
| "nat_of_nibble NibbleF = 15"
haftmann@51160
    69
haftmann@51160
    70
definition nibble_of_nat :: "nat \<Rightarrow> nibble" where
haftmann@51160
    71
  "nibble_of_nat n = Enum.enum ! (n mod 16)"
haftmann@51160
    72
haftmann@51160
    73
lemma nibble_of_nat_simps [simp]:
haftmann@51160
    74
  "nibble_of_nat  0 = Nibble0"
haftmann@51160
    75
  "nibble_of_nat  1 = Nibble1"
haftmann@51160
    76
  "nibble_of_nat  2 = Nibble2"
haftmann@51160
    77
  "nibble_of_nat  3 = Nibble3"
haftmann@51160
    78
  "nibble_of_nat  4 = Nibble4"
haftmann@51160
    79
  "nibble_of_nat  5 = Nibble5"
haftmann@51160
    80
  "nibble_of_nat  6 = Nibble6"
haftmann@51160
    81
  "nibble_of_nat  7 = Nibble7"
haftmann@51160
    82
  "nibble_of_nat  8 = Nibble8"
haftmann@51160
    83
  "nibble_of_nat  9 = Nibble9"
haftmann@51160
    84
  "nibble_of_nat 10 = NibbleA"
haftmann@51160
    85
  "nibble_of_nat 11 = NibbleB"
haftmann@51160
    86
  "nibble_of_nat 12 = NibbleC"
haftmann@51160
    87
  "nibble_of_nat 13 = NibbleD"
haftmann@51160
    88
  "nibble_of_nat 14 = NibbleE"
haftmann@51160
    89
  "nibble_of_nat 15 = NibbleF"
haftmann@51160
    90
  unfolding nibble_of_nat_def by (simp_all add: enum_nibble_def)
haftmann@51160
    91
haftmann@51160
    92
lemma nibble_of_nat_of_nibble [simp]:
haftmann@51160
    93
  "nibble_of_nat (nat_of_nibble x) = x"
haftmann@51160
    94
  by (cases x) (simp_all add: nibble_of_nat_def enum_nibble_def)
haftmann@51160
    95
haftmann@51160
    96
lemma nat_of_nibble_of_nat [simp]:
haftmann@51160
    97
  "nat_of_nibble (nibble_of_nat n) = n mod 16"
haftmann@51160
    98
  by (cases "nibble_of_nat n")
haftmann@51160
    99
     (simp_all add: nibble_of_nat_def enum_nibble_def nth_equal_first_eq nth_non_equal_first_eq, arith)
haftmann@51160
   100
haftmann@51160
   101
lemma inj_nat_of_nibble:
haftmann@51160
   102
  "inj nat_of_nibble"
haftmann@51160
   103
  by (rule inj_on_inverseI) (rule nibble_of_nat_of_nibble)
haftmann@51160
   104
haftmann@51160
   105
lemma nat_of_nibble_eq_iff:
haftmann@51160
   106
  "nat_of_nibble x = nat_of_nibble y \<longleftrightarrow> x = y"
haftmann@51160
   107
  by (rule inj_eq) (rule inj_nat_of_nibble)
haftmann@51160
   108
haftmann@51160
   109
lemma nat_of_nibble_less_16:
haftmann@51160
   110
  "nat_of_nibble x < 16"
haftmann@51160
   111
  by (cases x) auto
haftmann@51160
   112
haftmann@51160
   113
lemma nibble_of_nat_mod_16:
haftmann@51160
   114
  "nibble_of_nat (n mod 16) = nibble_of_nat n"
haftmann@51160
   115
  by (simp add: nibble_of_nat_def)
haftmann@51160
   116
blanchet@58310
   117
datatype char = Char nibble nibble
haftmann@31051
   118
  -- "Note: canonical order of character encoding coincides with standard term ordering"
haftmann@31051
   119
haftmann@31051
   120
syntax
wenzelm@46483
   121
  "_Char" :: "str_position => char"    ("CHR _")
haftmann@31051
   122
bulwahn@42163
   123
type_synonym string = "char list"
haftmann@31051
   124
haftmann@31051
   125
syntax
wenzelm@46483
   126
  "_String" :: "str_position => string"    ("_")
haftmann@31051
   127
wenzelm@48891
   128
ML_file "Tools/string_syntax.ML"
wenzelm@35123
   129
setup String_Syntax.setup
haftmann@31051
   130
haftmann@49972
   131
lemma UNIV_char:
haftmann@49972
   132
  "UNIV = image (split Char) (UNIV \<times> UNIV)"
haftmann@49972
   133
proof (rule UNIV_eq_I)
haftmann@49972
   134
  fix x show "x \<in> image (split Char) (UNIV \<times> UNIV)" by (cases x) auto
haftmann@49972
   135
qed
haftmann@49972
   136
haftmann@49972
   137
lemma size_char [code, simp]:
blanchet@56846
   138
  "size_char (c::char) = 0"
blanchet@56846
   139
  "size (c::char) = 0"
blanchet@56846
   140
  by (cases c, simp)+
haftmann@49972
   141
haftmann@49972
   142
instantiation char :: enum
haftmann@49972
   143
begin
haftmann@49972
   144
haftmann@49972
   145
definition
haftmann@49972
   146
  "Enum.enum = [Char Nibble0 Nibble0, Char Nibble0 Nibble1, Char Nibble0 Nibble2,
haftmann@31484
   147
  Char Nibble0 Nibble3, Char Nibble0 Nibble4, Char Nibble0 Nibble5,
haftmann@31484
   148
  Char Nibble0 Nibble6, Char Nibble0 Nibble7, Char Nibble0 Nibble8,
wenzelm@55015
   149
  Char Nibble0 Nibble9, CHR ''\<newline>'', Char Nibble0 NibbleB,
haftmann@31484
   150
  Char Nibble0 NibbleC, Char Nibble0 NibbleD, Char Nibble0 NibbleE,
haftmann@31484
   151
  Char Nibble0 NibbleF, Char Nibble1 Nibble0, Char Nibble1 Nibble1,
haftmann@31484
   152
  Char Nibble1 Nibble2, Char Nibble1 Nibble3, Char Nibble1 Nibble4,
haftmann@31484
   153
  Char Nibble1 Nibble5, Char Nibble1 Nibble6, Char Nibble1 Nibble7,
haftmann@31484
   154
  Char Nibble1 Nibble8, Char Nibble1 Nibble9, Char Nibble1 NibbleA,
haftmann@31484
   155
  Char Nibble1 NibbleB, Char Nibble1 NibbleC, Char Nibble1 NibbleD,
haftmann@31484
   156
  Char Nibble1 NibbleE, Char Nibble1 NibbleF, CHR '' '', CHR ''!'',
haftmann@31484
   157
  Char Nibble2 Nibble2, CHR ''#'', CHR ''$'', CHR ''%'', CHR ''&'',
haftmann@31484
   158
  Char Nibble2 Nibble7, CHR ''('', CHR '')'', CHR ''*'', CHR ''+'', CHR '','',
haftmann@31484
   159
  CHR ''-'', CHR ''.'', CHR ''/'', CHR ''0'', CHR ''1'', CHR ''2'', CHR ''3'',
haftmann@31484
   160
  CHR ''4'', CHR ''5'', CHR ''6'', CHR ''7'', CHR ''8'', CHR ''9'', CHR '':'',
haftmann@31484
   161
  CHR '';'', CHR ''<'', CHR ''='', CHR ''>'', CHR ''?'', CHR ''@'', CHR ''A'',
haftmann@31484
   162
  CHR ''B'', CHR ''C'', CHR ''D'', CHR ''E'', CHR ''F'', CHR ''G'', CHR ''H'',
haftmann@31484
   163
  CHR ''I'', CHR ''J'', CHR ''K'', CHR ''L'', CHR ''M'', CHR ''N'', CHR ''O'',
haftmann@31484
   164
  CHR ''P'', CHR ''Q'', CHR ''R'', CHR ''S'', CHR ''T'', CHR ''U'', CHR ''V'',
haftmann@31484
   165
  CHR ''W'', CHR ''X'', CHR ''Y'', CHR ''Z'', CHR ''['', Char Nibble5 NibbleC,
haftmann@31484
   166
  CHR '']'', CHR ''^'', CHR ''_'', Char Nibble6 Nibble0, CHR ''a'', CHR ''b'',
haftmann@31484
   167
  CHR ''c'', CHR ''d'', CHR ''e'', CHR ''f'', CHR ''g'', CHR ''h'', CHR ''i'',
haftmann@31484
   168
  CHR ''j'', CHR ''k'', CHR ''l'', CHR ''m'', CHR ''n'', CHR ''o'', CHR ''p'',
haftmann@31484
   169
  CHR ''q'', CHR ''r'', CHR ''s'', CHR ''t'', CHR ''u'', CHR ''v'', CHR ''w'',
haftmann@31484
   170
  CHR ''x'', CHR ''y'', CHR ''z'', CHR ''{'', CHR ''|'', CHR ''}'', CHR ''~'',
haftmann@31484
   171
  Char Nibble7 NibbleF, Char Nibble8 Nibble0, Char Nibble8 Nibble1,
haftmann@31484
   172
  Char Nibble8 Nibble2, Char Nibble8 Nibble3, Char Nibble8 Nibble4,
haftmann@31484
   173
  Char Nibble8 Nibble5, Char Nibble8 Nibble6, Char Nibble8 Nibble7,
haftmann@31484
   174
  Char Nibble8 Nibble8, Char Nibble8 Nibble9, Char Nibble8 NibbleA,
haftmann@31484
   175
  Char Nibble8 NibbleB, Char Nibble8 NibbleC, Char Nibble8 NibbleD,
haftmann@31484
   176
  Char Nibble8 NibbleE, Char Nibble8 NibbleF, Char Nibble9 Nibble0,
haftmann@31484
   177
  Char Nibble9 Nibble1, Char Nibble9 Nibble2, Char Nibble9 Nibble3,
haftmann@31484
   178
  Char Nibble9 Nibble4, Char Nibble9 Nibble5, Char Nibble9 Nibble6,
haftmann@31484
   179
  Char Nibble9 Nibble7, Char Nibble9 Nibble8, Char Nibble9 Nibble9,
haftmann@31484
   180
  Char Nibble9 NibbleA, Char Nibble9 NibbleB, Char Nibble9 NibbleC,
haftmann@31484
   181
  Char Nibble9 NibbleD, Char Nibble9 NibbleE, Char Nibble9 NibbleF,
haftmann@31484
   182
  Char NibbleA Nibble0, Char NibbleA Nibble1, Char NibbleA Nibble2,
haftmann@31484
   183
  Char NibbleA Nibble3, Char NibbleA Nibble4, Char NibbleA Nibble5,
haftmann@31484
   184
  Char NibbleA Nibble6, Char NibbleA Nibble7, Char NibbleA Nibble8,
haftmann@31484
   185
  Char NibbleA Nibble9, Char NibbleA NibbleA, Char NibbleA NibbleB,
haftmann@31484
   186
  Char NibbleA NibbleC, Char NibbleA NibbleD, Char NibbleA NibbleE,
haftmann@31484
   187
  Char NibbleA NibbleF, Char NibbleB Nibble0, Char NibbleB Nibble1,
haftmann@31484
   188
  Char NibbleB Nibble2, Char NibbleB Nibble3, Char NibbleB Nibble4,
haftmann@31484
   189
  Char NibbleB Nibble5, Char NibbleB Nibble6, Char NibbleB Nibble7,
haftmann@31484
   190
  Char NibbleB Nibble8, Char NibbleB Nibble9, Char NibbleB NibbleA,
haftmann@31484
   191
  Char NibbleB NibbleB, Char NibbleB NibbleC, Char NibbleB NibbleD,
haftmann@31484
   192
  Char NibbleB NibbleE, Char NibbleB NibbleF, Char NibbleC Nibble0,
haftmann@31484
   193
  Char NibbleC Nibble1, Char NibbleC Nibble2, Char NibbleC Nibble3,
haftmann@31484
   194
  Char NibbleC Nibble4, Char NibbleC Nibble5, Char NibbleC Nibble6,
haftmann@31484
   195
  Char NibbleC Nibble7, Char NibbleC Nibble8, Char NibbleC Nibble9,
haftmann@31484
   196
  Char NibbleC NibbleA, Char NibbleC NibbleB, Char NibbleC NibbleC,
haftmann@31484
   197
  Char NibbleC NibbleD, Char NibbleC NibbleE, Char NibbleC NibbleF,
haftmann@31484
   198
  Char NibbleD Nibble0, Char NibbleD Nibble1, Char NibbleD Nibble2,
haftmann@31484
   199
  Char NibbleD Nibble3, Char NibbleD Nibble4, Char NibbleD Nibble5,
haftmann@31484
   200
  Char NibbleD Nibble6, Char NibbleD Nibble7, Char NibbleD Nibble8,
haftmann@31484
   201
  Char NibbleD Nibble9, Char NibbleD NibbleA, Char NibbleD NibbleB,
haftmann@31484
   202
  Char NibbleD NibbleC, Char NibbleD NibbleD, Char NibbleD NibbleE,
haftmann@31484
   203
  Char NibbleD NibbleF, Char NibbleE Nibble0, Char NibbleE Nibble1,
haftmann@31484
   204
  Char NibbleE Nibble2, Char NibbleE Nibble3, Char NibbleE Nibble4,
haftmann@31484
   205
  Char NibbleE Nibble5, Char NibbleE Nibble6, Char NibbleE Nibble7,
haftmann@31484
   206
  Char NibbleE Nibble8, Char NibbleE Nibble9, Char NibbleE NibbleA,
haftmann@31484
   207
  Char NibbleE NibbleB, Char NibbleE NibbleC, Char NibbleE NibbleD,
haftmann@31484
   208
  Char NibbleE NibbleE, Char NibbleE NibbleF, Char NibbleF Nibble0,
haftmann@31484
   209
  Char NibbleF Nibble1, Char NibbleF Nibble2, Char NibbleF Nibble3,
haftmann@31484
   210
  Char NibbleF Nibble4, Char NibbleF Nibble5, Char NibbleF Nibble6,
haftmann@31484
   211
  Char NibbleF Nibble7, Char NibbleF Nibble8, Char NibbleF Nibble9,
haftmann@31484
   212
  Char NibbleF NibbleA, Char NibbleF NibbleB, Char NibbleF NibbleC,
haftmann@31484
   213
  Char NibbleF NibbleD, Char NibbleF NibbleE, Char NibbleF NibbleF]"
haftmann@31484
   214
haftmann@49972
   215
definition
haftmann@49972
   216
  "Enum.enum_all P \<longleftrightarrow> list_all P (Enum.enum :: char list)"
haftmann@49972
   217
haftmann@49972
   218
definition
haftmann@49972
   219
  "Enum.enum_ex P \<longleftrightarrow> list_ex P (Enum.enum :: char list)"
haftmann@49972
   220
haftmann@51160
   221
lemma enum_char_product_enum_nibble:
haftmann@51160
   222
  "(Enum.enum :: char list) = map (split Char) (List.product Enum.enum Enum.enum)"
haftmann@51160
   223
  by (simp add: enum_char_def enum_nibble_def)
haftmann@51160
   224
haftmann@49972
   225
instance proof
haftmann@49972
   226
  show UNIV: "UNIV = set (Enum.enum :: char list)"
nipkow@57247
   227
    by (simp add: enum_char_product_enum_nibble UNIV_char enum_UNIV)
haftmann@49972
   228
  show "distinct (Enum.enum :: char list)"
haftmann@51160
   229
    by (auto intro: inj_onI simp add: enum_char_product_enum_nibble distinct_map distinct_product enum_distinct)
haftmann@49972
   230
  show "\<And>P. Enum.enum_all P \<longleftrightarrow> Ball (UNIV :: char set) P"
haftmann@49972
   231
    by (simp add: UNIV enum_all_char_def list_all_iff)
haftmann@49972
   232
  show "\<And>P. Enum.enum_ex P \<longleftrightarrow> Bex (UNIV :: char set) P"
haftmann@49972
   233
    by (simp add: UNIV enum_ex_char_def list_ex_iff)
haftmann@49972
   234
qed
haftmann@49972
   235
haftmann@49972
   236
end
haftmann@49972
   237
haftmann@49972
   238
lemma card_UNIV_char:
haftmann@49972
   239
  "card (UNIV :: char set) = 256"
haftmann@49972
   240
  by (simp add: card_UNIV_length_enum enum_char_def)
haftmann@49948
   241
haftmann@51160
   242
definition nat_of_char :: "char \<Rightarrow> nat"
haftmann@51160
   243
where
haftmann@51160
   244
  "nat_of_char c = (case c of Char x y \<Rightarrow> nat_of_nibble x * 16 + nat_of_nibble y)"
haftmann@51160
   245
haftmann@51160
   246
lemma nat_of_char_Char:
haftmann@51160
   247
  "nat_of_char (Char x y) = nat_of_nibble x * 16 + nat_of_nibble y"
haftmann@51160
   248
  by (simp add: nat_of_char_def)
haftmann@49972
   249
haftmann@49972
   250
setup {*
haftmann@49972
   251
let
haftmann@49972
   252
  val nibbles = map_range (Thm.cterm_of @{theory} o HOLogic.mk_nibble) 16;
wenzelm@51717
   253
  val simpset =
wenzelm@51717
   254
    put_simpset HOL_ss @{context}
wenzelm@51717
   255
      addsimps @{thms nat_of_nibble.simps mult_0 mult_1 add_0 add_0_right arith_simps numeral_plus_one};
haftmann@51160
   256
  fun mk_code_eqn x y =
haftmann@51160
   257
    Drule.instantiate' [] [SOME x, SOME y] @{thm nat_of_char_Char}
haftmann@51160
   258
    |> simplify simpset;
haftmann@51160
   259
  val code_eqns = map_product mk_code_eqn nibbles nibbles;
haftmann@49972
   260
in
haftmann@49972
   261
  Global_Theory.note_thmss ""
haftmann@51160
   262
    [((@{binding nat_of_char_simps}, []), [(code_eqns, [])])]
haftmann@51160
   263
  #> snd
haftmann@49972
   264
end
haftmann@49972
   265
*}
haftmann@49972
   266
haftmann@51160
   267
declare nat_of_char_simps [code]
haftmann@51160
   268
haftmann@51160
   269
lemma nibble_of_nat_of_char_div_16:
haftmann@51160
   270
  "nibble_of_nat (nat_of_char c div 16) = (case c of Char x y \<Rightarrow> x)"
haftmann@51160
   271
  by (cases c)
haftmann@57512
   272
    (simp add: nat_of_char_def add.commute nat_of_nibble_less_16)
haftmann@51160
   273
haftmann@51160
   274
lemma nibble_of_nat_nat_of_char:
haftmann@51160
   275
  "nibble_of_nat (nat_of_char c) = (case c of Char x y \<Rightarrow> y)"
haftmann@51160
   276
proof (cases c)
haftmann@51160
   277
  case (Char x y)
haftmann@51160
   278
  then have *: "nibble_of_nat ((nat_of_nibble y + nat_of_nibble x * 16) mod 16) = y"
haftmann@51160
   279
    by (simp add: nibble_of_nat_mod_16)
haftmann@51160
   280
  then have "nibble_of_nat (nat_of_nibble y + nat_of_nibble x * 16) = y"
haftmann@51160
   281
    by (simp only: nibble_of_nat_mod_16)
haftmann@57512
   282
  with Char show ?thesis by (simp add: nat_of_char_def add.commute)
haftmann@51160
   283
qed
haftmann@51160
   284
haftmann@51160
   285
code_datatype Char -- {* drop case certificate for char *}
haftmann@51160
   286
blanchet@55416
   287
lemma case_char_code [code]:
blanchet@55416
   288
  "case_char f c = (let n = nat_of_char c in f (nibble_of_nat (n div 16)) (nibble_of_nat n))"
haftmann@51160
   289
  by (cases c)
blanchet@55642
   290
    (simp only: Let_def nibble_of_nat_of_char_div_16 nibble_of_nat_nat_of_char char.case)
haftmann@51160
   291
haftmann@51160
   292
lemma [code]:
blanchet@55416
   293
  "rec_char = case_char"
haftmann@49972
   294
  by (simp add: fun_eq_iff split: char.split)
haftmann@49972
   295
haftmann@51160
   296
definition char_of_nat :: "nat \<Rightarrow> char" where
haftmann@51160
   297
  "char_of_nat n = Enum.enum ! (n mod 256)"
haftmann@51160
   298
haftmann@51160
   299
lemma char_of_nat_Char_nibbles:
haftmann@51160
   300
  "char_of_nat n = Char (nibble_of_nat (n div 16)) (nibble_of_nat n)"
haftmann@51160
   301
proof -
haftmann@51160
   302
  from mod_mult2_eq [of n 16 16]
haftmann@51160
   303
  have "n mod 256 = 16 * (n div 16 mod 16) + n mod 16" by simp
haftmann@51160
   304
  then show ?thesis
haftmann@51160
   305
    by (simp add: char_of_nat_def enum_char_product_enum_nibble card_UNIV_nibble
haftmann@57512
   306
      card_UNIV_length_enum [symmetric] nibble_of_nat_def product_nth add.commute)
haftmann@51160
   307
qed
haftmann@51160
   308
haftmann@51160
   309
lemma char_of_nat_of_char [simp]:
haftmann@51160
   310
  "char_of_nat (nat_of_char c) = c"
haftmann@51160
   311
  by (cases c)
haftmann@51160
   312
    (simp add: char_of_nat_Char_nibbles nibble_of_nat_of_char_div_16 nibble_of_nat_nat_of_char)
haftmann@51160
   313
haftmann@51160
   314
lemma nat_of_char_of_nat [simp]:
haftmann@51160
   315
  "nat_of_char (char_of_nat n) = n mod 256"
haftmann@51160
   316
proof -
haftmann@51160
   317
  have "n mod 256 = n mod (16 * 16)" by simp
haftmann@51160
   318
  then have "n div 16 mod 16 * 16 + n mod 16 = n mod 256" by (simp only: mod_mult2_eq)
haftmann@51160
   319
  then show ?thesis
haftmann@51160
   320
    by (cases "char_of_nat n") (auto simp add: nat_of_char_def char_of_nat_Char_nibbles)
haftmann@51160
   321
qed
haftmann@51160
   322
haftmann@51160
   323
lemma inj_nat_of_char:
haftmann@51160
   324
  "inj nat_of_char"
haftmann@51160
   325
  by (rule inj_on_inverseI) (rule char_of_nat_of_char)
haftmann@51160
   326
haftmann@51160
   327
lemma nat_of_char_eq_iff:
haftmann@51160
   328
  "nat_of_char c = nat_of_char d \<longleftrightarrow> c = d"
haftmann@51160
   329
  by (rule inj_eq) (rule inj_nat_of_char)
haftmann@51160
   330
haftmann@51160
   331
lemma nat_of_char_less_256:
haftmann@51160
   332
  "nat_of_char c < 256"
haftmann@51160
   333
proof (cases c)
haftmann@51160
   334
  case (Char x y)
haftmann@51160
   335
  with nat_of_nibble_less_16 [of x] nat_of_nibble_less_16 [of y]
haftmann@51160
   336
  show ?thesis by (simp add: nat_of_char_def)
haftmann@51160
   337
qed
haftmann@51160
   338
haftmann@51160
   339
lemma char_of_nat_mod_256:
haftmann@51160
   340
  "char_of_nat (n mod 256) = char_of_nat n"
haftmann@51160
   341
proof -
haftmann@51160
   342
  from nibble_of_nat_mod_16 [of "n mod 256"]
haftmann@51160
   343
  have "nibble_of_nat (n mod 256) = nibble_of_nat (n mod 256 mod 16)" by simp
haftmann@51160
   344
  with nibble_of_nat_mod_16 [of n]
haftmann@51160
   345
  have *: "nibble_of_nat (n mod 256) = nibble_of_nat n" by (simp add: mod_mod_cancel)
haftmann@51160
   346
  have "n mod 256 = n mod (16 * 16)" by simp
haftmann@51160
   347
  then have **: "n mod 256 = n div 16 mod 16 * 16 + n mod 16" by (simp only: mod_mult2_eq)
haftmann@51160
   348
  show ?thesis
haftmann@51160
   349
    by (simp add: char_of_nat_Char_nibbles *)
haftmann@51160
   350
     (simp add: div_add1_eq nibble_of_nat_mod_16 [of "n div 16"] **)
haftmann@51160
   351
qed
haftmann@49948
   352
haftmann@31051
   353
bulwahn@39250
   354
subsection {* Strings as dedicated type *}
bulwahn@39250
   355
wenzelm@49834
   356
typedef literal = "UNIV :: string set"
bulwahn@39250
   357
  morphisms explode STR ..
bulwahn@39250
   358
Andreas@54594
   359
setup_lifting (no_code) type_definition_literal
Andreas@54594
   360
haftmann@57437
   361
definition implode :: "string \<Rightarrow> String.literal"
haftmann@57437
   362
where
haftmann@57437
   363
  "implode = STR"
haftmann@57437
   364
bulwahn@39250
   365
instantiation literal :: size
bulwahn@39250
   366
begin
haftmann@31051
   367
bulwahn@39250
   368
definition size_literal :: "literal \<Rightarrow> nat"
bulwahn@39250
   369
where
bulwahn@39250
   370
  [code]: "size_literal (s\<Colon>literal) = 0"
haftmann@31051
   371
bulwahn@39250
   372
instance ..
bulwahn@39250
   373
bulwahn@39250
   374
end
bulwahn@39250
   375
bulwahn@39250
   376
instantiation literal :: equal
bulwahn@39250
   377
begin
haftmann@31051
   378
Andreas@54594
   379
lift_definition equal_literal :: "literal \<Rightarrow> literal \<Rightarrow> bool" is "op =" .
haftmann@31051
   380
Andreas@54594
   381
instance by intro_classes (transfer, simp)
bulwahn@39250
   382
bulwahn@39250
   383
end
haftmann@31051
   384
Andreas@54594
   385
declare equal_literal.rep_eq[code]
Andreas@54594
   386
haftmann@52365
   387
lemma [code nbe]:
haftmann@52365
   388
  fixes s :: "String.literal"
haftmann@52365
   389
  shows "HOL.equal s s \<longleftrightarrow> True"
haftmann@52365
   390
  by (simp add: equal)
haftmann@52365
   391
haftmann@51160
   392
lemma STR_inject' [simp]:
haftmann@51160
   393
  "STR xs = STR ys \<longleftrightarrow> xs = ys"
haftmann@51160
   394
  by (simp add: STR_inject)
bulwahn@39274
   395
Andreas@55426
   396
lifting_update literal.lifting
Andreas@55426
   397
lifting_forget literal.lifting
Andreas@55426
   398
haftmann@31051
   399
subsection {* Code generator *}
haftmann@31051
   400
wenzelm@48891
   401
ML_file "Tools/string_code.ML"
haftmann@31051
   402
haftmann@33237
   403
code_reserved SML string
haftmann@33237
   404
code_reserved OCaml string
haftmann@34886
   405
code_reserved Scala string
haftmann@33237
   406
haftmann@52435
   407
code_printing
haftmann@52435
   408
  type_constructor literal \<rightharpoonup>
haftmann@52435
   409
    (SML) "string"
haftmann@52435
   410
    and (OCaml) "string"
haftmann@52435
   411
    and (Haskell) "String"
haftmann@52435
   412
    and (Scala) "String"
haftmann@31051
   413
haftmann@31051
   414
setup {*
haftmann@34886
   415
  fold String_Code.add_literal_string ["SML", "OCaml", "Haskell", "Scala"]
haftmann@31051
   416
*}
haftmann@31051
   417
haftmann@52435
   418
code_printing
haftmann@52435
   419
  class_instance literal :: equal \<rightharpoonup>
haftmann@52435
   420
    (Haskell) -
haftmann@52435
   421
| constant "HOL.equal :: literal \<Rightarrow> literal \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   422
    (SML) "!((_ : string) = _)"
haftmann@52435
   423
    and (OCaml) "!((_ : string) = _)"
haftmann@52435
   424
    and (Haskell) infix 4 "=="
haftmann@52435
   425
    and (Scala) infixl 5 "=="
haftmann@31051
   426
Andreas@52910
   427
setup {* Sign.map_naming (Name_Space.mandatory_path "Code") *}
Andreas@52910
   428
Andreas@52910
   429
definition abort :: "literal \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> 'a"
Andreas@52910
   430
where [simp, code del]: "abort _ f = f ()"
Andreas@52910
   431
Andreas@54317
   432
lemma abort_cong: "msg = msg' ==> Code.abort msg f = Code.abort msg' f"
Andreas@54317
   433
by simp
Andreas@54317
   434
Andreas@52910
   435
setup {* Sign.map_naming Name_Space.parent_path *}
Andreas@52910
   436
Andreas@54317
   437
setup {* Code_Simp.map_ss (Simplifier.add_cong @{thm Code.abort_cong}) *}
Andreas@54317
   438
Andreas@52910
   439
code_printing constant Code.abort \<rightharpoonup>
Andreas@52910
   440
    (SML) "!(raise/ Fail/ _)"
Andreas@52910
   441
    and (OCaml) "failwith"
Andreas@52910
   442
    and (Haskell) "error"
Andreas@52910
   443
    and (Scala) "!{/ sys.error((_));/  ((_)).apply(())/ }"
Andreas@52910
   444
wenzelm@36176
   445
hide_type (open) literal
haftmann@31205
   446
haftmann@57437
   447
hide_const (open) implode explode
haftmann@57437
   448
bulwahn@39250
   449
end