P. 1
Prolog Lab Manual

Prolog Lab Manual

|Views: 3,410|Likes:
Published by elfrich

More info:

Published by: elfrich on Jun 09, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/24/2012

pdf

text

original

In this experiment we’ll examine some powerful tools for constructing sets
and bags.

The Setof Predicate

When we work with sets in a programming language we normally work with
lists that have no repeated elements. The Prolog language treats sets in this
manner too and it uses list notation for sets. So, for example, when we think
about a set like

{a, b, c},

List Structures

59

we must use the notation

[a, b, c]

to represent it in Prolog.
We can often describe a set by describing a property (i.e., a predicate) that
the elements of the set must satisfy. For example, if we let p(a, b) mean that
“a is a parent of b,” then the set S of all people who are parents can be
described as

S = {x | p(x, y) for some y}

In terms of formal logic, the statement “p(x, y) for some y” can be written as
$y p(x, y). So we can describe S as

S = {x | $y p(x, y)}.

Prolog provides a useful tool called the “setof” predicate that can be quite
useful in calculating sets. We’ll introduce it with some examples. Suppose we
put the following four facts in the program.

p(a, b).
p(a, c).
p(b, d).
p(c, e).

Suppose that we want to find the set S of all letters that appear in the first
argument of one of the facts. In other words, we want to calculate the set
S that we defined above. Since there are only four facts in the program it is
easy to see that

S = {x | $y p(x, y)} = {a, b, c}.

Let’s try to use the setof predicate to find S. In Prolog the notation Y^ means
“there exists Y.” We can compute the set S with the following goal.

| ?- setof(X, Y^p(X, Y), S).

This goal returns S = [a, b, c].
For another example, suppose that we want to calculate the set of second
arguments that have the letter a as a first argument of p. In other words, we

60

Prolog Experiments

want to calculate the set

S = {x | p(a, x)} = {b, c}.

We can compute the set S with the following goal.

| ?- setof(X, p(a, X), S).

This goal returns S = [b, c].
For another example, suppose that we want to construct the set of
“grandparent” relationships from p. If we let g(x, y) mean that x is a
grandparent of y, then we can describe the set S of these relations as the
following set.

S = {g(x, y) | $z (p(x, z) Ÿ p(z, y))}.

We can compute the set S with the following goal.

| ?- setof(g(X, Y), Z^(p(X, Z), p(Z, Y)), S).

This goal returns S = [g(a, d), g(a, e)].

The Bagof Predicate

A bag (or multiset) is like a set except that repetitions of elements can occur
in a bag. Bags are represented in Prolog as lists. The bagof predicate works
just like the setof predicate except that repeated elements are kept. For
example, to find the bag B of all letters that appear in the first argument of
one of the facts in the preceding example we type the following goal.

| ?- bagof(X, Y^p(X, Y), B).

This goal returns B = [a, a, b, c].

The Findall Predicate

The findall predicate is similar to the bagof predicate. It finds a bag of
elements by examining all solutions to a query without any need to use
quantifiers. For example, to find the bag B of all letters that appear in the
first argument of one of the facts in the preceding example we type the
following goal.

List Structures

61

| ?- findall(X, p(X, Y), B).

This goal returns B = [a, a, b, c].

Experiments to Perform

1. Try the sample goals for the given set of facts. Then, for each of the
following cases, use the setof predicate to construct and test a goal to
compute the given set.
a.C = {y | $x p(x, y)} (children).
b.R = {c(y, x) | p(x, y)}

(child relations).

2.Modify the sample goals by eliminating quantified variables. For
example, in place of the goal
|?- setof(X, Y^p(X, Y), S).
use the goal
|?- setof(X, p(X, Y), S).
Also try the goal
|?- setof(X, p(X, _), S).
In each test, be sure to backtrack as much as possible. Try to explain the
difference in the outcomes.

3.The setof predicate returns no instead of the empty set. For example,
using the given set of facts we have {x | $y p(x, a)} = ∅.

a.Do a test to verify this fact.
b.Define a new predicate “newsetof” that calls the setof predicate. But
if the setof predicate returns no, then newsetof returns [ ]. Test
newsetof on 3 nonempty sets and 3 empty sets.

4.Enter the following set of facts in the Prolog database.
p(0, 0, 0, 49).
p(0, 0, 1, 143).
p(0, 1, 0, 78).
p(0, 1, 1, 398).
p(1, 0, 0, 87).
p(1, 0, 1, 398).
p(1, 1, 0, 49).

62

Prolog Experiments

p(1, 1, 1, 374).

For each of the following cases, use setof, bagof, or findall to construct
the indicated set or bag.
a.The set of all facts of the form p(0, X, Y, Z) for some X, Y, and Z.
b.The bag of all numbers N such that p(X, Y, 0, N) for some X and Y.
c.The bag of all numbers N such that p(X, Y, Z, N) for some X, Y, and Z.
d.The bag of all pairs [Y, Z] such that p(X, Y, Z, N) for some X and N.
e.The set of all facts of the form p(X, Y, Z, N) where N < 100.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->