Slide 1 Vitaly Shmatikov CS 345 Logic Programming.

Slides:



Advertisements
Similar presentations
Chapter 11 :: Logic Languages
Advertisements

Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Logic Programming Chapter 15 Part 2. Prolog Lists The list is Prolog’s basic data structure Lists are a series of Prolog terms, separated by commas Each.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Inference and Resolution for Problem Solving
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Contents 15.1 Logic and Horn Clauses 15.2 Logic Programming in Prolog
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
CS 321 Programming Languages and Compilers Prolog part 2.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Chapter 15 Part 2. Breadth-first v Depth-first Search Suppose a query has compound goals (several propositions must be satisfied) Depth-first.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
Logic Programming Lecture 2: Unification and proof search.
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Prolog Concepts.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Prolog Concepts.
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Logic Programming

slide 2 Reading Assignment uMitchell, Chapter 15

slide 3 Logic Programming uFunction (method) is the basic primitive in all languages we have seen so far F(x)=y – function F takes x and return y uRelation (predicate) is the basic primitive in logic programming R(x,y) – relationship R holds between x and y

slide 4 Prolog uShort for Programmation en logique Alain Colmeraurer (1972) uBasic idea: the program declares the goals of the computation, not the method for achieving them uApplications in AI, databases, even systems Originally developed for natural language processing Automated reasoning, theorem proving Database searching, as in SQL Expert systems Recent work at Berkeley on declarative programming

slide 5 Example: Logical Database AUS DAL OKC HOU PHX nonstop(aus, dal). nonstop(aus, hou). nonstop(aus, phx). nonstop(dal, okc). nonstop(dal, hou). nonstop(hou, okc). nonstop(okc, phx). nonstop(aus, dal). nonstop(aus, hou). nonstop(aus, phx). nonstop(dal, okc). nonstop(dal, hou). nonstop(hou, okc). nonstop(okc, phx). In Prolog:

slide 6 Logical Database Queries uWhere can we fly from Austin? uSQL SELECT dest FROM nonstop WHERE source=“aus”; uProlog ?- nonstop(aus, X). More powerful than SQL because can use recursion

slide 7 N-Queens Problem uPlace N non-attacking queens on the chessboard Example of a search problem (why?)

slide 8 N-Queens in Prolog diagsafe(_, _, []). diagsafe(Row, ColDist, [QR|QRs]) :- RowHit1 is Row + ColDist, QR =n= RowHit1, RowHit2 is Row - ColDist, QR =n= RowHit2, ColDist1 is ColDist + 1, diagsafe(Row, ColDist1, QRs). safe_position([_]). safe_position([QR|QRs]) :- diagsafe(QR, 1, QRs), safe_position(QRs). nqueens(N, Y) :- sequence(N, X), permute(X, Y), safe_position(Y).

slide 9 Type Inference in ML uGiven an ML term, find its type +2

slide 10 Flight Planning Example AUS DAL OKC HOU PHX nonstop(aus, dal). nonstop(aus, hou). nonstop(aus, phx). nonstop(dal, okc). nonstop(dal, hou). nonstop(hou, okc). nonstop(okc, phx). nonstop(aus, dal). nonstop(aus, hou). nonstop(aus, phx). nonstop(dal, okc). nonstop(dal, hou). nonstop(hou, okc). nonstop(okc, phx). Relation: nonstop(X, Y) – there is a flight from X to Y Each line is called a clause and represents a known fact A fact is true if and only if we can prove it true using some clause

slide 11 Queries in Prolog AUS DAL OKC HOU PHX ?- Yes ?- Yes ?- No ?- nonstop(aus, dal). nonstop(dal, okc). nonstop(aus, okc).

slide 12 Logical Variables in Prolog AUS DAL OKC HOU PHX ?- X=phx ; No ?- Y=aus ; No ?- nonstop(okc, X). nonstop(Y, dal). Is there an X such that nonstop(okc, X) holds?

slide 13 Non-Determinism AUS DAL OKC HOU PHX ?- X=hou ; X=okc ; No ?- No ?- nonstop(dal, X). nonstop(phx, X). Predicates may return multiple answers or no answers

slide 14 Logical Conjunction AUS DAL OKC HOU PHX ?- X=dal ; X=hou ; No ?- nonstop(aus, X), nonstop(X, okc). Combine multiple conditions into one query

