Upcoming SlideShare
Loading in...5




introduction to prolog ,,, this notes of course in prolog

introduction to prolog ,,, this notes of course in prolog



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Swi_prolog Swi_prolog Document Transcript

  • Muhammad Siddig H. @ @Prolog @òÌÜÛ@݁†ß @ @Zò߆Խa ‫ א‬ġ× ‫ א‬ª Programing in ‫א‬ Alan ę   W PROLOG ‫א ×و ج‬ Ę‫ذ‬ ‫א‬  ‫א‬ Ú‫و‬ ‫م‬ ‫ و‬ª ‫ن‬ K ‫هذ א‬ ‫مא‬ ‫א م‬ ‫ و‬KLogic .  Colmeraur ‫ ن‬ħ ª  ‫א‬ Ġ‫ א‬Ğ‫ א‬ª ‫א‬ ª ġ @ @ZProlog@òÌÛ@paŒî¾ W K ‫و‬ K‫אج‬ ‫א‬ª ‫א‬ ‫א‬ª ‫א ×و ج‬  ‫ א د ل وא‬ª‫א‬ ‫و‬ ‫א‬ ‫وא‬ ‫א א‬ħ L ‫ل א‬ Ksimplisiy in coding ġ× ‫א‬ ‫ وذ‬ESource CodeF ‫אم‬ × ‫ وذ‬Depugging K ‫א‬ K ‫א‬ Ġ‫א‬ ×‫א‬ ‫א‬ ‫ود‬ ‫ و‬ ‫و‬ ğ‫ א‬ª Kª ‫א‬ ‫لא‬ L ‫א‬ ĩL ‫ وא‬Ø ‫א‬  ‫ وא‬KTracing Facility ‫م‬ ‫م‬ ‫א‬ ‫א ×א‬ ª ‫ وא‬ª ‫א‬ ‫אد א‬ L ‫  ز  א‬ª‫א‬ ğ‫وא‬ L × ‫ א‬ġ× ‫ א‬ª ‫و‬ ‫א‬ ĩL ‫א‬ ‫ و‬Copilation ħ ‫د‬ ‫א‬ ‫א‬ ‫א‬ K ĩً ×  ‫ وא‬bit-wise ‫ودوאل א‬ L ª ‫وא‬ ‫א‬ ğ‫وאل א‬ @ @ZProlog@òÌÛ@¿@Statements@Ýà¦a@pbãìØß @ @@ZÕöbÔy@Facts@OQ ğ‫ وא‬Prolog ª ‫و א‬ ‫א‬  ‫א‬ ‫א‬ ġ‫ و‬Head ‫س‬ ‫ و‬ú ‫وא‬ ‫ن‬ ğ‫ א‬ĩ‫و‬ predicate KEArgumentsF Head (Arguments) E ‫وא‬ W 1 Predicate F W ‫א‬ prolog  Clauses father (mohamed,ali). ğ‫ً א‬ ĩ ī  ‫وא‬
  • Muhammad Siddig H. @ @@Z†ÇaìÓ@Rules@OR K ğ‫ل‬ ‫ج وא‬ ‫א‬ W ĩ ‫א ل א‬ ‫א‬ ‫ن‬ ‫ذא‬ ali likes ahmed noha likes sami aser likes noha W‫ن‬ ‫و‬ yaser likes everything noha likes ‫ن‬ ‫و د‬ ‫ل‬ yaser likes sami Wprolog  ĩ likes (ali,ahmed). likes (noha,sami). likes (aser,noha). likes(yaser,X) :- likes(noha,X). ú ‫ن א‬ ‫ذא‬ likes(yaser,sami). KYes ‫ و‬True ‫ن‬ ‫ و‬K‫ن‬ ‫و ن وא‬  ª‫ א‬Ğ‫ن א‬ ‫و وא‬ ‫ אذא‬W ً Ğ‫א‬ father (ahmed,ali). father(salman,ahmed). grandfather(X,Y):-father(X,Z),father(Z,Y). ‫ل‬ ‫א‬ ğ‫ א‬ª ‫نא‬ ‫ج‬ KĦ @ @@ZòÜ÷c@Queries@OS K ‫א‬ ‫א‬ ‫وא‬ ‫وא‬ ğ‫א‬ ‫م‬ ğ ‫א‬ ‫א‬ ě Prolog ğ‫א‬ Ø ħ ‫ن‬ ‫א‬  Kª  ‫ وא‬E ‫ن‬ F‫ن‬ ‫و‬ ‫ن‬ ‫א ل א‬ W grandfather(salman,ali). 2 ‫א‬ ‫و‬ ‫وא‬
  • Muhammad Siddig H. KYes ğ‫א‬ ‫א‬ @ @Data Typer in Prolog@pbãbîjÛa@Êaìãc Ĝ ġ× ‫ א‬ª  simple W‫ن‬ ª ‫ن‬ ī ‫אع‬ Ĝ prolog K lixical elements ‫א‬Ø ‫ و א‬KComplex structure  ‫ و‬structure ZConstants@oiaìrÛa@OQ ‫م و‬ ‫ و א‬ú‫و‬ ğ‫א‬ ‫• א‬ ‫ و‬WAtoms ª‫א‬ a..z,A..Z,!@#$%^&&*()_+‫ز‬ KReal ‫و‬ integer WNumber ‫م‬ ‫א‬ ‫• א‬ @ @ZVariables@paÌn½a@OR Upper-case letter Ù ‫ א‬ú ‫د‬ ?_? ‫א‬ ‫אم א‬ ‫אم א‬ ‫ج‬ ‫א‬  ª‫א‬Ù ‫א‬ ‫א‬ ‫ و‬EX,Y, M , Who, WhereF KAnonymous variable ‫ ل‬Ó‫ א‬Ù ‫א‬ Ù @ @prolog@òÌÛ@ÝîÌ’m Start All programs SWI-Prolog Prolog ‫א‬ ‫א‬ Prolog  ‫ن‬ WProlog  ª ‫אن و‬ ú‫א‬ ‫א‬ ‫א‬ Ò‫א‬ ‫ و‬ª‫א‬ Ĝ ‫א‬ ‫و‬ Kprolog ª ‫א‬ ‫وא‬ ‫ و‬Knowldege-Base ‫ א‬ª‫ א‬Ġ‫א‬ 3  ‫א‬ ‫א‬ Ø ‫وא‬ ğ‫א‬ ‫و‬ ‫و‬ ‫א‬
  • Muhammad Siddig H. ‫م‬ File Ò‫ن א‬  ‫א‬ New… ‫א‬ ‫ وא‬File ‫א‬L ‫ و‬ECreate New Prolog SourceF Ò‫م א‬ ğ‫ز א‬ Save ‫م‬ Name ‫א‬ ‫א‬ Ġ‫ א‬ ğ‫א‬ ‫א‬ ‫א‬ ‫و‬ ‫א‬ª K ‫א‬  ‫א‬ Save Buffer ‫א‬ ğ‫א‬ ‫و‬ File ‫א‬ L ª‫א‬ ‫ع‬ K ‫وذ‬ ‫א‬ ‫ و א‬Compile Buffer ‫ א‬ ğ‫ و‬L ‫م‬ĩ  Compile 4 L ‫א‬ ‫وא‬ ‫م‬ K  ‫ א‬ª‫א‬ ‫א‬ ‫א‬ ‫א‬ ‫א‬ KMake ‫א‬
  • Muhammad Siddig H. ZSearch Mechanism @szjÛa@pbîvîmaa @ @Z´Žîö‰@´àŽÓ@µg@Prolog@¿@szjÛa@pbîvîmaa@áŽÔäm @bß@ìçë@…ìäjÛaë@pajÈnÛa@´ië@bç‹aŠyg@lìÜĐ½a@Òa†çþa@´i@ÕÏaìnÛa@òîÜàÇ@óÜÇ@Ýàn’í@ZÞëþa@áŽÔÛa@MQ @ @NHUnificationI@†îyìnÛa@òîÜàÈi@ÒŠÈí @ @ZÙÛ‡@|™ìí@ïÛbnÛa@Þbr½aë likes (ali,ahmed). likes (noha,sami). likes (aser,noha). @ @ZÒ†a@æb×ë @ @likes(X,Y). ‫ ن‬ ª‫א‬Ù ‫א‬  ‫د‬ Ù ‫ ن א‬ X,Y ‫א‬ ğ‫א‬ Ù ‫א‬ Y‫و‬X W Ĩ ú ‫א‬ ě ‫א‬ ª‫א‬Ù ‫א‬ ‫א‬ ‫وא‬ X=aser X=ali Y=noha ú ‫א‬ X=noha Y=sami Y=ahmed ‫אس وذ‬ ‫א‬ ‫ دא‬Ĩ ª‫א‬Ù ‫א‬ ‫א‬ Kú ‫وא‬ ‫ذא‬ ‫ن‬ EClausesF ‫د‬ ‫وא‬ Ğ‫א‬ ‫نא‬ ‫א‬ ‫  א‬likes E ġ‫ و‬ú Ħ ‫א‬F ‫א‬ W‫ن‬ KĦ ğ‫ א‬ K Ğ‫ ود  א‬ğ‫د א‬ KĦ 5 Ħ ‫زو‬ ‫و‬ ‫א‬  ‫وא‬ ú ‫ ود  א‬ğ‫د א‬ ‫ א‬ú‫א‬ ‫א‬ ‫ א‬J J  J
  • Muhammad Siddig H. @ @NbèÔîÔ¤@ëc@Òa†çþa@‹aŠy⁄@Þìܧa@åÇ@szjÛa@¿@áØznm@Ñî×@òa‰…@óÜÇ@Ýàn’íë@ZïãbrÛa@áŽÔÛa@MR @ @ZÙÛ‡@|™ìí likes (ali,ahmed). likes (noha,sami). likes (aser,noha). @ @ZÒ†a@æb×ë likes(aser,Y). ‫ ذא‬Ù ‫א‬ ‫ل‬ ğ‫א‬ ú ‫א‬ Ø‫م א‬ Í ‫ ذא‬K Ĝ ú ‫א‬ ‫و‬ ‫د‬ ‫وא‬ ‫א‬ ‫א‬ ‫ًو‬ ú ‫ن א‬ ‫  א‬ú ‫و د א‬ ‫م‬ ú ‫א‬ ª ‫א‬ @ @ïÐܨa@ݎ܎nÛaë@ïßbßþa@ݎ܎nÛa @ @@ZForward tracking@@ïßbßþa@ݎ܎nÛa ‫و‬ Ğ ‫א‬ ğ‫א‬ ‫و‬ Ĩ ‫و‬ ‫ل‬ ‫א‬ ‫א‬Ø ‫א‬ ‫א‬ ª ĩ ‫א‬ ‫א‬ ‫אم א‬ ‫א‬ ‫ و‬ú ‫ א‬ħ ‫ل‬ K ‫א‬ ‫و‬ @ @ZBacktracking @ïÐܨa@ݎ܎nÛa ě  ‫ وא‬Prolog Ø‫م א‬ ‫ وא‬ª‫א‬  ‫د‬ ‫א‬  ‫ن‬ ‫א‬ ī ‫نא‬ 6 ‫ א‬ª ‫א‬ ‫א‬ ‫ א‬Ğ ‫ذא‬ ‫م‬ ‫مو د א‬ ‫و‬ ª ħ ‫א‬ Ġ‫א‬ ĩ‫و‬ ‫א‬ ‫א‬ª ‫א‬ × ģ‫و‬ ‫א‬  ‫ א‬ ‫ ن‬ħ ú‫א‬ ‫א‬ ‫مא‬ ‫ن‬ ‫א‬ ‫ذא‬ ‫و‬ ‫א א‬ K
  • Muhammad Siddig H. ª ě‫و‬ ‫ א‬ú ‫ א‬ħ Ġ‫א‬ ‫دو א‬ . W‫א ل‬ ‫دא‬ ‫م‬ ¯ ‫א‬ ĝ ‫א‬ ě‫و‬ ‫א‬ ‫و‬ª ‫א‬  ‫א‬ ‫د‬ ‫א ل א‬ location(desk, office). location(apple, kitchen). location(flashlight, desk). location('washing machine', cellar). location(nani, 'washing machine'). location(broccoli, kitchen). location(crackers, kitchen). location(computer, office). ‫مא‬ ‫א‬ ‫ن‬ Location(apple, kitchen). W ‫א‬ Eú ‫א‬F ‫م‬ ğ ‫م‬ ‫א‬ ً ‫و‬ ‫א‬ ‫א‬ ‫א‬ ú ğ‫ א‬ħ ‫א‬ ‫ن‬ ‫ و‬J ‫عא‬  ‫א‬ ‫م‬ ‫א‬ W ‫د‬ ‫א‬ Ğ‫א‬ ‫א‬ • ‫א‬ ‫و‬ J EUNIFICATIONF Ğ‫א‬ ‫ א‬Ğ‫א‬  ‫و‬ ‫ و‬X=apple  ‫د‬ ‫א‬ ‫א‬ ‫و‬ Ø‫א‬  ‫د‬ Ë‫و‬  ‫و‬ Ğ ‫א ول‬ ‫א‬ Ğ ģ ‫ א‬Ğ ‫ذא א‬ ‫و‬ ‫א د א‬ Ğ‫א‬  ħ‫א و‬ ‫א‬ KTrue  ‫א‬ ‫א‬ ‫و‬ ‫• و‬ ‫ و‬X Ù ‫א‬ ğ‫א‬ ‫ א א‬Ğ‫א‬ X=broccali ‫و‬ ‫א‬ ‫م‬ ‫א‬ ‫א‬ ‫و‬ ‫א‬ Z†ÇaìÔÛa@òÛby@¿@ïÐܨa@ÉjnnÛa clerk(mohamed). clerk(adam). typist(ali). manager(salih). manager(omar). super(X,Y):- manager(X), clerk(Y). super(X,Y):- clerk(Y), typist(Y). super(X,Y):- manager(X), typist(Y). super(Super,ali). 7 W ‫א‬ ‫א‬ ‫ن‬ ‫و‬
  • Muhammad Siddig H. ‫א‬ Ø‫א‬ ‫א ول‬ ‫ و‬K X=ali Ğ‫ن א‬ ‫א‬ ‫אم א‬  ‫ و‬clerk(adam)  ‫ ن‬ħ‫א و‬ ‫א‬  ‫ و‬Y=mohamed ‫אل א ول وא‬ ‫א‬ħ ‫و‬ KX=omar ‫ن‬ Ğ‫א‬ ‫قא‬ ‫א‬ K •  Ğ‫א‬ Ø‫א‬ ğ‫ א‬ħ ‫ل‬ Ø‫م א‬ ‫א‬ ‫א‬ ‫د א‬ ‫א‬ manager(omer) ‫א‬ managerEXF ‫ن‬ clerk(Y) Ġ‫א‬ ħ‫א و‬ ‫א‬ ‫و‬ ª ‫א‬ ‫א‬ Ğ‫א א‬ K ‫א‬ Ġ‫א‬ ‫وא‬ Ġ‫א‬ ‫و‬ ‫¯א‬ ‫א‬ ‫א‬ ī‫و‬ Prolog ‫א‬ ‫אم‬ ×‫א‬  ‫א‬ ‫א‬ ‫دא‬ ‫א‬ ×‫א‬ ×‫א‬ ‫א‬ú Ġ‫א‬ ĩ ‫א‬ú ×‫א‬ ęª E>F ‫د دא‬ ‫א‬ ĩ ‫א‬ ‫دون‬ ‫و‬K ‫א א‬ W ‫ א‬Ğ‫ً א‬ Factorial (0,1),!. GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG Ħ Ħ ‫ل‬ ğ‫ א ن وא‬Ħ ‫א‬ road('A','C',200). road('A','D',300). road('C','D',100). road('C','B',120). road('D','B',130). route(Town1,Town2,Dist):road(Town1,Town2,Dist). route(Town1,Town2,Dis):road(Town1,X,Dist1), route(X,Town2,Dist2), Dis is Dist1+Dist2. W route (City1,City2,Distance). ‫א‬ ú ‫ن א‬ W ‫ذא‬ ‫ א‬ú ‫ א‬ª route(A,B,X). GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG 8
  • Muhammad Siddig H. @ @Lists@áöaìÔÛa K ª ‫אم א‬ ‫א‬ ‫א‬ []Ħ ‫ א‬ª Ħ K[1,2,3,4,5,6,7,8,9] ً ‫ سא‬ú ‫ و‬KTail K K[X|Y] ً K ‫א‬ ‫دא‬ ‫א‬ ‫א‬ ‫وא‬ ‫ א س وא‬Ħ KMatching ‫א‬ E|F ‫א‬Ħ ‫א א‬ ‫א‬ K ‫ وא‬Head ‫ א س‬Ħ ‫א‬ • ‫×א‬ ħ ‫א‬Ħ • ‫א‬ ‫א‬ ‫ول‬ • ‫ א‬Prolog ‫م‬ ً ‫دא‬ ‫א‬ member(member_name,[member1,member2]). W ‫א‬  ‫א‬ ‫א‬ ‫ض ن‬Ø ‫ א‬W ً [mohamed,ali,suliman,hamdan,mohmoud] ‫א‬ ‫و‬ W × ‫ א‬Ø ‫ א‬ú ‫א‬ ‫ م‬ali member(ali,[mohamed,ali,suliman,hamdan,mahmoud]). ----------------------------------------------------------------------------------‫م‬ ‫ א‬ě‫و‬ ‫ سא‬Ħ ‫א‬ ‫ א‬ú ‫ذא ن א‬ W ‫ א‬ú ‫א‬ member(Name,[mohamed | _ ]). ---------------------------------------------------------------------------------@ @NòŽÜ@Þì@Êbu‰hi@âìÔí@Prolog@wßbãŠi mylength([],0). mylength([_|Tail], Len) :mylength(Tail, TailLen), Len is TailLen + 1. KX=0.  Ø‫ ن א‬mylength([],X). ú ‫ذא ن א‬ KX=7.  Ø‫ن א‬ mylength([a,b,c,d,e,f,g],X). ú ‫ن א‬ ‫ذא‬ @ @Zò܎܍@¿@ñ…ìuì½a@Š•bäÈÛa@Éà€@âìÔí@Prolog@wßbãŠi sum([],0). sum([Head|Tail],X):sum(Tail,TailSum), X is Head+TailSum. KSum=6 9  Ø‫ن א‬ sum([1,2,3],Sum). ú ‫ن א‬ ‫ذא‬
  • Muhammad Siddig H. @ @Recursion@‰aŠØnÛa ‫א‬ Ĩ ‫و‬ ‫א‬ ‫א‬ ‫א‬ K Ğ ‫ل א‬ N! ‫د‬ ‫ א‬ª‫و‬ ‫وא‬ ª ğ Prolog ª ‫א‬ ‫א‬ × ‫ א‬Wً W ‫א‬ factorial (0,1). factorial (N,Res) :N>1, N1 is N-1, factorial(N1,FacN1), Res is N*FacN1. W ‫ א‬Ø factorial(2,Answer). Answer=2. ‫ א‬ú ‫א‬ Ø‫ن د א‬ @ @Zòî™bíŠÛa@pbîÜàÈÛa False ‫ و‬True E=F ‫و‬ ‫א‬ ً WProlog ‫نא د‬ E3>5F ª ª is Ø ‫ א‬ª‫א‬ ‫א‬ ‫وא‬ ª 2?- X is 3+5. X=8. 3?- X is 3-5. X= -2. 4?- X is 3/5. X=0.6. 5?- X is 3*5. X=15. 10 ‫א‬  Ø‫ن א‬ ‫ א‬Ħ ‫ن‬ W ‫ذא‬ ‫א‬ W ‫א‬ ‫ذא‬ ‫א‬ W 1?- X=3+5. X=3+5. K ‫א‬ ‫ن‬ ‫و‬ ‫א‬ Ø‫א‬ ‫ن‬ Ø‫א‬ ‫ن‬ Ø‫א‬ ‫ن‬ Ø‫א‬ ‫ن‬
  • Muhammad Siddig H. @ @òî™bíŠÛa@Þaë†Ûa @ @ZïÈîiÛa@‰ˆ¦a@òÛa… 1?- sqrt(10,X). X=3.1622776601683795 Ø‫א‬ ‫ن‬ @ @ïÈîjĐÛa@áríŠËìÜÛa@òÛa… 2?- log(1,X). X=1.0. Ø‫א‬ ‫ن‬ @ @HLNI@QP@÷bÿÛ@áríŠËìÜÛa@òÛa… 3?- log10(1,X). X=0.301029995. Ø‫א‬ ‫ن‬ @ @@òÔÜĐ½a@òàîÔÛa@òÛa… 4?- abs(-3,X). X=3. Ø‫א‬ ‫ن‬ @ @ZòàŽÔÛa@ïÓbi@òÛa… 5?- X is 10 mod 20. X=10. Ø‫א‬ ‫ن‬ @ @òîrÜr½a@Þaë†Ûa @ @kî¦a@òÛa… 6?- sin(0,X). X=0.0. Ø‫א‬ ‫ن‬ @ @âbànÛa@kîu@òÛa… 7?- cos(0,X). X=1.0. Ø‫א‬ ‫ن‬ ‫دא א‬ 8?-tan(0,X). X=0.0. Ø‫א‬ ‫ن‬ @ @ 11