src/HOL/String.thy
author hoelzl
Thu Sep 02 10:14:32 2010 +0200 (2010-09-02)
changeset 39072 1030b1a166ef
parent 38858 1920158cfa17
child 39198 f967a16dfcdd
permissions -rw-r--r--
Add lessThan_Suc_eq_insert_0
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@31055
     7
uses
wenzelm@35115
     8
  ("Tools/string_syntax.ML")
haftmann@31055
     9
  ("Tools/string_code.ML")
haftmann@31051
    10
begin
haftmann@31051
    11
haftmann@31051
    12
subsection {* Characters *}
haftmann@31051
    13
haftmann@31051
    14
datatype nibble =
haftmann@31051
    15
    Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
haftmann@31051
    16
  | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
haftmann@31051
    17
haftmann@31051
    18
lemma UNIV_nibble:
haftmann@31051
    19
  "UNIV = {Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@31051
    20
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF}" (is "_ = ?A")
haftmann@31051
    21
proof (rule UNIV_eq_I)
haftmann@31051
    22
  fix x show "x \<in> ?A" by (cases x) simp_all
haftmann@31051
    23
qed
haftmann@31051
    24
haftmann@31051
    25
instance nibble :: finite
haftmann@31051
    26
  by default (simp add: UNIV_nibble)
haftmann@31051
    27
haftmann@31051
    28
datatype char = Char nibble nibble
haftmann@31051
    29
  -- "Note: canonical order of character encoding coincides with standard term ordering"
haftmann@31051
    30
haftmann@31051
    31
lemma UNIV_char:
haftmann@31051
    32
  "UNIV = image (split Char) (UNIV \<times> UNIV)"
haftmann@31051
    33
proof (rule UNIV_eq_I)
haftmann@31051
    34
  fix x show "x \<in> image (split Char) (UNIV \<times> UNIV)" by (cases x) auto
haftmann@31051
    35
qed
haftmann@31051
    36
haftmann@31051
    37
instance char :: finite
haftmann@31051
    38
  by default (simp add: UNIV_char)
haftmann@31051
    39
haftmann@31051
    40
lemma size_char [code, simp]:
haftmann@31051
    41
  "size (c::char) = 0" by (cases c) simp
haftmann@31051
    42
haftmann@31051
    43
lemma char_size [code, simp]:
haftmann@31051
    44
  "char_size (c::char) = 0" by (cases c) simp
haftmann@31051
    45
haftmann@31051
    46
primrec nibble_pair_of_char :: "char \<Rightarrow> nibble \<times> nibble" where
haftmann@31051
    47
  "nibble_pair_of_char (Char n m) = (n, m)"
haftmann@31051
    48
haftmann@31051
    49
