chapter 2 syntax and meaning of prolog programs. 344-302 lp and prolog chapter 22 prolog domains...

25
Chapter 2 Syntax and meaning of Prolog Programs

Upload: joan-poole

Post on 03-Jan-2016

233 views

Category:

Documents


1 download

TRANSCRIPT

Chapter 2

Syntax and meaning of Prolog Programs

Chapter 2 2344-302 LP and Prolog

PROLOG

domains

variable name = type

predicates

relation(variable name, variable name, …)

goal

run(X).

clauses

relation(value,value,…).

conclusion :- condition.

conclusion if condition.

type

symbol, integer, real, char

Chapter 2 3344-302 LP and Prolog

TURBO PROLOG

ftp://172.28.80.6/older/DosProgram/TPROLOGAlt + Enter = Big ScreenF1 : HelpF2 : SaveF3 : LoadF6 : Next/SwitchF8 : Previous GoalF9 : CompileF10 : Step (For trace) / EndAlt + T : Trace ON/OFFSet up window size edit Use arrow key to adjust the

size

Chapter 2 4344-302 LP and Prolog

char 1 byte characters

integer 2 byte integer numbers

real 8 byte floating point

numbers

symbol strings inserted in the

internal symbol table

string sequences of chars

"hello world\n"

PREDEFINED DOMAINS

Chapter 2 5344-302 LP and Prolog

SUMMARY OF PROGRAM SECTIONS

Options must precede the other sections

CONSTANTS const1 = definition const2 = definition[GLOBAL] DOMAINS dom [,dom] = [reference] declaration1; declaration2 listdom = dom* dom = <basisdom>[GLOBAL] DATABASE [ - <databasename> ] [determ] pred1(....) pred2(.....)

GLOBAL PREDICATES [determ|nondeterm] pred1(.........)

-(i,i,o,..)(i,o,i,..) [ language c|pascal|fortran ] [ as "name" ]

pred2(........)

PREDICATES [determ|nondeterm] pred1(.........) pred2(........)

CLAUSES p(....):-p1(...), p2(.....), ... . p(....):-p1(...), p2(.....), ... .

include "filename" Include a file during compilation.

Chapter 2 6344-302 LP and Prolog

MISCELLANEOUSrandom(RealVariable)

(real) - (o)

random(MaxValue,RandomInt)

(integer,integer) - (i,o)

sound(Duration,Frequency)

(integer,integer) - (i,i)

beep

date(Year,Month,Day)

(integer,integer,integer) - (o,o,o) (i,i,i)

time(Hours,Minutes,Seconds,Hundredths)

(integer,integer,integer,integer) - (o,o,o,o) (i,i,i,i)

trace(on/off)

(string) - (i) (o)

Chapter 2 7344-302 LP and Prolog

ERROR & BREAK CONTROL

trap (PredicateCall,ExitCode,Predicate

ToCallOnError)

exit

exit (ExitCode)

(integer) - (i)

if exit to DOS then the DOS errorlevel task processing variable will

contain the value given to the exit predicate.

break (on/off)

(string) - (i) (o)

Chapter 2 8344-302 LP and Prolog

EDITORdisplay(String)

(string) - (i)

edit(InputString,OutputString)

(string,string) - (i,o)

edit(InputString,OutputString,Headstr,Headstr2,Msg,Pos,Helpfilename,

EditMode,Indent,Insert,TextMode,RetPos,RetStatus)

(string,string,string,string,string,integer,string,integer,integer,integer,integer,integer,integer)

- (i,o,i,i,i,i,i,i,i,i,i,o,o)

If the user saves the text from the editor, HeadStr2 will be used as the file name.

editmsg(InputString,OutputString,Headstr,Headstr2,Msg,Pos,Helpfilename,RetStatus)

(string,string,string,string,string,integer,string,integer) - (i,o,i,i,i,i,i,o)

Chapter 2 9344-302 LP and Prolog

WINDOW SYSTEMmakewindow(WindowNo,ScrAtt,FrameAtt,Framestr,Row,Col

umn,Height,Width)

(integer,integer,integer,string,integer,integer,integer,integer)

shiftwindow(WindowNo)

(integer) - (i) (o)

gotowindow(WindowNo)

(integer) - (i)

resizewindow(StartRow,NoOfRows,StartCol,NoOfCols)

(integer,integer,integer,integer) - (i,i,i,i)

colorsetup(Main_Frame)

(integer) - (i)

Chapter 2 10344-302 LP and Prolog

INPUT

readln(StringVariable)

(string) - (o)

readint(IntgVariable)

(integer) - (o)

readreal(RealVariable)

(real) - (o)

readchar(CharVariable)

(char) - (o)

keypressed

unreadchar(CharToBePushedBack)

(Char) - (i)

readterm( Domain, Variable )

(DomainName,Domain) - (i,_)

Chapter 2 11344-302 LP and Prolog

OUTPUTwrite( Variable|Constant * )nlwritef( FormatString, Variable|Constant* )In the format string the following options are known after a

percentage sign: %d Normal decimal number. (chars and integers) %u As an unsigned integer. (chars and integers) %R As a database reference number. (database reference

numbers) %X As a long hexadecimal number. (strings, database

reference numb). %x As a hexadecimal number. (chars and integers). %s Strings. (symbols and strings). %c As a char. (chars and integers). %g Reals in shortest posible format (default for reals) %e Reals in exponetial notation %f Reals in fixed notation %lf Only for C compatibility (fixed reals)

