PROLOG: Fact Roles And Queries In Prolog


Published on

PROLOG: Fact Roles And Queries In Prolog

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PROLOG: Fact Roles And Queries In Prolog

  1. 1. Facts, Rules and Queries in Prolog<br />
  2. 2. overview<br />Introduction<br />Simple examples<br />Prolog Syntax<br />Atoms<br />Numbers<br />Variables<br />Complex terms<br />
  3. 3. Facts Roles and Queries<br />There are only three basic constructs in Prolog: facts, rules, and queries. <br />Prolog programming is all about writing knowledge bases.<br />A collection of facts and rules is called a knowledge base (or a database) <br />
  4. 4. Knowledge Base 1<br />Knowledge Base 1 (KB1) is simply a collection of facts. <br />Facts are used to state things that are unconditionally true of the domain of interest.<br />Ex: woman(mia).<br /> woman(jody).<br /> woman(yolanda).<br /> playsAirGuitar(jody).<br /> state that Mia, Jody, and Yolanda are women, and Jody plays air guitar.<br />
  5. 5. Knowledge Base 1 <br />KB1 can be used by posing queries.<br />Ex: <br />We can ask Prolog whether Mia is a woman by posing the query:<br />?- woman(mia).<br />Yes<br />we can ask whether Jody plays air guitar by posing the following query:<br />?- playsAirGuitar(jody).<br />Yes<br />
  6. 6. Knowledge Base 2 <br />Ex:listensToMusic(mia).<br /> happy(yolanda).<br /> playsAirGuitar(mia) :- listensToMusic(mia).<br /> playsAirGuitar(yolanda) :- listensToMusic(yolanda).<br /> listensToMusic(yolanda):- happy(yolanda).<br /><ul><li>KB2 contains two facts: listensToMusic(mia) and happy(yolanda).
  7. 7. The last three</li></ul> items are rules.<br />Rules state information that is conditionally true of the domain of interest.<br />
  8. 8. Knowledge Base 2 <br />:- should be read as ``if'', or``is implied by''.<br />The part on the left hand side of the :- is called the head of the rule.<br />The part on the right hand side is called the body.<br />Ex: We can ask Prolog whether Mia plays air guitar:<br />?- playsAirGuitar(mia) Yes<br />
  9. 9. Knowledge Base 3 <br />KB3 consists of five clauses:<br />happy(vincent).<br />listensToMusic(butch).<br />playsAirGuitar(vincent):-<br /> listensToMusic(vincent),<br /> happy(vincent).<br />playsAirGuitar(butch):-<br /> happy(butch).<br />playsAirGuitar(butch):-<br /> listensToMusic(butch).<br />There are two facts, namely happy(vincent) and listensToMusic(butch), and three<br />rules.<br />
  10. 10. Knowledge Base 4<br />KB4 contains no rules, only a collection of facts.<br />woman(mia).<br />woman(jody).<br />woman(yolanda).<br />loves(vincent,mia).<br />loves(marcellus,mia).<br />loves(pumpkin,honey_bunny).<br />loves(honey_bunny,pumpkin).<br />
  11. 11. Knowledge Base 4<br />Ex: if we query ?- woman(X).<br />Prolog reports back to us as follows:<br />X = mia <br />The first item in the knowledge base is woman(mia). <br />So, Prolog matches X to mia, thus making the<br />query agree perfectly with this first item.<br />
  12. 12. Knowledge Base 5<br />Ex:<br />loves(vincent,mia).<br />loves(marcellus,mia).<br />loves(pumpkin,honey_bunny).<br />loves(honey_bunny,pumpkin).<br />jealous(X,Y) :- loves(X,Z),loves(Y,Z).<br /><ul><li>KB5 contains four facts about the loves relation and one rule.</li></li></ul><li>Knowledge Base 5<br />In the previous example, It says that an individual X will be jealous of an individual Y if there is some individual Z that X loves, and Y loves that same individual Z too.<br />Ex: ?- jealous(marcellus,W).<br /><ul><li>This query asks: can you find an individual W such that Marcellus is jealous of W?</li></ul>Prolog will return the value W = vincent<br />
  13. 13. Prolog syntax<br />There are four kinds of terms in Prolog: <br />atoms, numbers, variables, and complex terms (or structures). <br />Atoms and numbers are lumped together under the heading constants, and constants and variables together make up the simple terms of Prolog.<br />The upper-case letters are A, B, ..., Z; <br />The lower-case letters are a, b, ..., z; <br />The digits are 1, 2, ..., 9;<br />The special characters are +, -, *, /, <, >,=, :, ., &, ~, and _.<br />The _ character is called underscore. <br />A string is an unbroken sequence of characters.<br />
  14. 14. ATOMS<br />An atom is either:<br /><ul><li>A string of characters made up of upper-case letters, lower-case letters, digits, and the underscore character, that begins with a lower-case letter.
  15. 15. An arbitrary sequence of character enclosed in single quotes. </li></ul>Ex: 'Vincent','The Gimp', ' A string of special characters. Five_Dollar_Shake', '&^%&#@$ &*', and ' '.<br /><ul><li> For example: @= and ====> and ; and :- are all atoms. </li></li></ul><li>NUMBERS<br />Prolog implementations support<br /><ul><li> Floating point numbers or floats (that is, representations of real numbers such as 1657.3087 or )
  16. 16. Integers (that is: ... -2, -1, 0, 1, 2, 3, ...) are useful for such tasks as counting the elements of a list.</li></li></ul><li>VARIABLES<br />A variable is a string of :<br />upper-case letters<br />lower-case letters<br />digits <br />underscore characters that starts either with an upper-case letter or with underscore.<br />
  17. 17. COMPLEX TERMS<br />Complex terms are build out of a functor followed by a sequence of arguments.<br /> The arguments are put in ordinary brackets, separated by commas, and placed after the functor.<br />The functor must be an atom. That is, variables cannot be used as functors. <br />Arguments can be any kind of term.<br />Ex: in the previous examples<br />playsAirGuitar(jody) is a complex term: <br />its functor is playsAirGuitar<br />and its argument is jody.<br />
  18. 18. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />