310 likes | 683 Vues
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
E N D
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: • prove facts • answer questions
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.
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.
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
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.
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?
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.
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.
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).
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?
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?
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?
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?
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.
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(_,_).
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).
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?