CS 321 Programming Languages and Compilers Prolog part 2.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Chapter 11 :: Logic Languages
Declarative Programming Lists in PROLOG Autumn 2014.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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).
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
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.
Inference and Resolution for Problem Solving
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Induction and recursion
Induction and recursion
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 CS Programming Languages Class 19 November 2, 2000.
ICOM 4036: PROGRAMMING LANGUAGES Lecture 5 Logic Programming 1/23/2016.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
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.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Prolog Programming.
Recursive stack-based version of Back-chaining using Propositional Logic
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Tests, Backtracking, and Recursion
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
PROGRAMMING IN HASKELL
Programming Techniques
Chapter 12 :: Logic Languages
Presentation transcript:

CS 321 Programming Languages and Compilers Prolog part 2

Prolog 2 Binary Search Trees I An example of user defined data structures. The Problem: Recall that a binary search tree (with integer labels) is either : 1. the empty tree empty,or 2. a node labelled with an integer N, that has a left subtree and a right subtree, each of which is a binary search tree such that the nodes in the left subtree are labelled by integers strictly smaller than N, while those in the right subtree are strictly greater than N.

Prolog 3 Data Types in Prolog The primitive data types in prolog can be combined via structures,to form complex datatypes: ::= (,,...) Example In the case of binary search trees we have: ::= empty | node(,, ) node(15,node(2,node(0,empty,empty), node(10,node(9,node(3,empty,empty), empty), node(12,empty,empty))), node(16,empty,node(19,empty,empty)))

Prolog 4 Binary Search Trees II The Problem: Define a unary predicate isbstree which is true only of those trees that are binary search trees. The Program isbtree(empty). isbtree(node(N,L,R)):- number(N),isbtree(L),isbtree(R), smaller(N,R),bigger(N,L). smaller(N,empty). smaller(N, node(M,L,R)) :- N < M, smaller(N,L), smaller(N,R). bigger(N, empty). bigger(N, node(M,L,R)) :- N > M, bigger(N,L), bigger(N,R).

Prolog 5 Watch it work: | ?- [btree]. | ?- isbtree(node(9,node(3,empty,empty),empty)). true ? yes

Prolog 6 Binary Search Trees III The Problem: Define a relation which tells whether a particular number is in a binary search tree. mymember(N,T) should be true if the number N is in the tree T. The Program mymember(K,node(K,_,_)). mymember(K,node(N,S,_)) :- K < N,mymember(K,S). mymember(K,node(N,_,T)) :- K > T,mymember(K,T).

Prolog 7 Watch it work: | ?- [btree]. | ?- [mymember]. | ?- member(3, node(10,node(9,node(3,empty,empty),empty), node(12,empty,empty))). true ? yes

Prolog 8 Examples (1): list list(Xs):- Xs is a list. list([]). list([X|Xs]):-list(Xs).

Prolog 9 Examples (2): member member(Element,List):-Element is an element of the list List. member(X,[X|Xs]). member(X,[Y|Ys]):-member(X,Ys).

Prolog 10 Examples (3): prefix prefix(Prefix,List):- Prefix is a prefix of List. prefix([],Ys). prefix([X|Xs],[X|Ys]):-prefix(Xs,Ys).

Prolog 11 Examples (4): suffix suffix(Suffix,List):- Suffix is a suffix of List. suffix(Xs,Xs). suffix(Xs,[Y|Ys]):-suffix(Xs,Ys).

Prolog 12 Examples (5): sublist sublist(Sub,List):- Sub is a sublist of List. a. Suffix of a prefix sublist(Xs,Ys):-prefix(Ps,Ys),suffix(Xs,Ps) b. Prefix of a suffix sublist(Xs,Ys):-prefix(Xs,Ss),suffix(Ss,Ys) c. Recursive definition of a sublist sublist(Xs,Ys):-prefix(Xs,Ys) sublist(Xs,[Y|Ys]:-sublist(Xs,Ys)

Prolog 13 Examples (6): member using sublist Member(X,Xs):-sublist([X],Xs).

Prolog 14 Examples (7): Suffix using append append(Xs,Ys,XsYs):- XsYs is the result of concatenating the lists Xs and Ys. append([],Xs,Ys). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs)

