\documentclass[11pt]{article}
\usepackage{ifthen}
\usepackage{alltt}
% \usepackage{indentfirst}
\usepackage{latexsym}
\usepackage{graphicx}
\usepackage{gdefs}
% \usepackage{logdef}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{rotating}
\usepackage{longtable}
\usepackage{hhline}
% \usepackage{refs}
\usepackage{changebar}
\usepackage{array}
\usepackage{wrapfig}
\usepackage[small,compact]{titlesec}

\usepackage{tweaklist}
% this sets the spacings in the itemize environment
\renewcommand{\itemhook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

% this sets the spacings in the enumerate environment
\renewcommand{\enumhook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

% this sets the spacings in the description environment
\renewcommand{\descripthook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0.0in}
\setlength{\headsep}{0.0in}
\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in}
\setlength{\textheight}{9.0in}

\setcounter{topnumber}{5}
\def\topfraction{0.99}
\def\textfraction{0.05}
\def\floatpagefraction{0.9}
\setcounter{bottomnumber}{5}
\def\bottomfraction{0.99}
\setcounter{totalnumber}{10}
\def\dbltopfraction{0.99}
\def\dblfloatpagefraction{0.8}
\setcounter{dbltopnumber}{5}

\setlength{\marginparwidth}{.95in}
\let\oldmarginpar\marginpar
\renewcommand\marginpar[1]{\-\oldmarginpar[\raggedleft\footnotesize #1]%
{\raggedright\footnotesize #1}}

\let\oldthebibliography=\thebibliography
  \let\endoldthebibliography=\endthebibliography
  \renewenvironment{thebibliography}[1]{%
    \begin{oldthebibliography}{#1}%
      \setlength{\parskip}{0ex}%
      \setlength{\itemsep}{0ex}%
  }%
  {%
    \end{oldthebibliography}%
  }

\newcommand{\pe}{{\textrm pe}}
\newcommand{\pgen}{{\textrm pgen}}
\newcommand{\cogen}{{\textrm cogen}}

\title{Problems: Partial Evaluation and Symbolic Composition}
\date{}

\begin{document}           % End of preamble and beginning of text.

\maketitle

\begin{enumerate}
\item
  \begin{enumerate}
    \item
      Describe three ways in which partial evaluation can speed up the
      execution of a program.
      That is, what are three optimizations that a partial evaluator
      may apply so that the execution of $p_s$ on $d$ is faster
      than the execution of $p$ on $[s,d]$.
    \item
      Explain why partial evaluation might slow a program down.
  \end{enumerate}

\medskip
\item
  \begin{enumerate}
    \item
      One way of speeding up the creation of specialized programs is via
      the notion of a \emph{generating extension}.
      A program $\pgen$ is a generating extension for $p$ if
      \[
        \semp{\pgen} s = p'_s, \textrm{ such that for all } d, \semp{p'_s} d = \semp{p}[s,d].
      \]
      That is, unlike normal partial evaluation of $p$, $\pgen$ already has $p$
      ``built into it'' so that, when supplied with an argument $s$, it creates a program
      $p'_s$, where $p'_s$ operates just like the
      program $p_s$ produced via partial evaluation.
      (Note that $p'_s$ and ${p_s}$ are not necessarily
      \emph{identical} programs---just ones with \emph{identical behaviors}.)

      \medskip
      Suppose that \texttt{DotProduct} computes the dot product of two vectors of length N:

      \smallskip
      \noindent
      \texttt{\begin{tabular}{l}
        const int N = <some constant>; \\
        \\
        int DotProduct(int x[], int y[])  // x and y assumed to be of length N \\
        \{ \\
        \hspace{2.0ex}int answer = 0; \\
        \\
        \hspace{2.0ex}for (int i = 0; i < N; i++) \{ \\
        \hspace{2.0ex}\hspace{2.0ex}answer = answer + x[i] * y[i]; \\
        \hspace{2.0ex}\} \\
        \hspace{2.0ex}return answer; \\
        \}
      \end{tabular}}

      \medskip
      Write a procedure \texttt{DotProduct-gen} that writes a version of
      \texttt{DotProduct}, specialized to the value of \texttt{x[]}, to the
      standard output:

      \smallskip
      \noindent
      \texttt{\begin{tabular}{l}
        void DotProduct-gen(int x[]) \\
        \{ \\
        \hspace{2.0ex}// MISSING -- body of DotProduct-gen \\
        \}
      \end{tabular}}
    \item
      Compared with applying a partial evaluator to $p$ and $s$,
      why is applying $\pgen$ to $s$ likely to be faster?
    \item
      Suppose that $\pe$ is a self-applicable partial evaluator.
      Let $\cogen \eqdef \semp{\pe}[\pe,\pe] = {\pe_\pe}$.
      Show that $\semp{\cogen} p$ yields a program that is a
      generating extension for $p$.
  \end{enumerate}

\newpage
Questions 3 and 4 explore certain aspects of \emph{symbolic
composition}, which is a program transformation that bears some
relationship to partial evaluation.
\medskip

\item
  An $m \times n$ matrix $M$ over the real numbers $\cal{R}$
  determines a linear transformation $\semp{M}$:
  ${\cal R}^n \rightarrow {\cal R}^m$.
  That is, if $v \in {{\cal R}^n}$, then $\semp{M}(v)$ is
  a vector $u \in {{\cal R}^m}$.
  (We can compute $u$ by doing a matrix-vector multiplication: $u~=~M \times v$.)\ 

  \hspace*{1.5ex}
  If $M$, $N$ are matrices of dimensions $m \times n$ and $n \times p$,
  respectively, and $M \times N$ is their matrix product, then
  $\semp{M \times N}$: ${\cal R}^p \rightarrow {\cal R}^m$.
  We have
  \[
    (\semp{M} \composition \semp{N})(w)) = \semp{M}(\semp{N}(w)) = \semp{M \times N}(w),
  \]
  which means that $M \times N$ represents the \emph{symbolic composition} of $M$ and $N$.

  \hspace*{1.5ex}
  Suppose that we have a collection of vectors $\{ v_i \}$ that we wish to transform
  by $\semp{M} \composition \semp{N}$.
  We can do the computation either as $\{ M(N(v_i)) \}$ (``sequential application'')
  or as $\{ (M \times N)(v_i) \}$ (``symbolic composition'').
  What is the break-even point for symbolic composition?
  That is, how many vectors do we have to have for it to be better to use the
  symbolic-composition method rather than the sequential-application method?
  (Your answer should focus on the number of scalar-multiplication operations
  performed; you do not have to count additions exactly.)
\medskip
\item
  A \emph{(nondeterministic) finite-state transducer} is a finite-state
  machine that transforms input strings from $\Sigma^*$ into output strings
  from $\Delta^*$ (where, in general, $\Sigma$ and $\Delta$ are two
  different alphabets).
  A finite-state transducer is similar to a standard finite-state
  automaton except that it also has an output alphabet $\Delta$, and
  the transiton relation, $\lambda$, associates each transition
  with an output symbol in $\Delta \cup \{ \epsilon \}$.
  Formally, a finite-state transducer has five components:

  \smallskip
  \begin{tabular}{l}
    $Q$, a set of states \\
    $\Sigma$, the input alphabet \\
    $\Delta$, the output alphabet \\
    $\lambda \subseteq Q \times ({\Sigma \cup \{ \epsilon \}}) \times ({\Delta \cup \{ \epsilon \}}) \times Q$, the transition relation \\
    $q_0$, the initial state
  \end{tabular}

  \smallskip
  \noindent
  (Note that there is no set of final states.)\ 

  At run-time, whenever the machine is in state $q$ and the current
  input symbol is $a$, the permissible transitions---with output $b$---are
  to the states $r$ such that $\B{q,a,b,r} \in \lambda$.
  For an input string $x$, the machine's output string can be any of the
  strings of output symbols generated in this nondeterministic fashion.

  It is convenient to think of a finite-state transducer as a directed
  multi-graph whose nodes are the states, and where each
  tuple $\B{q, a, b, r} \in \lambda$ corresponds
  to an edge from $q$ to $r$, labeled with the pair ``$(a,b)$''
  (meaning that on a transition from $q$ to $r$
  on which $a$ is ``consumed'' from the input string,
  $b$ is generated in the output string, where
  $a$ and $b$ are possibly $\epsilon$).

  \begin{enumerate}
    \item
      Give the formal definition of a nondeterministic finite-state transducer
      $M$ that is the ``single-error introducer'' from $\{ 0,1 \}^*$
      to $\{ 0,1 \}^*$.
      That is, $M$ should be a transducer that ``corrupts''
      \emph{up to one bit} of the input string.
      For example, if the input string is 101, $M$ can produce any of the
      following strings:
      101, 100, 111, 001, 01, 11, 10, 0101, 1101, 1001, 1011, and 1010 (but not, for instance, 011, 000, 00, or 0000).
    \item
      Suppose that you are given two finite-state transducers:
      $M$, which transforms strings from $\Sigma^*$ to strings
      from $\Delta^*$, and
      $N$, which transforms strings from $\Delta^*$ to strings
      from $\Gamma^*$.

      \hspace*{1.5ex}
      Give an algorithm for the composition of $N$ and $M$;
      that is, the output of the algorithm is to be a single finite-state transducer
      $P = N \composition M$ that transforms strings from $\Sigma^*$ directly to $\Gamma^*$,
      such that $P$ gives the same transduction that we would have if
      $M$ were to be applied first and then $N$ applied to $M$'s output.
      (Of course, since $P$ is a \emph{single} finite-state transducer, there
      is no opportunity for it to produce any kind of ``intermediate string.'')

      \hspace*{1.5ex}
      {\em Hint/warning}: make sure that you specify how the algorithm handles transitions that
      involve $\epsilon$---i.e., transitions of the form
      $\B{q, \epsilon, b, r}$, $\B{q, a, \epsilon, r}$, and $\B{q, \epsilon, \epsilon, r}$.
    \item
      Give the composed transducer that your construction from Part (b)
      creates when the machine from Part (a) is composed with itself.
  \end{enumerate}
\end{enumerate}


\end{document}
