Sunteți pe pagina 1din 35

INSTITUTE OF ENGINEERING AND TECHNOLOGY BHADDAL, ROPAR

PRACTICAL FILE SLLP-LAB (CS-416)

SUBMITTED TO:
Ms. Amandeep Kaur

SUBMITTED BY:

INDEX
Sr.No.
1. . $. ,. #. 2. 4. +. /. 11. 11. 1 . 1$. 1,. 1#.

N !" O# T$" E%&"r'!"()


Study of Propositional Logic. Study of !irst "rder Predicate Logic. Study of E%ecution Model of P&"L"' and (o) &ecursion )or*s in P&"L"'. Study of some -uilt.in predicates. Introduction to Prolog programming 0y a simple Prolog program. Program to find )(et(er t(e gi3en num0er is positi3e or negati3e. Program to find )(et(er t(e gi3en num0er is e3en or odd. Program to c(ec* )(et(er a gi3en person is mem0er of clu0 or not. Program s(o)ing mapping t(at is constructing ne) structure similar to old one. 5i6 Program to find lengt( of t(e list using &ecursion. 5ii6 Program to find lengt( of t(e list using Accumulators. Program to find t(e factorial of t(e num0er. Program to generate !i0onacci Series. Program to illustrate t(e use of recursion to find out sum of N natural num0ers. Program to illustrate 7ut and 7ut.!ail com0ination in Prolog. Program to descri0e some 0asic predicates t(at is useful for manipulating Lists. 5i6 8o concatenate t)o Lists. 5ii6 8o delete element from t(e List. 5iii6 8o re3erse elements of t(e List. 5i36 8o perform union operation on t)o Lists. 536 8o perform intersection operation on t)o Lists. Program to implement 0u00le sort. Program to implement intersection sort. Program to implement 8ic*.8ac*.8oe 'ame.

P *" No.
1. #. +. /. 11. 1$. 1,. 1#. 12. 14. 1/. 1. 1. . $.

12. 14. 1+.

+. /. $1.

