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

\newcommand{\lecnumber}{9}

\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}}
\newcommand{\uex}{\underline{Example}}
\newcommand{\un}{\underline{Note}}
\newcommand{\utitle}[1]{\underline{#1}\\\smallskip}

\title{\lecnumber. Quantifier-free Equality and Data Structures}
\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{The Theory of Equality $T_E$}
\qquad $\Sigma_E:\ \set{=,\ a,\ b,\ c,\ \ldots,\ f,\ g,\ h,\ \ldots,\ p,\ q,\ r,\ \ldots}$

\smallskip
uninterpreted symbols:\\
\begin{tabular}{@{$\bullet$\ }ll}
constants & $a,b,c,\ldots$\\
functions & $f,g,h,\ldots$\\
predicates & $p,q,r,\ldots$
\end{tabular}

\bigskip
\uex:\\
\smallskip
\quad $x=y \band f(x) \neq f(y)$ \qquad $T_E$-unsatisfiable\\
\smallskip
\quad $f(x)=f(y) \band x \neq y$ \qquad $T_E$-unsatisfiable\\
\smallskip
\quad $f(f(f(a))) = a \band f(f(f(f(f(a))))) = a \band f(a) \neq a$\\
\hspace{\stretch{1}} $T_E$-unsatisfiable\\
\end{frame}

\begin{frame}
\underline{Axioms of $T_E$}
\begin{enumerate}
\item $\forall x.\ x = x$ \naxiom{reflexivity}
\item $\forall x,y.\ x = y \bimplies y = x$ \naxiom{symmetry}
\item $\forall x,y,z.\ x = y \band y = z \bimplies x = z$ \naxiom{transitivity}
\end{enumerate}
define $=$ to be an \underline{equivalence relation}.

\smallskip
Axiom schema
\begin{enumerate}
\setcounter{enumi}{3}
\item for each positive integer $n$ and $n$-ary function symbol $f$,\\
\smallskip
$\forall x_1,\ldots,x_n,y_1,\ldots,y_n.\ \bigwedge_i x_i = y_i$\\
\qquad $\bimplies f(x_1,\ldots,x_n) = f(y_1,\ldots,y_n)$
\naxiom{congruence}
\end{enumerate}
\smallskip
For example,\\
\smallskip
\qquad $\forall x,y.\ x = y \bimplies f(x) = f(y)$\\
\smallskip
Then\\
\smallskip
\qquad $x = g(y,z) \bimplies f(x) = f(g(y,z))$\\
\smallskip
is $T_E$-valid.
\end{frame}

\begin{frame}
Axiom schema
\begin{enumerate}
\setcounter{enumi}{4}
\item for each positive integer $n$ and $n$-ary predicate symbol $p$,\\
\smallskip
$\displaystyle \forall x_1,\ldots,x_n,y_1,\ldots,y_n.\ \bigwedge_i x_i = y_i \bimplies$\\
\qquad $\displaystyle (p(x_1,\ldots,x_n) \biff p(y_1,\ldots,y_n))$
\naxiom{equivalence}
\end{enumerate}

\bigskip
Thus,
\[
x = y \bimplies (p(x) \biff p(y))
\]
is $T_E$-valid.
\end{frame}

\begin{frame}
\underline{We discuss $T_E$-formulae without predicates}\\
\smallskip
For example, for $\Sigma_E$-formula
\[
F:\ p(x) \band q(x,y) \band q(y,z) \bimplies \bnot q(x,z)
\]
introduce fresh constant $\bullet$ and fresh functions $f_p$ and $f_g$, and transform $F$ to
\[
G:\ f_p(x) = \bullet \band f_q(x,y) = \bullet \band f_q(y,z) = \bullet \bimplies f_q(x,z) \ne \bullet~.
\]
\end{frame}

\begin{frame}
\frametitle{Equivalence and Congruence Relations: Basics}
Binary relation $R$ over set $S$\\
\smallskip
$\bullet$ is an \underline{equivalence relation} if
\begin{itemize}
\item reflexive: $\forall s \in S.\ s R s$;
\item symmetric: $\forall s_1,s_2 \in S.\ s_1 R s_2 \bimplies s_2 R s_1$;
\item transitive: $\forall s_1,s_2,s_3 \in S.\ s_1 R s_2 \band s_2 R s_3 \bimplies s_1 R s_3$.
\end{itemize}

\smallskip
\uex:\\
Define the binary relation $\equiv_2$ over the set $\integers$ of integers\\
\smallskip
\qquad $m \equiv_2 n \quad\mbox{iff}\quad (m\ \mathsf{mod}\ 2) = (n\ \mathsf{mod}\ 2)$\\
\smallskip
That is, $m, n \in \integers$ are related iff they are both even or both odd.\\
$\equiv_2$ is an equivalence relation

\smallskip
$\bullet$ is a \underline{congruence relation} if in addition
\[
\forall \vectr{s},\vectr{t}.\
\bigwedge_{i=1}^n\
s_i R t_i
\bimplies
f(\vectr{s}) R f(\vectr{t})~.
\]
\end{frame}

\newcommand{\eqcon}{$\left\{ \begin{tabular}{c} equivalence\\congruence \end{tabular} \right\}$}
\begin{frame}
\underline{Classes}\\
\smallskip
For \eqcon relation $R$ over set $S$,\\
The $\left\{ \begin{tabular}{c} \underline{equivalence}\\\underline{congruence} \end{tabular} \right\}$ \underline{class} of $s \in S$ under $R$ is
\[
\eclass{s}{R} \eqdef \set{s' \in S\ :\ s R s'}~.
\]
\uex:\\
\smallskip
The equivalence class of $3$ under $\equiv_2$ over $\integers$ is
\[
\eclass{3}{\equiv_2} = \set{n \in \integers\ :\ \mbox{$n$ is odd}}~.
\]

\bigskip
\underline{Partitions}\\
\smallskip
A \underline{partition} $P$ of $S$ is a set of subsets of $S$ that is
\begin{itemize}
\item \underline{total} \quad $\displaystyle \left(\bigcup_{S' \in P} S'\right) = S$
\item \underline{disjoint} \quad $\displaystyle \forall S_1,S_2 \in P.\ S_1 \cap S_2 = \emptyset$
\end{itemize}
\end{frame}

\begin{frame}
\underline{Quotient}\\
\smallskip
The \underline{quotient} $S/R$ of $S$ by \eqcon relation $R$
is the set of \eqcon classes
\[
S/R\ =\ \set{\eclass{s}{R}\ :\ s \in S}~.
\]
It is a partition

\bigskip
\uex: The quotient $\integers/\equiv_2$ is a partition of $\integers$. The set of
equivalence classes
\[
\set{\set{n \in \integers\ :\ \mbox{$n$ is odd}},\ \set{n \in \integers\ :\ \mbox{$n$ is even}}}
\]

\bigskip
Note duality between relations and classes
\end{frame}

\begin{frame}
\underline{Refinements}\\
\smallskip
Two binary relations $R_1$ and $R_2$ over set $S$.\\
$R_1$ is \underline{refinement} of $R_2$, $R_1 \prec R_2$, if
\[
\forall s_1,s_2 \in S.\ s_1 R_1 s_2 \bimplies s_1 R_2 s_2~.
\]
$R_1$ \underline{refines} $R_2$.

\bigskip
\underline{Examples}:\\
\begin{itemize}
\item For $S = \set{a,b}$,\\
\quad $R_1: \set{a R_1 b} \qquad R_2: \set{a R_2 b,\ b R_2 b}$\\
Then $R_1 \prec R_2$

\item For set $S$,\\
\quad \begin{tabular}{@{}ccl}
$R_1$ & induced by the partition & $P_1: \set{\set{s}\ :\ s \in S}$\\
$R_2$ & induced by the partition & $P_2: \set{S}$
\end{tabular}

Then $R_1 \prec R_2$.

\item For set $\integers$\\
\quad $R_1: \set{xR_1y\ :\ x\ \mathsf{mod}\ 2 = y\ \mathsf{mod}\ 2}$\\
\quad $R_2: \set{xR_2y\ :\ x\ \mathsf{mod}\ 4 = y\ \mathsf{mod}\ 4}$

Then $R_2 \prec R_1$.
\end{itemize}
\end{frame}

\begin{frame}
\underline{Closures}

\smallskip
Given binary relation $R$ over $S$.\\
The \underline{equivalence closure} $R^E$ of $R$ is the equivalence relation s.t.
\begin{itemize}
\item $R$ refines $R^E$, i.e. $R \prec R^E$;
\item for all other equivalence relations $R'$ s.t. $R \prec R'$,\\
  \quad either $R' = R^E$ or $R^E \prec R'$
\end{itemize}
That is, $R^E$ is the ``smallest'' equivalence relation that
``covers'' $R$.

\bigskip
\uex:
If $S = \set{a,b,c,d}$ and $R = \set{aRb, bRc, dRd}$, then\\
\begin{tabular}{@{\quad $\bullet$\ }ll}
$aRb, bRc, dRd \in R^E$ & since $R \subseteq R^E$;\\
$aRa, bRb, cRc \in R^E$ & by reflexivity;\\
$bRa, cRb \in R^E$ & by symmetry;\\
$aRc \in R^E$ & by transitivity;\\
$cRa \in R^E$ & by symmetry.\\
\end{tabular}

Hence,\\
\smallskip
\quad $R^E = \set{aRb,bRa,aRa,bRb,bRc,cRb,cRc,aRc,cRa,dRd}~.$

\bigskip
Similarly, the \underline{congruence closure} $R^C$ of $R$ is the
``smallest'' congruence relation that ``covers'' $R$.  
\end{frame}

\begin{frame}
\frametitle{Congruence Closure Algorithm}
Given $\Sigma_E$-formula
\[
F:\ s_1 = t_1 \band \cdots \band s_m = t_m \band
s_{m+1} \ne t_{m+1} \band \cdots \band s_n \ne t_n
\]
decide if $F$ is $\Sigma_E$-satisfiable.

\bigskip
\underline{Definition}: For $\Sigma_E$-formula $F$,\\
\qquad the \underline{subterm set} $S_F$ of $F$ is the set that contains precisely\\
\qquad the subterms of $F$.

\bigskip
\uex: The subterm set of
\[
F:\ f(a,b) = a \band f(f(a,b),b) \ne a
\]
is
\[
S_F = \set{a,\ b,\ f(a,b),\ f(f(a,b),b)}~.
\]
\end{frame}

\begin{frame}
\underline{The Algorithm}

\smallskip
Given $\Sigma_E$-formula $F$\\
\smallskip
\quad $F:\ s_1 = t_1 \band \cdots \band s_m = t_m \band
s_{m+1} \ne t_{m+1} \band \cdots \band s_n \ne t_n$\\
\smallskip
with subterm set $S_F$, $F$ is $T_E$-satisfiable iff there exists a
congruence relation $\sim$ over $S_F$ such that
\begin{itemize}
\item for each $i \in \range{1}{m}$, $s_i \sim t_i$;
\item for each $i \in \range{m+1}{n}$, $s_i \not\sim t_i$.
\end{itemize}

\bigskip
Such congruence relation $\sim$ defines $T_E$-interpretation $I:
(\idom,\iasgn)$ of $F$.  $D_I$ consists of $|S_F/\sim|$ elements, one
for each congruence class of $S_F$ under $\sim$.

\bigskip
Instead of writing $I \Models F$ for this $T_E$-interpretation, we abbreviate\\
\qquad $\sim \Models F$

\bigskip
The goal of the algorithm is to construct
the congruence relation of $S_F$, or to prove that
no congruence relation exists.
\end{frame}

\begin{frame}
$F:\underbrace{s_1 = t_1 \band \cdots \band s_m = t_m}_{\mbox{generate congruence closure}} \sband
\ \underbrace{s_{m+1} \ne t_{m+1} \band \cdots \band s_n \ne t_n}_{\mbox{search for contradiction}}$

\bigskip
The algorithm performs the following steps:
\begin{enumerate}
\item Construct the congruence closure $\sim$ of
\[
\set{s_1 = t_1, \ldots, s_m = t_m}
\]
over the subterm set $S_F$.  Then
\[
\sim \Models s_1 = t_1 \band \cdots \band s_m = t_m~.
\]
\item If for any $i \in \range{m+1}{n}$, $s_i \sim t_i$, return {\sf
unsatisfiable}.
\item Otherwise, $\sim \models F$, so return {\sf satisfiable}.
\end{enumerate}

\smallskip
How do we actually construct the congruence closure in Step 1?

\end{frame}

\begin{frame}
Initially, begin with the finest congruence relation $\sim_0$ given by
the partition
\[
\set{\set{s}\ :\ s \in S_F}~.
\]
That is, let each term of $S_F$ be its own congruence class.  \\
\smallskip
Then, for each $i \in \range{1}{m}$, impose $s_i = t_i$ by merging the
congruence classes
\[
\eclass{s_i}{\sim_{i-1}}
\quad\mbox{and}\quad
\eclass{t_i}{\sim_{i-1}}
\]
to form a new congruence relation $\sim_i$.  To accomplish this merging,
\begin{itemize}
\item form the union of $\eclass{s_i}{\sim_{i-1}}$ and $\eclass{t_i}{\sim_{i-1}}$
\item propagate any new congruences that arise within this union.
\end{itemize}
The new relation $\sim_i$ is a congruence relation in which $s_i \sim t_i$.
\end{frame}

\begin{frame}
\uex: Given $\Sigma_E$-formula\\
\smallskip
\qquad \qquad $F:\ f(a,b) = a \band f(f(a,b),b) \ne a$\\
\smallskip
Construct initial partition by letting each member of
the subterm set $S_F$ be its own class:\\
\qquad 1. $\set{\set{a},\ \set{b},\ \set{f(a,b)},\ \set{f(f(a,b),b)}}$\\
\smallskip
\quad According to the first literal $f(a,b) = a$, merge\\
\smallskip
\qquad $\set{f(a,b)}
\quad\mbox{and}\quad
\set{a}$\\
\smallskip
\quad to form partition\\
\qquad 2. $\set{\set{a,f(a,b)},\ \set{b},\ \set{f(f(a,b),b)}}$\\
\smallskip
\quad According to the \axiom{congruence} axiom,\\
\smallskip
\qquad $f(a,b) \sim a,\ b \sim b
\quad\mbox{implies}\quad
f(f(a,b),b) \sim f(a,b)~,$\\
\smallskip
\quad resulting in the new partition\\
\qquad 3. $\set{\set{a,f(a,b),f(f(a,b),b)},\ \set{b}}$\\
\smallskip
\quad This partition represents the congruence closure of $S_F$.  Now, is\\
\quad it the case that\\
\qquad 4. $\set{\set{a,f(a,b),f(f(a,b),b)},\ \set{b}} \Models F~?$\\
\smallskip
\quad No, as $f(f(a,b),b) \sim a$ but $F$ asserts that $f(f(a,b),b) \ne a$.\\
\quad Hence, $F$ is $T_E$-unsatisfiable.
\end{frame}

\begin{frame}
\uex: Given $\Sigma_E$-formula\\
\qquad \qquad $F:\ f(f(f(a))) = a \band f(f(f(f(f(a))))) = a \band f(a) \ne a$\\
\smallskip
From the subterm set $S_F$, the initial partition is\\
\qquad 1. $\set{\set{a},\ \set{f(a)},\ \set{f^2(a)},\ \set{f^3(a)},\ \set{f^4(a)},\ \set{f^5(a)}}$\\
\smallskip
\quad where, for example, $f^3(a)$ abbreviates $f(f(f(a)))$.

\quad According to the literal $f^3(a) = a$, merge\\
\smallskip
\qquad $\set{f^3(a)} \quad\mbox{and}\quad \set{a}~.$\\
\smallskip
\quad From the union,\\
\qquad 2. $\set{\set{a, f^3(a)},\ \set{f(a)},\ \set{f^2(a)},\ \set{f^4(a)},\ \set{f^5(a)}}$\\
\smallskip
\quad deduce the following congruence propagations:\\
\smallskip
\qquad $f^3(a) \sim a\ \Rightarrow\ f(f^3(a)) \sim f(a)\quad \mbox{i.e.}\quad f^4(a) \sim f(a)$\\
\quad and\\
\qquad $f^4(a) \sim f(a)\ \Rightarrow\ f(f^4(a)) \sim f(f(a))\ \ \mbox{i.e.}\ \ f^5(a) \sim f^2(a)$\\
\smallskip
\quad Thus, the final partition for this iteration is the following:\\
\qquad 3. $\set{\set{a,f^3(a)},\ \set{f(a),f^4(a)},\ \set{f^2(a),f^5(a)}}~.$\\
\end{frame}

\begin{frame}
\qquad 3. $\set{\set{a,f^3(a)},\ \set{f(a),f^4(a)},\ \set{f^2(a),f^5(a)}}~.$\\
\smallskip
From the second literal, $f^5(a) = a$, merge\\
\smallskip
\qquad $\set{f^2(a),f^5(a)} \quad\mbox{and}\quad \set{a,f^3(a)}$\\
\smallskip
to form the partition\\
\smallskip
\qquad 4. $\set{\set{a,f^2(a),f^3(a),f^5(a)},\ \set{f(a),f^4(a)}}~.$\\
\smallskip
Propagating the congruence\\
\smallskip
\qquad $f^3(a) \sim f^2(a)\ \Rightarrow\ f(f^3(a)) \sim f(f^2(a))\ \mbox{i.e.}\ f^4(a) \sim f^3(a)$\\
\smallskip
yields the partition\\
\smallskip
\qquad 5. $\set{\set{a,f(a),f^2(a),f^3(a),f^4(a),f^5(a)}}~,$\\
\smallskip
which represents the congruence closure in which all of $S_F$ are
equal.  Now,\\
\smallskip
\qquad 6. $\set{\set{a,f(a),f^2(a),f^3(a),f^4(a),f^5(a)}} \Models F~?$\\
\smallskip
No, as $f(a) \sim a$, but $F$ asserts that $f(a) \ne a$.  Hence, $F$
is $T_E$-unsatisfiable.
\end{frame}

\begin{frame}
\uex: Given $\Sigma_E$-formula
\[
F:\ f(x) = f(y) \band x \ne y~.
\]
The subterm set $S_F$ induces the following initial partition:\\
\smallskip
\qquad 1. $\set{\set{x},\ \set{y},\ \set{f(x)},\ \set{f(y)}}~.$\\
\smallskip
Then $f(x) = f(y)$ indicates to merge\\
\smallskip
\qquad $\set{f(x)} \quad\mbox{and}\quad \set{f(y)}~.$\\
\smallskip
The union $\set{f(x), f(y)}$ does not yield any new congruences, so
the final partition is\\
\smallskip
\qquad 2. $\set{\set{x},\ \set{y},\ \set{f(x),f(y)}}~.$\\
\bigskip
Does\\
\smallskip
\qquad 3. $\set{\set{x},\ \set{y},\ \set{f(x),f(y)}} \Models F~?$\\
\smallskip
Yes, as $x \not\sim y$, agreeing with $x \ne y$.  Hence, $F$ is
$T_E$-satisfiable.
\end{frame}

\begin{frame}
\underline{Directed Acyclic Graph (DAG)}\\
\smallskip
For $\Sigma_E$-formula $F$, graph-based data structure for representing the subterms of $S_F$
(and congruence relation between them).

\begin{center}

\begin{tabular}{ll}
\\
\begin{tabular}{@{}c@{}c@{}c@{}}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} & \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$}
\end{tabular}

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}

