\documentclass[t,compress]{beamer}
\mode<presentation>

\newcommand{\lecnumber}{2}

\include{cocmacros}
\newcommand{\uuline}[1]{\underline{\underline{#1}}}
\newcommand{\ures}[3]{
  \begin{array}{c@{\hspace*{2em}}c} 
    #1 & #2 \\ \hline \multicolumn{2}{c}{#3} 
  \end{array}}

\title{\lecnumber. First-Order Logic (FOL)}
\date{}
\institute[Stanford University]

\begin{document}

\setbeamertemplate{footline}[text line]
{
	%\begin{large}
	\hspace*{\stretch{1}}\lecnumber - \insertframenumber \bigskip
	%\end{large}
}

\begin{frame}[c]
\Large
\textsc{The Calculus of Computation:}\\
Decision Procedures with\\
Applications to Verification

\bigskip
\bigskip
by\\
Aaron Bradley\\
Zohar Manna

\bigskip
\bigskip
Springer 2007
\end{frame}

\begin{frame}
	\titlepage
\end{frame}

\begin{frame}
\frametitle{First-Order Logic (FOL)}
Also called \underline{Predicate Logic} or \underline{Predicate Calculus}\\
\bigskip
{\Large FOL Syntax}\\
\quad\begin{tabular}{l@{\hspace*{2em}}l}
\underline{variables} & $x, y, z, \cdots$ \\
\underline{constants} & $a, b, c, \cdots$ \\
\underline{functions} & $f, g, h, \cdots$ \\
\underline{terms} & variables, constants or \\
& n-ary function applied to n terms as arguments \\
& $a, x, f(a), g(x, b), f(g(x, g(b)))$\\
\underline{predicates} & $p, q, r, \cdots$ \\
\underline{atom} & $\true$, $\false$, or an n-ary predicate applied to n terms \\
\underline{literal} & atom or its negation \\
& $p(f(x), g(x, f(x))),\quad \neg p(f(x), g(x, f(x)))$
\end{tabular}

\bigskip
\begin{tabular}{@{}ll}
\underline{Note}: & 0-ary functions: constant\\
& 0-ary predicates: $P, Q, R, \dots$
\end{tabular}
\end{frame}

\begin{frame}
\underline{quantifiers}\\
\smallskip
\quad existential quantifier \quad $\exists x.F[x]$\\
\qquad ``there exists an $x$ such that $F[x]$''\\
\smallskip
\quad universal quantifier \quad $\forall x.F[x]$\\
\qquad ``for all $x$, $F[x]$''\\
\bigskip
\underline{FOL formula} \quad literal, application of logical connectives\\
\quad ($\bnot, \bor, \band, \bimplies, \biff$) to formulae,\\
\quad or application of a quantifier to a formula\\
\end{frame}

\begin{frame}
\underline{Example}: \quad FOL formula
\[
\forall x.\ \underbrace{
	p(f(x), x) \bimplies (\exists y.\ \underbrace{p(f(g(x, y)), g(x, y))}_{G})
	\band q(x, f(x))}_{F}
\]
\quad The scope of $\forall x$ is $F$.\\
\quad The scope of $\exists y$ is $G$.\\
\quad The formula reads:\\
\qquad ``for all x,\\
\qquad if $p(f(x), x)$\\
\qquad then there exists a $y$ such that\\
\qquad $p(f(g(x, y)), g(x, y))$ and $q(x, f(x))$''
\end{frame}

\begin{frame}
\underline{Translations of English Sentences into FOL}\\
\begin{itemize}
\item The length of one side of a triangle is less than the sum of the lengths of the other two sides
\[
\forall x,y,z.\ triangle(x,y,z) \bimplies length(x) < length(y) + length(z)
\]
\item Fermat's Last Theorem.
\[
\begin{array}{l}
\forall n.\ integer(n) \band n > 2 \\
\hspace*{1em}\begin{array}{l}
\bimplies \forall x,y,z. \\
\hspace*{1em}\begin{array}{l}
integer(x) \band integer(y) \band integer(z)\\
\band x > 0 \band y > 0 \band z > 0 \\
\hspace*{1em} \bimplies x^n + y^n \ne z^n
\end{array}\end{array}\end{array}
\]
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{\uuline{FOL Semantics}}
An interpretation $I: (\idom, \iasgn)$ consists of:
\begin{itemize}
\item Domain $\idom$\\
non-empty set of values or objects\\
\begin{tabular}{@{}ll}
cardinality $|\idom|$ & finite (eg, 52 cards),\\
& countably infinite (eg, integers), or\\
& uncountably infinite (eg, reals)
\end{tabular}
\item Assignment $\iasgn$
	\begin{itemize}
	\item each variable $x$ assigned value $x_I \in \idom$
	\item each n-ary function $f$ assigned
	\[ f_I:\ \idom^n \to \idom \]
	In particular, each constant $a$ (0-ary function) assigned value $a_I \in \idom$
	\item each n-ary predicate $p$ assigned
	\[ p_I:\ \idom^n \to \set{\underline{\truev},\ \underline{\falsev}} \]
	In particular, each propositional variable $P$ (0-ary predicate) assigned truth value (\underline{$\truev$}, \underline{$\falsev$})
	\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}
\underline{Example}:\\
\qquad $F:\ p(f(x,y),z) \bimplies p(y,g(z,x))$\\
\bigskip
Interpretation $I: (\idom, \iasgn)$\\
\qquad $\idom = \mathbb{Z} = \set{\cdots, -2, -1, 0, 1, 2, \cdots}$ \quad integers\\
\qquad $\iasgn : \set{f\mapsto +, g\mapsto -, p\mapsto >}$\\
\qquad Therefore, we can write
\[ F_I: x+y>z \bimplies y>z-x \]
\qquad (This is the way we'll write it in the future!)\\
\quad Also\\
\qquad $\iasgn : \set{x\mapsto 13, y\mapsto 42, z\mapsto 1}$\\
\qquad Thus
\[ F_I: 13+42>1 \bimplies 42>1-13 \]
Compute the truth value of $F$ under $I$
\[
\begin{nsemantics}
1. & I & \Models & x + y > z &
\mbox{since $13 + 42 > 1$} \\
2. & I & \Models & y > z - x &
\mbox{since $42 > 1 - 13$} \\
3. & I & \Models & F &
\mbox{by 1, 2, and $\bimplies$}
\end{nsemantics}
\]
$F$ is \underline{$\truev$} under $I$
\end{frame}

\begin{frame}
\frametitle{Semantics: Quantifiers}
$x$ variable.\\
\underline{$x$-variant} of interpretation $I$ is an interpretation $J: (\jdom, \jasgn)$ such that
\begin{itemize}
\item $\idom = \jdom$
\item $\iasgn[y] = \jasgn[y]$ for all symbols $y$, except possibly $x$
\end{itemize}
That is, $I$ and $J$ agree on everything except possibly the value of $x$\\
\bigskip
Denote $J:\ I \icomp{x}{\dval{v}}$ the $x$-variant of $I$ in which  $\jasgn[x] = \dval{v}$ for some $\dval{v} \in \idom$.
Then
\begin{itemize}
\item $I \Models \forall x.\ F$
\quad\mbox{iff for all $\dval{v} \in \idom$, $I \icomp{x}{\dval{v}} \Models F$} 
\item $I \Models \exists x.\ F$
\quad\mbox{iff there exists $\dval{v} \in \idom$ s.t. $I \icomp{x}{\dval{v}} \Models F$}
\end{itemize}
\end{frame}

\begin{frame}
\underline{Example}\\
\smallskip
For $\mathbb{Q}$, the set of rational numbers, consider
\[ F_I: \forall x.\ \exists y.\ 2 \times y = x \]

Compute the value of $F_I$ ($F$ under $I$):\\
\smallskip
\quad Let
\[ \begin{tabular}{l@{\hspace*{3em}}l}
	$J_1: I \icomp{x}{\dval{v}}$ & $J_2: J_1 \icomp{y}{\frac{\dval{v}}{2}}$\\
	$x$-variant of $I$ & $y$-variant of $J_1$
\end{tabular}
\]
\quad for $\dval{v} \in \mathbb{Q}$.\\
\bigskip
Then
\[
\begin{nsemantics}
1. & J_2 & \Models & 2 \times y = x &
\mbox{since } 2 \times \frac{\dval{v}}{2} = v\\
2. & J_1 & \Models & \exists y.\ 2 \times y = x &\\
3. & I & \Models & \forall x.\ \exists y.\ 2 \times y = x &
\mbox{since $\dval{v} \in \rationals$ is arbitrary}
\end{nsemantics}
\]
\end{frame}

\begin{frame}
\frametitle{Satisfiability and Validity}
$F$ is \underline{satisfiable} iff there exists $I$ s.t. $I\Models F$\\
$F$ is \underline{valid} iff for all $I$, $I\Models F$\\
\smallskip
\begin{center} $F$ is valid iff $\neg F$ is unsatisfiable \end{center}
\smallskip
\underline{Example}: \quad $F:\ (\forall x.\ p(x)) \biff (\bnot \exists x.\ \bnot p(x))$ \quad valid?\\
\smallskip
Suppose not. Then there is $I$ s.t.
$\begin{nsemantics}
0. & \qquad I & \Nmodels & (\forall x.\ p(x)) \biff (\bnot \exists x.\ \bnot p(x)) & \mbox{}
\end{nsemantics}$\\
\smallskip
First case 
\[
\begin{nsemantics}
1. & I & \Models & \forall x.\ p(x) & \mbox{assumption} \\
2. & I & \Nmodels & \bnot \exists x.\ \bnot p(x) & \mbox{assumption} \\
3. & I & \Models & \exists x.\ \bnot p(x) & \mbox{2 and $\bnot$} \\
4. & I \icomp{x}{\dval{v}} & \Models & \bnot p(x) & \mbox{3 and $\exists$, for some $\dval{v} \in \idom$} \\
5. & I \icomp{x}{\dval{v}} & \Models & p(x) & \mbox{1 and $\forall$}
\end{nsemantics}
\]
4 and 5 are contradictory.
\end{frame}

\begin{frame}
Second case
\[
\begin{nsemantics}
1. & I & \Nmodels & \forall x.\ p(x) & \mbox{assumption} \\
2. & I & \Models & \bnot \exists x.\ \bnot p(x) & \mbox{assumption} \\
3. & I \icomp{x}{\dval{v}} & \Nmodels & p(x) & \mbox{1 and $\forall$, for some $\dval{v} \in \idom$} \\
4. & I & \Nmodels & \exists x.\ \bnot p(x) & \mbox{2 and $\bnot$} \\
5. & I \icomp{x}{\dval{v}} & \Nmodels & \bnot p(x) & \mbox{4 and $\exists$} \\
6. & I \icomp{x}{\dval{v}} & \Models & p(x) & \mbox{5 and $\bnot$}
\end{nsemantics}
\]
3 and 6 are contradictory.\\
Both cases end in contradictions for arbitrary $I \Bimplies F$ is valid.
\end{frame}

\begin{frame}
\underline{Example}: \quad Prove\\
\smallskip
\qquad $F:\ p(a) \bimplies \exists x.\ p(x)$ \quad is valid.\\
\bigskip
Assume otherwise.
\[
\begin{nsemantics}
1. & I & \Nmodels & F & \mbox{assumption} \\
2. & I & \Models & p(a) & \mbox{1 and $\bimplies$} \\
3. & I & \Nmodels & \exists x.\ p(x) & \mbox{1 and $\bimplies$} \\
4. & I\icomp{x}{\iasgn[a]} & \Nmodels & p(x) & \mbox{3 and $\exists$} \\
\end{nsemantics}
\]
2 and 4 are contradictory. Thus, $F$ is valid.
\end{frame}

\begin{frame}
\underline{Example}: Show\\
\smallskip
\qquad $F:\ (\forall x.\ p(x, x)) \bimplies (\exists x.\ \forall y.\ p(x, y))$ \quad is invalid.\\
\bigskip
Find interpretation $I$ such that
\[
I \Models \bnot [(\forall x.\ p(x, x)) \bimplies (\exists x.\ \forall y.\ p(x, y))]
\]
\qquad i.e.
\[
I \Models (\forall x.\ p(x, x)) \band \bnot (\exists x.\ \forall y.\ p(x, y))
\]

\begin{tabular}{@{}lll@{\;}l}
Choose & $\idom = \set{0, 1}$\\
& $p_I = \set{(0, 0),\ (1, 1)}$ & i.e.& $p_I(0, 0)$ and $p_I(1, 1)$ are $\truev$\\
&&& $p_I(1, 0)$ and $p_I(1,0)$ are $\falsev$
\end{tabular}\\
\bigskip
$I$ falsifying interpretation $\Bimplies$ $F$ is invalid.
\end{frame}

\begin{frame}
\frametitle{Safe Substitution $F\sigma$}
\underline{Example}:
\[
	\begin{array}{r@{\ }r@{}c@{\ }l}
		F: (\forall x.\ \overbrace{p(x, y)}^{\mbox{scope of }\forall x}) \bimplies & q(f(y & ), &x)\\
		$bound by$\ \forall x {\ }^\nearrow\; {\;}^\nwarrow\ $free$\ \ \ \ & $free${\ }^\nearrow\; & & {\;}^\nwarrow\ $free$
	\end{array}
\]
\qquad $free(F) = \set{x,\ y}$\\
\smallskip
substitution
\[
	\sigma: \set{x\mapsto g(x),\ y\mapsto f(x),\ q(f(y),x)\mapsto \exists x.\ h(x,y)}
\]
$F\sigma$?\\
\begin{enumerate}
\item Rename
\[
	\begin{array}{c@{}c@{}c@{}c@{}c}
	F': \forall &x'&.\ p(&x'&,y)\bimplies q(f(y), x)\\
	            &\uparrow&&\uparrow
	\end{array}
\]
\begin{flushright}where $x'$ is a fresh variable\end{flushright}
\item $F' \sigma: \forall x'.\ p(x', f(x)) \bimplies \exists x.\ h(x,y)$
\end{enumerate}
\end{frame}

\begin{frame}
\underline{Rename $x$ by $x'$}:\\
\medskip
replace $x$ in $\forall x$ by $x'$ and all free $x$ in the scope of $\forall x$ by $x'$.
\[ \forall x.\ G[x] \quad \Bequiv \quad \forall x'.\ G[x'] \]
Same for $\exists x$
\[ \exists x.\ G[x] \quad \Bequiv \quad \exists x'.\ G[x'] \]
where $x'$ is a fresh variable

\bigskip
\uuline{Proposition (Substitution of Equivalent Formulae)}
\[
	\sigma: \set{F_1 \mapsto G_1,\ \cdots,\ F_n \mapsto G_n}
\]
s.t. for each $i$, $F_i \Bequiv G_i$\\
\bigskip
If $F \sigma$ a safe substitution, then $F \Bequiv F \sigma$
\end{frame}

\begin{frame}
\frametitle{Formula Schema}
\underline{Formula}\\
\smallskip
\qquad $(\forall x.\ p(x)) \biff (\neg \exists x.\ \neg p(x))$

\bigskip
\underline{Formula Schema}\\
\smallskip
\qquad
\begin{tabular}{@{}r@{}c@{}l}
$H_1: (\forall x.\ $&$F$&$) \biff (\neg \exists x.\ \neg F)$\\
&${}^\uparrow$& place holder
\end{tabular}

\bigskip
\underline{Formula Schema (with side condition)}\\
\smallskip
\qquad $H_2: (\forall x.\ F) \biff F$ \quad provided $x \notin free(F)$

\bigskip
\underline{Valid Formula Schema}\\
\smallskip
\qquad $H$ is valid iff valid for any FOL formula $F_i$ obeying the side conditions

\bigskip
\underline{Example}: $H_1$ and $H_2$ are valid.
\end{frame}

\begin{frame}
\underline{Substitution $\sigma$ of $H$}\\
\[ \sigma: \set{F_1 \mapsto \quad , \dots, F_n \mapsto \quad } \]
\qquad mapping place holders $F_i$ of $H$ to FOL formulae,\\
\qquad (obeying the side conditions of $H$)\\

\bigskip
\uuline{Proposition (Formula Schema)}\\
\smallskip
\qquad If $H$ is valid formula schema and\\
\qquad \quad $\sigma$ is a substitution obeying $H$'s side conditions\\
\qquad then $H \sigma$ is also valid.

\bigskip
\underline{Example}:\\
\smallskip
\qquad $H: (\forall x.\ F) \biff F$ \quad provided $x \notin free(F)$ \qquad is valid\\
\smallskip
\qquad $\sigma: \set{F\mapsto p(y)}$ \qquad obeys the side condition\\

\bigskip
\qquad Therefore $H \sigma : \forall x.\ p(y) \biff p(y)$ \qquad is valid
\end{frame}

\begin{frame}
\frametitle{Proving Validity of Formula Schema}
\underline{Example}: Prove validity of\\
\smallskip
\qquad $H: (\forall x.\ F) \biff F$ \quad provided $x \notin free(F)$\\
\bigskip
Proof by contradiction. Consider the two directions of $\biff$.\\
First case:
\[
\begin{nsemantics}
1. & I & \Models & \forall x.\ F & \mbox{assumption} \\
2. & I & \Nmodels & F & \mbox{assumption} \\
3. & I & \Models & F & \mbox{1, $\forall$, since $x \not\in \freev(F)$}\\
4. & I & \Models & \false & \mbox{2, 3}
\end{nsemantics}
\]

Second Case:
\[
\begin{nsemantics}
1. & I & \Nmodels & \forall x.\ F & \mbox{assumption} \\
2. & I & \Models & F & \mbox{assumption} \\
3. & I & \Models & \exists x.\ \bnot F & \mbox{1 and $\bnot$} \\
4. & I & \Models & \bnot F & \mbox{3, $\exists$, since $x \not\in \freev(F)$}\\
5. & I & \Models & \false & \mbox{2, 4}
\end{nsemantics}
\]

Hence, $H$ is a valid formula schema.
\end{frame}

\begin{frame}
\frametitle{Normal Forms}
\underline{1. Negation Normal Forms (NNF)}\\
\smallskip
Augment the equivalence with (left-to-right)
\[
\bnot \forall x.\ F[x] \Bequiv \exists x.\ \bnot F[x]
\]
\[
\bnot \exists x.\ F[x] \Bequiv \forall x.\ \bnot F[x]
\]

\underline{Example}\\
\smallskip
\[
G:\ \forall x.\ (\exists y.\ p(x,y) \band p(x,z)) \bimplies \exists w. p(x, w)~.
\]

\begin{enumerate}
\item $\forall x.\ (\exists y.\ p(x,y) \band p(x,z)) \bimplies \exists w.\ p(x, w)$
\item $\forall x.\ \bnot (\exists y.\ p(x,y) \band p(x,z)) \bor \exists w.\ p(x, w)$\\
{\hspace{\stretch{1}} $F_1\bimplies F_2 \Bequiv \neg F_1 \bor F_2$}
\item $\forall x.\ (\forall y.\ \bnot (p(x,y) \band p(x,z))) \bor \exists w.\ p(x, w)$\\
\hspace{\stretch{1}} $\neg \exists x.\ F[x] \Bequiv \forall x.\ \neg F[x]$
\item $\forall x.\ (\forall y.\ \bnot p(x,y) \bor \bnot p(x,z)) \bor \exists w.\ p(x, w)$
\end{enumerate}
\end{frame}

\begin{frame}[allowframebreaks] 
\underline{2. Prenex Normal Form (PNF)}\\
\smallskip
All quantifiers appear at the beginning of the formula\\
\medskip
\qquad $Q_1 x_1 \cdots Q_n x_n.\ F[x_1, \cdots, x_n]$\\
\medskip
where $Q_i \in \set{\forall,\ \exists}$ and $F$ is quantifier-free.\\
\bigskip
Every FOL formula $F$ can be transformed to formula $F'$ in PNF s.t. $F'\Bequiv F$.\\
\bigskip
\underline{Example}: Find equivalent PNF of
\[
\begin{array}{r@{}l}
F:\ \forall x&.\ \bnot (\exists y.\ p(x,y) \band p(x,z)) \bor \exists y.\ p(x, y)\\
&{}^\uparrow$ to the end of the formula$
\end{array}
\]

\begin{enumerate}
\item Write $F$ in NNF
\[
F_1:\ \forall x.\ (\forall y.\ \bnot p(x,y) \bor \bnot p(x,z)) \bor \exists y.\ p(x, y)
\]
\item Rename quantified variables to fresh names
\[
\begin{array}{r@{}l}
F_2:\ \forall x.\ (\forall &y.\ \bnot p(x,y) \bor \bnot p(x,z)) \bor \exists w.\ p(x, w)\\
&{}^\uparrow $ in the scope of $ \forall x
\end{array}
\]
\item Remove all quantifiers to produce quantifier-free formula
\[
F_3:\ \bnot p(x,y) \bor \bnot p(x,z) \bor p(x,w)
\]
\item Add the quantifiers before $F_3$
\[
F_4:\ \forall x.\ \forall y.\ \exists w.\ \bnot p(x,y) \bor \bnot p(x,z) \bor p(x,w)
\]
Alternately,
\[
F_4':\ \forall x.\ \exists w.\ \forall y.\ \bnot p(x,y) \bor \bnot p(x,z) \bor p(x,w)
\]
\underline{Note}: In $F_2$, $\forall y$ is \underline{in the scope} of $\forall x$, 
therefore the order of quantifiers must be $\cdots\forall x\cdots\forall y\cdots$

\begin{center}
\fbox{$F_4 \Bequiv F$ and $F_4' \Bequiv F$}
\end{center}

\underline{Note}: However $G \ \nLeftrightarrow\ F$
\[
G:\ \forall \rnode{Y}{y}.\ \exists w.\ \forall \rnode{X}{x}.\ \bnot p(x,y) \bor \bnot p(x,z) \bor p(x,w)
\ncarc[arcangle=-60,angle=270]{<->}{Y}{X}
\]
\end{enumerate}
\end{frame}

\begin{frame}
\frametitle{Decidability of FOL}
\begin{itemize}
\item \underline{FOL is undecidable} (Turing \& Church)\\
There does not exist an algorithm for deciding if a FOL formula $F$ is valid,
i.e. always halt and says ``yes'' if $F$ is valid or say ``no'' if $F$ is invalid.
\item \underline{FOL is semi-decidable}\\
There is a procedure that always halts and says ``yes'' if $F$ is valid,
but may not halt if $F$ is invalid.
\end{itemize}

On the other hand,
\begin{itemize}
\item \underline{PL is decidable}\\
There does exist an algorithm for deciding if a PL formula $F$ is valid,
e.g. the truth-table procedure.
\end{itemize}

\begin{center}
Similarly for satisfiability
\end{center}
\end{frame}

\begin{frame}
\frametitle{Semantic Argument Proof}
To show FOL formula $F$ is valid,
assume $I \Nmodels F$ and derive a contradiction $I \Models \false$ in all branches

\begin{itemize}
\item \underline{Soundness}\\
If every branch of a semantic argument proof reach $I \Models \false$, then $F$ is valid
\item \underline{Completeness}\\
Each valid formula $F$ has a semantic argument proof in which every branch reach $I \Models \false$
\end{itemize}
\end{frame}
\end{document}

