\documentclass[10pt]{article}

\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}

%add by zhongxian gu for emphasizing
\usepackage{color}

% THEOREM-LIKE ENVIRONMENTS
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}

\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{conjecture}[theorem]{Conjecture}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{property}[theorem]{Property}
% END THEOREM-LIKE ENVIRONMENTS

% MACRO DEFINITIONS
\newcommand{\lecture}[2]{\section{#1 \small (scribe: #2)}}
\newcommand{\topic}[1]{\subsection*{#1}}
\newcommand{\frA}{\ensuremath{\mathfrak{A}}}  % swoopy A 
\newcommand{\bbD}{\ensuremath{\mathbb{D}}}    % domain D
\newcommand{\calV}{\ensuremath{\mathcal{V}}}    % view V
\newcommand{\abar}{\ensuremath{\bar{a}}}
\newcommand{\xbar}{\ensuremath{\bar{x}}}
\newcommand{\ybar}{\ensuremath{\bar{y}}}
\newcommand{\ubar}{\ensuremath{\bar{u}}}
\newcommand{\vbar}{\ensuremath{\bar{v}}}
\newcommand{\defeq}{\stackrel{\text{def}}{=}}
\newcommand{\intp}[2]{\ensuremath{#1^{#2}}}   % interpretation
\newcommand{\lsem}{\ensuremath{[\![}}
\newcommand{\rsem}{\ensuremath{]\!]}}
\newcommand{\llbracket}{\lsem}
\newcommand{\rrbracket}{\rsem}
\newcommand{\eval}[2]{\ensuremath{\lsem #1 \rsem^{#2}}}  % evaluation
\newcommand{\Class}{\ensuremath{\text{\em Class}}}
\newcommand{\Student}{\ensuremath{\text{\em Student}}}
\newcommand{\Takes}{\ensuremath{\text{\em Takes}}}
\newcommand{\Rew}{\ensuremath{\text{\em Rew}}}
\newcommand{\VALID}{\ensuremath{\text{VALID}}}
\newcommand{\FINVALID}{\ensuremath{\text{FIN-VALID}}}
\DeclareMathOperator{\cont}{\ensuremath{\textnormal{cont}}}
\newcommand{\sch}{\ensuremath{sch}}
\newcommand{\la}{\ensuremath{\mathbin{\mathsf{{:}{-}}}}}
\newcommand{\constants}{\ensuremath{\mathcal C}}
\newcommand{\relations}{\ensuremath{\mathcal R}}
\newcommand{\Bp}{\ensuremath{\mathcal B_P}}
\newcommand{\Mp}{\ensuremath{M_P}}
\newcommand{\Tp}{\ensuremath{T_P}}
\newcommand{\true}{\ensuremath{\mathit{True}}}
\newcommand{\false}{\ensuremath{\mathit{False}}}
\newcommand{\datalogneg}{\ensuremath{\mathrm{Datalog}^\neg}}
% END MACRO DEFINITIONS






\title{Notes for ECS 289F: Foundations of Relational Databases}
\date{Winter 2010\\\ \\Revision: \today}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 1/4/10}{Sven}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection*{Introduction/Administrivia}
Webpage:
\begin{verbatim}
http://www.cs.ucdavis.edu/~green/courses/cse289f
\end{verbatim}
Textbook (optional, use as reference): {\em Foundation of Databases}
by Abiteboul, Hull, and Vianu

\subsection*{Historical Background}  (see handout from 1975 book by
Date)


\subsubsection*{Hierarchical Approach}

Problem: Redundancy, awkward when data not hierarchical, strong
distinction between records and links, low-level query languages


\subsubsection*{Network Model}

Problem: More flexible, but links are still seperate elements


\subsubsection*{Relational Approach}

Everything (links, records) modeled as {\em relations}.  High-level,
declarative query language based on {\em first-order logic}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 1/6/10}{Sarah}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\topic{Review: Syntax and Semantics of First-Order Logic (FO)}

\begin{definition} A {\em relational schema} $\sigma$ is a collection
  of {\em predicate} symbols (relations) (denoted $P_1, P_2, \dots$)
  each with an associated arity (number of columns).

  A {\em $\sigma$-instance} $I$ is a collection of {\em
    interpretations} of predicate symbols, one for each $P_i$ in
  $\sigma$. i.e., if $P_i$ is a k-ary predicate symbol then $P_i^I$ (the
  {\em contents} of $P_i$) $\subseteq \bbD^k$ where $\bbD$ is the {\em
    domain} of the database and $P_i^I$ is finite.

  More generally, this is a {\em structure}/{\em model} where the
  vocabulary is a schema and the structure is an instance of a table
  (see below definitions).
\end{definition}

\begin{definition} A {\em vocabulary} $\sigma$ is a collection of {\em
    constant} symbols (denoted $c_1, c_2, \dots$), {\em relation} or
  {\em predicate} symbols ($P_1, P_2, \dots$), and {\em function}
  symbols ($f_1, f_2, \dots$).  Each relation and function symbol has
  an associated arity.

  A {\em $\sigma$-structure} (also called a {\em model})
\[
\frA = \langle A, \{ \intp{c_i}{\frA} \}, \{ \intp{P_i}{\frA} \}, \{
\intp{f_i}{\frA} \} \rangle
\]
consists of a {\em universe} $A$ together with an interpretation of
\begin{itemize}
\item each constant symbol $c_i$ from $\sigma$ as an element
  $\intp{c_i}{\frA} \in A$;
\item each $k$-ary relation symbol $P_i$ from $\sigma$ as a $k$-ary
  relation on $A$; that is, a set $\intp{P_i}{\frA} \subseteq A^k$;
  and
\item each $k$-ary function symbol $f_i$ from $\sigma$ as a function
  $\intp{f_i}{\frA} : A^k \to A$.
\end{itemize}
\end{definition}

Next, we define first-order (FO) formulae, free and bound variables,
and the semantics of FO formulae.
\begin{definition}[Syntax of FO]
  We assume a countably infinite set of variables $\{x_1, x_2,
  \dots\}$.  We inductively define {\em terms} and {\em formulae} of
  the {\em first-order predicate calculus} over vocabulary $\sigma$ as
  follows:

  A {\em term} is defined as either:
  \begin{itemize}
    \item a variable $x$;
    \item a constant $c$;
    \item $f(t_1, t_2, \dots\ t_k)$ where $t_i$ is a term for all $i$.
  \end{itemize}

  A {\em formula} is defined as either:
  \begin{itemize}
    \item $t_1 = t_2$ where $t_1, t_2$ are terms;
    \item $P(t_1, t_2,\dots\ t_k)$ where all $t_i$ are terms and $P$ is a
predicate;
    \item $\varphi_1 \wedge \varphi_2$ or $\varphi_1 \vee \varphi_2$
      or $\neg \varphi_1$ where $\varphi_1,\varphi_2$ are formulae;
    \item $\exists x \varphi$ or $\forall x \varphi$ where $\varphi$ is a
formula.
  \end{itemize}

Note: $\varphi \to \psi$ means $\neg \varphi \vee \psi$ (implication)

Note: a formula of no free variables is called a {\em sentence}. (ex:
$\forall x P_1(x)\vee\neg P_2(x)$).

\end{definition}


\begin{definition}[Semantics of FO]
Given a $\sigma$-structure $\frA$, we define inductively for each term
$t$ with free variables $(x_1, \dots, x_n)$ the value
$\intp{t}{\frA}(\abar)$, where $\abar \in A^n$, and for each formula
$\varphi(x_1, \dots, x_n)$, the notion of $\frA \models
\varphi(\abar)$ (i.e., $\varphi(\abar)$ is true in $\frA$), read $\frA$
{\em satisfies} $\varphi(\abar)$.

For {\em terms}:
\begin{itemize}
  \item if $t$ is a constant $c$, then $t^\frA = c^\frA$;
  \item if $t$ is a variable $x$, then $t^\frA (\bar{a})$ is $a_i$;
  \item if $t = f(t_1,t_2,\dots t_k)$, then $t^\frA (\bar{a}) = f^\frA
    (t_1^\frA(\bar{a}), t_2^\frA(\bar{a}),\dots t_k^\frA(\bar{a}))$;
\end{itemize}

For {\em formulae}:
\begin{itemize}
\item if $\varphi$ is $(t_1 = t_2)$, then $\frA \models
  \varphi(\bar{a})$ iff $t_1^\frA(\bar{a}) = t_1^\frA(\bar{a})$;
\item if $\varphi$ is $P(t_1,t_2,\dots t_k)$, then $\frA \models
  \varphi(\bar{a})$ iff $(t_1^\frA(\bar{a}), t_2^\frA(\bar{a}),\dots
  t_k^\frA(\bar{a})) \in P^\frA$;
\item if $\varphi$ is $\varphi_1 \wedge \varphi_2$, then $\frA \models
  \varphi(\bar{a}) \wedge \varphi(\bar{a})$ iff $\frA \models
  \varphi_1(\bar{a})$ AND $\frA \models \varphi(\bar{a})$; (etc for
  $\vee$ and $\neg$.)
\item if $\varphi$ is $\exists y \varphi$, then $\frA \models
  \exists y \varphi(y,\bar{a})$ iff $\frA \models \varphi(a',\bar{a})$
  for some $a' \in A$. (etc for $\forall$.)
\end{itemize}
\end{definition}

\begin{definition}[Relational calculus query.]
A $k$-ary {\em relational calculus query} over schema $\sigma$ is a
mapping $Q : \sigma\text{-instances} \to \bbD^n$ given by an
expression of the form
\[
\{ (x_1, \dots, x_n) \ | \ \varphi(x_1, \dots, x_n) \}
\]
where $\varphi(x_1, \dots, x_n)$ is a FO formula.  The {\em
  evaluation} $\eval{Q}{I}$ of $Q$ on $\sigma$-instance $I$ is 
\[
\eval{Q}{I} \defeq \{ (a_1, \dots, a_n) \in \bbD^n \ | \ I \models
\varphi(a_1, \dots, a_n) \}
\]
\end{definition}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 1/8/10}{Anand}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\topic{Examples of relational calculus queries}

Let us consider a schema having 3 relations: a ternary $\Class$ relation
(first column ``class ID'', second column ``class name'', third column
``room number''), a binary $\Student$ relation (first column ``student
ID'', second column ``student name''), and a binary $\Takes$ relation
(first column ``student ID'', second column ``class ID'').  We also assume
constants for names, IDs, etc.

\begin{example}
Q1: Find names of all students taking a class meeting in Wellman 201.
\[
 \{ (x) \ | \ \exists s \exists c \exists n \ \Student(s,x) \wedge
 \Takes(s,c) \wedge \Class(c,n,\text{``Wellman 201''})  \}
\]
\end{example}

\begin{example}
Q2: Find all pairs of students not taking a class together.
\[
\{ (x,y) \ | \ \exists s,s' \ \Student(s,x) \wedge \Student(s',y)
\wedge \neg \exists c (\Takes(s,c) \wedge \Takes(s',c)) \}
\]
\end{example}

\topic{Review of first-order logic, continued}

\begin{itemize}
 \item Satisfaction of FO formulae (with free variables):
$\frA \models \varphi(\bar{x})$ means $\frA \models \varphi(\bar{a})$
for all $\bar{a} \in A^k$

\item If $\Gamma$ be a set of FO sentences, then say $\Gamma \models
  \varphi$ holds if any structure $\frA$ satisfying $\Gamma$ also
  satisfies $\varphi$

  \item  Validity: $\models \varphi$  means $\frA \models \varphi$ for
    any structure $\frA$ (i.e., $\varphi$ is {\em valid})
 
  \item $\vdash$ means ``provable''; $ \Gamma \vdash \varphi $ means
    $\varphi $ is provable from $\Gamma$.
\end{itemize}

\begin{theorem}[\textbf {G\"{o}del's Completeness Theorem}]
\[
\Gamma \models \varphi \quad \text{iff} \quad \Gamma \vdash \varphi
\]
\end{theorem}
As a corollary, we have:
\[
\{ (\Gamma,\varphi) \mid \Gamma \models \varphi \}  \text{is \: r.e.}
\]
In particular, taking \VALID\ to be the set of all valid FO sentences 
\[
\VALID  \defeq \{ \varphi \ \mid \ \models \varphi \},
\]
we have
\begin{corollary}
 $\VALID$ is r.e.
\end{corollary}

Even better would be if we could actually {\em decide} validity of FO
sentences.  However, a negative answer was provided to this question
was Church and Turing:

\begin{theorem}[\textbf{Church-Turing Undecidability Theorem}]
$\VALID$ is undecidable.
\end{theorem}

In database theory, however, we are not concerned with validity over
{\em all} structures, but rather validity over finite structures (i.e.,
over all databases).  
 $$\FINVALID \defeq \{ \varphi \mid \frA \models \varphi  \text{ for
   all finite } \frA \} $$
Unforunately, this language turns out to be undecidable as well:
\begin{theorem}[\textbf{Trakhtenbrot's Theorem}]
\FINVALID\ is undecidable. 
\end{theorem}
In fact, \FINVALID\ is not even r.e.!  This is easy to see since
\FINVALID\ is clearly co-r.e.~(to check whether a sentence $\varphi$
is {\em not} in \FINVALID, it suffices to just enumerate finite
structures $\frA$ checking $\frA \models \varphi$ until a
counterexample is found).

\begin{definition}
Relational calculus queries $Q, Q'$ are {\em equivalent} if for any
database instance $I$, we have $\eval{Q}{I} = \eval{Q'}{I}$.
As a homework exercise, we will prove that equivalence is undecidable
as a consequence of results above.
\end{definition}



\begin{proof} (of Church-Turing Theorem) By reduction from the String
  Rewriting Problem (SRP).  We know that SRP is undecidable. So if we
  can show that SRP $\leq$ VALID then we can prove VALID is
  undecidable as well.

  We will describe the total computible function that performs the
  reduction SRP $\leq$ VALID by the program which computes it. The
  input to this program is of the form $(R,u,v)$ where $R$ is a finite
  set of string rewrite rules over an alphabet $\Sigma$ and $u$, $v$
  are strings over $\Sigma$.  The reduction produces as output an FO
  sentence $\Phi$.  For this to be a many-one reduction, we need to
  show that $u \stackrel{R}{\twoheadrightarrow} v$ iff $\models \Phi$.

First the reduction has to construct the FO vocabulary over which $\Phi
$ is built.  This contains: a binary predicate symbol $\Rew$; a unary
function symbol $f_{\alpha}$ for each $\alpha \in \Sigma$; and a
single constant symbol $c$.  

% Intuitively, we'll have in mind 

% think of $\Rew$ as
% representing the rewrite relation for $R$
% $\Sigma$, the alphabet over which strings in R, u and v are built.
% For each $\alpha \in \Sigma$, the FO vocabulary will contain
% a unary function symbol $f_{\alpha} $ (where $f_{\alpha} $ appends
% $\alpha$ in front of every word) and an empty string $c$(a constant
% symbol).

Next, for each rewrite rule $r \epsilon R$
$$ r:
\alpha_1 \cdots \alpha_m \rightarrow \beta_1 \cdots \beta_n
$$ 
the reduction will construct a sentence
$$ \varphi_{r} \defeq \Rew(f_{\alpha_1} (\dots f_{\alpha_m}(x)
\cdots), f_{\beta_1} (\dots f_{\beta_n}(x) \cdots) )$$
and then if $ R = \{r_1,\dots,r_{k} \}$, the reduction will construct 
$$
\varphi_{R} \defeq \varphi_{r_1} \wedge \cdots  \wedge \varphi_{r_k}.
$$
Now, if $u = \delta_1 \cdots \delta_p$ and $v = \epsilon_1 \cdots
\epsilon_q$,
the reduction will construct
$$ \varphi_{u,v}  \defeq \Rew(f_{\delta_1}
(\cdots f_{\delta_p}(c) \cdots), f_{\epsilon_1} (\cdots f_{\epsilon_q}(c)
\cdots )) $$ 
Next, we need to some add some assertions that specify that $\Rew$
behaves like a rewrite relation:
$$\varphi_{\Rew} \defeq ( \forall x \:
Rew(x,x) \wedge (\forall x,y,z \Rew(x,y) \wedge \Rew(y,z) \rightarrow
Rew(x,z)) \wedge $$ $$\bigwedge _{ \alpha \epsilon \Sigma} (\forall
x,y \Rew(x,y) \rightarrow \Rew(f_{\alpha} (x), f_{\alpha} (y))) $$ 
Finally, the reduction will construct
$$ \Phi \defeq (\varphi_{R} \wedge \varphi_{\Rew} ) \rightarrow \varphi_{u,v}
$$
It remains to show that $u \stackrel{R}{\twoheadrightarrow} v$ iff
$\models \Phi$.  For proof of this claim, see the Friendly Logics
notes.

\end{proof}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 1/11/10}{TJ}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

See the ``Friendly Logics'' notes, Section 4.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 1/13/10}{TJ}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

See the ``Friendly Logics'' notes, Section 5.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 1/15/10}{TJ}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

See the ``Friendly Logics'' notes, Section 6.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 1/20/10}{Vu}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\topic{Domain Independence}

Recall that a \emph{relational calculus query} (or FO query) has the
form
$$q \equiv \{\bar{x} \ | \ \varphi(\bar{x})\}$$
The output of relational calculus queries might be infinite.
For example, the output of relational calculus query $$\{x \ | \ \neg
R(x)\}$$ is infinite since $${\llbracket \{x \ | \ \neg R(x)\}
  \rrbracket}^\mathcal{I} = \mathbb{D} - R^\mathcal{I}.$$ 
Another infinite-output query is $\{(x,y) | R(x) \lor S(y) \}$.

More subtly, the output of query
$$\{x | \forall y R(x,y)\}$$
is finite, but {\em depends on the contents of $\bbD$} (and not just
the contents of the predicates in the database instance).

All the queries above are {\em domain-dependent queries}.
Domain-dependence is generally considered an undesirable property of
query languages.

Let $\mathcal{I}$ is a structure with domain $\mathbb{D}$. The {\em
  active domain} of database instance $\mathcal{I}$ is the set
$adom(\mathcal{I})$ of all values from $\mathbb{D}$ occurring in an
interpretation of some predicate $P^\mathcal{I}$. We have
$adom(\mathcal{I}) \subseteq \mathbb{D}\nonumber$.  Likewise if $Q$ is
a relational calculus query, then $adom(Q)$ is the finite set of
constants occurring in $Q$.


The restriction of a database instance $\mathcal{I}$ to the universe $D
\supseteq adom(I)$ is
\begin{center}
$\mathcal{I}/D \defeq \langle D, \{P_i^{\mathcal{I}/D}\}, \emptyset,
\{c\} \rangle$
\end{center}
where $P_i^{\mathcal{I}/D} =P_i^D$.\\
A relational calculus query $Q$ is {\em domain-independent} if for any
database instance $\mathcal{I}$ and any $D_1, D_2$ such that
\begin{center}
$adom(\mathcal{I}) \cup adom(Q) \subseteq D_1 \subseteq D_2 \subseteq
\mathbb{D}$
\end{center}
we have
\begin{center}
$\llbracket Q\rrbracket ^{\mathcal{I}/D_1} = \llbracket Q\rrbracket
^{\mathcal{I}/D_2} \big ( = \llbracket Q\rrbracket ^{\mathcal{I}} = \llbracket
Q\rrbracket ^{adom(\mathcal{I}) \cup adom(Q)}\big )$
\end{center}
\begin{theorem}
Domain-Independence of a relational calculus query is undecidable.
\end{theorem}
\begin{proof}
Reduce FIN-VALID to Domain-Independence.
\begin{center}
$\varphi \longmapsto q \equiv \{x | \neg \varphi \land \neg R(x)\}$
\end{center}
where $R$ does not occur in $\varphi$.\\
Suppose $\varphi \in $ FIN-VALID (and thus $\neg \varphi$ is unsatisfiable)
then 
\begin{center}
$\llbracket Q\rrbracket ^\mathcal{I} = \{\}$, for any $\mathcal{I}$
\end{center}
Hence, $Q$ is domain-dependent.\\
Suppose $\varphi \notin $ FIN-VALID then
\begin{center}
$\exists \mathcal{A} $ s.t. $\mathcal{A} \models \neg \varphi$
\end{center}
Construct database instance $\mathcal{I}$ which in $\mathcal{A}$ along with
interpretation of $R$
\begin{center}
$R^\mathcal{I} = \{\}$
\end{center}
Then $\llbracket Q\rrbracket ^\mathcal{I} = \mathbb{D}$.\\
Moreover, for any $\mathbb{D}$ s.t. $adom(\mathcal{I}) \subseteq D \subseteq
\mathbb{D}$ we have $[Q\rrbracket ^{\mathcal{I}/D} = \mathbb{D}$.\\
Thus $Q$ is not domain-independent.
\end{proof}


\topic{Relational Algebra}

The {\em relational algebra} has operators $P, \{a\}, \sigma, \pi,
\times, - $.  The semantics of a relational algebra expression $E$ is
defined inductively as follows.
\begin{itemize}
\item {\bf Predicate} $P$: $\llbracket P \rrbracket^\mathcal{I} =
P^\mathcal{I}$\\
\item {\bf Constant} $\{c\}$: $\llbracket \{c\} \rrbracket^\mathcal{I} =
\{c\}$\\
\item {\bf Selection-1} $\sigma_{i=j}E:$ $\llbracket \sigma_{i=j}E
  \rrbracket^\mathcal{I} = \{\bar{x}\ |\ \bar{x} \in \llbracket E
\rrbracket^\mathcal{I} \text{ and } x_i = x_j\}$\\
\item {\bf Selection-2} $\sigma_{i=c}E:$ $\llbracket \sigma_{i=c}E
  \rrbracket^\mathcal{I} = \{\bar{x}\ |]\ \bar{x} \in \llbracket E
\rrbracket^\mathcal{I} \text{ and } x_i = c\}$\\
\item {\bf Projection} $\pi_{i_1,...,i_k}E:$ $\llbracket
  \pi_{i_1,...,i_k}E \rrbracket^\mathcal{I} =
  \{(x_{i_1},...,x_{i_k})\ |\ \bar{x} \in \llbracket E \rrbracket^\mathcal{I}
\}$\\
\item {\bf Cross Product} $E_1 \times E_2:$ $\llbracket E_1 \times E_2
  \rrbracket^\mathcal{I} = \{\bar{x},\bar{y}\ |\ \bar{x} \in \llbracket E_1
\rrbracket^\mathcal{I} \text{ and } \bar{y} \in \llbracket E_2
\rrbracket^\mathcal{I} \}$\\
\item {\bf Union} $E_1 \cup E_2:$ $\llbracket E_1 \cup E_2
  \rrbracket^\mathcal{I} = \{\bar{x}\ |\ \bar{x} \in \llbracket E_1
  \rrbracket^\mathcal{I} \text{ or } \bar{x} \in \llbracket E_2
  \rrbracket^\mathcal{I} \}$\\ 
\item {\bf Difference} $E_1 - E_2:$ $\llbracket E_1 - E_2
  \rrbracket^\mathcal{I} = \{\bar{x}\ |\ \bar{x} \in \llbracket E_1
  \rrbracket^\mathcal{I} \text{ and } \bar{x} \notin \llbracket E_2
  \rrbracket^\mathcal{I} \}$\\ 
\end{itemize}

Note that intersection is not included in the list above as it can be
defined using cross product, selection, and projection.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 1/22/10}{Zhongxian}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\topic{Paper Presentation}

Students are going to present papers from the following four directions at the
last two weeks. 
\begin{itemize}
	
	\item Data exchange (2) [Zhongxian, Vu]
	
	\item Bag semantics     [Sarah]
	
	\item Probabilistic/incomplete databases (2) [Thanh, Mingming]
	
	\item Data provenance   [Sven]

\end{itemize}

\subsubsection*{Data exchange} Assume we have a source database schema and a
target database schema. How can we exchange data from the source to the target.
One approach is to use schema mappings(logical mappings).

\subsubsection*{Bag semantics} Bag semantics deal with the problem of duplicate
rows generated by queries. 

\subsubsection*{Probabilistic/incomplete database} Databases with not sure of
null values in rows. 

\subsubsection*{Data provenance} Data provenance studies unified formalism of
data representation.


\topic{Relational algebra query captures domain independent FO query}

We'd like to prove that :$\textrm{domain independent } FO = RA$.

\begin{theorem}
For any $E\textrm{(expression} \in RA$, we can compute an equivalent domain
independent FO query. 
\end{theorem}

\subsubsection*{proof (via structural reduction)}
\begin{itemize}
 \item case: $P \longmapsto \{\bar{x} | P(\bar{x})\}, \bar{x} \textrm{ fresh}$ 

 \item case: ${c} \longmapsto \{x | x = c \}, x \textrm{ fresh}$

 \item case: Assuming $E \longmapsto  \{\bar{x} |
\varphi(\bar{x})\}, \sigma_{i=j}(E) \longmapsto \{\bar{x} |
\varphi(\bar{x}) \land x_i = x_j\}$

 \item case: Assuming $E_1 \longmapsto \{\bar{x} |
\varphi(\bar{x})\}, E_2 \longmapsto \{\bar{y} |
\psi(\bar{y})\}, E_1 \times E_2 \longmapsto \{\bar{x},\bar{y}
| \varphi(\bar{x}) \land \psi(\bar{y})\} $

 \item case: Assuming $E \longmapsto  \{\bar{x} |
\varphi(\bar{x})\}, \pi_{i_1,..,i_k}(E) \longmapsto \{y_1,...,y_k
|\varphi(\bar{x})\land y_1=x_1 \land ... \land y_k = x_k\} $

 \item case: Assuming $ E_1 \longmapsto \{\bar{x} |
\varphi(\bar{x})\}, E_2 \longmapsto \{\bar{y} |
\psi(\bar{y})\}. E_1 \cup E_2  \longmapsto \{\bar{x} |
\varphi(\bar{x}) \lor \psi(\bar{x}) \}$

 \item case: Assuming $E_1 \longmapsto \{\bar{x} |
\varphi(\bar{x})\}, E_2 \longmapsto \{\bar{y} |
\psi(\bar{y})\}. E_1 - E_2 \longmapsto \{\bar{x} |
\varphi(\bar{x}) \land \neg \psi(\bar{x}) \}$

\end{itemize}


Now, we prove the other direction: Domain independent FO query can be expressed
using relational algebra. We use two steps to prove that. 
\newline
 
Step 1: For any FO query $Q$, can compute a RA query $E$ over schema $\sigma$
of $Q$ extended with a unary predicate $D$, such that for any database instance
$I$, we have $ \llbracket Q \rrbracket^{I/D^I} = \llbracket E \rrbracket^I $.
\newline

Step 2: Recall $Q$ is domain independent, then $ \llbracket Q \rrbracket^I =
\llbracket Q \rrbracket^{I/adom(I)\cup adom(Q)}$. Plugin $adom(I) \cup adom(Q)
= D^I$. Observe that $adom(I) \cup adom(Q)$ can be computed using a RA query
$E_{ad}$. 

Say $\sigma = \{R( , ), S( , , )\}$ and $adom(Q)$ has constants $a$,$b$. Then
define 

$E_ad \stackrel{def}{=} \pi_1(R) \cup \pi_2(R) \cup pi_1(S) \cup pi_2(S) \cup
pi_3(S) \cup \{a\} \cup \{b\}. $

\subsubsection*{proof (step 1, by induction on query)}

\begin{itemize}
 \item case: $\{x_1,...,x_n | x_i = c \} \longmapsto \sigma_{i=c}(D \times D
\times ... \times D) (n times) $

 \item case: $\{x_1,...,x_n | \neg\varphi(x_1,...,x_n)\} \longmapsto
\textrm{{\color{red}{$D^n$}}} - E$

 \item case: Assuming $\{\bar{x} | \varphi(\bar{x})\} \longmapsto E_1 ,
\{\bar{x} |\psi(\bar{x})\} \longmapsto E_2 , \{\bar{x} | \varphi(\bar{x})
\land \psi(\bar{x})\} \longmapsto E_1 \land E_2$

 \item case: Assuming without losing generosity, all variables are distinct,
$\{x_1,...,x_n | P(x_1,...,x_n)\} \longmapsto P $ 

 \item case: Assuming $\{x_1,...,x_m,z,y_1,...,y_n|\varphi(...)\} \longmapsto
E, \{x_1,...,x_m,y_1,...,y_n|\exists z, \varphi(x_1,...x_m,z,y_1,...y_n)\}
\longmapsto \pi_{1,...,m,m+2,..,m+n+1}(E) $

\end{itemize}

\begin{theorem}
 Equivalence of RA query is undecidable. So do satisfiability and validity of
RA query.
\end{theorem}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 1/25/10}{Mingmin}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\subsubsection*{Model-checking problem}
Given $\psi\;and\;\mathfrak{A}$, to check if $\mathfrak{A}\models\psi$\\

\begin{definition}
(recognition problem) Given an FO query Q, database instance I, and an output tuple $\overline{a}$, is $\overline{a}\in\llbracket Q \rrbracket^{I}$?
\end{definition}

\begin{theorem}
If Q is domain-independent, then the recognition problem is LOGSPACE (data complexity) PSPACE-complete (combined complexity). Then we no longer have to encode size of universe in representation of I.
\end{theorem}

\flushleft

\subsubsection*{Conjunctive queries}
Essentially, FO queries of the form 
\[\{\overline{x}\;|\;\exists\overline{y}\;\psi(\overline{x},\overline{y})\}\]
where $\psi$ is a conjunction of atoms(relational or equality).\\
\quad\quad\quad\qquad\quad\qquad\quad\qquad\quad\qquad\quad\qquad$\swarrow\quad\quad\quad\quad\searrow$\\
\quad\quad\quad\qquad\quad\qquad\quad\qquad\qquad predicate, P(x,y)\quad\quad x=y or x=c\\


\begin{example}
\{$(x,z)\;|\;\exists{y} R(x,y)\land z=y\land S(y,x)$)\}\\
\quad\quad\quad\quad\quad\quad$\;\;\{(x,y)\;|\;R(x)\}\;or\;\{(x,y)\;|\;\exists{z}\;R(x)\land y=z)\}$
$\;\rightarrow$ needs to be ruled out(via range-restriction).\\
\end{example}

\subsubsection*{Conjunctive queries in SQL}
SELECT-FROM-WHERE fragment of SQL\\
\quad\quad\quad select R1,S2\\
\quad\quad\quad from    R,S\\
\quad\quad\quad where \underline{R1=c and S1=R1 and R3=S2}\\
\quad\quad\quad\quad\quad\quad\quad conjunction of equalities.\\
\quad\\

\begin{definition}
A \emph{tableau} T is a set of atoms (rel. or eq.) that is \emph{range-restricted} i.e. for any variable x$\in$vars(T), either T$\vdash$x=c or T$\vdash$x=x$'$ where x$'$ occurs in a relational atom in T.
\[eg\quad\{R(y),y=z,z=x\}\vdash\;y=x\]\\
\end{definition}

\begin{definition}
A \emph{conjunctive query} is given by a pair \big<$\overline{u}$,T\big> where $\overline{u}$ is a tuple (the ``output'' tuple), T is a tableau, and vars($\overline{u}$)$\subseteq$vars(T).\\
T=\{$A_{1}\ldots A_{n}$\}, \{$\overline{u}$ | $A_{1}\land\ldots\land A_{n}$\}.\\
\quad\\
\end{definition}

Rule-based, Prolog-style syntax for CQs. (Datalog-style)\\
\[Q(x,y):- R(x,z),x=c,S(x,y,z)\quad\longrightarrow\quad Q(c,y):- R(c,z),S(c,y,z)\]
\[\downarrow\quad\quad\quad\quad\quad\quad\quad\quad\quad\downarrow\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\]
\[output\;tuple\;or\;\underline{head}\quad\quad tableau\;or\;\underline{body}\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\]\\


This can $always$ be done, provided the query is $satisfiable$, i.e. it is not the case that T$\vdash c_{1}=c_{2}$ for distict constants $c_{1}, c_{2}$.\\
\quad\\
Can define sem. of CQs via translation to FO queries. But can define (equivalent) semantics directly via notion of valuations.\\\quad\\

\begin{definition}
Given tableau T, d.b. instance I, a \emph{valuation} for T in I is a mapping $\beta$: vars(T) $\to$ $\mathbb{D}$, extended to map constants to themselves. We say that $\beta$ satisfies T in I if\\
\begin{description}
\item[*] for any atom R$(\overline{x})$ in T, $\beta(\overline{x})\in R^{I}$
\item[*] for any atom e=e$'$ in T, we have $\beta(e)=\beta(e')$
\end{description}
\end{definition}

\begin{proposition}
For any CQ Q=\big<$\overline{u},T$\big>, and d.b. instance I.
\[\llbracket Q\rrbracket^{I}=\{\beta(\overline{u})\;|\;valuateion\;\beta\;satisfies\;T\}\]
Q is viewed as FO query.
\end{proposition}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 1/27/10}{Thanh}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{property}
A conjuntive querry $Q = <\bar{u},T> $ is satisfiable iff for all $c_i,c_j$ 
if $T\vdash c_i=c_j$ then $c_i=c_j$ 
\end{property}
We give a sketch proof by an example. 
\begin{example}
We prove the querry $< (x,c),\{R(x,c),S(x,y),S(y,z)\} > $ is satisfiable. \\
Database instance I: \hspace{10 pt} \underline{\hspace{10 pt}R \hspace{10 pt} } \hspace{10 pt} \underline{\hspace{10 pt}S \hspace{10 pt}} \\
\hspace{110 pt} $c_x $ $c$ \hspace{25 pt} $c_x$  $c_y$\\
\hspace{110 pt}  \hspace{45 pt}  \textrm{{\color{red}{$c_y$}}} 
\textrm{{\color{red}{$c_z$}}}  \\
then valuate $(c_x,c) \in\llbracket Q \rrbracket ^ I.$ \\ 
\end{example}

\begin{theorem}
The expression (and combined) complexity of recognition problem for CQs is NP-complete. (Recall for FO, PSPACE-complete ). 	
\end{theorem}
\underline{Proof:} We have two staments to show: \\
1. Combined complexity in NP: use as witness satisfying valuation  $\beta : vars(Q) \rightarrow \mathbb{D}$ .\\
2. Expression complexity is NP-hard: we use reduction from 3-coloring problem.\\\ 
\\\underline{Useful fact of Graph theory:}\\
A directed graph $G = (V,E)$ is k-colorable iff there exists a graph homomorphism $h:G\rightarrow C_k$ where $C_k$ is the complete graph on k  vertices. \\
Given $G = (V,E)$ and $G'=(V',E')$. A mapping is called a graph homomorphism if for all $u,v \in V$ \\
\begin{center}
$(u,v) \in E \Rightarrow (h(u),h(v)) \in E' $
\end{center} 
\underline{Proof:} Given $G=(V,E)$. Construct CQ $Q=<(),\{P(u,v)|(u,v) \in E\} >$ and a database instance I defined: \\
\begin{center}
$P^I = \{(r,g),(g,r),(b,r),(g,b),(b,g)\}$
\end{center}

\underline{Claim:} $() \in \llbracket Q \rrbracket ^ I$ iff G is 3-colorable (iff exists graph homomorphism $h:G \rightarrow C_3$).\\
\quad SPC () in input \\
\quad Then have set valuation $\beta: vars(Q) \rightarrow \mathbb{D}$ .\\

\begin{theorem}
CQs and SPC queries are expressively equivalent. 
\end{theorem}
\underline{Proof:} (Sketch) \\
\quad $CQs \rightarrow SPC$\\
Illustrate by example: \\
\begin{center}
$Q = <(x,d), \{R(x,y),S(y,c,z)\}>$ \\
Normal form: $\pi_{2,1}(\{d\} \times \sigma_{4=c}(\sigma_{2=3}(R \times S)))$
\end{center}  

\quad $SPC \rightarrow CQs $ \\
Using HW2 and HW3 can always put SPC querry in normal form the "read" as CQ.

\begin{definition}
A querry Q is contained in another querry Q', written $Q \sqsubseteq Q'$ if for any database instance I, we have $\llbracket Q \rrbracket ^I \subseteq \llbracket Q' \rrbracket ^ I$. Moreover, Q and Q' are equivalent, written $Q \equiv Q'$ if
\begin{center}
$\forall I$ $\llbracket Q \rrbracket ^ I = \llbracket Q' \rrbracket ^ I$
\end{center}  
\end{definition}
(Note that $Q \equiv Q' $ if $Q\sqsubseteq Q'$ and $Q'\sqsubseteq Q$ ).

We'll show for CQs, $Q \sqsubseteq Q'$ is decidable (and NP-complete).

\begin{definition}
Given CQs $Q=<\bar{u},T>$ and $Q'=<\bar{v},T'>$, a containment mapping $h: Q' \rightarrow Q $ is a mapping $h: vars(T') \rightarrow \mathbb{D}$, extended to map any constant to itself and such that: \\
\quad * $h(\bar{V})=\bar{u} $\\
\quad * for any relational atom $P(\bar{e})$ in T', we have $P(h(\bar{e})) \in T $.
\end{definition} 
\begin{example}
\quad \quad $Q = <(x,y), {R(x,y,c),R(x,z,c)}>$ \\
\hspace{82 pt} $Q' = <(u,v), {R(u,v,c)}>$ \\
\hspace{82 pt} $h: Q' \rightarrow Q: \quad u \rightarrow x$ \\
\hspace{147 pt} $v \rightarrow y $ \\
\hspace{147 pt} $c \rightarrow c $ 
\end{example}

\begin{lemma}
1. The identity mapping is a containment mapping id $Q \rightarrow
\textrm{{\color{red}{$Q$}}} $ \\
\hspace{60 pt}2. Containment mappings compose:
\begin{center}
$h: Q \rightarrow Q' $, $g: Q' \rightarrow Q''$
\end{center}
\begin{center}
$g\circ h : Q \rightarrow Q'' $
\end{center}
\end{lemma}
\begin{definition}
If $Q=<\bar{u},T>$ is a CQ, the canonical database denoted can(Q) for Q is the database instance obtained b viewing T as a database instance.
\end{definition}

\begin{example}
$T = \{ R(x,y), S(y,z,c)\}$\\
\hspace{80 pt} can(Q) = \hspace{10 pt} \underline{\hspace{10 pt} R \hspace{10 pt}} \hspace{20 pt} \underline{\hspace{10 pt}S\hspace{10 pt}} \\
\hspace{150 pt} x y \hspace{30 pt} y z c \\
\hspace{150 pt} "frozen" variables
\end{example}
\begin{theorem}[Chandra and Merlin, 1977]
For CQs Q,Q' the following statements are equivalent:\\
1. $Q \sqsubseteq Q'$ \\
2. There is a containment mapping $h: Q' \rightarrow Q $\\
3. $\llbracket Q \rrbracket ^ {can(Q)} \subseteq \llbracket Q' \rrbracket ^ {can(Q)}$  
\end{theorem}
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 1/29/10}{Sarah}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\underline{proof:} of Chandra and Merlin
\quad $(1) \Rightarrow (3):$
\quad \quad  is self-evident.

\quad $(3) \Rightarrow (2):$
\quad \quad suppose $\bar{u} \in \llbracket Q' \rrbracket ^ {can(Q)}$, then $\exists$ a satisfying valuation $\beta : vars(Q') \rightarrow \mathbb{D}$, ie $\beta (\bar{v})=\bar{u}$.
\quad \quad For every atom $P(\bar{v})\in T'$ we have $\beta(\bar{v}) \in P^{can(Q)}$. Now we can view $\beta$ as a containment mapping $\beta: Q' \rightarrow Q$.

\quad $(2) \Rightarrow (2):$
\quad \quad suppose we have a containment mapping $h: Q \rightarrow Q'$. Consier an arbitrary $I$ and $\bar{a}$. If $\bar{a} \in \llbracket Q \rrbracket^{I}$ then we have a satisfying valuation $\beta: vars(Q) \rightarrow \mathbb{D}$, so $\beta \circ h$ is also a satisfying valuation, $\beta \circ h: vars(Q) \rightarrow \mathbb{D}$.
\quad \quad $\therefore h(\bar{v})=\bar{u}, \beta(\bar{u})=\bar{a}, \beta \circ h(\bar{v})=\bar{a}$
\quad \quad $\therefore P(\bar{v})\in T', P(h(\bar{v}))\in T, \beta(h(bar{v}))\in P^{I}$.

\underline{corollary:}
\quad testing $Q \sqsubseteq Q'$ is NP-complete.
\underline{proof:} (sketch)
\quad Guessing a containment mapping $h$ is NP, and hard because it is essentially a recognition problem, ie is $\bar{a} \in \llbracket Q \rrbracket ^{I}$?
\quad let $I \mapsto T$, so consider $I$ a ``frozen query.''
\quad let $\bar{a} \mapsto \bar{u}$ where $Q'=<\bar{u},T>$.
\quad $Q' \sqsubseteq Q$ iff $\bar{u} \in \llbracket Q \rrbracket ^ {can(Q')}$
\quad and iff $\bar{a} \in \llbracket Q \rrbracket ^ {I}$

\subsection*{Query Minimization}

\begin{example}
\begin{center}
$Q(x.y) :- R(x,y), R(x,z)$
$\downarrow$
$Q'(x,y) :- R(x,y)$
\end{center}
\end{example}

\begin{definition}
\quad Given a conjunctive query $Q = <\bar{u}, T>$, a \underline{subquery} is a conjunctive query of the form:
\quad \quad $Q_S = <\bar{u}, S>$ where $S \subseteq T$.
\end{definition}

\quad note: if $Q_S$ is a subquery of $Q$, then $Q \sqsubseteq Q_S$ because we can define $h: Q_S \rightarrow Q$ as a containment mapping. This seems backwards, so here's an example:

\begin{example}
\begin{center}
$Q(x,y) :- R(x,z), R(z,u), S(z,y)$
$\downarrow$
$Q_S (x,y) :- R(x,z), S(z,y)$
\end{center}
\quad note the original query is more restrictive and wants a more specific part of the database. It is therefore contained by the less restrictive subquery. 
\end{example}

\quad We are interested in cases where $Q_S \sqsubseteq Q$ because we can use this to infer $Q \equiv Q_S$.

\begin{definition}
A \underline{locally minimal} conjunctive query $Q = <\bar{u},T>$ has no strict subquery $Q_S$ such that $Q_S \equiv Q$.
\end{definition}

\begin{lemma}
\label{lemma_surjective_on_atoms_implies_surjective_on_vars}
\quad Let $h$ be a containment mapping from $<\bar{u},T>$ to $<\bar{v},T'>$. Then:
\begin{itemize}
\item if $h$ is \underline{injective} (ie one-to-one) on variables, then it is injective on atoms.
\item if $h$ is \underline{surjective} (ie onto) on atoms, then it is surjective on variables.
\end{itemize}
\quad Note that the converses of these statements \underline{need not hold}.
\end{lemma}

\begin{definition}
An \underline{isomorphism} is a containment mapping that is bijective on variables and atoms. (ie, a renaming of variables)
\end{definition}

\begin{lemma}
\quad A conjunctive query $Q$ is locally minimal iff \underline{any} containment mapping $h$ from $Q$ to itself is an isomorphism.
\end{lemma}

\underline{proof:} 
(of converse)
Consider $Q_S = <\bar{u},S>$ a subquery of $Q = <\bar{u},T>$ such that $Q_S \equiv Q$. Since $Q_S \sqsubseteq Q$, we have a containment mapping $h: Q \rightarrow Q_S$. We can also write $h: Q \rightarrow Q$ since $Q$ and $Q_S$ are equivalent, so $T = H(T)$. Now $T \subseteq S$ because of containment, and $S \subseteq T$ because of the subset, so $S = T$ and $Q = Q_S$, hence $Q$ is locally minimal.
(of lemma)
Let $h: Q \rightarrow Q_S$ be a containment mapping, and $Q' = <\bar{u},h(T)>$ be a conjunctive query, where $Q'$ is a subquery of $Q$. Because of $h$ we know $Q' \sqsubseteq Q$, and $Q' \equiv Q$. Since $Q$ is locally minimal, $Q = Q'$. So $h$ must be a surjection $T \rightarrow T$ and $var(T) \rightarrow var(T)$. Since $T$ and $vars(T)$ are finite, a surjection from $T$ to itself or $vars(T)$ to itself is also injective, ie an isomorphism $\therefore$ $h$ is also an isomorphism.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 2/1/10}{Daniel}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{proposition} \label{propminimapcmisiso} If a CQ $Q$ is locally minimal then any containment mapping $h:Q\rightarrow Q$ is an isomorphism.
\end{proposition}

\begin{theorem} \label{thm_locally_minimal_is_subquery}
Consider a CQ $Q$. Any locally minimal CQ $Q'$ equivalent to $Q$ is isomorphic to some subquery of $Q$.
\end{theorem}
\begin{proof}
Let $Q=<\bar{u},T>$, $Q'=<\bar{u},T'>$. Since $Q\equiv Q'$, we have containment mappings $h:Q\rightarrow Q'$ and $h:Q'\rightarrow Q$. Moreover, $h\circ g: Q' \rightarrow Q'$ is a containment mapping, in fact by Prop.~\ref{propminimapcmisiso}, it is an isomorphism. Since $h\circ g$ is an isomorphism, $g$ has to be injective (on variables and atoms). 

Now, consider $Q_s = <\bar{u},g(T')>$, a subquery of $Q$. We claim, $g$ is an isomorphism from $Q'$ to $Q'$. This is true, since (1) $g$ is a containment mapping, (2) $g$ is injective on atoms and variables, (3) $g$ is surjective on atoms by construction, and (4) due to Lemma~\ref{lemma_surjective_on_atoms_implies_surjective_on_vars}, also surjective.
\end{proof}

\begin{corollary} If two CQs $Q$ and $Q'$ are locally minimal and equivalent, then they are isomorphic.
\end{corollary}
\begin{proof}
   By theorem~\ref{thm_locally_minimal_is_subquery}, $Q$ is isomorphic to some subquery of $Q'$; but $Q'$ is locally minimal and thus this subquery has to be $Q'$, thus $Q$ isomorphic to $Q'$.
\end{proof}

\textbf{Optimization Procedure:} \\
Given CQ $Q = <\bar u, T>$\\
$\;$ Set $Q_s := Q$\\
$\;$ While exists a containment mapping from $Q_s$ to a strict subquery $Q'_s$ of $Q_s$ \\
$\;$ $\qquad$Set $Q_s := Q'_s$\\
$\;$ Return $Q_s$.

\begin{definition} A CQ is said to be \emph{globally minimal} if it has the smallest number of atoms of any CQ equivalent to it.
\end{definition}
\begin{proposition} A CQ is locally minimal iff it is globally minimal.
\end{proposition}
\begin{example}[CQ Minimization]
$$ Q(x,y,z) :- R(x,y_2,z_2),R(x_1,y,z_1),R(x_2,y_2,z),R(x,y_1,z_1),R(x_2,y_1,z) $$
We guess that a good containment mapping $h:Q\rightarrow Q$ would be one that changes $z_2 \mapsto z_1$ and $y_2\mapsto y_1$. With this, $Q'$ would be:
$$ Q'(x,y,z) :- R(x,y_1,z_1),R(x_1,y,z_1),R(x_2,y_1,z),R(x,y_1,z_1),R(x_2,y_1,z) $$
The last two atoms are redundant as they already occur in $Q'$. Also, each of the atoms in $Q'$ is present in $Q$, so $h$ actually was a containment mapping. Without the redundant atoms we get:
$$ Q''(x,y,z) :- R(x,y_1,z_1),R(x_1,y,z_1),R(x_2,y_1,z) $$
Through exhaustive search, we can verify that this query is actually minimal.
\end{example}

For a query $Q$, a minimal query $Q'$ is also called the \emph{core} of $Q$. This terminology comes from graph theory.

\subsubsection*{Integrity Constraints}

A commonly used constraint is a \emph{key constraint}: Given a relational schema $R(A,B,C)$, we say ``$A$ is a key for $R$'', if the value of $A$ identifies the tuple, i.e., if there is a functional dependency $A\rightarrow B,C$. Keys can also contain two or more attributes: $A,B\rightarrow C$ or ``$A,B$ is a superkey (or compound key) of $R$''.

A further constraint type is a \emph{foreign key constraint}: Given $R(A,B,C)$ and $S(C,D)$ we say ``$C$ is a foreign key in $S$ referencing $R$'' if for every tuple $(a,b,c) \in R$, there exists a tuple $(c,d) \in S$ for some $d$.

We will see that under constraints, non-equivalent queries can become equivalent. This will lead to further optimization opportunities. Consider $Q(x,y,z):-R(x,y,z)$ and $Q'(x,y,z):-R(x,y,z),S(z,u)$. These are clearly not equivalent; however under the constraint that the first column in $S$ is a foreign key referencing the third column in $R$, the queries are equivalent.

Dependencies in general, are FO logical assertions, i.e., sentences of various forms.

\begin{definition} An \emph{embedded dependency (ed)} is a FO sentence of the form
$$\forall \bar x \varphi(\bar x) \rightarrow \exists \bar y \psi(\bar x,\bar y),$$
where $\varphi$ is a conjunction of relational atoms, and $\psi$ is a conjunction of relational atoms or equality atoms.
\end{definition}
\begin{example} $\,$\\
``$A$ is key in $R(A,B,C)$'' would be $\forall x,y,z,y',z' \, R(x,y,z)\wedge R(x,y',z') \rightarrow y=y' \wedge z=z'$. \\
``$C$ is a foreign key in $S(C,D)$ referencing $R(A,B,C)$'' would be $\forall x,y,z \, R(x,y,z) \rightarrow \exists u \, S(z,u)$.
\end{example}

\begin{definition} A \emph{conjunctive containment dependency (ccd)} is an assertion of the form:
$$ Q \sqsubseteq Q',$$
where $Q,Q'$ are conjunctive queries.
\end{definition}

\begin{proposition} Embedded dependencies and conjunctive containment dependencies are equally expressive.
\end{proposition}
\begin{proof}\textbf{Sketch.} 
(1) Given an ed $d=\forall \bar x \varphi(\bar x)\rightarrow \exists \bar y \psi(\bar x,\bar y)$. Let $Q=<\bar x, \varphi>$ (where $\varphi$ is viewed as a tableau, i.e., as a set of atoms). Let further $Q' = <\bar x,\varphi \cup \psi>$ (again, with $\varphi$ and $\psi$ viewed as sets of atoms). We now claim that for any database instance $I$: $I\models d$ iff $\eval{Q}{I} \subseteq \eval{Q'}{I}$. \\
(2) Given a ccd $Q\sqsubseteq Q'$ with $Q=<\bar u,T>$ nd $Q'=<\bar u,T'>$. Let $\cont(Q,Q')$ be the embedded dependency. Further, let $\bar z$ be the variables in $T$ but not in $\bar u$ and let $\bar z'$ be the variables in $T'$ but not in $\bar u$. Now, 
$$\cont(Q,Q') \defeq \forall \bar u \forall \bar z \,T\, \rightarrow \exists \bar u' \exists \bar z' \, T' \wedge \bar u = \bar u'$$ where $T$ and $T'$ are viewed as conjunctions of atoms.
We now claim that for any database instance $I$:  $\eval{Q}{I} \subseteq \eval{Q'}{I}$ iff $I \models \cont(Q,Q')$.
\end{proof}
\begin{corollary} Testing whether an ed or a ccd holds in all instances is NP complete.
\end{corollary}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 2/3/10}{Mingmin}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{example}
Consider a CQ
\[Q(y,z) :-\;R(x,y,z'),R(x,y',z)\]
As is, Q is minimal. But suppose we assume the (key) dependency
\[\forall x,y,z,y',z'R(x,y,z)\wedge R(x,y',z')\to y=y'\wedge z=z'\]
Then, assuming dependency holds, Q is equivalent to 
\[Q'(y,z) :-\;R(x,y,z)\;.\]
\end{example}

\quad\\

\begin{example}
Consider a CQ
\[Q(x,y) :-\;R(x,y),S(x,z)\]
As is, Q is minimal. But if we assume the (foreign key) dependency
\[\forall x,yR(x,y)\to \exists zS(x,z)\]
Then, we can minimize Q to 
\[Q'(x,y) :-\;R(x,y)\;.\]
\end{example}
\quad\\

\textbf{Main Technique}: the $\mathbf{chase}$\\

\begin{example}
Consider a CQ (in relational calculus notation)
\[Q=\{\bar{x}\;|\;\exists\bar{y}\psi (\bar{x},\bar{y})\}\]
and an e.d.
\[d=\forall \bar{x},\bar{y}\psi (\bar{x},\bar{y})\to\exists \bar{z}\varphi
(\bar{x},\bar{y},\bar{z})\]
then a \textbf{chase step} from Q with d, written $Q\xrightarrow{d}Q'$, produces
CQ 
\[Q'=\{\bar{x}\;|\;\exists\bar{y}\exists\bar{z}\psi(\bar{x},\bar{y}
)\vee\varphi(\bar{x},\bar{y},\bar{z})\}\]
\end{example}
\quad\\

