| author | berghofe | 
| Mon, 23 Oct 2006 00:52:15 +0200 | |
| changeset 21088 | 13348ab97f5a | 
| parent 20820 | 58693343905f | 
| child 21404 | eb85850d3eb7 | 
| permissions | -rw-r--r-- | 
| 18400 | 1 | (* Title: HOL/Library/Coinductive_Lists.thy | 
| 2 | ID: $Id$ | |
| 3 | Author: Lawrence C Paulson and Makarius | |
| 4 | *) | |
| 5 | ||
| 6 | header {* Potentially infinite lists as greatest fixed-point *}
 | |
| 7 | ||
| 8 | theory Coinductive_List | |
| 9 | imports Main | |
| 10 | begin | |
| 11 | ||
| 12 | subsection {* List constructors over the datatype universe *}
 | |
| 13 | ||
| 19086 | 14 | definition | 
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 15 | "NIL = Datatype.In0 (Datatype.Numb 0)" | 
| 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 16 | "CONS M N = Datatype.In1 (Datatype.Scons M N)" | 
| 18400 | 17 | |
| 18 | lemma CONS_not_NIL [iff]: "CONS M N \<noteq> NIL" | |
| 19 | and NIL_not_CONS [iff]: "NIL \<noteq> CONS M N" | |
| 20 | and CONS_inject [iff]: "(CONS K M) = (CONS L N) = (K = L \<and> M = N)" | |
| 21 | by (simp_all add: NIL_def CONS_def) | |
| 22 | ||
| 23 | lemma CONS_mono: "M \<subseteq> M' \<Longrightarrow> N \<subseteq> N' \<Longrightarrow> CONS M N \<subseteq> CONS M' N'" | |
| 24 | by (simp add: CONS_def In1_mono Scons_mono) | |
| 25 | ||
| 26 | lemma CONS_UN1: "CONS M (\<Union>x. f x) = (\<Union>x. CONS M (f x))" | |
| 27 |     -- {* A continuity result? *}
 | |
| 28 | by (simp add: CONS_def In1_UN1 Scons_UN1_y) | |
| 29 | ||
| 19086 | 30 | definition | 
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 31 | "List_case c h = Datatype.Case (\<lambda>_. c) (Datatype.Split h)" | 
| 18400 | 32 | |
| 33 | lemma List_case_NIL [simp]: "List_case c h NIL = c" | |
| 34 | and List_case_CONS [simp]: "List_case c h (CONS M N) = h M N" | |
| 35 | by (simp_all add: List_case_def NIL_def CONS_def) | |
| 36 | ||
| 37 | ||
| 38 | subsection {* Corecursive lists *}
 | |
| 39 | ||
| 40 | consts | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 41 | LList :: "'a Datatype.item set \<Rightarrow> 'a Datatype.item set" | 
| 18400 | 42 | |
| 43 | coinductive "LList A" | |
| 44 | intros | |
| 45 | NIL [intro]: "NIL \<in> LList A" | |
| 46 | CONS [intro]: "a \<in> A \<Longrightarrow> M \<in> LList A \<Longrightarrow> CONS a M \<in> LList A" | |
| 47 | ||
| 48 | lemma LList_mono: "A \<subseteq> B \<Longrightarrow> LList A \<subseteq> LList B" | |
| 49 |     -- {* This justifies using @{text LList} in other recursive type definitions. *}
 | |
| 18730 | 50 | unfolding LList.defs by (blast intro!: gfp_mono) | 
| 18400 | 51 | |
| 52 | consts | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 53 |   LList_corec_aux :: "nat \<Rightarrow> ('a \<Rightarrow> ('b Datatype.item \<times> 'a) option) \<Rightarrow>
 | 
| 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 54 | 'a \<Rightarrow> 'b Datatype.item" | 
| 18400 | 55 | primrec | 
| 56 |   "LList_corec_aux 0 f x = {}"
 | |
| 57 | "LList_corec_aux (Suc k) f x = | |
| 58 | (case f x of | |
| 59 | None \<Rightarrow> NIL | |
| 60 | | Some (z, w) \<Rightarrow> CONS z (LList_corec_aux k f w))" | |
| 61 | ||
| 19086 | 62 | definition | 
| 63 | "LList_corec a f = (\<Union>k. LList_corec_aux k f a)" | |
| 18400 | 64 | |
| 65 | text {*
 | |
| 66 |   Note: the subsequent recursion equation for @{text LList_corec} may
 | |
| 67 | be used with the Simplifier, provided it operates in a non-strict | |
| 68 |   fashion for case expressions (i.e.\ the usual @{text case}
 | |
| 69 | congruence rule needs to be present). | |
| 70 | *} | |
| 71 | ||
| 72 | lemma LList_corec: | |
| 73 | "LList_corec a f = | |
| 74 | (case f a of None \<Rightarrow> NIL | Some (z, w) \<Rightarrow> CONS z (LList_corec w f))" | |
| 75 | (is "?lhs = ?rhs") | |
| 76 | proof | |
| 77 | show "?lhs \<subseteq> ?rhs" | |
| 78 | apply (unfold LList_corec_def) | |
| 79 | apply (rule UN_least) | |
| 80 | apply (case_tac k) | |
| 81 | apply (simp_all (no_asm_simp) split: option.splits) | |
| 82 | apply (rule allI impI subset_refl [THEN CONS_mono] UNIV_I [THEN UN_upper])+ | |
| 83 | done | |
| 84 | show "?rhs \<subseteq> ?lhs" | |
| 85 | apply (simp add: LList_corec_def split: option.splits) | |
| 86 | apply (simp add: CONS_UN1) | |
| 87 | apply safe | |
| 88 | apply (rule_tac a = "Suc ?k" in UN_I, simp, simp)+ | |
| 89 | done | |
| 90 | qed | |
| 91 | ||
| 92 | lemma LList_corec_type: "LList_corec a f \<in> LList UNIV" | |
| 93 | proof - | |
| 94 |   have "LList_corec a f \<in> {LList_corec a f | a. True}" by blast
 | |
