1 prolog :-in(2014).. 2 Prolog Based on first-order predicate calculus – Allow Horn clauses (a subset of predicate calculus) only – Execution of a Prolog.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

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.
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.
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).
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques 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
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
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.
CS 321 Programming Languages and Compilers Prolog part 2.
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.
Introduction to Logic Programming with Prolog (Section 11.3)
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
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 prolog :-in(2009).. 2 Prolog Based on first-order predicate calculus –Allow Horn clauses (a subset of predicate calculus) only –Execution of a Prolog.
1 Topics Covers everything –1) in slides –2) from the very beginning. Language classification, history, and comparison Programming paradigms –Structured.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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 CSC 358/ Outline Pattern matching Unification Logic programming.
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.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
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:... :-....
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Logic Programming Languages
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
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
Chapter 2: Prolog (Introduction and Basic Concepts)
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

1 prolog :-in(2014).

2 Prolog Based on first-order predicate calculus – Allow Horn clauses (a subset of predicate calculus) only – Execution of a Prolog program is an application of theorem proving Original motivation: study of mechanical theorem proving Developed in 1970 by Colmerauer & Roussel (Marseilles) and Kowalski (Edinburgh) + others. – The name Prolog is taken from PROgramming in LOGic. Used in artificial intelligence, databases, expert systems. – Non numeric applications Popular in Europe Adopted by Japan for its 5th Generation Computer program in 80’s Viewed as competitor for Lisp

3 Prolog is a declarative programming language Express programs in a form of symbolic logic and use a logical inferencing process to produce results Logic programs are declarative rather than procedural – only the specification of the desired results are stated rather detailed procedures for producing them

4 Run Prolog Popular Prolog implementations – GNU Prolog, SWI Prolog, …. SWI Prolog is installed on Luna machine – The command to run swi prolog: pl – The command to run gprolog: gprolog Edit the program suppose the file sample.pl consists of the following Prolog program: likes(john, mary). Run the program (red part are user inputs) luna:~/440/prolog>gprolog GNU Prolog By Daniel Diaz Copyright (C) Daniel Diaz | ?- consult(‘sample.pl’). compiling /fac2/jlu/440/prolog/sample.pl for byte code... /fac2/jlu/440/prolog/sample.pl compiled, 47 lines read bytes written, 33 ms (10 ms) yes | ?- likes(john, mary). yes | ?- likes(john, Whom). Whom = mary yes | ?- likes(john, george). No

5 Prolog program and query In Prolog you define a set of facts and rules, then you can query based on your definitions; The basic unit of your program is predicate; A predicate consists of a name and a sequence of arguments – likes(john, mary). – “ likes ” is the predicate name, john and mary are two arguments of the predicate. Once you enter this fact into the system, you can ask two types of questions: – true/false question: ?-like(john, mary). does john like mary? – what question: ?-likes(john, X). whom does john like? The X here is a variable.

6 Components of Prolog programs--facts A fact has the form p(t 1,...,t n ). Examples: likes(john, mary). parent(adam, bill). A fact is part of a program. Syntactic conventions – It is case sensitive: –Predicate and atom names must begin with a lowercase letter; – Every statement end with a dot “.”.

7 Facts with variables Variables starts with uppercase letters. Facts can have variables likes(john, mary). likes(dwight, X). % dwight likes anybody (or anything). %  X. likes(dwight, X). | ?- likes(dwight, mary). yes | ?- likes(dwight, john). yes | ?- likes(dwight, a). yes

8 Rules The second type of statements is the rule. likes(john, X):-likes(mary, X). – The symbol “:-” means “if”. – This rules means that if mary likes X, then john also likes X. – Rules make use of variables.  X. ( likes(mary, X)  likes(john, X)). Running example likes(john, mary). likes(dwight, X). likes(mary, sue). likes(john, X):-likes(mary, X). – | ?- likes(john, X). – X = mary ? ; – X = sue – yes – | ?- – Prolog will give you one answer for X; – If you hit return it will stop looking for more answers; – If you type “;”, it will look for another answer; – Prolog marks the database where it finds answers in order to start looking again.

9 Rule: clause body can have multiple predicates father(X,Y) :- parent(X,Y), male(X). The antecedent and consequent are in reverse order to that normally found in logic: parent(X,Y) ∧ male(X)  father(X,Y) – the consequent is written first and called the head of the rule; – the antecedent is called the body; – Conjunction (and) is written as ",“. parent(bill, dwight). male(bill). father(X,Y):-parent(X,Y), male(X). | ?- father(bill, X). X = dwight

