src/HOLCF/Library/List_Cpo.thy
author nipkow
Tue, 07 Sep 2010 12:04:18 +0200
changeset 39429 720112792ba0
parent 39377 d80990d8b909
child 39442 3989b2b44dba
permissions -rw-r--r--
renamed expand_*_eq in HOLCF as well
huffman@39377
     1
(*  Title:      HOLCF/Library/List_Cpo.thy
huffman@39377
     2
    Author:     Brian Huffman
huffman@39377
     3
*)
huffman@39377
     4
huffman@39377
     5
header {* Lists as a complete partial order *}
huffman@39377
     6
huffman@39377
     7
theory List_Cpo
huffman@39377
     8
imports HOLCF
huffman@39377
     9
begin
huffman@39377
    10
huffman@39377
    11
subsection {* Lists are a partial order *}
huffman@39377
    12
huffman@39377
    13
instantiation list :: (po) po
huffman@39377
    14
begin
huffman@39377
    15
huffman@39377
    16
definition
huffman@39377
    17
  "xs \<sqsubseteq> ys \<longleftrightarrow> list_all2 (op \<sqsubseteq>) xs ys"
huffman@39377
    18
huffman@39377
    19
instance proof
huffman@39377
    20
  fix xs :: "'a list"
huffman@39377
    21
  from below_refl show "xs \<sqsubseteq> xs"
huffman@39377
    22
    unfolding below_list_def
huffman@39377
    23
    by (rule list_all2_refl)
huffman@39377
    24
next
huffman@39377
    25
  fix xs ys zs :: "'a list"
huffman@39377
    26
  assume "xs \<sqsubseteq> ys" and "ys \<sqsubseteq> zs"
huffman@39377
    27
  with below_trans show "xs \<sqsubseteq> zs"
huffman@39377
    28
    unfolding below_list_def
huffman@39377
    29
    by (rule list_all2_trans)
huffman@39377
    30
next
huffman@39377
    31
  fix xs ys zs :: "'a list"
huffman@39377
    32
  assume "xs \<sqsubseteq> ys" and "ys \<sqsubseteq> xs"
huffman@39377
    33
  with below_antisym show "xs = ys"
huffman@39377
    34
    unfolding below_list_def
huffman@39377
    35
    by (rule list_all2_antisym)
huffman@39377
    36
qed
huffman@39377
    37
huffman@39377
    38
end
huffman@39377
    39
huffman@39377
    40
lemma below_list_simps [simp]:
huffman@39377
    41
  "[] \<sqsubseteq> []"
huffman@39377
    42
  "x # xs \<sqsubseteq> y # ys \<longleftrightarrow> x \<sqsubseteq> y \<and> xs \<sqsubseteq> ys"
huffman@39377
    43
  "\<not> [] \<sqsubseteq> y # ys"
huffman@39377
    44
  "\<not> x # xs \<sqsubseteq> []"
huffman@39377
    45
by (simp_all add: below_list_def)
huffman@39377
    46
huffman@39377
    47
lemma Nil_below_iff [simp]: "[] \<sqsubseteq> xs \<longleftrightarrow> xs = []"
huffman@39377
    48
by (cases xs, simp_all)
huffman@39377
    49
huffman@39377
    50
lemma below_Nil_iff [simp]: "xs \<sqsubseteq> [] \<longleftrightarrow> xs = []"
huffman@39377
    51
by (cases xs, simp_all)
huffman@39377
    52
huffman@39377
    53
text "Thanks to Joachim Breitner"
huffman@39377
    54
huffman@39377
    55
lemma list_Cons_below:
huffman@39377
    56
  assumes "a # as \<sqsubseteq> xs"
huffman@39377
    57
  obtains b and bs where "a \<sqsubseteq> b" and "as \<sqsubseteq> bs" and "xs = b # bs"
huffman@39377
    58
  using assms by (cases xs, auto)
huffman@39377
    59
huffman@39377
    60
lemma list_below_Cons:
huffman@39377
    61
  assumes "xs \<sqsubseteq> b # bs"
huffman@39377
    62
  obtains a and as where "a \<sqsubseteq> b" and "as \<sqsubseteq> bs" and "xs = a # as"
huffman@39377
    63
  using assms by (cases xs, auto)
huffman@39377
    64
huffman@39377
    65
lemma hd_mono: "xs \<sqsubseteq> ys \<Longrightarrow> hd xs \<sqsubseteq> hd ys"
huffman@39377
    66
by (cases xs, simp, cases ys, simp, simp)
huffman@39377
    67
huffman@39377
    68
lemma tl_mono: "xs \<sqsubseteq> ys \<Longrightarrow> tl xs \<sqsubseteq> tl ys"
huffman@39377
    69
by (cases xs, simp, cases ys, simp, simp)
huffman@39377
    70
huffman@39377
    71
lemma ch2ch_hd [simp]: "chain (\<lambda>i. S i) \<Longrightarrow> chain (\<lambda>i. hd (S i))"
huffman@39377
    72
by (rule chainI, rule hd_mono, erule chainE)
huffman@39377
    73
huffman@39377
    74
