src/HOL/Library/Coinductive_List.thy
changeset 28693 a1294a197d12
parent 27487 c8a6ce181805
child 28702 4867f2fa0e48
equal deleted inserted replaced
28692:a2bc5ce0c9fc 28693:a1294a197d12
   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 
   249       then show ?thesis ..
   260       then show ?thesis ..
   250     qed
   261     qed
   251   qed
   262   qed
   252 qed
   263 qed
   253 
   264 
   254 lemma llist_corec:
   265 lemma llist_corec [code]:
   255   "llist_corec a f =
   266   "llist_corec a f =
   256     (case f a of None \<Rightarrow> LNil | Some (z, w) \<Rightarrow> LCons z (llist_corec w f))"
   267     (case f a of None \<Rightarrow> LNil | Some (z, w) \<Rightarrow> LCons z (llist_corec w f))"
   257 proof (cases "f a")
   268 proof (cases "f a")
   258   case None
   269   case None
   259   then show ?thesis
   270   then show ?thesis