Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relational operations using only increment, loop, assign, zero

This is a follow up question for: Subtraction operation using only increment, loop, assign, zero

We're only allowed to use the following operations:

  1. incr(x) - Once this function is called it will assign x + 1 to x
  2. assign(x, y) - This function will assign the value of y to x (x = y)
  3. zero(x) - This function will assign 0 to x (x = 0)
  4. loop X { } - operations written within brackets will be executed X times

For example, addition can be implemented as follows:

add(x, y) {
    loop x
        { y = incr(y) }
    return y
}

How do I implement the relational operators using these four operations? The relational operations are:

  1. eq(x, y) - Is x equal to y?
  2. lt(x, y) - Is x lesser than y?
  3. gt(x, y) - Is x greater than y?

We also have their opposites:

  1. ne(x, y) - Is x not equal to y?
  2. gte(x, y) - Is x greater than or equal to y?
  3. lte(x, y) - Is x lesser than or equal to y?

Any help will be appreciated.

like image 604
Swathi Devireddy Avatar asked Jan 16 '16 17:01

Swathi Devireddy


1 Answers

The set of natural numbers N is closed under addition and subtraction:

N + N = N
N - N = N

This means that the addition or subtraction of two natural numbers is also a natural number (considering 0 - 1 is 0 and not -1, we can't have negative natural numbers).

However, the set of natural numbers N is not closed under relational operations:

N < N = {0, 1}
N > N = {0, 1}

This means that the result of comparing two natural numbers is either truthfulness (i.e. 1) or falsehood (i.e. 0).

So, we treat the set of booleans (i.e. {0, 1}) as a restricted set of the natural numbers (i.e. N).

false = 0
true  = incr(false)

The first question we must answer is “how do we encode if statements so that we may branch based on either truthfulness or falsehood?” The answer is simple, we use the loop operation:

isZero(x) {
    y = true
    loop x { y = false }
    return y
}

If the loop condition is true (i.e. 1) then the loop executes exactly once. If the loop condition is false (i.e. 0) then the loop doesn't execute. We can use this to write branching code.

So, how do we define the relational operations? Turns out, everything can be defined in terms of lte:

lte(x, y) {
    z = sub(x, y)
    z = isZero(z)
    return z
}

We know that x ≥ y is the same as y ≤ x. Therefore:

gte(x, y) {
    z = lte(y, x)
    return z
}

We know that if x > y is true then x ≤ y is false. Therefore:

gt(x, y) {
    z = lte(x, y)
    z = not(z)
    return z
}

We know that x < y is the same as y > x. Therefore:

lt(x, y) {
    z = gt(y, x)
    return z
}

We know that if x ≤ y and y ≤ x then x = y. Therefore:

eq(x, y) {
    l = lte(x, y)
    r = lte(y, x)
    z = and(l, r)
    return z
}

Finally, we know that if x = y is true then x ≠ y is false. Therefore:

ne(x, y) {
    z = eq(x, y)
    z = not(z)
    return z
}

Now, all we need to do is define the following functions:

  1. The sub function is defined as follows:

    sub(x, y) {
        loop y
            { x = decr(x) }
        return x
    }
    
    decr(x) {
        y = 0
        z = 0
    
        loop x {
            y = z
            z = incr(z)
        }
    
        return y
    }
    
  2. The not function is the same as the isZero function:

    not(x) {
        y = isZero(x)
        return y
    }
    
  3. The and function is the same as the mul function:

    and(x, y) {
        z = mul(x, y)
        return z
    }
    
    mul(x, y) {
        z = 0
        loop x { z = add(y, z) }
        return z
    }
    
    add(x, y) {
        loop x
            { y = incr(y) }
        return y
    }
    

That's all you need. Hope that helps.

like image 75
Aadit M Shah Avatar answered Nov 18 '22 06:11

Aadit M Shah