SlideShare a Scribd company logo
1 of 9
Download to read offline
EXP NO. 1
STUDY OF PROLOG, its insatllation procedure and working.
Prolog – Programming in Logic
PROLOG stands for Programming In Logic – an idea that emerged in the early 1970s to use logic
as programming language. PROLOG is a programming language centered around a small set of
basic mechanisms, including pattern matching, tree-based data structuring and automatic
backtracking.
FACTS , RULES AND QUERIES
Programming in PROLOG is accomplished by creating a database of facts and rules about objects,
their properties, and their relationships to other objects. Queries then can be posed about the objects
and valid conclusions will be determined and returned by the program.
For example:
1. Facts : Some facts about family relationships could be written as :
sister(sue, bill)
parent(ann, sam)
parent(joe,ann)
male(joe)
female(ann)
2. Rules : To represent the general rule for grandfather , we write :
Grandfather(X,Z):-
parent(X,Y),
parent(Y,Z),
male(X).
3. Queries : Given a data of facts and rules such as that above, we mat make queries by tying after
a query symbol ‘?_’ statements such as :
?_parent(X,sam)
X=ann
?_male(joe)
true
?_grandfather(X,Y)
X=joe;
Y=sam
?_female(joe)
false
Installation & Working with Prolog:
1. Download swi-prolog stable version
2. Run the installer
3. After SWI-Prolog has been installed on a Windows system, the following important new
things are available to the user:
➢ A folder (called directory in the remainder of this document) called swipl containing
the executables, libraries, etc., of the system. No files are installed outside this
directory.
➢ A program swipl-win.exe, providing a window for interaction with Prolog. The
program swipl.exe is a version of SWI-Prolog that runs in a console window.
➢ The file extension .pl is associated with the program swipl-win.exe. Opening a .pl file
will cause swipl-win.exe to start, change directory to the directory in which the file to
open resides, and load this file.
4. Create a notepad file with extension <filename>.pl, which consists of set of procedures
and each procedure consists of clauses. There are two types of clauses: facts and rules.
Order of these clauses are important.
➢ facts: That does not depend on any other information. A fact must start with a
predicate (which is an atom) and end with a fullstop.
➢ predicates: That provide information about individuals.A predicate can take any fixed
number of ARGUMENTS (parameters).
➢ The arguments can be atoms (in this case, these atoms are treated as constants),
numbers, variables or lists. Arguments are separated by commas.
The number of arguments that a predicate takes is called its ARITY ( like unary,
binary, ternary, and the like). Two distinct predicates can have the same name if they
have different arities; thus you might have both:
-- mother(melody), meaning Melody is a mother, and
-- mother(melody,sharon), meaning Melody is the mother of Sharon.
In a Prolog program, a presence of a fact indicates a statement that is true. An absence
of a fact indicates a statement that is not true.
➢ rules: the real power of Prolog is in rules. While facts state the relation explicitely,
rules define the relation in a more general way. Each rule has its head - name of the
defined relation, and its body - a real definition of the relation. A rule can be viewed as
an extension of a fact with added conditions that also have to be satisfied for it to be
true. In other words, A rule is a predicate expression that uses logical implication (:-) to
describe a relationship among facts.
Thus a Prolog rule takes the form left_hand_side :- right_hand_side .
This sentence is interpreted as: left_hand_side if right_hand_side.
The left_hand_side is restricted to a single, positive, literal, which means it must
consist of a positive atomic expression. It cannot be negated and it cannot contain
logical connectives.
➢ Goals: A goal is a statement starting with a predicate and probably followed by its
arguments. In a valid goal, the predicate must have appeared in at least one fact or
rule in the consulted program, and the number of arguments in the goal must be the
same as that appears in the consulted program. Also, all the arguemnts (if any) are
constants.
The purpose of submitting a goal is to find out whether the statement represented by
the goal is true according to the knowledge database (i.e. the facts and rules in the
consulted program).
➢ Queries: A query is a statement starting with a predicate and followed by its
arguments, some of which are variables. Similar to goals, the predicate of a valid
query must have appeared in at least one fact or rule in the consulted program, and
the number of arguments in the query must be the same as that appears in the
consulted program.
The purpose of submitting a query is to find values to substitute into the variables in
the query such that the query is satisfied. This is similar to asking a question, asking
for "what values will make my statement true".
5. In prolog window run [<filename>]. or consult [<filename>]. Result should be 'true'. If file
doesnot exist there, it will result 'false'].
if you want to enter fully qualified file name, then enter ['path..to..file/filename.pl'].
6. (dot). mark the end of command.
7. listing. displays the content of loaded program.
8. use command 'halt' with a . (dot) to stop the prolog system.
EXP NO. 2
Write a prolog code having facts and rules about students and the courses they are studying.
Query about the connection among students of different courses.
PROLOG CODE:
/* Facts */
studies(kanika,cse).
studies(niharika,cse).
studies(shivani,cse).
studies(rakhi,cse).
studies(chanchal, cse).
studies(akram, cse).
studies(alka, cse).
studies(shabistan, cse).
studies(prince, ece).
/* Rules */
batchmate(X,Y):­ studies(X,Z),studies(Y,Z).
OUTPUT
EXP NO. 3
Find all paths between any two vertices in the given graph:
PROLOG CODE:
/* facts: */
edge(1,2). 
edge(1,3). 
edge(1,4). 
edge(2,3). 
edge(2,5). 
edge(3,4). 
edge(3,5). 
edge(4,5). 
/* rules: */
connected(X,Y) :­ edge(X,Y) ; edge(Y,X). 
path(A,B,Path) :­
travel(A,B,[A],Q),
    reverse(Q,Path).
