1.sample Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1)

girl(priya).
girl(tiyasha).
girl(jaya).
boy(kaushal).
can_cook(priya).
can_dance(kaushal).

Save as sample.pl
Run as [sample].
Output:
| ?- [hello].
compiling C:/GNU-Prolog/hello.pl for byte code...
C:/GNU-Prolog/hello.pl compiled, 3 lines read - 469 bytes written, 12 ms

yes
| ?- girl(priya).

yes
| ?- boy(priya).
uncaught exception: error(existence_error(procedure,boy/1),top_level/0)
| ?- can_cook(jaya).

no
| ?- [hello].
compiling C:/GNU-Prolog/hello.pl for byte code...
C:/GNU-Prolog/hello.pl compiled, 5 lines read - 719 bytes written, 10 ms

yes
| ?- can_cook(jaya).

no
| ?- can_cook(kaushal).

no
| ?- boy(kaushal).

Yes
2)
sing_a_song(ananya).
listens_to_music(rohit).
listens_to_music(ananya) :- sing_a_song(ananya).
happy(ananya) :- sing_a_song(ananya).
happy(rohit) :- listens_to_music(rohit).
playes_guitar(rohit) :- listens_to_music(rohit).
Output:
D:/TP Prolog/Sample_Codes/kb2.pl compiled, 6 lines read - 1066 bytes written,
15 ms

yes
| ?- happy(rohit).

yes
| ?- sing_a_song(rohit).

no
| ?- sing_a_song(ananya).

yes
| ?- playes_guitar(rohit).

yes
| ?- playes_guitar(ananya).

no
| ?- listens_to_music(ananya).

yes

3)

can_cook(priya).
can_cook(jaya).
can_cook(tiyasha).
likes(priya,jaya) :- can_cook(jaya).
likes(priya,tiyasha) :- can_cook(tiyasha).

D:/TP Prolog/Sample_Codes/kb3.pl compiled, 5 lines read - 737 bytes written,


22 ms
warning: D:/TP Prolog/Sample_Codes/kb3.pl:1: redefining procedure
can_cook/1
D:/TP Prolog/Sample_Codes/kb1.pl:4: previous definition
yes
| ?- can_cook(X).
X = priya ? ;
X = jaya ? ;
X = tiyasha
yes
| ?- likes(priya,X).
X = jaya ? ;
X = tiyasha
Yes

4)
Example : Below food table shows the facts, rules, goals and their english meanings.
Facts English meanings
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner
Rules
// Every food is a meal OR
meal(X) :- food(X). Anything is a meal if it is a
food
Queries / Goals
?- food(pizza). // Is pizza a food?
// Which food is meal and
?- meal(X), lunch(X).
lunch?
?- dinner(sandwich). // Is sandwich a dinner?

Example 5 : Below student-professor relation table shows the facts, rules, goals and their
english meanings.
Facts English meanings
studies(charlie, csc135). // charlie studies csc135
studies(olivia, csc135). // olivia studies csc135
studies(jack, csc131). // jack studies csc131
studies(arthur, csc134). // arthur studies csc134
teaches(kirke, csc135). // kirke teaches csc135
teaches(collins, csc131). // collins teaches csc131
teaches(collins, csc171). // collins teaches csc171
teaches(juniper, csc134). // juniper teaches csc134
Rules
professor(X, Y) :- // X is a professor of Y if X
teaches(X, C), studies(Y, C). teaches C and Y studies C.

Queries / Goals
?- studies(charlie, What). // charlie studies what? OR
What does charlie study?
?- professor(kirke, Students). // Who are the students of
professor kirke.
6)
% Facts

1. Ram likes mango.


2. Seema is a girl.
3. Bill likes Cindy.
4. Rose is red.
5. John owns gold.

% Clauses

likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).

Output:
% Queries

?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= cindy

?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold

You might also like