LP-Lec 07
LP-Lec 07
Fall 2024
Programming in prolog(1)
3rd CS Department – 2nd AI Program
1 Horn Clause
3 Variables
2
Horn Clause
➢ Horn Clause is a clause with at most one positive literal
➢ Any horn clause belong to one of four categories
1. Fact : ( 1 positive , 0 negative )
EX: {Father(ali, ahmed)}
{P(c,b)}
{¬P(c, b),q(c)}
{q(c)}
4
Horn Clause
Resolution of Horn clause
The resolvent of two Horn clauses is a Horn clause
If you resolve Horn clauses A and B to get clause C, then the
positive literal of A will resolve against a negative literal in B, so
the only positive literal left in C is the one from B (if any)
EX
{P(c,b)}
{¬P(c, b)}
{}
5
Horn Clause
➢ Prolog adopt the horn clause resolution where:
6
Prolog
➢ Two types of programming languages:
▪ Imperative languages (C, C++, VB, C#, Java, …)
o You specify the exact steps or instructions to be executed in order to
achieve a desired result.
▪ Declarative languages (Prolog, lisp, …).
▪ You describe what you want to achieve, without specifying the exact
steps to achieve it. The language or system itself figures out the how.
➢ Logic programming is widely used in AI for tasks like NLP, expert systems, game,
and knowledge representation, because it allows systems to reason logically and solve
complex problems based on facts and rules. 7
Prolog
Imperative Languages
➢ They are also called procedural languages
➢ It is about how the problem be solved?
➢ Programmer gives all steps to solve a problem.
➢ He must know an algorithm to solve a problem.
8
Prolog
Declarative Languages
➢ It is about what is the problem?
➢ Programmer describe the problem without the control flow,
then the system will solve the problem.
➢ Programmer must know the relations between objects to
solve the problem.
➢ Programmer does not need to know an algorithm to
solve the problem.
9
Prolog
How to run prolog on 64-bit environment:
➢ Download DOSBox 0.74-3 ( free software)
➢ (http://www.dosbox.com)
➢ Install and then run the program
▪ write the following commands:
>>Mount c [prolog folder path]
>>C:
>>prolog.exe
▪ Or simply, drag the exe file of prolog program and
drop it on DosBox program shortcut
10
Prolog Program Sections
Prolog sections
11
Prolog Program Sections
➢ Clauses section
a) Facts
• examples
As relation Ayman plays Football plays( ayman, football).
b) Rules
• Rules enable you to infer facts from other facts
• Prolog rule has two parts Head and Body
Head :- <subgoal1>,<subgoal2> , … , <subgoalN>
12
Prolog Program Sections
, It mean Conjunction of sub-goals, all rules must be satisfied
; It means Disjunction of sub-goals, any sub-goal provide rule satisfied
13
Prolog Program Sections
➢ Domains section (Standard domains in prolog)
Domain Description
a character surrounded by a single
char
quotation mark Ex: ‘a’ ‘/’ ‘&’ ‘3’
14
Prolog Program Sections
➢ Domains section
▪ The section begins with the keyword Domains
▪ Enable you to give distinctive names to different kinds of data
Domains
Name = symbol
Age = integer
15
Prolog Program Sections
➢ Constants section
▪ Where you can declare symbolic constants in your Prolog
programs.
▪ A constant declaration section is indicated by the keyword
constants, followed by the declarations themselves
EX
constants
pi=3.14159
16
Prolog Program Sections
➢ Predicates section
❑ External goal
❑ Internal goal
20
Variables
➢ A anonymous variables is represented by single
underscore( _ )
21
Prolog program, example
domains Yes/no question
Person= symbol
predicates No variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). male(ali)
male(ahmed).
male(sleem). Answer
father(sleem, ali).
father(sleem, ahmed). yes
brother(X,Y):- father(P,X),
father(P,Y),
male(X), male(Y).
22
Prolog program, example
domains Yes/no question
Person= symbol
predicates No variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). male(muhammad)
male(ahmed).
male(sleem). Answer
father(sleem, ali).
father(sleem, ahmed). no
brother(X,Y):- father(P,X),
father(P,Y),
male(X), male(Y).
23
Prolog program, example
domains Fill in blank question
Person= symbol
predicates Use variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). male(X)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), X=ali
father(P,Y), X=ahmed
male(X), male(Y).
X=sleem
24
Prolog program, example
domains Fill in blank question
Person= symbol
predicates Use variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). father(X,Y)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), X=sleem, Y= ali
father(P,Y), X=sleem, Y= ahmed
male(X), male(Y).
25
Prolog program, example
domains Fill in blank question
Person= symbol
predicates Use variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). father(_,Y)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), Y= ali
father(P,Y), Y= ahmed
male(X), male(Y).
26
Prolog program, example
domains Fill in blank question
Person= symbol
predicates Use variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). father(X, _)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), X= sleem
father(P,Y), Y= sleem
male(X), male(Y).
27
Prolog program, example
domains Fill in blank question
Person= symbol
predicates Use variables in goal
male(person)
father(person, person)
brother(person, person)
clauses goal
male(ali). brother(X,Y)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), X= ali
father(P,Y), Y= ahmed
male(X), male(Y).
28
Prolog program, example
domains Compound goal:
Person= symbol
predicates You can use conjunction or
male(person) disjunction in your query
father(person, person)
brother(person, person)
clauses goal
male(ali). male(ali) , male(muhammad)
male(ahmed).
male(sleem).
father(sleem, ali). Answer
father(sleem, ahmed).
brother(X,Y):- father(P,X), No
father(P,Y),
male(X), male(Y).
29
Prolog program, example
domains Compound goal:
Person= symbol
food= symbol You can use conjunction or
predicates disjunction in your query
likes(person, food)
clauses
likes(ali, apple). goal
likes(ali,pizza). likes(ali,X), likes(omr,X)
likes(omr, apple).
Answer
X=apple
30
Arithmetic Operations
Addition +
subtraction -
multiplication *
division /
Integer part of division div
Remainder of division mod
31
Logical Operations
32
Compound objects
➢ Compound data objects allow you to treat several pieces of
information as a single item such a way that you can easily pick them
apart again
34
Thank you