Prolog - part 1

Language basics: clauses, database, and queries; resolution and unification Material adapted from Mark Hills’ Organization of Programming Languages

Lesson plan

Logic languages

Background

History

Logic programming

Logic programming concepts

Logic programming concepts

Why care about Prolog?

Logic programming

Declarative vs imperative programming

Logic programming is declarative: we will describe the problem, the language will try to find the solution.

Basic concepts of logic programming

First-order predicate logic

Objects in first order logic

Our concept of object will be based on what we are writing the program about, examples include:

Logic programming concepts

Logical connectives in first order logic

This just gives us ways to say more complex things:

Axioms

Prolog

Computing in Prolog

Computing in Prolog

The database

Let’s see how we can represent the database for the graph shown in Figure 1.

<a name='figure1'>Figure 1</a>. An example of a connected graph: node c is connected to nodes a and h, node a is connected to node f, node f is not connected to other nodes, etc.
Figure 1. An example of a connected graph: node c is connected to nodes a and h, node a is connected to node f, node f is not connected to other nodes, etc.

connected(c,a). connected(a,f). connected(d,b). connected(b,e). connected(d,e). connected(d,g). connected(g,e). connected(c,h). connected(h,f).

human(socrates). fatherof(socrates,jane). fatherof(zeus,apollo).

Rules

mortal(X) :- human(X). human(Y) :- fatherof(X,Y), human(X).

pathfrom(X,Y) :- connected(X,Y). pathfrom(X,Y) :- connected(X,Z), pathfrom(Z,Y).

\[\forall x . human(x) \rightarrow mortal(x).\] \[\forall y . (\exists x . fatherof(x,y) \wedge human(x)) \rightarrow human(y)\]

More than just true or false….

Prolog can also give you a list of elements that make a predicate true (via unification)

?- fatherof(Who,apollo).

Who = zeus ;

?- pathfrom(c,X).

X = a ;

X = h ;

X = f ;

X = f ;

false.

The semicolon is entered by the user – it means to keep searching

Tracing pathfrom

?- pathfrom(c,X).

$$\rightarrow$$ pathfrom(c,Y) :- connected(c,Y).

X = a ;

When we hit semicolon, we tell it to pretend that this is false, and to keep searching. We have to throw away the connected(c,a) result, so we backtrack through our database to try again.

$$\rightarrow$$ pathfrom(c,Y) :- connected(c,Y).

X = h ;

We tell it to try again with this one, too. At this point, we no longer have any rules that say that c is connected to something.

Tracing pathfrom, II

pathfrom(c,Y) :- connected(c,Z), pathfrom(Z,Y).`

We will first find something in the database that says that c is connected to some Z, and then check if there is a path between Z and Y.

We find a and h as last time. When we check a, we check for pathfrom(a,Y), and find that connected(a,f) is in the database. The same thing happens for h, which is why f is reported as an answer twice.

Statements

mother(mary, fred).

% you can either think of this as

% a predicate asserting that mary is the mother of fred -

% or a data structure (tree) in which

% the functor (atom) mother is the root,

% mary is the left child, and fred is the right child

rainy(rochester).

Prolog terms

Resolution

Resolution example

takes(jane_doe, his201).
takes(jane_doe, cs254).
takes(ajit_chandra, art302). takes(ajit_chandra, cs254).
classmates(X, Y) :- takes(X, Z), takes(Y, Z).

Unification

The unification rules for Prolog state that

Queries

Axioms

human(socrates).
human(Y) :- fatherof(X,Y), human(X).
mortal(X) :- human(X).

Goals

?- human(socrates). /* listed, therefore true */
?- mortal(socrates). /* not listed */

Socrates is not listed as being mortal, but mortal(socrates) unifies with mortal(X) if we replace X with socrates. This gives us a subgoal. Replace X with socrates and try it….

Queries

Replace X with socrates in this rule:

mortal(X) :- human(X).

to get

mortal(socrates) :- human(socrates).

Since human(socrates) is in the database, we know that mortal(socrates) is also true.

Summary


Back to course home