| 95 | then show ?thesis | |
| 96 | proof coinduct | |
| 97 | case (LList L) | |
| 98 | then obtain x where L: "L = LList_corec x f" by blast | |
| 99 | show ?case | |
| 100 | proof (cases "f x") | |
| 101 | case None | |
| 102 | then have "LList_corec x f = NIL" | |
| 103 | by (simp add: LList_corec) | |
| 104 | with L have ?NIL by simp | |
| 105 | then show ?thesis .. | |
| 106 | next | |
| 107 | case (Some p) | |
| 108 | then have "LList_corec x f = CONS (fst p) (LList_corec (snd p) f)" | |
| 109 | by (simp add: split_def LList_corec) | |
| 110 | with L have ?CONS by auto | |
| 111 | then show ?thesis .. | |
| 112 | qed | |
| 113 | qed | |
| 114 | qed | |
| 115 | ||
| 116 | ||
| 117 | subsection {* Abstract type definition *}
 | |
| 118 | ||
| 119 | typedef 'a llist = | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 120 | "LList (range Datatype.Leaf) :: 'a Datatype.item set" | 
| 18400 | 121 | proof | 
| 122 | show "NIL \<in> ?llist" .. | |
| 123 | qed | |
| 124 | ||
| 125 | lemma NIL_type: "NIL \<in> llist" | |
| 18730 | 126 | unfolding llist_def by (rule LList.NIL) | 
| 18400 | 127 | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 128 | lemma CONS_type: "a \<in> range Datatype.Leaf \<Longrightarrow> | 
| 18400 | 129 | M \<in> llist \<Longrightarrow> CONS a M \<in> llist" | 
| 18730 | 130 | unfolding llist_def by (rule LList.CONS) | 
| 18400 | 131 | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 132 | lemma llistI: "x \<in> LList (range Datatype.Leaf) \<Longrightarrow> x \<in> llist" | 
| 18400 | 133 | by (simp add: llist_def) | 
| 134 | ||
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 135 | lemma llistD: "x \<in> llist \<Longrightarrow> x \<in> LList (range Datatype.Leaf)" | 
| 18400 | 136 | by (simp add: llist_def) | 
| 137 | ||
| 138 | lemma Rep_llist_UNIV: "Rep_llist x \<in> LList UNIV" | |
| 139 | proof - | |
| 140 | have "Rep_llist x \<in> llist" by (rule Rep_llist) | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 141 | then have "Rep_llist x \<in> LList (range Datatype.Leaf)" | 
| 18400 | 142 | by (simp add: llist_def) | 
| 143 | also have "\<dots> \<subseteq> LList UNIV" by (rule LList_mono) simp | |
| 144 | finally show ?thesis . | |
| 145 | qed | |
| 146 | ||
| 19086 | 147 | definition | 
| 148 | "LNil = Abs_llist NIL" | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 149 | "LCons x xs = Abs_llist (CONS (Datatype.Leaf x) (Rep_llist xs))" | 
| 18400 | 150 | |
| 151 | lemma LCons_not_LNil [iff]: "LCons x xs \<noteq> LNil" | |
| 152 | apply (simp add: LNil_def LCons_def) | |
| 153 | apply (subst Abs_llist_inject) | |
| 154 | apply (auto intro: NIL_type CONS_type Rep_llist) | |
| 155 | done | |
| 156 | ||
| 157 | lemma LNil_not_LCons [iff]: "LNil \<noteq> LCons x xs" | |
| 158 | by (rule LCons_not_LNil [symmetric]) | |
| 159 | ||
| 160 | lemma LCons_inject [iff]: "(LCons x xs = LCons y ys) = (x = y \<and> xs = ys)" | |
| 161 | apply (simp add: LCons_def) | |
| 162 | apply (subst Abs_llist_inject) | |
| 163 | apply (auto simp add: Rep_llist_inject intro: CONS_type Rep_llist) | |
| 164 | done | |
| 165 | ||
| 166 | lemma Rep_llist_LNil: "Rep_llist LNil = NIL" | |
| 167 | by (simp add: LNil_def add: Abs_llist_inverse NIL_type) | |
| 168 | ||
| 169 | lemma Rep_llist_LCons: "Rep_llist (LCons x l) = | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 170 | CONS (Datatype.Leaf x) (Rep_llist l)" | 
| 18400 | 171 | by (simp add: LCons_def Abs_llist_inverse CONS_type Rep_llist) | 
| 172 | ||
| 20802 | 173 | lemma llist_cases [cases type: llist]: | 
| 174 | obtains | |
| 175 | (LNil) "l = LNil" | |
| 176 | | (LCons) x l' where "l = LCons x l'" | |
| 18400 | 177 | proof (cases l) | 
| 178 | case (Abs_llist L) | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 179 | from `L \<in> llist` have "L \<in> LList (range Datatype.Leaf)" by (rule llistD) | 
| 18400 | 180 | then show ?thesis | 
| 181 | proof cases | |
| 182 | case NIL | |
| 183 | with Abs_llist have "l = LNil" by (simp add: LNil_def) | |
| 184 | with LNil show ?thesis . | |
| 185 | next | |
| 186 | case (CONS K a) | |
| 187 | then have "K \<in> llist" by (blast intro: llistI) | |
| 188 | then obtain l' where "K = Rep_llist l'" by cases | |
| 189 | with CONS and Abs_llist obtain x where "l = LCons x l'" | |
| 190 | by (auto simp add: LCons_def Abs_llist_inject) | |
| 191 | with LCons show ?thesis . | |
| 192 | qed | |
| 193 | qed | |
| 194 | ||
| 195 | ||
| 19086 | 196 | definition | 
| 197 | "llist_case c d l = | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 198 | List_case c (\<lambda>x y. d (inv Datatype.Leaf x) (Abs_llist y)) (Rep_llist l)" | 
| 20770 | 199 | |
| 200 | syntax (* FIXME? *) | |
| 201 | LNil :: logic | |
| 202 | LCons :: logic | |
| 18400 | 203 | translations | 
| 20770 | 204 | "case p of LNil \<Rightarrow> a | LCons x l \<Rightarrow> b" \<rightleftharpoons> "CONST llist_case a (\<lambda>x l. b) p" | 
| 18400 | 205 | |
| 206 | lemma llist_case_LNil [simp]: "llist_case c d LNil = c" | |
| 207 | by (simp add: llist_case_def LNil_def | |
| 208 | NIL_type Abs_llist_inverse) | |
| 209 | ||
| 210 | lemma llist_case_LCons [simp]: "llist_case c d (LCons M N) = d M N" | |
| 211 | by (simp add: llist_case_def LCons_def | |
| 212 | CONS_type Abs_llist_inverse Rep_llist Rep_llist_inverse inj_Leaf) | |
| 213 | ||
| 214 | ||
| 19086 | 215 | definition | 
| 216 | "llist_corec a f = | |
| 18400 | 217 | Abs_llist (LList_corec a | 
| 218 | (\<lambda>z. | |
| 219 | case f z of None \<Rightarrow> None | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 220 | | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)))" | 
| 18400 | 221 | |
| 222 | lemma LList_corec_type2: | |
| 223 | "LList_corec a | |
| 224 | (\<lambda>z. case f z of None \<Rightarrow> None | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 225 | | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)) \<in> llist" | 
| 18400 | 226 | (is "?corec a \<in> _") | 
| 227 | proof (unfold llist_def) | |
| 228 | let "LList_corec a ?g" = "?corec a" | |
| 229 |   have "?corec a \<in> {?corec x | x. True}" by blast
 | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 230 | then show "?corec a \<in> LList (range Datatype.Leaf)" | 
