برولوغ - العملي

advertisement
Instructor :Bayan Ghozlan
‫ بيان غزالن‬: ‫االستاذه‬
‫‪ ‬البرولوغ ‪ Prolog‬هي لغة برمجة منطقية‪ .‬وتعني‬
‫برمجه المنطق ‪programing language‬‬
‫‪ ‬تم اختراع اللغة بواسطة ألين كولميرايور حوالي‬
‫العام ‪ .1972‬كانت محاولة لجعل لغة البرمجة قادرة‬
‫على استخدام عبارات منطقية بدل أن تكون‬
‫تعليمات محددة تلقن إلى الحاسوب‪.‬‬
‫ً‬
‫أساسا لتستخدم في عمل برامج‬
‫‪ ‬صممت اللغة‬
‫معالجة اللغات الطبيعية‪.‬‬
‫‪ ‬تستخدم لغة البرولوغ في العديد من برامج الذكاء‬
‫االصطناعي وبرامج معالجة اللغات الطبيعية‪.‬‬
‫‪ ‬عبارات وقواعد اللغة تعتبر بسيطة جداً وواضحة‬
‫(يتم كتابة البرنامج بالكامل باستخدام الحقائق‬
‫والقواعد)‪.‬‬
‫‪‬‬
‫‪ ‬لغة البرولوغ تسمى لغة تعريفيه واجرائيه ( تعريفيه‬
‫بمعنى انها تعرف وتحدد ما يجب ان تكون عليه‬
‫المخرجات واجرائيه بمعنى انها تعرف كيف يتم الحصول‬
‫على المخرجات )‬
‫مكونات لغة البرولوغ ‪-:‬‬
‫‪ declaring - 1‬التصريح ببعض الحقائق عن‬
‫المواضيع(العوامل ) والعالقات بينها ‪.‬‬
‫‪defining - 2‬تعريف القواعد والشروط بين المواضيع‬
‫والعالقات بينها ‪.‬‬
‫‪asking -:3‬اسئله عن هذه المواضيع وعن العالقات‬
‫بينها (االستعالمات)‬
‫‪.‬‬
‫‪ ‬مكونات الجمل كما اشرنا لها سابقا ‪-:‬‬
‫‪ Fact ‬الحقائق والمعارف ‪.‬‬
‫‪ Rules ‬العالقات والقواعد بينها وتشبه عمل ال‬
‫‪Function‬في لغات البرمجه االخرى مثل الجافا‬
‫والسي ‪ ++‬وتحتوي على ‪If/then .‬‬
‫‪ Queries ‬االسئله واالستعالمات في المترجم‬
‫والتي يتم العبور بها الى الحقائق والقواعد‬
‫‪ ‬بكل بساطه لغة البرولوغ تجعل جهاز الحاسوب‬
‫يستخدم كمستودع للحقائق والعالقات بينها ‪ ,‬وتوفر‬
‫سبل للوصول لالستنتاجات ‪.‬‬
There are only three basic constructs in Prolog:
- facts.
- rules.
- queries.
A collection of facts and rules is called a
knowledge base (or a database)
Prolog programming
is all about writing knowledge bases.
‫‪ ‬الحقائق في لغة البرولوغ تعبر عن العالقات بين‬
‫المواضيع( العوامل) من خالل المعطى الذي يمثل‬
‫المعرفه ‪.‬‬
‫‪Live(tiger,forest).‬‬
‫وهنا في الجمله السابقه ‪Live‬تمثل العالقه بين‬
‫‪tiger‬و ‪ forest ...‬اي ان النمر يعيش في الغابه‪.‬‬
‫‪ ‬والجمله كامله بكل محتوياها تسمى ‪clause‬‬
‫يطلق على المواضيع ( العوامل ) بــــ ‪arguments‬‬
‫ويطلق على العالقات بينها بـــــ ‪predicate‬‬
-: ‫ تحتوي على جزئيين هما‬
body of clause >---------- ‫جزء الشرط‬
head of clause >---------- ‫جزء الهدف‬
‫ وتعني‬
(head) is true/if the )body) true 
Read
as If
‫االستعالم او االسئله حيث تكبت في المترجم‬
‫بتتبع الهدف من خالل ) ‪engine‬فيقوم المحرك (‬
‫الحقائق والقواعد الموجوده في قاعدة المعرفه (‬
‫مالحظه مجموعة الحقائق والقواعد في البرولوغ‬
‫تسمى قاعدة المعارف ) ‪.‬‬
‫‪‬‬
‫‪ ‬االستعالم او االسئله حيث تكتب في المترجم‬
‫فيقوم المحرك ( ) ‪engine‬بتتبع الهدف من خالل‬
‫الحقائق والقواعد الموجوده في قاعدة المعرفه (‬
‫‪ ‬مالحظه ‪:‬مجموعة الحقائق والقواعد في البرولوغ‬
‫تسمى قاعدة المعارف ) ‪.‬‬
‫‪ ‬عندما يتم طرح سؤال في البرولوغ ‪ ,‬يصار الى‬
‫البحث في قاعدة البيانات والتي اشرنا انها تمثل‬
‫الحقائق والعالقات بنها ( القواعد)‬
‫‪ ‬ومن ثم يبحث عن الحقائق التي تطابق االستعالم‬
‫في السؤال ‪ ,‬لذا فاذا وجد هذه الحقائق التي‬
‫تطابق فيكون الجواب بـــ " " ‪Yes‬والعكس يكون‬
‫"‪No " .‬‬
‫‪ ‬أمر مهم جدا ‪ -:‬اسماء المواضيع ‪predicate‬‬
‫والعالقات بينها يجب ان تبدأ بأحرف صغيره (‬
‫‪lowercase letter ).‬‬
‫‪ ‬الحقائق والجمل وحتى االستعالمات يجب ان‬
‫تنتهي بــــ " ‪" .‬‬
‫‪ ‬مثال‬
‫‪- father(khaled, mohammad).‬‬
‫‪- woman(sara).‬‬
‫‪- ?-woman(sara).‬‬
‫‪yes‬‬
‫اين نكتب برنامج البرولوغ‬
‫؟‪‬‬
‫الحقائق والقواعد تخزن في ملفات تمثل ما يسمى‬
‫قاعدة المعارف ‪ ,‬ثم عندما نريد العمل عليها نقوم‬
‫بتحميل هذه القاعدة من المعارف الى المترجم ‪,‬‬
‫وعندما نغير اي معلومه في هذه الملفات فيصار‬
‫الى اعادة تحميلها مره اخرى الى المترجم ( لكي‬
‫يتم تفعيلها مره اخرى ) ‪.‬‬
‫هي امتداد ملفات برولوغ ‪or .pl‬‬
‫‪ .pro‬‬
‫مثلي الحقائق التاليه بشكل قاعده معرفه بلغه‬
‫برولوغ‬
‫ثم استعلمي اذا كان رامي هو اب لسامي ؟ وا‪1‬ذا‪1‬‬
‫كان سامي هو اخ لخالد؟ ماهي يالنتيجه‬
‫‪‬‬
‫‪‬‬
‫‪ father(sami,khaled).‬‬
‫‪ mother(yara,kaled).‬‬
‫‪ father(rami,sami).‬‬
‫‪ ‬ثم استعلمي اذا كان رامي هو اب لسامي ؟ وا‪1‬ذا‪1‬‬
‫كان سامي هو اخ لخالد؟ ماهي يالنتيجه‬
‫مع ان )‪ relax(alaa‬لم تظهر‬
‫كحقيقه اال ان اللغه استطاعت‬
‫استقرائها من القواعد الموجوده‬
‫لتستنتج انها صحيحه لتعطينا ‪yes‬‬
‫بعد اضافه البرنامج وتنزيله الى جهازك طبقي‬
‫تمرين‪ 1‬والحظي النتيجه بنفسك بعد االستعالم‬
‫كما ذكرنا سابقا‬
‫‪‬‬
‫تمرين ‪3‬‬
‫التعرف عبعض‬
‫االخطاء‬


OR ; means conjunction
AND ,means disjunction
 IF
amal is clever then she is in work and
drink coffee
 If Manal is happy then she is listening to
quran or read quran
 Examples
clever(amal):- inwork(amal), drinkcof(amal).
And
happy(manal):- listen2quran(manal).
happy(manal):-readquran(manal)
happy(manal):- listen2quran(manal); readquran(manal).
‫اشكال‬
‫ال‬
OR
 talk(sana).
 eat(ehab).
 sleep(tamara).
 read(tamara):-
eat(sana).
 read(tamara):- sleep(tamara).
 happy(ehab):-sleep(tamara),eat(ehab).
 ?-read(tamara).
 ?-happy(ehab).
‫‪ Two‬‬
‫‪terms match, if they are equal or if they‬‬
‫‪contain variables that can be instantiated in‬‬
‫‪such a way that the resulting terms are‬‬
‫‪equal.‬‬
‫تتوافق جملتين اذا كانتا متطابقتين تماما‬
‫او كانت احداهما تحتوي متغيرا لو عوضنا بقيمته من‬
‫الجمله الثانيه لتطابقتا عندها يحدث الربط بعد عمليه‬
‫التوافق هذه‬
woman(mai).
woman(jody).
woman(yomy).
loves(lama,mai).
loves(rand,mai).
loves(bee,honey_bunny).
loves(honey_bunny,bee).
NOW QUERY
?- woman(X).
‫‪ ‬في برولوغ اي اسم يبدا بحرف كبير ‪Upper case‬‬
‫يعتبر متغيرا‬
‫‪ ‬عند االستعالم عن القاعده السابقه ب )‪woman(X‬‬
‫‪ ‬فان برولوغ تطابق ‪ X‬مع الحقائق الموجوده فتعطي‬
‫النتيجه ‪X=mai‬‬
‫‪)matching & binding( ‬‬
‫‪ ‬واذا اعطيته ;الفاصله المنقوطه يفهمها «او» اي‬
‫ماذا بعد؟ فيعطي‬
‫‪ X=jody ‬وهكذا ثالثا‬
‫‪ ‬في المره الرابعه يعطي ‪no‬‬
woman(mai).
woman(jody).
woman(yomy).
friend(lama,mai).
friend(rand,mai).
friend(bee,honey_bunny).
friend(honey_bunny,bee).
?- friend (rand,X),woman(X).
 Answer
is X=mai
‫في المثال التالي نحظ ان القاعده تحتوي اربع‬
‫متغيرات‬3 ‫حقائق و قاعده تحتوي‬
friend(hana,sara).
friend(ruba,sara).
friend(eyad,rami).
friend(rami,eyad).
know(X,Y) :- friend(X,Z),friend(Y,Z).

‫بالتالي ‪‬‬
‫عند االستعالم‬
‫‪?- know(ruba,A).‬‬
‫من هو الشخص الذي تعرفه ربا ‪..‬‬
‫ما هي النتيجه ?‬
‫‪ ‬عند تطبيق االستعالم التالي (من الشخص الذي تعرفه‬
‫ربا) ?‪know(ruba,A) -‬‬
‫فان البرنامج يخضع هذا االستعالم للقاعده فيجد ان‬
‫)‪ know(ruba,A‬تتم اذا كان‬
‫)‪ friend(ruba,Z‬وبنفس الوقت )‪friend(Y,Z‬‬
‫عند مطابقتهما بالحقائق فانقيمه ‪Z=sara‬‬
‫عندها تصبح )‪ friend(Y,sara‬فنعود نطابقها مع الحقائق‬
‫وتكون نتيجه ‪ Y‬النهائيه بناء على الحقائق ‪hana‬و ‪ruba‬‬
‫‪ ‬حيث ان )‪ know(ruba,A‬تطابق القاعده بالبرنامج‬
‫فتصبح قيمه ‪ A‬هي نفس قيمه ‪Y‬‬
‫‪ ‬مم تتكون الحقائق والقواعد واالستعالمات فعليا في‬
‫برولوغ؟‬
‫‪ ‬هناك مصطلحات ‪ terms‬ويوجد منها اربع انواع‬
‫ببرولوغ‬
‫‪atoms, numbers, variables, and complex ‬‬
‫‪terms (o‬ء‪r structures).‬‬
An atom is either:
1.
A string of characters made up of
2.
(upper-case letters, lower-case letters, digits, and the
underscore character) that begins with a lower-case
letter.
For example: butch, big_kahuna_burGer, and m_mono2
listen2quran
2. A sequence of characters enclosed in single quotes,
(called the atom name)
For example ’Vincent’, ’The Gimp’, ’Five_Dollar_Shake’,
’&^%&#@$ &*’, and ’ ’. Note that we are allowed to use
spaces in such atoms — in fact, a common reason for using
single quotes is so we can do precisely that.
3. A string of special characters.
For example: @= and ====> and ; and :are all atoms. As we have seen, some of these atoms, such
as ; and :- have a pre-defined meaning
‫االعداد الحقيقيه ليست باالستخدام المهم في برولوغ‬
‫في هذه المرحله من تعلم اللغه لذلك سنتطرق لالعداد‬
‫الصحيحه االن فقط‬
-
integers (that is: ... -2, -1, 0, 1, 2, 3, ...)
are useful, and we’ll discuss how to
manipulate them in a later lecture.
-Their Prolog syntax is normal and obvious : 23,
1001, 0, -365, and so on.
A variable is a:
 string of upper-case letters, lower-case
letters, digits and underscore and starts
either with an upper-case letter or with
underscore.
For example X, Y, Variable, _tag, X_526, and
List, List24, _head, Tail, _input and Output

The variable _ (that is, a single underscore character) is
rather special. we will discuss it in a later lecture
Which of the following sequences of characters
are atoms, which are variables, and which are
neither?
1. vINCENT3
2. Footmassage
3. variable23
4. Variable2000
5. big_kahuna_burger
6. ’big kahuna burger’
7. _sara
8.===>
9.’#$%’
10.’_Jules’
1. vINCENT3
atom
2. Footmassage
variable
3. variable23
atom
4. Variable2000
variable
5. big_kahuna_burger
atom
6. ’big kahuna burger’
atom
7. _sara
variable
8.===>
atom
9.’#$%’
Atom
‘_jules’
Atom
How many facts, rules, clauses, and predicates(
‫ )االفعال دون تكرار‬are there in the following
knowledge base? What are the heads of the
rules, and what are the goals they contain?
 woman(vincent).
 woman(mia).
 man(jules).
 person(X) :- man(X); woman(X).
 loves(X,Y) :- knows(Y,X).
 father(Y,Z) :- man(Y), son(Z,Y).
 father(Y,Z) :- man(Y), daughter(Z,Y).

 Facts
are:..
 Rules are:..
 Remember: The facts and rules contained in
a knowledge base are called clauses
 7 clauses.
 7 predicates: Woman, man,person,loves,
knows,father, son,daughter.
 Heads are:
 Goals are:
Suppose we are working with the following knowledge base:
play(ron).
hasWand(harry).
quidditchPlayer(harry).
play(X) :- hasBroom(X),hasWand(X).
hasBroom(X) :- quidditchPlayer(X).
How does Prolog respond to the following queries?
1. play(ron).
2. eat(ron).
3. play(john).
4. eat(john).
5. play(harry).
6. play(Y).
7. eat(Y).
 Represent
the following in Prolog:
1. Bam is a killer.
2. Mai and Manal are worker .
3. Sama is dead.
4. Nada help everyone who gives Mai a book.
5. Mia loves everyone who is a good dancer.
6. Jules eats anything that is nutritious or
tasty.
killer(bam).
worker(mai),worker(manal).
Dead(sama).
help(nada,X):- gives_book(X,mai).
Another solution:
help(nada,X):- gives (X,book,mai).
loves(mai,Y):- good_dancer(Y).
eat(jol,A):- nutritious(A);tasty(A).
 Now
we need to know how to fit constants,
numbers, and variables together to make
complex terms.
 Complex terms are build out of a functor
followed by a sequence of arguments.
 Functor(term1, …,term n).
 The functor must be an atom. But not variable.
 Examples: eat(sana). draw(rami,picture).
work(sami,X).
 Work is the functor & sami and X is the
arguments.
 The
number of arguments that a complex
term has is called its Arity.
 For instance, woman(mai) is a complex term
with arity 1, while work(veky, hotel) is a
complex term with arity 2.
 Arity is important to Prolog. Since it help
Prolog to differentiate between clauses,
that have the same functor.
 Work(ali,sami,rami).
 Work(ali,sami).
 they are different in Prolog
E.g : hide(X,father(father(father(butch))))
 is a complex term.
 Its functor is hide, and it has two arguments:
the variable X, and the complex term
father(father(father(butch))).
 This complex term has father as its functor,
and the argument is a complex term,
namely father(father(butch)).
 And the argument of this complex term,
namely father(butch), is also complex. But
then the nesting “bottoms out”, for the
argument here is the constant butch.
‫المثال السابق مثال على التركيبه المتداخله‬
‫المسماه‬
‫‪ Nested structure‬‬
‫‪ Or recursive structure.‬‬
‫‪‬‬
‫تحدثنا في سابقا عن فكره المطابقه التي تقوم بها‬
‫لغه برولوغ خالل تنفيذ البرنامج‬
‫و )‪ woman(X‬الحظنا كيف ان اللغه طابقت بين‬
‫‪ X‬للمتغير ‪ mai‬وقامت باسناد )‪woman(mai‬بين‬
‫التي ناقشناها سويا في ‪terms‬تذكري معي ال‬
‫الشريحه التاليه‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
Recall that there are three types of term:
1. Constants. ‫ثابت‬These can either be atoms
(such as vincent) or numbers (such as 24)
2. Variables.‫متغير‬
3. Complex terms. ‫ مقطع معقد‬These have the
form: functor(term_1,...,term_n).
If term1 and term2 are constants, then term1 and term2 match
if and only if they are the same atom, or the same number.
atom‫يتوافق مقطعين ان كانا نفس الرقم او نفس ال‬
1.
That means that:
 the terms mai and mai match, because they are the same atom.
 The terms 42 and 42 match, because they are the same number,
 The terms X and X match, because they are the same variable
 The terms woman(mai) and woman(mai) match, because they are
the same complex term.
 The terms woman(mai) and woman(veky), however, do not
match, as they are not the same (and neither of them contains a
variable that could be instantiated to make them the same).
 Woman(mai) and woman(Samer).
2. If term1 is a variable and term2 is any type
of term, then term1 and term2 match, and
term1 is instantiated to term2. & vice versa
(So if they are both variables, they’re both
instantiated to each other, and we say that
they share values.)‫ا‬
‫اذا كان احد المقطعين متغير واالخر اي مقطع اخر‬
‫فانهما يتوافقان وياخذ المتغير قيمه المقطع‬
e.g: the terms eat(X) and eat(cake) match,
because they can be made equal by
instantiating X to cake.
3. If term1 and term2 are complex terms, then
they match if and only if:
(a) They have the same functor and arity.
(b) All their corresponding arguments match
(c) and the variable instantiations are
compatible. (I.e. it is not possible to
instantiate variable X to sami, when matching
one pair of arguments, and to then instantiate
X to rami, when matching another pair of
arguments.) ‫انظر المثال في الشريحه التاليه لتوضيح هذه‬
‫النقطه‬
‫‪ ‬مثال‬
‫‪work(rami,X) with work(X,sami) ‬‬
‫‪ ‬اليوجد قيمه لو اسندناها ل ‪ X‬هنا ستجعل الحقيقتين‬
‫متساويتين‬
‫‪ ‬حيث لو اسندنا ل ‪( X‬عند محاولتنا لمطابقتهما) القيمه‬
‫‪ rami‬عندها ستصبحان‬
‫‪ Work(rami,rami) and work(rami, sami) ‬وهما كما‬
‫مالحظ غير متساويتين‬
‫‪ ‬واالمر شبيه في حال اسندنا ل ‪ X‬القيمه ‪sami‬‬
‫‪ ‬مثال اخر‬
‫‪loves(X,X) = loves(mai,cakes).-- No ‬‬
‫‪ ‬حسب ‪ c‬من التعريف السابق المتغيرات غير متوافقه‬
‫رغم ان ال ‪ functor‬وال ‪ arguments‬متوافقين‬
 4.
Finally; two terms match if and only if it
follows from the previous three clauses that
they match.
‫في االمثله التاليه حددي الى اي قانون من قوانين‬
‫التطابق السابقه يتبع كل مثال مع التوضيح‬
=(mia,mia).
=(mia,vincent).

.2 = 2

yes

mia = vincent.

No

’mia’ = mia.

Yes

mia = X.

X = mia

Yes

X = Y.

X = Y.

Yes

X = _5071

Y = _5071

kill(shoot(gun),Y) = kill(X,stab(knife)).
 kill(shoot(gun),Y)
X
= shoot(gun)
 Y = stab(knife)
 yes
= kill(X,stab(knife)).
‫تمرين ‪:‬‬
‫اي االجزاء‬
‫التاليه‬
‫يتطابق‬
‫وايها ال‬
‫حل الجزء االول من‬
‫التمرين‬

Which of the following pairs of terms match? Where relevant, give the
variable instantiations that lead to successful matching.

1. bread = breadyes
2. ’Bread’ = bread no
3. ’bread’ = breadyes
4. Bread = bread
5. bread = sausage
6. food(bread) = bread
7. food(bread) = X
8. food(X) = food(bread)
9. food(bread,X) = food(Y,sausage)
10. food(bread,X,beer) = food(Y,sausage,X)
11. food(bread,X,beer) = food(Y,kahuna_burger)
12. food(X) = X
13. meal(food(bread),drink(beer)) = meal(X,Y)
14. meal(food(bread),X) = meal(X,drink(beer))













 We
are working with the following
knowledge base:
 house_elf(dobby).
 witch(hermione).
 witch(’McGonagall’).
 witch(rita_skeeter).
 magic(X):-house_elf(X).
 magic(X):-wizard(X).
 magic(X):-witch(X).
 Which
of the following queries are satisfied?
 1. ?- magic(house_elf).
 2. ?- wizard(harry).
 3. ?- magic(wizard).
 4. ?- magic(’McGonagall’).
 5. ?- magic(Hermione).
LISTS IN PROLOG


What is a list in Prolog
Lists are powerful data structures for holding and
manipulating groups of things. In Prolog, a list is
simply a collection of terms.
‫القوائم احد تراكيب البيانات المستخدمه ببرولوغ لتخزين‬
.‫ومعالجه مجموعه من البيانات او العناصر‬
The terms can be any Prolog data types, including
structures and other lists.
‫عناصر المصفوفه ممكن ان تكون اي نوع من انواع البياانات‬
.‫بمافيها تراكيب بيانات او بيانات عاديه‬
 Syntactically, a list is denoted by square brackets
with the terms separated by commas.
‫يشار للمصفوفه او القائمه برمز االقواس المربعه مع الفواصل‬
‫التي تفصل بين العناصر‬






e.g: [sara, haya, ruba, lama].
The previous example is a list of four elements
 Here
are some examples of lists in Prolog:
first_list([apple,orange,lemon], supermarket).
 second_list ([dish,fridge], kitchen).
 third_list ([shoes,bag], under_the_bed).
 fourth([air], everywhere).

** All the above represents container and things
inside(instead of define them individually lists make defining
them easier)
 The
empty list is represented by a set of
empty brackets [].
 This is equivalent to the nil in other
programming language.
 For our example in this section, it can
describe the lack of things in a place :
list_where([], cave).
 Means nothing exists in the cave

The Unification works on lists just as it works on
other data structure of Prolog. With that, we now can
ask questions about lists to prolog:
: ‫ البرنامج‬
first_list([apple,orange,lemon], supermarket).
 second_list ([dish,fridge], kitchen).
 third_list ([shoes,bag], under_the_bed).
 fourth([air], everywhere).

‫االستعالمات عليه‬
 ?- [_,_,X] = [lesson, work, sleeping].
X = sleeping
 ?- third_list(X, under_the_bed).
X = [shoes , bag],

 [mia,
robber(honey_bunny), X, 2, mia].
 Notice:
all sorts of Prolog objects can be
elements of a list.

The first element of this list is mia, an
atom; the second element is
robber(honey_bunny), a complex term; the
third element is X, a variable; the fourth
element is 2, a number.
 Also
redundancy allowed.

[mia, [vincent, jules], [butch, friend(butch)]].
lists can contain other lists as elements.
 lists can be made out of lists.
 Now ,what is the length of this list?

 [[],
dead(zed), [2, [b, chopper]], [], Z, [2,
[b, chopper]]].
 The last example mixes all previous ideas
together.
 We have here a list which contains the empty
list (contains it twice), the complex term
dead(zed), two copies of the list [2, [b,
chopper]], and the variable Z.
 Note that the third (and the last) elements
are lists which themselves contain lists
(namely [b, chopper]).
 Any
non-empty list can be thought of as
consisting of two parts: the head and the
tail.
 The head is simply the first item in the list;
the tail is everything else
 i.e. the tail of a list is always a list again.
 For example, the head of
[mia, vincent, jules, yolanda]
is mia and the tail is [vincent, jules, yolanda].
 Ex2,
the head of
 [[], dead(zed), [2, [b, chopper]], [], Z, [2,
[b, chopper]]]
 is [], and the tail is [dead(zed),
[2,[b,chopper]],[],Z,[2,[b, chopper]]].
 Ex3:
[dead(zed)]
The head is the first element of the list, which is
dead(zed), and the tail is the empty list [].
 Note: Empty list has no head and tail

 Prolog
has a special inbuilt operator | which
can be used to decompose a list into its
head and tail.
 The most obvious use of | is to extract
information from lists. We do this by using |
together with matching.
 E.g:
?- [H| T] = [madinah, majmaa, jaddah,qaseem].
H = madinah
T = [majmaa, jaddah,qaseem].
Yes.
 As
we mentioned, only non-empty lists have
heads and tails.
 If we try to use | to pull [] apart, Prolog will
fail:
 E.g:
?- [X|Y] = [].
no.
That is, Prolog treats [] as a special list
:‫ مانتيجه االستعالم التالي‬

?- [X|Y] = [[], dead(zed), [2, [b, chopper]], [], Z].
X = '[]'
Y = [dead(zed), [2, [b, chopper]], '[]', H155]
Z = H155
 Note:
Prolog has bound Z to the internal variable
H155.)
 Suppose
