advanced logic programming - software engineering · 2018-02-08 · prolog prolog stands for...

47
R O O T S Advanced Logic Programming https://sewiki.iai.uni-bonn.de/teaching/lectures/alp/2015/ Dr. Günter Kniesel Computer Science Department III University of Bonn, Germany [email protected]

Upload: others

Post on 12-Jun-2020

17 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

R O O T S

Advanced

Logic Programming

https://sewiki.iai.uni-bonn.de/teaching/lectures/alp/2015/

Dr. Günter Kniesel

Computer Science Department III

University of Bonn, Germany

[email protected]

Page 2: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

„Advanced Logic Programming“

https://sewiki.iai.uni-bonn.de/teaching/lectures/alp/2015/ R O O T S

Chapter 1. Prolog in a Nutshell

- Last updated: April 8, 2015 -

Prolog in a Nutshell

Prolog Syntax

Relations versus Functions

Application Example: Solving Logic Puzzles

Operators

Page 3: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-4 R O O T S

Prolog

Prolog stands for "Programming in Logic".

It is the most common logic-based programming language.

Bits of history

1965

John Alan Robinson develops the resolution calculus – the formal

foundation of automated theorem provers

1972

Alain Colmerauer (Marseilles) develops the first Prolog interpreter

mid 70th

David D.H. Warren (Edinburg) develops first Prolog compiler

Warren Abstract Machine (WAM) as compilation target like Java byte code

1981-92

„5th Generation Project“ in Japan boosts adoption of Prolog world-wide

Page 4: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter 1: Syntax, Informal Semantics, Application Example

R O O T S

Prolog in a Nutshell

A very quick tour of basic Syntax and Semantics

Page 5: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-6 R O O T S

It‘s all about truth

Page 6: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-7 R O O T S

isFatherOf: Person x Person Bool

Predicates Map entities to truth values

Function symbol (a name)

Domain (a crossproduct of sets)

Codomain (a set)

:

(kurt,peter) true

(peter,paul) true

(peter,hans) true

Predicate definition via truth table

Sample predicate declaration

Schema of a function declaration

isFatherOf(kurt,peter).

isFatherOf(peter,paul).

isFatherOf(peter,hans).

Predicate definition via facts

A predicate is a

boolean function.

Page 7: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Facts Statements that are true

isFatherOf('Kurt','Peter')

means

.

Page 8: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Rules Implications of what we know to be true

means

isGrandfatherOf('Kurt','Paul'):-

isFatherOf('Kurt','Peter'),

isFatherOf('Peter','Paul').

Page 9: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Variables Placeholders for domain values

means

isGrandfatherOf(G,C):-

isFatherOf(G,F),

isFatherOf(F,C).

Page 10: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter 1: Syntax, Informal Semantics, Application Example

R O O T S

Prolog Syntax

Predicates

Clauses, Rules, Facts

Terms, Variables, Constants, Structures

Page 11: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-12 R O O T S

Clauses Facts, Rules, Queries

isFatherOf(kurt,peter). isFatherOf(peter,paul).

isFatherOf(peter,hans).

isGrandfatherOf(G,C) :- isFatherOf(G,F), isFatherOf(F,C).

isGrandfatherOf(G,C) :-

isFatherOf(G,M), isMotherOf(M,C).

?- isGrandfatherOf(kurt,paul). ?- isGrandfatherOf(kurt,C).

?- isGrandfatherOf(G,paul).

?- isGrandfatherOf(G,paul),isFatherOf(X,G).

Rules

Goals / Querys

Facts

Clauses

Conjunction

Implication

Predicate symbol (just a name) Predicate definition (set of clauses)

Page 12: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-13 R O O T S

Recursion

Prolog predicates may be defined recursively

A predicate is recursive if one or more rules in its definition refer to itself.

ancestor(Anc,Desc):- parent(Anc,Desc).

ancestor(Anc,Desc):- parent(Anc,X), ancestor(X,Desc).

