SE 504 Formal Methods and Models
Spring 2020
HW #8: Tail Recursion, k-Majorities
Due: April 21

1. The reverse-2-adic numeral system is similar to the better known binary numeral system, but it differs in two ways:

To illustrate, we convert the reverse-2-adic numeral 12112 into base 10:

1×1 + 2×2 + 1×4 + 1×8 + 2×16 = 49

Develop a pseudo-tail recursive definition for the function f : ℕ ⟶ list of {1,2} such that

f.n = reverse-2-adic representation of n.

Here are a few examples:

f.49 = 12112 f.50 = 22112 f.0 = empty

You are to understand 12112, for example, as being an abbreviation for 1 ⊕ (2 ⊕ (1 ⊕ (1 ⊕ (2 ⊕ empty)))) (See HW #5). That is, head.(12112) = 1 and tail.(12112) = 2112.

Hint 1: The leftmost digit of a reverse-2-adic numeral determines whether the number it represents is odd or even.

Hint 2: Consider a reverse-2-adic numeral z = d ⊕ x, where d is either 1 or 2 and x is a reverse-2-adic numeral. Consider the relationship between the number represented by z and that represented by x (i.e., z with its leftmost digit "chopped off").

Using your definition of f (or the definition of a fully tail recursive function f' such that f'.n.empty = f.n for all n), develop a program satisfying this specification:

|[ con N : nat;
   var x : list of {1,2};

   x := ?

   {Q: x = f.N  }
]|

Be sure to indicate the loop invariant and bound function.


2. In a Zoom meeting we solved the Find a Majority problem. Recall that the majority of a bag B is defined to be a value whose multiplicity (i.e., # of occurrences) in B is greater than |B|/2, where |B| is the cardinality (i.e., size) of B. Obviously, a bag has at most one majority. For example, the bag U = {| 5, 3, 5, 5, 2, 5, 5 |} has 5 as its majority, whereas the bag V = {| 5, 3, 4, 5, 5, 2, 1 |} has no majority.

The concept of majority is easily generalized to k-majority, for k>1. We define a value x to be a k-majority of a bag B if the number of occurrences of x in B is greater than |B|/k. (Thus, what we called a majority above corresponds to a 2-majority under this generalized definition.)

Clearly, a bag has at most k-1 k-majorities. For example, the bag W = {| 2, 2, 2, 7, 7, 7, 5, 0 |} has both 2 and 7 as 3-majorities. The bags U and V from above have 5 alone as a 3-majority, and the bag X = {| 0, 0, 1, 2, 5, 7 |} has no 3-majority.

For this problem, you are to develop a program that, given as input a bag B represented by the constant array B, establishes variables r0 and r1 so that, if x is a 3-majority of B, either x = r0 or x = r1. (Hence, if B has two 3-majorities, r0 must be one of them and r1 the other. If, on the other hand, B has one 3-majority, it must be equal to one among r0 and r1.) Here is the program, partially completed:

|[con N : nat;
  con B : array[0..N) of T;
  var r0, r1 : T;
  var k0, k1 : int;
  var n : int;

  r0, k0, r1, k1, n := null, 0, null, 0, 0;

  { loop invariant I: {(r0,k0), (r1,k1)} ∈ Red3.Bn) ∧ 0≤n≤N }
  { bound function t: N-n}
  do n ≠ N →
     r0, k0, r1, k1 := ?,?,?,?
     ;n := n+1
  od
  {Q: {(r0,k0),(r1,k1)} ∈ Red3.BN}
]|

Regarding notation used above:

You may assume the following theorem, which is a generalization of the one used in class:

Theorem: If x is a 3-majority of A and C is a member of Red3.A, then x is also a 3-majority of C.

Similar to what we encountered in the lecture, the key to finishing the program is understanding how to transform a bag that is a member of Red3.Bn to a bag that is a member of Red3.Bn+1.

For the sake of convenience, you may assume that the value null (to which r0 and r1 are initialized) does not occur in the array B. This relieves you from having to worry about whether or not r0 and r1 have "well-defined" values.