| 18400 | 231 | proof coinduct | 
| 232 | case (LList L) | |
| 233 | then obtain x where L: "L = ?corec x" by blast | |
| 234 | show ?case | |
| 235 | proof (cases "f x") | |
| 236 | case None | |
| 237 | then have "?corec x = NIL" | |
| 238 | by (simp add: LList_corec) | |
| 239 | with L have ?NIL by simp | |
| 240 | then show ?thesis .. | |
| 241 | next | |
| 242 | case (Some p) | |
| 243 | then have "?corec x = | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 244 | CONS (Datatype.Leaf (fst p)) (?corec (snd p))" | 
| 18400 | 245 | by (simp add: split_def LList_corec) | 
| 246 | with L have ?CONS by auto | |
| 247 | then show ?thesis .. | |
| 248 | qed | |
| 249 | qed | |
| 250 | qed | |
| 251 | ||
| 252 | lemma llist_corec: | |
| 253 | "llist_corec a f = | |
| 254 | (case f a of None \<Rightarrow> LNil | Some (z, w) \<Rightarrow> LCons z (llist_corec w f))" | |
| 255 | proof (cases "f a") | |
| 256 | case None | |
| 257 | then show ?thesis | |
| 258 | by (simp add: llist_corec_def LList_corec LNil_def) | |
| 259 | next | |
| 260 | case (Some p) | |
| 261 | ||
| 262 | let "?corec a" = "llist_corec a f" | |
| 263 | let "?rep_corec a" = | |
| 264 | "LList_corec a | |
| 265 | (\<lambda>z. case f z of None \<Rightarrow> None | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 266 | | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w))" | 
| 18400 | 267 | |
| 268 | have "?corec a = Abs_llist (?rep_corec a)" | |
| 269 | by (simp only: llist_corec_def) | |
| 270 | also from Some have "?rep_corec a = | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 271 | CONS (Datatype.Leaf (fst p)) (?rep_corec (snd p))" | 
| 18400 | 272 | by (simp add: split_def LList_corec) | 
| 273 | also have "?rep_corec (snd p) = Rep_llist (?corec (snd p))" | |
| 274 | by (simp only: llist_corec_def Abs_llist_inverse LList_corec_type2) | |
| 275 | finally have "?corec a = LCons (fst p) (?corec (snd p))" | |
| 276 | by (simp only: LCons_def) | |
| 277 | with Some show ?thesis by (simp add: split_def) | |
| 278 | qed | |
| 279 | ||
| 280 | ||
| 281 | subsection {* Equality as greatest fixed-point; the bisimulation principle. *}
 | |
| 282 | ||
| 283 | consts | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 284 |   EqLList :: "('a Datatype.item \<times> 'a Datatype.item) set \<Rightarrow>
 | 
| 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 285 |     ('a Datatype.item \<times> 'a Datatype.item) set"
 | 
| 18400 | 286 | |
| 287 | coinductive "EqLList r" | |
| 288 | intros | |
| 289 | EqNIL: "(NIL, NIL) \<in> EqLList r" | |
| 290 | EqCONS: "(a, b) \<in> r \<Longrightarrow> (M, N) \<in> EqLList r \<Longrightarrow> | |
| 291 | (CONS a M, CONS b N) \<in> EqLList r" | |
| 292 | ||
| 293 | lemma EqLList_unfold: | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 294 |     "EqLList r = dsum (diag {Datatype.Numb 0}) (dprod r (EqLList r))"
 | 
