0% found this document useful (0 votes)
6 views19 pages

IS Sec1

The document provides an introduction to Prolog, a logic programming language, and its application in designing expert systems. It covers Prolog's basic syntax, rules, and backtracking mechanisms, along with examples of how to write facts and queries. Additionally, it includes an assignment to calculate population density using Prolog programming.

Uploaded by

dohasaeed2003
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)
6 views19 pages

IS Sec1

The document provides an introduction to Prolog, a logic programming language, and its application in designing expert systems. It covers Prolog's basic syntax, rules, and backtracking mechanisms, along with examples of how to write facts and queries. Additionally, it includes an assignment to calculate population density using Prolog programming.

Uploaded by

dohasaeed2003
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/ 19

Intelligent Systems

Section 1
By: Abdelrahim Alsadiq
Table of Contents
1. Course Content
2. Part 1: Prolog Introduction
3. Prolog in designing Expert Systems
4. Prolog Basic Syntax
5. How Backtracking Works in Prolog
6. Prolog basic programs
1. Course Content

Prolog Fuzzy
Robotics
Logic

• Basic Syntax • Union • Arduino Basic


• Tail Recursion • Intersection Circuits
• Lists • Compliment
• De-Morgan’s Law
• Simple Fuzzy Set
Operations
2. Part 1: Prolog Introduction

● Prolog Stands for “Programming, in Logic”.


● Suitable for problems that involve structured objects and relations between them.
● Based for symbolic, non-numeric computation (Declarative Language).
● Accomplished by creating a database of facts and rules about objects, their properties, and their
relationships to other objects.
○ Facts: Statements that define known information about objects, provide the base knowledge of
the program.
○ Rules: Logical statements that define relationships based on conditions, derive new knowledge
based on existing facts and other rules.
○ Queries: Questions asked to the Prolog system to retrieve information based on facts and rules.
3. Prolog in designing Expert Systems

● An Expert System is a set of programs that manipulates encoded knowledge to solve problems in a
specialized domain that normally requires human expertise.
● Prolog features:
○ Use of knowledge rather than data
○ Modification of the knowledge base without recompilation of the control programs.
○ Capable of explaining conclusion.
○ Symbolic computations resembling manipulations of natural language.
○ Reason with meta-knowledge.
■ A meta-program is a program that takes other programs as data.
4. Prolog Basic Syntax

● Writing Facts using Prolog:


○ Single-Argument Facts:
■ These facts assert that a particular entity belongs to a category.
■ Examples:
● Tom is a boy → boy(tom).
● Alice is a girl → girl(alice).

Notes:
1. Always end your line with a dot (.), similar to a semicolon in other programming languages.
2. Atoms must start with a lowercase letter, and empty atom can be represented by two single quotes.
3. Atoms do not change their value once defined.
4. Prolog Basic Syntax

● Writing Facts using Prolog:


○ Multi-Argument Facts:
■ These facts express relationships between entities.
■ Examples:
● John is a parent of Mary → parent(john, mary).
● Ahmed likes mango → likes(ahmed, mango).

Note: Ensure proper respect for entities when defining facts.


4. Prolog Basic Syntax

● Writing Rules using Prolog:

English Predicate Calculus Prolog


If → :-
Not ¬ not
Or ⋁ ;
And ⋀ ,

■ Examples:
● X will pay Y if X is a boy and Y is a girl → 𝑝𝑎𝑦 𝑋, 𝑌 : −𝑏𝑜𝑦 𝑋 , 𝑔𝑖𝑟𝑙 𝑌 .
4. Prolog Basic Syntax

● Additional Syntax:
○ ( is ) vs ( = ) operators:
■ ( is ) can be used for Arithmetic Evaluation
● right-hand side must be an arithmetic expression, while The left-hand side must
be a variable.
■ ( = ) can be used for unify two terms without evaluating expressions
● It checks if both sides can be made identical
○ Logical Operators:
■ Equivalence ( == ) and Non- Equivalence ( \== ).
■ Greater than ( > ) and Less than ( < ).
■ Greater than or Equal to ( >= ) and Less than or Equal to ( =< ).
5. How Backtracking Works in Prolog

● Backtracking is a procedure, in which prolog searches the truth value of different predicates by
checking whether they are correct or not.
● Example:
○ Facts:
■ 𝑏𝑜𝑦(𝑡𝑜𝑚).
■ 𝑏𝑜𝑦(𝑏𝑜𝑏).
■ 𝑔𝑖𝑟𝑙(𝑎𝑙𝑖𝑐𝑒).
■ 𝑔𝑖𝑟𝑙 𝑙𝑖𝑙𝑖 .
○ Rules:
■ 𝑝𝑎𝑦(𝑋, 𝑌) ∶ − 𝑏𝑜𝑦(𝑋), 𝑔𝑖𝑟𝑙(𝑌).
○ Query:
■ 𝑝𝑎𝑦 𝑋, 𝑌 .
5. How Backtracking Works in Prolog

● Output:
○ X = tom, Y = alice.
○ X = tom, Y = lili.
○ X = bob, Y = alice.
○ X = bob, Y = lili.

