Isabelle / Isar基本风格(练习4.6)

时间:2018-07-17 11:28:55

标签: isabelle proof isar

我有兴趣使用Isabelle / Isar来编写可人工读取和机器检查的证明,并且希望改善自己的风格并简化证明。

prog-prove 进行以下练习:

练习4.6。定义递归函数elems :: 'a list ⇒ 'a set并证明x ∈ elems xs ⟹ ∃ ys zs. xs = ys @ x # zs ∧ x ∉ elems ys

模仿类似于笔和纸的东西,我的解决方法是

fun elems :: "'a list ⇒ 'a set" where
"elems [] = {}" |
"elems (x # xs) = {x} ∪ elems xs"

fun takeUntil :: "('a ⇒ bool) ⇒ 'a list ⇒ 'a list" where
"takeUntil f [] = []" |
"takeUntil f (x # xs) = (case (f x) of False ⇒ x # takeUntil f xs | True ⇒ [])"

theorem "x ∈ elems xs ⟹ ∃ ys zs. xs = ys @ x # zs ∧ x ∉ elems ys"
proof -
  assume 1: "x ∈ elems xs"
  let ?ys = "takeUntil (λ z. z = x) xs"
  let ?zs = "drop (length ?ys + 1) xs"
  have "xs = ?ys @ x # ?zs ∧ x ∉ elems ?ys"
  proof
    have 2: "x ∉ elems ?ys"
    proof (induction xs)
      case Nil
      thus ?case by simp
    next
      case (Cons a xs)
      thus ?case
      proof -
        {
          assume "a = x"
          hence "takeUntil (λz. z = x) (a # xs) = []" by simp
          hence A: ?thesis by simp
        }
        note eq = this
        {
          assume "a ≠ x"
          hence "takeUntil (λz. z = x) (a # xs) = a # takeUntil (λz. z = x) xs" by simp
          hence ?thesis using Cons.IH by auto
        }
        note noteq = this
        have "a = x ∨ a ≠ x" by simp
        thus ?thesis using eq noteq by blast
      qed
    qed

    from 1 have "xs = ?ys @ x # ?zs"
    proof (induction xs)
      case Nil
      hence False by simp
      thus ?case by simp
    next
      case (Cons a xs)
      {
        assume 1: "a = x"
        hence 2: "takeUntil (λz. z = x) (a # xs) = []" by simp
        hence "length (takeUntil (λz. z = x) (a # xs)) + 1 = 1" by simp
        hence 3: "drop (length (takeUntil (λz. z = x) (a # xs)) + 1) (a # xs) = xs" by simp
        from 1 2 3 have ?case by simp
      }
      note eq = this
      {
        assume 1: "a ≠ x"
        with Cons.prems have "x ∈ elems xs" by simp
        with Cons.IH
         have IH: "xs = takeUntil (λz. z = x) xs @ x # drop (length (takeUntil (λz. z = x) xs) + 1) xs" by simp
        from 1 have 2: "takeUntil (λz. z = x) (a # xs) = a # takeUntil (λz. z = x) (xs)" by simp
        from 1 have "drop (length (takeUntil (λz. z = x) (a # xs)) + 1) (a # xs) = drop (length (takeUntil (λz. z = x) xs) + 1) xs" by simp
        hence ?case using IH 2 by simp
      }
      note noteq = this
      have "a = x ∨ a ≠ x" by simp
      thus ?case using eq noteq by blast
    qed
    with 2 have 3: ?thesis by blast
    thus "xs = takeUntil (λz. z = x) xs @ x # drop (length (takeUntil (λz. z = x) xs) + 1) xs" by simp
    from 3 show "x ∉ elems (takeUntil (λz. z = x) xs)" by simp
  qed
  thus ?thesis by blast
qed

但是看起来很长。特别是,我认为此处调用排除中间的定律很麻烦,我觉得应该有一些方便的示意图变量,例如?goal,可以引用当前目标或其他内容。

如何在不牺牲清晰度的情况下简化此证明?

2 个答案:

答案 0 :(得分:1)

并不是您的特定问题的真正答案,但我还是要指出,更简洁的证明仍然可以理解。

2,$s/\([^,]*\),\([^,]*\)/"\1","\2"/

答案 1 :(得分:0)

这是比您自己的证据更短的证明:

fun elems :: ‹'a list ⇒ 'a set› where
  ‹elems [] = {}› |
  ‹elems (x#xs) = {x} ∪ elems xs›

lemma elems_prefix_suffix:
  assumes ‹x ∈ elems xs›
  shows ‹∃pre suf. xs = pre @ [x] @ suf ∧ x ∉ elems pre›
using assms proof(induction xs)
  fix y ys
  assume *: ‹x ∈ elems (y#ys)›
    and IH: ‹x ∈ elems ys ⟹ ∃pre suf. ys = pre @ [x] @ suf ∧ x ∉ elems pre›
  {
    assume ‹x = y›
    from this have ‹∃pre suf. y#ys = pre @ [x] @ suf ∧ x ∉ elems pre›
      using * by fastforce
  }
  note L = this
  {
    assume ‹x ≠ y› and ‹x ∈ elems ys›
    moreover from this obtain pre and suf where ‹ys = pre @ [x] @ suf› and ‹x ∉ elems pre›
      using IH by auto
    moreover have ‹y#ys = y#pre @ [x] @ suf› and ‹x ∉ elems (y#pre)›
      by(simp add: calculation)+
    ultimately have ‹∃pre suf. y#ys = pre @ [x] @ suf ∧ x ∉ elems pre›
      by(metis append_Cons)
  }
  from this and L show ‹∃pre suf. y#ys = pre @ [x] @ suf ∧ x ∉ elems pre›
    using * by auto
qed auto ― ‹Base case trivial›

我使用了Isar的一些功能来压缩证明:

  1. 大括号{...}中的块使您可以进行假设推理。
  2. 可以使用note明确命名事实。
  3. moreover关键字启动一个计算,该计算在事实成立时会隐式“携带”事实。使用ultimately关键字可以使计算“走到头”。这种样式可以显着减少在证明过程中需要引入的显式命名事实的数量。
  4. qed auto通过将auto应用于所有剩余的子目标来完成证明。评论指出,剩余的子目标是归纳的基本情况,这是微不足道的。