This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Module-export and letrec


"Nic Ferrier" <nferrier@tapsellferrier.co.uk> writes:

> letrec is used in Kawa for tail-recursive procs (for understandable
> reasons).
> 
> The only trouble is that you can't export a name proc from inside a
> letrec. Because you can't do that you have to go to the trouble of
> having an external definition that you assign to the letrec defined
> proc that you want to export, like this:
> 
> 
> (module-export someproc)
> 
> (define someproc #!null)
> 
> (letrec
>   ((hiddenproc
>      (lambda (x)
>        (if (eqv? x 1)
>            1
>             (hiddenproc x-1)))))
>    (set! someproc hiddenproc))
> 
> 
> This is inconvienant at best. Does anyone else think that Kawa should
> allow exports from letrecs?

I would rather have it so the optimizer recognizes a tail-call to a
function in the same module and optimizes that.  (At least self-tail-calls.)

(module-export someproc)

(define (someproc x)
  (if (eqv? x 1) 1 (someproc x-1)))

Having this be optimized should be an easy change.  Of course this
would assume no-body assigns to someproc, but I think that is a
reasonable assuemption.  Kawa distinguishes:

(define (someproc x)
  (if (eqv? x 1) 1 (someproc x-1)))

from

(define someproc (lambda (x) (if (eqv? x 1) 1 (someproc x-1))))

in that the former is assumed to be constant and the latter is
initializing a variable.  This is similar to optimizations that
Common Lisp allows.

(Of course there should be an optimization to disable such
inlining, especialy when it is possible a perverse but legal program
would violate the assumption.)

Implementing tail-calls for top-level functions should be quite
simple - the logic is basically there.  But I have a backlog of
other not-quite-done things I need to take care of, including some
xml-related code for my day job.

A reminder: If you have a Kawa improvement/fix that is important to
you, including performance improvements, it is always possible to hire
me for either support or contract jobs, and that will put your needs
at the front of the line.  That said, I will put this change in the
TODO file to remind me.  Even better would be if someone can submit
a patch!

Until then, why not do:

(module-export someproc)

(define (someproc x)
  (let loop ((x x))
    (if (= x 1) 1 (loop (- x 1)))))
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]