\documentclass[a4paper]{article} \usepackage{listings} \usepackage{fontspec} \usepackage[margin=1in]{geometry} \usepackage{amsmath} \usepackage{multicol} \usepackage{tikz} \usetikzlibrary{automata,positioning} \renewcommand{\thesection}{Task \arabic{section}} \renewcommand{\thesubsection}{\arabic{subsection})} \renewcommand{\thesubsubsection}{\alph{subsubsection})} \input{fasto.sty} \setmonofont[Scale=0.9]{Antikor Mono Medium} \setlength{\parskip}{5pt} \setlength{\parindent}{0pt} \title{W2 - IPS} \author{Nikolaj Gade (qhp695)} \date{May 2022} \newcommand{\reg}[1]{% \begin{center} \large \texttt{#1} \end{center} } \begin{document} \newgeometry{top=0in} \maketitle \section{} \subsection{} \begin{multicols}{2} \subsubsection{} The string must consist of any number of pairs of '\texttt{o}'s and '\texttt{g}'s. The pairs can be any combination of 2 characters, both of which are either '\texttt{o}' or '\texttt{g}'. Thus, each character can be written as, '\texttt{o|g}', and each pair as '\texttt{(o|g)(o|g)}'. The full string can be written as: \reg{((o|g)(o|g))*} \subsubsection{} The first character must be '\texttt{o}', and it must be followed by a character that is either '\texttt{o}' or '\texttt{g}'. Following that, the answer is the same as the previous problem: \reg{o(o|g)((o|g)(o|g))*} \columnbreak \subsubsection{} Keeping with the idea that an even number of characters can be split up in "pairs" of characters, an odd number of '\texttt{o}'s or '\texttt{g}'s \textit{must} result in an odd number of both. Thus, the string will either consist of an even number of '\texttt{o}'s, followed by an even number of '\texttt{g}'s, \textbf{or} an even number of '\texttt{o}'s, followed by '\texttt{og}', followed by an even number of '\texttt{g}'s. The full string can be written as: \reg{(oo)*(og)?(gg)*} \end{multicols} \subsection{} \begin{multicols}{2} \subsubsection{} '\textit{a}'s and '\textit{b}'s are placed at the same time, using the \textit{T} starting symbol. After all '\textit{a}'s and '\textit{b}'s are placed, the symbol turns to '\textit{S}', which places at least 1 '\textit{c}'. \[ T = \begin{cases} aTb \\ S \end{cases} \] \[ S = \begin{cases} c \\ cS \end{cases} \] \subsubsection{} Like with the previous problem, both the '\textit{a}'s and the '\textit{b}'s are placed at the same time, but the '\textit{b}'s are placed 2 at a time. \[ T = \begin{cases} aTbb \\ abb \end{cases} \] \columnbreak \subsubsection{} Once again, '\textit{a}'s and '\textit{b}'s are placed at the same time with the starting symbol \textit{T}. Afterwards, any amount of '\textit{a}'s are placed before the '\textit{b}'s. \[ T = \begin{cases} aTb \\ S \end{cases} \] \[ S = \begin{cases} \\ aS \end{cases} \] \end{multicols} \newpage \newgeometry{top=1in} \subsection{} \subsubsection{} \texttt{\%nonassoc letprec} designates the \texttt{let} token as being non-associative, which means ambiguous implementations will lead to a syntax error. \subsubsection{} The order of the associativity declarations provide the precedence for the operators. So in the current way the code in written, the line \texttt{let x = 10 in x + 10 > 15} will be parsed as \texttt{let x = 10 in (x < 15)}, but if , it would be parsed as \texttt{(let x = 10 in x) < 15}. \subsubsection{} The code \texttt{\{ Let (Dec (fst \$2, \$4, \$3), \$6, \$1) \}} creates a \texttt{Let} instance, which containes the declared variable, the following expression, as well as the keyword. \section{} See code. \section{} \subsubsection{} \reg{filter (('a -> bool) * ['a]) -> ['a]} \subsubsection{} \begin{lstlisting} CheckExp(exp, vtable, ftable) = case exp of filter(p, arr_exp) => let array_type = CheckExp(arr_exp, vtable, ftable) let element_type = match array_type with | Array(type) -> type | _ -> Error() let function_type = lookup(ftable, name(p)) match function_type with | unbound -> Error() | (input_type, output_type) -> if input_type == element_type && output_type == bool then Array(element_type) else Error() | _ -> Error() \end{lstlisting} \newpage \section{} The unification graph with the initial R and ID values is as follows: \begin{tikzpicture}[text width=1cm, align=center] \node (a) at (0,0) {1,1\\{\Large*}}; \node (b) at (-2,-2) {2,2\\{\Large list}}; \node (c) at (2,-2) {3,3\\{\Large list}}; \node (d) at (-2,-4) {4,4\\{\Large int}}; \node (e) [text width=1.5cm] at (2,-4) {5,5\\{\Large\textit{alpha}}}; \node (f) at (4,0) {6,6\\{\Large*}}; \node (g) at (6,-4) {7,7\\{\Large\textit{beta}}}; \path [->] (a) edge node {} (b); \path [->] (a) edge node {} (c); \path [->] (b) edge node {} (d); \path [->] (c) edge node {} (e); \path [->] (f) edge node {} (e); \path [->] (f) edge node {} (g); \end{tikzpicture} The R values can then be substituted in the following order: \begin{itemize} \item 6 \rightarrow{} 1, Rule (IV) \item 5 \rightarrow{} 2, Rule (III) \item 7 \rightarrow{} 3, Rule (III) \end{itemize} So the final unification graph looks like this: \begin{tikzpicture}[text width=1cm, align=center] \node (a) at (0,0) {1,1\\{\Large*}}; \node (b) at (-2,-2) {2,2\\{\Large list}}; \node (c) at (2,-2) {3,3\\{\Large list}}; \node (d) at (-2,-4) {4,4\\{\Large int}}; \node (e) [text width=1.5cm] at (2,-4) {2,5\\{\Large\textit{alpha}}}; \node (f) at (4,0) {1,6\\{\Large*}}; \node (g) at (6,-4) {3,7\\{\Large\textit{beta}}}; \path [->] (a) edge node {} (b); \path [->] (a) edge node {} (c); \path [->] (b) edge node {} (d); \path [->] (c) edge node {} (e); \path [->] (f) edge node {} (e); \path [->] (f) edge node {} (g); \end{tikzpicture} Which means the final type is \texttt{list(int) * list(list(int))}. \end{document}