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