Getting Started with Prolog

advertisement

Getting Started with Visual

Prolog

LabLecture # 2

Lecturer : Sheriff Nafisa

TA : Mubarakah Otbi, Duaa al Ofi , Huda al Hakami

Lecture Contents

Getting Started with Visual Prolog 5.2.

PROgramming in LOGic

Sentences: Facts and Rules

Queries

Variables: General Sentences

From Natural Language to Prolog Programs

Clauses (Facts and Rules)

Predicates (Relations)

Variables (General Clauses)

Goals (Queries)

Comments

Matching

Getting Started with Prolog

 We will use Visual Prolog 5.2 Personal Edition .

 You can install the executable file of the VP from teachers websites or from Labs.

 In the following we will show you how to test goals and build a small Windows application in

Visual Prolog 5.2

.

Visual Prolog 5.2 Main Window

Opening an Editor Window

 To create a new edit window, you can use the menu command File | New . This will bring up a new editor window with the title

"NONAME".

 To check, that your system is set up properly, you should try to type in the following text in the window:

GOAL write("Hello world"),nl.

 This is what is called a GOAL in the Prolog terminology. To execute the GOAL, you should activate the menu item Project | Test

Goal . If your system is installed properly, your screen will look like the following:

Running and Testing a Program

Error Handling

If you, like all programmers do, happen to make some errors in your program, the

Visual Prolog system will display an error window, which contains a list of errors.

You can double click on one of these errors to come to the position of the error in the source text.

What is Prolog ?

 Prolog is PROgramming in LOGic.

 A computer language designed in Europe to support natural language processing.

 It was created by Alain Colmerauer and

Robert Kowalski around 1972 as an alternative to the American-dominated Lisp programming languages.

What is Prolog (Con.)

 Prolog is based on predicate logic.

 Prolog includes an inference engine, which is a process for reasoning logically about information. The inference engine includes a pattern matcher, which retrieves stored

(known) information by matching answers to questions.

 One important feature of Prolog is dealing with alternatives and find all possible solutions rather than only one. Instead of just proceeding from the beginning of the program to the end, Prolog can actually back up and look for more than one way of solving each part of the problem.

Sentences: Facts and Rules

A Prolog programmer defines objects and relations , then defines rules about when these relations are true. For example, the sentence

Bill likes dogs.

shows a relation between the objects Bill and dogs ; the relation is likes . Here is a rule that defines when the sentence Bill likes dogs is true:

Bill likes dogs if the dogs are nice.

