5
Introduction to Programming – Class 9 – Prolog Paul Brebner Logic Truth tables T/F for Negation, AND, OR, XOR. (Illustrated on whiteboard) Prolog: Facts and Rules Logic programming Program = facts and rules Facts = predicate(atom), predicate(atom1, atom2). Rules = predicate(Variable1, ...) :- predicate(Variable1, ...), predicate(Variable, ...). All predicates of same name must be together in this version. Run program by asking a question: E.g. predicate(X)? Can get 0 or more answers Try simple examples E.g. Family tree Family tree Extend with brother and sisters and define a new predicted for related father(a,c). father(c,e). mother(b,c). mother(d,e). parent(A,B) :- father(A,B). parent(A,B) :- mother(A,B). ancestor(A,B) :- parent(A,B). ancestor (A,B) :- parent(A,C), ancestor (C, B).

Introduction to programming - class 9

Embed Size (px)

Citation preview

Page 1: Introduction to programming - class 9

Introduction to Programming – Class 9 – Prolog Paul Brebner

Logic Truth tables T/F for Negation, AND, OR, XOR.

(Illustrated on whiteboard)

Prolog: Facts and Rules

• Logic programming

– Program = facts and rules

– Facts = predicate(atom), predicate(atom1, atom2).

– Rules = predicate(Variable1, ...) :- predicate(Variable1, ...),

predicate(Variable, ...).

– All predicates of same name must be together in this version.

– Run program by asking a question:

• E.g. predicate(X)? Can get 0 or more answers

• Try simple examples

• E.g. Family tree

Family tree

Extend with brother and sisters and define a new predicted for related

father(a,c).

father(c,e).

mother(b,c).

mother(d,e).

parent(A,B) :- father(A,B).

parent(A,B) :- mother(A,B).

ancestor(A,B) :- parent(A,B).

ancestor (A,B) :- parent(A,C), ancestor (C, B).

Page 2: Introduction to programming - class 9

Online prolog http://www.compileonline.com/execute_prolog_online.php

:- initialization(main). main :- likes(X,Y), write(likes(X,Y)), nl, fail. main. % Facts and Rules % gnasher is a dog. % ginger is a dog. dog(gnasher). dog(ginger). % jill is a cat. cat(jill). cat(jack). % X likes Y if X is a dog and Y is a cat. likes(X, Y) :- dog(X), cat(Y). % X likes Y if X is a dog and Y is a dog. likes(X, Y) :- dog(X), dog(Y).

More complex rules rely on RECURSION. In Prolog there are no LOOPS (e.g. for or while loops).

In Maths factorial function (!) is best known example. 3! = 3 x 2 x 1 = 3 x 2! Oddly 0! = 1.

N! = N * (N-1)! In Prolog

:- initialization(main). main :- factorial(10, X), write(X). % factorial of 0 is defined as 1, the basecase. factorial(0,1). % compute FN, the factorial of N recursively factorial(N,FN) :- M is N-1, factorial(M,FM), FN is N * FM.

The Towers of Hanoi – when will the world end?! The puzzle was first publicized in the West by

the French mathematician Édouard Lucas in 1883. There is a story about an Indian temple in Kashi Vishwanath

which contains a large room with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests,

acting out the command of an ancient prophecy, have been moving these disks, in accordance with the

immutable rules of the Brahma, since that time. According to the legend, when the last move of the puzzle will

be completed, the world will end. If the legend were true, and if the priests were able to move disks at a rate

of one per second, using the smallest number of moves, it would take them 264−1 seconds or roughly

585 billion years[3] or 18,446,744,073,709,551,615 turns to finish, or about 127 times the current age of the

sun.

:- initialization(main). main :- move(3, left, right, centre). % move(N,X,Y,Z) means move N discs from pole X to pole Y, using pole Z as spare pole. move(1,X,Y,_) :- write('Move top disk from '), write(X), write(' to '), write(Y), nl. move(N,X,Y,Z) :- N>1, M is N-1, move(M,X,Z,Y),

Page 3: Introduction to programming - class 9

move(1,X,Y,_), move(M,Z,Y,X).

Predators – who is topDog? Try adding predicates and changing the rules for eating! Add a giant

herbivore?

:- initialization(main). main :- topDog(X), write(X), nl, fail. main. species(oink, pig). species(woof, dog). species(growl, dog). species(moo, cow). species(quack, duck). species(roar, tiger). species(purr, lion). carnivore(dog). carnivore(tiger). carnivore(lion). herbivore(pig). herbivore(duck). herbivore(cow). large(oink). large(moo). large(roar). large(purr). large(growl). small(woof). small(quack). smaller(A, B) :- small(A), large(B). sameSpecies(A, B) :- species(A, X), species(B, X). eats(A, B) :- species(A, X), carnivore(X), species(B, Y), herbivore(Y). % larger carnivores eat smaller ones, unless same species eats(A, B) :- species(A, X), carnivore(X), species(B, Y), carnivore(Y), smaller(B,A), not(sameSpecies(A,B)). topDog(A) :- species(A, SA), not(eats(X,A)).

Note: In this version of Prolog not is \+

A Maze, add some treasure?

node(a).

node(b).

node(c).

Page 4: Introduction to programming - class 9

node(d).

node(e).

node(f).

left(a,d).

left(b,e).

right(a,b).

right(b,f).

ahead(b,c).

% bog(b).

pit(d).

monster(b).

isAgile.

hasSword.

danger(X) :- pit(X).

danger(X) :- monster(X).

% safe if no danger, or can solve danger

jumpable(X) :- bog(X).

jumpable(X) :- pit(X).

safe(X) :- not( danger(X) ).

safe(X) :- jumpable(X), isAgile, print(jump), nl.

safe(X) :- monster(X), hasSword, print(kill), nl.

connected(A,B) :- left(A,B), print(left), nl, print(A), nl, print(B), nl.

connected(A,B) :- ahead(A,B), print(ahead), nl, print(A), nl, print(B), nl.

connected(A,B) :- right(A,B), print(right), nl, print(A), nl, print(B), nl.

path(A,B) :- connected(A,B), safe(B).

path(A,B) :- connected(A,C), safe(C), path(C,B).

Page 5: Introduction to programming - class 9