[gambit-list] Generic functions with Gambit

mikel evins mevins at me.com
Tue Mar 20 14:45:36 EDT 2012


On Mar 20, 2012, at 9:24 AM, Marc Feeley wrote:

> 
> On 2012-03-19, at 11:17 PM, mikel evins wrote:
> 
>> Marc,
>> 
>> We're considering building the Bard interpreter into the Nelson game framework. Bard implements CLOS-stye generic functions. I wonder if Gambit provides a way to make that more convenient than the ways I've done it in the past?
>> 
>> What I'd like to do if I can is define generic functions and methods as objects for which the following hold true:
>> 
>> 1. generic function is a distinct type that I can distinguish with a predicate
>> 2. the same is true of method
>> 3. I can ask a generic function for its method table and modify it
>> 4. generic function and method are both callable, like Scheme procedures
>> 
>> Again, I can solve these problems with various techniques, but what would be really convenient would be a way to define a callable data type. If that's not supported by the current Gambit runtime, no sweat; I can fall back to the tricks I've used before.
> 
> [I think this is of general interest to the Gambit community, so I am CCing to the Gambit mailing list and changing the subject of the message]
> 
> Generic functions can be obtained by combining a table of methods and a closure.  That is, the table of methods is a free variable of the closure.  To get the method table from a generic function, you have to access this free variable.  Note that there is only one free variable per generic function.  Accessing this free variable can be done with the (##closure-ref genfun 1) primitive (for compiled closures), and with (##vector-ref (##interp-procedure-rte genfun) 1) (for an interpreted closure).  See the file genfun1.scm below.
> 
> Another approach is to represent generic functions with a type that is different from procedures, and to install an exception handler which will be invoked by the procedure call mechanism when it detects that the function position of the call is not a procedure.  See the file genfun2.scm below.
> 
> The performance of both approaches could be improved by a better strategy for encoding type identifiers and the hash table of methods.  I expect the approach based on closures to be the faster of the two approaches because it allocates less.

Thanks very much, Marc. This is very helpful.




More information about the Gambit-list mailing list