1 / 44

Logic Programming & Prolog

Logic Programming & Prolog. A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of Prolog The Basic Elements of Prolog Sebesta Ch. 16. Introduction.

varick
Download Presentation

Logic Programming & 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. Logic Programming & Prolog • A Brief Introduction to Predicate Calculus • Predicate Calculus and Proving Theorems • An Overview of Logic Programming • The Origins of Prolog • The Basic Elements of Prolog • Sebesta Ch. 16

  2. Introduction • Logic programming languages and declarative programming languages • Express programs in a form of symbolic logic • Use a logical inferencing process to produce results • Declarative rather that procedural: • only specification of resultsare stated • notdetailed procedures for producing them

  3. Introduction to Predicate Calculus • Proposition • A logical statement • May be true or false • Consists of objectsandrelationshipsofobjectsto each other • Examples: • John is a student : student(john) • Hillary is Bill's wife: wife(hillary,bill)

  4. Introduction to Predicate Calculus • Symbolic logic is a subset of formal logic • expresses propositions • expresses relationships between propositions • describes how new propositions can be inferred from other propositions • Predicate calculus • Form of symbolic logic used for logic programming • Prolog • PL based on predicate calculus

  5. Propositions • Objects in propositions • either constants or variables • Constant • A symbol that represents an object • In Prolog, starts with a lower case letter • Variable • A symbol that can represent different objects at different times • Instantiated with values during the reasoning process • Different from variables in imperative PLs • In Prolog, starts with an upper case letter or _

  6. AtomicPropositions • Atomic proposition • Consist of compound terms • Compound term • An element of a mathematical relation • Written like a mathematical function, e.g., f(x,y) • Can be written as a table • Mathematical function is a mapping • Compound term composed of two parts • Functor • Function symbol that names the relationship • Tuple • Ordered list of parameters

  7. Propositions in Prolog • Prolog Examples • Functors: • and, or, not, student, like • Propositions • student(john) • like(mary,mac) • like(nick,windows) • like(kris,linux)

  8. Forms of Propositions • Propositions can be stated in two forms; as • Fact • proposition that is assumed to be true • Query • whether a proposition is true • Compound proposition • Consists of two or more propositions • Connected by logical operators • and, or, not, etc.

  9. Logical Operators

  10. Truth Tables

  11. Quantifiers • Examples • ∀bird, wings(bird) • all birds have wings • ∃bird, blue(wings, bird) • at least one bird has blue wings

  12. Clausal Form • Too many ways to state the same preposition • Use a standard form • Clausal form: B1 ∪ B2 ∪…∪ Bn ⊂ A1 ∩ A2 ∩…∩ Am • means if all the As are true, then at least one B is true • Antecedent • right side • I.e., A1 ∩ A2 ∩…∩ Am • Consequent • left side • I.e., B1 ∪ B2 ∪…∪ Bn

  13. Predicate Calculus and Theorem Proving • The use of propositions is to discover new theorems • infer them from known axioms and theorems • Resolution • an inference principle • computes inferred propositions from given ones • Example Propositions: • older(joanne, jake) • mother(joanne, jake) • Rule: • older(joanne, jake) ⊂ mother(joanne, jake) • Joanne is older than Jake IF Joanne is the mother of Jake

  14. Reasoning with Propositions – Example • Rule: older(joanne, jake) ⊂ mother(joanne, jake) • Joanne is Jake’s mom therefore Joanne is older than Jake • a FACT with constants • Rule: wiser(joanne, jake) ⊂ older(joanne, jake) • Joanne is older than Jake, therefore Joanne is wiser than Jake • To proceed, combine rules using “and” of left side terms, and “and” of right side terms (looks like addition) A ⊂ B C ⊂ D . A ∩ C ⊂ B ∩ D • older(joanne, jake) ∩ wiser(joanne, jake) ⊂ mother(joanne, jake) ∩ older(joanne, jake) • Remove the same propositions on both sides • will have no effect on T/F • Result: wiser(joanne, jake) ⊂ mother(joanne, jake)

  15. Proof by Contradiction • Hypotheses • A set of pertinent propositions • Goal • Negation of theorem stated as a proposition • Combine • negated unknown goal proposition • with known true propositions • When an inconsistency results • this proves that the unknown propositions are not true • if they were true, inconsistency would not be possible

  16. Horn Clauses • Theorem broving is basis for logic programming • When propositions used for resolution, only restricted form can be used • Horn clauses - have only two forms • Headed clause • single atomic proposition on left side • propositions on the right connected with “and” • Headlessclause • empty left side • propositions on the right connected with “and” • used to state facts • Most propositions can be stated as Horn clauses

  17. Unification: Resolution with Variables • Instantiation • Assigning temporary values to variables in propositions • Unification • Finding such values for variables so that matching/instantiation to succeeds • When • a variable is instantiated with a value, and then • matching fails • Then • unification needs to backtrack • the variable is instantiated with a different value

  18. Semantics of Logic Programming • Declarative semantics • Determining the meaning of each statement is simple • Simpler than the semantics of imperative languages • Programming is nonprocedural • Programs do not state how to compute the result, but what the conditions the result needs to satisfy • Facts and variables • Are bound temporarily • They can't be re-bound unless they are unbound first • Binding and unbinding proceeds automatically

  19. Example with Variables • older(A, B) ⊂ mother(A, B); mother is older • wiser(C, D) ⊂ older(C, D) ; older is wiser • mother(joanne, jake); Joanne is Jake’s mom A/joanneB/jakeinstantiation A/joanne B/jake (1 & 3)older(joanne, jake) ⊂ mother(joanne,jake) C/joanne D/jakeinstantiation C/joanne D/jake (1 & 2) wiser(joanne, jake) ⊂ older(joanne, jake)

  20. Example: Sorting a List • Describe the characteristics of a sorted list, not the process of rearranging a list sort(old_list, new_list) ⊂ permute (old_list, new_list) ∩ sorted (new_list) sorted (list) ⊂ ∀j such that 1≤j<n, list(j)≤list(j+1)

  21. The Origins of Prolog Robert Kowalski University of Edinburgh, Scotland, U.K. Automated theorem proving • Alain Colmerauer, Philippe Roussel • University of Aix- Marseille, France • Natural language processing

  22. Basic Elements of Prolog • Edinburgh Syntax (most common) • Atom • symbolic value in Prolog • Atom consists of either • a string of letters, digits, and underscores beginning with a lowercase letter {a,b,….z},e.g., lucky • a string of printable ASCII characters delimited by apostrophes ',e.g. ,'this is a string' • Constant • an atom or an integer • Variable • a string of letters, digits, and underscores beginning with an uppercase letter, e.g.,Lucky

  23. Basic Elements of Prolog (cont.) • Term • A constant, variable, or structure • cat(lucky), Cat, sibling(S1,S2) • Structure • Represents atomic proposition • functor(parameter list) • Instantiation • Binding of a variable to a value • Only as long as it takes to satisfy one complete goal

  24. Statement of Facts • Headless Horn clauses student(jonathan). sophomore(ben). brother(tyler, cj). • Used for true statements, i.e. facts

  25. Rule Statements • Used for the hypotheses • Headed Horn clauses • Right side • antecedent (if part) • May be single term or conjunction • Left side • consequent (then part) • Must be single term • Conjunction: • multiple terms separated by implied logical "and"

  26. Rule Statements parent(kim,kathy):- mother(kim,kathy). • Variables (universal objects) are used to generalize meaning parent(X,Y):- mother(X,Y). sibling(X,Y):- mother(M,X), mother(M,Y), father(F,X), father(F,Y).

  27. Goal Statements • Goal statement • Proposition/ theorem we want to prove or disprove • Headless Horn clause form student(james). • Propositions with variables are legal goals father(X,joe). • Conjunctive propositions are also legal goals cat(lucky) ∩ horse(mr_ed).

  28. Inferencing Process • Query is a Goal • If a goal is a compound proposition, each of the sub-propositions is a sub-goal • To prove a goal is true, must find a chain of inference rules and/or facts. For goal Q: B :- A C :- B … Q :- P • Process of proving a sub-goal is called matching, satisfying, or resolution

  29. Inferencing Process • Bottom-up resolution, forward chaining • Begin with facts and rules of database and attempt to find sequence that leads to goal • Works well with a large set of possibly correct answers • Top-down resolution, backward chaining • Begin with goal and attempt to find sequence that leads to set of facts in database • Works well with a small set of possibly correct answers • Prolog implementations use backward chaining

  30. Top-Down Example • (Rule) #1 animal(A) -: cat(A). • (Fact) #2 cat(lucky). • Top-down resolution, backward chaining • Is lucky an animal? • Goal: animal(lucky). ??? • Find rule to unify with Goal. Rule #1contains animal(A) • #1 animal(A) -: cat(A). • Unify lucky from Goal with variable A in rule #1, yields: • #1(lucky) animal(lucky) -: cat(lucky). • Fact: Lucky is a cat • #2 cat(lucky). • Combine #1lucky and #2 (add then remove anything on both sides) • animal(lucky) and cat(lucky) -: cat(lucky). • Proven. • GOAL: animal(lucky). True if True!

  31. Bottom Up Example • (Rule) #1 animal(A) -: cat(A). • (Fact) #2 cat(lucky). • Top-down resolution, forward chaining • Given: lucky is a cat • #2 cat(lucky) • Given: If A is a cat, A is an animal • #1 animal(A) -: cat(A). • Unify lucky from #2 with variable A in rule #1, yields: • #1(lucky) animal(lucky) -: cat(lucky). • #2 cat(lucky). • #2 animal(lucky) -: True. • Proven: lucky is an animal • GOAL: animal(lucky).

  32. Inferencing Process Examples • (Rule) #1 animal(A) -: cat(A). • (Fact) #2 cat(lucky). • Negation by failure • Assume the opposite of what you want to prove • #3 ¬animal(lucky) • To unify with query, Negate Rule • #1 ¬cat(A) -: ¬ animal(A) • unify lucky #3 / A in #1 gives • #4 ¬cat(lucky) • Unify lucky in #4/ lucky in #2 • #5 ¬cat(lucky) ∩ cat(lucky) • Contradiction in #5 due to #3 • Proves Goal: animal(lucky)

  33. Inferencing Process • When goal has more than one sub-goalcan use either • Depth-first search • Find a complete proof for the 1st sub-goal then work on others • Breadth-first search • Work on all sub-goals in parallel • Prolog uses depth-first search • Can be done with fewer computer resources

  34. Inferencing Process (cont.) • For a goal with multiple sub-goals, • if fails to show truth of one of sub-goals, • then reconsider previous sub-goal(s) to find an alternative solution • backtracking • Begin search where previous search left off • Can take lots of time and space • May find all possible proofs to every sub-goal

  35. Simple Arithmetic • Prolog supports integer variables and integer arithmetic • is operator • takes an arithmetic expression asright operand and variable as left operand • A is B / 10 + C • Not the same as an assignment statement! • A temporary binding while processing continues • There can’t be any unbound variables on the right hand side!

  36. Example speed(ford,100). speed(chevy,105). speed(dodge,95). speed(volvo,80). time(ford,20). time(chevy,21). time(dodge,24). time(volvo,24). distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time. ?-distance(chevy,D). D = 2205

  37. Trace • Built-in structure that displays instantiations at each step • Tracing model of execution - four events: • Call • beginning of attempt to satisfy goal • Exit • when a goal has been satisfied • Redo • when backtrack occurs • Fail • when goal fails

  38. Example likes (jake, chocolate). likes (jake, apricots). likes (darcie, licorice). likes (darcie, apricots). Control flow model for the goal ?-trace. ?-likes(jake,X),likes(darcie,X). (1) 1 Call: likes(jake,_0)? (1) 1 Exit: likes(jake,chocolate) (2) 1 Call: likes(darcie,chocolate)? (2) 1 Fail: likes(darcie,chocolate) (1) 1 Redo: likes(jake,_0)? (1) 1 Exit: likes(jake,apricots) (2) 1 Call: likes(darcie,apricots)? (2) 1 Exit: likes(darcie,apricots) X = apricots Call Fail likes (jake, X) Redo Exit Fail Call likes (darcie, X) Exit Redo

  39. List Structure • List is a sequence of any number of elements • Elements can be atoms, atomic propositions, or other terms (including other lists) [apple, prune, grape, kumquat] [] • empty list [X | Y] • head X and tail Y • X and Y can each be of any length

  40. Example: append • Definition of append function: append([], List, List). append([Head | List_1], List_2, [Head | List_3]) :- append (List_1, List_2, List_3). • appending an empty list does nothing • if List_1+List_2 = List_3then Head+List_1+List_2 = Head+List_3

  41. Example: reverse • Definition of reverse function: reverse([], []). reverse([Head | Tail], List) :- reverse (Tail, Result), append (Result, [Head], List). • Lecture-question: • How would you express these two rules in plain English?

  42. Broader Issues in Prolog • Resolution order control • Used for efficiency, but violates declarative style • The closed-world assumption • Assume you know everything about the world • The negation problem • Is a proposition FALSE if it can’t be proven TRUE? • Intrinsic efficiency limitations • Not meant for complex numerical calculations

  43. Applications of Logic Programming • Relational database management systems • Expert and knowledge-based systems • Natural language processing • Education

  44. Conclusions • Advantages: • Prolog programs based on logic • More logically organized and written (aid readability and writability) • Processing is naturally parallel • Prolog interpreters can take advantage of multiprocessor machines • Programs are concise • Fewer lines of code necessary • Development time is decreased – good for prototyping

More Related