22
Artifical Intelligent 3 rd class Department of Network College of IT- University of Babylon Date: Tuesday, March 04, 2014 1 Page Programming in Logic Prolog Mehdi Ebady Manaa 1 Mouad Najam Abdullah 2 Master Student: Marwa Kadhim Ubeid عهوياثث انني قسى شبكا يهزيت يقذيت انثانثتشحهت ا اننزكاءدة ا يااعيصط ا1 Assistance Lecturer in Department of Information Network at College of Information Technology 2 Assistance Lecturer in Department of Information Network at College of Information Technology

Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

  • Upload
    buidung

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 1Page

Programming in Logic

Prolog

Mehdi Ebady Manaa 1

Mouad Najam Abdullah 2

Master Student: Marwa Kadhim Ubeid

يهزيت يقذيت اني قسى شبكاث انعهوياث

انشحهت انثانثت

االصطاعييادة انزكاء

1

Assistance Lecturer in Department of Information Network at College of Information Technology 2

Assistance Lecturer in Department of Information Network at College of Information Technology

Page 2: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 2Page

1. Introduction

Can a computer be intelligent? What would it mean for a computer to be

intelligent? John McCarthy, one of the founders of artificial intelligence

research, once defined the field as “getting a computer to do things

which, when done by people, are said to involve intelligence.” The point

of the definition was that he felt perfectly comfortable about carrying on

his research without first having to defend any particular philosophical

view of what the word “intelligence” means 3 .

انسؤال ها

٠ؽزط١غ اىج١رط ا ٠ى شو١ب ؟؟؟ بشا ٠ؼ ا ٠ى اىج١رط شو١ب ؟؟

٠ؽزط١غ اىج١رط ا ٠ى شو١ب اشا لب ثأػبي اشا ب ر رف١صب ثاؼطخ ٠م اب

اػبي شو١خ ...

Several AI researchers had the idea that ambiguities in the meanings of

words could be resolved by trying to understand English only in some

limited context.

حل اي مشكلة ف الذكاء االصطناع جب تعرفها بشكل جد ودقق بحث مكن التعبر عنها ل باستخدام الحاسبة ...

-المثال االت وضح ذلك :? Student [The price of a radio is $69.70. If this price is 15 percent less

than the marked price, find the marked price.]

The marked price = 80.155 برهن ذلك الحل الك المثال االت :

The weight of a giant size detergent box is 69.70 ounces. If this weight؟

is 15 percent less than the weight of an enormous size box, find the

weight of an enormous size box.

For Student, the problem same as above :

-ام بالنسبة لحلول المشاكل ف الحاسبة سكون حل المثال االول كاالت :

The top-level procedure Student takes one input, a list containing the

