\documentclass[sans]{beamer}

\usepackage{lmodern}
\usepackage{tikz}
\usepackage{pdfpages}
\usepackage{listings}
\usepackage{multirow}
\usetikzlibrary{arrows, positioning, shapes.geometric}
\usetikzlibrary{shadows.blur}

% hide the footline navigation
\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{}
%\setbeamertemplate{footline}{}

\tikzset{
    %Define standard arrow tip
    >=stealth',
}

\tikzset{
    invisible/.style={opacity=0,text opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
    },
}

\tikzset{
  setstyle/.style={#1},
  %bcg/.default={white},
  setstyle on/.style={alt=#1{}{setstyle}},
}

\tikzset{
  background fill/.style={fill=#1},
  background fill/.default={white},
  fill on/.style={alt=#1{}{background fill}},
}

\tikzset{
  background draw/.style={draw=#1},
  background draw/.default={white},
  draw on/.style={alt=#1{}{background draw}},
}

\tikzset{
  background filldraw/.style 2 args={draw=#1, fill=#2},
  background filldraw/.default={white}{white},
  filldraw on/.style={alt=#1{}{background filldraw}},
}

\tikzset{
  background shade/.style={#1},
  background shade/.default={top color=white, bottom color=white},
  shade on/.style={alt=#1{}{background shade}},
}

\tikzset{
  background shadedraw/.style 2 args={draw=#1, #2},
  background shadedraw/.default={white}{top color=white, bottom color=white},
  shadedraw on/.style={alt=#1{}{background shadedraw}},
}

\begin{document}
  \centering
%% -------------------------------------------------------------------
% \begin{frame}
% \huge{\textbf{Counterexample-guided Model Synthesis}}\\
%  \vspace{1cm}
%
%  Mathias Preiner, Aina Niemetz, and Armin Biere\\
%  \vspace{1cm}
%  Johannes Kepler University, Linz, Austria
% \end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
 \title{Counterexample-guided Model Synthesis}
 \author{Mathias Preiner, Aina Niemetz, and Armin Biere}
 %\institute{Johannes Kepler University, Linz, Austria}
 \institute{Presented by Marek Chalupa}
 \date {Seminar on Concurrency, May 2017}

 \frame[plain]{\titlepage}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
%\begin{frame}
%\begin{itemize}
% \item Goal: decide (un)satisfiability of quantified formulas
% \vspace{0.5cm}
% \item Theory of bit-vectors
%\end{itemize}
%\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell] (synthesis) {
  \begin{tikzpicture}
  %\filldraw[yellow] (synthesis.north east) -- (synthesis.north west) -- (synthesis.south east) -- cycle;
  \node[] (synth-prob) {Synthesis};
  %\node<1>[below= of synth-prob] (synth) {$\exists f \forall x . P[x, f]$};
  \node[below= of synth-prob] (synth)
    {$\exists f \forall x . x < 0 \implies f(x) = -x \land x \ge 0 \implies f(x) = x$};
  \end{tikzpicture}
};
%\node<1>[cell, below right=of synthesis] (fol) {
% \begin{tikzpicture}
% \node[] (sat-prob) {FO Quantified Formula};
% \node[below=of sat-prob] (form) {$\forall x \exists y . P[x, y]$};
% \end{tikzpicture}
%};
\node[cell, below =of synthesis] (fol) {
  \begin{tikzpicture}
  \node[] (sat-prob) {FO Quantified Formula};
  \node[below=of sat-prob] (form)
    {$\forall x \exists y . x < 0 \implies y = -x \land x \ge 0 \implies y = x$};
  \end{tikzpicture}
};
\draw[->, transform canvas={xshift=2mm}] (synthesis) edge (fol);
\draw[->, transform canvas={xshift=-2mm}] (fol) edge (synthesis);
\end{tikzpicture}
\end{frame}
%% -------------------------------------------------------------------


%% -------------------------------------------------------------------
  \begin{frame}
  \frametitle{Skolemization}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell] (non-skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\exists y \forall x \exists z. P[y, x, z]$};
  \end{tikzpicture}
};

\node[left=of non-skol] { $\varphi$};

\node[cell, below =of non-skol] (skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\forall x. P[f_y, x, f_z(x)]$};
  \end{tikzpicture}
};

\node[left=of skol] { $\varphi_{sk}$};
\uncover<2> {
\node[cell, fill=red!30, below =of skol] (synth) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\exists f_y f_z \forall x. P[f_y, x, f_z(x)]$};
  \end{tikzpicture}
};
}

\draw[->] (non-skol) edge (skol);
\draw<2>[->, dashed] (skol) edge (synth);
\end{tikzpicture}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Syntax Guided Synthesis}
\begin{itemize}
  \setlength\itemsep{1em}
  \item constrain to a set \emph{L} of possible functions
  \item set \emph{L} is usually given by a grammar
  \item search through elements of L and check whether some of them
        satisfy the specification
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Syntax Guided Synthesis}
\framesubtitle{Example}
\begin{itemize}
  \setlength\itemsep{1em}
  \item theory: LIA
  \item $\varphi := \forall x y .f(x, y) = f(y, x) \land f(x,y) \ge x$
  \item \emph{L}:
\only<1> {
  \begin{itemize}
    \item[] $Exp~:= x~|~y~|~Const~|~Exp + Exp$
  \end{itemize}
  \item result: none
}
\only<2> {
  \begin{itemize}
    \item[] $Term~:= x~|~y~|~Const~|~ITE(Cond, Term, Term)$
    \item[] $Cond~:= Term \le Term~|~Cond \land Cond~|~\neg Cond~|~(Cond)$
  \end{itemize}
  \item result: $f := ITE(x \ge y, x, y)$
}
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Counterexample-Guided Inductive Synthesis}
\begin{tikzpicture}
\tikzset{
  nd/.style={draw, rounded corners, fill=white, drop shadow},
  every node/.style={outer sep=8},
},
\node[nd] (init) {Initialize};
\node[nd, below=of init, align=center] (learn) {Learning\\Algorithm};
\node[nd, right=of learn, align=center] (verif) {Verification\\Oracle};
\node[below=of learn] (learn-fail) {Learning Fails};
\node[below=of verif] (learn-succ) {Learning Succeeds};

\draw[->] (init) edge (learn);
\draw[->, transform canvas={yshift=2mm}] (learn) edge node[yshift=3mm]{\tiny Candidate}(verif);
\draw[->, transform canvas={yshift=-2mm}] (verif) edge node[yshift=-3mm]{\tiny CEX} (learn);
\draw[->] (learn) edge (learn-fail);
\draw[->] (verif) edge (learn-succ);

\node<2>[overlay, draw, rounded corners, fill=yellow!30, xshift=-2.5cm, yshift=-1cm, align=center]
        (el) {Enumerative\\Learning};
\draw<2>[overlay, ->, dashed, thick] (el) |- (learn);
\end{tikzpicture}

\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Enumerative Learning}
\begin{itemize}
  \setlength\itemsep{1em}
  \item enumerate possible expressions from L
  \item start with smaller and create more complex from them
  \item keep set of test cases returned by the oracle
  \item evaluate every expression on test cases before passing it to oracle
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Enumerative Learning - cont.}
\begin{itemize}
  \setlength\itemsep{1em}
  \item if two expressions evaluate exactly the same for every test case,
        remember only one of them
  \item need to start from scratch everytime a test case is added
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Enumerative Learning}
\framesubtitle{Example}
\begin{tikzpicture}
\node[overlay, xshift=2.3cm, yshift=0cm, fill=blue!10, align=center, draw, rounded corners] {
$\varphi := \forall x y .f(x, y) = f(y, x) \land f(x,y) \ge x$\\
\\
Expr: $\{x, y, 1, 0\}$ Ops: $\{+, \le, ITE \}$
}

