src/HOL/Tools/numeral_syntax.ML
author nipkow
Tue, 21 Sep 1999 14:14:14 +0200
changeset 7550 060f9954f73d
parent 7429 8f284fbb8728
child 9230 17ae63f82ad8
permissions -rw-r--r--
moved inf_of(?) to hologic.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/numeral_syntax.ML
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     4
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     5
Concrete syntax for generic numerals.  Assumes consts and syntax of
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     6
theory HOL/Numeral.
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     7
*)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     8
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
     9
signature NUMERAL_SYNTAX =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    10
sig
7055
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    11
  val dest_bin : term -> int
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    12
  val mk_bin   : int -> term
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    13
  val setup    : (theory -> theory) list
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    14
end;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    15
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    16
structure NumeralSyntax: NUMERAL_SYNTAX =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    17
struct
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    18
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    19
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    20
(* bits *)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    21
7078
4e64b2bd10ce now using correctly-typed constants from HOLogic
paulson
parents: 7055
diff changeset
    22
fun mk_bit 0 = HOLogic.false_const
4e64b2bd10ce now using correctly-typed constants from HOLogic
paulson
parents: 7055
diff changeset
    23
  | mk_bit 1 = HOLogic.true_const
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    24
  | mk_bit _ = sys_error "mk_bit";
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    25
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    26
fun dest_bit (Const ("False", _)) = 0
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    27
  | dest_bit (Const ("True", _)) = 1
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    28
  | dest_bit _ = raise Match;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    29
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    30
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    31
(* bit strings *)   (*we try to handle superfluous leading digits nicely*)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    32
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    33
fun prefix_len _ [] = 0
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    34
  | prefix_len pred (x :: xs) =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    35
      if pred x then 1 + prefix_len pred xs else 0;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    36
7055
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    37
fun mk_bin n =
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    38
  let
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    39
    fun bin_of 0  = []
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    40
      | bin_of ~1 = [~1]
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    41
      | bin_of n  = (n mod 2) :: bin_of (n div 2);
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    42
7078
4e64b2bd10ce now using correctly-typed constants from HOLogic
paulson
parents: 7055
diff changeset
    43
    fun term_of []   = HOLogic.pls_const
4e64b2bd10ce now using correctly-typed constants from HOLogic
paulson
parents: 7055
diff changeset
    44
      | term_of [~1] = HOLogic.min_const
4e64b2bd10ce now using correctly-typed constants from HOLogic
paulson
parents: 7055
diff changeset
    45
      | term_of (b :: bs) = HOLogic.bit_const $ term_of bs $ mk_bit b;
7055
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    46
    in term_of (bin_of n) end;
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    47
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    48
fun bin_of_string str =
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    49
  let
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    50
    val (sign, digs) =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    51
      (case Symbol.explode str of
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    52
        "#" :: "-" :: cs => (~1, cs)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    53
      | "#" :: cs => (1, cs)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    54
      | _ => raise ERROR);
7055
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    55
    in mk_bin (sign * (#1 (Term.read_int digs))) end;
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    56
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    57
(*we consider all "spellings"; Min is likely to be declared elsewhere*)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    58
fun bin_of (Const ("Pls", _)) = []
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    59
  | bin_of (Const ("bin.Pls", _)) = []
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    60
  | bin_of (Const ("Numeral.bin.Pls", _)) = []
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    61
  | bin_of (Const ("Min", _)) = [~1]
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    62
  | bin_of (Const ("bin.Min", _)) = [~1]
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    63
  | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    64
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    65
  | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    66
  | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    67
  | bin_of _ = raise Match;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    68
7550
060f9954f73d moved inf_of(?) to hologic.
nipkow
parents: 7429
diff changeset
    69
val dest_bin = HOLogic.int_of o bin_of;
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    70
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    71
fun dest_bin_str tm =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    72
  let
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    73
    val rev_digs = bin_of tm;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    74
    val (sign, zs) =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    75
      (case rev rev_digs of
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    76
        ~1 :: bs => ("-", prefix_len (equal 1) bs)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    77
      | bs => ("", prefix_len (equal 0) bs));
7550
060f9954f73d moved inf_of(?) to hologic.
nipkow
parents: 7429
diff changeset
    78
    val num = string_of_int (abs (HOLogic.int_of rev_digs));
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    79
  in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    80
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    81
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    82
(* translation of integer numeral tokens to and from bitstrings *)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    83
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    84
fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    85
      (Syntax.const "Numeral.number_of" $
7055
91764e88d932 now exports mk_bin
paulson
parents: 6905
diff changeset
    86
        (bin_of_string str handle ERROR => raise TERM ("numeral_tr", [t])))
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    87
  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    88
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    89
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    90
      let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
7429
8f284fbb8728 observe show_types;
wenzelm
parents: 7078
diff changeset
    91
        if not (! show_types) andalso can Term.dest_Type T then t'
6905
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    92
        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    93
      end
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    94
  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    95
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    96
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    97
(* theory setup *)
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    98
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
    99
val setup =
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
   100
 [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
   101
  Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
   102
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
   103
9bc05ec3497b added Numeral.thy, Tools/numeral_syntax.ML;
wenzelm
parents:
diff changeset
   104
end;