Theory List_Prefix

Up to index of Isabelle/HOL/ex

theory List_Prefix
imports List
begin

(*  Title:      HOL/Library/List_Prefix.thy
    ID:         $Id: List_Prefix.thy,v 1.34 2008/03/27 18:04:39 haftmann Exp $
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
*)

header {* List prefixes and postfixes *}

theory List_Prefix
imports List
begin

subsection {* Prefix order on lists *}

instantiation list :: (type) order
begin

definition
  prefix_def [code func del]: "xs ≤ ys = (∃zs. ys = xs @ zs)"

definition
  strict_prefix_def [code func del]: "xs < ys = (xs ≤ ys ∧ xs ≠ (ys::'a list))"

instance
  by intro_classes (auto simp add: prefix_def strict_prefix_def)

end

lemma prefixI [intro?]: "ys = xs @ zs ==> xs ≤ ys"
  unfolding prefix_def by blast

lemma prefixE [elim?]:
  assumes "xs ≤ ys"
  obtains zs where "ys = xs @ zs"
  using assms unfolding prefix_def by blast

lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
  unfolding strict_prefix_def prefix_def by blast

lemma strict_prefixE' [elim?]:
  assumes "xs < ys"
  obtains z zs where "ys = xs @ z # zs"
proof -
  from `xs < ys` obtain us where "ys = xs @ us" and "xs ≠ ys"
    unfolding strict_prefix_def prefix_def by blast
  with that show ?thesis by (auto simp add: neq_Nil_conv)
qed

lemma strict_prefixI [intro?]: "xs ≤ ys ==> xs ≠ ys ==> xs < (ys::'a list)"
  unfolding strict_prefix_def by blast

lemma strict_prefixE [elim?]:
  fixes xs ys :: "'a list"
  assumes "xs < ys"
  obtains "xs ≤ ys" and "xs ≠ ys"
  using assms unfolding strict_prefix_def by blast


subsection {* Basic properties of prefixes *}

theorem Nil_prefix [iff]: "[] ≤ xs"
  by (simp add: prefix_def)

theorem prefix_Nil [simp]: "(xs ≤ []) = (xs = [])"
  by (induct xs) (simp_all add: prefix_def)

lemma prefix_snoc [simp]: "(xs ≤ ys @ [y]) = (xs = ys @ [y] ∨ xs ≤ ys)"
proof
  assume "xs ≤ ys @ [y]"
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
  show "xs = ys @ [y] ∨ xs ≤ ys"
    by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
