| author | Lukas Stevens <mail@lukas-stevens.de> | 
| Fri, 22 Jul 2022 14:21:53 +0000 | |
| changeset 75670 | acf86c9f7698 | 
| parent 75662 | ed15f2cd4f7d | 
| child 75694 | 1b812435a632 | 
| permissions | -rw-r--r-- | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 1 | (* Author: Tobias Nipkow, Florian Haftmann, TU Muenchen *) | 
| 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 2 | |
| 60758 | 3 | section \<open>Character and string types\<close> | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 4 | |
| 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 5 | theory String | 
| 74101 | 6 | imports Enum Bit_Operations Code_Numeral | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 7 | begin | 
| 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 8 | |
| 68028 | 9 | subsection \<open>Strings as list of bytes\<close> | 
| 10 | ||
| 11 | text \<open> | |
| 12 | When modelling strings, we follow the approach given | |
| 69593 | 13 | in \<^url>\<open>https://utf8everywhere.org/\<close>: | 
| 68028 | 14 | |
| 15 | \<^item> Strings are a list of bytes (8 bit). | |
| 16 | ||
| 17 | \<^item> Byte values from 0 to 127 are US-ASCII. | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 18 | |
| 68028 | 19 | \<^item> Byte values from 128 to 255 are uninterpreted blobs. | 
| 20 | \<close> | |
| 21 | ||
| 22 | subsubsection \<open>Bytes as datatype\<close> | |
| 23 | ||
| 24 | datatype char = | |
| 25 | Char (digit0: bool) (digit1: bool) (digit2: bool) (digit3: bool) | |
| 26 | (digit4: bool) (digit5: bool) (digit6: bool) (digit7: bool) | |
| 27 | ||
| 28 | context comm_semiring_1 | |
| 29 | begin | |
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 30 | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 31 | definition of_char :: \<open>char \<Rightarrow> 'a\<close> | 
| 72024 | 32 | where \<open>of_char c = horner_sum of_bool 2 [digit0 c, digit1 c, digit2 c, digit3 c, digit4 c, digit5 c, digit6 c, digit7 c]\<close> | 
| 68028 | 33 | |
| 34 | lemma of_char_Char [simp]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 35 | \<open>of_char (Char b0 b1 b2 b3 b4 b5 b6 b7) = | 
| 72024 | 36 | horner_sum of_bool 2 [b0, b1, b2, b3, b4, b5, b6, b7]\<close> | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 37 | by (simp add: of_char_def) | 
| 68028 | 38 | |
| 39 | end | |
| 40 | ||
| 75622 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 41 | lemma (in comm_semiring_1) of_nat_of_char: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 42 | \<open>of_nat (of_char c) = of_char c\<close> | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 43 | by (cases c) simp | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 44 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 45 | lemma (in comm_ring_1) of_int_of_char: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 46 | \<open>of_int (of_char c) = of_char c\<close> | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 47 | by (cases c) simp | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 48 | |
| 75662 | 49 | lemma nat_of_char [simp]: | 
| 50 | \<open>nat (of_char c) = of_char c\<close> | |
| 51 | by (cases c) (simp only: of_char_Char nat_horner_sum) | |
| 52 | ||
| 75622 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 53 | |
| 74108 
3146646a43a7
simplified hierarchy of type classes for bit operations
 haftmann parents: 
74101diff
changeset | 54 | context unique_euclidean_semiring_with_bit_operations | 
| 68028 | 55 | begin | 
| 56 | ||
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 57 | definition char_of :: \<open>'a \<Rightarrow> char\<close> | 
| 75085 | 58 | where \<open>char_of n = Char (bit n 0) (bit n 1) (bit n 2) (bit n 3) (bit n 4) (bit n 5) (bit n 6) (bit n 7)\<close> | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 59 | |
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 60 | lemma char_of_take_bit_eq: | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 61 | \<open>char_of (take_bit n m) = char_of m\<close> if \<open>n \<ge> 8\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 62 | using that by (simp add: char_of_def bit_take_bit_iff) | 
| 68028 | 63 | |
| 64 | lemma char_of_char [simp]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 65 | \<open>char_of (of_char c) = c\<close> | 
| 72024 | 66 | by (simp only: of_char_def char_of_def bit_horner_sum_bit_iff) simp | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 67 | |
| 68028 | 68 | lemma char_of_comp_of_char [simp]: | 
| 69 | "char_of \<circ> of_char = id" | |
| 70 | by (simp add: fun_eq_iff) | |
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 71 | |
| 68028 | 72 | lemma inj_of_char: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 73 | \<open>inj of_char\<close> | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 74 | proof (rule injI) | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 75 | fix c d | 
| 68028 | 76 | assume "of_char c = of_char d" | 
| 77 | then have "char_of (of_char c) = char_of (of_char d)" | |
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 78 | by simp | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 79 | then show "c = d" | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 80 | by simp | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 81 | qed | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 82 | |
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 83 | lemma of_char_eqI: | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 84 | \<open>c = d\<close> if \<open>of_char c = of_char d\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 85 | using that inj_of_char by (simp add: inj_eq) | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 86 | |
| 68028 | 87 | lemma of_char_eq_iff [simp]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 88 | \<open>of_char c = of_char d \<longleftrightarrow> c = d\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 89 | by (auto intro: of_char_eqI) | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 90 | |
| 68028 | 91 | lemma of_char_of [simp]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 92 | \<open>of_char (char_of a) = a mod 256\<close> | 
| 68028 | 93 | proof - | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 94 | have \<open>[0..<8] = [0, Suc 0, 2, 3, 4, 5, 6, 7 :: nat]\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 95 | by (simp add: upt_eq_Cons_conv) | 
| 75085 | 96 | then have \<open>[bit a 0, bit a 1, bit a 2, bit a 3, bit a 4, bit a 5, bit a 6, bit a 7] = map (bit a) [0..<8]\<close> | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 97 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 98 | then have \<open>of_char (char_of a) = take_bit 8 a\<close> | 
| 72024 | 99 | by (simp only: char_of_def of_char_def char.sel horner_sum_bit_eq_take_bit) | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 100 | then show ?thesis | 
| 68028 | 101 | by (simp add: take_bit_eq_mod) | 
| 102 | qed | |
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 103 | |
| 68028 | 104 | lemma char_of_mod_256 [simp]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 105 | \<open>char_of (n mod 256) = char_of n\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 106 | by (rule of_char_eqI) simp | 
| 68028 | 107 | |
| 108 | lemma of_char_mod_256 [simp]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 109 | \<open>of_char c mod 256 = of_char c\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 110 | proof - | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 111 | have \<open>of_char (char_of (of_char c)) mod 256 = of_char (char_of (of_char c))\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 112 | by (simp only: of_char_of) simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 113 | then show ?thesis | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 114 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 115 | qed | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 116 | |
| 68028 | 117 | lemma char_of_quasi_inj [simp]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 118 | \<open>char_of m = char_of n \<longleftrightarrow> m mod 256 = n mod 256\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 119 | proof | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 120 | assume ?Q | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 121 | then show ?P | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 122 | by (auto intro: of_char_eqI) | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 123 | next | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 124 | assume ?P | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 125 | then have \<open>of_char (char_of m) = of_char (char_of n)\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 126 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 127 | then show ?Q | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 128 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 129 | qed | 
| 68028 | 130 | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 131 | lemma char_of_eq_iff: | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 132 | \<open>char_of n = c \<longleftrightarrow> take_bit 8 n = of_char c\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 133 | by (auto intro: of_char_eqI simp add: take_bit_eq_mod) | 
| 68028 | 134 | |
| 135 | lemma char_of_nat [simp]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 136 | \<open>char_of (of_nat n) = char_of n\<close> | 
| 74309 
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
 haftmann parents: 
