I'm revising for an upcoming Haskell exam and I don't understand one of the questions on a past paper. Google turns up nothing useful
fst(x, y) = x
square i = i * i
i) Source reduce, using Haskells lazy evaluation, the expression:
fst(square(3+4), square 8)
ii) Source reduce, using strict evaluation, the same expression
iii) State one advantage of lazy evaluation and one advantage of strict evaluation
What I don't understand is what is source reduction?
Reduction is a term from the lambda calculus which involves a semantics-preserving transformation that replaces one term with an equivalent term. For the examples you've given, the most important kind of reductions are
Beta-reduction is the fundamental rule in the lambda calculus, and in a pure, lazy language like Haskell, it always preserves semantics. The beta rule is the one that says:
(\x. e) m
can be replaced by e
with m
substituted for x
. (The substitution must avoid "capturing" free instances of x
in m
.)
It's quite possible that your instructor wants you to combine reductions as follows:
Notice you often have a choice about which application to reduce; for example, in the term you give, there are two applications of square
and one of fst
that could be reduced in this fashion. (The application of + can also be reduced, but reduction involving constants requires different rules.)
From the questions I see that your instructor wants you to reduce each term repeatedly until it reaches a normal form and that your instructor wants you to demonstrate your understanding of different reduction strategies. The word "source" in "source reduce" is superfluous; reduction means manipulation of source terms in some language. I would have phrased the questions as follows:
Using the reduction strategy that corresponds to Haskell's lazy evaluation, reduce the following expression to weak head normal form. Show each step in the sequence of reductions.
Using the reduction strategy that corresponds to evaluation in a strict functional language, reduce the following expression to head normal form.
I probably would have chosen to be less coy and just name the reduction strategies: a call-by-need reduction strategy and a call-by-value reduction strategy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With