equal
deleted
inserted
replaced
7 theory Code_Target_Nat |
7 theory Code_Target_Nat |
8 imports Code_Abstract_Nat |
8 imports Code_Abstract_Nat |
9 begin |
9 begin |
10 |
10 |
11 subsection {* Implementation for @{typ nat} *} |
11 subsection {* Implementation for @{typ nat} *} |
|
12 |
|
13 context |
|
14 includes natural.lifting integer.lifting |
|
15 begin |
12 |
16 |
13 lift_definition Nat :: "integer \<Rightarrow> nat" |
17 lift_definition Nat :: "integer \<Rightarrow> nat" |
14 is nat |
18 is nat |
15 . |
19 . |
16 |
20 |
94 |
98 |
95 lemma num_of_nat_code [code]: |
99 lemma num_of_nat_code [code]: |
96 "num_of_nat = num_of_integer \<circ> of_nat" |
100 "num_of_nat = num_of_integer \<circ> of_nat" |
97 by transfer (simp add: fun_eq_iff) |
101 by transfer (simp add: fun_eq_iff) |
98 |
102 |
|
103 end |
|
104 |
99 lemma (in semiring_1) of_nat_code_if: |
105 lemma (in semiring_1) of_nat_code_if: |
100 "of_nat n = (if n = 0 then 0 |
106 "of_nat n = (if n = 0 then 0 |
101 else let |
107 else let |
102 (m, q) = divmod_nat n 2; |
108 (m, q) = divmod_nat n 2; |
103 m' = 2 * of_nat m |
109 m' = 2 * of_nat m |
118 "int_of_nat n = int_of_integer (of_nat n)" |
124 "int_of_nat n = int_of_integer (of_nat n)" |
119 by (simp add: int_of_nat_def) |
125 by (simp add: int_of_nat_def) |
120 |
126 |
121 lemma [code abstract]: |
127 lemma [code abstract]: |
122 "integer_of_nat (nat k) = max 0 (integer_of_int k)" |
128 "integer_of_nat (nat k) = max 0 (integer_of_int k)" |
123 by transfer auto |
129 including integer.lifting by transfer auto |
124 |
130 |
125 lemma term_of_nat_code [code]: |
131 lemma term_of_nat_code [code]: |
126 -- {* Use @{term Code_Numeral.nat_of_integer} in term reconstruction |
132 -- {* Use @{term Code_Numeral.nat_of_integer} in term reconstruction |
127 instead of @{term Code_Target_Nat.Nat} such that reconstructed |
133 instead of @{term Code_Target_Nat.Nat} such that reconstructed |
128 terms can be fed back to the code generator *} |
134 terms can be fed back to the code generator *} |
137 |
143 |
138 lemma nat_of_integer_code_post [code_post]: |
144 lemma nat_of_integer_code_post [code_post]: |
139 "nat_of_integer 0 = 0" |
145 "nat_of_integer 0 = 0" |
140 "nat_of_integer 1 = 1" |
146 "nat_of_integer 1 = 1" |
141 "nat_of_integer (numeral k) = numeral k" |
147 "nat_of_integer (numeral k) = numeral k" |
142 by (transfer, simp)+ |
148 including integer.lifting by (transfer, simp)+ |
143 |
149 |
144 code_identifier |
150 code_identifier |
145 code_module Code_Target_Nat \<rightharpoonup> |
151 code_module Code_Target_Nat \<rightharpoonup> |
146 (SML) Arith and (OCaml) Arith and (Haskell) Arith |
152 (SML) Arith and (OCaml) Arith and (Haskell) Arith |
147 |
153 |