\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}
\ncarc[arcangle=-50,linestyle=dotted]{->}{ffabb}{a}

&

$
\begin{array}{l}
f(f(a,b),b) \\ \\
f(a,b) \\ \\
a \quad\quad b
\end{array}
$
\end{tabular}

\end{center}

Efficient way for computing the congruence closure algorithm.
\end{frame}

\begin{frame}
\underline{$T_E$-Satisfiability (Summary of idea)}
\[
f(a,b) = a\ \wedge\ f(f(a,b),b) \ne a
\]
\renewcommand{\arraystretch}{1}
\hspace*{-1.5em}
\begin{tabular}{@{}c@{}c@{}c@{}}

\begin{tabular}{@{}c@{}c@{}c@{}}
&& \ovalnode{ffabb1}{$1:f$} \\ \\
& \ovalnode{fab1}{$2:f$} & \\ \\
\ovalnode{a1}{$3:a$} &&
\ovalnode{b1}{$4:b$}
\end{tabular}
\ncline{->}{fab1}{a1}
\ncline{->}{fab1}{b1}
\ncline{->}{ffabb1}{fab1}
\ncline{->}{ffabb1}{b1}

&

\begin{tabular}{@{}c@{}c@{}c@{}}
&& \ovalnode{ffabb2}{$1:f$} \\ \\
& \ovalnode{fab2}{$2:f$} & \\ \\
\ovalnode{a2}{$3:a$} &&
\ovalnode{b2}{$4:b$}
\end{tabular}
\ncline{->}{fab2}{a2}
\ncline{->}{fab2}{b2}
\ncline{->}{ffabb2}{fab2}
\ncline{->}{ffabb2}{b2}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab2}{a2}