| 18400 | 295 | by (fast intro!: EqLList.intros [unfolded NIL_def CONS_def] | 
| 296 | elim: EqLList.cases [unfolded NIL_def CONS_def]) | |
| 297 | ||
| 298 | lemma EqLList_implies_ntrunc_equality: | |
| 299 | "(M, N) \<in> EqLList (diag A) \<Longrightarrow> ntrunc k M = ntrunc k N" | |
| 20503 | 300 | apply (induct k arbitrary: M N rule: nat_less_induct) | 
| 18400 | 301 | apply (erule EqLList.cases) | 
| 302 | apply (safe del: equalityI) | |
| 303 | apply (case_tac n) | |
| 304 | apply simp | |
| 305 | apply (rename_tac n') | |
| 306 | apply (case_tac n') | |
| 307 | apply (simp_all add: CONS_def less_Suc_eq) | |
| 308 | done | |
| 309 | ||
| 310 | lemma Domain_EqLList: "Domain (EqLList (diag A)) \<subseteq> LList A" | |
| 311 | apply (simp add: LList.defs NIL_def CONS_def) | |
| 312 | apply (rule gfp_upperbound) | |
| 313 | apply (subst EqLList_unfold) | |
| 314 | apply auto | |
| 315 | done | |
| 316 | ||
| 317 | lemma EqLList_diag: "EqLList (diag A) = diag (LList A)" | |
| 318 | (is "?lhs = ?rhs") | |
| 319 | proof | |
| 320 | show "?lhs \<subseteq> ?rhs" | |
| 321 | apply (rule subsetI) | |
| 322 | apply (rule_tac p = x in PairE) | |
| 323 | apply clarify | |
| 324 | apply (rule diag_eqI) | |
| 325 | apply (rule EqLList_implies_ntrunc_equality [THEN ntrunc_equality], | |
| 326 | assumption) | |
| 327 | apply (erule DomainI [THEN Domain_EqLList [THEN subsetD]]) | |
| 328 | done | |
| 329 | show "?rhs \<subseteq> ?lhs" | |
| 330 | proof | |
| 331 | fix p assume "p \<in> diag (LList A)" | |
| 332 | then show "p \<in> EqLList (diag A)" | |
| 333 | proof coinduct | |
| 334 | case (EqLList q) | |
| 335 | then obtain L where L: "L \<in> LList A" and q: "q = (L, L)" .. | |
| 336 | from L show ?case | |
| 337 | proof cases | |
| 338 | case NIL with q have ?EqNIL by simp | |
| 339 | then show ?thesis .. | |
| 340 | next | |
| 341 | case CONS with q have ?EqCONS by (simp add: diagI) | |
| 342 | then show ?thesis .. | |
| 343 | qed | |
| 344 | qed | |
| 345 | qed | |
| 346 | qed | |
| 347 | ||
| 348 | lemma EqLList_diag_iff [iff]: "(p \<in> EqLList (diag A)) = (p \<in> diag (LList A))" | |
| 349 | by (simp only: EqLList_diag) | |
| 350 | ||
| 351 | ||
| 352 | text {*
 | |
| 353 | To show two LLists are equal, exhibit a bisimulation! (Also admits | |
| 354 | true equality.) | |
| 355 | *} | |
| 356 | ||
| 357 | lemma LList_equalityI | |
| 358 | [consumes 1, case_names EqLList, case_conclusion EqLList EqNIL EqCONS]: | |
| 359 | assumes r: "(M, N) \<in> r" | |
| 360 | and step: "\<And>p. p \<in> r \<Longrightarrow> | |
| 361 | p = (NIL, NIL) \<or> | |
| 362 | (\<exists>M N a b. | |
| 363 | p = (CONS a M, CONS b N) \<and> (a, b) \<in> diag A \<and> | |
| 364 | (M, N) \<in> r \<union> EqLList (diag A))" | |
| 365 | shows "M = N" | |
| 366 | proof - | |
| 367 | from r have "(M, N) \<in> EqLList (diag A)" | |
| 368 | proof coinduct | |
| 369 | case EqLList | |
| 370 | then show ?case by (rule step) | |
| 371 | qed | |
| 372 | then show ?thesis by auto | |
| 373 | qed | |
| 374 | ||
| 375 | lemma LList_fun_equalityI | |
| 376 | [consumes 1, case_names NIL_type NIL CONS, case_conclusion CONS EqNIL EqCONS]: | |
| 377 | assumes M: "M \<in> LList A" | |
| 378 | and fun_NIL: "g NIL \<in> LList A" "f NIL = g NIL" | |
| 379 | and fun_CONS: "\<And>x l. x \<in> A \<Longrightarrow> l \<in> LList A \<Longrightarrow> | |
| 380 | (f (CONS x l), g (CONS x l)) = (NIL, NIL) \<or> | |
| 381 | (\<exists>M N a b. | |
| 382 | (f (CONS x l), g (CONS x l)) = (CONS a M, CONS b N) \<and> | |
| 383 | (a, b) \<in> diag A \<and> | |
| 384 |                 (M, N) \<in> {(f u, g u) | u. u \<in> LList A} \<union> diag (LList A))"
 | |
| 385 | (is "\<And>x l. _ \<Longrightarrow> _ \<Longrightarrow> ?fun_CONS x l") | |
| 386 | shows "f M = g M" | |
| 387 | proof - | |
| 388 |   let ?bisim = "{(f L, g L) | L. L \<in> LList A}"
 | |
| 389 | have "(f M, g M) \<in> ?bisim" using M by blast | |
| 390 | then show ?thesis | |
| 391 | proof (coinduct taking: A rule: LList_equalityI) | |
| 392 | case (EqLList q) | |
| 393 | then obtain L where q: "q = (f L, g L)" and L: "L \<in> LList A" by blast | |
| 394 | from L show ?case | |
| 395 | proof (cases L) | |
| 396 | case NIL | |
| 397 | with fun_NIL and q have "q \<in> diag (LList A)" by auto | |
| 398 | then have "q \<in> EqLList (diag A)" .. | |
| 399 | then show ?thesis by cases simp_all | |
| 400 | next | |
| 401 | case (CONS K a) | |
| 402 | from fun_CONS and `a \<in> A` `K \<in> LList A` | |
| 403 | have "?fun_CONS a K" (is "?NIL \<or> ?CONS") . | |
| 404 | then show ?thesis | |
| 405 | proof | |
| 406 | assume ?NIL | |
| 407 | with q CONS have "q \<in> diag (LList A)" by auto | |
| 408 | then have "q \<in> EqLList (diag A)" .. | |
| 409 | then show ?thesis by cases simp_all | |
| 410 | next | |
| 411 | assume ?CONS | |
| 412 | with CONS obtain a b M N where | |
| 413 | fg: "(f L, g L) = (CONS a M, CONS b N)" | |
| 414 | and ab: "(a, b) \<in> diag A" | |
| 415 | and MN: "(M, N) \<in> ?bisim \<union> diag (LList A)" | |
| 416 | by blast | |
| 417 | from MN show ?thesis | |
| 418 | proof | |
| 419 | assume "(M, N) \<in> ?bisim" | |
| 420 | with q fg ab show ?thesis by simp | |
| 421 | next | |
| 422 | assume "(M, N) \<in> diag (LList A)" | |
| 423 | then have "(M, N) \<in> EqLList (diag A)" .. | |
| 424 | with q fg ab show ?thesis by simp | |
| 425 | qed | |
| 426 | qed | |
| 427 | qed | |
| 428 | qed | |
| 429 | qed | |
| 430 | ||
| 431 | text {*
 | |
| 432 |   Finality of @{text "llist A"}: Uniqueness of functions defined by corecursion.
 | |
| 433 | *} | |
| 434 | ||
| 435 | lemma equals_LList_corec: | |
| 436 | assumes h: "\<And>x. h x = | |
| 437 | (case f x of None \<Rightarrow> NIL | Some (z, w) \<Rightarrow> CONS z (h w))" | |
| 438 | shows "h x = (\<lambda>x. LList_corec x f) x" | |
| 439 | proof - | |
| 440 | def h' \<equiv> "\<lambda>x. LList_corec x f" | |
| 441 | then have h': "\<And>x. h' x = | |
| 442 | (case f x of None \<Rightarrow> NIL | Some (z, w) \<Rightarrow> CONS z (h' w))" | |
| 18730 | 443 | unfolding h'_def by (simp add: LList_corec) | 
| 18400 | 444 |   have "(h x, h' x) \<in> {(h u, h' u) | u. True}" by blast
 | 
| 445 | then show "h x = h' x" | |
| 446 | proof (coinduct rule: LList_equalityI [where A = UNIV]) | |
| 447 | case (EqLList q) | |
| 448 | then obtain x where q: "q = (h x, h' x)" by blast | |
| 449 | show ?case | |
| 450 | proof (cases "f x") | |
| 451 | case None | |
| 452 | with h h' q have ?EqNIL by simp | |
| 453 | then show ?thesis .. | |
| 454 | next | |
| 455 | case (Some p) | |
| 456 | with h h' q have "q = | |
| 457 | (CONS (fst p) (h (snd p)), CONS (fst p) (h' (snd p)))" | |
| 458 | by (simp add: split_def) | |
| 459 | then have ?EqCONS by (auto iff: diag_iff) | |
| 460 | then show ?thesis .. | |
| 461 | qed | |
| 462 | qed | |
| 463 | qed | |
| 464 | ||
| 465 | ||
| 466 | lemma llist_equalityI | |
| 467 | [consumes 1, case_names Eqllist, case_conclusion Eqllist EqLNil EqLCons]: | |
| 468 | assumes r: "(l1, l2) \<in> r" | |
| 469 | and step: "\<And>q. q \<in> r \<Longrightarrow> | |
| 470 | q = (LNil, LNil) \<or> | |
| 471 | (\<exists>l1 l2 a b. | |
| 472 | q = (LCons a l1, LCons b l2) \<and> a = b \<and> | |
| 473 | ((l1, l2) \<in> r \<or> l1 = l2))" | |
| 474 | (is "\<And>q. _ \<Longrightarrow> ?EqLNil q \<or> ?EqLCons q") | |
| 475 | shows "l1 = l2" | |
| 476 | proof - | |
| 477 | def M \<equiv> "Rep_llist l1" and N \<equiv> "Rep_llist l2" | |
| 478 |   with r have "(M, N) \<in> {(Rep_llist l1, Rep_llist l2) | l1 l2. (l1, l2) \<in> r}"
 | |
| 479 | by blast | |
| 480 | then have "M = N" | |
| 481 | proof (coinduct rule: LList_equalityI [where A = UNIV]) | |
| 482 | case (EqLList q) | |
| 483 | then obtain l1 l2 where | |
| 484 | q: "q = (Rep_llist l1, Rep_llist l2)" and r: "(l1, l2) \<in> r" | |
| 485 | by auto | |
| 486 | from step [OF r] show ?case | |
| 487 | proof | |
| 488 | assume "?EqLNil (l1, l2)" | |
| 489 | with q have ?EqNIL by (simp add: Rep_llist_LNil) | |
| 490 | then show ?thesis .. | |
| 491 | next | |
| 492 | assume "?EqLCons (l1, l2)" | |
| 493 | with q have ?EqCONS | |
| 494 | by (force simp add: Rep_llist_LCons EqLList_diag intro: Rep_llist_UNIV) | |
| 495 | then show ?thesis .. | |
| 496 | qed | |
| 497 | qed | |
| 498 | then show ?thesis by (simp add: M_def N_def Rep_llist_inject) | |
| 499 | qed | |
| 500 | ||
| 501 | lemma llist_fun_equalityI | |
| 502 | [case_names LNil LCons, case_conclusion LCons EqLNil EqLCons]: | |
| 503 | assumes fun_LNil: "f LNil = g LNil" | |
| 504 | and fun_LCons: "\<And>x l. | |
| 505 | (f (LCons x l), g (LCons x l)) = (LNil, LNil) \<or> | |
| 506 | (\<exists>l1 l2 a b. | |
| 507 | (f (LCons x l), g (LCons x l)) = (LCons a l1, LCons b l2) \<and> | |
| 508 |             a = b \<and> ((l1, l2) \<in> {(f u, g u) | u. True} \<or> l1 = l2))"
 | |
| 509 | (is "\<And>x l. ?fun_LCons x l") | |
| 510 | shows "f l = g l" | |
| 511 | proof - | |
| 512 |   have "(f l, g l) \<in> {(f l, g l) | l. True}" by blast
 | |
| 513 | then show ?thesis | |
| 514 | proof (coinduct rule: llist_equalityI) | |
| 515 | case (Eqllist q) | |
| 516 | then obtain l where q: "q = (f l, g l)" by blast | |
| 517 | show ?case | |
| 518 | proof (cases l) | |
| 519 | case LNil | |
| 520 | with fun_LNil and q have "q = (g LNil, g LNil)" by simp | |
| 521 | then show ?thesis by (cases "g LNil") simp_all | |
| 522 | next | |
| 523 | case (LCons x l') | |
| 524 | with `?fun_LCons x l'` q LCons show ?thesis by blast | |
| 525 | qed | |
| 526 | qed | |
| 527 | qed | |
| 528 | ||
| 529 | ||
| 530 | subsection {* Derived operations -- both on the set and abstract type *}
 | |
| 531 | ||
| 532 | subsubsection {* @{text Lconst} *}
 | |
| 533 | ||
| 19086 | 534 | definition | 
| 18400 | 535 | "Lconst M \<equiv> lfp (\<lambda>N. CONS M N)" | 
| 536 | ||
| 537 | lemma Lconst_fun_mono: "mono (CONS M)" | |
| 538 | by (simp add: monoI CONS_mono) | |
| 539 | ||
| 540 | lemma Lconst: "Lconst M = CONS M (Lconst M)" | |
| 541 | by (rule Lconst_def [THEN def_lfp_unfold]) (rule Lconst_fun_mono) | |
| 542 | ||
| 543 | lemma Lconst_type: | |
| 544 | assumes "M \<in> A" | |
| 545 | shows "Lconst M \<in> LList A" | |
| 546 | proof - | |
| 547 |   have "Lconst M \<in> {Lconst M}" by simp
 | |
| 548 | then show ?thesis | |
| 549 | proof coinduct | |
| 550 | case (LList N) | |
| 551 | then have "N = Lconst M" by simp | |
| 552 | also have "\<dots> = CONS M (Lconst M)" by (rule Lconst) | |
| 553 | finally have ?CONS using `M \<in> A` by simp | |
| 554 | then show ?case .. | |
| 555 | qed | |
| 556 | qed | |
| 557 | ||
| 558 | lemma Lconst_eq_LList_corec: "Lconst M = LList_corec M (\<lambda>x. Some (x, x))" | |
| 559 | apply (rule equals_LList_corec) | |
| 560 | apply simp | |
| 561 | apply (rule Lconst) | |
| 562 | done | |
| 563 | ||
| 564 | lemma gfp_Lconst_eq_LList_corec: | |
| 565 | "gfp (\<lambda>N. CONS M N) = LList_corec M (\<lambda>x. Some(x, x))" | |
| 566 | apply (rule equals_LList_corec) | |
| 567 | apply simp | |
| 568 | apply (rule Lconst_fun_mono [THEN gfp_unfold]) | |
| 569 | done | |
| 570 | ||
| 571 | ||
| 572 | subsubsection {* @{text Lmap} and @{text lmap} *}
 | |
| 573 | ||
| 19086 | 574 | definition | 
| 575 | "Lmap f M = LList_corec M (List_case None (\<lambda>x M'. Some (f x, M')))" | |
| 576 | "lmap f l = llist_corec l | |
| 18400 | 577 | (\<lambda>z. | 
| 578 | case z of LNil \<Rightarrow> None | |
| 579 | | LCons y z \<Rightarrow> Some (f y, z))" | |
| 580 | ||
| 581 | lemma Lmap_NIL [simp]: "Lmap f NIL = NIL" | |
| 582 | and Lmap_CONS [simp]: "Lmap f (CONS M N) = CONS (f M) (Lmap f N)" | |
| 583 | by (simp_all add: Lmap_def LList_corec) | |
| 584 | ||
| 585 | lemma Lmap_type: | |
| 586 | assumes M: "M \<in> LList A" | |
| 587 | and f: "\<And>x. x \<in> A \<Longrightarrow> f x \<in> B" | |
| 588 | shows "Lmap f M \<in> LList B" | |
| 589 | proof - | |
| 590 |   from M have "Lmap f M \<in> {Lmap f N | N. N \<in> LList A}" by blast
 | |
| 591 | then show ?thesis | |
| 592 | proof coinduct | |
| 593 | case (LList L) | |
| 594 | then obtain N where L: "L = Lmap f N" and N: "N \<in> LList A" by blast | |
| 595 | from N show ?case | |
| 596 | proof cases | |
| 597 | case NIL | |
| 598 | with L have ?NIL by simp | |
| 599 | then show ?thesis .. | |
| 600 | next | |
| 601 | case (CONS K a) | |
| 602 | with f L have ?CONS by auto | |
| 603 | then show ?thesis .. | |
| 604 | qed | |
| 605 | qed | |
| 606 | qed | |
| 607 | ||
| 608 | lemma Lmap_compose: | |
| 609 | assumes M: "M \<in> LList A" | |
| 610 | shows "Lmap (f o g) M = Lmap f (Lmap g M)" (is "?lhs M = ?rhs M") | |
| 611 | proof - | |
| 612 |   have "(?lhs M, ?rhs M) \<in> {(?lhs N, ?rhs N) | N. N \<in> LList A}"
 | |
| 613 | using M by blast | |
| 614 | then show ?thesis | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 615 | proof (coinduct taking: "range (\<lambda>N :: 'a Datatype.item. N)" | 
| 18400 | 616 | rule: LList_equalityI) | 
| 617 | case (EqLList q) | |
| 618 | then obtain N where q: "q = (?lhs N, ?rhs N)" and N: "N \<in> LList A" by blast | |
| 619 | from N show ?case | |
| 620 | proof cases | |
| 621 | case NIL | |
| 622 | with q have ?EqNIL by simp | |
| 623 | then show ?thesis .. | |
| 624 | next | |
| 625 | case CONS | |
| 626 | with q have ?EqCONS by auto | |
| 627 | then show ?thesis .. | |
| 628 | qed | |
| 629 | qed | |
| 630 | qed | |
| 631 | ||
| 632 | lemma Lmap_ident: | |
| 633 | assumes M: "M \<in> LList A" | |
| 634 | shows "Lmap (\<lambda>x. x) M = M" (is "?lmap M = _") | |
| 635 | proof - | |
| 636 |   have "(?lmap M, M) \<in> {(?lmap N, N) | N. N \<in> LList A}" using M by blast
 | |
| 637 | then show ?thesis | |
| 20820 
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
 wenzelm parents: 
20802diff
changeset | 638 | proof (coinduct taking: "range (\<lambda>N :: 'a Datatype.item. N)" | 
| 18400 | 639 | rule: LList_equalityI) | 
| 640 | case (EqLList q) | |
| 641 | then obtain N where q: "q = (?lmap N, N)" and N: "N \<in> LList A" by blast | |
| 642 | from N show ?case | |
| 643 | proof cases | |
| 644 | case NIL | |
| 645 | with q have ?EqNIL by simp | |
| 646 | then show ?thesis .. | |
| 647 | next | |
| 648 | case CONS | |
| 649 | with q have ?EqCONS by auto | |
| 650 | then show ?thesis .. | |
| 651 | qed | |
| 652 | qed | |
| 653 | qed | |
| 654 | ||
| 655 | lemma lmap_LNil [simp]: "lmap f LNil = LNil" | |
| 656 | and lmap_LCons [simp]: "lmap f (LCons M N) = LCons (f M) (lmap f N)" | |
| 657 | by (simp_all add: lmap_def llist_corec) | |
| 658 | ||
| 659 | lemma lmap_compose [simp]: "lmap (f o g) l = lmap f (lmap g l)" | |
| 660 | by (coinduct _ _ l rule: llist_fun_equalityI) auto | |
| 661 | ||
| 662 | lemma lmap_ident [simp]: "lmap (\<lambda>x. x) l = l" | |
| 663 | by (coinduct _ _ l rule: llist_fun_equalityI) auto | |
| 664 | ||
| 665 | ||
| 666 | ||
| 667 | subsubsection {* @{text Lappend} *}
 | |
| 668 | ||
| 19086 | 669 | definition | 
| 670 | "Lappend M N = LList_corec (M, N) | |
| 18400 | 671 | (split (List_case | 
| 672 | (List_case None (\<lambda>N1 N2. Some (N1, (NIL, N2)))) | |
| 673 | (\<lambda>M1 M2 N. Some (M1, (M2, N)))))" | |
| 19086 | 674 | "lappend l n = llist_corec (l, n) | 
| 18400 | 675 | (split (llist_case | 
| 676 | (llist_case None (\<lambda>n1 n2. Some (n1, (LNil, n2)))) | |
| 677 | (\<lambda>l1 l2 n. Some (l1, (l2, n)))))" | |
| 678 | ||
| 679 | lemma Lappend_NIL_NIL [simp]: | |
| 680 | "Lappend NIL NIL = NIL" | |
| 681 | and Lappend_NIL_CONS [simp]: | |
| 682 | "Lappend NIL (CONS N N') = CONS N (Lappend NIL N')" | |
| 683 | and Lappend_CONS [simp]: | |
| 684 | "Lappend (CONS M M') N = CONS M (Lappend M' N)" | |
| 685 | by (simp_all add: Lappend_def LList_corec) | |
| 686 | ||
| 687 | lemma Lappend_NIL [simp]: "M \<in> LList A \<Longrightarrow> Lappend NIL M = M" | |
| 688 | by (erule LList_fun_equalityI) auto | |
| 689 | ||
| 690 | lemma Lappend_NIL2: "M \<in> LList A \<Longrightarrow> Lappend M NIL = M" | |
| 691 | by (erule LList_fun_equalityI) auto | |
| 692 | ||
| 693 | lemma Lappend_type: | |
| 694 | assumes M: "M \<in> LList A" and N: "N \<in> LList A" | |
| 695 | shows "Lappend M N \<in> LList A" | |
| 696 | proof - | |
| 697 |   have "Lappend M N \<in> {Lappend u v | u v. u \<in> LList A \<and> v \<in> LList A}"
 | |
| 698 | using M N by blast | |
| 699 | then show ?thesis | |
| 700 | proof coinduct | |
| 701 | case (LList L) | |
| 702 | then obtain M N where L: "L = Lappend M N" | |
| 703 | and M: "M \<in> LList A" and N: "N \<in> LList A" | |
| 704 | by blast | |
| 705 | from M show ?case | |
| 706 | proof cases | |
| 707 | case NIL | |
| 708 | from N show ?thesis | |
| 709 | proof cases | |
| 710 | case NIL | |
| 711 | with L and `M = NIL` have ?NIL by simp | |
| 712 | then show ?thesis .. | |
| 713 | next | |
| 714 | case CONS | |
| 715 | with L and `M = NIL` have ?CONS by simp | |
| 716 | then show ?thesis .. | |
| 717 | qed | |
| 718 | next | |
| 719 | case CONS | |
| 720 | with L N have ?CONS by auto | |
| 721 | then show ?thesis .. | |
| 722 | qed | |
| 723 | qed | |
| 724 | qed | |
| 725 | ||
| 726 | lemma lappend_LNil_LNil [simp]: "lappend LNil LNil = LNil" | |
| 727 | and lappend_LNil_LCons [simp]: "lappend LNil (LCons l l') = LCons l (lappend LNil l')" | |
| 728 | and lappend_LCons [simp]: "lappend (LCons l l') m = LCons l (lappend l' m)" | |
| 729 | by (simp_all add: lappend_def llist_corec) | |
| 730 | ||
| 731 | lemma lappend_LNil1 [simp]: "lappend LNil l = l" | |
| 732 | by (coinduct _ _ l rule: llist_fun_equalityI) auto | |
| 733 | ||
| 734 | lemma lappend_LNil2 [simp]: "lappend l LNil = l" | |
| 735 | by (coinduct _ _ l rule: llist_fun_equalityI) auto | |
| 736 | ||
| 737 | lemma lappend_assoc: "lappend (lappend l1 l2) l3 = lappend l1 (lappend l2 l3)" | |
| 738 | by (coinduct _ _ l1 rule: llist_fun_equalityI) auto | |
| 739 | ||
| 740 | lemma lmap_lappend_distrib: "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)" | |
| 741 | by (coinduct _ _ l rule: llist_fun_equalityI) auto | |
| 742 | ||
| 743 | ||
| 744 | subsection{* iterates *}
 | |
| 745 | ||
| 746 | text {* @{text llist_fun_equalityI} cannot be used here! *}
 | |
| 747 | ||
| 19086 | 748 | definition | 
| 18400 | 749 |   iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist"
 | 
| 19086 | 750 | "iterates f a = llist_corec a (\<lambda>x. Some (x, f x))" | 
| 18400 | 751 | |
| 752 | lemma iterates: "iterates f x = LCons x (iterates f (f x))" | |
| 753 | apply (unfold iterates_def) | |
| 754 | apply (subst llist_corec) | |
| 755 | apply simp | |
| 756 | done | |
| 757 | ||
| 758 | lemma lmap_iterates: "lmap f (iterates f x) = iterates f (f x)" | |
| 759 | proof - | |
| 760 | have "(lmap f (iterates f x), iterates f (f x)) \<in> | |
| 761 |     {(lmap f (iterates f u), iterates f (f u)) | u. True}" by blast
 | |
| 762 | then show ?thesis | |
| 763 | proof (coinduct rule: llist_equalityI) | |
| 764 | case (Eqllist q) | |
| 765 | then obtain x where q: "q = (lmap f (iterates f x), iterates f (f x))" | |
| 766 | by blast | |
| 767 | also have "iterates f (f x) = LCons (f x) (iterates f (f (f x)))" | |
| 768 | by (subst iterates) rule | |
| 769 | also have "iterates f x = LCons x (iterates f (f x))" | |
| 770 | by (subst iterates) rule | |
| 771 | finally have ?EqLCons by auto | |
| 772 | then show ?case .. | |
| 773 | qed | |
| 774 | qed | |
| 775 | ||
| 776 | lemma iterates_lmap: "iterates f x = LCons x (lmap f (iterates f x))" | |
| 777 | by (subst lmap_iterates) (rule iterates) | |
| 778 | ||
| 779 | ||
| 780 | subsection{* A rather complex proof about iterates -- cf.\ Andy Pitts *}
 | |
| 781 | ||
| 782 | lemma funpow_lmap: | |
| 783 | fixes f :: "'a \<Rightarrow> 'a" | |
| 784 | shows "(lmap f ^ n) (LCons b l) = LCons ((f ^ n) b) ((lmap f ^ n) l)" | |
| 785 | by (induct n) simp_all | |
| 786 | ||
| 787 | ||
| 788 | lemma iterates_equality: | |
| 789 | assumes h: "\<And>x. h x = LCons x (lmap f (h x))" | |
| 790 | shows "h = iterates f" | |
| 791 | proof | |
| 792 | fix x | |
| 793 | have "(h x, iterates f x) \<in> | |
| 794 |       {((lmap f ^ n) (h u), (lmap f ^ n) (iterates f u)) | u n. True}"
 | |
| 795 | proof - | |
| 796 | have "(h x, iterates f x) = ((lmap f ^ 0) (h x), (lmap f ^ 0) (iterates f x))" | |
| 797 | by simp | |
| 798 | then show ?thesis by blast | |
| 799 | qed | |
| 800 | then show "h x = iterates f x" | |
| 801 | proof (coinduct rule: llist_equalityI) | |
| 802 | case (Eqllist q) | |
| 803 | then obtain u n where "q = ((lmap f ^ n) (h u), (lmap f ^ n) (iterates f u))" | |
| 804 | (is "_ = (?q1, ?q2)") | |
| 805 | by auto | |
| 806 | also have "?q1 = LCons ((f ^ n) u) ((lmap f ^ Suc n) (h u))" | |
| 807 | proof - | |
| 808 | have "?q1 = (lmap f ^ n) (LCons u (lmap f (h u)))" | |
| 809 | by (subst h) rule | |
| 810 | also have "\<dots> = LCons ((f ^ n) u) ((lmap f ^ n) (lmap f (h u)))" | |
| 811 | by (rule funpow_lmap) | |
| 812 | also have "(lmap f ^ n) (lmap f (h u)) = (lmap f ^ Suc n) (h u)" | |
| 813 | by (simp add: funpow_swap1) | |
| 814 | finally show ?thesis . | |
| 815 | qed | |
| 816 | also have "?q2 = LCons ((f ^ n) u) ((lmap f ^ Suc n) (iterates f u))" | |
| 817 | proof - | |
| 818 | have "?q2 = (lmap f ^ n) (LCons u (iterates f (f u)))" | |
| 819 | by (subst iterates) rule | |
| 820 | also have "\<dots> = LCons ((f ^ n) u) ((lmap f ^ n) (iterates f (f u)))" | |
| 821 | by (rule funpow_lmap) | |
| 822 | also have "(lmap f ^ n) (iterates f (f u)) = (lmap f ^ Suc n) (iterates f u)" | |
| 823 | by (simp add: lmap_iterates funpow_swap1) | |
| 824 | finally show ?thesis . | |
| 825 | qed | |
| 826 | finally have ?EqLCons by (auto simp del: funpow.simps) | |
| 827 | then show ?case .. | |
| 828 | qed | |
| 829 | qed | |
| 830 | ||
| 831 | lemma lappend_iterates: "lappend (iterates f x) l = iterates f x" | |
| 832 | proof - | |
| 833 | have "(lappend (iterates f x) l, iterates f x) \<in> | |
| 834 |     {(lappend (iterates f u) l, iterates f u) | u. True}" by blast
 | |
| 835 | then show ?thesis | |
| 836 | proof (coinduct rule: llist_equalityI) | |
| 837 | case (Eqllist q) | |
| 838 | then obtain x where "q = (lappend (iterates f x) l, iterates f x)" by blast | |
| 839 | also have "iterates f x = LCons x (iterates f (f x))" by (rule iterates) | |
| 840 | finally have ?EqLCons by auto | |
| 841 | then show ?case .. | |
| 842 | qed | |
| 843 | qed | |
| 844 | ||
| 845 | end |