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