src/HOL/Library/List_Prefix.thy
author nipkow
Thu Dec 06 19:58:21 2007 +0100 (2007-12-06)
changeset 25564 4ca31a3706a4
parent 25356 059c03630d6e
child 25595 6c48275f9c76
permissions -rw-r--r--
R&F: added sgn lemma
Prefix: sledge-hammered
wenzelm@10330
     1
(*  Title:      HOL/Library/List_Prefix.thy
wenzelm@10330
     2
    ID:         $Id$
wenzelm@10330
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@10330
     4
*)
wenzelm@10330
     5
wenzelm@14706
     6
header {* List prefixes and postfixes *}
wenzelm@10330
     7
nipkow@15131
     8
theory List_Prefix
nipkow@15140
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10330
    11
wenzelm@10330
    12
subsection {* Prefix order on lists *}
wenzelm@10330
    13
wenzelm@12338
    14
instance list :: (type) ord ..
wenzelm@10330
    15
wenzelm@10330
    16
defs (overloaded)
wenzelm@10389
    17
  prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
wenzelm@10389
    18
  strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
wenzelm@10330
    19
wenzelm@12338
    20
instance list :: (type) order
wenzelm@10389
    21
  by intro_classes (auto simp add: prefix_def strict_prefix_def)
wenzelm@10330
    22
wenzelm@10389
    23
lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
wenzelm@18730
    24
  unfolding prefix_def by blast
wenzelm@10330
    25
wenzelm@21305
    26
lemma prefixE [elim?]:
wenzelm@21305
    27
  assumes "xs \<le> ys"
wenzelm@21305
    28
  obtains zs where "ys = xs @ zs"
wenzelm@23394
    29
  using assms unfolding prefix_def by blast
wenzelm@10330
    30
wenzelm@10870
    31
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
wenzelm@18730
    32
  unfolding strict_prefix_def prefix_def by blast
wenzelm@10870
    33
wenzelm@10870
    34
lemma strict_prefixE' [elim?]:
wenzelm@21305
    35
  assumes "xs < ys"
wenzelm@21305
    36
  obtains z zs where "ys = xs @ z # zs"
wenzelm@10870
    37
