src/HOL/Library/Code_Integer.thy
author haftmann
Tue, 23 Jun 2009 14:24:58 +0200
changeset 31776 151c3f5f28f9
parent 31377 a48f9ef9de15
child 32657 5f13912245ff
permissions -rw-r--r--
simplified proof
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
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31192
diff changeset
     8
imports Main
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")
haftmann
parents:
diff changeset
    21
haftmann
parents:
diff changeset
    22
code_instance int :: eq
haftmann
parents:
diff changeset
    23
  (Haskell -)
haftmann
parents:
diff changeset
    24
haftmann
parents:
diff changeset
    25
setup {*
25928
042e877d9841 tuned code setup
haftmann
parents: 25919
diff changeset
    26
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
042e877d9841 tuned code setup
haftmann
parents: 25919
diff changeset
    27
    true true) ["SML", "OCaml", "Haskell"]
24999
haftmann
parents:
diff changeset
    28
*}
haftmann
parents:
diff changeset
    29
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    30
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
24999
haftmann
parents:
diff changeset
    31
  (SML "raise/ Fail/ \"Pls\""
haftmann
parents:
diff changeset
    32
     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
    33
     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
    34
     and "!((_);/ raise/ Fail/ \"Bit1\")")
24999
haftmann
parents:
diff changeset
    35
  (OCaml "failwith/ \"Pls\""
haftmann
parents:
diff changeset
    36
     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
    37
     and "!((_);/ failwith/ \"Bit0\")"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    38
     and "!((_);/ failwith/ \"Bit1\")")
24999
haftmann
parents:
diff changeset
    39
  (Haskell "error/ \"Pls\""
haftmann
parents:
diff changeset
    40
     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
    41
     and "error/ \"Bit0\""
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26009
diff changeset
    42
     and "error/ \"Bit1\"")
24999
haftmann
parents:
diff changeset
    43
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25767
diff changeset
    44
code_const Int.pred
24999
haftmann
parents:
diff changeset
    45
  (SML "IntInf.- ((_), 1)")
haftmann
parents:
diff changeset
    46
  (OCaml "Big'_int.pred'_big'_int")
haftmann
parents:
diff changeset
    47
  (Haskell "!(_/ -/ 1)")
haftmann
parents:
diff changeset
    48
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25767
diff changeset
    49
code_const Int.succ
24999
haftmann
parents:
diff changeset
    50
  (SML "IntInf.+ ((_), 1)")
haftmann
parents:
diff changeset
    51
  (OCaml "Big'_int.succ'_big'_int")
haftmann
parents:
diff changeset
    52
  (Haskell "!(_/ +/ 1)")
haftmann
parents:
diff changeset
    53
haftmann
parents:
diff changeset
    54
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    55
  (SML "IntInf.+ ((_), (_))")
haftmann
parents:
diff changeset
    56
  (OCaml "Big'_int.add'_big'_int")
haftmann
parents:
diff changeset
    57
  (Haskell infixl 6 "+")
haftmann
parents:
diff changeset
    58
haftmann
parents:
diff changeset
    59
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    60
  (SML "IntInf.~")
haftmann
parents:
diff changeset
    61
  (OCaml "Big'_int.minus'_big'_int")
haftmann
parents:
diff changeset
    62
  (Haskell "negate")
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.sub'_big'_int")
haftmann
parents:
diff changeset
    67
  (Haskell infixl 6 "-")
haftmann
parents:
diff changeset
    68
haftmann
parents:
diff changeset
    69
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann
parents:
diff changeset
    70
  (SML "IntInf.* ((_), (_))")
haftmann
parents:
diff changeset
    71
  (OCaml "Big'_int.mult'_big'_int")
haftmann
parents:
diff changeset
    72
  (Haskell infixl 7 "*")
haftmann
parents:
diff changeset
    73
29936
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    74
code_const pdivmod
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    75
  (SML "(fn k => fn l =>/ IntInf.divMod/ (IntInf.abs k,/ IntInf.abs l))")
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    76
  (OCaml "(fun k -> fun l ->/ Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int k)/ (Big'_int.abs'_big'_int l))")
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    77
  (Haskell "(\\k l ->/ divMod/ (abs k)/ (abs l))")
d3dfb67f0f59 added pdivmod on int (for code generation)
haftmann
parents: 28562
diff changeset
    78
28346
b8390cd56b8f discontinued special treatment of op = vs. eq_class.eq
haftmann
parents: 28228
diff changeset
    79
code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
24999
haftmann
parents:
diff changeset
    80
  (SML "!((_ : IntInf.int) = _)")
haftmann
parents:
diff changeset
    81
  (OCaml "Big'_int.eq'_big'_int")
haftmann
parents:
diff changeset
    82
  (Haskell infixl 4 "==")
haftmann
parents:
diff changeset
    83
haftmann
parents:
diff changeset
    84
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann
parents:
diff changeset
    85
  (SML "IntInf.<= ((_), (_))")
haftmann
parents:
diff changeset
    86
  (OCaml "Big'_int.le'_big'_int")
haftmann
parents:
diff changeset
    87
  (Haskell infix 4 "<=")
haftmann
parents:
diff changeset
    88
haftmann
parents:
diff changeset
    89
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann
parents:
diff changeset
    90
  (SML "IntInf.< ((_), (_))")
haftmann
parents:
diff changeset
    91
  (OCaml "Big'_int.lt'_big'_int")
haftmann
parents:
diff changeset
    92
  (Haskell infix 4 "<")
haftmann
parents:
diff changeset
    93
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    94
code_const Code_Numeral.int_of
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
    95
  (SML "IntInf.fromInt")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31205
diff changeset
    96
  (OCaml "_")
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
    97
  (Haskell "toEnum")
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
    98
24999
haftmann
parents:
diff changeset
    99
code_reserved SML IntInf
haftmann
parents:
diff changeset
   100
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   101
text {* Evaluation *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   102
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   103
code_const "Code_Eval.term_of \<Colon> int \<Rightarrow> term"
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 30663
diff changeset
   104
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 27487
diff changeset
   105
24999
haftmann
parents:
diff changeset
   106
end