src/HOL/Library/Coinductive_List.thy
author chaieb
Mon Feb 09 17:21:46 2009 +0000 (2009-02-09)
changeset 29847 af32126ee729
parent 28856 5e009a80fe6d
child 30198 922f944f03b2
permissions -rw-r--r--
added Determinants to Library
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
haftmann@28693
   153
code_datatype LNil LCons
haftmann@28693
   154
wenzelm@18400
   155
lemma LCons_not_LNil [iff]: "LCons x xs \<noteq> LNil"
wenzelm@18400
   156
  apply (simp add: LNil_def LCons_def)
wenzelm@18400
   157
  apply (subst Abs_llist_inject)
wenzelm@18400
   158
    apply (auto intro: NIL_type CONS_type Rep_llist)
wenzelm@18400
   159
  done
wenzelm@18400
   160
wenzelm@18400
   161
lemma LNil_not_LCons [iff]: "LNil \<noteq> LCons x xs"
wenzelm@18400
   162
  by (rule LCons_not_LNil [symmetric])
wenzelm@18400
   163
wenzelm@18400
   164
lemma LCons_inject [iff]: "(LCons x xs = LCons y ys) = (x = y \<and> xs = ys)"
wenzelm@18400
   165
  apply (simp add: LCons_def)
wenzelm@18400
   166
  apply (subst Abs_llist_inject)
wenzelm@18400
   167
    apply (auto simp add: Rep_llist_inject intro: CONS_type Rep_llist)
wenzelm@18400
   168
  done
wenzelm@18400
   169
wenzelm@18400
   170
lemma Rep_llist_LNil: "Rep_llist LNil = NIL"
wenzelm@18400
   171
  by (simp add: LNil_def add: Abs_llist_inverse NIL_type)
wenzelm@18400
   172
wenzelm@18400
   173
lemma Rep_llist_LCons: "Rep_llist (LCons x l) =
wenzelm@20820
   174
    CONS (Datatype.Leaf x) (Rep_llist l)"
wenzelm@18400
   175
  by (simp add: LCons_def Abs_llist_inverse CONS_type Rep_llist)
wenzelm@18400
   176
wenzelm@20802
   177
lemma llist_cases [cases type: llist]:
wenzelm@20802
   178
  obtains
wenzelm@20802
   179
    (LNil) "l = LNil"
wenzelm@20802
   180
  | (LCons) x l' where "l = LCons x l'"
wenzelm@18400
   181
proof (cases l)
wenzelm@18400
   182
  case (Abs_llist L)
wenzelm@20820
   183
  from `L \<in> llist` have "L \<in> LList (range Datatype.Leaf)" by (rule llistD)
wenzelm@18400
   184
  then show ?thesis
wenzelm@18400
   185
  proof cases
wenzelm@18400
   186
    case NIL
wenzelm@18400
   187
    with Abs_llist have "l = LNil" by (simp add: LNil_def)
wenzelm@18400
   188
    with LNil show ?thesis .
wenzelm@18400
   189
  next
berghofe@23755
   190
    case (CONS a K)
wenzelm@18400
   191
    then have "K \<in> llist" by (blast intro: llistI)
wenzelm@18400
   192
    then obtain l' where "K = Rep_llist l'" by cases
wenzelm@18400
   193
    with CONS and Abs_llist obtain x where "l = LCons x l'"
wenzelm@18400
   194
      by (auto simp add: LCons_def Abs_llist_inject)
wenzelm@18400
   195
    with LCons show ?thesis .
wenzelm@18400
   196
  qed
wenzelm@18400
   197
qed
wenzelm@18400
   198
wenzelm@18400
   199
wenzelm@19086
   200
definition
haftmann@28693
   201
  [code del]: "llist_case c d l =
wenzelm@20820
   202
    List_case c (\<lambda>x y. d (inv Datatype.Leaf x) (Abs_llist y)) (Rep_llist l)"
wenzelm@20770
   203
wenzelm@20770
   204
syntax  (* FIXME? *)
wenzelm@20770
   205
  LNil :: logic
wenzelm@20770
   206
  LCons :: logic
wenzelm@18400
   207
translations
wenzelm@20770
   208
  "case p of LNil \<Rightarrow> a | LCons x l \<Rightarrow> b" \<rightleftharpoons> "CONST llist_case a (\<lambda>x l. b) p"
wenzelm@18400
   209
haftmann@28693
   210
lemma llist_case_LNil [simp, code]: "llist_case c d LNil = c"
wenzelm@18400
   211
  by (simp add: llist_case_def LNil_def
wenzelm@18400
   212
    NIL_type Abs_llist_inverse)
wenzelm@18400
   213
haftmann@28693
   214
lemma llist_case_LCons [simp, code]: "llist_case c d (LCons M N) = d M N"
wenzelm@18400
   215
  by (simp add: llist_case_def LCons_def
wenzelm@18400
   216
    CONS_type Abs_llist_inverse Rep_llist Rep_llist_inverse inj_Leaf)
wenzelm@18400
   217
haftmann@28693
   218
lemma llist_case_cert:
haftmann@28693
   219
  assumes "CASE \<equiv> llist_case c d"
wenzelm@28856
   220
  shows "(CASE LNil \<equiv> c) &&& (CASE (LCons M N) \<equiv> d M N)"
haftmann@28693
   221
  using assms by simp_all
haftmann@28693
   222
haftmann@28693
   223
setup {*
haftmann@28693
   224
  Code.add_case @{thm llist_case_cert}
haftmann@28693
   225
*}
wenzelm@18400
   226
wenzelm@19086
   227
definition
haftmann@28693
   228
  [code del]: "llist_corec a f =
wenzelm@18400
   229
    Abs_llist (LList_corec a
wenzelm@18400
   230
      (\<lambda>z.
wenzelm@18400
   231
        case f z of None \<Rightarrow> None
wenzelm@20820
   232
        | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)))"
wenzelm@18400
   233
wenzelm@18400
   234
lemma LList_corec_type2:
wenzelm@18400
   235
  "LList_corec a
