src/ZF/Tools/numeral_syntax.ML
author wenzelm
Tue, 18 Sep 2007 16:08:00 +0200
changeset 24630 351a308ab58d
parent 23146 0bc590051d95
child 24712 64ed05609568
permissions -rw-r--r--
simplified type int (eliminated IntInf.int, integer);
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
    ID:         $Id$
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     4
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     5
Concrete syntax for generic numerals.  Assumes consts and syntax of
23146
0bc590051d95 moved Integ files to canonical place;
wenzelm
parents: 21781
diff changeset
     6
theory Bin.
9570
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     9
signature NUMERAL_SYNTAX =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    10
sig
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    11
  val dest_bin : term -> int
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    12
  val mk_bin   : int -> term
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    13
  val int_tr   : term list -> term
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    14
  val int_tr'  : bool -> typ -> term list -> term
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 15965
diff changeset
    15
  val setup    : theory -> theory
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    16
end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    17
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    18
structure NumeralSyntax: NUMERAL_SYNTAX =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    19
struct
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    20
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    21
(* Bits *)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    22
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    23
val zero = Const("0", iT);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    24
val succ = Const("succ", iT --> iT);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    25
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    26
fun mk_bit 0 = zero
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    27
  | mk_bit 1 = succ$zero
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    28
  | mk_bit _ = sys_error "mk_bit";
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
fun dest_bit (Const ("0", _)) = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    31
  | dest_bit (Const ("succ", _) $ Const ("0", _)) = 1
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    32
  | dest_bit _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    33
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    34
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    35
(* Bit strings *)   (*we try to handle superfluous leading digits nicely*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    36
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    37
fun prefix_len _ [] = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    38
  | prefix_len pred (x :: xs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    39
      if pred x then 1 + prefix_len pred xs else 0;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    40
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    41
val pls_const = Const ("Bin.bin.Pls", iT)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    42
and min_const = Const ("Bin.bin.Min", iT)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    43
and bit_const = Const ("Bin.bin.Bit", [iT, iT] ---> iT);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    44
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    45
fun mk_bin i =
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    46
  let fun bin_of_int 0  = []
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    47
  	    | bin_of_int ~1 = [~1]
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    48
    	| 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
    49
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    50
      fun term_of []   = pls_const
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    51
	    | term_of [~1] = min_const
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    52
	    | term_of (b :: bs) = bit_const $ term_of bs $ mk_bit b;
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    53
  in
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    54
    term_of (bin_of_int i)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    55
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    56
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    57
(*we consider all "spellings", since they could vary depending on the caller*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    58
fun bin_of (Const ("Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    59
  | bin_of (Const ("bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    60
  | bin_of (Const ("Bin.bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    61
  | bin_of (Const ("Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    62
  | bin_of (Const ("bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    63
  | bin_of (Const ("Bin.bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    64
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    65
  | 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
    66
  | 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
    67
  | bin_of _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    68
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    69
(*Convert a list of bits to an integer*)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    70
fun integ_of [] = 0
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    71
  | integ_of (b :: bs) = b + 2 * integ_of bs;
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    72
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    73
val dest_bin = integ_of o bin_of;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    74
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 10917
diff changeset
    75
(*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
    76
  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
    77
  them altogether.*)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    78
fun show_int t =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    79
  let
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    80
    val rev_digs = bin_of t;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    81
    val (sign, zs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    82
	(case rev rev_digs of
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    83
	     ~1 :: bs => ("-", prefix_len (equal 1) bs)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    84
	   | bs =>       ("",  prefix_len (equal 0) bs));
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23146
diff changeset
    85
    val num = string_of_int (abs (integ_of rev_digs));
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    86
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    87
    "#" ^ sign ^ implode (replicate zs "0") ^ num
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    88
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    89
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    90
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    91
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    92
(* translation of integer constant tokens to and from binary *)
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
fun int_tr (*"_Int"*) [t as Free (str, _)] =
21781
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 18708
diff changeset
    95
      Syntax.const "integ_of" $ mk_bin (#value (Syntax.read_xnum str))
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    96
  | int_tr (*"_Int"*) ts = raise TERM ("int_tr", ts);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    97
10917
1044648b3f84 use Syntax.read_xnum;
wenzelm
parents: 9570
diff changeset
    98
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
    99
  | int_tr' (_:bool) (_:typ)     _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   100
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   101
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   102
val setup =
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 15965
diff changeset
   103
 (Theory.add_trfuns ([], [("_Int", int_tr)], [], []) #>
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 15965
diff changeset
   104
  Theory.add_trfunsT [("integ_of", int_tr'), ("Bin.integ_of", int_tr')]);
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   105
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   106
end;