| OCR Text |
Show 106 A la zy evaluation effe ct is achieved by requiring that a biased unification algorithm perform "reduct ion - by- need," that is, perform only those reductions that are necessary in order t o make two terms semantically unifiable. For ex-ample, the reduction of a function term if_then_else(f(a), g(b), h(c)) will cause f(a) to be matched agai nst the boolean constant true or false, which will in turn demand the redu ct ion of f( a ). Notice that neither g(b) nor h(c) is reduced during this reduction step, because t hey are both matched to a vari-able, respecti vel y. Consider the foll owing example of summing the infinite se-quence 2,4,8, 16, .... Example 11. g(N) = N" g(2*N) sum(A"L) = A + sum(L) The process of reducing the function term sum(g( 4)) demands that sum(g( 4)) be unified with sum(A " L) . This in turn demands that g( 4) be unified with A"L. The biased semantic unification algorithm yields the unifier {A = 4, L = g( 2*4)}, and the procedure One_Step_Reduction returns 4+sum(g(2*4) ). Note that the value of the expression 2*4 is not yet demanded in the current reduction . D The above example serves to provide some intuition about the notions of pattern-driven reduction and reduction-by-need. When the terms A"L and g( 4) were matched, g(4) was reduced once to yield 4" g(2*4); this was then matched with A"L, resulting in A bound to 4 and L to g(2*4 ). If we t ry to match, say, A " B"L with g( 4), then g( 4) will be reduced twice and become 4"2*4"g(2*2*4 ). Thus, such reductions of function terms are in fact driven by patterns. The actual number of reductions carried out is based on the need to match a pattern. Notice that by adopting an equational model the not ion of lazy evaluation extends to any algebraic data type in a very natural w ay without in-creasing language complexity. |