&

\begin{tabular}{@{}c@{}c@{}c@{}}
&& \ovalnode{ffabb3}{$1:f$} \\ \\
& \ovalnode{fab3}{$2:f$} & \\ \\
\ovalnode{a3}{$3:a$} &&
\ovalnode{b3}{$4:b$}
\end{tabular}
\ncline{->}{fab3}{a3}
\ncline{->}{fab3}{b3}
\ncline{->}{ffabb3}{fab3}
\ncline{->}{ffabb3}{b3}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab3}{a3}
\ncarc[arcangle=-50,linestyle=dotted]{->}{ffabb3}{a3}

\\

\begin{tabular}[t]{@{}l}
Initial DAG \\ \\ \\ \\
\end{tabular}
&
\begin{tabular}[t]{@{}l}
$f(a,b) = a\ \Rightarrow$ \\
$\mlfn{merge}\ f(a,b)\ a$ \\ \\ \\
\rnode{2}{} \hspace{1em} \rnode{3}{} explicit equation
\ncline[linestyle=dashed]{-}{2}{3}
\end{tabular}
&
\begin{tabular}[t]{@{}l}
$f(a,b) \sim a,\ b \sim b\ \Rightarrow$ \\
\hspace*{1em} $f(f(a,b),b) \sim f(a,b)$ \\
$\mlfn{merge}\ f(f(a,b),b)$ \\
\hspace*{3em} $f(a,b)$ \\
\rnode{0}{} \hspace{1em} \rnode{1}{} by congruence
\ncline[linestyle=dotted]{-}{0}{1}
\end{tabular}

\\
\\
\multicolumn{3}{l}{
$
\left.\begin{array}{r}
\mlfn{find}\ f(f(a,b),b) = a = \mlfn{find}\ a \\
f(f(a,b),b) \ne a
\end{array}\right\}\ \Rightarrow$
{\bf Unsatisfiable}
}
\end{tabular}

\end{frame}

\begin{frame}
DAG representation
\[
\begin{mlblock}
\mlkwd{type}\ \mltype{node}\ =\ \{ \\
\mlindent\mlindent
\begin{array}{@{}l@{\hspace{1em}}c@{\hspace{1em}}l@{}}

  \mlfield{id} & \mbox{:} & \mltype{id} \\
\smallskip
  & & \mbox{node's unique identification number}\\
  \mlfield{fn} & \mbox{:} & \mltype{string} \\
\smallskip
  & & \mbox{constant or function name}\\
  \mlfield{args} & \mbox{:} & \mltype{id\ list} \\
\smallskip
  & & \mbox{list of function arguments}\\
  \mlkwd{mutable}\ \mlfield{find} & \mbox{:} & \mltype{id} \\
\smallskip
  & & \mbox{the representative of the congruence class}\\
  \mlkwd{mutable}\ \mlfield{ccpar} & \mbox{:} & \mltype{id\ set}\\
  & & \mbox{if the node is the representative for its}\\
  & & \mbox{congruence class, then its } \mlfield{ccpar}\\
  & & \mbox{(congruence closure parents) are all}\\
  & & \mbox{parents of nodes in its congruence class}

\end{array} \\ \mlindent \} \end{mlblock}
\]
\end{frame}

\begin{frame}
\underline{DAG Representation of node 2}
\[
\begin{array}{cc}
\begin{mlrecordtype}{node}
\mlfield{id} & \mltype{id} \\
\mlfield{fn} & \mltype{string} \\
\mlfield{args} & \mltype{id list} \\
\mlkwd{mutable}\ \mlfield{find} & \mltype{id} \\
\mlkwd{mutable}\ \mlfield{ccpar} & \mltype{id set}
\end{mlrecordtype}
&
\begin{array}[t]{r@{~}l}
\\
\ldots & 2 \\
\ldots & f \\
\ldots & [3, 4] \\
\ldots & 3 \\
\ldots & \emptyset
\end{array}
\end{array}
\]
\begin{center}
\begin{tabular}{@{}c@{}c@{}c@{}@{\hspace{2em}}l}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} && \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$}

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}

\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}
\end{tabular}
\end{center}
\end{frame}

\begin{frame}
\underline{DAG Representation of node 3}
\renewcommand{\arraystretch}{.95}
\[
\begin{array}{cc}
\begin{mlrecordtype}{node}
\mlfield{id} & \mltype{id} \\
\mlfield{fn} & \mltype{string} \\
\mlfield{args} & \mltype{id list} \\
\mlkwd{mutable}\ \mlfield{find} & \mltype{id} \\
\mlkwd{mutable}\ \mlfield{ccpar} & \mltype{id set}
\end{mlrecordtype}
&
\begin{array}[t]{r@{~}l}
\\
\ldots & 3 \\
\ldots & a \\
\ldots & [] \\
\ldots & 3 \\
\ldots & \set{1,2}
\end{array}
\end{array}
\]

\begin{center}
\begin{tabular}{@{}c@{}c@{}c@{}@{\hspace{2em}}l}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$} &

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}

\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}

\end{tabular}
\end{center}
\end{frame}

\begin{frame}
\frametitle{The Implementation}
\underline{$\mlfn{find}$ function}\\
\smallskip
returns the representative of node's congruence class
\[
\begin{mlrecfunction}{find}{i}
  \mlsmalllet{n}{\mlfn{node}\ i} \\
  \mlif n.\mlfield{find} = i\ \mlthen i\ \mlelse \mlfn{find}\ n.\mlfield{find}
\end{mlrecfunction}
\]
\begin{center}
\begin{tabular}{ccc@{\hspace{2em}}l}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$}

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}

\end{tabular}
\end{center}
\begin{tabular}{@{}ll}
\uex: & $\mlfn{find}\ 2=3$\\
& $\mlfn{find}\ 3=3$
\end{tabular}\\
$3$ is the representative of $2$.
\end{frame}

