Docsity

PROLOG: A Declarative Programming Language Based on Predicate Calculus, Exercises of Programming Languages

Toggle sidebar
bg1
PROLOG - PROgramming in LOGic(Lecture 22-26) VU
PROLOG - PROgramming in LOGic(Lecture 22-26)
An Introduction
PROLOG stands for PROgramming in LOGic and was design in 1975 by Phillippe
Roussell. It is a declarative programming language and is based upon Predicate Calculus.
A program in PROLOG is written by defining predicates and rules and it has a built-in
inference mechanism. Unfortunately, there is no effective standardization available.
PROLOG Paradigm
As mentioned earlier, PROLOG draws inferences from facts and rules. It is a declarative
language in which a programmer only specifies facts and logical relationships. It is non-
procedural. That is we only state "what" is to be done and do not specify "how" to do it.
That is, we do not specify the algorithm. The language just executes the specification. In
other words, program execution is carried out as a theorem proving exercise. It is similar
to SQL used in databases with automated search and the ability to follow general rules.
One of the main features of this language is its ability to handle and process symbols.
Hence it is used heavily in AI related applications. It is an interactive (hybrid
compiled/interpreted) language and its applications include expert systems, artificial
intelligence, natural language understanding, logical puzzles and games.
PROLOG Programming
PROLOG programming follows the following steps:
Declaring some facts about objects and their relationships
Defining some rules about objects and their relationships
Asking questions about objects and their relationships
The PROLOG Programmer loads facts and rules into the database and makes queries to
the database to see if a fact is in the database or can be implied from the facts and rules
therein.
This is depicted in the following diagram.
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download PROLOG: A Declarative Programming Language Based on Predicate Calculus and more Exercises Programming Languages in PDF only on Docsity!

PROLOG - PRO gramming in LOG ic( Lecture 22-26 )

An Introduction

PROLOG stands for PROgramming in LOGic and was design in 1975 by Phillippe Roussell. It is a declarative programming language and is based upon Predicate Calculus. A program in PROLOG is written by defining predicates and rules and it has a built-in inference mechanism. Unfortunately, there is no effective standardization available.

PROLOG Paradigm

As mentioned earlier, PROLOG draws inferences from facts and rules. It is a declarative language in which a programmer only specifies facts and logical relationships. It is non- procedural. That is we only state "what" is to be done and do not specify "how" to do it. That is, we do not specify the algorithm. The language just executes the specification. In other words, program execution is carried out as a theorem proving exercise. It is similar to SQL used in databases with automated search and the ability to follow general rules.

One of the main features of this language is its ability to handle and process symbols. Hence it is used heavily in AI related applications. It is an interactive (hybrid compiled/interpreted) language and its applications include expert systems, artificial intelligence, natural language understanding, logical puzzles and games.

PROLOG Programming

PROLOG programming follows the following steps:  Declaring some facts about objects and their relationships  Defining some rules about objects and their relationships  Asking questions about objects and their relationships

The PROLOG Programmer loads facts and rules into the database and makes queries to the database to see if a fact is in the database or can be implied from the facts and rules therein.

This is depicted in the following diagram.

Facts

Facts are used to represent unchanging information about objects and their relationships. Only facts in the PROLOG database can be used for problem solving. A programmer inserts facts into the database by typing the facts into a file and loading (consulting) the file into a running PROLOG system.

Queries

Queries are used to retrieve information from the database. A query is a pattern that PROLOG is asked to match against the database and has the syntax of a compound query. It may contain variables. A query will cause PROLOG to look at the database, try to find a match for the query pattern, execute the body of the matching head, and return an answer.

Logic Programming

Logic programming is a form of declarative programming. In this form, a program is a collection of axioms where each axiom is a Horn clause of the form: H :- B1, B2, ..., Bn. where H is the head term and Bi are the body terms , meaning H is true if all Bi are true. A user of the program states a goal (a theorem) to be proven. The logic programming system attempts to find axioms using inference steps that imply the goal (theorem) is true.

Resolution