10 General form of a rule (Horn Clause) A :- B1, B2,...., Bn. meaning A is true if B1 and B2 and... Bn are all true Clause: a disjunction of literals Horn clause: if it contains at most one positive literal.

11 Evaluation--unification When the interpreter is given a query, it tries to find facts that match the query. likes(john, mary). likes(john, sue). | ?- likes(john, X). Iterates in order through the program’s “likes” clauses – First match is likes(john, mary), so it returns X=mary. – If you hit semicolon “;”, it continues and find the next match. So it returns X=sue. Unification likes(john, mary). | | likes(john, X). Unification is a two-way match likes(Y, mary). | | likes(john, X). likes(Y, mary). | ?- likes(john, X). X=mary

12 Evaluation—backward chaining If no outright facts are available, it attempts to satisfy all rules that have the fact as a conclusion. For example: parent(bill, dwight). male(bill). father(X,Y):-parent(X,Y), male(X). | ?- trace. | ?- father(bill, W). 1 1 Call: father(bill,_16) ? 2 2 Call: parent(bill,_16) ? X=bill 2 2 Exit: parent(bill,dwight) ? Y=dwight 3 2 Call: male(bill) ? 3 2 Exit: male(bill) ? 1 1 Exit: father(bill,dwight) ? W = dwight yes

13 Recursive rules How to define the ancestor predicate? ancestor(X,Y):-parent(X,Y). ancestor(X,Y):-parent(X, U), parent(U, Y). ancestor(X,Y):-parent(X, U), parent(U, V), parent(V, W), parent(W, Y). … Prolog is not a pure logic programming language. Solution ancestor(X,Y):-parent(X,Y). ancestor(X,Z):-parent(X,Y), ancestor(Y,Z). parent(bill, dwight). parent(dwight, ryan). parent(dwight, samantha). parent(dwight, sue). | ?-ancestor(bill, X). X=dwight; X=ryan; X=samantha; X=sue; Another solution? – ancestor(X,Y):-parent(X,Y). – ancestor(X,Z):-ancestor(X, Y), parent(Y,Z).

14 Evaluation: infinite loop

15 The correct program: edge(a,b). edge(b,c). edge(c,d). edge(d,e). edge(b,e). edge(d,f). path(X,Y):-edge(X,Z), path(Z,Y). path(X,X).

16 Evaluation: depth first search | ?- f(3,X). 1 1 Call: f(3,_16) ? 2 2 Call: 3>0 ? 2 2 Exit: 3>0 ? 3 2 Call: _113 is 3-1 ? 3 2 Exit: 2 is 3-1 ? 4 2 Call: f(2,_138) ? 5 3 Call: 2>0 ? 5 3 Exit: 2>0 ? 6 3 Call: _190 is 2-1 ? 6 3 Exit: 1 is 2-1 ? 7 3 Call: f(1,_215) ? 8 4 Call: 1>0 ? 8 4 Exit: 1>0 ? 9 4 Call: _267 is 1-1 ? 9 4 Exit: 0 is 1-1 ? 10 4 Call: f(0,_292) ? 10 4 Exit: f(0,1) ? 11 4 Call: _320 is 1*1 ? 11 4 Exit: 1 is 1*1 ? 7 3 Exit: f(1,1) ? 12 3 Call: _349 is 2*1 ? 12 3 Exit: 2 is 2*1 ? 4 2 Exit: f(2,2) ? 13 2 Call: _16 is 3*2 ? 13 2 Exit: 6 is 3*2 ? 1 1 Exit: f(3,6) ? f(0, 1). f(N, F):-N>0, N1 is N-1, f(N1, F1), F is N*F1.

17 Evaluation: backtracking search

18 | ?- snowy(C). 1 1 Call: snowy(_16) ? 2 2 Call: rainy(_16) ? 2 2 Exit: rainy(seattle) ? 3 2 Call: cold(seattle) ? 3 2 Fail: cold(seattle) ? 2 2 Redo: rainy(seattle) ? 2 2 Exit: rainy(rochester) ? 3 2 Call: cold(rochester) ? 3 2 Exit: cold(rochester) ? 1 1 Exit: snowy(rochester) ? C = rochester

