




































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





































































Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Terms Using a Prolog language system Rules The two faces of Prolog Operators Lists Negation and failure What Prolog is good for
Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 2
Integer constants: 123 Real constants: 1. Atoms:
Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 4
An atom can look like an ML or Java variable:
Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 5
An atom followed by a parenthesized, comma-separated list of one or more terms: x(y,z) , +(1,2) , .(1,[]) , parent(adam,seth) , x(Y,x(Y,Z)) A compound term can look like an ML function call: f(x,y) Again, this is misleading Think of them as structured data
Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 7
All Prolog programs and data are built from such terms Later, we will see that, for instance, +(1,2) is usually written as 1+ But these are not new kinds of terms, just abbreviations
Chapter Nineteen Modern Programming Languages, 2nd ed. 8
< term > ::= < constant > | < variable > | < compound-term > < constant > ::= < integer > | < real number > | < atom > < compound-term > ::= < atom > ( < termlist > ) < termlist > ::= < term > | < term > , < termlist >
Docsity.com
A Prolog language system maintains a collection of facts and rules of inference It is like an internal database that changes as the Prolog language system runs A Prolog program is just a set of data for this database The simplest kind of thing in the database is a fact : a term followed by a period
Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 10
A Prolog program of six facts Defining a predicate parent of arity 2 We would naturally interpret these as facts about families: Kim is the parent of Holly and so on
Chapter Nineteen Modern Programming Languages, 2nd ed. 11
parent(kim,holly). parent(margaret,kim). parent(margaret,kent). parent(esther,margaret). parent(herbert,margaret). parent(herbert,jean).
Docsity.com
Prompting for a query with ?- Normally interactive: get query, print result, repeat
Chapter Nineteen Modern Programming Languages, 2nd ed. 13
Welcome to SWI-Prolog … For help, use ?- help(Topic). or ?- apropos(Word). ?-
Docsity.com
Predefined predicate to read a program from a file into the database File relations (or relations.pl ) contains our parent facts
Chapter Nineteen Modern Programming Languages, 2nd ed. 14
?- consult(relations). % relations compiled 0.00 sec, 852 bytes true. ?-
Docsity.com
Queries can take multiple lines If you forget the final period, Prolog prompts for more input with |
Chapter Nineteen Modern Programming Languages, 2nd ed. 16
?- parent(margaret,kent) |. true. ?-
Docsity.com
Any term can appear as a query, including a term with variables The Prolog system shows the bindings necessary to prove the query
Chapter Nineteen Modern Programming Languages, 2nd ed. 17
?- parent(P,jean). P = herbert.
?- parent(P,esther). false.
Docsity.com
When the system finds a solution, it prints the binding it found If it could continue to search for additional solutions, it then prompts for input Hitting Enter makes it stop searching and print the final period…
Chapter Nineteen Modern Programming Languages, 2nd ed. 19
?- parent(Parent,Child). Parent = kim, Child = holly.
Docsity.com
… entering a semicolon makes it continue the search
As often as you do this, it will try to find another solution
In this case, there is one for every fact in the database
Chapter Nineteen Modern Programming Languages, 2nd ed. 20
?- parent(Parent,Child). Parent = kim, Child = holly ; Parent = margaret, Child = kim ; Parent = margaret, Child = kent ; Parent = esther, Child = margaret ; Parent = herbert, Child = margaret ; Parent = herbert, Child = jean.
Docsity.com