src/HOL/String.thy
author hoelzl
Thu Jan 31 11:31:27 2013 +0100 (2013-01-31)
changeset 50999 3de230ed0547
parent 49972 f11f8905d9fd
child 51160 599ff65b85e2
permissions -rw-r--r--
introduce order topology
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
haftmann@49972
     6
imports List Enum
haftmann@31051
     7
begin
haftmann@31051
     8
haftmann@49972
     9
subsection {* Characters and strings *}
haftmann@31051
    10
haftmann@31051
    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]:
haftmann@49972
    23
  "size (x::nibble) = 0" by (cases x) simp_all
haftmann@49972
    24
haftmann@49972
    25
lemma nibble_size [code, simp]:
haftmann@49972
    26
  "nibble_size (x::nibble) = 0" by (cases x) simp_all
haftmann@49972
    27
haftmann@49972
    28
instantiation nibble :: enum
haftmann@49972
    29
begin
haftmann@49972
    30
haftmann@49972
    31
definition
haftmann@49972
    32
  "Enum.enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@49972
    33
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@49972
    34
haftmann@49972
    35
definition
haftmann@49972
    36
  "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
    37
     \<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
    38
haftmann@49972
    39
definition
haftmann@49972
    40
  "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
    41
     \<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
    42
haftmann@49972
    43
instance proof
haftmann@49972
    44
qed (simp_all only: enum_nibble_def enum_all_nibble_def enum_ex_nibble_def UNIV_nibble, simp_all)
haftmann@49972
    45
haftmann@49972
    46
end
haftmann@49972
    47
haftmann@49972
    48
lemma card_UNIV_nibble:
haftmann@49972
    49
  "card (UNIV :: nibble set) = 16"
haftmann@49972
    50
  by (simp add: card_UNIV_length_enum enum_nibble_def)
haftmann@31051
    51
haftmann@31051
    52
datatype char = Char nibble nibble
haftmann@31051
    53
  -- "Note: canonical order of character encoding coincides with standard term ordering"
haftmann@31051
    54
haftmann@31051
    55
syntax
wenzelm@46483
    56
  "_Char" :: "str_position => char"    ("CHR _")
haftmann@31051
    57
bulwahn@42163
    58
type_synonym string = "char list"
haftmann@31051
    59
haftmann@31051
    60
syntax
wenzelm@46483
    61
  "_String" :: "str_position => string"    ("_")
haftmann@31051
    62
wenzelm@48891
    63
ML_file "Tools/string_syntax.ML"
wenzelm@35123
    64
setup String_Syntax.setup
haftmann@31051
    65
haftmann@49972
    66
lemma UNIV_char:
haftmann@49972
    67
  "UNIV = image (split Char) (UNIV \<times> UNIV)"
haftmann@49972
    68
proof (rule UNIV_eq_I)
haftmann@49972
    69
  fix x show "x \<in> image (split Char) (UNIV \<times> UNIV)" by (cases x) auto
haftmann@49972
    70
qed
haftmann@49972
    71
haftmann@49972
    72
lemma size_char [code, simp]:
haftmann@49972
    73
  "size (c::char) = 0" by (cases c) simp
haftmann@49972
    74
haftmann@49972
    75
lemma char_size [code, simp]:
haftmann@49972
    76
  "char_size (c::char) = 0" by (cases c) simp
haftmann@49972
    77
haftmann@49972
    78
instantiation char :: enum
haftmann@49972
    79
begin
haftmann@49972
    80
haftmann@49972
    81
definition
haftmann@49972
    82
  "Enum.enum = [Char Nibble0 Nibble0, Char Nibble0 Nibble1, Char Nibble0 Nibble2,
haftmann@31484
    83
  Char Nibble0 Nibble3, Char Nibble0 Nibble4, Char Nibble0 Nibble5,
haftmann@31484
    84
  Char Nibble0 Nibble6, Char Nibble0 Nibble7, Char Nibble0 Nibble8,
haftmann@31484
    85
  Char Nibble0 Nibble9, Char Nibble0 NibbleA, Char Nibble0 NibbleB,
