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