travel(A,B,P,[B|P]) :­
connected(A,B).
travel(A,B,Visited,Path) :­
connected(A,C),
C == B,
    not(member(C,Visited)), 
    travel(C,B,[C|Visited],Path). 
OUTPUT
EXP NO. 4
"MURDER MYSTERY" Problem
Alice, her husband, son, daughter, and brother are involved in a murder. One of the five killed
one of the other four. Who was the killer?
1. A man and a woman were together in the bar at the time of the murder.
2. The victim and the killer were together on the beach at the time of the murder.
3. One of the children was alone at the time of the murder.
4. Alice and her husband were not together at the time of the murder.
5. The victim's twin was innocent.
6. The killer was younger than the victim.
PROLOG CODE
/*­­­­­ Facts ­­­­­*/
person(alice).
person(husband).
person(son).
person(daughter).
person(brother).
child(son).
child(daughter).
male(husband).
male(son).
male(brother).
female(alice).
female(daughter).
twin(alice,    brother).
twin(brother,  alice).
twin(son,      daughter).
twin(daughter, son).
/*­­­­­ Rules ­­­­­*/
istwin(X) :­ twin(X, _).
older(alice,   son).
older(alice,   daughter).
older(husband, son).
older(husband, daughter).
inbar(M, N) :­ person(M), person(N),
               male(M),   female(N).
together(S, T) :­ S=alice, T=husband.
together(S, T) :­ T=alice, S=husband.
alone(P) :­ person(P), child(P).
/*­­­­­ Rule Combining Clues ­­­­­*/
solution(Killer, Victim, InBarA, InBarB, Alone) :­
    person(Killer), person(Victim),
    /* The victim's twin was innocent. */
        istwin(Victim), + twin(Victim, Killer),
    /* The killer was younger than the victim. */
+ older(Killer, Victim),
    /* not the same as "older(Victim, Killer)"! */
    /* Alice and her husband were not together 
       at the time of the murder. */
        + together(Killer, Victim), Killer = Victim,
    /* A man and a woman were together in the 
       at the time of the murder. */
        inbar(InBarA, InBarB),
InBarA = Killer, InBarB = Killer,
InBarA = Victim, InBarB = Victim,
    /* Alice and her husband were not together 
       at the time of the murder. */
        + together(InBarA, InBarB),
    /* One of the children was alone at the 
       time of the murder. */
alone(Alone),
Alone = InBarA, Alone = InBarB,
Alone = Killer, Alone = Victim.
/*­­­­­ Goal ­­­­­*/
print_solution :­
    /* Find the solution */
        solution(Killer, Victim, InBarA, InBarB, Alone),
    /* Write solution */
