\documentclass[12pt,oneside]{amsart}
\usepackage[margin=1in]{geometry}
\usepackage{times}
\usepackage{graphicx}
\usepackage{amsmath,amsfonts}
\usepackage{url}
\usepackage[colorlinks=true,
            linkcolor=red,
            urlcolor=blue,
            citecolor=gray]{hyperref}
\parindent=12pt
\pagestyle{empty}

\newcommand{\header}[1]{\bigbreak\textbf{#1}}

\begin{document}

\begin{center}
  \bf
  \includegraphics[width=3in]{metu_logo} \\
Lecture Notes 1\\
 Electrical and Electronics Engineering - 5670402 \\
 Discrete Time Systems \\
 Fall, 2018-2019
\end{center}

\vspace{12pt}

\section{Continuous \& Discrete Time Signals}

\vspace{12pt}

A continuous time bilateral signal is a mapping defined by $f:
\mathbb{R} \mapsto \mathbb{R}$ (or for unilateral case $f:
\mathbb{R}^+ \mapsto \mathbb{R}$). Examples

\begin{align*}
  f(t) = \sin (t) \ , \
  f(t) = e^t \ , \
  f(t) = u(t) \ , \
  f(t) = \delta(t) \ , \ \mathrm{where} \ t \in \mathbb{R}
\end{align*}

\vspace{6pt}

A discrete time bilateral signal is a mapping defined by $g:
\mathbb{Z} \mapsto \mathbb{R}$ (or for unilateral case $g:
\mathbb{Z}^+ \mapsto \mathbb{R}$). Examples

\begin{align*}
  f[n] = \sin [n] \ , \
  f[n] = 5^n \ , \
  f[n] = u[n] \ , \
  f[n] = \delta[n] \ , \ \mathrm{where} \ n \in \mathbb{Z}
\end{align*}

\vspace{6pt}

Graphical Examples

\begin{figure}[h]
    \centering
      \includegraphics[width=0.9\textwidth]{signals}
    \caption{CT vs DT Signal}
\end{figure}

\newpage

\section{Continuous \& Discrete Time Dynamical Systems}

The system is modeled as a \textit{mapping} from a set of input
signals, $u(t)$ or $u[n]$, to a set of output signals, $y(t)$ or $y[n]$.
We may represent continuous time and discrete time maps as

\begin{align*}
  &\mathrm{Continuous}: \ \ \ y(t) = (S_c \ u) (t) \\
  &\mathrm{Discrete}: \ \ \ y[n] = (S_d \ u) [n]
\end{align*}

\vspace{6pt}

\textit{Operation} is performed on the entire input signal, $u(\cdot)$ or
$u[\cdot]$ where the mappings $S_c$ and $S_d$ yield the signals
$y(\cdot)$ and $y[\cdot]$.

\vspace{12pt}

    \begin{center}
  \begin{minipage}[h]{0.9\linewidth}
    \begin{center}
      \includegraphics[width=0.65\textwidth]{blocks}
    \end{center}
  \end{minipage}
    \end{center}

\vspace{12pt}

\subsection{Properties of Input--Output Systems}

\begin{itemize}

\item \textbf{Linearity}
\vspace{6pt}

A continuous time system is \textbf{linear} if and only if

\begin{align*}
(S_c \ (\alpha u_1 + \beta u_2) ) (t) &= \alpha  (S_c \ u_1) (t) +
  \beta  (S_c \ u_2 ) (t) \\
&\forall \alpha , \ \beta, \ u_1(\cdot), \ \& \ u_2(\cdot)
\end{align*}

A discrete time system is \textbf{linear} if and only if

\begin{align*}
(S_d \ (\alpha u_1 + \beta u_2) ) [n] &= \alpha  (S_d \ u_1) [n] +
  \beta  (S_d \ u_2 ) [n] \\
&\forall \alpha , \ \beta, \ u_1[\cdot], \ \& \ u_2[\cdot]
\end{align*}

\item \textbf{Time Invariance}
\vspace{6pt}

Let $\sigma_T$ be the time-shift operator as
%
\begin{align*}
\sigma_T \ u(t) = u(t-T)
\end{align*}

Then a continuous time system is time-invariant if and only if

\begin{align*}
(S_c \ \sigma_T \ u) ) (t) &= y(t - T) \ \forall T \in \mathbb{R}, \
                             \mathrm{where} \ (S_c \ u ) (t) = y(t) \ \forall T \in \mathbb{R}
\end{align*}

\vspace{6pt}

Similarly for discrete time systems

\begin{align*}
(S_d \ \sigma_k \ u) ) [n] &= y[n-k] \ \forall k \in \mathbb{Z}, \ \mathrm{where} \ (S_d \ u) ) [n] = y[n]
\end{align*}

\vspace{6pt}

\item \textbf{Memoryless Systems:}

\vspace{6pt}

A continuous time system is memoryless if and
only if $y(t)$ only depends on $u(t)$, $\forall \ t \in \mathbb{R}$.

\vspace{6pt}

A discrete time system is memoryless if and
only if $y[n]$ only depends on $u[n]$, $\forall \ n \in \mathbb{Z}$.

\vspace{12pt}

\item \textbf{Causality:}

\vspace{6pt}

We say the system is causal if the output does not depend on future
values of the input. Mathematically we can show causality using the
\textit{truncation} operator, $P_T$. For continuous systems
\textit{truncation} is defined as

\begin{align*}
(P_T \ u ) (t) &= \left\lbrace \begin{array}{c} u(t) \ \ \mathrm{for}
                                  \ t \leq T
\\ 0 \ \ \mathrm{otherwise} \end{array} \right.
\end{align*}

for discrete systems

\begin{align*}
(P_k \ u ) [n] &= \left\lbrace \begin{array}{c} u[n] \ \ \mathrm{for}
                                  \ n \leq k \\ 0 \ \ \mathrm{otherwise} \end{array} \right.
\end{align*}

\vspace{6pt}

then the system, $S$ (continuous or discrete), is said to be causal if
$P_T S = P_T S P_T \ , \forall T$

\vspace{12pt}

\item \textbf{Finite \& Infinite Dimensional Systems:}

\vspace{6pt}

A continuous time dynamical system, $S_c$, is finite dimensional
if there exist an ODE in $u,y$ that models $S_c$.

\vspace{6pt}

A discrete time dynamical system, $S_d$, is finite dimensional
if there exist an Ordinary Difference Equation in $u,y$ that models $S_d$.

\end{itemize}

\subsection{Examples}

\begin{enumerate}

\item

  \begin{minipage}[h]{0.5\linewidth}
      $u(t) = \tau(t) , \ y(t) = \theta(t)$
  \end{minipage}
  \begin{minipage}[h]{0.5\linewidth}
    \begin{center}
      \includegraphics[width=0.5\textwidth]{pendulum}
    \end{center}
  \end{minipage}

\vspace{6pt}

Non-linear, Time-invariant, causal, system has memory, and finite dimensional

\vspace{6pt}

\item  Now let's assume that $g = 0$, what happens?

\vspace{6pt}

Linear, Time-invariant, causal, system has memory, and finite dimensional

\vspace{6pt}

\item $y(t) = \int\limits_{0}^{t} (t-s)^3 u(s) ds$

\vspace{6pt}

Linear, causal, system has memory, finite dimensional,

Since the system is causal it may be OK to assume that the set of
inputs are limited to causal signals and the convolution is unilateral.
In this case the system is \textit{time-invariant}.

\vspace{6pt}

However if non-causal signals are allowed then the system becomes
\textit{time-varying}.

\end{enumerate}

\newpage

\section{Representations of Dynamical Systems}

\vspace{12pt}

\subsection{Differential \& Difference Equations }

\vspace{12pt}

\begin{itemize}

\item \textbf{Continuous Time Systems - ODEs}

\vspace{12pt}

Linear Time Invariant System (LTI)

\begin{align*}
a_n  y^{(n)} + ... + a_1 y' + a_0 y = b_{n}  u^{(n)} + ... + b_1 u' + b_0 u
\end{align*}

\vspace{12pt}

Linear Time Varying System (LTV)

\begin{align*}
a_n(t)  y^{(n)} + ... + a_1(t) y' + a_0(t) y = b_{n}(t)  u^{(n)} + ... + b_1(t) u' + b_0(t) u
\end{align*}

\vspace{12pt}

Non-linear Time Invariant System

\begin{align*}
y^{(n)} = f(y^{(n-1)}, ..., y', y, u^{(n)},  ... , u' , u)
\end{align*}

\vspace{12pt}

Non-linear Time Varying System

\begin{align*}
y^{(n)} = f(y^{(n-1)}, ..., y', y, u^{(n)},  ... , u' , u, t)
\end{align*}

\vspace{12pt}

\item \textbf{Discrete Time Systems - Difference Equations}

\vspace{12pt}

Discrete-Time Linear Time Invariant System (LTI)

\begin{align*}
a_n  y[k] + a_{n-1}  y[k-1] + ... + a_0 y[k-n] = b_{n}  u[k] + ... + b_0 u[k-n]
\end{align*}

\vspace{12pt}

Discrete-Time Linear Time Varying System (LTV)

\begin{align*}
a_n[k]  y[k] + a_{n-1}[k] y[k-1] + ... + a_0[k] y[k-n] = b_{n}[k]  u[k] + ... + b_0[k] u[k-n]
\end{align*}

\vspace{12pt}

Non-linear Time Invariant System

\begin{align*}
y[k] = f(y[k-1], ..., y[k-n], u[k],  ... , u[k-n])
\end{align*}

\vspace{12pt}

Non-linear Time Varying System

\begin{align*}
y[k] = f(y[k-1], ..., y[k-n], u[k],  ... , u[k-n],k)
\end{align*}

\vspace{12pt}

\end{itemize}

\vspace{12pt}

Discussion

\vspace{6pt}

\begin{itemize}
  \item When an ODE representation becomes \textit{memoryless}?
  \item When a difference equation representation becomes \textit{memoryless}?
  \item What about infinite dimensional systems?
  \item What about \textit{causality}?
\end{itemize}

\subsection{State-Space Representation of Dynamical Systems}

\begin{itemize}

