src/HOL/Tools/numeral_syntax.ML
author paulson
Wed Jul 21 15:18:36 1999 +0200 (1999-07-21)
changeset 7055 91764e88d932
parent 6905 9bc05ec3497b
child 7078 4e64b2bd10ce
permissions -rw-r--r--
now exports mk_bin
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
paulson@7055
    11
  val dest_bin : term -> int
paulson@7055
    12
  val mk_bin   : int -> term
paulson@7055
    13
  val setup    : (theory -> theory) list
wenzelm@6905
    14
end;
wenzelm@6905
    15
wenzelm@6905
    16
structure NumeralSyntax: NUMERAL_SYNTAX =
wenzelm@6905
    17
struct
wenzelm@6905
    18
wenzelm@6905
    19
wenzelm@6905
    20
(* bits *)
wenzelm@6905
    21
wenzelm@6905
    22
fun mk_bit 0 = Syntax.const "False"
wenzelm@6905
    23
  | mk_bit 1 = Syntax.const "True"
wenzelm@6905
    24
  | mk_bit _ = sys_error "mk_bit";
wenzelm@6905
    25
wenzelm@6905
    26
fun dest_bit (Const ("False", _)) = 0
wenzelm@6905
    27
  | dest_bit (Const ("True", _)) = 1
wenzelm@6905
    28
  | dest_bit _ = raise Match;
wenzelm@6905
    29
wenzelm@6905
    30
wenzelm@6905
    31
(* bit strings *)   (*we try to handle superfluous leading digits nicely*)
wenzelm@6905
    32
wenzelm@6905
    33
fun prefix_len _ [] = 0
wenzelm@6905
    34
  | prefix_len pred (x :: xs) =
wenzelm@6905
    35
      if pred x then 1 + prefix_len pred xs else 0;
wenzelm@6905
    36
paulson@7055
    37
fun mk_bin n =
paulson@7055
    38
  let
paulson@7055
    39
    fun bin_of 0  = []
paulson@7055
    40
      | bin_of ~1 = [~1]
paulson@7055
    41
      | bin_of n  = (n mod 2) :: bin_of (n div 2);
paulson@7055
    42
paulson@7055
    43
    fun term_of []   = Syntax.const "Numeral.bin.Pls"
paulson@7055
    44
      | term_of [~1] = Syntax.const "Numeral.bin.Min"
paulson@7055
    45
      | term_of (b :: bs) = Syntax.const "Numeral.bin.Bit" $ term_of bs 
paulson@7055
    46
                                                           $ mk_bit b;
paulson@7055
    47
    in term_of (bin_of n) end;
paulson@7055
    48
paulson@7055
    49
fun bin_of_string str =
wenzelm@6905
    50
  let
wenzelm@6905
    51
    val (sign, digs) =
wenzelm@6905
    52
      (case Symbol.explode str of
wenzelm@6905
    53
        "#" :: "-" :: cs => (~1, cs)
wenzelm@6905
    54
      | "#" :: cs => (1, cs)
wenzelm@6905
    55
      | _ => raise ERROR);
paulson@7055
    56
    in mk_bin (sign * (#1 (Term.read_int digs))) end;
wenzelm@6905
    57
wenzelm@6905
    58
(*we consider all "spellings"; Min is likely to be declared elsewhere*)
wenzelm@6905
    59
fun bin_of (Const ("Pls", _)) = []
wenzelm@6905
    60
  | bin_of (Const ("bin.Pls", _)) = []
wenzelm@6905
    61
  | bin_of (Const ("Numeral.bin.Pls", _)) = []
wenzelm@6905
    62
  | bin_of (Const ("Min", _)) = [~1]
wenzelm@6905
    63
  | bin_of (Const ("bin.Min", _)) = [~1]
wenzelm@6905
    64
  | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
wenzelm@6905
    65
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    66
  | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    67
  | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    68
  | bin_of _ = raise Match;
wenzelm@6905
    69
wenzelm@6905
    70
fun int_of [] = 0
wenzelm@6905
    71
  | int_of (b :: bs) = b + 2 * int_of bs;
wenzelm@6905
    72
wenzelm@6905
    73
val dest_bin = int_of o bin_of;
wenzelm@6905
    74
wenzelm@6905
    75
fun dest_bin_str tm =
wenzelm@6905
    76
  let
wenzelm@6905
    77
    val rev_digs = bin_of tm;
wenzelm@6905
    78
    val (sign, zs) =
wenzelm@6905
    79
      (case rev rev_digs of
wenzelm@6905
    80
        ~1 :: bs => ("-", prefix_len (equal 1) bs)
wenzelm@6905
    81
      | bs => ("", prefix_len (equal 0) bs));
wenzelm@6905
    82
    val num = string_of_int (abs (int_of rev_digs));
wenzelm@6905
    83
  in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
wenzelm@6905
    84
wenzelm@6905
    85
wenzelm@6905
    86
(* translation of integer numeral tokens to and from bitstrings *)
wenzelm@6905
    87
wenzelm@6905
    88
fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
wenzelm@6905
    89
      (Syntax.const "Numeral.number_of" $
paulson@7055
    90
        (bin_of_string str handle ERROR => raise TERM ("numeral_tr", [t])))
wenzelm@6905
    91
  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
wenzelm@6905
    92
wenzelm@6905
    93
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
wenzelm@6905
    94
      let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
wenzelm@6905
    95
        if can Term.dest_Type T then t'
wenzelm@6905
    96
        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
wenzelm@6905
    97
      end
wenzelm@6905
    98
  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
wenzelm@6905
    99
wenzelm@6905
   100
wenzelm@6905
   101
(* theory setup *)
wenzelm@6905
   102
wenzelm@6905
   103
val setup =
wenzelm@6905
   104
 [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
wenzelm@6905
   105
  Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
wenzelm@6905
   106
wenzelm@6905
   107
wenzelm@6905
   108
end;