we want to know what the first two
elements of the list were, and also the
remainder of the list after the second
element.
‫لو اردنا استخالص اول عنصرين من المصفوفه اضافه‬
‫ اكتبي‬.‫لمعرفه باقي العناصر بعد العنصر الثاني‬
‫االستعالم الذي يقوم بذلك‬
-Type a query that do this job!
?- [X,Y | W] = [[], dead(zed), [2, [b, chopper]],
[], Z].
X = '[]'
Y = dead(zed)
W = [[2, [b, chopper]], '[]', H158]
Z = H158






|
‫ تستخدم هذه االشاره لتقسيم القائمه في اي موقع وليس فقط‬
‫الى راس وتذييل‬
can not only be used to split a list into its head and its tail,
but we can in fact use it to split a list at any point.
Suppose we were interested in getting hold of the second
and fourth elements of the list
:‫ الستخالص العنصر الثاني والرابع من المصفوفه‬
[[], dead(zed), [2, [b, chopper]], [], Z].
We just have to enumerate how many elements we want to take away
from the beginning of the list, and right of the | we will then get what
remains of the list
The solution is .. Next slide
?- [X1,X2,X3,X4 | Tail] = [[], dead(zed), [2, [b,
chopper]], [], Z].
 X1
= '[]'
 X2 = dead(zed)
 X3 = [2, [b, chopper]]
 X4 = '[]'
 Tail = [H164]
 Z = H164
 yes
‫ هناك طريقه ثانيه للحصول على عناصر نحددها‬
: ‫نحن مسبقا من المصفوفه‬
 We use it when we need to use a variable,
but we’re not interested in what Prolog
instantiates it to
 It’s
simply a way of telling Prolog to bind
something to a given position, completely
independently of any other bindings.
‫غير معنيين به وال يهمنا ماهو ولكل شرطه قيمه‬
‫تختلف عن االخرى‬
 we
can pose the following query instead:
 ?- [_,X,_,Y|_] = [[], dead(zed), [2, [b,
chopper]], [], Z].
X
= dead(zed)
 Y = '[]'
 Z = H164
 yes
Download