src/HOL/Tools/numeral_syntax.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18708 4b3dadb4fe33
child 19381 6cd8abc7f15b
permissions -rw-r--r--
simplified type attribute;
wenzelm@6905
     1
(*  Title:      HOL/Tools/numeral_syntax.ML
wenzelm@6905
     2
    ID:         $Id$
wenzelm@6905
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6905
     4
wenzelm@6905
     5
Concrete syntax for generic numerals.  Assumes consts and syntax of
wenzelm@6905
     6
theory HOL/Numeral.
wenzelm@6905
     7
*)
wenzelm@6905
     8
wenzelm@6905
     9
signature NUMERAL_SYNTAX =
wenzelm@6905
    10
sig
wenzelm@18708
    11
  val setup: theory -> theory
wenzelm@6905
    12
end;
wenzelm@6905
    13
wenzelm@6905
    14
structure NumeralSyntax: NUMERAL_SYNTAX =
wenzelm@6905
    15
struct
wenzelm@6905
    16
wenzelm@6905
    17
wenzelm@6905
    18
(* bit strings *)   (*we try to handle superfluous leading digits nicely*)
wenzelm@6905
    19
wenzelm@6905
    20
fun prefix_len _ [] = 0
wenzelm@6905
    21
  | prefix_len pred (x :: xs) =
wenzelm@6905
    22
      if pred x then 1 + prefix_len pred xs else 0;
wenzelm@6905
    23
wenzelm@6905
    24
fun dest_bin_str tm =
wenzelm@6905
    25
  let
paulson@15620
    26
    val rev_digs = HOLogic.bin_of tm handle TERM _ => raise Match
wenzelm@6905
    27
    val (sign, zs) =
wenzelm@6905
    28
      (case rev rev_digs of
wenzelm@6905
    29
        ~1 :: bs => ("-", prefix_len (equal 1) bs)
wenzelm@6905
    30
      | bs => ("", prefix_len (equal 0) bs));
paulson@15965
    31
    val i = HOLogic.int_of rev_digs;
obua@15595
    32
    val num = IntInf.toString (IntInf.abs i);
wenzelm@11701
    33
  in
obua@15595
    34
    if i = IntInf.fromInt 0 orelse i = IntInf.fromInt 1 then raise Match
wenzelm@11701
    35
    else sign ^ implode (replicate zs "0") ^ num
wenzelm@11701
    36
  end;
wenzelm@6905
    37
wenzelm@6905
    38
(* translation of integer numeral tokens to and from bitstrings *)
wenzelm@6905
    39
wenzelm@11488
    40
fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
paulson@15965
    41
      (Syntax.const "Numeral.number_of" $ (HOLogic.mk_bin (valOf (IntInf.fromString str))))
wenzelm@6905
    42
  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
wenzelm@6905
    43
wenzelm@6905
    44
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
wenzelm@11488
    45
      let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
wenzelm@7429
    46
        if not (! show_types) andalso can Term.dest_Type T then t'
wenzelm@6905
    47
        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
wenzelm@6905
    48
      end
wenzelm@13110
    49
  | numeral_tr' _ (*"number_of"*) T (t :: ts) =
wenzelm@13110
    50
      if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
wenzelm@13110
    51
      else raise Match
wenzelm@6905
    52
  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
wenzelm@6905
    53
wenzelm@6905
    54
wenzelm@6905
    55
(* theory setup *)
wenzelm@6905
    56
wenzelm@6905
    57
val setup =
wenzelm@18708
    58
  Theory.hide_consts_i false ["Numeral.Pls", "Numeral.Min"] #>
wenzelm@18708
    59
  Theory.hide_consts_i false ["Numeral.bit.B0", "Numeral.bit.B1"] #>
wenzelm@18708
    60
  Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []) #>
wenzelm@18708
    61
  Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')];
wenzelm@6905
    62
wenzelm@6905
    63
end;