IntSet联合证明结构归纳

时间:2016-07-08 10:59:17

标签: scala induction

假设您有以下定义:

abstract class IntSet {
  def incl(x: Int): IntSet    
  def contains(x: Int): Boolean    
  def union(other: IntSet): IntSet
}

case class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet {
  def incl(x: Int) =
    if (x < elem) NonEmpty(elem, left incl x, right)
    else if (x > elem) NonEmpty(elem, left, right incl x)
    else this

  def contains(x: Int) =
    if (x < elem) left contains x
    else if (x > elem) right contains x
    else true

  def union(other: IntSet) = (left union (right union other)) incl elem
}

object Empty extends IntSet {
  def incl(x: Int) = NonEmpty(x, Empty, Empty)    
  def contains(x: Int) = false    
  def union(other: IntSet) = other
}

并且必须证明以下命题:

(xs union ys) contains x = xs contains x || ys contains x

从这里我推断出两个基本案例。 xs =空,ys =空。这是我因为以下原因而陷入困境的第二个基本案例:

// substituting ys = Empty
 (xs union Empty) contains x = xs contains x || Empty contains x
// RHS:
 xs contains x || false
 xs contains x
// LHS:
 ((left union (right union Empty)) incl elem) contains x // By definition of NonEmpty.union

如何将LHS减少到xs包含x?我是否必须对xs union Empty = xs做另一个归纳假设,如果是,那怎么能用于表达式呢?

1 个答案:

答案 0 :(得分:1)

证明:

(xs union ys) contains x = xs contains x || ys contains x

给出:

  1. 以上定义
  2. Empty contains x = false
  3. (s incl x) contains x = true
  4. (s incl y) contains x = s contains x ; if x != y
  

仅使用xs而不是ys的归纳步骤和结构证明,请参考原始问题


案例1:

if xs = Empty

  左手边:
  (Empty union ys) contains x
  = ys contains x => Definition of Empty union other

右手侧:
  Empty contains x || ys contains x
  = false || ys contains x => Definition of Empty contains x
  = ys contains x => Truth table of OR ( false OR true = true, false OR false = false)

  左手边=右手边,因此证明了归纳法,该语句对所有子树都是正确的


第二种情况:
  if (xs is NonEmpty(z, l, r) and z = x)

  左手边:
  (NonEmpty(x, l, r) union ys) contains x
  = ((l union(r union ys)) incl x) contains x => From definition of union on NonEmpty
  = true => from (3) above (s incl x) contains x = true

  右侧:
  xs contains x || ys contains x
  = NonEmpty(x, l, r) contains x || ys contains x => From definition of xs
  = true || ys contains x => From definition of contains on NonEmpty
  = true => Truth table of OR (true OR false = true, true OR true = true)

 左侧=右侧


案例III:
  if ( xs is NonEmpty(z, l, r) and z < x )

  左手边:
  (NonEmpty(z, l, r) union ys) contains x
  = (l union(r union ys) incl z) contains x => From definition of union
  = (l union(r union ys)) contains x => From (4) above (s incl x) contains y = s contains y; if x != y
  = (l contains x || (r union ys) contains x) => From induction step, the statement is true for all subtrees
  = (l contains x || r contains x || ys contains x) => From induction step, the statement is true for all subtrees
  = r contains x || ys contains x => since z < x, definition of contains (l contains x) returns false

  右侧:
  (if z < x)
  NonEmpty(z, l, r) contains x || ys contains x
  = r contains x || ys contains x => definition of contains,

  左侧=右侧


案例if ( xs is NonEmpty(z, l, r) and z > x )与上面的案例III类似   由此证明