src/HOL/Library/Code_Integer.thy
author haftmann
Fri Jan 22 13:38:28 2010 +0100 (2010-01-22)
changeset 34944 970e1466028d
parent 34899 8674bb6f727b
child 37947 844977c7abeb
permissions -rw-r--r--
code literals: distinguish numeral classes by different entries
     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
     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   #> Numeral.add_code @{const_name number_int_inst.number_of_int}
    30     true Code_Printer.literal_numeral "Scala"
    31 *}
    32 
    33 code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
    34   (SML "raise/ Fail/ \"Pls\""
    35      and "raise/ Fail/ \"Min\""
    36      and "!((_);/ raise/ Fail/ \"Bit0\")"
    37      and "!((_);/ raise/ Fail/ \"Bit1\")")
    38   (OCaml "failwith/ \"Pls\""
    39      and "failwith/ \"Min\""
    40      and "!((_);/ failwith/ \"Bit0\")"
    41      and "!((_);/ failwith/ \"Bit1\")")
    42   (Haskell "error/ \"Pls\""
    43      and "error/ \"Min\""
    44      and "error/ \"Bit0\""
    45      and "error/ \"Bit1\"")
    46   (Scala "!error(\"Pls\")"
    47      and "!error(\"Min\")"
    48      and "!error(\"Bit0\")"
    49      and "!error(\"Bit1\")")
    50 
    51 
    52 code_const Int.pred
    53   (SML "IntInf.- ((_), 1)")
    54   (OCaml "Big'_int.pred'_big'_int")
    55   (Haskell "!(_/ -/ 1)")
    56   (Scala "!(_/ -/ 1)")
    57 
    58 code_const Int.succ
    59   (SML "IntInf.+ ((_), 1)")
    60   (OCaml "Big'_int.succ'_big'_int")
    61   (Haskell "!(_/ +/ 1)")
    62   (Scala "!(_/ +/ 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 
    70 code_const "uminus \<Colon> int \<Rightarrow> int"
    71   (SML "IntInf.~")
    72   (OCaml "Big'_int.minus'_big'_int")
    73   (Haskell "negate")
    74   (Scala "!(- _)")
    75 
    76 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    77   (SML "IntInf.- ((_), (_))")
    78   (OCaml "Big'_int.sub'_big'_int")
    79   (Haskell infixl 6 "-")
    80   (Scala infixl 7 "-")
    81 
    82 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    83   (SML "IntInf.* ((_), (_))")
    84   (OCaml "Big'_int.mult'_big'_int")
    85   (Haskell infixl 7 "*")
    86   (Scala infixl 8 "*")
    87 
    88 code_const pdivmod
    89   (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
    90   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
    91   (Haskell "divMod/ (abs _)/ (abs _)")
    92   (Scala "!(_.abs '/% _.abs)")
    93 
    94 code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
    95   (SML "!((_ : IntInf.int) = _)")
    96   (OCaml "Big'_int.eq'_big'_int")
    97   (Haskell infixl 4 "==")
    98   (Scala infixl 5 "==")
    99 
   100 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   101   (SML "IntInf.<= ((_), (_))")
   102   (OCaml "Big'_int.le'_big'_int")
   103   (Haskell infix 4 "<=")
   104   (Scala infixl 4 "<=")
   105 
   106 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   107   (SML "IntInf.< ((_), (_))")
   108   (OCaml "Big'_int.lt'_big'_int")
   109   (Haskell infix 4 "<")
   110   (Scala infixl 4 "<=")
   111 
   112 code_const Code_Numeral.int_of
   113   (SML "IntInf.fromInt")
   114   (OCaml "_")
   115   (Haskell "toEnum")
   116   (Scala "!BigInt((_))")
   117 
   118 text {* Evaluation *}
   119 
   120 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
   121   (Eval "HOLogic.mk'_number/ HOLogic.intT")
   122 
   123 end