src/HOL/Library/Code_Integer.thy
author haftmann
Wed Jul 28 12:12:29 2010 +0200 (2010-07-28)
changeset 38053 9102e859dc0b
parent 37968 52fdcb76c0af
child 38773 f9837065b5e8
permissions -rw-r--r--
may use `int` in Isabelle runtime environment
     1 (*  Title:      HOL/Library/Code_Integer.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Pretty integer literals for code generation *}
     6 
     7 theory Code_Integer
     8 imports Main Code_Natural
     9 begin
    10 
    11 text {*
    12   HOL numeral expressions are mapped to integer literals
    13   in target languages, using predefined target language
    14   operations for abstract integer operations.
    15 *}
    16 
    17 code_type int
    18   (SML "IntInf.int")
    19   (OCaml "Big'_int.big'_int")
    20   (Haskell "Integer")
    21   (Scala "BigInt")
    22   (Eval "int")
    23 
    24 code_instance int :: eq
    25   (Haskell -)
    26 
    27 setup {*
    28   fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
    29     true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
    30 *}
    31 
    32 code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
    33   (SML "raise/ Fail/ \"Pls\""
    34      and "raise/ Fail/ \"Min\""
    35      and "!((_);/ raise/ Fail/ \"Bit0\")"
    36      and "!((_);/ raise/ Fail/ \"Bit1\")")
    37   (OCaml "failwith/ \"Pls\""
    38      and "failwith/ \"Min\""
    39      and "!((_);/ failwith/ \"Bit0\")"
    40      and "!((_);/ failwith/ \"Bit1\")")
    41   (Haskell "error/ \"Pls\""
    42      and "error/ \"Min\""
    43      and "error/ \"Bit0\""
    44      and "error/ \"Bit1\"")
    45   (Scala "!error(\"Pls\")"
    46      and "!error(\"Min\")"
    47      and "!error(\"Bit0\")"
    48      and "!error(\"Bit1\")")
    49 
    50 code_const Int.pred
    51   (SML "IntInf.- ((_), 1)")
    52   (OCaml "Big'_int.pred'_big'_int")
    53   (Haskell "!(_/ -/ 1)")
    54   (Scala "!(_/ -/ 1)")
    55   (Eval "!(_/ -/ 1)")
    56 
    57 code_const Int.succ
    58   (SML "IntInf.+ ((_), 1)")
    59   (OCaml "Big'_int.succ'_big'_int")
    60   (Haskell "!(_/ +/ 1)")
    61   (Scala "!(_/ +/ 1)")
    62   (Eval "!(_/ +/ 1)")
    63 
    64 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    65   (SML "IntInf.+ ((_), (_))")
    66   (OCaml "Big'_int.add'_big'_int")
    67   (Haskell infixl 6 "+")
    68   (Scala infixl 7 "+")
    69   (Eval infixl 8 "+")
    70 
    71 code_const "uminus \<Colon> int \<Rightarrow> int"
    72   (SML "IntInf.~")
    73   (OCaml "Big'_int.minus'_big'_int")
    74   (Haskell "negate")
    75   (Scala "!(- _)")
    76   (Eval "~/ _")
    77 
    78 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    79   (SML "IntInf.- ((_), (_))")
    80   (OCaml "Big'_int.sub'_big'_int")
    81   (Haskell infixl 6 "-")
    82   (Scala infixl 7 "-")
    83   (Eval infixl 8 "-")
    84 
    85 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    86   (SML "IntInf.* ((_), (_))")
    87   (OCaml "Big'_int.mult'_big'_int")
    88   (Haskell infixl 7 "*")
    89   (Scala infixl 8 "*")
    90   (Eval infixl 9 "*")
    91 
    92 code_const pdivmod
    93   (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
    94   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
    95   (Haskell "divMod/ (abs _)/ (abs _)")
    96   (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
    97   (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
    98 
    99 code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   100   (SML "!((_ : IntInf.int) = _)")
   101   (OCaml "Big'_int.eq'_big'_int")
   102   (Haskell infixl 4 "==")
   103   (Scala infixl 5 "==")
   104   (Eval infixl 6 "=")
   105 
   106 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   107   (SML "IntInf.<= ((_), (_))")
   108   (OCaml "Big'_int.le'_big'_int")
   109   (Haskell infix 4 "<=")
   110   (Scala infixl 4 "<=")
   111   (Eval infixl 6 "<=")
   112 
   113 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   114   (SML "IntInf.< ((_), (_))")
   115   (OCaml "Big'_int.lt'_big'_int")
   116   (Haskell infix 4 "<")
   117   (Scala infixl 4 "<")
   118   (Eval infixl 6 "<")
   119 
   120 code_const Code_Numeral.int_of
   121   (SML "IntInf.fromInt")
   122   (OCaml "_")
   123   (Haskell "toInteger")
   124   (Scala "!_.as'_BigInt")
   125   (Eval "_")
   126 
   127 text {* Evaluation *}
   128 
   129 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
   130   (Eval "HOLogic.mk'_number/ HOLogic.intT")
   131 
   132 end