El 11 de junio de 2010 15:25, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span> escribió:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><br>
On 2010-06-11, at 8:34 AM, Álvaro Castro-Castilla wrote:<br>
<br>
> Hi,<br>
><br>
> I can see sometimes these kind of procedures ##car, ##cdr, ##null? etc... in code that has been optimized for Gambit. What is exactly the difference between those and the standard ones? And if these ones are faster, why are they defined as separate functions instead of just substituting the standard ones when in (unsafe) compilation? Then, how are they really used?<br>


 > </div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Gambit has a namespace mechanism and identifiers can be qualified with the namespace.  So foo#bar is the identifier bar in the namespace foo.  The namespace declaration allows mapping unqualified identifiers to a particular namespace, so with the code<br>


</blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
(namespace ("foo#" bar baz))<br>
<br>
(bar 123)<br>
(other#bar 999)<br>
<br>
the call to bar is a call to foo#bar and the call to other#bar is really a call to other#bar.  So regardless of the namespace declarations, a call to ##car will really call ##car.<br>
<br>
The Gambit source uses namespace declarations sparingly.  From memory there are 3 namespaces in the runtime system:<br>
<br>
- "c#" is for the Gambit compiler<br>
- "sc#" is for the syntax-case expander<br>
- "##" is for the Gambit runtime system procedures<br>
<br>
The Gambit runtime system procedures implement a wide variety of functionality, from simple "primitive" procedures (such as ##car, ##pair?, ...) that translate to a few machine instructions, to complex procedures such as ##eval (the interpreter), ##repl (the REPL), etc.<br>



<br>
Gambit's runtime system is built on top of the primitive procedures.  Given that most of the runtime system is written in Scheme, for performance it is important to have primitives which assume that there are no run time errors.  So ##car assumes that its argument is a pair.  The car procedure on the other hand does type checking at run time.  In fact, car is defined like this (more or less):<br>



<br>
(define (car x) (if (##pair? x) (##car x) (error "pair expected"))<br>
<br>
When the "not safe" declaration is used (and "standard-bindings"), the compiler can avoid the type checking.  So what the compiler does is replace the call to car by a call to ##car.  Here's an example:<br>



<br>
% cat sum.scm<br>
(declare (standard-bindings) (not safe))<br>
<br>
(define (sum x) (fx+ (car x) (cdr x)))<br>
<br>
% gsc -expansion sum.scm<br>
Expansion:<br>
<br>
(define sum<br>
  (lambda (x)<br>
    ('#<procedure #2 ##fx+><br>
     ('#<procedure #3 ##car> x)<br>
     ('#<procedure #4 ##cdr> x))))<br>
<br>
As you see, it is now explicit what is being called.<br>
<br> </blockquote><div><br></div><div><br></div><div>Thanks for the deep explanation, it was really useful.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


Now why are qualified names (such as ##car) used all over the place in the runtime system?  Mostly for historical reasons (a large part of the runtime system was written before the existence of the namespace mechanism and the procedure specializer (i.e. car + "not safe" -> ##car).  I've been wanting to "clean things up" for a while now, but that's a major undertaking that adds little tangible value to Gambit users.  Perhaps someone on this list who likes manual labor would like to refactor the Gambit runtime system...  You know where to reach me if you have the urge!<br>



<font color="#888888"><br>
Marc<br>
<br>
</font></blockquote><div><br></div><div>Actually I would expect to see that procedures in the runtime implementation, and I think that is even good, and also saves (perhaps?) a tiny amount of computation avoiding all the transformations. I wondered about code that I was reading from different places, of libraries made for Gambit that I use as reference.</div>

<div><br></div><div>Thanks again for the explanations. Best regards,</div>
<div><br></div><div><br></div><div>Álvaro<br></div>