next
  assume "xs = ys @ [y] ∨ xs ≤ ys"
  then show "xs ≤ ys @ [y]"
    by (metis order_eq_iff strict_prefixE strict_prefixI' xt1(7))
qed

lemma Cons_prefix_Cons [simp]: "(x # xs ≤ y # ys) = (x = y ∧ xs ≤ ys)"
  by (auto simp add: prefix_def)

lemma same_prefix_prefix [simp]: "(xs @ ys ≤ xs @ zs) = (ys ≤ zs)"
  by (induct xs) simp_all

lemma same_prefix_nil [iff]: "(xs @ ys ≤ xs) = (ys = [])"
  by (metis append_Nil2 append_self_conv order_eq_iff prefixI)

lemma prefix_prefix [simp]: "xs ≤ ys ==> xs ≤ ys @ zs"
  by (metis order_le_less_trans prefixI strict_prefixE strict_prefixI)

lemma append_prefixD: "xs @ ys ≤ zs ==> xs ≤ zs"
  by (auto simp add: prefix_def)

theorem prefix_Cons: "(xs ≤ y # ys) = (xs = [] ∨ (∃zs. xs = y # zs ∧ zs ≤ ys))"
  by (cases xs) (auto simp add: prefix_def)

theorem prefix_append:
  "(xs ≤ ys @ zs) = (xs ≤ ys ∨ (∃us. xs = ys @ us ∧ us ≤ zs))"
  apply (induct zs rule: rev_induct)
   apply force
  apply (simp del: append_assoc add: append_assoc [symmetric])
  apply (metis append_eq_appendI)
  done

lemma append_one_prefix:
  "xs ≤ ys ==> length xs < length ys ==> xs @ [ys ! length xs] ≤ ys"
  unfolding prefix_def
  by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj
    eq_Nil_appendI nth_drop')

theorem prefix_length_le: "xs ≤ ys ==> length xs ≤ length ys"
  by (auto simp add: prefix_def)

lemma prefix_same_cases:
  "(xs1::'a list) ≤ ys ==> xs2 ≤ ys ==> xs1 ≤ xs2 ∨ xs2 ≤ xs1"
  unfolding prefix_def by (metis append_eq_append_conv2)

lemma set_mono_prefix: "xs ≤ ys ==> set xs ⊆ set ys"
  by (auto simp add: prefix_def)

lemma take_is_prefix: "take n xs ≤ xs"
  unfolding prefix_def by (metis append_take_drop_id)

lemma map_prefixI: "xs ≤ ys ==> map f xs ≤ map f ys"
  by (auto simp: prefix_def)

lemma prefix_length_less: "xs < ys ==> length xs < length ys"
  by (auto simp: strict_prefix_def prefix_def)

lemma strict_prefix_simps [simp]:
    "xs < [] = False"
    "[] < (x # xs) = True"
    "(x # xs) < (y # ys) = (x = y ∧ xs < ys)"
  by (simp_all add: strict_prefix_def cong: conj_cong)

lemma take_strict_prefix: "xs < ys ==> take n xs < ys"
  apply (induct n arbitrary: xs ys)
   apply (case_tac ys, simp_all)[1]
  apply (metis order_less_trans strict_prefixI take_is_prefix)
  done

lemma not_prefix_cases:
  assumes pfx: "¬ ps ≤ ls"
  obtains
    (c1) "ps ≠ []" and "ls = []"
  | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "¬ as ≤ xs"
  | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x ≠ a"
proof (cases ps)
  case Nil then show ?thesis using pfx by simp
next
  case (Cons a as)
  note c = `ps = a#as`
  show ?thesis
  proof (cases ls)
    case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
  next
    case (Cons x xs)
    show ?thesis
    proof (cases "x = a")
      case True
      have "¬ as ≤ xs" using pfx c Cons True by simp
      with c Cons True show ?thesis by (rule c2)
    next
      case False
      with c Cons show ?thesis by (rule c3)
    qed
  qed
qed

lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
  assumes np: "¬ ps ≤ ls"
    and base: "!!x xs. P (x#xs) []"
    and r1: "!!x xs y ys. x ≠ y ==> P (x#xs) (y#ys)"
    and r2: "!!x xs y ys. [| x = y; ¬ xs ≤ ys; P xs ys |] ==> P (x#xs) (y#ys)"
  shows "P ps ls" using np
proof (induct ls arbitrary: ps)
  case Nil then show ?case
    by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
next
  case (Cons y ys)
  then have npfx: "¬ ps ≤ (y # ys)" by simp
  then obtain x xs where pv: "ps = x # xs"
    by (rule not_prefix_cases) auto
  show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
qed


subsection {* Parallel lists *}

definition
  parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
  "(xs \<parallel> ys) = (¬ xs ≤ ys ∧ ¬ ys ≤ xs)"

lemma parallelI [intro]: "¬ xs ≤ ys ==> ¬ ys ≤ xs ==> xs \<parallel> ys"
  unfolding parallel_def by blast

lemma parallelE [elim]:
  assumes "xs \<parallel> ys"
  obtains "¬ xs ≤ ys ∧ ¬ ys ≤ xs"
  using assms unfolding parallel_def by blast

theorem prefix_cases:
  obtains "xs ≤ ys" | "ys < xs" | "xs \<parallel> ys"
  unfolding parallel_def strict_prefix_def by blast

theorem parallel_decomp:
  "xs \<parallel> ys ==> ∃as b bs c cs. b ≠ c ∧ xs = as @ b # bs ∧ ys = as @ c # cs"
proof (induct xs rule: rev_induct)
  case Nil
  then have False by auto
  then show ?case ..
next
  case (snoc x xs)
  show ?case
  proof (rule prefix_cases)
    assume le: "xs ≤ ys"
    then obtain ys' where ys: "ys = xs @ ys'" ..
    show ?thesis
    proof (cases ys')
      assume "ys' = []"
      then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
    next
      fix c cs assume ys': "ys' = c # cs"
      then show ?thesis
        by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixI
          same_prefix_prefix snoc.prems ys)
    qed
  next
    assume "ys < xs" then have "ys ≤ xs @ [x]" by (simp add: strict_prefix_def)
    with snoc have False by blast
    then show ?thesis ..
  next
    assume "xs \<parallel> ys"
    with snoc obtain as b bs c cs where neq: "(b::'a) ≠ c"
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
      by blast
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
    with neq ys show ?thesis by blast
  qed
qed

lemma parallel_append: "a \<parallel> b ==> a @ c \<parallel> b @ d"
  apply (rule parallelI)
    apply (erule parallelE, erule conjE,
      induct rule: not_prefix_induct, simp+)+
  done

lemma parallel_appendI: "xs \<parallel> ys ==> x = xs @ xs' ==> y = ys @ ys' ==> x \<parallel> y"
  by (simp add: parallel_append)

lemma parallel_commute: "a \<parallel> b <-> b \<parallel> a"
  unfolding parallel_def by auto


subsection {* Postfix order on lists *}

definition
  postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
  "(xs >>= ys) = (∃zs. xs = zs @ ys)"

lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys"
  unfolding postfix_def by blast

lemma postfixE [elim?]:
  assumes "xs >>= ys"
  obtains zs where "xs = zs @ ys"
  using assms unfolding postfix_def by blast

lemma postfix_refl [iff]: "xs >>= xs"
  by (auto simp add: postfix_def)
lemma postfix_trans: "[|xs >>= ys; ys >>= zs|] ==> xs >>= zs"
  by (auto simp add: postfix_def)
lemma postfix_antisym: "[|xs >>= ys; ys >>= xs|] ==> xs = ys"
  by (auto simp add: postfix_def)

lemma Nil_postfix [iff]: "xs >>= []"
  by (simp add: postfix_def)
lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])"
  by (auto simp add: postfix_def)

