Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 11 Memory Management C makes it easy to shoot.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Prolog.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Programming Languages 2e Tucker and Noonan
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
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/
Programming Languages Third Edition
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
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 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
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 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Slide 1 Vitaly Shmatikov CS 345 Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Evolution of Programming Languages Generations of PLs.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
F28PL1 Programming Languages Lecture 16: Prolog 1.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
© 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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Logic Programming and Declarative Paradigm. Declarative Programming Declare goals, not actions Query by Example HTML Prolog –AI work –Natural Language.
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.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R N I N E Logic Programming.
Programming Languages 2nd edition Tucker and Noonan
C H A P T E R N I N E Logic Programming.
Programming Techniques
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does a dog have if you call its tail a leg? A: Four. Calling a tail a leg doesn’t make it one. Abraham Lincoln

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 15.1 Logic and Horn Clauses 15.2 Logic Programming in Prolog Prolog Program Elements Practical Aspects of Prolog 15.3 Prolog Examples Symbolic Differentiation Solving Word Puzzles Natural Language Processing Semantics of Clite Eight Queens Problem

Copyright © 2006 The McGraw-Hill Companies, Inc Practical Aspects of Prolog Tracing The Cut Negation The is, not, and Other Operators The Assert Function

Copyright © 2006 The McGraw-Hill Companies, Inc. Tracing To see the dynamics of a function call, the trace function can be used. E.g., if we want to trace a call to the following function: factorial(0, 1). factorial(N, Result) :- N > 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes. we can activate trace and then call the function: ?- trace(factorial/2). ?- factorial(4, X). Note: the argument to trace must include the function’s arity.

Copyright © 2006 The McGraw-Hill Companies, Inc. Tracing Output ?- factorial(4, X). Call: ( 7) factorial(4, _G173) Call: ( 8) factorial(3, _L131) Call: ( 9) factorial(2, _L144) Call: ( 10) factorial(1, _L157) Call: ( 11) factorial(0, _L170) Exit: ( 11) factorial(0, 1) Exit: ( 10) factorial(1, 1) Exit: ( 9) factorial(2, 2) Exit: ( 8) factorial(3, 6) Exit: ( 7) factorial(4, 24) X = 24 These are temporary variables These are levels in the search tree

Copyright © 2006 The McGraw-Hill Companies, Inc. The Cut The cut is an operator (!) inserted on the right-hand side of a rule. semantics: the cut forces those subgoals not to be retried if the right-hand side succeeds once. E.g (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). So this code gives one answer rather than many.

Copyright © 2006 The McGraw-Hill Companies, Inc. Bubble Sort Trace ?- bsort([5,2,3,1], Ans). Call: ( 7) bsort([5, 2, 3, 1], _G221) Call: ( 8) bsort([2, 5, 3, 1], _G221) … Call: ( 12) bsort([1, 2, 3, 5], _G221) Redo: ( 12) bsort([1, 2, 3, 5], _G221) … Exit: ( 7) bsort([5, 2, 3, 1], [1, 2, 3, 5]) Ans = [1, 2, 3, 5] ; No Without the cut, this would have given some wrong answers.

Copyright © 2006 The McGraw-Hill Companies, Inc. The is Operator is instantiates a temporary variable. E.g., in factorial(0, 1). factorial(N, Result) :- N > 0, M is N - 1, factorial(M, SubRes), Result is N * SubRes. Here, the variables M and Result are instantiated This is like an assignment to a local variable in C-like languages.

Copyright © 2006 The McGraw-Hill Companies, Inc. Other Operators Prolog provides the operators + - * / ^ = >= =< \= with their usual interpretations. The not operator is implemented as goal failure. E.g., factorial(N, 1) :- N < 1. factorial(N, Result) :- not(N < 1), M is N - 1, factorial(M, P), Result is N * P. is equivalent to using the cut ( ! ) in the first rule.

