src/HOL/Library/Code_Integer.thy
author haftmann
Fri, 22 Jan 2010 13:38:28 +0100
changeset 34931 970e1466028d
parent 34899 8674bb6f727b
child 38185 844977c7abeb
permissions -rw-r--r--
code literals: distinguish numeral classes by different entries
haftmann@24999
     1
(*  Title:      HOL/Library/Code_Integer.thy
haftmann@24999
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24999
     3
*)
haftmann@24999
     4
haftmann@24999
     5
header {* Pretty integer literals for code generation *}
haftmann@24999
     6
haftmann@24999
     7
theory Code_Integer
haftmann@31203
     8
imports Main
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@24999
    11
text {*
haftmann@24999
    12
  HOL numeral expressions are mapped to integer literals
haftmann@24999
    13
  in target languages, using predefined target language
haftmann@24999
    14
  operations for abstract integer operations.
haftmann@24999
    15
*}
haftmann@24999
    16
haftmann@24999
    17
code_type int
haftmann@24999
    18
  (SML "IntInf.int")
haftmann@24999
    19
  (OCaml "Big'_int.big'_int")
haftmann@24999
    20
  (Haskell "Integer")
haftmann@34899
    21
  (Scala "BigInt")
haftmann@24999
    22
haftmann@24999
    23
code_instance int :: eq
haftmann@24999
    24
  (Haskell -)
haftmann@24999
    25
haftmann@24999
    26
setup {*
haftmann@25928
    27
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
haftmann@34931
    28
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
haftmann@34899
    29
  #> Numeral.add_code @{const_name number_int_inst.number_of_int}
haftmann@34931
    30
    true Code_Printer.literal_numeral "Scala"
haftmann@24999
    31
*}
haftmann@24999
    32
huffman@26086
    33
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
haftmann@24999
    34
  (SML "raise/ Fail/ \"Pls\""
haftmann@24999
    35
     and "raise/ Fail/ \"Min\""
huffman@26086
    36
     and "!((_);/ raise/ Fail/ \"Bit0\")"
huffman@26086
    37
     and "!((_);/ raise/ Fail/ \"Bit1\")")
haftmann@24999
    38
  (OCaml "failwith/ \"Pls\""
haftmann@24999
    39
     and "failwith/ \"Min\""
huffman@26086
    40
     and "!((_);/ failwith/ \"Bit0\")"
huffman@26086
    41
     and "!((_);/ failwith/ \"Bit1\")")
haftmann@24999
    42
  (Haskell "error/ \"Pls\""
haftmann@24999
    43
     and "error/ \"Min\""
huffman@26086
    44
     and "error/ \"Bit0\""
huffman@26086
    45
     and "error/ \"Bit1\"")
haftmann@34899
    46
  (Scala "!error(\"Pls\")"
haftmann@34899
    47
     and "!error(\"Min\")"
haftmann@34899
    48
     and "!error(\"Bit0\")"
haftmann@34899
    49
     and "!error(\"Bit1\")")
haftmann@34899
    50
haftmann@24999
    51
haftmann@25919
    52
code_const Int.pred
haftmann@24999
    53
  (SML "IntInf.- ((_), 1)")
haftmann@24999
    54
  (OCaml "Big'_int.pred'_big'_int")
haftmann@24999
    55
  (Haskell "!(_/ -/ 1)")
haftmann@34899
    56
  (Scala "!(_/ -/ 1)")
haftmann@24999
    57
haftmann@25919
    58
code_const Int.succ
haftmann@24999
    59
  (SML "IntInf.+ ((_), 1)")
haftmann@24999
    60
  (OCaml "Big'_int.succ'_big'_int")
haftmann@24999
    61
  (Haskell "!(_/ +/ 1)")
haftmann@34899
    62
  (Scala "!(_/ +/ 1)")
haftmann@24999
    63
haftmann@24999
    64
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    65
  (SML "IntInf.+ ((_), (_))")
haftmann@24999
    66
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
    67
  (Haskell infixl 6 "+")
haftmann@34899
    68
  (Scala infixl 7 "+")
haftmann@24999
    69
haftmann@24999
    70
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann@24999
    71
  (SML "IntInf.~")
haftmann@24999
    72
  (OCaml "Big'_int.minus'_big'_int")
haftmann@24999
    73
  (Haskell "negate")
haftmann@34899
    74
  (Scala "!(- _)")
haftmann@24999
    75
haftmann@24999
    76
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    77
  (SML "IntInf.- ((_), (_))")
haftmann@24999
    78
  (OCaml "Big'_int.sub'_big'_int")
haftmann@24999
    79
  (Haskell infixl 6 "-")
haftmann@34899
    80
  (Scala infixl 7 "-")
haftmann@24999
    81
haftmann@24999
    82
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    83
  (SML "IntInf.* ((_), (_))")
haftmann@24999
    84
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
    85
  (Haskell infixl 7 "*")
haftmann@34899
    86
  (Scala infixl 8 "*")
haftmann@24999
    87
haftmann@29873
    88
code_const pdivmod
haftmann@34899
    89
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
haftmann@34899
    90
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@34931
    91
  (Haskell "divMod/ (abs _)/ (abs _)")
haftmann@34899
    92
  (Scala "!(_.abs '/% _.abs)")
haftmann@29873
    93
haftmann@28346
    94
code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
    95
  (SML "!((_ : IntInf.int) = _)")
haftmann@24999
    96
  (OCaml "Big'_int.eq'_big'_int")
haftmann@24999
    97
  (Haskell infixl 4 "==")
haftmann@34899
    98
  (Scala infixl 5 "==")
haftmann@24999
    99
haftmann@24999
   100
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   101
  (SML "IntInf.<= ((_), (_))")
haftmann@24999
   102
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   103
  (Haskell infix 4 "<=")
haftmann@34899
   104
  (Scala infixl 4 "<=")
haftmann@24999
   105
haftmann@24999
   106
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   107
  (SML "IntInf.< ((_), (_))")
haftmann@24999
   108
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   109
  (Haskell infix 4 "<")
haftmann@34899
   110
  (Scala infixl 4 "<=")
haftmann@24999
   111
haftmann@31205
   112
code_const Code_Numeral.int_of
haftmann@31192
   113
  (SML "IntInf.fromInt")
haftmann@31377
   114
  (OCaml "_")
haftmann@31192
   115
  (Haskell "toEnum")
haftmann@34931
   116
  (Scala "!BigInt((_))")
haftmann@24999
   117
haftmann@28228
   118
text {* Evaluation *}
haftmann@28228
   119
haftmann@32657
   120
code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
haftmann@31192
   121
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
haftmann@28228
   122
haftmann@24999
   123
end