wenzelm@18400
   236
    (\<lambda>z. case f z of None \<Rightarrow> None
wenzelm@20820
   237
      | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w)) \<in> llist"
wenzelm@18400
   238
  (is "?corec a \<in> _")
wenzelm@18400
   239
proof (unfold llist_def)
wenzelm@18400
   240
  let "LList_corec a ?g" = "?corec a"
berghofe@23755
   241
  have "\<exists>x. ?corec a = ?corec x" by blast
wenzelm@20820
   242
  then show "?corec a \<in> LList (range Datatype.Leaf)"
wenzelm@18400
   243
  proof coinduct
wenzelm@18400
   244
    case (LList L)
wenzelm@18400
   245
    then obtain x where L: "L = ?corec x" by blast
wenzelm@18400
   246
    show ?case
wenzelm@18400
   247
    proof (cases "f x")
wenzelm@18400
   248
      case None
wenzelm@18400
   249
      then have "?corec x = NIL"
wenzelm@18400
   250
        by (simp add: LList_corec)
wenzelm@18400
   251
      with L have ?NIL by simp
wenzelm@18400
   252
      then show ?thesis ..
wenzelm@18400
   253
    next
wenzelm@18400
   254
      case (Some p)
wenzelm@18400
   255
      then have "?corec x =
wenzelm@20820
   256
          CONS (Datatype.Leaf (fst p)) (?corec (snd p))"
berghofe@22780
   257
        by (simp add: LList_corec split: prod.split)
wenzelm@18400
   258
      with L have ?CONS by auto
wenzelm@18400
   259
      then show ?thesis ..
wenzelm@18400
   260
    qed
wenzelm@18400
   261
  qed
wenzelm@18400
   262
qed
wenzelm@18400
   263
haftmann@28693
   264
lemma llist_corec [code]:
wenzelm@18400
   265
  "llist_corec a f =
wenzelm@18400
   266
    (case f a of None \<Rightarrow> LNil | Some (z, w) \<Rightarrow> LCons z (llist_corec w f))"
wenzelm@18400
   267
proof (cases "f a")
wenzelm@18400
   268
  case None
wenzelm@18400
   269
  then show ?thesis
wenzelm@18400
   270
    by (simp add: llist_corec_def LList_corec LNil_def)
wenzelm@18400
   271
next
wenzelm@18400
   272
  case (Some p)
wenzelm@18400
   273
wenzelm@18400
   274
  let "?corec a" = "llist_corec a f"
wenzelm@18400
   275
  let "?rep_corec a" =
wenzelm@18400
   276
    "LList_corec a
wenzelm@18400
   277
      (\<lambda>z. case f z of None \<Rightarrow> None
wenzelm@20820
   278
        | Some (v, w) \<Rightarrow> Some (Datatype.Leaf v, w))"
wenzelm@18400
   279
wenzelm@18400
   280
  have "?corec a = Abs_llist (?rep_corec a)"
wenzelm@18400
   281
    by (simp only: llist_corec_def)
wenzelm@18400
   282
  also from Some have "?rep_corec a =
wenzelm@20820
   283
      CONS (Datatype.Leaf (fst p)) (?rep_corec (snd p))"
berghofe@22780
   284
    by (simp add: LList_corec split: prod.split)
wenzelm@18400
   285
  also have "?rep_corec (snd p) = Rep_llist (?corec (snd p))"
wenzelm@18400
   286
    by (simp only: llist_corec_def Abs_llist_inverse LList_corec_type2)
wenzelm@18400
   287
  finally have "?corec a = LCons (fst p) (?corec (snd p))"
wenzelm@18400
   288
    by (simp only: LCons_def)
berghofe@22780
   289
  with Some show ?thesis by (simp split: prod.split)
wenzelm@18400
   290
qed
wenzelm@18400
   291
wenzelm@18400
   292
wenzelm@22367
   293
subsection {* Equality as greatest fixed-point -- the bisimulation principle *}
wenzelm@18400
   294
wenzelm@24860
   295
coinductive_set EqLList for r
wenzelm@24860
   296
where EqNIL: "(NIL, NIL) \<in> EqLList r"
berghofe@23755
   297
  | EqCONS: "(a, b) \<in> r \<Longrightarrow> (M, N) \<in> EqLList r \<Longrightarrow>
wenzelm@18400
   298
      (CONS a M, CONS b N) \<in> EqLList r"
wenzelm@18400
   299
wenzelm@18400
   300
lemma EqLList_unfold:
wenzelm@20820
   301
    "EqLList r = dsum (diag {Datatype.Numb 0}) (dprod r (EqLList r))"
wenzelm@18400
   302
  by (fast intro!: EqLList.intros [unfolded NIL_def CONS_def]
wenzelm@18400
   303
           elim: EqLList.cases [unfolded NIL_def CONS_def])
wenzelm@18400
   304
wenzelm@18400
   305
lemma EqLList_implies_ntrunc_equality:
wenzelm@18400
   306
    "(M, N) \<in> EqLList (diag A) \<Longrightarrow> ntrunc k M = ntrunc k N"
wenzelm@20503
   307
  apply (induct k arbitrary: M N rule: nat_less_induct)
wenzelm@18400
   308
  apply (erule EqLList.cases)
wenzelm@18400
   309
   apply (safe del: equalityI)
wenzelm@18400
   310
  apply (case_tac n)
wenzelm@18400
   311
   apply simp