What does the definition of ancestor/2 by the above clauses mean?

1. “Anc is a parent of Desc” implies that “Anc is an ancestor of Desc”

2. “Anc is a parent of X and X is an ancestor of Desc” implies that “Anc is an ancestor

of Desc”

Homework

Try ancestor/2 together with your own parent/2 predicate definition

Does it give all the expected results?

Does it give only expected results?

Page 13: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-14 R O O T S

Rules

Rules consist of a head and a body.

Facts are just syntactic sugar for rules with the body “true”.

isGrandfatherOf(G,C) :- isFatherOf(G,X), ( isFatherOf(X,C) ; isMotherOf(X,C) ).

“head literal”

represents the predicate that is defined by the clause

“body literals”

represent goals to be proven in order to

prove the head.

isFatherOf(peter,hans) :- true.

isFatherOf(peter,hans).

these clauses are equivalent

Page 14: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-15 R O O T S

Program Clause Literal Argument

Prolog programs consist of clauses (see previous slide)

Clauses consist of literals …

Head literal

Zero or more body literals

Literals consist of

a predicate symbol

punctuation symbols

arguments

Punctuation symbols

opening round brace “(“

comma “,”

closing round brace “)”

Logical connectors

implication “:-”

conjunction “,”

disjunction “;”

… separated by logical connectors

Page 15: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-16 R O O T S

Terms

Terms are the arguments of literals. They may be

Variables X,Y, Father, Method, Type, _type, _Type, . . .

Constants Numbers, Strings, ...

Function terms person(stan,laurel), +(1,*(3,4)), …

Terms are the only data structure in Prolog!

The only thing one can do with terms is unification with other terms!

All computation in Prolog is based on unification.

Page 16: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-17 R O O T S

Variables Syntax

Variables start with an upper case letter or an underscore '_'.

Anonymous Variables ('_')

For irrelevant values

“Does Peter have a father?” We neither care whether he has one or many

fathers nor who the father is:

Country Year M V _45 _G107 _europe _

?- isFatherOf(_,peter).

Page 17: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-18 R O O T S

Variables Semantics

The scope of a variable is the

clause in which it appears

Variables that appear only once

in a clause are called singletons.

Mostly results of typos

SWI Prolog warns about

singletons,

… unless you suppress the

warnings

All occurrences of the same

variable in the same clause must

have the same value!

Exception: the “anonymous

variable” (the underscore)

isGrandfatherOf(G,C) :-

isFatherOf(G,F),

isFatherOf(F,C).

isGrandfatherOf(G,Child) :-

isFatherOf(G,M),

isMotherOf(M,Chil).

loves(romeo,juliet).

loves(john,eve).

loves(jesus,_Everybody).

?- classDefT(ID, _, ‘Applet’, _).

_Everybody

Intentional singleton variable, for which singleton warnings should be supressed.

Page 18: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-19 R O O T S

Constants

Numbers -17 -2.67e+021 0 1 99.9 512

Atoms sequences of letters, digits or underscore characters '_' that

start with a lower case letter

OR

