In the lecture we presented natural numbers as Peano numbers. However, this representation is very inefficient for large numbers. We therefore want to present natural numbers as binary numbers.

* We distinguish the number $0$, represented as `o`, and positive numbers $>0$ represented as `pos(N)`.

* Positive numbers are represented as a bit sequence, where the most significant bit is always $1$, so there are no leading zeros. Positive numbers consist of the following functors.

* We distinguish the number 0, represented as `o`, and positive numbers >0 represented as `pos(N)`.

* Positive numbers are represented as a bit sequence, where the most significant bit is always 1, so there are no leading zeros. Positive numbers consist of the following functors.

* A term of the form `o(N)` represents the number $2\cdot N$,

* a term of the form `i(N)` represents the number $2\cdot N+1$ and

* the atom `i` represents the number $1$ (most significant bit).

* A term of the form `o(N)` represents the number 2*N,

* a term of the form `i(N)` represents the number 2*N+1 and

* the atom `i` represents the number 1 (most significant bit).

The number $0$ is therefore called `o`, $1$ as `pos(i)` and $4$ as `pos(o(o(i))`. Hence, the outermost functor represents the least significant bit.

The number 0 is therefore called `o`, 1 as `pos(i)` and 4 as `pos(o(o(i))`. Hence, the outermost functor represents the least significant bit.

1. Define the following predicates based on this number representation.

...

...

@@ -50,27 +50,27 @@

**Note:** Use auxiliary predicates such as `addP`, which are defined on positive numbers. Furthermore, use the following equations and develop equations for further combinations if necessary.

$$\begin{align*}

1+1 &=2\\

1+(2\cdot N) &=2\cdot N+1\\

1+(2\cdot N+1) &=2\cdot (N+1)

\end{align*}$$

~~~

1+1 =2

1+(2\cdot N) =2\cdot N+1

1+(2\cdot N+1) =2\cdot (N+1)

~~~

2. When used in the opposite direction, e.g. with a query like `add(X, Y, pos(o(i)))`, Prolog computes all solutions, but, unfortunately, does not terminate. The reason is, that the Prolog program does not know that the sum of two positive numbers is greater than each summand, and thus increasing summands are tried.

Implement for positive numbers $>0$ a predicate `lessP(X, Y)` that is satisfied, when `X` is less than `Y`. Then use this predicate to improve the previous implementation of `add`.

Implement for positive numbers >0 a predicate `lessP(X, Y)` that is satisfied, when `X` is less than `Y`. Then use this predicate to improve the previous implementation of `add`.

1.*Boolean Operations*

Implement Boolean functions `and`, `or`, and `not` as predicates working on the values `true` and `false`. Make sure that negation is also possible. Therefore, you must provide an explicit result as an additional parameter of your predicate.

Also implement the following three Boolean expressions as Prolog predicates `ex1(X, Y, Z, Res)`, `ex2(X, Y, Z, Res)`, and `ex3(X, Y, Z, Res)`.