src/ZF/Tools/numeral_syntax.ML
author berghofe
Mon, 19 Nov 2001 17:40:45 +0100
changeset 12238 09966ccbc84c
parent 10917 1044648b3f84
child 15965 f422f8283491
permissions -rw-r--r--
Improved error message.
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
(* 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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    26
fun mk_bit 0 = zero
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    45
fun mk_bin i =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    46
  let fun bin_of 0  = []
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    47
	| bin_of ~1 = [~1]
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    48
	| bin_of n  = (n mod 2) :: bin_of (n div 2);
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    51
	| term_of [~1] = min_const
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    52
	| term_of (b :: bs) = bit_const $ term_of bs $ mk_bit b;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    53
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    54
    term_of (bin_of i)
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
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    69
fun integ_of [] = 0
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    70
  | integ_of (b :: bs) = b + 2 * integ_of bs;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    71
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    72
val dest_bin = integ_of o bin_of;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    73
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    74
(*leading 0s and (for negative numbers) -1s cause complications,
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    75
  though they should never arise in normal use.*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    76
fun show_int t =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    77
  let
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    78
    val rev_digs = bin_of t;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    79
    val (sign, zs) =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    80
	(case rev rev_digs of
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    81
	     ~1 :: bs => ("-", prefix_len (equal 1) bs)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    82
	   | bs =>       ("",  prefix_len (equal 0) bs));
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    83
    val num = string_of_int (abs (integ_of rev_digs));
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    84
  in
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    85
    "#" ^ sign ^ implode (replicate zs "0") ^ num
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    86
  end;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    87
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    88
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
(* translation of integer constant tokens to and from binary *)
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
fun int_tr (*"_Int"*) [t as Free (str, _)] =
10917
1044648b3f84 use Syntax.read_xnum;
wenzelm
parents: 9570
diff changeset
    93
      Syntax.const "integ_of" $ mk_bin (Syntax.read_xnum str)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    94
  | int_tr (*"_Int"*) ts = raise TERM ("int_tr", ts);
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    95
10917
1044648b3f84 use Syntax.read_xnum;
wenzelm
parents: 9570
diff changeset
    96
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
    97
  | int_tr' (_:bool) (_:typ)     _ = raise Match;
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
    98
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
val setup =
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   101
 [Theory.add_trfuns ([], [("_Int", int_tr)], [], []),
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents:
diff changeset
   102
  Theory.add_trfunsT [("integ_of", int_tr'), ("Bin.integ_of", int_tr')]];
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
end;