setup {*
haftmann@31051
    50
let
haftmann@33063
    51
  val nibbles = map_range (Thm.cterm_of @{theory} o HOLogic.mk_nibble) 16;
haftmann@31051
    52
  val thms = map_product
haftmann@31051
    53
   (fn n => fn m => Drule.instantiate' [] [SOME n, SOME m] @{thm nibble_pair_of_char.simps})
haftmann@31051
    54
      nibbles nibbles;
haftmann@31051
    55
in
wenzelm@38808
    56
  PureThy.note_thmss Thm.definitionK [((@{binding nibble_pair_of_char_simps}, []), [(thms, [])])]
haftmann@31051
    57
  #-> (fn [(_, thms)] => fold_rev Code.add_eqn thms)
haftmann@31051
    58
end
haftmann@31051
    59
*}
haftmann@31051
    60
haftmann@32069
    61
lemma char_case_nibble_pair [code, code_unfold]:
haftmann@31051
    62
  "char_case f = split f o nibble_pair_of_char"
haftmann@31051
    63
  by (simp add: expand_fun_eq split: char.split)
haftmann@31051
    64
haftmann@32069
    65
lemma char_rec_nibble_pair [code, code_unfold]:
haftmann@31051
    66
  "char_rec f = split f o nibble_pair_of_char"
haftmann@31051
    67
  unfolding char_case_nibble_pair [symmetric]
haftmann@31051
    68
  by (simp add: expand_fun_eq split: char.split)
haftmann@31051
    69
haftmann@31051
    70
syntax
haftmann@31051
    71
  "_Char" :: "xstr => char"    ("CHR _")
haftmann@31051
    72
haftmann@31051
    73
haftmann@31051
    74
subsection {* Strings *}
haftmann@31051
    75
haftmann@31051
    76
types string = "char list"
haftmann@31051
    77
haftmann@31051
    78
syntax
haftmann@31051
    79
  "_String" :: "xstr => string"    ("_")
haftmann@31051
    80
wenzelm@35115
    81
use "Tools/string_syntax.ML"
wenzelm@35123
    82
setup String_Syntax.setup
haftmann@31051
    83
haftmann@31484
    84
definition chars :: string where
haftmann@31484
    85
  "chars = [Char Nibble0 Nibble0, Char Nibble0 Nibble1, Char Nibble0 Nibble2,
haftmann@31484
    86
  Char Nibble0 Nibble3, Char Nibble0 Nibble4, Char Nibble0 Nibble5,
haftmann@31484
    87
  Char Nibble0 Nibble6, Char Nibble0 Nibble7, Char Nibble0 Nibble8,
haftmann@31484
    88
  Char Nibble0 Nibble9, Char Nibble0 NibbleA, Char Nibble0 NibbleB,
haftmann@31484
    89
  Char Nibble0 NibbleC, Char Nibble0 NibbleD, Char Nibble0 NibbleE,
haftmann@31484
    90
  Char Nibble0 NibbleF, Char Nibble1 Nibble0, Char Nibble1 Nibble1,
haftmann@31484
    91
  Char Nibble1 Nibble2, Char Nibble1 Nibble3, Char Nibble1 Nibble4,
haftmann@31484
    92
  Char Nibble1 Nibble5, Char Nibble1 Nibble6, Char Nibble1 Nibble7,
haftmann@31484
    93
  Char Nibble1 Nibble8, Char Nibble1 Nibble9, Char Nibble1 NibbleA,
haftmann@31484
    94
  Char Nibble1 NibbleB, Char Nibble1 NibbleC, Char Nibble1 NibbleD,
haftmann@31484
    95
  Char Nibble1 NibbleE, Char Nibble1 NibbleF, CHR '' '', CHR ''!'',
haftmann@31484
    96
  Char Nibble2 Nibble2, CHR ''#'', CHR ''$'', CHR ''%'', CHR ''&'',
haftmann@31484
    97
  Char Nibble2 Nibble7, CHR ''('', CHR '')'', CHR ''*'', CHR ''+'', CHR '','',
haftmann@31484
    98
  CHR ''-'', CHR ''.'', CHR ''/'', CHR ''0'', CHR ''1'', CHR ''2'', CHR ''3'',
haftmann@31484
    99
  CHR ''4'', CHR ''5'', CHR ''6'', CHR ''7'', CHR ''8'', CHR ''9'', CHR '':'',
haftmann@31484
   100
  CHR '';'', CHR ''<'', CHR ''='', CHR ''>'', CHR ''?'', CHR ''@'', CHR ''A'',
haftmann@31484
   101
  CHR ''B'', CHR ''C'', CHR ''D'', CHR ''E'', CHR ''F'', CHR ''G'', CHR ''H'',
haftmann@31484
   102
  CHR ''I'', CHR ''J'', CHR ''K'', CHR ''L'', CHR ''M'', CHR ''N'', CHR ''O'',
haftmann@31484
   103
  CHR ''P'', CHR ''Q'', CHR ''R'', CHR ''S'', CHR ''T'', CHR ''U'', CHR ''V'',
haftmann@31484
   104
  CHR ''W'', CHR ''X'', CHR ''Y'', CHR ''Z'', CHR ''['', Char Nibble5 NibbleC,
haftmann@31484
   105
  CHR '']'', CHR ''^'', CHR ''_'', Char Nibble6 Nibble0, CHR ''a'', CHR ''b'',
haftmann@31484
   106
  CHR ''c'', CHR ''d'', CHR ''e'', CHR ''f'', CHR ''g'', CHR ''h'', CHR ''i'',
haftmann@31484
   107
  CHR ''j'', CHR ''k'', CHR ''l'', CHR ''m'', CHR ''n'', CHR ''o'', CHR ''p'',
haftmann@31484
   108
  CHR ''q'', CHR ''r'', CHR ''s'', CHR ''t'', CHR ''u'', CHR ''v'', CHR ''w'',
haftmann@31484
   109
  CHR ''x'', CHR ''y'', CHR ''z'', CHR ''{'', CHR ''|'', CHR ''}'', CHR ''~'',
haftmann@31484
   110
  Char Nibble7 NibbleF, Char Nibble8 Nibble0, Char Nibble8 Nibble1,
haftmann@31484
   111
  Char Nibble8 Nibble2, Char Nibble8 Nibble3, Char Nibble8 Nibble4,
haftmann@31484
   112
  Char Nibble8 Nibble5, Char Nibble8 Nibble6, Char Nibble8 Nibble7,
haftmann@31484
   113
  Char Nibble8 Nibble8, Char Nibble8 Nibble9, Char Nibble8 NibbleA,
haftmann@31484
   114
  Char Nibble8 NibbleB, Char Nibble8 NibbleC, Char Nibble8 NibbleD,
haftmann@31484
   115
  Char Nibble8 NibbleE, Char Nibble8 NibbleF, Char Nibble9 Nibble0,
haftmann@31484
   116
  Char Nibble9 Nibble1, Char Nibble9 Nibble2, Char Nibble9 Nibble3,
haftmann@31484
   117
  Char Nibble9 Nibble4, Char Nibble9 Nibble5, Char Nibble9 Nibble6,
haftmann@31484
   118
  Char Nibble9 Nibble7, Char Nibble9 Nibble8, Char Nibble9 Nibble9,
haftmann@31484
   119
  Char Nibble9 NibbleA, Char Nibble9 NibbleB, Char Nibble9 NibbleC,
haftmann@31484
   120
  Char Nibble9 NibbleD, Char Nibble9 NibbleE, Char Nibble9 NibbleF,
haftmann@31484
   121
  Char NibbleA Nibble0, Char NibbleA Nibble1, Char NibbleA Nibble2,
haftmann@31484
   122
  Char NibbleA Nibble3, Char NibbleA Nibble4, Char NibbleA Nibble5,
haftmann@31484
   123
  Char NibbleA Nibble6, Char NibbleA Nibble7, Char NibbleA Nibble8,
haftmann@31484
   124
  Char NibbleA Nibble9, Char NibbleA NibbleA, Char NibbleA NibbleB,
haftmann@31484
   125
  Char NibbleA NibbleC, Char NibbleA NibbleD, Char NibbleA NibbleE,
haftmann@31484
   126
  Char NibbleA NibbleF, Char NibbleB Nibble0, Char NibbleB Nibble1,
haftmann@31484
   127
  Char NibbleB Nibble2, Char NibbleB Nibble3, Char NibbleB Nibble4,
haftmann@31484
   128
  Char NibbleB Nibble5, Char NibbleB Nibble6, Char NibbleB Nibble7,
haftmann@31484
   129
  Char NibbleB Nibble8, Char NibbleB Nibble9, Char NibbleB NibbleA,
haftmann@31484
   130
  Char NibbleB NibbleB, Char NibbleB NibbleC, Char NibbleB NibbleD,
haftmann@31484
   131
  Char NibbleB NibbleE, Char NibbleB NibbleF, Char NibbleC Nibble0,
haftmann@31484
   132
  Char NibbleC Nibble1, Char NibbleC Nibble2, Char NibbleC Nibble3,
haftmann@31484
   133
  Char NibbleC Nibble4, Char NibbleC Nibble5, Char NibbleC Nibble6,
haftmann@31484
   134
  Char NibbleC Nibble7, Char NibbleC Nibble8, Char NibbleC Nibble9,
haftmann@31484
   135
  Char NibbleC NibbleA, Char NibbleC NibbleB, Char NibbleC NibbleC,
haftmann@31484
   136
  Char NibbleC NibbleD, Char NibbleC NibbleE, Char NibbleC NibbleF,
haftmann@31484
   137
  Char NibbleD Nibble0, Char NibbleD Nibble1, Char NibbleD Nibble2,
haftmann@31484
   138
  Char NibbleD Nibble3, Char NibbleD Nibble4, Char NibbleD Nibble5,
haftmann@31484
   139
  Char NibbleD Nibble6, Char NibbleD Nibble7, Char NibbleD Nibble8,
haftmann@31484
   140
  Char NibbleD Nibble9, Char NibbleD NibbleA, Char NibbleD NibbleB,
haftmann@31484
   141
  Char NibbleD NibbleC, Char NibbleD NibbleD, Char NibbleD NibbleE,
haftmann@31484
   142
  Char NibbleD NibbleF, Char NibbleE Nibble0, Char NibbleE Nibble1,
haftmann@31484
   143
  Char NibbleE Nibble2, Char NibbleE Nibble3, Char NibbleE Nibble4,
haftmann@31484
   144
  Char NibbleE Nibble5, Char NibbleE Nibble6, Char NibbleE Nibble7,
haftmann@31484
   145
  Char NibbleE Nibble8, Char NibbleE Nibble9, Char NibbleE NibbleA,
haftmann@31484
   146
  Char NibbleE NibbleB, Char NibbleE NibbleC, Char NibbleE NibbleD,
haftmann@31484
   147
  Char NibbleE NibbleE, Char NibbleE NibbleF, Char NibbleF Nibble0,
haftmann@31484
   148
  Char NibbleF Nibble1, Char NibbleF Nibble2, Char NibbleF Nibble3,
haftmann@31484
   149
  Char NibbleF Nibble4, Char NibbleF Nibble5, Char NibbleF Nibble6,
haftmann@31484
   150
  Char NibbleF Nibble7, Char NibbleF Nibble8, Char NibbleF Nibble9,
haftmann@31484
   151
  Char NibbleF NibbleA, Char NibbleF NibbleB, Char NibbleF NibbleC,
haftmann@31484
   152
  Char NibbleF NibbleD, Char NibbleF NibbleE, Char NibbleF NibbleF]"
