1 / 20

Prolog

Prolog. An Introduction. Introduction. Prolog (Programming Logic) is used to store information in a knowledge base using a logical representation. Facts and rules called clauses are stored in the knowledge base. The information in the knowledge base is used to answer queries: p rove facts

galvin
Télécharger la présentation

Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog An Introduction

  2. Introduction • Prolog (Programming Logic) is used to store information in a knowledge base using a logical representation. • Facts and rules called clauses are stored in the knowledge base. • The information in the knowledge base is used to answer queries: • prove facts • answer questions

  3. Predicates • Must begin with a lowercase letter. • Cannot begin with an underscore. • Arguments are separated by commas. • Arguments can be variables or constants. • Constants must begin with a lowercase letter and cannot begin with an underscore. • Variables are unified against constants and other variables.

  4. Example 1: Parent Tree

  5. Example 2 A number of friends are at a party. Bill likes Jean who enjoys reggae. Diane, who enjoys rock, likes Colin. Janet likes Ian who enjoys heavy-metal. The smokers amongst this group are Diane and Ian. Sam gives Diane a gin and tonic to drink. He also gives Colin a cola and Jean a white wine.

  6. Using SWI-Prolog • Command prompt - :- • Runtime knowledge base. • Listing the clauses currently in memory – type listing. at the command prompt. • Permanent knowledge base – create/edit a file. • Setting the editor: setenv(‘editor’, notepad). • Adding and removing clauses and rules: • assert • retract

  7. Queries • Are typed at the command prompt. • Uses performs a depth-first search of the possible options for the query. • Proving or disproving a fact : Prolog responds with a yes or no. • Answering a question: Prolog responds with an answer or a no. • Using the Prolog trace option: trace.

  8. Example: Parent Knowledge Base • Is Bill Jean’s parent? • Who are Bob’s parents? • Who are Jim’s children? • Who are Bob’s children? • Who is a parent and who are their children?

  9. Conjunctions • Conjunctions are used to combine queries and perform the function of the AND operator. • A comma (,) represents the AND operation. • Example query: Does Diane like Colin and enjoy rock: likes(diane, colin), enjoys(diane, rock). . • All clauses in the query must hold for Prolog to respond with a yes.

  10. Disjunctions • Perform the function of the OR operator. • The OR operator is represented by a semicolon (;). • Prolog returns a yes if a match is found for at least one of the predicates. • Prolog returns a no if a match is not found for any of the predicates.

  11. Rules • Are composed of conclusions and requirements. • Syntax: <conclusion> :- <requirements>. • The <conclusion> is called the head of the rule. • The <requirements> is called the tail of the rule. • The head can consist of only one predicate. • The tail can consist of one or more predicates separated by conjunctions of disjunctions. • Example: sky(blue):-weather(fair).

  12. Example 1: • Weather knowledge base. • Rule: If the weather is fair on a particular day the colour of the sky will be blue on that day. • Query: On which days of the week will the sky be blue?

  13. Example 2: Parent Knowledge Base • Add the following rule to the knowledge base: Y is an offspring of X, if X is Y’s parent. • Query: Is Liz Tom’s offspring?

  14. Example 3: Party Knowledge Base • Add the following rules: • Janet will get ill if Janet smokes. • Jean will be Ian’s partner if Ian enjoys heavy metal. • Janet will be Sam’s partner if Sam enjoys heavy metal and Sam likes Bill. • Someone is unhealthy if they smoke. • Someone is popular if anyone likes them. • Everybody who enjoys some kind of music and smokes should partner Diane in a dance. • Query: Who is popular?

  15. Example 4: Weather Knowledge Base • Add the following facts and rules: • If the weather is overcast the sky is grey. • Birds are active on Sunday, Tuesday and Thursday. • Rare birds are observed on Wednesday and Friday. • Bird watchers will be happy on a particular day of the week if the weather is fair and the birds are active. • Alternatively, bird watchers will be happy if they observe a rare bird on a particular day. • Query: On which days will bird watchers be happy?

  16. Terminology Revisited • Facts and rules are defined in terms of predicates. • Facts and rules in the knowledge base are called clauses. • Clauses consist of a number of data objects. • Data objects are simple objects and structures (e.g. a date). • Simple objects are constants and variables. • Constants are atoms and numbers.

  17. The Anonymous Variable • Is denoted by an underscore (_). • The anonymous variable is used in queries in cases where values unified with variables do not need to be known. • Given the Parent knowledge base, what will Prolog’s response be to the following queries: • parent(X, _). • parent(_,_).

  18. Negation • The not operator is used to represent negation. • The operator is used in queries and rules. • Example predicate: not(smokes(diane)). • Application of not: Prolog firstly evaluates the predicate and reverses the result. • Example rule: • partner(janet, X):-enjoys(X,rock), not(smokes(X)). • Query: partner(janet,Who).

  19. Recursive Rules • A “stopping” rule must be included for each recursive rule. • Bessy example: • Query: owned(bessy, Who). • Predecessor example: • Query 1: Is Pam a predecessor of Ann? • Query 2: Who are Bob’s predecessors?

  20. How Does Prolog Work?

More Related