Chapter 12 Logic Languages Programming Language Pragmatics Michael

  • Slides: 32
Download presentation
Chapter 12 : : Logic Languages Programming Language Pragmatics Michael L. Scott Copyright ©

Chapter 12 : : Logic Languages Programming Language Pragmatics Michael L. Scott Copyright © 2009 Elsevier

Announcements • Essay is due next Wednesday • Next HW over prolog will be

Announcements • Essay is due next Wednesday • Next HW over prolog will be out next week, and due last day of class – We’ll have 5 -ish lectures on prolog – Then a few “random topics”, not on hw (but look for review questions, and one WILL be on the final) • No class this Friday or next Monday. • Review is last day of class. • Final exam is 8 am on Wednesday. (Yes, I know, and no, I can’t change it!) – Question: any conflicts?

Logic Programming • One “other” principle declarative paradigm • Based in logic, which most

Logic Programming • One “other” principle declarative paradigm • Based in logic, which most of us see primarily in architecture and digital circuits • Arose as a programming paradigm in the 1970’s – due largely to the work of Cordell Green, Bertram Raphael, and others • Based on the methods of proof and logical inference, so appealing from the correctness standpoint – Real origins are in predicate calculus, in the same way that functional programming is based on lambda Copyright © 2009 Elsevier calculus

Prolog: uses today • Historically, prolog was useful in the AI community • Today,

Prolog: uses today • Historically, prolog was useful in the AI community • Today, still around, if a bit less common: – – – Building games Used to build an online CAD editor Semantic web development Natural language processing Rule based systems – finding results, especially for customers that want to draw a graphical set of logic rules, and then prolog can apply things – In general, good for enumeration of all possible solutions to a question. Copyright © 2009 Elsevier