\begin{frame}
\underline{$\mlfn{union}$ function}\\
\smallskip
\[
\begin{mlfunction}{union}{i_1\ i_2}
  \mlsmalllet{n_1}{\mlfn{node}\ (\mlfn{find}\ i_1)} \\
  \mlsmalllet{n_2}{\mlfn{node}\ (\mlfn{find}\ i_2)} \\
  n_1.\mlfield{find} \mlarw n_2.\mlfield{find}; \\
  n_2.\mlfield{ccpar} \mlarw n_1.\mlfield{ccpar}\ \cup\ n_2.\mlfield{ccpar}; \\
  n_1.\mlfield{ccpar} \mlarw \emptyset
\end{mlfunction}
\]

$n_2$ is the representative of the union class
\end{frame}

\begin{frame}
\uex
\begin{center}
\begin{tabular}{ccc@{\hspace{2em}}l}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$}

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}
\ncarc[arcangle=-50,linestyle=dotted]{->}{ffabb}{a}

\end{tabular}
\end{center}

\bigskip
$\mlfn{union}\ 1\ 2$ \qquad $n_1 = 1$ \quad $n_2 = 3$\\
\qquad $1.\mlfield{find} \leftarrow 3$\\
\qquad $3.\mlfield{ccpar} \leftarrow \set{1,2}$\\
\qquad $1.\mlfield{ccpar} \leftarrow \emptyset$
\end{frame}

\begin{frame}
\underline{$\mlfn{ccpar}$ function}\\
\smallskip
Returns parents of all nodes in $i$'s congruence class
\[
\begin{mlfunction}{ccpar}{i}
  (\mlfn{node}\ (\mlfn{find}\ i)).\mlfield{ccpar}
\end{mlfunction}
\]

\bigskip
\underline{$\mlfn{congruent}$ predicate}\\
\smallskip
Test whether $i_1$ and $i_2$ are congruent
\[
\begin{mlfunction}{congruent}{i_1\ i_2}
  \mlsmalllet{n_1}{\mlfn{node}\ i_1} \\
  \mlsmalllet{n_2}{\mlfn{node}\ i_2} \\
  n_1.\mlfield{fn} = n_2.\mlfield{fn} \\
  \mlindent \band |n_1.\mlfield{args}| = |n_2.\mlfield{args}| \\
  \mlindent \band \forall i \in \range{1}{|n_1.\mlfield{args}|}.\ \mlfn{find}\ n_1.\mlfield{args}[i] = \mlfn{find}\ n_2.\mlfield{args}[i]
\end{mlfunction}
\]
\end{frame}

\begin{frame}
\uex:
\begin{center}
\begin{tabular}{ccc@{\hspace{2em}}l}
&& \ovalnode{ffabb}{$1:f$} \\ \\
& \ovalnode{fab}{$2:f$} \\ \\
\ovalnode{a}{$3:a$} &&
\ovalnode{b}{$4:b$}

\ncline{->}{fab}{a}
\ncline{->}{fab}{b}
\ncline{->}{ffabb}{fab}
\ncline{->}{ffabb}{b}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab}{a}

\end{tabular}
\end{center}

Are 1 and 2 congruent?\\
\smallskip
\begin{tabular}{l@{\ ---\ }l}
$\mlfield{fn}$ fields & both $f$\\
\# of arguments & same\\
left arguments $f(a,b)$ and $a$ & both congruent to $3$\\
right arguments $b$ and $b$ & both $4$ (congruent)
\end{tabular}

\bigskip
Therefore $1$ and $2$ are congruent.
\end{frame}

\begin{frame}
\underline{$\mlfn{merge}$ function}\\
\smallskip
\[
\begin{mlrecfunction}{merge}{i_1\ i_2}
  \mlif \mlfn{find}\ i_1 \ne \mlfn{find}\ i_2\ \mlthen \mlkwd{begin} \\
  \mlindent\begin{mlblock}
    \mlsmalllet{P_{i_1}}{\mlfn{ccpar}\ i_1} \\
    \mlsmalllet{P_{i_2}}{\mlfn{ccpar}\ i_2} \\
    \mlfn{union}\ i_1\ i_2; \\
    \mlkwd{foreach}\ t_1,t_2 \in P_{i_1} \times P_{i_2}\ \mlkwd{do} \\
    \mlindent \begin{mlblock}
      \mlif \mlfn{find}\ t_1 \ne \mlfn{find}\ t_2 \band \mlfn{congruent}\ t_1\ t_2 \\
      \mlthen \mlfn{merge}\ t_1\ t_2
    \end{mlblock} \\
    \mlkwd{done}
  \end{mlblock} \\ \mlkwd{end}
\end{mlrecfunction}
\]
$P_{i_1}$ and $P_{i_2}$ store the current values of $\mlfn{ccpar}\ i_1$ and $\mlfn{ccpar}\ i_2$.
\end{frame}

\begin{frame}
\underline{Decision Procedure: $T_E$-satisfiability}\\
\smallskip
Given $\Sigma_E$-formula
\[
F:\ s_1 = t_1 \band \cdots \band s_m = t_m \band
s_{m+1} \ne t_{m+1} \band \cdots \band s_n \ne t_n~,
\]
with subterm set $S_F$, perform the following steps:
\begin{enumerate}
\item Construct the initial DAG for the subterm set $S_F$.
\item For $i \in \range{1}{m}$, $\mlfn{merge}\ s_i\ t_i$.
\item If $\mlfn{find}\ s_i = \mlfn{find}\ t_i$ for some $i \in
  \range{m+1}{n}$, return {\sf unsatisfiable}.
\item Otherwise (if $\mlfn{find}\ s_i \ne \mlfn{find}\ t_i$ for all $i
  \in \range{m+1}{n}$) return {\sf satisfiable}.
\end{enumerate}
\end{frame}

\begin{frame}
\underline{Example 1: $T_E$-Satisfiability}
\[
f(a,b) = a\ \wedge\ f(f(a,b),b) \ne a
\]

\renewcommand{\arraystretch}{1}
\hspace*{-1em}
\begin{tabular}{@{}c@{}c@{}c}

\begin{tabular}{@{}@{}c@{}c@{}c@{}@{}}
(1) && \ovalnode{ffabb1}{$1:f$} \\ \\
& \ovalnode{fab1}{$2:f$} & \\ \\
\ovalnode{a1}{$3:a$} &&
\ovalnode{b1}{$4:b$}
\end{tabular}
\ncline{->}{fab1}{a1}
\ncline{->}{fab1}{b1}
\ncline{->}{ffabb1}{fab1}
\ncline{->}{ffabb1}{b1}

&

\begin{tabular}{@{}@{}c@{}c@{}c@{}@{}}
(2) && \ovalnode{ffabb2}{$1:f$} \\ \\
& \ovalnode{fab2}{$2:f$} & \\ \\
\ovalnode{a2}{$3:a$} &&
\ovalnode{b2}{$4:b$}
\end{tabular}
\ncline{->}{fab2}{a2}
\ncline{->}{fab2}{b2}
\ncline{->}{ffabb2}{fab2}
\ncline{->}{ffabb2}{b2}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab2}{a2}

&

\begin{tabular}{@{}@{}c@{}c@{}c@{}@{}}
(3) && \ovalnode{ffabb3}{$1:f$} \\ \\
& \ovalnode{fab3}{$2:f$} & \\ \\
\ovalnode{a3}{$3:a$} &&
\ovalnode{b3}{$4:b$}
\end{tabular}
\ncline{->}{fab3}{a3}
\ncline{->}{fab3}{b3}
\ncline{->}{ffabb3}{fab3}
\ncline{->}{ffabb3}{b3}
\ncarc[arcangle=-50,linestyle=dashed]{->}{fab3}{a3}
\ncarc[arcangle=-50,linestyle=dotted]{->}{ffabb3}{a3}

\\

\begin{tabular}{@{}l}
Initial DAG \\ \\ \\ \\ \\
\end{tabular}
&
$
\begin{array}{@{}l}
\mlfn{merge}\ 2\ 3 \\
\hspace{1em}
\begin{array}{l}
\mlfn{union}\ 2\ 3\\
P_{2} = \set{1} \\
P_{3} = \set{2} \\
\end{array} \\
\mlfn{congruent}\ 1\ 2
\end{array}
$
&
$
\begin{array}{@{}l}
\mlfn{merge}\ 1\ 2 \\
\hspace{1em}
\begin{array}{l}
\mlfn{union}\ 1\ 2 \\
P_{1} = \set{} \\
P_{2} = \set{1,2} \\ \\
\end{array}
\end{array}
$

\end{tabular}

$\mlfn{find}\ f(f(a,b),b) = a = \mlfn{find}\ a$ $\Rightarrow$
{\bf Unsatisfiable}
\end{frame}

