\section{Regular languages}

\definition{}
We say a language is \textit{regular} if it is recognized by some $DFA$.

\problem{}
Draw a DFA over $\{A, B\}$ that accepts strings which do not start and end with the same letter. \par
\hint{Modify the DFA in \ref{SameStartAndEnd}.}

\vfill

\problem{}
Let $L$ be a regular language over an alphabet $Q$. \par
Show that $Q^* - L$ is also regular. \par
\hint{$Q^* - L$ is the set of objects in $Q^*$ but not in $L$. This is often called the \textit{complement} of $L$.}

\begin{solution}
	Invert accepting and rejecting states.
\end{solution}

\vfill
\pagebreak




\problem{}
Draw a DFA over the alphabet $\{A, B\}$ that accepts strings which have even length and do not start and end with the same letter. \par


\vfill

\problem{}
Let $L_1$, $L_2$ be two regular languages over an alphabet $Q$. \par
Show that their union and intersection are also regular.

\begin{solution}
	Consider a product of automatons where each state is a pair of states in the first and second automaton and every transition works if it was applied to both elements in pair.

	\vspace{2mm}

	For union, we call the state $(s_1, s_2)$ accepting if $s_1$ OR $s_2$ is accepting in their respective automaton.

	\vspace{2mm}

	For intersection, we call it accepting if $s_1$ AND $s_2$ are accepting in their respective automaton.
\end{solution}


\vfill
\pagebreak


\theorem{Pumping Lemma}
Let $A$ be a regular language. \par
There then exists a number $p$, called the \textit{pumping length}, so that any string $s \in A$ of length at least $p$ may be divided into three pieces $s = xyz$ satisfying the following:

\begin{itemize}
	\item $|y| > 0$          \tab~\tab \hint{In other words, the segment $y$ is not the empty string.}
	\item $|xy| \leq p$.     \tab~\tab \hint{$|s|$ is the length of a string.}
	\item $\forall i > 0$, $x y^i z \in A$     \tab \hint{$y^i$ means that $y$ is repeated $i$ times. $y^0$ is the empty string.}
\end{itemize}

When $s$ is divided into $xyz$, either $x$ or $z$ may be the empty string, but $y$ must not be empty. \par
Notice that without the first condition, this theorem is trivially true.

\vspace{2mm}

In english, the pumping lemma states that in any regular language, any string of sufficient length contains a substring that can be \say{pumped} (or repeated) to generate more strings in that language.


\problem{}
Check that the pumping lemma holds with $p = 3$ for the following DFA. \par
\hint{This is the same DFA as in \ref{fibonacci}. What kind of strings does it accept?}

\begin{center}
\begin{tikzpicture}
	\begin{scope}[layer = nodes]
		\node[main] (0) at (0, 0) {$\phantom{0}$};
		\node[accept] (1) at (3, 0) {$\phantom{0}$};
		\node[main] (2) at (5, 0) {$\phantom{0}$};
		\node[start] (s) at (-2, 0) {\texttt{start}};
	\end{scope}

	\draw[->]
		(s) edge (0)
		(0) edge[loop above] node[label] {\texttt{b}} (0)
		(0) edge[bend left] node[label] {\texttt{a}} (1)
		(1) edge[bend left] node[label] {\texttt{b}} (0)
		(1) edge node[label] {\texttt{a}} (2)
		(2) edge[loop above] node[label] {\texttt{a,b}} (2)
	;
\end{tikzpicture}
\end{center}


\vfill

\problem{}
How can we use the pumping lemma to show that a language is \textbf{not} regular?
\vfill

%\part{b} Suppose that there is a regular language $L$ in the alphabet $\{a\}$. $L$ contains all strings of $a$'s whose length is some set $S$. Derive from the pumping lemma that if $S$ is infinite then it contains some arithmetic progression.

% \part{c} Prove directly that if $S$ is infinite, than it contains some arithmetic progression. \textit{Hint: look at the first cycle in the DFA you get while reading $aaa\dots$.}

\problem{}
Prove the pumping lemma. \par
\hint{Look at the first cycle in the DFA you get while reading $s$.}

\begin{solution}
	Look at the first place where we come to an already visited state while reading the word. Say the first time we came to this state after reading $x$ and the second time after reading $xy$. Then $y$ doesn't move us from this state and we can omit it or repeat any number of times we want.
\end{solution}

\vfill
\pagebreak

\problem{}
Show that the following languages are not regular: \par

\begin{enumerate}[itemsep=2mm]
	% \item $\{a^{n^2}\}$, the language of all strings over the alphabet $\{a\}$ which have a square length. \par
	% $\{a^{n^2}\} = \{ \varepsilon, \texttt{a}, \texttt{aaaa}, \texttt{aaaaaaaaa}, ... \}$

	\item $\{0^n1^n ~|~ n \in \mathbb{Z}^+_0\}$ over $\{0, 1\}$, which is the shorthand for the set $\{\varepsilon, 01, 0011, \dots\}$

	\item The language ADD over the alphabet $\Sigma = \{0, 1, +, =\}$ where \par
		$\text{ADD} = \{ ~ \text{\say{x=y+z}} ~|~ x, y, z ~ \text{are binary integers, and $x$ is the sum of $y$ and $z$}\}$

	\item The language of all palindromes over the english alphabet

\end{enumerate}


\begin{solution}
	% \textbf{Part A:} \par
	% Follows from parts b-c of the previous problem;
	% \vspace{2mm}

	\textbf{Part A:} \par
	Assume this language is regular. Let $p$ be the pumping length. The string $0^p1^p$ must then be accepted, implying that the string $0^{p-|y|}1^p$ (or $0^{p+|y|}1^p$) is also accepted.

	\vspace{2mm}

	\textbf{Part B:} \par
	By pumping $10^{p+1}=10^p+10^p$

	\vspace{2mm}

	\textbf{Part C:} \par
	By pumping $a^pba^p$


\end{solution}


\vfill
\pagebreak

\definition{}
Let $w$ be a string over an alphabet $A$. \par
If $a \in A$, $|w|_a$ is the number of times the letter $a$ occurs in $w$.

\vspace{2mm}

For the following problems, we will use the alphabet $\{a, b\}$.

\problem{}
Show that the language $L_p = \Bigl\{w ~\Bigl|~ \text{$p$ divides } |w|_a - |w|_b \Bigr\}$ is regular for any prime $p$.

\vfill

\problem{}
Show that $L = \Bigl\{ w ~\Big|~ |w|_a - |w|_b = \pm 1 \Bigr\}$ is not regular.

\vfill

\problem{}
Prove that there are infinitely many primes.

\begin{solution}
	\texttt{https://www.jstor.org/stable/48661886}
\end{solution}

\vfill
\pagebreak