\documentclass[11pt]{article}

\usepackage[margin=1.45in]{geometry}
\usepackage{proof}
\usepackage{amsmath,amsthm,amssymb}
\usepackage[raiselinks=false,colorlinks=true,citecolor=blue,urlcolor=blue,linkcolor=blue,bookmarksopen=true,dvips]{hyperref}

\setlength{\inferLineSkip}{4pt}

% font{
\usepackage{pxfonts}
%% fix sans serif
\renewcommand\sfdefault{cmss}
\DeclareMathAlphabet{\mathsf}{OT1}{cmss}{m}{n}
\SetMathAlphabet{\mathsf}{bold}{OT1}{cmss}{b}{n}
% }font

\theoremstyle{definition}
\newtheorem{task}{Task}
\newtheorem{ectask}{Extra Credit Task}

\newtheorem*{theorem}{Theorem}
\newtheorem*{lemma}{Lemma}

\newcommand\pimp{\mathrel{\supset}}
\newcommand\pand{\mathrel{\wedge}}
\newcommand\por{\mathrel{\vee}}
\newcommand\ptrue{\top}
\newcommand\pfalse{\bot}
\newcommand\pnot{\neg}
\newcommand\pspades{\spadesuit}
\newcommand\pforall[3]{\forall #1{:}#2.\, #3}
\newcommand\pexists[3]{\exists #1{:}#2.\, #3}
\newcommand\qtri\blacktriangledown
\newcommand\ptri[3]{\qtri #1{:}#2.\, #3}

\newcommand\eqn{\mathrel{=_N}}
\newcommand\z{\mathsf{z}}
\newcommand\s{\mathsf{s}}
\newcommand\Cases{\mathit{Cases}}
\newcommand\ii{\mathsf{i}}
\newcommand\oo{\mathsf{o}}

\newcommand\true{\;\textit{true}}
\newcommand\false{\;\textit{false}}
\newcommand\contra{\#}
\newcommand\seq{\longrightarrow}
\newcommand\Seq{\Longrightarrow}
\newcommand\ddd{\raisebox{0.2em}[1.3em]{$\vdots$}}
\newcommand\com{\raisebox{0.3em}{$\ ,\ \ $}}
\newcommand\hyp[2][]{\infer[#1]{#2}{}}
\newcommand\sub[3]{\infer[#2]{#3}{#1}}
\newcommand\dec{{:}}
\newcommand\has{:}
\newcommand\semi{\,;\,}

\newcommand{\lred}{\mathrel{\raisebox{0.5em}{$\Longrightarrow_R$}}}
\newcommand{\lexp}{\mathrel{\raisebox{0.5em}{$\Longrightarrow_E$}}}

\newcommand{\DD}{\mathcal{D}}
\newcommand{\EE}{\mathcal{E}}
\newcommand{\FF}{\mathcal{F}}
\newcommand{\GG}{\mathcal{G}}

% some URLs used below
\newcommand\tutchGuideURL{http://www.cs.cmu.edu/~fp/courses/15317-f09/software/tutch/doc/html/tutch_ovr.html}
\newcommand\tutchProofTermsURL{http://www.cs.cmu.edu/~fp/courses/15317-f09/software/tutch/doc/html/tutch_4.html\#SEC18}
\newcommand\tutchProofTermsRefURL{http://www.cs.cmu.edu/~fp/courses/15317-f09/software/tutch/doc/html/tutch_9.html\#SEC28}
\newcommand\tutchArithmeticURL{http://www.cs.cmu.edu/~fp/courses/15317-f09/software/tutch/doc/html/tutch_7.html\#SEC21}

\title{Constructive Logic (15-317), Fall 2009 \\
       Assignment 6: Logic Programming and Inversion}
\author{William Lovas \texttt{(wlovas@cs)}}
\date{Out: Thursday, October 15, 2009 \\
      Due: Thursday, October 22, 2009 (before class)}

\begin{document}
\maketitle

This assignment serves to introduce you to Prolog programming by having you
play around with a more realistic representation of natural arithmetic.
You will also have a chance to review the key concepts of
\textit{invertibility} in the context of the quantifiers of first-order
logic.

The written portion of your work (Section~\ref{sec:inversion}) should be
submitted at the beginning of class.  As usual, if you are familiar with
\LaTeX, you are encouraged to use this document as a template for typesetting
your solutions, but you may alternatively write your solutions \textit{neatly}
by hand.

The programming portion of your work (Section~\ref{sec:binary}) should be
submitted via AFS by putting your code in a file \verb'binary.pl' and
copying it to the directory
\begin{verbatim}
  /afs/andrew/course/15/317/submit/<userid>/hw06
\end{verbatim}
where \verb'<userid>' is replaced with your Andrew ID.

\section{Building a Better Number}
\label{sec:binary}

Although we like to use natural numbers defined by $\z$ and $\s$ for logic
programming examples in a classroom setting, such a unary representation
sacrifices a great deal in asymptotic performance.  But a binary
representation with good complexity characteristics is just as possible in
a logic programming language like Prolog.

Consider representing binary numbers as strings of bits using the following
constants:
\begin{itemize}
\item \verb'e' stands for the empty string,
\item \verb'i(B)' stands for a one bit followed by the string \verb'B', and 
\item \verb'o(B)' stands for a zero bit followed by the string \verb'B'.
\end{itemize}

\begin{task}[4 pts]
Define a type predicate \verb'binary(B)' which is true for all bit strings
and false for everything else.
\end{task}

We interpret these bit strings in a little-endian fashion: the bit exposed
at the front is the \textit{least significant bit}.  Under this
interpretation, many algorithms can be written in a relatively natural
inductive fashion.

\begin{task}[10 pts]
Define a predicate \verb'add(B1, B2, B3)' with mode \verb'(+, +, -)' that
computes the sum \verb'B3' of binary numbers \verb'B1' and \verb'B2'.
\end{task}

An alternative representation of binary numbers is Prolog lists of Prolog
integers, where each integer in the list is either \verb'0' or \verb'1'.
%\footnote{We cheerfully ignore the objection that Prolog integers
%themselves offer an equally fine representation of numbers.}

\begin{task}[6 pts]
Define a predicate \verb'convert(B, L)' that converts between bit strings
and lists of \verb'1's and \verb'0's.  Write the clauses in such a way that
the conversion can be run in either direction.
\end{task}

\begin{ectask}[3 extra credit pts]
Can you define a conversion between bit strings and unary natural numbers
that can be run in either direction?
\end{ectask}

\section{Quantifiers and Inversion (20 points)}
\label{sec:inversion}

Recall the sequent calculus rules for quantifiers:
\[
    \infer[{\forall}R]
          {\Sigma \semi \Gamma \Seq \pforall x \tau {A(x)}}
          {(\Sigma, c \dec \tau) \semi \Gamma \Seq A(c)}
    \qquad\quad
    \infer[{\forall}L]
          {\Sigma \semi (\Gamma, \pforall x \tau {A(x)}) \Seq C}
          {\Sigma \vdash t \has \tau
         & \Sigma \semi (\Gamma, \pforall x \tau {A(x)}, A(t)) \Seq C}
\]
\[
    \infer[{\exists}R]
          {\Sigma \semi \Gamma \Seq \pexists x \tau {A(x)}}
          {\Sigma \vdash t \has \tau
         & \Sigma \semi \Gamma \Seq A(t)}
    \qquad\quad
    \infer[{\exists}L]
          {\Sigma \semi (\Gamma, \pexists x \tau {A(x)}) \Seq C}
          {(\Sigma, c \dec \tau) \semi (\Gamma, \pexists x \tau {A(x)}, A(c)) \Seq C}
\]

\subsection{Identity}

The Identity theorem states that assumptions are strong enough to become
conclusions, even when we limit the ``$\mathrm{init}$'' rule to atomic
formulas $P$.

\begin{theorem}[Identity]
For all $A$, we can derive $\Sigma \semi (\Gamma, A) \Seq A$.
\end{theorem}
\begin{proof}
By induction on the structure of $A$.
\end{proof}

\begin{task}[2 pts]
Prove the case of the Identity theorem for $\pforall x \tau {A(x)}$.
\end{task}

\begin{task}[2 pts]
Prove the case of the Identity theorem for $\pexists x \tau {A(x)}$.
\end{task}

Remember that for any term $t$, the proposition $A(t)$ is considered to be
a subformula of the propositions $\pforall x \tau {A(x)}$ and $\pexists x
\tau {A(x)}$.

\subsection{Invertibility}

A rule is said to be \textit{invertible} if its premises entail its
conclusion.  As you learned in the last assignment, the value of
invertibility is that it culls the search space of a goal-directed theorem
prover considerably: invertible rules can often be applied asynchronously
since they simplify the sequent in some way without changing its
provability.

\begin{task}[4 pts]
Is the ${\forall}R$ rule invertible?  If so, prove it; if not, give a
counterexample.
\end{task}

\begin{task}[4 pts]
Is the ${\forall}L$ rule invertible?  If so, prove it; if not, give a
counterexample.
\end{task}

\begin{task}[4 pts]
Is the ${\exists}R$ rule invertible?  If so, prove it; if not, give a
counterexample.
\end{task}

\begin{task}[4 pts]
Is the ${\exists}L$ rule invertible?  If so, prove it; if not, give a
counterexample.
\end{task}

A proof of invertibility for a rule should be an step-by-step argument that
given a deduction of the rule's conclusion, you can construct deductions of
its premises.  A counterexample should be a deduction of an instance of the
rule's conclusion along with an argument that no deduction exists for one
of its premises.

For each task, you may assume the following structural properties of the
sequent calculus:
    \begin{lemma}[Weakening]
    If $\Sigma \semi \Gamma \Seq C$, then $\Sigma \semi (\Gamma, A) \Seq C$.
    \end{lemma}
    \begin{lemma}[Term Weakening]
    If $\Sigma \semi \Gamma \Seq C$, then $(\Sigma, c \dec \tau) \semi \Gamma \Seq C$.
    \end{lemma}
    \begin{lemma}[Contraction]
    If $\Sigma \semi (\Gamma, A, A) \Seq C$, then $\Sigma \semi (\Gamma, A) \Seq C$.
    \end{lemma}
    \begin{lemma}[Identity]
    For all $A$, we can derive $\Sigma \semi (\Gamma, A) \Seq A$.
    \end{lemma}
    \begin{lemma}[Cut]
    If $\Sigma \semi \Gamma \Seq A$ and $\Sigma \semi (\Gamma, A) \Seq C$,
    then $\Sigma \semi \Gamma \Seq C$.
    \end{lemma}

\vspace{1em}

\begin{ectask}[3 extra credit pts]
If you were adding quantifiers to the restrictive sequent calculus $\Sigma
\semi \Gamma \seq C$, how would you change the above rules?  How would you
justify these changes?
\end{ectask}

\end{document}