\begin{definition}
A \textbf{homomorhisim of tableaux} is a mapping h:$T\to T'$ s.t. if atom A$\in$
T, then h(A)$\in$T$'$. R(x,y)$\Rightarrow$R(h(x),h(y))$\in$T$'$, i.e. they are
just c.m.s, minus the requirement to map output tuple to output tuple.
\end{definition}
\quad\\

\begin{definition}
Consider the e.e. d
$\overset{def}{=}\forall\bar{x}\psi(\bar{x})\to\exists\bar{y}\varphi{\bar{x},
\bar{y}}$ and a tableau T, we say that the $\mathbf{chase}$ is $applicable$ to T
if there exists a homomorphism h:$\psi\to T$ that cannot be extended to map
$\psi\cup\varphi\to T$, i.e. there does not exist a homomorphism
h$':\psi\cup\varphi\to T$, where h$'$=h on $\psi$ where the chase is applicable
the $result$ of one step of chase of T with d is the tableau
T$'\overset{def}{=}$T$\cup \psi[\bar{x}\longmapsto h(\bar{x})]$.
\end{definition}
\quad\\

\begin{example}
$d\overset{def}{=}\forall x,yR(x,y)\to \exists zS(x,z)$.\\
T=\{R(u,v)\}\\
T$\overset{d}{\to}$T$'$ where T$'=$\{R(u,v),S(u,z))\}\quad
S(x,z)[x$\longmapsto$u]=S(u,z)\\
Note: In the definition, we assume vars in d and T are disjunct (always can be
accomplished by renaming).
\end{example}
\quad\\

