src/HOL/Library/Code_Char.thy
author haftmann
Mon, 23 Jul 2012 09:28:03 +0200
changeset 49446 6efff142bb54
parent 43469 85ca44488a29
child 52297 599ff65b85e2
permissions -rw-r--r--
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
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
haftmann@32657
     8
imports List Code_Evaluation Main
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@24999
    11
code_type char
haftmann@24999
    12
  (SML "char")
haftmann@24999
    13
  (OCaml "char")
haftmann@49446
    14
  (Haskell "Prelude.Char")
haftmann@37221
    15
  (Scala "Char")
haftmann@24999
    16
haftmann@24999
    17
setup {*
haftmann@37221
    18
  fold String_Code.add_literal_char ["SML", "OCaml", "Haskell", "Scala"] 
haftmann@31053
    19
  #> String_Code.add_literal_list_string "Haskell"
haftmann@24999
    20
*}
haftmann@24999
    21
haftmann@39086
    22
code_instance char :: equal
haftmann@24999
    23
  (Haskell -)
haftmann@24999
    24
haftmann@24999
    25
code_reserved SML
haftmann@24999
    26
  char
haftmann@24999
    27
haftmann@24999
    28
code_reserved OCaml
haftmann@24999
    29
  char
haftmann@24999
    30
haftmann@37221
    31
code_reserved Scala
haftmann@37221
    32
  char
haftmann@37221
    33
haftmann@39086
    34
code_const "HOL.equal \<Colon> char \<Rightarrow> char \<Rightarrow> bool"
haftmann@24999
    35
  (SML "!((_ : char) = _)")
haftmann@24999
    36
  (OCaml "!((_ : char) = _)")
haftmann@39499
    37
  (Haskell infix 4 "==")
haftmann@37221
    38
  (Scala infixl 5 "==")
haftmann@24999
    39
haftmann@32657
    40
code_const "Code_Evaluation.term_of \<Colon> char \<Rightarrow> term"
haftmann@31046
    41
  (Eval "HOLogic.mk'_char/ (IntInf.fromInt/ (Char.ord/ _))")
haftmann@28228
    42
haftmann@33226
    43
haftmann@33226
    44
definition implode :: "string \<Rightarrow> String.literal" where
haftmann@33226
    45
  "implode = STR"
haftmann@33226
    46
haftmann@33226
    47
code_reserved SML String
haftmann@33226
    48
haftmann@33226
    49
code_const implode
haftmann@33226
    50
  (SML "String.implode")
bulwahn@43469
    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)")
haftmann@33226
    52
  (Haskell "_")
haftmann@37434
    53
  (Scala "!(\"\" ++/ _)")
haftmann@33226
    54
haftmann@33226
    55
code_const explode
haftmann@33226
    56
  (SML "String.explode")
bulwahn@43469
    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) [])")
haftmann@33226
    58
  (Haskell "_")
haftmann@37434
    59
  (Scala "!(_.toList)")
haftmann@33226
    60
haftmann@24999
    61
end