\documentclass[a4paper]{article} \usepackage{listings} \usepackage{fontspec} \usepackage[margin=1in]{geometry} \usepackage{amsmath} \usepackage{multicol} \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} \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} \] \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 \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{} \end{document}