74108diff
changeset | 137 | by (simp add: char_of_def String.char_of_def drop_bit_of_nat bit_simps possible_bit_def) | 
| 68033 
ad4b8b6892c3
uniform tagging for printable and non-printable literals
 haftmann parents: 
68028diff
changeset | 138 | |
| 68028 | 139 | end | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 140 | |
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 141 | lemma inj_on_char_of_nat [simp]: | 
| 68028 | 142 |   "inj_on char_of {0::nat..<256}"
 | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 143 | by (rule inj_onI) simp | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 144 | |
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 145 | lemma nat_of_char_less_256 [simp]: | 
| 68028 | 146 | "of_char c < (256 :: nat)" | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 147 | proof - | 
| 68028 | 148 | have "of_char c mod (256 :: nat) < 256" | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 149 | by arith | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 150 | then show ?thesis by simp | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 151 | qed | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 152 | |
| 68028 | 153 | lemma range_nat_of_char: | 
| 154 |   "range of_char = {0::nat..<256}"
 | |
| 155 | proof (rule; rule) | |
| 156 | fix n :: nat | |
| 157 | assume "n \<in> range of_char" | |
| 158 |   then show "n \<in> {0..<256}"
 | |
| 159 | by auto | |
| 160 | next | |
| 161 | fix n :: nat | |
| 162 |   assume "n \<in> {0..<256}"
 | |
| 163 | then have "n = of_char (char_of n)" | |
| 164 | by simp | |
| 165 | then show "n \<in> range of_char" | |
| 166 | by (rule range_eqI) | |
| 167 | qed | |
| 168 | ||
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 169 | lemma UNIV_char_of_nat: | 
| 68028 | 170 |   "UNIV = char_of ` {0::nat..<256}"
 | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 171 | proof - | 
| 68028 | 172 |   have "range (of_char :: char \<Rightarrow> nat) = of_char ` char_of ` {0::nat..<256}"
 | 