are enclosed in simple quotes ( ' ). If simple quotes should be part of an

atom they must be doubled.

OR

only contains special characters

Remember: Prolog has no static typing!

So it is up to you to make sure you write what you mean.

ok: peter ' Fritz ' 'I don''t know!' new_york :- -->

wrong: Fritz 123 _xyz new-york

Page 19: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-20 R O O T S

Function terms are terms that are composed of other terms

akin to “records” in Pascal (or objects without any behavior in Java)

Arbitrary nesting allowed

No static typing: person(1,2,'a') is legal!

Function terms are not function calls! They do not yield a result!!!

Notation for function symbols: Functor/Arity, e.g. person/3, date/3

person(peter, mueller, date(27,11,2007))

Function Terms (‘Structures’)

functor subterms

functor subterms

Page 20: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-21 R O O T S

ExampleEmployees

employee( tom, jones, 5000, 10 ).

employee( tim, james, 7000, 0 ).

employee( tam, junes, 6500, 15 ).

Without function terms

Concise but clumsy!

What do the arguments represent?

Page 21: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-22 R O O T S

ExampleEmployees

employee( who(tom, jones), salary(5000, bonus(10,%)) ).

employee( who(tim, james), salary(7000, bonus( 0,%)) ).

employee( who(tam, junes), salary(6500, bonus(15,%)) ).

With function terms

Tim James has the highest fixed

salary but gets no bonus!

Page 22: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-25 R O O T S

Lists – Recursive Structures with special Syntax

Lists are denoted by square brackets "[]"

The pipe symbol "|" delimits the initial elements of the list from its „tail“

[] [1,2,a] [1,[2,a],c]

[1|[2,a]] [1,2|[a]] [Head|Tail]

Page 23: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-27 R O O T S

Strings

Strings are enclosed in double quotes (")

"Prolog" (with double quotes) is a string

'Prolog' (with simple quotes) is an atom

Prolog (without any quotes) is a variable

A string is just a list of ASCII codes

Strings are seldom useful Better use atoms!

There are many predefined predicates for using atoms in the same way as

Java uses strings.

Prolog strings are useful just for low level manipulation

Their removal from the language has often been suggested

"Prolog" = [80,114,111,108,111,103]

= .(80,.(114,.(111,.(108,.(111,.(103,[])))))

Page 24: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-28 R O O T S

Terms, again

Terms are constants, variables or structures

A ground term is a variable-free term

peter

27

MM

[europe, asia, africa | Rest]

person(peter, Name, date(27, MM, 2007))

person(peter, mueller, date(27, 11, 2007))

Page 25: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-29 R O O T S

Terms: Summary

Relations between the different kinds of term

Term

Simple Term Function Term

Constant Variable

Number Atom

Functor Arguments

Page 26: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-30 R O O T S

Test Yourself

How would you represent the following hospital information?

Patient Tom gets Aspirine at 8:00, 16:00 and 22:00 with water.

Patient Tim gets Dimethylamine at 8:00

and Insuline at 14:00 and 20:00

… imagine more patients, each with different medications

You have 2 minutes to think about it for yourself with paper and pencil.

Page 27: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter 1: Syntax, Informal Semantics, Application Example

R O O T S

Using Prolog for Logic Puzzles

The Magic Square

Page 28: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-32 R O O T S

1

2

3

Magic Square

Can I fill the square

so that each line

(row or column)

has the same sum?

Page 29: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-33 R O O T S

Abstract Problem Statement

1 B C

D E 2

G 3 J

+ + = S

+ + = S

+ + = S

+

+

=

S

+

+

=

S

+

+ =

S

Represent an unknown cell

value by a variable

Same variable S: Same sum for each row and each column

Page 30: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-34 R O O T S

State the Problem (1) Sum of a Line

1 B C + + = S

line([X,Y,Z],S):-

S is X+Y+Z.

built-in predicate: left-hand-side is

the result of evaluating the right-hand-side

Represent a line as a list of

cell values

„S“ is the sum of the cell

values in a line

Page 31: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-36 R O O T S

State the Problem (2) Magic Square

%% magic( +Square )

%

% Square is a list of 9 integers, with the first 3

% representing the first line of the square,

% the next 3 representing the second line, etc.

% A square is magic if all lines (rows or columns)

% have the same sum S.

magic( [A,B,C, D,E,F, G,H,I] ):-

% rows (horizontal lines):

line([A,B,C],S), line([D,E,F],S), line([G,H,I],S),

% columns (vertical lines):

line([A,D,G],S), line([B,E,H],S), line([C,F,I],S).

Page 32: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-37 R O O T S

State the Problem (3) Solution of Magic Square

%% solve_square( Square)

%

% Square is a list of 9 cells, with the first 3

% representing the first line of the square,

% the next 3 representing the second line, etc.

% A cell is either a variable or an integer.

% The square is magic if the variables can be replaced

% by values from 1 to 9 so that magic(Square) is true.

solve_square(Square) :-

% Replace variables in Square with values from 1 to 9

permutation([1,2,3,4,5,6,7,8,9], Square),

% ... so that the resulting square is magic

magic(Square).

Page 33: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-39 R O O T S

Use the Solution Solve the magic square

?- solve_square([1,B,C,D,E,2,G,3,J]).

B = 8,

C = 6,

D = 9,

E = 4,

G = 5,

J = 7

;

B = 5,

C = 9,

D = 6,

E = 7,

G = 8,

J = 4

;

false.

1 B C

D E 2

G 3 J

1 5 9

6 7 2

8 3 4

1 8 6

9 4 2

5 3 7

first solution

second solution

ask for next solution

ask for next solution

Page 34: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-40 R O O T S

How to Create a Magic Square?

Can I generate a

magic square?

Page 35: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-41 R O O T S

Use the Solution Generate magic square

?- solve_square([A,B,C,D,E,F,G,H,J]).

A = 1

B = 6,

C = 8,

D = 9,

E = 2,

F = 4,

G = 5,

H = 7,

J = 3

;

... 71 other squares

;

false.

1 6 8

9 2 4

5 7 3

first square

Ask for the most

general square:

Variables in all cells!

The same program

solves and generates

magic squares!

No need to rewrite!

Awesome!

Try it!!!

Page 36: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter 1: Syntax, Informal Semantics, Application Example

R O O T S

Predicates versus Functions

Difference of predicates and functions

Input Modes

How to document predicates

Page 37: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-43 R O O T S

Predicates versus Functions (1)

In the functional programming language Haskell the following definition of the isFatherOf predicate is illegal:

In a functional language predicates must be modeled as boolean

functions:

isFatherOf x | x==frank = peter

isFatherOf x | x==peter = paul

isFatherOf x | x==peter = hans

x | otherwise = dummy

isFatherOf x y| x==frank y==peter = True

isFatherOf x y| x==peter y==paul = True

isFatherOf x y| x==peter y==hans = True

x y| otherwise = False

Page 38: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-44 R O O T S

Predicates versus Functions (2)

Function application in Haskell must not contain any variables!

Only the following “checks” are legal:

In Prolog each argument of a goal may be a variable!

So each predicate can be used / queried in many different input modes:

isFatherOf frank peter

isFatherOf kurt peter

True

False

?- isFatherOf(kurt,peter).

?- isFatherOf(kurt,X).

?- isFatherOf(paul,Y).

?- isFatherOf(X,Y).

Yes

Yes X = paul; X = hans

No

Yes X = frank, Y = peter; X = peter, Y= paul; X = peter, Y=hans; No

Page 39: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-45 R O O T S

Predicates versus Functions (3)

Haskell is based on functions

Length of a list in Haskell

Prolog is based on predicates

Length of a list in Prolog:

length([ ]) = 0

length(x:xs) = length(xs) 1

length([ ], 0).

length([X|Xs],N) :- length(Xs,M), N is M+1.

?- length([1,2,a],Length).

Length = 3

Most general list with 3 elements

?- length(List,3).

List = [_G330, _G331, _G332]

used in mode (ground, free)

used in mode (free, ground)

Page 40: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-46 R O O T S

Documenting Predicates Properly

Predicates are more general than functions

There is not one unique result but many, depending on the input

So resist temptation to document predicates as if they were functions!

Don’t write this:

Better write this instead:

% The predicate length(List, Int) returns in Arg2

% the number of elements in the list Arg1.

% The predicate length(List, Int) succeeds iff Arg2 is

% the number of elements in the list Arg1.

Page 41: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-47 R O O T S

Documenting Invocation Modes

Invocation mode of an argument

“–” means “is a free variable at invocation time”

“+” means “is ground at invocation time”

“?” means “don’t care whether free or not at invocation time”

Document behaviour that depends on the invocation mode!

%% length(+List, ?Int) is det

% length(?List, +Int) is det

% length(-List, -Int) is nondet

%

% The predicate length(List, Int) succeeds iff Arg2 is

% the number of elements in the list Arg1.

%

length([ ],0).

length([X|Xs],N) :- length(Xs,N1), N is N11.

Page 42: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter 1: Syntax, Informal Semantics, Application Example

R O O T S

Operators

Operators are just syntactic sugar but they make programs more readable.

Page 43: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-49 R O O T S

Example from page 2-12 rewritten using infix operators

% Declare infix operators:

:- op(500,xfy,isFatherOf).

:- op(500,xfy,isMotherOf).

:- op(500,xfy,isGrandfatherOf).

% Declare predicates using the operator notation:

kurt isFatherOf peter.

peter isFatherOf paul.

peter isFatherOf hans.

G isGrandfatherOf C :- G isFatherOf F, F isFatherOf C.

G isGrandfatherOf C :- G isFatherOf M, M isMotherOf C.

% Ask goals using the operator notation:

?- kurt isGrandfatherOf paul.

?- kurt isGrandfatherOf C.

?- isGrandfatherOf(G,paul).

?- isGrandfatherOf(G,paul), X isFatherOf G.

any combination of function term notation

and operator notation is legal

Page 44: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-50 R O O T S

Operators

Operators are just syntactic sugar for function terms:

1+3*4 is the infix notation for +(1,*(3,4))

head :- body is the infix notation for ':-'(head,body)

?- goal is the prefix notation for '?-'(goal)

Operator are declared via the directive

:- op(precedence, notation_and_associativity, operatorName ).

“f” indicates position of functor ( prefix, infix, postfix)

“x” indicates non-associative side argument with precedence strictly lower than the functor

“y” indicates associative side argument with precedence equal or lower than the functor

:- op(1200, fx, '?-').

:- op( 500, yfx, '+').

‘?-’ has higher precedence

than ‘+’

prefix notation

infix notation, left associative

Page 45: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-51 R O O T S

Declaraion

Effect

Structure of term

Declaration

Effect

Structure of term

Operator Associativity

?- T = 1+2+3, T = A+B.

T = 1+2+3,

A = 1+2,

B = 3.

?- T = (a,b,c), T =(A,B).

T = (a,b,c),

A = a,

B = (b,c).

3

2 1

+

+ , a

,

c b

:- op(500, yfx, '+'). :- op(1000, xfy, ',').

Left associative operators

are applied in left-to-right order 1+2+3 = ((1+2)+3)

Right associative operators

are applied in right-to-left order

a,b,c = (a,(b,c))

In Java, the assignment operator is right-associative.

That is, the statement "a = b = c;" is equivalent to

"(a = (b = c));". It first assigns the value of c to b, then

assigns the value of b to a.

Page 46: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

© 2009 -2015 Dr. G. Kniesel Course „Advanced Logic Progrmming“ (ALP) Page 1-52 R O O T S

Declaration

Effect

Operator Associativity

?- A=B=C.

Syntax error:

Operator priority clash

:- op( 700, xfx, '=').

:- op(1150, fx, dynamic).

Non-associative operators

must be explicitly bracketed

?- A=(B=C).

A = (B=C).

Declaration

Effect

Associative prefix operators

may be cascaded

anything(_).

?- anything(+ - + 1).

true.

:- op( 700, fy, '+').

:- op(1150, fy, '-').

anything(_).

?- anything(+-+ 1).

Syntax error:

Operator expected One atom,

not three

operators!

Three

associative

prefix

operators!

Page 47: Advanced Logic Programming - Software engineering · 2018-02-08 · Prolog Prolog stands for "Programming in Logic". It is the most common logic-based programming language. Bits of

Chapter Summary

Prolog Syntax

Programs, clauses, literals

Terms, variables, constants

Recusion

Application Example

Solving logic puzzles

Relations versus Functions

Input modes

Extending the syntax

Operator definitions