author | haftmann |
Tue, 21 Jul 2009 17:02:18 +0200 | |
changeset 32127 | 631546213601 |
parent 32069 | 6d28bbd33e2c |
child 33296 | a3924d1069e5 |
permissions | -rw-r--r-- |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
1 |
(* Author: Florian Haftmann, TU Muenchen *) |
24999 | 2 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
3 |
header {* Type of target language numerals *} |
24999 | 4 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
5 |
theory Code_Numeral |
31203
5c8fb4fd67e0
moved Code_Index, Random and Quickcheck before Main
haftmann
parents:
31192
diff
changeset
|
6 |
imports Nat_Numeral |
24999 | 7 |
begin |
8 |
||
9 |
text {* |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
10 |
Code numerals are isomorphic to HOL @{typ nat} but |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
11 |
mapped to target-language builtin numerals. |
24999 | 12 |
*} |
13 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
14 |
subsection {* Datatype of target language numerals *} |
24999 | 15 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
16 |
typedef (open) code_numeral = "UNIV \<Colon> nat set" |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
17 |
morphisms nat_of of_nat by rule |
24999 | 18 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
19 |
lemma of_nat_nat_of [simp]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
20 |
"of_nat (nat_of k) = k" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
21 |
by (rule nat_of_inverse) |
24999 | 22 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
23 |
lemma nat_of_of_nat [simp]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
24 |
"nat_of (of_nat n) = n" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
25 |
by (rule of_nat_inverse) (rule UNIV_I) |
24999 | 26 |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
27 |
lemma [measure_function]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
28 |
"is_measure nat_of" by (rule is_measure_trivial) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
29 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
30 |
lemma code_numeral: |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
31 |
"(\<And>n\<Colon>code_numeral. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))" |
24999 | 32 |
proof |
25767 | 33 |
fix n :: nat |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
34 |
assume "\<And>n\<Colon>code_numeral. PROP P n" |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
35 |
then show "PROP P (of_nat n)" . |
24999 | 36 |
next |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
37 |
fix n :: code_numeral |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
38 |
assume "\<And>n\<Colon>nat. PROP P (of_nat n)" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
39 |
then have "PROP P (of_nat (nat_of n))" . |
25767 | 40 |
then show "PROP P n" by simp |
24999 | 41 |
qed |
42 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
43 |
lemma code_numeral_case: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
44 |
assumes "\<And>n. k = of_nat n \<Longrightarrow> P" |
26140 | 45 |
shows P |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
46 |
by (rule assms [of "nat_of k"]) simp |
26140 | 47 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
48 |
lemma code_numeral_induct_raw: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
49 |
assumes "\<And>n. P (of_nat n)" |
26140 | 50 |
shows "P k" |
51 |
proof - |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
52 |
from assms have "P (of_nat (nat_of k))" . |
26140 | 53 |
then show ?thesis by simp |
54 |
qed |
|
55 |
||
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
56 |
lemma nat_of_inject [simp]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
57 |
"nat_of k = nat_of l \<longleftrightarrow> k = l" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
58 |
by (rule nat_of_inject) |
26140 | 59 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
60 |
lemma of_nat_inject [simp]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
61 |
"of_nat n = of_nat m \<longleftrightarrow> n = m" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
62 |
by (rule of_nat_inject) (rule UNIV_I)+ |
26140 | 63 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
64 |
instantiation code_numeral :: zero |
26140 | 65 |
begin |
66 |
||
28562 | 67 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
68 |
"0 = of_nat 0" |
26140 | 69 |
|
70 |
instance .. |
|
71 |
||
72 |
end |
|
73 |
||
74 |
definition [simp]: |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
75 |
"Suc_code_numeral k = of_nat (Suc (nat_of k))" |
26140 | 76 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
77 |
rep_datatype "0 \<Colon> code_numeral" Suc_code_numeral |
26140 | 78 |
proof - |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
79 |
fix P :: "code_numeral \<Rightarrow> bool" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
80 |
fix k :: code_numeral |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
81 |
assume "P 0" then have init: "P (of_nat 0)" by simp |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
82 |
assume "\<And>k. P k \<Longrightarrow> P (Suc_code_numeral k)" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
83 |
then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_code_numeral (of_nat n))" . |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
84 |
then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
85 |
from init step have "P (of_nat (nat_of k))" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
86 |
by (induct "nat_of k") simp_all |
26140 | 87 |
then show "P k" by simp |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
26304
diff
changeset
|
88 |
qed simp_all |
26140 | 89 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
90 |
declare code_numeral_case [case_names nat, cases type: code_numeral] |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
91 |
declare code_numeral.induct [case_names nat, induct type: code_numeral] |
26140 | 92 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
93 |
lemma code_numeral_decr [termination_simp]: |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
94 |
"k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Suc 0 < nat_of k" |
30245
e67f42ac1157
consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents:
29823
diff
changeset
|
95 |
by (cases k) simp |
e67f42ac1157
consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents:
29823
diff
changeset
|
96 |
|
e67f42ac1157
consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents:
29823
diff
changeset
|
97 |
lemma [simp, code]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
98 |
"code_numeral_size = nat_of" |
26140 | 99 |
proof (rule ext) |
100 |
fix k |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
101 |
have "code_numeral_size k = nat_size (nat_of k)" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
102 |
by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all) |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
103 |
also have "nat_size (nat_of k) = nat_of k" by (induct "nat_of k") simp_all |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
104 |
finally show "code_numeral_size k = nat_of k" . |
26140 | 105 |
qed |
106 |
||
30245
e67f42ac1157
consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents:
29823
diff
changeset
|
107 |
lemma [simp, code]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
108 |
"size = nat_of" |
26140 | 109 |
proof (rule ext) |
110 |
fix k |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
111 |
show "size k = nat_of k" |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
112 |
by (induct k) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all) |
26140 | 113 |
qed |
114 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
115 |
lemmas [code del] = code_numeral.recs code_numeral.cases |
30245
e67f42ac1157
consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents:
29823
diff
changeset
|
116 |
|
28562 | 117 |
lemma [code]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
118 |
"eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of k) (nat_of l)" |
28346
b8390cd56b8f
discontinued special treatment of op = vs. eq_class.eq
haftmann
parents:
28228
diff
changeset
|
119 |
by (cases k, cases l) (simp add: eq) |
24999 | 120 |
|
28351 | 121 |
lemma [code nbe]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
122 |
"eq_class.eq (k::code_numeral) k \<longleftrightarrow> True" |
28351 | 123 |
by (rule HOL.eq_refl) |
124 |
||
24999 | 125 |
|
25767 | 126 |
subsection {* Indices as datatype of ints *} |
127 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
128 |
instantiation code_numeral :: number |
25767 | 129 |
begin |
24999 | 130 |
|
25767 | 131 |
definition |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
132 |
"number_of = of_nat o nat" |
25767 | 133 |
|
134 |
instance .. |
|
135 |
||
136 |
end |
|
24999 | 137 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
138 |
lemma nat_of_number [simp]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
139 |
"nat_of (number_of k) = number_of k" |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
140 |
by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id) |
26264 | 141 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
142 |
code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral" |
24999 | 143 |
|
144 |
||
145 |
subsection {* Basic arithmetic *} |
|
146 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
147 |
instantiation code_numeral :: "{minus, ordered_semidom, semiring_div, linorder}" |
25767 | 148 |
begin |
24999 | 149 |
|
28562 | 150 |
definition [simp, code del]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
151 |
"(1\<Colon>code_numeral) = of_nat 1" |
24999 | 152 |
|
28562 | 153 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
154 |
"n + m = of_nat (nat_of n + nat_of m)" |
25767 | 155 |
|
28562 | 156 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
157 |
"n - m = of_nat (nat_of n - nat_of m)" |
25767 | 158 |
|
28562 | 159 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
160 |
"n * m = of_nat (nat_of n * nat_of m)" |
25767 | 161 |
|
28562 | 162 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
163 |
"n div m = of_nat (nat_of n div nat_of m)" |
24999 | 164 |
|
28562 | 165 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
166 |
"n mod m = of_nat (nat_of n mod nat_of m)" |
24999 | 167 |
|
28562 | 168 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
169 |
"n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m" |
24999 | 170 |
|
28562 | 171 |
definition [simp, code del]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
172 |
"n < m \<longleftrightarrow> nat_of n < nat_of m" |
24999 | 173 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
174 |
instance proof |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
175 |
qed (auto simp add: code_numeral left_distrib div_mult_self1) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
176 |
|
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
177 |
end |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
178 |
|
32069
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents:
31998
diff
changeset
|
179 |
lemma zero_code_numeral_code [code, code_unfold]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
180 |
"(0\<Colon>code_numeral) = Numeral0" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
181 |
by (simp add: number_of_code_numeral_def Pls_def) |
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31377
diff
changeset
|
182 |
lemma [code_post]: "Numeral0 = (0\<Colon>code_numeral)" |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
183 |
using zero_code_numeral_code .. |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
184 |
|
32069
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents:
31998
diff
changeset
|
185 |
lemma one_code_numeral_code [code, code_unfold]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
186 |
"(1\<Colon>code_numeral) = Numeral1" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
187 |
by (simp add: number_of_code_numeral_def Pls_def Bit1_def) |
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31377
diff
changeset
|
188 |
lemma [code_post]: "Numeral1 = (1\<Colon>code_numeral)" |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
189 |
using one_code_numeral_code .. |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
190 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
191 |
lemma plus_code_numeral_code [code nbe]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
192 |
"of_nat n + of_nat m = of_nat (n + m)" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
193 |
by simp |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
194 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
195 |
definition subtract_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
196 |
[simp, code del]: "subtract_code_numeral = op -" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
197 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
198 |
lemma subtract_code_numeral_code [code nbe]: |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
199 |
"subtract_code_numeral (of_nat n) (of_nat m) = of_nat (n - m)" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
200 |
by simp |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
201 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
202 |
lemma minus_code_numeral_code [code]: |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
203 |
"n - m = subtract_code_numeral n m" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
204 |
by simp |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
205 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
206 |
lemma times_code_numeral_code [code nbe]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
207 |
"of_nat n * of_nat m = of_nat (n * m)" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
208 |
by simp |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
209 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
210 |
lemma less_eq_code_numeral_code [code nbe]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
211 |
"of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m" |
25767 | 212 |
by simp |
24999 | 213 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
214 |
lemma less_code_numeral_code [code nbe]: |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
215 |
"of_nat n < of_nat m \<longleftrightarrow> n < m" |
25767 | 216 |
by simp |
24999 | 217 |
|
31266 | 218 |
lemma code_numeral_zero_minus_one: |
219 |
"(0::code_numeral) - 1 = 0" |
|
220 |
by simp |
|
221 |
||
222 |
lemma Suc_code_numeral_minus_one: |
|
223 |
"Suc_code_numeral n - 1 = n" |
|
224 |
by simp |
|
26140 | 225 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
226 |
lemma of_nat_code [code]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
227 |
"of_nat = Nat.of_nat" |
25918 | 228 |
proof |
229 |
fix n :: nat |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
230 |
have "Nat.of_nat n = of_nat n" |
25918 | 231 |
by (induct n) simp_all |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
232 |
then show "of_nat n = Nat.of_nat n" |
25918 | 233 |
by (rule sym) |
234 |
qed |
|
235 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
236 |
lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1" |
25928 | 237 |
by (cases i) auto |
238 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
239 |
definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where |
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
240 |
"nat_of_aux i n = nat_of i + n" |
25928 | 241 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
242 |
lemma nat_of_aux_code [code]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
243 |
"nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))" |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
244 |
by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero) |
25928 | 245 |
|
29815
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
246 |
lemma nat_of_code [code]: |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
247 |
"nat_of i = nat_of_aux i 0" |
9e94b7078fa5
mandatory prefix for index conversion operations
haftmann
parents:
28708
diff
changeset
|
248 |
by (simp add: nat_of_aux_def) |
25918 | 249 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
250 |
definition div_mod_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
251 |
[code del]: "div_mod_code_numeral n m = (n div m, n mod m)" |
26009 | 252 |
|
28562 | 253 |
lemma [code]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
254 |
"div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
255 |
unfolding div_mod_code_numeral_def by auto |
26009 | 256 |
|
28562 | 257 |
lemma [code]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
258 |
"n div m = fst (div_mod_code_numeral n m)" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
259 |
unfolding div_mod_code_numeral_def by simp |
26009 | 260 |
|
28562 | 261 |
lemma [code]: |
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
262 |
"n mod m = snd (div_mod_code_numeral n m)" |
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
263 |
unfolding div_mod_code_numeral_def by simp |
26009 | 264 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
265 |
definition int_of :: "code_numeral \<Rightarrow> int" where |
31192 | 266 |
"int_of = Nat.of_nat o nat_of" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
267 |
|
31192 | 268 |
lemma int_of_code [code]: |
269 |
"int_of k = (if k = 0 then 0 |
|
270 |
else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))" |
|
271 |
by (auto simp add: int_of_def mod_div_equality') |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
272 |
|
31192 | 273 |
hide (open) const of_nat nat_of int_of |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
274 |
|
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28562
diff
changeset
|
275 |
|
28228 | 276 |
subsection {* Code generator setup *} |
24999 | 277 |
|
25767 | 278 |
text {* Implementation of indices by bounded integers *} |
279 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
280 |
code_type code_numeral |
24999 | 281 |
(SML "int") |
31377 | 282 |
(OCaml "Big'_int.big'_int") |
25967 | 283 |
(Haskell "Int") |
24999 | 284 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
285 |
code_instance code_numeral :: eq |
24999 | 286 |
(Haskell -) |
287 |
||
288 |
setup {* |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
289 |
fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral} |
31377 | 290 |
false false) ["SML", "Haskell"] |
291 |
#> Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral} false true "OCaml" |
|
24999 | 292 |
*} |
293 |
||
25918 | 294 |
code_reserved SML Int int |
31377 | 295 |
code_reserved OCaml Big_int |
24999 | 296 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
297 |
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" |
25928 | 298 |
(SML "Int.+/ ((_),/ (_))") |
31377 | 299 |
(OCaml "Big'_int.add'_big'_int") |
24999 | 300 |
(Haskell infixl 6 "+") |
301 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
302 |
code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" |
25918 | 303 |
(SML "Int.max/ (_/ -/ _,/ 0 : int)") |
31377 | 304 |
(OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int") |
25918 | 305 |
(Haskell "max/ (_/ -/ _)/ (0 :: Int)") |
24999 | 306 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
307 |
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" |
25928 | 308 |
(SML "Int.*/ ((_),/ (_))") |
31377 | 309 |
(OCaml "Big'_int.mult'_big'_int") |
24999 | 310 |
(Haskell infixl 7 "*") |
311 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
312 |
code_const div_mod_code_numeral |
29823
0ab754d13ccd
session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents:
29815
diff
changeset
|
313 |
(SML "(fn n => fn m =>/ if m = 0/ then (0, n) else/ (n div m, n mod m))") |
31377 | 314 |
(OCaml "(fun k -> fun l ->/ Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int k)/ (Big'_int.abs'_big'_int l))") |
26009 | 315 |
(Haskell "divMod") |
25928 | 316 |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
317 |
code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool" |
24999 | 318 |
(SML "!((_ : Int.int) = _)") |
31377 | 319 |
(OCaml "Big'_int.eq'_big'_int") |
24999 | 320 |
(Haskell infixl 4 "==") |
321 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
322 |
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool" |
25928 | 323 |
(SML "Int.<=/ ((_),/ (_))") |
31377 | 324 |
(OCaml "Big'_int.le'_big'_int") |
24999 | 325 |
(Haskell infix 4 "<=") |
326 |
||
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31203
diff
changeset
|
327 |
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool" |
25928 | 328 |
(SML "Int.</ ((_),/ (_))") |
31377 | 329 |
(OCaml "Big'_int.lt'_big'_int") |
24999 | 330 |
(Haskell infix 4 "<") |
331 |
||
332 |
end |