lemma (in comm_semiring_1) of_nat_of_char: \<open>of_nat (of_char c) = of_char c\<close> by (cases c) simp
lemma (in comm_ring_1) of_int_of_char: \<open>of_int (of_char c) = of_char c\<close> by (cases c) simp
lemma nat_of_char [simp]: \<open>nat (of_char c) = of_char c\<close> by (cases c) (simp only: of_char_Char nat_horner_sum)
context linordered_euclidean_semiring_bit_operations begin
definition char_of :: \<open>'a \<Rightarrow> char\<close> 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>
lemma char_of_take_bit_eq: \<open>char_of (take_bit n m) = char_of m\<close> if \<open>n \<ge> 8\<close> using that by (simp add: char_of_def bit_take_bit_iff)
lemma char_of_char [simp]: \<open>char_of (of_char c) = c\<close> by (simp only: of_char_def char_of_def bit_horner_sum_bit_iff) simp
lemma inj_of_char: \<open>inj of_char\<close> proof (rule injI) fix c d assume"of_char c = of_char d" thenhave"char_of (of_char c) = char_of (of_char d)" by simp thenshow"c = d" by simp qed
lemma of_char_eqI: \<open>c = d\<close> if \<open>of_char c = of_char d\<close> using that inj_of_char by (simp add: inj_eq)
lemma of_char_eq_iff [simp]: \<open>of_char c = of_char d \<longleftrightarrow> c = d\<close> by (auto intro: of_char_eqI)
lemma of_char_of [simp]: \<open>of_char (char_of a) = a mod 256\<close> proof - have\<open>[0..<8] = [0, Suc 0, 2, 3, 4, 5, 6, 7 :: nat]\<close> by (simp add: upt_eq_Cons_conv) thenhave\<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> by simp thenhave\<open>of_char (char_of a) = take_bit 8 a\<close> by (simp only: char_of_def of_char_def char.sel horner_sum_bit_eq_take_bit) thenshow ?thesis by (simp add: take_bit_eq_mod) qed
lemma char_of_mod_256 [simp]: \<open>char_of (n mod 256) = char_of n\<close> by (rule of_char_eqI) simp
lemma of_char_mod_256 [simp]: \<open>of_char c mod 256 = of_char c\<close> proof - have\<open>of_char (char_of (of_char c)) mod 256 = of_char (char_of (of_char c))\<close> by (simp only: of_char_of) simp thenshow ?thesis by simp qed
lemma char_of_quasi_inj [simp]: \<open>char_of m = char_of n \<longleftrightarrow> m mod 256 = n mod 256\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) proof assume ?Q thenshow ?P by (auto intro: of_char_eqI) next assume ?P thenhave\<open>of_char (char_of m) = of_char (char_of n)\<close> by simp thenshow ?Q by simp qed
lemma char_of_eq_iff: \<open>char_of n = c \<longleftrightarrow> take_bit 8 n = of_char c\<close> by (auto intro: of_char_eqI simp add: take_bit_eq_mod)
lemma inj_on_char_of_nat [simp]: "inj_on char_of {0::nat..<256}" by (rule inj_onI) simp
lemma nat_of_char_less_256 [simp]: "of_char c < (256 :: nat)" proof - have"of_char c mod (256 :: nat) < 256" by arith thenshow ?thesis by simp qed
lemma range_nat_of_char: "range of_char = {0::nat..<256}" proof (rule; rule) fix n :: nat assume"n \ range of_char" thenshow"n \ {0..<256}" by auto next fix n :: nat assume"n \ {0..<256}" thenhave"n = of_char (char_of n)" by simp thenshow"n \ range of_char" by (rule range_eqI) qed
lemma UNIV_char_of_nat: "UNIV = char_of ` {0::nat..<256}" proof - have"range (of_char :: char \ nat) = of_char ` char_of ` {0::nat..<256}" by (simp add: image_image range_nat_of_char) with inj_of_char [where ?'a = nat] show ?thesis by (simp add: inj_image_eq_iff) qed
qualified definition ascii_of :: "char \ char" where"ascii_of c = Char (digit0 c) (digit1 c) (digit2 c) (digit3 c) (digit4 c) (digit5 c) (digit6 c) False"
instance by (standard; transfer) (simp add: equal)
end
end
instantiation String.literal :: linorder begin
context begin
qualified lift_definition less_eq_literal :: "String.literal \ String.literal \ bool" is"ord.lexordp_eq (\c d. of_char c < (of_char d :: nat))"
.
qualified lift_definition less_literal :: "String.literal \ String.literal \ bool" is"ord.lexordp (\c d. of_char c < (of_char d :: nat))"
.
instanceproof - from linorder_char interpret linorder "ord.lexordp_eq (\c d. of_char c < (of_char d :: nat))" "ord.lexordp (\c d. of_char c < (of_char d :: nat)) :: string \ string \ bool" by (rule linorder.lexordp_linorder) show"PROP ?thesis" by (standard; transfer) (simp_all add: less_le_not_le linear) qed
end
end
lemma infinite_literal: "infinite (UNIV :: String.literal set)" proof -
define S where"S = range (\n. replicate n CHR ''A'')" have"inj_on String.implode S" proof (rule inj_onI) fix cs ds assume"String.implode cs = String.implode ds" thenhave"String.explode (String.implode cs) = String.explode (String.implode ds)" by simp moreoverassume"cs \ S" and "ds \ S" ultimatelyshow"cs = ds" by (auto simp add: S_def) qed moreoverhave"infinite S" by (auto simp add: S_def dest: finite_range_imageI [of _ length]) ultimatelyhave"infinite (String.implode ` S)" by (simp add: finite_image_iff) thenshow ?thesis by (auto intro: finite_subset) qed
lemma [code]: \<open>Literal' b0 b1 b2 b3 b4 b5 b6 s = String.literal_of_asciis
[foldr (\<lambda>b k. of_bool b + k * 2) [b0, b1, b2, b3, b4, b5, b6] 0] + s\<close> proof - 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> by simp moreoverhave\<open>Literal' b0 b1 b2 b3 b4 b5 b6 s = String.literal_of_asciis
[of_char (Char b0 b1 b2 b3 b4 b5 b6 False)] + s\<close> by (unfold Literal'_def) (transfer, simp only: list.simps comp_apply char_of_char, simp) ultimatelyshow ?thesis by simp qed
lemma [code_computation_unfold]: "String.Literal = Literal'" by simp
end
code_reserved
(SML) string String Char Str_Literal and (OCaml) string String Char Str_Literal and (Haskell) Str_Literal and (Scala) String Str_Literal
code_identifier code_module String \<rightharpoonup>
(SML) Str and (OCaml) Str and (Haskell) Str and (Scala) Str
code_printing
type_constructor String.literal \<rightharpoonup>
(SML) "string" and (OCaml) "string" and (Haskell) "String" and (Scala) "String"
| constant "STR ''''"\<rightharpoonup>
(SML) "\"\"" and (OCaml) "\"\"" and (Haskell) "\"\"" and (Scala) "\"\""
code_printing code_module"Str_Literal"\<rightharpoonup>
(SML) \<open>structure Str_Literal : sig
type int = IntInf.int
val literal_of_asciis : int list -> string
val asciis_of_literal : string -> int list end = struct
open IntInf;
fun map f [] = []
| map f (x :: xs) = f x :: map f xs; (* deliberate clone not relying on List._ structure *)
fun check_ascii k = if 0 <= k andalso k < 128 then k
else raise Fail "Non-ASCII character in literal";
val char_of_ascii = Char.chr o toInt o (fn k => k mod 128);
val ascii_of_char = check_ascii o fromInt o Char.ord;
val literal_of_asciis = String.implode o map char_of_ascii;
val asciis_of_literal = map ascii_of_char o String.explode;
end;\<close> for constant String.literal_of_asciis String.asciis_of_literal and (OCaml) \<open>module Str_Literal : sig
val literal_of_asciis : Z.t list -> string
val asciis_of_literal: string -> Z.t list end = struct
(* deliberate clones not relying on List._ module *)
let rec length xs = match xs with
[] -> 0
| x :: xs -> 1 + length xs;;
let rec nth xs n = match xs with
(x :: xs) -> if n <= 0 then x else nth xs (n - 1);;
let rec map_range f n = if n <= 0 then []
else let m = n - 1 in map_range f m @ [f m];;
let implode f xs =
String.init (length xs) (fun n -> f (nth xs n));;
let explode f s =
map_range (fun n -> f (String.get s n)) (String.length s);;
let z_128 = Z.of_int 128;;
let check_ascii k = if 0 <= k && k < 128 then k
else failwith "Non-ASCII character in literal";;
let char_of_ascii k = Char.chr (Z.to_int (Z.rem k z_128));;
let ascii_of_char c = Z.of_int (check_ascii (Char.code c));;
let literal_of_asciis ks = implode char_of_ascii ks;;
let asciis_of_literal s = explode ascii_of_char s;;
end;;\<close> for constant String.literal_of_asciis String.asciis_of_literal and (Haskell) \<open>module Str_Literal(literalOfAsciis, asciisOfLiteral) where
check_ascii :: Int -> Int
check_ascii k
| (0 <= k && k < 128) = k
| otherwise = error "Non-ASCII character in literal"
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.16Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.