\documentclass[10pt]{article}

\usepackage{times,mathptm}
\pagestyle{myheadings}



\parindent 0in
\setlength{\parindent}{0in}
\setlength{\parskip}{1ex}

\topmargin -0.1in \headheight\baselineskip
\textheight 8.5in         % 1in top and bottom margin
\textwidth 6.5in        % 1in left and right margin
\oddsidemargin 0in
\evensidemargin 0in




\usepackage{amsmath}
\usepackage{amssymb}


% \setlength{\itemsep}{0pt}

\markright{\footnotesize CS 451 Programming Paradigms, Spring 2001}

\begin{document}

\section*{Homework set 6: Programming in Prolog --- due Wednesday 21 February}

Total number of points available on this homework is 100. Full credit
is equivalent to 100 points.

%\small

\begin{enumerate}



\item (100 pts.) 
Recall from class the language over the alphabet $\{a,b,c\}$, 
$L=\{a^n b^n c^n \mid n \in \mathbb{N}\}$.
It is generated by the following grammar:
\[G=(\{S,X,Y\}, \{a,b,c\}, S, F),\]
where the productions are:
\[F=\{S \rightarrow abc,
S \rightarrow  aXbc,
Xb \rightarrow  bX,
Xc \rightarrow  Ybcc,
bY \rightarrow  Yb,
aY \rightarrow  aaX,
aY \rightarrow  aa\}\]

This grammar is not context-free, because on the left-hand sides of some
productions there are strings longer than single non-terminals.
(In fact, there is no context-free grammar for this language.)
However, the productions in this grammar satisfy a
\emph{length-increasing} property: the number of symbols on the right is
never less than the number on the left. Such grammars are called
context-sensitive. (Note that there are grammars, and their languages,
that are not even context-sensitive.)

Write a Prolog program that determines whether a string belongs to
the language $L$. Strings over the alphabet $\{a,b,c\}$ will be represented as 
Prolog lists of Prolog atoms \texttt{a}, \texttt{b}, and \texttt{c}.
You should split the program into a set of
predicates that are general-purpose (for any context-sensitive
grammar), and a set of predicates that describe the grammar $G$.


The program should result in queries like these:

\begin{verbatim}
?- accepts([]).
No
?- accepts([a]).
No
?- accepts([a,b]).
No
?- accepts([a,b,b]).
No
?- accepts([a,b,c]).
Yes
?- accepts([a,a,b,b,c,c]).
Yes
?- accepts([a,a,a,a,a,a,b,b,b,b,b,b,c,c,c,c,c,c]).
Yes
?- accepts([a,a,a,a,a,a,b,b,b,b,b,b,c,c,c,c,c,c,a]).
No
\end{verbatim}



You may find useful the predefined predicate \texttt{length/2},
which works as follows: given a list \texttt{X}, the goal
\texttt{length(X,N)} succeeds and sets N to the integer equal
to the length of the list \texttt{X}.

You should test your program on the queries above and others you
find useful to cover the various cases of strings in the language
and outside the language. Make sure that your program does not go
into an infinite loop on any inputs. Do not submit your program unless
it has passed all these tests.

Submit the listing of the program and your test queries, as hard copy.

\end{enumerate}


\end{document}

