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