src/HOL/Library/Code_Char.thy
author blanchet
Wed Sep 24 15:45:55 2014 +0200 (2014-09-24)
changeset 58425 246985c6b20b
parent 57437 0baf08c075b9
child 58881 b9556a055632
permissions -rw-r--r--
simpler proof
haftmann@24999
     1
(*  Title:      HOL/Library/Code_Char.thy
haftmann@24999
     2
    Author:     Florian Haftmann
haftmann@24999
     3
*)
haftmann@24999
     4
haftmann@24999
     5
header {* Code generation of pretty characters (and strings) *}
haftmann@24999
     6
haftmann@24999
     7
theory Code_Char
wenzelm@51542
     8
imports Main Char_ord
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@52435
    11
code_printing
haftmann@52435
    12
  type_constructor char \<rightharpoonup>
haftmann@52435
    13
    (SML) "char"
haftmann@52435
    14
    and (OCaml) "char"
haftmann@52435
    15
    and (Haskell) "Prelude.Char"
haftmann@52435
    16
    and (Scala) "Char"
haftmann@24999
    17
haftmann@24999
    18
setup {*
haftmann@37222
    19
  fold String_Code.add_literal_char ["SML", "OCaml", "Haskell", "Scala"] 
haftmann@31053
    20
  #> String_Code.add_literal_list_string "Haskell"
haftmann@24999
    21
*}
haftmann@24999
    22
haftmann@52435
    23
code_printing
haftmann@52435
    24
  class_instance char :: equal \<rightharpoonup>
haftmann@52435
    25
    (Haskell) -
haftmann@52435
    26
| constant "HOL.equal :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
    27
    (SML) "!((_ : char) = _)"
haftmann@52435
    28
    and (OCaml) "!((_ : char) = _)"
haftmann@52435
    29
    and (Haskell) infix 4 "=="
haftmann@52435
    30
    and (Scala) infixl 5 "=="
haftmann@52435
    31
| constant "Code_Evaluation.term_of \<Colon> char \<Rightarrow> term" \<rightharpoonup>
haftmann@52435
    32
    (Eval) "HOLogic.mk'_char/ (IntInf.fromInt/ (Char.ord/ _))"
haftmann@24999
    33
haftmann@24999
    34
code_reserved SML
haftmann@24999
    35
  char
haftmann@24999
    36
haftmann@24999
    37
code_reserved OCaml
haftmann@24999
    38
  char
haftmann@24999
    39
haftmann@37222
    40
code_reserved Scala
haftmann@37222
    41
  char
haftmann@37222
    42
haftmann@33234
    43
code_reserved SML String
haftmann@33234
    44
haftmann@52435
    45
code_printing
haftmann@57437
    46
  constant String.implode \<rightharpoonup>
haftmann@52435
    47
    (SML) "String.implode"
haftmann@52435
    48
    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)"
haftmann@52435
    49
    and (Haskell) "_"
haftmann@52435
    50
    and (Scala) "!(\"\" ++/ _)"
haftmann@57437
    51
| constant String.explode \<rightharpoonup>
haftmann@52435
    52
    (SML) "String.explode"
haftmann@52435
    53
    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) [])"
haftmann@52435
    54
    and (Haskell) "_"
haftmann@52435
    55
    and (Scala) "!(_.toList)"
haftmann@33234
    56
haftmann@51160
    57
haftmann@51160
    58
definition integer_of_char :: "char \<Rightarrow> integer"
haftmann@51160
    59
where
haftmann@51160
    60
  "integer_of_char = integer_of_nat o nat_of_char"
haftmann@51160
    61
haftmann@51160
    62
definition char_of_integer :: "integer \<Rightarrow> char"
haftmann@51160
    63
where
haftmann@51160
    64
  "char_of_integer = char_of_nat \<circ> nat_of_integer"
haftmann@51160
    65
haftmann@51160
    66
lemma [code]:
haftmann@51160
    67
  "nat_of_char = nat_of_integer o integer_of_char"
haftmann@51160
    68
  by (simp add: integer_of_char_def fun_eq_iff)
haftmann@51160
    69
haftmann@51160
    70
lemma [code]:
haftmann@51160
    71
  "char_of_nat = char_of_integer o integer_of_nat"
