author  huffman 
Mon, 16 Feb 2009 13:08:21 0800  
changeset 29943  922b931fd2eb 
parent 29942  31069b8d39df 
child 29944  ca43d393c2f1 
permissions  rwrr 
28021  1 
(* Title: HOL/ex/Numeral.thy 
2 
ID: $Id$ 

3 
Author: Florian Haftmann 

4 

5 
An experimental alternative numeral representation. 

6 
*) 

7 

8 
theory Numeral 

9 
imports Int Inductive 

10 
begin 

11 

12 
subsection {* The @{text num} type *} 

13 

29943  14 
datatype num = One  Dig0 num  Dig1 num 
15 

16 
text {* Increment function for type @{typ num} *} 

17 

18 
primrec 

19 
inc :: "num \<Rightarrow> num" 

20 
where 

21 
"inc One = Dig0 One" 

22 
 "inc (Dig0 x) = Dig1 x" 

23 
 "inc (Dig1 x) = Dig0 (inc x)" 

24 

25 
text {* Converting between type @{typ num} and type @{typ nat} *} 

26 

27 
primrec 

28 
nat_of_num :: "num \<Rightarrow> nat" 

29 
where 

30 
"nat_of_num One = Suc 0" 

31 
 "nat_of_num (Dig0 x) = nat_of_num x + nat_of_num x" 

32 
 "nat_of_num (Dig1 x) = Suc (nat_of_num x + nat_of_num x)" 

33 

34 
primrec 

35 
num_of_nat :: "nat \<Rightarrow> num" 

36 
where 

37 
"num_of_nat 0 = One" 

38 
 "num_of_nat (Suc n) = (if 0 < n then inc (num_of_nat n) else One)" 

39 

40 
lemma nat_of_num_gt_0: "0 < nat_of_num x" 

41 
by (induct x) simp_all 

42 

43 
lemma nat_of_num_neq_0: " nat_of_num x \<noteq> 0" 

44 
by (induct x) simp_all 

45 

46 
lemma nat_of_num_inc: "nat_of_num (inc x) = Suc (nat_of_num x)" 

47 
by (induct x) simp_all 

48 

49 
lemma num_of_nat_double: 

50 
"0 < n \<Longrightarrow> num_of_nat (n + n) = Dig0 (num_of_nat n)" 

51 
by (induct n) simp_all 

52 

28021  53 
text {* 
29943  54 
Type @{typ num} is isomorphic to the strictly positive 
28021  55 
natural numbers. 
56 
*} 

57 

29943  58 
lemma nat_of_num_inverse: "num_of_nat (nat_of_num x) = x" 
59 
by (induct x) (simp_all add: num_of_nat_double nat_of_num_gt_0) 

28021  60 

29943  61 
lemma num_of_nat_inverse: "0 < n \<Longrightarrow> nat_of_num (num_of_nat n) = n" 
62 
by (induct n) (simp_all add: nat_of_num_inc) 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

63 

31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

64 
lemma num_eq_iff: "x = y \<longleftrightarrow> nat_of_num x = nat_of_num y" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

65 
apply safe 
29943  66 
apply (drule arg_cong [where f=num_of_nat]) 
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

67 
apply (simp add: nat_of_num_inverse) 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

68 
done 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

69 

31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

70 
instantiation num :: "semiring" 
28021  71 
begin 
72 

73 
definition plus_num :: "num \<Rightarrow> num \<Rightarrow> num" where 

28562  74 
[code del]: "m + n = num_of_nat (nat_of_num m + nat_of_num n)" 
28021  75 

76 
definition times_num :: "num \<Rightarrow> num \<Rightarrow> num" where 

28562  77 
[code del]: "m * n = num_of_nat (nat_of_num m * nat_of_num n)" 
28021  78 

29943  79 
lemma nat_of_num_add: "nat_of_num (x + y) = nat_of_num x + nat_of_num y" 
80 
unfolding plus_num_def 

81 
by (intro num_of_nat_inverse add_pos_pos nat_of_num_gt_0) 

28021  82 

29943  83 
lemma nat_of_num_mult: "nat_of_num (x * y) = nat_of_num x * nat_of_num y" 
84 
unfolding times_num_def 

85 
by (intro num_of_nat_inverse mult_pos_pos nat_of_num_gt_0) 

28021  86 

87 
instance proof 

29943  88 
qed (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult nat_distrib) 
28021  89 

90 
end 

91 

29943  92 
lemma num_induct [case_names One inc]: 
93 
fixes P :: "num \<Rightarrow> bool" 

94 
assumes One: "P One" 

95 
and inc: "\<And>x. P x \<Longrightarrow> P (inc x)" 

96 
shows "P x" 

97 
proof  

98 
obtain n where n: "Suc n = nat_of_num x" 

99 
by (cases "nat_of_num x", simp_all add: nat_of_num_neq_0) 