lemma postfix_ConsI: "xs >>= ys ==> x#xs >>= ys"
  by (auto simp add: postfix_def)
lemma postfix_ConsD: "xs >>= y#ys ==> xs >>= ys"
  by (auto simp add: postfix_def)

lemma postfix_appendI: "xs >>= ys ==> zs @ xs >>= ys"
  by (auto simp add: postfix_def)
lemma postfix_appendD: "xs >>= zs @ ys ==> xs >>= ys"
  by (auto simp add: postfix_def)

lemma postfix_is_subset: "xs >>= ys ==> set ys ⊆ set xs"
proof -
  assume "xs >>= ys"
  then obtain zs where "xs = zs @ ys" ..
  then show ?thesis by (induct zs) auto
qed

lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys"
proof -
  assume "x#xs >>= y#ys"
  then obtain zs where "x#xs = zs @ y#ys" ..
  then show ?thesis
    by (induct zs) (auto intro!: postfix_appendI postfix_ConsI)
qed

lemma postfix_to_prefix: "xs >>= ys <-> rev ys ≤ rev xs"
proof
  assume "xs >>= ys"
  then obtain zs where "xs = zs @ ys" ..
  then have "rev xs = rev ys @ rev zs" by simp
  then show "rev ys <= rev xs" ..
next
  assume "rev ys <= rev xs"
  then obtain zs where "rev xs = rev ys @ zs" ..
  then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp
  then have "xs = rev zs @ ys" by simp
  then show "xs >>= ys" ..
qed

lemma distinct_postfix: "distinct xs ==> xs >>= ys ==> distinct ys"
  by (clarsimp elim!: postfixE)

lemma postfix_map: "xs >>= ys ==> map f xs >>= map f ys"
  by (auto elim!: postfixE intro: postfixI)

lemma postfix_drop: "as >>= drop n as"
  unfolding postfix_def
  apply (rule exI [where x = "take n as"])
  apply simp
  done

lemma postfix_take: "xs >>= ys ==> xs = take (length xs - length ys) xs @ ys"
  by (clarsimp elim!: postfixE)

lemma parallelD1: "x \<parallel> y ==> ¬ x ≤ y"
  by blast

lemma parallelD2: "x \<parallel> y ==> ¬ y ≤ x"
  by blast

