tddd08 utotrial 1tddd08/tutorials/tutorial1.v2.pdf · lab 5: constraint logic programming. 4 / 18....

43

Upload: buikhanh

Post on 13-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

TDDD08 � Tutorial 1

Who? Victor Lagerkvist (& Wªodek Drabent)

From? Theoretical Computer Science Laboratory, Linköpings Universitet,Sweden

When? 6 september 2015

1 / 18

Preparations

Before you start with the labs.

Register yourself in Webreg to participate in the labs.Deadline 24th September.

Read the lab instructions.

Add module �le (use module [init]add prog/sicstus).

Modify your .emacs (see course page).

Strict deadline for completing the labs is 30thOctober.

2 / 18

Preparations

Using the system.

Separate code (�lename.pl) and query (*prolog*)bu�ers: all facts and rules in the �le-bu�er.

Just open or create �lename.pl to enter prolog modeautomatically.

Save �le and press C-c C-b to �consult bu�er� and createthe query window.

Quit command: �halt.�.

3 / 18

The labs

Five labs in total.

Lab 1: Basic Prolog.

Lab 2: Recursive Data Structures.

Lab 3: De�nite Clause Grammars.

Lab 4: Search.

Lab 5: Constraint Logic Programming.

4 / 18

Basic Prolog syntax

Functors (function symbols), predicate symbols:�atoms� in Prolog, lower case letter �rst(a, anna, aX, +, -, 'any quoted string', [ ]).

Variables: upper case letter �rst (X, Xs).

Special anonymous variable: _.

Comments: % Single line comment/* Block comment */

We use P/n to denote a predicate P of a speci�c arityn, e.g. append/3.

5 / 18

Basic Prolog syntax

Functors (function symbols), predicate symbols:�atoms� in Prolog, lower case letter �rst(a, anna, aX, +, -, 'any quoted string', [ ]).

Variables: upper case letter �rst (X, Xs).

Special anonymous variable: _.

Comments: % Single line comment/* Block comment */

We use P/n to denote a predicate P of a speci�c arityn, e.g. append/3.

5 / 18

Basic Prolog syntax

Functors (function symbols), predicate symbols:�atoms� in Prolog, lower case letter �rst(a, anna, aX, +, -, 'any quoted string', [ ]).

Variables: upper case letter �rst (X, Xs).

Special anonymous variable: _.

Comments: % Single line comment/* Block comment */

We use P/n to denote a predicate P of a speci�c arityn, e.g. append/3.

5 / 18

Basic Prolog syntax

Functors (function symbols), predicate symbols:�atoms� in Prolog, lower case letter �rst(a, anna, aX, +, -, 'any quoted string', [ ]).

Variables: upper case letter �rst (X, Xs).

Special anonymous variable: _.

Comments: % Single line comment/* Block comment */

We use P/n to denote a predicate P of a speci�c arityn, e.g. append/3.

5 / 18

Basic Prolog syntax

Facts

mother ( anna , bob ) . % anna i s mother o f bob .l i k e s (_, i c e c r eam ) . % Everybody l i k e s i c e c r eam .

A rule Head :- Body

g randpa r en t (X, Z) :− pa r en t (X,Y) , pa r en t (Y, Z ) .

X is a grandparent to Z if there is some Y such that Xis a parent of Y and Y is a parent of Z .

Both facts and rules end with a period.

6 / 18

Basic Prolog syntax

Facts

mother ( anna , bob ) . % anna i s mother o f bob .l i k e s (_, i c e c r eam ) . % Everybody l i k e s i c e c r eam .

A rule Head :- Body

g randpa r en t (X, Z) :− pa r en t (X,Y) , pa r en t (Y, Z ) .

X is a grandparent to Z if there is some Y such that Xis a parent of Y and Y is a parent of Z .

Both facts and rules end with a period.

6 / 18

Basic Prolog syntax

Facts

mother ( anna , bob ) . % anna i s mother o f bob .l i k e s (_, i c e c r eam ) . % Everybody l i k e s i c e c r eam .

A rule Head :- Body

g randpa r en t (X, Z) :− pa r en t (X,Y) , pa r en t (Y, Z ) .

X is a grandparent to Z if there is some Y such that Xis a parent of Y and Y is a parent of Z .

Both facts and rules end with a period.

6 / 18

Querying Prolog

A query (�goal�): (written in *prolog* bu�er)

| ?− g randpa r en t (X, j u l i a ) .no