Facts: What Is Known

 In Prolog, a relation between objects is called a predicate .

 A fact consists of the relation name followed by the object or objects (enclosed in

Facts in Prolog

The fact ends with a period (.).

Bill likes Cindy.

likes(bill, cindy).

Cindy likes Bill.

likes(cindy, bill).

Bill likes dogs.

likes(bill, dogs).

Facts: What Is Known

Facts can also express properties of objects as well as relations.

Facts in Prolog Facts in natural language

Kermit is green

Caitlin is a girl green(kermit).

girl(caitlin).

Rules:

What You Can Infer from Given Facts

Rules enable you to infer facts from other facts.

Another way to say this is that a rule, as conclusions is a conclusion that is known to be true if one or more other conclusions or facts are found to be true.

Here are some rules concerning a "likes" relation:

Cindy likes everything that Bill likes.

Caitlin likes everything that is green.

Given these rules, you can infer from the previous facts some of the things that Cindy and Caitlin like:

Cindy likes Cindy.

Caitlin likes Kermit.

Rules:

What You Can Infer from Given Facts (Con.)

To encode these same rules into Prolog, you only need to change the syntax a little, like this: likes(cindy, Something):- likes(bill, Something).

likes(caitlin, Something):- green(Something).

The :- symbol is simply pronounced "if", and serves to separate the two parts of a rule: the head and the body.

It is meaning "To prove that Cindy likes something, prove that Bill likes that same thing" and "To prove that

Caitlin likes something, prove that it is green."

Queries

Once we give Prolog a set of facts, we can proceed to ask questions concerning these facts; this is known as querying the Prolog system .

Natural language

Does Bill like Cindy?

Prolog Syntax likes(bill, cindy).

Answer yes

What does Bill like?

likes(bill, What).

What=cindy

What=dogs

2 Solutions

It is important to notice that the second object--

What --begins with a capital letter, while the first object-bill

—does not. This is because bill is a fixed constant object—a constants known value-but What is a variable.

Variables always begin with an upper-case letter or an underscore.

Putting Facts, Rules, and Queries Together

 Suppose you have the following facts and rules:

A fast car is fun.

A big car is nice.

A little car is practical.

Bill likes a car if the car is fun.

Diane is a vegetarian and eats only what her doctor tells her to eat.

 Write the Prolog Syntax for previous example?

From Natural Language to Prolog Programs

 In the first section of this lecture we talked about facts and rules, relations, general sentences, and queries. Those words are all part of a discussion of logic and natural language.

Now we're going to discuss the same ideas, but we're going to use more Prolog-ish words, like clauses, predicates, variables, and goals.

Clauses (Facts and Rules)

 Basically, there are only two types of phrases that make up the Prolog language; a phrase can be either a fact or a rule .

 These phrases are known in Prolog as clauses .

 The heart of a Prolog program is made up of clauses .

 predicate name.

The objects that it relates are called its arguments .

Ex: likes(bill,cindy) likes is predicate.

bill and cindy are the arguments.

 Here are some examples of Prolog predicates with zero or more arguments: pred(integer, symbol) person(last, first, gender) run insert_mode birthday(firstName, lastName, date )

Variables (General Clauses)

 In a simple query, you can use variables to ask

Prolog to find who likes tennis. For example: likes(X, tennis).

 Variables in Prolog get their values by being matched to constants in facts or rules .

 Until it gets a value, a variable is said to be free ; when it gets a value, it becomes bound .

Variables (General Clauses) (Con.)

 Anonymous variables enable you to unclutter your programs. If you only need certain information from a query, you can use anonymous variables to ignore the values you don't need. In Prolog, the anonymous variable is represented by a lone underscore ("_").

 Anonymous variables can also be used in facts.

Natural language

Everyone owns shoes.

Everyone eats.

Prolog Syntax owns(_, shoes).

eats(_).

 The anonymous variable matches anything.

Goals (Queries)

 Up to now, we've been mixing the word query when talking about the questions you ask Prolog, with the more common name goal , which we'll use from now on.

 Goals can be simple, such as: likes(ellen, swimming).

 or they can be more complex ( compound goal), and each part of the compound goal is called a subgoal , such as : likes(Person, reading), likes(Person, swimming).

Compound Goals:

Conjunctions and Disjunctions

 As you have seen, you can use a compound goal to find a solution where both subgoal A and subgoal B are true (a conjunction ), by separating the subgoals with a comma (,)

 You can also find a solution where subgoal A or subgoal B is true (a disjunction ), by separating the subgoals with a semicolon (;)

Comments

It's good programming style to include comments in your program.

 makes the program easy for you and others to understand.

 Multiple-line comments must begin with the characters /* (slash, asterisk) and end with the characters */ (asterisk, slash).

Ex

: /* This is an example of a comment */

 To set off single-line comments, you can use these same characters, or you can begin the comment with a percent sign (%).

Ex :

% This is also a comment

Matching

 identical structures match each other parent(joe,tammy) matches parent(joe,tammy)

 a match usually involves one or more free variables parent(joe,X) matches parent(joe,tammy)

 Two free variables can even match each other.

For example, parent(joe,X) matches parent(joe,Y)

Homework

Download the Homework #1 from the website and solve it ,, deliver it in the next week ..

Good Luck ,,

Download