Prolog Basics To edit your prolog source code

Prolog Basics

• To edit your prolog source code 1. edit(file(‘file name’)). ---lets you editting a new file with the given file name with the default editor pceemacs e. g ? -edit(file(‘fruit. pl’)). Without extension(. pl) is also possible 2. ? -emacs. —will open the default editor then you can create your own new prolog file 3. use editors like notepad and save it with extension. pl or without
![• To compile your prolog source code 1. [‘file name’]. 2. Open the • To compile your prolog source code 1. [‘file name’]. 2. Open the](http://slidetodoc.com/presentation_image_h2/65043900be42b2e2fd417995ffa609ac/image-3.jpg)
• To compile your prolog source code 1. [‘file name’]. 2. Open the file directly if it is a prolog source file(with. pl extention) If there is error in your code correct otherwise you will see an error free replay ‘true’ from prolog

3. Consulting program files: - Program files can be compiled using the built-in predicate consult/1. The argument has to be a Prolog atom denoting the program file you want to compile. For example, to compile the file biganimals. pl submit the following query to The /1 is used to Prolog: indicate that this predicate takes one ? - consult('big-animals. pl'). argument. If the compilation is successful, Prolog will reply with Yes. Otherwise a list of errors will be displayed.

• Once you compile the Knowledge base successfully you can make query to prolog • NB the consult predicate can also be accessed from the file menu FACTS • Example Predicate Name Edit(file (‘familly. pl’)). color(red). Arguments father_of(john, tom). mother_of(susan, tom). A period marks the parents(john, susan, tom). end of statement Save the file QUERY ? -[‘familly. pl’]. ? -parents(X, Y, tom). • Predicates and arguments should start with small letters while variables in capital letter

• To give comments you can use % e. g color(red) % red is a color • Programes in prolog consists of facts and rules In the general form of goals P: -p 1, p 2, p 3, …pn. P=the rules head and p 1, p 2, …pn are subgoals Normally this expression is the horn clause which states that the head goal, p, is satisfied iff the sub goals are satisfied.

• The , ’s separating the subgoals can be considered as the logical AND and the symbol : - is interpreted as IF • If only P or the head exists it is considered as true i. e it is a fact • So in prolog programming predicates without subgoals are called facts. • For example

• Another way of thinking about facts is they are unconditional conclusions which do not depend on anythingelse. In contrast rules are conditional conclusions which depends on one or more conditions • For example parent(X, Y): -mother(X, Y). parent(X, Y): -father(X, Y).

• Consider the following family hierarchy selam alemu taye bayu genet terunesh tsehay mulugeta hirut solomon reda alem almaz

• Consider the following prolog program. father(alemu, selam). mother(genet, selam). father(taye, alemu). mother(terunesh, alemu). parent(X, Y): -father(X, Y). parent(X, Y): -mother(X, Y). When we need more output we use ; otherwise. (period)



? - ancesstor(X, selam). • Improve the above prolog program so that. X = alemu ; X = genet ; it would give the X = taye ; following output for X = solomon ; corresponding queries. X=bayu; X=mulugeta; ? -grandparent(X, selam). X=reda; X = taye ; X=terunesh; X = solomon ; X=Almaz; X = terunesh ; X=tsehay; X = almaz ; X=hirut; false. X=Alem; false

Translate the above semantic net into prolog statements that expresses a family relation ship and make possible to infer Ann and bill are the grandparents of john even though there is no explicit link labeled “grandfather_of. ”
- Slides: 14