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