wenzelm@18400
   312
  apply (rename_tac n')
wenzelm@18400
   313
  apply (case_tac n')
wenzelm@18400
   314
   apply (simp_all add: CONS_def less_Suc_eq)
wenzelm@18400
   315
  done
wenzelm@18400
   316
wenzelm@18400
   317
lemma Domain_EqLList: "Domain (EqLList (diag A)) \<subseteq> LList A"
berghofe@23755
   318
  apply (rule subsetI)
berghofe@23755
   319
  apply (erule LList.coinduct)
berghofe@23755
   320
  apply (subst (asm) EqLList_unfold)
berghofe@23755
   321
  apply (auto simp add: NIL_def CONS_def)
wenzelm@18400
   322
  done
wenzelm@18400
   323
wenzelm@18400
   324
lemma EqLList_diag: "EqLList (diag A) = diag (LList A)"
wenzelm@18400
   325
  (is "?lhs = ?rhs")
wenzelm@18400
   326
proof
wenzelm@18400
   327
  show "?lhs \<subseteq> ?rhs"
wenzelm@18400
   328
    apply (rule subsetI)
wenzelm@18400
   329
    apply (rule_tac p = x in PairE)
wenzelm@18400
   330
    apply clarify
wenzelm@18400
   331
    apply (rule diag_eqI)
wenzelm@18400
   332
     apply (rule EqLList_implies_ntrunc_equality [THEN ntrunc_equality],
wenzelm@18400
   333
       assumption)
wenzelm@18400
   334
    apply (erule DomainI [THEN Domain_EqLList [THEN subsetD]])
wenzelm@18400
   335
    done
berghofe@23755
   336
  {
berghofe@23755
   337
    fix M N assume "(M, N) \<in> diag (LList A)"
berghofe@23755
   338
    then have "(M, N) \<in> EqLList (diag A)"
wenzelm@18400
   339
    proof coinduct
berghofe@23755
   340
      case (EqLList M N)
berghofe@23755
   341
      then obtain L where L: "L \<in> LList A" and MN: "M = L" "N = L" by blast
wenzelm@18400
   342
      from L show ?case
wenzelm@18400
   343
      proof cases
berghofe@23755
   344
        case NIL with MN have ?EqNIL by simp
wenzelm@18400
   345
        then show ?thesis ..
wenzelm@18400
   346
      next
berghofe@23755
   347
        case CONS with MN have ?EqCONS by (simp add: diagI)
wenzelm@18400
   348
        then show ?thesis ..
wenzelm@18400
   349
      qed
wenzelm@18400
   350
    qed
berghofe@23755
   351
  }
berghofe@23755
   352
  then show "?rhs \<subseteq> ?lhs" by auto
wenzelm@18400
   353
qed
wenzelm@18400
   354
wenzelm@18400
   355
lemma EqLList_diag_iff [iff]: "(p \<in> EqLList (diag A)) = (p \<in> diag (LList A))"
wenzelm@18400
   356
  by (simp only: EqLList_diag)
wenzelm@18400
   357
wenzelm@18400
   358
wenzelm@18400
   359
text {*
wenzelm@18400
   360
  To show two LLists are equal, exhibit a bisimulation!  (Also admits
wenzelm@18400
   361
  true equality.)
wenzelm@18400
   362
*}
wenzelm@18400
   363
wenzelm@18400
   364
lemma LList_equalityI
wenzelm@18400
   365
  [consumes 1, case_names EqLList, case_conclusion EqLList EqNIL EqCONS]:
wenzelm@18400
   366
  assumes r: "(M, N) \<in> r"
berghofe@23755
   367
    and step: "\<And>M N. (M, N) \<in> r \<Longrightarrow>
berghofe@23755
   368
      M = NIL \<and> N = NIL \<or>
berghofe@23755
   369
        (\<exists>a b M' N'.
berghofe@23755
   370
          M = CONS a M' \<and> N = CONS b N' \<and> (a, b) \<in> diag A \<and>
berghofe@23755
   371
            ((M', N') \<in> r \<or> (M', N') \<in> EqLList (diag A)))"
wenzelm@18400
   372
  shows "M = N"
wenzelm@18400
   373
proof -
wenzelm@18400
   374
  from r have "(M, N) \<in> EqLList (diag A)"
wenzelm@18400
   375
  proof coinduct
wenzelm@18400
   376
    case EqLList
wenzelm@18400
   377
    then show ?case by (rule step)
wenzelm@18400
   378
  qed
wenzelm@18400
   379
  then show ?thesis by auto
wenzelm@18400
   380
qed
wenzelm@18400
   381
wenzelm@18400
   382
lemma LList_fun_equalityI
wenzelm@18400
   383
  [consumes 1, case_names NIL_type NIL CONS, case_conclusion CONS EqNIL EqCONS]:
wenzelm@18400
   384
  assumes M: "M \<in> LList A"
wenzelm@18400
   385
    and fun_NIL: "g NIL \<in> LList A"  "f NIL = g NIL"
wenzelm@18400
   386
    and fun_CONS: "\<And>x l. x \<in> A \<Longrightarrow> l \<in> LList A \<Longrightarrow>
wenzelm@18400
   387
            (f (CONS x l), g (CONS x l)) = (NIL, NIL) \<or>
wenzelm@18400
   388
            (\<exists>M N a b.
wenzelm@18400
   389
              (f (CONS x l), g (CONS x l)) = (CONS a M, CONS b N) \<and>
wenzelm@18400
   390
                (a, b) \<in> diag A \<and>
wenzelm@18400
   391
                (M, N) \<in> {(f u, g u) | u. u \<in> LList A} \<union> diag (LList A))"
wenzelm@18400
   392
      (is "\<And>x l. _ \<Longrightarrow> _ \<Longrightarrow> ?fun_CONS x l")
wenzelm@18400
   393
  shows "f M = g M"
wenzelm@18400
   394
proof -
wenzelm@18400
   395
  let ?bisim = "{(f L, g L) | L. L \<in> LList A}"
wenzelm@18400
   396
  have "(f M, g M) \<in> ?bisim" using M by blast
wenzelm@18400
   397
  then show ?thesis
wenzelm@18400
   398
  proof (coinduct taking: A rule: LList_equalityI)
berghofe@23755
   399
    case (EqLList M N)
berghofe@23755
   400
    then obtain L where MN: "M = f L" "N = g L" and L: "L \<in> LList A" by blast
wenzelm@18400
   401
    from L show ?case
wenzelm@18400
   402
    proof (cases L)
wenzelm@18400
   403
      case NIL
berghofe@23755
   404
      with fun_NIL and MN 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
berghofe@23755
   408
      case (CONS a K)
wenzelm@18400
   409
      from fun_CONS and `a \<in> A` `K \<in> LList A`
wenzelm@18400
   410
      have "?fun_CONS a K" (is "?NIL \<or> ?CONS") .
wenzelm@18400
   411
      then show ?thesis
wenzelm@18400
   412
      proof
wenzelm@18400
   413
        assume ?NIL
berghofe@23755
   414
        with MN CONS have "(M, N) \<in> diag (LList A)" by auto
berghofe@23755
   415
        then have "(M, N) \<in> EqLList (diag A)" ..
wenzelm@18400
   416
        then show ?thesis by cases simp_all
wenzelm@18400
   417
      next
wenzelm@18400
   418
        assume ?CONS
berghofe@23755
   419
        with CONS obtain a b M' N' where
berghofe@23755
   420
            fg: "(f L, g L) = (CONS a M', CONS b N')"
wenzelm@18400
   421
          and ab: "(a, b) \<in> diag A"
berghofe@23755
   422
          and M'N': "(M', N') \<in> ?bisim \<union> diag (LList A)"
wenzelm@18400
   423
          by blast
berghofe@23755
   424
        from M'N' show ?thesis
wenzelm@18400
   425
        proof
berghofe@23755
   426
          assume "(M', N') \<in> ?bisim"
berghofe@23755
   427
          with MN fg ab show ?thesis by simp
wenzelm@18400
   428
        next
berghofe@23755
   429
          assume "(M', N') \<in> diag (LList A)"
berghofe@23755
   430
          then have "(M', N') \<in> EqLList (diag A)" ..
berghofe@23755
   431
          with MN fg ab show ?thesis by simp
wenzelm@18400
   432
        qed
wenzelm@18400
   433
      qed
wenzelm@18400
   434
    qed
wenzelm@18400
   435
  qed
wenzelm@18400
   436
qed
wenzelm@18400
   437
wenzelm@18400
   438
text {*
wenzelm@18400
   439
  Finality of @{text "llist A"}: Uniqueness of functions defined by corecursion.
wenzelm@18400
   440
*}
wenzelm@18400
   441
wenzelm@18400
   442
lemma equals_LList_corec:
wenzelm@18400
   443
  assumes h: "\<And>x. h x =
wenzelm@18400
   444
    (case f x of None \<Rightarrow> NIL | Some (z, w) \<Rightarrow> CONS z (h w))"
wenzelm@18400
   445
  shows "h x = (\<lambda>x. LList_corec x f) x"
wenzelm@18400
   446
proof -
wenzelm@18400
   447
  def h' \<equiv> "\<lambda>x. LList_corec x f"
wenzelm@18400
   448
  then have h': "\<And>x. h' x =
wenzelm@18400
   449
      (case f x of None \<Rightarrow> NIL | Some (z, w) \<Rightarrow> CONS z (h' w))"
wenzelm@18730
   450
    unfolding h'_def by (simp add: LList_corec)
wenzelm@18400
   451
  have "(h x, h' x) \<in> {(h u, h' u) | u. True}" by blast
wenzelm@18400
   452
  then show "h x = h' x"
wenzelm@24863
   453
  proof (coinduct taking: UNIV rule: LList_equalityI)
berghofe@23755
   454
    case (EqLList M N)
berghofe@23755
   455
    then obtain x where MN: "M = h x" "N = h' x" by blast
wenzelm@18400
   456
    show ?case
wenzelm@18400
   457
    proof (cases "f x")
wenzelm@18400
   458
      case None
berghofe@23755
   459
      with h h' MN have ?EqNIL by simp
wenzelm@18400
   460
      then show ?thesis ..
wenzelm@18400
   461
    next
wenzelm@18400
   462
      case (Some p)
berghofe@23755
   463
      with h h' MN have "M = CONS (fst p) (h (snd p))"
berghofe@23755
   464
	and "N = CONS (fst p) (h' (snd p))"
berghofe@23755
   465
        by (simp_all split: prod.split)
wenzelm@18400
   466
      then have ?EqCONS by (auto iff: diag_iff)
wenzelm@18400
   467
      then show ?thesis ..
wenzelm@18400
   468
    qed
wenzelm@18400
   469
  qed
wenzelm@18400
   470
qed
wenzelm@18400
   471
wenzelm@18400
   472
wenzelm@18400
   473
lemma llist_equalityI
wenzelm@18400
   474
  [consumes 1, case_names Eqllist, case_conclusion Eqllist EqLNil EqLCons]:
wenzelm@18400
   475
  assumes r: "(l1, l2) \<in> r"
wenzelm@18400
   476
    and step: "\<And>q. q \<in> r \<Longrightarrow>
wenzelm@18400
   477
      q = (LNil, LNil) \<or>
wenzelm@18400
   478
        (\<exists>l1 l2 a b.
wenzelm@18400
   479
          q = (LCons a l1, LCons b l2) \<and> a = b \<and>
wenzelm@18400
   480
            ((l1, l2) \<in> r \<or> l1 = l2))"
wenzelm@18400
   481
      (is "\<And>q. _ \<Longrightarrow> ?EqLNil q \<or> ?EqLCons q")
wenzelm@18400
   482
  shows "l1 = l2"
wenzelm@18400
   483
proof -
wenzelm@18400
   484
  def M \<equiv> "Rep_llist l1" and N \<equiv> "Rep_llist l2"
wenzelm@18400
   485
  with r have "(M, N) \<in> {(Rep_llist l1, Rep_llist l2) | l1 l2. (l1, l2) \<in> r}"
wenzelm@18400
   486
    by blast
wenzelm@18400
   487
  then have "M = N"
wenzelm@24863
   488
  proof (coinduct taking: UNIV rule: LList_equalityI)
berghofe@23755
   489
    case (EqLList M N)
wenzelm@18400
   490
    then obtain l1 l2 where
berghofe@23755
   491
        MN: "M = Rep_llist l1" "N = Rep_llist l2" and r: "(l1, l2) \<in> r"
wenzelm@18400
   492
      by auto
wenzelm@18400
   493
    from step [OF r] show ?case
wenzelm@18400
   494
    proof
wenzelm@18400
   495
      assume "?EqLNil (l1, l2)"
berghofe@23755
   496
      with MN have ?EqNIL by (simp add: Rep_llist_LNil)
wenzelm@18400
   497
      then show ?thesis ..
wenzelm@18400
   498
    next
wenzelm@18400
   499
      assume "?EqLCons (l1, l2)"
berghofe@23755
   500
      with MN have ?EqCONS
wenzelm@18400
   501
        by (force simp add: Rep_llist_LCons EqLList_diag intro: Rep_llist_UNIV)
wenzelm@18400
   502
      then show ?thesis ..
wenzelm@18400
   503
    qed
wenzelm@18400
   504
  qed
wenzelm@18400
   505
  then show ?thesis by (simp add: M_def N_def Rep_llist_inject)
wenzelm@18400
   506
qed
wenzelm@18400
   507
wenzelm@18400
   508
lemma llist_fun_equalityI
wenzelm@18400
   509
  [case_names LNil LCons, case_conclusion LCons EqLNil EqLCons]:
wenzelm@18400
   510
  assumes fun_LNil: "f LNil = g LNil"
wenzelm@18400
   511
    and fun_LCons: "\<And>x l.
wenzelm@18400
   512
      (f (LCons x l), g (LCons x l)) = (LNil, LNil) \<or>
wenzelm@18400
   513
        (\<exists>l1 l2 a b.
wenzelm@18400
   514
          (f (LCons x l), g (LCons x l)) = (LCons a l1, LCons b l2) \<and>
wenzelm@18400
   515
            a = b \<and> ((l1, l2) \<in> {(f u, g u) | u. True} \<or> l1 = l2))"
wenzelm@18400
   516
      (is "\<And>x l. ?fun_LCons x l")
wenzelm@18400
   517
  shows "f l = g l"
wenzelm@18400
   518
proof -
wenzelm@18400
   519
  have "(f l, g l) \<in> {(f l, g l) | l. True}" by blast
wenzelm@18400
   520
  then show ?thesis
wenzelm@18400
   521
  proof (coinduct rule: llist_equalityI)
wenzelm@18400
   522
    case (Eqllist q)
wenzelm@18400
   523
    then obtain l where q: "q = (f l, g l)" by blast
wenzelm@18400
   524
    show ?case
wenzelm@18400
   525
    proof (cases l)
wenzelm@18400
   526
      case LNil
wenzelm@18400
   527
      with fun_LNil and q have "q = (g LNil, g LNil)" by simp
wenzelm@18400
   528
      then show ?thesis by (cases "g LNil") simp_all
wenzelm@18400
   529
    next
wenzelm@18400
   530
      case (LCons x l')
wenzelm@18400
   531
      with `?fun_LCons x l'` q LCons show ?thesis by blast
wenzelm@18400
   532
    qed
wenzelm@18400
   533
  qed
wenzelm@18400
   534
qed
wenzelm@18400
   535
wenzelm@18400
   536
wenzelm@18400
   537
subsection {* Derived operations -- both on the set and abstract type *}
wenzelm@18400
   538
wenzelm@18400
   539
subsubsection {* @{text Lconst} *}
wenzelm@18400
   540
wenzelm@24860
   541
definition "Lconst M \<equiv> lfp (\<lambda>N. CONS M N)"
wenzelm@18400
   542
wenzelm@18400
   543
lemma Lconst_fun_mono: "mono (CONS M)"
wenzelm@18400
   544
  by (simp add: monoI CONS_mono)
wenzelm@18400
   545
wenzelm@18400
   546
lemma Lconst: "Lconst M = CONS M (Lconst M)"
wenzelm@18400
   547
  by (rule Lconst_def [THEN def_lfp_unfold]) (rule Lconst_fun_mono)
wenzelm@18400
   548
wenzelm@18400
   549
lemma Lconst_type:
wenzelm@18400
   550
  assumes "M \<in> A"
wenzelm@18400
   551
  shows "Lconst M \<in> LList A"
wenzelm@18400
   552
proof -
berghofe@23755
   553
  have "Lconst M \<in> {Lconst (id M)}" by simp
wenzelm@18400
   554
  then show ?thesis
wenzelm@18400
   555
  proof coinduct
wenzelm@18400
   556
    case (LList N)
wenzelm@18400
   557
    then have "N = Lconst M" by simp
wenzelm@18400
   558
    also have "\<dots> = CONS M (Lconst M)" by (rule Lconst)
wenzelm@18400
   559
    finally have ?CONS using `M \<in> A` by simp
wenzelm@18400
   560
    then show ?case ..
wenzelm@18400
   561
  qed
wenzelm@18400
   562
qed
wenzelm@18400
   563
wenzelm@18400
   564
lemma Lconst_eq_LList_corec: "Lconst M = LList_corec M (\<lambda>x. Some (x, x))"
wenzelm@18400
   565
  apply (rule equals_LList_corec)
wenzelm@18400
   566
  apply simp
wenzelm@18400
   567
  apply (rule Lconst)
wenzelm@18400
   568
  done
wenzelm@18400
   569
wenzelm@18400
   570
lemma gfp_Lconst_eq_LList_corec:
wenzelm@18400
   571
    "gfp (\<lambda>N. CONS M N) = LList_corec M (\<lambda>x. Some(x, x))"
wenzelm@18400
   572
  apply (rule equals_LList_corec)
wenzelm@18400
   573
  apply simp
wenzelm@18400
   574
  apply (rule Lconst_fun_mono [THEN gfp_unfold])
wenzelm@18400
   575
  done
wenzelm@18400
   576
wenzelm@18400
   577
wenzelm@18400
   578
subsubsection {* @{text Lmap} and @{text lmap} *}
wenzelm@18400
   579
wenzelm@19086
   580
definition
wenzelm@19086
   581
  "Lmap f M = LList_corec M (List_case None (\<lambda>x M'. Some (f x, M')))"
wenzelm@21404
   582
definition
wenzelm@19086
   583
  "lmap f l = llist_corec l
wenzelm@18400
   584
    (\<lambda>z.
wenzelm@18400
   585
      case z of LNil \<Rightarrow> None
wenzelm@18400
   586
      | LCons y z \<Rightarrow> Some (f y, z))"
wenzelm@18400
   587
wenzelm@18400
   588
lemma Lmap_NIL [simp]: "Lmap f NIL = NIL"
wenzelm@18400
   589
  and Lmap_CONS [simp]: "Lmap f (CONS M N) = CONS (f M) (Lmap f N)"
wenzelm@18400
   590
  by (simp_all add: Lmap_def LList_corec)
wenzelm@18400
   591
wenzelm@18400
   592
lemma Lmap_type:
wenzelm@18400
   593
  assumes M: "M \<in> LList A"
wenzelm@18400
   594
    and f: "\<And>x. x \<in> A \<Longrightarrow> f x \<in> B"
wenzelm@18400
   595
  shows "Lmap f M \<in> LList B"
wenzelm@18400
   596
proof -
wenzelm@18400
   597
  from M have "Lmap f M \<in> {Lmap f N | N. N \<in> LList A}" by blast
wenzelm@18400
   598
  then show ?thesis
wenzelm@18400
   599
  proof coinduct
wenzelm@18400
   600
    case (LList L)
wenzelm@18400
   601
    then obtain N where L: "L = Lmap f N" and N: "N \<in> LList A" by blast
wenzelm@18400
   602
    from N show ?case
wenzelm@18400
   603
    proof cases
wenzelm@18400
   604
      case NIL
wenzelm@18400
   605
      with L have ?NIL by simp
wenzelm@18400
   606
      then show ?thesis ..
wenzelm@18400
   607
    next
wenzelm@18400
   608
      case (CONS K a)
wenzelm@18400
   609
      with f L have ?CONS by auto
wenzelm@18400
   610
      then show ?thesis ..
wenzelm@18400
   611
    qed
wenzelm@18400
   612
  qed
wenzelm@18400
   613
qed
wenzelm@18400
   614
wenzelm@18400
   615
lemma Lmap_compose:
wenzelm@18400
   616
  assumes M: "M \<in> LList A"
wenzelm@18400
   617
  shows "Lmap (f o g) M = Lmap f (Lmap g M)"  (is "?lhs M = ?rhs M")
wenzelm@18400
   618
proof -
wenzelm@18400
   619
  have "(?lhs M, ?rhs M) \<in> {(?lhs N, ?rhs N) | N. N \<in> LList A}"
wenzelm@18400
   620
    using M by blast
wenzelm@18400
   621
  then show ?thesis
wenzelm@24863
   622
  proof (coinduct taking: "range (\<lambda>N. N)" rule: LList_equalityI)
berghofe@23755
   623
    case (EqLList L M)
berghofe@23755
   624
    then obtain N where LM: "L = ?lhs N" "M = ?rhs N" and N: "N \<in> LList A" by blast
wenzelm@18400
   625
    from N show ?case
wenzelm@18400
   626
    proof cases
wenzelm@18400
   627
      case NIL
berghofe@23755
   628
      with LM have ?EqNIL by simp
wenzelm@18400
   629
      then show ?thesis ..
wenzelm@18400
   630
    next
wenzelm@18400
   631
      case CONS
berghofe@23755
   632
      with LM have ?EqCONS by auto
wenzelm@18400
   633
      then show ?thesis ..
wenzelm@18400
   634
    qed
wenzelm@18400
   635
  qed
wenzelm@18400
   636
qed
wenzelm@18400
   637
wenzelm@18400
   638
lemma Lmap_ident:
wenzelm@18400
   639
  assumes M: "M \<in> LList A"
wenzelm@18400
   640
  shows "Lmap (\<lambda>x. x) M = M"  (is "?lmap M = _")
wenzelm@18400
   641
proof -
wenzelm@18400
   642
  have "(?lmap M, M) \<in> {(?lmap N, N) | N. N \<in> LList A}" using M by blast
wenzelm@18400
   643
  then show ?thesis
wenzelm@24863
   644
  proof (coinduct taking: "range (\<lambda>N. N)" rule: LList_equalityI)
berghofe@23755
   645
    case (EqLList L M)
berghofe@23755
   646
    then obtain N where LM: "L = ?lmap N" "M = N" and N: "N \<in> LList A" by blast
wenzelm@18400
   647
    from N show ?case
wenzelm@18400
   648
    proof cases
wenzelm@18400
   649
      case NIL
berghofe@23755
   650
      with LM have ?EqNIL by simp
wenzelm@18400
   651
      then show ?thesis ..
wenzelm@18400
   652
    next
wenzelm@18400
   653
      case CONS
berghofe@23755
   654
      with LM have ?EqCONS by auto
wenzelm@18400
   655
      then show ?thesis ..
wenzelm@18400
   656
    qed
wenzelm@18400
   657
  qed
wenzelm@18400
   658
qed
wenzelm@18400
   659
wenzelm@18400
   660
lemma lmap_LNil [simp]: "lmap f LNil = LNil"
wenzelm@18400
   661
  and lmap_LCons [simp]: "lmap f (LCons M N) = LCons (f M) (lmap f N)"
wenzelm@18400
   662
  by (simp_all add: lmap_def llist_corec)
wenzelm@18400
   663
wenzelm@18400
   664
lemma lmap_compose [simp]: "lmap (f o g) l = lmap f (lmap g l)"
wenzelm@24860
   665
  by (coinduct l rule: llist_fun_equalityI) auto
wenzelm@18400
   666
wenzelm@18400
   667
lemma lmap_ident [simp]: "lmap (\<lambda>x. x) l = l"
wenzelm@24860
   668
  by (coinduct l rule: llist_fun_equalityI) auto
wenzelm@18400
   669
wenzelm@18400
   670
wenzelm@18400
   671
wenzelm@18400
   672
subsubsection {* @{text Lappend} *}
wenzelm@18400
   673
wenzelm@19086
   674
definition
wenzelm@19086
   675
  "Lappend M N = LList_corec (M, N)
wenzelm@18400
   676
    (split (List_case
wenzelm@18400
   677
        (List_case None (\<lambda>N1 N2. Some (N1, (NIL, N2))))
wenzelm@18400
   678
        (\<lambda>M1 M2 N. Some (M1, (M2, N)))))"
wenzelm@21404
   679
definition
wenzelm@19086
   680
  "lappend l n = llist_corec (l, n)
wenzelm@18400
   681
    (split (llist_case
wenzelm@18400
   682
        (llist_case None (\<lambda>n1 n2. Some (n1, (LNil, n2))))
wenzelm@18400
   683
        (\<lambda>l1 l2 n. Some (l1, (l2, n)))))"
wenzelm@18400
   684
wenzelm@18400
   685
lemma Lappend_NIL_NIL [simp]:
wenzelm@18400
   686
    "Lappend NIL NIL = NIL"
wenzelm@18400
   687
  and Lappend_NIL_CONS [simp]:
wenzelm@18400
   688
    "Lappend NIL (CONS N N') = CONS N (Lappend NIL N')"
wenzelm@18400
   689
  and Lappend_CONS [simp]:
wenzelm@18400
   690
    "Lappend (CONS M M') N = CONS M (Lappend M' N)"
wenzelm@18400
   691
  by (simp_all add: Lappend_def LList_corec)
wenzelm@18400
   692
wenzelm@18400
   693
lemma Lappend_NIL [simp]: "M \<in> LList A \<Longrightarrow> Lappend NIL M = M"
wenzelm@18400
   694
  by (erule LList_fun_equalityI) auto
wenzelm@18400
   695
wenzelm@18400
   696
lemma Lappend_NIL2: "M \<in> LList A \<Longrightarrow> Lappend M NIL = M"
wenzelm@18400
   697
  by (erule LList_fun_equalityI) auto
wenzelm@18400
   698
wenzelm@18400
   699
lemma Lappend_type:
wenzelm@18400
   700
  assumes M: "M \<in> LList A" and N: "N \<in> LList A"
wenzelm@18400
   701
  shows "Lappend M N \<in> LList A"
wenzelm@18400
   702
proof -
wenzelm@18400
   703
  have "Lappend M N \<in> {Lappend u v | u v. u \<in> LList A \<and> v \<in> LList A}"
wenzelm@18400
   704
    using M N by blast
wenzelm@18400
   705
  then show ?thesis
wenzelm@18400
   706
  proof coinduct
wenzelm@18400
   707
    case (LList L)
wenzelm@18400
   708
    then obtain M N where L: "L = Lappend M N"
wenzelm@18400
   709
        and M: "M \<in> LList A" and N: "N \<in> LList A"
wenzelm@18400
   710
      by blast
wenzelm@18400
   711
    from M show ?case
wenzelm@18400
   712
    proof cases
wenzelm@18400
   713
      case NIL
wenzelm@18400
   714
      from N show ?thesis
wenzelm@18400
   715
      proof cases
wenzelm@18400
   716
        case NIL
wenzelm@18400
   717
        with L and `M = NIL` have ?NIL by simp
wenzelm@18400
   718
        then show ?thesis ..
wenzelm@18400
   719
      next
wenzelm@18400
   720
        case CONS
wenzelm@18400
   721
        with L and `M = NIL` have ?CONS by simp
wenzelm@18400
   722
        then show ?thesis ..
wenzelm@18400
   723
      qed
wenzelm@18400
   724
    next
wenzelm@18400
   725
      case CONS
wenzelm@18400
   726
      with L N have ?CONS by auto
wenzelm@18400
   727
      then show ?thesis ..
wenzelm@18400
   728
    qed
wenzelm@18400
   729
  qed
wenzelm@18400
   730
qed
wenzelm@18400
   731
wenzelm@18400
   732
lemma lappend_LNil_LNil [simp]: "lappend LNil LNil = LNil"
wenzelm@18400
   733
  and lappend_LNil_LCons [simp]: "lappend LNil (LCons l l') = LCons l (lappend LNil l')"
wenzelm@18400
   734
  and lappend_LCons [simp]: "lappend (LCons l l') m = LCons l (lappend l' m)"
wenzelm@18400
   735
  by (simp_all add: lappend_def llist_corec)
wenzelm@18400
   736
wenzelm@18400
   737
lemma lappend_LNil1 [simp]: "lappend LNil l = l"
wenzelm@24860
   738
  by (coinduct l rule: llist_fun_equalityI) auto
wenzelm@18400
   739
wenzelm@18400
   740
lemma lappend_LNil2 [simp]: "lappend l LNil = l"
wenzelm@24860
   741
  by (coinduct l rule: llist_fun_equalityI) auto
wenzelm@18400
   742
wenzelm@18400
   743
lemma lappend_assoc: "lappend (lappend l1 l2) l3 = lappend l1 (lappend l2 l3)"
wenzelm@24860
   744
  by (coinduct l1 rule: llist_fun_equalityI) auto
wenzelm@18400
   745
wenzelm@18400
   746
lemma lmap_lappend_distrib: "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)"
wenzelm@24860
   747
  by (coinduct l rule: llist_fun_equalityI) auto
wenzelm@18400
   748
wenzelm@18400
   749
wenzelm@18400
   750
subsection{* iterates *}
wenzelm@18400
   751
wenzelm@18400
   752
text {* @{text llist_fun_equalityI} cannot be used here! *}
wenzelm@18400
   753
wenzelm@19086
   754
definition
wenzelm@21404
   755
  iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist" where
wenzelm@19086
   756
  "iterates f a = llist_corec a (\<lambda>x. Some (x, f x))"
wenzelm@18400
   757
wenzelm@18400
   758
lemma iterates: "iterates f x = LCons x (iterates f (f x))"
wenzelm@18400
   759
  apply (unfold iterates_def)
wenzelm@18400
   760
  apply (subst llist_corec)
wenzelm@18400
   761
  apply simp
wenzelm@18400
   762
  done
wenzelm@18400
   763
wenzelm@18400
   764
lemma lmap_iterates: "lmap f (iterates f x) = iterates f (f x)"
wenzelm@18400
   765
proof -
wenzelm@18400
   766
  have "(lmap f (iterates f x), iterates f (f x)) \<in>
wenzelm@18400
   767
    {(lmap f (iterates f u), iterates f (f u)) | u. True}" by blast
wenzelm@18400
   768
  then show ?thesis
wenzelm@18400
   769
  proof (coinduct rule: llist_equalityI)
wenzelm@18400
   770
    case (Eqllist q)
wenzelm@18400
   771
    then obtain x where q: "q = (lmap f (iterates f x), iterates f (f x))"
wenzelm@18400
   772
      by blast
wenzelm@18400
   773
    also have "iterates f (f x) = LCons (f x) (iterates f (f (f x)))"
wenzelm@18400
   774
      by (subst iterates) rule
wenzelm@18400
   775
    also have "iterates f x = LCons x (iterates f (f x))"
wenzelm@18400
   776
      by (subst iterates) rule
wenzelm@18400
   777
    finally have ?EqLCons by auto
wenzelm@18400
   778
    then show ?case ..
wenzelm@18400
   779
  qed
wenzelm@18400
   780
qed
wenzelm@18400
   781
wenzelm@18400
   782
lemma iterates_lmap: "iterates f x = LCons x (lmap f (iterates f x))"
wenzelm@18400
   783
  by (subst lmap_iterates) (rule iterates)
wenzelm@18400
   784
wenzelm@18400
   785
wenzelm@18400
   786
subsection{* A rather complex proof about iterates -- cf.\ Andy Pitts *}
wenzelm@18400
   787
wenzelm@18400
   788
lemma funpow_lmap:
wenzelm@18400
   789
  fixes f :: "'a \<Rightarrow> 'a"
wenzelm@18400
   790
  shows "(lmap f ^ n) (LCons b l) = LCons ((f ^ n) b) ((lmap f ^ n) l)"
wenzelm@18400
   791
  by (induct n) simp_all
wenzelm@18400
   792
wenzelm@18400
   793
wenzelm@18400
   794
lemma iterates_equality:
wenzelm@18400
   795
  assumes h: "\<And>x. h x = LCons x (lmap f (h x))"
wenzelm@18400
   796
  shows "h = iterates f"
wenzelm@18400
   797
proof
wenzelm@18400
   798
  fix x
wenzelm@18400
   799
  have "(h x, iterates f x) \<in>
wenzelm@18400
   800
      {((lmap f ^ n) (h u), (lmap f ^ n) (iterates f u)) | u n. True}"
wenzelm@18400
   801
  proof -
wenzelm@18400
   802
    have "(h x, iterates f x) = ((lmap f ^ 0) (h x), (lmap f ^ 0) (iterates f x))"
wenzelm@18400
   803
      by simp
wenzelm@18400
   804
    then show ?thesis by blast
wenzelm@18400
   805
  qed
wenzelm@18400
   806
  then show "h x = iterates f x"
wenzelm@18400
   807
  proof (coinduct rule: llist_equalityI)
wenzelm@18400
   808
    case (Eqllist q)
wenzelm@18400
   809
    then obtain u n where "q = ((lmap f ^ n) (h u), (lmap f ^ n) (iterates f u))"
wenzelm@18400
   810
        (is "_ = (?q1, ?q2)")
wenzelm@18400
   811
      by auto
wenzelm@18400
   812
    also have "?q1 = LCons ((f ^ n) u) ((lmap f ^ Suc n) (h u))"
wenzelm@18400
   813
    proof -
wenzelm@18400
   814
      have "?q1 = (lmap f ^ n) (LCons u (lmap f (h u)))"
wenzelm@18400
   815
        by (subst h) rule
wenzelm@18400
   816
      also have "\<dots> = LCons ((f ^ n) u) ((lmap f ^ n) (lmap f (h u)))"
wenzelm@18400
   817
        by (rule funpow_lmap)
wenzelm@18400
   818
      also have "(lmap f ^ n) (lmap f (h u)) = (lmap f ^ Suc n) (h u)"
wenzelm@18400
   819
        by (simp add: funpow_swap1)
wenzelm@18400
   820
      finally show ?thesis .
wenzelm@18400
   821
    qed
wenzelm@18400
   822
    also have "?q2 = LCons ((f ^ n) u) ((lmap f ^ Suc n) (iterates f u))"
wenzelm@18400
   823
    proof -
wenzelm@18400
   824
      have "?q2 = (lmap f ^ n) (LCons u (iterates f (f u)))"
wenzelm@18400
   825
        by (subst iterates) rule
wenzelm@18400
   826
      also have "\<dots> = LCons ((f ^ n) u) ((lmap f ^ n) (iterates f (f u)))"
wenzelm@18400
   827
        by (rule funpow_lmap)
wenzelm@18400
   828
      also have "(lmap f ^ n) (iterates f (f u)) = (lmap f ^ Suc n) (iterates f u)"
wenzelm@18400
   829
        by (simp add: lmap_iterates funpow_swap1)
wenzelm@18400
   830
      finally show ?thesis .
wenzelm@18400
   831
    qed
wenzelm@18400
   832
    finally have ?EqLCons by (auto simp del: funpow.simps)
wenzelm@18400
   833
    then show ?case ..
wenzelm@18400
   834
  qed
wenzelm@18400
   835
qed
wenzelm@18400
   836
wenzelm@18400
   837
lemma lappend_iterates: "lappend (iterates f x) l = iterates f x"
wenzelm@18400
   838
proof -
wenzelm@18400
   839
  have "(lappend (iterates f x) l, iterates f x) \<in>
wenzelm@18400
   840
    {(lappend (iterates f u) l, iterates f u) | u. True}" by blast
wenzelm@18400
   841
  then show ?thesis
wenzelm@18400
   842
  proof (coinduct rule: llist_equalityI)
wenzelm@18400
   843
    case (Eqllist q)
wenzelm@18400
   844
    then obtain x where "q = (lappend (iterates f x) l, iterates f x)" by blast
wenzelm@18400
   845
    also have "iterates f x = LCons x (iterates f (f x))" by (rule iterates)
wenzelm@18400
   846
    finally have ?EqLCons by auto
wenzelm@18400
   847
    then show ?case ..
wenzelm@18400
   848
  qed
wenzelm@18400
   849
qed
wenzelm@18400
   850
wenzelm@18400
   851
end