1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.

Slides:



Advertisements
Similar presentations
Some Prolog Prolog is a logic programming language
Advertisements

Chapter 11 :: Logic Languages
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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 CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
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.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
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?
The AI War LISP and Prolog Basic Concepts of Logic Programming
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
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.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
Section 16.5, 16.6 plus other references
EA C461 Artificial Intelligence
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
CS 3304 Comparative Languages
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Logic Programming Languages
Chapter 11 :: Logic Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005

2 Prolog lectures Today –Overview of Prolog –Homework distributed –(also plan on spending time on Ocaml HW) Next lecture –More specifics of programming prolog How to install and use on Windows Gotchas Etc.

3 Prolog Based on first-order predicate logic Original motivation: study of mechanical theorem proving Developed in 1970 by Colmerauer & Roussel (Marseilles) and Kowalski (Edinburgh) + others. Used in Artificial Intelligence, databases, expert systems.

4 Lots of offshoots of Prolog Lots of offshoots of Prolog: Constraint logic programming: CLP(R), CHIP, Prolog III, Trilogy, HCLP, etc. Concurrent logic programming: FCP, Strand, etc etc. Concurrent constraint programming Similar ideas in spreadsheets.

5 Prolog Programs Program = a bunch of axioms Run your program by: –Enter a series of facts and declarations –Pose a query –System tries to prove your query by finding a series of inference steps “Philosophically” declarative Actual implementations are deterministic

6 Horn Clauses (Axioms) Axioms in logic languages are written: H  B1, B2,….,B3 Facts = clause with head and no body. Rules = have both head and body. Query – can be thought of as a clause with no body.

7 Terms H and B are terms. Terms = –Atoms - begin with lowercase letters: x, y, z, fred –Numbers: integers, reals –Variables - begin with captial letters: X, Y, Z, Alist –Structures: consist of an atom called a functor, and a list of arguments. ex. edge(a,b). line(1,2,4).

8 Lists [] % the empty list [1] [1,2,3] [[1,2], 3] % can be heterogeneous. The | separates the head and tail of a list: is [a | [b,c]]

9 Examples See separate page…

10 Backward Chaining START WITH THE GOAL and work backwards, attempting to decompose it into a set of (true) clauses. This is what the Prolog interpreter does.

11 Forward Chaining START WITH EXISTING FACTS and clauses and work forward, trying to derive the goal. Unless the number of facts is very small and the number of rules is large, backward chaining will probably be faster.

12 Searching the database as a tree DEPTH FIRST - finds a complete sequence of propositions for the first subgoal before working on the others. (what Prolog uses) BREADTH FIRST - works on all subgoals in parallel. The implementers of Prolog chose depth first because it can be done with a stack (expected to use fewer memory resources than breadth first).

13 Unification likes(sue,fondue). friends(X,Y) :- likes(X, Something), likes(Y, Something). % Y is a variable, find out who is friends with Sue. ?- friends(sue,Y). friends(sue,Y) :-% replace X with sue in the clause likes(sue, Something), likes(Y, Something). We replace the 1st clause in friends with the empty body of the likes(sue,fondue) clause: to get: friends(sue,Y) :- likes(Y, fondue). %- now we try to satisfy the second goal. (Finally we will return an answer to the original query like:Y=bob)

14 Backtracking search

15 Improperly ordered declarations

16 Lists member(X, [X|T]). member(X, [H|T]) :- member(X, T). ?- member(3, [1,2,3]). yes

17 Lists member(X, [X|T]). member(X, [H|T]) :- member(X, T). ?- member(X, [1,2,3]). X = 1 ; X = 2 ; X = 3 ; no

18 Lists append([], L, L). append([H|T], L, [H|L2]) :- append(T, L, L2). | ?- append([1,2], [3,4,5], X). X = [1,2,3,4,5]

19 | ?- append([1,2], Y, [1,2,3,4,5,6]). Y = [3,4,5,6] | ?- append(A,B,[1,2,3]). A = [], B = [1,2,3] ; A = [1], B = [2,3] ; A = [1,2], B = [3] ; A = [1,2,3], B = [] ; no | ?-

20 Truth table generator See separate sheet

21 Backtracking Consider a piece-wise function –if x < 3, then y = 0 –if x >=3 and x < 6, then y = 2 –if x >= 6, then y = 4 Let’s encode this in Prolog f(X,0) :- X < 3. f(X,2) :- 3 =< X, X < 6. f(X,4) :- 6 =< X.

22 Backtracking Let’s encode this in Prolog f(X,0) :- X < 3. f(X,2) :- 3 =< X, X < 6. f(X,4) :- 6 =< X. Consider ?- f(1,Y), 2<Y. This matches the f(X,0) predicate, which succeeds –Y is then instantiated to 0 –The second part (2<Y) causes this query to fail Prolog then backtracks and tries the other predicates –But if the first one succeeds, the others will always fail! –This, the extra backtracking is unnecessary

23 Backtracking Prolog then backtracks and tries the other predicates –But if the first one succeeds, the others will always fail! –This, the extra backtracking is unnecessary We want to tell Prolog that if the first one succeeds, there is no need to try the others We do this with a cut: f(X,0) :- X<3, !. f(X,2) :- 3 =< X, X<6, !. f(X,4) :- 6 =< X. The cut (‘!’) prevents Prolog from backtracking backwards through the cut

24 Backtracking New Prolog code: f(X,0) :- X<3, !. f(X,2) :- 3 =< X, X<6, !. f(X,4) :- 6 =< X. Note that if the first predicate fails, we know that x >= 3 –Thus, we don’t have to check it in the second one. –Similarly with x>=6 for the second and third predicates Revised Prolog code: f(X,0) :- X<3, !. f(X,2) :- X<6, !. f(X,4).

25 Backtracking What if we removed the cuts: f(X,0) :- X<3. f(X,2) :- X<6. f(X,4). Then the following query: ?- f(1,X). Will produce three answers (0, 2, 4)

26 Examples using a cut Maximum of two values without a cut: max(X,Y,X) :- X >= Y. max(X,Y,Y) :- X<Y. Maximum of two values with a cut: max(X,Y,X) :- X >= Y, !. max(X,Y,Y).

27 A mini-calculator calc(X,X) :- number(X). calc(X*Y) :- calc(X,A), calc(Y,B), Z is A*B. calc(X+Y) :- calc(X,A), calc(Y,B), Z is A+B. etc.