\begin{definition}
\textbf{chase} on CQs is defined in terms of their underlying tableau\\i.e. if
T$\to$T$'$ and Q=$<\bar{u}$,T$>$, then Q$\overset{d}{\to}$Q$'$ where
Q$'$=$<\bar{u},T'>$.
\end{definition}
\quad\\

\begin{lemma}
If the chase with d is not applicable to a (satisfiable) tableau T, then
Inst(T)$\models$d.\\
Proof. exercise.
\end{lemma}
\quad\\


\begin{lemma}
If Q$\overset{d}{\to}$Q$'$, then d$\models$Q$\equiv$Q$'$
\begin{proof}
$\models$Q$'\sqsubseteq$Q easy since T$\subseteq$T$'$, so identity is a
containment mapping Q$\to$Q$'$.\\
Now, wts. d$\models$Q$\sqsubseteq$Q$'$, i.e. for any DB instance I s.t.
d$\models$T, we have $\llbracket Q\rrbracket ^{I}\subseteq\llbracket
Q'\rrbracket ^{I}$.\\
Let d=$\forall\bar{x}\psi(\bar{x})\to\exists\bar{y}\varphi(\bar{x},\bar{y})$.
Let h:$\psi\to$T be the hom. used in chase step. Hence
T$'$=T$\cup\psi[\bar{x}\longmapsto h(\bar{x})]$. Since the output tuple is the
same in Q and Q$'$, it suffices to show that any sat. val. for T in I can be
extended to a sat. val. for T$'$.\\
Let $\beta$:T$\to$I be such a sat. val. It follows that $\beta\circ h$:
$\psi\to$I satisfies $\psi$ in I. And since d$\models$I, we can extend
$\beta\circ h$ to a sat. val. $\gamma:\psi\cup\varphi\to$I.
Now, define $\beta '$:T$'\to$I as follows:\\
\begin{equation*}
\beta '(z)=\left\{
\begin{array}{rl}
\gamma(z) & if\;z\in vars(\bar{y})\\
\beta(z) & otherwise
\end{array} \right.
\end{equation*}
So $\beta '$ extends $\beta$, remains to show that $\beta'$ satisfies T$'$ in
I.\\
Clearly, $\beta'$ satisfies the atoms that are also in T.\\
Now,suppose e.g. that R(h(x),y), where x$\in\bar{x}$ and y $\in\bar{y}$, is an
atom in T$'\to$T, e.e., in $\varphi[\bar{x}\longmapsto h(\bar{x})]$.\\
We have
$(\beta'(h(x)),\beta'(y))=(\beta(h(x)),\gamma(y))=(\gamma(x),\gamma(y))\in
R^{I}$, since $\gamma$ is a sat. val. for $\varphi$ in I.\\
Similarily for eq. atoms.
\end{proof}
\end{lemma}
\quad\\

