src/HOL/Library/Code_Integer.thy
author haftmann
Tue May 19 16:54:55 2009 +0200 (2009-05-19)
changeset 31205 98370b26c2ce
parent 31203 5c8fb4fd67e0
child 31377 a48f9ef9de15
permissions -rw-r--r--
String.literal replaces message_string, code_numeral replaces (code_)index
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@24999
    21
haftmann@24999
    22
code_instance int :: eq
haftmann@24999
    23
  (Haskell -)
haftmann@24999
    24
haftmann@24999
    25
setup {*
haftmann@25928
    26
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
haftmann@25928
    27
    true true) ["SML", "OCaml", "Haskell"]
haftmann@24999
    28
*}
haftmann@24999
    29
huffman@26086
    30
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
haftmann@24999
    31
  (SML "raise/ Fail/ \"Pls\""
haftmann@24999
    32
     and "raise/ Fail/ \"Min\""
huffman@26086
    33
     and "!((_);/ raise/ Fail/ \"Bit0\")"
huffman@26086
    34
     and "!((_);/ raise/ Fail/ \"Bit1\")")
haftmann@24999
    35
  (OCaml "failwith/ \"Pls\""
haftmann@24999
    36
     and "failwith/ \"Min\""
huffman@26086
    37
     and "!((_);/ failwith/ \"Bit0\")"
huffman@26086
    38
     and "!((_);/ failwith/ \"Bit1\")")
haftmann@24999
    39
  (Haskell "error/ \"Pls\""
haftmann@24999
    40
     and "error/ \"Min\""
huffman@26086
    41
     and "error/ \"Bit0\""
huffman@26086
    42
     and "error/ \"Bit1\"")
haftmann@24999
    43
haftmann@25919
    44
code_const Int.pred
haftmann@24999
    45
  (SML "IntInf.- ((_), 1)")
haftmann@24999
    46
  (OCaml "Big'_int.pred'_big'_int")
haftmann@24999
    47
  (Haskell "!(_/ -/ 1)")
haftmann@24999
    48
haftmann@25919
    49
code_const Int.succ
haftmann@24999
    50
  (SML "IntInf.+ ((_), 1)")
haftmann@24999
    51
  (OCaml "Big'_int.succ'_big'_int")
haftmann@24999
    52
  (Haskell "!(_/ +/ 1)")
haftmann@24999
    53
haftmann@24999
    54
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    55
  (SML "IntInf.+ ((_), (_))")
haftmann@24999
    56
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
    57
  (Haskell infixl 6 "+")
haftmann@24999
    58
haftmann@24999
    59
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann@24999
    60
  (SML "IntInf.~")
haftmann@24999
    61
  (OCaml "Big'_int.minus'_big'_int")
haftmann@24999
    62
  (Haskell "negate")
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.sub'_big'_int")
haftmann@24999
    67
  (Haskell infixl 6 "-")
haftmann@24999
    68
haftmann@24999
    69
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
    70
  (SML "IntInf.* ((_), (_))")
haftmann@24999
    71
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
    72
  (Haskell infixl 7 "*")
haftmann@24999
    73
haftmann@29936
    74
code_const pdivmod
haftmann@29936
    75
  (SML "(fn k => fn l =>/ IntInf.divMod/ (IntInf.abs k,/ IntInf.abs l))")
haftmann@29936
    76
  (OCaml "(fun k -> fun l ->/ Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int k)/ (Big'_int.abs'_big'_int l))")
haftmann@29936
    77
  (Haskell "(\\k l ->/ divMod/ (abs k)/ (abs l))")
haftmann@29936
    78
haftmann@28346
    79
code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
    80
  (SML "!((_ : IntInf.int) = _)")
haftmann@24999
    81
  (OCaml "Big'_int.eq'_big'_int")
haftmann@24999
    82
  (Haskell infixl 4 "==")
haftmann@24999
    83
haftmann@24999
    84
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
    85
  (SML "IntInf.<= ((_), (_))")
haftmann@24999
    86
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
    87
  (Haskell infix 4 "<=")
haftmann@24999
    88
haftmann@24999
    89
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
    90
  (SML "IntInf.< ((_), (_))")
haftmann@24999
    91
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
    92
  (Haskell infix 4 "<")
haftmann@24999
    93
haftmann@31205
    94
code_const Code_Numeral.int_of
haftmann@31192
    95
  (SML "IntInf.fromInt")
haftmann@31192
    96
  (OCaml "Big'_int.big'_int'_of'_int")
haftmann@31192
    97
  (Haskell "toEnum")
haftmann@31192
    98
haftmann@24999
    99
code_reserved SML IntInf
haftmann@24999
   100
code_reserved OCaml Big_int
haftmann@24999
   101
haftmann@28228
   102
text {* Evaluation *}
haftmann@28228
   103
haftmann@28228
   104
code_const "Code_Eval.term_of \<Colon> int \<Rightarrow> term"
haftmann@31192
   105
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
haftmann@28228
   106
haftmann@24999
   107
end