src/ZF/Tools/numeral_syntax.ML
author paulson
Thu, 10 Aug 2000 11:27:34 +0200
changeset 9570 e16e168984e1
child 10917 1044648b3f84
permissions -rw-r--r--
installation of cancellation simprocs for the integers
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
     6
theory ZF/Integ/Bin.
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    11
  val dest_bin : term -> int
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    12
  val mk_bin   : int -> term
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    15
  val setup    : (theory -> theory) list
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
open Syntax;
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
(* Bits *)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    24
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    25
val zero = Const("0", iT);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    26
val succ = Const("succ", iT --> iT);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    27
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    28
fun mk_bit 0 = zero
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    29
  | mk_bit 1 = succ$zero
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    30
  | mk_bit _ = sys_error "mk_bit";
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    31
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    32
fun dest_bit (Const ("0", _)) = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    33
  | dest_bit (Const ("succ", _) $ Const ("0", _)) = 1
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    34
  | dest_bit _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    35
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
(* Bit strings *)   (*we try to handle superfluous leading digits nicely*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    38
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    39
fun prefix_len _ [] = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    40
  | prefix_len pred (x :: xs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    41
      if pred x then 1 + prefix_len pred xs else 0;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    42
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    43
fun scan_int str =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    44
  let val (sign, digs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    45
       (case Symbol.explode str of
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    46
	 "#" :: "-" :: cs => (~1, cs)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    47
       | "#" :: cs => (1, cs)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    48
       | _ => raise ERROR);
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
  in  sign * (#1 (read_int digs))  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    51
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    52
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    53
val pls_const = Const ("Bin.bin.Pls", iT)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    54
and min_const = Const ("Bin.bin.Min", iT)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    55
and bit_const = Const ("Bin.bin.Bit", [iT, iT] ---> iT);
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
fun mk_bin i =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    58
  let fun bin_of 0  = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    59
	| bin_of ~1 = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    60
	| bin_of n  = (n mod 2) :: bin_of (n div 2);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    61
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    62
      fun term_of []   = pls_const
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    63
	| term_of [~1] = min_const
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    64
	| term_of (b :: bs) = bit_const $ term_of bs $ mk_bit b;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    65
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    66
    term_of (bin_of i)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    67
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    68
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    69
(*we consider all "spellings", since they could vary depending on the caller*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    70
fun bin_of (Const ("Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    71
  | bin_of (Const ("bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    72
  | bin_of (Const ("Bin.bin.Pls", _)) = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    73
  | bin_of (Const ("Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    74
  | bin_of (Const ("bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    75
  | bin_of (Const ("Bin.bin.Min", _)) = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    76
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    77
  | 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
    78
  | 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
    79
  | bin_of _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    80
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    81
fun integ_of [] = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    82
  | integ_of (b :: bs) = b + 2 * integ_of bs;
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
val dest_bin = integ_of o bin_of;
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
(*leading 0s and (for negative numbers) -1s cause complications,
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    87
  though they should never arise in normal use.*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    88
fun show_int t =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    89
  let
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    90
    val rev_digs = bin_of t;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    91
    val (sign, zs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    92
	(case rev rev_digs of
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    93
	     ~1 :: bs => ("-", prefix_len (equal 1) bs)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    94
	   | bs =>       ("",  prefix_len (equal 0) bs));
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    95
    val num = string_of_int (abs (integ_of rev_digs));
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    96
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    97
    "#" ^ sign ^ implode (replicate zs "0") ^ num
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    98
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    99
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
(* translation of integer constant tokens to and from binary *)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   103
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   104
fun int_tr (*"_Int"*) [t as Free (str, _)] =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   105
      (const "integ_of" $
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   106
	(mk_bin (scan_int str) handle ERROR => raise TERM ("int_tr", [t])))
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   107
  | int_tr (*"_Int"*) ts = raise TERM ("int_tr", ts);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   108
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   109
fun int_tr' _ _ (*"integ_of"*) [t] = const "_Int" $ free (show_int t)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   110
  | int_tr' (_:bool) (_:typ)     _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   111
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   112
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   113
val setup =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   114
 [Theory.add_trfuns ([], [("_Int", int_tr)], [], []),
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   115
  Theory.add_trfunsT [("integ_of", int_tr'), ("Bin.integ_of", int_tr')]];
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   116
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   117
end;