26
Getting Started with Visual Prolog LabLecture # 2 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi , Huda al Hakami

Getting Started with Visual Prolog

  • Upload
    brier

  • View
    120

  • Download
    2

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Getting Started  with Visual Prolog

Getting Started with Visual PrologLabLecture # 2

Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi , Huda al Hakami

Page 2: Getting Started  with Visual Prolog

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

Page 3: Getting Started  with Visual Prolog

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 .

Page 4: Getting Started  with Visual Prolog

Visual Prolog 5.2 Main Window

Page 5: Getting Started  with Visual Prolog

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:

Page 6: Getting Started  with Visual Prolog

Running and Testing a Program

Page 7: Getting Started  with Visual Prolog

Error HandlingIf 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.

Page 8: Getting Started  with Visual Prolog

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.

Page 9: Getting Started  with Visual Prolog

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.

Page 10: Getting Started  with Visual Prolog

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.

Page 11: Getting Started  with Visual Prolog

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 parentheses),

The fact ends with a period (.).Facts in Prolog Facts in natural

languagelikes(bill, cindy). Bill likes Cindy.

likes(cindy, bill). Cindy likes Bill.

likes(bill, dogs). Bill likes dogs.

Page 12: Getting Started  with Visual Prolog

Facts: What Is KnownFacts can also express properties of objects

as well as relations.

Facts in Prolog Facts in natural language

green(kermit). Kermit is green

girl(caitlin).

Caitlin is a girl

Page 13: Getting Started  with Visual Prolog

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.

Page 14: Getting Started  with Visual Prolog

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."

Page 15: Getting Started  with Visual Prolog

QueriesOnce we give Prolog a set of facts, we can proceed to

ask questions concerning these facts; this is known as querying the Prolog system.

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.

Answer Prolog Syntax Natural languageyes likes(bill, cindy). Does Bill like

Cindy?What=cindyWhat=dogs2 Solutions

likes(bill, What). What does Bill like?

Page 16: Getting Started  with Visual Prolog

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?

Page 17: Getting Started  with Visual Prolog

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.

Page 18: Getting Started  with Visual Prolog

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.

Page 19: Getting Started  with Visual Prolog

Predicates (Relations)The symbolic name of a relation is called the 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)

Page 20: Getting Started  with Visual Prolog

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.

Page 21: Getting Started  with Visual Prolog

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.

The anonymous variable matches anything.

Prolog Syntax Natural languageowns(_, shoes). Everyone owns shoes.

eats.)_( Everyone eats.

Page 22: Getting Started  with Visual Prolog

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).

Page 23: Getting Started  with Visual Prolog

Compound Goals: Conjunctions and DisjunctionsAs 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 (;)

Page 24: Getting Started  with Visual Prolog

CommentsIt'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

Page 25: Getting Started  with Visual Prolog

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)

Page 26: Getting Started  with Visual Prolog

HomeworkDownload the Homework #1

from the website and solve it ,, deliver it in the next week ..

Good Luck ,,