60 20 PROLOG Syntax and Meaning DP

35
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia

Transcript of 60 20 PROLOG Syntax and Meaning DP

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 1/35

PROLOG

SYNTAX AND MEANING

Ivan Bratko

University of LjubljanaFaculty of Computer and Info. Sc.

Ljubljana, Slovenia

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 2/35

DATA OBJECTS

data objects

simple objects structures

constants variables

atoms numbers

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 3/35

SYNTAX FOR DATA OBJECS

� Type of object always recognisable from its syntactic

form

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 4/35

THREE SYNTACIC FORMS FOR  ATOMS

(1) Strings of letters, digits, and ³_´, starting with

lowercase letter:

x x15 x_15 aBC_CBa7

alpha_beta_algorithm taxi_35

peter missJones miss_Jones2

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 5/35

 ATOMS, CTD.

(2) Strings of special characters

---> <==> <<

. < > + ++ ! .. .::. ::= []

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 6/35

 ATOMS, CTD.

(3) Strings in single quotes

µX  _35¶ µPeter¶ µBritney Spears¶

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 7/35

SYNTAX FOR NUMBERS

� Integers

1 1313 0 -55

� Real numbers (floating point)

3.14 -0.0045 1.34E-21 1.34e-21

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 8/35

SYNTAX FOR V ARI ABLES

� Strings of letters, digits, and underscores, starting with

uppercase letter 

X Results Object2B Participant_list

  _x35 _335

� Lexical scope of variable names is one clause

� Underscore stands for an anonymous variable

� Each appearance of underscore: another anon. var.

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 9/35

 ANONYMOUS V ARI ABLES

visible_block( B) :-

see( B, _, _).

Equivalent to:

visible_block( B) :-

see( B, X, Y).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 10/35

STRUCTURES

� Structures are objects that have several components

� For example: dates are structured objects with three

components

� Date 17 June 2006 can be represented by term:

date( 17, june, 2006)

functor arguments

� An argument can be any object, also a structure

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 11/35

FUNCTORS

� Functor name chosen by user 

� Syntax for functors: atoms

� Functor defined by name and arity 

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 12/35

TREE REPRESENTATION

OF STRUCTURES

Often, structures are pictured as trees

date( 17, june, 2006)

date

17 june 2006

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 13/35

� Therefore all structured objects in Prolog can be viewed

as trees

� This is the only way of building structured objects inProlog

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 14/35

SOME GEOMETRIC OBJECTS

P2 = (2,3) (6,4)

S T

(4,2)

P1=(1,1) (7,1)

P1 = point( 1, 1) P2 = point( 2, 3)

S = seg( P1, P2) = seg( point(1,1), point(2,3))

T = triangle( point(4,2), point(5,4), point(7,1))

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 15/35

LINE SEGMENT

S = seg( point(1,1), point(2,3))

S = seg

point point

1 1 2 3

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 16/35

 ARITHMETIC EXPRESSIONS

 ARE  ALSO STRUCTURES

� For example: (a + b) * (c - 5)

� Written as term with functors:

*( +( a, b), -( c, 5))

*

+ -

a b c 5

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 17/35

M ATCHING

� Matching is operation on terms (structures)

� Given two terms, they match if:

(1) They are identical, or 

(2) They can be made identical by properly

instantiating the variables in both terms

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 18/35

EXAMPLE OF M ATCHING

� Matching two dates:

date( D1, M1, 2006) = date( D2, june, Y2)

� This causes the variables to be instantianted as:D1 = D2

M1 = june

Y2 = 2006

� This is the most general instantiation

� A less general instantiation would be: D1=D2=17, ...

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 19/35

MOST GENER AL INSTANTI ATION

� In Prolog, matching always results in most general

instantiation

� This commits the variables to the least possible extent,leaving flexibility for further instantiation if required

� For example:

?- date( D1, M1, 2006) = date( D2, june, Y2),date( D1, M1, 2006) = date( 17, M3, Y3).

D1 = 17, D2 = 17, M1 = june, M3 = june,

Y2 = 2006, Y3 = 2006

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 20/35

M ATCHING

� Matching succeeds or fails; if succeeds then it results in

the most general instantiation

� To decide whether terms S and T match:(1) If S and T are constants then they match only if they

are identical

(2) If S is a variable then matching succeeds, S is

instantiated toT

; analogously if T

is a variable(3) If S and T are structures then they match only if 

(a) they both have the same principal functor, and

(b) all their corresponding arguments match

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 21/35

M ATCHING § UNIFIC ATION

� Unification known in predicate logic

� Unification = Matching + Occurs check

� What happens when we ask Prolog:

?- X = f(X).

Matching succeeds, unification fails

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 22/35

COMPUTATION WITH M ATCHING

% Definition of vertical and horizontal segments

vertical( seg( point( X1,Y1), point( X1, Y2))).

horizontal( seg( point( X1,Y1), point( X2, Y1))).

?- vertical( seg( point( 1,1), point( 1, 3))).

yes

?- vertical( seg( point( 1,1), point( 2, Y))).

no