haftmann@31484
    86
  Char Nibble0 NibbleC, Char Nibble0 NibbleD, Char Nibble0 NibbleE,
haftmann@31484
    87
  Char Nibble0 NibbleF, Char Nibble1 Nibble0, Char Nibble1 Nibble1,
haftmann@31484
    88
  Char Nibble1 Nibble2, Char Nibble1 Nibble3, Char Nibble1 Nibble4,
haftmann@31484
    89
  Char Nibble1 Nibble5, Char Nibble1 Nibble6, Char Nibble1 Nibble7,
haftmann@31484
    90
  Char Nibble1 Nibble8, Char Nibble1 Nibble9, Char Nibble1 NibbleA,
haftmann@31484
    91
  Char Nibble1 NibbleB, Char Nibble1 NibbleC, Char Nibble1 NibbleD,
haftmann@31484
    92
  Char Nibble1 NibbleE, Char Nibble1 NibbleF, CHR '' '', CHR ''!'',
haftmann@31484
    93
  Char Nibble2 Nibble2, CHR ''#'', CHR ''$'', CHR ''%'', CHR ''&'',
haftmann@31484
    94
  Char Nibble2 Nibble7, CHR ''('', CHR '')'', CHR ''*'', CHR ''+'', CHR '','',
haftmann@31484
    95
  CHR ''-'', CHR ''.'', CHR ''/'', CHR ''0'', CHR ''1'', CHR ''2'', CHR ''3'',
haftmann@31484
    96
  CHR ''4'', CHR ''5'', CHR ''6'', CHR ''7'', CHR ''8'', CHR ''9'', CHR '':'',
haftmann@31484
    97
  CHR '';'', CHR ''<'', CHR ''='', CHR ''>'', CHR ''?'', CHR ''@'', CHR ''A'',
haftmann@31484
    98
  CHR ''B'', CHR ''C'', CHR ''D'', CHR ''E'', CHR ''F'', CHR ''G'', CHR ''H'',
haftmann@31484
    99
  CHR ''I'', CHR ''J'', CHR ''K'', CHR ''L'', CHR ''M'', CHR ''N'', CHR ''O'',
haftmann@31484
   100
  CHR ''P'', CHR ''Q'', CHR ''R'', CHR ''S'', CHR ''T'', CHR ''U'', CHR ''V'',
haftmann@31484
   101
  CHR ''W'', CHR ''X'', CHR ''Y'', CHR ''Z'', CHR ''['', Char Nibble5 NibbleC,
haftmann@31484
   102
  CHR '']'', CHR ''^'', CHR ''_'', Char Nibble6 Nibble0, CHR ''a'', CHR ''b'',
haftmann@31484
   103
  CHR ''c'', CHR ''d'', CHR ''e'', CHR ''f'', CHR ''g'', CHR ''h'', CHR ''i'',
haftmann@31484
   104
  CHR ''j'', CHR ''k'', CHR ''l'', CHR ''m'', CHR ''n'', CHR ''o'', CHR ''p'',
haftmann@31484
   105
  CHR ''q'', CHR ''r'', CHR ''s'', CHR ''t'', CHR ''u'', CHR ''v'', CHR ''w'',
haftmann@31484
   106
  CHR ''x'', CHR ''y'', CHR ''z'', CHR ''{'', CHR ''|'', CHR ''}'', CHR ''~'',
haftmann@31484
   107
  Char Nibble7 NibbleF, Char Nibble8 Nibble0, Char Nibble8 Nibble1,
haftmann@31484
   108
  Char Nibble8 Nibble2, Char Nibble8 Nibble3, Char Nibble8 Nibble4,
haftmann@31484
   109
  Char Nibble8 Nibble5, Char Nibble8 Nibble6, Char Nibble8 Nibble7,
haftmann@31484
   110
  Char Nibble8 Nibble8, Char Nibble8 Nibble9, Char Nibble8 NibbleA,
haftmann@31484
   111
  Char Nibble8 NibbleB, Char Nibble8 NibbleC, Char Nibble8 NibbleD,
