28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

24
28-4-2008 P-Phunck - Prolog 1 Introduction to Prolog Brian Paden

Transcript of 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

Page 1: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 1

Introduction to PrologBrian Paden

Page 2: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 2

History

Creators: Alain Colmerauer & Phillipe Roussel University of Aix-Marseille 1972

Robert Kowalski University of Edinburgh

Named by: Philippe Roussel programmation en logique

First compiler: David H. D. Warren

Page 3: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 3

More History

1984 – Began working on standard 1987 – ISO Standard Group Formed 1995 – ISO Standard published

Most current New version expected “by early 2000”

Page 4: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 4

Predicate Logic

First order predicate calculus Propositions

Atomic propositions & compound terms Resolution

Inference rule Horn clauses

Single atomic proposition on left side Empty left side

Page 5: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 5

Data Types

Atom Letters or numbers 'x', 'longConstant17',

'“Var”' Variable

Start with uppercase 'X', 'NotanAtom' Anonymous variable - '_'

Fact 'doesntKnow( “Prolog”, brian ).'

Page 6: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 6

Variables

Two states Instantiated

Variable has a set value Uninstantiated

Has not been assigned a value

Closed-world assumption Only true if it can be proven

Page 7: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 7

Data Structures

Compound term Functor and arguments 'weird( prolog, yes ).'

Queries '?- good( presentation, brian )' Only returns 'yes' or 'no'

Conjunction Multiply queries and facts

Page 8: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 8

Special Compound Terms List

Head First element in list

Tail List containing everything else in list except the head Last element is empty list - '[]'

'[]', '.' - '[a,[b,[c]]]' == 'a.b.c.[]' String

Special list – all elements are atoms '[“This”,is,a,string,in,”Prolog”]'

Page 9: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 9

More Lists

Special list operators '[]' - Empty list '[X|Y]' – X is head, Y is tail '””' - convert list to list containing ascii values

Recursive search for element of listmember(X,[X|_]).

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

?- member(c,[a,b,c,d,e,f]).yes

Page 10: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 10

Built in Operators

Compound construction ',' - conjunction, ';' - disjunction

Comparison 'X = Y' – equality, 'X \= 2' – inequality 'X == Y' – strict equality, 'X \== 2' – strict inequality 'X < Y', 'X =< Y', - less than, less than equal to 'X > Y', 'X >= Y' – greater than, greater than equal to

Arithmetic 'X + Y', 'X - Y', 'X * Y', 'X / Y', 'X mod Y'

Page 11: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 11

Arithmetic

Operators that stand out '\' - not, '\=' - not equal '=<' - less equal, '>=' - greater equal

Perform arithmetic 'is'

' Y = Y/X' – tests equality 'Y is Y/X' – assigns numeric value to Y Right hand side of 'is' must be algebraic

Page 12: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 12

Looping

No iteration, only recursion

factorial(0,1). factorial(N,F) :-

N>0, N1 is N-1,

factorial(N1,F1), F is N * F1.

?- factorial(3,X).X=6

Page 13: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 13

I/O Special file

'user' Output

'tell(X).' - creates/opens file 'telling(X).' - gets open file name 'told' – closes file

Input see, seeing, seen

Misc 'tab(X)', 'nl'

Page 14: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 14

Backtracking

User query Searches database for fact declaring it true

Matching fact (head rule) found Mark place in database, move on to next goal

No matching fact found Attempt to re-satisfy goal

Undo last change and try again Cut

Shortcuts backtracking '!' symbol

Page 15: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 15

Backtracking in Action

father(mary,george).father(john,george).father(sue,harry).

father(george,edward).father(X) :- father(_,X).

Database

Query

?- father(Y).

Result

Y=george;Y=george;Y=harry;

Y=edward;no

Page 16: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 16

Lists and Backtracking

reverse([],[]).reverse([Head|Tail],List) :-

reverse(Tail,Result),append(Result,[Head],List).

trace.?- reverse([a,b,c],Q).

Page 17: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 17