| 173 | by (auto simp add: range_nat_of_char intro!: image_eqI) | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 174 | with inj_of_char [where ?'a = nat] show ?thesis | 
| 68028 | 175 | by (simp add: inj_image_eq_iff) | 
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 176 | qed | 
| 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 177 | |
| 62597 | 178 | lemma card_UNIV_char: | 
| 179 | "card (UNIV :: char set) = 256" | |
| 180 | by (auto simp add: UNIV_char_of_nat card_image) | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 181 | |
| 68028 | 182 | context | 
| 183 | includes lifting_syntax integer.lifting natural.lifting | |
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 184 | begin | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 185 | |
| 68028 | 186 | lemma [transfer_rule]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 187 | \<open>(pcr_integer ===> (=)) char_of char_of\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 188 | by (unfold char_of_def) transfer_prover | 
| 68028 | 189 | |
| 190 | lemma [transfer_rule]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 191 | \<open>((=) ===> pcr_integer) of_char of_char\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 192 | by (unfold of_char_def) transfer_prover | 
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 193 | |
| 68028 | 194 | lemma [transfer_rule]: | 
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 195 | \<open>(pcr_natural ===> (=)) char_of char_of\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 196 | by (unfold char_of_def) transfer_prover | 
| 68028 | 197 | |
| 198 | lemma [transfer_rule]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 199 | \<open>((=) ===> pcr_natural) of_char of_char\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 200 | by (unfold of_char_def) transfer_prover | 
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 201 | |
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 202 | end | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 203 | |
| 68028 | 204 | lifting_update integer.lifting | 
| 205 | lifting_forget integer.lifting | |
| 64630 
96015aecfeba
emphasize dedicated rewrite rules for congruences
 haftmann parents: 
63950diff
changeset | 206 | |
| 68028 | 207 | lifting_update natural.lifting | 
| 208 | lifting_forget natural.lifting | |
| 62364 
9209770bdcdf
more direct bootstrap of char type, still retaining the nibble representation for syntax
 haftmann parents: 
61799diff
changeset | 209 | |
| 75622 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 210 | lemma size_char_eq_0 [simp, code]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 211 | \<open>size c = 0\<close> for c :: char | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 212 | by (cases c) simp | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 213 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 214 | lemma size'_char_eq_0 [simp, code]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 215 | \<open>size_char c = 0\<close> | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 216 | by (cases c) simp | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 217 | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 218 | syntax | 
| 62597 | 219 |   "_Char" :: "str_position \<Rightarrow> char"    ("CHR _")
 | 
| 62678 | 220 |   "_Char_ord" :: "num_const \<Rightarrow> char"   ("CHR _")
 | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 221 | |
| 42163 
392fd6c4669c
renewing specifications in HOL: replacing types by type_synonym
 bulwahn parents: 
41750diff
changeset | 222 | type_synonym string = "char list" | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 223 | |
| 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 224 | syntax | 
| 68028 | 225 |   "_String" :: "str_position \<Rightarrow> string"    ("_")
 | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 226 | |
| 69605 | 227 | ML_file \<open>Tools/string_syntax.ML\<close> | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 228 | |
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 229 | instantiation char :: enum | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 230 | begin | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 231 | |
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 232 | definition | 
| 68028 | 233 | "Enum.enum = [ | 
| 234 | CHR 0x00, CHR 0x01, CHR 0x02, CHR 0x03, | |
| 62678 | 235 | CHR 0x04, CHR 0x05, CHR 0x06, CHR 0x07, | 
| 236 | CHR 0x08, CHR 0x09, CHR ''\<newline>'', CHR 0x0B, | |
| 237 | CHR 0x0C, CHR 0x0D, CHR 0x0E, CHR 0x0F, | |
| 238 | CHR 0x10, CHR 0x11, CHR 0x12, CHR 0x13, | |
| 239 | CHR 0x14, CHR 0x15, CHR 0x16, CHR 0x17, | |
| 240 | CHR 0x18, CHR 0x19, CHR 0x1A, CHR 0x1B, | |
| 241 | CHR 0x1C, CHR 0x1D, CHR 0x1E, CHR 0x1F, | |
| 242 | CHR '' '', CHR ''!'', CHR 0x22, CHR ''#'', | |
| 243 | CHR ''$'', CHR ''%'', CHR ''&'', CHR 0x27, | |
| 62597 | 244 |     CHR ''('', CHR '')'', CHR ''*'', CHR ''+'',
 | 
| 245 | CHR '','', CHR ''-'', CHR ''.'', CHR ''/'', | |
| 246 | CHR ''0'', CHR ''1'', CHR ''2'', CHR ''3'', | |
| 247 | CHR ''4'', CHR ''5'', CHR ''6'', CHR ''7'', | |
| 248 | CHR ''8'', CHR ''9'', CHR '':'', CHR '';'', | |
| 249 | CHR ''<'', CHR ''='', CHR ''>'', CHR ''?'', | |
| 250 | CHR ''@'', CHR ''A'', CHR ''B'', CHR ''C'', | |
| 251 | CHR ''D'', CHR ''E'', CHR ''F'', CHR ''G'', | |
| 252 | CHR ''H'', CHR ''I'', CHR ''J'', CHR ''K'', | |
| 253 | CHR ''L'', CHR ''M'', CHR ''N'', CHR ''O'', | |
| 254 | CHR ''P'', CHR ''Q'', CHR ''R'', CHR ''S'', | |
| 255 | CHR ''T'', CHR ''U'', CHR ''V'', CHR ''W'', | |
| 256 | CHR ''X'', CHR ''Y'', CHR ''Z'', CHR ''['', | |
| 62678 | 257 | CHR 0x5C, CHR '']'', CHR ''^'', CHR ''_'', | 
| 258 | CHR 0x60, CHR ''a'', CHR ''b'', CHR ''c'', | |
| 62597 | 259 | CHR ''d'', CHR ''e'', CHR ''f'', CHR ''g'', | 
| 260 | CHR ''h'', CHR ''i'', CHR ''j'', CHR ''k'', | |
| 261 | CHR ''l'', CHR ''m'', CHR ''n'', CHR ''o'', | |
| 262 | CHR ''p'', CHR ''q'', CHR ''r'', CHR ''s'', | |
| 263 | CHR ''t'', CHR ''u'', CHR ''v'', CHR ''w'', | |
| 264 |     CHR ''x'', CHR ''y'', CHR ''z'', CHR ''{'',
 | |
| 62678 | 265 | CHR ''|'', CHR ''}'', CHR ''~'', CHR 0x7F, | 
| 266 | CHR 0x80, CHR 0x81, CHR 0x82, CHR 0x83, | |
| 267 | CHR 0x84, CHR 0x85, CHR 0x86, CHR 0x87, | |
| 268 | CHR 0x88, CHR 0x89, CHR 0x8A, CHR 0x8B, | |
| 269 | CHR 0x8C, CHR 0x8D, CHR 0x8E, CHR 0x8F, | |
| 270 | CHR 0x90, CHR 0x91, CHR 0x92, CHR 0x93, | |
| 271 | CHR 0x94, CHR 0x95, CHR 0x96, CHR 0x97, | |
| 272 | CHR 0x98, CHR 0x99, CHR 0x9A, CHR 0x9B, | |
| 273 | CHR 0x9C, CHR 0x9D, CHR 0x9E, CHR 0x9F, | |
| 274 | CHR 0xA0, CHR 0xA1, CHR 0xA2, CHR 0xA3, | |
| 275 | CHR 0xA4, CHR 0xA5, CHR 0xA6, CHR 0xA7, | |
| 276 | CHR 0xA8, CHR 0xA9, CHR 0xAA, CHR 0xAB, | |
| 277 | CHR 0xAC, CHR 0xAD, CHR 0xAE, CHR 0xAF, | |
| 278 | CHR 0xB0, CHR 0xB1, CHR 0xB2, CHR 0xB3, | |
| 279 | CHR 0xB4, CHR 0xB5, CHR 0xB6, CHR 0xB7, | |
| 280 | CHR 0xB8, CHR 0xB9, CHR 0xBA, CHR 0xBB, | |
| 281 | CHR 0xBC, CHR 0xBD, CHR 0xBE, CHR 0xBF, | |
| 282 | CHR 0xC0, CHR 0xC1, CHR 0xC2, CHR 0xC3, | |
| 283 | CHR 0xC4, CHR 0xC5, CHR 0xC6, CHR 0xC7, | |
| 284 | CHR 0xC8, CHR 0xC9, CHR 0xCA, CHR 0xCB, | |
| 285 | CHR 0xCC, CHR 0xCD, CHR 0xCE, CHR 0xCF, | |
| 286 | CHR 0xD0, CHR 0xD1, CHR 0xD2, CHR 0xD3, | |
| 287 | CHR 0xD4, CHR 0xD5, CHR 0xD6, CHR 0xD7, | |
| 288 | CHR 0xD8, CHR 0xD9, CHR 0xDA, CHR 0xDB, | |
| 289 | CHR 0xDC, CHR 0xDD, CHR 0xDE, CHR 0xDF, | |
| 290 | CHR 0xE0, CHR 0xE1, CHR 0xE2, CHR 0xE3, | |
| 291 | CHR 0xE4, CHR 0xE5, CHR 0xE6, CHR 0xE7, | |
| 292 | CHR 0xE8, CHR 0xE9, CHR 0xEA, CHR 0xEB, | |
| 293 | CHR 0xEC, CHR 0xED, CHR 0xEE, CHR 0xEF, | |
| 294 | CHR 0xF0, CHR 0xF1, CHR 0xF2, CHR 0xF3, | |
| 295 | CHR 0xF4, CHR 0xF5, CHR 0xF6, CHR 0xF7, | |
| 296 | CHR 0xF8, CHR 0xF9, CHR 0xFA, CHR 0xFB, | |
| 297 | CHR 0xFC, CHR 0xFD, CHR 0xFE, CHR 0xFF]" | |
| 31484 | 298 | |
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 299 | definition | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 300 | "Enum.enum_all P \<longleftrightarrow> list_all P (Enum.enum :: char list)" | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 301 | |
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 302 | definition | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 303 | "Enum.enum_ex P \<longleftrightarrow> list_ex P (Enum.enum :: char list)" | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 304 | |
| 62597 | 305 | lemma enum_char_unfold: | 
| 68028 | 306 | "Enum.enum = map char_of [0..<256]" | 
| 62597 | 307 | proof - | 
| 68028 | 308 | have "map (of_char :: char \<Rightarrow> nat) Enum.enum = [0..<256]" | 
| 309 | by (simp add: enum_char_def of_char_def upt_conv_Cons_Cons numeral_2_eq_2 [symmetric]) | |
| 310 | then have "map char_of (map (of_char :: char \<Rightarrow> nat) Enum.enum) = | |
| 311 | map char_of [0..<256]" | |
| 312 | by simp | |
| 313 | then show ?thesis | |
| 314 | by simp | |
| 62597 | 315 | qed | 
| 51160 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
49972diff
changeset | 316 | |
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 317 | instance proof | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 318 | show UNIV: "UNIV = set (Enum.enum :: char list)" | 
| 62597 | 319 | by (simp add: enum_char_unfold UNIV_char_of_nat atLeast0LessThan) | 
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 320 | show "distinct (Enum.enum :: char list)" | 
| 62597 | 321 | by (auto simp add: enum_char_unfold distinct_map intro: inj_onI) | 
| 49972 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 322 | show "\<And>P. Enum.enum_all P \<longleftrightarrow> Ball (UNIV :: char set) P" | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 323 | by (simp add: UNIV enum_all_char_def list_all_iff) | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 324 | show "\<And>P. Enum.enum_ex P \<longleftrightarrow> Bex (UNIV :: char set) P" | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 325 | by (simp add: UNIV enum_ex_char_def list_ex_iff) | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 326 | qed | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 327 | |
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 328 | end | 
| 
f11f8905d9fd
incorporated constant chars into instantiation proof for enum;
 haftmann parents: 
