\chapterquote{%
The two operations of our understanding, intuition and deduction, on which alone we have said we must rely in the acquisition of knowledge.}
{-- Rene Descartes, \textit{Key Philosophical Writings} (1997)}

The purpose of this chapter is to characterize the distribution of the interference in a typical packet transmitted through an asynchronous channel, where fixed-length packets arrive according to a Poisson process.
%
Although simple and theoretical, because we do not consider spatial positions or fading effects, we deemed it important to build intuition on the structure of our main problem of characterizing the spatio-temporal interference on Poisson networks.

\newpage

\section{The Interference Model}

Let $\tau$ be the transmission time of the packets that use the channel. Let the homogeneous Poisson point process $\Pi$ on $\R$ with density $\lambda > 0$ be the times for which a transmission starts.

We are interested in the stochastic process $\{I_0(t)\}_{t\in[0,\tau]}$, which corresponds to the number of simultaneous interfering packets on a typical packet that started its transmission at $t=0$. Then, the interference is defined as a shot-noise field given by
\begin{align} \label{eq:interf_I0}
    I_0(t) \triangleq \sum_{x\in\Pi^!_0} \ind_{[0,\tau)}(t-x), \quad t\in[0,\tau),
\end{align}
where $\Pi^!_0$ is the \textit{reduced Palm version} of $\Pi$, i.e., $\Pi$ conditioned to having a point at the origin and excluding it.
%
Slivnyak's theorem (Theorem~\ref{th:slivnyak}) guarantees that the distribution of $\Pi^!_0(\cdot)$ is the same as $\Pi(\cdot)$ for a Poisson point process.

In view of the general network model of Chapter~\ref{cap:P2_00}, we provide two of many possible scenarios for which this model can be applied.

\begin{example}
    Consider a stationary bipolar high-mobility Poisson network with density of transmitters $\lambda_{\S}>0$ on the plane $\S=\R^2$, and transmission time $\tau_i = \tau$ and transmission power $P_i \equiv 1$ for every user $i\in\N$.

    Suppose packets arrive at each transmitter according to a Poisson point process of density $a>0$ on time $\T = \R$ and there is no buffer in the transmitters, thus packets are transmitted upon arrival and discarded afterward (independently of successful transmission), i.e., $A_{i} = T_{i} = T^*_{i}$ for all users $i\in\N$.

    In a scenario without small-scale fading ($H_{ij} \equiv 1$ for all $i,j\in\N$) and with path loss function $\ell(r) = \ind\{r < r_0\}$, $r_0>0$,
    %
    the interference model presented in \eqref{eq:interf_I0} applies to this network with $\lambda = \pi r_0^2\,\lambda_\S\,a$.
\end{example}

\begin{example}
    Consider a stationary bipolar static network with $n\in\N$ transmitters and $n$ receivers on a small disk of radius $1$ ($\S=\{x\in\R^2: ||x|| < 1\}$) such that small-scale fading and path loss can be ignored, i.e., $H_{ij} \equiv 1$ for all $i,j\in\{1,2,\dots,n\}$ and $\ell \equiv 1$.
    %
    Further, the transmission time $\tau_i = \tau$ and transmission power $P_i \equiv 1$ for every user $i\in\{1,2,\dots,n\}$.

    Suppose that the medium access protocol is to transmit a packet after an iid exponentially distributed time of parameter $\lambda_p > 0$ after the last transmission, i.e., $T_i$ is a Poisson point process of density $\lambda_p$ on $\T = \R$ for each $i\in\{1,2,\dots,n\}$.

    Let us consider that each transmitter has a buffer and we have saturated traffic, i.e., all transmitter's queues are non-empty at all times, so $A_i$ does not matter and $T_i^* = T_i$, because there is always a packet to transmit.
    %
    Then, the interference model presented in \eqref{eq:interf_I0} applies to this network with $\lambda = n\lambda_p$.
\end{example}

The above examples do not specify the transmission probability model $\mathscr{S}$. This is not done because it depends on the adopted model for success probability and each of the following sections presents a different model.

Although we provided only two scenarios, it is not hard to see that we must have a stationary network with a single user class, which implies that the throughput is given by the product of the traffic with the transmission success probability, i.e., $\mathscr{T} = \upsilon\,p_s$.

Let us begin the characterization of $I_0$ through its auto-covariance function, which is given by, for $\Delta t\in(-\tau,\tau)$,
\begin{align*}
    \mathrm{K}_{I_0}(\Delta t)
        &\triangleq \E[I_0(t) I_0(t+\Delta t)] - \E[I_0(t)] \E[I_0(t+\Delta t)], \quad t\in[0,\tau)\cap[-\Delta t, \tau-\Delta t),\\
        &= \sum_{k_0\ge 0}\sum_{k_1\ge 0}\sum_{k_2\ge 0} (k_0+k_1)(k_0+k_2) \frac{\lambda^{k_0}(\tau-|\Delta t|)^{k_0}}{k_0!}\,\euler^{-\lambda(\tau-|\Delta t|)}\\
        &\hspace{30mm} \times\frac{\lambda^{k_1}|\Delta t|^{k_1}}{k_1!}\euler^{-\lambda|\Delta t|} \,\frac{\lambda^{k_2}|\Delta t|^{k_2}}{k_2!}\euler^{-\lambda|\Delta t|} - (\lambda\tau)^2\\
        &= \lambda(\tau - |\Delta t|),
\end{align*}
where we have used the identity $\displaystyle\sum_{k\ge0} \frac{x^k}{k!} = \euler^x$, for any $x\in\R^*$.

As expected of a Poisson point process, which is independent across space (or, in this case, time), the factor responsible for correlation in the stochastic process $I_0$ is related to the duration $\tau$ of each transmitted packet. That is why $\mathrm{K}_{I_0}(\tau) = 0$.

\begin{remark} \label{remark:Ch5_I0}
    To illustrate a (rarely mentioned) dichotomy, we have calculated \textit{(i)} the Laplace transform of $I_0$ on the interval $[0,\tau)$ and \textit{(ii)} the Laplace transform of the distribution of $I_0(t)$ at $t\in[0,\tau)$.
    %
    On the one hand, \textit{(i)} characterizes the stochastic process on time, but not in distribution.
    %
    On the other hand, \textit{(ii)} characterizes the distribution of the stochastic process at a point in time, but we cannot analyze the process as a whole, which is essential to decide whether a transmission is successful.
    %
    The calculations follow.

    \textit{(i)} Let $s\in\C\backslash\{0\}$. The Laplace transform of $I_0$ on the interval $[0,\tau)$ is given by
    \begin{align}
        \mathscr{L}\{I_0\}(s)
            &= \int_0^\tau I_0(t)\,\euler^{-s t}\,\d t \nonumber\\
            &= \sum_{x\in\Pi} \int_0^\tau \ind_{[0,\tau]}(t-x)\,\euler^{-s t}\,\d t\nonumber\\
            &= \sum_{x\in\Pi} \left(\euler^{-s \lfloor -x\rceil_0^\tau} - \euler^{-s \lfloor \tau-x \rceil_0^\tau} \right)\frac{\euler^{-s x}}{s}, \label{eq:Ch5_LT_I0}
    \end{align}
    where $\lfloor \cdot\rceil_a^b = \max\{\min\{\cdot,b\},a\}$.

    \textit{(ii)} Let $s\in\C$. The Laplace transform of the distribution of $I_0(t)$, $t\in[0,\tau)$ is
    \begin{align*}
        \mathscr{L}_{I_0(t)}(s) &= \E[\euler^{-s\,I_0(t)}]\\
            &= \E\!\left[-\sum_{x\in\Pi} s\ind_{[0,\tau)}(t-x) \right]\\
            &= \exp\!\left(-\int_\R \left(1 - \euler^{-s\ind_{[0,\tau)}(t-x)}\right)\lambda\,\d x \right)\\
            &= \exp\!\left(-\int_0^\tau \left(1 - \euler^{-s}\right)\lambda\,\d x \right)\\
            &= \euler^{-\lambda\tau(1-\euler^{-s})},
    \end{align*}
    % Then, using Campbell Theorem (Theorem~\ref{th:campbell}),
    % \begin{align*}
    %     \E\!\left[\frac{1}{\tau}\mathscr{L}\{I_0(t)\}(s)\right]
    %         &= \int_\R \left(\euler^{-s \lfloor -x\rceil_0^\tau} - \euler^{-s \lfloor \tau-x \rceil_0^\tau} \right)\frac{\euler^{-s x}}{\tau s} \lambda\,\d x \\
    %         &= \lambda\tau \frac{1-\euler^{-\tau s}}{\tau s}.
    % \end{align*}
    where we have used Campbell's theorem (Theorem~\ref{th:campbell}) in the third equality.

    To analyze a successful transmission we need the characterization of the interference both in distribution and in time. As shown, it is easy to do one of them, however, both at the same time (e.g. calculating the Laplace transform of the distribution of \eqref{eq:Ch5_LT_I0}) does not yield a tractable formula.
\end{remark}

As illustrated in Remark~\ref{remark:Ch5_I0}, it is hard to characterize the distribution of $I_0$ on the interval $[0,\tau)$ and in distribution concomitantly.
%
Thus, we shall define a meaningful functional (related to a success probability) that receives as argument $I_0$ and give us a real number, e.g. the mean value of $I_0$ on $[0,\tau)$.
%
Then, we characterize the distribution of this functional\footnote{In the general model of Chapter~\ref{cap:P2_00} the transmission success probability model $\mathscr{S}$ receives as argument the $\SIR$ instead of the interference $I_0$, however, this is easy to adapt, because the $\SIR$ includes the interference.}.

% % % % % % % % % % % % % % % % % % % % %
\section{Error Correcting Code model}

In the Error Correcting Code model (ECC model) we assume that a packet is successfully transmitted if the proportion of the packet that was affected by interference is smaller than or equal to $\beta\in[0,1)$. %, i.e., the transmission fails if the proportion of the transmission time subjected to interference is greater than $\beta$.
%
Thus, the transmission success probability is given by $p_s = \P\!\left(\overline{S_0} \le \beta\right)$, where the random variable $\overline{S_0}$ is the proportion of the packet that was affected by interference and is defined as
\begin{align}\label{eq:ECC_I}
    \overline{S_0} \triangleq \frac{1}{\tau}\int_0^\tau \ind\{I_0(t) > 0\}\,\d t.
\end{align}
%
\begin{figure}[htb]
    \centering
    \if\printfig1
        \includegraphics{Figures/Ch5_PacketsRandomChannel.pdf}
    \else
        \includegraphics[draft]{Figures/Ch5_PacketsRandomChannel.pdf}
    \fi
    \caption{An early and late interfering packets with respect to the typical packet. The hachured regions correspond to packet overlapping. If $E+L>\beta\tau$, then the packet transmission fails.}
    \label{fig:P2_diagram_ECC}
\end{figure}%

~\\

\begin{theorem} \label{th:F_S0}
    The cumulative distribution function of $\overline{S_0}$ is
    \begin{align*}
        F_{\overline{S_0}}(x) =
        \begin{cases}
            0, & \text{if } x < 0, \\
            (1 + \upsilon\,x)\,\euler^{-\upsilon\,(2-x)}, & \text{if } 0 \le x < 1,\\
            1, & \text{if } x \ge 1,
        \end{cases}
    \end{align*}
    where $\upsilon = \lambda\tau$.
