1 / 25

159.331 Programming Languages & Algorithms

159.331 Programming Languages & Algorithms. Lecture 25 - Logic Programming Languages - Part 1. Logic languages. More ideas along the trend of letting the programmer specify the problem and the computer is tasked with figuring out how to solve it.

edie
Download Presentation

159.331 Programming Languages & Algorithms

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. 159.331 Programming Languages & Algorithms Lecture 25 - Logic Programming Languages - Part 1 Prog Lang & Alg

  2. Logic languages • More ideas along the trend of letting the programmer specify the problem and the computer is tasked with figuring out how to solve it. • Logic programming based around the formalism of Horn Logic that can specify problems and can also be executed automatically. Prog Lang & Alg

  3. Unfortunately Horn logic does not meet the ultimate goal of declarative programming - namely to avoid having to have a programmer at all! • To achieve reasonable efficiency on real problems the programmer still needs to be aware the machine executes the program and put appropriate extra controls in place. • Horn Logic is the basis of prolog - a system that is the most important logic programming language. Nevertheless there are others… • Also Prolog is a system that has some non-logic features and is no more a pure logic language than say Lisp is a pure functional language. Prog Lang & Alg

  4. Various concepts we will look at: • Horn clauses • Logical variables • Relations • Data structures • Search order • Some examples and Prolog snippets • Some directions for the future… Prog Lang & Alg

  5. Horn Clauses • A Horn clause expresses that a certain condition is true if zero or more other conditions are true. For example: • if a person is old and wise then that person is happy • if X is the father of Y and Y is the father of Z then X is the grandfather of Z • There can be zero or more conditions in the if-part and exactly one conclusion in the then-part • A Horn clause with no conditions expresses afact Prog Lang & Alg

  6. Logic programming involves the programmer giving a list of if-then rules so that the system can infer automatically whether or not a given condition is true. • The system does so by starting with the given condition and working its way back to the facts. • If it can derive a valid way back to the facts then the condition is true and it has proved it. • This is very different from the if-statements we use in imperative programming • We have no flow of control concept in logic programming Prog Lang & Alg

  7. A Horn clause in general can be written as: G0  G1, G2, … , Gn. • The declarative meaning of the Horn clause is: • If G1 until Gn are all true then G0 is true. • The left hand side of the rule is called the head; the right hand side is called the body • G0 until Gn are called goals • Goals in the body are called subgoals • The commas should be read as logical AND symbols • Note also that the  means just “if” - it does not mean “if and only if” - there may be other rules for G0 • Special case n=0 we have a single fact G0 is always true Prog Lang & Alg

  8. Each goal consists of a predicate name and zero or more arguments • Take arguments to be variables or constants - by convention use upper case for variables ie X • We might have a predicate lucky(jim) saying that the constant jim is somehow lucky • A Horn clause example: • old(confucius). (1) • wise(confucius). (2) • happy(X)  old(X), wise(X). (3) • 1 and 2 are facts, the 3rd says generally that old wise people are happy Prog Lang & Alg

  9. In the above, the facts are about a single person - constants • The 3rd is about a variable X and thus holds for all X • We say that variables in facts and rules are universally quantified (sometimes see this written  X, meaning “for all values of X,…”) • Once the system “knows” all the facts we can ask it questions or “pose queries” • ?- happy(charlie). Is a query asking if the constant charlie is happy Prog Lang & Alg

  10. The system will answer our query with either a Yes or a no • (incidentally the ?- is a Prolog syntax convention for a query) • We can also ask ?-happy(X). • If the system finds any X’s that satisfy the goal it will list them. • Variables in a initial goal are said to be existentially qualified - sometimes we write •  X ?… Essentially asking if any X exist for the stated criteria • The system will assign any X’s that match to the variable and we can match or retrieve/print them out Prog Lang & Alg

  11. Executing Horn Clauses • The Horn clauses are executed automatically - by the system • How this is implemented is of course the trick to making an efficient implementation • There might be several rules for solving the initial goal of ?-happy(charlie) happy(X)  rich(X), famous(X). happy(X)  young(X),in_love(X). happy(X)  old(X), wise(X). happy(X)  enrolled_in_159331(X). Prog Lang & Alg

  12. Each one of these four rules is sufficient to proving the initial goal. • Key idea is that the system tries ALL possibilities in whatever order it sees fit until one succeeds (or it has exhausted all possibilities) • If all clauses fail, the whole query fails and the system answers “no” • During search, the system has to prove subgoals. • For instance we might have: • Famous(X)  moviestar(X). • Famous(X)  popstar(X). Prog Lang & Alg

  13. If we had the facts: moviestar(charlie). and rich(charlie). We could prove that charlie is happy. • We have to be careful however not to write infinite loop rules that never terminate. • The pure logic model allows the system a lot of freedom in how it searches its rule base • It can choose which order to try alternative clauses • And what order to try different subgoals • The search order is non-deterministic (we the programmer have not specified it) • It turns out Prolog uses a fixed (known) search order Prog Lang & Alg

  14. The notion of the system just trying another subgoal when it fails to prove another is called backtracking • We can illustrate the search strategy with the tree diagram in 5.1 • The root of the tree is drawn as the initial goal • Below the root are the alternatives for solving the initial goal • At the next level we have the subgoals • The system searches all possible solutions and it will always find a solution if one exists - known as the completeness-of-search property Prog Lang & Alg

  15. Enrolled on 159331 Prog Lang & Alg

  16. Closed Worlds • If system fails to find a answer proving that charlie is happy it will return a no • We take this to mean that as far as the system can tell charlie is not happy and the goal is not true • This assumes that goals that can be proven are true and goals that cannot are false - known as the closed world assumption • (ie we ignore facts outside our system that we do not know about) • So note that our query answers are only as good as our knowledge-base allows Prog Lang & Alg

  17. The above is a simplification - the goals can in fact have arguments so an actual implementation is more complex • Horn logic is a subset of a more general logic known as predicate logic - which allows multiple conclusions • Restricting ourselves to Horn logic makes it possible to execute clauses automatically Prog Lang & Alg

  18. Logical variables • Used for storing state information and for passing arguments between goals happy(X)  old(X), wise(X). • Initially a logical variable does not contain a value - we say it is unbound • It can contain a value and become bound if it is matched against a value or another variable • After it is bound it cannot be changed (except during backtracking - as we describe later) Prog Lang & Alg

  19. Recall that a clause containing variable X is a general rule about any unspecified X • Within the rule, X must refer to the same object consistently • While executing the clause we can thus make a decision about Xexactly once • So logical variables are bound once only and are thus closer to the concept of mathematical variables rather than the state variables we are used to in imperative programming systems. • Most logic languages are typeless - we do not need to declare X - it is done implicitly Prog Lang & Alg

  20. Executing Queries with Predicates coauthor(X,Y)  author(X,Book),author(Y,Book). author(ullman, database_book). author(ullman, awk_book). author(kernighan, awk_book). author(aho, compiler_book). author(sethi, compiler_book). author(ullman, compiler_book). • First rule means that two people are coauthors if there exists a book of which both are an author. • The other clauses are facts about some books & authors. Prog Lang & Alg

  21. We want to answer a query like: ?- coauthor(ullman,aho). • Which asks if Ullman and Aho are coauthors of any book. • The system needs to find a clause whose head matches the query and whose subgoals can be solved. • The matching rule can be stated: • A head and a goal match if they are equal or if they can be made equal by binding one or more variables in the head or goal. • This is called unification of the head and the goal Prog Lang & Alg

  22. For the head and the goal to be equal they must have the same predicate name and the same number of arguments • Additionally, each argument in the head must be equal (or made equal) to the corresponding argument in the goal. • If two arguments denote the same value they are equal • If one of the two arguments is unbound, equality will arise if we bind the unbound to the other argument • If they are both unbound, they are bound to each other • If one later becomes bound then so (automatically) does the other - to the same value Prog Lang & Alg

  23. In our example, only the first rule can possibly match the query coauthor(ullman,aho). • The head of the rule coauthor(X,Y), can be made equal to the initial goal by binding X to ullman and Y to aho. • The query can thus be answered by solving the subgoals in the body of the clause, after replacing X and Y with the values to which they were just bound: author(ullman,Book), author(aho,Book). Prog Lang & Alg

  24. System might try to solve author(ullman,Book) first • Tries to bind Book to the constant database_book • But cannot solve author(aho,database_book) • System then backtracks over its last decision • Unbinds Book and look for another fact that matches the goal • author(ullman,compiler_book) is tried, binding Book to compiler_book • New attempt to match the subgoal author(aho,compiler_book) succeeds and so the whole query succeeds. • Same search process as in our simpler example but the unification of variables complicates the matching rules. Prog Lang & Alg

  25. Logic Programming Part 1 - Summary • Horn Clauses • Simple facts and queries • Logical Variables • Unification of head and goal variables • See Bal & Grune Chapter 5, Sebesta Chapter 16 • Next - Logic relations and Data Structures Prog Lang & Alg

More Related