19 Review Syntax: facts, rules. Horn clause f(0, 1). f(N, F):-N>0, N1 is N-1, f(N1, F1), F is N*F1. Evaluation – Unification – Backward chaining – Recursion – Depth first search – Backtracking

20 Datalog and Prolog What we’ve seen so far is called Datalog: “databases in logic.” Prolog is “programming in logic.” It goes a little bit further by allowing complex terms, including records, lists and trees. These complex terms are the source of the hard thing about Prolog, “unification.” at_uwindsor(student(128327, ‘Spammy K',date(2, may, 1986))). at_uwindsor (student(126547, ‘Blobby B’, date(15, dec, 1985))). at_uwindsor (student(456591, ‘Fuzzy W', date(23, aug, 1966))). It is no longer a simple fact at_uwindsor(snammy).

at_uwindsor(student(128327, ‘Spammy K',date(2, may, 1986))). at_uwindsor (student(126547, ‘Blobby B’, date(15, dec, 1985))). at_uwindsor (student(456591, ‘Fuzzy W', date(23, aug, 1966))). | ?- at_uwindsor(student(IDNum, Name, date(Day,Month,Year))), Year < Day = 23 IDNum = Month = aug Name = 'Fuzzy W' Year =

at_uwindsor(student(128327, ‘Spammy K',date(2, may, 1986))). at_uwindsor (student(126547, ‘Blobby B’, date(15, dec, 1985))). at_uwindsor (student(456591, ‘Fuzzy W', date(23, aug, 1966))). | ?- at_uwindsor(student(_, Name, date(_,_,Year))), Year < Name = 'Fuzzy W' Year = 1966 | ?- at_uwindsor(Person), Person=student(_,_,Birthday), Birthday=date(_,_,Year), Year < Birthday = date(23,aug,1966) Person = student(456591,'Fuzzy W',date(23,aug,1966)) Year =

23 List data structure built into prolog. – [ ] % the empty list – [1] – [1,2,3] | separate the head and tail of a list – [a | b] adds the first argument a to the list b. [[1,2], 3] % a list can be heterogeneous. All the following are the same as [a, b, c] [a, b, c|[]] [a, b| [c]] [a | [b, c]]

24 Decompose a list How to get the first element of a list? (car in scheme) – first(X, List) :- …? Solution(s) first(X, List) :- List=[X|Xs]. first(X, [X|Xs]). first(X, [X|_]). –eliminate the single-use variable Xs. Query: first(8, [7,8,9]). Answer: no Query: first(X, [7,8,9]). Answer: X=7 How to define rest of a list? rest(Xs, List):-? rest(Xs, [_|Xs]).

25 List processing (append) append([],Ys): return Ys append([X|Xs],Ys): return [X | append(Xs,Ys)] In Prolog there are no return values. Rather, the return value is a third argument: append(Xs,Ys,Result). This is a constraint saying that Result must be the append of the other lists. Any of the three arguments may be known (or partly known) at runtime. We look for satisfying assignments to the others. append(Xs,Ys,Result) is true if Xs and Ys are lists and Result is their concatenation (another list). Try this: append([],Ys,Ys). append([X|Xs],Ys,Result) :- … ? append([], Ys, Ys). append([H|T], L, [H|L2]) :- append(T, L, L2). | ?- append([1,2], [3,4,5], X). X = [1,2,3,4,5]

26 Run the program backwards In Prolog, once you’ve written it, you can also run it backwards! | ?- 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

27 List processing--length len([],0). len([H|T], X) :- len(T, Y), X is Y + 1. | ?- len([1,2,3],X). 1 1 Call: len([1,2,3],_22) ? 2 2 Call: len([2,3],_91) ? 3 3 Call: len([3],_115) ? 4 4 Call: len([],_139) ? 4 4 Exit: len([],0) ? 5 4 Call: _167 is 0+1 ? 5 4 Exit: 1 is 0+1 ? 3 3 Exit: len([3],1) ? 6 3 Call: _196 is 1+1 ? 6 3 Exit: 2 is 1+1 ? 2 2 Exit: len([2,3],2) ? 7 2 Call: _22 is 2+1 ? 7 2 Exit: 3 is 2+1 ? 1 1 Exit: len([1,2,3],3) ? X = 3 yes

