src/HOL/Tools/numeral_syntax.ML
author paulson
Fri Jul 23 17:30:27 1999 +0200 (1999-07-23)
changeset 7078 4e64b2bd10ce
parent 7055 91764e88d932
child 7429 8f284fbb8728
permissions -rw-r--r--
now using correctly-typed constants from HOLogic
     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 = HOLogic.false_const
    23   | mk_bit 1 = HOLogic.true_const
    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 []   = HOLogic.pls_const
    44       | term_of [~1] = HOLogic.min_const
    45       | term_of (b :: bs) = HOLogic.bit_const $ term_of bs $ mk_bit b;
    46     in term_of (bin_of n) end;
    47 
    48 fun bin_of_string str =
    49   let
    50     val (sign, digs) =
    51       (case Symbol.explode str of
    52         "#" :: "-" :: cs => (~1, cs)
    53       | "#" :: cs => (1, cs)
    54       | _ => raise ERROR);
    55     in mk_bin (sign * (#1 (Term.read_int digs))) end;
    56 
    57 (*we consider all "spellings"; Min is likely to be declared elsewhere*)
    58 fun bin_of (Const ("Pls", _)) = []
    59   | bin_of (Const ("bin.Pls", _)) = []
    60   | bin_of (Const ("Numeral.bin.Pls", _)) = []
    61   | bin_of (Const ("Min", _)) = [~1]
    62   | bin_of (Const ("bin.Min", _)) = [~1]
    63   | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
    64   | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    65   | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    66   | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    67   | bin_of _ = raise Match;
    68 
    69 fun int_of [] = 0
    70   | int_of (b :: bs) = b + 2 * int_of bs;
    71 
    72 val dest_bin = int_of o bin_of;
    73 
    74 fun dest_bin_str tm =
    75   let
    76     val rev_digs = bin_of tm;
    77     val (sign, zs) =
    78       (case rev rev_digs of
    79         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    80       | bs => ("", prefix_len (equal 0) bs));
    81     val num = string_of_int (abs (int_of rev_digs));
    82   in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
    83 
    84 
    85 (* translation of integer numeral tokens to and from bitstrings *)
    86 
    87 fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
    88       (Syntax.const "Numeral.number_of" $
    89         (bin_of_string str handle ERROR => raise TERM ("numeral_tr", [t])))
    90   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    91 
    92 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    93       let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
    94         if can Term.dest_Type T then t'
    95         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    96       end
    97   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    98 
    99 
   100 (* theory setup *)
   101 
   102 val setup =
   103  [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
   104   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
   105 
   106 
   107 end;