src/HOL/Tools/numeral_syntax.ML
 author wenzelm Fri Oct 05 21:52:39 2001 +0200 (2001-10-05) changeset 11701 3d51fbf81c17 parent 11488 4ff900551340 child 13110 ca8cd110f769 permissions -rw-r--r--
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
"num" syntax (still with "#"), Numeral0, Numeral1;
```     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 (*we consider all "spellings"; Min is likely to be declared elsewhere*)
```
```    33 fun bin_of (Const ("Pls", _)) = []
```
```    34   | bin_of (Const ("bin.Pls", _)) = []
```
```    35   | bin_of (Const ("Numeral.bin.Pls", _)) = []
```
```    36   | bin_of (Const ("Min", _)) = [~1]
```
```    37   | bin_of (Const ("bin.Min", _)) = [~1]
```
```    38   | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
```
```    39   | bin_of (Const ("Bit", _) \$ bs \$ b) = dest_bit b :: bin_of bs
```
```    40   | bin_of (Const ("bin.Bit", _) \$ bs \$ b) = dest_bit b :: bin_of bs
```
```    41   | bin_of (Const ("Numeral.bin.Bit", _) \$ bs \$ b) = dest_bit b :: bin_of bs
```
```    42   | bin_of _ = raise Match;
```
```    43
```
```    44 val dest_bin = HOLogic.int_of o bin_of;
```
```    45
```
```    46 fun dest_bin_str tm =
```
```    47   let
```
```    48     val rev_digs = bin_of tm;
```
```    49     val (sign, zs) =
```
```    50       (case rev rev_digs of
```
```    51         ~1 :: bs => ("-", prefix_len (equal 1) bs)
```
```    52       | bs => ("", prefix_len (equal 0) bs));
```
```    53     val i = HOLogic.int_of rev_digs;
```
```    54     val num = string_of_int (abs i);
```
```    55   in
```
```    56     if i = 0 orelse i = 1 then raise Match
```
```    57     else sign ^ implode (replicate zs "0") ^ num
```
```    58   end;
```
```    59
```
```    60
```
```    61 (* translation of integer numeral tokens to and from bitstrings *)
```
```    62
```
```    63 fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
```
```    64       (Syntax.const "Numeral.number_of" \$ HOLogic.mk_bin (Syntax.read_xnum str))
```
```    65   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
```
```    66
```
```    67 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
```
```    68       let val t' = Syntax.const "_Numeral" \$ Syntax.free (dest_bin_str t) in
```
```    69         if not (! show_types) andalso can Term.dest_Type T then t'
```
```    70         else Syntax.const Syntax.constrainC \$ t' \$ Syntax.term_of_typ show_sorts T
```
```    71       end
```
```    72   | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
```
```    73
```
```    74
```
```    75 (* theory setup *)
```
```    76
```
```    77 val setup =
```
```    78  [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
```
```    79   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
```
```    80
```
```    81
```
```    82 end;
```