49948diff
changeset | 329 | |
| 68028 | 330 | lemma linorder_char: | 
| 331 | "class.linorder (\<lambda>c d. of_char c \<le> (of_char d :: nat)) (\<lambda>c d. of_char c < (of_char d :: nat))" | |
| 332 | by standard auto | |
| 333 | ||
| 334 | text \<open>Optimized version for execution\<close> | |
| 335 | ||
| 336 | definition char_of_integer :: "integer \<Rightarrow> char" | |
| 337 | where [code_abbrev]: "char_of_integer = char_of" | |
| 338 | ||
| 339 | definition integer_of_char :: "char \<Rightarrow> integer" | |
| 340 | where [code_abbrev]: "integer_of_char = of_char" | |
| 341 | ||
| 62597 | 342 | lemma char_of_integer_code [code]: | 
| 68028 | 343 | "char_of_integer k = (let | 
| 344 | (q0, b0) = bit_cut_integer k; | |
| 345 | (q1, b1) = bit_cut_integer q0; | |
| 346 | (q2, b2) = bit_cut_integer q1; | |
| 347 | (q3, b3) = bit_cut_integer q2; | |
| 348 | (q4, b4) = bit_cut_integer q3; | |
| 349 | (q5, b5) = bit_cut_integer q4; | |
| 350 | (q6, b6) = bit_cut_integer q5; | |
| 351 | (_, b7) = bit_cut_integer q6 | |
| 352 | in Char b0 b1 b2 b3 b4 b5 b6 b7)" | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 353 | by (simp add: bit_cut_integer_def char_of_integer_def char_of_def div_mult2_numeral_eq bit_iff_odd_drop_bit drop_bit_eq_div) | 
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49834diff
changeset | 354 | |
| 68028 | 355 | lemma integer_of_char_code [code]: | 
| 356 | "integer_of_char (Char b0 b1 b2 b3 b4 b5 b6 b7) = | |
| 357 | ((((((of_bool b7 * 2 + of_bool b6) * 2 + | |
| 358 | of_bool b5) * 2 + of_bool b4) * 2 + | |
| 359 | of_bool b3) * 2 + of_bool b2) * 2 + | |
| 360 | of_bool b1) * 2 + of_bool b0" | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 361 | by (simp add: integer_of_char_def of_char_def) | 
| 66331 | 362 | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 363 | |
| 68028 | 364 | subsection \<open>Strings as dedicated type for target language code generation\<close> | 
| 365 | ||
| 366 | subsubsection \<open>Logical specification\<close> | |
| 367 | ||
| 368 | context | |
| 369 | begin | |
| 370 | ||
| 371 | qualified definition ascii_of :: "char \<Rightarrow> char" | |
| 372 | where "ascii_of c = Char (digit0 c) (digit1 c) (digit2 c) (digit3 c) (digit4 c) (digit5 c) (digit6 c) False" | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 373 | |
| 68028 | 374 | qualified lemma ascii_of_Char [simp]: | 
| 375 | "ascii_of (Char b0 b1 b2 b3 b4 b5 b6 b7) = Char b0 b1 b2 b3 b4 b5 b6 False" | |
| 376 | by (simp add: ascii_of_def) | |
| 377 | ||
| 75622 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 378 | qualified lemma digit0_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 379 | "digit0 (String.ascii_of c) \<longleftrightarrow> digit0 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 380 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 381 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 382 | qualified lemma digit1_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 383 | "digit1 (String.ascii_of c) \<longleftrightarrow> digit1 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 384 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 385 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 386 | qualified lemma digit2_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 387 | "digit2 (String.ascii_of c) \<longleftrightarrow> digit2 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 388 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 389 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 390 | qualified lemma digit3_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 391 | "digit3 (String.ascii_of c) \<longleftrightarrow> digit3 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 392 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 393 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 394 | qualified lemma digit4_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 395 | "digit4 (String.ascii_of c) \<longleftrightarrow> digit4 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 396 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 397 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 398 | qualified lemma digit5_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 399 | "digit5 (String.ascii_of c) \<longleftrightarrow> digit5 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 400 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 401 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 402 | qualified lemma digit6_ascii_of_iff [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 403 | "digit6 (String.ascii_of c) \<longleftrightarrow> digit6 c" | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 404 | by (simp add: String.ascii_of_def) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 405 | |
| 68028 | 406 | qualified lemma not_digit7_ascii_of [simp]: | 
| 407 | "\<not> digit7 (ascii_of c)" | |
| 408 | by (simp add: ascii_of_def) | |
| 409 | ||
| 410 | qualified lemma ascii_of_idem: | |
| 411 | "ascii_of c = c" if "\<not> digit7 c" | |
| 412 | using that by (cases c) simp | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 413 | |
| 68028 | 414 | qualified typedef literal = "{cs. \<forall>c\<in>set cs. \<not> digit7 c}"
 | 
| 415 | morphisms explode Abs_literal | |
| 416 | proof | |
| 417 |   show "[] \<in> {cs. \<forall>c\<in>set cs. \<not> digit7 c}"
 | |
| 418 | by simp | |
| 419 | qed | |
| 420 | ||
| 421 | qualified setup_lifting type_definition_literal | |
| 59484 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 haftmann parents: 
59483diff
changeset | 422 | |
| 68028 | 423 | qualified lift_definition implode :: "string \<Rightarrow> literal" | 
| 424 | is "map ascii_of" | |
| 425 | by auto | |
| 426 | ||
| 427 | qualified lemma implode_explode_eq [simp]: | |
| 428 | "String.implode (String.explode s) = s" | |
| 429 | proof transfer | |
| 430 | fix cs | |
| 431 | show "map ascii_of cs = cs" if "\<forall>c\<in>set cs. \<not> digit7 c" | |
| 432 | using that | |
| 433 | by (induction cs) (simp_all add: ascii_of_idem) | |
| 434 | qed | |
| 435 | ||
| 436 | qualified lemma explode_implode_eq [simp]: | |
| 437 | "String.explode (String.implode cs) = map ascii_of cs" | |
| 59484 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 haftmann parents: 
59483diff
changeset | 438 | by transfer rule | 
| 54594 
a2d1522cdd54
setup lifting/transfer for String.literal
 Andreas Lochbihler parents: 
54317diff
changeset | 439 | |
| 68028 | 440 | end | 
| 441 | ||
| 75622 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 442 | context unique_euclidean_semiring_with_bit_operations | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 443 | begin | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 444 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 445 | context | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 446 | begin | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 447 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 448 | qualified lemma char_of_ascii_of [simp]: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 449 | \<open>of_char (String.ascii_of c) = take_bit 7 (of_char c)\<close> | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 450 | by (cases c) (simp only: String.ascii_of_Char of_char_Char take_bit_horner_sum_bit_eq, simp) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 451 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 452 | qualified lemma ascii_of_char_of: | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 453 | \<open>String.ascii_of (char_of a) = char_of (take_bit 7 a)\<close> | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 454 | by (simp add: char_of_def bit_simps) | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 455 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 456 | end | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 457 | |
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 458 | end | 
| 
53b61706749b
Centralized some char-related lemmas in distribution.
 haftmann parents: 
75398diff
changeset | 459 | |
| 68028 | 460 | |
| 461 | subsubsection \<open>Syntactic representation\<close> | |
| 462 | ||
| 463 | text \<open> | |
| 464 | Logical ground representations for literals are: | |
| 465 | ||
| 69272 | 466 | \<^enum> \<open>0\<close> for the empty literal; | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66190diff
changeset | 467 | |
| 69272 | 468 | \<^enum> \<open>Literal b0 \<dots> b6 s\<close> for a literal starting with one | 
| 68028 | 469 | character and continued by another literal. | 
| 470 | ||
| 471 | Syntactic representations for literals are: | |
| 472 | ||
| 69272 | 473 | \<^enum> Printable text as string prefixed with \<open>STR\<close>; | 
| 68028 | 474 | |
| 69272 | 475 | \<^enum> A single ascii value as numerical hexadecimal value prefixed with \<open>STR\<close>. | 
| 68028 | 476 | \<close> | 
| 477 | ||
| 478 | instantiation String.literal :: zero | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 479 | begin | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 480 | |
| 68028 | 481 | context | 
| 482 | begin | |
| 483 | ||
| 484 | qualified lift_definition zero_literal :: String.literal | |
| 485 | is Nil | |
| 486 | by simp | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 487 | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 488 | instance .. | 
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 489 | |
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 490 | end | 
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 491 | |
| 68028 | 492 | end | 
| 493 | ||
| 494 | context | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 495 | begin | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 496 | |
| 68028 | 497 | qualified abbreviation (output) empty_literal :: String.literal | 
| 498 | where "empty_literal \<equiv> 0" | |
| 499 | ||
| 500 | qualified lift_definition Literal :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> String.literal \<Rightarrow> String.literal" | |
| 501 | is "\<lambda>b0 b1 b2 b3 b4 b5 b6 cs. Char b0 b1 b2 b3 b4 b5 b6 False # cs" | |
| 502 | by auto | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 503 | |
| 68028 | 504 | qualified lemma Literal_eq_iff [simp]: | 
| 505 | "Literal b0 b1 b2 b3 b4 b5 b6 s = Literal c0 c1 c2 c3 c4 c5 c6 t | |
| 506 | \<longleftrightarrow> (b0 \<longleftrightarrow> c0) \<and> (b1 \<longleftrightarrow> c1) \<and> (b2 \<longleftrightarrow> c2) \<and> (b3 \<longleftrightarrow> c3) | |
| 507 | \<and> (b4 \<longleftrightarrow> c4) \<and> (b5 \<longleftrightarrow> c5) \<and> (b6 \<longleftrightarrow> c6) \<and> s = t" | |
| 508 | by transfer simp | |
| 509 | ||
| 510 | qualified lemma empty_neq_Literal [simp]: | |
| 511 | "empty_literal \<noteq> Literal b0 b1 b2 b3 b4 b5 b6 s" | |
| 512 | by transfer simp | |
| 513 | ||
| 514 | qualified lemma Literal_neq_empty [simp]: | |
| 515 | "Literal b0 b1 b2 b3 b4 b5 b6 s \<noteq> empty_literal" | |
| 516 | by transfer simp | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 517 | |
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 518 | end | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 519 | |
| 68028 | 520 | code_datatype "0 :: String.literal" String.Literal | 
| 521 | ||
| 522 | syntax | |
| 523 |   "_Literal" :: "str_position \<Rightarrow> String.literal"   ("STR _")
 | |
| 68033 
ad4b8b6892c3
uniform tagging for printable and non-printable literals
 haftmann parents: 
68028diff
changeset | 524 |   "_Ascii" :: "num_const \<Rightarrow> String.literal"        ("STR _")
 | 
| 54594 
a2d1522cdd54
setup lifting/transfer for String.literal
 Andreas Lochbihler parents: 
54317diff
changeset | 525 | |
| 69605 | 526 | ML_file \<open>Tools/literal.ML\<close> | 
| 68028 | 527 | |
| 52365 
95186e6e4bf4
reflexive nbe equation for equality on String.literal
 haftmann parents: 
51717diff
changeset | 528 | |
| 68028 | 529 | subsubsection \<open>Operations\<close> | 
| 530 | ||
| 531 | instantiation String.literal :: plus | |
| 67730 | 532 | begin | 
| 533 | ||
| 68028 | 534 | context | 
| 535 | begin | |
| 536 | ||
| 537 | qualified lift_definition plus_literal :: "String.literal \<Rightarrow> String.literal \<Rightarrow> String.literal" | |
| 538 | is "(@)" | |
| 539 | by auto | |
| 67730 | 540 | |
| 541 | instance .. | |
| 542 | ||
| 543 | end | |
| 544 | ||
| 68028 | 545 | end | 
| 67730 | 546 | |
| 68028 | 547 | instance String.literal :: monoid_add | 
| 548 | by (standard; transfer) simp_all | |
| 549 | ||
| 550 | instantiation String.literal :: size | |
| 67729 | 551 | begin | 
| 552 | ||
| 68028 | 553 | context | 
| 554 | includes literal.lifting | |
| 555 | begin | |
| 556 | ||
| 557 | lift_definition size_literal :: "String.literal \<Rightarrow> nat" | |
| 558 | is length . | |
| 559 | ||
| 560 | end | |
| 67729 | 561 | |
| 562 | instance .. | |
| 563 | ||
| 564 | end | |
| 565 | ||
| 68028 | 566 | instantiation String.literal :: equal | 
| 567 | begin | |
| 568 | ||
| 569 | context | |
| 570 | begin | |
| 571 | ||
| 572 | qualified lift_definition equal_literal :: "String.literal \<Rightarrow> String.literal \<Rightarrow> bool" | |
| 573 | is HOL.equal . | |
| 574 | ||
| 575 | instance | |
| 576 | by (standard; transfer) (simp add: equal) | |
| 577 | ||
| 578 | end | |
| 579 | ||
| 580 | end | |
| 581 | ||
| 582 | instantiation String.literal :: linorder | |
| 583 | begin | |
| 67729 | 584 | |
| 68028 | 585 | context | 
| 586 | begin | |
| 587 | ||
| 588 | qualified lift_definition less_eq_literal :: "String.literal \<Rightarrow> String.literal \<Rightarrow> bool" | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 589 | is "ord.lexordp_eq (\<lambda>c d. of_char c < (of_char d :: nat))" | 
| 68028 | 590 | . | 
| 591 | ||
| 592 | qualified lift_definition less_literal :: "String.literal \<Rightarrow> String.literal \<Rightarrow> bool" | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 593 | is "ord.lexordp (\<lambda>c d. of_char c < (of_char d :: nat))" | 
| 68028 | 594 | . | 
| 595 | ||
| 596 | instance proof - | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 597 | from linorder_char interpret linorder "ord.lexordp_eq (\<lambda>c d. of_char c < (of_char d :: nat))" | 
| 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 598 | "ord.lexordp (\<lambda>c d. of_char c < (of_char d :: nat)) :: string \<Rightarrow> string \<Rightarrow> bool" | 
| 68028 | 599 | by (rule linorder.lexordp_linorder) | 
| 600 | show "PROP ?thesis" | |
| 601 | by (standard; transfer) (simp_all add: less_le_not_le linear) | |
| 602 | qed | |
| 603 | ||
| 604 | end | |
| 605 | ||
| 606 | end | |
| 67730 | 607 | |
| 68028 | 608 | lemma infinite_literal: | 
| 609 | "infinite (UNIV :: String.literal set)" | |
| 610 | proof - | |
| 611 | define S where "S = range (\<lambda>n. replicate n CHR ''A'')" | |
| 612 | have "inj_on String.implode S" | |
| 613 | proof (rule inj_onI) | |
| 614 | fix cs ds | |
| 615 | assume "String.implode cs = String.implode ds" | |
| 616 | then have "String.explode (String.implode cs) = String.explode (String.implode ds)" | |
| 617 | by simp | |
| 618 | moreover assume "cs \<in> S" and "ds \<in> S" | |
| 619 | ultimately show "cs = ds" | |
| 620 | by (auto simp add: S_def) | |
| 621 | qed | |
| 622 | moreover have "infinite S" | |
| 623 | by (auto simp add: S_def dest: finite_range_imageI [of _ length]) | |
| 624 | ultimately have "infinite (String.implode ` S)" | |
| 625 | by (simp add: finite_image_iff) | |
| 626 | then show ?thesis | |
| 627 | by (auto intro: finite_subset) | |
| 628 | qed | |
| 629 | ||
| 630 | ||
| 631 | subsubsection \<open>Executable conversions\<close> | |
| 632 | ||
| 633 | context | |
| 634 | begin | |
| 635 | ||
| 636 | qualified lift_definition asciis_of_literal :: "String.literal \<Rightarrow> integer list" | |
| 637 | is "map of_char" | |
| 638 | . | |
| 639 | ||
| 69879 | 640 | qualified lemma asciis_of_zero [simp, code]: | 
| 641 | "asciis_of_literal 0 = []" | |
| 642 | by transfer simp | |
| 643 | ||
| 644 | qualified lemma asciis_of_Literal [simp, code]: | |
| 645 | "asciis_of_literal (String.Literal b0 b1 b2 b3 b4 b5 b6 s) = | |
| 646 | of_char (Char b0 b1 b2 b3 b4 b5 b6 False) # asciis_of_literal s " | |
| 647 | by transfer simp | |
| 648 | ||
| 68028 | 649 | qualified lift_definition literal_of_asciis :: "integer list \<Rightarrow> String.literal" | 
| 650 | is "map (String.ascii_of \<circ> char_of)" | |
| 651 | by auto | |
| 55426 
90f2ceed2828
make lifting setup for String.literal local to prevent transfer from replacing STR ''...'' literals
 Andreas Lochbihler parents: 
55015diff
changeset | 652 | |
| 69879 | 653 | qualified lemma literal_of_asciis_Nil [simp, code]: | 
| 654 | "literal_of_asciis [] = 0" | |
| 655 | by transfer simp | |
| 656 | ||
| 657 | qualified lemma literal_of_asciis_Cons [simp, code]: | |
| 658 | "literal_of_asciis (k # ks) = (case char_of k | |
| 659 | of Char b0 b1 b2 b3 b4 b5 b6 b7 \<Rightarrow> String.Literal b0 b1 b2 b3 b4 b5 b6 (literal_of_asciis ks))" | |
| 660 | by (simp add: char_of_def) (transfer, simp add: char_of_def) | |
| 661 | ||
| 68028 | 662 | qualified lemma literal_of_asciis_of_literal [simp]: | 
| 663 | "literal_of_asciis (asciis_of_literal s) = s" | |
| 664 | proof transfer | |
| 665 | fix cs | |
| 666 | assume "\<forall>c\<in>set cs. \<not> digit7 c" | |
| 667 | then show "map (String.ascii_of \<circ> char_of) (map of_char cs) = cs" | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 668 | by (induction cs) (simp_all add: String.ascii_of_idem) | 
| 68028 | 669 | qed | 
| 670 | ||
| 671 | qualified lemma explode_code [code]: | |
| 672 | "String.explode s = map char_of (asciis_of_literal s)" | |
| 673 | by transfer simp | |
| 674 | ||
| 675 | qualified lemma implode_code [code]: | |
| 676 | "String.implode cs = literal_of_asciis (map of_char cs)" | |
| 677 | by transfer simp | |
| 64994 
6e4c05e8edbb
computation preprocessing rules to allow literals as input for computations
 haftmann parents: 
