Call-by-need

2 views
Skip to first unread message

llarsson

unread,
Mar 15, 2010, 3:34:59 PM3/15/10
to proglang-course-2010
Isn't it enough to implement call-by-need instead of both call-by-
value & call-by-name?

When do one know when to save the value in call-by-need?
The only bad way I have come up with is to check if there is any
lambda function nested and the best way to do that is to check the
application stack in the enviroment but I would like a better way as
to avoid modifing the code to return an env everywhere

ex.

add x = (x 5)+(x 7)
main = add (\x -> x) -- should return 12

Since I currently save after every lookup I will save the result of (x
5) in x which is wrong and how can I best detect this? Is the
application stack the best way?

[5,7] == [7]

llarsson

unread,
Mar 15, 2010, 3:45:38 PM3/15/10
to proglang-course-2010
or perhaps I should have formulated it differently, the above case can
easily be tested agains abstraction and appliance

but should it also support more difficult cases like

add x = (x 5) + (x 7)
main = add (1 + \x-> x)

or should this be stopped by parser

Krasimir Angelov

unread,
Mar 16, 2010, 8:37:53 AM3/16/10
to proglang-c...@googlegroups.com
On Mon, Mar 15, 2010 at 8:45 PM, llarsson <llar...@student.chalmers.se> wrote:
> add x = (x 5) + (x 7)
> main = add (1 + \x-> x)

This should result in type error.

Krasimir Angelov

unread,
Mar 16, 2010, 8:42:09 AM3/16/10
to proglang-c...@googlegroups.com
Yes. If you have implemented call-by-need then you don't need
call-by-name or call-by-value.

You don't have to save 5 in x because x is a function. An example when
this is needed is this:

(\x -> x+x) (1+2)

In this case x is first bound to (1+2). After the first computation x
is updated to 3 and then when x is accessed for a second time then it
already has the computed value 3.

Krasimir

Arnar Birgisson

unread,
Mar 16, 2010, 11:40:11 AM3/16/10
to proglang-c...@googlegroups.com

Since the language is untyped, it is sufficient to raise an error in
the interpreter when you visit the + node and see that the operands
are not two numbers.

cheers,
Arnar

llarsson

unread,
Mar 16, 2010, 5:55:34 PM3/16/10
to proglang-course-2010
Doesn't this cause a lot of overhead as I will evaluate each node
several times to see if it is a value or "stm/expression" (abs -
lambda, if, app... lambda value)

like the first example:


add x = (x 5) + (x 7)

should this be allowed?

Sorry if I talk noncense as I am a bit tired but don't really have
time to think... :)

Arnar Birgisson

unread,
Mar 16, 2010, 6:02:20 PM3/16/10
to proglang-course-2010
When you evaluate e.g. a + node, you have to evaluate the arguments
anyways. If they are not numbers, than you simply can't and have to
give an error.

On Tue, Mar 16, 2010 at 22:55, llarsson <llar...@student.chalmers.se> wrote:
> like the first example:
> add x = (x 5) + (x 7)
>
> should this be allowed?

Yes, this should be allowed.

cheers,
Arnar

Reply all
Reply to author
Forward
0 new messages