Introduction to Prolog Programming: Facts, Rules, and Queries

Slide Note
Embed
Share

Prolog is a powerful logic programming language where programs are built around a knowledge base of facts and rules. It involves three basic constructs: facts (unconditionally true statements), rules (conditionally true statements), and queries to extract information. Creating a knowledge base, querying it, and inferring results are key aspects of Prolog programming. The environment setup, file creation, and consulting process in Prolog are outlined in this introductory lecture.


Uploaded on Sep 11, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSC 270 Survey of Programming Languages Prolog Lecture 1 Facts, Rules, and Queries

  2. Basic Constructs of Prolog Prolog has only three basic constructs: Facts statements that are unconditionally true Rules statements that are conditionally true Queries questions pertaining to the information stored in a knowledge base A knowledge base is a collection of facts and rules Writing a program in Prolog is all about creating the knowledge base.

  3. Knowledge Base #1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(yolanda). party.

  4. Our Prolog Environment Panther swipl Create files using notepad. Consult the file using [filename without.pl]. For kb1.pl use [kb1]. Ctrl d to exit OR exit command SWI-Prolog on windows or mac http://www.swi-prolog.org/Download.html

  5. Querying Knowledge Base #1 1 ?- % c:/documents and settings/adelphi/my documents/prolog/kb1 compiled 0.00 sec, 6 clauses 1 ?- woman(mia). true. 2 ?- playsAirGuitar(jody). false. 3 ?- playsAirGuitar(mia). false. 4 ?- playsAirGuitar(vincent). false.

  6. 5 ?- tatooed(jody). ERROR: toplevel: Undefined procedure: tatooed/1 (DWIM could not correct goal) 6 ?- party. true. 7 ?- rockConcert. ERROR: toplevel: Undefined procedure: rockConcert/0 (DWIM could not correct goal) 8 ?-

  7. Infer (or Deduce) Prolog determines that Vincent doesn t play Air Guitar because there is no fact from which it can infer (or deduce). The error messages for tatooed and rockConcert are due to the fact that SWI Prolog did not find these terms when compiling the knowledge base.

  8. Knowledge Base #2 Add 3 rules: happy(yolanda). listens2Music(mia). listens2Music(yolanda) :- happy(yolanda). playsAirGuitar(mia) :- listens2Music(mia). playsAirGuitar(yolanda) :- listens2Music(yolanda). listens2Music(amy) :- happy(amy). If Yolanda is happy then she listens to music Left is head, right is body Prolog can infer the head from the body being true 3 rules, 2 facts, 5 clauses Predicates or procedures: happy, listens2Music and playsAirGuitar

  9. Querying Knowledge Base #2 9 ?- playsAirGuitar(mia). true. 10 ?- playsAirGuitar(yolanda). true. 11 ?- listens2Music(amy). False.

  10. Knowledge Base #3 This knowledge base consists of 2 facts and 3 rules. happy(vincent). listens2Music(butch). playsAirGuitar(vincent):- listens2Music(vincent),happy(vincent). playsAirGuitar(butch):-happy(butch). playsAirGuitar(butch):-listens2Music(butch). Translation: If vincent is happy and vincent listens2music, then vincent plays air guitar.

  11. Running Knowledge Base #3 11 ?- "playsAirGuitar(vincent)"? false. 12 ?- This is because while KB3 contains happy(vincent) , it does not explicitly contain the information listens2Music(vincent) , and this fact cannot be deduced either.

  12. Running Knowledge Base #3 (continued) 12 ?- playsAirGuitar(butch). true. 13 ?- The knowledge base gives us two ways of deducing this through two different rules: playsAirGuitar(butch):-happy(butch). playsAirGuitar(butch):-listens2Music(butch). This is effectively equivalent to an OR.

  13. Knowledge Base #4 7 facts woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). -- read as vincent loves mia

  14. Prolog Variables Capital letters start a variable name Actually a placeholder to lookup a match Ex : woman(X). Tell me which individual you know to be a woman Returns the first occurrence Press ; for each additional match X = mia ; X = jody ; X = yolanda ; X = honey-bumpkin ;

  15. Querying Knowledge Base #4 17 ?- loves(marsellus, X). -- who loves marsellus? X = mia. 18 ?- loves(marsellus, X), woman(X). -- who loves marsellus and is a woman?; requires unification of two facts with the matching X X = mia.

  16. Knowledge Base #5 A rule with a variable: loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). jealous(X,Y):- loves(Y,Z). loves(X,Z),

  17. Running Knowledge Base #5 20 ?- jealous(marsellus, W). W = vincent -- any other jealous people? -- trace. -- stop with notrace.

  18. Prolog Syntax Facts, rules and queries are built from terms. There are four types of terms in Prolog: atoms numbers variables complex terms (or structures) Atoms and numbers are constants and constants and variables make up the simple terms of Prolog.

  19. Atoms An atom is either: 1. A string of uppercase letters, lowercase letters, underscores and digits that begin with a lowercase letter. (E.g., butch, big_kahuna, etc.) 2. An arbitrary sequence of characters inside single quotes (E.g., Vincent , The Gimp , ((*&^*&^*&^ ) 3. A string of special characters (E.g., @= , ===>, ;, and :-)

  20. Numbers The typical Prolog application does not make heavy use of numbers. Most Prolog implmentations support floating point numbers and integers.

  21. Variables A variable is a string of uppercase letters, lowercase letters, digits and underscores that starts with either an uppercase letter or an underscore. (E.g., X, Y, Variable, _tag, X_526, List, List24, etc.) _ is a special variable and it is called the anonymous variable.

  22. Complex Terms We can combine atoms, numbers and variables to form complex terms (also called structures). Complex terms are built out of a functor followed by a sequence of arguments. Examples playsAirGuitar(jody). loves(vincent, mia). hide(X, father(father(father(butch)))).

  23. Recursive Structure Nesting complex terms inside complex terms indefinitely leads to recursive structures.

  24. Arity Arity refers to the number of arguments that a functor has. It becomes important when the same functor can have more than one different count of arguments (or different arities).

Related


More Related Content