64630diff
changeset | 678 | |
| 69879 | 679 | qualified lemma equal_literal [code]: | 
| 680 | "HOL.equal (String.Literal b0 b1 b2 b3 b4 b5 b6 s) | |
| 681 | (String.Literal a0 a1 a2 a3 a4 a5 a6 r) | |
| 682 | \<longleftrightarrow> (b0 \<longleftrightarrow> a0) \<and> (b1 \<longleftrightarrow> a1) \<and> (b2 \<longleftrightarrow> a2) \<and> (b3 \<longleftrightarrow> a3) | |
| 683 | \<and> (b4 \<longleftrightarrow> a4) \<and> (b5 \<longleftrightarrow> a5) \<and> (b6 \<longleftrightarrow> a6) \<and> (s = r)" | |
| 684 | by (simp add: equal) | |
| 68028 | 685 | |
| 69879 | 686 | end | 
| 68028 | 687 | |
| 688 | ||
| 689 | subsubsection \<open>Technical code generation setup\<close> | |
| 690 | ||
| 691 | text \<open>Alternative constructor for generated computations\<close> | |
| 692 | ||
| 693 | context | |
| 72170 
7fa9605b226c
Another go with lex: now lexordp back in class ord
 paulson <lp15@cam.ac.uk> parents: 
