src/HOL/Tools/numeral_syntax.ML
author paulson
Wed Jul 21 15:18:36 1999 +0200 (1999-07-21)
changeset 7055 91764e88d932
parent 6905 9bc05ec3497b
child 7078 4e64b2bd10ce
permissions -rw-r--r--
now exports mk_bin
     1 (*  Title:      HOL/Tools/numeral_syntax.ML
     2     ID:         $Id$
     3     Author:     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 dest_bin : term -> int
    12   val mk_bin   : int -> term
    13   val setup    : (theory -> theory) list
    14 end;
    15 
    16 structure NumeralSyntax: NUMERAL_SYNTAX =
    17 struct
    18 
    19 
    20 (* bits *)
    21 
    22 fun mk_bit 0 = Syntax.const "False"
    23   | mk_bit 1 = Syntax.const "True"
    24   | mk_bit _ = sys_error "mk_bit";
    25 
    26 fun dest_bit (Const ("False", _)) = 0
    27   | dest_bit (Const ("True", _)) = 1
    28   | dest_bit _ = raise Match;
    29 
    30 
    31 (* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    32 
    33 fun prefix_len _ [] = 0
    34   | prefix_len pred (x :: xs) =
    35       if pred x then 1 + prefix_len pred xs else 0;
    36 
    37 fun mk_bin n =
    38   let
    39     fun bin_of 0  = []
    40       | bin_of ~1 = [~1]
    41       | bin_of n  = (n mod 2) :: bin_of (n div 2);
    42 
    43     fun term_of []   = Syntax.const "Numeral.bin.Pls"
    44       | term_of [~1] = Syntax.const "Numeral.bin.Min"
    45       | term_of (b :: bs) = Syntax.const "Numeral.bin.Bit" $ term_of bs 
    46                                                            $ mk_bit b;
    47     in term_of (bin_of n) end;
    48 
    49 fun bin_of_string str =
    50   let
    51     val (sign, digs) =
    52       (case Symbol.explode str of
    53         "#" :: "-" :: cs => (~1, cs)
    54       | "#" :: cs => (1, cs)
    55       | _ => raise ERROR);
    56     in mk_bin (sign * (#1 (Term.read_int digs))) end;
    57 
    58 (*we consider all "spellings"; Min is likely to be declared elsewhere*)
    59 fun bin_of (Const ("Pls", _)) = []
    60   | bin_of (Const ("bin.Pls", _)) = []
    61   | bin_of (Const ("Numeral.bin.Pls", _)) = []
    62   | bin_of (Const ("Min", _)) = [~1]
    63   | bin_of (Const ("bin.Min", _)) = [~1]
    64   | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
    65   | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    66   | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    67   | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    68   | bin_of _ = raise Match;
    69 
    70 fun int_of [] = 0
    71   | int_of (b :: bs) = b + 2 * int_of bs;
    72 
    73 val dest_bin = int_of o bin_of;
    74 
    75 fun dest_bin_str tm =
    76   let
    77     val rev_digs = bin_of tm;
    78     val (sign, zs) =
    79       (case rev rev_digs of
    80         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    81       | bs => ("", prefix_len (equal 0) bs));
    82     val num = string_of_int (abs (int_of rev_digs));
    83   in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
    84 
    85 
    86 (* translation of integer numeral tokens to and from bitstrings *)
    87 
    88 fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
    89       (Syntax.const "Numeral.number_of" $
    90         (bin_of_string str handle ERROR => raise TERM ("numeral_tr", [t])))
    91   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    92 
    93 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    94       let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
    95         if can Term.dest_Type T then t'
    96         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    97       end
    98   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    99 
   100 
   101 (* theory setup *)
   102 
   103 val setup =
   104  [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
   105   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
   106 
   107 
   108 end;