prolog - university of auckland · prolog overview •any prolog program is made up of two main ......

20
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

Upload: phungkien

Post on 04-Apr-2019

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 2: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

Outline

• Prolog Language

• Prolog Syntax

• Interpretation

• List Structure

• Parsing language sample

Page 3: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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)

– …

Page 4: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 5: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 6: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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)).

Page 7: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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).

Page 8: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 9: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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).

Page 10: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 11: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

Install Prolog (swi)

• Download from http://www.swiprolog.org/download/stable

• Install with default settings

– File extension ‘.pl’

– Directory ‘document/prolog’

Page 12: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 13: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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]]

Page 14: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 15: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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]

Page 16: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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

Page 17: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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 = [] ;

Page 18: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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]).

Page 19: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

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]

Page 20: Prolog - University of Auckland · Prolog Overview •Any Prolog program is made up of two main ... Install Prolog (swi) ... –from menu File -> consult

Parsing Sentences in Prolog

• Queries

– ?- s([the, cat, heard, a, mouse], [ ]).

– ?- s([the, cat, that, heard, a, mouse, saw, the, dog], [ ]).