Prolog programming Introduction to Prolog CS370d
What is Prolog programming? Prolog is a programming language for symbolic , non-numeric commutation. It solves problems that involve objects and relations between objects.
Defining relations by facts The fact that Tom is a parent of Bob can be written in prolog as: parent(tom, bob). tom and bob are objects, parent(tom, bob) is a relation between these objects.
Defining relations by facts The whole family tree can be described by the following facts: pam tom bob liz ann pat jim
Defining relations by facts The whole family tree can be described by the following facts: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). pam tom bob liz ann pat jim
Defining relations by facts We can also add information about the gender of the people by the following facts: female(pam). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim).
Defining relations by facts female(pam) is a Unary relation parent(bob,pat) is a Binary relation. Think about this relation: sex(pam, feminist).
SWI-Prolog SWI-Prolog editor is an open source implementation of the programming language . Prolog is a free software and you can download it from here: http://www.swi-prolog.org/Download.html
SWI-Prolog Getting started: Any Prolog program in SWI-Prolog is written in a file with extension .pl. You can write your own one by following these steps:
SWI-Prolog After opening SWI Prolog environment, choose file -> new to create a new.pl file.
SWI-Prolog This will open a new window to create the new file, choose any name for your programs (here we name it tree) then choose to save it.
SWI-Prolog Now the .pl file we have just created will be opened, and we can write our own program. Let's write the following facts about parent , male and female relationships: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). female(pam). female(liz). female(pat). female(ann). male(tom). male(bob). male(jim).
SWI-Prolog
SWI-Prolog Compile your program. Compile - > Make
SWI-Prolog Then click “ OK ” to save the changes on your file.
SWI-Prolog In SWI Prolog environment, load your program by file -> consult. Then choose your program .
Questions in prolog. Prolog can posed questions about any relation. So, we can ask a question about Parent relation, For example : Is bob a parent of Pat? ?- parent(bob,pat). The answer will be true.
Exercise 1: Write prolog program of your own family tree .