To deduce a goal (theorem), the logic programming system searches axioms and combines sub-goals. For this purpose we may apply forward (from fact to goal) or backward (from goal to fact) chaining. For example, given the axioms:

C :- A , B. D :- C.

Given that A and B are true, forward chaining (from fact to goal) deduces that C is true because C :- A , B and then D is true because D :- C.

Backward chaining (from goal to fact) finds that D can be proven if sub-goal C is true because D :- C. The system then deduces that the sub-goal is C is true because C :- A , B. Since the system could prove C it has proven D.

Prolog Uses backward chaining because it is more efficient than forward chaining for larger collections of axioms.

Examples

Let us look at some examples of facts, rules, and queries in Prolog.

Facts

PROLOG syntax

Constants

There are two types of constants in Prolog: atoms and numbers.

Atoms:  Alphanumeric atoms - alphabetic character sequence starting with a lower case letter. Examples: apple a1 apple_cart  Quoted atoms - sequence of characters surrounded by single quotes. Examples: „Apple‟ „hello world‟  Symbolic atoms - sequence of symbolic characters. Examples: & < > * - + >>  Special atoms. Examples:! ; [ ] {}

Numbers Numbers include integers and floating point numbers. Examples: 0 1 9821 -10 1.3 - 1.3E102.

Variable Names In Prolog, a variable is a sequence of alphanumeric characters beginning with an upper case letter or an underscore. Examples: Anything _var X _

Compound Terms (structures) A compound term is an atom followed by an argument list containing terms. The arguments are enclosed within brackets and separated by commas. Example: isa(dog, mammal)

Comments

In Prolog % is used to write a comment just like // in C++. That is after % everything till the end of the line is a comment.

Important points to note

 The names of all relationships and objects must begin with a lower case letter. For example studies, ali, programming  The relationship is written first and the objects are enclosed within parentheses and are written separated by commas. For example studies(ali, programming)  The full stop character „.‟ must come at the end of a fact.  Order is arbitrary but it should be consistent.

An Example Prolog Program

This program has three facts and one rule. The facts are stated as follows:

rainy(columbo). rainy(ayubia). cold(ayubia).

The rule is:

snowy(X) :- rainy(X), cold(X).

Some queries and responses:

?- snowy(ayubia). yes

?- snowy(columbo). no

?- snowy(lahore). No

?- snowy(C). C = ayubia

Because rainy(ayubia) and cold(ayubia) are sub-goals that are both true facts in the database, snowy(X) with X=columbo is a goal that fails, because cold(X) fails, triggering backtracking.

This inference mechanism based upon back tracking is depicted in the following diagram:

Predicates are declarations of relations or rules. They are just like function prototypes (declaration). A predicate may have zero or more arguments. Example of predicates are:

man(symbol) family() a_new_predicate (integer, char)

Clauses Clauses are definition(s) of Predicate sentence or phrase. It has two types: rules and facts. A rule is a function definition. It may have 0 or more conditions. A fact has all parameters as constants and it cannot contain any condition.

Example – Fact: brother(ahmed, chand).

Saying that ahmed and chand are brothers.

Example – Rule:

brother(X,Y):- man(X), man(Y), father(Z,Y), father(Z,X).

It says that two symbols, X and Y, are brothers if X is a man and Y is a man and their father is same.

Goal

Goal is the objective of the program. There can be only and exactly one instance of the goal in a program. It is the only tentative section of the program. It may be thought as the main() function of prolog. The truth value of the goal is the output of the program. Syntactically and semantically, it is just another clause. It may be empty, simple (one goal), or compound (sub-goals).

Examples

  1. brother(X,chand). % In this case the output will be the name of Chand‟s brother.
  2. brother(ahmed,chand).% The output will be true or false.
  3. brother(X,chand), father(X, Y). % The output will be Chand‟s and his children.

Prolog Variables

Prolog variable are actually constant placeholders and NOT really variables. That is, a value to a variable can be bound only once and then it cannot be changed. These variables are loosely typed. That is, type of the variable is not defined at compile time. As mentioned earlier, a variable name starts with capital letter or underscore. Here are some examples of variable names: