src/HOL/Tools/numeral.ML
changeset 31056 01ac77eb660b
parent 28708 a1a436f09ec6
child 31375 815426e7dd3b
equal deleted inserted replaced
31055:2cf6efca6c71 31056:01ac77eb660b
     1 (*  Title:      HOL/Tools/numeral.ML
     1 (*  Title:      HOL/Tools/numeral.ML
     2     ID:         $Id$
       
     3     Author:     Makarius
     2     Author:     Makarius
     4 
     3 
     5 Logical operations on numerals (see also HOL/hologic.ML).
     4 Logical operations on numerals (see also HOL/hologic.ML).
     6 *)
     5 *)
     7 
     6 
    57 
    56 
    58 local open Basic_Code_Thingol in
    57 local open Basic_Code_Thingol in
    59 
    58 
    60 fun add_code number_of negative unbounded target thy =
    59 fun add_code number_of negative unbounded target thy =
    61   let
    60   let
    62     val pr_numeral = (Code_Printer.literal_numeral o Code_Target.the_literals thy) target;
    61     fun dest_numeral pls' min' bit0' bit1' thm =
    63     fun dest_numeral naming thm =
       
    64       let
    62       let
    65         val SOME pls' = Code_Thingol.lookup_const naming @{const_name Int.Pls};
       
    66         val SOME min' = Code_Thingol.lookup_const naming @{const_name Int.Min};
       
    67         val SOME bit0' = Code_Thingol.lookup_const naming @{const_name Int.Bit0};
       
    68         val SOME bit1' = Code_Thingol.lookup_const naming @{const_name Int.Bit1};
       
    69         fun dest_bit (IConst (c, _)) = if c = bit0' then 0
    63         fun dest_bit (IConst (c, _)) = if c = bit0' then 0
    70               else if c = bit1' then 1
    64               else if c = bit1' then 1
    71               else Code_Printer.nerror thm "Illegal numeral expression: illegal bit"
    65               else Code_Printer.nerror thm "Illegal numeral expression: illegal bit"
    72           | dest_bit _ = Code_Printer.nerror thm "Illegal numeral expression: illegal bit";
    66           | dest_bit _ = Code_Printer.nerror thm "Illegal numeral expression: illegal bit";
    73         fun dest_num (IConst (c, _)) = if c = pls' then SOME 0
    67         fun dest_num (IConst (c, _)) = if c = pls' then SOME 0
    77           | dest_num (t1 `$ t2) =
    71           | dest_num (t1 `$ t2) =
    78               let val (n, b) = (dest_num t2, dest_bit t1)
    72               let val (n, b) = (dest_num t2, dest_bit t1)
    79               in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
    73               in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
    80           | dest_num _ = Code_Printer.nerror thm "Illegal numeral expression: illegal term";
    74           | dest_num _ = Code_Printer.nerror thm "Illegal numeral expression: illegal term";
    81       in dest_num end;
    75       in dest_num end;
    82     fun pretty _ naming thm _ _ [(t, _)] =
    76     fun pretty literals [pls', min', bit0', bit1'] _ thm _ _ [(t, _)] =
    83       (Code_Printer.str o pr_numeral unbounded o the_default 0 o dest_numeral naming thm) t;
    77       (Code_Printer.str o Code_Printer.literal_numeral literals unbounded
       
    78         o the_default 0 o dest_numeral pls' min' bit0' bit1' thm) t;
    84   in
    79   in
    85     thy
    80     thy |>  Code_Target.add_syntax_const target number_of
    86     |> Code_Target.add_syntax_const target number_of (SOME (1, pretty))
    81       (SOME (1, ([@{const_name Int.Pls}, @{const_name Int.Min}, @{const_name Int.Bit0}, @{const_name Int.Bit1}], pretty)))
    87   end;
    82   end;
    88 
    83 
    89 end; (*local*)
    84 end; (*local*)
    90 
    85 
    91 end;
    86 end;