nl,
                write(Killer),   write('   killed   '),   write(Victim),
write('.'), nl,
write(InBarA), write(' and '), write(InBarB),
write(' were together in the bar.'), nl,
write(Alone), write(' was alone.'), nl, nl.
?­ print_solution.
OUTPUT
EXP NO. 5
Missionaries and cannibals. Suppose 3 missionaries and 3 cannibals are walking together
through the forest. They arrive at a river they have to cross, but there is only one boat, and
that boat can carry at most 2 people. Of course, for the boat to cross the river, there should be
at least one person (missionary or cannibal) in the boat (to row the boat). The problem is that
if there are more cannibals than missionaries at any place, they will eat the missionaries.
Write a program that finds a strategy for the six people to cross the river without a missionary
being eaten.
PROLOG CODE
start([3,3,left,0,0]).
goal([0,0,right,3,3]).
legal(CL,ML,CR,MR) :­
/* is this state a legal one? */
ML>=0, CL>=0, MR>=0, CR>=0,
(ML>=CL ; ML=0),
(MR>=CR ; MR=0).
/* Possible moves: */
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):­
/* Two missionaries cross left to right. */
MR2 is MR+2,
ML2 is ML­2,
legal(CL,ML2,CR,MR2).
move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):­
/* Two cannibals cross left to right. */
CR2 is CR+2,
CL2 is CL­2,
legal(CL2,ML,CR2,MR).
move([CL,ML,left,CR,MR],[CL2,ML2,right,CR2,MR2]):­
/* One missionary and one cannibal cross left to right. */
CR2 is CR+1,
CL2 is CL­1,
MR2 is MR+1,
ML2 is ML­1,
legal(CL2,ML2,CR2,MR2).
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):­
/* One missionary crosses left to right. */
MR2 is MR+1,
ML2 is ML­1,
legal(CL,ML2,CR,MR2).
move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):­
/* One cannibal crosses left to right. */
CR2 is CR+1,
CL2 is CL­1,
legal(CL2,ML,CR2,MR).
move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):­
/* Two missionaries cross right to left. */
MR2 is MR­2,
ML2 is ML+2,
legal(CL,ML2,CR,MR2).
move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):­
/* Two cannibals cross right to left. */
CR2 is CR­2,
CL2 is CL+2,
legal(CL2,ML,CR2,MR).
move([CL,ML,right,CR,MR],[CL2,ML2,left,CR2,MR2]):­
/* One missionary and one cannibal cross right to left. */
CR2 is CR­1,
CL2 is CL+1,
MR2 is MR­1,
ML2 is ML+1,
legal(CL2,ML2,CR2,MR2).
move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):­
/* One missionary crosses right to left. */
MR2 is MR­1,
ML2 is ML+1,
legal(CL,ML2,CR,MR2).
move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):­
/* One cannibal crosses right to left. */
CR2 is CR­1,
CL2 is CL+1,
legal(CL2,ML,CR2,MR).
/* Recursive call to solve the problem */
path([CL1,ML1,B1,CR1,MR1],[CL2,ML2,B2,CR2,MR2],Explored,MovesList)
:­ 
   move([CL1,ML1,B1,CR1,MR1],[CL3,ML3,B3,CR3,MR3]), 
   not(member([CL3,ML3,B3,CR3,MR3],Explored)),
      path([CL3,ML3,B3,CR3,MR3],[CL2,ML2,B2,CR2,MR2],
[[CL3,ML3,B3,CR3,MR3]|Explored],[   [[CL3,ML3,B3,CR3,MR3],
[CL1,ML1,B1,CR1,MR1]] | MovesList ]).
/* Solution found */
path([CL,ML,B,CR,MR],[CL,ML,B,CR,MR],_,MovesList):­ 
output(MovesList).
/* Printing */
output([]) :­ nl. 
output([[A,B]|MovesList]) :­ 
output(MovesList), 
    write(B), write(' ­> '), write(A), nl.
/* Find the solution for the missionaries and cannibals problem */
find :­ 
   path([3,3,left,0,0],[0,0,right,3,3],[[3,3,left,0,0]],_).
