Moment.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 04 Mar 2014 08:45:11 +0000
changeset 23 24e6884d9258
parent 0 110247f9d47e
child 35 92f61f6a0fe7
permissions -rw-r--r--
made some small chages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Moment
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
imports Main
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
fun firstn :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
  "firstn 0 s = []" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  "firstn (Suc n) [] = []" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  "firstn (Suc n) (e#s) = e#(firstn n s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
fun restn :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
where "restn n s = rev (firstn (length s - n) (rev s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
definition moment :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
where "moment n s = rev (firstn n (rev s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
definition restm :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
where "restm n s = rev (restn n (rev s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
definition from_to :: "nat \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  where "from_to i j s = firstn (j - i) (restn i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
definition down_to :: "nat \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
where "down_to j i s = rev (from_to i j (rev s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
(*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
value "down_to 6 2 [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
value "from_to 2 6 [0, 1, 2, 3, 4, 5, 6, 7]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
*)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
lemma length_eq_elim_l: "\<lbrakk>length xs = length ys; xs@us = ys@vs\<rbrakk> \<Longrightarrow> xs = ys \<and> us = vs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
lemma length_eq_elim_r: "\<lbrakk>length us = length vs; xs@us = ys@vs\<rbrakk> \<Longrightarrow> xs = ys \<and> us = vs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
lemma firstn_nil [simp]: "firstn n [] = []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  by (cases n, simp+)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
(*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
value "from_to 0 2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] @ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
       from_to 2 5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
*)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
lemma firstn_le: "\<And> n s'. n \<le> length s \<Longrightarrow> firstn n (s@s') = firstn n s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
proof (induct s, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  fix a s n s'
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  assume ih: "\<And>n s'. n \<le> length s \<Longrightarrow> firstn n (s @ s') = firstn n s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  and le_n: " n \<le> length (a # s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  show "firstn n ((a # s) @ s') = firstn n (a # s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  proof(cases n, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
    fix k
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
    assume eq_n: "n = Suc k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
    with le_n have "k \<le> length s" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
    from ih [OF this] and eq_n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
    show "firstn n ((a # s) @ s') = firstn n (a # s)" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
lemma firstn_ge [simp]: "\<And>n. length s \<le> n \<Longrightarrow> firstn n s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
proof(induct s, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  fix a s n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  assume ih: "\<And>n. length s \<le> n \<Longrightarrow> firstn n s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
    and le: "length (a # s) \<le> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  show "firstn n (a # s) = a # s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  proof(cases n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    assume eq_n: "n = 0" with le show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
    fix k
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
    assume eq_n: "n = Suc k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    with le have le_k: "length s \<le> k" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
    from ih [OF this] have "firstn k s = s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
    from eq_n and this
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
    show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
lemma firstn_eq [simp]: "firstn (length s) s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
lemma firstn_restn_s: "(firstn n (s::'a list)) @ (restn n s) = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
proof(induct n arbitrary:s, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  fix n s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  assume ih: "\<And>t. firstn n (t::'a list) @ restn n t = t"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  show "firstn (Suc n) (s::'a list) @ restn (Suc n) s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  proof(cases s, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
    fix x xs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
    assume eq_s: "s = x#xs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
    show "firstn (Suc n) s @ restn (Suc n) s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
      have "firstn (Suc n) s @ restn (Suc n) s =  x # (firstn n xs @ restn n xs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
        from eq_s have "firstn (Suc n) s =  x # firstn n xs" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
        moreover have "restn (Suc n) s = restn n xs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
          from eq_s have "restn (Suc n) s = rev (firstn (length xs - n) (rev xs @ [x]))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
          also have "\<dots> = restn n xs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
          proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
            have "(firstn (length xs - n) (rev xs @ [x])) = (firstn (length xs - n) (rev xs))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
              by(rule firstn_le, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
            hence "rev (firstn (length xs - n) (rev xs @ [x])) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
              rev (firstn (length xs - n) (rev xs))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
            also have "\<dots> = rev (firstn (length (rev xs) - n) (rev xs))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
            finally show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
          qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
          finally show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
        ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
      qed with ih eq_s show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
lemma moment_restm_s: "(restm n s)@(moment n s) = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  have " rev  ((firstn n (rev s)) @ (restn n (rev s))) = s" (is "rev ?x = s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
    have "?x = rev s" by (simp only:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
    thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
  thus ?thesis 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
    by (auto simp:restm_def moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
declare restn.simps [simp del] firstn.simps[simp del]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
lemma length_firstn_ge: "length s \<le> n \<Longrightarrow> length (firstn n s) = length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
proof(induct n arbitrary:s, simp add:firstn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  case (Suc k)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  assume ih: "\<And> s. length (s::'a list) \<le> k \<Longrightarrow> length (firstn k s) = length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  and le: "length s \<le> Suc k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  show ?case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
  proof(cases s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
    case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
    from Nil show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
    case (Cons x xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
    from le and Cons have "length xs \<le> k" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
    from ih [OF this] have "length (firstn k xs) = length xs" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
    moreover from Cons have "length (firstn (Suc k) s) = Suc (length (firstn k xs))" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
      by (simp add:firstn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
    moreover note Cons
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
    ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
lemma length_firstn_le: "n \<le> length s \<Longrightarrow> length (firstn n s) = n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
proof(induct n arbitrary:s, simp add:firstn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  case (Suc k)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  assume ih: "\<And>s. k \<le> length (s::'a list) \<Longrightarrow> length (firstn k s) = k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
    and le: "Suc k \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
  show ?case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
  proof(cases s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
    case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
    from Nil and le show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
    case (Cons x xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
    from le and Cons have "k \<le> length xs" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
    from ih [OF this] have "length (firstn k xs) = k" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
    moreover from Cons have "length (firstn (Suc k) s) = Suc (length (firstn k xs))" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
      by (simp add:firstn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
    ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
lemma app_firstn_restn: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
  fixes s1 s2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  shows "s1 = firstn (length s1) (s1 @ s2) \<and> s2 = restn (length s1) (s1 @ s2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
proof(rule length_eq_elim_l)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
  have "length s1 \<le> length (s1 @ s2)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  from length_firstn_le [OF this]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
  show "length s1 = length (firstn (length s1) (s1 @ s2))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
  from firstn_restn_s 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  show "s1 @ s2 = firstn (length s1) (s1 @ s2) @ restn (length s1) (s1 @ s2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
    by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
lemma length_moment_le:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
  fixes k s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  assumes le_k: "k \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
  shows "length (moment k s) = k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
  have "length (rev (firstn k (rev s))) = k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
    have "length (rev (firstn k (rev s))) = length (firstn k (rev s))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
    also have "\<dots> = k" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
    proof(rule length_firstn_le)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
      from le_k show "k \<le> length (rev s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
  thus ?thesis by (simp add:moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
lemma app_moment_restm: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
  fixes s1 s2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  shows "s1 = restm (length s2) (s1 @ s2) \<and> s2 = moment (length s2) (s1 @ s2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
proof(rule length_eq_elim_r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  have "length s2 \<le> length (s1 @ s2)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
  from length_moment_le [OF this]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  show "length s2 = length (moment (length s2) (s1 @ s2))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
  from moment_restm_s 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
  show "s1 @ s2 = restm (length s2) (s1 @ s2) @ moment (length s2) (s1 @ s2)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
    by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
lemma length_moment_ge:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
  fixes k s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  assumes le_k: "length s \<le> k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
  shows "length (moment k s) = (length s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
  have "length (rev (firstn k (rev s))) = length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
    have "length (rev (firstn k (rev s))) = length (firstn k (rev s))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
    also have "\<dots> = length s" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
      have "\<dots> = length (rev s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
      proof(rule length_firstn_ge)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
        from le_k show "length (rev s) \<le> k" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
      also have "\<dots> = length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
      finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
  thus ?thesis by (simp add:moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
lemma length_firstn: "(length (firstn n s) = length s) \<or> (length (firstn n s) = n)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
proof(cases "n \<le> length s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
  case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  from length_firstn_le [OF True] show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
  case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
  from False have "length s \<le> n" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  from firstn_ge [OF this] show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
lemma firstn_conc: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  fixes m n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  assumes le_mn: "m \<le> n"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
  shows "firstn m s = firstn m (firstn n  s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
proof(cases "m \<le> length s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
  case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
  have "s = (firstn n s) @ (restn n s)" by (simp add:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
  hence "firstn m s = firstn m \<dots>" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
  also have "\<dots> = firstn m (firstn n s)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
    from length_firstn [of n s]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
    have "m \<le> length (firstn n s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
    proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
      assume "length (firstn n s) = length s" with True show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
    next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
      assume "length (firstn n s) = n " with le_mn show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
    from firstn_le [OF this, of "restn n s"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
    show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
  finally show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
  case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
  from False and le_mn have "length s \<le> n"  by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
  from firstn_ge [OF this] show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
lemma restn_conc: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
  fixes i j k s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
  assumes eq_k: "j + i = k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
  shows "restn k s = restn j (restn i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
  have "(firstn (length s - k) (rev s)) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
        (firstn (length (rev (firstn (length s - i) (rev s))) - j) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
                            (rev (rev (firstn (length s - i) (rev s)))))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
  proof  -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
    have "(firstn (length s - k) (rev s)) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
            (firstn (length (rev (firstn (length s - i) (rev s))) - j) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
                                           (firstn (length s - i) (rev s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
      have " (length (rev (firstn (length s - i) (rev s))) - j) = length s - k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
        have "(length (rev (firstn (length s - i) (rev s))) - j) = (length s - i) - j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
          have "(length (rev (firstn (length s - i) (rev s))) - j) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
                                         length ((firstn (length s - i) (rev s))) - j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
            by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
          also have "\<dots> = length ((firstn (length (rev s) - i) (rev s))) - j" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
          also have "\<dots> = (length (rev s) - i) - j" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
          proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
            have "length ((firstn (length (rev s) - i) (rev s))) = (length (rev s) - i)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
              by (rule length_firstn_le, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
            thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
          qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
          also have "\<dots> = (length s - i) - j" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
          finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
        with eq_k show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
      moreover have "(firstn (length s - k) (rev s)) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
                             (firstn (length s - k) (firstn (length s - i) (rev s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
      proof(rule firstn_conc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
        from eq_k show "length s - k \<le> length s - i" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
      ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
    thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
  thus ?thesis by (simp only:restn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
(*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
value "down_to 2 0 [5, 4, 3, 2, 1, 0]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
value "moment 2 [5, 4, 3, 2, 1, 0]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
*)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
lemma from_to_firstn: "from_to 0 k s = firstn k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
by (simp add:from_to_def restn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
lemma moment_app [simp]:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
  assumes 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
  ile: "i \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
  shows "moment i (s'@s) = moment i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
  have "moment i (s'@s) = rev (firstn i (rev (s'@s)))" by (simp add:moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
  moreover have "firstn i (rev (s'@s)) = firstn i (rev s @ rev s')" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
  moreover have "\<dots> = firstn i (rev s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
  proof(rule firstn_le)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
    have "length (rev s) = length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
    with ile show "i \<le> length (rev s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
  ultimately show ?thesis by (simp add:moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
lemma moment_eq [simp]: "moment (length s) (s'@s) = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
  have "length s \<le> length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
  from moment_app [OF this, of s'] 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
  have " moment (length s) (s' @ s) = moment (length s) s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   341
  moreover have "\<dots> = s" by (simp add:moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
  ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
lemma moment_ge [simp]: "length s \<le> n \<Longrightarrow> moment n s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
  by (unfold moment_def, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
lemma moment_zero [simp]: "moment 0 s = []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
  by (simp add:moment_def firstn.simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
lemma p_split_gen: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
  "\<lbrakk>Q s; \<not> Q (moment k s)\<rbrakk> \<Longrightarrow>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
  (\<exists> i. i < length s \<and> k \<le> i \<and> \<not> Q (moment i s) \<and> (\<forall> i' > i. Q (moment i' s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
proof (induct s, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
  fix a s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
  assume ih: "\<lbrakk>Q s; \<not> Q (moment k s)\<rbrakk>
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
           \<Longrightarrow> \<exists>i<length s. k \<le> i \<and> \<not> Q (moment i s) \<and> (\<forall>i'>i. Q (moment i' s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
    and nq: "\<not> Q (moment k (a # s))" and qa: "Q (a # s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
  have le_k: "k \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
    { assume "length s < k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
      hence "length (a#s) \<le> k" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
      from moment_ge [OF this] and nq and qa
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
      have "False" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
    } thus ?thesis by arith
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
  have nq_k: "\<not> Q (moment k s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
    have "moment k (a#s) = moment k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
      from moment_app [OF le_k, of "[a]"] show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   373
    with nq show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
  show "\<exists>i<length (a # s). k \<le> i \<and> \<not> Q (moment i (a # s)) \<and> (\<forall>i'>i. Q (moment i' (a # s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
    { assume "Q s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
      from ih [OF this nq_k]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
      obtain i where lti: "i < length s" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
        and nq: "\<not> Q (moment i s)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
        and rst: "\<forall>i'>i. Q (moment i' s)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
        and lki: "k \<le> i" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
      have ?thesis 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
        from lti have "i < length (a # s)" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
        moreover have " \<not> Q (moment i (a # s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
          from lti have "i \<le> (length s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
          from moment_app [OF this, of "[a]"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
          have "moment i (a # s) = moment i s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
          with nq show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   392
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
        moreover have " (\<forall>i'>i. Q (moment i' (a # s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   394
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
          {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
            fix i'
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
            assume lti': "i < i'"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
            have "Q (moment i' (a # s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   399
            proof(cases "length (a#s) \<le> i'")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
              case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   401
              from True have "moment i' (a#s) = a#s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   402
              with qa show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   403
            next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   404
              case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
              from False have "i' \<le> length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   406
              from moment_app [OF this, of "[a]"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
              have "moment i' (a#s) = moment i' s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
              with rst lti' show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
            qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
          } thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
        moreover note lki
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   413
        ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   414
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
    } moreover {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
      assume ns: "\<not> Q s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
      have ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
        let ?i = "length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
        have "\<not> Q (moment ?i (a#s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
          have "?i \<le> length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
          from moment_app [OF this, of "[a]"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   424
          have "moment ?i (a#s) = moment ?i s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   425
          moreover have "\<dots> = s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
          ultimately show ?thesis using ns by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   428
        moreover have "\<forall> i' > ?i. Q (moment i' (a#s))" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   429
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
          { fix i'
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
            assume "i' > ?i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   432
            hence "length (a#s) \<le> i'" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
            from moment_ge [OF this] 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   434
            have " moment i' (a # s) = a # s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
            with qa have "Q (moment i' (a#s))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   436
          } thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   437
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   438
        moreover have "?i < length (a#s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   439
        moreover note le_k
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   440
        ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   441
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   442
    } ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   443
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   444
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   445
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
lemma p_split: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
  "\<And> s Q. \<lbrakk>Q s; \<not> Q []\<rbrakk> \<Longrightarrow> 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
       (\<exists> i. i < length s \<and> \<not> Q (moment i s) \<and> (\<forall> i' > i. Q (moment i' s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
  fix s Q
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
  assume qs: "Q s" and nq: "\<not> Q []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
  from nq have "\<not> Q (moment 0 s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   453
  from p_split_gen [of Q s 0, OF qs this]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
  show "(\<exists> i. i < length s \<and> \<not> Q (moment i s) \<and> (\<forall> i' > i. Q (moment i' s)))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
    by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   458
lemma moment_plus: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   459
  "Suc i \<le> length s \<Longrightarrow> moment (Suc i) s = (hd (moment (Suc i) s)) # (moment i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
proof(induct s, simp+)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
  fix a s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
  assume ih: "Suc i \<le> length s \<Longrightarrow> moment (Suc i) s = hd (moment (Suc i) s) # moment i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
    and le_i: "i \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
  show "moment (Suc i) (a # s) = hd (moment (Suc i) (a # s)) # moment i (a # s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   465
  proof(cases "i= length s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
    case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   467
    hence "Suc i = length (a#s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   468
    with moment_eq have "moment (Suc i) (a#s) = a#s" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
    moreover have "moment i (a#s) = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
      from moment_app [OF le_i, of "[a]"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   472
      and True show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   473
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
    ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
    case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
    from False and le_i have lti: "i < length s" by arith
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
    hence les_i: "Suc i \<le> length s" by arith
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
    show ?thesis 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   481
      from moment_app [OF les_i, of "[a]"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   482
      have "moment (Suc i) (a # s) = moment (Suc i) s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   483
      moreover have "moment i (a#s) = moment i s" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   484
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   485
        from lti have "i \<le> length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
        from moment_app [OF this, of "[a]"] show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   487
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   488
      moreover note ih [OF les_i]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   489
      ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   491
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   492
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   493
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   494
lemma from_to_conc:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   495
  fixes i j k s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   496
  assumes le_ij: "i \<le> j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   497
  and le_jk: "j \<le> k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   498
  shows "from_to i j s @ from_to j k s = from_to i k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   499
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   500
  let ?ris = "restn i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   501
  have "firstn (j - i) (restn i s) @ firstn (k - j) (restn j s) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   502
         firstn (k - i) (restn i s)" (is "?x @ ?y = ?z")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   503
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   504
    let "firstn (k-j) ?u" = "?y"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   505
    let ?rst = " restn (k - j) (restn (j - i) ?ris)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   506
    let ?rst' = "restn (k - i) ?ris"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   507
    have "?u = restn (j-i) ?ris"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   508
    proof(rule restn_conc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   509
      from le_ij show "j - i + i = j" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   510
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
    hence "?x @ ?y = ?x @ firstn (k-j) (restn (j-i) ?ris)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   512
    moreover have "firstn (k - j) (restn (j - i) (restn i s)) @ ?rst = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   513
                      restn (j-i) ?ris" by (simp add:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
    ultimately have "?x @ ?y @ ?rst = ?x @ (restn (j-i) ?ris)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   515
    also have "\<dots> = ?ris" by (simp add:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   516
    finally have "?x @ ?y @ ?rst = ?ris" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   517
    moreover have "?z @ ?rst = ?ris"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   519
      have "?z @ ?rst' = ?ris" by (simp add:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   520
      moreover have "?rst' = ?rst"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   521
      proof(rule restn_conc)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   522
        from le_ij le_jk show "k - j + (j - i) = k - i" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   523
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   524
      ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   525
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   526
    ultimately have "?x @ ?y @ ?rst = ?z @ ?rst" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   527
    thus ?thesis by auto    
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   528
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   529
  thus ?thesis by (simp only:from_to_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   530
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   531
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   532
lemma down_to_conc:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   533
  fixes i j k s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   534
  assumes le_ij: "i \<le> j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   535
  and le_jk: "j \<le> k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   536
  shows "down_to k j s @ down_to j i s = down_to k i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   537
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   538
  have "rev (from_to j k (rev s)) @ rev (from_to i j (rev s)) = rev (from_to i k (rev s))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   539
    (is "?L = ?R")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   540
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   541
    have "?L = rev (from_to i j (rev s) @ from_to j k (rev s))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   542
    also have "\<dots> = ?R" (is "rev ?x = rev ?y")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   543
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   544
      have "?x = ?y" by (rule from_to_conc[OF le_ij le_jk])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   545
      thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   546
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   547
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   548
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   549
  thus ?thesis by (simp add:down_to_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   550
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   551
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   552
lemma restn_ge:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   553
  fixes s k
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   554
  assumes le_k: "length s \<le> k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   555
  shows "restn k s = []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   556
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   557
  from firstn_restn_s [of k s, symmetric] have "s = (firstn k s) @ (restn k s)" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   558
  hence "length s = length \<dots>" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   559
  also have "\<dots> = length (firstn k s) + length (restn k s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   560
  finally have "length s = ..." by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   561
  moreover from length_firstn_ge and le_k 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   562
  have "length (firstn k s) = length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   563
  ultimately have "length (restn k s) = 0" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   564
  thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   565
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   566
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   567
lemma from_to_ge: "length s \<le> k \<Longrightarrow> from_to k j s = []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   568
proof(simp only:from_to_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   569
  assume "length s \<le> k"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   570
  from restn_ge [OF this] 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   571
  show "firstn (j - k) (restn k s) = []" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   572
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   573
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   574
(*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   575
value "from_to 2 5 [0, 1, 2, 3, 4]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   576
value "restn 2  [0, 1, 2, 3, 4]"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   577
*)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   578
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   579
lemma from_to_restn: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   580
  fixes k j s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   581
  assumes le_j: "length s \<le> j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   582
  shows "from_to k j s = restn k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   583
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   584
  have "from_to 0 k s @ from_to k j s = from_to 0 j s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   585
  proof(cases "k \<le> j")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   586
    case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   587
    from from_to_conc True show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   588
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   589
    case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   590
    from False le_j have lek: "length s \<le>  k" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   591
    from from_to_ge [OF this] have "from_to k j s = []" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   592
    hence "from_to 0 k s @ from_to k j s = from_to 0 k s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   593
    also have "\<dots> = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   594
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   595
      from from_to_firstn [of k s]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   596
      have "\<dots> = firstn k s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   597
      also have "\<dots> = s" by (rule firstn_ge [OF lek])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   598
      finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   599
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   600
    finally have "from_to 0 k s @ from_to k j s = s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   601
    moreover have "from_to 0 j s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   602
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   603
      have "from_to 0 j s = firstn j s" by (simp add:from_to_firstn)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   604
      also have "\<dots> = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   605
      proof(rule firstn_ge)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   606
        from le_j show "length s \<le> j " by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   607
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   608
      finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   609
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   610
    ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   611
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   612
  also have "\<dots> = s" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   613
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   614
    from from_to_firstn have "\<dots> = firstn j s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   615
    also have "\<dots> = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   616
    proof(rule firstn_ge)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   617
      from le_j show "length s \<le> j" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   618
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   619
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   620
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   621
  finally have "from_to 0 k s @ from_to k j s = s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   622
  moreover have "from_to 0 k s @ restn k s = s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   623
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   624
    from from_to_firstn [of k s]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   625
    have "from_to 0 k s = firstn k s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   626
    thus ?thesis by (simp add:firstn_restn_s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   627
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   628
  ultimately have "from_to 0 k s @ from_to k j s  = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   629
                    from_to 0 k s @ restn k s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   630
  thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   631
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   632
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   633
lemma down_to_moment: "down_to k 0 s = moment k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   634
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   635
  have "rev (from_to 0 k (rev s)) = rev (firstn k (rev s))" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   636
    using from_to_firstn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   637
  thus ?thesis by (simp add:down_to_def moment_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   638
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   639
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   640
lemma down_to_restm:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   641
  assumes le_s: "length s \<le> j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   642
  shows "down_to j k s = restm k s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   643
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   644
  have "rev (from_to k j (rev s)) = rev (restn k (rev s))" (is "?L = ?R")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   645
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   646
    from le_s have "length (rev s) \<le> j" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   647
    from from_to_restn [OF this, of k] show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   648
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   649
  thus ?thesis by (simp add:down_to_def restm_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   650
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   651
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   652
lemma moment_split: "moment (m+i) s = down_to (m+i) i s @down_to i 0 s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   653
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   654
  have "moment (m + i) s = down_to (m+i) 0 s" using down_to_moment by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   655
  also have "\<dots> = (down_to (m+i) i s) @ (down_to i 0 s)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   656
    by(rule down_to_conc[symmetric], auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   657
  finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   658
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   659
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   660
lemma length_restn: "length (restn i s) = length s - i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   661
proof(cases "i \<le> length s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   662
  case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   663
  from length_firstn_le [OF this] have "length (firstn i s) = i" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   664
  moreover have "length s = length (firstn i s) + length (restn i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   665
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   666
    have "s = firstn i s @ restn i s" using firstn_restn_s by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   667
    hence "length s = length \<dots>" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   668
    thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   669
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   670
  ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   671
next 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   672
  case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   673
  hence "length s \<le> i" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   674
  from restn_ge [OF this] have "restn i s = []" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   675
  with False show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   676
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   677
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   678
lemma length_from_to_in:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   679
  fixes i j s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   680
  assumes le_ij: "i \<le> j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   681
  and le_j: "j \<le> length s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   682
  shows "length (from_to i j s) = j - i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   683
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   684
  have "from_to 0 j s = from_to 0 i s @ from_to i j s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   685
    by (rule from_to_conc[symmetric, OF _ le_ij], simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   686
  moreover have "length (from_to 0 j s) = j"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   687
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   688
    have "from_to 0 j s = firstn j s" using from_to_firstn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   689
    moreover have "length \<dots> = j" by (rule length_firstn_le [OF le_j])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   690
    ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   691
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   692
  moreover have "length (from_to 0 i s) = i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   693
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   694
    have "from_to 0 i s = firstn i s" using from_to_firstn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   695
    moreover have "length \<dots> = i" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   696
    proof (rule length_firstn_le)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   697
      from le_ij le_j show "i \<le> length s" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   698
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   699
    ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   700
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   701
  ultimately show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   702
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   703
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   704
lemma firstn_restn_from_to: "from_to i (m + i) s = firstn m (restn i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   705
proof(cases "m+i \<le> length s")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   706
  case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   707
  have "restn i s = from_to i (m+i) s @ from_to (m+i) (length s) s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   708
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   709
    have "restn i s = from_to i (length s) s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   710
      by(rule from_to_restn[symmetric], simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   711
    also have "\<dots> = from_to i (m+i) s @ from_to (m+i) (length s) s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   712
      by(rule from_to_conc[symmetric, OF _ True], simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   713
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   714
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   715
  hence "firstn m (restn i s) = firstn m \<dots>" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   716
  moreover have "\<dots> = firstn (length (from_to i (m+i) s)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   717
                    (from_to i (m+i) s @ from_to (m+i) (length s) s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   718
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   719
    have "length (from_to i (m+i) s) = m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   720
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   721
      have "length (from_to i (m+i) s) = (m+i) - i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   722
        by(rule length_from_to_in [OF _ True], simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   723
      thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   724
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   725
    thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   726
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   727
  ultimately show ?thesis using app_firstn_restn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   728
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   729
  case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   730
  hence "length s \<le> m + i" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   731
  from from_to_restn [OF this]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   732
  have "from_to i (m + i) s = restn i s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   733
  moreover have "firstn m (restn i s) = restn i s" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   734
  proof(rule firstn_ge)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   735
    show "length (restn i s) \<le> m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   736
    proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   737
      have "length (restn i s) = length s - i" using length_restn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   738
      with False show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   739
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   740
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   741
  ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   742
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   743
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   744
lemma down_to_moment_restm:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   745
  fixes m i s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   746
  shows "down_to (m + i) i s = moment m (restm i s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   747
  by (simp add:firstn_restn_from_to down_to_def moment_def restm_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   748
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   749
lemma moment_plus_split:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   750
  fixes m i s
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   751
  shows "moment (m + i) s = moment m (restm i s) @ moment i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   752
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   753
  from moment_split [of m i s]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   754
  have "moment (m + i) s = down_to (m + i) i s @ down_to i 0 s" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   755
  also have "\<dots> = down_to (m+i) i s @ moment i s" using down_to_moment by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   756
  also from down_to_moment_restm have "\<dots> = moment m (restm i s) @ moment i s"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   757
    by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   758
  finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   759
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   760
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   761
lemma length_restm: "length (restm i s) = length s - i"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   762
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   763
  have "length (rev (restn i (rev s))) = length s - i" (is "?L = ?R")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   764
  proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   765
    have "?L = length (restn i (rev s))" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   766
    also have "\<dots>  = length (rev s) - i" using length_restn by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   767
    also have "\<dots> = ?R" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   768
    finally show ?thesis .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   769
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   770
  thus ?thesis by (simp add:restm_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   771
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   772
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   773
lemma moment_prefix: "(moment i t @ s) = moment (i + length s) (t @ s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   774
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   775
  from moment_plus_split [of i "length s" "t@s"]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   776
  have " moment (i + length s) (t @ s) = moment i (restm (length s) (t @ s)) @ moment (length s) (t @ s)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   777
    by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   778
  with app_moment_restm[of t s]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   779
  have "moment (i + length s) (t @ s) = moment i t @ moment (length s) (t @ s)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   780
  with moment_app show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   781
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   782
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   783
end