src/HOL/Library/Diagonal_Subsequence.thy
 changeset 50087 635d73673b5e child 51526 155263089e7b
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Library/Diagonal_Subsequence.thy	Thu Nov 15 10:49:58 2012 +0100
1.3 @@ -0,0 +1,111 @@
1.4 +(* Author: Fabian Immler, TUM *)
1.5 +
1.6 +header {* Sequence of Properties on Subsequences *}
1.7 +
1.8 +theory Diagonal_Subsequence
1.9 +imports SEQ
1.10 +begin
1.11 +
1.12 +locale subseqs =
1.13 +  fixes P::"nat\<Rightarrow>(nat\<Rightarrow>nat)\<Rightarrow>bool"
1.14 +  assumes ex_subseq: "\<And>n s. subseq s \<Longrightarrow> \<exists>r'. subseq r' \<and> P n (s o r')"
1.15 +begin
1.16 +
1.17 +primrec seqseq where
1.18 +  "seqseq 0 = id"
1.19 +| "seqseq (Suc n) = seqseq n o (SOME r'. subseq r' \<and> P n (seqseq n o r'))"
1.20 +
1.21 +lemma seqseq_ex:
1.22 +  shows "subseq (seqseq n) \<and>
1.23 +  (\<exists>r'. seqseq (Suc n) = seqseq n o r' \<and> subseq r' \<and> P n (seqseq n o r'))"
1.24 +proof (induct n)
1.25 +  case 0
1.26 +  let ?P = "\<lambda>r'. subseq r' \<and> P 0 r'"
1.27 +  let ?r = "Eps ?P"
1.28 +  have "?P ?r" using ex_subseq[of id 0] by (intro someI_ex[of ?P]) (auto simp: subseq_def)
1.29 +  thus ?case by (auto simp: subseq_def)
1.30 +next
1.31 +  case (Suc n)
1.32 +  then obtain r' where
1.33 +    Suc': "seqseq (Suc n) = seqseq n \<circ> r'" "subseq (seqseq n)" "subseq r'"
1.34 +      "P n (seqseq n o r')"
1.35 +    by blast
1.36 +  let ?P = "\<lambda>r'a. subseq (r'a ) \<and> P (Suc n) (seqseq n o r' o r'a)"
1.37 +  let ?r = "Eps ?P"
1.38 +  have "?P ?r" using ex_subseq[of "seqseq n o r'" "Suc n"] Suc'
1.39 +    by (intro someI_ex[of ?P]) (auto intro: subseq_o simp: o_assoc)
1.40 +  moreover have "seqseq (Suc (Suc n)) = seqseq n \<circ> r' \<circ> ?r"
1.41 +    by (subst seqseq.simps) (simp only: Suc' o_assoc)
1.42 +  moreover note subseq_o[OF `subseq (seqseq n)` `subseq r'`]
1.43 +  ultimately show ?case unfolding Suc' by (auto simp: o_def)
1.44 +qed
1.45 +
1.46 +lemma subseq_seqseq:
1.47 +  shows "subseq (seqseq n)" using seqseq_ex[OF assms] by auto
1.48 +
1.49 +definition reducer where "reducer n = (SOME r'. subseq r' \<and> P n (seqseq n o r'))"
1.50 +
1.51 +lemma subseq_reducer: "subseq (reducer n)" and reducer_reduces: "P n (seqseq n o reducer n)"
1.52 +  unfolding atomize_conj unfolding reducer_def using subseq_seqseq
1.53 +  by (rule someI_ex[OF ex_subseq])
1.54 +
1.55 +lemma seqseq_reducer[simp]:
1.56 +  "seqseq (Suc n) = seqseq n o reducer n"
1.57 +  by (simp add: reducer_def)
1.58 +
1.59 +declare seqseq.simps(2)[simp del]
1.60 +
1.61 +definition diagseq where "diagseq i = seqseq i i"
1.62 +
1.63 +lemma diagseq_mono: "diagseq n < diagseq (Suc n)"
1.64 +  unfolding diagseq_def seqseq_reducer o_def
1.65 +  by (metis subseq_mono[OF subseq_seqseq] less_le_trans lessI seq_suble subseq_reducer)
1.66 +
1.67 +lemma subseq_diagseq: "subseq diagseq"
1.68 +  using diagseq_mono by (simp add: subseq_Suc_iff diagseq_def)
1.69 +
1.70 +primrec fold_reduce where
1.71 +  "fold_reduce n 0 = id"
1.72 +| "fold_reduce n (Suc k) = fold_reduce n k o reducer (n + k)"
1.73 +
1.74 +lemma subseq_fold_reduce: "subseq (fold_reduce n k)"
1.75 +proof (induct k)
1.76 +  case (Suc k) from subseq_o[OF this subseq_reducer] show ?case by (simp add: o_def)
1.77 +qed (simp add: subseq_def)
1.78 +
1.79 +lemma ex_subseq_reduce_index: "seqseq (n + k) = seqseq n o fold_reduce n k"
1.80 +  by (induct k) simp_all
1.81 +
1.82 +lemma seqseq_fold_reduce: "seqseq n = fold_reduce 0 n"
1.83 +  by (induct n) (simp_all)
1.84 +
1.85 +lemma diagseq_fold_reduce: "diagseq n = fold_reduce 0 n n"
1.86 +  using seqseq_fold_reduce by (simp add: diagseq_def)
1.87 +
1.88 +lemma fold_reduce_add: "fold_reduce 0 (m + n) = fold_reduce 0 m o fold_reduce m n"
1.89 +  by (induct n) simp_all
1.90 +
1.91 +lemma diagseq_add: "diagseq (k + n) = (seqseq k o (fold_reduce k n)) (k + n)"
1.92 +proof -
1.93 +  have "diagseq (k + n) = fold_reduce 0 (k + n) (k + n)"
1.94 +    by (simp add: diagseq_fold_reduce)
1.95 +  also have "\<dots> = (seqseq k o fold_reduce k n) (k + n)"
1.96 +    unfolding fold_reduce_add seqseq_fold_reduce ..
1.97 +  finally show ?thesis .
1.98 +qed
1.99 +
1.100 +lemma diagseq_sub:
1.101 +  assumes "m \<le> n" shows "diagseq n = (seqseq m o (fold_reduce m (n - m))) n"
1.102 +  using diagseq_add[of m "n - m"] assms by simp
1.103 +
1.104 +lemma subseq_diagonal_rest: "subseq (\<lambda>x. fold_reduce k x (k + x))"
1.105 +  unfolding subseq_Suc_iff fold_reduce.simps o_def
1.106 +  by (metis subseq_mono[OF subseq_fold_reduce] less_le_trans lessI add_Suc_right seq_suble
1.107 +      subseq_reducer)
1.108 +
1.109 +lemma diagseq_seqseq: "diagseq o (op + k) = (seqseq k o (\<lambda>x. fold_reduce k x (k + x)))"
1.110 +  by (auto simp: o_def diagseq_add)
1.111 +
1.112 +end
1.113 +
1.114 +end
```