src/HOL/Library/Code_Integer.thy
author haftmann
Fri, 27 Aug 2010 19:34:23 +0200
changeset 38857 97775f3e8722
parent 38773 f9837065b5e8
child 39272 0b61951d2682
permissions -rw-r--r--
renamed class/constant eq to equal; tuned some instantiations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24999
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Code_Integer.thy
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann
parents:
diff changeset
     3
*)
haftmann
parents:
diff changeset
     4
haftmann
parents:
diff changeset
     5
header {* Pretty integer literals for code generation *}
haftmann
parents:
diff changeset
     6
haftmann
parents:
diff changeset
     7
theory Code_Integer
37968
52fdcb76c0af added Code_Natural.thy
haftmann
parents: 37958
diff changeset
     8
imports Main Code_Natural
24999
haftmann
parents:
diff changeset
     9
begin
haftmann
parents:
diff changeset
    10
haftmann
parents:
diff changeset
    11
text {*
haftmann
parents:
diff changeset
    12
  HOL numeral expressions are mapped to integer literals
haftmann
parents:
diff changeset
    13
  in target languages, using predefined target language
haftmann
parents:
diff changeset
    14
  operations for abstract integer operations.
haftmann
parents:
diff changeset
    15
*}
haftmann
parents:
diff changeset
    16
haftmann
parents:
diff changeset
    17
code_type int
haftmann
parents:
diff changeset
    18
  (SML "IntInf.int")
haftmann
parents:
diff changeset
    19
  (OCaml "Big'_int.big'_int")
haftmann
parents:
diff changeset
    20
  (Haskell "Integer")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    21
  (Scala "BigInt")
38053
9102e859dc0b may use `int` in Isabelle runtime environment
haftmann
parents: 37968
diff changeset
    22
  (Eval "int")
24999
haftmann
parents:
diff changeset
    23
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38773
diff changeset
    24
code_instance int :: equal
24999
haftmann
parents:
diff changeset
    25
  (Haskell -)
haftmann
parents:
diff changeset
    26
haftmann
parents:
diff changeset
    27
setup {*
25928
042e877d9841 tuned code setup
haftmann
parents: 25919
diff changeset
    28
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34899
diff changeset
    29
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
24999
haftmann
parents:
diff changeset
    30
*}
haftmann
parents:
diff changeset
    31
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    32
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
24999
haftmann
parents:
diff changeset
    33
  (SML "raise/ Fail/ \"Pls\""
haftmann
parents:
diff changeset
    34
     and "raise/ Fail/ \"Min\""
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    35
     and "!((_);/ raise/ Fail/ \"Bit0\")"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    36
     and "!((_);/ raise/ Fail/ \"Bit1\")")
24999
haftmann
parents:
diff changeset
    37
  (OCaml "failwith/ \"Pls\""
haftmann
parents:
diff changeset
    38
     and "failwith/ \"Min\""
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    39
     and "!((_);/ failwith/ \"Bit0\")"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    40
     and "!((_);/ failwith/ \"Bit1\")")
24999
haftmann
parents:
diff changeset
    41
  (Haskell "error/ \"Pls\""
haftmann
parents:
diff changeset
    42
     and "error/ \"Min\""
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    43
     and "error/ \"Bit0\""
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    44
     and "error/ \"Bit1\"")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    45
  (Scala "!error(\"Pls\")"
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    46
     and "!error(\"Min\")"
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    47
     and "!error(\"Bit0\")"
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    48
     and "!error(\"Bit1\")")
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    49
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25767
diff changeset
    50
code_const Int.pred
24999
haftmann
parents:
diff changeset
    51
  (SML "IntInf.- ((_), 1)")
haftmann
parents:
diff changeset
    52
  (OCaml "Big'_int.pred'_big'_int")
haftmann
parents:
diff changeset
    53
  (Haskell "!(_/ -/ 1)")
38773
f9837065b5e8 prevent line breaks after Scala symbolic operators
haftmann
parents: 38053
diff changeset
    54
  (Scala "!(_ -/ 1)")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    55
  (Eval "!(_/ -/ 1)")
24999
haftmann
parents:
diff changeset
    56
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25767
diff changeset
    57
code_const Int.succ
24999
haftmann
parents:
diff changeset
    58
  (SML "IntInf.+ ((_), 1)")
