Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.

Similar presentations


Presentation on theme: "CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones."— Presentation transcript:

1 CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones

2 Lecture 192 Overview Announcements Story Hour, Houser 108, 3PM Friday MP2 Control in Prolog More Examples

3 Lecture 193 Control in Prolog I How Prolog tries to solve a query like:,,...., This is the control side of the equation: Algorithm=Logic+Control Step 1: Find Things that solve, if none then fail else goto Step 2 Step 2: Do the things found from the previous step allow more things to be found that solve ? If not then go back to step1 else goto step 3.......

4 Lecture 194 Control in Prolog I Prolog tries to solve the clauses from left to right If there is a database file around it will use it in a similarly sequential fashion. 1. Goal Order: Solve goals from left to right. 2. Rule Order: Select the first applicable rule, where first refers to their order of appearance in the program/file/database

5 Lecture 195 Control in Prolog II The actual search algorithm is: 1. start with a query as the current goal. 2. WHILE the current goal is non-empty DO choose the leftmost subgoal ; IF a rule applies to the subgoal THEN select the first applicable rule; form a new current goal; ELSE backtrack; SUCCEED

6 Lecture 196 Control in Prolog II Note 1: Thus the order of the queries is of paramount importance. Note 2: The general paradigm in Prolog is Guess then Verify: Queries with the fewest solutions should come first, followed by those that filter or verify these few solutions

7 Lecture 197 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.

8 Lecture 198 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)))

9 Lecture 199 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).

10 Lecture 1910 Watch it work: ?- [btree]. ?- isbtree(node(9,node(3,empty,empty),empt y)). true ? yes

11 Lecture 1911 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).

12 Lecture 1912 Watch it work: ?- [btree]. ?- [mymember]. ?- member(3, node(10,node(9,node(3,empty,empty),em pty), node(12,empty,empty))). true ? yes

13 Lecture 1913 Unification Unification is a 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.

14 Lecture 1914 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

15 Lecture 1915 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).

16 Lecture 1916 Watch them work: | ?- [sublist]. consulting....sublist.pl yes | ?- sublist1([e], [a,b,c]). no | ?- sublist2([e], [a,b,c]). Fatal Error: global stack overflow …

17 Lecture 1917 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.

18 Lecture 1918 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


Download ppt "CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones."

Similar presentations


Ads by Google