0% found this document useful (0 votes)
10 views20 pages

Ai NKL

The document is a lab file for an Artificial Intelligence course that focuses on the programming language PROLOG. It includes experiments on PROLOG basics, installation, facts, predicates, and examples of converting temperatures and removing punctuation. Additionally, it contains viva questions that assess understanding of PROLOG concepts and Python programming.

Uploaded by

nishant114999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Ai NKL

The document is a lab file for an Artificial Intelligence course that focuses on the programming language PROLOG. It includes experiments on PROLOG basics, installation, facts, predicates, and examples of converting temperatures and removing punctuation. Additionally, it contains viva questions that assess understanding of PROLOG concepts and Python programming.

Uploaded by

nishant114999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MAHARAJA SURAJMALINSTITUTE

OF TECHNOLOGY

ARTIFICIAL INTELLIGENCE
LAB FILE
(AI-302P)

Submitted To: Submitted By: Ritesh


Ms. Saba Khanum Kumar
Enroll No:03896303122
IT-E 6th Sem
Experiment 1

Aim : Study PROLOG.

THEORY:

1. Introduction to PROLOG:

PROLOG (Programming in Logic) is a high-level programming language designed for


artificial intelligence (AI), natural language processing, and expert systems. Unlike traditional
programming languages like C, Java, and Python, which follow a step-by-step (imperative)
approach, PROLOG is declarative—you describe what you want to achieve, and the system
figures out how to get the result.

PROLOG is widely used in fields like knowledge representation, automated reasoning, expert
systems, and natural language understanding.

2. Installation of Prolog Compiler:


Step 1: Download SWI-Prolog
• Go to the official SWI-Prolog website:
https://www.swi-prolog.org/download/stable

• Click on "Windows" and download the latest .exeinstaller.

Step 2: Run the Installer

• Open the downloaded .exe file.

• Follow the installation wizard:


o Click Next.
o Accept the license agreement.
o Choose the installation directory (default is fine).
o Click Install.
• Once the installation is complete, click Finish.

Name: Ritesh Kumar


Enroll: 03896303122
Step 3: Verify Installation

• Open Command Prompt (cmd).

Step 4: Run Prolog Programs

• Open SWI-Prolog from the Start menu.

3. Minimum Requirements of SWI Compiler

(i) Operating System:

• Windows (32-bit or 64-bit)

• macOS (10.14 Mojave or later)

• Linux (various distributions)

(ii) Processor:

Name: Ritesh Kumar


Enroll: 03896303122
• 1 GHz or faster processor

(iii) Memory:

• At least 512 MB of RAM (1 GB recommended for better


performance)

(iv) Disk Space:

• Around 50 MB of free disk space for the installation

(v) Additional Software:

• For macOS, you may need XQuartz to run the development tools.
• For Linux, you might need to add a PPA or use package
managers like apt, yum, or snap.

You can download the SWI-Prolog compiler from the official SWI-Prolog
download page

4. How is PROLOG Different from other programming languages

PROLOG (Programming in logic) is different from traditional programming


languages like C, Java, Python, and JavaScript because it focuses on logic and
reasoning rather than step-by-step instructions. The key differences are:

1. Way of Thinking: Logic vs. Instructions


• PROLOG: "Here are some facts and rules—figure out the answer
yourself!"
• Other Languages: "Follow these steps exactly as I say!"

2. How It Works: Backtracking vs. Step-by-Step Execution

Name: Ritesh Kumar


Enroll: 03896303122
• PROLOG uses backtracking to find solutions automatically. If one
approach doesn’t work, it tries another until it finds the right answer.
• Other languages follow a fixed sequence of steps (loops, if-else)
without automatically trying different possibilities.

What happens here?


• PROLOG looks for a path where John is a grandparent of Alice.
• It first checks if John is a parent of Alice (fails).
• Then, it backtracks and checks if John is a parent of someone (Mary),
who is then a parent of Alice.
• Since the rule matches, it returns Yes.

