1 (* Title: HOL/Library/Code_Char.thy
2 Author: Florian Haftmann
5 header {* Code generation of pretty characters (and strings) *}
8 imports Main "~~/src/HOL/Library/Char_ord"
14 (Haskell "Prelude.Char")
18 fold String_Code.add_literal_char ["SML", "OCaml", "Haskell", "Scala"]
19 #> String_Code.add_literal_list_string "Haskell"
22 code_instance char :: equal
34 code_const "HOL.equal \<Colon> char \<Rightarrow> char \<Rightarrow> bool"
35 (SML "!((_ : char) = _)")
36 (OCaml "!((_ : char) = _)")
37 (Haskell infix 4 "==")
40 code_const "Code_Evaluation.term_of \<Colon> char \<Rightarrow> term"
41 (Eval "HOLogic.mk'_char/ (IntInf.fromInt/ (Char.ord/ _))")
44 definition implode :: "string \<Rightarrow> String.literal" where
47 code_reserved SML String
50 (SML "String.implode")
51 (OCaml "!(let l = _ in let res = String.create (List.length l) in let rec imp i = function | [] -> res | c :: l -> String.set res i c; imp (i + 1) l in imp 0 l)")
53 (Scala "!(\"\" ++/ _)")
56 (SML "String.explode")
57 (OCaml "!(let s = _ in let rec exp i l = if i < 0 then l else exp (i - 1) (String.get s i :: l) in exp (String.length s - 1) [])")
62 definition integer_of_char :: "char \<Rightarrow> integer"
64 "integer_of_char = integer_of_nat o nat_of_char"
66 definition char_of_integer :: "integer \<Rightarrow> char"
68 "char_of_integer = char_of_nat \<circ> nat_of_integer"
71 "nat_of_char = nat_of_integer o integer_of_char"
72 by (simp add: integer_of_char_def fun_eq_iff)
75 "char_of_nat = char_of_integer o integer_of_nat"
76 by (simp add: char_of_integer_def fun_eq_iff)
78 code_const integer_of_char and char_of_integer
79 (SML "!(IntInf.fromInt o Char.ord)"
80 and "!(Char.chr o IntInf.toInt)")
81 (OCaml "Big'_int.big'_int'_of'_int (Char.code _)"
82 and "Char.chr (Big'_int.int'_of'_big'_int _)")
83 (Haskell "Prelude.toInteger (Prelude.fromEnum (_ :: Prelude.Char))"
84 and "!(let chr k | (0 <= k && k < 256) = Prelude.toEnum k :: Prelude.Char in chr . Prelude.fromInteger)")
85 (Scala "BigInt(_.toInt)"
86 and "!((k: BigInt) => if (BigInt(0) <= k && k < BigInt(256)) k.charValue else error(\"character value out of range\"))")
89 code_const "Orderings.less_eq \<Colon> char \<Rightarrow> char \<Rightarrow> bool"
90 (SML "!((_ : char) <= _)")
91 (OCaml "!((_ : char) <= _)")
92 (Haskell infix 4 "<=")
96 code_const "Orderings.less \<Colon> char \<Rightarrow> char \<Rightarrow> bool"
97 (SML "!((_ : char) < _)")
98 (OCaml "!((_ : char) < _)")