haftmann@51160
    72
  by (simp add: char_of_integer_def fun_eq_iff)
haftmann@51160
    73
Andreas@55427
    74
lemmas integer_of_char_code [code] =
Andreas@55427
    75
  nat_of_char_simps[
Andreas@55427
    76
    THEN arg_cong[where f="integer_of_nat"],
Andreas@55427
    77
    unfolded integer_of_nat_numeral integer_of_nat_1 integer_of_nat_0,
Andreas@55427
    78
    folded fun_cong[OF integer_of_char_def, unfolded o_apply]]
Andreas@55427
    79
Andreas@55427
    80
lemma char_of_integer_code [code]:
Andreas@55427
    81
  "char_of_integer n = enum_class.enum ! (nat_of_integer n mod 256)"
Andreas@55427
    82
by(simp add: char_of_integer_def char_of_nat_def)
Andreas@55427
    83
haftmann@52435
    84
code_printing
haftmann@52435
    85
  constant integer_of_char \<rightharpoonup>
haftmann@52435
    86
    (SML) "!(IntInf.fromInt o Char.ord)"
haftmann@52435
    87
    and (OCaml) "Big'_int.big'_int'_of'_int (Char.code _)"
haftmann@52435
    88
    and (Haskell) "Prelude.toInteger (Prelude.fromEnum (_ :: Prelude.Char))"
haftmann@52435
    89
    and (Scala) "BigInt(_.toInt)"
haftmann@52435
    90
| constant char_of_integer \<rightharpoonup>
haftmann@52435
    91
    (SML) "!(Char.chr o IntInf.toInt)"
haftmann@52435
    92
    and (OCaml) "Char.chr (Big'_int.int'_of'_big'_int _)"
haftmann@52435
    93
    and (Haskell) "!(let chr k | (0 <= k && k < 256) = Prelude.toEnum k :: Prelude.Char in chr . Prelude.fromInteger)"
haftmann@52435
    94
    and (Scala) "!((k: BigInt) => if (BigInt(0) <= k && k < BigInt(256)) k.charValue else error(\"character value out of range\"))"
haftmann@52435
    95
| constant "Orderings.less_eq :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
    96
    (SML) "!((_ : char) <= _)"
haftmann@52435
    97
    and (OCaml) "!((_ : char) <= _)"
haftmann@52435
    98
    and (Haskell) infix 4 "<="
haftmann@52435
    99
    and (Scala) infixl 4 "<="
haftmann@52435
   100
    and (Eval) infixl 6 "<="
haftmann@52435
   101
| constant "Orderings.less :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   102
    (SML) "!((_ : char) < _)"
haftmann@52435
   103
    and (OCaml) "!((_ : char) < _)"
haftmann@52435
   104
    and (Haskell) infix 4 "<"
haftmann@52435
   105
    and (Scala) infixl 4 "<"
haftmann@52435
   106
    and (Eval) infixl 6 "<"
Andreas@54596
   107
|  constant "Orderings.less_eq :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
Andreas@54596
   108
    (SML) "!((_ : string) <= _)"
Andreas@54596
   109
    and (OCaml) "!((_ : string) <= _)"
Andreas@54599
   110
    -- {* Order operations for @{typ String.literal} work in Haskell only 
Andreas@54599
   111
          if no type class instance needs to be generated, because String = [Char] in Haskell
Andreas@54599
   112
          and @{typ "char list"} need not have the same order as @{typ String.literal}. *}
Andreas@54596
   113
    and (Haskell) infix 4 "<="
Andreas@54596
   114
    and (Scala) infixl 4 "<="
Andreas@54596
   115
    and (Eval) infixl 6 "<="
Andreas@54596
   116
| constant "Orderings.less :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
Andreas@54596
   117
    (SML) "!((_ : string) < _)"
Andreas@54596
   118
    and (OCaml) "!((_ : string) < _)"
Andreas@54596
   119
    and (Haskell) infix 4 "<"
Andreas@54596
   120
    and (Scala) infixl 4 "<"
Andreas@54596
   121
    and (Eval) infixl 6 "<"
haftmann@51160
   122
haftmann@24999
   123
end
haftmann@51160
   124