Advantage: You don’t need to write loops or conditions manually—PROLOG


automatically explores all possibilities and finds the correct answer.

Name: Ritesh Kumar


Enroll: 03896303122
5. Facts, Data and Information

(i) Facts

Definition: Facts are universally true statements that do not change based on opinion
or interpretation.
Why are they important? They serve as the f oundation of knowledge and decision-
making.
Example:

● "Water freezes at 0°C."


● "The Eiffel Tower is in Paris."

(ii) Data

Definition: Data consists of raw numbers, symbols, or observations that do not carry
meaning by themselves.
Why is it important? It is the input that, when processed, turns into meaningful
information.
Example:

• A weather station records daily temperatures: 22°C, 24°C, 19°C, 25°C, 21°C.
• A company records sales numbers: 200, 350, 420, 300, 280.

Name: Ritesh Kumar


Enroll: 03896303122
(iii) Information

Definition: Information is processed data that provides meaning and helps in


decision-making.
Why is it important? It allows us to analyze trends, make predictions, and
solve problems.
Example:
• From the raw temperature data (22°C, 24°C, 19°C, 25°C, 21°C), we can conclude:
"The average temperature this week was 22.2°C."
• From the company sales data (200, 350, 420, 300, 280), we can conclude:
"Sales were highest on the third day, reaching 420 units."

Facts → Unchangeable truths that form the basis of knowledge. Data


→ Raw observations or numbers that need processing. Information →
Processed data that provides

Name: Ritesh Kumar


Enroll: 03896303122
Experiment 2

AIM: Write simple fact for the statements using PROLOG.

a. Ram likes mango.


b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. John owns gold.

CODE:

Facts:

Queries:

Name: Nakul Jain


Enroll: 03096303122
OUTPUT:

Name: Nakul Jain


Enroll: 03096303122
VIVA QUESTIONS

1. What is declarative programming, and how is it different from


imperative programming?

Answer:
Declarative programming focuses on "what" needs to be done rather than "how"
it should be done. PROLOG is a declarative language, meaning that instead of
writing step-by-step instructions, we define facts and rules, and the system figures
out the solution.

Example:

2. How does PROLOG handle case sensitivity in facts and queries?

• PROLOG is case-sensitive.
• Atoms (names of predicates and constants) must start with a lowercase letter.
• Variables must start with an uppercase letter or an underscore (_).

Name: Nakul Jain


Enroll: 03096303122
Example:

3. Can you explain the syntax of a fact in PROLOG with an example?

Answer:

In PROLOG, a fact consists of a predicate followed by arguments enclosed in


parentheses. It must end with a period (.).

• Example:

4. What are atoms, variables, and structures in PROLOG?

Answer:

• Atoms: Names that begin with a lowercase letter or are enclosed in single
quotes. (e.g., apple, 'New Delhi')
• Variables: Names that begin with an uppercase letter or an underscore. (e.g.,
X, Person)
• Structures: Complex terms made up of a functor and arguments. (e.g.,
student(john, 22).)

Name: Nakul Jain


Enroll: 03096303122
5. What is backtracking in PROLOG, and how does it work?

Answer:
Backtracking is PROLOG’s way of finding solutions by exploring all possible
options. If a query fails, it goes back to the previous choice point and tries another
path.

Example:

Name: Nakul Jain


Enroll: 03096303122
Experiment 3
AIM: Write predicate, one converts centigrade temperature to Fahrenheit, the
other checks if a temperature is below freezing using Prolog.

CODE:

QUERIES:

?- celsius_to_fahrenheit(0, F).

?- below_freezing(-5).

?- below_freezing(10).

OUTPUT:

Name: Nakul Jain


Enroll: 03096303122
VIVA QUESTIONS

1. What is a predicate in PROLOG, and how is it different from a fact?

Answer:

