Programming in Logic Prolog

advertisement
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
Programming in Logic
Prolog
Mehdi Ebady Manaa 1
Mouad Najam Abdullah 2
Master Student: Marwa Kadhim Ubeid
‫يهزيت يقذيت اني قسى شبكاث انًعهوياث‬
‫انًشحهت انثانثت‬
‫يادة انزكاء االصطُاعي‬
Assistance Lecturer in Department of Information Network at College of Information Technology 1
Assistance Lecturer in Department of Information Network at College of Information Technology 2
Page 1
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
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 2
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
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 3
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
ْ‫خ) ٌٕفػ اٌؼاللخ ال‬٠‫ اٌجسا‬ٟ‫ رىزت ف‬ٍٝ‫خ االػ‬٠ٌٛٚ‫اػس شاد اال‬ٛ‫اٌم‬ٚ ‫خ ( فبٌحمبئك‬٠ٌٛٚ‫اال‬
.ً‫ االؼف‬ٌٝ‫ ا‬ٍٝ‫ ِٓ االػ‬ٞ‫غط‬٠ ‫ ٘صٖ اٌٍغخ‬ٟ‫االؼزٕزبط ف‬
___________ )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 4
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
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 lefthand 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 righthand 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 ِٓ ‫مخ‬١‫ْ اٌحم‬ٛ‫رزى‬
: body ٚ head ‫ّ٘ب‬
Headbody
‫ػخ‬ّٛ‫ ِغ‬ٍٝ‫ ٔحظً ػ‬ٚ‫ح ا‬١‫ طح‬head ‫ ٔؽزٕزظ اْ اٌـ‬body ‫ؽ اٌـ‬ٚ‫ أٗ ثزحمك شط‬ٟٕ‫ؼ‬٠ ‫٘صا‬ٚ
ْ‫طارٗ فا‬١‫ ِزغ‬ٟ‫ب ف‬ٌٙ‫ رُ احال‬ٟ‫بٔبد اٌز‬١‫ ثبٌى‬body ‫زحمك اٌـ‬٠ ٌُ ‫اشا‬ٚ ً ‫حب‬١‫ رغؼٍٗ طح‬ٟ‫ي اٌز‬ٍٛ‫اٌح‬
.ٗ‫طف‬ٛ‫ لّٕب ث‬ٞ‫ شٌه اٌؼبٌُ اٌص‬ٟ‫ ِّىٕخ ف‬ٚ‫زح ا‬ٛ‫ع‬ِٛ ‫ط‬١‫ ِؼطفخ غ‬head ‫اٌـ‬
‫صا‬ٙ‫رىزت ث‬ٚ ) :- ( ‫ب‬ٌٙ ‫ٔطِع‬ٚ ‫ اٌٍغبد‬ٟ‫ ثبل‬ٟ‫ ف‬if ‫ عٍّخ‬prolog ٟ‫مخ وزبثخ اٌمبػسح ف‬٠‫ ؽط‬ٞ‫اظ‬ٛ‫ر‬
:ً‫اٌشى‬
head :- body.
.‫ ِؼطفخ ِزحممخ‬head ٌ‫ ِزحممب فا‬body ٌ‫ ارا كا‬ٟٕ‫ رؼ‬ٟ٘ٚ
‫ػٕس٘ب‬ٚ head ٟ‫ ٔفػ اٌؼاللخ ف‬ٞٛ‫حز‬٠ ‫ اوضط وّب أٗ لس‬ٚ‫احسح ا‬ٚ clause ِٓ body ْٛ‫زى‬٠
.‫ب‬١‫ ٔفؽٗ شار‬ٟ‫ؽزسػ‬٠
ٟ٘ٚ inference rules ‫اػس االؼزسالي‬ٛ‫ ثم‬prolog ‫ ثطٔبِظ‬ٟ‫اػس ف‬ٛ‫ػخ اٌم‬ّٛ‫ ِغ‬ٌٝ‫ط ا‬١‫ٔش‬
ٍٛ‫ر‬٠ ‫غت اْ ال‬٠ prolog ‫وّب اْ ثطٔبِظ‬, ‫ لبػسح أٌّؼطفخ‬ٟ‫اؼبغ اؼزٕزبط اٌّؼطفخ ِٓ اٌحمبئك ف‬
ٞ‫ حمبئك صبثزخ رؽزّس٘ب ِٓ اٌؼبٌُ اٌص‬ٌٝ‫خ ا‬٠‫ب‬ٌٕٙ‫ ا‬ٟ‫غت اْ رؽزٕس ف‬٠ ‫خ االؼزٕزبط‬١ٌ‫ِٓ اٌحمبئك الْ آ‬
‫ي‬ٍٛ‫خ اشزمبق اٌح‬١ٍّ‫ ػىػ شٌه فاْ ػ‬ٍٝ‫ػ‬ٚ , ٌٗٛ‫بَ اٌّؽزرسَ ح‬ٙ‫ي الؼزف‬ٍٛ‫غبز ح‬٠‫ ا‬ٍٝ‫رؼًّ ػ‬
.‫خ ِّىٕخ‬١‫ي ِٕطم‬ٍٛ‫ش ال ح‬١‫ك اٌّغٍك ح‬٠‫ اٌطط‬ٚ‫خ ا‬٠‫ب‬ٙٔ‫ اٌال‬ٌٝ‫رص٘ت ا‬
:)2(‫يثـــــــــــــال‬
‫االؼزٕزبط‬ٚ ‫اػس‬ٛ‫اٌم‬ٚ ‫خ اٌحمبئك‬١٘‫ٗ ِب‬١‫ػح ف‬ٌٕٛ ‫خ‬١‫ػزٕب اٌشّؽ‬ّٛ‫ ِغ‬ٛ٘ٚ ‫ؼٕأذص ِضبال ِٓ ػبٌّٕب‬
Page 5
Date: Tuesday, March 04, 2014
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Sun‬‬
‫‪mercury‬‬
‫‪Artifical Intelligent‬‬
‫‪venus‬‬
‫‪earth‬‬
‫اٌى‪ٛ‬اوت اٌضالصخ اال‪ ِٓ ٌٝٚ‬اٌساذً اٌ‪ ٝ‬اٌربضط ٘‪ ٟ‬ػطبضز ‪ٚ‬اٌع٘طح ‪ٚ‬األضع ‪ِٚ‬طوع اٌّغّ‪ٛ‬ػخ‬
‫٘‪ ٛ‬أٌشّػ‪ ,‬ؼٕظف ٘صا اٌؼبٌُ اؼزٕبزا اٌ‪ ٝ‬رطر‪١‬ت ِ‪ٛ‬الغ اٌى‪ٛ‬اوت ثبٌٕؽجخ ٌٍّطوع ‪ ِٓٚ‬صُ ٔؽزٕزظ‬
‫اٌّؼطفخ ِٓ ٘صٖ اٌحمبئك اٌضبثزخ‪:‬‬
‫‪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.‬‬
‫‪ٚ‬اٌمبػسح رؼٕ‪ :ٟ‬احظً ػٍ‪ ٝ‬رطر‪١‬ت اٌى‪ٛ‬وت ‪ ِٓ X‬اٌحمبئك ‪ٚ‬اذعٔٗ ف‪ D1 ٟ‬صُ احظً ػٍ‪ٝ‬‬
‫رطر‪١‬ت اٌى‪ٛ‬وت ‪ٚY‬اذعٔٗ ف‪ٚ D2 ٟ‬لبضْ ث‪ ٓ١‬اٌزطر‪١‬ج‪ ٓ١‬فبشا وبْ اال‪ٚ‬ي اطغط ِٓ اٌضبٔ‪ ٟ‬فبْ ‪X‬‬
‫الطة اٌ‪ ٝ‬اٌشّػ ِٓ ‪. Y‬‬
‫‪ٚ‬الْ اٌشّػ ‪٠‬فزطع اْ ال رمبضْ ِغ ٔفؽ‪ٙ‬ب اش ال ‪ّ٠‬ىٓ اْ ‪٠‬ى‪ ْٛ‬اٌى‪١‬بْ " الطة اٌ‪ٔ ٝ‬فؽٗ ِٓ‬
‫ٔفؽٗ" ٔؽزضٕ‪ ٟ‬اٌشّػ ِٓ اٌحٍ‪ٛ‬ي ٘ىصا‪:‬‬
‫‪nearer_to_sun(X,Y):‬‬‫‪distance_order(X,D1),distance_order(Y,D2),D1<D2,X<>sun.‬‬
‫‪ٚ‬ا‪ّ٠ ْ٢‬ىٓ ٌٍّؽزرسَ اْ ‪٠‬ؽزفؽط ػٓ ا‪ ٞ‬و‪ٛ‬وج‪ ٓ١‬ف‪ ٟ‬اٌّغّ‪ٛ‬ػخ‪ ,‬وأْ ‪٠‬ؽأي‪ ً٘ :‬االضع الطة‬
‫اٌ‪ ٝ‬اٌشّػ ِٓ ػطبضز؟‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 6‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫‪nearer_to_sun(earth,mercury).‬‬
‫‪٠ٚ‬م‪ِ َٛ‬فؽط ‪prolog‬ثبشزمبق اٌحً ث‪ٙ‬صٖ اٌطط‪٠‬مخ‪:‬‬
‫‪X=earth‬‬
‫‪Y=venus‬‬
‫‪٠‬أذص اٌغعء اال‪ٚ‬ي ِٓ اٌمبػسح )‪٠ٚ distance_order(earth,D1‬جحش ػٓ ِغّ‪ٛ‬ػخ حً ‪ D1‬ف‪ٟ‬‬
‫اٌحمبئك اٌضبثزخ‪ٚ ,‬ػٕس اٌجحش ال ‪٠‬زطبثك اٌّزؼٍك اال‪ٚ‬ي اال ف‪ ٟ‬اٌحم‪١‬مخ اٌطاثؼخ‬
‫)‪ distance_order(earth,3‬ف‪١‬طعغ ل‪ّ١‬خ ‪D1=3‬‬
‫صُ ‪٠‬ىًّ اٌ‪ ٝ‬اٌغعء اٌزبٌ‪ ِٓ ٟ‬اٌمبػسح ‪٠ٚ distance_order(venus,D2) ٛ٘ٚ‬غط‪ٔ ٞ‬فػ‬
‫اٌّمبضٔخ وّب ف‪ ٟ‬اٌؼاللخ اال‪ ٌٝٚ‬ف‪١‬حظً ػٍ‪ ٝ‬اٌحً ف‪ ٟ‬اٌحم‪١‬مخ اٌضبٌضخ‬
‫)‪ٚ distance_order(venus,2‬رى‪ ْٛ‬ل‪ّ١‬خ ‪D2=2‬‬
‫اٌغعء اٌضبٌش ِٓ اٌؼاللخ ٘‪ ٛ‬اٌّمبضٔخ ث‪ ٓ١‬ل‪ّ١‬ز‪ٚ D1,D2 ٟ‬ثّب اْ اٌشطؽ غ‪١‬ط ِزحمك ( ‪D1‬‬
‫ٌ‪١‬ؽذ اطغط ِٓ ‪٠ )D2‬م‪ َٛ‬اٌّفؽط ثمطغ االؼزٕزبط ‪٠ٚ‬ؼٍُ اٌّؽزرسَ اْ اٌّؼطفخ اٌّطٍ‪ٛ‬ة‬
‫اٌحظ‪ٛ‬ي ػٍ‪ٙ١‬ب غ‪١‬ط ِ‪ٛ‬ع‪ٛ‬زح ف‪٘ ٟ‬صا اٌؼبٌُ‪.‬‬
‫‪٠‬ؽزط‪١‬غ اٌّؽزرسَ االؼزفؽبض ثططق ِزٕ‪ٛ‬ػخ‪ِ ,‬ضال‪:‬‬
‫ا‪ ٞ‬اٌى‪ٛ‬اوت الطة اٌ‪ ٝ‬اٌشّػ ِٓ االضع؟‬
‫‪nearer_to_sun(X,earth).‬‬
‫فزى‪ِ ْٛ‬غّ‪ٛ‬ػخ حً اٌّزغ‪١‬ط ‪:ٟ٘ X‬‬
‫‪X=mercury‬‬
‫‪X=venus‬‬
‫ا‪ ٞ‬اْ اٌى‪ٛ‬وج‪ ٓ١‬ػطبضز ‪ٚ‬اٌع٘طح الطة اٌ‪ ٝ‬اٌشّػ ِٓ االضع‪.‬‬
‫*تًشيٍ‪ :‬لُ ثظ‪١‬بغخ لبػسح ِٕبؼجخ إل‪٠‬غبز ِغّ‪ٛ‬ػخ اٌحً ػٕسِب ‪٠‬س‪ٚ‬ض اٌزؽبؤي ح‪ٛ‬ي اٌجؼس ‪١ٌٚ‬ػ‬
‫اٌمطة‬
‫?‪farther_from_sun(X,Y):-‬‬
‫‪ 2-2‬اشا وبْ اٌى‪ٛ‬وت ‪X‬الطة اٌ‪ ٝ‬اٌشّػ ِٓ اٌى‪ٛ‬وت ‪ Y‬فاْ اٌى‪ٛ‬وت ‪ ٛ٘ X‬االوضط حطاضح‪:‬‬
‫‪hoter(X,Y):- nearer_to_sun(X,Y).‬‬
‫*تًشيٍ‪ :‬اوزت ط‪١‬غخ ِٕبؼجخ ٌمبػسح رؽزٕزظ اشا وبْ اٌى‪ٛ‬وت ‪ X‬اثطز ِٓ اٌى‪ٛ‬وت ‪Y‬‬
‫?‪colder(X,Y):-‬‬
‫‪ 3-2‬ا‪ ٞ‬اٌى‪ٛ‬اوت رمغ ث‪ ٓ١‬اٌى‪ٛ‬وج‪ X,Y ٓ١‬؟‬
‫‪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).‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 7‬‬
3rd class – Department of Network
College of IT- University of Babylon
Artifical Intelligent
(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 8
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
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( ‫بٔبد ثّفطزاد‬١‫رّضً اٌى‬
, ) _ ( ‫اٌطِع اٌربص‬ٚ َ‫االضلب‬ٚ ‫طح‬١‫طغ‬ٚ ‫طح‬١‫ف وج‬ٚ‫ْ ِٓ ؼٍؽٍخ ِٓ اٌحط‬ٛ‫ رزى‬: atoms .1
:ً‫ ِض‬,‫ط‬١‫شطؽ اْ رجسأ ثحطف طغ‬
elephant, b, abcXYZ , x_Y, x123
.‫ع‬ٌٕٛ‫غت اْ رحمك شطؽ ٘صا ا‬٠ ‫اؼّبء اٌؼاللبد‬
‫اٌطِع اٌربص‬ٚ َ‫االضلب‬ٚ ‫طح‬١‫اٌظغ‬ٚ ‫طح‬١‫ف اٌىج‬ٚ‫ْ ِٓ ؼٍؽٍخ ِٓ اٌحط‬ٛ‫ رزى‬: variables .2
:ً‫ ِض‬, _ ‫ ثبٌطِع‬ٚ‫ط ا‬١‫ شطؽ اْ رجسأ ثحطف وج‬, ) _ (
Elephant, B, _abc, X_1, MyVariable, _
-: ً‫لناخذ مثاال بسٌطا كاالت‬
The child eats coconut.
‫ٕس‬ٌٙ‫ظ ا‬ٛ‫ع‬ٚ the child ً‫ اٌطف‬ٟ٘ ‫ اٌغٍّخ‬ٟ‫زح ف‬ٛ‫ع‬ٌّٛ‫بٔبد ا‬١‫ اٌى‬, )‫ٕس‬ٌٙ‫ظ ا‬ٛ‫أوً ع‬٠ ً‫(اٌطف‬
ً‫ اْ اٌطف‬ٟ٘ ‫ٕس‬ٌٙ‫ظ ا‬ٛ‫ (ػاللخ اٌطفً ثغ‬eats ٟٙ‫ّب ف‬ٕٙ١‫ رطثؾ ث‬ٟ‫ اِب اٌؼاللخ اٌز‬coconut
:ٌٟ‫ وبٌزب‬prolog ٟ‫ٌخ ف‬ٛ‫ً اٌغٍّخ ثؼجبضح ِمج‬١‫زُ رّض‬٠ ٗ١ٍ‫ػ‬ٚ )ٍٗ‫أو‬٠
eats( the_child, coconut).
:‫ ِضال‬,ً‫ب اٌطف‬ٌٙٚ‫زٕب‬٠ ٜ‫بء اذط‬١‫ً اش‬١‫ّىٕٕب رّض‬٠
The child eats biscuit
The child eats meat
:‫٘ىصا‬
eats( the_child, biscuit).
eats(the_child, meat).
Page 9
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
‫ي‬ٛ‫بَ ح‬ٙ‫بد االؼزف‬١ٍّ‫ّىٕٕب اعطاء ػ‬٠ , ‫ب ثجؼغ‬ٙ‫ رطثط‬ٟ‫اٌؼاللبد اٌز‬ٚ ‫بٔبد‬١‫ف اٌى‬١‫ط‬ٛ‫ثؼس ر‬
.‫ف‬ٛ‫ط‬ٌّٛ‫اٌؼبٌُ ا‬
:ٟ٘ ‫خ ٌٍجطٔبِظ‬١‫بئ‬ٌٕٙ‫غخ ا‬١‫اٌظ‬
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
Date: Tuesday, March 04, 2014
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫‪(1) degree(ali,X). (2)degree(Y,X),Y=ali.‬‬
‫‪X=50‬‬
‫‪X=40‬‬
‫‪X=60‬‬
‫ف‪ ٟ‬اٌحبٌخ اٌضبٔ‪١‬خ لّٕب ثبؼزطعبع عّ‪١‬غ اٌسضعبد ٌغّ‪١‬غ اٌطالة ‪ ِٓٚ‬صُ اؼزجؼسٔب ِٓ ٘‪١ٌ ٛ‬ػ‬
‫ػٍ‪.ٟ‬‬
‫‪ .2‬اؽٍت ِٓ اٌّفؽط اؼزطعبع زضعبد ػٍ‪ ٟ‬اٌز‪ ٟ‬رى‪ ْٛ‬اوجط ِٓ ‪:55‬‬
‫اؼزرسَ احس‪ ٜ‬اٌظ‪١‬غز‪ ٓ١‬اػالٖ ‪ٚ‬اػف اٌ‪ٙ١‬ب اٌشطؽ ‪ٚX>50‬اضثطٗ ثـ ‪(,( and‬‬
‫‪(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‬اػطع االؼّبء اٌز‪ٌ ٟ‬س‪ٙ٠‬ب زضعبد ِزؽب‪٠ٚ‬خ‬
‫‪ .5‬اػطع االؼّبء اٌز‪ٌ ٟ‬س‪ٙ٠‬ب زضعبد اوجط ِٓ ا‪ ٚ‬رؽب‪ٚ 65 ٞٚ‬ال رؽب‪75 ٞٚ‬‬
‫‪ .6‬اػطع عّ‪١‬غ اٌسضعبد ثبؼزضٕبء حبٌز‪ ٟ‬اْ اٌسضعخ ضلُ ظ‪ٚ‬ع‪ ٟ‬ا‪ ٚ‬اؼُ اٌطبٌت ‪samir‬‬
‫‪ .7‬اػطع اؼّبء اٌطالة اٌص‪ٌ ٓ٠‬س‪ ُٙ٠‬زضعبد رمً ػٓ زضعبد ا‪٢‬ذط‪ ٓ٠‬ة‪ 35‬زضعخ‬
‫ِضبي(‪ :)4‬اوزت ثطٔبِظ ‪٠‬حمك ٘صٖ اال٘ساف‪:‬‬
‫‪test(1,X).‬‬
‫‪X=true‬‬
‫‪test(0,X).‬‬
‫‪X=false‬‬
‫‪test(4,X).‬‬
‫‪X=error‬‬
‫اٌجطٔبِظ ‪٠‬م‪ َٛ‬ثزظٕ‪١‬ف ‪ 1‬اٌ‪ٚ true ٝ‬اٌـ ‪ 5‬اٌ‪ٚ false ٝ‬غ‪١‬ط شٌه ِٓ االػساز اٌ‪error ٝ‬‬
‫‪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).‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 11‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫‪Z=5‬‬
‫عٍّخ اٌمطغ (!) ‪Cut Statement‬‬
‫رؽزرسَ ٌ‪ٛ‬لف اٌجحش ػٓ حٍ‪ٛ‬ي اذط‪ ٜ‬ػٕسِب ‪٠‬زحمك اٌشطؽ اٌّطافمخ ٌٗ‪ٚ ,‬ف‪٘ ٟ‬صٖ اٌحبٌخ ‪٠‬غت‬
‫اٌزطو‪١‬ع ػٍ‪ ٝ‬ا‪٠ٌٛٚ‬بد وزبثخ اٌحمبئك‪.‬‬
‫ِضبي(‪ :)5‬اػبزح وزبثخ اٌّضبي (‪ِ )4‬غ اؼزرساَ عٍّخ اٌمطغ‬
‫‪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‬ثغغ إٌظط ػٓ اٌزؽٍؽً اٌص‪ ٞ‬رطز ف‪.ٗ١‬‬
‫‪ٛ٠‬عس ٔ‪ٛ‬ػ‪ ِٓ ٓ١‬االؼزسػبء اٌصار‪:ٟ‬‬
‫‪٠ : tail .1‬ى‪ ْٛ‬اؼزرساَ ػاللخ االؼزسػبء ف‪ٙٔ ٟ‬ب‪٠‬خ اٌمبػسح‬
‫‪rel1( ):-rel2( ),rel3( ), …, rel1( ).‬‬
‫‪ :stack .2‬رؽزرسَ ػاللخ االؼزسػبء ف‪ ٟ‬ا‪ِٛ ٞ‬لغ ػّٓ اٌمبػسح ػسا إٌ‪ٙ‬ب‪٠‬خ‬
‫‪rel1( ):-rel1( ),rel2( ), …,rel3( ).‬‬
‫‪rel1( ):-rel2( ),rel1( ),…,rel3( ).‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 12‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫يثـــــــــال‪:‬‬
‫ثطٔبِظ ال‪٠‬غبز اٌّؼط‪ٚ‬ة ٌؼسز طح‪١‬ح‬
‫‪predicates‬‬
‫‪fact(integer,real).‬‬
‫‪clauses‬‬
‫‪fact(0,1):-!.‬‬
‫!‪fact(1,1):-‬‬
‫‪fact(X,Y):-X1=X-1,fact(X1,Y1),Y=Y1*X.‬‬
‫٘صا اٌجطٔبِظ ِضبي ٌالؼزسػبء اٌصار‪ٛٔ ِٓ ٟ‬ع ‪ stack‬ح‪١‬ش ‪٠‬إعً حؽبة ل‪ّ١‬خ ‪ Y‬حز‪٠ ٝ‬ظً ‪X‬‬
‫اٌ‪ ٝ‬اٌظفط ا‪ ٚ‬اٌ‪ٛ‬احس ‪ ِٓٚ‬صُ ‪٠‬ؼ‪ٛ‬ز ٌؼطة اٌم‪ّ١‬خ اٌطاعؼخ ػٍ‪ ٝ‬اٌز‪ٛ‬اٌ‪ ٟ‬ثم‪ّ١‬خ ‪١ٌ 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(X) ٚ write‬‬
‫*** ؽجبػخ ؼطط عس‪٠‬س ثبؼزرساَ ‪nl‬‬
‫*** غبٌجب رؽزرسَ )(‪ nlٚ write‬ؼ‪٠ٛ‬خ ٌٍطجبػخ ف‪ ٟ‬ؼطط عس‪٠‬س‪.‬‬
‫يثـــــــــال‪:‬‬
‫بشَايح نطباعت االسقاو ابتذاءا يٍ سقى يذخم َزوال اني ‪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.‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 13‬‬
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
:ٞ‫ٌٍطجبػخ ثشىً رظبػس‬ٚ
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 14
Date: Tuesday, March 04, 2014
3rd class – Department of Network
College of IT- University of Babylon
Artifical Intelligent
‫ شدشة انعائهت كًثال تطبيقي نالستُتاج واستخشاج حقائق باستخذاو انقواعذ اعتًادا‬: ‫خايسا‬
‫عهي حقائق اخشى‬
ً‫ االل‬ٍٝ‫احس ػ‬ٚ ‫ ِضبي‬ٍٝ‫ ػ‬ٞٛ‫ اْ رحز‬ٍٝ‫ه ػ‬٠‫ػح شغطح ػبئٍزه اثزساءا ِٓ ع ّس‬ٛ٠ ‫اضؼُ ِرطؾ‬
)‫ اٌد‬... ‫اد‬ٛ‫االذ‬ٚ ‫ح‬ٛ‫اثٕبء االذ‬ٚ ‫اد‬ٛ‫االذ‬ٚ ‫ح‬ٛ‫االذ‬ٚ ‫اي‬ٛ‫االذ‬ٚ َ‫ِٓ وً طٍخ لطاثخ (االػّب‬
:‫ٕب شغطح اٌؼبئٍخ‬٠‫ؼٕفزطع اْ ٌس‬
Ali
Reem
Muna
Mays
Ahmed
Salma
Samir
Majid
Noor
Ali2
Layla
Tiba
Saif
Zaid
:‫ ػطف اعٕبغ وً االفطاز‬.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).
Page 15
Date: Tuesday, March 04, 2014
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
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 ْٛ‫ى‬٠ :‫ ػاللخ االة‬1-3
father(X,Y):-parent(X,Y),male(X).
ٝ‫ أض‬ٟ٘ٚ ٗ‫اٌسر‬ٚ ‫ اشا وبٔذ‬Y َ‫ ا‬X ْٛ‫ رى‬:َ‫ ػاللخ اال‬2-3
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 ْٛ‫ رى‬:‫ ػاللخ اٌجٕذ‬4-3
ٝ‫أض‬
daughter(X,Y):-parent(Y,X),female(X).
daughter(X,Y):-mother(Y,X),female(X);father(Y,X),female(X).
ٗ١‫ اة اث‬ٚ‫ اشا وبْ اة اِٗ ا‬Y ‫ عس‬X ْٛ‫ى‬٠ :‫ ػاللخ اٌغس‬5-3
grandfather(X,Y):-father(X,Z),father(Z,Y);father(X,Z),mother(Z,Y).
.. ‫؟ فىط‬ٜ‫مخ اذط‬٠‫ّىٕه اْ رظفٗ ثطط‬٠ ً٘
ِٗ‫ اَ ا‬ٚ‫ٗ ا‬١‫ اشا وبٔذ اَ اث‬Y ‫ ع ّسح‬X ْٛ‫ رى‬:‫ ػاللخ اٌغ ّسح‬6-3
grandmother(X,Y):-mother(X,Z),father(Z,Y);mother(X,Z),mother(Z,Y).
:‫ اٌؼاللبد‬ٟ‫عس ثبل‬ٚ‫ا‬
grandson ‫س‬١‫ ػاللخ اٌحف‬7-3
grand_daughter ‫سح‬١‫ ػاللخ اٌحف‬8-3
brother ‫ ػاللخ االخ‬9-3
sister ‫ ػاللخ االذذ‬15-3
uncle ُ‫ ػاللخ اٌؼ‬11-3
uncle2‫ ػاللخ اٌربي‬12-3
aunt ‫ ػاللخ اٌؼّخ‬13-3
aunt2 ‫ ػاللخ اٌربٌخ‬14-3
‫اثٓ اٌربٌخ‬ٚ ‫اثٓ اٌؼّخ‬ٚ ‫اثٓ اٌربي‬ٚ ُ‫ ػاللبد اثٓ اٌؼ‬15-3
‫ثٕذ اٌربٌخ‬ٚ ‫ثٕذ اٌؼّخ‬ٚ ‫ثٕذ اٌربي‬ٚ ُ‫ ػاللبد ثٕذ اٌؼ‬16-3
Page 16
Date: Tuesday, March 04, 2014
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫‪ 17-3‬ػاللخ اثٓ االخ ‪ٚ‬االذذ ‪nephew‬‬
‫‪ 18-3‬ػاللخ ثٕذ االخ ‪ٚ‬االذذ ‪niece‬‬
‫‪ 19-3‬ػاللخ اٌع‪ٚ‬ط ‪husband‬‬
‫‪ 25-3‬ػاللخ اٌع‪ٚ‬عخ ‪wife‬‬
‫سادسا ‪ :‬انقوائى‬
‫‪ .1‬يا هي انقائًت في ‪prolog‬؟‬
‫اٌمبئّخ‪ِ :‬غّ‪ٛ‬ػخ ِٓ اٌؼٕبطط اٌز‪ ٟ‬ررعْ ثطط‪٠‬مخ ِزؽٍؽٍخ ‪ٚ‬ال ‪ّ٠‬ىٓ اٌ‪ٛ‬ط‪ٛ‬ي ٌؼٕظط ِؼ‪ ٓ١‬ف‪ٙ١‬ب‬
‫ثطط‪٠‬مخ ِجبشطح‪ ,‬ئّٔب ‪٠‬زُ شٌه ػٓ ؽط‪٠‬ك اٌمطاءح ٌغّ‪١‬غ اٌؼٕبطط اٌز‪ ٟ‬رؽجمٗ ثظ‪ٛ‬ضح ِزؽٍؽٍخ‬
‫‪ٚ‬ثبٌززبثغ‪.‬‬
‫‪ّ٠‬ىٓ ٌٍمبئّخ أْ رحز‪ ٞٛ‬ػسز غ‪١‬ط ِحسز ِٓ اٌؼٕبطط ‪ٚ‬ثس‪ ْٚ‬رحس‪٠‬س ِؽجك (‪١ٌٚ‬ػ وجبل‪ ٟ‬اٌٍغبد‬
‫اٌز‪ ٟ‬رفطع رحس‪٠‬س ؽ‪ٛ‬ي اٌّظف‪ٛ‬فخ ػٕس اٌزؼط‪٠‬ف)‬
‫رؼط‪٠‬ف اٌمبئّخ‪:‬‬
‫ف‪ ٟ‬ععء اٌـ ‪ٔ domains‬ؽزرسَ اٌظ‪١‬غخ اٌزبٌ‪١‬خ‬
‫*إٌ‪ٛ‬ع اٌج‪١‬بٔ‪=ٟ‬اؼُ اٌمبئّخ‬
‫ِضً‪:‬‬
‫*‪x= integer‬‬
‫ح‪١‬ش‪:‬‬
‫اؼُ اٌمبئّخ (‪ ٛ٘ )x‬أ‪ ٞ‬ؼٍؽٍخ ضِع‪٠‬خ رزى‪ ِٓ ْٛ‬ضِع ‪ٚ‬احس أ‪ ٚ‬أوضط ثشطؽ أْ رٕطجك ػٍ‪ٙ١‬ب‬
‫شط‪ٚ‬ؽ االؼُ ف‪ ٟ٘ٚProlog ٟ‬أْ ‪٠‬جسأ ثحطف (‪ )a-z‬ال غ‪١‬ط‪.‬‬
‫إٌ‪ٛ‬ع اٌج‪١‬بٔ‪ ٛ٘ )integer(ٟ‬احس األٔ‪ٛ‬اع اٌز‪ ٟ‬زضؼٕب٘ب ؼبثمب‬
‫ثؼس رؼط‪٠‬ف اٌمبئّخ‪ٔ ,‬ؽزرسِ‪ٙ‬ب ف‪١٘ ٟ‬ىً اٌؼاللخ ثٕفػ ؽط‪٠‬مخ اؼزرساَ أ‪ٛٔ ٞ‬ع آذط ‪ٚ‬شٌه ثىزبثخ‬
‫االؼُ اٌص‪ ٞ‬اؼزرسِٕبٖ ٌٍزؼط‪٠‬ف‬
‫(‪,...‬اؼُ اٌمبئّخ)=اؼُ اٌؼاللخ‬
‫ِضً‪:‬‬
‫)‪sum( x, real‬‬
‫‪ .2‬كيف َتعايم يع انقائًت في ‪prolog‬؟‬
‫ٔزؼبًِ ِغ اٌمبئّخ ػٓ ؽط‪٠‬ك لطاءح ػٕبطط٘ب ثبٌزؽٍؽً اٌ‪ٛ‬احس رٍ‪ ٛ‬ا‪٢‬ذط ‪ٚ‬ثؽحت (أ‪ ٚ‬حغع)‬
‫ػسز ِحسز ِٓ رٍه اٌؼٕبطط‪ ,‬وأْ ٔؽحت ػٕظط ‪ٚ‬احس ‪ ,‬ػٕظط‪ ,ٓ٠‬صالصخ ‪ ,‬أ‪ ٚ‬أ‪ ٞ‬ػسز آذط ِحسز‬
‫حؽت حبعخ اٌزطج‪١‬ك اٌص‪ٔ ٞ‬ؼًّ ػٍ‪.ٗ١‬‬
‫أِب ػٍّ‪١‬خ اٌؽحت فززُ ػٓ ؽط‪٠‬ك رغع‪ٞ‬ء اٌمبئّخ ئٌ‪ ٝ‬ععأ‪ّ٘ ٓ٠‬ب اٌجسا‪٠‬خ ‪ٚ‬اٌّزجم‪ ِٓ ٟ‬اٌمبئّخ‬
‫ثبؼزرساَ اٌظ‪١‬غخ اٌزبٌ‪١‬خ‪:‬‬
‫]اٌّزجم‪ ِٓ ٟ‬اٌمبئّخ | اٌجسا‪٠‬خ[‬
‫أ‪ٚ‬‬
‫]‪[Head | Tail‬‬
‫ح‪١‬ش ‪ ٛ٘ head‬ػسز ِحسز ِٓ اٌؼٕبطط ‪ٚ ,‬ئشا وبٔذ أوضط ِٓ ػٕظط ‪ٚ‬احس ٔفظً ث‪ٕٙ١‬ب ثف‪ٛ‬اطً‬
‫(‪ ),‬أِب ‪ tail‬ف‪ ٛٙ‬لبئّخ ‪ٚ‬احسح ‪ٚ‬ال ‪٠‬غ‪ٛ‬ظ اؼزرساَ اٌف‪ٛ‬اطً أ‪ ٚ‬ؼحت ػٕبطط ِٕ‪ٙ‬ب ِجبشطح‪ِ ,‬ضبي‬
‫ػٍ‪ ٝ‬شٌه‪:‬‬
‫ ؼحت ػٕظط ‪ٚ‬احس ِٓ اٌمبئّخ‬‫]‪[X | T‬‬
‫ ؼحت ػٕظط‪ ِٓ ٓ٠‬اٌمبئّخ‬‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 17‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫]‪[X1,X2 | T‬‬
‫ ؼحت صالس ػٕبطط‬‫]‪[X1,X2,X3 |T‬‬
‫‪ٚ‬ف‪ ٟ‬حبٌخ ئٕٔب ال ٔط‪٠‬س اٌ‪ٛ‬ط‪ٛ‬ي ئٌ‪ ٝ‬ػٕبطط اٌمبئّخ فبٔٗ ال ر‪ٛ‬عس حبعخ ئٌ‪ ٝ‬ػٍّ‪١‬خ اٌؽحت ‪ٔٚ‬ىزت‬
‫اؼُ اٌّزغ‪١‬ط اٌص‪٠ ٞ‬حز‪ ٞٛ‬ػٕبطط اٌمبئّخ عّ‪١‬ؼب ثس‪ ْٚ‬أل‪ٛ‬اغ وّب ٌ‪ ٛ‬وبْ ِزغ‪١‬ط ِٓ ٔ‪ٛ‬ع ث‪١‬بٔ‪ٟ‬‬
‫ثؽ‪١‬ؾ غ‪١‬ط ِطوت (…‪.)integer, real,‬‬
‫اَتبه‪:‬‬
‫ ال رىزت ]‪ٌ [X‬زش‪١‬ط ئٌ‪ ٝ‬اٌمبئّخ ‪ X‬ألٔ‪ٙ‬ب رؼٕ‪ ٟ‬لبئّخ رحز‪ ٞٛ‬ػٕظط ‪ٚ‬احس ‪ , X‬ئّٔب اوزت‬‫‪ X‬ثس‪ ْٚ‬األل‪ٛ‬اغ ٌزش‪١‬ط ئٌ‪ ٝ‬اٌمبئّخ ‪ X‬اٌز‪ ٟ‬لس رى‪ ْٛ‬ئحس‪ ٜ‬اٌحبالد اٌزبٌ‪١‬خ‪:‬‬
‫لبئّخ فبضغخ ] [‬
‫لبئّخ رحز‪ ٞٛ‬ػٍ‪ ٝ‬ػٕظط ‪ٚ‬احس]‪[X‬‬
‫لبئّخ رحز‪ ٞٛ‬ػٍ‪ ٝ‬ػٕظط‪ ٓ٠‬اصٕ‪[X,Y] ٓ١‬‬
‫لبئّخ رحز‪ ِٓ n ٞٛ‬اٌؼٕبطط ]‪[X1,X2,…, Xn‬‬
‫ ؼحت اٌؼٕظط ثبٌطط‪٠‬مخ أػالٖ ال ‪٠‬ؼٕ‪ ٟ‬أه رم‪ َٛ‬ثؽحت ل‪ّ١‬خ ِحسزح ِٓ اٌمبئّخ ‪ ,‬ف‪ٟ‬‬‫ثؼغ األح‪١‬بْ ٔؽزرسَ ٘صٖ اٌظ‪١‬غخ إلػبفخ ػٕظط ئٌ‪ ٝ‬اٌمبئّخ‪ ,‬أ‪ ٞ‬ئْ اٌؼالِخ ( | ) رؼٕ‪ٟ‬‬
‫رحس‪٠‬س أ‪ ٚ‬ػعي ِ‪ٛ‬لغ ِٓ اٌمبئّخ ‪ّ٠‬ىٓ لطاءرٗ ئشا وبْ ‪٠‬حز‪ ٞٛ‬ػٍ‪ ٝ‬ػٕظط أ‪ ٚ‬رحس‪٠‬ضٗ ئشا‬
‫وبْ فبضغب‪.‬‬
‫ ػٕسِب رم‪ َٛ‬ثؽحت أ‪ ٚ‬ئػبفخ اٌؼٕظط ِٓ أ‪ ٚ‬ئٌ‪ ٝ‬اٌمبئّخ فبٔه ثصٌه رغزبظ ِ‪ٛ‬لؼٗ ‪ٚ‬رمف‬‫ػٕس اٌّ‪ٛ‬لغ اٌزبٌ‪ٚ ٌٗ ٟ‬ثبٌٕز‪١‬غخ ٌٓ رؽزط‪١‬غ اٌطع‪ٛ‬ع ئٌ‪.ٗ١‬‬
‫‪َ . 3‬تعهى بعض األفكاس‬
‫‪ ‬و‪١‬ف رمطأ اٌمبئّخ‬
‫اٌجطٔبِظ اٌزبٌ‪ ٟ‬ؼ‪١‬ؼًّ ػٍ‪ ٝ‬ؽجبػخ ػٕبطط لبئّخ ‪ ,‬ضرت أفىبضن لجً أْ رجطِظ‪:‬‬
‫‪ -1‬حسز ٔ‪ٛ‬ع اٌج‪١‬بٔبد (ؼٕفزطع ئٔ‪ٙ‬ب أؼّبء ؽالة)‬
‫*‪name=symbol‬‬
‫‪ -2‬حسز اؼُ اٌؼاللخ ‪ٚ‬ػسز ِؼبِالر‪ٙ‬ب‬
‫ؼٕرزبض اؼُ ‪ٌٕ print‬ى‪ ْٛ‬لط‪٠‬ج‪٘ ِٓ ٓ١‬سف اٌجطٔبِظ‬
‫ٌس‪ٕ٠‬ب أؼّبء فمؾ ‪ٌٙٚ‬صا ٔحزبط ِؼبًِ ‪ٚ‬احس ‪ ٛ٘ٚ‬لبئّخ األؼّبء‬
‫)‪print(name‬‬
‫‪ -3‬فىط ث‪ٛ‬ػغ ئؼزطار‪١‬غ‪١‬خ اٌجطٔبِظ‪ ,‬وأْ رم‪ٛ‬ي ٌس ّ‬
‫‪ ٞ‬لبئّخ ‪ٚ‬أض‪٠‬س أْ أؽجغ‬
‫ػٕبطط٘ب ‪٘ٚ‬صا ‪٠‬ؼٕ‪ ٟ‬ئٔ‪ ٟ‬ؼأحزبط ٌمطاءر‪ٙ‬ب عّ‪١‬ؼب اثزساء ِٓ اٌؼٕظط األ‪ٚ‬ي ‪ٚ‬ألٔ‪ ٟ‬ال ‪ّ٠‬ىٓ أْ‬
‫احسز ػسز ػٕبطط اٌمبئّخ فبٔ‪ ٟ‬ثحبعخ ئٌ‪ ٝ‬لطاءر‪ٙ‬ب حز‪ٔ ٝ‬مطخ إٌ‪ٙ‬ب‪٠‬خ ‪ ٟ٘ٚ‬ػٕسِب احظً ػٍ‪ٝ‬‬
‫لبئّخ فبضغخ‪ ,‬ف‪ ٟ‬وً ِطح ػٍ ّ‪ ٟ‬أْ اؼحت ػٕظط ‪ٚ‬احس ‪ٚ‬ػٍّ‪١‬خ اٌؽحت ؼزإز‪ ٞ‬اٌغطع ‪ ِٓٚ‬صُ‬
‫أرؼبًِ ِغ اٌّزجم‪ ِٓ ٟ‬اٌمبئّخ ألػ‪١‬س اؼزرساِٗ حز‪ ٝ‬رٕز‪ ٟٙ‬اٌمبئّخ‪:‬‬
‫)]‪1. print([X | T‬‬
‫لّذ ثؽحت االؼُ األ‪ٚ‬ي ‪ٚٚ‬ػؼزٗ ف‪ ٟ‬اٌّزغ‪١‬ط ‪ٚ X‬ثبل‪ ٟ‬األؼّبء ف‪ ٟ‬اٌّزغ‪١‬ط ‪T‬‬
‫)‪2. print([ X | T):-write(X‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 18‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫‪ٚ‬ا‪ ْ٢‬رّذ ؽجبػخ اٌؼٕظط األ‪ٚ‬ي‪ٌٚ ,‬ىٓ ِب ظٌذ ثحبعخ ئٌ‪ ٝ‬ؽجبػخ ثبل‪ ٟ‬األؼّبء!‬
‫‪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.‬‬
‫‪ ‬و‪١‬ف رٕؽد ثؼغ اٌؼٕبطط ِٓ لبئّخ ئٌ‪ ٝ‬لبئّخ أذط‪ ٜ‬؟‬
‫ً٘ رزصوط ػٕسِب لٍٕب ئْ اٌؽحت ال ‪٠‬ؼٕ‪ ٟ‬ثبٌؼط‪ٚ‬ضح ؼحت ػٕظط ِ‪ٛ‬ع‪ٛ‬ز فؼال ‪ٌٚ‬ىٓ ٘‪ٛ‬‬
‫ِغطز حغع ٌّ‪ٛ‬لغ ف‪ ٟ‬اٌمبئّخ؟‬
‫٘صا اٌّضبي ‪٠‬ؽبػسٔب ف‪ ٟ‬ف‪ ُٙ‬فىطح ٔؽد ثؼغ اٌؼٕبطط‪ ,‬ؼٕم‪ َٛ‬ثىزبثخ ثطٔبِظ ثٕؽد األػساز‬
‫اٌع‪ٚ‬ع‪١‬خ ئٌ‪ ٝ‬لبئّخ أذط‪ٜ‬‬
‫‪ .1‬حسز أٔ‪ٛ‬اع اٌج‪١‬بٔبد ‪ :‬فطز‪ٚ ٞ‬ظ‪ٚ‬ع‪ٟ‬؟ ئشْ أػساز طح‪١‬حخ ‪integer‬‬
‫*‪x=integer‬‬
‫‪ .2‬حسز اؼُ اٌؼاللخ ‪ٚ‬ػسز ِؼبِالر‪ٙ‬ب‪ :‬ؼٕم‪ َٛ‬ثفطظ األػساز اٌع‪ٚ‬ع‪١‬خ‪ ٛ٘ even ..‬اؼُ‬
‫اٌؼاللخ ‪ٌٚ ,‬س‪ٕ٠‬ب لبئّخ أطٍ‪١‬خ ‪ٚ‬لبئّخ ٔؼغ ف‪ٙ١‬ب اٌؼٕبطط اٌع‪ٚ‬ع‪١‬خ ‪ ..‬ئشْ ٌس‪ٕ٠‬ب ِؼبٍِ‪ٓ١‬‬
‫‪ٚ‬والّ٘ب ل‪ٛ‬ائُ ػٕبطط٘ب أػساز طح‪١‬حخ *‪integer‬‬
‫‪even(x,x).‬‬
‫‪ .3‬لُ ث‪ٛ‬ػغ ئؼزطار‪١‬غ‪١‬خ ٌٍجطٔبِظ‪ ,‬رؼٍّٕب و‪١‬ف ٔؽحت ػٕبطط اٌمبئّخ ثبٌزؽٍؽً ِٓ‬
‫اٌجطٔبِظ األ‪ٚ‬ي ‪ٚ ,‬ا‪ ْ٢‬ؼٕفىط ثى‪١‬ف‪١‬خ رطج‪١‬ك شطؽ أْ ‪٠‬ى‪ ْٛ‬اٌؼٕظط ظ‪ٚ‬ع‪ ٟ‬أَ فطز‪( ٞ‬ئشا‬
‫وبْ ‪٠‬مجً اٌمؽّخ ػٍ‪ 2 ٝ‬أ‪ ٚ‬ال) ‪ٚ‬ػٍ‪ ٝ‬أؼبغ ٔز‪١‬غخ اٌشطؽ ٔمطض ئػبفخ اٌؼٕظط ئٌ‪ٝ‬‬
‫اٌمبئّخ اٌغس‪٠‬سح أَ ال‪.‬‬
‫ف‪ ٟ‬اٌرط‪ٛ‬ح األ‪ ٌٝٚ‬اؼحت اٌؼٕظط ِٓ اٌمبئّخ األ‪ . ٌٝٚ‬ئشا وبْ اٌؼٕظط ‪٠‬مجً اٌمؽّخ ػٍ‪ٝ‬‬
‫‪ 2‬احغع ػٕظط عس‪٠‬س ف‪ ٟ‬اٌمبئّخ اٌضبٔ‪١‬خ ‪ٚ‬أؽد اٌؼٕظط ئٌ‪ ,ٗ١‬أِب ئشا وبْ ػىػ شٌه فمُ‬
‫ثؼٍّ‪١‬خ اٌؽحت ػٍ‪ ٝ‬اٌمبئّخ األ‪ ٌٝٚ‬ز‪ ْٚ‬أْ رؽحت ػٕظط ِٓ اٌمبئّخ اٌضبٔ‪١‬خ‪ ..‬و‪١‬ف شٌه؟‬
‫)‪1. even([X|T],Y‬‬
‫‪٠‬ؼٕ‪ ٟ‬ئْ اٌؼٕظط األ‪ٚ‬ي ‪ X‬لس رُ ؼحجٗ ِٓ اٌمبئّخ األ‪, ٌٝٚ‬ث‪ّٕ١‬ب اٌمبئّخ اٌضبٔ‪١‬خ ‪ Y‬ثم‪١‬ذ‬
‫ػٍ‪ ٝ‬حبٌ‪ٙ‬ب ز‪ ْٚ‬رغ‪١١‬ط‪.‬‬
‫)]‪2. even([X|T],[Y1|T2‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 19‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫اٌؼٕظط األ‪ٚ‬ي ‪ X‬الزطغ ِٓ اٌمبئّخ األ‪ٚ ٌٝٚ‬رُ حغع اٌّ‪ٛ‬لغ األ‪ٚ‬ي ِٓ اٌمبئّخ اٌضبٔ‪١‬خ ف‪ٟ‬‬
‫‪.Y1‬‬
‫‪3. even([X|T],[Y1|T2]):-X mod 2=0‬‬
‫اذزجط ‪ , X‬ئشا وبْ ‪٠‬مجً اٌمؽّخ ػٍ‪ 2 ٝ‬لُ ثٕؽرٗ ئٌ‪ Y1 ٝ‬صُ وطض اٌؼٍّ‪١‬خ ػٍ‪ ٝ‬ثم‪١‬خ‬
‫اٌؼٕبطط ثبؼزسػبء ٔفػ اٌؼاللخ ‪ٚ‬ئػطبء٘ب اٌّزجم‪ ِٓ ٟ‬اٌمبئّز‪.ٓ١‬‬
‫‪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).‬‬
‫‪٘ٚ‬صا ‪٠‬ؼٕ‪ ٟ‬أه ؼزّٕغ اٌجطٔبِظ ِٓ اٌص٘بة ئٌ‪ ٝ‬اٌمبػسح اٌضبٌضخ ػٕس رحمك اٌمبػسح اٌضبٔ‪١‬خ ‪ٚ‬ف‪ٙ١‬ب رٕؽد‬
‫اٌؼٕظط األ‪ٚ‬ي ئٌ‪ ٝ‬اٌمبئّخ اٌضبٔ‪١‬خ ِجبشطح ‪ٚ ,‬ف‪ ٟ‬حبٌخ ػسَ رحمك اٌمبػسح اٌضبٔ‪١‬خ ف‪ٙ‬صا ‪٠‬ؼٕ‪ ٟ‬ئْ‬
‫اٌؼٕظط فطز‪ٚ ٞ‬ػٕس٘ب ؼ‪١‬م‪ َٛ‬ثمطؼٗ ف‪ ٟ‬اٌمبػسح اٌضبٌضخ ‪٠ٚ‬زطن اٌمبئّخ اٌضبٔ‪١‬خ ز‪ ْٚ‬رغ‪١١‬ط‪.‬‬
‫‪ ‬و‪١‬ف رزحىُ ثؼسز اٌؼٕبطط اٌّطٍ‪ٛ‬ثخ ٌزٕفص ٘سفه؟‬
‫افزطع أه رط‪٠‬س ئْ رحؽت ِؼسالد ؽالة ‪ٌ ,‬ىً ؽبٌت صالس زضعبد ‪ٌٚ ,‬ىٓ اٌسضعبد‬
‫ٌ‪١‬ؽذ ِ‪ٛ‬ظػخ ثضالس ل‪ٛ‬ائُ ئّٔب ِزؽٍؽٍخ ف‪ ٟ‬لبئّخ ‪ٚ‬احسح ثح‪١‬ش وً ؽبٌت ٌس‪ ٗ٠‬صالس‬
‫زضعبد ِززبثؼخ ف‪ ٟ‬اٌمبئّخ‪:‬‬
‫اٌطبٌت األ‪ٚ‬ي ٌس‪ ٗ٠‬اٌسضعبد األ‪ٚ ٌٝٚ‬اٌضبٔ‪١‬خ ‪ٚ‬اٌضبٌضخ‬
‫اٌطبٌت اٌضبٔ‪ٌ ٟ‬س‪ ٗ٠‬اٌسضعبد اٌطاثؼخ ‪ٚ‬اٌربِؽخ ‪ٚ‬اٌؽبزؼخ‬
‫اٌطبٌت اٌضبٌش ٌس‪ ٗ٠‬اٌسضعبد اٌؽبثؼخ ‪ٚ‬اٌضبِٕخ ‪ٚ‬اٌزبؼؼخ ‪٘ٚ ...‬ىصا‬
‫‪ .1‬حسز أٔ‪ٛ‬اع اٌج‪١‬بٔبد‪ٌ :‬س‪٠‬ه أؼّبء *‪ٚ symbol‬زضعبد *‪integer‬‬
‫*‪x=integer‬‬
‫*‪s=symbol‬‬
‫‪ .2‬حسز اؼُ اٌؼاللخ ‪ٚ‬ػسز ‪ٛٔٚ‬ع ِؼبِالر‪ٙ‬ب‬
‫)‪average ( s,x‬‬
‫‪.3‬ذطؾ ٌٍزٕف‪١‬ص‪ ,‬صالس زضعبد ِزؽٍؽٍخ؟ ئشْ‪:‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 21‬‬
‫‪3rd class – Department of Network‬‬
‫‪College of IT- University of Babylon‬‬
‫‪Artifical Intelligent‬‬
‫]‪ٚ [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‬أْ رٕز‪ ٟٙ‬لبئّخ اٌسضعبد لجً لبئّخ األؼّبء! (ٔمض ف‪ ٟ‬ػسز اٌسضعبد)‬
‫‪ .2‬أْ ‪٠‬زجم‪ ٝ‬زضعخ ‪ٚ‬احسح ‪ ,‬ف‪ ٟ‬ح‪ ٓ١‬أٔذ رط‪٠‬س ‪ 3‬زضعبد!‬
‫‪ .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).‬‬
‫رصوط زائّب‪:‬‬
‫‪Date: Tuesday, March 04, 2014‬‬
‫‪Page 21‬‬
Artifical Intelligent
3rd class – Department of Network
College of IT- University of Babylon
‫خ‬١ٔ‫ب‬١‫اع اٌج‬ٛٔ‫ األ‬ٟ٘ ‫حساد‬ٌٛ‫٘صٖ ا‬ٚ ‫حساد اطغط‬ٚ ِٓ ْٛ‫زى‬٠ ً‫ى‬١٘ ٟ٘ ُ‫ائ‬ٛ‫ئْ اٌم‬
‫ ػٕظط رمزطؼٗ ِٓ اٌمبئّخ‬ٞ‫ّىٕه أْ رؽزرسَ أ‬٠ ‫ ٘صا األؼبغ‬ٍٝ‫ػ‬ٚ ,‫ زضؼٕب٘ب ؼبثمب‬ٟ‫خ اٌز‬١‫األؼبؼ‬
.ٗ‫زٕبؼت ِغ ذظبئظ‬٠ ْ‫ّىٓ أ‬٠ ْ‫ ِىب‬ٞ‫ أ‬ٟ‫ف‬
‫ع‬ٛٔ ِٓ ‫ب‬ٍِٙ‫ ِؼب‬ٜ‫ ػاللخ أذط‬ٟ‫ّىٕه أْ رسذٍٗ ف‬٠ 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.
Page 22
Date: Tuesday, March 04, 2014
Download