0% found this document useful (0 votes)
17 views35 pages

LP-Lec 07

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)
17 views35 pages

LP-Lec 07

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/ 35

Mansoura University

Faculty of Computers and Information


Department of Computer Science

Fall 2024

[CS313P] Logic Programming

Programming in prolog(1)
3rd CS Department – 2nd AI Program

DR. Muhammad Haggag Zayyan


CS Department
Contents

1 Horn Clause

2 Prolog Program Sections

3 Variables

4 Compound Data objects

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)}

2. Negated goal: ( 0 positive , 1 or more negative )


EX: {¬ male(X), ¬ Father(ali,X)}

3. Null clause : ( 0 positive , 0 negative )


EX: { } the clause appears at the end of resolution proof

4. Rule : ( 1 positive , 1 or more negative )


EX: {¬ father(X,Y), parent(X,Y)}
This can be interpreted as: "If X is not the father of Y, then X
is a parent of Y." It expresses a conditional relationship.
3
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),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:

▪ The resolution utilize the set of support strategy, in


which no resolution ever involves resolving two
clauses from premises together

▪ Every resolution step combines a negated goal with


a fact or rule from premises

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 a type of programming called declarative


programming
▪ It involves writing programs using a set of logical statements (facts and
rules) that define relationships between entities. The system then
derives conclusions or answers based on these relationships.

➢ 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.

➢ Example: find average of list of numbers:


» Input numbers
» Compute total
» Average = total / numbers_count
» Print Average

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

Section keyword Purpose


Database To declare data base contain set of facts
Domains To declare domains of the variables
Constants To declare constant values
Predicates To declare the predicates
Clauses The beady of the prolog program
Goal Internal or external goal (run)

11
Prolog Program Sections
➢ Clauses section

a) Facts
• examples
As relation Ayman plays Football plays( ayman, football).

As Properties Sky is blue blue (sky).

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

:- It means( if ) placed between rule head and rule body


. it means the termination of Fact,Rule or Goal

➢ Comments in prolog has two types:

▪ Multiple line comments begin with the characters /* and


end with */
▪ single line comments starting with %

13
Prolog Program Sections
➢ Domains section (Standard domains in prolog)

Domain Description
a character surrounded by a single
char
quotation mark Ex: ‘a’ ‘/’ ‘&’ ‘3’

real To define the decimal value like 2.4, 3.0, 5,-2.67, ….

integer To define numerical value like 1, 20, 0,-3,-50, …

A sequence of characters, two formats are permitted:


Symbol 1. sequence of letters, numbers and underscores, provided the
or first character is lower-case or
string 2. A character sequence surrounded by a pair of double quotations
Ex : telephone_number “Railway ticket”

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 section serves two very useful purposes:


❑ Giving meaningful names to domains
❑ Declare data structures that don’t defined by the standard
prolog domains

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

syntax: <id> = <value>

EX
constants
pi=3.14159

16
Prolog Program Sections
➢ Predicates section

How to declare user-defined predicates ?


A predicate declaration begins with the predicate name followed by an open
left parenthesis, then zero or more arguments to the predicate, then the
closing right parenthesis

predicate_name ( arg_typel , arg_type2, … , arg_typeN)

Predicate Names: must begin with letter followed by a sequence of


letters , digits and underscores
Predicate Arguments: must belongs to known prolog standard domains,
or one you declare in the domains section
17
Prolog Program Sections
➢ Predicates section
▪ The section begins with the keyword Predicates
▪ List each predicate showing the type( domain) of it’s argument
▪ A predicate name is not followed by period .
▪ The argument type could be standard domains or user defined
domains declared in the domains section
EX
Predicates
parent(symbol, symbol)
Or
Domains
Person=symbol
Predicates
parent(person, person)
18
Prolog Program Sections
➢ Goal section

▪ There are two types of goal depend on its place

❑ External goal

➢ Written outside the program


➢ Can be changed each time the program is executed

❑ Internal goal

➢ Written in the Goal Section inside the program


➢ Run automatically when the program is executed
➢ Can’t be changed during execution.
19
Variables
➢ Variables name must begin with a capital letter or an
underscore, followed by any number of letters, digits or
underscores.

➢ Variables in prolog get their values by being matched to


constant in facts or rule ( Unification process).

➢ You can not store information by giving a value to a variable.

➢ Variables are used to express general rules and to ask


questions.

20
Variables
➢ A anonymous variables is represented by single
underscore( _ )

▪ It can be used to ignore the values you do not need


▪ It never get a value
▪ It match anything

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

Greater than >


Less than <
Equal =
Not equal <>
Greater than or equal >=
Less than or equal <=

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

➢ You can define this object by declaring a domain containing


the compound object data
Domains
date_cmp=myDate(string, integer, integer)
Predicates
Person(string, date_cmp)
Clauses
Person(ali, myDate(“October”,4,1993)).

➢ A functor is just a name that identifies a kind of compound data object


and hold it’s argument together
33
https://shorturl.at/EHYuO

34
Thank you

You might also like