Re:I'm Betting on Call-by-Push-Value
by 082349872349872 on Sunday March 10, 2024 @07:47AM
(#39658407)
If it doesn't evaluate functions, how do closures get their lexical environments?
[ Parent
| on HN ]
How do they figure out what goes into their environment, or how do their environments get populated?
I believe the first happens at compile time, and the second happens after the function is passed.
1 f x =
2 let xx = x * x in
3 (\y -> y + xx)
4
5 main = do
6 let clo = f 2
7 let applied = clo 3
8 print applied
The closure is defined on line 3, and instantiated on line 6.xx is known to be the captured variable at compile time. But in a lazy setting, it's fine for the closure to be passed around before the value of xx is known:
f x =
let xx = trace "Evaluating xx" (x * x) in
trace "Returning closure" (\y -> y + xx)
main = do
let clo = f 2
let applied = clo 3
print applied
> Returning closure> Evaluating xx
> 7