100 
have "P (num_of_nat (Suc n))" 

101 
proof (induct n) 

102 
case 0 show ?case using One by simp 

28021  103 
next 
29943  104 
case (Suc n) 
105 
then have "P (inc (num_of_nat (Suc n)))" by (rule inc) 

106 
then show "P (num_of_nat (Suc (Suc n)))" by simp 

28021  107 
qed 
29943  108 
with n show "P x" 
109 
by (simp add: nat_of_num_inverse) 

28021  110 
qed 
111 

112 
text {* 

113 
From now on, there are two possible models for @{typ num}: 

29943  114 
as positive naturals (rule @{text "num_induct"}) 
28021  115 
and as digit representation (rules @{text "num.induct"}, @{text "num.cases"}). 
116 

117 
It is not entirely clear in which context it is better to use 

118 
the one or the other, or whether the construction should be reversed. 

119 
*} 

120 

121 

122 
subsection {* Binary numerals *} 

123 

124 
text {* 

125 
We embed binary representations into a generic algebraic 

126 
structure using @{text of_num} 

127 
*} 

128 

129 
ML {* 

130 
structure DigSimps = 

131 
NamedThmsFun(val name = "numeral"; val description = "Simplification rules for numerals") 

132 
*} 

133 

134 
setup DigSimps.setup 

135 

136 
class semiring_numeral = semiring + monoid_mult 

137 
begin 

138 

139 
primrec of_num :: "num \<Rightarrow> 'a" where 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

140 
of_num_one [numeral]: "of_num One = 1" 
28021  141 
 "of_num (Dig0 n) = of_num n + of_num n" 
142 
 "of_num (Dig1 n) = of_num n + of_num n + 1" 

143 

29943  144 
lemma of_num_inc: "of_num (inc x) = of_num x + 1" 
145 
by (induct x) (simp_all add: add_ac) 

146 

28021  147 
declare of_num.simps [simp del] 
148 

149 
end 

150 

151 
text {* 

152 
ML stuff and syntax. 

153 
*} 

154 

155 
ML {* 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

156 
fun mk_num 1 = @{term One} 
28021  157 
 mk_num k = 
158 
let 

159 
val (l, b) = Integer.div_mod k 2; 

160 
val bit = (if b = 0 then @{term Dig0} else @{term Dig1}); 

161 
in bit $ (mk_num l) end; 

162 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

163 
fun dest_num @{term One} = 1 
28021  164 
 dest_num (@{term Dig0} $ n) = 2 * dest_num n 
165 
 dest_num (@{term Dig1} $ n) = 2 * dest_num n + 1; 

166 

167 
(*FIXME these have to gain proper context via morphisms phi*) 

168 

169 
fun mk_numeral T k = Const (@{const_name of_num}, @{typ num} > T) 

170 
$ mk_num k 

171 

172 
fun dest_numeral (Const (@{const_name of_num}, Type ("fun", [@{typ num}, T])) $ t) = 

173 
(T, dest_num t) 

174 
*} 

175 

176 
syntax 

177 
"_Numerals" :: "xnum \<Rightarrow> 'a" ("_") 

178 

179 
parse_translation {* 

180 
let 

181 
fun num_of_int n = if n > 0 then case IntInf.quotRem (n, 2) 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

182 
of (0, 1) => Const (@{const_name One}, dummyT) 
28021  183 
 (n, 0) => Const (@{const_name Dig0}, dummyT) $ num_of_int n 
184 
 (n, 1) => Const (@{const_name Dig1}, dummyT) $ num_of_int n 

185 
else raise Match; 

186 
fun numeral_tr [Free (num, _)] = 

187 
let 

188 
val {leading_zeros, value, ...} = Syntax.read_xnum num; 

189 
val _ = leading_zeros = 0 andalso value > 0 

190 
orelse error ("Bad numeral: " ^ num); 

191 
in Const (@{const_name of_num}, @{typ num} > dummyT) $ num_of_int value end 

192 
 numeral_tr ts = raise TERM ("numeral_tr", ts); 

193 
in [("_Numerals", numeral_tr)] end 

194 
*} 

195 