\begin{frame}
Given $\Sigma_E$-formula\\
\smallskip
\qquad $F:\ f(a,b) = a \band f(f(a,b),b) \ne a~.$\\
\smallskip
The subterm set is\\
\smallskip
\qquad $S_F = \set{a,\ b,\ f(a,b),\ f(f(a,b),b)}~,$\\
\smallskip
resulting in the initial partition\\
\smallskip
\qquad (1) $\set{\set{a},\ \set{b},\ \set{f(a,b)},\ \set{f(f(a,b),b)}}$\\
\smallskip
in which each term is its own congruence class. Fig (1).

\bigskip
Final partition\\
\smallskip
\qquad (2) $\set{\set{a,f(a,b),f(f(a,b),b)},\ \set{b}}$

\smallskip
\begin{tabular}{@{}ll}
\underline{Note}: & dash edge \rnode{a}{\ } \qquad \rnode{b}{\ } merge dictated by equalities in $F$\\
& dotted edge \rnode{c}{\ } \qquad \rnode{d}{\ } deduced merge
\ncline[linestyle=dashed]{a}{b}
\ncline[linestyle=dotted]{c}{d}
\end{tabular}

\bigskip
Does\\
\smallskip
\qquad (3) $\set{\set{a,f(a,b),f(f(a,b),b)},\ \set{b}} \Models F~?$\\
\smallskip
No, as $f(f(a,b),b) \sim a$, but $F$ asserts that $f(f(a,b),b) \ne a$.
Hence, $F$ is $T_E$-unsatisfiable.
\end{frame}

\begin{frame}
\underline{Example 2: $T_E$-Satisfiability}
\[
f(f(f(a))) = a\ \wedge\ f(f(f(f(f(a))))) = a\ \wedge\ f(a) \ne a
\]
\begin{tabular}{@{}l}
\begin{tabular}{@{}cccccc}
\ovalnode{f5}{$5:f$} &
\ovalnode{f4}{$4:f$} &
\ovalnode{f3}{$3:f$} &
\ovalnode{f2}{$2:f$} &
\ovalnode{f1}{$1:f$} &
\ovalnode{a}{$0:a$}\quad (1)
\end{tabular}
\ncline{->}{f5}{f4}
\ncline{->}{f4}{f3}
\ncline{->}{f3}{f2}
\ncline{->}{f2}{f1}
\ncline{->}{f1}{a}
\\
\\
\hspace*{2em}Initial DAG
\\
\\
\\
\\
\\
\begin{tabular}{@{}cccccc}
\ovalnode{f5}{$5:f$} &
\ovalnode{f4}{$4:f$} &
\ovalnode{f3}{$3:f$} &
\ovalnode{f2}{$2:f$} &
\ovalnode{f1}{$1:f$} &
\ovalnode{a}{$0:a$}\quad (2)
\end{tabular}
\ncline{->}{f5}{f4}
\ncline{->}{f4}{f3}
\ncline{->}{f3}{f2}
\ncline{->}{f2}{f1}
\ncline{->}{f1}{a}
\ncarc[arcangle=50,linestyle=dashed]{->}{f3}{a}
\ncarc[arcangle=50,linestyle=dotted]{->}{f4}{f1}
\ncarc[arcangle=50,linestyle=dotted]{->}{f5}{f2}
\\
\\
$f(f(f(a))) =  a\ \Rightarrow\
\mlfn{merge}\ 3\ 0~~~~~ P_3 = \set{4}~~ P_0 = \set{1}$ \\
\hspace*{7.1em}$\Rightarrow\ \mlfn{merge}\ 4\ 1~~~~~ P_4 = \set{5}~~ P_1 = \set{2}$ \\
\hspace*{7.1em}$\Rightarrow\ \mlfn{merge}\ 5\ 2~~~~~ P_5 = \set{}~~ P_2 = \set{3}$ \\
\end{tabular}
\end{frame}

\begin{frame}
\underline{Example 2: $T_E$-Satisfiability}
\[
f(f(f(a))) = a\ \wedge\ f(f(f(f(f(a))))) = a\ \wedge\ f(a) \ne a
\]
\begin{tabular}{@{}l}
\\
\\
\begin{tabular}{@{}cccccc}
\ovalnode{f5}{$5:f$} &
\ovalnode{f4}{$4:f$} &
\ovalnode{f3}{$3:f$} &
\ovalnode{f2}{$2:f$} &
\ovalnode{f1}{$1:f$} &
\ovalnode{a}{$0:a$}\quad (2)
\end{tabular}
\ncline{->}{f5}{f4}
\ncline{->}{f4}{f3}
\ncline{->}{f3}{f2}
\ncline{->}{f2}{f1}
\ncline{->}{f1}{a}
\ncarc[arcangle=50,linestyle=dashed]{->}{f3}{a}
\ncarc[arcangle=50,linestyle=dotted]{->}{f4}{f1}
\ncarc[arcangle=50,linestyle=dotted]{->}{f5}{f2}
\\
\\
\\
\\
\\
\begin{tabular}{@{}cccccc}
\ovalnode{f5}{$5:f$} &
\ovalnode{f4}{$4:f$} &
\ovalnode{f3}{$3:f$} &
\ovalnode{f2}{$2:f$} &
\ovalnode{f1}{$1:f$} &
\ovalnode{a}{$0:a$}\quad (3)
\end{tabular}
\ncline{->}{f5}{f4}
\ncline{->}{f4}{f3}
\ncline{->}{f3}{f2}
\ncline{->}{f2}{f1}
\ncline{->}{f1}{a}
\ncarc[arcangle=50,linestyle=dashed]{->}{f3}{a}
\ncarc[arcangle=50,linestyle=dotted]{->}{f4}{f1}
\ncarc[arcangle=50,linestyle=dotted]{->}{f5}{f2}
\ncarc[arcangle=-50,linestyle=dashed]{->}{f2}{a}
\ncarc[arcangle=50,linestyle=dotted]{->}{a}{f1}
\\
\\
\\
\begin{tabular}{lll}
$f(f(f(f(f(a))))) = a$ & $\Rightarrow\ \mlfn{merge}\ 5\ 0$ & $P_5 = \set{3}~~ P_0 = \set{1, 4}$ \\
& $\Rightarrow\ \mlfn{merge}\ 3\ 1$ & STOP. Why?
\end{tabular}
\\
\end{tabular}
\\
\smallskip
$\mlfn{find}\ f(a) = f(a) = \mlfn{find}\ a\ \Rightarrow\
\mbox{\bf Unsatisfiable}$
\end{frame}

\begin{frame}
Given $\Sigma_E$-formula
\[
F:\ f(f(f(a))) = a \band f(f(f(f(f(a))))) = a \band f(a) \ne a~,
\]
which induces the initial partition
\begin{enumerate}
\item
$\set{\set{a},\ \set{f(a)},\ \set{f^2(a)},\ \set{f^3(a)},\ \set{f^4(a)},\ \set{f^5(a)}}~.$

\smallskip
The equality $f^3(a)=a$ induces the partition
\item
$\set{\set{a,\ f^3(a)},\ \set{f(a),\ f^4(a)},\ \set{f^2(a),\ f^5(a)}}~.$

\smallskip
The equality $f^5(a)=a$ induces the partition
\item
$\set{\set{a,\ f(a),\ f^2(a),\ f^3(a),\ f^4(a),\ f^5(a)}}~.$

\smallskip
Now, does
\[
\set{\set{a,f(a),f^2(a),f^3(a),f^4(a),f^5(a)}} \Models F~?
\]
No, as $f(a) \sim a$, but $F$ asserts that $f(a) \ne a$.  Hence, $F$
is $T_E$-unsatisfiable.
\end{enumerate}
\end{frame}

\begin{frame}
\underline{Theorem (Sound and Complete)}\\
\bigskip
Quantifier-free conjunctive $\Sigma_E$-formula $F$ is $T_E$-satisfiable
iff the congruence closure algorithm returns {\sf satisfiable}.
\end{frame}

\begin{frame}
\frametitle{Recursive Data Structures}
\utitle{Quantifier-free Theory of Lists $\tcons$}
\bigskip
\qquad $\Sigma_\cons:\ \set{\cons,\ \car,\ \cdr,\ \atom,\ =}$

\bigskip
\begin{tabular}{@{\quad}l@{\ }c@{\ }l}
	$\bullet$\ \underline{constructor} $\cons$ & : & $\cons(a, b)$ list constructed by\\
	\smallskip
 	&& prepending $a$ to $b$\\
	\smallskip
	$\bullet$\ \underline{left projector} $\car$ & : & $\car(\cons(a, b)) = a$\\
	\smallskip
	$\bullet$\ \underline{right projector} $\cdr$ & : & $\cdr(\cons(a, b)) = b$\\
	$\bullet$\ \underline{$\atom$} & : & unary predicate
\end{tabular}
\end{frame}

\begin{frame}
\utitle{Axioms of $\tcons$}
\begin{itemize}
\item reflexivity, symmetry, transitivity
\item congruence axioms:
  \[
  \begin{array}{l}
    \forall x_1,x_2,y_1,y_2.\ x_1 = x_2\ \wedge\ y_1 = y_2\ \rightarrow\ \cons(x_1,y_1) = \cons(x_2,y_2) \\
    \forall x,y.\ x = y\ \rightarrow\ \car(x) = \car(y) \\
    \forall x,y.\ x = y\ \rightarrow\ \cdr(x) = \cdr(y)
  \end{array}
  \]