lemma ch2ch_tl [simp]: "chain (\<lambda>i. S i) \<Longrightarrow> chain (\<lambda>i. tl (S i))"
huffman@39377
    75
by (rule chainI, rule tl_mono, erule chainE)
huffman@39377
    76
huffman@39377
    77
lemma below_same_length: "xs \<sqsubseteq> ys \<Longrightarrow> length xs = length ys"
huffman@39377
    78
unfolding below_list_def by (rule list_all2_lengthD)
huffman@39377
    79
huffman@39377
    80
lemma list_chain_cases:
huffman@39377
    81
  assumes S: "chain S"
huffman@39377
    82
  obtains "\<forall>i. S i = []" |
huffman@39377
    83
    A B where "chain A" and "chain B" and "\<forall>i. S i = A i # B i"
huffman@39377
    84
proof (cases "S 0")
huffman@39377
    85
  case Nil
huffman@39377
    86
  have "\<forall>i. S 0 \<sqsubseteq> S i" by (simp add: chain_mono [OF S])
huffman@39377
    87
  with Nil have "\<forall>i. S i = []" by simp
huffman@39377
    88
  thus ?thesis ..
huffman@39377
    89
next
huffman@39377
    90
  case (Cons x xs)
huffman@39377
    91
  have "\<forall>i. S 0 \<sqsubseteq> S i" by (simp add: chain_mono [OF S])
huffman@39377
    92
  hence *: "\<forall>i. S i \<noteq> []" by (rule all_forward) (auto simp add: Cons)
huffman@39377
    93
  let ?A = "\<lambda>i. hd (S i)"
huffman@39377
    94
  let ?B = "\<lambda>i. tl (S i)"
huffman@39377
    95
  from S have "chain ?A" and "chain ?B" by simp_all
huffman@39377
    96
  moreover have "\<forall>i. S i = ?A i # ?B i" by (simp add: *)
huffman@39377
    97
  ultimately show ?thesis ..
huffman@39377
    98
qed
huffman@39377
    99
huffman@39377
   100
subsection {* Lists are a complete partial order *}
huffman@39377
   101
huffman@39377
   102
lemma is_lub_Cons:
huffman@39377
   103
  assumes A: "range A <<| x"
huffman@39377
   104
  assumes B: "range B <<| xs"
huffman@39377
   105
  shows "range (\<lambda>i. A i # B i) <<| x # xs"
huffman@39377
   106
using assms
huffman@39377
   107
unfolding is_lub_def is_ub_def Ball_def [symmetric]
huffman@39377
   108
by (clarsimp, case_tac u, simp_all)
huffman@39377
   109
huffman@39377
   110
lemma list_cpo_lemma:
huffman@39377
   111
  fixes S :: "nat \<Rightarrow> 'a::cpo list"
huffman@39377
   112
  assumes "chain S" and "\<forall>i. length (S i) = n"
huffman@39377
   113
  shows "\<exists>x. range S <<| x"
huffman@39377
   114
using assms
huffman@39377
   115
 apply (induct n arbitrary: S)
huffman@39377
   116
  apply (subgoal_tac "S = (\<lambda>i. [])")
huffman@39377
   117
  apply (fast intro: lub_const)
nipkow@39429
   118
  apply (simp add: ext_iff)
huffman@39377
   119
 apply (drule_tac x="\<lambda>i. tl (S i)" in meta_spec, clarsimp)
huffman@39377
   120
 apply (rule_tac x="(\<Squnion>i. hd (S i)) # x" in exI)
huffman@39377
   121
 apply (subgoal_tac "range (\<lambda>i. hd (S i) # tl (S i)) = range S")
huffman@39377
   122
  apply (erule subst)
huffman@39377
   123
  apply (rule is_lub_Cons)
huffman@39377
   124
   apply (rule thelubE [OF _ refl])
huffman@39377
   125
   apply (erule ch2ch_hd)
huffman@39377
   126
  apply assumption
huffman@39377
   127
 apply (rule_tac f="\<lambda>S. range S" in arg_cong)
huffman@39377
   128
 apply (rule ext)
huffman@39377
   129
 apply (rule hd_Cons_tl)
huffman@39377
   130
 apply (drule_tac x=i in spec, auto)
huffman@39377
   131
done
huffman@39377
   132
huffman@39377
   133
instance list :: (cpo) cpo
huffman@39377
   134
proof
huffman@39377
   135
  fix S :: "nat \<Rightarrow> 'a list"
huffman@39377
   136
  assume "chain S"
huffman@39377
   137
  hence "\<forall>i. S 0 \<sqsubseteq> S i" by (simp add: chain_mono)
huffman@39377
   138
  hence "\<forall>i. length (S i) = length (S 0)"
huffman@39377
   139
    by (fast intro: below_same_length [symmetric])
huffman@39377
   140
  with `chain S` show "\<exists>x. range S <<| x"
huffman@39377
   141
    by (rule list_cpo_lemma)
huffman@39377
   142
qed
huffman@39377
   143
huffman@39377
   144
subsection {* Continuity of list operations *}
huffman@39377
   145