proof -
wenzelm@21305
    38
  from `xs < ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"
wenzelm@18730
    39
    unfolding strict_prefix_def prefix_def by blast
wenzelm@21305
    40
  with that show ?thesis by (auto simp add: neq_Nil_conv)
wenzelm@10870
    41
qed
wenzelm@10870
    42
wenzelm@10389
    43
lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
wenzelm@18730
    44
  unfolding strict_prefix_def by blast
wenzelm@10330
    45
wenzelm@10389
    46
lemma strict_prefixE [elim?]:
wenzelm@21305
    47
  fixes xs ys :: "'a list"
wenzelm@21305
    48
  assumes "xs < ys"
wenzelm@21305
    49
  obtains "xs \<le> ys" and "xs \<noteq> ys"
wenzelm@23394
    50
  using assms unfolding strict_prefix_def by blast
wenzelm@10330
    51
wenzelm@10330
    52
wenzelm@10389
    53
subsection {* Basic properties of prefixes *}
wenzelm@10330
    54
wenzelm@10330
    55
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    56
  by (simp add: prefix_def)
wenzelm@10330
    57
wenzelm@10330
    58
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    59
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    60
wenzelm@10330
    61
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    62
proof
wenzelm@10389
    63
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    64
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    65
  show "xs = ys @ [y] \<or> xs \<le> ys"
nipkow@25564
    66
    by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
nipkow@25564
    67
(*
wenzelm@10389
    68
  proof (cases zs rule: rev_cases)
wenzelm@10389
    69
    assume "zs = []"
wenzelm@10389
    70
    with zs have "xs = ys @ [y]" by simp
wenzelm@23254
    71
    then show ?thesis ..
wenzelm@10389
    72
  next
wenzelm@10389
    73
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    74
    with zs have "ys = xs @ zs'" by simp
wenzelm@23254
    75
    then have "xs \<le> ys" ..
wenzelm@23254
    76
    then show ?thesis ..
wenzelm@10389
    77
  qed
nipkow@25564
    78
*)
wenzelm@10389
    79
next
wenzelm@10389
    80
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@23254
    81
  then show "xs \<le> ys @ [y]"
nipkow@25564
    82
    by (metis order_eq_iff strict_prefixE strict_prefixI' xt1(7))
nipkow@25564
    83
(*
wenzelm@10389
    84
  proof
wenzelm@10389
    85
    assume "xs = ys @ [y]"
wenzelm@23254
    86
    then show ?thesis by simp
wenzelm@10389
    87
  next
wenzelm@10389
    88
    assume "xs \<le> ys"
wenzelm@10389
    89
    then obtain zs where "ys = xs @ zs" ..
wenzelm@23254
    90
    then have "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@23254
    91
    then show ?thesis ..
wenzelm@10389
    92
  qed
nipkow@25564
    93
*)
wenzelm@10389
    94
qed
wenzelm@10330
    95
wenzelm@10330
    96
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    97
  by (auto simp add: prefix_def)
wenzelm@10330
    98
wenzelm@10330
    99
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
   100
  by (induct xs) simp_all
wenzelm@10330
   101
wenzelm@10389
   102
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
nipkow@25564
   103
by (metis append_Nil2 append_self_conv order_eq_iff prefixI)
nipkow@25564
   104
(*
wenzelm@10389
   105
proof -
wenzelm@10389
   106
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@23254
   107
  then show ?thesis by simp
wenzelm@10389
   108
qed
nipkow@25564
   109
*)
wenzelm@10330
   110
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
nipkow@25564
   111
by (metis order_le_less_trans prefixI strict_prefixE strict_prefixI)
nipkow@25564
   112
(*
wenzelm@10389
   113
proof -
wenzelm@10389
   114
  assume "xs \<le> ys"
wenzelm@10389
   115
  then obtain us where "ys = xs @ us" ..
wenzelm@23254
   116
  then have "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@23254
   117
  then show ?thesis ..
wenzelm@10389
   118
qed
nipkow@25564
   119
*)
nipkow@14300
   120
lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
wenzelm@17201
   121
  by (auto simp add: prefix_def)
nipkow@14300
   122
wenzelm@10330
   123
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
   124
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
   125
wenzelm@10330
   126
theorem prefix_append:
nipkow@25564
   127
  "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   128
  apply (induct zs rule: rev_induct)
wenzelm@10330
   129
   apply force
wenzelm@10330
   130
  apply (simp del: append_assoc add: append_assoc [symmetric])
nipkow@25564
   131
  apply (metis append_eq_appendI)
nipkow@25564
   132
(*
wenzelm@10330
   133
  apply simp
wenzelm@10330
   134
  apply blast
nipkow@25564
   135
*)
wenzelm@10330
   136
  done
wenzelm@10330
   137
wenzelm@10330
   138
lemma append_one_prefix:
nipkow@25564
   139
  "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
nipkow@25564
   140
by (unfold prefix_def)
nipkow@25564
   141
   (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj eq_Nil_appendI nth_drop')
nipkow@25564
   142
(*
wenzelm@10330
   143
  apply (auto simp add: nth_append)
wenzelm@10389
   144
  apply (case_tac zs)
wenzelm@10330
   145
   apply auto
wenzelm@10330
   146
  done
nipkow@25564
   147
*)
wenzelm@10330
   148
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   149
  by (auto simp add: prefix_def)
wenzelm@10330
   150
nipkow@14300
   151
lemma prefix_same_cases:
nipkow@25564
   152
  "(xs\<^isub>1::'a list) \<le> ys \<Longrightarrow> xs\<^isub>2 \<le> ys \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1"
nipkow@25564
   153
by (unfold prefix_def) (metis append_eq_append_conv2)
nipkow@25564
   154
(*
wenzelm@17201
   155
  apply (erule exE)+
wenzelm@17201
   156
  apply (simp add: append_eq_append_conv_if split: if_splits)
wenzelm@17201
   157
   apply (rule disjI2)
wenzelm@17201
   158
   apply (rule_tac x = "drop (size xs\<^isub>2) xs\<^isub>1" in exI)
wenzelm@17201
   159
   apply clarify
wenzelm@17201
   160
   apply (drule sym)
wenzelm@17201
   161
   apply (insert append_take_drop_id [of "length xs\<^isub>2" xs\<^isub>1])
wenzelm@17201
   162
   apply simp
wenzelm@17201
   163
  apply (rule disjI1)
wenzelm@17201
   164
  apply (rule_tac x = "drop (size xs\<^isub>1) xs\<^isub>2" in exI)
wenzelm@17201
   165
  apply clarify
wenzelm@17201
   166
  apply (insert append_take_drop_id [of "length xs\<^isub>1" xs\<^isub>2])
wenzelm@17201
   167
  apply simp
wenzelm@17201
   168
  done
nipkow@25564
   169
*)
nipkow@25564
   170
lemma set_mono_prefix: "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@25564
   171
by (auto simp add: prefix_def)
nipkow@14300
   172
nipkow@25564
   173
lemma take_is_prefix: "take n xs \<le> xs"
nipkow@25564
   174
by (unfold prefix_def) (metis append_take_drop_id)
nipkow@25564
   175
(*
kleing@25299
   176
  apply (rule_tac x="drop n xs" in exI)
kleing@25299
   177
  apply simp
kleing@25299
   178
  done
nipkow@25564
   179
*)
wenzelm@25355
   180
lemma map_prefixI:
kleing@25322
   181
  "xs \<le> ys \<Longrightarrow> map f xs \<le> map f ys"
nipkow@25564
   182
by (clarsimp simp: prefix_def)
kleing@25322
   183
kleing@25299
   184
lemma prefix_length_less:
kleing@25299
   185
  "xs < ys \<Longrightarrow> length xs < length ys"
nipkow@25564
   186
by (clarsimp simp: strict_prefix_def prefix_def)
nipkow@25564
   187
(*
kleing@25299
   188
  apply (frule prefix_length_le)
kleing@25299
   189
  apply (rule ccontr, simp)
kleing@25299
   190
  apply (clarsimp simp: prefix_def)
kleing@25299
   191
  done
nipkow@25564
   192
*)
kleing@25299
   193
lemma strict_prefix_simps [simp]:
kleing@25299
   194
  "xs < [] = False"
kleing@25299
   195
  "[] < (x # xs) = True"
kleing@25299
   196
  "(x # xs) < (y # ys) = (x = y \<and> xs < ys)"
nipkow@25564
   197
by (simp_all add: strict_prefix_def cong: conj_cong)
kleing@25299
   198
nipkow@25564
   199
lemma take_strict_prefix: "xs < ys \<Longrightarrow> take n xs < ys"
nipkow@25564
   200
apply (induct n arbitrary: xs ys)
nipkow@25564
   201
 apply (case_tac ys, simp_all)[1]
nipkow@25564
   202
apply (metis order_less_trans strict_prefixI take_is_prefix)
nipkow@25564
   203
(*
kleing@25299
   204
  apply (case_tac xs, simp)
kleing@25299
   205
  apply (case_tac ys, simp_all)
nipkow@25564
   206
*)
nipkow@25564
   207
done
kleing@25299
   208
wenzelm@25355
   209
lemma not_prefix_cases:
kleing@25299
   210
  assumes pfx: "\<not> ps \<le> ls"
wenzelm@25356
   211
  obtains
wenzelm@25356
   212
    (c1) "ps \<noteq> []" and "ls = []"
wenzelm@25356
   213
  | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> as \<le> xs"
wenzelm@25356
   214
  | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
kleing@25299
   215
proof (cases ps)
nipkow@25564
   216
  case Nil thus ?thesis using pfx by simp
kleing@25299
   217
next
kleing@25299
   218
  case (Cons a as)
nipkow@25564
   219
  hence c: "ps = a#as" .
kleing@25299
   220
  show ?thesis
kleing@25299
   221
  proof (cases ls)
nipkow@25564
   222
    case Nil thus ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
nipkow@25564
   223
(*
wenzelm@25355
   224
    have "ps \<noteq> []" by (simp add: Nil Cons)
wenzelm@25355
   225
    from this and Nil show ?thesis by (rule c1)
nipkow@25564
   226
*)
kleing@25299
   227
  next
kleing@25299
   228
    case (Cons x xs)
kleing@25299
   229
    show ?thesis
kleing@25299
   230
    proof (cases "x = a")
wenzelm@25355
   231
      case True
wenzelm@25355
   232
      have "\<not> as \<le> xs" using pfx c Cons True by simp
wenzelm@25355
   233
      with c Cons True show ?thesis by (rule c2)
wenzelm@25355
   234
    next
wenzelm@25355
   235
      case False
wenzelm@25355
   236
      with c Cons show ?thesis by (rule c3)
kleing@25299
   237
    qed
kleing@25299
   238
  qed
kleing@25299
   239
qed
kleing@25299
   240
kleing@25299
   241
lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
kleing@25299
   242
  assumes np: "\<not> ps \<le> ls"
wenzelm@25356
   243
    and base: "\<And>x xs. P (x#xs) []"
wenzelm@25356
   244
    and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
wenzelm@25356
   245
    and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> xs \<le> ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
wenzelm@25356
   246
  shows "P ps ls" using np
kleing@25299
   247
proof (induct ls arbitrary: ps)
wenzelm@25355
   248
  case Nil then show ?case
kleing@25299
   249
    by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
kleing@25299
   250
next
wenzelm@25355
   251
  case (Cons y ys)
wenzelm@25355
   252
  then have npfx: "\<not> ps \<le> (y # ys)" by simp
wenzelm@25355
   253
  then obtain x xs where pv: "ps = x # xs"
kleing@25299
   254
    by (rule not_prefix_cases) auto
nipkow@25564
   255
  show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
nipkow@25564
   256
(*
kleing@25299
   257
  from Cons
kleing@25299
   258
  have ih: "\<And>ps. \<not>ps \<le> ys \<Longrightarrow> P ps ys" by simp
wenzelm@25355
   259
kleing@25299
   260
  show ?case using npfx
kleing@25299
   261
    by (simp only: pv) (erule not_prefix_cases, auto intro: r1 r2 ih)
nipkow@25564
   262
*)
kleing@25299
   263
qed
nipkow@14300
   264
wenzelm@25356
   265
wenzelm@10389
   266
subsection {* Parallel lists *}
wenzelm@10389
   267
wenzelm@19086
   268
definition
wenzelm@21404
   269
  parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
wenzelm@19086
   270
  "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)"
wenzelm@10389
   271
wenzelm@10389
   272
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
nipkow@25564
   273
unfolding parallel_def by blast
wenzelm@10330
   274
wenzelm@10389
   275
lemma parallelE [elim]:
nipkow@25564
   276
assumes "xs \<parallel> ys"
nipkow@25564
   277
obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs"
nipkow@25564
   278
using assms unfolding parallel_def by blast
wenzelm@10330
   279
wenzelm@10389
   280
theorem prefix_cases:
nipkow@25564
   281
obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys"
nipkow@25564
   282
unfolding parallel_def strict_prefix_def by blast
wenzelm@10330
   283
wenzelm@10389
   284
theorem parallel_decomp:
wenzelm@10389
   285
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   286
proof (induct xs rule: rev_induct)
wenzelm@11987
   287
  case Nil
wenzelm@23254
   288
  then have False by auto
wenzelm@23254
   289
  then show ?case ..
wenzelm@10408
   290
next
wenzelm@11987
   291
  case (snoc x xs)
wenzelm@11987
   292
  show ?case
wenzelm@10408
   293
  proof (rule prefix_cases)
wenzelm@10408
   294
    assume le: "xs \<le> ys"
wenzelm@10408
   295
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   296
    show ?thesis
wenzelm@10408
   297
    proof (cases ys')
nipkow@25564
   298
      assume "ys' = []"
nipkow@25564
   299
      thus ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
nipkow@25564
   300
(*
nipkow@25564
   301
      with ys have "xs = ys" by simp
wenzelm@11987
   302
      with snoc have "[x] \<parallel> []" by auto
wenzelm@23254
   303
      then have False by blast
wenzelm@23254
   304
      then show ?thesis ..
nipkow@25564
   305
*)
wenzelm@10389
   306
    next
wenzelm@10408
   307
      fix c cs assume ys': "ys' = c # cs"
nipkow@25564
   308
      thus ?thesis
nipkow@25564
   309
	by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixI same_prefix_prefix snoc.prems ys)
nipkow@25564
   310
(*
wenzelm@11987
   311
      with snoc ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@23254
   312
      then have "x \<noteq> c" by auto
wenzelm@10408
   313
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   314
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   315
      ultimately show ?thesis by blast
nipkow@25564
   316
*)
wenzelm@10389
   317
    qed
wenzelm@10408
   318
  next
wenzelm@23254
   319
    assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
wenzelm@11987
   320
    with snoc have False by blast
wenzelm@23254
   321
    then show ?thesis ..
wenzelm@10408
   322
  next
wenzelm@10408
   323
    assume "xs \<parallel> ys"
wenzelm@11987
   324
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   325
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   326
      by blast
wenzelm@10408
   327
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   328
    with neq ys show ?thesis by blast
wenzelm@10389
   329
  qed
wenzelm@10389
   330
qed
wenzelm@10330
   331
nipkow@25564
   332
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
nipkow@25564
   333
by (rule parallelI)
nipkow@25564
   334
   (erule parallelE, erule conjE,
nipkow@25564
   335
          induct rule: not_prefix_induct, simp+)+
kleing@25299
   336
nipkow@25564
   337
lemma parallel_appendI: "\<lbrakk> xs \<parallel> ys; x = xs @ xs' ; y = ys @ ys' \<rbrakk> \<Longrightarrow> x \<parallel> y"
nipkow@25564
   338
by simp (rule parallel_append)
kleing@25299
   339
wenzelm@25356
   340
lemma parallel_commute: "(a \<parallel> b) = (b \<parallel> a)"
nipkow@25564
   341
unfolding parallel_def by auto
oheimb@14538
   342
wenzelm@25356
   343
oheimb@14538
   344
subsection {* Postfix order on lists *}
wenzelm@17201
   345
wenzelm@19086
   346
definition
wenzelm@21404
   347
  postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
wenzelm@19086
   348
  "(xs >>= ys) = (\<exists>zs. xs = zs @ ys)"
oheimb@14538
   349
wenzelm@21305
   350
lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys"
nipkow@25564
   351
unfolding postfix_def by blast
wenzelm@21305
   352
wenzelm@21305
   353
lemma postfixE [elim?]:
nipkow@25564
   354
assumes "xs >>= ys"
nipkow@25564
   355
obtains zs where "xs = zs @ ys"
nipkow@25564
   356
using assms unfolding postfix_def by blast
wenzelm@21305
   357
wenzelm@21305
   358
lemma postfix_refl [iff]: "xs >>= xs"
wenzelm@14706
   359
  by (auto simp add: postfix_def)
wenzelm@17201
   360
lemma postfix_trans: "\<lbrakk>xs >>= ys; ys >>= zs\<rbrakk> \<Longrightarrow> xs >>= zs"
wenzelm@14706
   361
  by (auto simp add: postfix_def)
wenzelm@17201
   362
lemma postfix_antisym: "\<lbrakk>xs >>= ys; ys >>= xs\<rbrakk> \<Longrightarrow> xs = ys"
wenzelm@14706
   363
  by (auto simp add: postfix_def)
oheimb@14538
   364
wenzelm@17201
   365
lemma Nil_postfix [iff]: "xs >>= []"
wenzelm@14706
   366
  by (simp add: postfix_def)
wenzelm@17201
   367
lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])"
wenzelm@21305
   368
  by (auto simp add: postfix_def)
oheimb@14538
   369
wenzelm@17201
   370
lemma postfix_ConsI: "xs >>= ys \<Longrightarrow> x#xs >>= ys"
wenzelm@14706
   371
  by (auto simp add: postfix_def)
wenzelm@17201
   372
lemma postfix_ConsD: "xs >>= y#ys \<Longrightarrow> xs >>= ys"
wenzelm@14706
   373
  by (auto simp add: postfix_def)
oheimb@14538
   374
wenzelm@17201
   375
lemma postfix_appendI: "xs >>= ys \<Longrightarrow> zs @ xs >>= ys"
wenzelm@14706
   376
  by (auto simp add: postfix_def)
wenzelm@17201
   377
lemma postfix_appendD: "xs >>= zs @ ys \<Longrightarrow> xs >>= ys"
wenzelm@21305
   378
  by (auto simp add: postfix_def)
oheimb@14538
   379
wenzelm@21305
   380
lemma postfix_is_subset: "xs >>= ys ==> set ys \<subseteq> set xs"
wenzelm@21305
   381
proof -
wenzelm@21305
   382
  assume "xs >>= ys"
wenzelm@21305
   383
  then obtain zs where "xs = zs @ ys" ..
wenzelm@21305
   384
  then show ?thesis by (induct zs) auto
wenzelm@21305
   385
qed
oheimb@14538
   386
wenzelm@21305
   387
lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys"
wenzelm@21305
   388
proof -
wenzelm@21305
   389
  assume "x#xs >>= y#ys"
wenzelm@21305
   390
  then obtain zs where "x#xs = zs @ y#ys" ..
wenzelm@21305
   391
  then show ?thesis
wenzelm@21305
   392
    by (induct zs) (auto intro!: postfix_appendI postfix_ConsI)
wenzelm@21305
   393
qed
oheimb@14538
   394
wenzelm@21305
   395
lemma postfix_to_prefix: "xs >>= ys \<longleftrightarrow> rev ys \<le> rev xs"
wenzelm@21305
   396
proof
wenzelm@21305
   397
  assume "xs >>= ys"
wenzelm@21305
   398
  then obtain zs where "xs = zs @ ys" ..
wenzelm@21305
   399
  then have "rev xs = rev ys @ rev zs" by simp
wenzelm@21305
   400
  then show "rev ys <= rev xs" ..
wenzelm@21305
   401
next
wenzelm@21305
   402
  assume "rev ys <= rev xs"
wenzelm@21305
   403
  then obtain zs where "rev xs = rev ys @ zs" ..
wenzelm@21305
   404
  then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp
wenzelm@21305
   405
  then have "xs = rev zs @ ys" by simp
wenzelm@21305
   406
  then show "xs >>= ys" ..
wenzelm@21305
   407
qed
wenzelm@17201
   408
nipkow@25564
   409
lemma distinct_postfix: "distinct xs \<Longrightarrow> xs >>= ys \<Longrightarrow> distinct ys"
nipkow@25564
   410
by (clarsimp elim!: postfixE)
kleing@25299
   411
nipkow@25564
   412
lemma postfix_map: "xs >>= ys \<Longrightarrow> map f xs >>= map f ys"
nipkow@25564
   413
by (auto elim!: postfixE intro: postfixI)
kleing@25299
   414
wenzelm@25356
   415
lemma postfix_drop: "as >>= drop n as"
nipkow@25564
   416
unfolding postfix_def
nipkow@25564
   417
by (rule exI [where x = "take n as"]) simp
kleing@25299
   418
nipkow@25564
   419
lemma postfix_take: "xs >>= ys \<Longrightarrow> xs = take (length xs - length ys) xs @ ys"
nipkow@25564
   420
by (clarsimp elim!: postfixE)
kleing@25299
   421
wenzelm@25356
   422
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"
nipkow@25564
   423
by blast
kleing@25299
   424
wenzelm@25356
   425
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"
nipkow@25564
   426
by blast
wenzelm@25355
   427
wenzelm@25355
   428
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
nipkow@25564
   429
unfolding parallel_def by simp
wenzelm@25355
   430
kleing@25299
   431
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
nipkow@25564
   432
unfolding parallel_def by simp
kleing@25299
   433
nipkow@25564
   434
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
nipkow@25564
   435
by auto
kleing@25299
   436
nipkow@25564
   437
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
nipkow@25564
   438
by (metis Cons_prefix_Cons parallelE parallelI)
nipkow@25564
   439
(*
kleing@25299
   440
  apply simp
kleing@25299
   441
  apply (rule parallelI)
kleing@25299
   442
   apply simp
kleing@25299
   443
   apply (erule parallelD1)
kleing@25299
   444
  apply simp
kleing@25299
   445
  apply (erule parallelD2)
kleing@25299
   446
 done
nipkow@25564
   447
*)
kleing@25299
   448
lemma not_equal_is_parallel:
kleing@25299
   449
  assumes neq: "xs \<noteq> ys"
wenzelm@25356
   450
    and len: "length xs = length ys"
wenzelm@25356
   451
  shows "xs \<parallel> ys"
kleing@25299
   452
  using len neq
wenzelm@25355
   453
proof (induct rule: list_induct2)
wenzelm@25356
   454
  case 1
wenzelm@25356
   455
  then show ?case by simp
kleing@25299
   456
next
kleing@25299
   457
  case (2 a as b bs)
wenzelm@25355
   458
  have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
kleing@25299
   459
  show ?case
kleing@25299
   460
  proof (cases "a = b")
wenzelm@25355
   461
    case True
wenzelm@25355
   462
    then have "as \<noteq> bs" using 2 by simp
wenzelm@25355
   463
    then show ?thesis by (rule Cons_parallelI2 [OF True ih])
kleing@25299
   464
  next
kleing@25299
   465
    case False
wenzelm@25355
   466
    then show ?thesis by (rule Cons_parallelI1)
kleing@25299
   467
  qed
kleing@25299
   468
qed
haftmann@22178
   469
wenzelm@25355
   470
wenzelm@25356
   471
subsection {* Executable code *}
haftmann@22178
   472
haftmann@22178
   473
lemma less_eq_code [code func]:
wenzelm@25356
   474
    "([]\<Colon>'a\<Colon>{eq, ord} list) \<le> xs \<longleftrightarrow> True"
wenzelm@25356
   475
    "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> [] \<longleftrightarrow> False"
wenzelm@25356
   476
    "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"
haftmann@22178
   477
  by simp_all
haftmann@22178
   478
haftmann@22178
   479
lemma less_code [code func]:
wenzelm@25356
   480
    "xs < ([]\<Colon>'a\<Colon>{eq, ord} list) \<longleftrightarrow> False"
wenzelm@25356
   481
    "[] < (x\<Colon>'a\<Colon>{eq, ord})# xs \<longleftrightarrow> True"
wenzelm@25356
   482
    "(x\<Colon>'a\<Colon>{eq, ord}) # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"
haftmann@22178
   483
  unfolding strict_prefix_def by auto
haftmann@22178
   484
haftmann@22178
   485
lemmas [code func] = postfix_to_prefix
haftmann@22178
   486
wenzelm@10330
   487
end