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