72164diff
changeset | 694 | begin | 
| 68028 | 695 | |
| 696 | qualified definition Literal' :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> String.literal \<Rightarrow> String.literal" | |
| 697 | where [simp]: "Literal' = String.Literal" | |
| 698 | ||
| 699 | lemma [code]: | |
| 71535 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 700 | \<open>Literal' b0 b1 b2 b3 b4 b5 b6 s = String.literal_of_asciis | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 701 | [foldr (\<lambda>b k. of_bool b + k * 2) [b0, b1, b2, b3, b4, b5, b6] 0] + s\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 702 | proof - | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 703 | have \<open>foldr (\<lambda>b k. of_bool b + k * 2) [b0, b1, b2, b3, b4, b5, b6] 0 = of_char (Char b0 b1 b2 b3 b4 b5 b6 False)\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 704 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 705 | moreover have \<open>Literal' b0 b1 b2 b3 b4 b5 b6 s = String.literal_of_asciis | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 706 | [of_char (Char b0 b1 b2 b3 b4 b5 b6 False)] + s\<close> | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 707 | by (unfold Literal'_def) (transfer, simp only: list.simps comp_apply char_of_char, simp) | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 708 | ultimately show ?thesis | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 709 | by simp | 
| 
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
 haftmann parents: 
71094diff
changeset | 710 | qed | 
| 64994 
6e4c05e8edbb
computation preprocessing rules to allow literals as input for computations
 haftmann parents: 
64630diff
changeset | 711 | |
| 
6e4c05e8edbb
computation preprocessing rules to allow literals as input for computations
 haftmann parents: 
64630diff
changeset | 712 | lemma [code_computation_unfold]: | 
| 68028 | 713 | "String.Literal = Literal'" | 
| 714 | by simp | |
| 64994 
6e4c05e8edbb
computation preprocessing rules to allow literals as input for computations
 haftmann parents: 
64630diff
changeset | 715 | |
| 68028 | 716 | end | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 717 | |
| 69879 | 718 | code_reserved SML string String Char List | 
| 68028 | 719 | code_reserved OCaml string String Char List | 
| 720 | code_reserved Haskell Prelude | |
| 34886 | 721 | code_reserved Scala string | 
| 33237 | 722 | |
| 75647 
34cd1d210b92
officical abstract characters for code generation
 haftmann parents: 
75622diff
changeset | 723 | code_identifier | 
| 
34cd1d210b92
officical abstract characters for code generation
 haftmann parents: 
75622diff
changeset | 724 | code_module String \<rightharpoonup> | 
| 
34cd1d210b92
officical abstract characters for code generation
 haftmann parents: 
75622diff
changeset | 725 | (SML) Str and (OCaml) Str and (Haskell) Str and (Scala) Str | 
| 
34cd1d210b92
officical abstract characters for code generation
 haftmann parents: 
75622diff
changeset | 726 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 727 | code_printing | 
| 68028 | 728 | type_constructor String.literal \<rightharpoonup> | 
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 729 | (SML) "string" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 730 | and (OCaml) "string" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 731 | and (Haskell) "String" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 732 | and (Scala) "String" | 
| 68028 | 733 | | constant "STR ''''" \<rightharpoonup> | 
| 734 | (SML) "\"\"" | |
| 735 | and (OCaml) "\"\"" | |
| 736 | and (Haskell) "\"\"" | |
| 737 | and (Scala) "\"\"" | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 738 | |
| 60758 | 739 | setup \<open> | 
| 68028 | 740 | fold Literal.add_code ["SML", "OCaml", "Haskell", "Scala"] | 
| 60758 | 741 | \<close> | 
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 742 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 743 | code_printing | 
| 68028 | 744 | constant "(+) :: String.literal \<Rightarrow> String.literal \<Rightarrow> String.literal" \<rightharpoonup> | 
| 745 | (SML) infixl 18 "^" | |
| 746 | and (OCaml) infixr 6 "^" | |
| 747 | and (Haskell) infixr 5 "++" | |
| 748 | and (Scala) infixl 7 "+" | |
| 749 | | constant String.literal_of_asciis \<rightharpoonup> | |
| 69879 | 750 | (SML) "!(String.implode/ o List.map (fn k => if 0 <= k andalso k < 128 then (Char.chr o IntInf.toInt) k else raise Fail \"Non-ASCII character in literal\"))" | 
| 69743 | 751 | and (OCaml) "!(let xs = _ | 
| 752 | and chr k = | |
| 69906 
55534affe445
migrated from Nums to Zarith as library for OCaml integer arithmetic
 haftmann parents: 
69879diff
changeset | 753 | let l = Z.to'_int k | 
| 69743 | 754 | in if 0 <= l && l < 128 | 
| 755 | then Char.chr l | |
| 756 | else failwith \"Non-ASCII character in literal\" | |
| 757 | in String.init (List.length xs) (List.nth (List.map chr xs)))" | |
| 68028 | 758 | and (Haskell) "map/ (let chr k | (0 <= k && k < 128) = Prelude.toEnum k :: Prelude.Char in chr . Prelude.fromInteger)" | 
| 759 | and (Scala) "\"\"/ ++/ _.map((k: BigInt) => if (BigInt(0) <= k && k < BigInt(128)) k.charValue else sys.error(\"Non-ASCII character in literal\"))" | |
| 760 | | constant String.asciis_of_literal \<rightharpoonup> | |
| 75398 | 761 | (SML) "!(List.map (fn c => let val k = Char.ord c in if k < 128 then IntInf.fromInt k else raise Fail \"Non-ASCII character in literal\" end)/ o String.explode)" | 
| 69743 | 762 | and (OCaml) "!(let s = _ in let rec exp i l = if i < 0 then l else exp (i - 1) (let k = Char.code (String.get s i) in | 
| 69906 
55534affe445
migrated from Nums to Zarith as library for OCaml integer arithmetic
 haftmann parents: 