haftmann
parents:
diff changeset
    59
  (OCaml "Big'_int.succ'_big'_int")
haftmann
parents:
diff changeset
    60
  (Haskell "!(_/ +/ 1)")
38773
f9837065b5e8 prevent line breaks after Scala symbolic operators
haftmann
parents: 38053
diff changeset
    61
  (Scala "!(_ +/ 1)")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    62
  (Eval "!(_/ +/ 1)")
24999
haftmann
parents:
diff changeset
    63
haftmann
parents:
diff changeset
    64
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    65
  (SML "IntInf.+ ((_), (_))")
haftmann
parents:
diff changeset
    66
  (OCaml "Big'_int.add'_big'_int")
haftmann
parents:
diff changeset
    67
  (Haskell infixl 6 "+")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    68
  (Scala infixl 7 "+")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    69
  (Eval infixl 8 "+")
24999
haftmann
parents:
diff changeset
    70
haftmann
parents:
diff changeset
    71
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    72
  (SML "IntInf.~")
haftmann
parents:
diff changeset
    73
  (OCaml "Big'_int.minus'_big'_int")
haftmann
parents:
diff changeset
    74
  (Haskell "negate")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    75
  (Scala "!(- _)")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    76
  (Eval "~/ _")
24999
haftmann
parents:
diff changeset
    77
haftmann
parents:
diff changeset
    78
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    79
  (SML "IntInf.- ((_), (_))")
haftmann
parents:
diff changeset
    80
  (OCaml "Big'_int.sub'_big'_int")
haftmann
parents:
diff changeset
    81
  (Haskell infixl 6 "-")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    82
  (Scala infixl 7 "-")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    83
  (Eval infixl 8 "-")
24999
haftmann
parents:
diff changeset
    84
haftmann
parents:
diff changeset
    85
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    86
  (SML "IntInf.* ((_), (_))")
haftmann
parents:
diff changeset
    87
  (OCaml "Big'_int.mult'_big'_int")
haftmann
parents:
diff changeset
    88
  (Haskell infixl 7 "*")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    89
  (Scala infixl 8 "*")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    90
  (Eval infixl 9 "*")
24999
haftmann
parents:
diff changeset
    91
29936
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    92
code_const pdivmod
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    93
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
    94
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34899
diff changeset
    95
  (Haskell "divMod/ (abs _)/ (abs _)")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    96
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
    97
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
29936
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    98
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38773
diff changeset
    99
code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
24999
haftmann
parents:
diff changeset
   100
  (SML "!((_ : IntInf.int) = _)")
haftmann
parents:
diff changeset
   101
  (OCaml "Big'_int.eq'_big'_int")
haftmann
parents:
diff changeset
   102
  (Haskell infixl 4 "==")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
   103
  (Scala infixl 5 "==")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   104
  (Eval infixl 6 "=")
24999
haftmann
parents:
diff changeset
   105
haftmann
parents:
diff changeset
   106
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann
parents:
diff changeset
   107
  (SML "IntInf.<= ((_), (_))")
haftmann
parents:
diff changeset
   108
  (OCaml "Big'_int.le'_big'_int")
haftmann
parents:
diff changeset
   109
  (Haskell infix 4 "<=")
34899
8674bb6f727b added Scala setup
haftmann
parents: 32657
diff changeset
   110
  (Scala infixl 4 "<=")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   111
  (Eval infixl 6 "<=")
24999
haftmann
parents:
diff changeset
   112
haftmann
parents:
diff changeset
   113
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann
parents:
diff changeset
   114
  (SML "IntInf.< ((_), (_))")
haftmann
parents:
diff changeset
   115
  (OCaml "Big'_int.lt'_big'_int")
haftmann
parents:
diff changeset
   116
  (Haskell infix 4 "<")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   117
  (Scala infixl 4 "<")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   118
  (Eval infixl 6 "<")
24999
haftmann
parents:
diff changeset
   119
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   120
code_const Code_Numeral.int_of
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
   121
  (SML "IntInf.fromInt")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31205
diff changeset
   122
  (OCaml "_")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   123
  (Haskell "toInteger")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   124
  (Scala "!_.as'_BigInt")
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   125
  (Eval "_")
24999
haftmann
parents:
diff changeset
   126
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   127
text {* Evaluation *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   128
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 31377
diff changeset
   129
code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
   130
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   131
24999
haftmann
parents:
diff changeset
   132
end