\documentclass{article}
\usepackage{graphics}
\usepackage{fancyheadings}

\newcommand{\coursenumber}{131}
\newcommand{\coursetitle}{Principles of Programming Languages}

\topmargin  18pt        % 27pt
\headheight 12pt        % 12pt
\headsep    35pt        % 45pt
\textheight 8.0in
\textwidth  6.2in

\oddsidemargin   22pt      % Left margin on odd-numbered pages.
\evensidemargin  22pt      % Left margin on even-numbered pages.
\marginparwidth  72pt       % Width of marginal notes.
\marginparsep    11pt      % Horizontal space between outer margin and 
                           % marginal note

\pagestyle{fancy}
\lhead{CSCI \coursenumber}
\chead{}
\rhead{\semester}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\setlength{\headrulewidth}{0pt}
\setlength{\footrulewidth}{0pt}

\newcommand{\semester}[0]{
     \ifnum\month>7{Fall}\fi
     \ifnum\month<8{Spring}\fi\  \number\year
}

\newcommand{\points}[1]{(#1 points)}
\newcommand{\phead}[2]{\points {#1} \textbf{#2}}

\def\rightarrowfill{\mathsurround=0pt  \mathord- \mkern-6mu
  \cleaders\hbox{$\mkern-2mu \mathord- \mkern-2mu$}\hfill
  \mkern-6mu \mathord-\!\!\!\!\longrightarrow}
\newcommand{\step}[1]{~~\stackrel{\texttt{\scriptsize #1}}{\rightarrowfill}~~}


\begin{document}

\begin{center}
    \Large Homework 8\\ \vspace{1 ex} Due Friday, 11/5/2010
\end{center}

\vspace{2 ex} 

Please turn in your homework solutions online at  
\texttt{http://www.dci.pomona.edu/tools-bin/cs131upload.php}
before the beginning of class on the due date. 

Several questions on this homework involve ML program.  However, they
behave identically to equivalent Haskell programs.  If you have any
problem understanding them, please let me know.  There are also some
Ada programs, where Ada is a procedural language designed about 1980,
but again the syntax should be pretty understandable.

\vspace{2 ex}


\begin{enumerate}
    
    \item \phead{20}{Activation Records for Inline Blocks}
    
   Please do problem 7.1 from Mitchell, page 191.
 

    \item \phead{10}{Time and Space Requirements}
    
    Please do problem 7.3 from Mitchell, page 193.
    
    \item \phead{15}{Ada Parameter Modes}


    The Ada programming language permits parameters to be labeled as
    \texttt{in}, \texttt{out}, or \texttt{in out}, as in the following
    procedure definitions, where \texttt{T} is some type:

    \begin{verbatim}
	procedure test1(in x: T) is begin ... end
	procedure test2(out x: T) is begin ... end
	procedure test3(in out x: T) is begin ... end
    \end{verbatim}
    The modifiers, or modes, have the following meaning:
    \begin{itemize}
	\item \texttt{in}: The value of the parameter \texttt{x}
	cannot be changed inside the procedure.  If we call
	\texttt{test1(y)}, the value of \texttt{y} is the same before
	and after the call..

	\item \texttt{out}: The parameter \texttt{x} can be written
	to, but it cannot be read.  If we call \texttt{test2(y)}, the
	value of \texttt{y} after the call is the last value written
	to \texttt{x} in the procedure.

	\item \texttt{in out}: The parameter \texttt{x} can be both
	read and written, and the value of \texttt{y} after a call to
	\texttt{test3(y)} is the last value written to \texttt{x} in
	the procedure.
    \end{itemize}

    The language definition does not specify how each mode should be
    implemented, and the compiler may use any appropriate parameter
    passing mechanism to implement them.
    \begin{enumerate}

	\item Which parameter passing mechanism could be used to
	implement \texttt{test1}, \texttt{test2}, and \texttt{test3}?
	The choices are pass-by-reference, pass-by-value, and
	pass-by-value-result (as described in problem 7.6).  If more
	than one is possible, describe the advantages/disadvantages of
	each.

	\item Consider the following procedure that takes two
	parameters.  Does the following program print the same value
	for all strategies you outlined for \texttt{in out} parameters
	above?
	\pagebreak
	
	\begin{verbatim}
	procedure incTwo(in out x:integer, in out y:integer) is
	  begin
	    x := x + 1;
	    y := y + 1;
	  end

	procedure main() is
	  w : integer = 3;
	  begin
	    incTwo(w,w);
	    print w;
	  end
        \end{verbatim}


	\item Discuss the advantages and disadvantages of permitting
	the compiler such flexibility in how it implements parameter
	modes.

    \end{enumerate}
    
    \item \phead{10}{Activation records}

    Draw the stack of activation records for the following Ada program
    (a) after the first call to procedure b; (b) after the second call
    to procedure b.  Show the static (access) and dynamic (control)
    links for each activation record.  (c) Indicate how x is found in
    procedure c.
    \begin{verbatim}
        procedure env is
           integer x = 12;        
           procedure a is
              integer y = 2;           
              procedure b(z) is            
                 procedure c is
                 begin
                    b(x);
                 end c;                 
              begin
                 c;
              end b              
           begin
              b(x+y);
           end a;           
        begin
           a;
        end env
    \end{verbatim}
    
    \item \phead{15}{Function Calls and Memory Management}
    
   Please do problem 7.12 from Mitchell, page 198.
 

    \item \phead{15}{Function Returns and Memory Management}
    
    Please do problem 7.13 from Mitchell, page 199.
\end{enumerate}

\end{document}