\n - newline\t - tabulator\nnn - character with code nnn

Chapter 2 12344-302 LP and Prolog

PROLOG_HELP

ARITHMETICArithmetic operators: +, -, *, /, mod, div

Relational operators: >, <, =, >=, <=, <>, ><

Functions: sin, cos, tan, arctan, ln, log, exp, sqrt, round, trunc, abs

EX: 1 + 2 = 2 + 1, X = 5/2, X = 5 mod 2, 5 <> 9

Chapter 2 13344-302 LP and Prolog

TURBO PROLOG

Use the example from the EXAMPLE directory to try to program.Start with EX03EX01.PROpredicates likes(symbol,symbol)

clauses likes(ellen, tennis).

likes(john, football).

FACTS likes(tom, baseball). likes(eric, swimming). likes(mark, tennis).

likes(bill, Activity) if likes(tom, Activity).

likes(mark, Activity) :- likes(ellen, Activity). RULES

Chapter 2 14344-302 LP and Prolog

bear.propredicates

big(symbol)

small(symbol)

brown(symbol)

black(symbol)

gray(symbol)

dark(symbol)

clauses

big(bear).

big(elephant).

small(cat).

brown(bear).

black(cat).

gray(elephant).

dark(Z) :- black(Z).

dark(Z) :- brown(Z).

?black(X),big(X)

?brown(X),big(X)

?big(X),black(X)

?black(X),big(X)

No solution

?brown(X),big(X)

X=bear

?big(X),black(X)

No solution

Chapter 2 15344-302 LP and Prolog

EX04EX02.PROdomains

brand, color = symbol

age, price = integer

mileage = real

predicates

car(brand, mileage, age, color, price)

clauses

car(chrysler, 130000, 3, red, 12000).

car(ford, 90000, 4, gray, 25000).

car(datsun, 8000, 1, red, 30000).

Chapter 2 16344-302 LP and Prolog

SUM, MULTIPLY : EX04EX01

domains

product, sum = integer

predicates

add_em_up(sum, sum, sum)

multiply_em(product, product, product)

clauses

add_em_up(X, Y, Sum) :- Sum = X + Y.

multiply_em(X, Y, Product) :- Product = X * Y.

Chapter 2 17344-302 LP and Prolog

sister

Z

X Y

parent parent

sister

For any X and Y,

X is a sister of Y if

1. Both X and Y have the same parent, and

2. X is a female.

sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X).

Chapter 2 18344-302 LP and Prolog

predecessor

A

B

C

E

D

parent

parent

parent

parent

Chapter 2 19344-302 LP and Prolog

predecessor

A

B

C

E

D

predecessor(X,Z) :-

parent(X,Y),

parent(Y,Z).

parent

parent

parent

parent

predecessor(X,Z) :-

parent(X,Y1),

parent(Y1,Y2),

parent(Y2,Z).

predecessor(X,Z) :-

parent(X,Y1),

parent(Y1,Y2),

parent(Y2,Y3),

parent(Y3,Z).

Chapter 2 20344-302 LP and Prolog

Parent1.Pro

predicates

parent(symbol,symbol)

predecessor(symbol,symbol)

clauses

parent(pam,bob).

parent(tom,bob).

parent(bob,ann).

parent(ann,jim).

parent(jim,joe). parent(joe,john).

parent(john,jack).

parent(tom,liz).

predecessor(X,Z) :- parent(X,Z).

predecessor(X,Z) :- parent(X,Y), parent(Y,Z). predecessor(X,Z) :- parent(X,Y1),parent(Y1,Y2),parent(Y2,Z).

predecessor(X,Z) :- parent(X,Y1),parent(Y1,Y2),parent(Y2,Y3)

parent(Y3,Z).

ann

bob

pam

jim

john

joe

jack

Chapter 2 21344-302 LP and Prolog

predecessor

A

B

C

E

D

parent

parent

parent

parent

For all X and Z,

X is a predecessor of Z if there is a Y such that

1. X is a parent of Y and

2. Y is a predecessor of Z.

Chapter 2 22344-302 LP and Prolog

Parent2.Pro

predecessor(X,Z) :- parent(X,Z).

predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z).

predicates

parent(symbol,symbol)

predecessor(symbol,symbol)

clauses

parent(pam,bob).

parent(tom,bob).

parent(bob,ann).

parent(ann,jim).

parent(jim,joe). parent(joe,john).

parent(john,jack).

parent(tom,liz).

Chapter 2 23344-302 LP and Prolog

ISA Relationship

Animal kingdom Plant kingdom

Animal Human

Dog Cat

Toop

Suwit

Mew

Sunee

plant

Flower

Rose Carnation

isa

isa

isa

isa isa

isaisa

isa

isa

isaisa

isa

Chapter 2 24344-302 LP and Prolog

isa1.Pro

is(X,Z) :- isa(X,Z).

is(X,Z) :- isa(X,Y), is(Y,Z).

predicates

isa(symbol,symbol)

is(symbol,symbol)

clauses

isa(human,animal_kingdom).

isa(plant,plant_kingdon).

isa(flower,plant).

isa(rose,flower).

isa(carnation,flower).

isa(suwit,human).

isa(sunee,human).

isa(dog,animal).

isa(animal,animal_kingdom).

isa(cat,animal).

isa(toop,dog).

isa(mew,cat).

isa(white,cat).

For all X and Z,

X is a predecessor of Z if there is a Y such that

1. X is a parent of Y and

2. Y is a predecessor of Z.