\end{tikzpicture}

\only<1> {
\vspace{2cm}

\begin{tikzpicture}
\tikzset{ every node/.style={minimum height=5.5mm}, },
\node[draw, rounded corners] { x };
\end{tikzpicture}

$$\varphi[f\slash x] := x = y \land x \ge x$$
$$\neg \varphi[f\slash x] := x \not = y \lor x < x$$
Counter-example: $x \rightarrow 0$, $y \rightarrow 1$
}

\only<2> {
\vspace{2cm}
\begin{tikzpicture}
\tikzset{ every node/.style={minimum height=5.5mm}, },
\node[draw, rounded corners, color=gray] (1){ x };
\node[draw, rounded corners, color=gray, right=of 1] (2){ y };
\node[draw, rounded corners, right=of 2] (3) { 1 };
\end{tikzpicture}

$$\varphi[f\slash 1] := 1 = 1 \land 1 \ge x$$
$$\neg \varphi[f\slash 1] := 1 \not = 1 \lor 1 < x$$
Counter-example: $x \rightarrow 2$, $y \rightarrow 0$
}

\only<3> {
\vspace{2cm}
\begin{tikzpicture}
\tikzset{ every node/.style={minimum height=5.5mm}, },
\node[draw, rounded corners, color=gray] (1){ x };
\node[draw, rounded corners, color=gray, right=of 1] (2){ y };
\node[draw, rounded corners, color=gray, right=of 2] (3) { 1 };
\node[draw, rounded corners, color=gray, right=of 3] (4) { 0 };
\node[draw, rounded corners, right=of 4] (5) { x + y };
\end{tikzpicture}

$$\varphi[f\slash x+y] := x + y = y + x \land x + y \ge x$$
$$\neg \varphi[f\slash x+y] := x + y \not = x + y \lor x + y < x$$
Counter-example: $x \rightarrow 1$, $y \rightarrow -1$
}

