src/HOL/Tools/numeral_syntax.ML
author kleing
Tue Jul 11 00:43:54 2006 +0200 (2006-07-11)
changeset 20067 26bac504ef90
parent 19481 a6205c6203ea
child 20094 99f27df2b6d0
permissions -rw-r--r--
hex and binary numerals (contributed by Rafal Kolanski)
     1 (*  Title:      HOL/Tools/numeral_syntax.ML
     2     ID:         $Id$
     3     Authors:    Markus Wenzel, TU Muenchen
     4 
     5 Concrete syntax for generic numerals.  Assumes consts and syntax of
     6 theory HOL/Numeral.
     7 *)
     8 
     9 signature NUMERAL_SYNTAX =
    10 sig
    11   val setup: theory -> theory
    12 end;
    13 
    14 structure NumeralSyntax: NUMERAL_SYNTAX =
    15 struct
    16 
    17 
    18 (* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    19 
    20 fun prefix_len _ [] = 0
    21   | prefix_len pred (x :: xs) =
    22       if pred x then 1 + prefix_len pred xs else 0;
    23 
    24 fun dest_bin_str tm =
    25   let
    26     val rev_digs = HOLogic.bin_of tm handle TERM _ => raise Match
    27     val (sign, zs) =
    28       (case rev rev_digs of
    29         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    30       | bs => ("", prefix_len (equal 0) bs));
    31     val i = HOLogic.int_of rev_digs;
    32     val num = IntInf.toString (IntInf.abs i);
    33   in
    34     if i = IntInf.fromInt 0 orelse i = IntInf.fromInt 1 then raise Match
    35     else sign ^ implode (replicate zs "0") ^ num
    36   end;
    37 
    38 (* translation of integer numeral tokens to and from bitstrings *)
    39 
    40 (*additional translations of binary and hex numbers to normal numbers*)
    41 local
    42 
    43 (*get A => ord"0" + 10, etc*)
    44 fun remap_hex c =
    45   let 
    46     val zero = ord"0"; 
    47     val a  = ord"a";
    48     val ca = ord"A";
    49     val x  = ord c;
    50   in
    51     if x >= a then chr (x - a + zero + 10)
    52     else if x >= ca then chr (x - ca + zero + 10) 
    53     else c
    54   end;
    55 
    56 fun str_to_int' ("0" :: "x" :: ds) = read_radixint (16, map remap_hex ds) |> #1
    57   | str_to_int' ("0" :: "b" :: ds) = read_radixint (2, ds) |> #1
    58   | str_to_int' ds = implode ds |> IntInf.fromString |> valOf; 
    59 
    60 in
    61 
    62 val str_to_int = str_to_int' o explode;
    63 
    64 end;
    65 
    66 fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
    67       (Syntax.const "Numeral.number_of" $ (HOLogic.mk_binum (str_to_int str)))
    68   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    69 
    70 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    71       let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
    72         if not (! show_types) andalso can Term.dest_Type T then t'
    73         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    74       end
    75   | numeral_tr' _ (*"number_of"*) T (t :: ts) =
    76       if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
    77       else raise Match
    78   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    79 
    80 
    81 (* theory setup *)
    82 
    83 val setup =
    84   Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []) #>
    85   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')];
    86 
    87 end;