PPAI/L4

From WikiZMSI

Spis treści

Part 4 - Cutting off

1. Define a clause function/2, that checks X and Y satisfy following conditions:

  • If X <3, then Y = 0.
  • If X ≥ 3 i X < 6, then Y = 2.
  • If X ≥ 6, then Y = 4.

Check:

function(1,Y),Y > 2. 

2. Get the program. Do the following:

2.1. Draw the tree of rules. The root of the tree contains:

top( X, Y)

leaves contains facts.

2.2. Ask if

top( X, Y). 

2.3. Change

true(1) 

to

! 

Ask again if

top( X, Y).

2.4. Change

true(2) 

to

! 

Ask again if

top( X, Y).

Tasks

1. Define a predicate element/2 to check if a given element is in the list. Using a cut-off, we should ensure that as soon as a solution is found, no further search is required. What is the difference between the member from the previous class and the one defined in this task?

2. Define a predicate add/3 that adds an element at the beginning of a given list. A predicate does not fail when argument 3 is a list containing the result of a connection. An addition should be done only if the item is not on the list yet, otherwise, the result is a given list. When using a cut-off, it should be ensured that as soon as it is checked that the item is on the list, no further search is required. Compare the behavior of add/3 with and without the cut-off operator.

3. Enter the command in the query mode ?- help(->). The Elseif.pro file shows the use of the operator ->.

4. Define a predicate max/3 based on ->

5. Suppose we have the following database:

p(1).
p(2) :- !.
p(3).

Write all of Prolog's answers to the following queries:

?- p(X).
?- p(X),p(Y).
?- p(X),!,p(Y).

6. Without using cut, write a predicate split/3 that splits a list of integers into two lists: one containing the positive ones (and zero), the other containing the negative ones. For example:

split([3,4,-5,-1,0,4,-9],P,N)

should return:

   P = [3,4,0,4]
   N = [-5,-1,-9].

Then improve this program, without changing its meaning, with the help of cut.

Negation in Prolog

Negation in Prolog is made by procedural meaning not logical, but results are more or less as we expect.

Not is the operator \+ or not(). Its definition is based on the fail/0 built-in predicate Not.pro.

Check help(not).

Problems using negation

Let's consider the code Rest.pro. Check in the query mode:

high_standard(X), recommended(X).
recommended(X), high_standard(X). 


7. More tasks