equal
deleted
inserted
replaced
1 (* Title: HOL/Tools/numeral_syntax.ML |
1 (* Title: HOL/Tools/numeral_syntax.ML |
2 ID: $Id$ |
|
3 Authors: Markus Wenzel, TU Muenchen |
2 Authors: Markus Wenzel, TU Muenchen |
4 |
3 |
5 Concrete syntax for generic numerals -- preserves leading zeros/ones. |
4 Concrete syntax for generic numerals -- preserves leading zeros/ones. |
6 *) |
5 *) |
7 |
6 |
17 |
16 |
18 local |
17 local |
19 |
18 |
20 fun mk_bin num = |
19 fun mk_bin num = |
21 let |
20 let |
22 val {leading_zeros = z, value, ...} = Syntax.read_xnum num; |
|
23 fun bit b bs = HOLogic.mk_bit b $ bs; |
21 fun bit b bs = HOLogic.mk_bit b $ bs; |
24 fun mk 0 = (* FIXME funpow z (bit 0) *) (Syntax.const @{const_name Int.Pls}) |
22 fun mk 0 = Syntax.const @{const_name Int.Pls} |
25 | mk ~1 = (* FIXME funpow z (bit 1) *) (Syntax.const @{const_name Int.Min}) |
23 | mk ~1 = Syntax.const @{const_name Int.Min} |
26 | mk i = let val (q, r) = Integer.div_mod i 2 in bit r (mk q) end; |
24 | mk i = let val (q, r) = Integer.div_mod i 2 in bit r (mk q) end; |
27 in mk value end; |
25 in mk (#value (Syntax.read_xnum num)) end; |
28 |
26 |
29 in |
27 in |
30 |
28 |
31 fun numeral_tr (*"_Numeral"*) [t as Const (num, _)] = |
29 fun numeral_tr (*"_Numeral"*) [t as Const (num, _)] = |
32 Syntax.const @{const_name Int.number_of} $ mk_bin num |
30 Syntax.const @{const_name Int.number_of} $ mk_bin num |
63 in |
61 in |
64 if (i = 0 orelse i = 1) andalso z = 0 then raise Match |
62 if (i = 0 orelse i = 1) andalso z = 0 then raise Match |
65 else sign ^ implode (replicate z "0") ^ num |
63 else sign ^ implode (replicate z "0") ^ num |
66 end; |
64 end; |
67 |
65 |
|
66 fun syntax_numeral t = |
|
67 Syntax.const "_Numeral" $ (Syntax.const "_numeral" $ Syntax.free (dest_bin_str t)); |
|
68 |
68 in |
69 in |
69 |
70 |
70 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) = |
71 fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) = |
71 let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in |
72 let val t' = |
72 if not (! show_types) andalso can Term.dest_Type T then t' |
73 if not (! show_types) andalso can Term.dest_Type T then syntax_numeral t |
73 else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T |
74 else Syntax.const Syntax.constrainC $ syntax_numeral t $ Syntax.term_of_typ show_sorts T |
74 end |
75 in list_comb (t', ts) end |
75 | numeral_tr' _ (*"number_of"*) T (t :: ts) = |
76 | numeral_tr' _ (*"number_of"*) T (t :: ts) = |
76 if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) |
77 if T = dummyT then list_comb (syntax_numeral t, ts) |
77 else raise Match |
78 else raise Match |
78 | numeral_tr' _ (*"number_of"*) _ _ = raise Match; |
79 | numeral_tr' _ (*"number_of"*) _ _ = raise Match; |
79 |
80 |
80 end; |
81 end; |
81 |
82 |