src/HOL/Tools/numeral_syntax.ML
author wenzelm
Sat Jul 01 19:55:22 2000 +0200 (2000-07-01)
changeset 9230 17ae63f82ad8
parent 7550 060f9954f73d
child 9314 fd8b0f219879
permissions -rw-r--r--
GPLed;
     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 dest_bin : term -> int
    13   val mk_bin   : int -> term
    14   val setup    : (theory -> theory) list
    15 end;
    16 
    17 structure NumeralSyntax: NUMERAL_SYNTAX =
    18 struct
    19 
    20 
    21 (* bits *)
    22 
    23 fun mk_bit 0 = HOLogic.false_const
    24   | mk_bit 1 = HOLogic.true_const
    25   | mk_bit _ = sys_error "mk_bit";
    26 
    27 fun dest_bit (Const ("False", _)) = 0
    28   | dest_bit (Const ("True", _)) = 1
    29   | dest_bit _ = raise Match;
    30 
    31 
    32 (* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    33 
    34 fun prefix_len _ [] = 0
    35   | prefix_len pred (x :: xs) =
    36       if pred x then 1 + prefix_len pred xs else 0;
    37 
    38 fun mk_bin n =
    39   let
    40     fun bin_of 0  = []
    41       | bin_of ~1 = [~1]
    42       | bin_of n  = (n mod 2) :: bin_of (n div 2);
    43 
    44     fun term_of []   = HOLogic.pls_const
    45       | term_of [~1] = HOLogic.min_const
    46       | term_of (b :: bs) = HOLogic.bit_const $ term_of bs $ 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 val dest_bin = HOLogic.int_of o bin_of;
    71 
    72 fun dest_bin_str tm =
    73   let
    74     val rev_digs = bin_of tm;
    75     val (sign, zs) =
    76       (case rev rev_digs of
    77         ~1 :: bs => ("-", prefix_len (equal 1) bs)
    78       | bs => ("", prefix_len (equal 0) bs));
    79     val num = string_of_int (abs (HOLogic.int_of rev_digs));
    80   in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
    81 
    82 
    83 (* translation of integer numeral tokens to and from bitstrings *)
    84 
    85 fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
    86       (Syntax.const "Numeral.number_of" $
    87         (bin_of_string str handle ERROR => raise TERM ("numeral_tr", [t])))
    88   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    89 
    90 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    91       let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
    92         if not (! show_types) andalso can Term.dest_Type T then t'
    93         else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    94       end
    95   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    96 
    97 
    98 (* theory setup *)
    99 
   100 val setup =
   101  [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
   102   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
   103 
   104 
   105 end;