\begin{definition}
Let Q be a CQ and D a set of e.d.s.\\
A \textbf{terminating chase sequence} of Q with D is a sequence of chase steps:
\[Q\overset{d_{1}}{\to}Q_{1}\overset{d_{2}}{\to}\ldots\overset{d_{n}}{\to}Q_{n}\]
where $ d_{1}, \ldots, d_{n}\in $ D and no chase with deps from D is applicable
to Q$_{n}$.
\end{definition}
\quad\\

\begin{theorem}
Let Q, Q$'\in$CQ and D is a set of e.d.s. Suppose exists a term chase sequence
of Q with D producing Q$_{n}$. Then
\[D\models Q\sqsubseteq Q'\;iff\;Q_{n}\sqsubseteq Q'\]
\end{theorem}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 2/05/10}{Zhongxian}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{theorem}
 Let $Q$, $Q'$ be $CQ$s and $D$ a set of $ed$s. Suppose $Q_n$ is the result of
a terminating chase sequence from $Q$ with $D$. Then: 
$$ D \models Q \sqsubseteq Q' \textrm{ }\textrm{  iff  }\textrm{ } Q_n
\sqsubseteq Q'$$
\end{theorem}
\begin{proof}\textbf{Sketch.}
``$\Leftarrow$'' is immediate, since $D \models Q \equiv Q_n$. (Lemma 59)
 