69879diff
changeset | 763 | if k < 128 then Z.of'_int k :: l else failwith \"Non-ASCII character in literal\") in exp (String.length s - 1) [])" | 
| 68028 | 764 | and (Haskell) "map/ (let ord k | (k < 128) = Prelude.toInteger k in ord . (Prelude.fromEnum :: Prelude.Char -> Prelude.Int))" | 
| 765 |     and (Scala) "!(_.toList.map(c => { val k: Int = c.toInt; if (k < 128) BigInt(k) else sys.error(\"Non-ASCII character in literal\") }))"
 | |
| 766 | | class_instance String.literal :: equal \<rightharpoonup> | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 767 | (Haskell) - | 
| 68028 | 768 | | constant "HOL.equal :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup> | 
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 769 | (SML) "!((_ : string) = _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 770 | and (OCaml) "!((_ : string) = _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 771 | and (Haskell) infix 4 "==" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52365diff
changeset | 772 | and (Scala) infixl 5 "==" | 
| 68028 | 773 | | constant "(\<le>) :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup> | 
| 774 | (SML) "!((_ : string) <= _)" | |
| 775 | and (OCaml) "!((_ : string) <= _)" | |
| 69743 | 776 | and (Haskell) infix 4 "<=" | 
| 69593 | 777 | \<comment> \<open>Order operations for \<^typ>\<open>String.literal\<close> work in Haskell only | 
| 68028 | 778 | if no type class instance needs to be generated, because String = [Char] in Haskell | 
| 69593 | 779 | and \<^typ>\<open>char list\<close> need not have the same order as \<^typ>\<open>String.literal\<close>.\<close> | 
| 68028 | 780 | and (Scala) infixl 4 "<=" | 
| 781 | and (Eval) infixl 6 "<=" | |
| 782 | | constant "(<) :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup> | |
| 783 | (SML) "!((_ : string) < _)" | |
| 784 | and (OCaml) "!((_ : string) < _)" | |
| 785 | and (Haskell) infix 4 "<" | |
| 786 | and (Scala) infixl 4 "<" | |
| 787 | and (Eval) infixl 6 "<" | |
| 788 | ||
| 789 | ||
| 790 | subsubsection \<open>Code generation utility\<close> | |
| 31051 
4d9b52e0a48c
refined HOL string theories and corresponding ML fragments
 haftmann parents: diff
changeset | 791 | |
| 60758 | 792 | setup \<open>Sign.map_naming (Name_Space.mandatory_path "Code")\<close> | 
| 52910 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 793 | |
| 68028 | 794 | definition abort :: "String.literal \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> 'a" | 
| 795 | where [simp]: "abort _ f = f ()" | |
| 52910 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 796 | |
| 68028 | 797 | declare [[code drop: Code.abort]] | 
| 798 | ||
| 799 | lemma abort_cong: | |
| 800 | "msg = msg' \<Longrightarrow> Code.abort msg f = Code.abort msg' f" | |
| 801 | by simp | |
| 54317 
da932f511746
add congruence rule to prevent code_simp from looping
 Andreas Lochbihler parents: 
52910diff
changeset | 802 | |
| 60758 | 803 | setup \<open>Sign.map_naming Name_Space.parent_path\<close> | 
| 52910 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 804 | |
| 60758 | 805 | setup \<open>Code_Simp.map_ss (Simplifier.add_cong @{thm Code.abort_cong})\<close>
 | 
| 54317 
da932f511746
add congruence rule to prevent code_simp from looping
 Andreas Lochbihler parents: 
52910diff
changeset | 806 | |
| 68028 | 807 | code_printing | 
| 808 | constant Code.abort \<rightharpoonup> | |
| 52910 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 809 | (SML) "!(raise/ Fail/ _)" | 
| 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 810 | and (OCaml) "failwith" | 
| 59483 
ddb73392356e
explicit type annotation avoids problems with Haskell type inference
 haftmann parents: 
58889diff
changeset | 811 | and (Haskell) "!(error/ ::/ forall a./ String -> (() -> a) -> a)" | 
| 68028 | 812 |     and (Scala) "!{/ sys.error((_));/ ((_)).apply(())/ }"
 | 
| 813 | ||
| 52910 
7bfe0df532a9
abort execution of generated code with explicit exception message
 Andreas Lochbihler parents: 
52435diff
changeset | 814 | |
| 68028 | 815 | subsubsection \<open>Finally\<close> | 
| 31205 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 haftmann parents: 
31176diff
changeset | 816 | |
| 68028 | 817 | lifting_update literal.lifting | 
| 818 | lifting_forget literal.lifting | |
| 57437 | 819 | |
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 820 | end |