haftmann@31484
   153
haftmann@31051
   154
haftmann@31051
   155
subsection {* Strings as dedicated datatype *}
haftmann@31051
   156
haftmann@31205
   157
datatype literal = STR string
haftmann@31051
   158
haftmann@33237
   159
declare literal.cases [code del] literal.recs [code del]
haftmann@31051
   160
haftmann@31205
   161
lemma [code]: "size (s\<Colon>literal) = 0"
haftmann@31051
   162
  by (cases s) simp_all
haftmann@31051
   163
haftmann@31205
   164
lemma [code]: "literal_size (s\<Colon>literal) = 0"
haftmann@31051
   165
  by (cases s) simp_all
haftmann@31051
   166
haftmann@31051
   167
haftmann@31051
   168
subsection {* Code generator *}
haftmann@31051
   169
haftmann@31055
   170
use "Tools/string_code.ML"
haftmann@31051
   171
haftmann@33237
   172
code_reserved SML string
haftmann@33237
   173
code_reserved OCaml string
haftmann@34886
   174
code_reserved Scala string
haftmann@33237
   175
haftmann@31205
   176
code_type literal
haftmann@31051
   177
  (SML "string")
haftmann@31051
   178
  (OCaml "string")
haftmann@31051
   179
  (Haskell "String")
