CSc 372 - Comparative Programming Languages
22 : Prolog -- The Database

Christian Collberg

Department of Computer Science

University of Arizona

1 Manipulating the Database

2 Assert

3 Assert...

4 Assert ...- Example


\begin{gprogram}
\redtxt{?- loc(whitehorse, Ave, St).} \\
\x Ave = 8, St = 11 \...
...2 \\
\redtxt{?- loc(airport, Ave, St).} \\
\x Ave = 5, St = 32
\end{gprogram}

5 Assert ...- Example


\begin{gprogram}
location(whitehorse, 8, 11). \\
location(microsoft, 8, 42).\ ...
...at street? '), read(Street),\\
\x assert(location(X, Ave, Str)).
\end{gprogram}

6 Retract

7 Retract...


\begin{gprogram}
retractall(X) :- \\
\x retract(X), fail.\\
retractall(X) :- \\
\x retract((X :- Y))),\\
\x fail.\\
retractall(\_).
\end{gprogram}

8 Clause


\begin{gprogram}
append([], X, X). \\
append([A\vert B],C,[A\vert D]) :- \\
\x...
...\_6, Z=[\_4\vert\_7], \\
\xx Y=append(\_5, \_6, \_7) ; \\
\x no
\end{gprogram}

9 Clause...

10 Clause...

List all the clauses whose head matches X.

\begin{gprogram}
list(X) :- clause(X, Y),\\
\x print(X, Y), \\
\x write('.'), ...
...5\vert\_6],\_7,[\_5\vert\_8]) :- \\
\xxx append(\_6, \_8, \_8).
\end{gprogram}

11 Clausal Representation of Data Structures

12 Clauses as Data Structures - Lists


\begin{gprogram}
list(c). \\
list(h). \\
list(r). \\
list(i). \\
list(s). \\...
...cess\_list :- list(X), process\_item(X), fail. \\
process\_list.
\end{gprogram}

13 Clauses as Data Structures - Trees


\begin{gprogram}
t(node1, node2, phone(thompson, 2432), node3). \\
t(node2, nil...
...white, 2432), nil). \\
t(node4, nil, phone(mcbride, 1781), nil).
\end{gprogram}

14 Clauses as Data Structures - Trees...

Tree

15 Clauses as Data Structures - Trees...


\begin{gprogram}
inorder(nil).\\
inorder(Node) :-\\
\x t(Node, Left, P, Right)...
...(mcbride,1781)\\
\x phone(thompson,2432)\\
\x phone(white,2432)
\end{gprogram}

16 Clausal Representation...

17 Switches

18 Switches...


\begin{gprogram}
turnon(Switch) :- \\
\x call(Switch), !. \\
turnon(Switch) :-...
...x retract(Switch), !. \\
flip(Switch) :- \\
\x assert(Switch).
\end{gprogram}

19 Switches...


\begin{gprogram}
turnon(terse\_mess). \\
\xx ..... \\
flip(terse\_mess). \\
\...
... \\
\x write(C), write('. Please accept our...'), \\
\x nl, !.
\end{gprogram}

20 Memoization

21 Memoization - Towers of Hanoi

22 Memoization - Towers of Hanoi...

Hanoi

23 Memoization - Towers of Hanoi...


\begin{gprogram}
:- op(100, xfx, to).\\
\\
hanoi(1, A, B, C, [A to B]).\\
han...
... M2], Ms).\\
\\
go(N, Moves) :-\\
\x hanoi(N, a, b, c, Moves).
\end{gprogram}

24 Memoization - Towers of Hanoi...


\begin{gprogram}
\redtxt{?- go(2,M).} \\
\x M = [a to c, a to b, c to b] \\
\\...
..., \\
\xx c to a, b to a, c to b, \\
\xx a to c, a to b, c to b]
\end{gprogram}

25 Memoization - Towers of Hanoi...


\begin{gprogram}
hanoi(1, A, B, C, [A to B]).\\
hanoi(N, A, B, C, Ms) :-\\
\x ...
... Moves) :-\\
\x hanoi(N, A, B, C, Moves), \\
\x Pegs=[A, B, C].
\end{gprogram}

26 Memoization - Towers of Hanoi...


\begin{gprogram}
hanoi(1, \_3, \_5, \_4, [\_3 to \_5]) :- !.\\
hanoi(2, \_3, \_...
...\xx \_3 to \_5, \_4 to \_3, \_4 to \_5,\\
\xx \_3 to \_5]) :- !.
\end{gprogram}

27 Example - Gensym

28 Example - Gensym...


\begin{gprogram}
gensym(Root, Atom) :- \\
\x get\_num(Root, Num),\\
\x name(Ro...
...).\\
get\_num(Root, 1) :- \\
\x asserta(current\_num(Root, 1)).
\end{gprogram}

29 Example - Gensym...


\begin{gprogram}
int\_name(Int, List) :- int\_name(Int, [], List).\\
int\_name(...
...x A = chris2 \\
\redtxt{?- gensym(chris, A).} \\
\x A = chris3
\end{gprogram}

30 Readings and References



Christian S. Collberg
2005-10-21