Prolog 15 Examples (8): Sublist using append d. Suffix of a prefix using append sublist(Xs,AsXsBs):- append(As,XsBs,AsXsBs),append(Xs,Bs,XsBs) e. Prefix of a suffix using append sublist(Xs,AsXsBs):- append(AsXs,Bs,AsXsBs),append(As,Xs,AsXs)

Prolog 16 Examples (9): Prefix, member and adjacent using append prefix(Xs,Ys):-append(Xs,As,Ys). suffix(Xs,Ys):-append(As,Xs,Ys). member(X,Ys):-append(As,[X|Xs],Ys). adjacent(X,Y,Zs):-append(As,[X,Y|Ys],Zs).

Prolog 17 Examples (10): Reversing a list reverse(List,Tsil):- Tsil is the result of reversing List. a. Naïve reverse reverse([],[]). reverse([X|Xs],Zs):-reverse(Xs,Ys),append(Ys,[X],Zs). b. Reverse-accumulate reverse(Xs,Ys):-reverse(Xs,[],Ys). reverse([X|Xs],Acc,Ys):-reverse(Xs,[X|Acc],Ys). reverse([],Ys,Ys).

Prolog 18 Unification Unification is a (slightly) more general form of pattern matching. In that pattern variables can appear in both the pattern and the target. The following summarizes how unification works: 1. a variable and any term unify 2. two atomic terms unify only if they are identical 3. two complex terms unify if they have the same functor and their arguments unify.

Prolog 19 Prolog Search Trees Summary 1. Goal Order affects solutions 2. Rule Order affects Solutions 3. Gaps in Goals can creep in 4. More advanced Prolog programming manipulates the searching

Prolog 20 Sublists (Goal Order) Two definitions of S being a sublist of Z use: myappend([], Y, Y). myappend([H|X], Y, [H|Z]) :- myappend(X,Y,Z). & myprefix(X,Z) :- myappend(X,Y,Z). mysuffix(Y,Z) :- myappend(X,Y,Z). Version 1 sublist1(S,Z) :- myprefix(X,Z), mysuffix(S,X). Version 2 sublist2(S,Z) :- mysuffix(S,X), myprefix(X,Z). Version 3 sublist3(S,Z) :- mysuffix(Y,Z), myprefix(S,Y).

Prolog 21 Watch them work: | ?- [sublist]. consulting....sublist.plyes | ?- sublist1([e], [a,b,c]). no | ?- sublist2([e], [a,b,c]). Fatal Error: global stack overflow …

Prolog 22 Version 1 So what’s happening? If we ask the question: sublist1([e], [a,b,c]). this becomes prefix(X,[a,b,c]), suffix([e],X). and using the guess-query idea we see that the first goal will generate four guesses: [] [a] [a,b] [a,b,c] none of which pass the verify goal, so we fail.

Prolog 23 Version 2 On the other hand, if we ask the question: sublist2([e], [a,b,c]) this becomes suffix([e],X),prefix(X,[a,b,c]). using the guess-query idea note: Goal will generate an infinite number of guesses. [e] [_,e] [_,_,e] [_,_,_,e] [_,_,_,_,e] [_,_,_,_,_,e].... None of which pass the verify goal, so we never terminate

Prolog 24 Prolog Search Trees (Rule Order) Rule Order affects Solutions Compare the two versions of append append([], Y, Y). append([H|X], Y, [H|Z]) :- append(X,Y,Z). append2([H|X], Y, [H|Z]) :- append2(X,Y,Z). append2([], Y, Y).

Prolog 25 Watch it: | ?- [append]. consulting....append.pl yes | ?- append(X,[c],Z). X = [] Z = [c]?; X = [A] Z = [A,c]?; X = [A,B] Z = [A,B,c]? yes | ?- append2(X,[c],Z). Fatal Error: local stack overflow …

Prolog 26 Prolog Search Trees Sometimes Prolog Tries TOO HARD The Programs: fac(0,1). fac(N,M) :- N1 is N-1, fac(N1,M1), M is N * M1. The Problem: | ?- [fac]. consulting....fac.pl yes | ?- fac(4,X). X = 24? yes | ?- fac(4,X), X=55. Fatal Error: local stack overflow … What's Happening??

