9570
|
1 |
(* Title: ZF/Tools/numeral_syntax.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
|
|
5 |
Concrete syntax for generic numerals. Assumes consts and syntax of
|
|
6 |
theory ZF/Integ/Bin.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature NUMERAL_SYNTAX =
|
|
10 |
sig
|
|
11 |
val dest_bin : term -> int
|
|
12 |
val mk_bin : int -> term
|
|
13 |
val int_tr : term list -> term
|
|
14 |
val int_tr' : bool -> typ -> term list -> term
|
|
15 |
val setup : (theory -> theory) list
|
|
16 |
end;
|
|
17 |
|
|
18 |
structure NumeralSyntax: NUMERAL_SYNTAX =
|
|
19 |
struct
|
|
20 |
|
|
21 |
open Syntax;
|
|
22 |
|
|
23 |
(* Bits *)
|
|
24 |
|
|
25 |
val zero = Const("0", iT);
|
|
26 |
val succ = Const("succ", iT --> iT);
|
|
27 |
|
|
28 |
fun mk_bit 0 = zero
|
|
29 |
| mk_bit 1 = succ$zero
|
|
30 |
| mk_bit _ = sys_error "mk_bit";
|
|
31 |
|
|
32 |
fun dest_bit (Const ("0", _)) = 0
|
|
33 |
| dest_bit (Const ("succ", _) $ Const ("0", _)) = 1
|
|
34 |
| dest_bit _ = raise Match;
|
|
35 |
|
|
36 |
|
|
37 |
(* Bit strings *) (*we try to handle superfluous leading digits nicely*)
|
|
38 |
|
|
39 |
fun prefix_len _ [] = 0
|
|
40 |
| prefix_len pred (x :: xs) =
|
|
41 |
if pred x then 1 + prefix_len pred xs else 0;
|
|
42 |
|
|
43 |
fun scan_int str =
|
|
44 |
let val (sign, digs) =
|
|
45 |
(case Symbol.explode str of
|
|
46 |
"#" :: "-" :: cs => (~1, cs)
|
|
47 |
| "#" :: cs => (1, cs)
|
|
48 |
| _ => raise ERROR);
|
|
49 |
|
|
50 |
in sign * (#1 (read_int digs)) end;
|
|
51 |
|
|
52 |
|
|
53 |
val pls_const = Const ("Bin.bin.Pls", iT)
|
|
54 |
and min_const = Const ("Bin.bin.Min", iT)
|
|
55 |
and bit_const = Const ("Bin.bin.Bit", [iT, iT] ---> iT);
|
|
56 |
|
|
57 |
fun mk_bin i =
|
|
58 |
let fun bin_of 0 = []
|
|
59 |
| bin_of ~1 = [~1]
|
|
60 |
| bin_of n = (n mod 2) :: bin_of (n div 2);
|
|
61 |
|
|
62 |
fun term_of [] = pls_const
|
|
63 |
| term_of [~1] = min_const
|
|
64 |
| term_of (b :: bs) = bit_const $ term_of bs $ mk_bit b;
|
|
65 |
in
|
|
66 |
term_of (bin_of i)
|
|
67 |
end;
|
|
68 |
|
|
69 |
(*we consider all "spellings", since they could vary depending on the caller*)
|
|
70 |
fun bin_of (Const ("Pls", _)) = []
|
|
71 |
| bin_of (Const ("bin.Pls", _)) = []
|
|
72 |
| bin_of (Const ("Bin.bin.Pls", _)) = []
|
|
73 |
| bin_of (Const ("Min", _)) = [~1]
|
|
74 |
| bin_of (Const ("bin.Min", _)) = [~1]
|
|
75 |
| bin_of (Const ("Bin.bin.Min", _)) = [~1]
|
|
76 |
| bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
|
|
77 |
| bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
|
|
78 |
| bin_of (Const ("Bin.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
|
|
79 |
| bin_of _ = raise Match;
|
|
80 |
|
|
81 |
fun integ_of [] = 0
|
|
82 |
| integ_of (b :: bs) = b + 2 * integ_of bs;
|
|
83 |
|
|
84 |
val dest_bin = integ_of o bin_of;
|
|
85 |
|
|
86 |
(*leading 0s and (for negative numbers) -1s cause complications,
|
|
87 |
though they should never arise in normal use.*)
|
|
88 |
fun show_int t =
|
|
89 |
let
|
|
90 |
val rev_digs = bin_of t;
|
|
91 |
val (sign, zs) =
|
|
92 |
(case rev rev_digs of
|
|
93 |
~1 :: bs => ("-", prefix_len (equal 1) bs)
|
|
94 |
| bs => ("", prefix_len (equal 0) bs));
|
|
95 |
val num = string_of_int (abs (integ_of rev_digs));
|
|
96 |
in
|
|
97 |
"#" ^ sign ^ implode (replicate zs "0") ^ num
|
|
98 |
end;
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
(* translation of integer constant tokens to and from binary *)
|
|
103 |
|
|
104 |
fun int_tr (*"_Int"*) [t as Free (str, _)] =
|
|
105 |
(const "integ_of" $
|
|
106 |
(mk_bin (scan_int str) handle ERROR => raise TERM ("int_tr", [t])))
|
|
107 |
| int_tr (*"_Int"*) ts = raise TERM ("int_tr", ts);
|
|
108 |
|
|
109 |
fun int_tr' _ _ (*"integ_of"*) [t] = const "_Int" $ free (show_int t)
|
|
110 |
| int_tr' (_:bool) (_:typ) _ = raise Match;
|
|
111 |
|
|
112 |
|
|
113 |
val setup =
|
|
114 |
[Theory.add_trfuns ([], [("_Int", int_tr)], [], []),
|
|
115 |
Theory.add_trfunsT [("integ_of", int_tr'), ("Bin.integ_of", int_tr')]];
|
|
116 |
|
|
117 |
end;
|