This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

### Publishers

Scribd Selects Books

Hand-picked favorites from

our editors

our editors

Scribd Selects Audiobooks

Hand-picked favorites from

our editors

our editors

Scribd Selects Comics

Hand-picked favorites from

our editors

our editors

Scribd Selects Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

P. 1

Prolog Lab Manual|Views: 3,410|Likes: 5

Published by elfrich

See more

See less

https://www.scribd.com/doc/57481900/Prolog-Lab-Manual

12/24/2012

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

S = {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

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

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

(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

**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.

OX710-SCHEMA

Atmel Avr Efficient c Coding

Using Matlab in Linear Algebra

Running Labview in Matlab

Numerical Analysis With MATLAB

Matlab Training - Basic Control Theory

Matlab Tutorial for Systems and Control Theory

MATLAB Symbolic Math Toolbox

Matlab Partial Differential Equation Toolbox

Matlab6xConatec

Matlab 1

Matlab 0

Introduccion a La Programacion Visual Con Matlab

herramientas simular Matlab2

Fourier Transforms Using Matlab

Examples of Programming in Matlab

Aprenda Matlab 6.1 Como Si Estuviera en Primero

pdf Engineering - Fluid Dynamics - Shallow Liquid Simulation Using Matlab (2001 Neumann)

(eBook) Chemical Process Control a First Course With MATLAB

(2001) E-Book - Electronics and Circuit Analysis Using Matlab - Theory - Diodes

op

Gyros Cop

giroscopio

giros

Como hacer moldes y vaciados

- Read and print without ads
- Download to keep your version
- Edit, email or read offline

50_Math_And_Science_Games_For_Leadership

Autocad 2012 Basics

Autocad 2012 Keyboard Shortcuts

Advance Engineering Materials

AUTOCAD 2012

AutoCAD 2012 Tutorial

Good Practice Guide for Measurement Gloss

An Introduction to Mathematics

Renewable Energy Global Status Report

Know Your Oscilloscope - Paul C. Smith - 1958

Calculus II

Measurement and Instrument

Finite Element Method - Boundary Element Method

Problems and Solutions in Mechanical Engineering - Malestrom

Basic Physics

Fluid Mechanics

GRE, Math Practice Test

∑ x [n ]e

M. Rades - Mechanical Vibrations 2

Elementary Algebra

FW55

Getting Started Guide for Camtasia 8.0

Trigonometry

Engineering Fundamentals of the Internal Combustion Engine

How2 Make Robots

Finite Element Book

Offshore Engineering for Structural Engineers

Thermodynamics 1

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd

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