\end{theorem}
%
\begin{proof}
    Since $\Pi$ is a Poisson point process on $\R$, the inter-arrival times follow iid exponential distributions of parameter $\lambda$. This can be shown through the void probabilities of the PPP, i.e., $\P(\Pi((0,t))=0) = \euler^{-\lambda t}$.

    Let the inter-arrival time of the early interferer and the late interferer be represented by the random variables $T_E\sim\mathscr{E}(\lambda)$ and $T_L\sim\mathscr{E}(\lambda)$, respectively.
    %
    Then, the superposition of the late interfering packet with the typical packet is given by $L = (\tau - T_L)_+$, where $(\cdot)_+ = \max\{\cdot,0\}$ as illustrated in Fig.~\ref{fig:P2_diagram_ECC}. Analogously for the early interferer $E = (\tau - T_E)_+$.
    %
    Thus, the cdf of $E$ and $L$ is
    \[
        F_E(t) = F_L(t) = % \ind\{t > \tau\} + \ind\{0\le t \le \tau\} \euler^{-\lambda(\tau-t)} =
        \begin{cases}
            0, &\text{if } t < 0 \\
            \euler^{-\lambda(\tau-t)}, &\text{if } 0 \le t < \tau,\\
            1, &\text{if } t \ge \tau.
        \end{cases}
    \]
    Note there is a discontinuity at $t=0$, thus the density does not exist with respect to the Lebesgue measure. Then, it is convenient to use the Lebesgue–-Stieltjes notation (Definition~\ref{def:lebesgue-stieltjes}).

    From \eqref{eq:ECC_I} we can see that $\overline{S_0}\,\tau = \max\{E+L,\tau\}$. Thus, for $x\in[0,1)$, we have that
    \begin{align*}
        \qquad
        \P(\overline{S_0} \le x)
            &= \P(\max\{E+L,\tau\} \le x\,\tau) \\
            &= \P(E+L \le x\,\tau) &&\hspace{-10mm}\text{as }\tau > x\,\tau\\
            &= \int F_E(x\,\tau - t)\,\d F_L(t) &&\hspace{-10mm}\text{as $E$ and $L$ are independent}\\
            &= \euler^{-\lambda(\tau-x\,\tau)} \euler^{-\lambda\tau} + \int_0^{x\,\tau} \euler^{-\lambda(\tau-x\,\tau+t)} \lambda\euler^{-\lambda(\tau-t)}\d t \\
            &= (1+\lambda\tau\,x)\,\euler^{-\lambda\tau(2-x)}.
    \end{align*}
    The cases for which $x\notin[0,1)$ are trivial.
\end{proof}

% Note that the random variable $\overline{S_0}$ does not admit a density with respect to the Lebesgue measure, because of the discontinuities of the cdf at $0$ and $1$, that is why we used the Lebesgue--Stieltjes notation.

\begin{proposition}
	In the ECC model, where $\beta\in[0,1)$, the transmission success probability and throughput is given, respectively, by
    \begin{align*} \label{AlohaECC_thr}
        p_s     &= (1 + \beta\,\upsilon)\,\euler^{-(2-\beta)\,\upsilon},\\
    	\mathscr{T} &= \upsilon\,(1 + \beta\,\upsilon)\,\euler^{-(2-\beta)\,\upsilon},
    \end{align*}
    where $\upsilon = \lambda\tau$ is the occupation rate of the channel.

    Further, the unique and global maximum throughput is achieved when $\upsilon = \upsilon^*$, where
    \begin{equation*} \label{AlohaECC_max}
	    \upsilon^* = \frac{\sqrt{(2-\beta)^2+4\beta^2}-(2-3\beta)}{2\beta\,(2-\beta)}.
    \end{equation*}
\end{proposition}
\begin{proof}
    The transmission success probability and throughput follow directly from Theorem~\ref{th:F_S0} along with $p_s = \P(\overline{S_0}\le\beta) = F_{\overline{S_0}}(\beta)$ and $\mathscr{T} = \lambda\tau\,p_s$.

    Then, we use Theorem~\ref{th:unique_opt} to verify that the throughput has a unique local maximum which is a global maximum.
\end{proof}

If we let $\beta = 0$ in the ECC model we recover the unslotted ALOHA model \cite{abramson1970aloha}.
%
On the other hand, to achieve the maximum throughput of the slotted ALOHA, which is $1/\euler$, we must have $\beta \approx 0.56$.

Figures \ref{fig:P2_ECC} and \ref{fig:P2_ECC_ps} show the throughput $\mathscr{T}$ as a function of the traffic $\upsilon$ and as a function of the transmission success probability $p_s$, respectively.
%
As expected, the throughput grows when the system is more robust (when $\beta$ increases), even if we consider a fixed transmission success probability.

\begin{figure}[htb]
    \centering
    \if\printfig1
        % \input{Plots/Ch5_ECC.tex}
        \includegraphics[]{Figures/Ch5_ECC.pdf}
    \else
        \includegraphics[draft, width=\textwidth]{Figures/Ch5_ECC.pdf}
    \fi
    \caption{Throughput $\mathscr{T}$ of the ECC model as a function of occupation rate of the channel $\upsilon$ for different values of $\beta$.}
    \label{fig:P2_ECC}
\end{figure}%
%
\begin{figure}[htb]
    \centering
    \if\printfig1
        % \input{Plots/Ch5_ECC_ps.tex}
        \includegraphics[]{Figures/Ch5_ECC_ps.pdf}
    \else
        \includegraphics[draft, width=\textwidth]{Figures/placeholder.png}
    \fi
    \caption{Parametric curve of the throughput $\mathscr{T}$ and the transmission success probability $p_s$ as we vary $\upsilon$ in the ECC model for different values of $\beta$.}
    \label{fig:P2_ECC_ps}
\end{figure}

% % % % % % % % % % % % % % % % % % % % %
\section{Average Interference model}
\label{sec:AI_model}

In the Average Interference model (AI model) we assume that a packet is successfully transmitted if the average of the received interference does not exceed the threshold $\beta \ge 0$.
%
Thus, the transmission success probability is given by $p_s = \P(\overline{I_0}\le\beta)$, where the random variable $\overline{I_0}$ is the mean interference on the typical packet and is defined as
\begin{align}\label{eq:AI_I}
    \overline{I_0} \triangleq \frac{1}{\tau}\int_0^\tau I_0(t)\,\d t.
\end{align}

\begin{theorem} \label{th:F_I0}
    The cumulative distribution function of $\overline{I_0}$ is given by the finite sum
    \begin{equation*}
    	F_{\overline{I_0}}(x) = \euler^{-2\upsilon} \sum_{k=0}^{\lfloor x \rfloor} \dfrac{(-1)^k}{k!} \left(\sqrt{2\,\upsilon\,(x-k)}\right)^k \cal{I}_k\!\left(\sqrt{8\,\upsilon\,(x-k)}\right),\quad x \geq 0,
    \end{equation*}
    where $\cal{I}_k$ is the $k$th order modified Bessel function of the first kind\footnote{Let $k\in\N$. The $k$th order modified Bessel function of the first kind is defined as \vspace{-2mm}\[\cal{I}_k(x) \triangleq \frac{1}{\pi}\int_0^\pi \euler^{x\cos\theta}\cos(k\theta)\,\d\theta,\quad x\in\R.\vspace{-3mm}\]}, $\lfloor \cdot \rfloor$ is the floor function\footnote{The floor function returns the biggest integer less than or equal to the argument.}, and $\upsilon = \lambda\tau$.
\end{theorem}
\begin{proof}
    From \eqref{eq:interf_I0} and \eqref{eq:AI_I}, we have that
    \begin{align*}\label{eq:AI_I}
        \overline{I_0}
            &= \frac{1}{\tau}\int_0^\tau \sum_{x\in\Pi} \ind_{[0,\tau]}(t-x)\,\d t \\
            &= \frac{1}{\tau}\sum_{x\in\Pi} \int_0^\tau  \ind_{[0,\tau]}(t-x)\,\d t \\
            &= \frac{1}{\tau}\sum_{x\in\Pi\cap[-\tau,\tau]} (\tau - |x|),
    \end{align*}
    where we used the Fubini--Tonelli theorem (Theorem~\ref{th:fubini-tonelli}) to interchange the sum with the integral, because we have positive terms.
    %
    Note that the term $(\tau - |x|)$ gives the superposition time between an interfering packet and the typical packet.

    Now, using the Campbell theorem (Theorem~\ref{th:campbell}) we have that the Laplace--Stieltjes transform of the distribution of $\overline{I_0}$ is
    \begin{align*}
    	\E\!\left[\euler^{-s \overline{I_0}}\right]	&= \exp\!\left[ -\int_{-\tau}^\tau(1-\euler^{-s(1-|x|/\tau)}) \,\lambda\,dx \right] \\
        				&= \exp\!\left[ -2\upsilon\left( 1 - \frac{1-\euler^{-s}}{s} \right) \right],\quad s\in\C \backslash \{0\},
    \end{align*}
    where we used that the traffic $\upsilon = \lambda\tau$.

    Using the power series expansion of the exponential function on $\exp(-2\upsilon\,\euler^{-s}/s)$ and dividing by $s$ both sides of the equation, we can rewrite it as
    \begin{equation*}
    	\frac{1}{s}\E\!\left[\euler^{-s \overline{I_0}}\right] = \euler^{-2\upsilon} \sum_{k\geq0} \frac{(-2\upsilon)^k}{k!} \frac{\euler^{2\upsilon/s}}{s^{k+1}}\,\euler^{-k s},\quad s\in\C \backslash \{0\}.
    \end{equation*}

    % Para $k$ suficientemente grande, os valores absolutos dos termos dessa série podem ser limitados por cima pela função $e^-s$. Logo, podemos aplicar o teorema da convergência dominada para calcular a transformada de Laplace–Stieltjes inversa termo a termo.
    %Aplicando a transformada inversa de Laplace–Stieltjes nos dois lados da equação e usando o Teorema da convergência dominada de Lebesgue \cite[Teorema~1.34]{rudin1987real} para trocar o limite da soma com a transformada inversa, obtemos a c.d.f. de $\overline{I_0}$,
    Then, we apply the inverse Laplace–-Stieltjes transform on both sides of the equation to obtain the cdf of $\overline{I_0}$, which concludes the proof.
\end{proof}

\begin{proposition}
In the AI model with $\beta\in[0,1]$, the transmission success probability and throughput is given, respectively, by
\begin{align*}
    p_s &= \cal{I}_0\!\left( \sqrt{8\,\beta\,\upsilon} \right) \euler^{-2\,\upsilon},\\
	\mathscr{T} &= \upsilon\,\cal{I}_0\!\left( \sqrt{8\,\beta\,\upsilon} \right) \euler^{-2\,\upsilon},
\end{align*}
where $\cal{I}_0$ is the zeroth order modified Bessel function of the first kind.

For $\beta \ge 0$, then $p_s = F_{\overline{I_0}}(\beta)$ and $\mathscr{T} = \upsilon\,p_s$, where $F_{\overline{I_0}}$ is given by Theorem~\ref{th:F_I0}.
\end{proposition}

\begin{proof}
We know that the packet success probability $p_s = \P(\overline{I_0}\leq \beta) = F_{\overline{I_0}}(\beta)$ and throughput $\mathscr{T} = \upsilon\,p_s$.
%
Now, note that if $\beta\in[0,1]$, then the sum in Theorem~\ref{th:F_I0} consists of a single term.
\end{proof}

Since $\cal{I}_0(0) = 1$, we recover the classical result of the unslotted ALOHA for $\beta = 0$ as expected.
%
On the other hand, to achieve the maximum throughput of the slotted ALOHA, we need to have $\beta\approx0.621$.

Analogously to the ECC model, Figures \ref{fig:P2_AI} and \ref{fig:P2_AI_ps} show the throughput $\mathscr{T}$ as a function of the traffic $\upsilon$ and as a function of the transmission success probability $p_s$, respectively.
%
Again, we observe the same behavior that the throughput grows when the system is more robust ($\beta$ increases).

Furthermore, for the same $\beta$ and $\upsilon$, we have that the transmission success probability of the ECC model is bigger than the one of the AI model. This happens because $\overline{S_0} \le \overline{I_0}$.

