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