OUTPUT

More Related Content

What's hot

What's hot (20)

Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Problem solving agents
Problem solving agentsProblem solving agents
Problem solving agents
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Data structure tries
Data structure triesData structure tries
Data structure tries
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rules
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 

Similar to Ai lab manual

UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptxUOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptxqasim ali
 
Prolog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfProlog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfCS With Logic
 
________ ________1.ppt
________ ________1.ppt________ ________1.ppt
________ ________1.pptSamiAAli44
 
An introduction to Prolog language slide
An introduction to Prolog language slideAn introduction to Prolog language slide
An introduction to Prolog language slide2021uam4641
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Sabu Francis
 
6&7-Query Languages & Operations.ppt
6&7-Query Languages & Operations.ppt6&7-Query Languages & Operations.ppt
6&7-Query Languages & Operations.pptBereketAraya
 
NAISTビッグデータシンポジウム - 情報 松本先生
NAISTビッグデータシンポジウム - 情報 松本先生NAISTビッグデータシンポジウム - 情報 松本先生
NAISTビッグデータシンポジウム - 情報 松本先生ysuzuki-naist
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)Nitesh Singh
 
MACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULEMACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULEDrBindhuM
 

Similar to Ai lab manual (20)

UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptxUOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
 
Deductive databases
Deductive databasesDeductive databases
Deductive databases
 
Prolog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfProlog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdf
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
 
Prolog final
Prolog finalProlog final
Prolog final
 
Ics1019 ics5003
Ics1019 ics5003Ics1019 ics5003
Ics1019 ics5003
 
________ ________1.ppt
________ ________1.ppt________ ________1.ppt
________ ________1.ppt
 
AI Lab Manual.docx
AI Lab Manual.docxAI Lab Manual.docx
AI Lab Manual.docx
 
ICS1019.pdf
ICS1019.pdfICS1019.pdf
ICS1019.pdf
 
An introduction to Prolog language slide
An introduction to Prolog language slideAn introduction to Prolog language slide
An introduction to Prolog language slide
 
Chapter 5 (final)
Chapter 5 (final)Chapter 5 (final)
Chapter 5 (final)
 
Ics1019 ics5003
Ics1019 ics5003Ics1019 ics5003
Ics1019 ics5003
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1
 
Mcs 021
Mcs 021Mcs 021
Mcs 021
 
6&7-Query Languages & Operations.ppt
6&7-Query Languages & Operations.ppt6&7-Query Languages & Operations.ppt
6&7-Query Languages & Operations.ppt
 
Plc part 4
Plc  part 4Plc  part 4
Plc part 4
 
NAISTビッグデータシンポジウム - 情報 松本先生
NAISTビッグデータシンポジウム - 情報 松本先生NAISTビッグデータシンポジウム - 情報 松本先生
NAISTビッグデータシンポジウム - 情報 松本先生
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
 
MACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULEMACHINE LEARNING-LEARNING RULE
MACHINE LEARNING-LEARNING RULE
 

More from Shipra Swati

Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process SchedulingShipra Swati
 
Operating System-Concepts of Process
Operating System-Concepts of ProcessOperating System-Concepts of Process
Operating System-Concepts of ProcessShipra Swati
 
Operating System-Introduction
Operating System-IntroductionOperating System-Introduction
Operating System-IntroductionShipra Swati
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Shipra Swati
 
Fundamental of Information Technology - UNIT 6
Fundamental of Information Technology - UNIT 6Fundamental of Information Technology - UNIT 6
Fundamental of Information Technology - UNIT 6Shipra Swati
 
Fundamental of Information Technology
Fundamental of Information TechnologyFundamental of Information Technology
Fundamental of Information TechnologyShipra Swati
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationShipra Swati
 

More from Shipra Swati (20)

Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process Scheduling
 
Operating System-Concepts of Process
Operating System-Concepts of ProcessOperating System-Concepts of Process
Operating System-Concepts of Process
 
Operating System-Introduction
Operating System-IntroductionOperating System-Introduction
Operating System-Introduction
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Java unit 14
Java unit 14Java unit 14
Java unit 14
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
Java unit 2
Java unit 2Java unit 2
Java unit 2
 