haftmann@31484
   112
  Char Nibble8 NibbleE, Char Nibble8 NibbleF, Char Nibble9 Nibble0,
haftmann@31484
   113
  Char Nibble9 Nibble1, Char Nibble9 Nibble2, Char Nibble9 Nibble3,
haftmann@31484
   114
  Char Nibble9 Nibble4, Char Nibble9 Nibble5, Char Nibble9 Nibble6,
haftmann@31484
   115
  Char Nibble9 Nibble7, Char Nibble9 Nibble8, Char Nibble9 Nibble9,
haftmann@31484
   116
  Char Nibble9 NibbleA, Char Nibble9 NibbleB, Char Nibble9 NibbleC,
haftmann@31484
   117
  Char Nibble9 NibbleD, Char Nibble9 NibbleE, Char Nibble9 NibbleF,
haftmann@31484
   118
  Char NibbleA Nibble0, Char NibbleA Nibble1, Char NibbleA Nibble2,
haftmann@31484
   119
  Char NibbleA Nibble3, Char NibbleA Nibble4, Char NibbleA Nibble5,
haftmann@31484
   120
  Char NibbleA Nibble6, Char NibbleA Nibble7, Char NibbleA Nibble8,
haftmann@31484
   121
  Char NibbleA Nibble9, Char NibbleA NibbleA, Char NibbleA NibbleB,
haftmann@31484
   122
  Char NibbleA NibbleC, Char NibbleA NibbleD, Char NibbleA NibbleE,
haftmann@31484
   123
  Char NibbleA NibbleF, Char NibbleB Nibble0, Char NibbleB Nibble1,
haftmann@31484
   124
  Char NibbleB Nibble2, Char NibbleB Nibble3, Char NibbleB Nibble4,
haftmann@31484
   125
  Char NibbleB Nibble5, Char NibbleB Nibble6, Char NibbleB Nibble7,
haftmann@31484
   126
  Char NibbleB Nibble8, Char NibbleB Nibble9, Char NibbleB NibbleA,
haftmann@31484
   127
  Char NibbleB NibbleB, Char NibbleB NibbleC, Char NibbleB NibbleD,
haftmann@31484
   128
  Char NibbleB NibbleE, Char NibbleB NibbleF, Char NibbleC Nibble0,
haftmann@31484
   129
  Char NibbleC Nibble1, Char NibbleC Nibble2, Char NibbleC Nibble3,
haftmann@31484
   130
  Char NibbleC Nibble4, Char NibbleC Nibble5, Char NibbleC Nibble6,
haftmann@31484
   131
  Char NibbleC Nibble7, Char NibbleC Nibble8, Char NibbleC Nibble9,
haftmann@31484
   132
  Char NibbleC NibbleA, Char NibbleC NibbleB, Char NibbleC NibbleC,
haftmann@31484
   133
  Char NibbleC NibbleD, Char NibbleC NibbleE, Char NibbleC NibbleF,
haftmann@31484
   134
  Char NibbleD Nibble0, Char NibbleD Nibble1, Char NibbleD Nibble2,
haftmann@31484
   135
  Char NibbleD Nibble3, Char NibbleD Nibble4, Char NibbleD Nibble5,
haftmann@31484
   136
  Char NibbleD Nibble6, Char NibbleD Nibble7, Char NibbleD Nibble8,
haftmann@31484
   137
  Char NibbleD Nibble9, Char NibbleD NibbleA, Char NibbleD NibbleB,
haftmann@31484
   138
  Char NibbleD NibbleC, Char NibbleD NibbleD, Char NibbleD NibbleE,
haftmann@31484
   139
  Char NibbleD NibbleF, Char NibbleE Nibble0, Char NibbleE Nibble1,
haftmann@31484
   140
  Char NibbleE Nibble2, Char NibbleE Nibble3, Char NibbleE Nibble4,
haftmann@31484
   141
  Char NibbleE Nibble5, Char NibbleE Nibble6, Char NibbleE Nibble7,