lemma parallel_Nil1 [simp]: "¬ x \<parallel> []"
  unfolding parallel_def by simp

lemma parallel_Nil2 [simp]: "¬ [] \<parallel> x"
  unfolding parallel_def by simp

lemma Cons_parallelI1: "a ≠ b ==> a # as \<parallel> b # bs"
  by auto

lemma Cons_parallelI2: "[| a = b; as \<parallel> bs |] ==> a # as \<parallel> b # bs"
  by (metis Cons_prefix_Cons parallelE parallelI)

lemma not_equal_is_parallel:
  assumes neq: "xs ≠ ys"
    and len: "length xs = length ys"
  shows "xs \<parallel> ys"
  using len neq
proof (induct rule: list_induct2)
  case Nil
  then show ?case by simp
next
  case (Cons a as b bs)
  have ih: "as ≠ bs ==> as \<parallel> bs" by fact
  show ?case
  proof (cases "a = b")
    case True
    then have "as ≠ bs" using Cons by simp
    then show ?thesis by (rule Cons_parallelI2 [OF True ih])
  next
    case False
    then show ?thesis by (rule Cons_parallelI1)
  qed
qed


subsection {* Executable code *}

lemma less_eq_code [code func]:
    "([]::'a::{eq, ord} list) ≤ xs <-> True"
    "(x::'a::{eq, ord}) # xs ≤ [] <-> False"
    "(x::'a::{eq, ord}) # xs ≤ y # ys <-> x = y ∧ xs ≤ ys"
  by simp_all

lemma less_code [code func]:
    "xs < ([]::'a::{eq, ord} list) <-> False"
    "[] < (x::'a::{eq, ord})# xs <-> True"
    "(x::'a::{eq, ord}) # xs < y # ys <-> x = y ∧ xs < ys"
  unfolding strict_prefix_def by auto

lemmas [code func] = postfix_to_prefix

end

Prefix order on lists

lemma prefixI:

  ys = xs @ zs ==> xs  ys

lemma prefixE:

  [| xs  ys; !!zs. ys = xs @ zs ==> thesis |] ==> thesis

lemma strict_prefixI':

  ys = xs @ z # zs ==> xs < ys

