Attic/Lsp.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Jan 2019 21:09:05 +0000
changeset 208 a5afc26b1d62
parent 1 c4783e4ef43f
permissions -rw-r--r--
final version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Lsp
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 lsp :: "('a \<Rightarrow> ('b::linorder)) \<Rightarrow> 'a list \<Rightarrow> ('a list \<times> 'a list \<times> '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
   "lsp f [] = ([], [], [])" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
   "lsp f [x] = ([], [x], [])" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
   "lsp f (x#xs) = (case (lsp f xs) of
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
                     (l, [], r) \<Rightarrow> ([], [x], []) |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
                     (l, y#ys, r) \<Rightarrow> if f x \<ge> f y then ([], [x], xs) else (x#l, y#ys, r))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
inductive lsp_p :: "('a \<Rightarrow> ('b::linorder)) \<Rightarrow> 'a list \<Rightarrow> ('a list \<times> 'a list \<times> 'a list) \<Rightarrow> bool"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
for f :: "('a \<Rightarrow> ('b::linorder))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  lsp_nil [intro]: "lsp_p f [] ([], [], [])" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  lsp_single [intro]: "lsp_p f [x] ([], [x], [])" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  lsp_cons_1 [intro]: "\<lbrakk>xs \<noteq> []; lsp_p f xs (l, [m], r); f x \<ge> f m\<rbrakk> \<Longrightarrow> lsp_p f (x#xs) ([], [x], xs)" |
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  lsp_cons_2 [intro]: "\<lbrakk>xs \<noteq> []; lsp_p f xs (l, [m], r); f x < f m\<rbrakk> \<Longrightarrow> lsp_p f (x#xs) (x#l, [m], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
lemma lsp_p_lsp_1: "lsp_p f x y \<Longrightarrow> y = lsp f x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
proof (induct rule:lsp_p.induct)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  case (lsp_cons_1 xs  l m r x)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  assume lsp_xs [symmetric]: "(l, [m], r) = lsp f xs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
    and le_mx: "f m \<le> f x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  show ?case (is "?L = ?R")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  proof(cases xs, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    case (Cons v vs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
      apply (simp add:Cons)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
      apply (fold Cons)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
      by (simp add:lsp_xs le_mx)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  case (lsp_cons_2 xs l m r x)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  assume lsp_xs [symmetric]: "(l, [m], r) = lsp f xs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
    and lt_xm: "f x < f m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  show ?case (is "?L = ?R")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  proof(cases xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    case (Cons v vs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
      apply (simp add:Cons)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
      apply (fold Cons)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
      apply (simp add:lsp_xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
      by (insert lt_xm, auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
    from prems show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
qed auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
lemma lsp_mid_nil: "lsp f xs = (a, [], c) \<Longrightarrow> xs = []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  apply (induct xs arbitrary:a c, auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  apply (case_tac xs, auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  by (case_tac "(lsp f (ab # list))", auto split:if_splits list.splits)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
lemma lsp_mid_length: "lsp f x = (u, v, w) \<Longrightarrow> length v \<le> 1"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
proof(induct x arbitrary:u v w, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case (Cons x xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  assume ih: "\<And> u v w. lsp f xs = (u, v, w) \<Longrightarrow> length v \<le> 1"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  and h: "lsp f (x # xs) = (u, v, w)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  show "length v \<le> 1" using h
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  proof(cases xs, simp add:h)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    case (Cons z zs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    assume eq_xs: "xs = z # zs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    proof(cases "lsp f xs")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
      fix l m r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
      assume eq_lsp: "lsp f xs = (l, m, r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
      show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
      proof(cases m)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
        case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
        from Nil and eq_lsp have "lsp f xs = (l, [], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
        from lsp_mid_nil [OF this] have "xs = []" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
        with h show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
      next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
        case (Cons y ys)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
        assume eq_m: "m = y # ys"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
        from ih [OF eq_lsp] have eq_xs_1: "length m \<le> 1" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
        show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
        proof(cases "f x \<ge> f y")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
          case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
          from eq_xs eq_xs_1 True h eq_lsp show ?thesis 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
            by (auto split:list.splits if_splits)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
        next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
          case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
          from eq_xs eq_xs_1 False h eq_lsp show ?thesis 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
             by (auto split:list.splits if_splits)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
    assume "[] = u \<and> [x] = v \<and> [] = w"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
    hence "v = [x]" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
    thus "length v \<le> Suc 0" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
lemma lsp_p_lsp_2: "lsp_p f x (lsp f x)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
proof(induct x, auto)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  case (Cons x xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  assume ih: "lsp_p f xs (lsp f xs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  show ?case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  proof(cases xs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
    case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
    thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    case (Cons v vs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
    show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
    proof(cases "xs")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
      case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
      thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
    next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
      case (Cons v vs)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
      assume eq_xs: "xs = v # vs"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
      show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
      proof(cases "lsp f xs")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
        fix l m r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
        assume eq_lsp_xs: "lsp f xs = (l, m, r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
        show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
        proof(cases m)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
          case Nil
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
          from eq_lsp_xs and Nil have "lsp f xs = (l, [], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
          from lsp_mid_nil [OF this] have eq_xs: "xs = []" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
          hence "lsp f (x#xs) = ([], [x], [])" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
          with eq_xs show ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
        next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
          case (Cons y ys)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
          assume eq_m: "m = y # ys"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
          show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
          proof(cases "f x \<ge> f y")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
            case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
            from eq_xs eq_lsp_xs Cons True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
            have eq_lsp: "lsp f (x#xs) = ([], [x], v # vs)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
            show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
            proof (simp add:eq_lsp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
              show "lsp_p f (x # xs) ([], [x], v # vs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
              proof(fold eq_xs, rule lsp_cons_1 [OF _])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
                from eq_xs show "xs \<noteq> []" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
              next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
                from lsp_mid_length [OF eq_lsp_xs] and Cons
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
                have "m = [y]" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
                with eq_lsp_xs have "lsp f xs = (l, [y], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
                with ih show "lsp_p f xs (l, [y], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
              next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
                from True show "f y \<le> f x" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
              qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
            qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
          next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
            case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
            from eq_xs eq_lsp_xs Cons False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
            have eq_lsp: "lsp f (x#xs) = (x # l, y # ys, r) " by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
            show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
            proof (simp add:eq_lsp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
              from lsp_mid_length [OF eq_lsp_xs] and eq_m
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
              have "ys = []" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
              moreover have "lsp_p f (x # xs) (x # l, [y], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
              proof(rule lsp_cons_2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
                from eq_xs show "xs \<noteq> []" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
              next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
                from lsp_mid_length [OF eq_lsp_xs] and Cons
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
                have "m = [y]" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
                with eq_lsp_xs have "lsp f xs = (l, [y], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
                with ih show "lsp_p f xs (l, [y], r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
              next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
                from False show "f x < f y" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
              qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
              ultimately show "lsp_p f (x # xs) (x # l, y # ys, r)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
            qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
          qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
        qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
lemma lsp_induct:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
  fixes f x1 x2 P
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  assumes h: "lsp f x1 = x2"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
  and p1: "P [] ([], [], [])"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  and p2: "\<And>x. P [x] ([], [x], [])"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
  and p3: "\<And>xs l m r x. \<lbrakk>xs \<noteq> []; lsp f xs = (l, [m], r); P xs (l, [m], r); f m \<le> f x\<rbrakk> \<Longrightarrow> P (x # xs) ([], [x], xs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
  and p4: "\<And>xs l m r x. \<lbrakk>xs \<noteq> []; lsp f xs = (l, [m], r); P xs (l, [m], r); f x < f m\<rbrakk> \<Longrightarrow> P (x # xs) (x # l, [m], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
  shows "P x1 x2"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
proof(rule lsp_p.induct)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
  from lsp_p_lsp_2 and h
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  show "lsp_p f x1 x2" by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  from p1 show "P [] ([], [], [])" by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
  from p2 show "\<And>x. P [x] ([], [x], [])" by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
  fix xs l m r x 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
  assume h1: "xs \<noteq> []" and h2: "lsp_p f xs (l, [m], r)" and h3: "P xs (l, [m], r)" and h4: "f m \<le> f x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
  show "P (x # xs) ([], [x], xs)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
  proof(rule p3 [OF h1 _ h3 h4])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
    from h2 and lsp_p_lsp_1 show "lsp f xs = (l, [m], r)" by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  fix xs l m r x 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
  assume h1: "xs \<noteq> []" and h2: "lsp_p f xs (l, [m], r)" and h3: "P xs (l, [m], r)" and h4: "f x < f m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  show "P (x # xs) (x # l, [m], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
  proof(rule p4 [OF h1 _ h3 h4])
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
    from h2 and lsp_p_lsp_1 show "lsp f xs = (l, [m], r)" by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
lemma lsp_set_eq: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
  fixes f x u v w
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
  assumes h: "lsp f x = (u, v, w)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  shows "x = u@v@w"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
  have "\<And> f x r. lsp f x = r \<Longrightarrow> \<forall> u v w. (r = (u, v, w) \<longrightarrow> x = u@v@w)" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
    by (erule lsp_induct, simp+)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
  from this [rule_format, OF h] show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
lemma lsp_set: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
  assumes h: "(u, v, w) = lsp f x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
  shows "set (u@v@w) = set x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
  from lsp_set_eq [OF h[symmetric]] 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
  show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
lemma max_insert_gt:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
  fixes S fx
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
  assumes h: "fx < Max S"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
  and np: "S \<noteq> {}"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
  and fn: "finite S" 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
  shows "Max S = Max (insert fx S)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
  from Max_insert [OF fn np]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  have "Max (insert fx S) = max fx (Max S)" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
  moreover have "\<dots> = Max S"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
  proof(cases "fx \<le> Max S")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
    case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
    with h
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
    show ?thesis by (simp add:max_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
    case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
    thus ?thesis by (simp add:max_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
  ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
lemma max_insert_le: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
  fixes S fx
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
  assumes h: "Max S \<le> fx"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
  and fn: "finite S"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
  shows "fx = Max (insert fx S)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
proof(cases "S = {}")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
  case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
  thus ?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
  case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
  from Max_insert [OF fn False]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
  have "Max (insert fx S) = max fx (Max S)" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
  moreover have "\<dots> = fx"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  proof(cases "fx \<le> Max S")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
    case False
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
    thus ?thesis by (simp add:max_def)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
  next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
    case True
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
    have hh: "\<And> x y. \<lbrakk> x \<le> (y::('a::linorder)); y \<le> x\<rbrakk> \<Longrightarrow> x = y" by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
    from hh [OF True h]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
    have "fx = Max S" .
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
    thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
  qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
  ultimately show ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
lemma lsp_max: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
  fixes f x u m w
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
  assumes h: "lsp f x = (u, [m], w)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
  shows "f m = Max (f ` (set x))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
  { fix y
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
    have "lsp f x = y \<Longrightarrow> \<forall> u m w. y = (u, [m], w) \<longrightarrow> f m = Max (f ` (set x))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
    proof(erule lsp_induct, simp)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
      { fix x u m w
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
        assume "(([]::'a list), ([x]::'a list), ([]::'a list)) = (u, [m], w)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
        hence "f m = Max (f ` set [x])"  by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
      } thus "\<And>x. \<forall>u m w. ([], [x], []) = (u, [m], w) \<longrightarrow> f m = Max (f ` set [x])" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
    next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
      fix xs l m r x
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
      assume h1: "xs \<noteq> []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
        and h2: " lsp f xs = (l, [m], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
        and h3: "\<forall>u ma w. (l, [m], r) = (u, [ma], w) \<longrightarrow> f ma = Max (f ` set xs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
        and h4: "f m \<le> f x"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
      show " \<forall>u m w. ([], [x], xs) = (u, [m], w) \<longrightarrow> f m = Max (f ` set (x # xs))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
        have "f x = Max (f ` set (x # xs))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
        proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
          from h2 h3 have "f m = Max (f ` set xs)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
          with h4 show ?thesis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
            apply auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
            by (rule_tac max_insert_le, 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
        thus ?thesis by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
    next
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
      fix xs l m r x
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
      assume h1: "xs \<noteq> []"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
        and h2: " lsp f xs = (l, [m], r)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
        and h3: " \<forall>u ma w. (l, [m], r) = (u, [ma], w) \<longrightarrow> f ma = Max (f ` set xs)"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
        and h4: "f x < f m"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
      show "\<forall>u ma w. (x # l, [m], r) = (u, [ma], w) \<longrightarrow> f ma = Max (f ` set (x # xs))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
      proof -
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
        from h2 h3 have "f m = Max (f ` set xs)" by simp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
        with h4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
        have "f m =  Max (f ` set (x # xs))"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
          apply auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
          apply (rule_tac max_insert_gt, simp+)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
          by (insert h1, simp+)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
        thus ?thesis by auto
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
      qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
    qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
  } with h show ?thesis by metis
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
qed
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
end