You are on page 1of 20

Programming Language

Instructor: Dr. Einollah Khanjari By: Omid Shirkhorshidi


The slides included information was taken from the Site Wikipedia And Books: 1) Learn Prolog Now!, Patrick Blackburn, Johan Bos, Kristina Striegnitz, 2006 2) PROLOG Programming for Artificial Intelligence, William F. Clocksin, Christopher S., 2000 3) Programming in Prolog: Using the ISO Standard. Springer, 5th ed., 2003

Spring 2011

1. 2. 3. 4. 5. 6. 7. 8.

History Of Prolog Language A Brief Identification Basic Elements and How to Program? Syntax and semantics & Some Examples Usages Why Prolog? Criticisms About this language References

History Of Prolog Language


Creator :
Alain Colmerauer with Philippe Roussel

Date Of Creation :
Around 1972

Based On Past Works Of :


Robert Kowalski Alain Colmerauer Philippe Roussel

Prolog Is An Abbreviation Of:


programmation en logique (French for programming in logic)

Robert Kowalski

Brief Identification
Prolog is a general purpose logic programming language associated with artificial intelligence
and computational linguistics. There is some Useful Information :

1. Paradigms : Logic Programming ( It should be mentioned here that there are many extensions of Prolog with other Paradigms features)

2. Some Features Of Prolog : Declarative , Reflective , Rule-based & High-level interactive language.

Brief Identification
3. Major implementations (Extensions) :

HiLog and Prolog extend Prolog with higher-order programming features. Logtalk and Oblog are some prolog extension with Object Orientation features. SWI-prolog , Visual-prolog and B-Prolog are some Prolog systems that
provide a graphics library.
Some Prolog implementations, notably SWI-Prolog, support server-side web programming with support for web protocols, HTML and XML.
4. Usual File Extensions : .PL & .PRO

5. Web-site : ????????????????????????????????

What is a Declarative Lang.?


Programming languages are of two kinds:

Procedural : (BASIC, ForTran, C++, Pascal, Java); Declarative : (LISP, Prolog, ML).
In procedural programming, we tell the computer In declarative programming, we tell the computer

how to solve a problem. what problem we want solved.

(However, in Prolog, we are often forced to give clues as to the solution method).

Basic Elements of Prolog:


Our program is a database of facts and rules.
Some are always true (facts):

father( john, jim).

Some are dependent on others being true (rules):

parent( Person1, Person2 ) :father( Person1, Person2 ).


To run a program, we ask questions about the

database. (Queries)

Basic Elements of Prolog:

Example Database In English:


John is the father of Jim. Jane is the mother of Jim. Jack is the father of John.
Person 1 is a parent of Person 2 if Person 1 is the father of Person 2 or Person 1 is the mother of Person 2.

Example Database In Prolog:


father( john, jim ). mother( jane, jim ). father( jack, john ).
parent( Person1, Person2 ) :father( Person1, Person2 ). parent( Person1, Person2 ) :mother( Person1, Person2 ).

Person 1 is a grandparent of Person 2 if some Person 3 is a parent of Person 2 and Person 1 is a parent of Person 3.

grandparent( Person1, Person2 ) :parent( Person3, Person2 ), parent( Person1, Person3 ).

Example questions In English:


Who is Jim's father? Is Jane the mother of Fred? Is Jane the mother of Jim? Does Jack have a grandchild?

Example questions In Prolog:


?- father( Who, jim ). ?- mother( jane, fred ). ?- mother( jane, jim ). ?- grandparent( jack, _ ).

.pl files contain lists of clauses.


Clauses can be either facts or rules.

The Prolog comment characters: Single line comments: (%) % This is a comment This is not a comment, but an error

Multiple line comments: ( /* */ ) /* This is a multi-line comment which must be closed with a */

Prolog's single data type is the term. Terms are either atoms, numbers, variables or compound terms.

1)An atom is a general-purpose name with no inherent meaning. Examples of


atoms include x, blue, 'Taco', and 'some atom'.

2)Numbers can be floats or integers.

3)Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper-case letter or underscore. Variables closely resemble variables in logic in that they are placeholders for arbitrary terms.

4)A compound term is composed of an atom called a "functor" and a number of "arguments", which are again terms.

Examples of compound terms are truck_year('Mazda', 1986) and 'Person_Friends'(zelda,[tom,jim]).

A rule is of the form: Head :- Body.


and is read as "Head is true if Body is true".

Given the beside fact, one can ask: is tom a cat? ?- cat(tom). Yes what things are cats? ?- cat(X). X = tom

An example of a fact is: cat(tom).

which is equivalent to the rule: cat(tom) :- true. An example of a rule is: animal(X):- cat(X).

If we add that rule and ask


what things are animals? ?- animal(X). X = tom

greetings:write(What is your name?), nl, read(X), write(Hello ), write(X). Prolog Code

| ?- greetings. What is your name? |: tim. Hello tim X = tim ? yes Terminal

mother_child(trude, sally). father_child(tom, sally). Prolog Code father_child(tom, erica). father_child(mike, tom). sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y). parent_child(X, Y) :- mother_child(X, Y).
This results in the following query being evaluated as true: ?- sibling(sally, erica). Terminal Yes

Recursive Definitions
For all X and Y X is in Ys food chain if Y eats X

or Y eats some Z and X is in Zs food chain: food_chain(X,Y) :- eats(Y,X). food_chain(X,Y) :- eats(Y,Z), food_chain(X,Z). Afterwards we can ask: ?-food_chain(X,rat). X=salmon ; X=worm Or: ?-food_chain(worm,X). X=rat ; X=bear

Hello world !
As an example of a query:
?- write('Hello world!'), nl. Hello world! true. ?-

Artificial intelligence or any Intelligent systems Complicated knowledge databases Natural language processing Logic data analysis intelligent data base retrieval natural language understanding expert systems specification language machine learning robot planning automated reasoning problem solving

Prolog is interactive. It comes with a toplevel interpreter: a command interface that you can type code at and get it executed immediately. Prolog is fast. Some extensions like SWI-Prolog are free, open-source, and very well maintained. Unlike many other programming languages, Prolog is declarative

Complicated syntax
Difficult to understand programs at first sight Programming in the large is considered to be complicated because not all Prolog compilers support modules, and there are compatibility problems between the module systems of the major Prolog compilers. Portability of Prolog code across implementations has also been a problem

The slides included information was taken from the Site Wikipedia And Books: 1) Learn Prolog Now!, Patrick Blackburn, Johan Bos, Kristina Striegnitz, 2006

2) PROLOG Programming for Artificial Intelligence, William F. Clocksin, Christopher S., 2000
3) Programming in Prolog: Using the ISO Standard. Springer, 5th ed., 2003

You might also like