[plt-scheme] Source location infromation and tracebacks (was: match-lambda and source location)

From: Jos Koot (jos.koot at telefonica.net)
Date: Sun Jan 4 14:33:22 EST 2009

----- Original Message ----- 
From: "Jakub Piotr Cłapa" <jpc-ml at zenburn.net>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>
Cc: "Matthew Flatt" <mflatt at cs.utah.edu>
Sent: Sunday, January 04, 2009 7:16 PM
Subject: [plt-scheme] Source location infromation and tracebacks (was: 
match-lambda and source location)


> This would provide for Scheme loops what tracebacks provide for normal 
> function calls. An answer to the question: How I got here? This is 
> something that Scheme style iteration could do much better than an average 
> imperative loop.
>
> A related concern is that even when tail calls are not used for looping we 
> sometimes loose information:
> (define (sqrt-inv a)
>   (let ([inv (/ a)])
>     (sqrt inv)))
>
> (define (do-b b)
>   (sqrt-inv (* 2 b)))
>
> (define (do-c c)
>   (if (< c 0)
>       #f
>       (do-b c)))
>
> When we call (do-c 0) we don't get any of the do- functions in the 
> traceback.

Because almost everything is in tail position. When finally '/' is called, 
the stack no longer contains info on the calls to do-c and do-b. They have 
gone. The only thing that is left is the argument given to '/' and the 
continuation of (do-c 0) Even variable a is no longer on the stack (only its 
value) The only things left on the stack are the call to ',' and the 
continuation of (do-c 0) and they are both shown in the trace back. Without 
deleting the stuff from the stack we would not have proper tail-recursion. 
So asking for a trace of the last so many tail recursive calls would violate 
the proper tail-recursivity. You may see the diffrence by removing the tail 
positions

(define (sqrt-inv a)
   (let ([inv (/ a)])
     (sqrt inv)))

(define (do-b b)
  (let ((x (sqrt-inv (* 2 b)))) x)) ; inhibit tail position

(define (do-c c)
   (if (< c 0)
       #f
      (let ((x  (do-b c))) x))) ; inhibit tail position

(do-c 0)

This gives a trace back along all stuff.
Jos



Posted on the users mailing list.