Reverse Example(1) 1 Call: reverse([a,b,c],_6)?(2) 2 Call: reverse([b,c],_65636)?(3) 3 Call: reverse([c],_65646)?(4) 4 Call: reverse([],_65656)?(4) 4 Call: Exit: reverse([],[])(5) 4 Call: append([],[c],_65646)?(5) 4 Exit: append([],[c],[c])(3) 3 Exit: reverse([c],[c])

(6) 3 Call: append([c],[b],_65636)?(7) 4 Call: append([],[b],_25)?(7) 4 Exit: append([],[b],[b])

(6) 3 Exit: append([c],[b],[c,b])(2) 2 Exit: reverse([b,c],[c,b)(8) 2 Call: append([c,b],[a],_6)?(9) 3 Call: append([b],[a],_32)?(10) 4 Call: append([],[a],_39)?(10) 4 Exit: append([],[a],[a])(9) 3 Exit: append([b],[a],[b,a])

(8) 2 Exit: append([c,b],[a],[c,b,a])(1) Exit: reverse([a,b,c],[c,b,a])

Page 18: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 18

How to Cut

sum_to(1,1) :- !.

sum_to(N,Res) :-N1 is N – 1,

sum_to(N1,Res),Res is Res + N.

Database

Query

?- sum_to(5,X).

Result

X = 15;no

Page 19: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 19

Useful Built in Predicates

Conditional 'true', 'fail' – Always their namesakes 'atom(X)', 'integer(X)' – true if X is an atom or int 'atomic(X)' – X is not a variable 'asserta(X)' – adds clause X to start of database 'assertz(X)' – adds clause to end of database 'X =.. L' - Univ

X is variable L is list where head is functor and tail is arguments

Page 20: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 20

Example: Binary Tree

lookup(H,w(H,G,_,__,G) :- !.

lookup(H,w(H1,_,Before,_),G) :-

aless(H,H1),lookup(H,Before,G).

lookup(H,w(H1,_,_,After),G) :-

not(aless(H,H1)),lookup(H,After,G).

?- lookup(key,X,582),lookup(val,X,356).

Implementation Usage

Page 21: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 21

Example: Quicksortpartition([], _, [], []).

partition([X|Xs], Pivot, Smalls, Bigs) :- ( X @< Pivot ->

Smalls = [X|Rest], partition(Xs, Pivot, Rest, Bigs)

; Bigs = [X|Rest], partition(Xs, Pivot, Smalls, Rest)

).

quicksort([]) --> [].quicksort([X|Xs]) -->

{ partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), [X],

quicksort(Bigger).

Page 22: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 22

Example: Turing Machineturing(Tape0, Tape) :-

perform(q0, [], Ls, Tape0, Rs), reverse(Ls, Ls1),

append(Ls1, Rs, Tape).

perform(qf, Ls, Ls, Rs, Rs) :- !.perform(Q0, Ls0, Ls, Rs0, Rs) :- symbol(Rs0, Sym, RsRest),

once(rule(Q0, Sym, Q1, NewSym, Action)), action(Action, Ls0, Ls1, [NewSym|RsRest], Rs1),

perform(Q1, Ls1, Ls, Rs1, Rs).

symbol([], b, []).symbol([Sym|Rs], Sym, Rs).

action(left, Ls0, Ls, Rs0, Rs) :- left(Ls0, Ls, Rs0, Rs).action(stay, Ls, Ls, Rs, Rs).

action(right, Ls0, [Sym|Ls0], [Sym|Rs], Rs).

left([], [], Rs0, [b|Rs0]).left([L|Ls], Ls, Rs, [L|Rs]).

Page 23: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 23

Compilers

ISO Compliant GNU Prolog

http://www.gprolog.org/ SWI-Prolig

http://www.swi-prolog.org/ YAP

http://www.ncc.up.pt/yap/

Visual Prolog (Turbo Prolog) http://www.visual-prolog.com/

Page 24: 28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

28-4-2008 P-Phunck - Prolog 24

Sources

Clocksin, W.F and Mellish, C.S. Programming in Prolog. Berlin. Springer-Verlag: 1984

Sebesta, Robert W. Concepts of Programming Languages: Eighth Edition. 2007

http://en.wikipedia.org/wiki/Prolog