``$\Rightarrow$'': Suppose $D \models Q \sqsubseteq Q'$, let $T_n$ be the
tableau underlying $Q_n$. 

Suppose $Q_n$ is unsatisfiable, then $Q_n \sqsubseteq
X$ or CQ $X$ in particular $Q_n \sqsubseteq Q'$. 

Assume $Q = <\bar{u},T>, Q' = <\bar{u'},T'>, Q_n = <\bar{u_n},T_n>$. Now
suppose $Q_n$ is satisfiable. Assuming $Q,Q',Q_n$ do not contain equality
atoms. Let $I_n = Inst(T_n)$. According to Lemma 58, we have $ \bar{u} \in
\llbracket Q \rrbracket^{I_n}$, also $I_n \models D$, Since $D \models Q
\sqsubseteq Q', \bar{u} \in \llbracket Q' \rrbracket^{I_n}$. Then there exists
a satisfiable valuation $\beta: Q' \to dom(I_n)$, such that $\beta(\bar{u'}) =
\bar{u}$. It's easy to see that this yields the required containment mapping
$h: Q' \rightarrow Q_n$. 
\end{proof}

\emph{Essential Question:} When does the Chase terminate ?

Example: $d_1 \stackrel{def}{=} \forall x,y, R(x,y) \rightarrow \exists z
S(z,y)$   

$d_2 \stackrel{def}{=} \forall x,y, S(x,y) \rightarrow
\exists z R(z,y) $.

Consider a tableau $T = \{R(u_0,u_1)\}$. \\
\hspace{80 pt} \underline{Chase Step} \hspace{50 pt} \underline{\hspace{30 pt}
yields \hspace{30 pt}} \\
\hspace{100 pt}    $d_1$  \hspace{93 pt}  $S(u_2,u_1)$ \\
\hspace{100 pt}    $d_2$  \hspace{93 pt}  $R(u_1,u_3)$ \\
\hspace{100 pt}    $d_1$  \hspace{93 pt}  $S(u_3,u_4)$ \\
\hspace{100 pt}    $d_2$  \hspace{93 pt}  $R(u_3,u_5)$ \\
\hspace{100 pt}    ...  \hspace{103 pt}  ... \\

