On Friday 30 March 2012 at 15:30, Mikael wrote:
Den 30 mars 2012 15:39 skrev Per Eckerdal <per.eckerdal@gmail.com (mailto:per.eckerdal@gmail.com)>:
The main drawback of allowing prefix-less global names is that it requires *all* local bindings to be renamed by the auto aliasing, because you can't predict if a local binding will shadow a global one. If all globals had a namespace prefix, auto aliasing would only have to rename bindings that shadow other local bindings, which is fairly rare. This would result in prettier macroexpanded code.
Very nice post. Can you give an example that illustrates what you meant with this paragraph?
Consider the following code:
ten-times.scm:
(define a-name 10)
ten-times#.scm:
(define-macro (ten-times code) (let ((loop (gensym)) (i (gensym))) (alias (let ,loop ((,i a-name)) (if (positive? ,i) (begin ,code (,loop (+ ,i 1))))))))
main.scm:
(##include "ten-times#.scm")
(let ((a-name 5)) (execute-ten-times ;; A macro defined somewhere else (println "Five is " a-name)))
(This is, if memory serves me, the way separate compilation and macros are normally done in Gambit today)
Let's compile this program
gsc -c ten-times.scm gsc -c main.scm gsc -o a.out -exe ten-times.c main.c
The |a-name| global in ten-times.scm will not have any prefix, because the file does not contain any namespace declaration.
The |a-name| local binding in main.scm will not have any prefix, because the compiler does not know anything about any global or local in the current scope named |a-name| so it believes it's safe to not prefix that particular local binding.
This results in unhygiene, and the program will only print five lines, instead of the intended ten.
Because of this, all local bindings must be prefixed by auto-aliasing, even ones that seemingly don't require it, because there is no way to predict if a local binding will shadow a global one.
If auto-aliasing would rename all local bindings, this code would work. There would still be name clash hazards, but those name clash issues only happen with globals, and are present in Gambit already.
Regards,
Per