chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] macros / imports


From: Peter Bex
Subject: Re: [Chicken-users] macros / imports
Date: Fri, 25 Apr 2014 17:09:36 +0200
User-agent: Mutt/1.4.2.3i

On Fri, Apr 25, 2014 at 06:51:53PM +0200, Chris Mueller wrote:
> Hi,
> 
> i'm currently writing some small helper macros for myself, that makes 
> chicken's module system syntactically a little bit more similar to 
> python's one.

Hi, and welcome to CHICKEN!

Please note that it's often not a good idea to try and force another
language's idioms onto Scheme.  Sometimes this can provide for good
new additions to the language, but it's generally a bad idea to try
and do that when just getting started.

For instance, in Scheme if you want to be more explicit about
namespaces, you can pick and choose which names to import:

(import (only srfi-1 every))

This would import only the "every" identifier into your module's
namespace.  Alternatively, you could also explicitly prefix the names
upon every import.

But, having said that, let's get to the fix for your stated problem:

> I would wish to have a macro that doesn't pollute the current namespace 
> environment by default. It should introduce for each module a seperate 
> prefix
> 
> Concretly in chicken the following templates
> 
> (imports std.list)
> 
> should be expanded to:
> 
> (import (prefix std.list std.list/))
> 
> Hint:
> It's the same problem discussed here in 
> http://lists.nongnu.org/archive/html/chicken-users/2005-08/msg00025.html 
> (2005)
> Unfortunetely, it's not uptodate anymore with chicken's API.
> 
> 1) I have tried to following snippet
> 
> (define-syntax imports
>   (ir-macro-transformer
>     (lambda (x i c)
>       (let ((name (cadr x))
>             (pre (symbol-append (i (caddr x)) '\.)))
>         `(import (prefix ,name ,pre))))))
> 
> that expands the top expression to:
> 
> (import (prefix std.list std.list/)) correctly.
> 
> But here the call seems to fail. Functions and the namespace are not 
> available. If i use directly the expanded
> version, it works.

The problem is that the implicit macro transformer renames identifiers.
If you want to unhygienically manipulate identifiers which are input
for the macro, you'll need to undo the renaming with strip-syntax before
appending other symbols to it:

(define-syntax imports                                                          
           
  (ir-macro-transformer                                                         
                                                          
    (lambda (x i c)
      (let ((name (cadr x))
            (pre (i (symbol-append (strip-syntax (cadr x)) '\.))))
      `(import (prefix ,name ,pre))))))

> In the sources it's seems at the first view the import system is very 
> tight integrated with the macro expansion.

Yes, this is necessarily the case.

> fty. I'm not familar with low-level macro transformers :] but I got the 
> prefix symbol extension not working with syntax-rules().

Yeah, this is an unhygienic transformation of the data: the macro expands
to a form which is not purely a reshuffled form of the input with calls
to things from the environment in which the macro was defined.  Instead,
it introduces new identifiers, which appear "out of thin air" as far as
the syntactic environment is concerned.  So syntax-rules is not capable
of doing this.

Cheers,
Peter
-- 
http://www.more-magic.net



reply via email to

[Prev in Thread] Current Thread [Next in Thread]