\item equivalence axiom:
\[
  \forall x,y.\ x = y\ \rightarrow\ (\atom(x)\ \leftrightarrow\ \atom(y))
\]
\item $
\begin{array}[t]{@{}l@{\ }l@{}r}
(A1) & \forall x,y.\ \car(\cons(x,y)) = x & \mbox{({\sf left projection})} \\
(A2) & \forall x,y.\ \cdr(\cons(x,y)) = y & \mbox{({\sf right projection})} \\
(A3) & \forall x.\ \neg\atom(x) \rightarrow \cons(\car(x),\cdr(x)) = x
& \mbox{({\sf construction})} \\
(A4) & \forall x,y.\ \neg\atom(\cons(x,y)) & \mbox{({\sf atom})}
%\\
%(A5) & (\forall x)[\car(x) \ne x] & \mbox{({\it acyclicity})} \\
%(A6) & (\forall x)[\cdr(x) \ne x] & \mbox{({\it acyclicity})} \\
%(A7) & (\forall x)[\car(\car(x)) \ne x] & \mbox{({\it acyclicity})} \\
%& \ldots &
\end{array}
$
\end{itemize}
\end{frame}

\begin{frame}
\utitle{Simplifications}
\begin{itemize}
\setlength{\itemsep}{1ex}
\item Consider only quantifier-free conjunctive $\scons$-formulae.\\
Convert non-conjunctive formula to DNF and check each disjunct.

\item $\bnot\atom(u_i)$ literals are removed:\\
\smallskip
\qquad \fbox{replace \quad $\neg\atom(u_i) \quad\mbox{with}\quad u_i = \cons(u_i^1,u_i^2)$}\\
\smallskip
by the \axiom{construnction} axiom.

\item Because of similarity to $\sequ$, we sometimes combine $\scons \cup \sequ$.
\end{itemize}
\end{frame}

\begin{frame}
\utitle{Algorithm: $\tcons$-Satisfiability (the idea)}

\[
\begin{array}{lll}
F: &&
\underbrace{
s_1 = t_1\ \wedge\ \cdots\ \wedge\ s_m = t_m
}_{\mbox{generate congruence closure}} \\
&\wedge &
\underbrace{
s_{m+1} \ne t_{m+1}\ \wedge\ \cdots\ \wedge\ s_n \ne t_n
}_{\mbox{search for contradiction}} \\
&\wedge &
\underbrace{
\atom(u_1)\ \wedge\ \cdots\ \wedge\ \atom(u_\ell)
}_{\mbox{search for contradiction}}
\end{array}
\]
where $s_i$, $t_i$, and $u_i$ are $\tcons$-terms
\end{frame}

\begin{frame}
\utitle{Algorithm: $\tcons$-Satisfiability}

\begin{enumerate}
\item Construct the initial DAG for $S_F$
\item for each node $n$ with $n.\mlfield{fn} = \cons$
\begin{itemize}
\item add $\car(n)$ and $\mlfn{merge}\ \car(n)\ n.\mlfield{args}[1]$
\item add $\cdr(n)$ and $\mlfn{merge}\ \cdr(n)\ n.\mlfield{args}[2]$
\end{itemize}
by axioms (A1), (A2)
\item for $1 \le i \le m$, $\mlfn{merge}\ s_i\ t_i$
\item for $m+1 \le i \le n$, if $\mlfn{find}\ s_i = \mlfn{find}\ t_i$,
return {\bf unsatisfiable}
\item for $1 \le i \le \ell$,
if $\exists v.\ \mlfn{find}\ v = \mlfn{find}\ u_i\ \wedge\
v.\mlfield{fn} = \cons$, \\ return {\bf unsatisfiable}
\item Otherwise, return {\bf satisfiable}
\end{enumerate}
\vspace*{-15em}
\hspace{20em}
\begin{tabular}{@{}c@{}c@{}c@{}}
\circlenode{car}{$\car$} && \circlenode{cdr}{$\cdr$} \\
\\
& \circlenode{cons}{$\cons$} & \\
\\
\circlenode{x}{$x$} && \circlenode{y}{$y$}
\end{tabular}
\ncline{->}{car}{cons}
\ncline{->}{cdr}{cons}
\ncline{->}{cons}{x}
\ncline{->}{cons}{y}
\ncbox[nodesep=.15,boxsize=.55,linearc=.2,linestyle=dotted]{car}{cdr}
\ncline[linestyle=dashed]{->}{car}{x}
\ncline[linestyle=dashed]{->}{cdr}{y}
\end{frame}

\begin{frame}
\uex:\\
\smallskip
Given $(\scons \cup \sequ)$-formula\\
\smallskip
\qquad $F:\quad
\begin{array}{c}
\car(x) = \car(y)\ \wedge\ \cdr(x) = \cdr(y)\ \\
\wedge\ \neg\atom(x)\ \wedge\ \neg\atom(y)\ \wedge\ f(x) \ne f(y) \\
\end{array}
$\\
\smallskip
\qquad where the function symbol $f$ is in $\sequ$\\
\bigskip
\qquad $F':\quad
\begin{array}{ll@{\hspace*{10em}}r}
\car(x) = \car(y) & \wedge & (1)\\
\cdr(x) = \cdr(y) & \wedge & (2)\\
x = \cons(u_1,v_1) & \wedge & (3)\\
y = \cons(u_2,v_2) & \wedge & (4)\\
f(x) \ne f(y) & & (5)
\end{array}
$\\
\bigskip
Recall the projection axioms:\\
\smallskip
\qquad $
\begin{array}{ll}
(A1) & \forall x,y.\ \car(\cons(x,y)) = x \\
(A2) & \forall x,y.\ \cdr(\cons(x,y)) = y \\
\end{array}
$
\end{frame}

\begin{frame}
\utitle{Example(cont): Initial DAG}

\vspace*{1em}
\begin{tabular}{@{}l@{}l}
\begin{tabular}{@{}cccccc}
\circlenode{carx}{$\car$} &
\circlenode{fx}{$f$} &
\circlenode{cdrx}{$\cdr$} &
\circlenode{cary}{$\car$} &
\circlenode{fy}{$f$} &
\circlenode{cdry}{$\cdr$} \\
\\
& \circlenode{x}{$x$} &
&
& \circlenode{y}{$y$} & \\
\\
\circlenode{carcons1}{$\car$} &
&
\circlenode{cdrcons1}{$\cdr$} &
\circlenode{carcons2}{$\car$} &
&
\circlenode{cdrcons2}{$\cdr$} \\
\\
& \circlenode{cons1}{$\cons$} &
&
& \circlenode{cons2}{$\cons$} & \\
\\
\circlenode{u1}{$u_1$} &
&
\circlenode{v1}{$v_1$} &
\circlenode{u2}{$u_2$} &
&
\circlenode{v2}{$v_2$}
\end{tabular}
\ncline{->}{carx}{x}
\ncline{->}{fx}{x}
\ncline{->}{cdrx}{x}
\ncline{->}{cary}{y}
\ncline{->}{fy}{y}
\ncline{->}{cdry}{y}
\ncline{->}{carcons1}{cons1}
\ncline{->}{cdrcons1}{cons1}
\ncbox[nodesep=.15,boxsize=.55,linearc=.2,linestyle=dotted]{carcons1}{cdrcons1}
\ncline[linestyle=dashed]{->}{carcons1}{u1}
\ncline[linestyle=dashed]{->}{cdrcons1}{v1}
\ncline{->}{carcons2}{cons2}
\ncline{->}{cdrcons2}{cons2}
\ncbox[nodesep=.15,boxsize=.55,linearc=.2,linestyle=dotted]{carcons2}{cdrcons2}
\ncline[linestyle=dashed]{->}{carcons2}{u2}
\ncline[linestyle=dashed]{->}{cdrcons2}{v2}
\ncline{->}{cons1}{u1}
\ncline{->}{cons1}{v1}
\ncline{->}{cons2}{u2}
\ncline{->}{cons2}{v2}
&
axioms (A1), (A2)
\end{tabular}
\end{frame}

\begin{frame}
\utitle{Example(cont): $\mlfn{merge}$}