• A predicate in PROLOG defines a relationship between objects or a rule. Unlike a


fact, a predicate can have a condition that needs to be satisfied.

• Example:

2. What is the difference between the is operator and the = operator in


PROLOG?

Answer:

3. What happens if there are multiple matching rules for a query in PROLOG?

Name: Nakul Jain


Enroll: 03096303122
Answer:
PROLOG tries the first matching rule and, if it fails, backtracks to try the next rule.

• Example:

4. What is unification in PROLOG, and how does it work in queries?

Answer:
Unification is the process of matching two terms by making their values identical.

• Example:

5. Can PROLOG handle arithmetic operations? If yes, how?

Answer:
Yes, PROLOG can perform arithmetic using the is operator.

• Example:

Name: Nakul Jain


Enroll: 03096303122
Name: Nakul Jain
Enroll: 03096303122
Experiment 4
AIM: Write a program to remove punctuation from input string..

CODE:

import string

def remove_punctuation(input_string):

# Create a translation table that maps each punctuation character to None

translator = str.maketrans('', '', string.punctuation)

return input_string.translate(translator)

text = "Hello, remove! punctuations?/"

cleaned_text = remove_punctuation(text)

print(cleaned_text)

OUTPUT:

Name: Nakul Jain


Enroll: 03096303122
VIVA QUESTIONS

1. What is the purpose of the string Punctuation in the code?

Answer: String punctuation provides a string containing all punctuation characters


(like!"#$%&'() *+, -. /:;<=>?@[\\]^_{|}~`). It helps to identify the characters that

2. How does the str.maketrans() function work in this code?


Answer: str.maketrans() is used to create a translation table that maps each punctuation
character to None. This table is then used by translate() to remove those characters from
the

3. Can you explain the role of the translate() method?

Answer: The translate() method is used to apply the translation table (created by
str.maketrans()) to the string. It processes the string and removes the characters
specified in the translation table (punctuation marks in this case).
4. What happens if there is no punctuation in the input string?

Answer: If there is no punctuation, the function will return the string as it is, without
any modifications.

5. How would you modify the function to remove digits as well as punctuation?

Answer: You could update the string.punctuation by adding string.digits to it, like so:

translator = str.maketrans('', '', string.punctuation + string.digits)

Name: Nakul Jain


Enroll: 03096303122
Experiment 5
AIM: Write a program to arrange sort sentences.

CODE:

: Write a program to arrange sort sentences.

def arrange_sentences(sentences):

return sorted(sentences)

sentences = [

"Daisy teaches English.",

"Anitha called me yesterday.",

"The girl wearing the yellow dress is my new neighbour.",

"He is cooking dinner."

ordered_sentences = arrange_sentences(sentences)

for sentence in ordered_sentences:

print(sentence)

OUTPUT:

Name: Nakul Jain


Enroll: 03096303122
VIVA QUESTIONS

1. What is the purpose of arranging sentences in a specific order?

Answer: Arranging sentences helps organize the text logically, either chronologically,
alphabetically, or based on any criteria that makes the text clearer or more coherent.

2. How can you sort sentences alphabetically in Python?

Answer: You can use Python's sorted() function to sort a list of sentences
alphabetically. It arranges the sentences in ascending order based on lexicographical
order.

3. What are the different methods available in Python to sort a list?

Answer: Python provides methods like sorted(), which returns a new sorted list, and
the list.sort() method, which sorts the list in-place.

4. What happens if the sentences have mixed capital and lowercase letters?

Answer: By default, Python sorts uppercase letters before lowercase letters in


lexicographical order. You can use the key=str.lower argument with sorted() to make
the sorting case-insensitive.

5. What will the output be if you try to sort a list of sentences that are already in
order?

Answer: If the sentences are already in alphabetical order, the output will be the same
as the input, as no changes will be necessary.

Name: Nakul Jain


Enroll: 03096303122

You might also like