# LAB: Introduction to logic programming

This is and introductory lab to Prolog and logic programming.

For homework/report, please refer to UPEL.

## 1. Introduction to Prolog

Prolog is a logic programming language (PROgrammation en LOGique, PROgramming in LOGic).

If you want to know more, see the old Prolog lab page.

## 2. SWI Prolog

Existing implementations of Prolog compilers:

During this course, we will use the SWISH online compiler that implements swi-prolog.

## 3. Logic programming in Prolog

Prolog syntax:

• constants: string and numerical constants (atoms)
• variables (logical variables - not the same as `variables` in classical programming languages)
• terms: functions symbols with arguments, structurally complex objects.
• Terms denote objects in the universe of discourse
• Predicates denote the properties of the terms and relations among them
• logical connectors (and, or, negation, implication)

And that's it!

But all in all… if you dive in it, in Prolog, everything is a term , actually, even the whole program itself…

Now a program in Prolog consists of:

• a list of clauses, including:
• facts (simple, “atomic” clauses)
• rules (complex clauses)
• and a goal.

How to build a program?

• State something about the terms using predicates (predicate consists of a functor name and the arity (the number of arguments)) to create atoms
• add (or not) negation to create literals
• connect literals with logical operators to build clauses

### 3.1. A simple program

Take a look at the following family fdescription:

```parent(kasia,robert).
parent(tomek,robert).
parent(tomek,eliza).

female(kasia).
female(eliza).

male(tomek).
male(robert).```

Go to SWISH editor, start a new *program* and copy-paste the above code or design a similar one for your family.

Now test the program stating the following goal:

`?- female(X).`

After the first answer, click `Next`. Observe what happens.

### Listing the knowledge

Test the `listing/1` predicate:

`?- listing(parent).`

Test the `listing/0` predicate.

If we are not interested in the values of certain variables, we can use the so-calle anonymous variables. E.g, : “Does Robert have (any) parents?”

`?- parent(_,robert).`

### 3.2. Asking questions (stating goals)

We can now pose questions (goals) to the knowledge base system.

Who is a male?

`?- male(X).`

Is tomek a male?

`?- male(tomek).`

Is rex a male?

`?- male(rex).`

Is kasia a parent of robert?

`?- parent(kasia,robert).`

Whose parent is kasia?

`?- parent(kasia,X).`

Can we replace X with another symbol? What can it be?

Who is robert's parent?

`?- parent(Y,robert).`

Can we replace `Y` with another symbol?

### 3.3. Extending the program

Please extend the program adding more facts (e.g. as given below, but you can be creative)

```parent(kasia,robert).
parent(tomek,robert).
parent(tomek,eliza).
parent(robert,anna).
parent(robert,magda).
parent(magda,jan).

female(kasia).
female(eliza).
female(magda).
female(anna).

male(tomek).
male(robert).
male(jan).```

Do the order of the clauses matter?

☝ Knowing that we denote the conjunction with a comma (`,`), how to ask who is robert's mother and who is his father?

Do the names influence the results of the execution of the program? Are there any restriction imposed on the predicate names?

### 3.5. Rules

Add the following rules to the program analyze their meaning and test them:

```mother(X,Y) :-
parent(X,Y),
female(X).

father(X,Y) :-
parent(X,Y),
male(X).```

Define the rules for the following predicates:

• brother
• sister
• grandfather
• grandmother

Test if they are correct by asking e.g. “Who is a father of anna?”

What's the problem with brother/sister? What it the `\=` operator?

### 3.6. Recursion

Recursion is one of the fundamental mechanism in programming in Prolog. Analyze the following rules:

```ancestor(X,Y) :-
parent(X,Y).

ancestor(X,Z) :-
parent(X,Y),
ancestor(Y,Z).```

These two clauses describe one predicate: ancestor/2.

How to define:

• a descendant (an opposite of an ancestor)?
• a relative (a member of the family)? – Please write in the report what you mean by a “relative” as it may be understood in various ways

## 4. Arithmetic operations in Prolog

In Prolog, we don't do the arithmetic operations directly, but we use the predicate is/2.

1. Check the following operations/goals:

```?- X is 2 + 2.
?- Y is 2.5 + ( 4 / 2).
?- Z is 2 + 0.001.```

Warning:

```?- A is 3.
?- B is A + 4.
?- A is 3, B is A + 4.```

Arithmetic operations:

```?- X is 2 + 2.
?- X is 2 * 3.
?- X is 4 / 2.
?- X is 4 / 3.
?- X is 4 // 3.```

Be careful:

```?- X is 2 + 5.
?- X = 2 + 5.
?- 2 + 5 =:= 1 + 4.
?- 2 + 5 =:= 3 + 4.
?- 2 + 5 =:= 4 + 4.```

Test the following operators:

```?- 2 < 3.
?- 2 > 3.
?- 3 > 3.
?- 3 >= 3.
?- 3 =< 3.```

2. Write a program that calculates the result(s) of a Quadratic Equation `ax^2 + bx + c = 0` in real numbers. Implement the predicates:

• `delta/4` – calculate delta, the arguments are: A, B, C, Result
• `quadratic_eq/4` – calculating the result(s) of the quadratic equations, the arguments are: A, B, C, Result.

Note the non-determinism of the `quadratic_eq/4` predicate that can return 0, 1 or 2 Results; see also arithmetic functions, in particular square root function: `sqrt/1`.

## 5. Extra credit: More recursion

A typical application of recursion may be calculating the factorial - see the following code:

``` factorial(0,1).
factorial(Number,Result) :-
Number > 0,
NewNumber is  Number-1,
factorial(NewNumber,NewResult),
Result  is  Number*NewResult.```

Execute and analyze the factorial program.

Based on the factorial program, write a program that writes the Fibonacci sequence.

## 6. Extra credit: Selected problems modelled in Prolog

### 6.1. Map colouring

We have the following map:

```                 |Bialorus
|------------
Polska      |
---------------|
|         | Ukraina
Czechy| Slowacja|-----------
-----------------```

The task is to color the map with 3 colors such that none of the neughbouring countries are of the same color: Four_color_theorem

Let's define 3 colors

```color(red).
color(green).
color(blue).```

Define the predicate `map_coloring/5`, such that when one poses the question:

`?- map_coloring(Polska,Bialorus,Ukraina,Slowacja,Czechy).`

they get (all) the possibilities of valid coloring (the country variables “get” particular colors).

declare the contraints (of this particular map) in the predicate

operator `\=` denotes the “impossibility” for two things (variables) to be unified with the same constant.