Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ......
Transcript of Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ......
Prolog
Junli Tao [email protected]
Rm 321-723, Tamaki Campus
(Wed. 1:00pm-2:00pm)
Partially based on lecture node ‘prolog.pdf’ from Pat Langley
Outline
• Prolog Language
• Prolog Syntax
• Interpretation
• List Structure
• Parsing language sample
Prolog Language
• Logic Programming Language
• Developed in 1972 for natural language processing
• Successful applications
– Generating quotes for airline fares (Cendant Travel)
– Dialogue-based checklist navigator (Space Station)
– …
Prolog Overview
• Any Prolog program is made up of two main components: – Rules that specify general relations among
predicates
– Facts that specify concrete instances of predicates
• To call on a Prolog program, the user provides it with Queries – The result is a set of predicate instances that
satisfy the query
Facts
• A fact is a predicate followed by a dot parent(abe, bob). male(abe). parent(ann, bob). female(ann). parent(X, bob). female(Ann).
• Strings starting with a capital letter or an underscore are taken as Variables – X, Elephant, _4711, X_1_2, MyVariable – _
• anonymous variable • used when the value of a variable is of no particular interest
Rules
• A rule consists of a head and a body
– Head: a single nonnegated predicate
– Body: a set of predicates separated by commas
• Head and body are separated with :- (if)
• Terminated by a dot .
father(X, Y) :- parent(X, Y), male(X). brother(X, Z) :- parent(Y, X), parent(Y, Z), male(X). only_child(X, Y) :- parent(Y, X), not((parent(Y, Z), X \= Z)).
Queries
• A user runs a Prolog program by providing a query stated as one or more predicates with (partially) specified arguments
– ?- parent(abe, bob).
– ?- parent(P, bob).
– ?- parent(A, B), male(A), male(B).
Interpretation
• Pattern matching
– Simple Unification:
match to facts directly
– Complex Unification:
exist a set of simple unifications with consistent bindings
• Rule chaining
– Not all Prolog queries use predicates for which facts are present
Examples
Program:
Queries: ?- bigger(donkey, dog).
?- bigger(monkey, elephant).
?- bigger(elephant, monkey).
Program:
bigger(X, Y) :- bigger(X, Z), bigger(Z, Y).
Queries: ?- bigger(elephant, monkey).
?- bigger(X, donkey).
Answer Query
• ?- is_bigger(X, donkey).
– Y = donkey
– Check bigger(X, donkey)
– X=horse
OR
– Check is_bigger(Z, donkey), Z=hourse
– Check bigger(X, hourse)
– X=elephant
Install Prolog (swi)
• Download from http://www.swiprolog.org/download/stable
• Install with default settings
– File extension ‘.pl’
– Directory ‘document/prolog’
Practice with Prolog
• Load source file with – consult(‘elephant.pl’).
– from menu File -> consult…
• Provide query – ?- is_bigger(X, donkey).
• Press Return to obtain result(s): – X = horse
– Press semicolon (;) to obtain more result(s)
– Press Return to stop current query
List Structure
• Contained in square brackets with the elements being separated by commas
– [elephant, horse, donkey, dog]
– [] %empty list
– [elephant, [], X, parent(X, tom), [a, b, c]]
List Structure: head|tail
• The first element of a list is called its head and the remaining list is called the tail
– ?- [1, 2, 3, 4, 5] = [Head | Tail].
Head = 1
Tail = [2, 3, 4, 5]
• Retrieve a element from a given list
– ?- [quod, licet, jovi, non, licet, bovi] = [_, X | _].
– X = licet
Exercise
• [a,d,z,c] and [H|T]
• [apple,pear,grape] and [A,pear|Rest]
• [a|Rest] and [a,b,c]
• [a,[]] and [A,B|Rest]
• [One] and [two|[]]
• [one] and [Two]
• [a,b,X] and [a,b,c,d]
Exercise
• [a,d,z,c] and [H|T] Y
• [apple,pear,grape] and [A,pear|Rest] Y
• [a|Rest] and [a,b,c] Y
• [a,[]] and [A,B|Rest] Y
• [One] and [two|[]] Y
• [one] and [Two] Y
• [a,b,X] and [a,b,c,d] N
List Structure: Built-in Predicates
• Concatenate two lists (or append) – ?- concat_lists([1, 2, 3], [d, e, f, g], X). – X = [1, 2, 3, d, e, f, g]
• Decompose a list – ?- concat_lists(X, Y, [a, b, c, d]). – X = [] Y = [a, b, c, d] ; – X = [a] Y = [b, c, d] ; – X = [a, b] Y = [c, d] ; – X = [a, b, c] Y = [d] ; – X = [a, b, c, d] Y = [] ;
List Structure: Built-in Predicates
• length/2: The second argument is matched with the length of the list in the first argument – ?- length([elephant, [], [1, 2, 3, 4]], Length).
– Length = 3
• member/2: The goal member(Elem, List) will succeed, if the term Elem can be matched with one of the members of the list List – ?- member(dog, [elephant, horse, donkey, dog,
monkey]).
List Structure: Built-in Predicates
• reverse/2: This predicate can be used to reverse the order of elements in a list – ?- reverse([1, 2, 3, 4, 5], X).
– X = [5, 4, 3, 2, 1]
• select/3: Given a list in the second argument and an element of that list in the first, this predicate will match the third argument with the remainder of that list – ?- select(bird, [mouse, bird, jellyfish, zebra], X).
– X = [mouse, jellyfish, zebra]
Parsing Sentences in Prolog
• Queries
– ?- s([the, cat, heard, a, mouse], [ ]).
– ?- s([the, cat, that, heard, a, mouse, saw, the, dog], [ ]).