23
1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi , Huda al Hakami

1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

Embed Size (px)

Citation preview

Page 1: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

1

Visual Prolog Programs LabLecture # 3

Lecturer : Sheriff Nafisa

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

Page 2: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

2

In this lecture|:

• Visual Prolog's Basic Program Sections– The Clauses Section– The Predicates Section– The Domains Section– The Goal Section

• A Closer Look at Declarations and Rules• Other Program Sections

– The Facts Section– The Constants Section

– The Global Sections

Page 3: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

3

Visual Prolog Programs: Introduction• The syntax of Visual Prolog is designed to express

knowledge about properties and relationships. You've already seen the basics of how this is done; in LabLecture2 you learned about clauses (facts and rules), predicates, variables, and goals.

• Visual Prolog is a typed Prolog compiler; you declare the types of the objects that each predicate applies to.

• The type declarations allow Visual Prolog programs to be compiled right down to native machine code, giving execution speeds similar to those of compiled C and pascal.

Page 4: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

4

Visual Prolog's Basic Program Sections

Visual Prolog program includes four basic program sections:

• The clauses section • The predicates section• The domains section

• The goal section.

Page 5: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

5

A Visual Prolog program has the following basic structure:

DOMAINS/* ... domain declarations... */PREDICATES/* ... predicate declarations... */CLAUSES/* ... clauses (rules and facts)... */GOAL/* ... subgoal_1,subgoal_2,etc. */

Page 6: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

6

DOMAINSargument_type1, ..., argument_typeN = <standard domain>argument_1, ..., argument_N) = <compound domain 1>; <compound domain 2>; < ... >; <compound domain N>; PREDICATESpredicateName(argument_type1, argument_type2, ...,

argument_typeN) CLAUSESclauses (rules and facts)

GOALsubgoal_1,subgoal_2,etc.

Page 7: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

7

The clauses section:

• The clauses section is the heart of a Visual Prolog program;

• this is where you put the facts and rules that Visual Prolog will operate on when trying to satisfy the program's goal.

Page 8: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

8

The predicates section

• The predicates section is where you declare your predicates and the domains (types) of the arguments to your predicates.

(You don't need to declare Visual Prolog's built-in predicates.)

- a sequence of clauses defining a predicate is

called a procedure.

- If you define your own predicate in the clauses

section, you must declare it in a predicates

section.

Page 9: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

9

How to Declare User-Defined Predicates

• predicateName(argument_type1, argument_type2, ...,argument_typeN)

(Note: that, unlike the clauses in the clauses section of your program, a predicate declaration is not followed by a period.)

PredicateNames:Rule

• Must begin with a lower-case letter, followed

by a sequence of letters, digits, and underscores.

• Predicate names can be up to 250 characters long. Contd..

Page 10: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

10

• If you declare a predicate my_predicate(symbol, integer) in the predicates section, like this:

PREDICATES

my_predicate(symbol, integer)

you don't need to declare its arguments' domains in a domains section, because symbol and integer are standard domains.

• But if you declare a predicate my_predicate(name, number) in the predicates section, like this:

PREDICATES

my_predicate(name, number)

contd..

Page 11: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

11

you will need to declare suitable domains for name and number. Assuming you want these to be symbol and integer respectively, the domain declaration looks like this:

DOMAINS

name = symbol

number = integer

PREDICATES

my_predicate(name, number)

Page 12: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

12

Valid naming characters in Visual Prolog:• Upper-case Letters: A, B, ... , Z

• Lower-case Letters: a, b, ... , z

• Digits: 0, 1, ... , 9

• Underscore character: _

Examples of legal and illegal predicate names:Legal Predicate Names Illegal Predicate Names

fact [fact]

is_a *is_a*

has_a has/a

patternCheckList pattern-Check-List

choose_Menu_Item choose Menu Item

predicateName predicate<Name>

first_in_10 >first_in_10

Page 13: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

13

The domains section:

• The domains section is where you declare any domains.

Domains enable you to give distinctive names to different kinds of data that would otherwise look alike.

(You don't need to declare standard domains.)

Ex: Frank is a male who is 45 years old.

DOMAINS

name, sex = symbol

age = integer

PREDICATES

person(name, sex, age)

Page 14: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

14

The goal section:

• The goal section is where you put the starting goal for a Visual Prolog program.

• It's simply a list of subgoals.

• The goal keyword is not followed by :-.

• Visual Prolog automatically executes the goal when the program runs.

Page 15: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

15

Basic Standard DomainsDescription Description and implementation

char A character, implemented as an unsigned byte. Syntactically, it is written as a character surrounded by single quotation marks: 'a'.

real A floating-point number, implemented as 8 bytes.

Examples: 42705 9999 86.72

string A sequnce of characters.

Examples: telephone_number "railway ticket" "Dorid Inc "

symbol A sequence of characters.

The syntax is the same as for strings

Page 16: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

16

Multiple Arity

• The arity of a predicate is the number of arguments that it takes.

• You can have two predicates with the same name but different arity.

• You must group different arity versions of a given predicate name together in both the predicates and clauses sections of your program;

• apart from this restriction, the different arities are treated as completely different predicates

Page 17: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

17

Multiple Arity:Example

DOMAINS person = symbolPREDICATES father(person) % This person is a father

father(person, person) % One person is the father of the other person

CLAUSES father(Man):- father(Man,_). father(adam,seth). father(abraham,isaac).

Page 18: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

18

Other Program Sections

Other commonly-used program sections:

• The facts section,

• The constants section,

• and the various global sections.

Page 19: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

19

The Facts Section

• A Visual Prolog program is a collection of facts and rules.

• The keyword facts declares the facts section. It is here that you declare the facts to be included in the dynamic facts section

Page 20: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

20

The Constants Section

• A constant declaration section is indicated by the keyword constants.

syntax: <Id> = <Macro definition>

• <Id> is the name of your symbolic constant, and <Macro definition> is what you're assigning to that constant.

• Each <Macro definition> is terminated by a newline character, so there can only be one constant declaration per line.

Page 21: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

21

CONSTANTS:Example

A = (10*(10-1)+10)*34, delay(A),

zero = 0

one = 1

two = 2

hundred = (10*(10-1)+10)pi = 3.141592653

ega = 3

slash_fill = 4

red = 4

Before compiling your program, Visual Prolog will replace each constant with the actual string to which it corresponds. For instance:

...,

A = hundred*34, delay(A),

Page 22: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

22

The Global Sections

Visual Prolog allows you to declare some domains, predicates, and clauses in your program to be global (rather than local);

you do this by setting aside separate global domains, global predicates, and global facts sections at the top of your program.

These global sections are discussed later.

Page 23: 1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami

23

• Exercises..