460 likes | 744 Views
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.
E N D
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 • 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
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)
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
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 _
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
Propositions in Prolog • Prolog Examples • Functors: • and, or, not, student, like • Propositions • student(john) • like(mary,mac) • like(nick,windows) • like(kris,linux)
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.
Quantifiers • Examples • ∀bird, wings(bird) • all birds have wings • ∃bird, blue(wings, bird) • at least one bird has blue wings
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
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
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)
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
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
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
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
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)
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)
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
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
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
Statement of Facts • Headless Horn clauses student(jonathan). sophomore(ben). brother(tyler, cj). • Used for true statements, i.e. facts
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"
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).
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).
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
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
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!
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).
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)
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
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
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!
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
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
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
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
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
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?
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
Applications of Logic Programming • Relational database management systems • Expert and knowledge-based systems • Natural language processing • Education
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