Queries may be conjuctions of atomic queriesseparated by commas:

| ?− g randpa r en t (X, j ona tan ) ,g r andpa r en t (X, skorpan ) .

X = a s t r i d

Prolog answers with • an answer substitutionrepresented as equations,

• �yes� (empty answer substitution), or• �no�.

To �nd all the answers, repeatedly press ; which forcesProlog to back-track and give another solution if it can�nd one. When debugging always use ; to check thatyour program does not give erroneous answers.

7 / 18

Querying Prolog

A query (�goal�): (written in *prolog* bu�er)

| ?− g randpa r en t (X, j u l i a ) .no

Queries may be conjuctions of atomic queriesseparated by commas:

| ?− g randpa r en t (X, j ona tan ) ,g r andpa r en t (X, skorpan ) .

X = a s t r i d

Prolog answers with • an answer substitutionrepresented as equations,

• �yes� (empty answer substitution), or• �no�.

To �nd all the answers, repeatedly press ; which forcesProlog to back-track and give another solution if it can�nd one. When debugging always use ; to check thatyour program does not give erroneous answers.

7 / 18

Querying Prolog

A query (�goal�): (written in *prolog* bu�er)

| ?− g randpa r en t (X, j u l i a ) .no

Queries may be conjuctions of atomic queriesseparated by commas:

| ?− g randpa r en t (X, j ona tan ) ,g r andpa r en t (X, skorpan ) .

X = a s t r i d

Prolog answers with • an answer substitutionrepresented as equations,

• �yes� (empty answer substitution), or• �no�.

To �nd all the answers, repeatedly press ; which forcesProlog to back-track and give another solution if it can�nd one. When debugging always use ; to check thatyour program does not give erroneous answers.

7 / 18

Querying Prolog

A query (�goal�): (written in *prolog* bu�er)

| ?− g randpa r en t (X, j u l i a ) .no

Queries may be conjuctions of atomic queriesseparated by commas:

| ?− g randpa r en t (X, j ona tan ) ,g r andpa r en t (X, skorpan ) .

X = a s t r i d

Prolog answers with • an answer substitutionrepresented as equations,

• �yes� (empty answer substitution), or• �no�.

To �nd all the answers, repeatedly press ; which forcesProlog to back-track and give another solution if it can�nd one. When debugging always use ; to check thatyour program does not give erroneous answers.

7 / 18

A simple example

Simple database example on whiteboard.

8 / 18

Data structures in Prolog

Terms � the only data type in logic programming.Built out of constants, variables and function symbols.

Some built-in predicates require that their arguments arerestricted to certain classes of terms.

9 / 18

Data structures in Prolog. Lists

Single-linked lists just as in Lisp.

Formal object Alternative notation

.(a,t) [a|t] (cons of a and t)

.(a,[ ]) [a|[ ]] [a]

.(a, .(b,[ ])) [a|[b|[ ]]] [a,b]

.(a, .(b, .(c,[ ]))) [a|[b|[c|[ ]]]] [a,b,c]

Basic list processing programs on whiteboard.

10 / 18

Data structures in Prolog. Ex. � binary trees.i s_ t r e e ( l e a f (_) ) .i s_ t r e e ( t r e e (L , R) ) :− i s_ t r e e (L ) , i s_ t r e e (R ) .

% sea r ch ( Tree ,X) − Tree has a l e a f c o n t a i n i n g X

s e a r c h ( l e a f (X) , X ) .s e a r c h ( t r e e (L , _R) , X) :−

s e a r c h (L , X ) .s e a r c h ( t r e e (_L, R) , X) :−

s e a r c h (R , X ) .

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , b ) .y e s

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , d ) .no

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , X ) .X = a ? ;X = b ? ;X = c ? ;no

?− s e a r c h ( T, d ) .T = l e a f ( d ) ? ;T = t r e e ( l e a f ( d ) ,_A) ? ;T = t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ? ;T = t r e e ( t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ,_C) ?ye s

11 / 18

Data structures in Prolog. Ex. � binary trees.i s_ t r e e ( l e a f (_) ) .i s_ t r e e ( t r e e (L , R) ) :− i s_ t r e e (L ) , i s_ t r e e (R ) .

% sea r ch ( Tree ,X) − Tree has a l e a f c o n t a i n i n g X

s e a r c h ( l e a f (X) , X ) .s e a r c h ( t r e e (L , _R) , X) :−