Logic Programming • Based on predicate calculus • Predicates - building-blocks P(x 1, x

Logic Programming • Based on predicate calculus • Predicates - building-blocks P(x 1, x 2, . . . , x. K) – limit(f, infinity, 0) – enrolled(you, CS 3200) • These are simply true/false statements that are based on their inputs. Copyright © 2009 Elsevier

Logic Programming Concepts • Operators: conjunction, disjunction, negation, implication – – P(x) and Q(x)

Logic Programming Concepts • Operators: conjunction, disjunction, negation, implication – – P(x) and Q(x) P(y) or Q(z) Not P(x) implies Q(x) • You saw these (and more) in discrete math, and probably in other places where working on architecture or truth tables. – If you’re panicking now, I recommend going back and reading that section on logic! Won’t need much, but basic familiarity is helpful. Copyright © 2009 Elsevier

Logic Programming Concepts • Universal and existential quantifiers – For all x, P(x): true

Logic Programming Concepts • Universal and existential quantifiers – For all x, P(x): true if P(x) holds for every x in the “universe” – There exists an x, P(x): true if a single x exists in the “universe” with P(x) being true • Remember, these are connected: – Not (for all x, P(x)) = there exists an x, not P(x) – Not (there exists an x, P(x)) = for all x, not P(x) • (All sorts of rules and laws on these also…) Copyright © 2009 Elsevier

Logic Programming Concepts • Statements – sometimes true, sometimes false, often unknown – axioms

Logic Programming Concepts • Statements – sometimes true, sometimes false, often unknown – axioms - assumed true – theorems - provably true – hypotheses (goals) - things we'd like to prove true • We’ve used truth tables, logical implications, and all those laws to derive true logical statements. Copyright © 2009 Elsevier

Logic Programming Concepts • Familiar example statements (right? ): all f, l [ limit(f,

Logic Programming Concepts • Familiar example statements (right? ): all f, l [ limit(f, x 0, l) <=> (all e [ e > 0 => (exists d [ d > 0 and all x [ ((|x-x 0| < d) => (|f(x)-l|) < e)]])])] all f, g [f = O(g) <= (exist c, n 0 [ all n [ n > n 0 => f(n) < cg(n)]])] Copyright © 2009 Elsevier

Logic Programming Concepts • Most statements can be written many ways • That's great

Logic Programming Concepts • Most statements can be written many ways • That's great for people but a nuisance for computers • It turns out that if you make certain restrictions on the format of statements you can prove theorems mechanically – That's what logic programming systems do • Unfortunately, the restrictions that we will put on our statements will not allow us to handle most of theorems you learned in math, but we will have a surprising amount of power left anyway Copyright © 2009 Elsevier

Logic Programming Concepts • We insist that all statements be in the form of

Logic Programming Concepts • We insist that all statements be in the form of HORN CLAUSES – Consists of a head and a body – Example: H <- B 1, B 2, …, Bn means: “if B 1 and B 2 and…Bn are true, then H is true” – So “, ” is the “and”, and <- is an implication – Each Bi can be a constant or can be a predicate Copyright © 2009 Elsevier

Logic Programming Concepts • Structures consists of a functor plus a list of arguments.

Logic Programming Concepts • Structures consists of a functor plus a list of arguments. • Functors look like function calls, but they are not! (They also aren’t the same as functors in Haskell. ) • These are all true/false statements or predicates, asserting some fact. Examples: – rainy(rochester) – teaches(chambers, cs 3200) – rainy(stlouis) • Any file begins with a list of these facts to start from. Copyright © 2009 Elsevier

Logic Programming Concepts • The meaning of the statement is that the conjunction of

Logic Programming Concepts • The meaning of the statement is that the conjunction of the terms in the body implies the head – A clause with an empty body is called a FACT – A clause with an empty head is a QUERY, or top-level GOAL – A clause with both sides is a RULE • The Prolog interpreter has a collection of facts and rules in its DATABASE – Facts are axioms - things the interpreter assumes to be true Copyright © 2009 Elsevier

Prolog atoms • Prolog runs in the context of having a database of information,

Prolog atoms • Prolog runs in the context of having a database of information, from which it can infer other things • Examples: – Atom: foo, my_const, +, ‘Hello’ – Numbers: 2, 5. 1, etc – Variable: Foo, X, My_var • There are no declarations • All types are discovered implicitly Copyright © 2009 Elsevier

Prolog structures • Structures in prolog consists of functors and a list of arguments:

Prolog structures • Structures in prolog consists of functors and a list of arguments: – rainy(stlouis) – teaches(chambers, cs 344) • Syntax: parens right after the functor, with no space • The arguments can be constants, variables or nested structures. • These structures are often called predicates (like in logic) with arity = the number of arguments. Copyright © 2009 Elsevier

Prolog • Clauses are either facts or rules. • Facts: – rainy(rochester). – cold(rochester).

Prolog • Clauses are either facts or rules. • Facts: – rainy(rochester). – cold(rochester). – rainy(seattle) • Rules: – snowy(X) : - rainy(X), cold(X). – The : - is implication, and , is an and. • Query: – ? - snowy(A) • Given these facts and rules along with this query, Prolog would return A = rochester Copyright © 2009 Elsevier

Prolog • Given this: – – rainy(rochester). cold(rochester). rainy(seattle) snowy(X) : - rainy(X), cold(X).

Prolog • Given this: – – rainy(rochester). cold(rochester). rainy(seattle) snowy(X) : - rainy(X), cold(X). • Query: ? - rainy(B) • Prolog will return B = rochester (since it goes in order in the database). • Type semicolon to ask prolog to continue: – B = rochester ; – B = seattle ; – No Copyright © 2009 Elsevier

Prolog • Prolog can be thought of declaratively or imperatively: – We’ll emphasize the

Prolog • Prolog can be thought of declaratively or imperatively: – We’ll emphasize the declarative semantics for now, because that's what makes logic programming interesting – We'll get into the imperative semantics later • Prolog allows you to state a bunch of axioms – Then you pose a query (goal) and the system tries to find a series of inference steps (and assignments of values to variables) that allow it to prove your query starting from the axioms • Essentially, it is building up a proof for you, following the atoms and rules you began with. Copyright © 2009 Elsevier

Prolog • Rules are theorems that allow the interpreter to infer things • To

Prolog • Rules are theorems that allow the interpreter to infer things • To be interesting, rules generally contain variables employed(X) : - employs(Y, X). can be read: for all X, X is employed if there exists a Y such that Y employs X • Note the direction of the implication: – The example does NOT say that X is employed ONLY IF there is a Y that employs X Copyright © 2009 Elsevier

Prolog • The scope of a variable is the clause in which it appears

Prolog • The scope of a variable is the clause in which it appears – Variables whose first appearance is on the left hand side of the clause have implicit universal quantifiers – Variables whose first appearance is in the body of the clause have implicit existential quantifiers • Similarly: Copyright © 2009 Elsevier

Prolog grandmother(A, C) : - mother(A, B), mother(B, C). can be read: for all

Prolog grandmother(A, C) : - mother(A, B), mother(B, C). can be read: for all A, C [A is the grandmother of C if there exists a B such that A is the mother of B and B is the mother of C]. We probably want another rule that says grandmother(A, C) : - mother(A, B), father(B, C). Copyright © 2009 Elsevier

Prolog • To run a Prolog program, one asks the interpreter a question –

Prolog • To run a Prolog program, one asks the interpreter a question – This is done by stating a theorem - asserting a predicate - which the interpreter tries to prove • If it can, it says yes • If it can't, it says no • If your predicate contained variables, the interpreter prints the values it had to give them to make the predicate true. Copyright © 2009 Elsevier

Prolog • The interpreter works by what is called BACKWARD CHAINING – It begins

Prolog • The interpreter works by what is called BACKWARD CHAINING – It begins with the thing it is trying to prove and works backwards looking for things that would imply it, until it gets to facts • It is also possible in theory to work forward from the facts trying to see if any of the things you can prove from them are what you were looking for - that can be very timeconsuming – Fancier logic languages use both kinds of chaining, with special smarts or hints from the user to bound the searches Copyright © 2009 Elsevier

Prolog • The predicate you ask for is the interpreter's original GOAL – In

Prolog • The predicate you ask for is the interpreter's original GOAL – In an attempt to SATISFY that goal, it looks for facts or rules with which the goal can be UNIFIED – Any variables that do not yet have values but which correspond to constants or to variables with values in the other clause get INSTANTIATED with that value – Anyplace where uninstantiated variables correspond, those variables are identified with each other, but remain without values Copyright © 2009 Elsevier

Prolog • The interpreter starts at the beginning of your database (this ordering is

Prolog • The interpreter starts at the beginning of your database (this ordering is part of Prolog, NOT of logic programming in general) and looks for something with which to unify the current goal – If it finds a fact, great; it succeeds – If it finds a rule, it attempts to satisfy the terms in the body of the rule depth first – This process is motivated by the RESOLUTION PRINCIPLE, due to Robinson: • It says that if C 1 and C 2 are Horn clauses, where C 2 represents a true statement and the head of C 2 unifies with one of the terms in the body of C 1, then we can replace the term in C 1 with the body of C 2 to obtain another statement that is true if and only if C 1 is true Copyright © 2009 Elsevier

Prolog: Resolution and Unification • Example: takes(jane, cs 344). takes(jane, math 266). takes(alice, phil

Prolog: Resolution and Unification • Example: takes(jane, cs 344). takes(jane, math 266). takes(alice, phil 205). takes(alice, cs 344). classmates(X, Y) : - takes(X, Z), takes(Y, Z). • Now, let X be jane and Z be cs 344, we can replace the first term on righthand side of the last clause (C 1) with the (empty) body of the first clause above (C 2), giving: classmates(jane, Y) : - takes(Y, cs 344). • This is essentially pattern matching. Associating X with jane and Z with cs 344 is called unification, and the variables are said to be instantiated. Copyright © 2009 Elsevier

Prolog: Resolution and unification • So unification in prolog is applying the resolution principle,

Prolog: Resolution and unification • So unification in prolog is applying the resolution principle, and pattern matching things into appropriate spots. • Unification rules: – A constant only unifies with itself – Two structures unify if they have the same functor and arity, and the corresponding arguments unify recursively – A variable unifies with anything. If the other thing has a value, then the variable is instantiated. If the other thing is an uninstantiated variable, then the two are associated so that later values will be shared. Copyright © 2009 Elsevier

Prolog: Equality • Equality is defined in terms of unifiability. • Example: =(A, B)succeeds

Prolog: Equality • Equality is defined in terms of unifiability. • Example: =(A, B)succeeds if and only if A and B can be unified • Example: ? - a = a. Yes ? - a = b. No ? - foo(a, b) = foo(a, b). Yes ? - X=a. X = a; No Copyright © 2009 Elsevier

Prolog: Equality • Equality with variables is a bit different; this unifies the variables

Prolog: Equality • Equality with variables is a bit different; this unifies the variables without actually instantiating them. • Example: ? - A = B, A = x, B = Y. A = x B = x Y = x Copyright © 2009 Elsevier

Using prolog • Let’s try it, for those who haven’t used prolog. Log into

Using prolog • Let’s try it, for those who haven’t used prolog. Log into hopper (just use ssh – no gui needed). – Backup – find online swi-prolog editor, which is what I’ll do. • Type “gprolog” • Now type (into a document called firstexample. pl): likes(mary, food). likes(mary, beer). likes(mary, movies). likes(john, wine). likes(john, mary). • Type “prolog”, then “consult(‘firstexample. pl’). • Type “listing. ” to get all the current facts. Copyright © 2009 Elsevier

Using prolog • Now try a few simple facts: | ? - likes(mary, food).

Using prolog • Now try a few simple facts: | ? - likes(mary, food). true. | ? - likes(john, wine). true. | ? - likes(john, food). false. Copyright © 2009 Elsevier

Using prolog: an exercise • How do you add the following facts? (Try them

Using prolog: an exercise • How do you add the following facts? (Try them either at the prompt or in your file, and you can use “reconsult” to reload if you edit the file. ) 1. Bob likes wine and beer. 2. John likes anything Mary likes. 3. John likes anyone who likes beer. 4. John likes anyone who likes themselves. • How would you query at the prompt to find everything that John likes? (Email me your solutions at the end of class) Copyright © 2009 Elsevier