\begin{figure}[htb]
    \centering
    \if\printfig1
        % \input{Plots/Ch5_AI.tex}
        \includegraphics[]{Figures/Ch5_AI.pdf}
    \else
        \includegraphics[draft, width=\textwidth]{Figures/placeholder.png}
    \fi
    \caption{Throughput $\mathscr{T}$ of the AI model as a function of occupation rate of the channel $\upsilon$ for different values of $\beta$.}
    \label{fig:P2_AI}
\end{figure}%
%
\begin{figure}[htb]
    \centering
    \if\printfig1
        % \input{Plots/Ch5_AI_ps.tex}
        \includegraphics[]{Figures/Ch5_AI_ps.pdf}
    \else
        \includegraphics[draft, width=\textwidth]{Figures/placeholder.png}
    \fi
    \caption{Parametric curve of the throughput $\mathscr{T}$ and the transmission success probability $p_s$ as we vary $\upsilon$ in the AI model for different values of $\beta$.}
    \label{fig:P2_AI_ps}
\end{figure}

% % % % % % % % % % % % % % % % % % % % %
\section{High Interference model}
\label{sec:HI_model}

In the High Interference model (HI model) we assume that a packet is successfully transmitted if the proportion of the packet that was affected by \textit{high interference} is smaller than or equal to $\beta\in[0,1]$. We define the period of \textit{high interference} as the times for which the number of interfering packets is greater than a threshold $h\in\N$.

Thus, the transmission success probability is given by $p_s = \P\!\left(\overline{S_h} \le \beta\right)$, where $\overline{S_h}$ is the proportion of time the typical packet was affected by \textit{high interference} and is defined as
\begin{align}\label{eq:Sh_def}
    \overline{S_h} \triangleq \frac{1}{\tau}\int_0^\tau \ind\{I_0(t) > h\}\,\d t.
\end{align}

Considering every $h\in\N$, the distribution of the random variable $\overline{S_h}$ would provide a thorough characterization of the stochastic process $\{I(t)\}_t$,
%
because it would capture every interference level and the distribution of its duration in the typical packet.
%
However, as we shall see, the distribution of $\overline{S_h}$ is quite intricate.

\begin{remark} \label{remark:Ch5_ineq}
    The HI model is a generalization of the ECC model because we recover the latter by making $h=0$ in the former.
    %
    Also, one can show that the HI model is related to the AI model through the following identity
    \begin{align*}
        \sum_{h\ge 0} \overline{S_h} = \overline{I_0}.
    \end{align*}
    Furthermore, $\overline{S_0} \ge \overline{S_1} \ge \cdots$.
\end{remark}

Let the number of interferers that start transmitting during the transmission of the typical packet be represented by the random variable $LI$ (late interferers) and the number of interferers that stop transmitting during the transmission of the typical packet be represented by the random variable $EI$ (early interferers), i.e.,
\begin{align*}
    EI \triangleq I_0(0) \sim \mathscr{P}(\lambda\tau), \qquad LI \triangleq I_0(\tau) \sim \mathscr{P}(\lambda\tau).
\end{align*}
Then, since $\Pi$ is a Poisson point process, it is easy to see that $EI$ and $LI$ are independent and follow a Poisson distribution of parameter $\upsilon = \lambda\tau$.

Let $(t_k)_{k=1}^{EI+LI}$ be the sequence of times for which an interferer stops or starts a transmission.
%
Then, it is possible to uniquely determine $\{I(t)\}_t$ from the sequences $(t_1,\dots,t_{EI+LI})$ and $(I(0),I(t_1),\dots,I(t_{EI+LI}))$.
%
Furthermore, the proportion of time the typical packet was affected by \textit{high interference} can be written as
\begin{align} \label{eq:ch5_Sh}
    \overline{S_h}
        &= \sum_{k=0}^{EI+LI} \dfrac{\Delta t_k}{\tau}\,\ind{\{I(t_k) > h\}},
\end{align}
where $\Delta t_k \triangleq t_{k+1} - t_{k}$ and we use $t_0 = 0$, $t_{EI+LI+1} = \tau$.

Given $EI = k$ and $LI = l$, the distribution of the $k+l$ times of starting or stopping a transmission are independent and uniformly distributed on $[0,\tau)$.
\begin{note}
    We can check the above-mentioned property by showing the random variable that represents the number of points of the PPP that are in a subset $T$ of $[0,\tau)$ follow a binomial distribution of parameters $(k+l,\mu(T))$, where $\mu(T)$ is the length of the subset.

    To prove this property in a more general form, it is enough to show that for every $A,B\in\cal{B}(\R)$ such that $A\subset B$ the random variable $\Pi(A)|\Pi(B)$ follows a binomial distribution of parameters $(\Pi(B), \mu(A)/\mu(B))$ , where $\mu$ is the Lebesgue measure.
\end{note}

Further, the random vector $(I(t_i))_{i=0}^{k+l}$ is a symmetric Bernoulli random walk\footnote{A symmetric Bernoulli random walk is a random walk on $\Z$ that performs unitary steps and each step has equal probability of being $+1$ or $-1$.} subject to begin at $k$ and end at $l$ with $k+l$ steps.
%
% In this random walk, every path is equiprobable.
The total number of different paths is given by the binomial coefficient $\binom{k+l}{k}$.
%
We are interested to know how many of these paths stay a total number of $m\in\N$ steps above the threshold $h$. Let us denote the number of paths that satisfy this as $C_{k,l}^{h,m}\in\N$.
%
Formally, we can write that
\begin{align} \label{eq:Ch5_Cklhm}
    C_{k,l}^{h,m} \triangleq \sum_{\bm{x}\in\mathscr{X}_{k,l}}\ind
        \!\left\{ \sum_{i=0}^{k+l} \ind\{x_i > h\} = m\right\},
\end{align}
where $\mathscr{X}_{k,l} \subset \N^{k+l+1}$ is the set that contains all paths from $k$ to $l$ with $k+l$ unit steps.

Now we can express the probability of having exactly $m$ times of the random vector $(t_i)_{i=0}^{k+l}$ for which the typical packet is affected by an interference greater than $h$ as $C_{k,l}^{h,m}/\binom{k+l}{k}$.

Fig.~\ref{fig:random_walk} illustrate some random walks that starts at $k=5$ and end at $l=3$ in $k+l=8$ steps. We show $3$ of the $\binom{8}{3} = 56$ possible paths.
%
Note that for this case, the only path that surpass $h=7$ (\textit{high interference}) is the red path and it stays above $h$ for only $m=1$ step.
%
Indeed, $C_{5,3}^{7,m} = \ind\{m=1\}$, thus there is only one path that does that.
\begin{figure}[htb]
\centering
    \if\printfig1
        \includegraphics[width=0.45\textwidth]{Figures/Ch5_RandomWalk.pdf}
    \else
        \includegraphics[draft, width=0.5\textwidth]{Figures/Ch5_RandomWalk.pdf}
    \fi
	\caption{Examples of random walks starting at $k=5$ and finishing at $l = 3$.}
	\label{fig:random_walk}
\end{figure}
%
It is worth remembering that in the original problem the $i$th step of the random walk stays in the corresponding state a time given by the random variable $\Delta t_i$.

Different from the other models, the HI model is much more complex, because the calculation of $C_{k,l}^{h,m}$ is done case by case despite having a (strictly speaking) \textit{closed form} expression.
%
As we shall see, we need $C_{k,l}^{r,m}$ for an infinite number of argument combinations and, then, we stumble upon a difficult problem.

\begin{remark} \label{rem:Ch5_Crmkl}
    On the other hand, we can find simple formulas for some special argument values of $C_{k,l}^{r,m}$ as follows.
    \begin{itemize}
        \item $C_{k,l}^{r,m} = C_{l,k}^{r,m}$ by symmetry;

        \item If $k\leq r$ and $l\leq r$, then $C_{k,l}^{r,0} = \binom{k+l}{k} - \binom{k+l}{r+1}$ by the reflection method \cite{feller1968introduction};

        \item If $k>r$ and $l>r$, then $C_{k,l}^{r,k+l+1} = \binom{k+l}{k} - \binom{k+l}{r}$ by the reflection method again;

        \item If $k\leq r$ and $k+l-(r-k) \leq m \leq k+l$, then $C_{k,l}^{r,m}=0$,
    \end{itemize}
    where we consider the binomial coefficient $\binom{n}{k} = 0$ when $k \notin \{0,1,\dots,n\}$.
\end{remark}

Assuming that we have $m$ intervals of time with \textit{high interference}, then we need to find the probability that the sum of $m$ intervals of \textit{high interference} is smaller or equal than $\beta\tau$, which is the condition for successful transmission.
%
Indeed, we have that
\[
    \P(\Delta t_0 + \Delta t_1 + \cdots + \Delta t_{m-1} \leq \beta\tau) = \cal{P}_{k+l,m}(\beta),
\]
where
\begin{align} \label{eq:Ch5_Pdef}
    \cal{P}_{n,m}(x) \triangleq
    \begin{cases}
        \ind\{m = 0\}, &\text{if } x = 0,\\
        \displaystyle\sum_{i=m}^{n} \binom{n}{i}x^i(1-x)^{n-i}, &\text{if } x\in(0,1),\\
        1, &\text{if } x = 1,
    \end{cases}
\end{align}
because we have $k+l$ independent random variables uniformly distributed on $[0,\tau)$ and we want that at least $m$ of them are in the interval $[0,\beta\tau]$.
%
From order statistics theory we can generalize this result to
\begin{align} \label{eq:ch5_Pdeltat}
	\P(\Delta t_{\nu(0)} + \Delta t_{\nu(1)} + \cdots + \Delta t_{\nu(m-1)} \leq \beta\tau) = \cal{P}_{k+l,m}(\beta),
\end{align}
where $\nu:\{0,1,\cdots,m-1\} \longrightarrow \{0,1,\cdots,k+l\}$ is an arbitrary injective function.
%
This takes into account the cases where the intervals of \textit{high interference} are not contiguous.

Finally, we can conclude from \eqref{eq:ch5_Sh} and \eqref{eq:ch5_Pdeltat} that
\begin{align} \label{eq:Ch5_Sh_EIEL}
    \P(\overline{S_h} \leq x~|~EI=k,LI=l)
	    &= \sum_{m=0}^{k+l+1} \dfrac{C_{k,l}^{r,m}}{\binom{k+l}{k}} \cal{P}_{k+l,m}(x), \quad x\in[0,1].
\end{align}

Now, we are prepared to prove the following theorem.
\begin{theorem} \label{th:F_Sh}
    The cumulative distribution function of $\overline{S_h}$, $h\in\N$, is
    \begin{align*}
        F_{\overline{S_h}}(x)
            &= \euler^{-2\upsilon} \sum_{n \geq 0} \frac{\upsilon^n}{n!} \sum_{m=0}^{n+1}  \cal{P}_{n,m}(x) \sum_{k=0}^n C^{h,m}_{k,n-k}, \quad x\in[0,1],
    \end{align*}
    where $\upsilon = \lambda\tau$, $C$ is defined in \eqref{eq:Ch5_Cklhm} and $\cal{P}$ is defined in \eqref{eq:Ch5_Pdef}.

    Further, we can express the probabilities at the discontinuities of $F_{\overline{S_h}}$ in \textit{closed form}:
    \begin{align*}
        \P(\overline{S_h}=0)
            &= \left( f_{h}(\upsilon) \right)^2 + (\upsilon-h) \frac{\upsilon^{h+1}\euler^{-\upsilon}}{(h+1)!} f_{h}(\upsilon)  - \dfrac{\upsilon^{2(h+1)}\euler^{-2 \upsilon}}{h!(h+1)!},\\
        \P(\overline{S_h}=1)
            &= \left( 1 - f_{h}(\upsilon) \right)^2 + (h+1-\upsilon) \left( 1 - f_{h-1}(\upsilon) \right) \frac{\upsilon^h \euler^{-\upsilon}}{h!} -(h+1)\frac{\upsilon^{2h}\euler^{-2\upsilon}}{(h!)^2},
    \end{align*}
    where $f_h(\upsilon)\triangleq \euler^{-\upsilon}\displaystyle\sum_{k=0}^{h}\frac{\upsilon^h}{h!}$.