Copyright © 2006 The McGraw-Hill Companies, Inc. The assert Function The assert function can update the facts and rules of a program dynamically. E.g., if we add the following to the foregoing database program: ?- assert(mother(jane, joe)). Then the query: ?- mother(jane, X). gives: X = ron ; X = joe; No F

Copyright © 2006 The McGraw-Hill Companies, Inc Prolog Examples 1.Symbolic Differentiation Symbol manipulation and logical deduction united 2.Solving Word Problems Nondeterminism seeks all solutions, not just one 3. Natural Language Processing One of Prolog’s traditional research applications 4.Semantics of Clite Declarative languages help model rapid designs 5.Eight Queens Problem Exploiting Prolog’s natural backtracking mechanism

Copyright © 2006 The McGraw-Hill Companies, Inc Symbolic Differentiation Symbolic Differentiation Rules Fig 15.9

Copyright © 2006 The McGraw-Hill Companies, Inc. Prolog Encoding 1.Uses Infix notation E.g., 2x + 1 is written as 2*x+1 2.Function d incorporates these rules. E.g., d(x,2*x+1, Ans) should give an answer. 3.However, no simplification is performed. E.g. the answer for d(x,2*x+1, Ans) is 2*1+x*0+0 which is equivalent to the simplified answer, 2.

Copyright © 2006 The McGraw-Hill Companies, Inc. Prolog Program d(X, U+V, DU+DV) :- d(X, U, DU), d(X, V, DV). d(X, U-V, DU-DV) :- d(X, U, DU), d(X, V, DV). d(X, U*V, U*DV + V*DU) :- d(X, U, DU), d(X, V, DV). d(X, U/V, (V*DU - U*DV)/(V*V)) :- d(X, U, DU), d(X, V, DV). d(X, C, 0) :- atomic(C), C\=X. d(X, X, 1).

Copyright © 2006 The McGraw-Hill Companies, Inc. Search Tree for d(x, 2*x+1, Ans)

Copyright © 2006 The McGraw-Hill Companies, Inc Solving Word Problems A simple example: Baker, Cooper, Fletcher, Miller, and Smith live in a five-story building. Baker doesn't live on the 5th floor and Cooper doesn't live on the first. Fletcher doesn't live on the top or the bottom floor, and he is not on a floor adjacent to Smith or Cooper. Miller lives on some floor above Cooper. Who lives on what floors? We can set up the solution as a list of five entries: [floor(_,5), floor(,4), floor(_,3), floor(_,2), floor(_,1)] The don’t care entries are placeholders for the five names.

Copyright © 2006 The McGraw-Hill Companies, Inc. Modeling the solution We can identify the variables B, C, F, M, and S with the five persons, and the structure floors(Floors) as a function whose argument is the list to be solved. Here’s the first constraint: member(floor(baker, B), Floors), B\=5 which says that Baker doesn't live on the 5th floor. The other four constraints are coded similarly, leading to the following program:

Copyright © 2006 The McGraw-Hill Companies, Inc. Prolog solution floors([floor(_,5),floor(_,4),floor(_,3),floor(_,2), floor(_,1)]). building(Floors) :- floors(Floors), member(floor(baker, B), Floors), B \= 5, member(floor(cooper, C), Floors), C \= 1, member(floor(fletcher, F), Floors), F \= 1, F \= 5, member(floor(miller, M), Floors), M > C, member(floor(smith, S), Floors), not(adjacent(S, F)), not(adjacent(F, C)), print_floors(Floors).

Copyright © 2006 The McGraw-Hill Companies, Inc. Auxiliary functions Floor adjacency: adjacent(X, Y) :- X =:= Y+1. adjacent(X, Y) :- X =:= Y-1. Note: =:= tests for numerical equality. Displaying the results: print_floors([A | B]) :- write(A), nl, print_floors(B). print_floors([]). Note: write is a Prolog function and nl stands for “new line.” Solving the puzzle is done with the query: ?- building(X). which finds an instantiation for X that satisfies all the constraints.