huffman@39377
   146
lemma cont2cont_Cons [simp, cont2cont]:
huffman@39377
   147
  assumes f: "cont (\<lambda>x. f x)"
huffman@39377
   148
  assumes g: "cont (\<lambda>x. g x)"
huffman@39377
   149
  shows "cont (\<lambda>x. f x # g x)"
huffman@39377
   150
apply (rule contI)
huffman@39377
   151
apply (rule is_lub_Cons)
huffman@39377
   152
apply (erule contE [OF f])
huffman@39377
   153
apply (erule contE [OF g])
huffman@39377
   154
done
huffman@39377
   155
huffman@39377
   156
lemma lub_Cons:
huffman@39377
   157
  fixes A :: "nat \<Rightarrow> 'a::cpo"
huffman@39377
   158
  assumes A: "chain A" and B: "chain B"
huffman@39377
   159
  shows "(\<Squnion>i. A i # B i) = (\<Squnion>i. A i) # (\<Squnion>i. B i)"
huffman@39377
   160
by (intro thelubI is_lub_Cons cpo_lubI A B)
huffman@39377
   161
huffman@39377
   162
lemma cont2cont_list_case:
huffman@39377
   163
  assumes f: "cont (\<lambda>x. f x)"
huffman@39377
   164
  assumes g: "cont (\<lambda>x. g x)"
huffman@39377
   165
  assumes h1: "\<And>y ys. cont (\<lambda>x. h x y ys)"
huffman@39377
   166
  assumes h2: "\<And>x ys. cont (\<lambda>y. h x y ys)"
huffman@39377
   167
  assumes h3: "\<And>x y. cont (\<lambda>ys. h x y ys)"
huffman@39377
   168
  shows "cont (\<lambda>x. case f x of [] \<Rightarrow> g x | y # ys \<Rightarrow> h x y ys)"
huffman@39377
   169
apply (rule cont_apply [OF f])
huffman@39377
   170
apply (rule contI)
huffman@39377
   171
apply (erule list_chain_cases)
huffman@39377
   172
apply (simp add: lub_const)
huffman@39377
   173
apply (simp add: lub_Cons)
huffman@39377
   174
apply (simp add: cont2contlubE [OF h2])
huffman@39377
   175
apply (simp add: cont2contlubE [OF h3])
huffman@39377
   176
apply (simp add: diag_lub ch2ch_cont [OF h2] ch2ch_cont [OF h3])
huffman@39377
   177
apply (rule cpo_lubI, rule chainI, rule below_trans)
huffman@39377
   178
apply (erule cont2monofunE [OF h2 chainE])
huffman@39377
   179
apply (erule cont2monofunE [OF h3 chainE])
huffman@39377
   180
apply (case_tac y, simp_all add: g h1)
huffman@39377
   181
done
huffman@39377
   182
huffman@39377
   183
lemma cont2cont_list_case' [simp, cont2cont]:
huffman@39377
   184
  assumes f: "cont (\<lambda>x. f x)"
huffman@39377
   185
  assumes g: "cont (\<lambda>x. g x)"
huffman@39377
   186
  assumes h: "cont (\<lambda>p. h (fst p) (fst (snd p)) (snd (snd p)))"
huffman@39377
   187
  shows "cont (\<lambda>x. case f x of [] \<Rightarrow> g x | y # ys \<Rightarrow> h x y ys)"
huffman@39377
   188
proof -
huffman@39377
   189
  have "\<And>y ys. cont (\<lambda>x. h x (fst (y, ys)) (snd (y, ys)))"
huffman@39377
   190
    by (rule h [THEN cont_fst_snd_D1])
huffman@39377
   191
  hence h1: "\<And>y ys. cont (\<lambda>x. h x y ys)" by simp
huffman@39377
   192
  note h2 = h [THEN cont_fst_snd_D2, THEN cont_fst_snd_D1]
huffman@39377
   193
  note h3 = h [THEN cont_fst_snd_D2, THEN cont_fst_snd_D2]
huffman@39377
   194
  from f g h1 h2 h3 show ?thesis by (rule cont2cont_list_case)
huffman@39377
   195
qed
huffman@39377
   196
huffman@39377
   197
text {* The simple version (due to Joachim Breitner) is needed if the
huffman@39377
   198
  element type of the list is not a cpo. *}
huffman@39377
   199
huffman@39377
   200
lemma cont2cont_list_case_simple [simp, cont2cont]:
huffman@39377
   201
  assumes "cont (\<lambda>x. f1 x)"
huffman@39377
   202
  assumes "\<And>y ys. cont (\<lambda>x. f2 x y ys)"
huffman@39377
   203
  shows "cont (\<lambda>x. case l of [] \<Rightarrow> f1 x | y # ys \<Rightarrow> f2 x y ys)"
huffman@39377
   204
using assms by (cases l) auto
huffman@39377
   205
huffman@39377
   206
text {* There are probably lots of other list operations that also
huffman@39377
   207
deserve to have continuity lemmas.  I'll add more as they are
huffman@39377
   208
needed. *}
huffman@39377
   209
huffman@39377
   210
end