\end{theorem}
\begin{proof}
    We know that the random variables $EI$ and $LI$ follow a Poisson distribution of parameter $\upsilon=\lambda\tau$. Thus, we can decondition \eqref{eq:Ch5_Sh_EIEL} on $EI$ and $LI$ to obtain, for $x\in[0,1]$,
    \begin{align}
    	\P(\overline{S_h} \leq x)
    	    &= \euler^{-2\upsilon}\sum_{k \geq 0}\sum_{l \geq 0} \frac{\upsilon^{k+l}}{k!l!}\sum_{m=0}^{k+l+1} \dfrac{C_{k,l}^{r,m}}{\binom{k+l}{k}} \cal{P}_{k+l,m}(x) \label{eq:Ch5_F_Sh_aux1}\\
    		&= \euler^{-2\upsilon}\sum_{k \geq 0}\sum_{l \geq 0} \frac{\upsilon^{k+l}}{(k+l)!}\sum_{m=0}^{k+l+1} C_{k,l}^{r,m} \cal{P}_{k+l,m}(x) \label{eq:Ch5_F_Sh_aux2}\\
        	&= \euler^{-2\upsilon}\sum_{n \geq 0} \sum_{m=0}^{n+1}  \sum_{k=0}^n C^{r,m}_{k,n-k} \cal{P}_{n,m}(x)  \frac{\upsilon^n}{n!},
    \end{align}
    where we performed the variable change $l=n-k$ and we can interchange the sums because all terms are positive (Fubini–-Tonelli theorem, Theorem~\ref{th:fubini-tonelli}).
    %
    This proves the first result because $F_{\overline{S_h}}(x) = \P(\overline{S_h} \le x).$

    Now, using \eqref{eq:Ch5_F_Sh_aux2} and Remark~\ref{rem:Ch5_Crmkl} we have that
    \begin{align*}
    \P(\overline{S_h} = 0)
    	 &= \euler^{-2\upsilon}\sum_{k \geq 0}\sum_{l \geq 0} \frac{\upsilon^{k+l}}{(k+l)!}\sum_{m=0}^{k+l} C_{k,l}^{h,m} \cal{P}_{k+l,m}(0)\\
         &= \euler^{-2\upsilon}\sum_{k \geq 0}\sum_{l \geq 0} \frac{\upsilon^{k+l}}{(k+l)!} C_{k,l}^{h,0}\\
         &= \euler^{-2\upsilon}\sum_{k \geq 0}\sum_{l \geq 0} \frac{\upsilon^{k+l}}{(k+l)!} \left( \binom{k+l}{k} - \binom{k+l}{h+1} \ind_{\{k+l > h\}} \right)\ind_{\{k \leq h\}}\ind_{\{l \leq h\}}\\
         &= \euler^{-2\upsilon}\sum_{k = 0}^h \sum_{l = 0}^h \frac{\upsilon^{k+l}}{k!l!} \left( 1 - \frac{\binom{k+l}{h+1}}{\binom{k+l}{k}} \right).
    \end{align*}
    %
    Now, using $f_h$ and some tedious manipulations we find the \textit{closed form} of $\P(\overline{S_h} = 0)$.

    Using \eqref{eq:Ch5_F_Sh_aux1} and Remark~\ref{rem:Ch5_Crmkl} we have that
    \begin{align*}
	\P(\overline{S_h}=1)
	    &= F_{\overline{S_h}}(1) - \lim_{x\uparrow 1} F_{\overline{S_h}}(x)\\
    	&= \euler^{-2\upsilon}\sum_{k > 0}\sum_{l > 0} \frac{\upsilon^{k+l}}{k!l!} \dfrac{C_{k,l}^{h,k+l+1}}{\binom{k+l}{k}}\\
    	&= \euler^{-2\upsilon} \sum_{k > 0}\sum_{l > 0} \frac{\upsilon^{k+l}}{k!l!} \left( 1 - \frac{\binom{k+l}{h}}{\binom{k+l}{k}} \right)\ind_{\{k>h\}}\ind_{\{l>h\}}\\
    	&= \euler^{-2\upsilon} \sum_{k > h}\sum_{l > h} \frac{\upsilon^{k+l}}{k!l!} \left( 1 - \frac{\binom{k+l}{h}}{\binom{k+l}{k}} \right).
    \end{align*}
    %
    Again, using $f_h$ and some tedious manipulations we find the \textit{closed form} of $\P(\overline{S_h} = 1)$.
\end{proof}

Using Theorem~\ref{th:F_Sh} we can find simple expressions for the probability of not receiving \textit{high interference} at all during a typical packet transmission, i.e., $\P(\overline{S_h} = 0)$, for $h=0,1,2,\dots$.
%
\begin{align*}
    \P(\overline{S_0} = 0) &= \euler^{-2\,\upsilon},\\
    \P(\overline{S_1} = 0) &= \left( 1+2\upsilon+{\upsilon}^{2}/2 \right){\euler^{-2\,\upsilon}} ,\\
    \P(\overline{S_2} = 0) &= \left( 1+2\,\upsilon+2\,\upsilon^2+(2/3)\,\upsilon^3+(1/12)\,\upsilon^4 \right){{\euler}^{-2\,\upsilon}},\\
                &~\,\vdots\\
    \P(\overline{S_h} = 0) &= \frac{\upsilon^{2h}\euler^{-2\upsilon}}{h!(h+1)!} + \cal{O}(\upsilon^{2h-1}\euler^{-2\upsilon}), \quad \text{as } \upsilon\to\infty.
\end{align*}

The same can be done for the case of receiving \textit{high interference} during all transmission, i.e., $\P(\overline{S_h} = 1)$.
%
\begin{align*}
    \P(\overline{S_0} = 1) &= 1 - (1+\upsilon)\,\euler^{-\upsilon},\\
    \P(\overline{S_1} = 1) &= (1-\euler^{-\upsilon})^2 - \upsilon^2\,\euler^{-\upsilon},\\
    \P(\overline{S_2} = 1) &= (1-(1+\upsilon)\,\euler^{-\upsilon})^2 + (1-\upsilon-\euler^{-\upsilon})\,\frac{\upsilon^2}{2}\,\euler^{-\upsilon},\\
        &~\,\vdots\\
    \P(\overline{S_h} = 1) &= 1 - \frac{\upsilon^{h+1}}{h!}\,\euler^{-\upsilon} + \cal{O}\left(\upsilon^h\euler^{-\upsilon}\right), \quad \text{as } \upsilon\to\infty.
\end{align*}

\begin{figure}[htb]
    \centering
    \if\printfig1
        \begin{subfigure}{.45\textwidth}
          \centering
            % \input{Plots/Ch5_HI_0.tex}
            \includegraphics[width=\columnwidth]{Figures/Ch5_HI_0.pdf}
            % \caption{$\beta = 0$.}
        \label{fig:HI_0}
        \end{subfigure}%
        \begin{subfigure}{.05\textwidth}
        \hspace{.05\textwidth}
        \end{subfigure}%
        \begin{subfigure}{.45\textwidth}
          \centering
            % \input{Plots/Ch5_HI_1.tex}
            \includegraphics[width=\columnwidth]{Figures/Ch5_HI_1.pdf}
            % \caption{$\beta \uparrow 1$.}
        \label{fig:HI_1}
        \end{subfigure}
    \else
        \includegraphics[draft, width=\textwidth]{Figures/placeholder.png}
    \fi
    \caption{Transmission success probability $p_s$ as a function of traffic $\upsilon$ in the HI model. The blue and red curves represent the slotted and unslotted ALOHA, respectively.} \label{fig:HI}
\end{figure}

As shown in the plots of Fig.~\ref{fig:HI}, the transmission success probability $p_s$ increases with $h$, because more concurrent interfering transmissions are necessary to cause \textit{high interference} in the typical packet transmission.

When we fix the threshold $h$ and let the traffic $\upsilon\to\infty$, we have that ${\P(\overline{S_h}=0)=0}$ and $\P(\overline{S_h}=1)=1$.
%
On the other hand, when we fix $\upsilon$ and let $h\to\infty$, then ${\P(\overline{S_h}=0)=1}$ and $\P(\overline{S_h}=1)=0$.
%
Thus, an interesting scenario to analyse is what happens when we increase the traffic $\upsilon$ along with the threshold $h$, i.e., let $\upsilon\to\infty$ and $h\to\infty$ such that $\frac{h}{\upsilon} \to 1$.
%
In this case we can show that
\begin{align*}
   \lim_{\substack{h,\upsilon\to\infty\\ h/\upsilon\to1}} \P(\overline{S_h} = 0) &=  \lim_{\substack{h,\upsilon\to\infty\\ h/\upsilon\to1}} \P(\overline{S_h} = 1) = \frac{1}{4}\left( 1 - \frac{2}{\pi}\right) \approx 0.091.
\end{align*}

\begin{note}
    To find the above result it is necessary to calculate an interesting limit problem, which cannot be solved through standard techniques, indeed the Software \textit{Mathematica} (version 12) does not solve it. In a simpler form, the problem is to show that
    \begin{align*}
        \lim_{n\to\infty} f_n(n) = \lim_{n\to\infty} \frac{\Gamma(n,n)}{\Gamma(n)} = \frac{1}{2},
    \end{align*}
    where $\Gamma(\cdot,\cdot)$ is the incomplete gamma function and is defined as $\Gamma(s,x)\triangleq \int_{x}^\infty t^{s-1} \euler^{-t}\,\d t$, and $\Gamma(\cdot) \triangleq \Gamma(\cdot,0)$ is the gamma function.

    To prove this identity we use the central limit theorem!

    Let $\{X_k\}_k$ be iid exponentially distributed random variables with parameter $1$. Let the sum $S_n = \sum_{k=1}^n X_k$, then $S_n$ follows an Erlang distribution of parameters $(n,1)$. Then, $\P(S_n > n) = \frac{\Gamma(n,n)}{\Gamma(n)}$. However, $\P(S_n > n) = \P(\frac{S_n-n}{\sqrt{n}} > 0) \xrightarrow{n\to\infty} 1/2$ by the central limit theorem.
\end{note}

To conclude this part, let us state the following inequalities, for $\beta > 0$, $\upsilon > 0$, $h\ge 0$,
\begin{align*}
    p_s^{\mathrm{(ALOHA)}} &\le p_s^{\mathrm{(ECC)}} \le p_s^{\mathrm{(AI)}} \le p_s^{\mathrm{(HI)}},\\
    \mathscr{T}^{\mathrm{(ALOHA)}} &\le \mathscr{T}^{\mathrm{(ECC)}} \le \mathscr{T}^{\mathrm{(AI)}} \le \mathscr{T}^{\mathrm{(HI)}},
\end{align*}
which are easily obtained from Remark~\ref{remark:Ch5_ineq} and the definitions of throughput and transmission success probability.

% % % % % % % % % % % % % %
\section{Summary} \label{sec:summ_P2_01}

In this chapter, we characterized the distribution of the interference in a typical packet, where interferers transmit according to a Poisson process.
%
The characterization was performed through the analysis of some transmission success models (ECC, AI, HI), for which we obtained the throughput and the transmission success probability in closed form.
%
We also compared the obtained distributions with those of the slotted/unslotted ALOHA.


% BEGIN LICENSE BLOCK
% Version: CMPL 1.1
%
% The contents of this file are subject to the Cisco-style Mozilla Public
% License Version 1.1 (the "License"); you may not use this file except
% in compliance with the License.  You may obtain a copy of the License
% at www.eclipse-clp.org/license.
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
% the License for the specific language governing rights and limitations
% under the License.
%
% The Original Code is  The ECLiPSe Constraint Logic Programming System.
% The Initial Developer of the Original Code is  Cisco Systems, Inc.
% Portions created by the Initial Developer are
% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
%
% Contributor(s):
%
% END LICENSE BLOCK

