\documentclass[11pt]{article} \input{exer-defs} %\renewcommand{\thepage}{} \begin{document} \begin{center}\large\bf CIS 705 --- Programming Languages --- Spring 2009 \end{center} \begin{center}\Large\bf Assignment 3 \end{center} \begin{center}\large\bf Due by 2:30 p.m.\ on Thursday, March 12 \end{center} \begin{center} The context for this assignment is Chapter~2 of \emph{DAWOC}, the core and standard\\OCaml libraries, and Assignment 2 (including the model answers \texttt{balanced.ml}\\and \texttt{balanced-inefficient.ml}). \end{center} \section*{Exercise 1 (25 Points)} Using a few well-chosen examples, explain how the versions of \begin{verbatim} val balanced : int list -> int list list \end{verbatim} defined by \texttt{balanced-inefficient.ml} and \texttt{balanced.ml} work, and compare their relative efficiencies. \section*{Exercise 2 (35 Points)} The function \begin{verbatim} val insert : 'a -> 'a list -> 'a list \end{verbatim} of \texttt{balanced.ml} runs out of stack space when used with lists containing millions of elements, resulting in the error message \begin{verbatim} Stack overflow during evaluation (looping recursion?). \end{verbatim} Reimplement \texttt{insert} to meet the same specification, but to be usable with lists of millions of elements. Try to minimize the stack space it uses, as well as the number of \texttt{::}'s and comparisons that it does; minimizing stack space should have highest priority. You may use as many auxiliary functions as you wish, and these functions may be defined either inside or before \texttt{insert}'s definition. Program in a strictly functional style, without using the imperative features of Chapter~3 of \emph{DAWOC}. Format your program in a way that makes its structure clear. Choose meaningful names for functions, and choose other identifiers with care. Document your functions by abstractly explaining their input/output behavior. \section*{Exercise 3 (40 Points)} Prove that the functions \begin{verbatim} val extend : int -> int list -> int list option * int list val balanced : int list -> int list list \end{verbatim} of \texttt{balanced.ml} meet their specifications. You may assume that the auxiliary and library functions they use are correct, as well as that the auxiliary function \texttt{bal} of \texttt{balanced}'s definition terminates on all inputs meeting its precondition. (A function meets its specification iff, for all inputs to the function, if those inputs meet the function's precondition, then the function returns a value meeting the function's postcondition. When proving a recursively defined function correct, one may assume that a recursive call whose arguments meet the precondition returns a value that meets the postcondition.) \section*{Submission} Submit your solutions to Exercises 1 and 3 on paper, not electronically. Email your solution to Exercise 2 to me, being sure to keep an electronic copy. I will acknowledge receiving it. Your submission should \emph{not} include any testing or extraneous code. It should begin with a comment including your name. \end{document}