IS Sec1
IS Sec1
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
● 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
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
■ 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
● Answer 1 :
○ likes(ram ,mango).
○ likes(bill ,cindy).
○ girl(seema).
○ red(rose).
○ owns(john ,gold).
● Answer 2 :
○ male(albert).
○ male(edward).
○ female(victoria).
○ female(alice).
○ parents(edward, victoria, albert).
○ parents(alice, victoria, albert).
○ 𝑠𝑖𝑠𝑡𝑒𝑟 𝑋, 𝑌 : − 𝑓𝑒𝑚𝑎𝑙𝑒 𝑋 , 𝑝𝑎𝑟𝑒𝑛𝑡𝑠 𝑋, 𝑀, 𝐹 , 𝑝𝑎𝑟𝑒𝑛𝑡𝑠 𝑌, 𝑀, 𝐹 .
● 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 :
○ 𝑝𝑟𝑖𝑛𝑐𝑒 𝑃𝑟𝑖𝑛𝑐𝑒, 𝑌𝑒𝑎𝑟 : −
𝑟𝑒𝑖𝑔𝑛𝑠 𝑃𝑟𝑖𝑛𝑐𝑒, 𝑆𝑡𝑎𝑟𝑡, 𝐸𝑛𝑑 ,
𝑌𝑒𝑎𝑟 >= 𝑆𝑡𝑎𝑟𝑡,
𝑌𝑒𝑎𝑟 =< 𝐸𝑛𝑑.