src/HOL/Tools/numeral_syntax.ML
author wenzelm
Tue May 07 14:28:34 2002 +0200 (2002-05-07)
changeset 13110 ca8cd110f769
parent 11701 3d51fbf81c17
child 13491 ddf6ae639f21
permissions -rw-r--r--
be liberal about missing types;
wenzelm@6905
     1
(*  Title:      HOL/Tools/numeral_syntax.ML
wenzelm@6905
     2
    ID:         $Id$
wenzelm@6905
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@9230
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6905
     5
wenzelm@6905
     6
Concrete syntax for generic numerals.  Assumes consts and syntax of
wenzelm@6905
     7
theory HOL/Numeral.
wenzelm@6905
     8
*)
wenzelm@6905
     9
wenzelm@6905
    10
signature NUMERAL_SYNTAX =
wenzelm@6905
    11
sig
wenzelm@10891
    12
  val setup: (theory -> theory) list
wenzelm@6905
    13
end;
wenzelm@6905
    14
wenzelm@6905
    15
structure NumeralSyntax: NUMERAL_SYNTAX =
wenzelm@6905
    16
struct
wenzelm@6905
    17
wenzelm@6905
    18
wenzelm@6905
    19
(* bits *)
wenzelm@6905
    20
wenzelm@6905
    21
fun dest_bit (Const ("False", _)) = 0
wenzelm@6905
    22
  | dest_bit (Const ("True", _)) = 1
wenzelm@6905
    23
  | dest_bit _ = raise Match;
wenzelm@6905
    24
wenzelm@6905
    25
wenzelm@6905
    26
(* bit strings *)   (*we try to handle superfluous leading digits nicely*)
wenzelm@6905
    27
wenzelm@6905
    28
fun prefix_len _ [] = 0
wenzelm@6905
    29
  | prefix_len pred (x :: xs) =
wenzelm@6905
    30
      if pred x then 1 + prefix_len pred xs else 0;
wenzelm@6905
    31
wenzelm@6905
    32
(*we consider all "spellings"; Min is likely to be declared elsewhere*)
wenzelm@6905
    33
fun bin_of (Const ("Pls", _)) = []
wenzelm@6905
    34
  | bin_of (Const ("bin.Pls", _)) = []
wenzelm@6905
    35
  | bin_of (Const ("Numeral.bin.Pls", _)) = []
wenzelm@6905
    36
  | bin_of (Const ("Min", _)) = [~1]
wenzelm@6905
    37
  | bin_of (Const ("bin.Min", _)) = [~1]
wenzelm@6905
    38
  | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
wenzelm@6905
    39
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    40
  | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    41
  | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
wenzelm@6905
    42
  | bin_of _ = raise Match;
wenzelm@6905
    43
nipkow@7550
    44
val dest_bin = HOLogic.int_of o bin_of;
wenzelm@6905
    45
wenzelm@6905
    46
fun dest_bin_str tm =
wenzelm@6905
    47
  let
wenzelm@6905
    48
    val rev_digs = bin_of tm;
wenzelm@6905
    49
    val (sign, zs) =
wenzelm@6905
    50
      (case rev rev_digs of
wenzelm@6905
    51
        ~1 :: bs => ("-", prefix_len (equal 1) bs)
wenzelm@6905
    52
      | bs => ("", prefix_len (equal 0) bs));
wenzelm@11701
    53
    val i = HOLogic.int_of rev_digs;
wenzelm@11701
    54
    val num = string_of_int (abs i);
wenzelm@11701
    55
  in
wenzelm@11701
    56
    if i = 0 orelse i = 1 then raise Match
wenzelm@11701
    57
    else sign ^ implode (replicate zs "0") ^ num
wenzelm@11701
    58
  end;
wenzelm@6905
    59
wenzelm@6905
    60
wenzelm@6905
    61
(* translation of integer numeral tokens to and from bitstrings *)
wenzelm@6905
    62
wenzelm@11488
    63
fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] =
nipkow@10693
    64
      (Syntax.const "Numeral.number_of" $ HOLogic.mk_bin (Syntax.read_xnum str))
wenzelm@6905
    65
  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
wenzelm@6905
    66
wenzelm@6905
    67
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
wenzelm@11488
    68
      let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in
wenzelm@7429
    69
        if not (! show_types) andalso can Term.dest_Type T then t'
wenzelm@6905
    70
        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
wenzelm@6905
    71
      end
wenzelm@13110
    72
  | numeral_tr' _ (*"number_of"*) T (t :: ts) =
wenzelm@13110
    73
      if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t)
wenzelm@13110
    74
      else raise Match
wenzelm@6905
    75
  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
wenzelm@6905
    76
wenzelm@6905
    77
wenzelm@6905
    78
(* theory setup *)
wenzelm@6905
    79
wenzelm@6905
    80
val setup =
wenzelm@6905
    81
 [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
wenzelm@6905
    82
  Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
wenzelm@6905
    83
wenzelm@6905
    84
wenzelm@6905
    85
end;