\only<4> {
\vspace{2cm}
\begin{tikzpicture}
\tikzset{ every node/.style={minimum height=5.5mm}, },

\node[draw, rounded corners, color=gray] (1){ x };
\node[color=gray, right=of 1] (4) { ... };
\node[draw, rounded corners, color=gray, right=of 4] (5) { x + y };
\node[color=gray, right=of 5] (6) { ... };
\node[draw, rounded corners, right=of 6] (7) { $ITE(x \le y, y, x)$ };
\end{tikzpicture}

$$$$
$$$$
Counter-example: none
}

\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Counterexample-guided model synthesis}
\begin{tikzpicture}
\tikzset{
  nd/.style={draw, rounded corners, fill=white, drop shadow},
  every node/.style={scale=0.8},
},
\node[] (phi) { $\varphi$ };
\node[nd, below=of phi] (skol) {Skolemization};
\node[nd, right=of skol, align=center] (ground) {Check\\Ground Instances};
\node[nd, right=of ground, align=center] (synth) {Synthesize\\Candidate Model};
\node[nd, below=of synth, align=center] (chmd) {Check\\Candidate Model};
\node[nd, left=of chmd, align=center] (cegqi) {Create New\\Ground Instance};`
\node[below=of chmd, align=center] (sat) {SAT};
\node[right=of sat, align=center] (unsat) {UNSAT};
\node[inner sep=0, outer sep=0, overlay, above=of ground] (grnd-aux) {};

\draw[->] (phi) edge (skol);
\draw[->] (skol) edge node[yshift=3mm] {$\varphi_{sk}$} (ground);
\draw[->] (ground) edge node[yshift=3mm] {\tiny sat} node[yshift=-3mm] {\tiny $M_G$} (synth);
\draw[->] (synth) edge node[align=center, xshift=6mm] {\tiny candidate} (chmd);
\draw[->] (chmd) edge node[xshift=4mm] {\tiny done} (sat);
\draw[->] (chmd) edge node[yshift=-3mm] {\tiny cex } (cegqi);
\draw[->] (cegqi) edge node[xshift=8mm] {\tiny $G = G \land g_i$ } (ground);
\draw[overlay,->] (ground) --  node[yshift=9mm, xshift=6mm] {\tiny unsat} (3.2,0) -| (unsat);
\end{tikzpicture}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Counterexample-guided model synthesis}
\framesubtitle{What grammar to use?}
\begin{itemize}
  \setlength\itemsep{1em}
  \item taking too much operators into the account may explore too many expressions
  \item use $\{ite, = \}$ and the operators from formula
  \item use parameters from the function and constants from the formula as base expressions
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Counterexample-guided model synthesis}
\framesubtitle{Example}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell] (non-skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\forall x \exists y. (x < 0 \implies y = -x) \land (x \ge 0 \implies y = x)$};
  \end{tikzpicture}
};
\node[overlay, left=of non-skol, xshift=0.5cm] { $\varphi$};

\pause

\node[cell, below =of non-skol] (skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\forall x . (x < 0 \implies f(x) = -x) \land (x \ge 0 \implies f(x) = x)$};
  \end{tikzpicture}
};
\node[overlay, left=of skol, xshift=1.1cm] { $\varphi_{sk}$};

\draw[->] (non-skol) edge (skol);
\end{tikzpicture}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Counterexample-guided model synthesis}
\framesubtitle{Example}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell, below =of non-skol] (skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\forall x . (x < 0 \implies f(x) = -x) \land (x \ge 0 \implies f(x) = x)$};
  \end{tikzpicture}
};
\node[overlay, left=of skol, xshift=1.1cm] { $\varphi_{sk}$};
\end{tikzpicture}

\vspace*{1cm}
Initialize:~$Expr := \{0, x\}$, $Ops := \{-, =, <, ite \}$
\end{frame}
%% -------------------------------------------------------------------

\begin{frame}
\frametitle{Counterexample-guided model synthesis}
\framesubtitle{Example}
\begin{tabular}{l | c | l | c}
G           & Candidate (S) & \varphi_{sk}[f\slash S]& CEX \\
\hline
\multirow{2}{*}{$\top$}      & \multirow{2}{*}{$f(x) = 0$}      & $(x < 0 \implies 0 = -x)~\land$ & \multirow{2}{*}{$x \rightarrow 1$}\\
            &               & $(x \ge 0 \implies 0 = x)$       &\\
\hline
\multirow{2}{*}{$f(1) = 1$}      & \multirow{2}{*}{$f(x) = x$}      & $(x < 0 \implies x = -x)~\land$ & \multirow{2}{*}{$x \rightarrow -1$}\\
            &               & $(x \ge 0 \implies x = x)$       &\\

\hline
$f(1) = 1~\land$ & \multirow{2}{*}{\footnotesize$ite(x < 0, -x, x)$}   & \multirow{2}{*}{$(-x = -x)~\land (x = x)$} & \multirow{2}{*}{--}\\
$f(-1) = 1$            &               &        &\\


\end{tabular}
\end{frame}



%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Dual Counterexample-guided model synthesis}
\begin{itemize}
  \setlength\itemsep{1em}
  \item counterexample-guided model synthesis is a model finding procedure
  \item it may have problem with unsatisfiable formulas
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Dual Counterexample-guided model synthesis - cont.}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell] (non-skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\exists a b c \forall x . a * c + b * c \not = x * c$};
  \end{tikzpicture}
};
\end{tikzpicture}

\vspace{1cm}
We synthesize only constants and oracle won't help us.
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Dual Counterexample-guided model synthesis - cont.}
Observation: Let $\varphi := \forall x \exists y. P[x, y]$.
If $\neg \varphi := \exists x \forall y. \neg P[x, y]$ is satisfiable,
then the model of $x$ that satisfies $\neg \varphi$
can be used to prove unsatisfiability of $\varphi$.
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Dual Counterexample-guided model synthesis - cont.}
\begin{tikzpicture}
\tikzset{
  cell/.style={draw, rounded corners, fill=blue!10, drop shadow},
  every node/.style={outer sep=8},
}

\node[cell] (non-skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\exists a b c \forall x . a * c + b * c \not = x * c$};
  \end{tikzpicture}
};

\node[overlay, left=of non-skol, xshift=0.5cm] { $\varphi$};

\node[cell, below=of non-skol] (skol) {
  \begin{tikzpicture}
  \node[] (n1)
    {$\forall a b c \exists x . a * c + b * c = x * c$};
  \end{tikzpicture}
};

\node[overlay, left=of skol, xshift=0.5cm] { $\neg \varphi$};
\end{tikzpicture}

\vspace{1cm}
Running CEGMS on $\neg \varphi$ yields $f_x(a, b, c) = a + b$,
which proves unsatisfiability of $varphi$.
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Dual Counterexample-guided model synthesis - cont.}
\begin{itemize}
  \setlength\itemsep{1em}
  \item Run CEGMS in parallel on $\varphi$ and $\neg \varphi$
  \item If $\varphi$ is satisfiable, we're done.
  \item If $\neg \varphi$ is satisfiable, $\varphi$ is unsatisfiable.
  \item If $\neg \varphi$ is unsatisfiable, $\varphi$ is satisfiable
  (no model for $\varphi$ produced in this case yet).
\end{itemize}
\end{frame}
%% -------------------------------------------------------------------

%% -------------------------------------------------------------------
\begin{frame}
\frametitle{Experiments}
In the paper ;)
\end{frame}
%% -------------------------------------------------------------------

\end{document}
