Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution).

Similar presentations


Presentation on theme: "1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution)."— Presentation transcript:

1 1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution). n Facts & Rules (some theory); n Exercise

2 © L.Lúcio, 2003 2 The SWI Prolog interpreter n SWI-Prolog (http://www.swi-prolog.org/) is the Prolog interpreter we are going to use.http://www.swi-prolog.org/ u For Windows (at the university): F « Démarrer->Programmes->Programmation->SWIProlog2.8.0 » F Available text editor: Notepad, Wordpad u For Linux (at the university): F « /unige/SWI-Prolog/bin/pl » F Available text editor: emacs n Difference between Compilers and Interpreters?

3 © L.Lúcio, 2003 3 How to load a Prolog program n There are several ways: u Load directly from the OS: F In windows: double click on the program file; F In linux: « /unige/SWI-Prolog/bin/pl your_code.pl » u Load from the interpreter itself: F « ?- consult ‘path_to_code’ » or simpler F [‘path_to_code’] n Download the example at « http://cui.unige.ch/smv/teaching/li/material/exercises/animals.pl »

4 © L.Lúcio, 2003 4 The first Prolog program… ?- consult(‘animals.pl’). ?- listing. habitat(bear,forest). habitat(elephant,savanah). habitat(cat,house). brown(bear). black(cat). gray(elephant). dark(Z) :- black(Z). dark(Z) :- brown(Z). n A Prolog program is composed of facts (faits) and rules (règles). n After defining all the facts and rules, it is possible to ask the interpreter questions (requêtes). } } The facts define what the system knows its true. The rules define how new facts can be created

5 © L.Lúcio, 2003 5 Asking questions n We can now ask questions to the program: ?- black(cat). yes. It is a fact. The program knows it. ?- brown(cat). ?- no. The program never heard about brown cats, so it replies ‘no’. ?- habitat(X, house). X = bear. yes. To the question « Who lives in a house? » or «Which X lives in a house? », there is one solution (X = cat). X is a variable for which the program tries to find a solution.

6 © L.Lúcio, 2003 6 Asking questions (2) ?- dark(bear). Yes. There is no fact stating that a bear is dark. But the rule « dark(X) :- brown (X) » means « all the X that are brown are also dark ». ?- dark(X). Who is dark? What would be the answer to this one? n How would we ask the program the following question: « From all the animals, give me those who are dark and live in the forest ». u Hint: comma (virgule) defines the conjunction between questions

7 © L.Lúcio, 2003 7 Prolog’s syntax n From the example, we can see that: u Atoms (atomes) always start with small letters. Atoms are used for defining basic Facts or to name relations: F Basic fact: bear F Relation: habitat(bear,forest) In this case « habitat » is the Predicate of the relation. u Variables always start with capital letters: F Example: X, Animal, _variable u Atoms and Variables define Terms (termes): F Example: X, 5, bear, black(cat), habitat(X,forest) u The comma (virgule) defines conjunction between terms; F Example: brown(Animal), habitat(Animal,forest) means Animal is brown AND Animal lives in the forest.

8 © L.Lúcio, 2003 8 Prolog’s syntax (2) u A Fact is a Term with no Variables: F Example: cat, black(cat), habitat(elephant,savanah) u Rules (Régles) are defined by a head (one term) followed by « :- » followed by a conjunction of terms which are the conditions that make the head true: F Example: dark(Z) :- brown(Z). scary(Animal) :- dark(Animal),habitat(Animal,forest). u A Question or Goal (requête) is a conjuction of terms: F Example: dark(Animal),habitat(Animal,forest). u And, don’t forget: a Prolog Fact or Rule (also called Clauses) always ends with a dot «. ».

9 © L.Lúcio, 2003 9 Exercise n Add new facts to the program stating the size of an animal (ex: big, small); n Add a new rule saying that a scary animal is one who is dark, big and lives in the forest; n When you are done try adding more animals and playing with the program…


Download ppt "1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution)."

Similar presentations


Ads by Google