src/ZF/Tools/numeral_syntax.ML
author wenzelm
Sat, 17 Oct 2009 14:43:18 +0200
changeset 32960 69916a850301
parent 26190 cf51a23c0cd0
child 35112 ff6f60e6ab85
permissions -rw-r--r--
eliminated hard tabulators, guessing at each author's individual tab-width; tuned headers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     1
(*  Title:      ZF/Tools/numeral_syntax.ML
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     3
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     4
Concrete syntax for generic numerals.  Assumes consts and syntax of
23146
0bc590051d95 moved Integ files to canonical place;
wenzelm
parents: 21781
diff changeset
     5
theory Bin.
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     6
*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     7
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     8
signature NUMERAL_SYNTAX =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     9
sig
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    10
  val dest_bin : term -> int
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    11
  val mk_bin   : int -> term
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    12
  val int_tr   : term list -> term
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    13
  val int_tr'  : bool -> typ -> term list -> term
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 15965
diff changeset
    14
  val setup    : theory -> theory
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    15
end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    16
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    17
structure NumeralSyntax: NUMERAL_SYNTAX =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    18
struct
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    19
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    20
(* Bits *)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    21
26190
cf51a23c0cd0 tuned ML code, more antiquotations;
wenzelm
parents: 24712
diff changeset
    22
fun mk_bit 0 = @{term "0"}
cf51a23c0cd0 tuned ML code, more antiquotations;
wenzelm
parents: 24712
diff changeset
    23
  | mk_bit 1 = @{term "succ(0)"}
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    24
  | mk_bit _ = sys_error "mk_bit";
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    25
26190
cf51a23c0cd0 tuned ML code, more antiquotations;
wenzelm
parents: 24712
diff changeset
    26
fun dest_bit (Const (@{const_name "0"}, _)) = 0
cf51a23c0cd0 tuned ML code, more antiquotations;
wenzelm
parents: 24712
diff changeset
    27
  | dest_bit (Const (@{const_name succ}, _) $ Const (@{const_name "0"}, _)) = 1
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    28
  | dest_bit _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    29
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    30
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    31
(* Bit strings *)   (*we try to handle superfluous leading digits nicely*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    32
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    33
fun prefix_len _ [] = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    34
  | prefix_len pred (x :: xs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    35
      if pred x then 1 + prefix_len pred xs else 0;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    36
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    37
fun mk_bin i =
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    38
  let fun bin_of_int 0  = []
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    39
        | bin_of_int ~1 = [~1]
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    40
        | bin_of_int n  = (n mod 2) :: bin_of_int (n div 2);
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    41
26190
cf51a23c0cd0 tuned ML code, more antiquotations;
wenzelm
parents: 24712
diff changeset
    42
      fun term_of [] = @{const Pls}
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    43
            | term_of [~1] = @{const Min}
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    44
            | term_of (b :: bs) = @{const Bit} $ term_of bs $ mk_bit b;
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    45
  in
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    46
    term_of (bin_of_int i)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    47
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    48
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    49
(*we consider all "spellings", since they could vary depending on the caller*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    50
fun bin_of (Const ("Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    51
  | bin_of (Const ("bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    52
  | bin_of (Const ("Bin.bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    53
  | bin_of (Const ("Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    54
  | bin_of (Const ("bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    55
  | bin_of (Const ("Bin.bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    56
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    57
  | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    58
  | bin_of (Const ("Bin.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    59
  | bin_of _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    60
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    61
(*Convert a list of bits to an integer*)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    62
fun integ_of [] = 0
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    63
  | integ_of (b :: bs) = b + 2 * integ_of bs;
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    64
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    65
val dest_bin = integ_of o bin_of;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    66
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    67
(*leading 0s and (for negative numbers) -1s cause complications, though they 
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    68
  should never arise in normal use. The formalization used in HOL prevents 
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    69
  them altogether.*)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    70
fun show_int t =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    71
  let
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    72
    val rev_digs = bin_of t;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    73
    val (sign, zs) =
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    74
        (case rev rev_digs of
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    75
             ~1 :: bs => ("-", prefix_len (equal 1) bs)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 26190
diff changeset
    76
           | bs =>       ("",  prefix_len (equal 0) bs));
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    77
    val num = string_of_int (abs (integ_of rev_digs));
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    78
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    79
    "#" ^ sign ^ implode (replicate zs "0") ^ num
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    80
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    81
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    82
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    83
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    84
(* translation of integer constant tokens to and from binary *)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    85
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    86
fun int_tr (*"_Int"*) [t as Free (str, _)] =
21781
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 18708
diff changeset
    87
      Syntax.const "integ_of" $ mk_bin (#value (Syntax.read_xnum str))
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    88
  | int_tr (*"_Int"*) ts = raise TERM ("int_tr", ts);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    89
10917
1044648b3f84 use Syntax.read_xnum;
wenzelm
parents: 9570
diff changeset
    90
fun int_tr' _ _ (*"integ_of"*) [t] = Syntax.const "_Int" $ Syntax.free (show_int t)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    91
  | int_tr' (_:bool) (_:typ)     _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    92
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    93
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    94
val setup =
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24630
diff changeset
    95
 (Sign.add_trfuns ([], [("_Int", int_tr)], [], []) #>
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24630
diff changeset
    96
  Sign.add_trfunsT [("integ_of", int_tr'), ("Bin.integ_of", int_tr')]);
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    97
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    98
end;