230 likes | 481 Views
Introduction to Prolog. CS321. Brief History. The first, official version of Prolog was developed at the University of Marseilles, France by Alain Colmerauer in the early 1970s as a tool for PROgramming in LOGic. . Application Areas. Prolog has been a very important tool in
E N D
Introduction to Prolog CS321
Brief History • The first, official version of Prolog was developed • at the University of Marseilles, France • by Alain Colmerauer • in the early 1970s • as a tool for PROgramming in LOGic.
Application Areas • Prolog has been a very important tool in • artificial intelligence applications • expert systems • Updated versions of Prolog are now being used for • natural language interfaces • smart information management systems
Declarative Language • This means that • The programmer • declares facts • defines rules for reasoning with the facts • Prolog uses deductive reasoning to • determine new facts from old • decide whether a proposed fact (goal) can be logically derived from known facts (such a decision is called a conclusion)
Predicate • In classic logic, a declarative statement (fact) is called a predicate. • The next few slides review relevant classic logic, as related to logic programming
Predicates • A predicate is • decidable (true or false) • A sentence that can be put in the place of “…” in the sentence Is it true that “…”? • Examples: The sky is red. I am honest.
Primitive, Compound • A predicate is a primitive statement • it cannot be broken down into simpler form • can be represented by a single symbol p: I am a computer science major. q: My printer is not working today. • Predicates can be combined into compound statements with logical connectors
Logical Connectors • Negation ! q • Conjunction • p and q • Disjunction (inclusive, one or both) • p or q • Disjunction (exclusive, one or the other) • p or q , but not both
Implication (rule) • An if…then “relationship” between predicates p :- q can mean any of • “ if p is true, then q is also true” • “q is necessary for p” • “ p only if q’ Note: this is not necessarily a “cause and effect” type of relationship • p is the hypothesis (premise) and q is the conclusion
Arguments • Central to the notion of logic is an argument: … a set of predicates used as hypotheses to justify a conclusion • Example: If Socrates is a man then Socrates is mortal. Socrates is a man. Therefore, Socrates is mortal.
Argument Structure • A (modus ponens) argument structure can be represented as: If Ssss is an xxxxx then Ssss is a yyyyy. Ssss is an xxxxx. Therefore, Ssss is a yyyyy. • We can judge the validity of the argument by its structure (form), rather than by its meaning.
Formalizing Arguments • Abstracting with symbols for predicates, we get an argument form that looks like this: if p then q p therefore q • Thus, the formal representation ((p :- q) p) q
Predicate Logic • When we look at predicates abstractly, we see that the correctness of an argument is not governed by its content (meaning), but by its logical form. • There are rules (procedures) to decide whether or not a given argument is acceptable. (Rules of Inference)
Rules of Inference • Modus Ponens • If p then q, p, therefore q • Law of Syllogism • p, then q, q then rp, therefore r • Modus Tollens (mode that denies) • If P, then Q. Q is false. therefore P is false. • Rule of Conjunction • If p and q, therefore p or q
Logic Programming Language • Prolog is a language where the language implementation is a collection of procedures (functions) based on the rules of inference in classical logic Modus Ponens Law of Syllogism Modus Tollens Rule of Conjunction
Inference Engine • The procedures for reasoning logically about facts are combined with a pattern matcher to create the inference engine • A pattern matcher • retrieves stored facts by matching answers to questions. • infers that a hypothesis is true by questioning the set of facts already known to be true
Prolog program • Prolog's known world is the finite set of facts (and rules) that are given in a program. • Typically, a Prolog program isn't a sequence of actions--it's a list of facts together with inference rules. • The Prolog system arrives at conclusions by logically inferring new facts from known facts.
Writing Predicates in Prolog • First eliminate all unnecessary words from your sentences. • Then transform the sentence, placing the relationship first and grouping the objects after the relationship. • The objects then become arguments that the relationship acts upon. • See examples, next slide
Examples Natural Language: Predicate Logic: A car is fun. fun(car). A rose is red. red(rose). Bill likes a car likes(bill,Car)if fun(Car). if the car is fun.
Facts • A Fact declares a relation between objects likes(bill, cindy). likes(cindy, bill). likes(bill, dogs). • Facts can also express properties of objects green(kermit). girl(caitlin).
Rules • Rules enable you to infer facts from other facts. • Another way to say this is: a rule states a way to conclude a new fact -- if one or more other facts can be shown to be true. Cindy likes everything that Bill likes. Caitlin likes everything that is green.
FACTS: likes(bill, cindy). likes(cindy, bill). likes(bill, dogs). green(kermit). girl(caitlin). RULES: Cindy likes everything that Bill likes. Caitlin likes everything that is green. What new facts can we infer from the known facts and rules? Cindy likes Cindy. Caitlin likes Kermit. Inference