Aha, thank you, very nice, ok so it can be done in demonstration now, though kind of not more than that.<div><br></div><div>What drove me to ask this question was that it could be used for some kinds of type checking and possibly as part of a type system, that maybe could be very useful.</div>

<div><br></div><div>Roughly approx what step would be needed to make (procedure-creator) also work for the compiled world? Is there any issue about that the underlying information is destroyed during compilation currently, due to inlining or alike?</div>

<div><br></div><div>Kind regards,</div><div>Mikael</div><div><br></div><div><div class="gmail_quote">2011/3/11 Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br>
On 2011-03-11, at 7:14 AM, Mikael wrote:<br>
<br>
> Dear Marc,<br>
><br>
> Is there any way to get which procedure a closure comes from? If not, would it be very easy to implement? Just wanted to check.<br>
><br>
> Kind regards,<br>
> Mikael<br>
<br>
</div>It is possible with interpreted code because the "run time environment" (rte) that is stored in the closures has back-pointers to the closure that created it (also known as the "container" procedure).  Below is the code, and a demo.<br>


<br>
Marc<br>
<br>
<br>
(define (procedure-creator proc)<br>
  (if (##interp-procedure? proc)<br>
      (let* (($code (##interp-procedure-code proc))<br>
             (rte (##interp-procedure-rte proc)))<br>
        (##extract-container $code rte))<br>
      #f)) ;; not possible with compiled code, so return #f<br>
<br>
(define (make-adder3 x)<br>
  (lambda (y)<br>
    (lambda (z)<br>
      (+ x y z))))<br>
<br>
(define f ((make-adder3 11) 22))<br>
<br>
(pretty-print (f 33))<br>
(pretty-print (procedure-creator f))<br>
<br>
(pp f)<br>
(pp (procedure-creator f))<br>
(pp (procedure-creator (procedure-creator f)))<br>
(pp (procedure-creator (procedure-creator (procedure-creator f))))<br>
<br>
(pp (eq? make-adder3 (procedure-creator (procedure-creator f))))<br>
<br>
;; prints:<br>
;;<br>
;; 66<br>
;; #<procedure #2><br>
;; (lambda (z) (+ x y z))<br>
;; (lambda (y) (lambda (z) (+ x y z)))<br>
;; (lambda (x) (lambda (y) (lambda (z) (+ x y z))))<br>
;; #f<br>
;; #t<br>
<br>
</blockquote></div><br></div>