Docsity

Understanding Prolog: A Declarative Programming Language, Slides of Advanced Computer Programming

Partial preview of the text

Download Understanding Prolog: A Declarative Programming Language and more Slides Advanced Computer Programming in PDF only on Docsity!

A First Look At Prolog

Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Outline

 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

Constants

 Integer constants: 123  Real constants: 1.  Atoms:

  • A lowercase letter followed by any number of additional letters, digits or underscores: fred
  • A sequence of non-alphanumeric characters: ***** ,. , = , @#$
  • Plus a few special atoms: []

Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Atoms Are Not Variables

 An atom can look like an ML or Java variable:

  • i , size , length  But an atom is not a variable; it is not bound to anything, never equal to anything else  Think of atoms as being more like string constants: "i" , "size" , "length"

Chapter Nineteen Modern Programming Languages, 2nd ed. (^) Docsity.com 5

Compound Terms

 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

Terms

 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

The Prolog Database

 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

Example

 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

SWI-Prolog

 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

The consult Predicate

 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

Final Period

 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

Queries With Variables

 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

Multiple Solutions

 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

Multiple Solutions

 … 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