haftmann@31484
   142
  Char NibbleE Nibble8, Char NibbleE Nibble9, Char NibbleE NibbleA,
haftmann@31484
   143
  Char NibbleE NibbleB, Char NibbleE NibbleC, Char NibbleE NibbleD,
haftmann@31484
   144
  Char NibbleE NibbleE, Char NibbleE NibbleF, Char NibbleF Nibble0,
haftmann@31484
   145
  Char NibbleF Nibble1, Char NibbleF Nibble2, Char NibbleF Nibble3,
haftmann@31484
   146
  Char NibbleF Nibble4, Char NibbleF Nibble5, Char NibbleF Nibble6,
haftmann@31484
   147
  Char NibbleF Nibble7, Char NibbleF Nibble8, Char NibbleF Nibble9,
haftmann@31484
   148
  Char NibbleF NibbleA, Char NibbleF NibbleB, Char NibbleF NibbleC,
haftmann@31484
   149
  Char NibbleF NibbleD, Char NibbleF NibbleE, Char NibbleF NibbleF]"
haftmann@31484
   150
haftmann@49972
   151
definition
haftmann@49972
   152
  "Enum.enum_all P \<longleftrightarrow> list_all P (Enum.enum :: char list)"
haftmann@49972
   153
haftmann@49972
   154
definition
haftmann@49972
   155
  "Enum.enum_ex P \<longleftrightarrow> list_ex P (Enum.enum :: char list)"
haftmann@49972
   156
haftmann@49972
   157
instance proof
haftmann@49972
   158
  have enum: "(Enum.enum :: char list) = map (split Char) (List.product Enum.enum Enum.enum)"
haftmann@49972
   159
    by (simp add: enum_char_def enum_nibble_def)
haftmann@49972
   160
  show UNIV: "UNIV = set (Enum.enum :: char list)"
haftmann@49972
   161
    by (simp add: enum UNIV_char product_list_set enum_UNIV)
haftmann@49972
   162
  show "distinct (Enum.enum :: char list)"
haftmann@49972
   163
    by (auto intro: inj_onI simp add: enum distinct_map distinct_product enum_distinct)
haftmann@49972
   164
  show "\<And>P. Enum.enum_all P \<longleftrightarrow> Ball (UNIV :: char set) P"
haftmann@49972
   165
    by (simp add: UNIV enum_all_char_def list_all_iff)
haftmann@49972
   166
  show "\<And>P. Enum.enum_ex P \<longleftrightarrow> Bex (UNIV :: char set) P"
haftmann@49972
   167
    by (simp add: UNIV enum_ex_char_def list_ex_iff)
haftmann@49972
   168
qed
haftmann@49972
   169
haftmann@49972
   170
end
haftmann@49972
   171
haftmann@49972
   172
lemma card_UNIV_char:
haftmann@49972
   173
  "card (UNIV :: char set) = 256"
haftmann@49972
   174
  by (simp add: card_UNIV_length_enum enum_char_def)
haftmann@49948
   175
haftmann@49972
   176
primrec nibble_pair_of_char :: "char \<Rightarrow> nibble \<times> nibble" where
haftmann@49972
   177
  "nibble_pair_of_char (Char n m) = (n, m)"
haftmann@49972
   178
haftmann@49972
   179
