1 / 23

ICS 313: Programming Language Theory

ICS 313: Programming Language Theory. Module 15: Prolog. Logic programming vs. Prolog. Logic programming involves use of inference across relations to perform computation. Prolog is a particular instance of a logic programming language.

walda
Download Presentation

ICS 313: Programming Language Theory

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. ICS 313: Programming Language Theory Module 15: Prolog

  2. Logic programming vs. Prolog • Logic programming involves use of inference across relations to perform computation. • Prolog is a particular instance of a logic programming language. • Other languages use logic-based programming (i.e. constraint-based languages). • Prolog is the most popular and successful example of a logic programming language

  3. Logic programming as computing with relations • A relation is a table with a fixed number of columns and a possibly infinite set of rows. • A tuple (x1, x2, ..., xn) is in a relation if the tuple appears in some row of the relation. • Consider the following table of relations representing the append operation.

  4. Append as a table: • Relations can be used as predicates: • Is tuple T in relation R? • Example: • ([a], [b], [a, b]) is in R • ([a], [b], []) is not in R

  5. Relations vs. Functions • The functional version of append: • (x, y) -> z • The relational version of append: • Allows x, y, and/or z to be return value! • Example relational queries: • Does [a,b] appended with [c, d] yield [a,b,c,d]? • Answer: yes. • Is there a Z such that [w, x] appended with [y] yields Z? • Answer: yes, when Z = [w, x, y] • Is there a Q such that [x, y] appended with Q yields [x, y, z]? • Answer: yes, when Q = [z].

  6. Logic Programming Concepts • Logic programming languages have in common: • Declaring primitive facts about a domain • Defining rules to express relationships between facts in the domain • Use of deduction to answer questions about domain

  7. Logic Programming vs. Imperative • Conceptual differences between “pure” logic paradigm and imperative paradigm: • Little to no explicit control flow statements • No if-then-else or other conditionals • No while loops or other iteration • No global variables or other globally modifiable state • However, there is a lot of implicit control flow (Prolog allows “cheating” via the cut goal.)

  8. Logic + Control • Kowalski (1979) coined the following famous expression: • Algorithm = Logic + Control • Prolog supplies the “control” part of an algorithm automatically. • User provides only “Logic” portion.

  9. Defining facts • One way to express the fact that “John likes Mary” is: • likes (john, mary). • This defines two arguments, john and mary, and a predicate, likes • The period is needed to terminate a fact. • This fact is a constant value, not a function invocation! • The use of lower case indicates atomic values • likes (mary, john). is a different fact

  10. Facts cont. • Facts can have arbitrary arity: • valuable (gold). • gives (john, book, mary). • food. • The current facts (and rules) constitutes the “database”.

  11. Asking questions • Questions are simply facts prefaced by ?- • ?- likes (mary, john). • Informally: Does mary like john? • More formally: Can it be proven from the current facts in the database that the relation likes (mary, john) holds?

  12. Questions cont. • Proving a fact involves matching: • Two facts match if their predicates are the same and if each of their corresponding arguments are the same from left to right. • Prolog searches the database in order that the facts were entered for matching facts • If Prolog can find a matching fact for the question, it responds yes, otherwise it will respond no. • “No” does not mean “it is false”.

  13. Variables • Variables allow Prolog to return information from the database. • Variables are represented by names beginning with a capital letter • Variables become instantiated when bound to an object in the database

  14. Variable Instantiation • ?- likes (john, X). X = wine

  15. Implicit control flow: • Database is searched from top to bottom for a predicate matching likes • Each matching predicate is searched in order for a first argument matching john • Each matching predicate and first argument is searched for a second argument matching (the uninstantiated) X • X matches wine • X is instantiated to wine • Prolog marks the place where the match was made

  16. Re-satisfying questions • When a question is satisfied by finding a match for the predicate and each argument, the values for all instantiated variables are printed out. • Typing a semi-colon instructs Prolog to attempt to re-satisfy the last match it found, starting from the place marker. • ?- likes (john, X). X = wine; X = mary; no

  17. Conjunctions • Conjunctions allow specification of multiple conditions to be satisfied simultaneously • Conjunctions are represented by commas between predicates • Do john and mary like each other? ?- likes (john, mary), likes (mary, john). no • Is there anything that both John and Mary like? ?- likes (mary, X), likes (john, X). • First, is there anything that John likes? • Second, does Mary also like it?

  18. Backtracking • Database is searched for match to first goal. • The first match is: likes (mary, food) • X is instantiated to food everywhere • Prolog marks place in database where this goal was satisfied • Database is searched (from top) for • likes (john, food) • No such fact exists, so goal fails.

  19. Backtracking (cont). • Prolog attempts to re-satisfy previous goal • All instantiations to previous goal are undone • Search continues from the placemarker • Database searched again for match to first goal • The new match is: likes (mary, wine) • X is instantiated to wine • Prolog marks this place in database • Database searched (from top) for: likes (john, wine) • This matches a fact in the database • Prolog marks the place where the match occurred • Prolog returns: X = wine

  20. Rules • Rules • express general statements about objects and their relationships to each other. • indicate when a fact depends on other facts. • “name” collections of terms (subroutine). • Rules consist of two parts: • Head: what fact the rule is intended to define. • Body: a conjunction of goals to be satisfied if the head is to be true.

  21. Rules (cont.) • To satisfy/match a rule head, all of its body must be satisfied. • Example: • Mary likes anyone who likes wine • likes (mary, X) :- likes (X, wine).

  22. Rule example • ?- likes (mary, Y). • Y = food ; • Y = wine ; • Y = mary ; • Y = john ; • no

  23. end of module

More Related