6 *) |
6 *) |
7 |
7 |
8 theory Numeral = Datatype |
8 theory Numeral = Datatype |
9 files "Tools/numeral_syntax.ML": |
9 files "Tools/numeral_syntax.ML": |
10 |
10 |
11 (* The constructors Pls/Min are hidden in numeral_syntax.ML. |
11 text{* The file @{text numeral_syntax.ML} hides the constructors Pls and Min. |
12 Only qualified access bin.Pls/Min is allowed. |
12 Only qualified access bin.Pls and bin.Min is allowed. |
13 Should also hide Bit, but that means one cannot use BIT anymore. |
13 We do not hide Bit because we need the BIT infix syntax.*} |
14 *) |
14 |
|
15 text{*A number can have multiple representations, namely leading Falses with |
|
16 sign @{term Pls} and leading Trues with sign @{term Min}. |
|
17 See @{text "ZF/Integ/twos-compl.ML"}, function @{text int_of_binary}, |
|
18 for the numerical interpretation. |
|
19 |
|
20 The representation expects that @{text "(m mod 2)"} is 0 or 1, |
|
21 even if m is negative; |
|
22 For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus |
|
23 @{text "-5 = (-3)*2 + 1"}. |
|
24 *} |
15 |
25 |
16 datatype |
26 datatype |
17 bin = Pls |
27 bin = Pls --{*Plus: Stands for an infinite string of leading Falses*} |
18 | Min |
28 | Min --{*Minus: Stands for an infinite string of leading Trues*} |
19 | Bit bin bool (infixl "BIT" 90) |
29 | Bit bin bool (infixl "BIT" 90) |
20 |
30 |
21 axclass |
31 axclass |
22 number < type -- {* for numeric types: nat, int, real, \dots *} |
32 number < type -- {* for numeric types: nat, int, real, \dots *} |
23 |
33 |
56 |
66 |
57 lemma Let_1 [simp]: "Let 1 f == f 1" |
67 lemma Let_1 [simp]: "Let 1 f == f 1" |
58 by (simp add: Let_def) |
68 by (simp add: Let_def) |
59 |
69 |
60 |
70 |
|
71 consts |
|
72 ring_of :: "bin => 'a::ring" |
|
73 |
|
74 NCons :: "[bin,bool]=>bin" |
|
75 bin_succ :: "bin=>bin" |
|
76 bin_pred :: "bin=>bin" |
|
77 bin_minus :: "bin=>bin" |
|
78 bin_add :: "[bin,bin]=>bin" |
|
79 bin_mult :: "[bin,bin]=>bin" |
|
80 |
|
81 text{*@{term NCons} inserts a bit, suppressing leading 0s and 1s*} |
|
82 primrec |
|
83 NCons_Pls: "NCons bin.Pls b = (if b then (bin.Pls BIT b) else bin.Pls)" |
|
84 NCons_Min: "NCons bin.Min b = (if b then bin.Min else (bin.Min BIT b))" |
|
85 NCons_BIT: "NCons (w BIT x) b = (w BIT x) BIT b" |
|
86 |
|
87 |
|
88 primrec |
|
89 ring_of_Pls: "ring_of bin.Pls = 0" |
|
90 ring_of_Min: "ring_of bin.Min = - (1::'a::ring)" |
|
91 ring_of_BIT: "ring_of(w BIT x) = (if x then 1 else 0) + |
|
92 (ring_of w) + (ring_of w)" |
|
93 |
|
94 primrec |
|
95 bin_succ_Pls: "bin_succ bin.Pls = bin.Pls BIT True" |
|
96 bin_succ_Min: "bin_succ bin.Min = bin.Pls" |
|
97 bin_succ_BIT: "bin_succ(w BIT x) = |
|
98 (if x then bin_succ w BIT False |
|
99 else NCons w True)" |
|
100 |
|
101 primrec |
|
102 bin_pred_Pls: "bin_pred bin.Pls = bin.Min" |
|
103 bin_pred_Min: "bin_pred bin.Min = bin.Min BIT False" |
|
104 bin_pred_BIT: "bin_pred(w BIT x) = |
|
105 (if x then NCons w False |
|
106 else (bin_pred w) BIT True)" |
|
107 |
|
108 primrec |
|
109 bin_minus_Pls: "bin_minus bin.Pls = bin.Pls" |
|
110 bin_minus_Min: "bin_minus bin.Min = bin.Pls BIT True" |
|
111 bin_minus_BIT: "bin_minus(w BIT x) = |
|
112 (if x then bin_pred (NCons (bin_minus w) False) |
|
113 else bin_minus w BIT False)" |
|
114 |
|
115 primrec |
|
116 bin_add_Pls: "bin_add bin.Pls w = w" |
|
117 bin_add_Min: "bin_add bin.Min w = bin_pred w" |
|
118 bin_add_BIT: |
|
119 "bin_add (v BIT x) w = |
|
120 (case w of Pls => v BIT x |
|
121 | Min => bin_pred (v BIT x) |
|
122 | (w BIT y) => |
|
123 NCons (bin_add v (if (x & y) then bin_succ w else w)) |
|
124 (x~=y))" |
|
125 |
|
126 primrec |
|
127 bin_mult_Pls: "bin_mult bin.Pls w = bin.Pls" |
|
128 bin_mult_Min: "bin_mult bin.Min w = bin_minus w" |
|
129 bin_mult_BIT: "bin_mult (v BIT x) w = |
|
130 (if x then (bin_add (NCons (bin_mult v w) False) w) |
|
131 else (NCons (bin_mult v w) False))" |
|
132 |
|
133 |
|
134 subsection{*Extra rules for @{term bin_succ}, @{term bin_pred}, |
|
135 @{term bin_add} and @{term bin_mult}*} |
|
136 |
|
137 lemma NCons_Pls_0: "NCons bin.Pls False = bin.Pls" |
|
138 by simp |
|
139 |
|
140 lemma NCons_Pls_1: "NCons bin.Pls True = bin.Pls BIT True" |
|
141 by simp |
|
142 |
|
143 lemma NCons_Min_0: "NCons bin.Min False = bin.Min BIT False" |
|
144 by simp |
|
145 |
|
146 lemma NCons_Min_1: "NCons bin.Min True = bin.Min" |
|
147 by simp |
|
148 |
|
149 lemma bin_succ_1: "bin_succ(w BIT True) = (bin_succ w) BIT False" |
|
150 by simp |
|
151 |
|
152 lemma bin_succ_0: "bin_succ(w BIT False) = NCons w True" |
|
153 by simp |
|
154 |
|
155 lemma bin_pred_1: "bin_pred(w BIT True) = NCons w False" |
|
156 by simp |
|
157 |
|
158 lemma bin_pred_0: "bin_pred(w BIT False) = (bin_pred w) BIT True" |
|
159 by simp |
|
160 |
|
161 lemma bin_minus_1: "bin_minus(w BIT True) = bin_pred (NCons (bin_minus w) False)" |
|
162 by simp |
|
163 |
|
164 lemma bin_minus_0: "bin_minus(w BIT False) = (bin_minus w) BIT False" |
|
165 by simp |
|
166 |
|
167 |
|
168 subsection{*Binary Addition and Multiplication: |
|
169 @{term bin_add} and @{term bin_mult}*} |
|
170 |
|
171 lemma bin_add_BIT_11: |
|
172 "bin_add (v BIT True) (w BIT True) = |
|
173 NCons (bin_add v (bin_succ w)) False" |
|
174 by simp |
|
175 |
|
176 lemma bin_add_BIT_10: |
|
177 "bin_add (v BIT True) (w BIT False) = NCons (bin_add v w) True" |
|
178 by simp |
|
179 |
|
180 lemma bin_add_BIT_0: |
|
181 "bin_add (v BIT False) (w BIT y) = NCons (bin_add v w) y" |
|
182 by auto |
|
183 |
|
184 lemma bin_add_Pls_right: "bin_add w bin.Pls = w" |
|
185 by (induct_tac "w", auto) |
|
186 |
|
187 lemma bin_add_Min_right: "bin_add w bin.Min = bin_pred w" |
|
188 by (induct_tac "w", auto) |
|
189 |
|
190 lemma bin_add_BIT_BIT: |
|
191 "bin_add (v BIT x) (w BIT y) = |
|
192 NCons(bin_add v (if x & y then (bin_succ w) else w)) (x~= y)" |
|
193 by simp |
|
194 |
|
195 lemma bin_mult_1: |
|
196 "bin_mult (v BIT True) w = bin_add (NCons (bin_mult v w) False) w" |
|
197 by simp |
|
198 |
|
199 lemma bin_mult_0: "bin_mult (v BIT False) w = NCons (bin_mult v w) False" |
|
200 by simp |
|
201 |
|
202 |
61 end |
203 end |