setup {*
haftmann@49972
   180
let
haftmann@49972
   181
  val nibbles = map_range (Thm.cterm_of @{theory} o HOLogic.mk_nibble) 16;
haftmann@49972
   182
  val thms = map_product
haftmann@49972
   183
   (fn n => fn m => Drule.instantiate' [] [SOME n, SOME m] @{thm nibble_pair_of_char.simps})
haftmann@49972
   184
      nibbles nibbles;
haftmann@49972
   185
in
haftmann@49972
   186
  Global_Theory.note_thmss ""
haftmann@49972
   187
    [((@{binding nibble_pair_of_char_simps}, []), [(thms, [])])]
haftmann@49972
   188
  #-> (fn [(_, thms)] => fold_rev Code.add_eqn thms)
haftmann@49972
   189
end
haftmann@49972
   190
*}
haftmann@49972
   191
haftmann@49972
   192
lemma char_case_nibble_pair [code, code_unfold]:
haftmann@49972
   193
  "char_case f = split f o nibble_pair_of_char"
haftmann@49972
   194
  by (simp add: fun_eq_iff split: char.split)
haftmann@49972
   195
haftmann@49972
   196
lemma char_rec_nibble_pair [code, code_unfold]:
haftmann@49972
   197
  "char_rec f = split f o nibble_pair_of_char"
haftmann@49972
   198
  unfolding char_case_nibble_pair [symmetric]
haftmann@49972
   199
  by (simp add: fun_eq_iff split: char.split)
haftmann@49948
   200
haftmann@31051
   201
bulwahn@39250
   202
subsection {* Strings as dedicated type *}
bulwahn@39250
   203
wenzelm@49834
   204
typedef literal = "UNIV :: string set"
bulwahn@39250
   205
  morphisms explode STR ..
bulwahn@39250
   206
bulwahn@39250
   207
instantiation literal :: size
bulwahn@39250
   208
begin
haftmann@31051
   209
bulwahn@39250
   210
definition size_literal :: "literal \<Rightarrow> nat"
bulwahn@39250
   211
where
bulwahn@39250
   212
  [code]: "size_literal (s\<Colon>literal) = 0"
haftmann@31051
   213
bulwahn@39250
   214
instance ..
bulwahn@39250
   215
bulwahn@39250
   216
end
bulwahn@39250
   217
bulwahn@39250
   218
instantiation literal :: equal
bulwahn@39250
   219
begin
haftmann@31051
   220
bulwahn@39250
   221
definition equal_literal :: "literal \<Rightarrow> literal \<Rightarrow> bool"
bulwahn@39250
   222
where
bulwahn@39250
   223
  "equal_literal s1 s2 \<longleftrightarrow> explode s1 = explode s2"
haftmann@31051
   224
bulwahn@39250
   225
instance
bulwahn@39250
   226
proof
bulwahn@39250
   227
qed (auto simp add: equal_literal_def explode_inject)
bulwahn@39250
   228
bulwahn@39250
   229
end
haftmann@31051
   230
bulwahn@39274
   231
lemma STR_inject' [simp]: "(STR xs = STR ys) = (xs = ys)"
bulwahn@39274
   232
by(simp add: STR_inject)
bulwahn@39274
   233
bulwahn@39274
   234
haftmann@31051
   235
subsection {* Code generator *}
haftmann@31051
   236
wenzelm@48891
   237
ML_file "Tools/string_code.ML"
haftmann@31051
   238
haftmann@33237
   239
code_reserved SML string
haftmann@33237
   240
code_reserved OCaml string
haftmann@34886
   241
code_reserved Scala string
haftmann@33237
   242
haftmann@31205
   243
code_type literal
haftmann@31051
   244
  (SML "string")
haftmann@31051
   245
  (OCaml "string")
haftmann@31051
   246
  (Haskell "String")
haftmann@34886
   247
  (Scala "String")
haftmann@31051
   248
haftmann@31051
   249
setup {*
haftmann@34886
   250
  fold String_Code.add_literal_string ["SML", "OCaml", "Haskell", "Scala"]
haftmann@31051
   251
*}
haftmann@31051
   252
haftmann@38857
   253
code_instance literal :: equal
haftmann@31051
   254
  (Haskell -)
haftmann@31051
   255
haftmann@38857
   256
code_const "HOL.equal \<Colon> literal \<Rightarrow> literal \<Rightarrow> bool"
haftmann@31051
   257
  (SML "!((_ : string) = _)")
haftmann@31051
   258
  (OCaml "!((_ : string) = _)")
haftmann@39272
   259
  (Haskell infix 4 "==")
haftmann@34886
   260
  (Scala infixl 5 "==")
haftmann@31051
   261
wenzelm@36176
   262
hide_type (open) literal
haftmann@31205
   263
bulwahn@39250
   264
end
haftmann@49948
   265