Concepts+Techniques+and+Models+of+Computer+Programming_Part25

Concepts+Techniques+and+Models+of+Computer+Programming_Part25

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 678 Relational Programming There are many ways that sublist/2 can execute, depending on which ar- guments are instantiated when it is called. Some of these will be efficient; others may go into infinite loops. Let us execute the call: | ?- sublist(L1, [a,b]). (Prolog lists are written with commas separating the elements.) We can execute this in the relational model as follows: {Browse {SolveAll proc {$ X} {Sublist X [a b]} end }} This displays a list of six solutions: [nil [a] nil [a b] [b] nil] These are the same solutions in the same order as would be calculated by a Prolog system. Why are the solutions returned in this order? Why is nil returned three times? Trace through the execution of the program to find out. The order of the append/3 calls in the definition of sublist/2 might seem strange. The following definition is more intuitive: sublist(L1, L2) :- append(H, L1, V), append(V, T, L2). That is, we first append H and L1 to get V , and then we append V and T to get L2 . What happens if we execute sublist(L1, [a,b]) with this definition? From this example, we can see that programming solely by reasoning about the logic can lead to inefficient programs. To get efficiency, we have to take the execution order into account. The art of programming in Prolog is to write definitions that are both logical and efficient [139, 182]. Let us see how to achieve this. How to write Prolog programs One way to write good Prolog programs is to follow these three steps: • First write the program as if it were a statement in pure logic. The resulting program is logically correct but it might be inefficient or go into an infinite loop as a result of Prolog’s depth-first search strategy. • Second, rewrite (part of) the program for efficiency, taking care not to break the logical semantics. This step introduces algorithmic knowledge into the program’s logic. The program remains purely logical, however. In the case of sublist/2 , changing the order of the append/3 calls changes efficiency but does not change the logical meaning. The best order is the one in which the append/3 with fewest solutions comes first. • Third, add extralogical operations where they are needed. These operations are needed for various reasons, such as efficiency, adjusting execution order, modularity, or parameterizing the program. They should be used in a disciplined way, so that the logical view holds as much as possible. Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved. 9.7 The Prolog language 679 Let us look again at factorial/2 in the light of this methodology. It needs to be rewritten slightly for efficiency reasons. As it stands, if the first clause is chosen, then the system might still store information allowing to backtrack to the second clause....
View Full Document

Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part25

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online