src/HOL/Tools/numeral_syntax.ML
author huffman
Sun Feb 17 06:49:53 2008 +0100 (2008-02-17)
changeset 26086 3c243098b64a
parent 25919 8b1c0d434824
child 29316 0a7fcdd77f4b
permissions -rw-r--r--
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
wenzelm@6905
     1
(*  Title:      HOL/Tools/numeral_syntax.ML
wenzelm@6905
     2
    ID:         $Id$
kleing@20067
     3
    Authors:    Markus Wenzel, TU Muenchen
wenzelm@6905
     4
wenzelm@21780
     5
Concrete syntax for generic numerals -- preserves leading zeros/ones.
wenzelm@6905
     6
*)
wenzelm@6905
     7
wenzelm@6905
     8
signature NUMERAL_SYNTAX =
wenzelm@6905
     9
sig
wenzelm@18708
    10
  val setup: theory -> theory
wenzelm@6905
    11
end;
wenzelm@6905
    12
wenzelm@6905
    13
structure NumeralSyntax: NUMERAL_SYNTAX =
wenzelm@6905
    14
struct
wenzelm@6905
    15
wenzelm@21780
    16
(* parse translation *)
wenzelm@21780
    17
wenzelm@21780
    18
local
wenzelm@21780
    19
wenzelm@21780
    20
fun mk_bin num =
wenzelm@21780
    21
  let
wenzelm@21780
    22
    val {leading_zeros = z, value, ...} = Syntax.read_xnum num;
huffman@26086
    23
    fun bit b bs = HOLogic.mk_bit b $ bs;
haftmann@25919
    24
    fun mk 0 = (* FIXME funpow z (bit 0) *) (Syntax.const @{const_name Int.Pls})
haftmann@25919
    25
      | mk ~1 = (* FIXME funpow z (bit 1) *) (Syntax.const @{const_name Int.Min})
wenzelm@24630
    26
      | mk i = let val (q, r) = Integer.div_mod i 2 in bit r (mk q) end;
wenzelm@21780
    27
  in mk value end;
wenzelm@21780
    28
wenzelm@21780
    29
in
wenzelm@21780
    30
wenzelm@21780
    31
fun numeral_tr (*"_Numeral"*) [t as Const (num, _)] =
haftmann@25919
    32
      Syntax.const @{const_name Int.number_of} $ mk_bin num
wenzelm@21780
    33
  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
wenzelm@21780
    34
wenzelm@21780
    35
end;
wenzelm@6905
    36
wenzelm@21780
    37
wenzelm@21780
    38
(* print translation *)
wenzelm@21780
    39
wenzelm@21780
    40
local
wenzelm@21780
    41
haftmann@25919
    42
fun dest_bin (Const (@{const_syntax "Int.Pls"}, _)) = []
haftmann@25919
    43
  | dest_bin (Const (@{const_syntax "Int.Min"}, _)) = [~1]
huffman@26086
    44
  | dest_bin (Const (@{const_syntax "Int.Bit0"}, _) $ bs) = 0 :: dest_bin bs
huffman@26086
    45
  | dest_bin (Const (@{const_syntax "Int.Bit1"}, _) $ bs) = 1 :: dest_bin bs
wenzelm@21780
    46
  | dest_bin _ = raise Match;
wenzelm@21780
    47
wenzelm@21780
    48
fun leading _ [] = 0
wenzelm@21780
    49
  | leading (i: int) (j :: js) = if i = j then 1 + leading i js else 0;
wenzelm@21780
    50
wenzelm@21780
    51
fun int_of [] = 0
wenzelm@24630
    52
  | int_of (b :: bs) = b + 2 * int_of bs;
wenzelm@6905
    53
wenzelm@6905
    54
fun dest_bin_str tm =
wenzelm@6905
    55
  let
wenzelm@21780
    56
    val rev_digs = dest_bin tm;
wenzelm@21780
    57
    val (sign, z) =
wenzelm@6905
    58
      (case rev rev_digs of
wenzelm@21780
    59
        ~1 :: bs => ("-", leading 1 bs)
wenzelm@21780
    60
      | bs => ("", leading 0 bs));
wenzelm@21780
    61
    val i = int_of rev_digs;
wenzelm@24630
    62
    val num = string_of_int (abs i);
wenzelm@11701
    63
  in
wenzelm@21780
    64
    if (i = 0 orelse i = 1) andalso z = 0 then raise Match
wenzelm@21780
    65
    else sign ^ implode (replicate z "0") ^ num
wenzelm@11701
    66
  end;
wenzelm@6905
    67
wenzelm@21780
    68
in
wenzelm@6905
    69
wenzelm@6905
    70
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
wenzelm@11488
    71
      let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
wenzelm@7429
    72
        if not (! show_types) andalso can Term.dest_Type T then t'
wenzelm@6905
    73
        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
wenzelm@6905
    74
      end
wenzelm@13110
    75
  | numeral_tr' _ (*"number_of"*) T (t :: ts) =
wenzelm@13110
    76
      if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
wenzelm@13110
    77
      else raise Match
wenzelm@6905
    78
  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
wenzelm@6905
    79
wenzelm@21780
    80
end;
wenzelm@21780
    81
wenzelm@6905
    82
wenzelm@6905
    83
(* theory setup *)
wenzelm@6905
    84
wenzelm@6905
    85
val setup =
wenzelm@24712
    86
  Sign.add_trfuns ([], [("_Numeral", numeral_tr)], [], []) #>
haftmann@25919
    87
  Sign.add_trfunsT [(@{const_syntax Int.number_of}, numeral_tr')];
wenzelm@6905
    88
wenzelm@6905
    89
end;