25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1. Evaluate f to λy.e<br />

2. Evaluate [y/x]e<br />

Call-by-need:<br />

1. Allocate a thunk v for x<br />

2. Evaluate f to λy.e<br />

3. Evaluate [y/v]e<br />

Terms that have a normal form in one model, may or may not have a normal form in another. In callby-need<br />

and call-by-name evaluation diverging terms are not necessarily evaluated before entry, so some<br />

terms that have a normal form in these models may diverge under call-by-value.<br />

Call-by-value<br />

Call by value is an extremely common evaluation model. Many programming languages both imperative<br />

and functional use this evaluation strategy. e essence of call-by-value is that there are two categories of<br />

expressions: terms and values. Values are lambda expressions and other terms which are in normal form<br />

and cannot be reduced further. All arguments to a function will be reduced to normal form before they<br />

are bound inside the lambda and reduction only proceeds once the arguments are reduced.<br />

For a simple arithmetic expression, the reduction proceeds as follows. Notice how the subexpression (2<br />

+ 2) is evaluated to normal form before being bound.<br />

(\x. \y. y x) (2 + 2) (\x. x + 1)<br />

=> (\x. \y. y x) 4 (\x. x + 1)<br />

=> (\y. y 4) (\x. x + 1)<br />

=> (\x. x + 1) 4<br />

=> 4 + 1<br />

=> 5<br />

Naturally there are two evaluation rules for applications.<br />

e 1 → e ′ 1<br />

e 1 e 2 → e ′ 1 e 2<br />

e 2 → e ′ 2<br />

v 1 e 2 → v 1 e ′ 2<br />

(λx.e)v → [x/v]e<br />

(E-App1)<br />

(E-App2)<br />

(E-AppLam)<br />

For a simple little lambda calculus the call-by-value interpreter is quite simple. Part of the runtime evaluation<br />

of lambda calculus involves the creation of closures, environments which hold the local variables<br />

in scope. In our little language there are two possible values which reduction may converge on, VInt<br />

and VClosure.<br />

69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!