-
Notifications
You must be signed in to change notification settings - Fork 1
/
msolving.tex
319 lines (309 loc) · 10.8 KB
/
msolving.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
%------------------------------------------------------------
\lecture{Multi-shot ASP Solving}{msolving}
%------------------------------------------------------------
\part{Multi-shot ASP Solving}
%------------------------------------------------------------
%\newcommand{\PRG}{\ensuremath{P}}
\newcommand{\QRG}{\ensuremath{Q}}
\newcommand{\RRG}{\ensuremath{R}}
%------------------------------------------------------------
\section{Motivation}
%------------------------------------------------------------
\input{msolving/motivation}
% ----------------------------------------------------------------------
\begin{frame}[c]{Clingo \ = \ ASP + Control}
\begin{itemize}
\item<2-> \structure{ASP}
\begin{itemize}
\item<2-> \texttt{\#program <name> [ (<parameters>) ]}
\begin{itemize}
\item<3-> \structure{Example} \texttt{\#program play(t).}
% \item \structure{Default}\, \ \texttt{\#program base.}
\end{itemize}
\item <2->\texttt{\#external <atom> [ : <body> ]}
\begin{itemize}
\item<3-> \structure{Example} \texttt{\#external mark(X,Y,P,t)\;:\;field(X,Y),\;player(P).}
\end{itemize}
\end{itemize}
\medskip
\item<4-> \structure{Control}
\begin{itemize}
% \item<4-> Lua \ (\texttt{www.lua.org})
% \begin{itemize}
% \item<5-> \structure{Example} \texttt{prg:solve(), prg:ground(parts), \dots}
% \end{itemize}
\item<4-> Python \ (\texttt{www.python.org})
\begin{itemize}
\item<5-> \structure{Example} \texttt{prg.solve(), prg.ground(parts), \dots}
\end{itemize}
\item <6-> C, Lua, and Prolog embeddings are available too
\end{itemize}
\medskip
\item<7-> \structure{Integration}
\begin{itemize}
\item <7-> in ASP: embedded scripting language \ (\texttt{\#script})
\item <7-> in Python: library import \ (\texttt{import clingo})
\end{itemize}
\end{itemize}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}[fragile,c]{Vanilla \textit{clingo}}
\pause
\begin{center}
\begin{minipage}{0.5\linewidth}
\begin{semiverbatim}
\alert<2>{#script} (python)
def \alert<3>{main}(prg):
parts = []
parts.append(("base", []))
prg.\alert<3>{ground}(parts)
prg.\alert<3>{solve}()
\alert<2>{#end}.
\end{semiverbatim}
\end{minipage}
\end{center}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}[fragile,c]{Hello world!}
\begin{center}
\begin{minipage}{0.5\linewidth}
\begin{semiverbatim}
{\bf{}#script} (python)
def \alert<3>{main}(prg):
print("Hello world!")
{\bf{}#end}.
\end{semiverbatim}
\end{minipage}
\bigskip\pause\bigskip
\begin{minipage}{0.95\linewidth}\footnotesize
\begin{semiverbatim}
$ clingo hello.lp\pause
clingo version 4.5.0
Reading from hello.lp
\alert<4>{Hello world!}
UNKNOWN
Models : 0+
Calls : 1
Time : 0.009s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.000s
\end{semiverbatim}
\end{minipage}
\end{center}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}[fragile,c]{Preview on incremental solving}
\begin{lstlisting}
#(\pause#)#program base.
p(0).#(\pause#)
#program step (t).
p(t) :- p(t-1).#(\pause#)
#program check (t).
#external plug(t).
:- not p(42), plug(t).
\end{lstlisting}
\end{frame}
% ------------------------------------------------------------
\section{\texttt{\#program} and \texttt{\#external} declaration}
%------------------------------------------------------------
\begin{frame}[fragile]{\texttt{\#program} declaration}
\begin{itemize}
\item A \alert{program declaration} is of form
\[
\texttt{\#program}~n\,(p_1,\dots,p_k)
\]
where $n,p_1,\dots,p_k$ are non-integer constants
\item <2-> We call $n$ the \alert<2>{name} of the declaration and $p_1,\dots,p_k$ its \alert<2>{parameters}
\smallskip
\item <3-> \structure{Convention}
Different occurrences of program declarations with the same name share the same parameters
% \only<4>{(yet \clingo~4 permits others)}
\smallskip
\item <5-> \structure{Example} \
\begin{minipage}[t]{0.5\linewidth}
\begin{semiverbatim}
#program \alert<5>{acid(k)}.
b(k).
c(X,k) :- a(X).
#program \alert<5>{base}.
a(2).
\end{semiverbatim}
\end{minipage}
\end{itemize}
\end{frame}
%------------------------------------------------------------
\begin{frame}[fragile]{Scope of \texttt{\#program} declarations}
\begin{itemize}
\item <1->
The \alert{scope} of an occurrence of a program declaration in a list of rules and declarations
consists of the set of all rules and non-program declarations appearing between
the occurrence and the next occurrence of a program declaration or the end of the list
\item <2->
Rules and non-program declarations outside the scope of any program declaration
are implicitly preceded by a \alert<2>{\lstinline{base}} program declaration
\medskip
\item <3-> \structure{Example} \
\begin{minipage}[t]{0.5\linewidth}
\begin{semiverbatim}
\alert<5>{a(1).}
#program \alert<4>{acid(k)}.
\alert<4>{b(k).}
\alert<4>{c(X,k) :- a(X).}
#program \alert<5>{base}.
\alert<5>{a(2).}
\end{semiverbatim}
\end{minipage}
\end{itemize}
\end{frame}
%------------------------------------------------------------
\begin{frame}{Scope of \texttt{\#program} declarations}
\begin{itemize}
\item <1->
Given a list $\RRG$ of (non-ground) rules and declarations and a name $n$,
we define $\RRG(n)$ as the set of all rules and non-program declarations
in the scope of all occurrences of program declarations with name $n$
\item <2->
We often refer to $\RRG(n)$ as a subprogram of $\RRG$
\medskip
\item <3-> \structure{Example} \
\begin{itemize}\normalsize
\item \(
\RRG(\texttt{base})=\{a(1),a(2)\}
\)
\smallskip
\item
\(
\RRG(\texttt{acid})\only<5->{[k/42]}=\{b(\only<-5>{k}\only<6>{42}), c(X,\only<-5>{k}\only<6>{42})\leftarrow a(X)\}\only<5>{[k/42]}
\)
\end{itemize}
\smallskip
\item <4->
Given a name $n$ with associated parameters $(p_1,\dots,p_k)$,
the instantiation of $\RRG(n)$ with a term tuple $(t_1,\dots,t_k)$
results in the set
\[
\RRG(n)[p_1/t_1,\dots,p_k/t_k]
\]
obtained by replacing in $\RRG(n)$ each occurrence of $p_i$ by $t_i$ % for $1\leq i\leq k$
\end{itemize}
\end{frame}
% ------------------------------------------------------------
\begin{frame}{Contextual grounding}
\begin{itemize}
\item <1-> Rules are grounded relative to a set of atoms, called \alert{atom base}
\item <2->
Given a set \RRG\ of (non-ground) rules and two sets $C,D$ of ground atoms,
we define an instantiation of \RRG\ relative to $C$ as a ground program \cground{\RRG}{C} over $D$
subject to the following conditions:
\begin{align*}
C& {}\subseteq D\subseteq C \cup\head{\cground{\RRG}{C}}
\\
\cground{\RRG}{C}& {} \subseteq
\{
\head{r}\leftarrow\pbody{r}\cup \{\naf{a} \mid a\in\nbody{r}\cap D\}
\\&\qquad\qquad\qquad\qquad
\mid
r\in \ground{\RRG},
\pbody{r}\subseteq D
\}
\end{align*}
\item <3-> \structure{Example} Given
\(
R= \{\; a(X) \leftarrow f(X), e(X);\ b(X) \leftarrow f(X), \naf{e(X)} \;\}
\)
and
\(
C=\{f(1),f(2),e(1)\}
\),
we obtain
\[
\cground{R}{C}
=
\left\{
\begin{array}{ll}
a(1) \leftarrow f(1), e(1); & b(1) \leftarrow f(1), \naf{e(1)}\\
& b(2) \leftarrow f(2)
\end{array}
\right\}
\]
\end{itemize}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{\texttt{\#external} declaration}
\begin{itemize}
\item<1-> An \alert{external declaration} is of form
\[ % begin{equation}\label{eq:external:declaration}
\texttt{\#external}~a : B
\] % end{equation}
where $a$ is an atom and $B$ a rule body
\item <2->
A logic program with external declarations is said to be \alert<2>{extensible}
\medskip
\item <3-> \structure{Example} \
\begin{minipage}[t]{0.5\linewidth}
\begin{semiverbatim}
#external e(X) : f(X), X < 2.
f(1..2).
a(X) :- f(X), e(X).
b(X) :- f(X), not e(X).
\end{semiverbatim}
\end{minipage}
\end{itemize}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Grounding extensible logic programs}
\begin{itemize}
\item <1->
Given an extensible program $\RRG$,
we define
\begin{align*}
\QRG & {} = \{a\leftarrow B,\varepsilon\mid({\small\textbf{\texttt{\#external}}}~a : B)\in \RRG\}\\
\RRG'& {} = \{a\leftarrow B\in\RRG\}
\end{align*}
\item <2-> \structure{Note} \
An external declaration is treated as a rule \
\(
a \leftarrow B,\varepsilon
\)\\
where $\varepsilon$ is a ground marking atom
\item <3->
Given an atom base $C$,
the ground instantiation of an extensible logic program $\RRG$ is defined as
a (ground) \alert{logic program}~$P$ \alert{with externals}~$E$
where
\begin{align*}
P & {} = \{r\in\cground{\RRG'\cup \QRG}{C\cup\{\varepsilon\}} \mid \varepsilon\notin\body{r}\}\\
E & {} = \{\head{r}\mid r\in\cground{\RRG'\cup \QRG}{C\cup\{\varepsilon\}}, \varepsilon\in\body{r}\}
\end{align*}
\item <4-> \structure{Note} The marking atom $\varepsilon$ appears neither in $P$ nor $E$, respectively,
\phantom{Note} and $P$ is a logic program over $C\cup E\cup\head{P}$
\end{itemize}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Example}
\begin{itemize}
\item Extensible program
\begin{semiverbatim}
\only<1-2>{\only<1>{#external }e(X) :\only<2->{-} f(X), g(X)\only<2->{, \(\varepsilon\)}.}\only<3->{e(1) :- f(1), g(1), \(\varepsilon\).}\only<3-7>{ \textcolor<7>{red}{e(2)} :- f(2), \textcolor<6->{red}{g(2)}, \(\varepsilon\).}
f(1). f(2).
\only<1-3>{a(X) :- f(X), e(X).}\only<4->{a(1) :- f(1), e(1).}\only<4-7>{ a(2) :- f(2), \textcolor<7>{red}{e(2)}.}
\only<1-3>{b(X) :- f(X), not e(X).}\only<4->{b(1) :- f(1), not e(1).}\only<4->{ b(2) :- f(2)\only<-7>{, not \textcolor<7>{red}{e(2)}}.}
\end{semiverbatim}
\vspace{-3ex}
\item[]<5-> Atom base $\{\text{\lstinline{g(1)}}\}\cup\{\varepsilon\}$
\medskip
\item<9-> Ground program
\begin{semiverbatim}
f(1). f(2).
a(1) :- \only<-9>{f(1), }e(1).
b(1) :- \only<-9>{f(1), }not e(1).\only<10>{ } b(2)\only<-9>{ :- f(2)}.
\end{semiverbatim}
\vspace{-3ex}
\item[]<9-> with externals $\{\text{\lstinline{e(1)}}\}$
\end{itemize}
\end{frame}
% ------------------------------------------------------------
% \section{Interaction}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "asp"
%%% End: