src/HOL/Library/Code_Integer.thy
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 39272 0b61951d2682
child 47108 2a1953f0d20d
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
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@38053
    22
  (Eval "int")
haftmann@24999
    23
haftmann@38857
    24
code_instance int :: equal
haftmann@24999
    25
  (Haskell -)
haftmann@24999
    26
haftmann@24999
    27
setup {*
haftmann@25928
    28
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
haftmann@34944
    29
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
haftmann@24999
    30
*}
haftmann@24999
    31
huffman@26086
    32
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
haftmann@24999
    33
  (SML "raise/ Fail/ \"Pls\""
haftmann@24999
    34
     and "raise/ Fail/ \"Min\""
huffman@26086
    35
     and "!((_);/ raise/ Fail/ \"Bit0\")"
huffman@26086
    36
     and "!((_);/ raise/ Fail/ \"Bit1\")")
haftmann@24999
    37
  (OCaml "failwith/ \"Pls\""
haftmann@24999
    38
     and "failwith/ \"Min\""
huffman@26086
    39
     and "!((_);/ failwith/ \"Bit0\")"
huffman@26086
    40
     and "!((_);/ failwith/ \"Bit1\")")
haftmann@24999
    41
  (Haskell "error/ \"Pls\""
haftmann@24999
    42
     and "error/ \"Min\""
huffman@26086
    43
     and "error/ \"Bit0\""
huffman@26086
    44
     and "error/ \"Bit1\"")
haftmann@34899
    45
  (Scala "!error(\"Pls\")"
haftmann@34899
    46
     and "!error(\"Min\")"
haftmann@34899
    47
     and "!error(\"Bit0\")"
haftmann@34899
    48
     and "!error(\"Bit1\")")
haftmann@34899
    49
haftmann@25919
    50
code_const Int.pred
haftmann@24999
    51
  (SML "IntInf.- ((_), 1)")
haftmann@24999
    52
  (OCaml "Big'_int.pred'_big'_int")
haftmann@24999
    53
  (Haskell "!(_/ -/ 1)")
haftmann@38773
    54
  (Scala "!(_ -/ 1)")
haftmann@37958
    55
  (Eval "!(_/ -/ 1)")
haftmann@24999
    56
haftmann@25919
    57
code_const Int.succ
haftmann@24999
    58
  (SML "IntInf.+ ((_), 1)")
haftmann@24999
    59
  (OCaml "Big'_int.succ'_big'_int")
haftmann@24999
    60
  (Haskell "!(_/ +/ 1)")
haftmann@38773
    61
  (Scala "!(_ +/ 1)")
haftmann@37958
    62
  (Eval "!(_/ +/ 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@37958
    69
  (Eval infixl 8 "+")
haftmann@24999
    70
haftmann@24999
    71
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann@24999
    72
  (SML "IntInf.~")
haftmann@24999
    73
  (OCaml "Big'_int.minus'_big'_int")
haftmann@24999
    74
  (Haskell "negate")
haftmann@34899
    75
  (Scala "!(- _)")
haftmann@37958
    76
  (Eval "~/ _")
haftmann@24999
    77
haftmann@24999
    78
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    79
  (SML "IntInf.- ((_), (_))")
haftmann@24999
    80
  (OCaml "Big'_int.sub'_big'_int")
haftmann@24999
    81
  (Haskell infixl 6 "-")
haftmann@34899
    82
  (Scala infixl 7 "-")
haftmann@37958
    83
  (Eval infixl 8 "-")
haftmann@24999
    84
haftmann@24999
    85
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    86
  (SML "IntInf.* ((_), (_))")
haftmann@24999
    87
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
    88
  (Haskell infixl 7 "*")
haftmann@34899
    89
  (Scala infixl 8 "*")
haftmann@37958
    90
  (Eval infixl 9 "*")
haftmann@24999
    91
haftmann@29936
    92
code_const pdivmod
haftmann@34899
    93
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
haftmann@34899
    94
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@34944
    95
  (Haskell "divMod/ (abs _)/ (abs _)")
haftmann@37958
    96
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@37958
    97
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
haftmann@29936
    98
haftmann@38857
    99
code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   100
  (SML "!((_ : IntInf.int) = _)")
haftmann@24999
   101
  (OCaml "Big'_int.eq'_big'_int")
haftmann@39272
   102
  (Haskell infix 4 "==")
haftmann@34899
   103
  (Scala infixl 5 "==")
haftmann@37958
   104
  (Eval infixl 6 "=")
haftmann@24999
   105
haftmann@24999
   106
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   107
  (SML "IntInf.<= ((_), (_))")
haftmann@24999
   108
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   109
  (Haskell infix 4 "<=")
haftmann@34899
   110
  (Scala infixl 4 "<=")
haftmann@37958
   111
  (Eval infixl 6 "<=")
haftmann@24999
   112
haftmann@24999
   113
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   114
  (SML "IntInf.< ((_), (_))")
haftmann@24999
   115
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   116
  (Haskell infix 4 "<")
haftmann@37958
   117
  (Scala infixl 4 "<")
haftmann@37958
   118
  (Eval infixl 6 "<")
haftmann@24999
   119
haftmann@31205
   120
code_const Code_Numeral.int_of
haftmann@31192
   121
  (SML "IntInf.fromInt")
haftmann@31377
   122
  (OCaml "_")
haftmann@37958
   123
  (Haskell "toInteger")
haftmann@37958
   124
  (Scala "!_.as'_BigInt")
haftmann@37958
   125
  (Eval "_")
haftmann@24999
   126
haftmann@28228
   127
text {* Evaluation *}
haftmann@28228
   128
haftmann@32657
   129
code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
haftmann@31192
   130
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
haftmann@28228
   131
haftmann@24999
   132
end