Prolog 27 What’s Happening? {trace} | ?- fac(2,X), X= Call: fac(2,_15) ? 2 2 Call: _93 is 2-1 ? 2 2 Exit: 1 is 2-1 ? 3 2 Call: fac(1,_118) ? 4 3 Call: _145 is 1-1 ? 4 3 Exit: 0 is 1-1 ? 5 3 Call: fac(0,_170) ? 5 3 Exit: fac(0,1) ? 6 3 Call: _198 is 1*1 ? 6 3 Exit: 1 is 1*1 ? 3 2 Exit: fac(1,1) ? 7 2 Call: _15 is 1*2 ? 7 2 Exit: 2 is 1*2 ? 1 1 Exit: fac(2,2) ?

Prolog 28 What’s Happening? 8 1 Call: 2=55 ? 8 1 Fail: 2=55 ? 1 1 Redo: fac(2,2) ? 3 2 Redo: fac(1,1) ? 5 3 Redo: fac(0,1) ? 6 4 Call: _197 is 0-1 ? 6 4 Exit: -1 is 0-1 ? 7 4 Call: fac(-1,_222) ?

Prolog 29 Solution to this Problem: The Cut. A cut, written ! is a goal that always succeeds when reached it alters the subsequent search tree.

Prolog 30 Cut Defined B :- C1,... CK, !, C(K+1),... CJ. When applied during a search, and the cut ! is reached, then if at some later stage this rule fails, then Prolog backtracks past the CK,... C1, B without considering anymore rules for them. In particular, this and any subsequent rule for B will not be tried.

Prolog 31 Cut Example So using the cut in the guess -verify clause style: conclusion(S) :- guess(S), !, verify(S). eliminates all but the first guess

Prolog 32 The solution for Factorial: Want to stipulate “Once you’ve found the solution A=1 for ‘fac(0,A)’, don’t look for any others.” fac(0,1) :- !. fac(N,M) :- N1 is N-1, fac(N1,M1), M is N * M1.

Prolog 33 Negation as Unsatisfiability Consider the predicate can_marry(X,Y) meaning that X can legally marry Y provided that they are no more closely related than first cousins. can_marry(X,Y) :- X\=Y, nonsibling(X,Y), noncousin(X,Y). where nonsibling(X,Y):-X=Y. nonsibling(X,Y):-mother(M1,X),mother(M2,Y),M1\=M2. nonsibling(X,Y):-father(F1,X),father(F2,Y),F1\=F2. but | ?- nonsibling(albert,alice). no

Prolog 34 What’s Going On? For nonsibling(X,Y) to be true, it must be that X (and Y) have a common parent. However, albert has no parents stated as facts in the database. Therefore, nonsibling(albert,Y) fails. Prolog uses a Closed World Model. We might try writing nonsibling(X,Y) :- no_parent(X). nonsibling(X,Y) :- no_parent(Y). But how can we express the absence of a fact in the database? We could add no_parent(albert). … But that’s tedious.

Prolog 35 What’s Going On? We’d like to define nonsibling(X,,Y) to be true whenever sibling(X,Y) is false, but something like nonsibling(X,Y) :- \ sibling(X,Y). is non-Horn

Prolog 36 A “Solution” The apparent solution in Prolog is to use the “cut- fail” combination to simulate negation. nonsibling(X,Y) :- sibling(X,Y), !, fail. nonsibling(X,Y). so nonsibling(jeffrey,george) will fail, since sibling(jeffrey,george) succeeds, ! succeeds, and fail fails (but ! prevents any further backtracking). nonsibling(albert,alice) will succeed, since sibling(albert,alice) fails, causing the first rule to fail; then the second rule succeeds (always).

Prolog 37 Generalized “not” predicate Define not(A) :- call(A), !, fail. not(_). so can_marry(A,B) :- A\=B, not(sibling(A,B)), not(cousin(A,B)). But caution -- not doesn’t behave exactly like logical negation. It merely means “not satisfiable”, I.e. “unable to be proved true.”