s e a r c h (L , X ) .s e a r c h ( t r e e (_L, R) , X) :−

s e a r c h (R , X ) .

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , b ) .y e s

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , d ) .no

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , X ) .X = a ? ;X = b ? ;X = c ? ;no

?− s e a r c h ( T, d ) .T = l e a f ( d ) ? ;T = t r e e ( l e a f ( d ) ,_A) ? ;T = t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ? ;T = t r e e ( t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ,_C) ?ye s

11 / 18

Data structures in Prolog. Ex. � binary trees.i s_ t r e e ( l e a f (_) ) .i s_ t r e e ( t r e e (L , R) ) :− i s_ t r e e (L ) , i s_ t r e e (R ) .

% sea r ch ( Tree ,X) − Tree has a l e a f c o n t a i n i n g X

s e a r c h ( l e a f (X) , X ) .s e a r c h ( t r e e (L , _R) , X) :−

s e a r c h (L , X ) .s e a r c h ( t r e e (_L, R) , X) :−

s e a r c h (R , X ) .

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , b ) .y e s

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , d ) .no

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , X ) .X = a ? ;X = b ? ;X = c ? ;no

?− s e a r c h ( T, d ) .T = l e a f ( d ) ? ;T = t r e e ( l e a f ( d ) ,_A) ? ;T = t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ? ;T = t r e e ( t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ,_C) ?ye s

11 / 18

Data structures in Prolog. Ex. � binary trees.i s_ t r e e ( l e a f (_) ) .i s_ t r e e ( t r e e (L , R) ) :− i s_ t r e e (L ) , i s_ t r e e (R ) .

% sea r ch ( Tree ,X) − Tree has a l e a f c o n t a i n i n g X

s e a r c h ( l e a f (X) , X ) .s e a r c h ( t r e e (L , _R) , X) :−

s e a r c h (L , X ) .s e a r c h ( t r e e (_L, R) , X) :−

s e a r c h (R , X ) .

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , b ) .y e s

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , d ) .no

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , X ) .X = a ? ;X = b ? ;X = c ? ;no

?− s e a r c h ( T, d ) .T = l e a f ( d ) ? ;T = t r e e ( l e a f ( d ) ,_A) ? ;T = t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ? ;T = t r e e ( t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ,_C) ?ye s

11 / 18

Data structures in Prolog. Ex. � binary trees.i s_ t r e e ( l e a f (_) ) .i s_ t r e e ( t r e e (L , R) ) :− i s_ t r e e (L ) , i s_ t r e e (R ) .

% sea r ch ( Tree ,X) − Tree has a l e a f c o n t a i n i n g X

s e a r c h ( l e a f (X) , X ) .s e a r c h ( t r e e (L , _R) , X) :−

s e a r c h (L , X ) .s e a r c h ( t r e e (_L, R) , X) :−

s e a r c h (R , X ) .

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , b ) .y e s

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , d ) .no

?− s e a r c h ( t r e e ( t r e e ( l e a f ( a ) , l e a f ( b ) ) , l e a f ( c ) ) , X ) .X = a ? ;X = b ? ;X = c ? ;no

?− s e a r c h ( T, d ) .T = l e a f ( d ) ? ;T = t r e e ( l e a f ( d ) ,_A) ? ;T = t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ? ;T = t r e e ( t r e e ( t r e e ( l e a f ( d ) ,_A) ,_B) ,_C) ?ye s

11 / 18

Checking whether a Boolean formula is true

A Boolean formula F is a formula built out of theconnectives ∧ (conjunction), ∨ (disjunction) and ¬(negation), where the basic propositional atoms are trueand false.

Checking whether a Boolean formula is trueInput: A Boolean formula F .Question: Is F true?

Program on white board.

12 / 18

Extra-logical and built-in predicates

Arithmetic: Prolog has built in support for evaluatingarithmetical expressions.is/2 computes the value of its 2nd argument

(must be an arithmetical expression.).

For example:

| ?− X i s 1+2∗3.14.X = 7.28 ? ;no

Compare it with

| ?− X = 1+2∗3.14.X = 1+2∗3.14 ? ;no

</2 and >/2 evaluate both arguments as arithmeticexpressions.

13 / 18

Extra-logical and built-in predicates

Arithmetic: Prolog has built in support for evaluatingarithmetical expressions.is/2 computes the value of its 2nd argument

(must be an arithmetical expression.). For example:

