src/HOL/Library/Code_Integer.thy
author haftmann
Mon, 26 Jul 2010 11:10:35 +0200
changeset 38205 52fdcb76c0af
parent 38195 9728342bcd56
child 38299 9102e859dc0b
permissions -rw-r--r--
added Code_Natural.thy
     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 
    23 code_instance int :: eq
    24   (Haskell -)
    25 
    26 setup {*
    27   fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
    28     true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
    29 *}
    30 
    31 code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
    32   (SML "raise/ Fail/ \"Pls\""
    33      and "raise/ Fail/ \"Min\""
    34      and "!((_);/ raise/ Fail/ \"Bit0\")"
    35      and "!((_);/ raise/ Fail/ \"Bit1\")")
    36   (OCaml "failwith/ \"Pls\""
    37      and "failwith/ \"Min\""
    38      and "!((_);/ failwith/ \"Bit0\")"
    39      and "!((_);/ failwith/ \"Bit1\")")
    40   (Haskell "error/ \"Pls\""
    41      and "error/ \"Min\""
    42      and "error/ \"Bit0\""
    43      and "error/ \"Bit1\"")
    44   (Scala "!error(\"Pls\")"
    45      and "!error(\"Min\")"
    46      and "!error(\"Bit0\")"
    47      and "!error(\"Bit1\")")
    48 
    49 code_const Int.pred
    50   (SML "IntInf.- ((_), 1)")
    51   (OCaml "Big'_int.pred'_big'_int")
    52   (Haskell "!(_/ -/ 1)")
    53   (Scala "!(_/ -/ 1)")
    54   (Eval "!(_/ -/ 1)")
    55 
    56 code_const Int.succ
    57   (SML "IntInf.+ ((_), 1)")
    58   (OCaml "Big'_int.succ'_big'_int")
    59   (Haskell "!(_/ +/ 1)")
    60   (Scala "!(_/ +/ 1)")
    61   (Eval "!(_/ +/ 1)")
    62 
    63 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    64   (SML "IntInf.+ ((_), (_))")
    65   (OCaml "Big'_int.add'_big'_int")
    66   (Haskell infixl 6 "+")
    67   (Scala infixl 7 "+")
    68   (Eval infixl 8 "+")
    69 
    70 code_const "uminus \<Colon> int \<Rightarrow> int"
    71   (SML "IntInf.~")
    72   (OCaml "Big'_int.minus'_big'_int")
    73   (Haskell "negate")
    74   (Scala "!(- _)")
    75   (Eval "~/ _")
    76 
    77 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    78   (SML "IntInf.- ((_), (_))")
    79   (OCaml "Big'_int.sub'_big'_int")
    80   (Haskell infixl 6 "-")
    81   (Scala infixl 7 "-")
    82   (Eval infixl 8 "-")
    83 
    84 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    85   (SML "IntInf.* ((_), (_))")
    86   (OCaml "Big'_int.mult'_big'_int")
    87   (Haskell infixl 7 "*")
    88   (Scala infixl 8 "*")
    89   (Eval infixl 9 "*")
    90 
    91 code_const pdivmod
    92   (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
    93   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
    94   (Haskell "divMod/ (abs _)/ (abs _)")
    95   (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
    96   (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
    97 
    98 code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
    99   (SML "!((_ : IntInf.int) = _)")
   100   (OCaml "Big'_int.eq'_big'_int")
   101   (Haskell infixl 4 "==")
   102   (Scala infixl 5 "==")
   103   (Eval infixl 6 "=")
   104 
   105 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   106   (SML "IntInf.<= ((_), (_))")
   107   (OCaml "Big'_int.le'_big'_int")
   108   (Haskell infix 4 "<=")
   109   (Scala infixl 4 "<=")
   110   (Eval infixl 6 "<=")
   111 
   112 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   113   (SML "IntInf.< ((_), (_))")
   114   (OCaml "Big'_int.lt'_big'_int")
   115   (Haskell infix 4 "<")
   116   (Scala infixl 4 "<")
   117   (Eval infixl 6 "<")
   118 
   119 code_const Code_Numeral.int_of
   120   (SML "IntInf.fromInt")
   121   (OCaml "_")
   122   (Haskell "toInteger")
   123   (Scala "!_.as'_BigInt")
   124   (Eval "_")
   125 
   126 text {* Evaluation *}
   127 
   128 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
   129   (Eval "HOLogic.mk'_number/ HOLogic.intT")
   130 
   131 end