src/HOL/String.thy
author haftmann
Tue May 19 16:54:55 2009 +0200 (2009-05-19)
changeset 31205 98370b26c2ce
parent 31176 92e0ed53db25
child 31484 cabcb95fde29
permissions -rw-r--r--
String.literal replaces message_string, code_numeral replaces (code_)index
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
haftmann@31055
     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
declare nibble_pair_of_char.simps [code del]
haftmann@31051
    50
haftmann@31051
    51
setup {*
haftmann@31051
    52
let
haftmann@31051
    53
  val nibbles = map (Thm.cterm_of @{theory} o HOLogic.mk_nibble) (0 upto 15);
haftmann@31051
    54
  val thms = map_product
haftmann@31051
    55
   (fn n => fn m => Drule.instantiate' [] [SOME n, SOME m] @{thm nibble_pair_of_char.simps})
haftmann@31051
    56
      nibbles nibbles;
haftmann@31051
    57
in
haftmann@31176
    58
  PureThy.note_thmss Thm.definitionK [((Binding.name "nibble_pair_of_char_simps", []), [(thms, [])])]
haftmann@31051
    59
  #-> (fn [(_, thms)] => fold_rev Code.add_eqn thms)
haftmann@31051
    60
end
haftmann@31051
    61
*}
haftmann@31051
    62
haftmann@31051
    63
lemma char_case_nibble_pair [code, code inline]:
haftmann@31051
    64
  "char_case f = split f o nibble_pair_of_char"
haftmann@31051
    65
  by (simp add: expand_fun_eq split: char.split)
haftmann@31051
    66
haftmann@31051
    67
lemma char_rec_nibble_pair [code, code inline]:
haftmann@31051
    68
  "char_rec f = split f o nibble_pair_of_char"
haftmann@31051
    69
  unfolding char_case_nibble_pair [symmetric]
haftmann@31051
    70
  by (simp add: expand_fun_eq split: char.split)
haftmann@31051
    71
haftmann@31051
    72
syntax
haftmann@31051
    73
  "_Char" :: "xstr => char"    ("CHR _")
haftmann@31051
    74
haftmann@31051
    75
haftmann@31051
    76
subsection {* Strings *}
haftmann@31051
    77
haftmann@31051
    78
types string = "char list"
haftmann@31051
    79
haftmann@31051
    80
syntax
haftmann@31051
    81
  "_String" :: "xstr => string"    ("_")
haftmann@31051
    82
haftmann@31051
    83
setup StringSyntax.setup
haftmann@31051
    84
haftmann@31051
    85
haftmann@31051
    86
subsection {* Strings as dedicated datatype *}
haftmann@31051
    87
haftmann@31205
    88
datatype literal = STR string
haftmann@31051
    89
haftmann@31205
    90
lemmas [code del] = literal.recs literal.cases
haftmann@31051
    91
haftmann@31205
    92
lemma [code]: "size (s\<Colon>literal) = 0"
haftmann@31051
    93
  by (cases s) simp_all
haftmann@31051
    94
haftmann@31205
    95
lemma [code]: "literal_size (s\<Colon>literal) = 0"
haftmann@31051
    96
  by (cases s) simp_all
haftmann@31051
    97
haftmann@31051
    98
haftmann@31051
    99
subsection {* Code generator *}
haftmann@31051
   100
haftmann@31055
   101
use "Tools/string_code.ML"
haftmann@31051
   102
haftmann@31205
   103
code_type literal
haftmann@31051
   104
  (SML "string")
haftmann@31051
   105
  (OCaml "string")
haftmann@31051
   106
  (Haskell "String")
haftmann@31051
   107
haftmann@31051
   108
setup {*
haftmann@31205
   109
  fold String_Code.add_literal_string ["SML", "OCaml", "Haskell"]
haftmann@31051
   110
*}
haftmann@31051
   111
haftmann@31205
   112
code_instance literal :: eq
haftmann@31051
   113
  (Haskell -)
haftmann@31051
   114
haftmann@31205
   115
code_const "eq_class.eq \<Colon> literal \<Rightarrow> literal \<Rightarrow> bool"
haftmann@31051
   116
  (SML "!((_ : string) = _)")
haftmann@31051
   117
  (OCaml "!((_ : string) = _)")
haftmann@31051
   118
  (Haskell infixl 4 "==")
haftmann@31051
   119
haftmann@31051
   120
code_reserved SML string
haftmann@31051
   121
code_reserved OCaml string
haftmann@31051
   122
haftmann@31051
   123
haftmann@31051
   124
types_code
haftmann@31051
   125
  "char" ("string")
haftmann@31051
   126
attach (term_of) {*
haftmann@31051
   127
val term_of_char = HOLogic.mk_char o ord;
haftmann@31051
   128
*}
haftmann@31051
   129
attach (test) {*
haftmann@31051
   130
fun gen_char i =
haftmann@31051
   131
  let val j = random_range (ord "a") (Int.min (ord "a" + i, ord "z"))
haftmann@31051
   132
  in (chr j, fn () => HOLogic.mk_char j) end;
haftmann@31051
   133
*}
haftmann@31051
   134
haftmann@31051
   135
setup {*
haftmann@31051
   136
let
haftmann@31051
   137
haftmann@31051
   138
fun char_codegen thy defs dep thyname b t gr =
haftmann@31051
   139
  let
haftmann@31051
   140
    val i = HOLogic.dest_char t;
haftmann@31051
   141
    val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false
haftmann@31051
   142
      (fastype_of t) gr;
haftmann@31051
   143
  in SOME (Codegen.str (ML_Syntax.print_string (chr i)), gr')
haftmann@31051
   144
  end handle TERM _ => NONE;
haftmann@31051
   145
haftmann@31051
   146
in Codegen.add_codegen "char_codegen" char_codegen end
haftmann@31051
   147
*}
haftmann@31051
   148
haftmann@31205
   149
hide (open) type literal
haftmann@31205
   150
haftmann@31051
   151
end