Ilya Zakharevich on Thu, 11 Jan 2024 21:56:52 +0100


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

Is it possible to implement closures in PARI?


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?

   ¹⁾ I remember that when Larry Wall announced that Perl 5 supports
      closures, it took quite a lot of effort to make this
      “implementation” actually compatible with what is “a closure in
      CS⸣ terms”.  This resulted in a test suite for “the expected
      semantic of closures”.
      
      In the early versions of Perl 5, this test suite was in a test
      file named like closures.t.  (I’m pretty sure that it should
      still be present in newer versions of Perl 5 too.)

Currently, the simplest test case (with RO access only, no referenced
lexicals containing closures, and no (mutual) recursion) would emit:

  (12:30) gp > my(a=3, b()=print(a)); a++; b()
  3

(with v2.15.4). — Of course, this is not how closures should behave…

Thanks,
Ilya

  P.S.  In PARI⸣s case, the semantic may be further complicated by a
  	(potential?) difference between write access to a variable, and
  	write access to an array’s element…