author | wenzelm |
Wed, 31 Dec 2008 18:53:16 +0100 | |
changeset 29270 | 0eade173f77e |
parent 26086 | 3c243098b64a |
child 29316 | 0a7fcdd77f4b |
permissions | -rw-r--r-- |
6905 | 1 |
(* Title: HOL/Tools/numeral_syntax.ML |
2 |
ID: $Id$ |
|
20067
26bac504ef90
hex and binary numerals (contributed by Rafal Kolanski)
kleing
parents:
19481
diff
changeset
|
3 |
Authors: Markus Wenzel, TU Muenchen |
6905 | 4 |
|
21780 | 5 |
Concrete syntax for generic numerals -- preserves leading zeros/ones. |
6905 | 6 |
*) |
7 |
||
8 |
signature NUMERAL_SYNTAX = |
|
9 |
sig |
|
18708 | 10 |
val setup: theory -> theory |
6905 | 11 |
end; |
12 |
||
13 |
structure NumeralSyntax: NUMERAL_SYNTAX = |
|
14 |
struct |
|
15 |
||
21780 | 16 |
(* parse translation *) |
17 |
||
18 |
local |
|
19 |
||
20 |
fun mk_bin num = |
|
21 |
let |
|
22 |
val {leading_zeros = z, value, ...} = Syntax.read_xnum num; |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
23 |
fun bit b bs = HOLogic.mk_bit b $ bs; |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
24 |
fun mk 0 = (* FIXME funpow z (bit 0) *) (Syntax.const @{const_name Int.Pls}) |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
25 |
| mk ~1 = (* FIXME funpow z (bit 1) *) (Syntax.const @{const_name Int.Min}) |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
22997
diff
changeset
|
26 |
| mk i = let val (q, r) = Integer.div_mod i 2 in bit r (mk q) end; |
21780 | 27 |
in mk value end; |
28 |
||
29 |
in |
|
30 |
||
31 |
fun numeral_tr (*"_Numeral"*) [t as Const (num, _)] = |
|
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
32 |
Syntax.const @{const_name Int.number_of} $ mk_bin num |
21780 | 33 |
| numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts); |
34 |
||
35 |
end; |
|
6905 | 36 |
|
21780 | 37 |
|
38 |
(* print translation *) |
|
39 |
||
40 |
local |
|
41 |
||
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
42 |
fun dest_bin (Const (@{const_syntax "Int.Pls"}, _)) = [] |
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
43 |
| dest_bin (Const (@{const_syntax "Int.Min"}, _)) = [~1] |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
44 |
| dest_bin (Const (@{const_syntax "Int.Bit0"}, _) $ bs) = 0 :: dest_bin bs |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
25919
diff
changeset
|
45 |
| dest_bin (Const (@{const_syntax "Int.Bit1"}, _) $ bs) = 1 :: dest_bin bs |
21780 | 46 |
| dest_bin _ = raise Match; |
47 |
||
48 |
fun leading _ [] = 0 |
|
49 |
| leading (i: int) (j :: js) = if i = j then 1 + leading i js else 0; |
|
50 |
||
51 |
fun int_of [] = 0 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
22997
diff
changeset
|
52 |
| int_of (b :: bs) = b + 2 * int_of bs; |
6905 | 53 |
|
54 |
fun dest_bin_str tm = |
|
55 |
let |
|
21780 | 56 |
val rev_digs = dest_bin tm; |
57 |
val (sign, z) = |
|
6905 | 58 |
(case rev rev_digs of |
21780 | 59 |
~1 :: bs => ("-", leading 1 bs) |
60 |
| bs => ("", leading 0 bs)); |
|
61 |
val i = int_of rev_digs; |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
22997
diff
changeset
|
62 |
val num = string_of_int (abs i); |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11488
diff
changeset
|
63 |
in |
21780 | 64 |
if (i = 0 orelse i = 1) andalso z = 0 then raise Match |
65 |
else sign ^ implode (replicate z "0") ^ num |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11488
diff
changeset
|
66 |
end; |
6905 | 67 |
|
21780 | 68 |
in |
6905 | 69 |
|
70 |
fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) = |
|
11488
4ff900551340
constify numeral tokens in order to allow translations;
wenzelm
parents:
10891
diff
changeset
|
71 |
let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in |
7429 | 72 |
if not (! show_types) andalso can Term.dest_Type T then t' |
6905 | 73 |
else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T |
74 |
end |
|
13110 | 75 |
| numeral_tr' _ (*"number_of"*) T (t :: ts) = |
76 |
if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) |
|
77 |
else raise Match |
|
6905 | 78 |
| numeral_tr' _ (*"number_of"*) _ _ = raise Match; |
79 |
||
21780 | 80 |
end; |
81 |
||
6905 | 82 |
|
83 |
(* theory setup *) |
|
84 |
||
85 |
val setup = |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24630
diff
changeset
|
86 |
Sign.add_trfuns ([], [("_Numeral", numeral_tr)], [], []) #> |
25919
8b1c0d434824
joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents:
24712
diff
changeset
|
87 |
Sign.add_trfunsT [(@{const_syntax Int.number_of}, numeral_tr')]; |
6905 | 88 |
|
89 |
end; |