lemma strict_prefixE':

  [| xs < ys; !!z zs. ys = xs @ z # zs ==> thesis |] ==> thesis

lemma strict_prefixI:

  [| xs  ys; xs  ys |] ==> xs < ys

lemma strict_prefixE:

  [| xs < ys; [| xs  ys; xs  ys |] ==> thesis |] ==> thesis

Basic properties of prefixes

theorem Nil_prefix:

  []  xs

theorem prefix_Nil:

  (xs  []) = (xs = [])

lemma prefix_snoc:

  (xs  ys @ [y]) = (xs = ys @ [y] ∨ xs  ys)

lemma Cons_prefix_Cons:

  (x # xs  y # ys) = (x = yxs  ys)

lemma same_prefix_prefix:

  (xs @ ys  xs @ zs) = (ys  zs)

lemma same_prefix_nil:

  (xs @ ys  xs) = (ys = [])

lemma prefix_prefix:

  xs  ys ==> xs  ys @ zs

lemma append_prefixD:

  xs @ ys  zs ==> xs  zs

theorem prefix_Cons:

  (xs  y # ys) = (xs = [] ∨ (∃zs. xs = y # zszs  ys))

theorem prefix_append:

  (xs  ys @ zs) = (xs  ys ∨ (∃us. xs = ys @ usus  zs))

lemma append_one_prefix:

  [| xs  ys; length xs < length ys |] ==> xs @ [ys ! length xs]  ys

theorem prefix_length_le:

  xs  ys ==> length xs  length ys

lemma prefix_same_cases:

  [| xs1  ys; xs2  ys |] ==> xs1  xs2xs2  xs1

lemma set_mono_prefix:

  xs  ys ==> set xs  set ys

lemma take_is_prefix:

  take n xs  xs

lemma map_prefixI:

  xs  ys ==> map f xs  map f ys

lemma prefix_length_less:

  xs < ys ==> length xs < length ys

lemma strict_prefix_simps:

  (xs < []) = False
  ([] < x # xs) = True
  (x # xs < y # ys) = (x = yxs < ys)

lemma take_strict_prefix:

  xs < ys ==> take n xs < ys

lemma not_prefix_cases:

  [| ¬ ps  ls; [| ps  []; ls = [] |] ==> thesis;
     !!a as x xs. [| ps = a # as; ls = x # xs; x = a; ¬ as  xs |] ==> thesis;
     !!a as x xs. [| ps = a # as; ls = x # xs; x  a |] ==> thesis |]
  ==> thesis

lemma not_prefix_induct:

  [| ¬ ps  ls; !!x xs. P (x # xs) []; !!x xs y ys. x  y ==> P (x # xs) (y # ys);
     !!x xs y ys. [| x = y; ¬ xs  ys; P xs ys |] ==> P (x # xs) (y # ys) |]
  ==> P ps ls

Parallel lists

lemma parallelI:

  [| ¬ xs  ys; ¬ ys  xs |] ==> xs \<parallel> ys

lemma parallelE:

  [| xs \<parallel> ys; ¬ xs  ys ∧ ¬ ys  xs ==> thesis |] ==> thesis

theorem prefix_cases:

  [| xs  ys ==> thesis; ys < xs ==> thesis; xs \<parallel> ys ==> thesis |]
  ==> thesis

theorem parallel_decomp:

  xs \<parallel> ys ==> ∃as b bs c cs. b  cxs = as @ b # bsys = as @ c # cs

lemma parallel_append:

  a \<parallel> b ==> a @ c \<parallel> b @ d

lemma parallel_appendI:

  [| xs \<parallel> ys; x = xs @ xs'; y = ys @ ys' |] ==> x \<parallel> y

lemma parallel_commute:

  (a \<parallel> b) = (b \<parallel> a)

Postfix order on lists

lemma postfixI:

  xs = zs @ ys ==> xs >>= ys

lemma postfixE:

  [| xs >>= ys; !!zs. xs = zs @ ys ==> thesis |] ==> thesis

lemma postfix_refl:

  xs >>= xs

lemma postfix_trans:

  [| xs >>= ys; ys >>= zs |] ==> xs >>= zs

lemma postfix_antisym:

  [| xs >>= ys; ys >>= xs |] ==> xs = ys

lemma Nil_postfix:

  xs >>= []

lemma postfix_Nil:

  ([] >>= xs) = (xs = [])

lemma postfix_ConsI:

  xs >>= ys ==> x # xs >>= ys

lemma postfix_ConsD:

  xs >>= y # ys ==> xs >>= ys

lemma postfix_appendI:

  xs >>= ys ==> zs @ xs >>= ys

lemma postfix_appendD:

  xs >>= zs @ ys ==> xs >>= ys

lemma postfix_is_subset:

  xs >>= ys ==> set ys  set xs

lemma postfix_ConsD2:

  x # xs >>= y # ys ==> xs >>= ys

lemma postfix_to_prefix:

  (xs >>= ys) = (rev ys  rev xs)

lemma distinct_postfix:

  [| distinct xs; xs >>= ys |] ==> distinct ys

lemma postfix_map:

  xs >>= ys ==> map f xs >>= map f ys

lemma postfix_drop:

  as >>= drop n as

lemma postfix_take:

  xs >>= ys ==> xs = take (length xs - length ys) xs @ ys

lemma parallelD1:

  x \<parallel> y ==> ¬ x  y

lemma parallelD2:

  x \<parallel> y ==> ¬ y  x

lemma parallel_Nil1:

  ¬ x \<parallel> []

lemma parallel_Nil2:

  ¬ [] \<parallel> x

lemma Cons_parallelI1:

  a  b ==> a # as \<parallel> b # bs

lemma Cons_parallelI2:

  [| a = b; as \<parallel> bs |] ==> a # as \<parallel> b # bs

lemma not_equal_is_parallel:

  [| xs  ys; length xs = length ys |] ==> xs \<parallel> ys

Executable code

lemma less_eq_code:

  ([]  xs) = True
  (x # xs  []) = False
  (x # xs  y # ys) = (x = yxs  ys)

lemma less_code:

  (xs < []) = False
  ([] < x # xs) = True
  (x # xs < y # ys) = (x = yxs < ys)

lemma

  (xs >>= ys) = (rev ys  rev xs)