● Notes:
○ Enter dot (.) to stop backtracking.
○ Enter semi-colon (;) to continue trying to find other solutions.
6. Prolog Examples

● Example 1 : Write simple fact for following:


○ Ram likes mango.
○ Seema is a girl.
○ Bill likes Cindy.
○ Rose is red.
○ John owns gold.
● Queries:
○ Ram likes what?
○ Who likes cindy?
○ Who owns what?
6. Prolog Examples

● Answer 1 :
○ likes(ram ,mango).
○ likes(bill ,cindy).
○ girl(seema).
○ red(rose).
○ owns(john ,gold).

○ likes(ram, What). 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑊ℎ𝑎𝑡 = 𝑚𝑎𝑛𝑔𝑜.


○ likes(Who, cindy). 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑊ℎ𝑜 = 𝑏𝑖𝑙𝑙.
○ owns(Who, What). 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑊ℎ𝑜 = 𝑗𝑜ℎ𝑛, 𝑊ℎ𝑎𝑡 = 𝑔𝑜𝑙𝑑.
6. Prolog Examples

● Example 2 : Write simple fact for following:


○ Albert and Edward are male.
○ Alice and Victoria are female.
○ Edward has parents Victoria and Albert.
○ Alice has parents Victoria and Albert.
● Rules:
○ X is a sister of Y if:
■ X is a female, X has parents M and F, and Y has parents M and F.
● Queries:
○ Is Victoria a sister of Albert?
○ Is Alice a sister of Edward?
6. Prolog Examples

● Answer 2 :
○ male(albert).
○ male(edward).
○ female(victoria).
○ female(alice).
○ parents(edward, victoria, albert).
○ parents(alice, victoria, albert).
○ 𝑠𝑖𝑠𝑡𝑒𝑟 𝑋, 𝑌 : − 𝑓𝑒𝑚𝑎𝑙𝑒 𝑋 , 𝑝𝑎𝑟𝑒𝑛𝑡𝑠 𝑋, 𝑀, 𝐹 , 𝑝𝑎𝑟𝑒𝑛𝑡𝑠 𝑌, 𝑀, 𝐹 .

Queries: s𝑖𝑠𝑡𝑒𝑟 𝑣𝑖𝑐𝑡𝑜𝑟𝑖𝑎, 𝑎𝑙𝑏𝑒𝑟𝑡 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑓𝑎𝑙𝑠𝑒.


𝑠𝑖𝑠𝑡𝑒𝑟(𝑎𝑙𝑖𝑐𝑒, 𝑒𝑑𝑤𝑎𝑟𝑑) 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑡𝑟𝑢𝑒.
6. Prolog Examples

● Example 3 : Given the following facts, write a correct rule and answer the following queries:
○ reigns(rhodri,844,878).
○ reigns(anarawd,878,916).
○ reigns(hywel_dda,916,950).
○ reigns(lago_ad_idwal,950,979).
● Rules:
○ Prince X was a prince during year Y if:
■ X reigned between years Start, End and Y is between them.
● Queries:
○ Was Rhodri a prince in 878?
○ Was Anarawd a prince in 963?
6. Prolog Examples

● Answer 3 :
○ 𝑝𝑟𝑖𝑛𝑐𝑒 𝑃𝑟𝑖𝑛𝑐𝑒, 𝑌𝑒𝑎𝑟 : −
𝑟𝑒𝑖𝑔𝑛𝑠 𝑃𝑟𝑖𝑛𝑐𝑒, 𝑆𝑡𝑎𝑟𝑡, 𝐸𝑛𝑑 ,
𝑌𝑒𝑎𝑟 >= 𝑆𝑡𝑎𝑟𝑡,
𝑌𝑒𝑎𝑟 =< 𝐸𝑛𝑑.

Queries: 𝑝𝑟𝑖𝑛𝑐𝑒(𝑟ℎ𝑜𝑑𝑟𝑖, 878) 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑡𝑟𝑢𝑒.


𝑝𝑟𝑖𝑛𝑐𝑒(𝑎𝑛𝑎𝑟𝑎𝑤𝑑, 963) 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑓𝑎𝑙𝑠𝑒.
Assignment
Write a prolog program that calculate the population density of a country population over the Area.
Given the following facts:

1. Population of USA is 305 Area of USA is 3.


2. Population of India is 1132 Area of India is 1.
3. Population of China is 1321 Area of China is 4.
4. Population of Brazil is 187 Area of Brazil is 3.

And the following rule:


1. The density of a country can be calculated by:
𝐷𝑒𝑛𝑠𝑖𝑡𝑦 = 𝑃𝑜𝑝𝑢𝑙𝑎𝑡𝑖𝑜𝑛/𝐴𝑟𝑒𝑎

Required Query: de𝑛𝑠𝑖𝑡𝑦 𝑋, 𝑌


References
• Download SWI-Prolog from Here.
• Watch how to install SWI-Prolog from Here.
• Watch a complete SWI-Prolog Tutorial in Arabic from Here.
• Join Google Classroom from Here (Class Code: 4cipz5p)
• Contact with me via WhatsApp or Discord.

You might also like