word problem. (The disk file that accompanies this project includes

several variables containing sample problems. For example,

? student :radio The next step is to separate the problem into simple sentences

3

اغ اؼطث١خ ب ال رؼ رطعخ ب لجب ى رز ثػع غ١خ اجحش اطاز االشبضح ا١

Page 3: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 3Page

The simple sentences are The price numof a radio is 69.70 dollars . This price is 15 perless the marked price . Find the marked price Example 3: Mary is 24 years old. It is translated to:- Mary s age is 24 . االمثلة اعاله ه احدى طرق حل المشكلة باستخدام المعادالت الراضة ... الطرق االخرى ه بأستخدام لغات البرمجة ومن بن هذه اللغات المهمة المستخدمة ف الذكاء االصطناع

Prolog : Programming in Logic لغة البرمجة باستخدام المنطق

2. What is Prolog?

Prolog (Programming in Logic) is a programming language for AI and

non-numerical programming in general.

What is new?

Conventional (well established, standard) languages are procedurally

oriented, while Prolog introduces the declarative view.

(, ثؼ: ػس رطج١ك declarative programming languageغخ طف١خ ) Prologرؼزجط

ح شىخ ب, ثسال رحس٠س و١ف١خ ا٠غبز سف ؼ١ حبخ ؼ١خ م ثزحس٠س و احبخ

٠شزك اح prolog ( زطن فؽطgoal( اسف )facts and rules)ث١ئخ حمبئك لاػس

(Bart, Phuong-Lan, Tom, & Troncon, 2005) .ب

رؼ طف ػب أشىخ اطط٠مخ االؼ طف صا اؼب prolog اجطغخ ثبؼزرسا

ػغ احمبئك اماػس شه ثطف ثبؼزرسا غػخ اى١ببد اىخ اؼاللبد از

رطثؾ اى١ببد ثؼؼب ثجؼغ.

3. Syntax of Prolog

-كاالتي : Prologيمكن تعريف محتويات لغة (a) : Clauses الجمل

o A Prolog program consists of clauses.

o A Prolog clause is a Horn clause.

o Each clause terminates with a full stop.

o Prolog clauses are of three types: rules, facts, and questions.

o Example1: rule { o_spring(X, Y) :- parent(Y, X).

o Example2: fact { parent(tom, ann).

o Example3: questions { parent(X, ann).

رىزت ف١ ع١غ احمبئك اماػس ثح١ش ع١غ احمبئك اماػس از ب فػ اؼ اؼاللخ

حمك ف شه جسأ رزؽؽ ز ا رزساذ غ لاػس حمبئك رؼز ؼاللبد اذط, ا

Page 4: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 4Page

اال٠خ ) فبحمبئك اماػس شاد اال٠خ االػ رىزت ف اجسا٠خ( فػ اؼاللخ ال

االؼززبط ف ص اغخ ٠غط االػ ا االؼف.

( ___________1ضــــــــــبي)

ػب االشىبي اسؼ١خ اػح ثبشى:

:٠ؼجط ػ صا اؼب ثغػخ اغ

Circle color is gray.

Square color is white.

Triangle color is gray.

Rectangle color is white.

The square is inside the circle.

The triangle is inside the rectangle.

فؽط اؼب ث١ئخ ػاللبد و١ببد :

االث١غ, اطبز{اى١ببد } اسائطح, اطثغ, اضش, اؽزط١,

اؼاللبد } ػاللخ ا, ػاللخ الغ{

color(circle,gray).

color(square, white).

color(triangle,gray).

color(rectangle,white).

inside(square,circle).

inside(triangle,rectangle).

٠ؼطف ف اـ اب رطو١ت اؼاللبد اص clausesرىزت ص اؼاللبد ف ععء اـ

predicates :ف

color(symbol,symbol).

inside(symbol,symbol).

symbolال ااع اى١ببد ع١ؼب ع domainsال حبعخ الؼزرسا ععء اـ

ع ل١بؼ غ١ط ؼمس.

(b) : procedures االجراءات

o A group of clauses which the head of each clause are the

same is called a procedure.

_ Example:

connects(Baghdad, Hillah, Kerbala).

Page 5: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 5Page

connects(Baghda, Al-Anbar, Diyala).

connects(Diwanyah, Samiyah, Kut).

(c) : rules القواعد A rule is a clause with one or more conditions. For a rule to

be true, all of its conditions must also be true.

o The right-hand side of the rule is a condition and the left-

hand side of the rule is the conclusion.

o The ':-' sign means 'if'. The clause in Example1 can be stated

as X is an o_spring of Y if Y is a parent of X.

o The left-hand side is called the head of the rule and the right-

hand side is called

o The body of the rule. The body of a rule is a list of goals

separated by commas.

o Commas are understood as conjunctions.

Example: p :- q, r, s, t. means p is true if q and r and s and t

are true.

(d) Facts الحقائق

Facts are always unconditionally true. It only has a head part (the

conclusion) with an empty body.

Example: has(student, books). احم١مخ اؼطفخ از ؽزسب اؼب رحذ أؼبغخ, اب امبػسح ف غع اشطؽ از

احمبئك الؼززبط ؼطفخ عس٠سح ا رحم١ك عاة الؼزفب ب.رطجك ػ

صبثذ ذب ازغ١طاد ػ ػىػ امبػسح از رمؽ ا ععأ٠ clauseرزى احم١مخ

: head bodyب

Headbody

طح١ح ا حظ ػ غػخ headؽززظ ا اـ bodyصا ٠ؼ ا ثزحمك شطؽ اـ

ثبى١ببد از ر احالب ف زغ١طار فا bodyاحي از رغؼ طح١حب اشا ٠زحمك اـ

ؼطفخ غ١ط عزح ا ىخ ف شه اؼب اص لب ثطف. headاـ

( رىزت ثصا : -ف ثبل اغبد طع ب ) ifعخ prologراظ ؽط٠مخ وزبثخ امبػسح ف

اشى:

head :- body.

ؼطفخ زحممخ. head فازحممب body ارا كا رؼ

ػسب headاحسح ا اوضط وب ا لس ٠حز فػ اؼاللخ ف ٠body clauseزى

٠ؽزسػ فؽ شار١ب.

inference rulesثماػس االؼزسالي prologش١ط ا غػخ اماػس ف ثطبظ

٠غت ا ال ٠ر prologاؼبغ اؼززبط اؼطفخ احمبئك ف لبػسح أؼطفخ ,وب ا ثطبظ

احمبئك ال آ١خ االؼززبط ٠غت ا رؽزس ف اب٠خ ا حمبئك صبثزخ رؽزسب اؼب اص

مبق احي رؼ ػ ا٠غبز حي الؼزفب اؽزرس ح , ػ ػىػ شه فا ػ١خ اشز

رصت ا االب٠خ ا اطط٠ك اغك ح١ش ال حي طم١خ ىخ.

(:2يثـــــــــــــال)

ؼأذص ضبال ػبب غػزب اشؽ١خ ػح ف١ ب١خ احمبئك اماػس االؼززبط

Page 6: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 6Page

اال اساذ ا اربضط ػطبضز اعطح األضع طوع اغػخ خاىاوت اضالص

أشػ, ؼظف صا اؼب اؼزبزا ا رطر١ت الغ اىاوت ثبؽجخ طوع ص ؽززظ

اؼطفخ ص احمبئك اضبثزخ:

The sun order is 0.

Mercury order is 1.

Venus order is 2.

Earth order is 3.

اؼطفخ از ط٠س احظي ػ١ب :

ا اىاوت االلطة شػ؟ ثؼ ػس مبضخ ا ووج١ و١ف ؼطف ا ب -

االلطة شػ أ ب االثؼس؟

و١ف رزطرت اىاوت رجؼب سضعخ حطاضرب ؟ ا ب االوضط حطاضح أ ب االوضط -

ثطزح؟

ثبؽجخ جؼؼب اجؼغ؟ب الغ اىاوت -

. رح٠ اطف ا ػاللبد:1

distance_order(sun,0).

distance_order(mercury,1).

distance_order(venus,2).

distance_order(earth,3).

.١distance_order(symbol,integer)ى اؼاللخ

االلطة ا االثؼس(: : اػف ثبل اىاوت ثظ١غخ حمبئك, اىاوت ) طرجخ تشي*

Mars, Jupiter, Saturn, Uranus, Neptune, Pluto

. ط١بغخ لاػس االؼززبط:2

االلطة Xفب اىوت Yاطغط رطر١ت لغ اىوت Xاشا وب رطر١ت لغ اىوت

nearer_to_sun(X,Y):-

distance_order(X,D1),distance_order(Y,D2),D1<D2.

ص احظ ػ D1 احمبئك اذع ف Xامبػسح رؼ: احظ ػ رطر١ت اىوت

Xلبض ث١ ازطر١ج١ فبشا وب االي اطغط اضب فب D2اذع ف Yرطر١ت اىوت

. Yالطة ا اشػ

فؽ ال اشػ ٠فزطع ا ال رمبض غ فؽب اش ال ٠ى ا ٠ى اى١ب " الطة ا

فؽ" ؽزض اشػ احي ىصا:

nearer_to_sun(X,Y):-

distance_order(X,D1),distance_order(Y,D2),D1<D2,X<>sun.

ا٢ ٠ى ؽزرس ا ٠ؽزفؽط ػ ا ووج١ ف اغػخ, وأ ٠ؽأي: االضع الطة

ا اشػ ػطبضز؟

Sun earth venus mercury

Page 7: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 7Page

nearer_to_sun(earth,mercury).

ثبشزمبق اح ثص اطط٠مخ: prologفؽط ٠م

X=earth

Y=venus

ف ٠D1جحش ػ غػخ ح ٠distance_order(earth,D1)أذص اغعء االي امبػسح

احمبئك اضبثزخ, ػس اجحش ال ٠زطبثك ازؼك االي اال ف احم١مخ اطاثؼخ

distance_order(earth,3) ف١طعغ ل١خD1=3

٠غط فػ distance_order(venus,D2)ا اغعء ازب امبػسح ص ٠ى

امبضخ وب ف اؼاللخ اال ف١حظ ػ اح ف احم١مخ اضبضخ

distance_order(venus,2) رى ل١خD2=2

D1ثب ا اشطؽ غ١ط زحمك ) D1,D2اغعء اضبش اؼاللخ امبضخ ث١ ل١ز

( ٠م افؽط ثمطغ االؼززبط ٠ؼ اؽزرس ا اؼطفخ اطة D2اطغط ١ؽذ

احظي ػ١ب غ١ط عزح ف صا اؼب.

٠ؽزط١غ اؽزرس االؼزفؽبض ثططق زػخ, ضال:

ا اىاوت الطة ا اشػ االضع؟

nearer_to_sun(X,earth).

: Xفزى غػخ ح ازغ١ط

X=mercury

X=venus

ا ا اىوج١ ػطبضز اعطح الطة ا اشػ االضع.

: ل ثظ١بغخ لبػسح بؼجخ إل٠غبز غػخ اح ػسب ٠سض ازؽبؤي حي اجؼس ١ػ تشي*

امطة

farther_from_sun(X,Y):-?

االوضط حطاضح: Xفا اىوت Yالطة ا اشػ اىوت Xاشا وب اىوت 2-2

hoter(X,Y):- nearer_to_sun(X,Y).

Yاثطز اىوت X: اوزت ط١غخ بؼجخ مبػسح رؽززظ اشا وب اىوت تشي*

colder(X,Y):-?

؟ X,Yا اىاوت رمغ ث١ اىوج١ 2-3

between(X,Y,Z):-

distance_order(X,D1),distance_order(Y,D2),distance_order(Z,D3),D1<D

3,D2>D3.

ضال: ا اىاوت رمغ ث١ االضع اشػ؟

between(sun,earth,X).

X=mercury

X=venus

(e) questions

Questions are queries for retrieving facts through rules. It only has

the body (the goals).

_ Example: fly(penguin).

Page 8: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 8Page

(f) arguments

Arguments can be an atom, a number, a variable, or a structure.

(g) atoms

o Atoms are concrete objects (but numbers are NOT atoms).

o The _rst letter of an atom is always a small letter.

o Strings of letters: tom, nil, x25, x 25AB, x , x y, top part

o Strings of special characters: <|>, ===>, ..., .:. (with caution)

o Quoted strings: 'Tom', 'South America'

:ايثهت نبعض انعهياث االساسيت

اؼ١بد احؽبث١خ:

/ + - * div mod

اساي احؽبث١خ:

abs(X) sin(X) log(X)

ػ١بد امبضخ:

> < >= <= <> =

ازؼ١ك:

% this is a comment

اؼ١بد اطم١خ:

not() , (and) ; (or)

اؼزرسا فػ ازغ١ط ػ عبج فػ اؼبزخ, وص احبخ: prologالحظخ: ال ٠غظ ف

X=X+1

(h) numbers

The syntax of integers is simple except one must remember that the

range of integers is limited to an interval, in SICSTUS Prolog,

between -16383 to 16383.

Examples: 3.1416, -0.001, 22.223, 100

numbers اب طح١حخ ا حم١م١خ :

ب االاع اج١ب١خ ام١بؼ١خ ف: ا

Integer -3,-1, 0, 1, 4

Real 5.6, 0, -1.2, 8

Char 'a', 'b'

String "a", "aaa", "***", "@1"

Symbol ali, a, ab صا اع ٠غت ا ٠حمك شطؽ اـع االي افطزاد

ثص اطط٠مخ: domainsاالاع اؼمسح وبمبئخ ؼطفب ف ععء اـ

x=integer* رؼ لبئخ االػساز اظح١حخ غ١ط حسزح اطي

s=string* رؼ لبئخ اؽالؼ اطع٠خ غ١ط حسزح اطي

(i) variables

o Variables are general objects.

o The first letter of a variable is always a Capital letter.

Page 9: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 9Page

o Example: X and Y in Example1.

o Variables can be substituted by another object (becomes

instantiated).

o Variables are assumed to be universally quantified (for all).

(j) recursive

Recursive is one of the fundamental principles of programming in

Prolog.

Example:

o predecessor (X, Z):- parent(X, Z).

o predecessor (X, Z):- parent(X, Y), predecessor(Y, Z).

سوف نتطرق الى االستدعاء الذات اكثر ف الملزمة تباعا ....

(k) comments

_ Example: /* this is a comment */

_ Example: % This is also a comment

(l) predicates

a functor that species some relationship existing in the problem domain.

ؼطف ف١ لات اؼاللبد رزى اؼ اؼاللخ زؼمبرب )اى١ببد(

relation_name(object1,object2,…., object).

( رى ثحبالد رزفخ, ب:termsرض اى١ببد ثفطزاد )

1 .atoms , ) _ ( رزى ؼؽخ احطف وج١طح طغ١طح االضلب اطع اربص :

شطؽ ا رجسأ ثحطف طغ١ط, ض:

elephant, b, abcXYZ , x_Y, x123

صا اع.اؼبء اؼاللبد ٠غت ا رحمك شطؽ

2 .variables رزى ؼؽخ احطف اىج١طح اظغ١طح االضلب اطع اربص :

) _ ( , شطؽ ا رجسأ ثحطف وج١ط ا ثبطع _ , ض:

Elephant, B, _abc, X_1, MyVariable, _

-لناخذ مثاال بسطا كاالت :

The child eats coconut.

عظ اس the childى١ببد اعزح ف اغخ اطف )اطف ٠أو عظ اس( , ا

coconut اب اؼاللخ از رطثؾ ث١ب فeats ػاللخ اطف ثغظ اس ا اطف(

وبزب: ٠prologأو( ػ١ ٠ز رض١ اغخ ثؼجبضح مجخ ف

eats( the_child, coconut).

٠زبب اطف, ضال:٠ىب رض١ اش١بء اذط

The child eats biscuit

The child eats meat

:ىصا

eats( the_child, biscuit).

eats(the_child, meat).

Page 10: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 11Page

ثؼس رط١ف اى١ببد اؼاللبد از رطثطب ثجؼغ , ٠ىب اعطاء ػ١بد االؼزفب حي

اؼب اطف.

اظ١غخ ابئ١خ جطبظ :

predicates

distance_order(symbol,integer).

nearer_to_sun(symbol,symbol).

hoter(symbol,symbol).

between(symbol,symbol,symbol).

clauses

distance_order(sun,0).

distance_order(mercury,1).

distance_order(venus,2).

distance_order(earth,3).

nearer_to_sun(X,Y):- distance_order(X,D1),

distance_order(Y,D2), D1<D2,X<>sun.

hoter(X,Y):- nearer_to_sun(X,Y).

between(X,Y,Z):-distance_order(X,D1), distance_order(Y,D2),

distance_order(Z,D3),D1<D3,D2>D3.

اطم١خ ال ٠حظط ا اؼزرسا اؼ١بد ال ع١غ اى١ببد ل١بؼ١خ domains** ال حبعخ ـ

ف اماػس , اب ٠ى اؼزرساب ف االؼزفب جبشطح:

يثـــــــــــــــال:

ؼزحسس ػ صالس ؽالة ى صالس زضعبد ص ؽزف ػ إالء اطالة زضعبر

ثططق زػخ:

Ali {50,40,60}

Muna {70,65,80}

Samir {90,70,75}

___________

predicates

degree(symbol,integer).

clauses

degree(ali,50).

degree(ali,40).

degree(ali,60).

degree(muna,70).

degree(muna,65).

degree(muna,80).

degree(samir,90).

degree(samir,70).

degree(samir,75).

. اؽت افؽط ا ٠ؽزطعغ ع١غ زضعبد ػ:1

Page 11: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 11Page

(1) degree(ali,X). (2)degree(Y,X),Y=ali.

X=50

X=40

X=60

ف احبخ اضب١خ لب ثبؼزطعبع ع١غ اسضعبد غ١غ اطالة ص اؼزجؼسب ١ػ

ػ.

:55. اؽت افؽط اؼزطعبع زضعبد ػ از رى اوجط 2

,)) andثـ اضثط X>50اؼزرس احس اظ١غز١ اػال اػف ا١ب اشطؽ

(1) degree(ali,X),X>50. (2)degree(Y,X),Y=ali,X>50.

X=60

. اػطع االؼبء اسضعبد ثبؼزضبء ػ زضعبر:3

degree(X,Y),X< >ali.

X=muna,Y=70

X=muna,Y=65

X=muna,Y=80

X=samir,Y=90

X=samir,Y=70

X=samir,Y=75

اكتب استفساس ياسب نكم ي:

از س٠ب زضعبد زؽب٠خ. اػطع االؼبء 4

75ال رؽب 65. اػطع االؼبء از س٠ب زضعبد اوجط ا رؽب 5

samir. اػطع ع١غ اسضعبد ثبؼزضبء حبز ا اسضعخ ضل ظع ا اؼ اطبت 6

زضعخ 35. اػطع اؼبء اطالة اص٠ س٠ زضعبد رم ػ زضعبد ا٢ذط٠ ة7

٠حمك ص االساف:(: اوزت ثطبظ 4ضبي)

test(1,X).

X=true

test(0,X).

X=false

test(4,X).

X=error

errorغ١ط شه االػساز ا falseا 5اـ trueا 1اجطبظ ٠م ثزظ١ف

predicates

test(integer,symbol).

clauses

test(1,error).

test(0,false).

test(X,error):-X<>0,X<>1.

-----------------------------------------------------------------------------------------

: اوزت ثطبظ ال٠غبز اؼسز االوجط ث١ ػسز٠:تشي

max(X,Y,Z).

max(2,5,Z).

Page 12: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 12Page

Z=5

Cut Statementعخ امطغ )!(

خ , ف ص احبخ ٠غت رؽزرس لف اجحش ػ حي اذط ػسب ٠زحمك اشطؽ اطافم

ازطو١ع ػ ا٠بد وزبثخ احمبئك.

( غ اؼزرسا عخ امطغ4(: اػبزح وزبثخ اضبي )5ضبي)

predicates

test(integer,symbol).

clauses

test(1,error):-!.

test(0,false):-!.

test( _ ,error).

طبثك احم١مخ اال ا اضب١خ غ اسف ف ٠م افؽط ثمطغ اجحش ػ حي اذط حبب رز

حبي رزحمك ا ب فب افؽط ٠ف ا اؼسز اسذ ١ػ احسا ال طفطا صه ٠طعغ

error .وز١غخ الؼزفؽبض

-------------------

:تشي

هقاث(. اكتب بشايح نتصيف االعذاد اني يوخبت وسانبت وصفش )اختش اواع ياسبت نهتع1

classify(X,Y).

اوزت ثطبظ حؽبة غع ػسز٠ ص رظ١ف اغع احزؽت ا عت ا يثال:

ؼبت )اػزجط اظفط ػ االػساز اعجخ(.

predicates

sum_class(integer,integer,string).

clauses

sum_calss(X,Y,C):-Z=X+Y,Z>=0,C="positive".

sum_class(X,Y,C):-Z=X+Y,Z<0,C="negative".

ا ثبؼزرسا عخ امطغ

sum_class(X,Y,C):-Z=X+Y,Z>=0,C="positive",!.

sum_class(_ ,_ ,"negative").

وبؼزفب .sum_class(-1,4,X) عطة ازذبي:

Recursionاالستذعاء انزاتي

وغعء اـ ٠headز االؼزسػبء اصار ذالي اؼزرسا فػ اؼاللخ از ٠زى ب اـ

body.ثغغ اظط ػ ازؽؽ اص رطز ف١ ,

٠عس ػ١ االؼزسػبء اصار:

1 .tail ٠ى اؼزرسا ػاللخ االؼزسػبء ف ب٠خ امبػسح :

rel1( ):-rel2( ),rel3( ), …, rel1( ).

2 .stackرؽزرس ػاللخ االؼزسػبء ف ا لغ ػ امبػسح ػسا اب٠خ :

rel1( ):-rel1( ),rel2( ), …,rel3( ).

rel1( ):-rel2( ),rel1( ),…,rel3( ).

Page 13: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 13Page

ثـــــــــال:ي

ثطبظ ال٠غبز اؼطة ؼسز طح١ح

predicates

fact(integer,real).

clauses

fact(0,1):-!.

fact(1,1):-!

fact(X,Y):-X1=X-1,fact(X1,Y1),Y=Y1*X.

Xحز ٠ظ Yح١ش ٠إع حؽبة ل١خ stackصا اجطبظ ضبي الؼزسػبء اصار ع

١حظ ػ Xا اظفط ا ااحس ص ٠ؼز ؼطة ام١خ اطاعؼخ ػ ازا ثم١خ

ام١خ ابئ١خ ؼطة.

زلف ثحصض ال افؽط ٠زلف ػ اشزمبق حي ثس٠خ اماػس ب ٠غت ا رزبض شطؽ ا

ح بئ. ػسب ٠ؼضط ػ حمبئك صبثزخ ٠م ثزطو١جب غ ثؼؼب فك اماػس ١ى

-----------------

( ـــــــال:8يثــ )

ثطبظ ال٠غبز برظ ضفغ ػسز طح١ح ا اغ طح١ح عت

predicates

power(integer,integer,real).

clauses

power(0, _ ,0):-!.

power(1, _ ,1):-!.

power( _, 0,1):-!.

power( X,1,X):-!.

power(X,Y,Z):-Y1=Y-1,power(X,Y1,Z1),Z=X*Z1.

اوزت ثطبظ ال٠غبز برظ ضفغ ػسز طح١ح ا اغ طح١ح ؼبت :تشي

write*** عخ اطجبػخ

write("***") write(X)رؽزرس طجبػخ ل١ ازغ١طاد اضاثذ , ضال

nl*** ؽجبػخ ؼطط عس٠س ثبؼزرسا

ؼ٠خ طجبػخ ف ؼطط عس٠س. write() nl*** غبجب رؽزرس

يثـــــــــال:

1بشايح نطباعت االسقاو ابتذاءا ي سقى يذخم زوال اني

predicates

count(integer).

clauses

count(1):-write(1),nl,!.

count(X):-write(X),nl,X1=X-1,count(X1),!.

count( _ ):-write("unable to count"),nl.

Page 14: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 14Page

طجبػخ ثشى رظبػس:

predicates

count(integer).

clauses

count(1):-write(1),nl,!.

count(X):-X>1,X1=X-1,count(X1),write(X),nl,!.

count( _ ):-write("unable to count"),nl.

:رط٠

اوزت ثطبظ ؼس اثزساءا ضل ازبءا ثطل آذط رظبػس٠ب طح اذط ربظ١ب:

count(4,7).

4

5

6

7

count(7,4).

7

6

5

4

ضــــــــبي:

ثطبظ طجبػخ ضش لبئ اعا٠خ ى اغ

draw(4)

* * * *

* * *

* *

*

predicates

draw(integer).

count(integer,integer)

clauses

draw(0):-!.

draw(1):-count(1,1),!.

draw(X):-X1=X-1,count(X1,0),draw(X1),!.

count(X,Y):-X=Y,write("*"),nl,!.

count(X,Y):-X>Y,X1=X-1,write("*"),count(X1,Y),!.

count(X,Y):-X<Y,!.

:رط٠

اجطبظ اؽبثك ثح١ش ٠ظط اضش غ١ط مة, ا ثصا اشى: اػس وزبثخ

*

* *

* * *

* * * *

Page 15: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 15Page

خايسا : شدشة انعائهت كثال تطبيقي نالستتاج واستخشاج حقائق باستخذاو انقواعذ اعتادا

عهي حقائق اخشى

االل اضؼ رطؾ ٠ػح شغطح ػبئزه اثزساءا عس٠ه ػ ا رحز ػ ضبي احس ػ

و طخ لطاثخ )االػب االذاي االذح االذاد اثبء االذح االذاد ... اد(

ؼفزطع ا س٠ب شغطح اؼبئخ:

. ػطف اعبغ و االفطاز:1

male(ali).

male(ahmed).

male(samir).

male(majid).

male(ali2).

male(saif).

male(zaid).

female(muna).

female(mays).

female(salma).

female(noor).

female(layla).

female(reem).

female(tiba).

. ػطف اؼاللبد االؼبؼ١خ: ػاللخ االثح غ١غ االفطاز ػاللخ االضرجبؽ 2

parent(ali,mays).

parent(ali,ahmed).

parent(ali,salma).

parent(ali,samir).

parent(muna,mays).

parent(muna,ahmed).

parent(muna,salma).

parent(muna,samir).

Ali Muna

Samir Salma Ahmed Mays

Layla Ali2 Noor Majid

Saif Reem Tiba Zaid

Page 16: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 16Page

parent(mays,reem).

parent(mays,saif).

parent(majid,reem).

parent(majid,saif).

parent(ahmed,tiba).

parent(noor,tiba).

parent(salma,zaid).

parent(ali2,zaid).

married(ali,muna).

married(majid,mays).

married(ahmed,noor).

married(salma,ali2).

married(samir,layla).

. اشئ لاػس الؼززبط اؼاللبد اؼبئ١خ االذط:3

وط ش Yاس Xاشا وب Yاة Xػاللخ االة: ٠ى 3-1

father(X,Y):-parent(X,Y),male(X).

اض اشا وبذ اسر Yا Xػاللخ اال: رى 3-2

mother(X,Y):-parent(X,Y),female(X).

اس )ا اسر( شوط Yاث اشا وب Xاالث: ٠ى 3-3

son(X,Y):-parent(Y,X),male(X).

٠ى ا ا اث١ شوط Yاشا وب Yاث Xا ٠ى

son(X,Y):-mother(Y,X),male(X);father(Y,X),male(X).

اشا وب اسب ا اسرب اض, ا اب ا اث١ب Yاثخ Xػاللخ اجذ: رى 3-4

اض

daughter(X,Y):-parent(Y,X),female(X).

daughter(X,Y):-mother(Y,X),female(X);father(Y,X),female(X).

اشا وب اة ا ا اة اث١ Yعس Xػاللخ اغس: ٠ى 3-5

grandfather(X,Y):-father(X,Z),father(Z,Y);father(X,Z),mother(Z,Y).

٠ىه ا رظف ثطط٠مخ اذط؟ فىط ..

ا ا ااشا وبذ ا اث١ Yعسح Xػاللخ اغسح: رى 3-6

grandmother(X,Y):-mother(X,Z),father(Z,Y);mother(X,Z),mother(Z,Y).

اعس ثبل اؼاللبد:

grandsonػاللخ احف١س 3-7

grand_daughterػاللخ احف١سح 3-8

brotherػاللخ االخ 3-9

sister ػاللخ االذذ 3-15

uncleػاللخ اؼ 3-11

uncle2 ػاللخ اربي 3-12

auntػاللخ اؼخ 3-13

aunt2ػاللخ اربخ 3-14

ػاللبد اث اؼ اث اربي اث اؼخ اث اربخ 3-15

ػاللبد ثذ اؼ ثذ اربي ثذ اؼخ ثذ اربخ 3-16

Page 17: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 17Page

nephewػاللخ اث االخ االذذ 3-17

nieceػاللخ ثذ االخ االذذ 3-18

husbandػاللخ اعط 3-19

wifeاععخ ػاللخ 3-25

سادسا : انقوائى

؟prolog. يا هي انقائت في 1

امبئخ: غػخ اؼبطط از ررع ثطط٠مخ زؽؽخ ال ٠ى اطي ؼظط ؼ١ ف١ب

ثطط٠مخ جبشطح, ئب ٠ز شه ػ ؽط٠ك امطاءح غ١غ اؼبطط از رؽجم ثظضح زؽؽخ

ثبززبثغ.

رحز ػسز غ١ط حسز اؼبطط ثس رحس٠س ؽجك )١ػ وجبل اغبد ٠ى مبئخ أ

از رفطع رحس٠س ؽي اظففخ ػس ازؼط٠ف(

رؼط٠ف امبئخ:

ؽزرس اظ١غخ ازب١خ domainsف ععء اـ

*اع اج١ب=اؼ امبئخ

ض:

x= integer*

ح١ش:

ضع احس أ أوضط ثشطؽ أ رطجك ػ١ب ( أ ؼؽخ ضع٠خ رزى xاؼ امبئخ )

( ال غ١ط.a-z أ ٠جسأ ثحطف ) Prologشطؽ االؼ ف

( احس األاع از زضؼبب ؼبثمبintegerاع اج١ب)

ثؼس رؼط٠ف امبئخ, ؽزرسب ف ١ى اؼاللخ ثفػ ؽط٠مخ اؼزرسا أ ع آذط شه ثىزبثخ

زؼط٠ف االؼ اص اؼزرسب

)...,اؼ امبئخ(=اؼ اؼاللخ

ض:

sum( x, real)

؟prolog. كيف تعايم يع انقائت في 2

زؼب غ امبئخ ػ ؽط٠ك لطاءح ػبططب ثبزؽؽ ااحس ر ا٢ذط ثؽحت )أ حغع(

ػسز حسز ره اؼبطط, وأ ؽحت ػظط احس , ػظط٠, صالصخ , أ أ ػسز آذط حسز

حؽت حبعخ ازطج١ك اص ؼ ػ١.

أب ػ١خ اؽحت فزز ػ ؽط٠ك رغعء امبئخ ئ ععأ٠ ب اجسا٠خ ازجم امبئخ

ثبؼزرسا اظ١غخ ازب١خ:

]ازجم امبئخ | اجسا٠خ[

أ

[Head | Tail]

فظ ث١ب ثفاط ػسز حسز اؼبطط , ئشا وبذ أوضط ػظط احس headح١ش

ف لبئخ احسح ال ٠غظ اؼزرسا افاط أ ؼحت ػبطط ب جبشطح, ضبي tail),( أب

ػ شه:

ؼحت ػظط احس امبئخ -

[X | T]

ؼحت ػظط٠ امبئخ -

Page 18: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 18Page

[X1,X2 | T]

ؼحت صالس ػبطط -

[X1,X2,X3 |T]

فب ال رعس حبعخ ئ ػ١خ اؽحت ىزت ف حبخ ئب ال ط٠س اطي ئ ػبطط امبئخ

اؼ ازغ١ط اص ٠حز ػبطط امبئخ ع١ؼب ثس ألاغ وب وب زغ١ط ع ث١ب

(.…,integer, realثؽ١ؾ غ١ط طوت )

اتبه:

, ئب اوزت Xألب رؼ لبئخ رحز ػظط احس Xزش١ط ئ امبئخ [X]ال رىزت -

X اغ زش١ط ئ امبئخ ثس األلX :از لس رى ئحس احبالد ازب١خ

[ ]لبئخ فبضغخ

[X]لبئخ رحز ػ ػظط احس

[X,Y]لبئخ رحز ػ ػظط٠ اص١

[X1,X2,…, Xn] اؼبطط nلبئخ رحز

ؼحت اؼظط ثبطط٠مخ أػال ال ٠ؼ اه رم ثؽحت ل١خ حسزح امبئخ , ف -

ثؼغ األح١ب ؽزرس ص اظ١غخ إلػبفخ ػظط ئ امبئخ, أ ئ اؼالخ ) | ( رؼ

رحس٠س أ ػعي لغ امبئخ ٠ى لطاءر ئشا وب ٠حز ػ ػظط أ رحس٠ض ئشا

وب فبضغب.

ػسب رم ثؽحت أ ئػبفخ اؼظط أ ئ امبئخ فبه ثصه رغزبظ لؼ رمف -

لغ ازب ثبز١غخ رؽزط١غ اطعع ئ١.ػس ا

. تعهى بعض األفكاس 3

و١ف رمطأ امبئخ

اجطبظ ازب ؼ١ؼ ػ ؽجبػخ ػبطط لبئخ , ضرت أفىبضن لج أ رجطظ:

حسز ع اج١ببد )ؼفزطع ئب أؼبء ؽالة( -1

name=symbol*

حسز اؼ اؼاللخ ػسز ؼبالرب -2

ى لط٠ج١ سف اجطبظ printؼرزبض اؼ

س٠ب أؼبء فمؾ صا حزبط ؼب احس لبئخ األؼبء

print(name)

لبئخ أض٠س أ أؽجغ -3 فىط ثػغ ئؼزطار١غ١خ اجطبظ, وأ رمي س

ال ٠ى أ ػبططب صا ٠ؼ ئ ؼأحزبط مطاءرب ع١ؼب اثزساء اؼظط األي أل

احسز ػسز ػبطط امبئخ فب ثحبعخ ئ لطاءرب حز مطخ اب٠خ ػسب احظ ػ

أ اؼحت ػظط احس ػ١خ اؽحت ؼزإز اغطع ص لبئخ فبضغخ, ف و طح ػ

أرؼب غ ازجم امبئخ ألػ١س اؼزرسا حز رز امبئخ:

1. print([X | T])

Tثبل األؼبء ف ازغ١ط Xذ ثؽحت االؼ األي ػؼز ف ازغ١ط ل

2. print([ X | T):-write(X)

Page 19: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 19Page

ا٢ رذ ؽجبػخ اؼظط األي, ى ب ظذ ثحبعخ ئ ؽجبػخ ثبل األؼبء!

3. print ([X|T]):-write(X),print(T).

ؼبطط, ف االؼزسػبء ازب ؼ١م اؼزرسذ االؼزسػبء اصار إلػبزح اؼ١خ ػ ثم١خ ا

عس٠سح ٠ؼ١س فػ اؼ١خ.... ى ز ؼ١زلف اجطبظ؟! Xاجطبظ ثمطغ

4. print([])

ػسب احظ ػ لبئخ فبضغخ ؼأل ثمطغ ازف١ص. امبئخ افبضغخ

ثطبغب ؼ١ى:اػس رطر١ت أفىبضن اوزت اجطبظ ثظ١غخ مجخ, ػ صا األؼبغ فب -4

domains

x=symbol*

predicates

print(x).

clauses

print([]):-!.

print([ X | T]):- write (X), nl, print (T).

ثص اطط٠مخ ؼزم ثطجبػخ اؼبطط األي ئ األذ١ط, فىطد أ رطجغ اؼبطط ثطط٠مخ

رإع ػ١خ اطجبػخ ثؼس االؼزسػبء زظ ئ ػىؽ١خ )أ األذ١ط ئ األي(؟ ب ضأ٠ه أ

اب٠خ ص رطجغ اؼبطط ف ؽط٠مه ؼزح ئ ثسا٠خ االؼزسػبء ؟

عطة ص

print([X|T]):-print(T),write(X),nl.

و١ف رؽد ثؼغ اؼبطط لبئخ ئ لبئخ أذط ؟

فؼال ى رزصوط ػسب لب ئ اؽحت ال ٠ؼ ثبؼطضح ؼحت ػظط عز

غطز حغع لغ ف امبئخ؟

صا اضبي ٠ؽبػسب ف ف فىطح ؽد ثؼغ اؼبطط, ؼم ثىزبثخ ثطبظ ثؽد األػساز

اعع١خ ئ لبئخ أذط

integer. حسز أاع اج١ببد : فطز ظع؟ ئش أػساز طح١حخ 1

x=integer*

اؼ evenثفطظ األػساز اعع١خ.. . حسز اؼ اؼاللخ ػسز ؼبالرب: ؼم2

اؼاللخ , س٠ب لبئخ أط١خ لبئخ ؼغ ف١ب اؼبطط اعع١خ .. ئش س٠ب ؼب١

*integerوالب لائ ػبططب أػساز طح١حخ

even(x,x).

. ل ثػغ ئؼزطار١غ١خ جطبظ, رؼب و١ف ؽحت ػبطط امبئخ ثبزؽؽ 3

األي , ا٢ ؼفىط ثى١ف١خ رطج١ك شطؽ أ ٠ى اؼظط ظع أ فطز )ئشا اجطبظ

أ ال( ػ أؼبغ ز١غخ اشطؽ مطض ئػبفخ اؼظط ئ 2وب ٠مج امؽخ ػ

امبئخ اغس٠سح أ ال.

ف ارطح األ اؼحت اؼظط امبئخ األ . ئشا وب اؼظط ٠مج امؽخ ػ

ط عس٠س ف امبئخ اضب١خ اؽد اؼظط ئ١, أب ئشا وب ػىػ شه فم احغع ػظ 2

ثؼ١خ اؽحت ػ امبئخ األ ز أ رؽحت ػظط امبئخ اضب١خ.. و١ف شه؟

1. even([X|T],Y)

ثم١ذ Yلس ر ؼحج امبئخ األ ,ث١ب امبئخ اضب١خ ٠Xؼ ئ اؼظط األي

ز رغ١١ط. ػ حبب

2. even([X|T],[Y1|T2])

Page 20: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 21Page

الزطغ امبئخ األ ر حغع الغ األي امبئخ اضب١خ ف Xاؼظط األي

Y1.

3. even([X|T],[Y1|T2]):-X mod 2=0

ص وطض اؼ١خ ػ ثم١خ Y1ل ثؽر ئ 2, ئشا وب ٠مج امؽخ ػ Xاذزجط

اؼبطط ثبؼزسػبء فػ اؼاللخ ئػطبءب ازجم امبئز١.

even([X|T],[Y1|T2]):-X mod 2=0,Y1=X, even(T,T2).

ى!

بشا فؼ ئشا وب اشطؽ ال ٠طجك ػ اؼظط؟

ب ثس أ اغاة ثؽ١ؾ: الطغ اؼظط امبئخ األ ارطن امبئخ اضب١خ ػ حب

ؼحت, اؼزسػ اؼاللخ عس٠س زفحض ثبل اؼبطط.

4. even ([X|T],Y):-X mod 2<>0,even(T,Y).

اعغ أفىبضن عس٠س ضرجب ثشى ثطبظ

domains

x=integer*

predicates

even(x,x).

clauses

even([],[]):-!.

even([X|T],[Y|T2]):-X mod 2=0, Y=X, even(T,T2).

even([X|T],Y):-X mod 2<>0, even(T,Y).

ئشا وذ ٠حج االذزظبض, عطة صا

even([],[]):-!.

even([X|T],[X|T2]):-X mod 2=0,even(T,T2),!.

even([ _ |T],Y):-even(T,Y).

١خ ف١ب رؽد صا ٠ؼ اه ؼزغ اجطبظ اصبة ئ امبػسح اضبضخ ػس رحمك امبػسح اضب

اؼظط األي ئ امبئخ اضب١خ جبشطح , ف حبخ ػس رحمك امبػسح اضب١خ فصا ٠ؼ ئ

اؼظط فطز ػسب ؼ١م ثمطؼ ف امبػسح اضبضخ ٠زطن امبئخ اضب١خ ز رغ١١ط.

و١ف رزحى ثؼسز اؼبطط اطثخ زفص سفه؟

ؼسالد ؽالة , ى ؽبت صالس زضعبد , ى اسضعبد افزطع اه رط٠س ئ رحؽت

١ؽذ ظػخ ثضالس لائ ئب زؽؽخ ف لبئخ احسح ثح١ش و ؽبت س٠ صالس

زضعبد ززبثؼخ ف امبئخ:

اطبت األي س٠ اسضعبد األ اضب١خ اضبضخ

اطبت اضب س٠ اسضعبد اطاثؼخ اربؽخ اؽبزؼخ

بت اضبش س٠ اسضعبد اؽبثؼخ اضبخ ازبؼؼخ ... ىصااط

*integerزضعبد *symbol. حسز أاع اج١ببد: س٠ه أؼبء 1

x=integer*

s=symbol*

. حسز اؼ اؼاللخ ػسز ع ؼبالرب 2

average ( s,x)

.ذطؾ زف١ص, صالس زضعبد زؽؽخ؟ ئش:3

Page 21: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 21Page

[X1,X2,X3|T] اؼ احس[Y|T2] أذ ا٢ رؽحت صالس ػبطط لبئخ

اسضعبد ػظط احس لبئخ األؼبء , بشا ثؼس؟

ػ١ه حؽبة اؼسي: اؼزرس زغ١طا ؼط١ب احؽت اؼسي:

Av=(X1+X2+X3)/3

average([Y|T1],[X1,X2,X3|T2]):-

Av=(X1+X2+X3)/3,write("name=",Y,"av=",Av), average(T1,T2).

فىط عس٠س.. بشا رى اسضعبد رىف ١ز رظ٠ؼب ػ اطالة ثح١ش ٠حظ

اغ١غ ػ صالس زضعبد؟

ا٢ ػ١ه أ رؼ١س حؽبة ع١غ االحزبالد

أ رز لبئخ اسضعبد لج لبئخ األؼبء! )مض ف ػسز اسضعبد( .1

زضعبد! 3أ ٠زجم زضعخ احسح , ف ح١ أذ رط٠س .2

زضعبد! 3زجم زضعزب , أذ رط٠س أ ٠ .3

أ رز لبئخ األؼبء لج لبئخ اسضعبد! )ظ٠بزح ف ػسز اسضعبد( .4

ف و ص احبالد ٠غت ئ٠مبف ازف١ص فضا .

رىزشف ثبماػس ازب١خ حؽت ازؽؽ:

1. average( _, []).

2. average( _, [X]).

3. average( _, [X,Y]).

4. average( [],_).

ال رؽ أ٠ؼب, ف ع١غ األحاي ػ١ه ئ٠مبف ازف١ص , فؼس ازبء امبئز١ ؼب

أفؼ احبالد ػ١ه ئ٠مبف اجطبظ:

average([],[]).

فطرت األفىبض زى ثطبظ مجي:

domains

x=integer*

s=symbol*

predicates

average(s,x).

clauses

average([],[]):-!.

average( _, []):-!.

average( _, [_]):-!.

average( _, [_,_]):-!.

average( [],_):-!.

average([Y|T1],[X1,X2,X3|T2]):-Av=(X1+X2+X3)/3,

write("name=",Y,"av=",Av),nl, average(T1,T2).

رصوط زائب:

Page 22: Programming in Logic Prolog - University of · PDF fileProgramming in Logic Prolog Mehdi Ebady Manaa 1 ... (The disk file that accompanies this project includes several variables containing

Artifical Intelligent 3rd

class – Department of Network

College of IT- University of Babylon

Date: Tuesday, March 04, 2014 22Page

اج١ب١خ ئ امائ ١ى ٠زى حساد اطغط ص احساد األاع

األؼبؼ١خ از زضؼبب ؼبثمب, ػ صا األؼبغ ٠ىه أ رؽزرس أ ػظط رمزطؼ امبئخ

ف أ ىب ٠ى أ ٠زبؼت غ ذظبئظ.

٠ىه أ رسذ ف ػاللخ أذط ؼبب ع Integerضال, اؼظط ع

integer طبغب اؽبثك ػ أؼبغ ص افىطح.وصا ثبؽجخ جم١خ اؼبطط. ؼم ثزحس٠ش ث

ف لبػسح ص realا٢ذط symbolافزطع ئ س٠ب ػاللخ ب ؼبال , احسب

اؼاللخ اظ١فخ از رإز٠ب م ثطجبػخ ص٠ اؼب١, ىصا:

domains

x=integer*

s=symbol*

predicates

average(s,x).

print(symbol,real).

Clauses

print(X,Y):-write("name=",X,"Av=",Y),nl.

average([],[]):-!.

average( _, []):-!.

average( _, [_]):-!.

average( _, [_,_]):-!.

average( [],_):-!.

average([Y|T1],[X1,X2,X3|T2]):-Av=(X1+X2+X3)/3,

print(Y,Av), average(T1,T2).

انشاخعBart, D., Phuong-Lan, N., Tom, S., & Troncon, R. (2005). The First 10 Prolog

Programming Contests. Belgium: ISBN.