Java unit 1
Java unit 1Java unit 1
Java unit 1
 
OOPS_Unit_1
OOPS_Unit_1OOPS_Unit_1
OOPS_Unit_1
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7Fundamental of Information Technology - UNIT 7
Fundamental of Information Technology - UNIT 7
 
Fundamental of Information Technology - UNIT 6
Fundamental of Information Technology - UNIT 6Fundamental of Information Technology - UNIT 6
Fundamental of Information Technology - UNIT 6
 
Fundamental of Information Technology
Fundamental of Information TechnologyFundamental of Information Technology
Fundamental of Information Technology
 
Disk Management
Disk ManagementDisk Management
Disk Management
 
File Systems
File SystemsFile Systems
File Systems
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

Ai lab manual

  • 1. EXP NO. 1 STUDY OF PROLOG, its insatllation procedure and working. Prolog – Programming in Logic PROLOG stands for Programming In Logic – an idea that emerged in the early 1970s to use logic as programming language. PROLOG is a programming language centered around a small set of basic mechanisms, including pattern matching, tree-based data structuring and automatic backtracking. FACTS , RULES AND QUERIES Programming in PROLOG is accomplished by creating a database of facts and rules about objects, their properties, and their relationships to other objects. Queries then can be posed about the objects and valid conclusions will be determined and returned by the program. For example: 1. Facts : Some facts about family relationships could be written as : sister(sue, bill) parent(ann, sam) parent(joe,ann) male(joe) female(ann) 2. Rules : To represent the general rule for grandfather , we write : Grandfather(X,Z):- parent(X,Y), parent(Y,Z), male(X). 3. Queries : Given a data of facts and rules such as that above, we mat make queries by tying after a query symbol ‘?_’ statements such as : ?_parent(X,sam) X=ann ?_male(joe) true ?_grandfather(X,Y) X=joe; Y=sam ?_female(joe) false Installation & Working with Prolog: 1. Download swi-prolog stable version 2. Run the installer 3. After SWI-Prolog has been installed on a Windows system, the following important new things are available to the user: ➢ A folder (called directory in the remainder of this document) called swipl containing the executables, libraries, etc., of the system. No files are installed outside this directory. ➢ A program swipl-win.exe, providing a window for interaction with Prolog. The program swipl.exe is a version of SWI-Prolog that runs in a console window. ➢ The file extension .pl is associated with the program swipl-win.exe. Opening a .pl file will cause swipl-win.exe to start, change directory to the directory in which the file to open resides, and load this file. 4. Create a notepad file with extension <filename>.pl, which consists of set of procedures and each procedure consists of clauses. There are two types of clauses: facts and rules. Order of these clauses are important.
  • 2. ➢ facts: That does not depend on any other information. A fact must start with a predicate (which is an atom) and end with a fullstop. ➢ predicates: That provide information about individuals.A predicate can take any fixed number of ARGUMENTS (parameters). ➢ The arguments can be atoms (in this case, these atoms are treated as constants), numbers, variables or lists. Arguments are separated by commas. The number of arguments that a predicate takes is called its ARITY ( like unary, binary, ternary, and the like). Two distinct predicates can have the same name if they have different arities; thus you might have both: -- mother(melody), meaning Melody is a mother, and -- mother(melody,sharon), meaning Melody is the mother of Sharon. In a Prolog program, a presence of a fact indicates a statement that is true. An absence of a fact indicates a statement that is not true. ➢ rules: the real power of Prolog is in rules. While facts state the relation explicitely, rules define the relation in a more general way. Each rule has its head - name of the defined relation, and its body - a real definition of the relation. A rule can be viewed as an extension of a fact with added conditions that also have to be satisfied for it to be true. In other words, A rule is a predicate expression that uses logical implication (:-) to describe a relationship among facts. Thus a Prolog rule takes the form left_hand_side :- right_hand_side . This sentence is interpreted as: left_hand_side if right_hand_side. The left_hand_side is restricted to a single, positive, literal, which means it must consist of a positive atomic expression. It cannot be negated and it cannot contain logical connectives. ➢ Goals: A goal is a statement starting with a predicate and probably followed by its arguments. In a valid goal, the predicate must have appeared in at least one fact or rule in the consulted program, and the number of arguments in the goal must be the same as that appears in the consulted program. Also, all the arguemnts (if any) are constants. The purpose of submitting a goal is to find out whether the statement represented by the goal is true according to the knowledge database (i.e. the facts and rules in the consulted program). ➢ Queries: A query is a statement starting with a predicate and followed by its arguments, some of which are variables. Similar to goals, the predicate of a valid query must have appeared in at least one fact or rule in the consulted program, and the number of arguments in the query must be the same as that appears in the consulted program. The purpose of submitting a query is to find values to substitute into the variables in the query such that the query is satisfied. This is similar to asking a question, asking for "what values will make my statement true". 5. In prolog window run [<filename>]. or consult [<filename>]. Result should be 'true'. If file doesnot exist there, it will result 'false']. if you want to enter fully qualified file name, then enter ['path..to..file/filename.pl']. 6. (dot). mark the end of command. 7. listing. displays the content of loaded program. 8. use command 'halt' with a . (dot) to stop the prolog system.
  • 3. EXP NO. 2 Write a prolog code having facts and rules about students and the courses they are studying. Query about the connection among students of different courses. PROLOG CODE: /* Facts */ studies(kanika,cse). studies(niharika,cse). studies(shivani,cse). studies(rakhi,cse). studies(chanchal, cse). studies(akram, cse). studies(alka, cse). studies(shabistan, cse). studies(prince, ece). /* Rules */ batchmate(X,Y):­ studies(X,Z),studies(Y,Z). OUTPUT
  • 4. EXP NO. 3 Find all paths between any two vertices in the given graph: PROLOG CODE: /* facts: */ edge(1,2).  edge(1,3).  edge(1,4).  edge(2,3).  edge(2,5).  edge(3,4).  edge(3,5).  edge(4,5).  /* rules: */ connected(X,Y) :­ edge(X,Y) ; edge(Y,X).  path(A,B,Path) :­ travel(A,B,[A],Q),     reverse(Q,Path). travel(A,B,P,[B|P]) :­ connected(A,B). travel(A,B,Visited,Path) :­ connected(A,C), C == B,     not(member(C,Visited)),      travel(C,B,[C|Visited],Path).  OUTPUT
  • 5. EXP NO. 4 "MURDER MYSTERY" Problem Alice, her husband, son, daughter, and brother are involved in a murder. One of the five killed one of the other four. Who was the killer? 1. A man and a woman were together in the bar at the time of the murder. 2. The victim and the killer were together on the beach at the time of the murder. 3. One of the children was alone at the time of the murder. 4. Alice and her husband were not together at the time of the murder. 5. The victim's twin was innocent. 6. The killer was younger than the victim. PROLOG CODE /*­­­­­ Facts ­­­­­*/ person(alice). person(husband). person(son). person(daughter). person(brother). child(son). child(daughter). male(husband). male(son). male(brother). female(alice). female(daughter). twin(alice,    brother). twin(brother,  alice). twin(son,      daughter). twin(daughter, son). /*­­­­­ Rules ­­­­­*/ istwin(X) :­ twin(X, _). older(alice,   son). older(alice,   daughter). older(husband, son). older(husband, daughter). inbar(M, N) :­ person(M), person(N),                male(M),   female(N). together(S, T) :­ S=alice, T=husband. together(S, T) :­ T=alice, S=husband. alone(P) :­ person(P), child(P). /*­­­­­ Rule Combining Clues ­­­­­*/ solution(Killer, Victim, InBarA, InBarB, Alone) :­     person(Killer), person(Victim),
  • 6.     /* The victim's twin was innocent. */         istwin(Victim), + twin(Victim, Killer),     /* The killer was younger than the victim. */ + older(Killer, Victim),     /* not the same as "older(Victim, Killer)"! */     /* Alice and her husband were not together         at the time of the murder. */         + together(Killer, Victim), Killer = Victim,     /* A man and a woman were together in the         at the time of the murder. */         inbar(InBarA, InBarB), InBarA = Killer, InBarB = Killer, InBarA = Victim, InBarB = Victim,     /* Alice and her husband were not together         at the time of the murder. */         + together(InBarA, InBarB),     /* One of the children was alone at the         time of the murder. */ alone(Alone), Alone = InBarA, Alone = InBarB, Alone = Killer, Alone = Victim. /*­­­­­ Goal ­­­­­*/ print_solution :­     /* Find the solution */         solution(Killer, Victim, InBarA, InBarB, Alone),     /* Write solution */ nl,                 write(Killer),   write('   killed   '),   write(Victim), write('.'), nl, write(InBarA), write(' and '), write(InBarB), write(' were together in the bar.'), nl, write(Alone), write(' was alone.'), nl, nl. ?­ print_solution. OUTPUT
  • 7. EXP NO. 5 Missionaries and cannibals. Suppose 3 missionaries and 3 cannibals are walking together through the forest. They arrive at a river they have to cross, but there is only one boat, and that boat can carry at most 2 people. Of course, for the boat to cross the river, there should be at least one person (missionary or cannibal) in the boat (to row the boat). The problem is that if there are more cannibals than missionaries at any place, they will eat the missionaries. Write a program that finds a strategy for the six people to cross the river without a missionary being eaten. PROLOG CODE start([3,3,left,0,0]). goal([0,0,right,3,3]). legal(CL,ML,CR,MR) :­ /* is this state a legal one? */ ML>=0, CL>=0, MR>=0, CR>=0, (ML>=CL ; ML=0), (MR>=CR ; MR=0). /* Possible moves: */ move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):­ /* Two missionaries cross left to right. */ MR2 is MR+2, ML2 is ML­2, legal(CL,ML2,CR,MR2). move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):­ /* Two cannibals cross left to right. */ CR2 is CR+2, CL2 is CL­2, legal(CL2,ML,CR2,MR). move([CL,ML,left,CR,MR],[CL2,ML2,right,CR2,MR2]):­ /* One missionary and one cannibal cross left to right. */ CR2 is CR+1, CL2 is CL­1, MR2 is MR+1, ML2 is ML­1, legal(CL2,ML2,CR2,MR2). move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):­ /* One missionary crosses left to right. */ MR2 is MR+1, ML2 is ML­1, legal(CL,ML2,CR,MR2).
  • 8. move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):­ /* One cannibal crosses left to right. */ CR2 is CR+1, CL2 is CL­1, legal(CL2,ML,CR2,MR). move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):­ /* Two missionaries cross right to left. */ MR2 is MR­2, ML2 is ML+2, legal(CL,ML2,CR,MR2). move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):­ /* Two cannibals cross right to left. */ CR2 is CR­2, CL2 is CL+2, legal(CL2,ML,CR2,MR). move([CL,ML,right,CR,MR],[CL2,ML2,left,CR2,MR2]):­ /* One missionary and one cannibal cross right to left. */ CR2 is CR­1, CL2 is CL+1, MR2 is MR­1, ML2 is ML+1, legal(CL2,ML2,CR2,MR2). move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):­ /* One missionary crosses right to left. */ MR2 is MR­1, ML2 is ML+1, legal(CL,ML2,CR,MR2). move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):­ /* One cannibal crosses right to left. */ CR2 is CR­1, CL2 is CL+1, legal(CL2,ML,CR2,MR). /* Recursive call to solve the problem */ path([CL1,ML1,B1,CR1,MR1],[CL2,ML2,B2,CR2,MR2],Explored,MovesList) :­     move([CL1,ML1,B1,CR1,MR1],[CL3,ML3,B3,CR3,MR3]),     not(member([CL3,ML3,B3,CR3,MR3],Explored)),       path([CL3,ML3,B3,CR3,MR3],[CL2,ML2,B2,CR2,MR2], [[CL3,ML3,B3,CR3,MR3]|Explored],[   [[CL3,ML3,B3,CR3,MR3], [CL1,ML1,B1,CR1,MR1]] | MovesList ]). /* Solution found */ path([CL,ML,B,CR,MR],[CL,ML,B,CR,MR],_,MovesList):­