196 
typed_print_translation {* 

197 
let 

198 
fun dig b n = b + 2 * n; 

199 
fun int_of_num' (Const (@{const_syntax Dig0}, _) $ n) = 

200 
dig 0 (int_of_num' n) 

201 
 int_of_num' (Const (@{const_syntax Dig1}, _) $ n) = 

202 
dig 1 (int_of_num' n) 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

203 
 int_of_num' (Const (@{const_syntax One}, _)) = 1; 
28021  204 
fun num_tr' show_sorts T [n] = 
205 
let 

206 
val k = int_of_num' n; 

207 
val t' = Syntax.const "_Numerals" $ Syntax.free ("#" ^ string_of_int k); 

208 
in case T 

209 
of Type ("fun", [_, T']) => 

210 
if not (! show_types) andalso can Term.dest_Type T' then t' 

211 
else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T' 

212 
 T' => if T' = dummyT then t' else raise Match 

213 
end; 

214 
in [(@{const_syntax of_num}, num_tr')] end 

215 
*} 

216 

217 

218 
subsection {* Numeral operations *} 

219 

220 
text {* 

221 
First, addition and multiplication on digits. 

222 
*} 

223 

224 
lemma Dig_plus [numeral, simp, code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

225 
"One + One = Dig0 One" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

226 
"One + Dig0 m = Dig1 m" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

227 
"One + Dig1 m = Dig0 (m + One)" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

228 
"Dig0 n + One = Dig1 n" 
28021  229 
"Dig0 n + Dig0 m = Dig0 (n + m)" 
230 
"Dig0 n + Dig1 m = Dig1 (n + m)" 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

231 
"Dig1 n + One = Dig0 (n + One)" 
28021  232 
"Dig1 n + Dig0 m = Dig1 (n + m)" 
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

233 
"Dig1 n + Dig1 m = Dig0 (n + m + One)" 
29943  234 
by (simp_all add: num_eq_iff nat_of_num_add) 
28021  235 

236 
lemma Dig_times [numeral, simp, code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

237 
"One * One = One" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

238 
"One * Dig0 n = Dig0 n" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

239 
"One * Dig1 n = Dig1 n" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

240 
"Dig0 n * One = Dig0 n" 
28021  241 
"Dig0 n * Dig0 m = Dig0 (n * Dig0 m)" 
242 
"Dig0 n * Dig1 m = Dig0 (n * Dig1 m)" 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

243 
"Dig1 n * One = Dig1 n" 
28021  244 
"Dig1 n * Dig0 m = Dig0 (n * Dig0 m + m)" 
245 
"Dig1 n * Dig1 m = Dig1 (n * Dig1 m + m)" 

29943  246 
by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult 
247 
left_distrib right_distrib) 

28021  248 

249 
text {* 

250 
@{const of_num} is a morphism. 

251 
*} 

252 

253 
context semiring_numeral 

254 
begin 

255 

29943  256 
abbreviation "Num1 \<equiv> of_num One" 
28021  257 

258 
text {* 

259 
Alas, there is still the duplication of @{term 1}, 

260 
thought the duplicated @{term 0} has disappeared. 

261 
We could get rid of it by replacing the constructor 

262 
@{term 1} in @{typ num} by two constructors 

263 
@{text two} and @{text three}, resulting in a further 

264 
blowup. But it could be worth the effort. 

265 
*} 

266 

267 
lemma of_num_plus_one [numeral]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

268 
"of_num n + 1 = of_num (n + One)" 
29943  269 
by (rule sym, induct n) (simp_all add: of_num.simps add_ac) 
28021  270 

271 
lemma of_num_one_plus [numeral]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

272 
"1 + of_num n = of_num (n + One)" 
28021  273 
unfolding of_num_plus_one [symmetric] add_commute .. 
274 

29943  275 
text {* Rules for addition in the One/inc view *} 
276 

277 
lemma add_One: "x + One = inc x" 

278 
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) 

279 

280 
lemma add_inc: "x + inc y = inc (x + y)" 

281 
by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc) 

282 

283 
text {* Rules for multiplication in the One/inc view *} 

284 

285 
lemma mult_One: "x * One = x" 

286 
by (simp add: num_eq_iff nat_of_num_mult) 

287 

288 
lemma mult_inc: "x * inc y = x * y + x" 

289 
by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc) 

290 

28021  291 
lemma of_num_plus [numeral]: 
292 
"of_num m + of_num n = of_num (m + n)" 

293 
by (induct n rule: num_induct) 

29943  294 
(simp_all add: add_One add_inc of_num_one of_num_inc add_ac) 
28021  295 

296 
lemma of_num_times_one [numeral]: 

297 
"of_num n * 1 = of_num n" 

298 
by simp 

299 

300 
lemma of_num_one_times [numeral]: 

301 
"1 * of_num n = of_num n" 

302 
by simp 

303 

304 
lemma of_num_times [numeral]: 

305 
"of_num m * of_num n = of_num (m * n)" 

306 
by (induct n rule: num_induct) 

29943  307 
(simp_all add: of_num_plus [symmetric] mult_One mult_inc 
308 
semiring_class.right_distrib right_distrib of_num_one of_num_inc) 

28021  309 

310 
end 

311 

312 
text {* 

313 
Structures with a @{term 0}. 

314 
*} 

315 

316 
context semiring_1 

317 
begin 

318 

319 
subclass semiring_numeral .. 

320 

321 
lemma of_nat_of_num [numeral]: "of_nat (of_num n) = of_num n" 

322 
by (induct n) 

323 
(simp_all add: semiring_numeral_class.of_num.simps of_num.simps add_ac) 

324 

325 
declare of_nat_1 [numeral] 

326 

327 
lemma Dig_plus_zero [numeral]: 

328 
"0 + 1 = 1" 

329 
"0 + of_num n = of_num n" 

330 
"1 + 0 = 1" 

331 
"of_num n + 0 = of_num n" 

332 
by simp_all 

333 

334 
lemma Dig_times_zero [numeral]: 

335 
"0 * 1 = 0" 

336 
"0 * of_num n = 0" 

337 
"1 * 0 = 0" 

338 
"of_num n * 0 = 0" 

339 
by simp_all 

340 

341 
end 

342 

343 
lemma nat_of_num_of_num: "nat_of_num = of_num" 

344 
proof 

345 
fix n 

29943  346 
have "of_num n = nat_of_num n" 
347 
by (induct n) (simp_all add: of_num.simps) 

28021  348 
then show "nat_of_num n = of_num n" by simp 
349 
qed 

350 

351 
text {* 

352 
Equality. 

353 
*} 

354 

355 
context semiring_char_0 

356 
begin 

357 

358 
lemma of_num_eq_iff [numeral]: 

359 
"of_num m = of_num n \<longleftrightarrow> m = n" 

360 
unfolding of_nat_of_num [symmetric] nat_of_num_of_num [symmetric] 

29943  361 
of_nat_eq_iff num_eq_iff .. 
28021  362 

363 
lemma of_num_eq_one_iff [numeral]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

364 
"of_num n = 1 \<longleftrightarrow> n = One" 
28021  365 
proof  
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

366 
have "of_num n = of_num One \<longleftrightarrow> n = One" unfolding of_num_eq_iff .. 
28021  367 
then show ?thesis by (simp add: of_num_one) 
368 
qed 

369 

370 
lemma one_eq_of_num_iff [numeral]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

371 
"1 = of_num n \<longleftrightarrow> n = One" 
28021  372 
unfolding of_num_eq_one_iff [symmetric] by auto 
373 

374 
end 

375 

376 
text {* 

377 
Comparisons. Could be perhaps more general than here. 

378 
*} 

379 

380 
lemma (in ordered_semidom) of_num_pos: "0 < of_num n" 

381 
proof  

382 
have "(0::nat) < of_num n" 

383 
by (induct n) (simp_all add: semiring_numeral_class.of_num.simps) 

384 
then have "of_nat 0 \<noteq> of_nat (of_num n)" 

385 
by (cases n) (simp_all only: semiring_numeral_class.of_num.simps of_nat_eq_iff) 

386 
then have "0 \<noteq> of_num n" 

387 
by (simp add: of_nat_of_num) 

388 
moreover have "0 \<le> of_nat (of_num n)" by simp 

389 
ultimately show ?thesis by (simp add: of_nat_of_num) 

390 
qed 

391 

392 
instantiation num :: linorder 

393 
begin 

394 

395 
definition less_eq_num :: "num \<Rightarrow> num \<Rightarrow> bool" where 

28562  396 
[code del]: "m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n" 
28021  397 

398 
definition less_num :: "num \<Rightarrow> num \<Rightarrow> bool" where 

28562  399 
[code del]: "m < n \<longleftrightarrow> nat_of_num m < nat_of_num n" 
28021  400 

401 
instance proof 

29943  402 
qed (auto simp add: less_eq_num_def less_num_def num_eq_iff) 
28021  403 

404 
end 

405 

406 
lemma less_eq_num_code [numeral, simp, code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

407 
"One \<le> n \<longleftrightarrow> True" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

408 
"Dig0 m \<le> One \<longleftrightarrow> False" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

409 
"Dig1 m \<le> One \<longleftrightarrow> False" 
28021  410 
"Dig0 m \<le> Dig0 n \<longleftrightarrow> m \<le> n" 
411 
"Dig0 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" 

412 
"Dig1 m \<le> Dig1 n \<longleftrightarrow> m \<le> n" 

413 
"Dig1 m \<le> Dig0 n \<longleftrightarrow> m < n" 

414 
using of_num_pos [of n, where ?'a = nat] of_num_pos [of m, where ?'a = nat] 

415 
by (auto simp add: less_eq_num_def less_num_def nat_of_num_of_num of_num.simps) 

416 

417 
lemma less_num_code [numeral, simp, code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

418 
"m < One \<longleftrightarrow> False" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

419 
"One < One \<longleftrightarrow> False" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

420 
"One < Dig0 n \<longleftrightarrow> True" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

421 
"One < Dig1 n \<longleftrightarrow> True" 
28021  422 
"Dig0 m < Dig0 n \<longleftrightarrow> m < n" 
423 
"Dig0 m < Dig1 n \<longleftrightarrow> m \<le> n" 

424 
"Dig1 m < Dig1 n \<longleftrightarrow> m < n" 

425 
"Dig1 m < Dig0 n \<longleftrightarrow> m < n" 

426 
using of_num_pos [of n, where ?'a = nat] of_num_pos [of m, where ?'a = nat] 

427 
by (auto simp add: less_eq_num_def less_num_def nat_of_num_of_num of_num.simps) 

428 

429 
context ordered_semidom 

430 
begin 

431 

432 
lemma of_num_less_eq_iff [numeral]: "of_num m \<le> of_num n \<longleftrightarrow> m \<le> n" 

433 
proof  

434 
have "of_nat (of_num m) \<le> of_nat (of_num n) \<longleftrightarrow> m \<le> n" 

435 
unfolding less_eq_num_def nat_of_num_of_num of_nat_le_iff .. 

436 
then show ?thesis by (simp add: of_nat_of_num) 

437 
qed 

438 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

439 
lemma of_num_less_eq_one_iff [numeral]: "of_num n \<le> 1 \<longleftrightarrow> n = One" 
28021  440 
proof  
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

441 
have "of_num n \<le> of_num One \<longleftrightarrow> n = One" 
28021  442 
by (cases n) (simp_all add: of_num_less_eq_iff) 
443 
then show ?thesis by (simp add: of_num_one) 

444 
qed 

445 

446 
lemma one_less_eq_of_num_iff [numeral]: "1 \<le> of_num n" 

447 
proof  

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

448 
have "of_num One \<le> of_num n" 
28021  449 
by (cases n) (simp_all add: of_num_less_eq_iff) 
450 
then show ?thesis by (simp add: of_num_one) 

451 
qed 

452 

453 
lemma of_num_less_iff [numeral]: "of_num m < of_num n \<longleftrightarrow> m < n" 

454 
proof  

455 
have "of_nat (of_num m) < of_nat (of_num n) \<longleftrightarrow> m < n" 

456 
unfolding less_num_def nat_of_num_of_num of_nat_less_iff .. 

457 
then show ?thesis by (simp add: of_nat_of_num) 

458 
qed 

459 

460 
lemma of_num_less_one_iff [numeral]: "\<not> of_num n < 1" 

461 
proof  

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

462 
have "\<not> of_num n < of_num One" 
28021  463 
by (cases n) (simp_all add: of_num_less_iff) 
464 
then show ?thesis by (simp add: of_num_one) 

465 
qed 

466 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

467 
lemma one_less_of_num_iff [numeral]: "1 < of_num n \<longleftrightarrow> n \<noteq> One" 
28021  468 
proof  
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

469 
have "of_num One < of_num n \<longleftrightarrow> n \<noteq> One" 
28021  470 
by (cases n) (simp_all add: of_num_less_iff) 
471 
then show ?thesis by (simp add: of_num_one) 

472 
qed 

473 

474 
end 

475 

476 
text {* 

477 
Structures with subtraction @{term "op "}. 

478 
*} 

479 

29941  480 
text {* A doubleanddecrement function *} 
28021  481 

29941  482 
primrec DigM :: "num \<Rightarrow> num" where 
29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

483 
"DigM One = One" 
29941  484 
 "DigM (Dig0 n) = Dig1 (DigM n)" 
485 
 "DigM (Dig1 n) = Dig1 (Dig0 n)" 

28021  486 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

487 
lemma DigM_plus_one: "DigM n + One = Dig0 n" 
29941  488 
by (induct n) simp_all 
28021  489 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

490 
lemma one_plus_DigM: "One + DigM n = Dig0 n" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

491 
unfolding add_commute [of One] DigM_plus_one .. 
28021  492 

493 
class semiring_minus = semiring + minus + zero + 

494 
assumes minus_inverts_plus1: "a + b = c \<Longrightarrow> c  b = a" 

495 
assumes minus_minus_zero_inverts_plus1: "a + b = c \<Longrightarrow> b  c = 0  a" 

496 
begin 

497 

498 
lemma minus_inverts_plus2: "a + b = c \<Longrightarrow> c  a = b" 

499 
by (simp add: add_ac minus_inverts_plus1 [of b a]) 

500 

501 
lemma minus_minus_zero_inverts_plus2: "a + b = c \<Longrightarrow> a  c = 0  b" 

502 
by (simp add: add_ac minus_minus_zero_inverts_plus1 [of b a]) 

503 

504 
end 

505 

506 
class semiring_1_minus = semiring_1 + semiring_minus 

507 
begin 

508 

509 
lemma Dig_of_num_pos: 

510 
assumes "k + n = m" 

511 
shows "of_num m  of_num n = of_num k" 

512 
using assms by (simp add: of_num_plus minus_inverts_plus1) 

513 

514 
lemma Dig_of_num_zero: 

515 
shows "of_num n  of_num n = 0" 

516 
by (rule minus_inverts_plus1) simp 

517 

518 
lemma Dig_of_num_neg: 

519 
assumes "k + m = n" 

520 
shows "of_num m  of_num n = 0  of_num k" 

521 
by (rule minus_minus_zero_inverts_plus1) (simp add: of_num_plus assms) 

522 

523 
lemmas Dig_plus_eval = 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

524 
of_num_plus of_num_eq_iff Dig_plus refl [of One, THEN eqTrueI] num.inject 
28021  525 

526 
simproc_setup numeral_minus ("of_num m  of_num n") = {* 

527 
let 

528 
(*TODO proper implicit use of morphism via pattern antiquotations*) 

529 
fun cdest_of_num ct = (snd o split_last o snd o Drule.strip_comb) ct; 

530 
fun cdest_minus ct = case (rev o snd o Drule.strip_comb) ct of [n, m] => (m, n); 

531 
fun attach_num ct = (dest_num (Thm.term_of ct), ct); 

532 
fun cdifference t = (pairself (attach_num o cdest_of_num) o cdest_minus) t; 

533 
val simplify = MetaSimplifier.rewrite false (map mk_meta_eq @{thms Dig_plus_eval}); 

534 
fun cert ck cl cj = @{thm eqTrueE} OF [@{thm meta_eq_to_obj_eq} OF [simplify (Drule.list_comb (@{cterm "op = :: num \<Rightarrow> _"}, 

535 
[Drule.list_comb (@{cterm "op + :: num \<Rightarrow> _"}, [ck, cl]), cj]))]]; 

536 
in fn phi => fn _ => fn ct => case try cdifference ct 

537 
of NONE => (NONE) 

538 
 SOME ((k, ck), (l, cl)) => SOME (let val j = k  l in if j = 0 

539 
then MetaSimplifier.rewrite false [mk_meta_eq (Morphism.thm phi @{thm Dig_of_num_zero})] ct 

540 
else mk_meta_eq (let 

541 
val cj = Thm.cterm_of (Thm.theory_of_cterm ct) (mk_num (abs j)); 

542 
in 

543 
(if j > 0 then (Morphism.thm phi @{thm Dig_of_num_pos}) OF [cert cj cl ck] 

544 
else (Morphism.thm phi @{thm Dig_of_num_neg}) OF [cert cj ck cl]) 

545 
end) end) 

546 
end 

547 
*} 

548 

549 
lemma Dig_of_num_minus_zero [numeral]: 

550 
"of_num n  0 = of_num n" 

551 
by (simp add: minus_inverts_plus1) 

552 

553 
lemma Dig_one_minus_zero [numeral]: 

554 
"1  0 = 1" 

555 
by (simp add: minus_inverts_plus1) 

556 

557 
lemma Dig_one_minus_one [numeral]: 

558 
"1  1 = 0" 

559 
by (simp add: minus_inverts_plus1) 

560 

561 
lemma Dig_of_num_minus_one [numeral]: 

29941  562 
"of_num (Dig0 n)  1 = of_num (DigM n)" 
28021  563 
"of_num (Dig1 n)  1 = of_num (Dig0 n)" 
29941  564 
by (auto intro: minus_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) 
28021  565 

566 
lemma Dig_one_minus_of_num [numeral]: 

29941  567 
"1  of_num (Dig0 n) = 0  of_num (DigM n)" 
28021  568 
"1  of_num (Dig1 n) = 0  of_num (Dig0 n)" 
29941  569 
by (auto intro: minus_minus_zero_inverts_plus1 simp add: DigM_plus_one of_num.simps of_num_plus_one) 
28021  570 

571 
end 

572 

573 
context ring_1 

574 
begin 

575 

576 
subclass semiring_1_minus 

29667  577 
proof qed (simp_all add: algebra_simps) 
28021  578 

579 
lemma Dig_zero_minus_of_num [numeral]: 

580 
"0  of_num n =  of_num n" 

581 
by simp 

582 

583 
lemma Dig_zero_minus_one [numeral]: 

584 
"0  1 =  1" 

585 
by simp 

586 

587 
lemma Dig_uminus_uminus [numeral]: 

588 
" ( of_num n) = of_num n" 

589 
by simp 

590 

591 
lemma Dig_plus_uminus [numeral]: 

592 
"of_num m +  of_num n = of_num m  of_num n" 

593 
" of_num m + of_num n = of_num n  of_num m" 

594 
" of_num m +  of_num n =  (of_num m + of_num n)" 

595 
"of_num m   of_num n = of_num m + of_num n" 

596 
" of_num m  of_num n =  (of_num m + of_num n)" 

597 
" of_num m   of_num n = of_num n  of_num m" 

598 
by (simp_all add: diff_minus add_commute) 

599 

600 
lemma Dig_times_uminus [numeral]: 

601 
" of_num n * of_num m =  (of_num n * of_num m)" 

602 
"of_num n *  of_num m =  (of_num n * of_num m)" 

603 
" of_num n *  of_num m = of_num n * of_num m" 

604 
by (simp_all add: minus_mult_left [symmetric] minus_mult_right [symmetric]) 

605 

606 
lemma of_int_of_num [numeral]: "of_int (of_num n) = of_num n" 

607 
by (induct n) 

608 
(simp_all only: of_num.simps semiring_numeral_class.of_num.simps of_int_add, simp_all) 

609 

610 
declare of_int_1 [numeral] 

611 

612 
end 

613 

614 
text {* 

615 
Greetings to @{typ nat}. 

616 
*} 

617 

618 
instance nat :: semiring_1_minus proof qed simp_all 

619 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

620 
lemma Suc_of_num [numeral]: "Suc (of_num n) = of_num (n + One)" 
28021  621 
unfolding of_num_plus_one [symmetric] by simp 
622 

623 
lemma nat_number: 

624 
"1 = Suc 0" 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

625 
"of_num One = Suc 0" 
29941  626 
"of_num (Dig0 n) = Suc (of_num (DigM n))" 
28021  627 
"of_num (Dig1 n) = Suc (of_num (Dig0 n))" 
29941  628 
by (simp_all add: of_num.simps DigM_plus_one Suc_of_num) 
28021  629 

630 
declare diff_0_eq_0 [numeral] 

631 

632 

633 
subsection {* Code generator setup for @{typ int} *} 

634 

635 
definition Pls :: "num \<Rightarrow> int" where 

636 
[simp, code post]: "Pls n = of_num n" 

637 

638 
definition Mns :: "num \<Rightarrow> int" where 

639 
[simp, code post]: "Mns n =  of_num n" 

640 

641 
code_datatype "0::int" Pls Mns 

642 

643 
lemmas [code inline] = Pls_def [symmetric] Mns_def [symmetric] 

644 

645 
definition sub :: "num \<Rightarrow> num \<Rightarrow> int" where 

28562  646 
[simp, code del]: "sub m n = (of_num m  of_num n)" 
28021  647 

648 
definition dup :: "int \<Rightarrow> int" where 

28562  649 
[code del]: "dup k = 2 * k" 
28021  650 

651 
lemma Dig_sub [code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

652 
"sub One One = 0" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

653 
"sub (Dig0 m) One = of_num (DigM m)" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

654 
"sub (Dig1 m) One = of_num (Dig0 m)" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

655 
"sub One (Dig0 n) =  of_num (DigM n)" 
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

656 
"sub One (Dig1 n) =  of_num (Dig0 n)" 
28021  657 
"sub (Dig0 m) (Dig0 n) = dup (sub m n)" 
658 
"sub (Dig1 m) (Dig1 n) = dup (sub m n)" 

659 
"sub (Dig1 m) (Dig0 n) = dup (sub m n) + 1" 

660 
"sub (Dig0 m) (Dig1 n) = dup (sub m n)  1" 

29667  661 
apply (simp_all add: dup_def algebra_simps) 
29941  662 
apply (simp_all add: of_num_plus one_plus_DigM)[4] 
28021  663 
apply (simp_all add: of_num.simps) 
664 
done 

665 

666 
lemma dup_code [code]: 

667 
"dup 0 = 0" 

668 
"dup (Pls n) = Pls (Dig0 n)" 

669 
"dup (Mns n) = Mns (Dig0 n)" 

670 
by (simp_all add: dup_def of_num.simps) 

671 

28562  672 
lemma [code, code del]: 
28021  673 
"(1 :: int) = 1" 
674 
"(op + :: int \<Rightarrow> int \<Rightarrow> int) = op +" 

675 
"(uminus :: int \<Rightarrow> int) = uminus" 

676 
"(op  :: int \<Rightarrow> int \<Rightarrow> int) = op " 

677 
"(op * :: int \<Rightarrow> int \<Rightarrow> int) = op *" 

28367  678 
"(eq_class.eq :: int \<Rightarrow> int \<Rightarrow> bool) = eq_class.eq" 
28021  679 
"(op \<le> :: int \<Rightarrow> int \<Rightarrow> bool) = op \<le>" 
680 
"(op < :: int \<Rightarrow> int \<Rightarrow> bool) = op <" 

681 
by rule+ 

682 

683 
lemma one_int_code [code]: 

29942
31069b8d39df
replace 1::num with One; remove monoid_mult instance
huffman
parents:
29941
diff
changeset

684 
"1 = Pls One" 
28021  685 
by (simp add: of_num_one) 
686 

687 
lemma plus_int_code [code]: 

688 
"k + 0 = (k::int)" 

689 
"0 + l = (l::int)" 

690 
"Pls m + Pls n = Pls (m + n)" 

691 
"Pls m  Pls n = sub m n" 

692 
"Mns m + Mns n = Mns (m + n)" 

693 
"Mns m  Mns n = sub n m" 

694 
by (simp_all add: of_num_plus [symmetric]) 

695 

696 
lemma uminus_int_code [code]: 

697 
"uminus 0 = (0::int)" 

698 
"uminus (Pls m) = Mns m" 

699 
"uminus (Mns m) = Pls m" 

700 
by simp_all 

701 

702 
lemma minus_int_code [code]: 

703 
"k  0 = (k::int)" 

704 
"0  l = uminus (l::int)" 

705 
"Pls m  Pls n = sub m n" 

706 
"Pls m  Mns n = Pls (m + n)" 

707 
"Mns m  Pls n = Mns (m + n)" 

708 
"Mns m  Mns n = sub n m" 

709 
by (simp_all add: of_num_plus [symmetric]) 

710 

711 
lemma times_int_code [code]: 

712 
"k * 0 = (0::int)" 

713 
"0 * l = (0::int)" 

714 
"Pls m * Pls n = Pls (m * n)" 

715 
"Pls m * Mns n = Mns (m * n)" 

716 
"Mns m * Pls n = Mns (m * n)" 

717 
"Mns m * Mns n = Pls (m * n)" 

718 
by (simp_all add: of_num_times [symmetric]) 

719 

720 
lemma eq_int_code [code]: 

28367  721 
"eq_class.eq 0 (0::int) \<longleftrightarrow> True" 
722 
"eq_class.eq 0 (Pls l) \<longleftrightarrow> False" 

723 
"eq_class.eq 0 (Mns l) \<longleftrightarrow> False" 

724 
"eq_class.eq (Pls k) 0 \<longleftrightarrow> False" 

725 
"eq_class.eq (Pls k) (Pls l) \<longleftrightarrow> eq_class.eq k l" 

726 
"eq_class.eq (Pls k) (Mns l) \<longleftrightarrow> False" 

727 
"eq_class.eq (Mns k) 0 \<longleftrightarrow> False" 

728 
"eq_class.eq (Mns k) (Pls l) \<longleftrightarrow> False" 

729 
"eq_class.eq (Mns k) (Mns l) \<longleftrightarrow> eq_class.eq k l" 

28021  730 
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] 
28367  731 
by (simp_all add: of_num_eq_iff eq) 
28021  732 

733 
lemma less_eq_int_code [code]: 

734 
"0 \<le> (0::int) \<longleftrightarrow> True" 

735 
"0 \<le> Pls l \<longleftrightarrow> True" 

736 
"0 \<le> Mns l \<longleftrightarrow> False" 

737 
"Pls k \<le> 0 \<longleftrightarrow> False" 

738 
"Pls k \<le> Pls l \<longleftrightarrow> k \<le> l" 

739 
"Pls k \<le> Mns l \<longleftrightarrow> False" 

740 
"Mns k \<le> 0 \<longleftrightarrow> True" 

741 
"Mns k \<le> Pls l \<longleftrightarrow> True" 

742 
"Mns k \<le> Mns l \<longleftrightarrow> l \<le> k" 

743 
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] 

744 
by (simp_all add: of_num_less_eq_iff) 

745 

746 
lemma less_int_code [code]: 

747 
"0 < (0::int) \<longleftrightarrow> False" 

748 
"0 < Pls l \<longleftrightarrow> True" 

749 
"0 < Mns l \<longleftrightarrow> False" 

750 
"Pls k < 0 \<longleftrightarrow> False" 

751 
"Pls k < Pls l \<longleftrightarrow> k < l" 

752 
"Pls k < Mns l \<longleftrightarrow> False" 

753 
"Mns k < 0 \<longleftrightarrow> True" 

754 
"Mns k < Pls l \<longleftrightarrow> True" 

755 
"Mns k < Mns l \<longleftrightarrow> l < k" 

756 
using of_num_pos [of l, where ?'a = int] of_num_pos [of k, where ?'a = int] 

757 
by (simp_all add: of_num_less_iff) 

758 

759 
lemma [code inline del]: "(0::int) \<equiv> Numeral0" by simp 

760 
lemma [code inline del]: "(1::int) \<equiv> Numeral1" by simp 

761 
declare zero_is_num_zero [code inline del] 

762 
declare one_is_num_one [code inline del] 

763 

764 
hide (open) const sub dup 

765 

766 

767 
subsection {* Numeral equations as default simplification rules *} 

768 

769 
text {* TODO. Be more precise here with respect to subsumed facts. *} 

770 
declare (in semiring_numeral) numeral [simp] 

771 
declare (in semiring_1) numeral [simp] 

772 
declare (in semiring_char_0) numeral [simp] 

773 
declare (in ring_1) numeral [simp] 

774 
thm numeral 

775 

776 

777 
text {* Toy examples *} 

778 

779 
definition "bar \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat) \<and> #4 * #2 + #7 \<ge> (#8 :: int)  #3" 

780 
code_thms bar 

781 
export_code bar in Haskell file  

782 
export_code bar in OCaml module_name Foo file  

783 
ML {* @{code bar} *} 

784 

785 
end 