src/HOL/Tools/numeral_syntax.ML
author nipkow
Mon Aug 12 17:48:19 2002 +0200 (2002-08-12)
changeset 13491 ddf6ae639f21
parent 13110 ca8cd110f769
child 13495 af27202d6d1c
permissions -rw-r--r--
*** empty log message ***
     1 (*  Title:      HOL/Tools/numeral_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Concrete syntax for generic numerals.  Assumes consts and syntax of
     7 theory HOL/Numeral.
     8 *)
     9 
    10 signature NUMERAL_SYNTAX =
    11 sig
    12   val setup: (theory -> theory) list
    13 end;
    14 
    15 structure NumeralSyntax: NUMERAL_SYNTAX =
    16 struct
    17 
    18 
    19 (* bits *)
    20 
    21 fun dest_bit (Const ("False", _)) = 0
    22   | dest_bit (Const ("True", _)) = 1
    23   | dest_bit _ = raise Match;
    24 
    25 
    26 (* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    27 
    28 fun prefix_len _ [] = 0
    29   | prefix_len pred (x :: xs) =
    30       if pred x then 1 + prefix_len pred xs else 0;
    31 
    32 fun bin_of (Const ("bin.Pls", _)) = []
    33   | bin_of (Const ("Numeral.bin.Pls", _)) = []
    34   | bin_of (Const ("bin.Min", _)) = [~1]
    35   | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
    36   | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    37   | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    38   | bin_of _ = raise Match;
    39 
    40 val dest_bin = HOLogic.int_of o bin_of;
    41 
    42 fun dest_bin_str tm =
    43   let
    44     val rev_digs = bin_of tm;
    45     val (sign, zs) =
    46       (case rev rev_digs of
    47         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    48       | bs => ("", prefix_len (equal 0) bs));
    49     val i = HOLogic.int_of rev_digs;
    50     val num = string_of_int (abs i);
    51   in
    52     if i = 0 orelse i = 1 then raise Match
    53     else sign ^ implode (replicate zs "0") ^ num
    54   end;
    55 
    56 
    57 (* translation of integer numeral tokens to and from bitstrings *)
    58 
    59 fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
    60       (Syntax.const "Numeral.number_of" $ HOLogic.mk_bin (Syntax.read_xnum str))
    61   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    62 
    63 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    64       let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
    65         if not (! show_types) andalso can Term.dest_Type T then t'
    66         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    67       end
    68   | numeral_tr' _ (*"number_of"*) T (t :: ts) =
    69       if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
    70       else raise Match
    71   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    72 
    73 
    74 (* theory setup *)
    75 
    76 val setup =
    77  [Theory.hide_consts false
    78     ["Numeral.bin.Pls", "Numeral.bin.Min"],
    79 Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
    80   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
    81 
    82 
    83 end;