\chapter{Propia and CHR}
\label{chappropiachr}
%HEVEA\cutdef[1]{section}

\section{Two Ways of Specifying Constraint Behaviours}
There are two elegant and simple ways of building constraints
available in \eclipse{}, called {\em Propia} and {\em Constraint
Handling Rules} (or {\em CHR}'s).
They are themselves built using the facilities
described in chapter \ref{chapimpl}.

\index{noclash}
Consider a simple {\em noclash} constraint requiring that two
activities cannot be in progress at the same time.
For the sake of the example, the constraint involves two variables,
the start times $S1$ and $S2$
of the two activities, which both have duration $5$.
Logically this constraint states that
{\em noclash} $ \Leftrightarrow (S1 >= S2 + 5 \vee S2 >= S1 + 5)$.
The same logic can be expressed as two \eclipse{} clauses:
\begin{code}
noclash(S1,S2) :-
    ic:(S1 \$>= S2+5).
noclash(S1,S2) :-
    ic:(S2 \$>= S1+5).
\end{code}
Constraint propagation elicits information from constraints without
leaving any choice points.  Constraint propagation behaviour can be
associated with each of the above representations, by CHR's
and by Propia.

One way to propagate information from {\em noclash} is to wait until
the domains of the start times are reduced sufficiently that only one
ordering of the tasks is possible, and then to enforce the constraint
that the second task not start until the first is finished.

\index{constraints/1}
This behaviour can be implemented in CHR's as follows:
\begin{code}
:- constraints noclash/2.
noclash(S1,S2) <=> ic:(S2 #< S1+5) | ic:(S1 #>= S2+5).
noclash(S1,S2) <=> ic:(S1 #< S2+5) | ic:(S2 #>= S1+5).
\end{code}

Consider the query:
\begin{quote}
\begin{verbatim}
?- ic:([S1,S2]::1..10),
   noclash(S1,S2),
   S1 #>= 6.
\end{verbatim}
\end{quote}
In this query {\em noclash} achieves no propagation when it is
initially posted with the start time domains set to \verb91..109.
However, after imposing $S1>=6$,
the domain of $S1$ is reduced to \verb96..109.
Immediately the {\em noclash}
constraint wakes, detects that the first
condition $S1+5 >= S2$ is entailed,
and narrows the domain of $S2$ to \verb91..59.

The same behaviour can be expressed in Propia, but this time the
original \eclipse{} representation of {\em noclash} as two clauses is
used directly.  The propagation behaviour is automatically
extracted from the two clauses by Propia when the {\em noclash} goal
is annotated as follows:
\begin{quote}
\begin{verbatim}
?-      [S1,S2]::1..10,
        noclash(S1,S2) infers most,
        S1 #>= 6.
\end{verbatim}
\end{quote}
%For readability \verb0infers0 is declared to be an infix operator,
%enabling the annotation to be written thus:
%\begin{quote}
%\begin{verbatim}
%?- [S1,S2]::1..10, noclash(S1,S2) infers most, S1 #>= 6
%\end{verbatim}
%\end{quote}

\quickref{Building Constraints without Tears}{
Propia and CHRs make it easy to turn the logical statement of a
constraint into code that efficiently enforces that constraint.
}

\section{The Role of Propia and CHR in Problem Modelling}

\index{modelling}
To formulate and solve a problem in \eclipse{} the standard pattern is
as follows:
\begin{enumerate}
\item Initialise the problem variables
\item State the constraints
\item Specify the search behaviour
\end{enumerate}
Very often, however, the constraints involve logical implications or
disjunctions, as in the case of the {\em noclash} constraint above.
Such constraints are most naturally formulated in a way that would
introduce choice points during the constraint posting phase.
The two \eclipse{} clauses defining {\em noclash}, above, are a case in
point.

There are two major disadvantages of introducing choice points during
constraint posting:
\begin{itemize}
\item Posting and reposting constraints during search is an
unnecessary and computationally expensive overhead
\item Mixing constraint behaviour and search behaviour makes it harder
to explore and optimize the algorithm executed by the program.
\end{itemize}
Propia and CHR's support the separation of constraint setup and search
behaviour, by allowing constraints to be formulated naturally without
their execution setting up any choice points.

The effect on performance is illustrated by the following small
example.
The aim is to choose a set of $9$ products (\verb0Products0,
identified by their product number 101-109) to
manufacture, with a
limited quantity of raw materials (\verb0Raw10 and \verb0Raw20),
so as to achieve a profit (\verb0Profit0) of over
$40$.
The amount of raw materials (of two kinds) needed to produce
each product is listed in a table, together with its profit.

\index{product\_plan}
\index{product}
\begin{code}
product_plan(Products) :-
    length(Products,9),
    Raw1 #=< 95,
    Raw2 #=< 95,
    Profit #>= 40,
    sum(Products,Raw1,Raw2,Profit),
    labeling(Products).

product( 101,1,19,1).  product( 102,2,17,2).  product( 103,3,15,3).
product( 104,4,13,4).  product( 105,10,8,5).  product( 106,16,4,4).
product( 107,17,3,3).  product( 108,18,2,2).  product( 109,19,1,1).

sum(Products,Raw1,Raw2,Profit) :-
    ( foreach(Item,Products),
      foreach(R1,R1List),
      foreach(R2,R2List),
      foreach(P,PList)
    do
        product(Item,R1,R2,P)
    ),
    Raw1 #= sum(R1List),
    Raw2 #= sum(R2List),
    Profit #= sum(PList).

\end{code}

The drawback of this program is that the \verb0sum0 constraint calls
\verb0product0 which chooses an item and leaves a choice point at each
call.
Thus the setup of the \verb0sum0 constraint leaves $9$ choice points.
Try running it, and the program
fails to terminate within a reasonable amount of time.

Now to make the program run efficiently, we can simply annotate the call
to \verb0product0 as a Propia constraint making:
\verb0product(Item,R1,R2,P) infers most0.
This program leaves no choice points during constraint setup, and
finds a solution in a fraction of a second.

In the remainder of this chapter we show how to use Propia and CHR's,
give some
examples, and outline their implementation.

\quickref{Modelling without Choice Points}{
Propia and CHRs can be used to build clear problem models that have no
(hidden) choice points.
}

\section{Propia}
\label{secpropia}

\index{propia}
\index{generalised propagation}
Propia is an implementation of {\em Generalised Propagation}
which is described in the paper  \cite{LeProvost93b}.

\subsection{How to Use Propia}
\index{infers/2}
In principle Propia propagates information from an annotated goal by
finding all solutions to the goal and extracting any information that
is common to all the different solutions.
(In practice, as we shall see later, Propia does not typically need to
find all the solutions.)

The ``common'' information that can be extracted depends upon what
constraint solvers are used when evaluating the underlying
un-annotated \eclipse{} goal.  To illustrate this, consider another
simple example.

\begin{quote}
\begin{verbatim}
p(1,3).
p(1,4).

?-  p(X,Y) infers most.
\end{verbatim}
\end{quote}
If the {\tt ic} library is not loaded when this query is
invoked, then the information propagated by Propia is that $X=1$.
If, on the other hand, {\tt ic} is loaded, then more common
information is propagated.  Not only does Propia propagate $X=1$ but
also the domain of $Y$ is tightened from \verb0-inf..inf0 to
\verb03..40. (In this case the additional common information is that
$Y \neq  0$, $Y \neq 1$, $Y \neq 2$ and so on for all values except $3$
and $4$!)

\index{most}
\index{consistent}
\index{unique}
\index{ac}
Any goal \verb0Goal0 in an \eclipse{} program, can be transformed into a
constraint by annotating it thus: \verb0Goal infers Parameter0.
Different behaviours can be specified with different parameters, viz:
\begin{itemize}
\item \verb0Goal infers most0\\
Propagates all common information produced by the loaded solvers
\item \verb0Goal infers unique0\\
Fails if there is no solution, propagates the solution if it is
unique, and succeeds without propagating further information if there
is more than one solution.
\item \verb0Goal infers consistent0\\
Fails if there is no solution, and propagates no information otherwise
\end{itemize}

%\item \verb0Goal infers ic0\\
%Tightens the domains of the variables.
%\item \verb0Goal infers ac0\\
%Tightens the domains of the variables: more efficient than \verb0ic0,
%but can only handle goals with a finite set of ground answers.

\index{crossword}
These behaviours are nicely illustrated by the crossword demonstration
program \verb0crossword0 in the examples code directory.
There are 72 ways to complete the crossword grid with words from the
accompanying directory.
For finding all 72 solutions,
the comparative performance of the different annotations is given in the
table {\em Comparing Annotations}.

\begin{table}[tb]
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
Annotation & CPU time (secs)\\
\hline
consistent & 13.3 \\
unique & 2.5 \\
most & 9.8 \\
ac & 0.3 \\
\hline
\end{tabular}
\end{center}
\caption{Comparing Annotations}
\end{table}

The example program also illustrates the effect of specifying the waking
conditions for Propia.  By only waking a Propia constraint when it
becomes instantiated, the time to solve the crossword problem can be
changed considerably.  For example by changing the annotation from
\verb0Goal infers most0 to
\verb0suspend(Goal,4,Goal->inst) infers most0
the time needed to find all solutions goes down from 10 seconds to
just one second.

For other problems, such as the square tiling problem in the example
directory, the fastest version is the
one using \verb0infers consistent0.  To find the best Propia
annotation it is necessary to experiment with the current problem
using realistic data sets.

\quickref{Transforming Procedures to Constraints}{
Propia extracts information from a procedure which may be defined by
multiple \eclipse{} clauses.
The information to be extracted is
controlled by the Propia annotation.
% (e.g. \verb0consistent0, \verb0unique0, \verb0most0 and \verb0ac0).
}

\subsection{Propia Implementation}
In this section we describe how Propia works.

\subsubsection{Outline}
When a goal is annotated as a Propia constraint, eg.
\verb0p(X,Y) infers most0, first the goal \verb0p(X,Y)0 is in effect
evaluated in the normal way by \eclipse{}.
However Propia does not stop at the first solution, but continues to
find more and more solutions, each time combining the information from
the solutions retrieved.
When all the information has been accumulated, Propia propagates this
information (either by narrowing the domains of variables in the goal,
or partially instantiating them).

Propia then suspends the goal again, until the variables become
further constrained, at which point it wakes, extracts information
from solutions to the more constrained goal, propagates it, and
suspends again.

If Propia detects that the goal is entailed (i.e. the goal would
succeed whichever way the variables were instantiated), then after
propagation it does not suspend any more.

\subsubsection{Most Specific Generalisation}
\index{most specific generalisation}
\index{MSG}
Propia works by treating its input both as a {\em goal} to be called,
and as a term which can be manipulated as data.
As with any \eclipse{} goal, when executed its result is a further
instantiation of the term.
For example the first result of calling \verb0member(X,[a,b,c])0 is
to further instantiate the term yielding \verb0member(a,[a,b,c])0.
This instantiated term represents the (first) solution to the goal.

Propia combines information from the solutions to a goal using their
{\em most specific generalisation} ({\em MSG}).
The MSG of two terms is a term
that can be instantiated (in different ways) to either of the two
terms. For example
$p(a,f(Y))$ is the MSG of $p(a,f(b))$ and $p(a,f(c))$.
This is the meaning of {\em generalisation}.
The meaning of {\em most specific} is that any other term that
generalises the two terms, is more general than the MSG.
For example, any other term that generalises $p(a,f(b)$ and
$p(a,f(c))$ can be instantiated to $p(a,f(Y))$.
The MSG of two terms captures only information that is common to both
terms (because it generalises the two terms), and it captures all the
information possible in the two terms (because it is the most specific
generalisation).

Some surprising information is caught by the MSG.  For example the MSG
of $p(0,0)$ and $p(1,1)$ is $p(X,X)$.
We can illustrate this being exploited by Propia in the following
example:
\begin{code}
% Definition of logical conjunction
conj(1,1,1).
conj(1,0,0).
conj(0,1,0).
conj(0,0,0).

conjtest(X,Z) :-
    conj(X,Y,Z) infers most,
    X=Y.
\end{code}
The test succeeds, recognising that $X$ must take the same truth value
as $Z$.  Running this in \eclipse{} yields:
\begin{quote}
\begin{verbatim}
[eclipse]: conjtest(X,Z).
X = X
Z = X
Delayed goals:
        conj(X, X, X) infers most
Yes (0.00s cpu)
\end{verbatim}
\end{quote}

If the {\tt ic} library is loaded more information can be extracted,
because the MSG of $0$ and $1$ is a variable with domain \verb90..19.
Thus the result of the above example is not only to equate $X$ and $Z$
but to associate with them the domain \verb90..19.

The MSG of two terms depends upon what information is expressible in
the MSG term.  As the above example shows, if the term can employ
variable domains the MSG is more precise.

By choosing the class of terms in which the MSG can be
expressed, we can capture more or less information in the MSG.
If, for example, we allow only terms of maximum depth 1 in the class,
then MSG can only capture functor and arity.
In this case the MSG of $f(a,1)$ and $f(a,2)$ is simply $f(_,_)$, even
though there is more shared information at the next depth.

In fact the class of terms can be extended to a lattice, by
introducing a bottom $\bot$ and a top $\top$.
$\bot$ is a term carrying no
information; $\top$ is a term representing inconsistent information;
the
meet of two terms is the result of unifying them; and their join is
their MSG.

\subsubsection{The Propia Algorithm}
We can now specify the Propia algorithm more precisely.
The Propia constraint is
\begin{verbatim}Goal infers Parameter \end{verbatim}

\begin{itemize}
\item  Set $OutTerm := \top$
\item  Repeat
\begin{itemize}
\item   Find a solution $S$ to $Goal$ which is {\em not} an instance of
$OutTerm$
\item   Find the MSG, in the class specified by \verb0Parameter0,
of $OutTerm$ and $S$.  Call it $MSG$
\item   Set $OutTerm := MSG$
\end{itemize}
until either $Goal$ is an instance of $OutTerm$, or no such
solution remains
\item Return $OutTerm$
\end{itemize}

When \verb0infers most0 is being handled, the class of terms admitted
for the MSG is the biggest class expressible in terms of the currently
loaded solvers.  In case $ic$ is loaded, this includes variable
domain, but otherwise it includes any \eclipse{} term without variable
attributes.

The algorithm supports \verb0infers consistent0 by admitting only the
two terms $\top$ and $\bot$ in the MSG class.
\verb0infers unique0 is a variation of the algorithm in which the
first step $OutTerm := \top$ is changed to finding a first solution
$S$ to $Goal$ and initialising $OutTerm := S$.

Propia's termination is dramatically improved by the check that the
next solution found is not an
instance of $OutTerm$.  In the absence of domains, there is no
infinite sequence of terms that strictly generalise each other.
Moreover, if
the variables in $Goal$ have finite domains, the same result holds.
Thus, because of this check, Propia will terminate as long as each
call of $Goal$ terminates.

For example the Propia constraint
\verb0member(Var,List) infers Parameter0 will
always terminate, if each call of \verb0member(Var,List)0 does, even in
case
\verb0member(Var,List)0 has infinitely many solutions!

\quickref{Most Specific Generalisation}{
Propia computes the Most Specific Generalisation (MSG) of the set of
solutions to a procedure.  It does so without, necessarily,
backtracking through all the solutions to the procedure.
The MSG depends upon the annotation of the Propia call.
}

\subsection{Propia and Related Techniques}
If the finite domain solver is loaded then \verb0Goal infers most0 prunes
the variable domains so every value is supported by values in the
domains of the other variables.  If every problem constraint was
annotated this way, then Propia would enforce arc consistency.

\index{arc consistency}
Propia generalises traditional arc consistency in two ways.  Firstly
it admits n-ary constraints, and secondly it handles predicates
defined by rules, as well as ground facts.  In the special case that
the goal can be ``unfolded'' into a finite set of ground solutions,
this can be exploited by using \verb0infers ac0 to make Propia run
more efficiently.   When called with parameter \verb0infers ac0,
Propia simply finds all solutions and
applies n-ary arc-consistency to the resulting tables.

%associates an identifier with each tuple.  Arc-consistency is then
%applied to the binary constraints between the tuple identifiers and
%tuple values for each attribute, using the \verb0element0 constraint
%in the {\tt ic} library.

\index{constructive disjunction}
Propia also generalises {\em constructive disjunction}.  Constructive
disjunction could be applied in case the
predicate was unfolded into a finite set of solutions, where each
solution was expressed using {\tt ic} constraints (such as equations,
inequations etc.).
Propia can also handle recursively defined predicates, like
\verb0member0, exampled above, which may have an infinite number of
solutions.

\section{CHR}
\label{secchr}
\index{CHR}
Constraint Handling Rules were originally implemented in \eclipse{}.
They are introduced in the paper \cite{Fruehwirth}.

\subsection{How to Use CHR}
\index{simplification rule}
\index{propagation rule}
CHR's offer a rule-based programming style to express constraint
simplification and constraint propagation.
The rules all have a {\em head}, an explicit or implicit {\em guard},
and a {\em body}, and are written either
\begin{quote}
\begin{verbatim}
Head <=> Guard | Body.  %Simplification Rule
\end{verbatim}
\end{quote}
or
\begin{quote}
\begin{verbatim}
Head ==> Guard | Body.   %Propagation Rule
\end{verbatim}
\end{quote}
When a constraint is posted that is an instance of the head, the guard
is checked to determine whether the rule can fire.
If the guard is satisfied (i.e. CHR detects that it is entailed by the
current search state), the
rule {\em fires}.
Unlike \eclipse{} clauses, the rules leave no choice points.
Thus if several rules share the same head and one fires, the other
rules are never fired even after a failure.

Normally the guards exclude each other, as in the \verb0noclash0
example:
\begin{code}
:- lib(ech).
:- constraints noclash/2.
noclash(S1,S2) <=> ic:(S2 #< S1+5) | ic:(S1 #>= S2+5).
noclash(S1,S2) <=> ic:(S1 #< S2+5) | ic:(S2 #>= S1+5).
\end{code}
Henceforth we will not explicitly load the {\tt ech} library.

The power of guards lies in the behaviour of the rules when they are
neither entailed, nor disentailed.
Thus in the query
\begin{quote}
\begin{verbatim}
?- ic:([S1,S2]::1..10),
   noclash(S1,S2),
   S1 #>= 6.
\end{verbatim}
\end{quote}
\begin{sloppypar}
when the \verb0noclash0 constraint is initially posted, neither guard
is entailed, and CHR simply postpones the handling of the constraint
until further constraints are posted.
As soon as a guard becomes entailed, however, the rule fires.
For simplification rules, of the form
\verb0Head <=> Guard | Body0,
the head is replaced by the body.
In this example, therefore, \verb0noclash(S1,S2)0 is replaced by
\verb0S1 #>= S2+50.
\end{sloppypar}

Propagation rules are useful to add constraints, instead of replacing
them.
Consider, for example, an application to temporal reasoning.
If the time $T1$ is before time $T2$, then we can propagate an
additional $ic$ constraint saying $T1 =< T2$:
\begin{code}
:- constraints before/2.
before(T1,T2) ==> ic:(T1 \$=< T2)
\end{code}
This rule simply posts the constraint \verb0T1 $=< T20 to $ic$.
When a propagation rule fires its body is invoked, but its head
remains in the constraint store.

\subsection{Multiple Heads}

Sometimes different constraints interact, and more can be deduced
from the combination of constraints than can be deduced from the
constraints separately.  Consider the following query:
\begin{quote}
\begin{verbatim}
?- ic:([S1,S2]::1..10),
   noclash(S1,S2),
   before(S1,S2).
\end{verbatim}
\end{quote}
Unfortunately the {\tt ic} bounds are not tight enough for the
\verb0noclash0 rule to fire.
The two constraints can be combined so as to propagate $S2 \ge S1+5$
using a two-headed
CHR:
\begin{quote}
\begin{verbatim}
noclash(S1,S2), before(S1,S2) ==> ic:(S2 #>= S1+5).
\end{verbatim}
\end{quote}
We would prefer to write a set of rules that captured this kind of
inference in a general way.

\index{simpagation rule}
This can be achieved by writing a more complete solver for
\verb0prec0, and combining it with \verb0noclash0.
$prec(S1,D,S2)$ holds if the time $S1$ precedes the time $S2$ by at
least $D$ units of time.
For the following code to work, $S1$ and $S2$ may be numbers or
variables, but $D$ must be a number.
\begin{code}
:- constraints prec/3.
prec(S,D,S) <=> D=<0.
prec(S1,0,S2), prec(S2,0,S1) <=> S1=S2.
prec(S1,D1,S2), prec(S2,D2,S3) ==> D3 is D1+D2, prec(S1,D3,S3).
prec(S1,D1,S2) \verb.\. prec(S1,D2,S2) <=> D2=<D1 | true.     % Simpagation

noclash(S1,S2), prec(S1,D,S2) ==> D > -5 | prec(S1,5,S2).
noclash(S1,S2), prec(S2,D,S1) ==> D > -5 | prec(S2,5,S1).
\end{code}
Note the {\em simpagation} rule, whose head has two parts
\verb0Head1 \ Head20.
In a simpagation rule \verb0Head20 is replaced, but \verb0Head10 is
kept in the constraint store.

\quickref{CHRs}{
CHRs are guarded rules which fire without leaving choice points.
A CHR rule may have one or many goals in the head, and may take the
following forms: Simplification rule, Propagation rule or Simpagation
rule.
%\begin{itemize}
%\item[{\em Simplification} rule] \verb0Head <=> Guard | Body0
%\item[{\em Propagation} rule] \verb0Head ==> Guard | Body0
%\item[{\em Simpagation} rule] \verb0Head1 \ Head2 <=> Guard | Body0
%\end{itemize}

}

\section{A Complete Example of a CHR File}

Sometimes whole sets of constraints can be combined.
Consider, for example, a program where disequalities on pairs of
variables are accumulated during search.
Whenever a point is reached where any subset of the variables are all
constrained to be different an \verb0alldifferent0 constraint can be
posted on that subset, thus supporting more powerful propagation.
This can be achieved by finding {\em cliques} in the graph whose nodes
are variables and edges are disequality constraints.

\index{clique}
We start our code with a declaration to load the {\em ech} library.
The constraints are then declared, and subsequently defined by rules.
The CHR encoding starts by generating a clique whenever two variables
are constrained to be different.
\begin{code}
:- lib(ech).
:- constraints neq/2.

neq(X,Y) ==>
    sort([X,Y],List),
    clique(List),
    neq(Y,X).
\end{code}
Each clique is held as a sorted list to avoid any duplication.
The symmetrical disequality is added to simplify the detection of new
cliques, below.
Whenever a clique is found, the \verb0alldifferent0 constraint is
posted, and the CHRs seek to extend this clique to
include another variable:
\begin{code}
:- constraints clique/1.

clique(List) ==> alldifferent(List).
clique(List),neq(X,Y) ==>
    in_clique(Y,List), not in_clique(X,List) |
    sort([X|List],Clique),
    extend_clique(X,List,Clique).

in_clique(Var,List) :-
    member(El,List), El==Var, !.
\end{code}
The idea is to search the constraint store for a disequality between
the new variable \verb0X0 and each other variable in the original
clique.  This is done by recursing down the list of remaining
variables.
When there are no more variables left, a new clique has been found.
\begin{code}
neq(X,Y) \verb.\. extend_clique(X,[Y|Tail],Clique) <=>
    extend_clique(X,Tail,Clique).
extend_clique(_,[],Clique) <=>
    clique(Clique).
\end{code}

Finally, we add three optimisations.
Don't try and find a clique that has already been found, or
find the same clique twice.  If the new variable is equal to a
variable in the list, then don't try any further.
\begin{code}
clique(Clique) \verb.\. extend_clique(_,_,Clique) <=> true.
extend_clique(_,_,Clique) \verb.\. extend_clique(_,_,Clique) <=> true.
extend_clique(Var,List,_) <=> in_clique(Var,List) | true.
\end{code}

\subsection{CHR Implementation}
CHR's are implemented using the \eclipse{} suspension and waking
mechanisms.
A rule is woken if:
\begin{itemize}
\item a new goal is posted, which matches one of the goals in its head
\item a goal which has already been posted earlier becomes further
instantiated.
\end{itemize}

\index{entailment}
The rule cannot fire unless the goal is more instantiated than the
rule head.  Thus the rule
\verb0p(a,f(Y),Y) <=> q(Y)0 is really a shorthand for the guarded
rule:
\begin{quote}
\begin{verbatim}
p(A,B,C) <=> A=a, B=f(Y), C=Y | q(Y)
\end{verbatim}
\end{quote}
The guard is ``satisfied'' if, logically, it is entailed by the
constraints posted already.

In practice the CHR implementation cannot always detect the
entailment.
The consequence is that goals may fire later than they could.
For example consider the program
\begin{code}
:- constraints p/2.
p(X,Y) <=> ic:(X \$> Y) | q(X,Y).
\end{code}
and the goal
\begin{quote}
\begin{verbatim}
?-  ic:(X $> Y),
    p(X,Y).
\end{verbatim}
\end{quote}
Although the guard is clearly satisfied, the CHR implementation cannot
detect this and \verb0p(X,Y)0 does not fire.
If the programmer needs the entailment of inequalities to be detected,
it is necessary to express inequalities as CHR constraints, which
propagate {\tt ic} constraints as illustrated in the example
\verb0prec(S1,D,S2)0 above.

CHRs can detect entailment via variable bounds, so \verb.p(X,0).
does fire in the following example:
\begin{quote}
\begin{verbatim}
?-  ic:(X $> 1),
    p(X,0).
\end{verbatim}
\end{quote}

The implementation of this entailment test in \eclipse{} is to impose
the guard as a constraint, and fail (the entailment test) as soon as
any variable becomes more constrained.
A variable becomes more constrained if:
\begin{itemize}
\item it becomes more instantiated
\item its domain is tightened
\item a new goal is added to its suspension list
\end{itemize}

There are many examples of applications expressed in CHR in the
\eclipse{} distribution.
They are held as files in the {\em chr} subdirectory of the standard
\eclipse{} library directory {\em lib}.

\quickref{CHR Implementation}{
CHRs suspend on the variables in the rule head.  On waking the CHR
tests if its guard is entailed by the current constraint store.  The
entailment test is efficient but incomplete, and therefore rules may
fail to fire as early as they could in theory.
}

\section{Global Reasoning}
\index{global reasoning}
Constraints in {\tt ic} are handled separately and individually.  More
global consistency techniques can be achieved using global
constraints.
Propia and CHRs provide alternative methods of achieving more global
consistency.
Propia allows any subproblem to be treated as a single constraint.
CHRs allow any set of constraints to be handled by a single rule.
Each technique has special strengths.  Propia is good for handling
complicated logical combinations of constraints.  CHRs are good for
combining sets of constraints to extract transitive closures, and
cliques.

Both are fun to implement and use!

\section{Propia and CHR Exercise}

The problem is to implement three constraints, \verb'and', \verb'or'
and \verb'xor'
in CHRs and, as a separate exercise, in Propia.
The constraints are specified as follows:
All boolean variables have domain $\{0,1\}$: $0$ for 'false' and $1$
for 'true'.
\begin{quotation}
\noindent and(X,Y,Z) =def (X \& Y) = Z\\
or(X,Y,Z)  =def (X or Y) = Z\\
xor(X,Y,Z) =def ((X \& -Y) or (-X \& Y)) = Z
\end{quotation}

Suppose your constraints are called
\verb0cons_and0, \verb0cons_or0 and \verb0cons_xor0
Now write enter the following procedure:
\begin{code}
full_adder(I1,I2,I3,O1,O2) :-
    cons_xor(I1,I2,X1),
    cons_and(I1,I2,Y1),
    cons_xor(X1,I3,O1),
    cons_and(I3,X1,Y2),
    cons_or(Y1,Y2,O2).
\end{code}
The problem is solved if you enter the query:
\begin{quote}
\begin{verbatim}
?- full_adder(I1,I2,0,O1,1).
\end{verbatim}
\end{quote}
and get the correct answer.

Note: you are not allowed to load the ic library nor to use search and
backtracking!

%HEVEA\cutend


% Copyright 2018 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\section{Transformations}

\pgfname\ has a powerful transformation mechanism that is similar to the
transformation capabilities of \textsc{metafont}. The present section explains
how you can access it in \tikzname.

\subsection{The Different Coordinate Systems}

It is a long process from  a coordinate like, say, $(1,2)$ or
$(1\mathrm{cm},5\mathrm{pt})$, to the position a point is finally placed on the
display or paper. In order to find out where the point should go, it is
constantly ``transformed'', which means that it is mostly shifted around and
possibly rotated, slanted, scaled, and otherwise mutilated.

In detail, (at least) the following transformations are applied to a coordinate
like $(1,2)$ before a point on the screen is chosen:
%
\begin{enumerate}
    \item \pgfname\ interprets a coordinate like $(1,2)$  in its
        $xy$-coordinate system as ``add the current $x$-vector once and the
        current $y$-vector twice to obtain the new point''.
    \item \pgfname\ applies its coordinate transformation matrix to the
        resulting coordinate. This yields the final position of the point
        inside the picture.
    \item The backend driver (like |dvips| or |pdftex|) adds transformation
        commands such that the coordinate is shifted to the correct position in
        \TeX's page coordinate system.
    \item \textsc{pdf} (or PostScript) apply the canvas transformation matrix
        to the point, which can once more change the position on the page.
    \item The viewer application or the printer applies the device
        transformation matrix to transform the coordinate to its final pixel
        coordinate on the screen or paper.
\end{enumerate}

In reality, the process is even more involved, but the above should give the
idea: A point is constantly transformed by changes of the coordinate system.

In \tikzname, you only have access to the first two coordinate systems: The
$xy$-coordinate system and the coordinate transformation matrix (these will be
explained later). \pgfname\ also allows you to change the canvas transformation
matrix, but you have to use commands of the core layer directly to do so and
you ``better know what you are doing'' when you do this. The moment you start
modifying the canvas matrix, \pgfname\ immediately loses track of all
coordinates and shapes, anchors, and bounding box computations will no longer
work.

\subsection{The XY- and XYZ-Coordinate Systems}
\label{section-xyz}

The first and easiest coordinate systems are \pgfname's $xy$- and
$xyz$-coordinate systems. The idea is very simple: Whenever you specify a
coordinate like |(2,3)| this means $2v_x + 3v_y$, where $v_x$ is the current
\emph{$x$-vector} and $v_y$ is the current \emph{$y$-vector}. Similarly, the
coordinate |(1,2,3)| means $v_x + 2v_y + 3v_z$.

Unlike other packages, \pgfname\ does not insist that $v_x$ actually has a
$y$-component of $0$, that is, that it is a horizontal vector. Instead, the
$x$-vector can point anywhere you want. Naturally, \emph{normally} you will
want the $x$-vector to point horizontally.

One undesirable effect of this flexibility is that it is not possible to
provide mixed coordinates as in $(1,2\mathrm{pt})$. Life is hard.

To change the $x$-, $y$-, and $z$-vectors, you can use the following options:

\begin{key}{/tikz/x=\meta{value} (initially 1cm)}
    If \meta{value} is a dimension, the $x$-vector of \pgfname's
    $xyz$-coordinate system is set up to point \meta{value} to the right, that
    is, to $(\meta{value},0pt)$.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw                  (0,0)   -- +(1,0);
  \draw[x=2cm,color=red] (0,0.1) -- +(1,0);
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\tikz \draw[x=1.5cm] (0,0) grid (2,2);
\end{codeexample}

    The last example shows that the size of steppings in grids, just like all
    other dimensions, are not affected by the $x$-vector. After all, the
    $x$-vector is only used to determine the coordinate of the upper right
    corner of the grid.

    If \meta{value} is a coordinate, the $x$-vector of \pgfname's
    $xyz$-coordinate system is set to the specified coordinate. If \meta{value}
    contains a comma, it must be put in braces.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw                            (0,0) -- (1,0);
  \draw[x={(2cm,0.5cm)},color=red] (0,0) -- (1,0);
\end{tikzpicture}
\end{codeexample}

    You can use this, for example, to exchange the meaning of the $x$- and
    $y$-coordinate.
    %
\begin{codeexample}[]
\begin{tikzpicture}[smooth]
  \draw plot coordinates{(1,0) (2,0.5) (3,0) (3,1)};
  \draw[x={(0cm,1cm)},y={(1cm,0cm)},color=red]
        plot coordinates{(1,0) (2,0.5) (3,0) (3,1)};
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/y=\meta{value} (initially 1cm)}
    Works like the |x=| option, only if \meta{value} is a dimension, the
    resulting vector points to $(0,\meta{value})$.
\end{key}

\begin{key}{/tikz/z=\meta{value} (initially \normalfont$-3.85$mm)}
    Works like the |y=| option, but now a dimension is the point
    $(\meta{value},\meta{value})$.
    %
\begin{codeexample}[]
\begin{tikzpicture}[z=-1cm,->,thick]
  \draw[color=red] (0,0,0) -- (1,0,0);
  \draw[color=blue] (0,0,0) -- (0,1,0);
  \draw[color=orange] (0,0,0) -- (0,0,1);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\subsection{Coordinate Transformations}

\pgfname\ and \tikzname\ allow you to specify \emph{coordinate
transformations}. Whenever you specify a coordinate as in |(1,0)| or
|(1cm,1pt)| or |(30:2cm)|, this coordinate is first ``reduced'' to a position
of the form ``$x$ points to the right and $y$ points upwards''. For example,
|(1in,5pt)| is reduced to ``$72\frac{72}{100}$ points to the right and 5 points
upwards'' and |(90:100pt)| means ``0pt to the right and 100 points upwards''.

The next step is to apply the current \emph{coordinate transformation matrix}
to the coordinate. For example, the coordinate transformation matrix might
currently be set such that it adds a certain constant to the $x$ value. Also,
it might be set up such that it, say, exchanges the $x$ and $y$ value. In
general, any ``standard'' transformation like translation, rotation, slanting,
or scaling or any combination thereof is possible. (Internally, \pgfname\ keeps
track of a coordinate transformation matrix very much like the concatenation
matrix used by \textsc{pdf} or PostScript.)
%
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw (0,0) rectangle (1,0.5);
  \begin{scope}[xshift=1cm]
    \draw             [red]    (0,0) rectangle (1,0.5);
    \draw[yshift=1cm] [blue]   (0,0) rectangle (1,0.5);
    \draw[rotate=30]  [orange] (0,0) rectangle (1,0.5);
  \end{scope}
\end{tikzpicture}
\end{codeexample}

The most important aspect of the coordinate transformation matrix is \emph{that
it applies to coordinates only!} In particular, the coordinate transformation
has no effect on things like the line width or the dash pattern or the shading
angle. In certain cases, it is not immediately clear whether the coordinate
transformation matrix \emph{should} apply to a certain dimension. For example,
should the coordinate transformation matrix apply to grids? (It does.) And what
about the size of arced corners? (It does not.) The general rule is: ``If there
is no `coordinate' involved, even `indirectly', the matrix is not applied.''.
However, sometimes, you simply have to try or look it up in the documentation
whether the matrix will be applied.

Setting the matrix cannot be done directly. Rather, all you can do is to
``add'' another transformation to the current matrix. However, all
transformations are local to the current \TeX-group. All transformations are
added using graphic options, which are described below.

Transformations apply immediately when they are encountered ``in the middle of
a path'' and they apply only to the coordinates on the path following the
transformation option.
%
\begin{codeexample}[]
\tikz \draw (0,0) rectangle (1,0.5) [xshift=2cm] (0,0) rectangle (1,0.5);
\end{codeexample}

A final word of warning: You should refrain from using ``aggressive''
transformations like a scaling of a factor of 10\,000. The reason is that all
transformations are done using \TeX, which has a fairly low accuracy.
Furthermore, in certain situations it is necessary that \tikzname\
\emph{inverts} the current transformation matrix and this will fail if the
transformation matrix is badly conditioned or even singular (if you do not know
what singular matrices are, you are blessed).

\begin{key}{/tikz/shift={\ttfamily\char`\{}\meta{coordinate}{\ttfamily\char`\}}}
    Adds the \meta{coordinate} to all coordinates.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                       (0,0) -- (1,1) -- (1,0);
  \draw[shift={(1,1)},blue]   (0,0) -- (1,1) -- (1,0);
  \draw[shift={(30:1cm)},red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/shift only}
    This option does not take any parameter. Its effect is to cancel all
    current transformations except for the shifting. This means that the origin
    will remain where it is, but any rotation around the origin or scaling
    relative to the origin or skewing will no longer have an effect.

    This option is useful in situations where a complicated transformation is
    used to ``get to a position'', but you then wish to draw something
    ``normal'' at this position.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                                      (0,0) -- (1,1) -- (1,0);
  \draw[rotate=30,xshift=2cm,blue]           (0,0) -- (1,1) -- (1,0);
  \draw[rotate=30,xshift=2cm,shift only,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/xshift=\meta{dimension}}
    Adds \meta{dimension} to the $x$ value of all coordinates.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                   (0,0) -- (1,1) -- (1,0);
  \draw[xshift=2cm,blue]  (0,0) -- (1,1) -- (1,0);
  \draw[xshift=-10pt,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/yshift=\meta{dimension}}
    Adds \meta{dimension} to the $y$ value of all coordinates.
\end{key}

\begin{key}{/tikz/scale=\meta{factor}}
    Multiplies all coordinates by the given \meta{factor}. The \meta{factor}
    should not be excessively large in absolute terms or very close to zero.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw               (0,0) -- (1,1) -- (1,0);
  \draw[scale=2,blue] (0,0) -- (1,1) -- (1,0);
  \draw[scale=-1,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/scale around={\ttfamily\char`\{}\meta{factor}|:|\meta{coordinate}{\ttfamily\char`\}}}
    Scales the coordinate system by \meta{factor}, with the ``origin of
    scaling'' centered on \meta{coordinate} rather than the origin.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                             (0,0) -- (1,1) -- (1,0);
  \draw[scale=2,blue]               (0,0) -- (1,1) -- (1,0);
  \draw[scale around={2:(1,1)},red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/xscale=\meta{factor}}
    Multiplies only the $x$-value of all coordinates by the given
    \meta{factor}.
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                (0,0) -- (1,1) -- (1,0);
  \draw[xscale=2,blue] (0,0) -- (1,1) -- (1,0);
  \draw[xscale=-1,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/yscale=\meta{factor}}
    Multiplies only the $y$-value of all coordinates by \meta{factor}.
\end{key}

\begin{key}{/tikz/xslant=\meta{factor}}
    Slants the coordinate horizontally by the given \meta{factor}:
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                (0,0) -- (1,1) -- (1,0);
  \draw[xslant=2,blue] (0,0) -- (1,1) -- (1,0);
  \draw[xslant=-1,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/yslant=\meta{factor}}
    Slants the coordinate vertically by the given \meta{factor}:
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                (0,0) -- (1,1) -- (1,0);
  \draw[yslant=2,blue] (0,0) -- (1,1) -- (1,0);
  \draw[yslant=-1,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/rotate=\meta{degree}}
    Rotates the coordinate system by \meta{degree}:
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                 (0,0) -- (1,1) -- (1,0);
  \draw[rotate=40,blue] (0,0) -- (1,1) -- (1,0);
  \draw[rotate=-20,red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/rotate around={\ttfamily\char`\{}\meta{degree}|:|\meta{coordinate}{\ttfamily\char`\}}}
    Rotates the coordinate system by \meta{degree} around the point
    \meta{coordinate}.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                                (0,0) -- (1,1) -- (1,0);
  \draw[rotate around={40:(1,1)},blue] (0,0) -- (1,1) -- (1,0);
  \draw[rotate around={-20:(1,1)},red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/rotate around x=\meta{angle}}
    This key sets the $x$, $y$ and $z$ vectors of the \pgfname\
    $xyz$-coordinate system so that they are rotated by \meta{angle} around the
    axis corresponding to the $x$-vector. The rotation is applied so that when
    looking towards the origin along this axis, positive angles result in an
    anticlockwise rotation.
    %
\begin{codeexample}[]
\begin{tikzpicture}[>=stealth]
  \draw [->] (0,0,0) -- (2,0,0) node [at end, right] {$x$};
  \draw [->] (0,0,0) -- (0,2,0) node [at end, left]  {$y$};
  \draw [->] (0,0,0) -- (0,0,2) node [at end, left]  {$z$};

  \draw [red,   rotate around x=0]  (0,0,0) -- (1,1,0) -- (1,0,0);
  \draw [green, rotate around x=45] (0,0,0) -- (1,1,0) -- (1,0,0);
  \draw [blue,  rotate around x=90] (0,0,0) -- (1,1,0) -- (1,0,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/rotate around y=\meta{angle}}
    This key sets the $x$, $y$ and $z$ vectors of the \pgfname\
    $xyz$-coordinate system so that they are rotated by \meta{angle} around
    the axis corresponding to the $y$-vector. The rotation is applied so that
    when looking towards the origin along this axis, positive angles result in
    an anticlockwise rotation.
    %
\begin{codeexample}[]
\begin{tikzpicture}[>=stealth]
  \draw [->] (0,0,0) -- (2,0,0) node [at end, right] {$x$};
  \draw [->] (0,0,0) -- (0,2,0) node [at end, left]  {$y$};
  \draw [->] (0,0,0) -- (0,0,2) node [at end, left]  {$z$};

  \draw [red,   rotate around y=0]   (0,0,0) -- (1,1,0) -- (1,0,0);
  \draw [green, rotate around y=-45] (0,0,0) -- (1,1,0) -- (1,0,0);
  \draw [blue,  rotate around y=-90] (0,0,0) -- (1,1,0) -- (1,0,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/rotate around z=\meta{angle}}
    This key sets the $x$, $y$ and $z$ vectors of the \pgfname\
    $xyz$-coordinate system so that they are rotated by \meta{angle} around the
    axis corresponding to the $z$-vector. The rotation is applied so that when
    looking towards the origin along this axis, positive angles result in an
    anticlockwise rotation.
    %
\begin{codeexample}[]
\begin{tikzpicture}[>=stealth]
  \draw [->] (0,0,0) -- (2,0,0) node [at end, right] {$x$};
  \draw [->] (0,0,0) -- (0,2,0) node [at end, left]  {$y$};
  \draw [->] (0,0,0) -- (0,0,2) node [at end, left]  {$z$};

  \draw [red,   rotate around z=0]  (0,0) -- (1,1) -- (1,0);
  \draw [green, rotate around z=45] (0,0) -- (1,1) -- (1,0);
  \draw [blue,  rotate around z=90] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/cm={\ttfamily\char`\{}\meta{$a$}|,|\meta{$b$}|,|\meta{$c$}|,|\meta{$d$}|,|\meta{coordinate}{\ttfamily\char`\}}}
    applies the following transformation to all coordinates: Let $(x,y)$ be the
    coordinate to be transformed and let \meta{coordinate} specify the point
    $(t_x,t_y)$. Then the new coordinate is given by
    $\left(\begin{smallmatrix} a & c \\ b & d\end{smallmatrix}\right)
    \left(\begin{smallmatrix} x \\ y \end{smallmatrix}\right) +
    \left(\begin{smallmatrix} t_x \\ t_y \end{smallmatrix}\right)$.
    Usually, you do not use this option directly.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                             (0,0) -- (1,1) -- (1,0);
  \draw[cm={1,1,0,1,(0,0)},blue]    (0,0) -- (1,1) -- (1,0);
  \draw[cm={0,1,1,0,(1cm,1cm)},red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/reset cm}
    Completely resets the coordinate transformation matrix to the identity
    matrix. This will destroy not only the transformations applied in the
    current scope, but also all transformations inherited from surrounding
    scopes. Do not use this option, unless you really, really know what you are
    doing.
\end{key}

\subsection{Canvas Transformations}

A \emph{canvas transformation}, see
Section~\ref{section-design-transformations} for details, is best thought of as
a transformation in which the drawing canvas is stretched or rotated. Imaging
writing something on a balloon (the canvas) and then blowing air into the
balloon: Not only does the text become larger, the thin lines also become
larger. In particular, if you scale the canvas by a factor of two, all lines
are twice as thick.

Canvas transformations should be used with great care. In most circumstances
you do \emph{not} want line widths to change in a picture as this creates
visual inconsistency.

Just as important, when you use canvas transformations \emph{\pgfname\ loses
track of positions of nodes and of picture sizes} since it does not take the
effect of canvas transformations into account when it computes coordinates of
nodes (do not, however, rely on this; it may change in the future).

Finally, note that a canvas transformation always applies to a path as a whole,
it is not possible (as for coordinate transformations) to use different
transformations in different parts of a path.

In short, you should not use canvas transformations unless you really know what
you are doing.

\begin{key}{/tikz/transform canvas=\meta{options}}
    The \meta{options} should contain coordinate transformations options like
    |scale| or |xshift|. Multiple options can be given, their effects
    accumulate in the usual manner. The effect of these \meta{options}
    (immediately) changes the current canvas transformation matrix. The
    coordinate transformation matrix is not changed. Tracking of the picture
    size is (locally) switched off and the node coordinate will no longer be
    correct.
    %
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \draw                                    (0,0) -- (1,1) -- (1,0);
  \draw[transform canvas={scale=2},blue]   (0,0) -- (1,1) -- (1,0);
  \draw[transform canvas={rotate=180},red] (0,0) -- (1,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}


\documentclass{article}
\input{/Users/ayberkt/Developer/latex-basis/basis.tex}

\usepackage{boxproof}
\usepackage{mathpazo}

\author{Ayberk Tosun}
\title{
  Practical Foundations of Mathematics\\
  Exercise Solutions
}

\begin{document}
\maketitle

\section*{Chapter 1}

\subsection*{Exercise 22}

\begin{proposition}
  \( (\alpha \land \beta) \Rightarrow \gamma
     \dashv \vdash \alpha \Rightarrow (\beta \Rightarrow \gamma) \).
\end{proposition}

\begin{proof}
\begin{proofbox}
   \lbl{1}\: (\alpha \land \beta) \Rightarrow \gamma \\
   \[
      \lbl{2}\: \alpha\= \mathsf{hyp} \\
      \[
        \: \beta \= \mathsf{hyp} \\
        \: \alpha \= (\ref{2}) \\
        \: \alpha \land \beta \= \land\mathcal{I}\\
        \: \gamma \= \elim\Rightarrow
      \]
      \: \beta \Rightarrow \gamma \= \intro\Rightarrow
   \]
   \: \alpha \Rightarrow (\beta \Rightarrow \gamma) \= \intro\Rightarrow
\end{proofbox}
\end{proof}

\end{document}