src/HOL/Library/Code_Integer.thy
author haftmann
Mon Jul 26 11:10:35 2010 +0200 (2010-07-26)
changeset 37968 52fdcb76c0af
parent 37958 9728342bcd56
child 38053 9102e859dc0b
permissions -rw-r--r--
added Code_Natural.thy
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@37968
     8
imports Main Code_Natural
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@34944
    28
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
haftmann@24999
    29
*}
haftmann@24999
    30
huffman@26086
    31
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
haftmann@24999
    32
  (SML "raise/ Fail/ \"Pls\""
haftmann@24999
    33
     and "raise/ Fail/ \"Min\""
huffman@26086
    34
     and "!((_);/ raise/ Fail/ \"Bit0\")"
huffman@26086
    35
     and "!((_);/ raise/ Fail/ \"Bit1\")")
haftmann@24999
    36
  (OCaml "failwith/ \"Pls\""
haftmann@24999
    37
     and "failwith/ \"Min\""
huffman@26086
    38
     and "!((_);/ failwith/ \"Bit0\")"
huffman@26086
    39
     and "!((_);/ failwith/ \"Bit1\")")
haftmann@24999
    40
  (Haskell "error/ \"Pls\""
haftmann@24999
    41
     and "error/ \"Min\""
huffman@26086
    42
     and "error/ \"Bit0\""
huffman@26086
    43
     and "error/ \"Bit1\"")
haftmann@34899
    44
  (Scala "!error(\"Pls\")"
haftmann@34899
    45
     and "!error(\"Min\")"
haftmann@34899
    46
     and "!error(\"Bit0\")"
haftmann@34899
    47
     and "!error(\"Bit1\")")
haftmann@34899
    48
haftmann@25919
    49
code_const Int.pred
haftmann@24999
    50
  (SML "IntInf.- ((_), 1)")
haftmann@24999
    51
  (OCaml "Big'_int.pred'_big'_int")
haftmann@24999
    52
  (Haskell "!(_/ -/ 1)")
haftmann@34899
    53
  (Scala "!(_/ -/ 1)")
haftmann@37958
    54
  (Eval "!(_/ -/ 1)")
haftmann@24999
    55
haftmann@25919
    56
code_const Int.succ
haftmann@24999
    57
  (SML "IntInf.+ ((_), 1)")
haftmann@24999
    58
  (OCaml "Big'_int.succ'_big'_int")
haftmann@24999
    59
  (Haskell "!(_/ +/ 1)")
haftmann@34899
    60
  (Scala "!(_/ +/ 1)")
haftmann@37958
    61
  (Eval "!(_/ +/ 1)")
haftmann@24999
    62
haftmann@24999
    63
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    64
  (SML "IntInf.+ ((_), (_))")
haftmann@24999
    65
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
    66
  (Haskell infixl 6 "+")
haftmann@34899
    67
  (Scala infixl 7 "+")
haftmann@37958
    68
  (Eval infixl 8 "+")
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@37958
    75
  (Eval "~/ _")
haftmann@24999
    76
haftmann@24999
    77
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    78
  (SML "IntInf.- ((_), (_))")
haftmann@24999
    79
  (OCaml "Big'_int.sub'_big'_int")
haftmann@24999
    80
  (Haskell infixl 6 "-")
haftmann@34899
    81
  (Scala infixl 7 "-")
haftmann@37958
    82
  (Eval infixl 8 "-")
haftmann@24999
    83
haftmann@24999
    84
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    85
  (SML "IntInf.* ((_), (_))")
haftmann@24999
    86
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
    87
  (Haskell infixl 7 "*")
haftmann@34899
    88
  (Scala infixl 8 "*")
haftmann@37958
    89
  (Eval infixl 9 "*")
haftmann@24999
    90
haftmann@29936
    91
code_const pdivmod
haftmann@34899
    92
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
haftmann@34899
    93
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@34944
    94
  (Haskell "divMod/ (abs _)/ (abs _)")
haftmann@37958
    95
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@37958
    96
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
haftmann@29936
    97
haftmann@28346
    98
code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
    99
  (SML "!((_ : IntInf.int) = _)")
haftmann@24999
   100
  (OCaml "Big'_int.eq'_big'_int")
haftmann@24999
   101
  (Haskell infixl 4 "==")
haftmann@34899
   102
  (Scala infixl 5 "==")
haftmann@37958
   103
  (Eval infixl 6 "=")
haftmann@24999
   104
haftmann@24999
   105
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   106
  (SML "IntInf.<= ((_), (_))")
haftmann@24999
   107
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   108
  (Haskell infix 4 "<=")
haftmann@34899
   109
  (Scala infixl 4 "<=")
haftmann@37958
   110
  (Eval infixl 6 "<=")
haftmann@24999
   111
haftmann@24999
   112
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   113
  (SML "IntInf.< ((_), (_))")
haftmann@24999
   114
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   115
  (Haskell infix 4 "<")
haftmann@37958
   116
  (Scala infixl 4 "<")
haftmann@37958
   117
  (Eval infixl 6 "<")
haftmann@24999
   118
haftmann@31205
   119
code_const Code_Numeral.int_of
haftmann@31192
   120
  (SML "IntInf.fromInt")
haftmann@31377
   121
  (OCaml "_")
haftmann@37958
   122
  (Haskell "toInteger")
haftmann@37958
   123
  (Scala "!_.as'_BigInt")
haftmann@37958
   124
  (Eval "_")
haftmann@24999
   125
haftmann@28228
   126
text {* Evaluation *}
haftmann@28228
   127
haftmann@32657
   128
code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
haftmann@31192
   129
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
haftmann@28228
   130
haftmann@24999
   131
end