Docsity

Logic Programming in Prolog: Examples, Concepts, and Exercises, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Logic Programming
May 1, 2007
2
A Prolog Example
• Facts
– 'It is sunny'.
–'It is summer'.
•Query
– ?- 'It is sunny'.
•Yes
•Rules
– 'It is hot' :- 'It is summer', 'It is sunny'.
– 'It is cold ' :- 'It is winter', 'It is s nowing'.
•Queries
– ?- 'It is cold '.
•No
– ?- 'It is hot'.
•Yes
3
Another Example
• female(amy).
• female(johnette).
• male(anthony).
• male(bruce).
• male(ogden).
• parentof(amy, johnette).
• parentof(amy, anthony).
• parentof(amy, bruce).
• parentof(ogden, johnette).
• parentof(ogden, anthony).
• parentof(ogden, bruce).
• siblingof(X,Y) :-
parentof(Z,X),
parentof(Z,Y), X != Y.
• brotherof(X,Y) :-
parentof(Z,X),
male(X ),
parentof(Z,Y),
X != Y.
• ? – parentof(amy, Y).
• ? – parentof(X, anthony).
• ? – siblingof(X, Y).
• ? – siblingof(johnette, Y).
• ? – siblingof(johnette, bruce).
4
SWI Prolog
• A Prolog program consists of a database of facts and rules,
and queries (questions).
– Fact: ... .
• 'It is sunny'.
• male(anthony).
– Rule: ... :- ... .
• 'It is hot' :- 'It is summer', 'It is sunny'.
• siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y ).
– Query: ?- ... .
• ?- 'It is hot'.
– Variables: must begin with an upper case letter.
– Constants: numbers, begin with lowercase letter, or enclosed in
single quotes.
pf3

Partial preview of the text

Download Logic Programming in Prolog: Examples, Concepts, and Exercises and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Logic ProgrammingSpring 2007

May 1, 2007

A Prolog Example

  • Facts 'It is sunny'.

'It is summer'.

  • Query
  • ?- 'It is sunny'. Yes
  • Rules 'It is hot' :- 'It is summer', 'It is sunny'.

'It is cold' :- 'It is winter', 'It is snowing'.

  • Queries
  • ?- 'It is cold'. No
  • ?- 'It is hot'. Yes

3

Another Example

female(amy).

female(johnette).

male(anthony).

male(bruce).

male(ogden).

parentof(amy, johnette).

parentof(amy, anthony).

parentof(amy, bruce).

parentof(ogden, johnette).

parentof(ogden, anthony).

parentof(ogden, bruce).

siblingof(X,Y) :- parentof(Z,Y), X != Y.parentof(Z,X),

brotherof(X,Y) :- X != Y.parentof(Z,Y),male(X),parentof(Z,X),

? – parentof(amy, Y).

? – parentof(X, anthony).

? – siblingof(X, Y).

? – siblingof(johnette, Y).

? – siblingof(johnette, bruce).

SWI Prolog

  • and queries (questions).A Prolog program consists of a database of facts and rules,
  • Fact: ....
  • (^) male(anthony).'It is sunny'.
  • Rule: ... :- ....
  • (^) siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y).'It is hot' :- 'It is summer', 'It is sunny'.
  • Query: ?- .... ?- 'It is hot'.

Variables: must begin with an upper case letter.

single quotes.Constants: numbers, begin with lowercase letter, or enclosed in

5

Important Concepts

– Pattern matchingUnification

Backtracking

Lists

  • Find the total cost of a list of items cost(cornflakes, 230).

cost(cocacola, 210).

cost(chocolate, 250).

cost(crisps, 190).

total_cost([], 0).

Cost is ItemCost + CostOfRest.total_cost(Rest, CostOfRest),cost(Item, ItemCost),total_cost([Item|Rest], Cost) :-

  • Sample query: ?- (^) total_cost([cornflakes, crisps], X).
  • Output X = 420

7

Complex Structures

– tree(empty, jill, empty)– fred,– tree(empty, jack, empty),tree(

Computing the Size of a Tree

Size of tree = number of nodes

– tree_size(empty, 0).The size of an empty tree is zero.

– tree_size(tree(L, _, R), Total_Size) :-for the current tree node.sub-tree plus the size of the right sub-tree plus oneThe size of a non-empty tree is the size of the left

Total_Size is Left_Size + Right_Size + 1.tree_size(R, Right_Size),tree_size(L, Left_Size),