Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

Prolog Basics

Getting Started
 There are only three basic constructs in Prolog: facts,
rules, and queries
 A collection of facts and rules is called a knowledge
base (or a database)
The syntax for a fact is
 Predicate(arg1, arg2, ... argN).
 E.g1. man(socrates). Socrates is a man.
 E.g2. A pie is good = good(pie).
 Selam is a person.
 Selam likes chocolates.
Getting Started
The syntax for a rule is
 <head>:-<body>.
 E.g1. mortal(X) :- man(X). All men are mortal
The syntax for a Query is
 the same form as facts
Construct facts and rule in prolog form
1. John likes Merry
2. Merry likes John
3. Jack likes Merry
4. X and Y are friends if X likes Y and Y likes X
likes(john, marry).
likes(marry, john).
likes(jack, merry).
friends(X,Y):-likes(X,Y), likes(Y,X).
friends(X,Y).
Construct facts, rules and queries in prolog form

1. burger is a food 1. food(burger).


2. sandwich is a food 2. food(sandwich).
3. pizza is a food 3. food(pizza).
4. sandwich is a lunch
4. lunch(sandwich).
5. pizza is a dinner
5. dinner(pizza).
6. Every food is a meal
6. meal(X) :- food(X).
7. Is pizza a food?
7. food(pizza).
8. Which food is meal and lunch?
8. meal(X), lunch(X).
9. Is sandwich a dinner?
9. dinner(sandwich).
Try below queries by yourself and find out why did
you get those answers.

1. meal(X), dinner(X).
2. meal(What).
3. meal(X), dinner(Y).

You might also like