src/HOL/Tools/numeral_syntax.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 13495 af27202d6d1c
child 15013 34264f5e4691
permissions -rw-r--r--
Merged in license change from Isabelle2004
     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 setup: (theory -> theory) list
    12 end;
    13 
    14 structure NumeralSyntax: NUMERAL_SYNTAX =
    15 struct
    16 
    17 
    18 (* bits *)
    19 
    20 fun dest_bit (Const ("False", _)) = 0
    21   | dest_bit (Const ("True", _)) = 1
    22   | dest_bit _ = raise Match;
    23 
    24 
    25 (* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    26 
    27 fun prefix_len _ [] = 0
    28   | prefix_len pred (x :: xs) =
    29       if pred x then 1 + prefix_len pred xs else 0;
    30 
    31 fun bin_of (Const ("bin.Pls", _)) = []
    32   | bin_of (Const ("bin.Min", _)) = [~1]
    33   | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    34   | bin_of _ = raise Match;
    35 
    36 val dest_bin = HOLogic.int_of o bin_of;
    37 
    38 fun dest_bin_str tm =
    39   let
    40     val rev_digs = bin_of tm;
    41     val (sign, zs) =
    42       (case rev rev_digs of
    43         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    44       | bs => ("", prefix_len (equal 0) bs));
    45     val i = HOLogic.int_of rev_digs;
    46     val num = string_of_int (abs i);
    47   in
    48     if i = 0 orelse i = 1 then raise Match
    49     else sign ^ implode (replicate zs "0") ^ num
    50   end;
    51 
    52 
    53 (* translation of integer numeral tokens to and from bitstrings *)
    54 
    55 fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
    56       (Syntax.const "Numeral.number_of" $ HOLogic.mk_bin (Syntax.read_xnum str))
    57   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    58 
    59 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    60       let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
    61         if not (! show_types) andalso can Term.dest_Type T then t'
    62         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    63       end
    64   | numeral_tr' _ (*"number_of"*) T (t :: ts) =
    65       if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
    66       else raise Match
    67   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    68 
    69 
    70 (* theory setup *)
    71 
    72 val setup =
    73  [Theory.hide_consts false ["Numeral.bin.Pls", "Numeral.bin.Min"],
    74   Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
    75   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
    76 
    77 
    78 end;