0% found this document useful (0 votes)
74 views23 pages

Foundations of Logic Programming

The document introduces Prolog programs and queries using examples. It shows how Prolog allows for declarative programming through facts and rules. Queries to a flight connection program are provided to illustrate how Prolog finds solutions through logic inference. Lists are discussed as an important data structure in Prolog. An imperative program for comparison is shown. Advantages of declarative programming are described, though shortcomings of Prolog related to non-termination are also outlined.

Uploaded by

Parham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views23 pages

Foundations of Logic Programming

The document introduces Prolog programs and queries using examples. It shows how Prolog allows for declarative programming through facts and rules. Queries to a flight connection program are provided to illustrate how Prolog finds solutions through logic inference. Lists are discussed as an important data structure in Prolog. An imperative program for comparison is shown. Advantages of declarative programming are described, though shortcomings of Prolog related to non-termination are also outlined.

Uploaded by

Parham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 1

Introduction

Foundations of Logic Programming Introduction 1


Outline

Introducing Prolog programs and queries

Showing the advantages of declarative programming

Illustrating shortcomings of Prolog

Foundations of Logic Programming Introduction 2


A Prolog Program

direct(frankfurt,san_francisco).
direct(frankfurt,chicago). Facts
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Y). Rules


connection(X, Y) :- direct(X, Z), connection(Z, Y).

Foundations of Logic Programming Introduction 3


Queries (I)

direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Y).


connection(X, Y) :- direct(X, Z), connection(Z, Y).

| ?- connection(frankfurt, maui).
yes

Foundations of Logic Programming Introduction 4


Queries (II)

direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Y).


connection(X, Y) :- direct(X, Z), connection(Z, Y).

| ?- connection(san_francisco, X).
X = honolulu ;
X = maui ;
no

Foundations of Logic Programming Introduction 5


Queries (III)

direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Y).


connection(X, Y) :- direct(X, Z), connection(Z, Y).

| ?- connection(maui, X).
no

Foundations of Logic Programming Introduction 6


An Important Data Structure: Lists

[a1,..., an] [apples,pears,plums]


[head | tail] = [apples | [pears,plums]]

member(X, [X | List]).
member(X, [Y | List]) :- member(X, List).

member_both(X, L1, L2) :- member(X, L1), member(X, L2).

| ?- member_both(X, [apples,pears,plums], [peaches,plums,pears]).


X = pears ;
X = plums ;
no

Foundations of Logic Programming Introduction 7


An Imperative Program for Comparison

type List=array[1..n] of integer;


procedure members(a, b : List; var c : List; var x : integer);
var i, j, k : integer;
begin
k := 1;
for i := 1 to n do
for j := 1 to n do
if a[i] = b[j] then begin
c[k] := a[i]; k := k + 1
end;
x := k – 1
end;

Foundations of Logic Programming Introduction 8


Declarative Programs are Flexible

member(X, [X | List]).
member(X, [Y | List]) :- member(X, List).

member_both(X, L1, L2) :- member(X, L1), member(X, L2).

| ?- member_both(pears,[apples,pears,plums],[peaches,plums,pears]).
yes

| ?- member_both(apples,[apples,pears,plums],[peaches,X]).
X = apples

Foundations of Logic Programming Introduction 9


Declarative Programs are Flexible
add(X,0,X). /* x + 0 = x */
add(X,s(Y),s(Z)) :- add(X,Y,Z). /* x + y = z → x + s(y) = s(z) */
| ?- add(s(0),s(0),Z).
Z = s(s(0))
| ?- add(X,Y,s(s(0))).
X = s(s(0)),
Y = 0 ;
X = s(0),
Y = s(0) ;
X = 0
Y = s(s(0))

Foundations of Logic Programming Introduction 10


Yet Another Declarative Specification

The square of 45 is 2025, and 20 + 25 is 45, isn't that strange?


Find more pairs of numbers that exhibit this pecularity!

solution(N, Z) :- between(1, 99, N),


