Prolog Prolog: Programming in Logic Prolog programs consist of clauses and rules. Declarative programming –Emphasis is on data and relations between objects. –No familiar procedural controls. –Built-in inference engine.
Where is declarative programming useful? Data-driven programming tasks –Verification –Natural Language –Expert Systems –Relational Databases –Diagnostic Systems
Logical Operators ABA^B AND: Conjunction ABAvB OR: Disjunction NOT: Inversion A!A 01 10
Logical Operators ABA->B Implication A->B is equivalent to !AvB ABA B Equivalence A B is equivalent to: A->B ^ B->A (A ^ B) v (!A ^ !B)
Propositional Calculus Represent facts as symbols. –Homer_likes_beer. –Lisa_likes_tofu. –!(Bart_likes_school) –Bart_likes_school -> Bart_does_homework. –Lisa_likes_school ^ Lisa_likes_music Easy, but overly verbose. No reuse.
Predicate Calculus A predicate is a function that maps from zero or more objects to {T,F}. –Likes(Homer, beer) –!Likes(Bart, school) –Likes(Bart, school) -> Does(Bart, homework). –Happy(Lisa). –Gives(Moe, Homer, beer) -> Happy(Homer). Predicates provides a syntactic shorthand for propositions. No extra representational power.
Inference Rules AND-elimination: –If A^B is true, then A is true. –likes(Homer,beer) ^ likes(Homer, food)->likes(Homer,beer) OR-introduction: –If A is true, then AvB is true. –likes(Homer,food)->likes(Homer,food)v likes(Homer,work). Modus Ponens: –If A->B is true, and A is true, then B is true. –likes(Lisa,school)->does(Lisa,homework) ^ likes(Lisa,school) -> Does(Lisa,homework)
Quantification We would like to be able to express facts such as: –“Everyone who likes school does their homework.” –“No one who likes beef also likes tofu.” –“Someone likes beef.” This requires us to be able to quantify over variables in the domain.
First-Order Logic First order logic allows quantification over objects. Universal quantification: “For all x” x likes(x,school) -> does(x, homework). “Everyone who likes school does their homework.” Existential quantification: “There exists an x” x likes(x,tofu). “There exists someone who likes tofu.”
Computational Issues Full FOL is too computationally difficult to work with. –Nested quantifiers are semi-decidable. x y z likes(x,y) ^ likes(y,z). “Everyone likes a person who likes everyone.” –Implications with an AND in the consequent are exponentially hard. x likes(x, school) -> does(x,homework) ^ attends(x,class) “Everyone who likes school does their homework and attends class.”
Horn clauses Horn clauses are implications of the form: x ^ y ^ z -> a Only a single term in the consequent. Horn clause inference is computationally tractable. Prolog uses Horn clauses.
Knowledge Representation in Prolog Facts (unit clauses): –likes(homer, beer). Constants are lower case. Names of relations are lower case. Conjunctions: –likes(homer,beer). –likes(homer,food). Statement is represented as separate Prolog clauses. <- ends with a period
Knowledge Representation in Prolog Rules –does(lisa,homework) :- likes(lisa,school). –Equivalent to: likes(lisa,school) ->does(lisa,homework) –Read as: “Lisa does homework if she likes school.” or … –“To prove that Lisa does homework, prove that she likes school.”
Variables Variables are all upper-case. Universal quantification is achieved by using variables in rules. –does(X,homework) :- likes(X,school). “Everyone who likes school does their homework.” “To prove that someone does their homework, prove that they like school.”