| ?− X i s 1+2∗3.14.X = 7.28 ? ;no

Compare it with

| ?− X = 1+2∗3.14.X = 1+2∗3.14 ? ;no

</2 and >/2 evaluate both arguments as arithmeticexpressions.

13 / 18

Extra-logical and built-in predicates

Arithmetic: Prolog has built in support for evaluatingarithmetical expressions.is/2 computes the value of its 2nd argument

(must be an arithmetical expression.). For example:

| ?− X i s 1+2∗3.14.X = 7.28 ? ;no

Compare it with

| ?− X = 1+2∗3.14.X = 1+2∗3.14 ? ;no

</2 and >/2 evaluate both arguments as arithmeticexpressions.

13 / 18

Extra-logical and built-in predicates

Arithmetic: Prolog has built in support for evaluatingarithmetical expressions.is/2 computes the value of its 2nd argument

(must be an arithmetical expression.). For example:

| ?− X i s 1+2∗3.14.X = 7.28 ? ;no

Compare it with

| ?− X = 1+2∗3.14.X = 1+2∗3.14 ? ;no

</2 and >/2 evaluate both arguments as arithmeticexpressions.

13 / 18

All-solution predicates:Use �ndall/3 to �nd all solutions to a query. E.g.:

�ndall(X, append(_, X, [a,b,c]), Xs)

�nds all su�xes of the list [a,b,c].

Sort: Use sort/2 to sort a list and remove duplicates.Much more e�cient than writing your own sorting algorithms.

14 / 18

All-solution predicates:Use �ndall/3 to �nd all solutions to a query. E.g.:

�ndall(X, append(_, X, [a,b,c]), Xs)

�nds all su�xes of the list [a,b,c].

Sort: Use sort/2 to sort a list and remove duplicates.Much more e�cient than writing your own sorting algorithms.

14 / 18

Debugging logic programs

Logic programs introduces an extra dimension todebugging due to non-determinism. Need to ensure thatyour program only gives correct answers and that it givesall the correct answers.

trace: Use trace/0 to trace all predicates on all ports(CALL, EXIT, REDO, FAIL). Not practical for largeprograms.

spy: Use spy/1 to set a spy-point on a speci�cpredicate.

For testing your own code you can also use write/1 tooutput relevant information at certain stages in theprogram. NB: do not forget to �ush the bu�er with anewline with nl/0 � otherwise it might not be displayedimmediately.

15 / 18

Debugging logic programs

Logic programs introduces an extra dimension todebugging due to non-determinism. Need to ensure thatyour program only gives correct answers and that it givesall the correct answers.

trace: Use trace/0 to trace all predicates on all ports(CALL, EXIT, REDO, FAIL). Not practical for largeprograms.

spy: Use spy/1 to set a spy-point on a speci�cpredicate.

For testing your own code you can also use write/1 tooutput relevant information at certain stages in theprogram. NB: do not forget to �ush the bu�er with anewline with nl/0 � otherwise it might not be displayedimmediately.

15 / 18

Debugging logic programs

Logic programs introduces an extra dimension todebugging due to non-determinism. Need to ensure thatyour program only gives correct answers and that it givesall the correct answers.

trace: Use trace/0 to trace all predicates on all ports(CALL, EXIT, REDO, FAIL). Not practical for largeprograms.

spy: Use spy/1 to set a spy-point on a speci�cpredicate.

For testing your own code you can also use write/1 tooutput relevant information at certain stages in theprogram. NB: do not forget to �ush the bu�er with anewline with nl/0 � otherwise it might not be displayedimmediately.

15 / 18

Hints and tips

There are no �return values�!Using the result of a predicate is done by reusingvariables:

do_something ( Input , Output ) :−f i n d_th i ng ( Input , Thing ) ,p r o c e s s ( Thing , Output ) .

Not

do_something ( I npu t ) :−Thing = f i n d_th i n g s ( I npu t ) ,p r o c e s s ( Thing )

Prolog data are (uninterpreted) terms.5+6 is not 11. (Canonically writen, it is +(5,6)).

Arithmetic expressions are evaluated to numbers only ina special context of a few built-in predicates (e.g. is/2)

16 / 18

Hints and tips

There are no �return values�!Using the result of a predicate is done by reusingvariables:

do_something ( Input , Output ) :−f i n d_th i ng ( Input , Thing ) ,p r o c e s s ( Thing , Output ) .

Not