slide 15 Derived Predicates AUS DAL OKC HOU PHX ?- Via=dal ; Via=hou ; No ?- flyvia(From, To, Via) :- nonstop(From, Via), nonstop(Via, To). flyvia(aus, okc, Via). Define new predicates using rules conclusion :- premises. - conclusion is true if premises are true

slide 16 Recursion AUS DAL OKC HOU PHX ?- X=aus ; X=dal ; … ?- reach(X, X). reach(X,Z) :- nonstop(X, Y), reach(Y, Z). reach(X, phx). Predicates can be defined recursively

slide 17 Prolog Program Elements uProlog programs are made from terms Variables, constants, structures uVariables begin with a capital letter Bob uConstants are either integers, or atoms 24, zebra, ‘Bob’, ‘.’ uStructures are predicates with arguments n(zebra), speaks(Y, English)

slide 18 Horn Clauses uA Horn clause has a head h, which is a predicate, and a body, which is a list of predicates p1, p2, …, pn It is written as h  p1, p2, …, pn This means, “h is true if p1, p2, …, and pn are simultaneously true” uExample snowing(C)  precipitation(C), freezing(C) This says, “it is snowing in city C if there is precipitation in city C and it is freezing in city C”

slide 19 Facts, Rules, and Programs uA Prolog fact is a Horn clause without a right- hand side Term. –The terminating period is mandatory uA Prolog rule is a Horn clause with a right-hand side (:- represents  ) term :- term1, term2, … termn. LHS is called the head of the rule uProlog program = a collection of facts and rules

slide 20 Horn Clauses and Predicates uAny Horn clause h  p1, p2, …, pn can be written as a predicate p1  p2  …  pn  h, or, equivalently,  (p1  p2  …  pn)  h uNot every predicate can be written as a Horn clause (why?) Example: literate(x)  reads(x)  writes(x)

slide 21 Lists uA list is a series of terms separated by commas and enclosed in brackets The empty list is written [] A “don’t care” entry is signified by _, as in [_, X, Y] A list can also be written in the form [Head | Tail]

slide 22 Appending a List append([], X, X). append([Head | Tail], Y, [Head | Z]) :- append(Tail, Y, Z). The last parameter designates the result of the function, so a variable must be passed as an argument uThis definition says: Appending X to the empty list returns X If Y is appended to Tail to get Z, then Y can be appended to a list one element larger [Head | Tail] to get [Head | Z]

slide 23 List Membership member(X, [X | _]). member(X, [_ | Y]) :- member(X, Y). uThe test for membership succeeds if either: X is the head of the list [X | _] X is not the head of the list [_ | Y], but X is a member of the remaining list Y uPattern matching governs tests for equality u“Don’t care” entries (_) mark parts of a list that aren’t important to the rule

slide 24 More List Functions uX is a prefix of Z if there is a list Y that can be appended to X to make Z prefix(X, Z) :- append(X, Y, Z). Suffix is similar: suffix(Y, Z) :- append(X, Y, Z). uFinding all the prefixes (suffixes) of a list ?- prefix(X, [my, dog, has, fleas]). X = []; X = [my]; X = [my, dog]; …

slide 25 Answering Prolog Queries uComputation in Prolog (answering a query) is essentially searching for a logical proof uGoal-directed, backtracking, depth-first search Resolution strategy: if h is the head of a Horn clause h  terms and it matches one of the terms of another Horn clause t  t1, h, t2 then that term can be replaced by h’s terms to form t  t1, terms, t2 What about variables in terms?

slide 26 Flight Planning Example AUS DALHOU ?- n(aus, hou). ?- n(aus, dal). ?- r(X, X). ?- r(X, Z) :- n(X, Y), r(Y, Z). ?- r(aus, X)

slide 27 Flight Planning: Proof Search AUS DALHOU Rule 1: r(X, X). Rule 2: r(X, Z) :- n(X, Y), r(Y, Z). Solution r(aus, aus) r(aus, hou) Solution r(aus, aus) r(aus, hou) r(aus, X) r(aus,aus) n(aus,Y), r(Y,X) n(aus,Y) n(aus,hou) r(hou,X) r(hou,hou)n(hou,Z), r(Z,X)

