Prolog - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Prolog

Description:

X = susie. X = wine. no. Points to Consider. Successive user prompts ; cause the interpreter to ... friends(george,susie) yes. Quantifiers ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 27
Provided by: ricardo125
Category:
Tags: prolog | susie

less

Transcript and Presenter's Notes

Title: Prolog


1
Prolog
  • Overview
  • Syntax
  • Mechanism
  • Summary

2
Overview
  • PROLOG Programming with Logic
  • Uses predicate (first-order) calculus
  • History
  • Roots J.A. Robinson (1965) created algorithms
  • for resolution refutation.

3
Resolution
P V Q P V A
Q V A
star(A75) V galaxy(andromeda)
star(X) V
bright(X)
galaxy(andromeda) V bright(A75) T X/A75
4
History
  • First Prolog Program written in France early
    1970s
  • as part of a project in natural language
    understanding.
  • Theoretical background defined by Kowalski,
    Hayes,
  • and others.
  • Major development of the language done from
    1975-1979
  • at the dept. of artificial intelligence of the
    University of
  • Edinburgh.

5
Applications
  • Automatic code generation
  • Program verification
  • Design of high-level specification languages
  • Example Mathematica

6
Examples
Everyone likes Susie likes(X,susie) George
likes Kate and George likes Susie likes(george,ka
te) , likes(george,susie) George likes Kate or
George likes Susie likes(george,kate)
likes(george,susie) George likes Susie if
George does not like Kate likes(george,susie) -
not(likes(george,kate))
7
Prolog
  • Overview
  • Syntax
  • Mechanism
  • Summary

8
Syntax
  • There are many dialects of Prolog.
  • We will use C-Prolog (Clocksin and Mellish 1984)
  • Prolog uses Predicate Calculus as its language.

9
Prolog
  • Overview
  • Syntax
  • Mechanism
  • Summary

10
Prologs Mechanism
  • A Prolog program is a set of specifications in
    predicate
  • calculus.
  • The specification is known as the database of
    the system.
  • Prolog is an interactive language (the user
    enters queries
  • in response to a prompt).

11
Example 1
likes(george,kate). likes(george,susie). likes(geo
rge,wine). likes(susie,wine). likes(kate,gin). lik
es(kate,susie). ?- likes(george,kate) yes ?-
likes(george,beer) no
12
Example 2
likes(george,kate). likes(george,susie). likes(geo
rge,wine). ?- likes(george,X) X kate X
susie X wine no
13
Points to Consider
  • Successive user prompts cause the
    interpreter to
  • return all terms that can be substituted for X.
  • They are returned in the order found.
  • ORDER IS IMPORTANT
  • PROLOG adopts the closed-world assumption
  • All knowledge of the world is present in the
    database.
  • If a term is not in the database, assume it is
    false.

14
Defining Rules
To define a rule use logical implication -
  • Example
  • friends(X,Y) - likes(X,Z), likes(Y,Z).
  • Only one predicate is allowed on the left of the
    implication
  • The predicate on the left cannot be negated
  • This is Horn Clause Logic

15
Example Rule
likes(george,kate). likes(george,susie). likes(geo
rge,wine). likes(susie,wine). likes(kate,gin). lik
es(kate,susie). friends(X,Y) - likes(X,Z),
likes(Y,Z). ?- friends(george,susie) yes
16
Quantifiers
  • When a variable appears in the specification of a
    database,
  • the variable is universally quantified. Example
  • likes(susie,Y) Susie likes everyone
  • For the existentially quantifier one may do two
    things
  • Enter the value directly into the database
  • likes(george,Z) becomes likes(george,wine)
  • b. Query the interpreter
  • ?- likes(george,Z) returns a value for Z
    if one exists.

17
Recursion in Prolog
Recursion is the primary control structure in
Prolog. Lets look at an example with
lists Elements in lists are enclosed by
brackets 1,2,3,4 george,kate,allen,amy,d
on,pat
18
lists
The first element of a list can be separated from
the tail using operator Example Match the
list tom,dick,harry,fred to XY
then X tom and Y dick,harry,fred X,YZ
then X tom, Y dick, and Z
harry,fred V,W,X,Y,ZU will not
match tom,Xharry,fred gives X dick
19
List membership
We want to write a function member that works as
follows ?- member(a,a,b,c,d,e) yes ?-
member(a,1,2,3,4) no ?- member(X,a,b,c) X
a X b X c no
20
Function membership
Define two predicates member(X,XT). member(X,
YT) - member(X,T). Trace the following
call ?- member(c,a,b,c) yes
21
Function membership
A more elegant definition uses anonymous
variables member(X,X,_). member(X,_T) -
member(X,T). The symbol _ indicates that the
contents of that variable is unimportant.
22
Function write list
writelist(). writelist(HT) - write(H), nl,
writelist(T). A reverse write of a list is as
follows reverse_writelist(). reverse_writelist
(HT) - reverse_writelist(T), write(H), nl.
23
Other functions
?- assertz(likes(david,sarah)) Adds this
predicate to the database. To remove a
predicate p use ?- retract(p)
24
Other functions
To download a database of sentences ?-
consult(myfile) yes Or ?- myfile To read and
write from console ?- read(X) ?- write(X)
25
Prolog
  • Overview
  • Syntax
  • Mechanism
  • Summary

26
Summary
  • There are many dialects of Prolog we will use
  • C-Prolog (Clocksin and Mellish 1984). Prolog
    uses
  • Predicate Calculus as its language.
  • A Prolog program is a set of specifications in
    predicate
  • calculus. The specification is known as the
    database
  • of the system.
  • Prolog is an interactive language (the user
    enters queries
  • in response to a prompt).
  • Recursion is the primary control structure in
    Prolog.
  • PROLOG adopts the closed-world assumption
  • All knowledge of the world is present in the
    database.
  • If a term is not in the database assume it is
    false.
Write a Comment
User Comments (0)
About PowerShow.com