148 qed |
148 qed |
149 |
149 |
150 definition "LNil = Abs_llist NIL" |
150 definition "LNil = Abs_llist NIL" |
151 definition "LCons x xs = Abs_llist (CONS (Datatype.Leaf x) (Rep_llist xs))" |
151 definition "LCons x xs = Abs_llist (CONS (Datatype.Leaf x) (Rep_llist xs))" |
152 |
152 |
|
153 code_datatype LNil LCons |
|
154 |
153 lemma LCons_not_LNil [iff]: "LCons x xs \<noteq> LNil" |
155 lemma LCons_not_LNil [iff]: "LCons x xs \<noteq> LNil" |
154 apply (simp add: LNil_def LCons_def) |
156 apply (simp add: LNil_def LCons_def) |
155 apply (subst Abs_llist_inject) |
157 apply (subst Abs_llist_inject) |
156 apply (auto intro: NIL_type CONS_type Rep_llist) |
158 apply (auto intro: NIL_type CONS_type Rep_llist) |
157 done |
159 done |
194 qed |
196 qed |
195 qed |
197 qed |
196 |
198 |
197 |
199 |
198 definition |
200 definition |
199 "llist_case c d l = |
201 [code del]: "llist_case c d l = |
200 List_case c (\<lambda>x y. d (inv Datatype.Leaf x) (Abs_llist y)) (Rep_llist l)" |
202 List_case c (\<lambda>x y. d (inv Datatype.Leaf x) (Abs_llist y)) (Rep_llist l)" |
201 |
203 |
202 syntax (* FIXME? *) |
204 syntax (* FIXME? *) |
203 LNil :: logic |
205 LNil :: logic |
204 LCons :: logic |
206 LCons :: logic |
205 translations |
207 translations |
206 "case p of LNil \<Rightarrow> a | LCons x l \<Rightarrow> b" \<rightleftharpoons> "CONST llist_case a (\<lambda>x l. b) p" |
208 "case p of LNil \<Rightarrow> a | LCons x l \<Rightarrow> b" \<rightleftharpoons> "CONST llist_case a (\<lambda>x l. b) p" |
207 |
209 |
208 lemma llist_case_LNil [simp]: "llist_case c d LNil = c" |
210 lemma llist_case_LNil [simp, code]: "llist_case c d LNil = c" |
209 by (simp add: llist_case_def LNil_def |
211 by (simp add: llist_case_def LNil_def |
210 NIL_type Abs_llist_inverse) |
212 NIL_type Abs_llist_inverse) |
211 |
213 |
212 lemma llist_case_LCons [simp]: "llist_case c d (LCons M N) = d M N" |
214 lemma llist_case_LCons [simp, code]: "llist_case c d (LCons M N) = d M N" |
213 by (simp add: llist_case_def LCons_def |
215 by (simp add: llist_case_def LCons_def |
214 CONS_type Abs_llist_inverse Rep_llist Rep_llist_inverse inj_Leaf) |
216 CONS_type Abs_llist_inverse Rep_llist Rep_llist_inverse inj_Leaf) |
215 |
217 |
|
218 lemma llist_case_cert: |
|
219 includes meta_conjunction_syntax |
|
220 assumes "CASE \<equiv> llist_case c d" |
|
221 shows "(CASE LNil \<equiv> c) && (CASE (LCons M N) \<equiv> d M N)" |
|
222 using assms by simp_all |
|
223 |
|
224 setup {* |
|
225 Code.add_case @{thm llist_case_cert} |
|
226 *} |
216 |
227 |
217 definition |
228 definition |
218 "llist_corec a f = |
229 [code del]: "llist_corec a f = |
219 Abs_llist (LList_corec a |
230 Abs_llist (LList_corec a |
220 (\<lambda>z. |
231 (\<lambda>z. |
221 case f z of None \<Rightarrow> None |
232 case f z of None \<Rightarrow> None |
222 | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)))" |
233 | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)))" |
223 |
234 |