28 List processing: member member(X,Y) should be true if X is any object, Y is a list, and X is a member of the list Y. member(X, [X|_]). % same as first(..). member(X, [H|T]) :- member(X, T). ?- member(3, [1,2,3]). %member test yes ?- member(X, [1,2,3]). % member generator X = 1 ; X = 2 ; X = 3 ; No ?-member(2, X). % list generator X = [2|_] ? ; X = [_,2|_] ? ; X = [_,_,2|_] ? ; X = [_,_,_,2|_] ? …

29 Query: member(7, List), member(8,List), length(List, 3). – Answer: doesn’t terminate Query: length(List, 3), member(7, List), member(8,List). – Answer: List=[7, 8, X] ; List=[7, X, 8] ; List=[8, 7, X] ; List=[X, 7, 8] ; List=[8, X, 7] ; List=[X, 8, 7] Again, only “theoretically” Prolog does not depend on the orders of predicates and clauses.

30 Example: test subset relationship subset([ ],Y). subset([A|X],Y) :- member(A,Y), subset(X,Y). | ?- subset ([a, b, c], [a, c, d, b]). true ?

31 Example: nth element find_nth([H|T], 1, H). find_nth([H|T], N, X):- N1 is N-1, find_nth(T,N1,X). | ?- find_nth([a, b, c, d], 2, X). X = b ?

32 Example: what are the changes for a dollar? % suppose there are half-dollars, quarters, dimes, nickels. P is the purchase change ([H,Q,D,N,P]) :- member(H,[0,1,2]), // Half-dollars member(Q,[0,1,2,3,4]), // quarters member(D,[0,1,2,3,4,5,6,7,8,9,10]), // dimes member(N, [0,1,2,3,4,5,6,7,8,9,10, // nickels 11,12,13,14,15,16,17,18,19,20]), S is 50*H + 25*Q +10*D + 5*N, S =< 100, P is 100-S. | ?- change([H,Q,D,N,P]). D = 0 H = 0 N = 0 P = 100 Q = 0 ? ; D = 0 H = 0 N = 1 P = 95 Q = 0 ? ; D = 0 H = 0 N = 2 P = 90 Q = 0 ? … | ?- change([H, Q,D,N,20]). D = 0 H = 0 N = 16 Q = 0 ? ; D = 1 H = 0 N = 14 Q = 0 ? ; D = 2 H = 0 N = 12 Q = 0 ?

33 | ?- isort([1, 3, 2], X). 1 1 Call: isort([1,3,2],_22) ? 2 2 Call: isort([3,2],_91) ? 3 3 Call: isort([2],_115) ? 4 4 Call: isort([],_139) ? 4 4 Exit: isort([],[]) ? 5 4 Call: insert(2,[],_165) ? 5 4 Exit: insert(2,[],[2]) ? 3 3 Exit: isort([2],[2]) ? 6 3 Call: insert(3,[2],_194) ? 7 4 Call: 3=<2 ? 7 4 Fail: 3=<2 ? 7 4 Call: 3>2 ? 7 4 Exit: 3>2 ? 8 4 Call: insert(3,[],_181) ? 8 4 Exit: insert(3,[],[3]) ? 6 3 Exit: insert(3,[2],[2,3]) ? 2 2 Exit: isort([3,2],[2,3]) ? 9 2 Call: insert(1,[2,3],_22) ? 10 3 Call: 1=<2 ? 10 3 Exit: 1=<2 ? 9 2 Exit: insert(1,[2,3],[1,2,3]) ? 1 1 Exit: isort([1,3,2],[1,2,3]) ? Insertion sort isort([ ],[ ]). isort([X|UnSorted], AllSorted) :- isort(UnSorted, Sorted), insert(X, Sorted, AllSorted). insert(X, [ ], [X]). insert(X, [Y|L], [X, Y|L]) :- X =< Y. insert(X, [Y|L], [Y|IL]) :- X > Y, insert(X, L, IL).

34 A more declarative sort program sort(List, Sorted):-permutation(List, Sorted), is_sorted(Sorted). is_sorted([ ]). is_sorted([ _ ]). is_sorted([X,Y|T]):-X<=Y, is_sorted([Y|T]). perm([ ], [ ]). perm(List, [H|Perm]) :-takeout(H, List, Rest), perm(Rest, Perm). takeout(X, [X|Xs], Xs). takeout(X, [Y|Xs], [Y|Ys]):-takeout(X,Xs,Ys).

35 Summary of 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

36 Questions?