E%&"r'!"() No.:1
S)+,- o# Pro&o.')'o( / Lo*'0
I()ro,+0)'o(
Propositional logic9 also *no)n as sentential logic9 is t(at 0ranc( of logic t(at studies )ays of com0ining or altering statements or propositions to form more complicated statements or propositions. :oining t)o simpler propositions )it( t(e )ord ;and; is one common )ay of com0ining statements. <(en t)o statements are =oined toget(er )it( ;and;9 t(e comple% statement formed 0y t(em is true if and only if 0ot( t(e component statements are true. -ecause of t(is9 an argument of t(e follo)ing form is logically 3alid> Paris is t(e capital of !rance and Paris (as a population of o3er t)o million. 8(erefore9 Paris (as a population of o3er t)o million. Propositional logic largely in3ol3es studying logical connecti3es suc( as t(e )ords ;and; and ;or; and t(e rules determining t(e trut(.3alues of t(e propositions t(ey are used to =oin9 as )ell as )(at t(ese rules mean for t(e 3alidity of arguments9 and suc( logical relations(ips 0et)een statements as 0eing consistent or inconsistent )it( one anot(er9 as )ell as logical properties of propositions9 suc( as 0eing tautologically true9 0eing contingent9 and 0eing self.contradictory. Propositional logic also studies )ay of modifying statements9 suc( as t(e addition of t(e )ord ;not; t(at is used to c(ange an affirmati3e statement into a negati3e statement. ?ere9 t(e fundamental logical principle in3ol3ed is t(at if a gi3en affirmati3e statement is true9 t(e negation of t(at statement is false and if a gi3en affirmati3e statement is false9 t(e negation of t(at statement is true. Propositional logic can 0e t(oug(t of as primarily t(e study of logical operators. A logical operator is any )ord or p(rase used eit(er to modify one statement to ma*e a different statement9 or =oin multiple statements toget(er to form a more complicated statement. In Englis(9 )ords suc( as ;and;9 ;or;9 ;not;9 ;if ... t(en...;9 ;0ecause;9 and ;necessarily;9 are all operators. A logical operator is said to 0e trut(.functional if t(e trut(.3alues 5t(e trut( or falsity9 etc.6 of t(e statements it is used to construct al)ays depend entirely on t(e trut( or falsity of t(e statements from )(ic( t(ey are constructed. 8(e Englis( )ords ;and;9 ;or; and ;not; are 5at least argua0ly6 trut(.functional9 0ecause a compound statement =oined toget(er )it( t(e )ord ;and; is true if 0ot( t(e statements so =oined are true9 and false if eit(er or 0ot( are false9 a compound statement =oined toget(er )it( t(e )ord ;or; is true if at least one of t(e =oined statements is true9 and false if 0ot( =oined statements are false9 and t(e negation of a statement is true if and only if t(e statement negated is false. 8rut(.functional propositional logic is t(at 0ranc( of propositional logic t(at limits itself to t(e study of trut(.functional operators. 7lassical 5or ;0i3alent;6 trut(.functional propositional logic is t(at 0ranc( of trut(.functional propositional logic t(at assumes t(at t(ere are only t)o possi0le trut(.3alues a statement 5)(et(er simple or comple%6 can (a3e> 516 trut(9 and 5 6 falsity9 and t(at e3ery statement is eit(er true or false 0ut not 0ot(. 7lassical trut(.functional propositional logic is 0y far t(e most )idely studied 0ranc( of propositional logic9 and for t(is reason9 most of t(e remainder of t(is article focuses e%clusi3ely on t(is area of logic. In addition to classical trut(.functional propositional logic9 t(ere are ot(er 0ranc(es of propositional logic t(at study logical operators9 suc( as ;necessarily;9 t(at are not trut(.functional. 8(ere are also ;non.classical; propositional logics in )(ic( suc( possi0ilities as 5i6 a proposition@s (a3ing a trut(.3alue ot(er t(an trut( or falsity9 5ii6 a proposition@s (a3ing an indeterminate trut(.3alue or lac*ing a trut(.

3alue altoget(er9 and sometimes e3en 5iii6 a proposition@s 0eing 0ot( true and false9 are considered.

T$" L (*+ *" o# Pro&o.')'o( / Lo*'0


8(e 0asic rules and principles of classical trut(.functional propositional logic are9 among contemporary logicians9 almost entirely agreed upon9 and capa0le of 0eing stated in a definiti3e )ay. 8(is is most easily done if )e utiliAe a simplified logical language t(at deals only )it( simple statements considered as indi3isi0le units as )ell as comple% statements =oined toget(er 0y means of trut(.functional connecti3es. <e first consider a language called PL for ;Propositional Logic;. Later )e s(all consider t)o e3en simpler languages9 PL@ and PL@@. 6 S-() % (, For! )'o( R+/". o# PL In any ordinary language9 a statement )ould ne3er consist of a single )ord9 0ut )ould al)ays at t(e 3ery least consist of a noun or pronoun along )it( a 3er0. ?o)e3er9 0ecause propositional logic does not consider smaller parts of statements9 and treats simple statements as indi3isi0le )(oles9 t(e language PL uses uppercase letters @A@9 @-@9 @7@9 etc.9 in place of complete statements. 8(e logical signs @B@9 @3@9 @C@9 @D@9 and @E@ are used in place of t(e trut(.functional operators9 ;and;9 ;or;9 ;if... t(en...;9 ;if and only if;9 and ;not;9 respecti3ely. So9 consider again t(e follo)ing e%ample argument Paris is t(e capital of !rance and Paris (as a population of o3er t)o million. 8(erefore9 Paris (as a population of o3er t)o million. If )e use t(e letter @7@ as our translation of t(e statement ;Paris is t(e capital of !rance; in PL9 and t(e letter @P@ as our translation of t(e statement ;Paris (as a population of o3er t)o million;9 and use a (oriAontal line to separate t(e premise5s6 of an argument from t(e conclusion9 t(e a0o3e argument could 0e sym0oliAed in language PL as follo)s> D"#'(')'o(> A statement letter of PL is defined as any uppercase letter )ritten )it( or )it(out a numerical su0script. D"#'(')'o(> A connecti3e or operator of PL is any of t(e signs @E@9 @B@9 @3@9 @C@9 and @D@. D"#'(')'o(> A )ell.formed formula 5(ereafter a00re3iated as )ff6 of PL is defined recursi3ely as follo)s> 1. Any statement letter is a )ell.formed formula. . If F is a )ell.formed formula9 t(en so is EF . $. If F and G are )ell.formed formulas9 t(en so is 5F B G6 . ,. If F and G are )ell.formed formulas9 t(en so is 5F 3 G6 . #. If F and G are )ell.formed formulas9 t(en so is 5F C G6 . 2. If F and G are )ell.formed formulas9 t(en so is 5F D G6 . 4. Not(ing t(at cannot 0e constructed 0y successi3e steps of 516.526 is a )ell.formed formula. 1) Tr+)$ F+(0)'o(. (, Tr+)$ T 1/". Co(2+(0)'o(: 8(e con=unction of t)o statements F and G9 )ritten in PL as 5F B G6 9 is true if 0ot( F and G are true9 and is false if eit(er F is false or G is false or 0ot( are false.

F 8 8 ! !

G 8 ! 8 !

5F B G6 8 ! ! !

D'.2+(0)'o(: 8(e dis=unction of t)o statements F and G9 )ritten in PL as 5F 3 G6 9 is true if eit(er F is true or G is true9 or 0ot( F and G are true9 and is false only if 0ot( F and G are false. F 8 8 ! ! G 8 ! 8 ! 5F 3 G6 8 8 8 !

M )"r' / I!&/'0 )'o(: 8(is trut(.function is represented in language PL )it( t(e sign @C@. A statement of t(e form 5F C G6 9 is false if F is true and G is false9 and is true if eit(er F is false or G is true 5or 0ot(6. 8(is trut(.function generates t(e follo)ing c(art> F 8 8 ! ! G 8 ! 8 ! 5F C G6 8 ! 8 8

M )"r' / E3+'4 /"(0": 8(is trut(.function is represented in language PL )it( t(e sign @D@. A statement of t(e form 5F D G6 is regarded as true if F and G 0ot( are eit(er true or false9 and is regarded as false if t(ey (a3e different trut(.3alues. ?ence9 )e (a3e t(e follo)ing c(art>

F 8 8 ! !

G 8 ! 8 !

5F D G6 8 ! ! 8

N"* )'o(: 8(e negation of statement F9 simply )ritten EF in language PL9 is regarded as true if F is false9 and false if F is true. Hnli*e t(e ot(er operators )e (a3e considered9 negation is applied to a single statement. 8(e corresponding c(art can t(erefore 0e dra)n more simply as follo)s> I 8 ! EF ! 8

7onsider a com0ined trut( ta0le for t(e )ffs ;EP C EJ; and ;E5J B EP6;> P 5 8 ! 8 ! K 6 ! ! 8 8 P 8 8 ! ! 7 8 8 ! 8 6 ! 8 ! 8 5 8 ! 8 ! 6 8 8 ! 8 (5 8 8 ! 8 ! ! ! 8 ! 6 ! ! 8 8 P)) 8 8 ! !

E%&"r'!"() No.:9
S)+,- o# F'r.) Or,"r Pr",'0 )" Lo*'0
I()ro,+0)'o(
F'r.)-or,"r /o*'0 (FOL) is a language in sym0olic science9 )(ic( is used 0y mat(ematicians9 p(ilosop(ers9 linguists9 and computer scientists. It goes 0y many names9 including> #'r.)-or,"r &r",'0 )" 0 /0+/+. 5FOPC69 )$" /o:"r &r",'0 )" 0 /0+/+.9 )$" / (*+ *" o# #'r.)-or,"r /o*'0 or &r",'0 )" /o*'0. 8(e most used name is (o)e3er FOL9 pronounced ef.o(.el. !"L is a system of deduction e%tending propositional logic 0y t(e a0ility to e%press relations 0et)een indi3iduals 5e.g. people9 num0ers9 and ;t(ings;6 more generally. Propositional logic is not adeLuate for formaliAing 3alid arguments t(at rely on t(e internal structure of t(e propositions in3ol3ed. !or e%ample9 a translation of t(e 3alid argument

All men are mortal Socrates is a man 8(erefore9 Socrates is mortal

)(ic( is in3alid. 8(e rest of t(e article e%plains (o) !"L is a0le to (andle t(ese sorts of arguments. 8(e atomic sentences of first.order logic (a3e t(e form P5t19 ...9 tn6 5a predicate )it( one or more ;arguments;6 rat(er t(an 0eing propositional letters as in propositional logic. 8(is is usually )ritten )it(out parent(eses or commas9 as 0elo) 8(e ne) ingredient of first.order logic not found in propositional logic is Luantification> )(ere M is any 5)ell.formed6 formula9 t(e ne) constructions % M and % M N read ;for all %9 M; and ;for some %9 M; N are introduced9 )(ere % is an indi3idual 3aria0le )(ose range is t(e set of indi3iduals of some gi3en uni3erse of discourse 5or domain6. !or e%ample9 if t(e uni3erse consists solely of people9 t(en % ranges o3er people. !or con3enience9 )e )rite M as M5%6 to s(o) t(at it contains only t(e 3aria0le % free and9 for 0 a mem0er of t(e uni3erse9 )e let MO0P e%press t(at 0 satisfies 5i.e. (as t(e property e%pressed 0y6 M. 8(en % M5%6 states t(at MO0P is true for e3ery 0 in t(e uni3erse9 and % M5%6 means t(at t(ere is a 0 5in t(e uni3erse6 suc( t(at MO0P (olds. 8(e argument a0out Socrates can 0e formaliAed in first.order logic as follo)s. Let t(e uni3erse of discourse 0e t(e set of all people9 li3ing and deceased and let Man5%6 0e a predicate 5)(ic(9 informally9 means t(at t(e person represented 0y 3aria0le % is a man6 and Mortal5%6 0e a second predicate. 8(en t(e argument a0o3e 0ecomes

% 5Man5%6 C Mortal5%66 Man5Socrates6 Mortal5Socrates6

A literal translation of t(e first line )ould 0e ;!or all %9 if % is descri0ed 0y @Man@9 % must also 0e descri0ed 0y @Mortal@.; 8(e second line states t(at t(e predicate ;Man; applies to Socrates9 and t(e t(ird line translates to ;8(erefore9 t(e description @Mortal@ applies to Socrates.;

D"#'('(* #'r.)-or,"r /o*'0


A predicate calculus consists of>.

formation rules 5i.e. recursi3e definitions for forming )ell.formed formulas6. transformation rules 5i.e. inference rules for deri3ing t(eorems6. a%ioms or a%iom sc(emata 5possi0ly counta0le infinite6.

8(e a%ioms considered (ere are logical a%ioms )(ic( are part of classical FOL. !urt(er9 non.logical a%ioms are added to yield specific first.order t(eories t(at are 0ased on t(e a%ioms of classical FOL 5and (ence are called classical first.order t(eories9 suc( as classical set.t(eory6. 8(e a%ioms of first.order t(eories are not regarded as trut(s of logic per se9 0ut rat(er as trut(s of t(e particular t(eory t(at usually (as associated )it( it an intended interpretation of its non.logical sym0ols. 5See an analogous idea at logical 3ersus non.logical sym0ols.6 7lassical FOL does not (a3e associated )it( it an intended interpretation of its non.logical 3oca0ulary 5e%cept argua0ly a sym0ol denoting identity9 depending on )(et(er one regards suc( a sym0ol as logical6. <(en t(e set of a%ioms is infinite9 it is reLuired t(at t(ere 0e an algorit(m )(ic( can decide for a gi3en )ell.formed formula )(et(er or not it is an a%iom. 8(ere s(ould also 0e an algorit(m )(ic( can decide )(et(er a gi3en application of an inference rule is correct or not.It is important to note t(at FOL can 0e formaliAed in many eLui3alent )aysQ t(ere is not(ing canonical a0out t(e a%ioms and rules of inference gi3en (ere. 8(ere are infinitely many eLui3alent formaliAations all of )(ic( yield t(e same t(eorems and non.t(eorems9 and all of )(ic( (a3e eLual rig(t to t(e title @FOL;.

<o0 1+/ r8(e ;3oca0ulary; is composed of 1. A set of &r",'0 )" 4 r' 1/". 5or r"/ )'o(.6 eac( )it( some 4 /"(0" 5or r')-6 R19 )(ic( are often denoted 0y uppercase letters P9 J9 &.... . A set of 0o(.) ().9 often denoted 0y lo)ercase letters at t(e 0eginning of t(e alp(a0et a9 09 c.... $. A set of #+(0)'o(.9 eac( of some 3alence R 19 )(ic( are often denoted 0y lo)ercase letters f9 g9 (.... ,. An infinite set of 4 r' 1/".9 often denoted 0y lo)ercase letters at t(e end of t(e alp(a0et %9 y9 A.... #. Sym0ols denoting /o*'0 / o&"r )or. 5or 0o(("0)'4".6> Logical And S T Logical "r . UV Logical Not . W Logical Implication . Logical -iconditional .

2. Sym0ols denoting 3+ ()'#'"r.> 5uni3ersal Luantification69 5e%istential Luantification6. 4. Left and &ig(t parent(esis. +. An identity or eLuality sym0ol XYZ is sometimes 0ut not al)ays included in t(e 3oca0ulary.

E%&"r'!"() No.:=
S)+,- o# E%"0+)'o( Mo,"/ '( PROLOG (, $o: R"0+r.'o( :or>. '( PROLOG.
E%"0+)'o( Mo,"/: 8(e e%ecution of a Prolog Program consists of follo)ing t(ree steps>.
De3elopment of a program. 7ompiling of a program. Lin*ing of a program.

1) D"4"/o&!"() o# Pro*r !: Program consists of facts9 rules and Lueries to t(e interpreter. !acts are al)ays true 5unconditionally6. &ules 5clauses6 are in t(e form of synta%>. [?ead\ >. [8ail\ or Atom> . Atom19 Atom 9]..9Atomn. If Atom19].9Atomn all are true9 only t(en Atom is true ot(er)ise false. Jueries are t(e Luestions )(ose result is deri3ed from t(e facts and rules and synta% is>. ^.Predicate 9) Co!&'/'(* o# Pro*r !: <(en Luery is entered9 t(e second stage of e%ecution starts )(ic( is compiling of a program in )(ic( interpreter c(ec*s for t(e synta% errors. If t(ere e%ists error9 t(en it prompt t(e user9 else it )ill c(ange t(e source code into t(e o0=ect code. =) L'(>'(* o# )$" Pro/o* Pro*r !: 8(e lin*er c(ec*s for t(e lin*ing error. If no error is found t(en t(e o0=ect code of t(e prolog program is c(anged into e%ecuta0le code.

M )0$'(* '( Pro/o* ,+r'(* "%"0+)'o(


!or a perfect matc( to ta*e place>. 8(e predicate names and all t(e succeeding arguments must matc(. If t(e goal succeeds9 t(en t(e interpreter lea3es a mar*er 0y t(e fact )(ere t(e goal succeeded9 to remind itself t(at t(e goal succeeded at t(is point in t(e data0ase. 8(e mar*er can 0e t(oug(t of as a notice 0oard saying9 e.g.>. 8(e goal planet5mercury6. )as satisfied at t(is point. It t(en reports success to t(e user 0y saying X_esZ. If goal fails9 t(en t(e interpreter returns and says XNoZ. Matc(ing a simple Luery and facts>. If X^.or0its5t(e`moon9t(e`eart(6.Z is t(e goal t(en t(e fact Xor0its5t(e`moon9t(e`eart(6.Z )ill result in a perfect matc( and t(e goal succeeds 0ut t(e fact Xor0its5t(e`moon9eart(6.Z is a 0ad matc( and (ence t(e goal fails.

R"0+r.'o(: 8(e recursion in any language is a function t(at can call itself until goal
succeeds. In prolog9 recursion appears )(en a predicate contains a goal t(at refers to itself. In Prolog and in any language9 a recursi3e definition al)ays (as at least t)o parts. 8(e first is a fact t(at acts li*e a slopping condition and t(e second is a rule t(at call itself simplified. At eac( le3el9 t(e first fact is c(ec*ed. If t(e fact is true9 t(en t(e recursion ends. If not9 t(e recursion continues. A recursi3e rule must ne3er call itself )it( t(e same argument. If t(at (appens9 t(e program )ill ne3er end. E%ample> !actorial. 8(e 0est )ay in prolog to calculate a factorial is to do it recursi3ely.

E%&"r'!"() No.:4
S)+,- o# .o!" B+'/)-'( &r",'0 )"..
-y -uilt.in Predicates9 )e mean t(at t(e predicateas definition is pro3ided in ad3ance 0y t(e Prolog system9 instead of 0y your o)n clauses. -uilt.in predicates may pro3ide facilities t(at cannot 0e o0tained 0y definitions in pure Prolog. "r t(ey may pro3ide con3enient facilities =ust to sa3e eac( programmer from (a3ing to define t(em (imself. Some of t(e -uilt.in predicates are defined 0elo)>. 1.0o(.+/)(?): It is meant for t(ose situations )(en you )ant t(e clauses in some file to augment t(ose already in t(e data0ase. 8(e argument must 0e an atom gi3ing t(e name of t(e file t(e clauses are to 0e ta*en from. E.g.>. ^. consult5myfile6. 9.r"0o(.+/)(?): It is same li*e consult e%cept t(at t(e clauses read in are ta*en to supersede all e%isting clauses for t(e same predicate. -ecause of t(is9 reconsult is appropriate for correcting programming mista*es. =.)r+": 8(is goal al)ays succeeds. It is not actually necessary9 as clauses and goals can 0e reordered or recom0ined to o03iate any use of true. ?o)e3er9 it e%ists for con3enience. 4.# '/: 8(is goal al)ays fails. 8(ere are t)o places )(ere it is (elpful. "ne place is t(e Xcut failZ com0ination. Anot(er place to use fail is )(ere you e%plicitly )ant anot(er goal to 0ac*trac* t(roug( all solutions. @. .."r) (?): It adds a clause at t(e 0eginning of t(e data0ase. !or t(is9 X must 0e already instantiated to somet(ing representing a clause. 6. .."r)A(?): It is similar to asserta5X6 0ut t(e difference is t(at it adds a clause at t(e end of t(e data0ase. B.r")r 0)(?): It ena0les a program to remo3e clauses from t(e data0ase. 8(e predicate ta*es a single argument9 representing a term t(at t(e clause is to matc(. "nce a clause (as 0een remo3ed it is ne3er reinstated9 e3en )(en 0ac*trac*ing tries to re.satisfy t(e retract goal. C.(o)(?): 8(e not5X6 goal succeeds if an attempt to satisfy X fails. 8(e not5X6 goal fails if an attempt to satisfy X succeeds. D.(/: It )rites a control seLuence to t(e current output stream t(at causes a Xne) lineZ. It succeeds only once. 1E.:r')"(?): 8(is goal )rites t(e term X to t(e current output stream. It succeeds only once. 11.0+): It allo)s you to tell Prolog )(ic( pre3ious c(oices it need not consider again )(en it 0ac*trac*s t(roug( t(e c(ain of satisfied goals. Syntactically9 a use of XcutZ in a rule loo*s =ust li*e t(e appearance of a goal )(ic( (as t(e predicate XbZ and no arguments. As a goal9 t(is succeeds immediately and cannot 0e re.satisfied. 19.4 r(?): 8(e goal 3ar5X6 succeeds if X is currently an uninstantiated 3aria0le.8(us )e )ould e%pect t(e follo)ing 0e(a3ior. ^. 3ar5X6.

_es ^.3ar5 $6. No ^. X Y _9 _Y $9 3ar5X6. No An uninstantiated 3aria0le can represent part of a structure t(at (as not yet 0een filled in. 1=.(o(4 r(?): 8(e goal non3ar5X1 succeeds if X is not currently an uninstantiated 3aria0le. 8(e predicate non3ar is t(erefore t(e opposite of 3ar.It could 0e defined in Prolog 0y> non3ar5X6 >.3ar5X69 b 9 fail. non3ar5 ` 6 . 14.'()"*"r(?):8(e goal integer5X6 succeeds if X is currently stands for an integer5a )(ole num0er6 1@. r*(N,T,A):8(e predicate arg must al)ays 0e used )it( its first t)o argument instantiated. It is used to access a particular argument of structure. 8(e first argument of arg specifies )(ic( argument is reLuired.8(e second specifies t(e structure t(et t(e argument is to 0e found inside.Prolog finds t(e appropriate argument and t(en tries to matc( it )it( t(e t(ird argument.

E%&"r'!"() No.:@
I()ro,+0)'o( To Pro/o* Pro*r !!'(* 1.'!&/" Pro/o* Pro*r !.
Pro/o* is a general purpose logic programming language associated )it( artificial intelligence and computational linguistics. Prolog (as its roots in formal logic9 and unli*e many ot(er programming languages9 Prolog is declarati3e> 8(e program logic is e%pressed in terms of relations9 represented as facts and rules. E%ecution is triggered 0y running queries o3er t(ese relations. 8(e first Prolog system )as de3eloped in 1/4 0y Alain 7olmerauer and P(ilippe &oussel. Prolog )as one of t(e first logic programming languages9 and remains among t(e most popular suc( languages today9 )it( many free and commercial implementations a3aila0le. <(ile initially aimed at natural language processing9 t(e language (as since t(en stretc(ed far into ot(er areas li*e t(eorem pro3ing9 e%pert systems9 games9 automated ans)ering systems9 ontologies and sop(isticated control systems. Modern Prolog en3ironments support t(e creation of grap(ical user interfaces9 as )ell as administrati3e and net)or*ed applications.

Pro*r !!'(* '( Pro/o*


In Prolog9 program logic is e%pressed in terms of relations9 and e%ecution is triggered 0y running queries o3er t(ese relations. &elations and Lueries are constructed using Prolog@s single data type9 t(e term. &elations are defined 0y clauses. 'i3en a Luery9 t(e Prolog engine attempts to find a resolution refutation of t(e negated Luery. If t(e negated Luery can 0e refuted9 i.e.9 an instantiation for all free 3aria0les is found t(at ma*es t(e union of clauses and t(e singleton set consisting of t(e negated Luery false9 it follo)s t(at t(e original Luery9 )it( t(e found instantiation applied9 is a logical conseLuence of t(e program. 8(is ma*es Prolog 5and ot(er logic programming languages6 particularly useful for data0ase9 sym0olic mat(ematics9 and language parsing applications. -ecause Prolog allo)s impure predicates9 c(ec*ing t(e trut( 3alue of certain special predicates may (a3e some deli0erate side effect9 suc( as printing a 3alue to t(e screen. 8(is permits t(e programmer to use some amount of con3entional imperati3e programming )(en t(e logical paradigm is incon3enient. It (as a purely logical su0set9 called ;pure Prolog;9 as )ell as a num0er of e%tra logical features.

D ) )-&".
Prolog@s single data type is t(e term. 8erms are atoms, numbers9 variables or compound terms. An )o! is a general.purpose name )it( no in(erent meaning. E%amples of atoms include %9 0lue9 @8aco@9 and @some atom@. N+!1"r. can 0e floats or integers. < r' 1/". are denoted 0y a string consisting of letters9 num0ers and underscore c(aracters9 and 0eginning )it( an upper.case letter or underscore. Taria0les closely resem0le 3aria0les in logic in t(at t(ey are place(olders for ar0itrary terms. A 0o!&o+(, )"r! is composed of an atom called a ;functor; and a num0er of ;arguments;9 )(ic( are again terms. 7ompound terms are ordinarily )ritten as a functor follo)ed 0y a comma.separated list of argument terms9 )(ic( is contained in parent(eses. 8(e num0er of arguments is called t(e term@s arity. An atom can 0e regarded as a

compound term )it( arity Aero. E%amples of compound terms are truc*`year5@MaAda@9 1/+26 and @Person`!riends@5Aelda9Otom9=imP6. S&"0' / 0 .". o# 0o!&o+(, )"r!.: A List is an ordered collection of terms. It is denoted 0y sLuare 0rac*ets )it( t(e terms separated 0y commas or in t(e case of t(e empty list9 OP. !or e%ample O19 9 $P or Ored9 green9 0lueP. Strings> A seLuence of c(aracters surrounded 0y Luotes is eLui3alent to a list of 5numeric6 c(aracter codes9 generally in t(e local c(aracter encoding or Hnicode if t(e system supports Hnicode. !or e%ample> ;to 0e9 or not to 0e;.

R+/". (, # 0).
Prolog programs descri0e relations9 defined 0y means of clauses. Pure Prolog is restricted to ?orn clauses9 a 8uring.complete su0set of first.order predicate logic. 8(ere are t)o types of clauses> !acts and rules. A rule is of t(e form
?ead >. -ody.

and is read as ;?ead is true if -ody is true;. A rule@s 0ody consists of calls to predicates9 )(ic( are called t(e rule@s *o /.. 8(e 0uilt.in predicate 9U 5meaning a .arity operator )it( name 96 denotes con=unction of goals9 and QU denotes dis=unction. 7on=unctions and dis=unctions can only appear in t(e 0ody9 not in t(e (ead of a rule. 7lauses )it( empty 0odies are called # 0).. An e%ample of a fact is>
cat5tom6.

)(ic( is eLui3alent to t(e rule>


cat5tom6 >. true.

8(e 0uilt.in predicate trueU1 is al)ays true. 'i3en t(e a0o3e fact9 one can as*> is tom a cat?
^. cat5tom6. _es

what things are cats?


^. cat5X6. X Y tom

Due to t(e relational nature of many 0uilt.in predicates9 t(ey can typically 0e used in se3eral directions. !or e%ample9 lengt(U can 0e used to determine t(e lengt( of a list 5lengt(5List9 L69 gi3en a list List6 as )ell as to generate a list s*eleton of a gi3en lengt( 5lengt(5X9 #669 and also to generate 0ot( list s*eletons and t(eir lengt(s toget(er 5 lengt(5X9 L66. Similarly9 appendU$ can 0e used 0ot( to append t)o lists 5 append5ListA9 List-9 X6 gi3en lists ListA and List-6 as )ell as to split a gi3en list into parts 5 append5X9 _9 List69 gi3en a list List6. !or t(is reason9 a comparati3ely small set of li0rary predicates suffices for many Prolog programs. All predicates can also 0e used to perform unit tests> Jueries can 0e em0edded in programs and allo) for automatic compile.time regression testing. As a general purpose language9 Prolog also pro3ides 3arious 0uilt.in predicates to perform routine acti3ities li*e inputUoutput9 using grap(ics and ot(er)ise communicating )it( t(e operating system. 8(ese predicates are not gi3en a relational meaning and are only useful for t(e side.effects t(ey e%(i0it on t(e system. !or e%ample9 t(e predicate )riteU1 displays a term on t(e screen.

Pro/o* Pro*r !
Pro*r ! Co,":

Do! '(: I) '(0/+,". ,"0/ r )'o( (, '(')' /'A )'o( o# 4 r' 1/".. nYinteger. Pr",'0 )": I) ,"#'(". #+(0)'o(. o( )$" 4 r' 1/".. fact5n9n6. C/ +.".: I) '. +.", )o r"&r"."() 1o,- o# )$" &ro*r ! +.'(* r+/". (, # 0).. fact51916. fact51916. fact5N9&6>. N\19N1 is N . 19fact5N19&169& is &1 c N. Go /: I) '. )$" #'( / 3+"r- & ..", )o )$" Pro/o* (, r".+/). r" ,'.&/ -", '( o+)&+) :'(,o:. ^.fact5,9X69)rite5X6. O+)&+) F'(,o::

E%&"r'!"() No.:6
Fr')" Pro*r ! )o #'(, o+) :$")$"r )$" (+!1"r '. "4"( or o,,.
Pro*r ! Co,":
NYinteger. input5n6. input5N6>.N mod Y 19)rite5;no is e3en;69nl. input5N6>.)rite5;no is odd;6. ^.input5$6.

O+)&+):

E%&"r'!"() No.:B
Fr')" Pro*r ! )o #'(, o+) :$")$"r )$" (+!1"r '. &o.')'4" or ("* )'4"

Pro*r ! Co,":
NYinteger. input5n6. input5N6>.N\19)rite5;no is positi3e;69nl. input5N6>.N[19)rite5;no is negati3e;6. ^.input5$6.

O+)&+):

E%&"r'!"() No.:C
Fr')" Pro*r ! )o #'(, o+) :$")$"r )$" &"r.o( '. )$" !"!1"r o# )$" 0/+1 or (o).

Pro*r ! Co,":
XYinteger. LYXc mem0er5element9list6. mem0er5X9OXK`P6. mem0er5X9O`K8P6>.mem0er5X986. ^.mem0er5c(andan9Oc(andan9c(aran.c(arruP6.

O+)&+):

E%&"r'!"() No.:D
Fr')" Pro*r ! '( Pro/o* .$o:'(* M &&'(* )$ ) '. 0o(.)r+0)'(* (": .)r+0)+r" )o .'!'/ r )o o/, o(".

Pro*r ! Co,":
XYinteger. LYXc c(ange5element9element6. alter5list9list6. c(ange5you9i6. c(ange5are9am6. c(ange5my9your6. c(ange5X9X6. alter5OP9OP6. alter5O?K8P9OXK_P6>.c(ange5?9X69alter589_6. ^.alter5O@you@9@are@9@my@9@freind@P9X69)rite5X69nl.

O+)&+):

E%&"r'!"() No.:1E
5i6

Fr')"

Pro*r ! )o #'(, o+) /"(*)$ o# )$" /'.) +.'(* R"0+r.'o(.

Pro*r ! Co,":
XYinteger. LYXc lengt(5list9element6. lengt(5OP916. lengt(5O?K8P9N6>.lengt(589N169NYN1d1. ^.lengt(5O19 9$9,9#P9X69)rite5X69nl.

O+)&+):

5ii6

Fr')"

Pro*r ! )o #'(, o+) /"(*)$ o# )$" /'.) +.'(* A00+!+/ )or..

Pro*r ! Co,":
XYinteger. LYXc lengt(5list9element6. lengt(acc5list9element9element6. lengt(acc5OP91916. lengt(acc5O?K8P9A9N6>.lengt(acc589A19N169AYA1d1. ^.lengt(acc5O19 9$9,9#P9A9X69)rite5A69nl.

O+)&+):

E%&"r'!"() No.:11
Fr')" Pro*r ! )o #'(, o+) F 0)or' / o# (+!1"r +.'(* r"0+r.'o(.
Pro*r ! Co,":
NYinteger. fact5n9n6. fact51916. fact51916. fact5N9&6>. N\19N1 is N . 19fact5N19&169& is &1 c N. ^.fact5,9X69)rite5X6.

O+)&+):

E%&"r'!"() No.:19
Fr')"
Pro*r ! Co,":
NYinteger. fa05n9n6. fa051916. fa051916. fa05N9&6>.N \ 19N1 is N . 19fa05N19&169N is N . 9 fa05N 9& 69 & is &1 d & . ^.fa05,9X69)rite5X6.

Pro*r ! )o *"("r )" F'1o( 00' S"r'"..

O+)&+):

E%&"r'!"() No.:1=
Fr')" Pro*r ! )o '//+.)r )" )$" +." o# r"0+r.'o( )o #'(, o+) .+! o# #'r.) o# N ( )+r / (+!1"r..

Pro*r ! Co,":
NYinteger. sum5n9n6. sum51916. sum51916. sum5N9&6>.N \ 19N1 is N . 19sum5N19&169 & is &1 d N. ^.sum5,9X69)rite5X6.

O+)&+):

E%&"r'!"() No.:14
Fr')" Pro*r ! )o '//+.)r )" )$" +." o# C+) &r",'0 )" (, C+)-F '/ Co!1'( )'o( '( Pro/o*.

CUT PREDICATE
Pro*r ! Co,":
NYinteger. Sum`to5n9n6. Sum`to5N916>. NY[19b. Sum`to5N9&es6>. n1 is N S 19Sum`to5N19&es169&es is &es1 d N. ^.sum`to5,9X69)rite5X6.

O+)&+):

CUT-FAIL PREDICATE
Pro*r ! Co,":
li*es5aarti9X6>.mango5X69b9fail. li*es5aarti9X6>.fruit5X6. fruit5X6>.apple5X6. fruit5X6>.mango5X6. fruit5X6>.orange5X6. apple5a6. mango506. orange5c6. ^.li*es5aarti9a6.

E%&"r'!"() No.:1@
Fr')" Pro*r ! )o ,".0r'1" .o!" B .'0 Pr",'0 )". )$ ) '. +."#+/ o# M ('&+/ )'(* L'.)..

(') To 0o(0 )"( )" ):o L'.).. Pro*r ! Co,":


NYinteger. LYNc append5list9list9list6. append5OP9L9L6. append5OXKL1P9L 9OXKL$P6>.append5L19L 9L$6. ^.append5O19 9$P9O,9#P9X69)rite5X69nl.

O+)&+):

('') To ,"/")" "/"!"() #ro! )$" /'.). Pro*r ! Co,":


XYinteger. LYXc delete5element9list9list6. delete5X9OXK8P986. delete5X9O?K8P.O?K81P6>.delete5X989816. ^.delete5 9O19 9$9,9#P9X69)rite5X69nl.

O+)&+):

(''') To r"4"r." "/"!"(). o# )$" /'.). Pro*r ! Co,":


XYinteger. LYXc re3erse5list9list6. append5list9list9list6. re3erse5OP9OP6. re3erse5O?K8P9L6>.re3erse589e69append5e9O?P9L6. append5OP9L9L6. append5OXKL1P9L 9OXKL$P6>.append5L19L 9L$6. ^.re3erse5O19 9$9,P9X69)rite5X69nl.

O+)&+):

('4) To &"r#or! U('o( o&"r )'o( o( ):o /'.).. Pro*r ! Co,":


XYinteger. LYXc union5list9list9list6. mem0er5element9list6. union5OP9L9L6. union5O?K81P9L98 6>.mem0er5?9L69b9union5819L98 6. union5O?K81P9L9O?K8 P6>.union5819L98 6. mem0er5?9O?K`P6. mem0er5?9O`K8P6>.mem0er5?986. ^.union5O19 9$9,P9O#9294P9X69)rite5X69nl.

O+)&+):

536

To &"r#or! I()"r."0)'o( o&"r )'o( o( ):o /'.).. Pro*r ! Co,":


XYinteger. LYXc intersect5list9list9list6. mem0er5element9list6. intersect5OP9`9OP6. intersect5O?K81P9L9O?K8 P6>.mem0er5?9L69b9intersect5819L98 6. intersect5O`K81P9L98 6>.intersect5819L98 6. mem0er5?9O?K`P6. mem0er5?9O`K8P6>.mem0er5?986. ^.intersect5O19 9$9,9#92P9O#9294919$P9X69)rite5X69nl.

O+)&+):

E%&"r'!"() No.:16
Fr')"
Pro*r ! Co,":
XYinteger. LYXc 0sort5list9list6. s)ap5list9list6. greater5element9element6. 0sort5L9S6>.s)ap5L9L169b90sort5L19S6. 0sort5S9S6. s)ap5OX9_K8P9O_9XK8P6>.greater5X9_6. s)ap5OeK8P9OeK81P6>.s)ap589816. greater5X9_6>. X \ _. ^.0sort5O 9#919+9$P9X69)rite5X6.

Pro*r ! )o '!&/"!"() B+11/" Sor).

O+)&+):

E%&"r'!"() No.:1B
Fr')"
Pro*r ! Co,":
XYinteger. LYXc insort5list9list6. insort(5element9list9list6. order5element9element6. insort5OP9OP6. insort5O?K8P9M6>.insort589N69insort(5?9N9M6. insort(5?9OAK8P9OAKMP6>.order5A9?69b9insort(5?989M6. insort(5?989O?K8P6. order5P9J6>.P[J. ^.insort5O 9#919+9$P9X69)rite5X6.

Pro*r ! )o '!&/"!"() I(."r)'o( Sor).

O+)&+):

E%&"r'!"() No.:1C
Fr')"
Pro*r ! Co,":
retractall50oard5`669 assert50oard5O`e19`e 9`e$9`e,9`e#9`e29`e49`e+9`e/P66. mar*5Player9O%K`P91916>.3ar5X69XYPlayer. mar*5Player9O`9%K`P9 916>.3ar5X69XYPlayer. mar*5Player9O`9`9%K`P9$916>.3ar5X69XYPlayer. mar*5Player9O`9`9`9%K`P919 6>.3ar5X69XYPlayer. mar*5Player9O`9`9`9`9%K`P9 9 6>.3ar5X69XYPlayer. mar*5Player9O`9`9`9`9`9%K`P9$9 6>.3ar5X69XYPlayer. mar*5Player9O`9`9`9`9`9`9%K`P919$6>.3ar5X69XYPlayer. mar*5Player9O`9`9`9`9`9`9`9%K`P9 9$6>.3ar5X69XYPlayer. mar*5Player9O`9`9`9`9`9`9`9`9%K`P9$9$6>.3ar5X69XYPlayer. mo3e5P9519169OX1K&P9OPK&P6>.3ar5X16. mo3e5P95 9169OX19X KK&P9OX19PK&P6>.3ar5X 6. mo3e5P95$9169OX19X 9X$K&P9OX19X 9PK&P6>.3ar5X$6. mo3e5P9519 69OX19X 9X$9X,K&P9OX19X 9X$9PK&P6>.3ar5X,6. mo3e5P95 9 69OX19X 9X$9X,9X#K&P9OX19X 9X$9X,PK&P6>.3ar5X#6. mo3e5P95$9 69OX19X 9X$9X,9X#9X2K&P9OX19X 9X$9X,9X#9PK&P6>.3ar5X26. mo3e5P9519$69OX19X 9X$9X,9X#9X29X4K&P9OX19X 9X$9X,9X#9X29PK&P6>.3ar5X46. mo3e5P95 9$69OX19X 9X$9X,9X#9X29X49X+K&P9OX19X 9X$9X,9X#9X29X49PK&P6>.3ar5X+6. mo3e5P95$9$69OX19X 9X$9X,9X#9X29X49X+9X/K&P9OX19X 9X$9X,9X#9X29X49X+9PK&P6>. 3ar5X/6. record5Player99X9_6>.retract50oard5-669mar*5Player9-9X9_69assert50oard5-66. )in5Oe19e 9e$K`P9P6>.e1YYP9e YYP9e$YYP. )in5O`9`9`e19e 9e$K`P9P6>.e1YYP9e YYP9e$YYP. )in5O`9`9`9`9`9`9e19e 9e$P9P6>.e1YYP9e YYP9e$YYP. )in5Oe19`9`e 9`9`9e$9`9`P9P6>.e1YYP9e YYP9e$YYP. )in5O`9e19`9`9e 9`9`9e$`P9P6>.e1YYP9e YYP9e$YYP. )in5O`9`9e19`9`9e 9`9`9e$P9P6>.e1YYP9e YYP9e$YYP. )in5Oe19`9`9`9e 9`9`9`9e$P9P6>.e1YYP9e YYP9e$YYP. )in5O`9`9e19`9e 9`9e$9`9`P9P6>.e1YYP9e YYP9e$YYP. open5Oe19e 9e$K`P9Player6>. 53ar5e16Ke1YYPlayer6953ar5e 6Ke YYPlayer6953ar5e$6Ke$YYPlayer6. open5O`9`9`9e19e 9e$K`P9Player6>.53ar5e16Ke1YYPlayer6953ar5e 6Ke YYPlayer6953ar5e$6K e$YYPlayer6. open5O`9`9`9`9`9`9e19e 9e$P9Player6>.53ar5e16Ke1YYPlayer6953ar5e 6Ke YYPlayer6953ar5e$6K e$YYPlayer6. open5Oe19`9`9e 9`9`9e$9`9`P9Player6>.53ar5e16Ke1YYPlayer6953ar5e 6Ke YYPlayer6953ar5e$6K e$YYPlayer6. open5O`9e19`9`9e 9`9`9e$`P9Player6>.53ar5e16Ke1YYPlayer6953ar5e 6Ke YYPlayer6953ar5e$6K e$YYPlayer6.

Pro*r ! )o '!&/"!"() T'0>-T 0>-To" G !".

open5O`9`9e19`9`9e 9`9`9e$P9Player6>.53ar5e16Ke1YYPlayer6953ar5e e$YYPlayer6. open5Oe19`9`9`9e 9`9`9`9e$P9Player6>.53ar5e16Ke1YYPlayer6953ar5e e$YYPlayer6. open5O`9`9e19`9e 9`9e$9`9`P9Player6>.53ar5e16Ke1YYPlayer6953ar5e e$YYPlayer6. open5O`9`9e19`9e 9`9e$9`9`P9Player6>.53ar5e16Ke1YYPlayer6953ar5e e$YYPlayer6.

6Ke YYPlayer6953ar5e$6K 6Ke YYPlayer6953ar5e$6K 6Ke YYPlayer6953ar5e$6K 6Ke YYPlayer6953ar5e$6K

3alue5-oard91116>.)in5-oard9o69b. 3alue5-oard91116>.)in5-oard9X69b. 3alue5-oard9E6>.findall5o9open5-oard9o69MAX69leng(t5MAX9Ema%6fglines open to o findall5%9open5-oard9%69MIN69leng(t5MIN9Emin69 fglines open to % E is Ema%.Emin. searc(5Position9Dept(95Mo3e9Talue66>. alp(a`0eta5o9Dept(9Position9.11191119Mo3e9Tlaue6. alp(a`0eta5Player919Position9`Alp(a9`-eta9`NoMo3e9Tlaue6>. 3alue5Positon9Talue69spy5Player9Position9Talue6. alp(a`0eta5Player9D9Position9Alp(a9-eta9Mo3e9Talue6>.D\19 findall55X9_69mar*5Player9Position9X9_69Mo3es69 Alp(a1 is .-eta9f ma%Umin -eta1 is .Alp(a9 D1 is D.19 e3aluate`and`c(oose5Player9Mo3es9Position9D19Alp(a19-eta19nil9Mo3e9Talue66. e3aluate`and`c(oose5Player9OMo3eKMo3esP9Position9Alp(a9-eta9&ecord9-est9Mo3e6>. mo3e5Player9Mo3e9Position9Position169 ot(er`Player5Player9"t(erPlayer69 ot(er`Player5"t(erPlayer9D9Position19Alp(a9-eta9`"t(erMo3e9Talue69 Talue1 is .Talue9 cutoff5Player9Mo3e9Talue19D9Alp(a9-eta9Mo3es9Position9&ecord9-estMo3e69 e3aluate`and`c(oose5`Player9OP9`Position9`D9Alp(a9`-eta9Mo3e95Mo3e9Alp(a66. cutoff5`Player9Mo3e9Talue9`D9`Alp(a9-eta9`Mo3es9`Position9`&ecord95Mo3e9Talue66>. Talue\Y-eta9b. cutoff5Player9Mo3e9Talue9D9Alp(a9-eta9Mo3es9Position9`&eacord9-estMo3e6>. Alp(a[Talue9Talue[-eta9b. e3aluate`and`c(oose5Player9Mo3es9Positon9D9Talue9-eta9Mo3e9-estMo3e6. cutoff5player9`Mo3e9Talue9D9Alp(a9-eta9mo3es9Position9&ecord9-estMo3e6>.TalueY[Alp(a9b9 e3aluate`and`c(oose5Player9Mo3es9Position9D9Alp(a9-eta9&ecord9-estMo3e6. ot(er`Player5o9%6. ot(er`Player5%9o6. spy5Player9Position9Talue6>.spy9b9 )rite5Player69 )rite5@ @69 )rite5Positin69 )rite5@ @69 )riteln5Talue6. spy5`9`9`6.fdo not(ing (5X9_6>.record5%9X9_69s(o)-oard. c>.0oard5-69alp(a`0eta5o9 9-9. 119 1195X9_69`Talue69record5o9X9_69 s(o)-oard. s(o)-oard>.0oard5Oe19e 9e$9e,9e#9e29e49e+9e/P69 )rite5@ @69mar*5e169)rite5@ @69mar*5e 69)rite5@ @69mar*5e$69nl9 )rite5@ @69mar*5e,69)rite5@ @69mar*5e#69)rite5@ @69mar*5e269nl9

)rite5@ @69mar*5e469)rite5@ @69mar*5e+69)rite5@ @69mar*5e/69nl. s>.s(o)-oard. mar*5X6>.3ar5X69)rite5@g6. mar*5X6>.Vd3ar5X69)rite5X6. connect5Port6>.tcp`soc*et5Soc*et69get(ostname5?ost69 f local (ost tcp`connect5Soc*et9?ost>Port69 tcp`open`soc*et5Soc*et9INs9"H8s69 assert5connnected&eadStream5INs669 assert5connected<riteStream5"H8s66. ttt>.connected&eadStream5IStream69read5IStream95X9_669record5%9X9_69 0oard5-69alp(a`0eta5o9 9-9. 119 1195H.T69`Talue69record5o9H9T69 connected<rit)Stream5"Stream69)rite5"Stream95H9T669 nl5"Stream69flus(`output5"Stream69ttt.

S-ar putea să vă placă și