?- vertical( seg( point( 2,3), P)).

P = point( 2, _173).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 23/35

 AN INTERESTING SEGMENT

� Is there a segment that is both vertical and horizontal?

?- vertical( S), horizontal( S).

S = seg( point( X,Y), point(X,Y))

� Note, Prolog may display this with new variables names

as for example:

S = seg( point( _13,_14), point( _13, _14))

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 24/35

DECL AR ATIVE ME ANING

� Given a program P and a goal G,G is true ( i.e. logically follows from P) if and only if:

(1) There is a clause C in P such that

(2) there is a clause instance I of C such that

(a) the head of I is identical to G, and(b) all the goals in the body of I are true

� An instance of a clause C is obtained by renaming each

variable in C and possibly substituting the variable bysome term. E.g. an instance of 

p(X,Y) :- q(Y,Z)

is

p(U,a) :- q(a,V).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 25/35

DECL AR ATIVE vs PROCEDUR AL

ME ANING OF

PROLOG PROGR AMS� Consider:

P :- Q, R.

� Declarative readings of this:

� P is true if Q and R are true.

� From Q and R follows P.

� Procedural readings:

� To solve problem P, first solve subproblem Q and then R.

� To satisfy P, first satisfy Q and then R.

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 26/35

PROCEDUR AL ME ANING

� Specifies how Prolog answers questions

� Procedural meaning is an algorithm to execute a list of goals

given a Prolog program:

program

success/failure indication

goal list execute

instantiation of variables

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 27/35

procedure execute( Program, GoalList, Success)

� execute = declarative meaning + procedural elements

G is true ( i.e. logically follows from P) if and only if:

(1) there is a clause C in P such that

(2) there is a clause instance I of C such that

(a) the head of I is identical to G, and

(b) all the goals in the body of I are true

Search program from top to bottom to find such clause

Match G and

head of C

Execute goals in order as they

appear in program

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 28/35

EXAMPLE

QU ALITATIVE CIRCUIT  AN ALYSIS

� Electric circuits made of resistors and diodes through

sequential and parallel connections

� Circuit = par( res, seq( res, diode))

Current

Voltage

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 29/35

REL ATION CIRC

circ( Circuit, Voltage, Current)

Current Circuit

+ -

Voltage

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 30/35

% Qualitative definition of electric circuits

% Currents and voltages have qualitative values: zero, pos or neg

%  All circuits have two terminals

%  A circuit may consist of a simple element,

% or it can be composed from two circuits through parallel or sequential

% connection

% circ( Circuit, Voltage, Current)

% The behaviour of resistor: "qualitative" Ohm's law

circ( res, pos, pos). % Both voltage and current positive

circ( res, zero, zero).

circ( res, neg, neg).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 31/35

% The behaviour of diode

circ( diode, zero, pos). % Open diode: zero voltage, any pos. current

circ( diode, zero, zero).

circ( diode, neg, zero). % Closed diode, zero current, any neg. voltage

% Reversed diode

circ( revdiode, zero, neg). % Open: zero voltage, any neg. current

circ( revdiode, zero, zero).

circ( revdiode, pos, zero).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 32/35

% Sequential composition of circuits

circ( seq( Circuit1, Circuit2), Volt, Curr) :-

circ( Circuit1, Volt1, Curr),

circ( Circuit2, Volt2, Curr), % The same current

qsum( Volt1, Volt2, Volt). %  Add voltages

% Parallel composition of circuits

circ( par( Circuit1, Circuit2), Volt, Curr) :-

circ( Circuit1, Volt, Curr1),circ( Circuit2, Volt, Curr2), % The same voltage

qsum( Curr1, Curr2, Curr). %  Add currents

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 33/35

% qsum( Q1, Q2, Q3):

% Q3 = Q1 + Q2, qualitative sum over domain [pos,zero,neg]

qsum( pos, pos, pos).

qsum( pos, zero, pos).

qsum( pos, neg, pos).

qsum( pos, neg, zero).

qsum( pos, neg, neg).

qsum( zero, pos, pos).

qsum( zero, zero, zero).

qsum( zero, neg, neg).

qsum( neg, pos, pos).

qsum( neg, pos, zero).

qsum( neg, pos, neg).

qsum( neg, zero, neg).

qsum( neg, neg, neg).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 34/35

SOME QUERIES

?- circ( par( seq( res, res), res), V, C).

C = pos,V = pos ? ;

C = zero,V = zero ? ;

C = neg,V = neg ? ;

% Construct a circuit

?- circ( C, pos, pos), circ( C, zero, zero), circ( C, neg, neg).

8/6/2019 60 20 PROLOG Syntax and Meaning DP

http://slidepdf.com/reader/full/60-20-prolog-syntax-and-meaning-dp 35/35

?- circ( C, pos,pos), circ(C, neg, zero).

C = seq(res,diode) ? ;

C = seq(res,seq(res,diode)) ? ;

C = seq(res,seq(res,seq(res,diode))) ? ;C = seq(res,seq(res,seq(res,seq(res,diode)))) ? ;

% How about some parallel compositions?