chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] need help with hygienic macros


From: Jörg F . Wittenberger
Subject: Re: [Chicken-users] need help with hygienic macros
Date: 12 May 2013 23:57:54 +0200

On May 12 2013, Marco Maggi wrote:

Jörg F. Wittenberger wrote:
not exactly chicken  related; but I hope  someone here can
help me.

I  am  not  a  Chicken   user,  neither  I  have  a  general
solution. ;-)

I'm  trying   to  replace  an  unhygienic   macro  with  a
syntax-rules based version.  Just I can't.
...
 This is what  I understand: you would like  to execute the
code:

  (define (red a b r s)
    (list a b r s))

  (define (blue c d r s)
    (list c d r s))

  (list (red 1 2 3 4)
        (blue 5 6 7 8))

but you only want to write:

  (define-with-more (red a b)
    (list a b r s))

  (define-with-more (blue c d)
    (list c d r s))

  (list (red 1 2 3 4)
        (blue 5 6 7 8))

Pretty much.  Partially even simpler: all my color-procedures
share the same signature.  So it's more like

(define (red a b c d) (list a b c d))
(define (blue a b c d) (vector a b c d))

but I want to write (almost) something like:

(define-with-more red (list a b c d))
(define-with-more blue (vector a b c d))

By "(almost)" I mean that I don't want to be forced to
mention all of {a,b,c,d} in the calls to {list,vector}.
(Best: not any.)

Instead I want some macro magic to 1.) access (or construct)
{a,b,c,d} - or for that matter also {e,f,...} which are
derived from {a,b,c,d} - and 2.) create the call
here simulated by {list,vector,...}.

Thus more like writing

(define-with-more red
(create-call list (set-a (+ (get-a) 1)) ))

to be expanded into

(define (red a b c d) (list (+ a 1) b c d))

with 1st bonus point for `set-a` and `get-a` being spelled the
same `a` and 2nd bonus point for `(get-a)` without parenthesis.
Such that I would write either

(define-with-more red
(create-call list (a (+ a 1)) ))

or something like

(define-with-more red
(create-call list a: (+ a 1) ))

this would be gokuraku for your code.

 It  is  easy  to   envision  a  DEFINE  replacement  which
introduces arguments in the formals list:
...
but how to access the  additional formals from the body?  In
other  words:  how  does  one introduce  identifiers  in  an
expression in lexical context A by expanding a macro defined
in lexical context B?

 With an expander providing the features of SYNTAX-CASE and
friends, that would be easy:
...

Ah! I see.  Too bad.  Now either I can find a syntax-rules
way or resort to another macro expander (which is what I'm
doing by now, just that I'm now using a worse one: unhygienic).

 I  am unable  to come  up with  a solution  involving only
SYNTAX-RULES for the  general case; but if  it is acceptable
to split the expressions using  the formals you type by hand
from the expression using the introduced formals:

 (define-syntax define-with-more
   (syntax-rules ()
     ((_ (?name ?formal ...)
          ?body0 ?body ...)
      (define (?name ?formal ... r s)
         (helper (begin ?body0 ?body ...)
                 r s)))))

Not precisely.  I do not (yet) have a need for formals typed
by hand.  Thus your condition would be collapsed to:
"there are only expressions using the introduced formals".

 (define-syntax helper
   (syntax-rules ()
     ((_ ?body ?r ?s)
      (append ?body (list ?r ?s)))))

Thus ?body would have to have access to ?r ?s in some way.

Thanks anyway.

Still trying to find a solution.

/Jörg





......



reply via email to

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