prolog programming introduction to prolog cs370d - cs 461

Download Prolog programming Introduction to Prolog CS370d - CS 461

If you can't read please download the document

Upload: gillian-maxwell

Post on 24-Dec-2015

233 views

Category:

Documents


6 download

TRANSCRIPT

  • Slide 1
  • Prolog programming Introduction to Prolog CS370d - CS 461
  • Slide 2
  • What is Prolog programming? Prolog is a programming language for symbolic, non-numeric commutation. It solves problems that involve objects and relations between objects.
  • Slide 3
  • 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.
  • Slide 4
  • Defining relations by facts The whole family tree can be described by the following facts: pam tom bob liz ann pat jim
  • Slide 5
  • 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
  • Slide 6
  • Defining relations by facts We can also add information about the sex of the people by the following facts: female(pam). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim).
  • Slide 7
  • Defining relations by facts female(pam) is a Unary relation parent(bob,pat) is a Binary relation. Think about this relation: sex(pam, feminist).
  • Slide 8
  • 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
  • Slide 9
  • 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:
  • Slide 10
  • SWI-Prolog After opening SWI Prolog environment, choose file -> new to create a new.pl file.
  • Slide 11
  • 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.
  • Slide 12
  • 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).
  • Slide 13
  • SWI-Prolog
  • Slide 14
  • Compile your program. Compile - > Make
  • Slide 15
  • SWI-Prolog Then click OK to save the changes on your file.
  • Slide 16
  • SWI-Prolog In SWI Prolog environment, load your program by file -> consult. Then choose your program.
  • Slide 17
  • 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.
  • Slide 18
  • Exercise 1: Write prolog program of your own family tree.