slide 28 Flight Planning: Backtracking AUS DALHOU Rule 1: r(X, X). Rule 2: r(X, Z) :- n(X, Y), r(Y, Z). Solution r(aus, aus) r(aus, hou) r(aus, dal) Solution r(aus, aus) r(aus, hou) r(aus, dal) r(aus, X) r(aus,aus) n(aus,Y), r(Y,X) n(aus,Y) n(aus,hou) r(dal,X) r(dal,dal)n(dal,Z), r(Z,X) n(aus,dal)

slide 29 Unification uTwo terms are unifiable if there is a variable substitution such that they become the same For example, f(X) and f(3) are unified by [X=3] f(f(Y)) and f(X) are unified by [X=f(Y)] How about g(X,Y) and f(3)? uAssignment of values to variables during resolution is called instantiation uUnification is a pattern-matching process that determines what instantiations can be made to variables during a series of resolutions

slide 30 Example: List Membership Rule 1: mem(X, [X | _]). Rule 2: mem(X, [_ | Y]) :- mem(X, Y). mem(Z, [1,2]) mem(1, [1,2])mem(X, [2]) mem(2, [2]) mem(X’, []) ?? {X=Z, X=1} {Z=X, Y=[2]} {X=2} {X’=X, Y’=[]} ?- mem(X, [1,2]). X=1 ; X=2 ; No ?- ?- mem(X, [1,2]). X=1 ; X=2 ; No ?- Prolog

slide 31 Soundness and Completeness uSoundness If we can prove something, then it is logically true uCompleteness We can prove everything that is logically true uProlog search procedure is sound, but incomplete

slide 32 Flight Planning: Small Change AUS DALHOU Rule 1: r(X, X). Rule 2: r(X, Z) :- r(X, Y), n(Y, Z). Solution r(aus, aus) Solution r(aus, aus) r(aus, X) r(aus,aus) r(aus,Y), n(Y,Z) r(aus,Y) instead of n(X,Y), r(Y,Z) Infinite loop!

slide 33 “Is” Operator uis instantiates a temporary variable Similar to a local variable in Algol-like languages uExample: defining a factorial function ?- factorial(0, 1). ?- factorial(N, Result) :- N > 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes.

slide 34 Tracing uTracing helps programmer see the dynamics of a proof search uExample: tracing a factorial call ?- factorial(0, 1). ?- factorial(N, Result) :- N > 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes. ?- trace(factorial/2). –Argument to “trace” must include function’s arity ?- factorial(4, X).

slide 35 Tracing Factorial u?- factorial(4, X). uCall: ( 7) factorial(4, _G173) uCall: ( 8) factorial(3, _L131) uCall: ( 9) factorial(2, _L144) uCall: ( 10) factorial(1, _L157) uCall: ( 11) factorial(0, _L170) uExit: ( 11) factorial(0, 1) uExit: ( 10) factorial(1, 1) uExit: ( 9) factorial(2, 2) uExit: ( 8) factorial(3, 6) uExit: ( 7) factorial(4, 24) uX = 24 These are temporary variables These are levels in the search tree

slide 36 The Cut uWhen inserted on the right-hand side of the rule, the cut operator ! operator forces subgoals not to be re-tried if r.h.s. succeeds once uExample: bubble sort bsort(L, S) :- append(U, [A, B | V], L), B < A, !, append(U, [B, A | V], M), bsort(M, S). bsort(L, L). Gives one answer rather than many

slide 37 Tracing Bubble Sort u?- bsort([5,2,3,1], Ans). uCall: ( 7) bsort([5, 2, 3, 1], _G221) uCall: ( 8) bsort([2, 5, 3, 1], _G221) u… uCall: ( 12) bsort([1, 2, 3, 5], _G221) uRedo: ( 12) bsort([1, 2, 3, 5], _G221) u… uExit: ( 7) bsort([5, 2, 3, 1], [1, 2, 3, 5]) uAns = [1, 2, 3, 5] ; uNo Without the cut, this would have given some wrong answers

slide 38 Negation in Prolog unot operator is implemented as goal failure not(G) :- G, !, fail –“fail” is a special goal that always fail What does this mean? uExample: factorial factorial(N, 1) :- N < 1. factorial(N, Result) :- not(N < 1), M is N - 1, factorial(M, P), Result is N * P.