\item \textbf{Continuous-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  \mathrm{Let} \ x(t) &\in \mathbb{R}^n \ , \ y(t) \in \mathbb{R} \ ,\  u(t) \in
  \mathbb{R} , \\
  \dot{x}(t) &= A x(t) + B u(t) , \\
  y(t) &= C x(t) + D u(t) , \\
  \mathrm{where} \ A &\in \mathbb{R}^{n \times n} \ , \
    B \in \mathbb{R}^{n \times 1} \ ,\  C \in \mathbb{R}^{1 \times n} \ , \ D \in \mathbb{R}
\end{align*}

Linear Time Varying Systems

\begin{align*}
  \mathrm{Let} \ x(t) &\in \mathbb{R}^n \ , \ y(t) \in \mathbb{R} \ ,\  u(t) \in
  \mathbb{R} , \\
  \dot{x}(t) &= A(t) x(t) + B(t) u(t) , \\
  y(t) &= C(t) x(t) + D(t) u(t) , \\
  \mathrm{where} \ A(t) &\in \mathbb{R}^{n \times n} \ , \
    B(t) \in \mathbb{R}^{n \times 1} \ ,\  C(t) \in \mathbb{R}^{1 \times n} \ , \ D(t) \in \mathbb{R}
\end{align*}

Non-Linear Time Invariant Systems

\begin{align*}
  \mathrm{Let} \ x(t) &\in \mathbb{R}^n \ , \ y(t) \in \mathbb{R} \ ,\  u(t) \in
  \mathbb{R} , \\
  \dot{x}(t) &= F(x(t),u(t)) , \\
  y(t) &= H(x(t),u(t)) ,
\end{align*}

Non-Linear Time Varying Systems

\begin{align*}
  \mathrm{Let} \ x(t) &\in \mathbb{R}^n \ , \ y(t) \in \mathbb{R} \ ,\  u(t) \in
  \mathbb{R} , \\
  \dot{x}(t) &= F(x(t),u(t),t) , \\
  y(t) &= H(x(t),u(t),t) ,
\end{align*}

\item \textbf{Discrete-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  \mathrm{Let} \ x[n] &\in \mathbb{R}^n \ , \ y[n] \in \mathbb{R} \ ,\  u[n] \in
  \mathbb{R} , \\
  x[n+1] &= A x[n] + B u[n] , \\
  y[n] &= C x[n] + D u[n] , \\
  \mathrm{where} \ A &\in \mathbb{R}^{n \times  n} \ , \
    B \in \mathbb{R}^{n \times 1} \ ,\  C \in \mathbb{R}^{1 \times n} \ , \ D \in \mathbb{R}
\end{align*}

\vspace{12pt}

Linear Time Varying Systems

\begin{align*}
  \mathrm{Let} \ x[n] &\in \mathbb{R}^n \ , \ y[n] \in \mathbb{R} \ ,\  u[n] \in
  \mathbb{R} , \\
  x[n+1] &= A[n] x[n] + B[n] u[n] , \\
  y[n] &= C[n] x[n] + D[n] u[n] , \\
  \mathrm{where} \ A[n] &\in \mathbb{R}^{n \times n} \ , \
    B[n] \in \mathbb{R}^{n \times 1} \ ,\  C[n] \in \mathbb{R}^{1
                          \times  n} \ , \ D[n] \in \mathbb{R}
\end{align*}

Non-Linear Time Invariant Systems

\begin{align*}
  \mathrm{Let} \ x[n] &\in \mathbb{R}^n \ , \ y[n] \in \mathbb{R} \ ,\
                        u[n] in \mathbb{R} , \\
  x[n+1] &= F(x[n],u[n]) , \\
  y[n] &= H(x[n],u[n]) ,
\end{align*}

Non-Linear Time Varying Systems

\begin{align*}
  \mathrm{Let} \ x[n] &\in \mathbb{R}^n \ , \ y[n] \in \mathbb{R} \ ,\  u[n] \in
  \mathbb{R} , \\
  x[n+1] &= F(x[n],u[n],n) , \\
  y[n] &= H(x[n],u[n],n) ,
\end{align*}

\end{itemize}

\vspace{12pt}

Discussion

\vspace{6pt}

\begin{itemize}
  \item When a state-space representation becomes \textit{memoryless}?
  \item What about infinite dimensional systems?
  \item What about \textit{causality}?
\end{itemize}

\vspace{12pt}

\subsection{Impulse-Response Representation of Dynamical Systems}

\begin{itemize}

\item \textbf{Continuous-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  y(t) &= h(t) \ast u(t) = \int\limits_{-\infty}^{\infty} h(t-\tau) u(\tau) d\tau
\end{align*}

Linear Time Varying Systems

\begin{align*}
  y(t) &= \int\limits_{-\infty}^{\infty} h(t,\tau) u(\tau) d\tau
\end{align*}

\noindent where $h(t,\tau)$ is called time-varying impulse response
function.

\vspace{12pt}

\item \textbf{Discrete-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  y[n] &= \sum\limits_{k = -\infty}^{\infty} h[n -k] u[k]
\end{align*}

Linear Time Varying Systems

\begin{align*}
  y[n] &= \sum\limits_{k = -\infty}^{\infty} h[n,k] u[k]
\end{align*}

\vspace{12pt}

\end{itemize}

\vspace{12pt}

Discussion

\vspace{6pt}

\begin{itemize}
  \item Under what condition(s) an impulse response representation
    becomes \textit{memoryless}?
  \item Under what condition(s) an impulse response representation
    becomes \textit{causal}?
  \item What about finite and infinite dimensional systems?
  \item What are the differences between continuous time and discrete
    time impulse response?
\end{itemize}

\subsection{Transfer Functions Representation of Dynamical Systems}

\begin{itemize}

\item \textbf{Continuous-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  Y(s) &= G(s) U(s) , \ \mathrm{where}  , \\
  Y(s) &= \mathcal{L}\lbrace y(t) \rbrace \ , \& \
  U(s) = \mathcal{L}\lbrace u(t) \rbrace
\end{align*}

\vspace{12pt}

\item \textbf{Discrete-Time Dynamical Systems}

Linear Time Invariant Systems

\begin{align*}
  Y(z) &= G(z) U(z) , \ \mathrm{where}  , \\
  Y(z) &= \mathcal{Z}\lbrace y[n] \rbrace \ , \& \
  U(z) = \mathcal{Z}\lbrace u[n] \rbrace
\end{align*}

\vspace{12pt}

Discussion

\vspace{6pt}

\begin{itemize}
  \item Can we model/represent non-linear systems using transfer
    functions?
  \item Can we model/represent linear time-varying systems using transfer
    functions?
  \item What about finite and infinite dimensional systems?
  \item What about \textit{causality}?
\end{itemize}

\vspace{12pt}

\end{itemize}
\end{document}


\documentclass{wg21}

\usepackage{xcolor}
\usepackage{soul}
\usepackage{ulem}
\usepackage{fullpage}
\usepackage{parskip}
\usepackage{csquotes}
\usepackage{listings}
\usepackage{minted}
\usepackage{enumitem}

\lstdefinestyle{base}{
  language=c++,
  breaklines=false,
  basicstyle=\ttfamily\color{black},
  moredelim=**[is][\color{green!50!black}]{@}{@},
  escapeinside={(*@}{@*)}
}

\newcommand{\cc}[1]{\mintinline{c++}{#1}}
\newminted[cpp]{c++}{}

\title{Making \cc{std::priority_queue} constexpr}
\docnumber{P1926R0}
\audience{LEWGI}
\author{Alexander Zaitsev}{zamazan4ik@tut.by, zamazan4ik@gmail.com}

\begin{document}
\maketitle

\section{Revision history}
\begin{itemize}
  \item R0 -- Initial draft
\end{itemize}

\section{Abstract}
\cc{std::priority_queue} is not currently \cc{constexpr} friendly. With the loosening
of requirements on \cc{constexpr} in \cite{P0784R1} and related papers, we
can now make \cc{std::priority_queue} \cc{constexpr}, and we should in order to support
the \cc{constexpr} reflection effort (and other evident use cases).

\section{Motivation}
\cc{std::priority_queue} is not so widely-used standard container as \cc{std::vector} or \cc{std::string}. But there is no reason to keep \cc{std::priority_queue} in non-\cc{constexpr} state since one of the main directions of C++ evolution is compile-time programming. And we want to use in compile-time as much as possible from STL. And this paper makes \cc{std::priority_queue} available in compile-time.

\section{Proposed wording}
We basically mark all the member and non-member functions of \cc{std::priority_queue} \cc{constexpr}.

Direction to the editor: please apply \cc{constexpr} to all of \cc{std::priority_queue},
including any additions that might be missing from this paper.

In \textbf{[support.limits.general]}, add the new feature test macro
\cc{__cpp_lib_constexpr_priority_queue} with the corresponding value for header
\cc{<queue>} to Table 36 \textbf{[tab:support.ft]}.

Change in \textbf{[queue.syn] 22.6.2}:
\begin{quote}
\begin{codeblock}
#include <initializer_list>

namespace std {
  [...]
  template<class T, class Container = vector<T>,
           class Compare = less<typename Container::value_type>>
    class priority_queue;

  template<class T, class Container, class Compare>
    @\added{constexpr}@ void swap(priority_queue<T, Container, Compare>& x,
      priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));
  template<class T, class Container, class Compare, class Alloc>
    struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>;
}
\end{codeblock}
[...]
\end{quote}

Change in \textbf{[priqueue.overview] 22.6.5.1}:
\begin{quote}
\begin{codeblock}
namespace std {
  template<class T, class Container = vector<T>,
           class Compare = less<typename Container::value_type>>
  class priority_queue {
  public:
	using value_type      = typename Container::value_type;
	using reference       = typename Container::reference;
	using const_reference = typename Container::const_reference;
	using size_type       = typename Container::size_type;
	using container_type  = Container;
	using value_compare   = Compare;

  protected:
	Container c;
	Compare comp;

  public:
	@\added{constexpr}@ priority_queue() : priority_queue(Compare()) {}
	@\added{constexpr}@ explicit priority_queue(const Compare& x) : priority_queue(x, Container()) {}
	@\added{constexpr}@ priority_queue(const Compare& x, const Container&);
	@\added{constexpr}@ priority_queue(const Compare& x, Container&&);
	template<class InputIterator>
	  @\added{constexpr}@ priority_queue(InputIterator first, InputIterator last, const Compare& x,
	const Container&);
	template<class InputIterator>
	  @\added{constexpr}@ priority_queue(InputIterator first, InputIterator last,
	const Compare& x = Compare(), Container&& = Container());
	template<class Alloc> @\added{constexpr}@ explicit priority_queue(const Alloc&);
	template<class Alloc> @\added{constexpr}@ priority_queue(const Compare&, const Alloc&);
	template<class Alloc> @\added{constexpr}@ priority_queue(const Compare&, const Container&, const Alloc&);
	template<class Alloc> @\added{constexpr}@ priority_queue(const Compare&, Container&&, const Alloc&);
	template<class Alloc> @\added{constexpr}@ priority_queue(const priority_queue&, const Alloc&);
	template<class Alloc> @\added{constexpr}@ priority_queue(priority_queue&&, const Alloc&);

	[[nodiscard]] @\added{constexpr}@ bool empty() const { return c.empty(); }
	@\added{constexpr}@ size_type size()  const          { return c.size(); }
	@\added{constexpr}@ const_reference   top() const    { return c.front(); }
	@\added{constexpr}@ void push(const value_type& x);
	@\added{constexpr}@ void push(value_type&& x);
	template<class... Args> @\added{constexpr}@ void emplace(Args&&... args);
	@\added{constexpr}@ void pop();
	@\added{constexpr}@ void swap(priority_queue& q) noexcept(is_nothrow_swappable_v<Container> &&
	  is_nothrow_swappable_v<Compare>)
	    { using std::swap; swap(c, q.c); swap(comp, q.comp); }
  };

  template<class Compare, class Container>
    priority_queue(Compare, Container)
      -> priority_queue<typename Container::value_type, Container, Compare>;

  template<class InputIterator,
           class Compare = less<typename iterator_traits<InputIterator>::value_type>,
           class Container = vector<typename iterator_traits<InputIterator>::value_type>>
    priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
      -> priority_queue<typename iterator_traits<InputIterator>::value_type, Container, Compare>;

  template<class Compare, class Container, class Allocator>
    priority_queue(Compare, Container, Allocator)
      -> priority_queue<typename Container::value_type, Container, Compare>;

  // no equality is provided

  template<class T, class Container, class Compare>
    @\added{constexpr}@ void swap(priority_queue<T, Container, Compare>& x,
      priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));

  template<class T, class Container, class Compare, class Alloc>
    struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>
      : uses_allocator<Container, Alloc>::type { };
}
\end{codeblock}%
\end{quote}

Change in \textbf{[priqueue.cons] 22.6.5.2}:
\begin{quote}
\begin{itemdecl}
@\added{constexpr}@ priority_queue(const Compare& x, const Container& y);
@\added{constexpr}@ priority_queue(const Compare& x, Container&& y);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class InputIterator>
  @\added{constexpr}@ priority_queue(InputIterator first, InputIterator last,
                           const Compare& x, const Container& y);
template<class InputIterator>
  @\added{constexpr}@ priority_queue(InputIterator first, InputIterator last,
                           const Compare& x = Compare(), Container&& y = Container());
\end{itemdecl}
\end{quote}

Change in \textbf{[priqueue.cons.alloc] 22.6.5.3}:
\begin{quote}
\begin{itemdecl}
template<class Alloc> @\added{constexpr}@ explicit priority_queue(const Alloc& a);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class Alloc> @\added{constexpr}@ priority_queue(const Compare& compare, const Alloc& a);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class Alloc>
  @\added{constexpr}@ priority_queue(const Compare& compare, const Container& cont, const Alloc& a);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class Alloc>
  @\added{constexpr}@ priority_queue(const Compare& compare, Container&& cont, const Alloc& a);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class Alloc> @\added{constexpr}@ priority_queue(const priority_queue& q, const Alloc& a);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class Alloc> @\added{constexpr}@ priority_queue(priority_queue&& q, const Alloc& a);
\end{itemdecl}
[...]
\end{quote}

Change in \textbf{[priqueue.members] 22.6.5.4}:
\begin{quote}
\begin{itemdecl}
@\added{constexpr}@ void push(const value_type& x);
\end{itemdecl}
[...]
\begin{itemdecl}
@\added{constexpr}@ void push(value_type&& x);
\end{itemdecl}
[...]
\begin{itemdecl}
template<class... Args> @\added{constexpr}@ void emplace(Args&&... args)
\end{itemdecl}
[...]
\begin{itemdecl}
@\added{constexpr}@ void pop();
\end{itemdecl}
[...]
\end{quote}

Change in \textbf{[priqueue.special] 22.6.5.5}:
\begin{quote}
\begin{itemdecl}
template<class T, class Container, class Compare>
  @\added{constexpr}@ void swap(priority_queue<T, Container, Compare>& x,
    priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));
\end{itemdecl}

\end{quote}

\section{Implementation}
Possible implementation can be found here: \href{https://github.com/ZaMaZaN4iK/llvm-project/tree/feature/priority_queue_constexpr}{LLVM fork}. Notice that when proposal was written constexpr destructors were not supported in Clang.

\section{References}
\renewcommand{\section}[2]{}%
\begin{thebibliography}{9}

\bibitem[P0784R1]{P0784R1}
  Multiple authors,
  \emph{Standard containers and constexpr}\newline
  \url{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0784r1.html}

\end{thebibliography}

\end{document}


\chapter{Implementation II:  Optimizing {\em walk}}\label{walkimpl}

In this chapter we examine the efficiency of the \mbox{\scheme|walk|}
algorithm presented in Chapter~\ref{mkimplchapter}, which is the heart
of the unification algorithm.  We present various optimizations to
\mbox{\scheme|walk|}, which significantly improve performance of
unification, and indeed the entire miniKanren implementation.

This chapter is organized as follows.  In section~\ref{walkexpensive}
we examine the worst-case performance of the \mbox{\scheme|walk|}
algorithm, with emphasis on the cost of looking up an unassociated
variable.  Section~\ref{birthrecords} introduces an optimization using
\emph{birth records}, which can greatly increase the speed of looking
up an unassociated variable.  In section~\ref{elimassq} we look at an
additional optimization that requires we rewrite \mbox{\scheme|walk|}
using explicit recursion instead of \mbox{\scheme|assq|}.  Finally,
section~\ref{storesubst} shows how we can further improve on the
birth-records optimization by storing the current substitution in a
variable when it is first introduced.
% Finally, section~\ref{smartwalkbenchmark} demonstrates that the
% optimizations work in practice by benchmarking the performance of
% several miniKanren applications using the various walks.

\section{Why {\em walk} is Expensive}\label{walkexpensive}
In the worst case, the number of cdrs and tests
performed by \mbox{\scheme|walk|} is quadratic in the length of the
substitution.  This happens when looking up a variable at the
beginning of a long ``unification chain''---for example, when looking
up \mbox{\scheme|v|} in the ``perfectly triangular'' substitution
\mbox{\scheme|`((,y . ,z) (,x . ,y) (,w . ,x) (,v . ,w))|}.  Contrast
this with the linear cost of looking up \mbox{\scheme|v|} in the
equivalent idempotent substitution
\mbox{\scheme|`((,y . ,z) (,x . ,z) (,w . ,z) (,v . ,z))|}.

Fortunately, extremely long unification chains rarely occur in real
logic programs.  Rather, the major cost of variable lookups is in
walking unassociated variables.  When using triangular substitutions
(or even idempotent substitutions), the entire substitution must be
examined to determine that a variable in unassociated\footnote{Prolog
  implementations based on the Warren Abstract Machine~\cite{wamtutorial} do
  not use explicit substitutions to represent variable associations.
  Instead, they represent each variable as a mutable box, and
  side-effect the box during unification.  This makes variable lookup
  extremely fast, but requires remembering and undoing these
  side-effects during backtracking.  In addition, this simple model
  assumes a depth-first search strategy, whereas our purely functional
  representation can be used with interleaving search without
  modification.}.

One solution to this problem is to use a more sophisticated data
structure to represent triangular substitutions---for example, we
might use a trie~\cite{triememory} instead of a list, to ensure logarithmic
cost when looking up an unassociated variable\footnote{Abdulaziz
  Ghuloum has implemented miniKanren using a trie-based representation
  of triangular substitutions.  David Bender and Lindsey Kuper have
  extended this work, using a variety of purely functional data
  structures to represent triangular substitutions.  These more
  sophisticated representations of substitutions can result in much
  faster walking of variables, which can greatly speed up many
  miniKanren programs.  The best performance for their benchmarks
  was achieved using a skew binary number representation within a
  random access list~\cite{randomaccesslists}.}.  For simplicity we
will retain our association list representation of substitutions.
Instead of changing the substitution representation, we will use a
trick to determine if a variable is unassociated without having to
look at the entire substitution.

\section{Birth Records}\label{birthrecords}

To avoid examining the entire substitution when walking an
unassociated variable, we will add a \emph{birth record} to the
substitution whenever we introduce a variable using
\mbox{\scheme|exist|}.  For example, to run the goal
\mbox{\scheme|(exist (x y) (== 5 x))|} we would add the birth records
\mbox{\scheme|`(,x . ,x)|} and
\mbox{\scheme|`(,y . ,y)|} to the current substitution, then run
\mbox{\scheme|(== 5 x)|} in the extended substitution.
Unifying \mbox{\scheme|x|} with \mbox{\scheme|5|} requires us to walk \mbox{\scheme|x|}:
when we do so, we immediately encounter the birth record \mbox{\scheme|`(,x . ,x)|},
indicating \mbox{\scheme|x|} is unassociated.  Unification then succeeds, adding the association
\mbox{\scheme|`(,x . 5)|} to the substitution to produce \mbox{\scheme|`((,x . 5) (,x . ,x) (,y . ,y) ...)|}.

Here are \mbox{\scheme|exist|} and \mbox{\scheme|walk|}, modified to use birth records.

\schemedisplayspace
\begin{schemedisplay}
(define-syntax exist
  (syntax-rules ()
    ((_ (x ...) g0 g ...)
     (lambdag@ (s)
       (inc
         (let ((x (var 'x)) ...)
           (let* ((s (ext-s x x s))
                  ...)
             (bind* (g0 s) g ...))))))))
\end{schemedisplay}
\newpage
\begin{schemedisplay}
(define walk
  (lambda (v s)
    (cond
      ((var? v)
       (let ((a (assq v s)))
         (cond
           (a (if (eq? (rhs a) v) v (walk (rhs a) s)))
           (else v))))
      (else v))))
\end{schemedisplay}

Technically, birth records ensure that we need not examine the entire
substitution to determine a variable is unassociated.  However, in the
worst case our situation has not improved\footnote{Indeed, the
  situation is even worse, since the birth records more than double
  the length of the substitution that must be walked.}: if a variable
is introduced at the beginning of a program, but is not unified until
the end of the program, the birth record will occur at the very end of
the substitution, and lookup will still take linear time.
Fortunately, in most real-world programs variables are unified shortly
after they have been introduced.  This locality of reference means
that, in practice, birth records significantly reduce the cost of
walking unassociated variables.
% (see section~\ref{smartwalkbenchmark}).

\section{Eliminating {\em assq} and Checking the {\em rhs}}\label{elimassq}

We can optimize \mbox{\scheme|walk|} in another way, although we will
need to eliminate our call to \mbox{\scheme|assq|}, and introduce a
recursion using ``named'' \scheme|let|
\footnote{This chapter assumes miniKanren is
  run under an optimizing compiler, such as Ikarus Scheme
  or Chez Scheme.  When run under an interpreter, the
  ``named''-\mbox{\scheme|let|} based \mbox{\scheme|walk|} described in this
  section may run much slower than the \mbox{\scheme|assq|}-based
  version, since \mbox{\scheme|assq|} is often hand-coded in C.  When
  running under an interpreter, the \mbox{\scheme|assq|}-based
  \mbox{\scheme|walk|} with birth records will probably be fastest.}.
Here is the standard \mbox{\scheme|walk|}, without birth records.

\schemedisplayspace
\begin{schemedisplay}
(define walk
  (lambda (v s^)
    (let loop ((s s^))
      (cond
        ((var? v)
         (cond
           ((null? s) v)
           ((eq? v (lhs (car s))) (walk (rhs (car s)) s^))
           (else (loop (cdr s)))))
        (else v)))))
\end{schemedisplay}

We can optimize \mbox{\scheme|walk|} by exploiting an important
property of the triangular substitutions produced by
\mbox{\scheme|unify|}: in the substitution \mbox{\scheme|`((,x . ,y) . ,s^)|},
the variable \mbox{\scheme|y|} will never appear in the
left-hand-side (lhs) of any binding in \mbox{\scheme|s^|}.  Therefore,
when walking a variable \mbox{\scheme|y|} we can look for
\mbox{\scheme|y|} in both the lhs and rhs of each association.  If
\mbox{\scheme|y|} is the lhs, we found the variable we are looking
for, and need to walk the rhs in the original substitution.  However,
if we find \mbox{\scheme|y|} in the rhs of an association, we know
that \mbox{\scheme|y|} is unassociated.

Here is the optimized version of \mbox{\scheme|walk|}

\schemedisplayspace
\begin{schemedisplay}
(define walk
  (lambda (v s^)
    (let loop ((s s^))
      (cond
        ((var? v)
         (cond
           ((null? s) v)
           ((eq? v (rhs (car s))) v)
           ((eq? v (lhs (car s))) (walk (rhs (car s)) s^))
           (else (loop (cdr s)))))
        (else v)))))
\end{schemedisplay}

\noindent where \scheme|lhs| and \scheme|rhs|\footnote{\scheme|lhs| is
just defined to be \scheme|car|; \scheme|rhs| is just defined to be
\scheme|cdr|.} return the left-hand-side and right-hand-side of an
association, respectively\footnote{By checking the rhs before
the lhs, we ensure that \scheme|walk| always terminates, even
with substitutions that contain circularities.  If the substitution
contains a circularity of the form \mbox{\scheme|`(,x . ,x)|} (a birth
record), then walking \scheme|x| clearly terminates, since the
\scheme|rhs| test will find \scheme|x| before performing the
recursion.  If the substitution contains associations
\mbox{\scheme|`(,x . ,y)|} and \mbox{\scheme|`(,y . ,x)|},
walking \scheme|x| still terminates despite the circularity.
Assume \mbox{\scheme|`(,y . ,x)|} appears after
\mbox{\scheme|`(,x . ,y)|}
(which will never happen for substitutions returned by
\scheme|unify|); then when we walk \scheme|x|, we will end up walking
\scheme|y| in the recursion.  But we will then find \scheme|y| on the
rhs of \mbox{\scheme|`(,x . ,y)|}, which will end the walk.  The
only other possibility is that \mbox{\scheme|`(,y . ,x)|} appears before \mbox{\scheme|`(,x . ,y)|}.  In this case, walking \scheme|x| does not
result in a recursive call, since we find \scheme|x| on the
rhs of \mbox{\scheme|`(,y . ,x)|}.  Similar reasoning applies for
arbitrarily complicated circularity chains.}.

Once we make a recursive call to \scheme|walk|, the
\scheme|null?| test becomes superfluous, so we
redefine \scheme|walk| using the \scheme|step| helper function.

\schemedisplayspace
\begin{schemedisplay}
(define walk
  (lambda (v s^)
    (let loop ((s s^))
      (cond
        ((var? v)
         (cond
           ((null? s) v)
           ((eq? v (rhs (car s))) v)
           ((eq? v (lhs (car s))) (step (rhs (car s)) s^))
           (else (loop (cdr s)))))
        (else v)))))
\end{schemedisplay}
\newpage
\begin{schemedisplay}
(define step
  (lambda (v s^)
    (let loop ((s s^))
      (cond
        ((var? v)
         (cond
           ((eq? v (rhs (car s))) v)
           ((eq? v (lhs (car s))) (step (rhs (car s)) s^))
           (else (loop (cdr s)))))
        (else v)))))
\end{schemedisplay}

\section{Storing the Substitution in the Variable}\label{storesubst}

We now combine the birth records optimization presented in
section~\ref{birthrecords} with checking for the walked variable in
the rhs of each association, described in section~\ref{elimassq}.
However, we wish to avoid polluting the substitution with birth
records, which not only lengthen the substitution but also violate
important invariants of our substitution
representation\footnote{Namely, that a variable never appears on the
  lhs of more than one association, and that substitutions never
  contain circularities of the form \mbox{\scheme|`(,x . ,x)|}.}.
Instead of adding birth records to the substitution, we will add a
``birth substitution'' to each variable by storing the current
substitution in the variable when it is created.

\schemedisplayspace
\begin{schemedisplay}
(define-syntax exist
  (syntax-rules ()
    ((_ (x ...) g0 g ...)
     (lambdag@ (s)
       (inc
         (let ((x (var s)) ...)
           (bind* (g0 s) g ...)))))))
\end{schemedisplay}

Now, instead of checking for the birth records as we walk down the
substitution, we check if the entire substitution is
\mbox{\scheme|eq?|} to the substitution stored in the walked variable;
if so, we know the variable is unassociated\footnote{It should be
  noted that none of these optimizations avoid the $n+1$ passes that
  might be required when looking up a variable in a perfectly
  triangular substitution of length $n$.}.

\newpage

Here, then, is the most efficient definition of
\mbox{\scheme|walk|}\footnote{Exercise for the reader: show that this
  definition of \mbox{\scheme|walk|} works correctly on the renaming
  substitution used in reification (section~\ref{mkreification})}.

\schemedisplayspace
\begin{schemedisplay}
(define walk
  (lambda (v s^)
    (let loop ((s s^))
      (cond
        ((var? v)
         (cond
           ((eq? (vector-ref v 0) s) v)
           ((eq? v (rhs (car s))) v)
           ((eq? v (lhs (car s))) (step (rhs (car s)) s^))
           (else (loop (cdr s)))))
        (else v)))))
\end{schemedisplay}

% \section{Benchmarking {\em walk}}\label{smartwalkbenchmark}

% [TODO Time the arithemtic system using the different walks.  From
% previous informal benchmarks I know these optimizations are effective
% for a wide variety of programs.]

% [check our various implementation for variants of smart walk code]

% [objective for this chapter--improve performance of walk algorithm]

% [start with standard definition of exist and walk (non-interleaving
% for exist; interleaving variant is obvious---just wrap an inc around
% the body of the lambdag@)]

% \begin{schemedisplay}
% (define-syntax exist
%   (syntax-rules ()
%     ((_ (x ...) g0 g ...)
%      (lambdag@ (s)
%        (inc
%          (let ((x (var 'x)) ...)
%            (bind* (g0 s) g ...)))))))

% (define walk
%   (lambda (v s)
%     (cond
%       ((var? v)
%        (let ((a (assq v s)))
%          (cond
%            (a (walk (rhs a) s))
%            (else v))))
%       (else v))))
% \end{schemedisplay}

% [may also want to include unify-check and ext-s defintions]

% \section{Birth Records}

% [discuss performance implications.  Ikarus and Chez vs. MzScheme, for
% example.  birthrecords vs. our more efficient approach.  what if we
% were using Haskell, and wanted to avoid side effects?]

% [an advantage of birth records: can use normal walk inside of
% walk*---don't need a second, naive version of walk for reification]

% If we find the association \mbox{\scheme|`(,x . ,x)|} when walking
% \scheme|x| in a substitution \scheme|s|, we know that \scheme|x| is
% not associated with any value---there is no need to continue walking
% \scheme|x|.

% \section{Eliminating {\em assq}}

% We can define the naive version of \scheme|walk| without \scheme|assq|:
% \newpage

% Since we walk both \scheme|u| and \scheme|v| at the beginning of
% \scheme|unify|, we know that \scheme|u| and \scheme|v| do not appear
% as the {\em lhs} of any association.
% If when walking \scheme|u| in a substitution \scheme|s|
% we find the association \mbox{\scheme|`(,u . ,v)|}, then we know
% \scheme|v| does not have an association to the right of
% \mbox{\scheme|`(,u . ,v)|}.  Hence we need to walk \scheme|v|
% only up to its first occurrence as a {\em rhs}, and thus we can
% redefine \scheme|step|.

% \section{Storing Substitutions in Variables}

% [disadvantage of this approach: walk* must use naive walk for
% reification---need two definitions of walk]

% [present variants of walk in increasing efficiency]

% [Mitch's optimization]

% [code from /iu/mk/alphatap/optimized/mk.scm is the smartest walk
% version in TRS (second edition?)]

% A vector passed in as one of the first two arguments to \scheme|unify|
% is treated as a variable.  In the definition of \scheme|exist|, we
% create each vector that represents a variable with a symbol. Here, we
% create each vector with the current substitution.

% If the stored substitution of the variable \scheme|x| is encountered
% (with \scheme|eq?|), then we know that the variable \scheme|x| that we
% are walking cannot be a \scheme|lhs| of any association.  Thus, we can
% redefine \scheme|step| for the last time.

% \section{Alternative Substitution Representations}

% [Aziz's trie-based representation---look in /Users/wilja/iu/mk/alphatap/aziz tree-subst (slow)]

% [advantages and disadvantages]

% [can we still use the clever approach of implementing disequality
% constraints with this representation?]

% [this is Aziz's code.  make sure he is okay with including the code in
% my dissertation]

% \begin{schemedisplay}
% (library (mk)
%   (export run == ==-check exist conde conda condu project var)
%   (import (ikarus) (rnrs) (tree))

%   (define-syntax lambdag@
%     (syntax-rules ()
%       ((_ (s) e) (lambda (s) e))))

%   (define-syntax lambdaf@
%     (syntax-rules ()
%       ((_ () e) (lambda () e))))

%   (define-syntax rhs
%     (syntax-rules ()
%       ((_ x) (cdr x))))

%   (define-syntax lhs
%     (syntax-rules ()
%       ((_ x) (car x))))

%   (define-syntax size-s
%     (syntax-rules ()
%       ((_ x) (t:size x))))

%   (define counter -1)

%   (define var
%     (lambda (x)
%       (set! counter (add1 counter))
%       (vector x counter)))

%   (define-syntax var?
%     (syntax-rules ()
%       ((_ x) (vector? x))))

%   (define empty-s '())

%   (define var-idx
%     (lambda (x)
%       (vector-ref x 1)))

%   (define walk
%     (lambda (v s)
%       (cond
%         ((var? v)
%          (cond
%            ((t:lookup (var-idx v) s) =>
%             (lambda (a)
%               (walk (t:binding-value a) s)))
%            (else v)))
%         (else v))))

%   (define ext-s
%     (lambda (x v s)
%       (t:bind (var-idx x) v s)))

%   (define unify
%     (lambda (v w s)
%       (let ((v (walk v s))
%             (w (walk w s)))
%         (cond
%           ((eq? v w) s)
%           ((var? v) (ext-s v w s))
%           ((var? w) (ext-s w v s))
%           ((and (pair? v) (pair? w))
%            (let ((s (unify (car v) (car w) s)))
%              (and s (unify (cdr v) (cdr w) s))))
%           ((equal? v w) s)
%           (else #f)))))

%   (define unify-check
%     (lambda (u v s)
%       (let ((u (walk u s))
%             (v (walk v s)))
%         (cond
%           ((eq? u v) s)
%           ((var? u) (ext-s-check u v s))
%           ((var? v) (ext-s-check v u s))
%           ((and (pair? u) (pair? v))
%            (let ((s (unify-check
%                       (car u) (car v) s)))
%              (and s (unify-check
%                       (cdr u) (cdr v) s))))
%           ((equal? u v) s)
%           (else #f)))))

%   (define ext-s-check
%     (lambda (x v s)
%       (cond
%         ((occurs-check x v s) #f)
%         (else (ext-s x v s)))))

%   (define occurs-check
%     (lambda (x v s)
%       (let ((v (walk v s)))
%         (cond
%           ((var? v) (eq? v x))
%           ((pair? v)
%            (or
%              (occurs-check x (car v) s)
%              (occurs-check x (cdr v) s)))
%           (else #f)))))

%   (define walk*
%     (lambda (w s)
%       (let ((v (walk w s)))
%         (cond
%           ((var? v) v)
%           ((pair? v)
%            (cons
%              (walk* (car v) s)
%              (walk* (cdr v) s)))
%           (else v)))))

%   (define reify-s
%     (lambda (v s)
%       (let ((v (walk v s)))
%         (cond
%           ((var? v)
%            (ext-s v (reify-name (size-s s)) s))
%           ((pair? v) (reify-s (cdr v)
%                        (reify-s (car v) s)))
%           (else s)))))

%   (define reify-name
%     (lambda (n)
%       (string->symbol
%         (string-append "_" "." (number->string n)))))

%   (define reify
%     (lambda (v s)
%       (let ((v (walk* v s)))
%         (walk* v (reify-s v empty-s)))))

%   (define ==-check
%     (lambda (v w)
%       (lambdag@ (s)
%         (unify-check v w s))))

%   (define-syntax mzero
%     (syntax-rules () ((_) #f)))
%   (define-syntax inc
%     (syntax-rules () ((_ e) (lambdaf@ () e))))
%   (define-syntax unit
%     (syntax-rules () ((_ a) a)))
%   (define-syntax choice
%     (syntax-rules () ((_ a f) (cons a f))))

%   (define-syntax case-inf
%     (syntax-rules ()
%       ((_ e (() e0) ((f^) e1) ((a^) e2) ((a f) e3))
%        (let ((a-inf e))
%          (cond
%            ((not a-inf) e0)
%            ((procedure? a-inf)  (let ((f^ a-inf)) e1))
%            ((not (and (pair? a-inf)
%                       (procedure? (cdr a-inf))))
%             (let ((a^ a-inf)) e2))
%            (else (let ((a (car a-inf)) (f (cdr a-inf)))
%                    e3)))))))

%   (define-syntax run
%     (syntax-rules ()
%       ((_ n (x) g0 g ...)
%        (take n
%          (lambdaf@ ()
%            ((exist (x) g0 g ...
%               (lambdag@ (s)
%                 (cons (reify x s) '())))
%             empty-s))))))

%   (define take
%     (lambda (n f)
%       (if (and n (zero? n))
%         '()
%         (case-inf (f)
%           (() '())
%           ((f) (take n f))
%           ((a) a)
%           ((a f)
%            (cons (car a)
%              (take (and n (- n 1)) f)))))))

%   (define ==
%     (lambda (u v)
%       (lambdag@ (s)
%         (unify u v s))))

%   (define-syntax exist
%     (syntax-rules ()
%       ((_ (x ...) g0 g ...)
%        (lambdag@ (s)
%          (inc
%            (let ((x (var 'x)) ...)
%              (bind* (g0 s) g ...)))))))

%   (define-syntax bind*
%     (syntax-rules ()
%       ((_ e) e)
%       ((_ e g0 g ...) (bind* (bind e g0) g ...))))

%   (define bind
%     (lambda (a-inf g)
%       (case-inf a-inf
%         (() (mzero))
%         ((f) (inc (bind (f) g)))
%         ((a) (g a))
%         ((a f) (mplus (g a) (lambdaf@ () (bind (f) g)))))))

%   (define-syntax conde
%     (syntax-rules ()
%       ((_ (g0 g ...) (g1 g^ ...) ...)
%        (lambdag@ (s)
%          (inc
%            (mplus*
%              (bind* (g0 s) g ...)
%              (bind* (g1 s) g^ ...) ...))))))

%   (define-syntax mplus*
%     (syntax-rules ()
%       ((_ e) e)
%       ((_ e0 e ...) (mplus e0
%                       (lambdaf@ () (mplus* e ...))))))

%   (define mplus
%     (lambda (a-inf f)
%       (case-inf a-inf
%         (() (f))
%         ((f^) (inc (mplus (f) f^)))
%         ((a) (choice a f))
%         ((a f^) (choice a (lambdaf@ () (mplus (f) f^)))))))

%   (define-syntax conda
%     (syntax-rules ()
%       ((_ (g0 g ...) (g1 g^ ...) ...)
%        (lambdag@ (s)
%          (inc
%            (ifa ((g0 s) g ...)
%                 ((g1 s) g^ ...) ...))))))

%   (define-syntax ifa
%     (syntax-rules ()
%       ((_) (mzero))
%       ((_ (e g ...) b ...)
%        (let loop ((a-inf e))
%          (case-inf a-inf
%            (() (ifa b ...))
%            ((f) (inc (loop (f))))
%            ((a) (bind* a-inf g ...))
%            ((a f) (bind* a-inf g ...)))))))

%   (define-syntax condu
%     (syntax-rules ()
%       ((_ (g0 g ...) (g1 g^ ...) ...)
%        (lambdag@ (s)
%          (inc
%            (ifu ((g0 s) g ...)
%                 ((g1 s) g^ ...) ...))))))

%   (define-syntax ifu
%     (syntax-rules ()
%       ((_) (mzero))
%       ((_ (e g ...) b ...)
%        (let loop ((a-inf e))
%          (case-inf a-inf
%            (() (ifu b ...))
%            ((f) (inc (loop (f))))
%            ((a) (bind* a-inf g ...))
%            ((a f) (bind* (unit a) g ...)))))))

%   (define-syntax project
%     (syntax-rules ()
%       ((_ (x ...) g g* ...)
%        (lambdag@ (s)
%          (let ((x (walk* x s)) ...)
%            ((exist () g g* ...) s))))))

% )
% \end{schemedisplay}

% \begin{schemedisplay}
% (library (tree)
%   (export t:bind t:unbind t:lookup t:binding-value t:size)
%   (import (rnrs) (rnrs records syntactic) (ikarus))

% ;  (import scheme)
%   ;;; subst ::= (empty)
%   ;;;         | (node even odd)
%   ;;;         | (data idx val)

%   (define-record-type node (fields e o))

%   (define-record-type data (fields idx val))

%   (define shift (lambda (n) (fxsra n 1)))

%   (define unshift (lambda (n i) (fx+ (fxsll n 1) i)))

%   (define t:size
%     (lambda (x) (size x)))

%   (define t:bind
%     (lambda (xi v s)
%       (unless (and (fixnum? xi) (>= xi 0))
%         (error 't:bind "index must be a fixnum, got ~s" xi))
%       (bind xi v s)))

%   (define t:unbind
%     (lambda (xi s)
%       (unless (and (fixnum? xi) (>= xi 0))
%         (error 't:unbind "index must be a fixnum, got ~s" xi))
%       (unbind xi s)))

%   (define t:lookup
%     (lambda (xi s)
%       (unless (and (fixnum? xi) (>= xi 0))
%         (error 't:lookup "index must be a fixnum, got ~s" xi))
%       (lookup xi s)))

%   (define t:binding-value
%     (lambda (s)
%       (unless (data? s)
%         (error 't:binding-value "not a binding ~s" s))
%       (data-val s)))

%   (define push
%     (lambda (xi vi xj vj)
%       (if (fxeven? xi)
%           (if (fxeven? xj)
%               (make-node (push (shift xi) vi (shift xj) vj) '())
%               (make-node (make-data (shift xi) vi) (make-data (shift xj) vj)))
%           (if (fxeven? xj)
%               (make-node (make-data (shift xj) vj) (make-data (shift xi) vi))
%               (make-node '() (push (shift xi) vi (shift xj) vj))))))

%   (define bind
%     (lambda (xi vi s*)
%       (cond
%        [(node? s*)
%         (if (fxeven? xi)
%             (make-node (bind (shift xi) vi (node-e s*)) (node-o s*))
%             (make-node (node-e s*) (bind (shift xi) vi (node-o s*))))]
%        [(data? s*)
%         (let ([xj (data-idx s*)] [vj (data-val s*)])
%           (if (fx= xi xj)
%               (make-data xi vi)
%               (push xi vi xj vj)))]
%        [else (make-data xi vi)])))

%   (define lookup
%     (lambda (xi s*)
%       (cond
%        [(node? s*)
%         (if (fxeven? xi)
%             (lookup (shift xi) (node-e s*))
%             (lookup (shift xi) (node-o s*)))]
%        [(data? s*)
%         (if (fx= (data-idx s*) xi)
%             s*
%             #f)]
%        [else #f])))

%   (define size
%     (lambda (s*)
%       (cond
%        [(node? s*) (fx+ (size (node-e s*)) (size (node-o s*)))]
%        [(data? s*) 1]
%        [else 0])))

%   (define cons^
%     (lambda (e o)
%       (cond
%        [(or (node? e) (node? o)) (make-node e o)]
%        [(data? e)
%         (make-data (unshift (data-idx e) 0) (data-val e))]
%        [(data? o)
%         (make-data (unshift (data-idx o) 1) (data-val o))]
%        [else '()])))

%   (define unbind
%     (lambda (xi s*)
%       (cond
%        [(node? s*)
%         (if (fxeven? xi)
%             (cons^ (unbind (shift xi) (node-e s*)) (node-o s*))
%             (cons^ (node-e s*) (unbind (shift xi) (node-o s*))))]
%        [(and (data? s*) (fx= (data-idx s*) xi)) '()]
%        [else s*])))
% )
% \end{schemedisplay}

% \section{Performance}

% [compare mk's performance using different walk algorithms, with
% benchmarks based on applications in the dissertation]

% [do I want an entire chapter on performance?  if so, might need to
% incorporate performance sections on walk, alphatap, and alphaleantap]


% Include Preamble
\include{preamble}
\begin{document}
\maketitle
% Sectuion Title
\section{Section--1.1: Introduction to Biostatistics}

% Slide-Statistics
\begin{frame}[t]{Statistics and Biostatistics: Definitions--1}
	\textbf{Definition--1: What is Statistics?} \\
	Statistics is the discipline that concerns the collection, organization,
	analysis, interpretation and presentation of data. In applying statistics
	to a scientific, industrial, or social problem, it is conventional to begin
	with a statistical population or a statistical model to be studied.
	(Source: \url{https://en.wikipedia.org/wiki/Statistics})

	\textbf{Definition--2: What is Statistics?}\\
	Simply, Statistics is a branch of mathematics that deals with collecting,
	organizing, analyzing, and interpreting data.
	(Source:
	\url{https://app.pluralsight.com/library/courses/interpreting-data-descriptive-statistics-python/})

\end{frame}

\begin{frame}[t]{Statistics and Biostatistics: Definitions--2}
	\textbf{Definition--3: What is Biostatistics?} \\
	Biostatistics is the application of statistics to a variety of topics in
	biology. In this course, we tend to focus on biological topics in the
	health sciences as we learn about statistics.
	(Source: \url{https://bolt.mph.ufl.edu/6050-6052/}) \\

	\vspace{6pt}

	\textbf{Definition--4: What is Biostatistics?} \\
	Biostatistics is the application of statistics to problems in the
	biological sciences, health, and medicine.
	(Source:
	\url{https://ocw.jhsph.edu/index.cfm/go/viewCourse/course/MethodsInBiostatisticsI/})

\end{frame}

% Slide-Data-1
\begin{frame}[t]{What is Data?}
	\begin{itemize}
		\item \textbf{Definition--1:} Data is a collection of facts, such as
		numbers,
		images, words,
		measurements, observations, audios, videos or just descriptions of
		things.
		\item \textbf{Definition--2:} Data is a tool to reach suitable
		conclusion.
	\end{itemize}

\begin{figure} [ht]
	\centering
	\includegraphics[width=0.8\textwidth]{stats_img/data.png}
	\source{\url{https://www.edureka.co/blog/statistics-and-probability/}}
\end{figure}
\end{frame}

% Slide-Data-2
\begin{frame}[t]{Data can be Numbers}
	\begin{figure} [ht]
		\centering
		\includegraphics[width=0.95\textwidth]{img/nchs}
	\end{figure}
	\centering
	\textit{Source:} \url{https://www.cdc.gov/nchs/nhanes/index.htm}
\end{frame}

% Slide-Data-3
\begin{frame}[t]{Data can be Images}
	\centering\tiny
	\begin{columns}[T]
		\column{0.32\linewidth}
		\includegraphics[height=3cm, width=3cm]{img/gaussian} \\ (Filtered
		Image)

		\column{0.32\linewidth}
		\includegraphics[height=3cm, width=3cm]{img/median} \\ (Filtered Image)
	\end{columns}

	\vspace{10pt}
	\centering\tiny
	\begin{columns}[T]
		\column{0.32\linewidth}
		\includegraphics[height=3cm, width=3cm]{img/nlm} \\ (Noisy Image)

		\column{0.32\linewidth}
		\includegraphics[height=3cm, width=3cm]{img/noisy_img} \\ (Noisy Image)
	\end{columns}
	\textit{Source:} \url{https://www.cdc.gov/nchs/nhanes/index.htm}
\end{frame}

% Slide-Data-4
\begin{frame}[t]{Data can be Words}
\centering\tiny
\begin{columns}[T]
	\column{0.32\linewidth}
	\includegraphics[height=3cm, width=3cm]{words/1.jpg} \\

	\column{0.32\linewidth}
	\includegraphics[height=3cm, width=3cm]{words/2.jpg} \\
\end{columns}

\vspace{10pt}
\centering\tiny
\begin{columns}[T]
	\column{0.32\linewidth}
	\includegraphics[height=3cm, width=3cm]{words/3.jpg} \\

	\column{0.32\linewidth}
	\includegraphics[height=3cm, width=3cm]{words/4.jpg} \\
\end{columns}
\textit{Source:}
\url{https://www.healthit.gov/topic/health-it-and-health-information-exchange-basics/health-it-and-health-information-exchange}
\end{frame}

% Slide-n
\begin{frame}[t]{Types of Data}
	\begin{figure} [ht] \vspace{20pt}
		\centering
		\includegraphics[width=0.98\textwidth]{stats_img/types_of_data.png}
	\end{figure}
\end{frame}

% Slide-n
\begin{frame}[t]{Qualitative  or Categorical Data}
	\begin{itemize}
		\item Classifies individuals or items into different groups.
		\item Qualitative data is further divided into two types of data
		\begin{itemize}
			\item[--] \textbf{Ordinal:} groups have an order or ranking.
			\item [--]\textbf{Nominal:} groups are merely names, no ranking.
		\end{itemize}
	\end{itemize}

		\begin{figure}[h]
			\centering
			\begin{subfigure}{0.45\textwidth}
				\centering
				\includegraphics[width=3.5cm,
				height=3.5cm]{stats_img/ordinal.png}
				\caption{Ordinal Data}
			\end{subfigure}
			\hfil
			\begin{subfigure}{0.45\textwidth}
				\centering
				\includegraphics[width=3.5cm,
				height=3.5cm]{stats_img/nominal.png}
				\caption{Nominal Data}
			\end{subfigure}
			\source{\url{https://www.edureka.co/blog/statistics-and-probability/}}
		\end{figure}
\end{frame}

\begin{frame}[t]{Quantitative or Numeric Data}
	\begin{itemize}
		\item Numerical, measurable quantities in which arithmetic
		operations often make sense.
		\item Quantitative data is also further divided into two types of data
		\begin{itemize}
			\item \textbf{Continuous:} could take on any value within an
			interval,many possible values.
			\begin{itemize}
				\item[--] A person's height: could be any value (within the
				range
				of human heights), not just certain fixed heights.
				\item[--] Time in a race: you could even measure it to
				fractions of
				a second.
				\item [--]Blood pressure, mmHg.
				\item [--]Weight, pounds (kilograms, ounces, etc.)
			\end{itemize}
			\item \textbf{Discrete:} countable value, finite number of values.
			\begin{itemize}
				\item[--] The number of students in a class.
				\item[--] The results of rolling a die.
			\end{itemize}
	\end{itemize}
	\end{itemize}
\end{frame}

% Slide-n
\begin{frame}[t]{Binary Data}
	\begin{itemize}
		\item Yes/No
		\item Polio: Yes/No
		\item Cure: Yes/No
		\item Sex: Female/Male(0 or 1)
	\end{itemize}
\end{frame}

\begin{frame}[t]{Types of Variables}
	\begin{itemize}
		\item \textbf{Independent Variable(IV)} \\
		A variable whose value does not change by the effect of other variables
		and is used to manipulate the dependent variables. It is often denoted
		as $X$.
		\item \textbf{Dependent Variable(DV)}
		A variable whose value change when there is any manipulation in the
		values of independent variables. Is is often denoted as $Y$
	\end{itemize}
\centering
\textbf{$X$ Causes $Y$} \\
$X$ (effect) $\rightarrow$ Year of Experience $\rightarrow$ Independent \\
$Y$ (cause) $\rightarrow$ Salary $\rightarrow$ Dependent
\end{frame}

% Slide-n
\begin{frame}[t]{Other Names for IV and DV}
	\textbf{Other Names for Independent Variables}
	\begin{itemize}
		\item Explanatory Variables (they explain an event or outcome)
		\item Predictor Variables (they can be used to predict the value of a
		dependent variable)
	\end{itemize}
	\textbf{Other Names for Dependent Variables}
	\begin{itemize}
	\item Response Variables (they respond to a change in another variable)
	\item Outcome Variables (they represent the outcome you want to measure)
 \end{itemize}
\end{frame}

% Slide-n
\begin{frame}[t]{A Typical Dataset}
	\begin{figure} [ht]
		\centering
		\includegraphics[width=0.5\textwidth]{stats_img/dataset.png}
	\end{figure}

\begin{itemize}
	\item \textbf{Variables} contain the information about a particular
	characteristic
	for all individuals in a dataset.
	\item An \textbf{observation} in statistics is a value of something of
	interest you're measuring or counting during a study or experiment: a
	person's height, a bank account value at a certain point in time, or number
	of animals.
\end{itemize}

\end{frame}

% Slide-Soures of Data
\begin{frame}[t]{Sources of Data}
\begin{columns}[T]
	\column{0.32\linewidth}
	\fbox{\textbf{Primary Sources of Data}} \\
	\begin{itemize}
		\item Collection of data from source of origin.
		\item Conducting interviews, experimentation.
		\item Provide first hand information.
	\end{itemize}
	\column{0.32\linewidth}
   \fbox{\textbf{Secondary Sources of Data}} \\
  \begin{itemize}
  	\item Collection of data from agency which already has collected data and
  	processed it.
  	\item Conducting interviews, experimentation
  \end{itemize}
\end{columns}
\end{frame}

% Slide-Soures of Data
\begin{frame}[t]{Pros and Cons of Primary Data}
	\begin{columns}[T]
		\column{0.32\linewidth}
		\fbox{\textbf{Pros}} \\
		\begin{itemize}
			\item Can be collected to answer your specific research question.
			\item You have control over the sampling and measurement methods.
		\end{itemize}
		\column{0.32\linewidth}
		\fbox{\textbf{Cons}} \\
		\begin{itemize}
			\item More expensive and time-consuming to collect.
			\item Requires training in data collection methods.
		\end{itemize}
	\end{columns}
\end{frame}

\begin{frame}[t]{Pros and Cons of Secondary Data}
	\begin{columns}[T]
		\column{0.32\linewidth}
		\fbox{\textbf{Pros}} \\
		\begin{itemize}
			\item Easier and faster to access.
			\item You can collect data that spans longer timescales and broader
			geographical locations.
		\end{itemize}
		\column{0.32\linewidth}
		\fbox{\textbf{Cons}} \\
		\begin{itemize}
			\item No control over how data was generated.
			\item Requires extra processing to make sure it works for your
			analysis.
		\end{itemize}
	\end{columns}
\end{frame}

% Slide-n
\begin{frame}[t]{Data Collection Methods}
	\begin{itemize}
		\item Interviews
		\item Questionnaires and surveys
		\item Observations
		\item Focus groups
		\item Oral histories
	\end{itemize}
See More \url{https://www.jotform.com/data-collection-methods/}
\end{frame}

% Slide-n
\begin{frame}[t]{Terms used in Data Collection}
	\begin{itemize}
		\item \textbf{Variable} – values which changes
		\item \textbf{Observations} – values from variables are referred as
		observations.
		\item \textbf{Statistical Investigation} – search for information
		conducted by using statistical methods.
		\item \textbf{text} – who conducts statistical inquiry.
		\item ITEM 5
	\end{itemize}
\end{frame}

% Slide-2.4
\begin{frame}[t]{Important Agencies for Secondary Data}
\begin{itemize}
	\item \url{https://dghs.gov.bd/index.php/en/data}
	\item \url{https://framinghamheartstudy.org/}
	\item \url{https://www.data.gov/}
	\item \url{https://healthdata.gov/}
	\item \url{https://www.who.int/ictrp/network/trds/en/}
	\item \url{https://data.cdc.gov/}
	\item \url{https://www.kaggle.com/}
\end{itemize}
\end{frame}

\begin{frame}[t]{Terminologies In Statistics: Population and Sample}

	\textbf{Population:} The population is the entire group that you want to
	draw conclusions about.

	\textbf{Sample:} The sample is the specific group of individuals that you
	will collect data from.

	\begin{figure} [ht]
		\centering
		\includegraphics[width=0.5\textwidth]{stats_img/population_sample.png}
		\source{\url{https://online.stat.psu.edu/stat500/}}
	\end{figure}
\end{frame}

\begin{frame}[t]{Terminologies In Statistics: Sampling Frame and Sample Size}

	\textbf{Sampling Frame} \\
	The sampling frame is the actual list of
	individuals that the sample will be drawn from. Ideally, it should include
	the entire target population (and nobody who is not part of that
	population).

	\textbf{Sample Size} \\
	The number of individuals in your sample depends on
	the size of the population, and on how precisely you want the results to
	represent the population as a whole.

	\textbf{Sample Size Calculator} \\
	Surveymonkey--\url{https://www.surveymonkey.com/mp/sample-size-calculator/}

\end{frame}

% Slide-n
\begin{frame}[t]{Characteristics of a Good Sample--1}
	\begin{itemize}
		\item \textbf{Goal-oriented:} A sample should be goal oriented. It
		should be oriented to the research objectives and fitted to the survey
		conditions. \pause
		\item \textbf{Acurate representative of the population:} A sample
		should be an accurate representative of the population from which it is
		taken. \pause
		\item \textbf{Proportional:} A sample should be proportional. It should
		be large enough to represent the population properly. In general, the
		larger the sample size, the more accurately and confidently
		you can make inferences about the whole population.\pause

		\item \textbf{Random Selection:} A sample should be selected at random.
		This means that any item in the group has a full and equal chance of
		being selected and included in the sample.This makes the selected
		sample truly representative in character. \pause
		\item \textbf{Economical:} A sample should be economical.The objective
		of the survey should be achieved with minimum cost and effort.

	\end{itemize}
\end{frame}

\begin{frame}[t]{Characteristics of a Good Sample--2}
	\begin{itemize}
		\item \textbf{Practical:} A sample should be practical. The sample
		design should be simple. It should be capable of being understood and
		followed in the fieldwork. \pause
		\item \textbf{Actual information provider:} A sample should be designed
		so as to provide actual information required for the study and also
		provide an adequate basis for the measurement of its own reliability.
		\pause
	\end{itemize}
\end{frame}

\begin{frame}[t]{Types of Statistics}
	\say{There are two kinds of statistics,
		the kind you look up and the kind
		you make up} \\
	    --Rex Stout
	\begin{itemize}
		\item \textbf{Descriptive Statistics} -- Identify important elements in
		a
		dataset.
		\item \textbf{Inferential Statistics} -- Explain those elements via
		relationships with other elements.
	\end{itemize}
\end{frame}

% Slide-n
\begin{frame}[t]{Descriptive Statistics}
	\textbf{Descriptive statistical} methods provide an exploratory assessment
	of the data from a study.
	\begin{itemize}
		\item Descriptive statistical methods provide a exploratory data
		analysis.
		\begin{itemize}
			\item[--] Frequency Distribution Table
			\item[--] Graphs / Charts
			\item [--] Summary
		\end{itemize}
		\item Descriptive statistical methods divide into 3 categories.
		\begin{itemize}
			\item[--] \textbf{Univariate analysis} summarize only one variable
			at a
			time.
			\item[--] \textbf{Bivariate analysis}  compare two variables.
			\item [--]\textbf{Multivariate analysis} compare more than two
			variables.
		\end{itemize}
	\end{itemize}
\end{frame}

% Slide-n
\begin{frame}[t]{Inferential Statistics}
	\textbf{Assess the strength of evidence} for/against a hypothesis; evaluate
	the data
	\begin{itemize}
		\item Inferential statistical methods provide a confirmatory data
		analysis
		\begin{itemize}
			\item [--]Generalize conclusions from data from part of a group
			(sample) to the whole group
			(population)
			\item[--] Assess the strength of the evidence
			\item [--]Make comparisons
			\item [--]Make predictions
		\end{itemize}
	\item Inferential statistical methods divide into 2 categories.
	\begin{itemize}
		\item[--] \textbf{ Hypothesis Testing:} Hypothesis testing is a formal
		procedure for investigating our ideas about the world using statistics.
		It is most often used by scientists to test specific predictions,
		called hypotheses, that arise from theories.
		\item[--] \textbf{Model Fitting:} Model fitting is a measure of how
		well a statistical learning model generalizes to similar data to that
		on which it was trained. A model that is well-fitted produces more
		accurate outcomes.
	\end{itemize}
	\end{itemize}
\end{frame}

% Slide-n
\begin{frame}[t]{References}
	\begin{itemize}
		\item \url{https://bolt.mph.ufl.edu/6050-6052/}
		\item \url{https://online.stat.psu.edu/stat500/}
		\item \url{https://online.stat.psu.edu/stat100/}
		\item \url{https://online.stat.psu.edu/stat200/}
	\end{itemize}
\end{frame}

% Thank you slide
\plain{Thank You\\ \ \\ \Huge{\smiley}}

\end{document}


\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{amsmath}

\usepackage[hmargin=3cm,vmargin=6.0cm]{geometry}
%\topmargin=0cm
\topmargin=-2cm
\addtolength{\textheight}{6.5cm}
\addtolength{\textwidth}{2.0cm}
%\setlength{\leftmargin}{-5cm}
\setlength{\oddsidemargin}{0.0cm}
\setlength{\evensidemargin}{0.0cm}

\newcommand{\HRule}{\rule{\linewidth}{1mm}}

%misc libraries goes here
\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\begin{document}

\noindent
\HRule \\[3mm]
\begin{flushright}

                                         \LARGE \textbf{CENG 222}  \\[4mm]
                                         \Large Statistical Methods for Computer Engineering \\[4mm]
                                        \normalsize      Spring '2018-2019 \\
                                           \Large   Homework 1 \\
\end{flushright}
\HRule

\section*{Student Information }
%Write your full name and id number between the colon and newline
%Put one empty space character after colon and before newline
Full Name : Yavuz Selim Yesilyurt \\
Id Number : 2259166

% Write your answers below the section tags
\section*{Answer 2.15}
Let $A$ is the event that there is an error in the first block and let $B$ is the event that there is an error in the second block and $C$ is the event that the computer program returns error. Given that $P\{A\}= 0.2$ and $P\{B\} = 0.3$ and the events $A$ and $B$ are independent. The probability that the computer program will return error is $P\{A \cup B\}$ and it can be found adding all the cases that can bring an error and subtracting their intersection, namely we need to add probability that first block contains an error whereas second one does not, probability that second block contains an error whereas first one does not and we need to subtract the probability that both of them contains error:

\begin{align*}
P\{A \cup B\} &= P\{A\} + P\{B\} - P\{A \cap B\} \\
&= 0.2 + 0.3 - 0.2 \times 0.3 \\
&= 0.44
\end{align*}

Then by definition of conditional probability:

\begin{align*}
P\{A \cap B \ | \ A \cup B\} &= \frac{P\{(A \cap B) \cap (A \cup B) \}}{P\{A \cup B\}}  \\
&= \frac{P\{A \cap B \}}{P\{A \cup B\}}  \\
&= \frac{0.06}{0.44} \\
&= 0.1364
\end{align*}

\section*{Answer 2.20}
Let $U$ denotes the event that an athlete uses steroids and let $P$ denotes the test gives a positive result. Then the given probabilities can be rewritten as:
\begin{center}
$P\{P\ |\ U\} = 0.9$ and $P\{P\ |\ \bar{U}\} = 0.02$ and $P\{U\} = 0.05$
\end{center}
We are required to find $P\{U\ | \ \bar{P}\}$. From the Bayes's rule, we have:
\begin{center}
$P\{U\ | \ \bar{P}\} = \frac{P\{\bar{P}\ |\ U\}\times P\{U\}}{P\{\bar{P}\}}$
\end{center}
We know that, from the law of total probability, $P\{\bar{P}\}$ can be rewritten as:
\begin{center}
$P\{\bar{P}\ |\ U\}\times P\{U\} \times P\{\bar{P}\ |\ \bar{U}\}\times P\{\bar{U}\}$
\end{center}
So:
\begin{align*}
P\{U\ | \ \bar{P}\} &= \frac{P\{\bar{P}\ |\ U\}\times P\{U\}}{P\{\bar{P}\}}  \\\\
&= \frac{P\{\bar{P}\ |\ U\}\times P\{U\}}{P\{\bar{P}\ |\ U\}\times P\{U\} \times P\{\bar{P}\ |\ \bar{U}\}\times P\{\bar{U}\}}  \\\\
&= \frac{0.1 \times 0.05}{0.1 \times 0.05 + 0.98 \times 0.95} \\\\
&= 0.00534
\end{align*}

\section*{Answer 2.29}

Let $A$ be the event that the database contain the given keyword. Then $P\{A\}=\frac{5}{9}$ and event that the database does not contain the given keyword is $P\{\bar{A}\}=\frac{4}{9}$ \\

Probability that the keyword will be found in at least 2 of the first 4 searched databases can be found by examining all the favorable outcomes, adding them up and then dividing them to total number of outcomes, namely: \\

 = finding in exactly 2 of the first 4 searched databases + finding in exactly 3 of the first 4 searched databases + finding in exactly 4 of the first 4 searched databases $\div$ choosing 4 databases to search for word out of 9 databases \\

Namely:

\begin{align*}
&= \frac{C(5,2)\times C(4,2) + C(5,3) \times C(4,1) + C(5,4)}{C(9,4)} \\
&= \frac{105}{126} \\\\
&= 0.833
\end{align*}

\end{document}


\chapter{Iteration}
\label{iteration}

This chapter is about iteration, which is the ability to run
a block of statements repeatedly.  We saw a kind of iteration,
using recursion, in Section~\ref{recursion}.
We saw another kind, using a {\tt for} loop,
in Section~\ref{for_loops}.  In this chapter we'll see yet another
kind, using a {\tt while} statement.
But first we want to say a little more about variable assignment.

\section{Assignment versus equality}
\index{assignment!operator}
\index{equality operator}

Before going further, I want to address a common source of confusion.
Because Perl uses the equal sign ({\tt =}) for assignment, it is
tempting to interpret a statement like {\tt \$a = \$b} as a
mathematical proposition of equality; that is, the claim that
{\tt \$a} and {\tt \$b} are equal.  But this interpretation is
wrong.
\index{equality and assignment}

First, equality is a symmetric relationship and assignment is not.  For
example, in mathematics, if $a=7$ then $7=a$.  But in Perl, the
statement {\tt \$a = 7} is legal and {\tt 7 = \$a} is not.

Also, in mathematics, a proposition of equality is either true or
false for all time.  If $a=b$ now, then $a$ will always equal $b$.
In Perl, an assignment statement can make two variables equal, but
they don't have to stay that way:

\begin{verbatim}
> my $a = 5;
5
> my $b = $a;   # $a and $b are now equal
5
> $a = 3;       # $a and $b are no longer equal
3
> say $b;
5
\end{verbatim}
%
The third line changes the value of {\tt \$a} but does not
change the value of {\tt \$b}, so they are no longer equal.

In brief, remember that {\tt =} is an assignment operator and
not an equality operator; the operators for testing equality
between two terms are {\tt ==} for numbers and {\tt eq} for strings.

\section{Reassignment}
\index{assignment}
\index{statement!assignment}
\index{reassignment}

As you may have discovered, it is legal to make more than one
assignment to the same variable.  A new assignment makes an existing
variable refer to a new value (and stop referring to the old value).

\begin{verbatim}
> my $x = 5;
5
> say $x;
5
> $x = 7;
7
> say $x
7
\end{verbatim}
%
The first time we display
{\tt \$x}, its value is 5; the second time, its
value is 7.

Figure~\ref{fig.assign2} shows what {\bf reassignment} looks
like in a state diagram. \index{state diagram} \index{diagram!state}

Reassigning variables is often useful, but you should use this feature
with some caution.  If the values of variables change frequently, it can
make the code difficult to read and debug.

\begin{figure}
\centerline
{\includegraphics[scale=0.5]{figs/reassignment.png}}
\caption{State diagram.}
\label{fig.assign2}
\end{figure}

\section{Updating variables}
\label{update}

\index{update}
\index{variable!updating}

A common kind of reassignment is an {\bf update},
where the new value of the variable depends on the old.

\begin{verbatim}
> $x = $x + 1;
\end{verbatim}
%
This means ``get the current value of {\tt \$x}, add one, and then
update {\tt \$x} with the new value.''

If you try to update a variable that has not been given a value,
you get a warning, because Perl evaluates the right side
of the assignment statement before it assigns a value to {\tt \$x}:

\begin{verbatim}
> my $x;
> $x = $x + 1;
Use of uninitialized value of type Any in numeric context
 in block <unit> at <unknown file> line 1
\end{verbatim}
%
Before you can update a variable, you have to declare it and
to {\bf initialize} it, usually with an assignment:
\index{initialization (before update)}

\begin{verbatim}
> my $x = 0;
> $x = $x + 1;
\end{verbatim}
%
Updating a variable by adding 1 is called an {\bf increment};
subtracting 1 is called a {\bf decrement}.
\index{increment operator}
\index{decrement operator}

As mentioned earlier in Section~\ref{expr_and_statements},
Perl has some shortcuts for increment and decrement :

\begin{verbatim}
$x += 1; # equivalent to $x = $x + 1
$x++;    # also equivalent

$x -= 1; # equivalent to $x = $x - 1
$x--;    # also equivalent
\end{verbatim}

\section{The {\tt while} statement}
\index{statement!while}
\index{while loop}
\index{loop!while}
\index{iteration}

Computers are often used to automate repetitive tasks.  Repeating
identical or similar tasks without making errors is something that
computers do well and people do poorly.  In a computer program,
repetition is also called {\bf iteration}.

We have already seen two functions, {\tt countdown} and
\verb"print-n-times", that iterate using recursion
(Section~\ref{recursion}.  Because
iteration is so common, most programming languages including
Perl provide language features to make it easier.
One is the {\tt for} statement we saw in Section~\ref{for_loops}.
We'll get back to that later.

Another is the {\tt while} statement.  Here is a version of {\tt
countdown} that uses a {\tt while} statement:

\begin{verbatim}
sub countdown(Int $n is copy) {
    while $n > 0 {
        say $n;
        $n--;
    }
    say 'Blastoff!';
}
\end{verbatim}
%
You can almost read the {\tt while} statement as if it were English.
It means, ``While {\tt \$n} is greater than 0,
display the value of {\tt n} and then decrement
{\tt \$n}.  When you get to 0, display the word {\tt Blastoff!}''
\index{flow of execution}

More formally, here is the flow of execution for a {\tt while} statement:

\begin{enumerate}

\item Determine whether the condition is true or false.

\item If false, exit the {\tt while} statement
and continue execution at the next statement.

\item If the condition is true, run the
body and then go back to step 1.

\end{enumerate}

This type of flow is called a loop because the third step
loops back around to the top.
\index{condition}
\index{loop}
\index{body}

The body of the loop should change the value of one or more variables
so that the condition becomes false eventually and the loop
terminates.  Otherwise, the loop will repeat forever, which is called
an {\bf infinite loop}.  An endless source of amusement for computer
scientists is the observation that the directions on shampoo,
``Lather, rinse, repeat'', are an infinite loop.
\index{infinite loop}
\index{loop!infinite}

In the case of {\tt countdown}, we can prove that the loop
terminates: if {\tt \$n} is zero or negative, the loop never runs.
Otherwise, {\tt \$n} gets smaller each time through the
loop, so eventually we have to get to 0.

For some other loops, it is not so easy to tell whether the
loop terminates.  For example:

\begin{verbatim}
sub sequence($n is copy) {
    while $n != 1 {
        say $n;
        if $n %% 2 {        # $n is even
            $n = $n / 2;
        } else {            # $n is odd
            $n = $n*3 + 1
        }
    }
    return $n;
}
\end{verbatim}
%
The condition for this loop is {\tt \$n != 1}, so the loop will continue
until {\tt \$n} is {\tt 1}, which makes the condition false.

Each time through the loop, the program outputs the value of {\tt \$n}
and then checks whether it is even or odd.  If it is even, {\tt \$n} is
divided by 2.  If it is odd, the value of {\tt \$n} is replaced with
{\tt \$n*3 + 1}. For example, if the argument passed to {\tt sequence}
is 42, the resulting values of {\tt n} are 42, 21, 64, 32, 16, 8, 4, 2, 1.

Since {\tt \$n} sometimes increases and sometimes decreases, there is no
obvious proof that {\tt \$n} will ever reach 1, or that the program
terminates.  For some particular values of {\tt n}, we can prove
termination.  For example, if the starting value is a power of two,
{\tt n} will be even every time through the loop
until it reaches 1. The previous example ends with such a sequence
of powers of two, starting with 64.
\index{Collatz conjecture}

The hard question is whether we can prove that this program terminates
for {\em all} positive values of {\tt n}.  So far, no one has
been able to prove it {\em or} disprove it!  (See
\url{http://en.wikipedia.org/wiki/Collatz_conjecture}.)

As an exercise, you might want to rewrite the function
\verb"print-n-times" from Section~\ref{recursion} using
iteration instead of recursion.

\index{statement modifier}
\index{postfix!syntax}
The {\tt while} statement can also be used as a statement modifier (or postfix syntax):

\begin{verbatim}
my $val = 5;
print $val while $val-- > 0;   # prints 43210
print "\n";
\end{verbatim}

The {\tt while} loop statement executes the block as long as
its condition is true. There is also an {\tt until} loop
statement, which executes the block as long as its condition
is false:
\index{until loop}

\begin{verbatim}
my $val = 1;
until $val > 5 {
    print $val++;              # prints 12345
}
print "\n";
\end{verbatim}

\section{Local variables and variable scoping}

We have seen in Section~\ref{localvar} that variables created
within a subroutine (with the {\tt my} keyword are \emph{local}
to that subroutine. The {\tt my}
keyword if often called a \emph{declarator}, because it
is used for declaring a new variable (or other identifier).
It is by far the most common declarator. Other declarators
include {\tt our} or {\tt state}, briefly described later
in this chapter.
\index{lexical scope}
\index{lexical variable}
\index{variable!lexical}
\index{my}
\index{declarator}

Similarly, subroutine
parameters are also usually \emph{local} to the subroutine
in the signature of which they are declared.
\index{subroutine parameters}

We briefly mentioned that the term \emph{lexically scoped}
is probably more accurate than local, but it was
too early at that point to really explain what this means.

Declaring a variable with {\tt my} gives it \emph{lexical
scope}. This means it only exists within the current block.
Loosely speaking, a
block is a piece of Perl code within curly brackets or
braces. For example, the body of a subroutine and the code of a
{\tt while} or {\tt for} loop or of an {\tt if} conditional
statement are code blocks. Any variable created with the
{\tt my} declarator exists and is available for use only
between the place where it is declared and the end of the
enclosing code block.
\index{bracket!curly}
\index{curly bracket}

For example, this code:

\begin{verbatim}
if $condition eq True {
    my $foo = "bar";
    say $foo;  # prints "bar"
}
say $foo;      # ERROR: "Variable '$foo' is not declared ..."
\end{verbatim}
%
will fail on the second print statement, because the \verb'say'
function call is not in
the lexical scope of the {\tt \$foo} variable, which ends
with the closing brace of the condition block. If we want
this variable to be accessible after the end of the condition,
then we would need to declare it before the {\tt if} statement.
For example:

\begin{verbatim}
my $foo;
if $condition eq True {
    $foo = "bar";
    say $foo;  # prints "bar"
} else {
    $foo = "baz";
}
say $foo;      # prints "bar" or "baz" depending on $condition
\end{verbatim}
%
If a lexical variable is not declared within a block, its
scope will extend until the end of the file (this is sometimes
called a static or a global variable, although these terms are
somewhat inaccurate). For example, in the last code snippet
above, the scope of the {\tt \$foo} variable will extend
until the end of the file, which may or may not be a good thing,
depending on how you intend to use it. It
is often better to reduce the scope of variables as much as
possible, because this helps reduce dependencies between
various parts of the code and limits
the risk of subtle bugs. In the code above, if we want to
limit the scope of {\tt \$foo}, we could add braces to create
an enclosing block for the sole purpose of limiting the scope:
\begin{verbatim}
{
    my $foo;
    if $condition eq True {
        $foo = "bar";
        say $foo;  # prints "bar"
    } else {
        $foo = "baz";
    }
    say $foo;      # prints "bar" or "baz" depending on $condition
}
\end{verbatim}
%
Now, the outer braces create an enclosing block limiting the
scope of {\tt \$foo} to where we need it. This may seem to be a
somewhat contrived example, but it is not uncommon to add
braces only for the purpose of precisely defining the scope
of something.

Lexical scoping also means that variables with the same names
can be temporarily redefined in a new scope:

\begin{verbatim}
my $location = "outside";
sub outer {
    say $location;
}
sub inner {
    my $location = "inside";
    say $location;
}
say $location;   # -> outside
outer();         # -> outside
inner();         # -> inside
say $location;   # -> outside
\end{verbatim}
%
We have in effect two variables with the same name
{\tt \$location} but different scopes. One is valid only
within the {\tt inner} subroutine where it has been redefined,
and the other anywhere else.

If we add a new subroutine:
\begin{verbatim}
sub nowhere {
    my $location = "nowhere";
    outer();
}
nowhere();       # -> outside
\end{verbatim}
%
this will still print ``outside'', because the {\tt outer}
subroutine knows about the ``outside'' version of the
{\tt \$location} variable, which existed when {\tt outer} was defined.
In other word, the {\tt outer} code that referenced to the
outer variable (``outside'') knows about the variable that existed
when it was created, but not about the variable existing where
it was called. This is how \emph{lexical} variables work.
This behavior is the basis for building \emph{closures}, a
form of subroutine with some special properties that we
will study later in this book, but
is in fact implicitly present everywhere in Perl~6.
\index{closure}
\index{variable!lexical}
\index{lexical variable}

While having different variables with the same name can give
you a lot of expressive power, we would
advise you to avoid creating different variables with the
same name and different scopes, at least until you really understand
these concepts well enough to know what you are doing, as this
can be quite tricky.

By far, most variables used in Perl are lexical variables,
declared with the {\tt my} declarator. Although they are
not declared with {\tt my}, parameters declared
in the signature of subroutines and parameters of pointy
blocks also have a lexical scope limited to body of the
subroutine or the pointy block.
\index{my}
\index{declarator}

There are other declarators, such as {\tt our}, which creates
a package-scoped variable, and {\tt state}, which creates
lexically scoped variable but with persistent value. They
are relatively rarely used.
\index{our}
\index{state}

One last point: although they are usually not declared with
a {\tt my} declarator, subroutines themselves also have by default
a lexical scope. If they are defined within a block, they will
be seen only within that block. An example of this has been
given at the end of the solution to the GCD exercise of the
previous chapter (see Subsection~\ref{sol_gcd}).
That being said, you \emph{can} declare a subroutine with
with a {\tt my} declarator if you wish:

\begin{verbatim}
my sub frobnicate {
    # ...
}
\end{verbatim}

This technique might add some consistency or some form of
self-documenting feature, but you won't buy very much
added functionality with that.

\section{Control flow statements ({\tt last}, {\tt next}, etc.)}
\index{control flow}
\index{flow!control}
\index{last statement}
\index{statement!last}
\index{next statement}
\index{statement!next}

Sometimes you don't know it's time to end a loop until you get half
way through the body.  In that case, you can use a control flow
statement such as {\tt last} to jump out of the loop.

For example, suppose you want to take input from the user until they
type {\tt done}.  You could write:

\begin{verbatim}
while True {
    my $line = prompt "Enter something ('done' for exiting)\n";
    last if $line eq "done";
    say $line;
}
say 'Done!';
\end{verbatim}
%
The loop condition is {\tt True}, which is always true, so the
loop runs until it hits the {\tt last} statement.

Each time through, it prompts the user to type something.
If the user types {\tt done}, the {\tt last} statement exits
the loop.  Otherwise, the program echoes whatever the user types
and goes back to the top of the loop.  Here's a sample run:

\begin{verbatim}
$ perl6 while_done.pl6
Enter something ('done' for exiting)
Not done
Not done
Enter something ('done' for exiting)
done
Done!
\end{verbatim}
%
This way of writing {\tt while} loops is common because you
can check the condition anywhere in the loop (not just at the
top) and you can express the stop condition affirmatively
(``stop when this happens'') rather than negatively (``keep
going until that happens'').

Using a {\tt while} loop with a condition that is always true
is a quite natural way of writing an infinite loop, i.e. a
loop that will run forever until something else in the code
(such as the {\tt last} statement used above) forces the
program to break out of the loop. This is commonly used in
many programming languages, and this works well in Perl. There
is, however, another common and more idiomatic way of constructing
infinite loops in Perl~6: using the {\tt loop} statement,
which we will study in Section~\ref{C-style loop}
(p.~\pageref{C-style loop}). For now, we'll use the {\tt while True}
statement, which is fairly legitimate.
\index{idiomatic}
\index{loop!infinite}
\index{infinite loop}

Sometimes, rather than simply breaking out of the {\tt while}  loop
as with the {\tt last} control statement, you need to start the
body of the loop at the beginning. For example, you may want
to check whether the user input is correct with some
(unspecified) {\tt is-valid} subroutine before processing
the data, and ask the user to try again if the input was not
correct. In this case, the {\tt next} control statement lets
you start at the top the loop body again:
\index{next statement}
\index{statement!next}

\begin{verbatim}
while True {
    my $line = prompt "Enter something ('done' for exiting)\n";
    last if $line eq "done";
    next unless is-valid($line);
    # further processing of $line;
}
print('Done!')
\end{verbatim}
%
Here, the loop terminates if the user types ``done''. If not, the user input is checked by the {\tt is-valid} subroutine; if the subroutine returns a true value, the processing continues forward; if it returns a false value, then the control flow starts again at the beginning of the body of the loop, so the user is prompted again to submit a valid input.

The {\tt last} and {\tt next} control statements also work in
{\tt for} loops. For example, the following {\tt for} loop
iterates in theory on a range of integer numbers between 1 and 20,
but discards odd numbers by virtue of a {\tt next} statement
and breaks out of the loop with a {\tt last} statement as
soon as the loop variable is greater than {\tt \$max} (i.e. 10 in this  example):
\index{for loop}
\index{loop!for}
\index{statement!for}

\begin{verbatim}
my $max = 10;
for 1..20 -> $i {
    next unless $i %% 2; # keeps only even values
    last if $i > $max;   # stops loop if $i is greater than $max
    say $i;              # prints 2 4 6 8 10
}
\end{verbatim}

You may have as many {\tt last} and {\tt next} statements as you
like, just as you may have as many {\tt return} statements as
you like in a subroutine. Using such control flow statements
is not considered poor
practice. During the early days of structured programming,
some people insisted that loops and subroutines have only one
entry and one exit. The one-entry notion is still a good idea,
but the one-exit notion has led people to bend over backward
and write a lot of unnatural code. Much of programming consists of traversing
decision trees. A decision tree naturally starts with a single
trunk but ends with many leaves. Write your code with the number
of loop controls (and subroutine exits) that is natural to the
problem you're trying to solve. If you've declared your variables
with reasonable scopes, everything gets automatically cleaned up
at the appropriate moment, no matter how you leave the block.

\section{Square roots}
\label{squareroot}
\index{square root}

Loops are often used in programs that compute
numerical results by starting with an approximate answer and
iteratively improving it.
\index{Newton's method}

For example, one way of computing square roots is Newton's
method (also known as Newton-Raphson's method).
Suppose that you want to know the square root of $a$.  If you start
with almost any estimate, $x$, you can compute a better
estimate $y$ with the following formula:

\[ y = \frac{x + a/x}{2} \]
%
For example, if $a$ is 4 and $x$ is 3:

\begin{verbatim}
> my $a = 4;
4
> my $x = 3;
3
> my $y = ($x + $a/$x)/2;
2.166667
\end{verbatim}
%
The result is closer than 3 to the correct answer
($\sqrt{4} = 2$) .  If we repeat the process with the new estimate, it gets even closer:

\begin{verbatim}
> $x = $y;
2.166667
> $y = ($x + $a/$x)/2;
2.006410
\end{verbatim}
%
After a few more updates, the estimate is almost exact:
\index{update}

\begin{verbatim}
> $x = $y;
2.006410
> $y = ($x + $a/$x)/2;
2.000010
> $x = $y;
2.000010
> $y = ($x + $a/$x)/2;
2.000000000026
\end{verbatim}
%
In general we don't know ahead of time how many steps it takes
to get to the right answer, but we know when we get there
because the estimate stops changing:

\begin{verbatim}
> $x = $y;
2.000000000026
> $y = ($x + $a/$x)/2;
2
> $x = $y;
2
> $y = ($x + $a/$x)/2;
2
\end{verbatim}
%
When {\tt \$y == \$x}, we can stop.  Here is a loop that starts
with an initial estimate, {\tt x}, and improves it until it
stops changing:

\begin{verbatim}
my ($a, $x) = (4, 3);
while True {
    say "-- Intermediate value: $x";
    my $y = ($x + $a/$x) / 2;
    last if $y == $x;
    $x = $y;
}
say "Final result is $x";
\end{verbatim}
%
This will print:
\begin{verbatim}
-- Intermediate value: 3
-- Intermediate value: 2.166667
-- Intermediate value: 2.006410
-- Intermediate value: 2.000010
-- Intermediate value: 2.000000000026
-- Intermediate value: 2
Final result is 2
\end{verbatim}
%

For most values of {\tt \$a} this works fine, but there are a
couple of caveats with this approach. First, in most programming
languages, it is dangerous to test {\tt float} equality, because
floating-point values are only approximately right. We do not
have this problem with Perl~6, because, as we have already
mentioned, it is using a better representation of rational
numbers than most generalist programming languages. (You
may want to keep this in mind if you are using some other
languages.) Even if we don't have this problem with Perl, there
may also be some problems with algorithms that do not behave
as well as Newton's algorithm. For example, some algorithms
might not converge as fast and as neatly as Newton's algorithm but might instead produce alternate values above and below the accurate result.
\index{floating-point}
\index{epsilon}

Rather than checking whether {\tt \$x} and {\tt \$y} are exactly equal, it
is safer to use the built-in function {\tt abs} to compute the
absolute value, or magnitude, of the difference between them:

\begin{verbatim}
    last if abs($y - $x) < $epsilon:
\end{verbatim}
%
where \verb"epsilon" has a very small value like {\tt 0.0000001}
that determines how close is close enough.

\section{Algorithms}
\index{algorithm}

Newton's method is an example of an {\bf algorithm}: it is a
mechanical process for solving a category of problems (in this
case, computing square roots).

To understand what an algorithm is, it might help to start with
something that is not an algorithm.  When you learned to multiply
single-digit numbers, you probably memorized the multiplication table.
In effect, you memorized 100 specific solutions.  That kind of
knowledge is not algorithmic.

But if you were ``lazy'', you might have learned a few
tricks.  For example, to find the product of $n$ and 9, you can
write $n-1$ as the first digit and $10-n$ as the second
digit.  (For example, to figure out $9*7$, $n-1$ is 6 and
$10-n$ is 3, so that the product $9*7$ is 63.) This trick
is a general solution for multiplying any single-digit
number by 9.  That's an algorithm!
\index{addition with carrying}
\index{carrying, addition with}
\index{subtraction!with borrowing}
\index{borrowing, subtraction with}

Similarly, the techniques you learned in school for addition
(with carrying), subtraction (with borrowing), and long
division are all algorithms.  One
of the characteristics of algorithms is that they do not require any
intelligence to carry out.  They are mechanical processes where
each step follows from the last according to a simple set of rules.

Executing algorithms is boring, but designing them is interesting,
intellectually challenging, and a central part of computer science.

Some of the things that people do naturally, without difficulty or
conscious thought, are the hardest to express algorithmically.
Understanding natural language is a good example.  We all do it, but
so far no one has been able to explain {\em how} we do it, at least
not in the form of an algorithm.

\section{Debugging}
\label{bisectbug}

As you start writing bigger programs, you might find yourself
spending more time debugging.  More code means more chances to
make an error and more places for bugs to hide.
\index{debugging!by bisection}
\index{bisection, debugging by}

One way to cut your debugging time is ``debugging by bisection''.
For example, if there are 100 lines in your program and you
check them one at a time, it would take 100 steps.

Instead, try to break the problem in half.  Look at the middle
of the program, or near it, for an intermediate value you
can check.  Add a {\tt say} statement (or something else
that has a verifiable effect) and run the program.

If the mid-point check is incorrect, there must be a problem in the
first half of the program.  If it is correct, the problem is
in the second half.

Every time you perform a check like this, you halve the number of
lines you have to search.  After six steps (which is fewer than 100),
you would be down to one or two lines of code, at least in theory.

In practice it is not always clear what
the ``middle of the program'' is and not always possible to
check it.  It doesn't make sense to count lines and find the
exact midpoint.  Instead, think about places
in the program where there might be errors and places where it
is easy to put a check.  Then choose a spot where you
think the chances are about the same that the bug is before
or after the check.

\section{Glossary}

\begin{description}

\item[reassignment:] Assigning a new value to a variable that
already exists.
\index{reassignment}

\item[update:] An assignment where the new value of the variable
depends on the old.
\index{update}

\item[initialization:] An assignment that gives an initial value to
a variable that may later be updated.
\index{initialization!variable}

\item[increment:] An update that increases the value of a variable
(often by one).
\index{increment operator}

\item[decrement:] An update that decreases the value of a variable.
\index{decrement operator}

\item[iteration:] Repeated execution of a set of statements using
either a recursive function call or a loop.
\index{iteration}

\item[infinite loop:] A loop in which the terminating condition is
never satisfied.
\index{infinite loop}
\index{loop!infinite}

\item[algorithm:]  A general process for solving a category of
problems.
\index{algorithm}

\end{description}

\section{Exercises}

\begin{exercise}
\label{test_sqrt}
\index{square root}
\index{algorithm!square root}

Copy the loop from Section~\ref{squareroot}
and encapsulate it in a subroutine called
\verb"my-sqrt" that takes {\tt \$a} as a parameter, chooses a
reasonable value of {\tt \$x}, and returns an estimate of
the square root of {\tt \$a}.

To test it, write a function named \verb"test-square-root"
that prints a table like this:

\begin{verbatim}[fontshape=up]
a  mysqrt(a)        sqrt(a)          diff
1  1.0000000000000  1.0000000000000  1.110223e-15
2  1.4142135623747  1.4142135623731  1.594724e-12
3  1.7320508075689  1.7320508075689  0.000000e+00
4  2.0000000000000  2.0000000000000  0.000000e+00
5  2.2360679774998  2.2360679774998  0.000000e+00
6  2.4494897427832  2.4494897427832  8.881784e-16
7  2.6457513110647  2.6457513110646  1.025846e-13
8  2.8284271247494  2.8284271247462  3.189449e-12
9  3.0000000000000  3.0000000000000  0.000000e+00

\end{verbatim}
%
The first column is a number, $a$; the second column is the
square root of $a$ computed with \verb"my-sqrt"; the third
column is the square root computed by the {\tt sqrt} built-in
function of Perl; the fourth column is the absolute value of
the difference between the two estimates. Don't worry too much about
obtaining a clean tabular formatting, we haven't
seen the built-in functions to do that. Solution: \ref{sol_test_sqrt}
%

\end{exercise}

\begin{exercise}
\index{Ramanujan, Srinivasa}
\index{Ramanujan, Srinivasa!pi estimate}
\index{pi!estimate}
\label{pi_estimate}

The mathematician Srinivasa Ramanujan found an
infinite series
that can be used to generate a numerical
approximation of $1 / \pi$:
\index{pi}

\[ \frac{1}{\pi} = \frac{2\sqrt{2}}{9801}
\sum^\infty_{k=0} \frac{(4k)!(1103+26390k)}{(k!)^4 396^{4k}} \]

Write a function called \verb"estimate-pi" that uses this
formula to compute and return an estimate of $\pi$.  It
should use a {\tt while} loop to compute terms of the
summation until the last term is smaller than {\tt 1e-15}
(which is Perl notation for $10^{-15}$). You can check
the result by comparing it to {\tt pi} built-in constant.
Solution: \ref{sol_pi_estimate}.

\end{exercise}