1 (* Title: HOL/Library/Code_Char.thy
2 Author: Florian Haftmann
5 header {* Code generation of pretty characters (and strings) *}
12 type_constructor char \<rightharpoonup>
15 and (Haskell) "Prelude.Char"
19 fold String_Code.add_literal_char ["SML", "OCaml", "Haskell", "Scala"]
20 #> String_Code.add_literal_list_string "Haskell"
24 class_instance char :: equal \<rightharpoonup>
26 | constant "HOL.equal :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
27 (SML) "!((_ : char) = _)"
28 and (OCaml) "!((_ : char) = _)"
29 and (Haskell) infix 4 "=="
30 and (Scala) infixl 5 "=="
31 | constant "Code_Evaluation.term_of \<Colon> char \<Rightarrow> term" \<rightharpoonup>
32 (Eval) "HOLogic.mk'_char/ (IntInf.fromInt/ (Char.ord/ _))"
43 definition implode :: "string \<Rightarrow> String.literal" where
46 code_reserved SML String
49 constant implode \<rightharpoonup>
50 (SML) "String.implode"
51 and (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 and (Scala) "!(\"\" ++/ _)"
54 | constant explode \<rightharpoonup>
55 (SML) "String.explode"
56 and (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) [])"
58 and (Scala) "!(_.toList)"
61 definition integer_of_char :: "char \<Rightarrow> integer"
63 "integer_of_char = integer_of_nat o nat_of_char"
65 definition char_of_integer :: "integer \<Rightarrow> char"
67 "char_of_integer = char_of_nat \<circ> nat_of_integer"
70 "nat_of_char = nat_of_integer o integer_of_char"
71 by (simp add: integer_of_char_def fun_eq_iff)
74 "char_of_nat = char_of_integer o integer_of_nat"
75 by (simp add: char_of_integer_def fun_eq_iff)
78 constant integer_of_char \<rightharpoonup>
79 (SML) "!(IntInf.fromInt o Char.ord)"
80 and (OCaml) "Big'_int.big'_int'_of'_int (Char.code _)"
81 and (Haskell) "Prelude.toInteger (Prelude.fromEnum (_ :: Prelude.Char))"
82 and (Scala) "BigInt(_.toInt)"
83 | constant char_of_integer \<rightharpoonup>
84 (SML) "!(Char.chr o IntInf.toInt)"
85 and (OCaml) "Char.chr (Big'_int.int'_of'_big'_int _)"
86 and (Haskell) "!(let chr k | (0 <= k && k < 256) = Prelude.toEnum k :: Prelude.Char in chr . Prelude.fromInteger)"
87 and (Scala) "!((k: BigInt) => if (BigInt(0) <= k && k < BigInt(256)) k.charValue else error(\"character value out of range\"))"
88 | constant "Orderings.less_eq :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
89 (SML) "!((_ : char) <= _)"
90 and (OCaml) "!((_ : char) <= _)"
91 and (Haskell) infix 4 "<="
92 and (Scala) infixl 4 "<="
93 and (Eval) infixl 6 "<="
94 | constant "Orderings.less :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
95 (SML) "!((_ : char) < _)"
96 and (OCaml) "!((_ : char) < _)"
97 and (Haskell) infix 4 "<"
98 and (Scala) infixl 4 "<"
99 and (Eval) infixl 6 "<"
100 | constant "Orderings.less_eq :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
101 (SML) "!((_ : string) <= _)"
102 and (OCaml) "!((_ : string) <= _)"
103 -- {* Order operations for @{typ String.literal} work in Haskell only
104 if no type class instance needs to be generated, because String = [Char] in Haskell
105 and @{typ "char list"} need not have the same order as @{typ String.literal}. *}
106 and (Haskell) infix 4 "<="
107 and (Scala) infixl 4 "<="
108 and (Eval) infixl 6 "<="
109 | constant "Orderings.less :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
110 (SML) "!((_ : string) < _)"
111 and (OCaml) "!((_ : string) < _)"
112 and (Haskell) infix 4 "<"
113 and (Scala) infixl 4 "<"
114 and (Eval) infixl 6 "<"