1 / 26

Programming Paradigms and Languages

Programming Paradigms and Languages. Using Data Structures and Backtracking and the ”Cut”. Linkon Islam Justin Prabhu Kamran Mammadli. Introduction ● Structures and trees ● Lists ● Recursive search ● Mapping ● Joining structures ● Accumulators ● Backtracking ● Cut.

tmcgill
Download Presentation

Programming Paradigms and Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms and Languages Using Data Structures and Backtracking and the ”Cut” Linkon Islam Justin Prabhu Kamran Mammadli

  2. Introduction ● Structures and trees ● Lists ● Recursive search ● Mapping ● Joining structures ● Accumulators ● Backtracking ● Cut

  3. Structures and trees • Trees can be a very convenient way to represent some Prolog data structures. • parents(charles, elizabeth, philip).

  4. Tree representation example • “John loves Mary” - how to represent syntax? sentence(noun(X),verb_phrase(verb(Y),noun(Z)))

  5. LISTS The list is an ordered sequence of elements that can have any length List consists of head and tail Empty list - []

  6. Split list into head and tail p([1,2,3]). p([the,cat,sat,[on,the,mat]]) ?-p(X|Y). X = 1 Y = [2,3] X = the Y =cat,sat,[on,the,mat]

  7. Recursive search X is a member of the list that has X as its head: member(X, [X|_]). X is a member of the list if X is a member of the tail of the list: member(X, [_|Y]) :-member(X,Y). These rules define the membership predicate and tell how to search from beginning to end.

  8. MAPPINGJOINING STRUCTURESACCUMULATORS

  9. WHAT IS MAPPING? In languages which support first-class functions, map may be partially applied to lift a function that works on only one value to an element-wise equivalent that works on an entire container

  10. WHAT IS FIRST-CLASS? In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures

  11. Suppose we have a list of integers [1, 2, 3, 4, 5] and would like to calculate the square of each integer. To do this, we first define a function to square a single number square x = x * xAfterwards we may call>>> map square [1, 2, 3, 4, 5]which yields [1, 4, 9, 16, 25]

  12. MAP IN VARIOUS LANGUAGES

  13. JOINING STRUCTURES The list processing predicate append is used to join two lists together to form another Append([a,b,c],[3,2,1],[a,b,c,3,2,1]) The predicate append is most often used to create a new list from concatenating two others For example: append([alpha,beta],[gamma,delta],X) X=[alpha,beta,gamma,delta]

  14. ACCUMULATORS Accumulator is some variable to hold intermediate result. Example : accLen(List,Acc,Length) accLen([_|T],A,L) :- Anew is A+1,accLen(T,Anew,L). accLen([],A,A).

  15. Backtracking Backtracking is basically a form of searching. But how does it make its choice? It starts by trying to solve each goal in a query, left to right (recall goals are connected using “,” which is the and operator). For each goal it tries to match a corresponding fact or the head of a corresponding rule. A fact or head of rule matches a goal if: • Both use the same predicate. • Both have the same number of terms following the predicate. • Each term in the goal and fact or rule head match (are equal), possibly binding a free variable to force a match.

  16. Suppose that we have the following database female(marge). female(lisa). female(maggie). female(wilma). female(pebbles). male(homer). male(bart). male(fred). parent(marge,bart). parent(homer,bart). parent(marge,lisa). parent(homer,lisa). parent(marge,maggie). parent(homer,maggie). parent(wilma,pebbles). parent(fred,pebbles). father(X,Y) :- parent(X,Y), male(X).

  17. Our first goal will be finding male(X). We get 3 matches: homer bart fred When there are not matches, first goal is complete. What happens if we perform a query that involves a rule? For example, father(X,bart). Prolog begins by searching for the predicate father. When found, variable Y can be instantiated to Bart. Prologue then examines the next part of the rule because Prolog is looking for a parent of Bart, This is the new goal. It will start searching from top for a parent of Bart. It finds Marge. Prolog can now instantiate the variable X to Marge. After this, it examines the next part of the rule. Because X is Marge, Prolog is now looking for a male called Marge.

  18. However, it does not stop there! Now Prolog backtracks to the last successful goal which was to find a parent of Bart and continue searching. Once again it is successful. Variable X now can be instantiated to Homer. Prolog will be looking for a male called Homer. So it will be successful,but it is not over yet. Prolog is concerned there might be another male called Homer, so it will continue to search and there is not one. Prolog now backtracks to the last successful match because Prolog thinks Bart may have more than two parents so it continues seaching again. Now Prolog is finished.

  19. The “Cut” The cut, in Prolog, is a goal, written as !, which always succeeds, but cannot be backtracked past. It is used to prevent unwanted backtracking, for example, to prevent extra solutions being found by Prolog. But backtracking can lead to inefficiency by exploring possibilities that lead nowhere. Example: Suppose we have the following facts: teaches(fred, history). studies(alice, english). teaches(fred, english). studies(angus, english). teaches(fred, drama). studies(amelia, drama). teaches(fiona, physics).         studies(alex, physics).

  20. ?- teaches(fred, Course), studies(Student, Course). Course = english Student = alice ; Course = english Student = angus ; Course = drama Student = amelia ; false. Backtracking is not inhibited here. Course is initially bound to history, but there are no students of history, so the second goals fails.  Course is re-bound to english, the second goal is tried and two solutions are found (alice and angus), then backtracking occurs again, and Course is bound to drama, and a final Student, amelia, is found. ?- teaches(fred, Course), !, studies(Student, Course). false. This time Course is initially bound to history, then the cut goal is executed, and then studies goal is tried and fails (because nobody studies history). Because of the cut, we cannot backtrack to the teaches  goal to find another binding for Course.

  21. ?- teaches(fred, Course), studies(Student, Course), !. Course = english Student = alice ; false. Here the teaches goal is tried as usual, and Course is bound to history, again as usual. Next the studies goal is tried and fails, so we don't get to the cut at the end of the query at this point, and backtracking can occur. Thus the teaches goal is re-tried, and Course is bound to english. Then the studies goal is tried again, and succeeds, with Student = alice. After that, the cut goal succeeds. ?- !, teaches(fred, Course), studies(Student, Course). Course = english Student = alice ; Course = english Student = angus ; Course = drama Student = amelia ; false. In this final example, the same solutions are found as if no cut was present, because it is never necessary to backtrack past the cut to find the next solution, so backtracking is never inhibited.

  22. In practice, the cut is used in rules rather than in multi-goal queries, and some particular idioms apply in such cases. For example, consider the following code for max(X, Y, Max), which is supposed to bind Max to the larger of X and Y, which are assumed to be numbers. max(X, Y, Max) where max=X if X>=Y and max=Y if X<Y in Prolog: max(X, Y, X) :- X>=Y. max(X, Y, Y) :- X<Y. They are mutually exclusive. Only one can succeed.

  23. With the help of cut, this is easy to fix. We need to insist that Prolog should never try both clauses, and the following code does this: max(X,Y,Y) :- X =< Y,!.   max(X,Y,X) :- X>Y.   Note that this cut does not change the meaning of the program. Our new code gives exactly the same answers as the old one, it's just a bit more efficient. So reformulation to overcome all limitations calc(X, Y, Max) :- X >= Y, ! , Max = X ; Max = Y.

  24. But there is another kind of cut: cuts which do change the meaning of a program. These are called red cuts, and are usually best avoided. Here's an example of a red cut. max(X,Y,Y) :- X =< Y,!.max(X,Y,X). Consider what happens when all three arguments are instantiated. For example, max(2,3,2). Obviously this query should fail. But in the red cut version, it will succeed! Why? Well, this query simply won't match the head of the first clause, so Prolog goes straight to the second clause. And the query will match with the second clause, and (trivially) the query succeeds!  It does not truly define the max predicate, rather it changes it's meaning and only gets things right for certain types of queries.

  25. References http://pages.cs.wisc.edu/~fischer/cs538.s08/lectures/Lecture34.4up.pdf http://www.cse.unsw.edu.au/~billw/dictionaries/prolog/backtrack.html https://www.youtube.com/watch?v=AmWf6SeFmqc http://cs.union.edu/~striegnk/learn-prolog-now/html/node88.html http://www.cse.unsw.edu.au/~billw/dictionaries/prolog/cut.html https://www.youtube.com/watch?v=n4cm2zZb1Xo

More Related