\vspace*{2em}
\begin{tabular}{@{}l@{}l}
\begin{tabular}{@{}cccccc@{}}
\circlenode{carx}{$\car$} &
\circlenode{fx}{$f$} &
\circlenode{cdrx}{$\cdr$} &
\circlenode{cary}{$\car$} &
\circlenode{fy}{$f$} &
\circlenode{cdry}{$\cdr$} \\
\\
& \circlenode{x}{$x$} &
&
& \circlenode{y}{$y$} & \\
\\
\circlenode{carcons1}{$\car$} &
&
\circlenode{cdrcons1}{$\cdr$} &
\circlenode{carcons2}{$\car$} &
&
\circlenode{cdrcons2}{$\cdr$} \\
\\
& \circlenode{cons1}{$\cons$} &
&
& \circlenode{cons2}{$\cons$} & \\
\\
\circlenode{u1}{$u_1$} &
&
\circlenode{v1}{$v_1$} &
\circlenode{u2}{$u_2$} &
&
\circlenode{v2}{$v_2$}
\end{tabular}
\ncline{->}{carx}{x}
\ncline{->}{fx}{x}
\ncline{->}{cdrx}{x}
\ncline{->}{cary}{y}
\ncline{->}{fy}{y}
\ncline{->}{cdry}{y}
\ncline{->}{carcons1}{cons1}
\ncline{->}{cdrcons1}{cons1}
\ncline[linestyle=dashed]{->}{carcons1}{u1}
\ncline[linestyle=dashed]{->}{cdrcons1}{v1}
\ncline{->}{carcons2}{cons2}
\ncline{->}{cdrcons2}{cons2}
\ncline[linestyle=dashed]{->}{carcons2}{u2}
\ncline[linestyle=dashed]{->}{cdrcons2}{v2}
\ncline{->}{cons1}{u1}
\ncline{->}{cons1}{v1}
\ncline{->}{cons2}{u2}
\ncline{->}{cons2}{v2}
\ncarc[arcangle=50,linestyle=dashed]{->}{carx}{cary}
\tbput{(1)}
\ncarc[arcangle=50,linestyle=dashed]{->}{cdrx}{cdry}
\tbput{(2)}
\ncline[linestyle=dashed]{->}{x}{cons1}
\tlput{(3)}
&
{\small
$
\begin{array}{l}
\rnode{2}{} \hspace{1em} \rnode{3}{} \mbox{~explicit equation} \\
\rnode{0}{} \hspace{1em} \rnode{1}{} \mbox{~by congruence} \\
\ncline[linestyle=dotted]{-}{0}{1}
\ncline[linestyle=dashed]{-}{2}{3}
\\
1: \mlfn{merge}\ \car(x)\ \car(y) \\
2: \mlfn{merge}\ \cdr(x)\ \cdr(y) \\
\\
3: \mlfn{merge}\ x\ \cons(u_1,v_1) \\
\hspace{1em}\Downarrow
\end{array}
$
}
\end{tabular}
\end{frame}

\begin{frame}
\utitle{Example(cont): Propagation}

\vspace*{2em}
\begin{tabular}{@{}l@{}l}
\begin{tabular}{@{}cccccc@{}}
\circlenode{carx}{$\car$} &
\circlenode{fx}{$f$} &
\circlenode{cdrx}{$\cdr$} &
\circlenode{cary}{$\car$} &
\circlenode{fy}{$f$} &
\circlenode{cdry}{$\cdr$} \\
\\
& \circlenode{x}{$x$} &
&
& \circlenode{y}{$y$} & \\
\\
\circlenode{carcons1}{$\car$} &
&
\circlenode{cdrcons1}{$\cdr$} &
\circlenode{carcons2}{$\car$} &
&
\circlenode{cdrcons2}{$\cdr$} \\
\\
& \circlenode{cons1}{$\cons$} &
&
& \circlenode{cons2}{$\cons$} & \\
\\
\circlenode{u1}{$u_1$} &
&
\circlenode{v1}{$v_1$} &
\circlenode{u2}{$u_2$} &
&
\circlenode{v2}{$v_2$}
\end{tabular}
\ncline{->}{carx}{x}
\ncline{->}{fx}{x}
\ncline{->}{cdrx}{x}
\ncline{->}{cary}{y}
\ncline{->}{fy}{y}
\ncline{->}{cdry}{y}
\ncline{->}{carcons1}{cons1}
\ncline{->}{cdrcons1}{cons1}
\ncline[linestyle=dashed]{->}{carcons1}{u1}
\ncline[linestyle=dashed]{->}{cdrcons1}{v1}
\ncline{->}{carcons2}{cons2}
\ncline{->}{cdrcons2}{cons2}
\ncline[linestyle=dashed]{->}{carcons2}{u2}
\ncline[linestyle=dashed]{->}{cdrcons2}{v2}
\ncline{->}{cons1}{u1}
\ncline{->}{cons1}{v1}
\ncline{->}{cons2}{u2}
\ncline{->}{cons2}{v2}
\ncarc[arcangle=50,linestyle=dashed]{->}{carx}{cary}
\ncarc[arcangle=50,linestyle=dashed]{->}{cdrx}{cdry}
\ncline[linestyle=dashed]{->}{x}{cons1}
\ncarc[arcangle=-20,linestyle=dotted]{->}{cary}{u1}
\tbput{3A}
\ncarc[arcangle=20,linestyle=dotted]{->}{cdry}{v1}
\tlput{3B}
&
{\small
$
\begin{array}{l}
\mbox{Congruent:} \\
\car(x)\ \car(\cons(u_1,v_1)) \\
\mlfn{find}\ \car(x) = \car(y) \\
\mlfn{find}\ \car(\cons(\ldots)) = u_1 \\
\\
\mbox{Congruent:} \\
\cdr(x)\ \cdr(\cons(u_1,v_1)) \\
\mlfn{find}\ \cdr(x) = \cdr(y) \\
\mlfn{find}\ \cdr(\cons(\ldots)) = v_1
\end{array}
$
}
\end{tabular}

\end{frame}

\begin{frame}
\utitle{Example(cont): $\mlfn{merge}$}

\vspace*{2em}
\begin{tabular}{@{}l@{}l}
\begin{tabular}{@{}cccccc@{}}
\circlenode{carx}{$\car$} &
\circlenode{fx}{$f$} &
\circlenode{cdrx}{$\cdr$} &
\circlenode{cary}{$\car$} &
\circlenode{fy}{$f$} &
\circlenode{cdry}{$\cdr$} \\
\\
& \circlenode{x}{$x$} &
&
& \circlenode{y}{$y$} & \\
\\
\circlenode{carcons1}{$\car$} &
&
\circlenode{cdrcons1}{$\cdr$} &
\circlenode{carcons2}{$\car$} &
&
\circlenode{cdrcons2}{$\cdr$} \\
\\
& \circlenode{cons1}{$\cons$} &
&
& \circlenode{cons2}{$\cons$} & \\
\\
\circlenode{u1}{$u_1$} &
&
\circlenode{v1}{$v_1$} &
\circlenode{u2}{$u_2$} &
&
\circlenode{v2}{$v_2$}
\end{tabular}
\ncline{->}{carx}{x}
\ncline{->}{fx}{x}
\ncline{->}{cdrx}{x}
\ncline{->}{cary}{y}
\ncline{->}{fy}{y}
\ncline{->}{cdry}{y}
\ncline{->}{carcons1}{cons1}
\ncline{->}{cdrcons1}{cons1}
\ncline[linestyle=dashed]{->}{carcons1}{u1}
\ncline[linestyle=dashed]{->}{cdrcons1}{v1}
\ncline{->}{carcons2}{cons2}
\ncline{->}{cdrcons2}{cons2}
\ncline[linestyle=dashed]{->}{carcons2}{u2}
\ncline[linestyle=dashed]{->}{cdrcons2}{v2}
\ncline{->}{cons1}{u1}
\ncline{->}{cons1}{v1}
\ncline{->}{cons2}{u2}
\ncline{->}{cons2}{v2}

\ncarc[arcangle=50,linestyle=dashed]{->}{carx}{cary}
\ncarc[arcangle=50,linestyle=dashed]{->}{cdrx}{cdry}
\ncline[linestyle=dashed]{->}{x}{cons1}
\ncarc[arcangle=-20,linestyle=dotted]{->}{cary}{u1}
\ncarc[arcangle=20,linestyle=dotted]{->}{cdry}{v1}
\ncline[linestyle=dashed]{->}{y}{cons2}
\tlput{(4)}
\ncarc[arcangle=-30,linestyle=dotted]{->}{u1}{u2}
\tbput{4A}
\ncarc[arcangle=-30,linestyle=dotted]{->}{v1}{v2}
\tbput{4B}
&
{\small
$
\begin{array}{l}
4: \mlfn{merge}\ y\ \cons(u_2,v_2) \\
\hspace*{1em}\Downarrow \\
\mbox{Congruent:} \\
\car(y)\ \car(\cons(u_2,v_2)) \\
\mlfn{find}\ \car(y) = u_1 \\
\mlfn{find}\ \car(\cons(\ldots)) = u_2 \\
\\
\mbox{Congruent:} \\
\cdr(y)\ \cdr(\cons(u_2,v_2)) \\
\mlfn{find}\ \cdr(y) = v_1 \\
\mlfn{find}\ \cdr(\cons(\ldots)) = v_2 \\
\hspace*{1em}\Downarrow
\end{array}
$
}
\end{tabular}

\end{frame}

\begin{frame}
\utitle{Example(cont): $\mlfn{congruence}$}