do_something ( I npu t ) :−Thing = f i n d_th i n g s ( I npu t ) ,p r o c e s s ( Thing )

Prolog data are (uninterpreted) terms.5+6 is not 11. (Canonically writen, it is +(5,6)).

Arithmetic expressions are evaluated to numbers only ina special context of a few built-in predicates (e.g. is/2)

16 / 18

Hints and tips

Recursion, not for-loops.

No space between a symbol and its arguments.f(x) is a valid term, f (x) is not.

Facts, not functions.Each fact and rule should be literally correct, taken as astatement by itself.

Prolog is not typed.The query member([1, 2, 3], 1) will just fail and not giveany warning.

Upper case means variables, lower case foreverything else.member(X , [x , y , z ]) and member(x , [x , y , z ]) givecompletely di�erent results.

17 / 18

Hints and tips

Recursion, not for-loops.

No space between a symbol and its arguments.f(x) is a valid term, f (x) is not.

Facts, not functions.Each fact and rule should be literally correct, taken as astatement by itself.

Prolog is not typed.The query member([1, 2, 3], 1) will just fail and not giveany warning.

Upper case means variables, lower case foreverything else.member(X , [x , y , z ]) and member(x , [x , y , z ]) givecompletely di�erent results.

17 / 18

Hints and tips

Recursion, not for-loops.

No space between a symbol and its arguments.f(x) is a valid term, f (x) is not.

Facts, not functions.Each fact and rule should be literally correct, taken as astatement by itself.

Prolog is not typed.The query member([1, 2, 3], 1) will just fail and not giveany warning.

Upper case means variables, lower case foreverything else.member(X , [x , y , z ]) and member(x , [x , y , z ]) givecompletely di�erent results.

17 / 18

Hints and tips

Recursion, not for-loops.

No space between a symbol and its arguments.f(x) is a valid term, f (x) is not.

Facts, not functions.Each fact and rule should be literally correct, taken as astatement by itself.

Prolog is not typed.The query member([1, 2, 3], 1) will just fail and not giveany warning.

Upper case means variables, lower case foreverything else.member(X , [x , y , z ]) and member(x , [x , y , z ]) givecompletely di�erent results.

17 / 18

Hints and tips

Recursion, not for-loops.

No space between a symbol and its arguments.f(x) is a valid term, f (x) is not.

Facts, not functions.Each fact and rule should be literally correct, taken as astatement by itself.

Prolog is not typed.The query member([1, 2, 3], 1) will just fail and not giveany warning.

Upper case means variables, lower case foreverything else.member(X , [x , y , z ]) and member(x , [x , y , z ]) givecompletely di�erent results.

17 / 18

Hints and tips

The Prolog bu�er is for queries, not programming.Unlike languages like Python, Perl, Ruby etc, you shouldnot create new predicates in the Prolog bu�er, onlyquery the existing set. Use the evaluateregion/�le/bu�er commands in emacs.

Be careful when you reorder your program.It is very easy to forget to change commas and periodsin your subqueries.

Sicstus allows in�nite structures to be created.

| ?− X = [ 1 |X ] .

X = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 | . . . ] ?

This can be occasionally be useful, but it can also be asource of confusing bugs, since they are meaningless inconventional logic.

18 / 18

Hints and tips

The Prolog bu�er is for queries, not programming.Unlike languages like Python, Perl, Ruby etc, you shouldnot create new predicates in the Prolog bu�er, onlyquery the existing set. Use the evaluateregion/�le/bu�er commands in emacs.

Be careful when you reorder your program.It is very easy to forget to change commas and periodsin your subqueries.

Sicstus allows in�nite structures to be created.

| ?− X = [ 1 |X ] .

X = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 | . . . ] ?

This can be occasionally be useful, but it can also be asource of confusing bugs, since they are meaningless inconventional logic.

18 / 18

Hints and tips

The Prolog bu�er is for queries, not programming.Unlike languages like Python, Perl, Ruby etc, you shouldnot create new predicates in the Prolog bu�er, onlyquery the existing set. Use the evaluateregion/�le/bu�er commands in emacs.

Be careful when you reorder your program.It is very easy to forget to change commas and periodsin your subqueries.

Sicstus allows in�nite structures to be created.

| ?− X = [ 1 |X ] .

X = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 | . . . ] ?

This can be occasionally be useful, but it can also be asource of confusing bugs, since they are meaningless inconventional logic.

18 / 18