There is just one chase sequence (up to variable renaming, and it is infinite.

$d_3 \stackrel{def}{=} \forall x,y, R(x,y) \rightarrow x=y$   

$d_4 \stackrel{def}{=} \forall x,y, S(x,y) \rightarrow x=y$ \\

So, we have $R\{u_0,u_1\} S\{\} \stackrel{d_3}{\rightarrow} R\{u_0,u_0\} S\{\}
\stackrel{d_1}{\rightarrow} R\{u_0,u_0\} S\{u_1,u_0\}
\stackrel{d_4}{\rightarrow} R\{u_0,u_0\} S\{u_0,u_0\}$. For given $D$ and $T$,
we can have finite and infinite chases sequences. 

\begin{theorem}
 (Dentsch, Nash, Remel, 2008) Termination of the chase is undecidable. 
\end{theorem}

\begin{theorem}
 $D \models Q \sqsubseteq Q'$ is undecidable.
\end{theorem}

\begin{proof} \textbf{Sketch.}
 $Q_E \sqsubseteq Q'$ is $ccd$, hence an $ed$. So $D \models d $ is undecidable.
\end{proof}

Several variants of the (classical) chase proposed, to make it ``more
deterministic''. \\
Culmilating in the \underline{core Chase}(PNR 2008).\\
Idea: at each step of the classical chase, several (but finitely many)
dependences apply, each with several (but finitely many) associated tableau
homomophisms. At each step of the core chases, do two things: 
\begin{itemize}
 \item ``Fire'' \underline{all} applicable dependences in parallel, produce
$T'$. 

 \item Minimize $T'$ by computing its core. 

\end{itemize}

\begin{theorem} 
  
 \begin{itemize} 
  
  \item Preserve equivalence with respect to $D$.
  \item Deterministic(up to renaming variables).
  \item If there is any terminating classical chase sequence from $T$ with $D$,
then the core chase with $T$ and $D$ terminates. 
 \end{itemize}

\end{theorem}
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 2/8/10}{Vu}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{definition}
Given $D$, focus on tuple-generating dependencies in $D$ (t.g.d.s).\\
\[
\forall \xbar \varphi(\xbar) \implies \exists \ybar \psi(\xbar,\ybar)
\]
where $\psi$ contains only predicate atoms (no equality) (contrast to equality-generating dependencies (e.g.d.s)).\\
\[
\forall \xbar \varphi(\xbar) \implies x_i = x_j
\]
\end{definition}
{\bf Fact}: any e.d. can be expressed using just t.g.d.s and e.g.d.s.\\
\begin{definition}
Given set $D$ of t.g.d.s, build the chase-flow graph for $D$ as follows:
\begin{itemize}
\item vertices: for each predicate symbol of arity $k$ in schema, $G$ has a vertex $(P,i)$ for each $1 \le i \le k$.
\item for each t.g.d. and each variable $x$ that occurs in position $i$ in an R-atom of the premise of the t.g.d. and in position $j$ in an S-atom in the conclusion, $G$ has an edge from $(R,i)$ to $(S,j)$.
\item for each t.g.d. and each vertex $x$ that occurs in position $i$ in a R-atom of the premise and $x$ occurs somewhere in the conclusion and each variable $y$ that is existentially quantified in a S-atom at position $j$ of the conclusion, $G$ has an edge from $(R,i)$ to $(S,j)$ labeled with a *.
\end{itemize}
\end{definition} 
We say $D$ is weakly acylic iff its chase-flow graph has no cycle through an edge, marked *.\\
{\bf Note:} t.g.d. $\forall \xbar \underbrace{\varphi(\xbar)}_{\text{premise}} \implies \exists \ybar \underbrace{\psi(\xbar, \ybar)}_{\text{conclusion}}$\\
\begin{theorem}(Deutsch, Popa, Tannen 2003)
If $D$ is weakly acylic, then chasing $Q$ with $D$ always terminates (in a number of steps polynomial in $|Q|$ and $|D|$, assuming fixed schema).
\end{theorem}
\begin{definition}(Minimality under constraints)
Given set $D$ of e.d.s, a CQ $Q$ is $D$-minimal if there are no CQs $S_1$, $S_2$ where
\begin{itemize}
\item $S_1$ is obtained from $Q$ by replacing zero or more variables in $Q$ with other variables of $Q$
\item $S_2$ is a strict subquery of $S_1$ such that $D \models Q \equiv S_1 \equiv S_2$.
\end{itemize}
\end{definition}
For example,
\begin{align}
&Q(x,y):-R(z,x),R(z,y)\nonumber\\
&\forall x,y,z R(x,y) \land R(x,z) \implies y=z \nonumber
\end{align}
\begin{align}
Q(x,y) &:- R(z,x), R(z,y), x = y \nonumber\\
S_1(x,y) &:= R(z,x), R(z,x) \nonumber\\
S_2(x,y) &:= R(z,x) \nonumber
\end{align}
\begin{definition}(Chase \& backchase - Deutsch, Popa, Tannen 2003)
Given a CQ $Q$, set $D$ of e.d.s, assume $D$ is weakly acyclic.
\begin{enumerate}
\item (Chase): chase $Q$ with $D$, producing $\mathbb{U}$, the universal plan for $Q$.
\item (Backchase):\\ 
for each subquery $Q_s$ of $\mathbb{U}$\\
if $Q_s$ is $D$-minimal, and $Q_s D\implies Q_s \equiv \mathbb{U}$ (tested by chasing) then output $Q_s$
\end{enumerate}
\end{definition}
\begin{theorem}
Chase and backchase output precisely the $D$-minimal rewriting of $Q$.\\
(If $D \models Q' \equiv Q$ and $Q'$ is $D$-minimal, then $Q'$ is isomorphic to a subquery of $\mathbb{U}$)
\end{theorem}
Chase \& backchase is interesting because it is a unifying and general technique for:
\begin{itemize}
\item optimizing queries using views
\item answering queries using views
\end{itemize}
Given a CQ $Q$ and a set of views definitions $\calV$, find an efficient plan to answer $Q$ (possibly using views in $\calV$).
For example:
\begin{align}
Q(x,y) &:- R(x,u), R(u,v), R(y,y) \nonumber\\
V(x,y) &:- R(x,u), R(u,y) \nonumber\\
\implies Q'(x,y) &:- V(x,z), R(z,y) \nonumber
\end{align}
Encode using e.d.s
\begin{align}
\forall x,y,z R(x,z) \land R(z,y) \implies V(x,y) \nonumber\\
\forall x,y V(x,y) \implies \exists z R(x,z) \land R(z,y) \nonumber
\end{align}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 2/10/10}{Vu}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Example for chase \& backchase
\[
Q(x,y) :- R(x,y), R(x,z), S(y,u), S(z,u)
\]
\begin{equation*} 
D \left\{
\begin{array}{ll} 
d_1 \defeq \forall u,v,w  R(u,v) \land R(u,w) \implies v = w & \text{(e.g.d.)}\\
d_2 \defeq \forall u,v  R(u,v) \implies \exists w S(v,w) & \text{(t.g.d.)}
\end{array} \right.
\end{equation*}
Step 1: Chase $Q$ with $D$
\[
Q \stackrel{d_1}{\longrightarrow} Q_1: Q_1(x,y):- R(x,y), R(x,z),S(y,u),S(z,u),y = z
\]
i.e., 
\[
Q_1(x,y):-R(x,y), S(y,u)
\]
And the chase terminates with $Q_1$ as the universal plan.\\
Step 2: Backchase
\[
Q_s(x,y):-R(x,y)
\]
\[
Q_s \stackrel{d_2}{\longrightarrow} Q': Q'(x,y):- R(x,y), S(y,w)
\]
and the chase terminates.\\
Since $Q' \cong Q_1$, hence $D \models Q_s \equiv Q_1 \equiv Q$.\\
Output $Q_s$ as the minimal rewriting.\\
Another application for chase \& backchase: answering queries with views.\\
Two flavors: Given CQ $Q$ and CQ view $\calV$
\begin{enumerate}
\item Find rewriting of $Q$ using any combination of source and view predicates - for performance.
\item Find rewriting of $Q$ using only view predicates
\end{enumerate}
Eg, when source database is remote/unavailable/non-existant.\\
Chase \& backchase applies to both scenerios.\\
Given a CQ view
\[
V(\ubar):- \varphi (\ubar, \vbar)
\]
we can model the view using a pair of t.g.d.s.
\begin{align}
\forall \ubar,\vbar \varphi(\ubar,\vbar) \implies V(\ubar) \nonumber\\
\forall \ubar V(\ubar) \implies \exists \vbar \varphi(\ubar,\vbar) \nonumber
\end{align}
For example,
\begin{align}
Q(x,y) &:- R(x,u), R(u,v), R(v,y) \nonumber\\
V(u,v)&:-R(u,w), R(w,v) \nonumber
\end{align}
\begin{equation*} 
D \left\{
\begin{array}{ll} 
d_1 \defeq \forall u,v,w  R(u,w) \land R(w,v) \implies V(u,v)\\
d_2 \defeq \forall u,v  V(u,v) \implies \exists w R(u,w) \land R(w,v)
\end{array} \right.
\end{equation*}
Step 1: chase $Q$ with $D$
\begin{align}
Q  &\stackrel{d_1}{\longrightarrow} Q_1: Q_1(x,y) :- R(x,u), R(u,v), R(v,y),V(x,v) \nonumber\\
Q_1 &\stackrel{d_1}{\longrightarrow} Q_2: Q_2(x,y) :- R(x,u), R(u,v), R(v,y),V(x,v),V(u,y) \nonumber
\end{align}
and the chase terminates with $Q_2$ as the universal plan.\\
Step 2: backchase: out put of backchase step will be
\begin{align}
Q'(x,y) &:-R(x,u), V(u,y) \nonumber\\
Q''(x,y)&:- V(x,v), R(v,y) \nonumber\\
Q'''(x,y) &:- R(x,u), R(u,v), R(v,y) \nonumber
\end{align}
Backchase with 
\begin{align}
Q'  &\stackrel{d_2}{\longrightarrow} Q'_1: Q'_1(x,y) :- R(x,u), V(u,y),R(u,w),R(w,y) \nonumber\\
Q'_1 &\stackrel{d_1}{\longrightarrow} Q'_2: Q_2(x,y) :- R(x,u),V(u,y),R(u,w),R(w,y),V(x,w) \nonumber
\end{align}
Note that $Q'_2 \cong Q_2$, so $D \models Q' \equiv Q_2 \equiv Q$.\\
On the other hand, consider 
\[
Q_{bad}(x,y):- R(x,u), R(v,y)
\]
Backchase terminates after 0 steps: $Q_{bad} \not\cong Q_2$, so $D \not \models Q_{bad} \equiv Q_2$\\
Consider the query $TC$ which computes transitive closure of a graph $G$
\[
TC(G) \defeq \{ (x,y) | \text{ there exists a path from } x \text{ to } y \text{ in } G \}
\]
\begin{theorem}(Compactness)
A theory $T$ (i.e., a set - possibly infinite set - of FO sentences) is consistent (i.e., $\exists A$ s.t. $A \models T$) iff every finite subset of $T$ is consistent.\\
(A corollary of God\"{e}l's Completeness Theorem)
\end{theorem}
Connectivity query:
\[
conn(G) \defeq \text{true iff } G \text{ is connected}
\]
Ex: if $TC$ is FO-expressible, then so is connectivity.\\
\begin{proposition}
Connectivity over arbitrary (e.g., possibly infinite) graphs is not FO-expressible.
\end{proposition}
\begin{proof}
Assume towards a contradiction that it is expressible, via FO sentence $\Phi$.\\
Vocabulary $\sigma = \{E, c_1, c_2\}$. Now for every $n$, let $\psi_n$ be the FO sentence.
\[
\psi_n \defeq \neg (\exists x_1,...,x_n (E(c_1,x_1) \land E(x_1,x_2) \land ... \land E(x_n,c_2)))
\]
i.e., $\psi_n$ says there is no path of length $n$ from $c_1$ to $c_2$.\\
Let $T$ be the theory. 
\[
T = \{ \psi_n |n >0 \} \cup \{ \neg (c_1 = c_2) , \neg E(c_1,c_2)\} \cup \Phi
\]
Claim: $T$ is consistent.\\
By compactness, we need to show every finite $T' \subseteq T$ is consistent. Let $N$ be s.t. forall $\psi_n \in T', n<N$. Then a connected graph in which the shortest path from $c_1$ to $c_2$ has length $N+1$ is a model of $T'$.\\
Since $C$ is consistent, it has a model, say $G \models T$. $G$ is connected, but has no path of any length from $c_1$ to $c_2$ - a contradiction.
\end{proof}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 2/12/10}{Thanh}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\underline{Compactness:} Any theory T is consistent if every finite subset of T is consistent.
\begin{proposition}
Compactness fails for finite models, i.e, there exists a theory T which is unsatisfiable by any finite model, but every finite subset of T is finitely satisfiable.
\end{proposition}
\underline{Proof:} Let $T = \{\lambda _n| n > 0\}$ where $\lambda _n$ is defined (over vocabulary $\sigma = \Phi$) \\
\hspace{150 pt}{$\lambda_n = \exists x_1,x_2,...,x_n \wedge x_i \neq x_j $}\\
\hspace{230 pt}$i\neq j$\\
$\lambda _n$ says the universe has at least n distinct finite elements. Clearly, T is not finitely satisfiable. On the other hand, any finite subset $T' \subset T$ if stisfiable by a model with universe $>$ N where N is $max\{i|\lambda _i \in T'\}$ \\[12 pt]
\underline{Ehrenfeucht-Fraisse Games (EF-games):}\newline 
\newline Two players, the spoiler and the duplicator. Given two finite structures, $\frA$ and $\mathfrak{B}$. In each round,\\
1. Spoiler picks one of $\frA$ or $\mathfrak{B}$ and an element of that structure.\\
2. Duplicator picks an element of the other structure. \\
After n rounds, let $(a_1,a_2,...,a_n)$ and $(b_1,b_2,...,b_n)$ be the elements chosen so far. We say that $(\bar{a},\bar{b})$ is minimizing position for the duplicator if $(\bar{a},\bar{c}^A)$, $(\bar{b},\bar{c}^B)$ is the partial isomomorphism between A and B ($\bar{c}$ is the constansts in $\sigma$).  
We say that the duplicator was an n-round winning statergy if the duplicator can play in a way that guarantee a winning position after n rounds (other wise the spoiler has an n-round winning startergy). \\
If the duplicator has an n-round winning statergy we wrote $A \equiv_n B$ (Note that $A \equiv_n B \Rightarrow A\equiv _k B$ for $k \le n$ ).

\begin{definition}
Let A,B be finite $\sigma$-structures, $\sigma$ is relational and $\bar{a}=(a_1,a_2,...,a_n)$ and $\bar{b}=(b_1,b_2,...,b_n)$ two tuples over elements of A and B, repectively, then $(\bar{a},\bar{b})$ defines a partial isomomorphism between A and B if following holds:\\
\quad * For every $i,j \le n$ $a_i = a_j$ if $b_i = b_j$\\
\quad * For every constant symbol $c\in \sigma$ and every $i \le n$, $a_i = c^ A$ if $b_i = c^B$\\
\quad * For every k-arry predicate symbol $P\in \sigma$ and every sequence $(i_1,i_2,...,i_k)$ of (not nesscessarily distinct) number from [1,n] \\
$(a_{i_1},a_{i_2},...,a_{i_k}) \in P^A$ iff $(b_{i_1},...,b_{i_k}) \in P^B$ 
	
\end{definition} 

\begin{definition}
The quantifier rank of a FO sentence $\phi$ is the depth of quantifier nesting $qr(\varphi)$ \\
\quad * If $\varphi$ is atomic, $qr(\varphi)=0$\\
\quad * $qr(\phi \wedge \psi) = qr (\varphi \vee \psi) = max(qr(\varphi), qr(\vee))$  \\
\quad * $qr(\neg \varphi) = qr(\varphi)$\\
\quad * $qr(\forall x \varphi) = qr(\exists x \varphi) = qr(\varphi) +1 $ \\
\begin{example}
$qr(\exists x (\forall y R(x))) \vee \exists z P(z) = 2$\\
\end{example}
Denote FO[k] is the fragment of FO sentences ${\varphi \in FO| qr(\varphi) \le k }$ \\
\begin{theorem}
Let $\mathfrak{A}, \mathfrak{B}$ be finite $\sigma$-structures, then the following are equivalent: \\
1. A and B agree on FO[k], i.e. for any $\varphi \in FO[k]$, $A \models \varphi$ iff $B \models \varphi$\\
2. $A \equiv_k B$ \\
A property P of finite $\sigma$-structures is not expressible in FO iff for any $k \in \mathbb{N}$, then exist two finite $\sigma$-structures $A_k$ and $B_k$ such that \\
\quad * $A_k \equiv _k B$ \\
\quad * $A_k$ has property P, but $B_k$ does not. \\
\underline{Proof:} Supposes $\varphi$ expresses P, k = qr($\varphi$) and suppose have $A_k,B_k$ such that $A_k$ has P, $B_k$ does not have P but $A_k \equiv _k B_k$. Then, $ \varphi \models A_k$ iff $\varphi \models B_k$, a contradiction.\\

\begin{example}
games on sets vocabulary $\sigma = \Phi$. Supposes $|A|, |B| \geq n$. Then, $A \equiv_n B$.
\end{example}
\begin{example}
games on linear order. Let $\sigma = \{\lambda\}$ interpreted as a linear oreder. Supposes $L_1, L_2$ are two linear orders of size $\geq$ n. It is not true that $L_1 \equiv _n L_2$ even for n =2. \\
Let $L_1 = a < b < c$ \\
Let $L_2 = u < v $
First round, spoiler picks $L_1$ and b. Duplicator picks u or v. 
Second round, if u picked, spoiler picks a ( if v then c).\\
\begin{example}
If $L_1$ and $L_2$ are linear orders of size at least $2^k$, then $L_1 \equiv_k L_2$ \\
for: EVEN is not FO-expressible even if $\sigma =\{<\}$ and < is a linear order where $EVEN^A = true$ if $|A|$ has even number of elements. \\
\underline{Proof:} Supposes $\varphi \in FO$ expresses EVEN. Let $k = qr(\varphi)$.\\
\hspace{30 pt}Pick $L_1$ is linear order of size $2^k$ \\
\hspace{30 pt}Pick $L_2$ is linear order of size $2^{k+1}$ \\
\hspace{30 pt}Then, $L_1 \equiv_k L_2$, a contradiction.
\end{example}
\end{example}

 
\end{theorem}

\end{definition}  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 2/15/10}{Mingmin}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{theorem}
If L$_{1}$ and L$_{2}$ are linear orders (with $\sigma$=\{$<$\}) of size $\geq 2^{k}\;then\;L_{1}\equiv _{k}L_{2}$.
\end{theorem}

\begin{proof}
$L_{1}=\{1, \ldots, n\},\;L_{2}=\{1, \ldots, m\}$ where $n,m\geq 2^{k}$.\\
Define $distance$ between x and y d(x,y)$\overset{def}{=}|$x-y$|$.\\
Assume the vocabulary $\sigma$ has constants $\underline{min}$ and $\underline{max}$.\\
We claim that the dup. has a strategy  s.t. after round i:\
\begin{description}
\item[*] Let $\bar{a}=(a_{-1},\;a_{0},\;a_{1},\ldots,a_{i})$ and $\bar{b}=(b_{-1},\;b_{0},\;b_{1},\ldots,b_{i})$ be moves played in $L_{1},L_{2}$ respectively.\\
$a_{-1}=min^{L_{1}}=1,\;a_{0}=max^{L_{1}}=n;b_{-1}=min^{L_{2}}=1,\;b_{0}=max^{L_{2}}=m$.\\
\item[*] Then for $-1\leq j,l\leq i$\\
IH$\lbrace$\\
1. if $d(a_{j},a_{l})<2^{k-i}$, then $d(a_{j},a_{l})=d(b_{j},b_{l})$.\\
2. if $d(a_{j},a_{l})\geq 2^{k-i}$, then $d(b_{j},b_{l})\geq 2^{k-l}$.\\
3. $a_{j}\leq a_{l}\iff b_{j}\leq b_{l}\;\Longrightarrow$ the partial isomorphism condition.\\

\end{description}
$\underline{Proof\;of\;claim}$: By induction on L:
\begin{quote}
$\underline{base\;case}$: (i=0) immeiate since $d(a_{-1},a_{0}),d(b_{-1},b_{0})\geq 2^{k}$ by assumption.\\
$\underline{induction\;case}$: Sps spoiler picks $L_{1}$ in round i+1, plays $a_{i+1}$ (case for $L_{2}$ is symmetric)\\
$\underline{case\;1}$: $a_{i+1}=a_{j}$ for some j$\leq$i$\Rightarrow$dup. responds with $b_{j}$.\\
$\underline{case\;2}$: spoiler plays new point $a_{i+1}$, falling into some interval, $a_{j}<a_{i+1}<a_{l}$, s.t. no other moves previously played in the same interval. By IH(3), the corresponding interval ($b_{j},b_{l}$) also does not contain any previously played elements.\\ 
\end{quote}
There are two (sub)cases:
\begin{description}
\item[(a)] $d(a_{j},a_{l})<2^{k-i}$, dup. responds with the element $b_{j+1}$ in interval ($b_{j},b_{l}$) s.t. $d(b_{j},b_{i+1})=d(a_{j},a_{i+1})$.
\item[(b)] $d(a_{j},a_{l})\geq 2^{k-i}$. There are 3 cases:
\begin{description}
\item[*] $d(a_{j},a_{i+1})<2^{k-(i+1)}$ and $d(b_{i+1},b_{l})\geq 2^{k-(i+1)}$.\\
\item[*] $d(a_{i+1},a_{l})<2^{k-(i+1)}$ and $d(a_{j},a_{i+1})\geq 2^{k-(i+1)}$. Similar to first case.\\
\item[*] $d(a_{j},a_{i+1}),d(a_{i+1},a_{l}\geq 2^{k-(i+1)})$.\\
\end{description}
\end{description}
\end{proof}

\begin{corollary}
Graph connectivity (and also transitive closure) is not FO-expressible even over graphs with a linearly-ordered domain.
\end{corollary}
\begin{proof}
Define a $\underline{successor}$ relation succ$(x,y)\equiv x<y\land\forall z(z\leq x\lor z\geq y)$.\\
Define $\gamma (x,y)$ as the FO formula s.t. $\gamma (x,y)$ holds iff one of the following is true:
\begin{description}
\item[*] y is the succ. of the succ of x.\\
\item[*] x is the predecessor of the last element and y is the first element.\\
\item[*] x is the last element, and y is the succ. of the first element.\\
\end{description}
We can show that when underlying order is \underline{even}, $\gamma$-graph is disconnected; \underline{odd}, $\gamma$-graph is connected.\\
Now, sps connectivity is FO-expressible via sentence $\Phi$ over vocab. $\sigma=\{<,E\}$.\\
Take $\Phi$ replace every occurrence of E by $\gamma$\\
Call the result $\Psi$. But $\Psi$ expresses EVEN, a contradiction.
\end{proof}

\textbf{Datalog}:
\begin{quote}
T(x,y):- E(x,y)
T(x,y):- E(x,z),T(z,y)
\end{quote}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 2/17/10 and Monday, 2/20/10}{Bertram}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\paragraph{Reading.} Sections 12.1--12.3, Foundations of Databases,
Abiteboul, Hull, Vianu, 1995 (e-version available).


\paragraph{Datalog Syntax.}


We already know conjunctive queries which can be expressed as rules
$r$ of the form
\begin{itemize}
\item $A_0(\bar x_0) \la  A_1(\bar x_1), \dots, A_n(\bar x_n)$ 
\end{itemize}
Here each $A_i(\bar x_i)$ is a logic \emph{atom}, with $A_i$ a $k$-ary
relation symbol and $\bar x_i$ a $k$-tuple of variables or
constants. All variables occurring in the \emph{head} $A_0(\bar x_0)$
of $r$ must also occur in the \emph{body} (the rhs) of
$r$.\footnote{Later, when allowing negated literals $\neg B_i(\bar
  x_i)$ in the body, we require that all variables in a rule appear in
  some \emph{positive} literal of the body.  Such rules are called
  \emph{range-restricted}; this implies they are domain-independent.}

A finite set of rules $P$ of the above form is called a \emph{Datalog
  program}. The relations occurring only in the body of rules of $P$
are called \emph{edb}-relations; those that occur in the head (and
possibly in the body) are called \emph{idb}-relations. Thus, we can
associate with a Datalog program $P$ a schema $\sch(P)$ as follows:
\begin{itemize}
\item $\sch(P) = edb(P) \cup idb(P)$. 
\end{itemize}

As we shall see, at Datalog program $P$ can be given a query
semantics, i.e., it can be viewed as a mapping from instances of
$edb(P)$ to instances of $idb(P)$.

\paragraph{Example.} Consider the following Datalog program $P_{tc}$
with edb-relation $G$ and idb-relation $T$:
\begin{displaymath}
  \begin{array}{l}
T(x,y) \la G(x,y).\\
T(x,y) \la G(x,z), T(z,y).
  \end{array}
\end{displaymath}
It maps instances of the edge relation $G/2$ of a graph to instances
of another relation $T/2$, the transitive closure of $G$.


\paragraph{Model-theoretic Semantics of Datalog.}  We can view a
Datalog program $P$ as a set of first-order logic formulas $\Phi_P$;
e.g. for $P_{tc}$ we get\footnote{The arrow $A \leftarrow B$ is just a
  shorthand for $\neg B \lor A$.  The $\forall z$ in the second
  formula can be moved inside as follows: $\forall x,y: T(x,y)
  \leftarrow \exists z: G(x,z) \land T(z,y)$.}  \begin{itemize} \item
  $\Phi_{P_{tc}} = \{ \forall x, y: T(x,y) \leftarrow G(x,y), ~~
  \forall x,y,z: T(x,y) \leftarrow G(x,z) \land T(z,y)
  \}$.  \end{itemize}



We can ask ourselves: what are the \emph{models} of $\Phi_{P_{tc}}$,
given a particular input instance of $G/2$?  We can focus our
attention on \emph{Herbrand Models}.

\paragraph{Herbrand Models.} 
A (Herbrand) model $M$ of a set of (closed) formulas $\Phi$ is a
(Herbrand) interpretation that satisfies all sentences in $\Phi$,
denoted $M\models \Phi$.  A \emph{Herbrand interpretation} is one
where symbols are interpreted ``syntactically'', i.e., they stand for
themselves (in general, an interpretation maps symbols from the
syntactic domain to a semantic domain). Thus, a Herbrand
interpretation is built over a \emph{Herbrand universe}, consisting of
constants (and if function symbols are allowed: \emph{ground terms},
i.e., function terms containing only constants as arguments, but no
variables).  Using constants (and other ground terms), the
\emph{Herbrand base} \Bp\ of ground atoms is built.



To simplify the presentation, we sometimes denote by $P$ a Datalog
program together with a set of (ground) facts, i.e., and instance of
$edb(P)$.  For example we might have $P=$


\begin{displaymath}
  \begin{array}{l}
T(x,y) \la G(x,y).\\
T(x,y) \la G(x,z), T(z,y). \medskip\\
G(a,b).\\
G(b,c).\\
G(c,d).\\
 \end{array}
\end{displaymath}

Here we have as set of constants $\constants = \{a,b,c,d\}$ and
relation symbols $\relations = \{G/2, T/2 \}$.  The Herbrand base \Bp\
of $P$ consists of all ground atoms that can be built using
\constants\ and \relations; here: $\{ G(a,a), G(a,b),\dots G(d,d),
T(a,a),\dots, T(d,d)\}$.  Since we consider only finite database
instances, \Bp\ will be finite as well.\footnote{Exercise:What is the
  size of \Bp? Is it polynomially bounded by the size of the given
  edb?}



For Herbrand interpretations, we simply view constants symbols as
distinct domain elements, and any subset $I\subseteq\Bp$ can be viewed
as an interpretation assigning \true\ to all ground atoms $A(\bar c)
\in I$ and \false\ to all $A(\bar c) \notin I$ .
% \footnote{In general, a logic interpretation maps constants and
%   relation symbols to domain elements and (finite) relations over
%   these, respectively.}


\paragraph{Immediate Consequence Operator \Tp.} Given a Datalog
program (with $edb$) $P$ and an interpretation $I\subseteq\Bp$, we can
compute the set of \emph{immediate consequences}
\begin{displaymath}
  \Tp(I) := \{ head(r) \mid I \models body(r), r\in ground(P) \}
\end{displaymath}
Here, $ground(P)$ is the finite set of ground rules that can be
obtained from $P$ by substituting variables in all possible ways by
constants from \constants. So given $I$, we find the ground rules
whose body is satisfied, then derive the ground atom in the head of
the rule.



\paragraph{Fact:}
An interpretation $I$ is a model of $P$, denoted $I\models P$ iff
$T_p(I)\subseteq I$. To see this, note that if there was a head atom
$A(\bar c)$ derived by \Tp\ but not in $I$, then the corresponding
ground rule would be violated (the body of that rule must be true for
the head to be derived; yet the head is not in $I$.)

\paragraph{Fact:} \Bp\ is a (Herbrand) model of $P$ (or of $\Phi_P$ to
be more precise).  To see this, note that $\Bp$ makes true all heads
of rules of $P$, for all possible ground instances of those
rules.\footnote{$G/2$ are viewed as implications of the form $G(\dots)
  \leftarrow true$.}


Clearly \Bp\ is usually not the desired model. In the example, it
contains ``unsupported'' atoms such as $G(a,a)$. We can also ``make
up'' models by adding ``self-supporting'' atoms to $T/2$, without the
underlying graph $G/2$ necessarily having a corresponding transitive
path.

% \paragraph{Minimal (Herbrand) Model.} Given a Datalog program $P$
% (that includes also input instance of edb relations), we only need
% to consider Herbrand interpretations $I\subseteq\Bp$.

\paragraph{Minimal Model \Mp.} Consider a Datalog program $P$ (with
$edb$). Let $I\subseteq\Bp$ be any Herbrand interpretation of $P$. We
define \Mp\ as the intersection of all models of $P$, i.e., $$\Mp =
\bigcap_{I\subseteq\Bp} \{~A \in I ~\mid~ I\models P ~\}$$

\noindent Note that \Mp\ is unique and minimal (no proper subset of
\Mp\ is a model of $P$).

\paragraph{Fixpoint Semantics.}
Computing the intersection of a large (even if finite) set of
interpretations is highly inefficient.  Instead we can compute the
semantics of $P$ bottom up using \Tp.
%
First, note that \Tp\ is \emph{monotone}, i.e., $I\subseteq J$ implies
$\Tp(I) \subseteq \Tp(J)$.\footnote{This is no longer the case, if
  Datalog rules have negative literals $\neg B$ in the body.}  With
this, and the fact that $\Bp$ is finite, we can see that the following
iteration reaches a fixpoint:
\begin{itemize}
\item $\Tp^0 = \emptyset$,
\item $\Tp^{n+1} = \Tp(\Tp^n)$.
\end{itemize}
Note: In the base step $\Tp^0$, we already derive all facts (=
edb-instance).


\paragraph{Theorem.}  The fixpoint $\Tp^\omega$ coincides with \Mp.


\paragraph{Proof-theoretic Semantics.}
The fixpoint semantics can be implemented in an obvious way in a
bottom-up, set-oriented way (example on blackboard).

There is an alternative, top-down and tuple-oriented way.  For that,
we view the rules of a Datalog program $P$ as inference rules. We
start with a fact that we want to derive, e.g. $T(a,d)$ in the
example.  We then apply the rules ``backwards'' to obtain new proof
obligations. This yields a proof-tree with leaves that are either
successful (we can fulfill our proof obligations using the given
($edb$) facts, or that fail (the fact we need to assume is not in the
$edb$). For details see [AHV95, Section 12.4].


\subsection*{Adding Negation}

Datalog can express certain recursive queries, but not negation.
First-order logic can express negation but not recursion. The
combination of ``recursion through negation'' can be problematic (and
in fact has fueled the research areas of KR and Nonmonotonic Reasoning
and Logic Programming in the 1990).  The problems can be illustrated
with ground (propositional) rules.

\paragraph{Syntax of \datalogneg.} If we allow negated literals $\neg
A(\bar x)$ in the body (not in the head!) of rules, we obtain
\datalogneg. We also require that every variable in a \datalogneg\
rule appears positively in the body, i.e., we assume rules are
\emph{range-restricted}.

Note that for \datalogneg, \Tp\ is no longer monotone!

To illustrate the problems with negation, we can focus on
propositional cases (after all, we can replace a Datalog$^{(\neg)}$
program with $edb$ by its finite ground instantiated version.




\paragraph{Multiple Minimal Models (Stratified Case).}

Consider the program $P_1 = \{ p \la q,~ q\la \neg r\}$.  It has two
different minimal models $M_1 = \{p,q\}$ and $M_2 = \{r\}$.  For $M_1$
we can assume $r$ to be false (there is no fact or rule that could
derive $r$), hence $q$ and thus $p$ must be true. This models seems to
be the ``right'' (intended) one. On the other hand, $M_2$ is a minimal
model, too: If $r$ is true, then the body of the second rule is false,
so we do not have to derive $q$ (and thus we don't need to derive $p$
either). So somehow we want to say that $M_1$ is preferred over
$M_2$. The notion of stratification will do the job (see below/later).


\paragraph{Multiple Minimal Models (Non-Stratified Case).}
Now consider the following program $P_2= \{ p \la \neg q,~ q\la \neg
p\}$.  Again we have two minimal models; here: $M_1 =\{p\}$ and $M_2
=\{q\}$. But now we cannot say that one model is ``better'' than the
other (for they are complete symmetrical).  The STABLE semantics,
assigns to any program a set of models. The stratified semantics
excludes such programs from consideration (because the program
contains a negative cylic dependency). The well-founded model
semantics (WFM) assigns a third thruth value to atoms that are
``ambiguous''. Here WFM would make $p$ and $q$ undefined.

\paragraph{Stratified \datalogneg.}
A \datalogneg\ program $P$ is called \emph{stratified} if its
dependency graph $G$ does not contain negative cycles. The nodes of
the dependency graph are the relation symbols of $P$. There is an edge
from $R$ to $S$ in $G$ if there is a rule in $P$ such that $R$ occurs
in the body and $S$ occurs in the head. The edge is called
\emph{negative} and labled with ``$\neg$'' if $R$ occurs under a
negation symbol in the body.

It is easy to see that if a program $P$ is stratified, then we can
create a partition of rules $P = P_1\cup\cdots\cup P_n$, such that the
different ``layers'' of rules $P_i$ can be evaluated one after
another. That is, the relations defined by rules in $P_i$ only depend
on relations defined by $P_j$ with $j\leq i$, and only depend
negatively on relations $p_j$ with $j<i$ (i.e., from lower strata).

\paragraph{Beyond Stratified Datalog.}
There are certain queries that involve negation and recursion that
cannot be expressed in stratified \datalogneg\ (or S-Datalog for
short). There are several extensions that are strictly more
expressive:

\textbf{Inflationary Datalog} (I-Datalog) corresponds to IFP
(inflationary fixpoint logic), which itself is equivalent ot LFP. The
idea of I-Datalog is to allow negation in the head of rules and
evaluate this rules similar to the \Tp\ iteration, but (somewhat
artificially) keeping all previously derived facts $\Tp'(I) = \Tp(I)
\cup I$.

\textbf{Non-inflationary Datalog} (P-Datalog) corresponds to PFP
(partial fixpoint logic). Again we allow negation in the head (which
is interpreted as ``deletion'') but w allow the fixpoint iteration to
diverge (we use \Tp\ instead of $\Tp'$).

I-Datalog and P-Datalog have been considered by the database and
finite model theory community (as syntactic variants of IFP and PFP)
to study the expressive power and computational complexity of query
languages. For KR and NMR purposes, however, they have not gained
traction. Instead, the \textbf{well-founded Datalog} (WF-Datalog)
semantics and the \textbf{stable semantics} (sets of stable models,
ST-Datalog) have been adopted by the KR, NMR, and LP communities.

A model $M\models P$ is called \emph{stable} if after replacing in
$ground(P)$ all atoms in the rule bodies with their truth value in
$M$, the resulting reduced program (now a program without negation!)
has as its unique minimal model exactly $M$. A program can have zero
or more stable models (e.g. $p\la\neg p$ has not stable models,
whereas $P_2$ above has the indicated two stable models).

The WF-Datalog semantics, on the other hand, is a 3-valued model. It
can be computed iteratively by an alternating fixpoint
construction. The basic idea is to keep track of two subsets of \Bp,
i.e., those atoms that are definitely true and those that are
definitely false (or equivalently those that are true or undefined).
One can then define a fixpoint operator $\Gamma^2_P$ whose lfp and
complement of gfp yield the definitely true and definitely false
facts, respectively; all remaining atoms are assigned the truth-value
\emph{undefined}.

The well-founded semantics yields the intended meaning of
non-stratified programs such as
\begin{displaymath}
  win(X) \la move(X,Y), \neg win(Y)
\end{displaymath}

In fact, it can be shown that all Fixpoint queries (LFP, IFP) can be
expressed in the form of a win-move game, i.e., with a quantifier-free
FO formula for $move$ and a single recursive rule
\begin{displaymath}
  win(\bar X) \la move(\bar X, \bar Y), \neg win(\bar Y)
\end{displaymath}
and that this win-move game can even be reduced to a draw-free game
(Flum, Kubierschky, Lud\"ascher, ICDT'97).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Friday, 2/26/10}{Zhongxian}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{theorem}
 Containment/equivalence of Datalog program is undecidable. 
\begin{itemize}
 \item containment: (CGKV 1988, Shmueli 1987)
 \item equivalence: (Shmueli 1993)
\end{itemize}
\end{theorem}

\begin{theorem}
  Equivalence of Datalog and NR-Datalog (non-recursive) is decidable,
  but complete for 3-EXPTIME (Chaudhuri and Vardi, 1992)
\end{theorem}

\begin{theorem}
 Boundedness of Datalog program is undecidable. (GMSV 1993)
\end{theorem}

%%%%
%Bertram introduced datalog with negation and win lose here. 
%%%%

\begin{theorem}
 WF-Datalog$\neg$ $+$ order captures PTIME. (Vardi 1982)
\end{theorem}

\begin{theorem} (Fagin 1976)
 \begin{itemize}
  \item $\exists SO = NP$
  \item $\forall SO = CO-NP$
 \end{itemize}
\end{theorem}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Monday, 3/01/10}{Thanh}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{Large}
{\bf Bag-theoretic Sematics}
\end{Large}
%Presenter: Sarah
\begin{definition}
a bag (or multiset) is a group of annotated elements \\
\end{definition}
\begin{example}
\{(CS140,Olsson);[2],(CS235,chen);[1]\} \\
\hspace{142 pt} $\uparrow$\\
\hspace{122 pt} multiplicity
\end{example}
\begin{definition}
A sub bag $B_s$ of bag B ( written $\le _b$) where: \\ 
1) every element $b \in B_s$ is $\in B$ \\
2) multiplicity of b |b| in B $\ge$ |b| in $B_s$ \\
* This is more restrictive than subset * \\
\end{definition}
\vspace{10 pt}
\begin{large}
{Optimizing querries} \\
\end{large}
\vspace{10 pt}
- \underline{Containment:} \\
\begin{definition}
Q is bag-contained in Q' (writtten Q $\le _b$ Q') iff $Q(I) \le_b Q'(I)$ for any bag database instance I \\
* strictly stronger than set containment *
\end{definition} 
\begin{proposition}
1) For any Q, Q' if Q $\le_b$ Q' then Q $\le_b$ Q'\\
2) $\exists$ CQs Q,Q' s.t Q $\le_s$ Q' but Q $\nleq _b$ Q'\\ 
\end{proposition}
\begin{example}
two relations \{(R,x;[3]\} and \{(A,x;[2]),B,y;[1]),(c,x;[1])\} \\
\underline{join:} cross product\\
\{(R,x,A,x;[6]), ..., (R,x,C,x;[3])\}
\end{example}
\underline{Complexity:} bag-containment is more complex, possibly undecidable. \\
\vspace{6 pt}
\underline{Equivalence:} \\
\vspace{6 pt}
\begin{definition}
Two CQs Q is bag-equivalent to Q' (written Q $\equiv _b$ Q') iff Q(I) $=_b$ Q'(I) for any bag-database I, that is, Q $\le _b$ Q' and Q' $\le _b$ Q \\
\end{definition}
\begin{theorem}
Q $\equiv _b$ Q' iff Q and Q' are graph isomorphic. \\
\end{theorem}
\begin{corollary}
$\equiv _b$ is polynomially equivalent to graph isomorphism (not to be known in NP-complete). 
\end{corollary}
This means we can not reduce bag querries! \\
\underline{Complexity:} NP, but not known NP-complete or in PTIME ? \\
UCQs: on SQL: A UNION ALL B
\begin{definition}
A bag union ($\sqcup$) B $\sqcup$ B' is: \{ unique elements of B\} $\cup$ \{unique elements of B'\} $\cup$ \{shared elements b, $|b|$ in B + $|b|$ in B' \}
\end{definition}
\begin{proposition}
$\exists$ CQs Q,Q',Q'' s.t \\
Q $\le_b$ Q' $\sqcup$ Q'' but \\
Q $\nleq _b$ Q' and Q $\nleq _b$ Q'' \\
\end{proposition}
\underline{Complexity:}\\
\begin{theorem}
bag containment of UCQs is undecidable
\end{theorem}
\underline{Proof:}
\begin{definition}
a Diophantine Equation is of the form: $\Lambda (x_1, x_2, ..., x_k) = \Phi$, a polynomial of variables with no constant and integer cofficients \\
\end{definition}
Solve for integer-vallues roots.\\
\begin{theorem}
the solution to Diophantine is undeciable rewritten as: $\forall$ $x_1,...,x_k$) $\Phi_1$($x_1, ..., x_k$) $\le$ $\Phi_2$($x_1,...,x_k$) such that $\Phi_1$, $\Phi_2$ are positive\\
\end{theorem}
\begin{example}
$2x^2y + yz \le x^2y + 2xy + x^3$ \\
take predicate P, constants a,b,c\\
Q() :- P(a), P(a), P(b)\\
\hspace{16 pt} :- P(a), P(a), P(b) \\
\hspace{16 pt} :- P(b), P(c) \\
Q'() :- P(a), P(a), P(b) \\
\hspace{16 pt} :- P(a), P(b) \\
\hspace{16 pt} :- P(a), P(b) \\
\hspace{16 pt} :- P(a), P(b), P(a) \\
$\therefore$ bag-containment of UCQ is undecidable
\end{example}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\lecture{Wednesday, 3/03/10}{Mingmin}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
\textbf{\underline{Incomplete \& Probabilistic information}}
\end{center}

\begin{example}
Codd table:
\begin{tabular}{ccc}
\multicolumn{3}{c}{R}\\
\hline
0 & 1 & @\\
@ & @ & 1\\
2 & 0 & @\\
\end{tabular}
where @ means null value
\end{example}

\begin{definition}
The incomplete database  represented by a table defined as follows\\
\[rep(T)=\{v(T)\;|\;v\;is\;a\;valuation\;of\;variables\;in\;T\}\]\\
\end{definition}


\begin{definition}
Consider a table $T$ and a query $q$. For each $I\in rep(T)$, q produces an answer q(I). The set of all possible answer q(rep(T)) is an incomplete database.
\end{definition}

\textbf{Goal}: For each representation T and a query $q$, there exists a representation. $\overline{q}(T)$ such that $rep(\overline{q}(T))=q(rep(T))$.

\begin{definition}
If some representation system $\mathcal{T}$ has the property described for a query language $L$, then we say that $\mathcal{T}$ is a strong representation system for $\mathcal{L}$.
\end{definition}

%\begin{example}

%\end{example}

\begin{definition}
For a table $T$ and a query $q$, the set of sure fact $sure(q,\;T)$ is defined as \[sure(q,\;T)=\bigcap\{q(I)|I\in rep(I)\}.\]
\end{definition}

\begin{example}
$sure(q,\;T)=\emptyset,\;q'=\Pi_{1,2}(R)$\\
$sure(q'(q(rep(T))))=\{<2,0>\},\;q'(sure(q,T))=\emptyset$.
\end{example}

\begin{definition}
$\mathcal{L}$ is a query language, two incomplete DBs $I,J$ are $L$ equivalent, $I\equiv_{L}J$ if for each $q$ in $L$ we have
\[\bigcap \{q(I)|I\in \mathcal{I}\}=\bigcap\{q(J)|J\in \mathcal{I}\}\]
\end{definition}

\begin{definition}
A representation system is weak for $\mathcal{L}$ if for each $T$ of a DB and each $q$ in $\mathcal{L}$, there exists a representation $\overline{q}(T)$ s.t.
\[rep(\overline{q}(T))\equiv_{\mathcal{L}}q(rep(T)).\]
\end{definition}

\begin{theorem}
Codd tables form a weak representation system for selection-project
\[\overline{\sigma}_{cond}(T)=\{t|t\in T\;and\;cond(v(t))\;holds\;for\;all\;valuations\;of\;vars\;in\;T\}\]
\end{theorem}

\begin{example}
$\overline{\sigma}_{1=`2'}(T)=\{(2,0,@)\}$.\\
v-table: 
\begin{tabular}{ccc}
\multicolumn{3}{c}{R}\\
\hline
0 & 1 & x\\
x & z & 1\\
2 & 0 & v\\
\end{tabular}
\end{example}

\begin{theorem}
A condition is conjunct of equality/inequality atoms, x=y,x=c,x$\ne$y,x$\ne$c, where x,y are vars and c constant..
\end{theorem}

\begin{definition}
$\Phi$ is condition, a valuation V satisfies $\Phi$ if its assignment of const. to var. makes the form be true.
\end{definition}

\begin{definition}
A condition table is triple ($T,\Phi_{T},\varphi$)\\
\begin{quote}
\begin{description}
\item[-] $T$ is a v-table
\item[-] $\Phi_{T}$ is global condition
\item[-] $\varphi$ is a mapping over $T$ that associates a local condition $\varphi_{t}$ with each tuple $t\in T$
\end{description}
\end{quote}
\end{definition}

\begin{theorem}
For each c-table T and a relational algebra query $q$, one constant c-table $\overline{q}(T)$ such that $rep(\overline{q}(T))=q(rep(T))$.
\end{theorem}

\begin{proposition}
\quad\\
\begin{description}
\item[-] $\llbracket E_{1}\times E_{2} \rrbracket (t)=\llbracket E_{1}\rrbracket (t)\wedge\llbracket E_{2}\rrbracket (t)$
\item[-] $\llbracket E_{1}\cap E_{2} \rrbracket (t)=\llbracket E_{1}\rrbracket (t)\vee\llbracket E_{2}\rrbracket (t)$
\item[-] $\llbracket\sigma_{i=`c'} E_{1}\rrbracket (t)=\llbracket E_{1}\rrbracket (t)\wedge t[i]=`c'$
\item[-] $\llbracket\Pi_{i_{1},i_{2},\ldots,i_{k}} E_{1}\rrbracket (t)=\displaystyle{\bigvee_{t'\;s.t.\;\Pi_{i_{1},i_{2},\ldots,i_{k}}} E_{1}(t')=t}\llbracket E_{1}\rrbracket (t')$
\end{description}
\end{proposition}

\end{document}