Z is N*N,
Z >= 1000,
(Z // 100) + (Z mod 100) =:= N.
| ?- solution(N, Z).
N = 45, Z = 2025 ;
N = 55, Z = 3025 ;
N = 99, Z = 9801

Foundations of Logic Programming Introduction 11


Programming Languages

Imperative Programming Languages


- Declaration part defines possible states (of variables); statement part defines
transformation on states
- Close to von Neumann computer architecture
- Description of how something is computed
- Example: Java

Declarative Programming Languages


- Abstraction from states and state transformations
- Direct formulation of mathematical objects (functions, relations, constraints)
- Description of what is computed
- Example: Prolog, Eclipse, Haskell, and Curry

Foundations of Logic Programming Introduction 12


Declarative Programming Languages

Logic Programming Languages


Example language: Prolog

Constraint Logic Programming Languages


Example language: Eclipse

Functional Programming Languages


Example language: Haskell

Integrated (Functional-logic) Programming Languages


Example language: Curry

Foundations of Logic Programming Introduction 13


Advantages of Declarative Programming

Specifications are programs

The computation mechanism is not part of the program

“Thinking” declaratively is easier than “thinking” procedurally

Declarative programs are therefore much simpler to understand, develop,


and verify

The output of a logic program is a logical consequence of the program

Logic programs are flexible

Foundations of Logic Programming Introduction 14


Shortcomings of Prolog: Termination (I)
direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).
direct(san_francisco,san_francisco).

connection(X, Y) :- direct(X, Y).


connection(X, Y) :- direct(X, Z), connection(Z, Y).

| ?- connection(san_francisco, X).
X = honolulu ;
X = san_francisco ;
X = maui ;
X = honolulu ;
...

Foundations of Logic Programming Introduction 15


Shortcomings of Prolog: Termination (II)
direct(san_francisco,san_francisco).
direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Y).


connection(X, Y) :- direct(X, Z), connection(Z, Y).

| ?- connection(san_francisco, X).
X = san_francisco ;
X = honolulu ;
X = san_francisco ;
X = honolulu ;
...

Foundations of Logic Programming Introduction 16


Shortcomings of Prolog: Termination (III)
direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).
direct(san_francisco,san_francisco).

connection(X, Y) :- direct(X, Z), connection(Z, Y).


connection(X, Y) :- direct(X, Y).

| ?- connection(san_francisco, X).
X = maui ;
X = maui ;
X = maui ;
...

Foundations of Logic Programming Introduction 17


Shortcomings of Prolog: Termination (IV)
direct(san_francisco,san_francisco).
direct(frankfurt,san_francisco).
direct(frankfurt,chicago).
direct(san_francisco,honolulu).
direct(honolulu,maui).

connection(X, Y) :- direct(X, Z), connection(Z, Y).


connection(X, Y) :- direct(X, Y).

| ?- connection(san_francisco, X).

Foundations of Logic Programming Introduction 18


Shortcomings of Prolog: “Occur Check” - Failure

A person x and the mother of x can never be the same.

mystery :- same_person(X, mother_of(X)).


same_person(Z, Z).

| ?- mystery.
yes

Foundations of Logic Programming Introduction 19


Shortcomings of Prolog:
Is Prolog Truly Declarative?

This rule can only be “called” if all three arguments are numbers:

between(X, Y, Z) :- X =< Z, Z =< Y.

This is the “simplest” usable specification:

between (X, Y, Z) :- X =< Y, Z is X.


between (X, Y, Z) :- X < Y, X1 is X+1, between(X1, Y, Z).

Foundations of Logic Programming Introduction 20


How to Use a Prolog System (I)

% add-program in file add.pl:


add(X,0,X).
add(X,s(Y),s(Z)) :- add(X,Y,Z).

irz601:~> sicstus
SICStus 3 #5: Fri Nov 1 15:49:55 MET 1996
| ?- [add].
{consulting/usr/users/ith/ak15/add.pl...}
{/usr/users/ith/ak15/add.pl consulted, 0 msec 352 bytes}

yes
| ?- add(X,Y,s(s(0))).

Foundations of Logic Programming Introduction 21


How to Use a Prolog System (II)

X = s(s(0)),
Y = 0 ? ;

X = s(0),
Y = s(0) ? ;

X = 0,
Y = s(s(0)) ? ;

no
| ?- halt.

Foundations of Logic Programming Introduction 22


Objectives

Introducing Prolog programs and queries

Showing the advantages of declarative programming

Illustrating shortcomings of Prolog

Foundations of Logic Programming Introduction 23

You might also like