\vspace*{3em}
\begin{tabular}{@{}l@{\hspace*{-.6em}}l}
\begin{tabular}{@{}cccccc@{}}
\circlenode{carx}{$\car$} &
\circlenode{fx}{$f$} &
\circlenode{cdrx}{$\cdr$} &
\circlenode{cary}{$\car$} &
\circlenode{fy}{$f$} &
\circlenode{cdry}{$\cdr$} \\
\\
& \circlenode{x}{$x$} &
&
& \circlenode{y}{$y$} & \\
\\
\circlenode{carcons1}{$\car$} &
&
\circlenode{cdrcons1}{$\cdr$} &
\circlenode{carcons2}{$\car$} &
&
\circlenode{cdrcons2}{$\cdr$} \\
\\
& \circlenode{cons1}{$\cons$} &
&
& \circlenode{cons2}{$\cons$} & \\
\\
\circlenode{u1}{$u_1$} &
&
\circlenode{v1}{$v_1$} &
\circlenode{u2}{$u_2$} &
&
\circlenode{v2}{$v_2$}
\end{tabular}
\ncline{->}{carx}{x}
\ncline{->}{fx}{x}
\ncline{->}{cdrx}{x}
\ncline{->}{cary}{y}
\ncline{->}{fy}{y}
\ncline{->}{cdry}{y}
\ncline{->}{carcons1}{cons1}
\ncline{->}{cdrcons1}{cons1}
\ncline[linestyle=dashed]{->}{carcons1}{u1}
\ncline[linestyle=dashed]{->}{cdrcons1}{v1}
\ncline{->}{carcons2}{cons2}
\ncline{->}{cdrcons2}{cons2}
\ncline[linestyle=dashed]{->}{carcons2}{u2}
\ncline[linestyle=dashed]{->}{cdrcons2}{v2}
\ncline{->}{cons1}{u1}
\ncline{->}{cons1}{v1}
\ncline{->}{cons2}{u2}
\ncline{->}{cons2}{v2}

\ncarc[arcangle=50,linestyle=dashed]{->}{carx}{cary}
\ncarc[arcangle=50,linestyle=dashed]{->}{cdrx}{cdry}
\ncline[linestyle=dashed]{->}{x}{cons1}
\ncarc[arcangle=-20,linestyle=dotted]{->}{cary}{u1}
\ncarc[arcangle=20,linestyle=dotted]{->}{cdry}{v1}
\ncline[linestyle=dashed]{->}{y}{cons2}
\ncarc[arcangle=-30,linestyle=dotted]{->}{u1}{u2}
\ncarc[arcangle=-30,linestyle=dotted]{->}{v1}{v2}
\ncline[linestyle=dotted]{->}{cons1}{cons2}
\taput{4C}
\ncarc[arcangle=50,linestyle=dotted]{->}{fx}{fy}
\taput{4D}
&
{\small
$
\begin{array}{l}
\mbox{Congruent:} \\
\cons(u_1,v_1)\ \cons(u_2,v_2) \\
\\
\mbox{Congruent:}\ f(x)\ f(y) \\
\\
5: \begin{array}{l}
\mlfn{find}\ f(x) = f(y) \\
\mlfn{find}\ f(y) = f(y)
\end{array} \\
\hspace*{1em}\Downarrow \\
F \mbox{ is \bf unsatisfiable}
\end{array}
$
}
\end{tabular}

\end{frame}

\begin{frame}
\frametitle{Arrays}
(1) \underline{Quantifier-free Fragment of $\tarr$}\\
\medskip
\qquad $\Sigma_\arr:\ \set{\ard{\cdot}{\cdot},\ \awt{\cdot}{\cdot}{\cdot},\ =}~,$\\
\smallskip
where
\begin{itemize}
\item $\ard{a}{i}$ is a binary function representing\\
 read of array $a$ at index $i$;
\item $\awt{a}{i}{v}$ is a ternary function representing\\
 write of value $v$ to index $i$ of array $a$;
\item $=$ is a binary predicate.
\end{itemize}
Axioms of $\tarr$:
\begin{enumerate}
\item axioms of \axiom{reflexivity}, \axiom{symmetry}, and \axiom{transitivity} of $\tequ$
\item $\forall a,i,j.\ i = j \bimplies \ard{a}{i} = \ard{a}{j}$
  \naxiom{array congruence}
\item $\forall a,v,i,j.\ i = j \bimplies \ard{\awt{a}{i}{v}}{j} = v$
  \naxiom{read-over-write 1}
\item $\forall a,v,i,j.\ i \ne j \bimplies \ard{\awt{a}{i}{v}}{j} = \ard{a}{j}$
  \naxiom{read-over-write 2}
\end{enumerate}
\un: $a$ may itself be a write term, e.g., $\awt{a}{i'}{v'}$. Then\\
\qquad $\awt{(\awt{a}{i'}{v'})}{i}{v}$\\
\begin{tabular}{@{}l@{\ }l}
means: & first write the value $v'$ to index $i'$ of $a$\\
& then write the value $v$ to index $i$ of $a$
\end{tabular}
\end{frame}

\begin{frame}
\utitle{The Decision Procedure}
Given quantifier-free conjunctive $\sarr$-formula $F$.\\
To decide the $\tarr$-satisfiability of $F$:

\bigskip
{\bf Step 1}\\
\medskip
If $F$ does not contain any write terms $\awt{a}{i}{v}$, then
\begin{enumerate}
\item associate array variables $a$ with fresh function symbol $f_a$,
  and replace read terms $\ard{a}{i}$ with $f_a(i)$;
\item decide the $\tequ$-satisfiability of the resulting formula.
\end{enumerate}

\end{frame}

\begin{frame}
{\bf Step 2}\\
\medskip
Select some read-over-write term $\ard{\awt{a}{i}{v}}{j}$ (note that
$a$ may itself be a write term) and split on two cases:
\begin{enumerate}
\item According to \axiom{read-over-write 1}, replace
  \[
  F[\ard{\awt{a}{i}{v}}{j}]
  \quad\mbox{with}\quad
  F_1:\ F[v] \band i = j~,
  \]
  and recurse on $F_1$.  If $F_1$ is found to be
  $\tarr$-satisfiable, return {\sf satisfiable}.
\item According to \axiom{read-over-write 2}, replace
  \[
  F[\ard{\awt{a}{i}{v}}{j}]
  \quad\mbox{with}\quad
  F_2:\ F[\ard{a}{j}] \band i \ne j~,
  \]
  and recurse on $F_2$.  If $F_2$ is found to be
  $\tarr$-satisfiable, return {\sf satisfiable}.
\end{enumerate}
If both $F_1$ and $F_2$ are found to be $\tarr$-unsatisfiable, return
{\sf unsatisfiable}.
\end{frame}

\begin{frame}
\uex: Consider $\sarr$-formula
\[
F:\ i_1 = j \band i_1 \ne i_2 \band \ard{a}{j} = v_1 \band \ard{\awt{\awt{a}{i_1}{v_1}}{i_2}{v_2}}{j} \ne \ard{a}{j}~.
\]
$F$ contains a write term,
\[
\ard{\awt{\awt{a}{i_1}{v_1}}{i_2}{v_2}}{j} \ne \ard{a}{j}~.
\]

According to \axiom{read-over-write 1}, assume \underline{$i_2 = j$} and recurse
on
\[
F_1:\ i_2 = j \band i_1 = j \band i_1 \ne i_2 \band \ard{a}{j} = v_1 \band v_2 \ne \ard{a}{j}~.
\]
$F_1$ does not contain any write terms, so rewrite it to
\[
F_1':\ i_2 = j \band i_1 = j \band i_1 \ne i_2 \band f_a(j) = v_1 \band v_2 \ne f_a(j)~.
\]
The first two literals imply that $i_1 = i_2$, contradicting the third
literal, so $F_1'$ is $\tequ$-unsatisfiable.

\end{frame}

\begin{frame}
Returning, we try the second case:\\
 according to \axiom{read-over-write 2}, assume \underline{$i_2 \ne j$} and recurse on
\[
F_2:\ i_2 \ne j \band i_1 = j \band i_1 \ne i_2 \band \ard{a}{j} = v_1 \band \ard{\awt{a}{i_1}{v_1}}{j} \ne \ard{a}{j}~.
\]
$F_2$ contains a write term. According to \axiom{read-over-write 1}, assume \underline{$i_1 = j$} and recurse on
\[
F_3:\ i_1 = j \band i_2 \ne j \band i_1 = j \band i_1 \ne i_2 \band \ard{a}{j} = v_1 \band v_1 \ne \ard{a}{j}~.
\]
Contradiction because of the
final two terms.  Thus, according to \axiom{read-over-write 2}, assume
\underline{$i_1 \ne j$} and recurse on
\[
F_4:\ i_1 \ne j \band i_2 \ne j \band i_1 = j \band i_1 \ne i_2 \band \ard{a}{j} = v_1 \band \ard{a}{j} \ne \ard{a}{j}~.
\]
Two contradictions: the first and third
literals contradict each other, and the final literal is
contradictory.  As all branches have been tried, $F$ is
$\tarr$-unsatisfiable.

\smallskip
Suppose instead that $F$ does not contain the literal $i_1 \ne i_2$.
Is this new formula $\tarr$-satisfiable?

\end{frame}


\end{document}
