unisonweb-org icon indicating copy to clipboard operation
unisonweb-org copied to clipboard

Clarify evaluation order in language ref

Open pchiusano opened this issue 3 years ago • 2 comments

Aaron Sikes brought this up in Slack:

The evaluation semantics of function application is applicative order Call-by-Value. In the expression f x y, x and y are fully evaluated in left-to-right order, then f is fully evaluated, then x and y are substituted into the body of f, and lastly the body is evaluated

Originally I was thinking this should be changed to:

The evaluation semantics of function application is applicative order Call-by-Value. In the expression f x: f is fully evaluated, then x, then x is substituted into the body of f, and lastly the body is evaluated. This rule also covers expressions like g a b, which are parsed as (g a) b.

But I don't think we want to guarantee one at a time evaluation order like this. For instance, consider:

g x = 
  Stream.emit 1
  (y -> 12)

> Stream.toList 'let g (Stream.emit 0) (Stream.emit 2)

This currently will produce [0,2,1] - it evaluates left to right all the arguments being supplied, and then does substitutions and evaluation of the body. And this is what you want, since the one at a time order is crazy inefficient and would involve building N temporary closures just to pass N args to a function. I don't think any real language uses this.

So I think maybe the current description is not bad.

pchiusano avatar Dec 11 '21 03:12 pchiusano

Perhaps just add a note that this diverges from the normal model for performance?

courajs avatar Dec 11 '21 03:12 courajs

It might be worth including an example like what I gave above just to clarify the matter further. I'd be inclined to do that and leave it at that.

I'm not sure how many people would be expecting the one-at-a-time evaluation order, given that it is so rarely used in real languages, so mentioning that might just be confusing for some people.

pchiusano avatar Dec 11 '21 03:12 pchiusano