haftmann@34886
   180
  (Scala "String")
haftmann@31051
   181
haftmann@31051
   182
setup {*
haftmann@34886
   183
  fold String_Code.add_literal_string ["SML", "OCaml", "Haskell", "Scala"]
haftmann@31051
   184
*}
haftmann@31051
   185
haftmann@38857
   186
code_instance literal :: equal
haftmann@31051
   187
  (Haskell -)
haftmann@31051
   188
haftmann@38857
   189
code_const "HOL.equal \<Colon> literal \<Rightarrow> literal \<Rightarrow> bool"
haftmann@31051
   190
  (SML "!((_ : string) = _)")
haftmann@31051
   191
  (OCaml "!((_ : string) = _)")
haftmann@31051
   192
  (Haskell infixl 4 "==")
haftmann@34886
   193
  (Scala infixl 5 "==")
haftmann@31051
   194
haftmann@31051
   195
types_code
haftmann@31051
   196
  "char" ("string")
haftmann@31051
   197
attach (term_of) {*
haftmann@31051
   198
val term_of_char = HOLogic.mk_char o ord;
haftmann@31051
   199
*}
haftmann@31051
   200
attach (test) {*
haftmann@31051
   201
fun gen_char i =
haftmann@31051
   202
  let val j = random_range (ord "a") (Int.min (ord "a" + i, ord "z"))
haftmann@31051
   203
  in (chr j, fn () => HOLogic.mk_char j) end;
haftmann@31051
   204
*}
haftmann@31051
   205
haftmann@31051
   206
setup {*
haftmann@31051
   207
let
haftmann@31051
   208
haftmann@31051
   209
fun char_codegen thy defs dep thyname b t gr =
haftmann@31051
   210
  let
haftmann@31051
   211
    val i = HOLogic.dest_char t;
haftmann@31051
   212
    val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false
haftmann@31051
   213
      (fastype_of t) gr;
haftmann@31051
   214
  in SOME (Codegen.str (ML_Syntax.print_string (chr i)), gr')
haftmann@31051
   215
  end handle TERM _ => NONE;
haftmann@31051
   216
haftmann@31051
   217
in Codegen.add_codegen "char_codegen" char_codegen end
haftmann@31051
   218
*}
haftmann@31051
   219
wenzelm@36176
   220
hide_type (open) literal
haftmann@31205
   221
haftmann@37743
   222
haftmann@37743
   223
text {* Code generator setup *}
haftmann@37743
   224
haftmann@37743
   225
code_modulename SML
haftmann@37743
   226
  String String
haftmann@37743
   227
haftmann@37743
   228
code_modulename OCaml
haftmann@37743
   229
  String String
haftmann@37743
   230
haftmann@37743
   231
code_modulename Haskell
haftmann@37743
   232
  String String
haftmann@37743
   233
haftmann@31051
   234
end