Bill Allombert on Sun, 14 Jan 2024 13:05:47 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Is it possible to implement closures in PARI?


On Thu, Jan 11, 2024 at 12:56:47PM -0800, Ilya Zakharevich wrote:
> Currently, PARI supports anonymous subroutines memoizing values of
> lexical variables (at the moment of creation).  Is it theoretically
> possible to implement¹⁾ closures instead? — In other words: are
> closures compatible with the PARI⸣s memory model?

Probably not. The problem is well-documented:
<https://en.wikipedia.org/wiki/Funarg_problem>

The problem is that the variable scope might end before the closure scope,
so the closure would have a reference to a freed variable, so we would need
smarter memory management than the stack.

Also it would not be compatible with parallelism, which require closures to
be immutable.

Cheers,
Bill