scsh-users
[Top] [All Lists]

Re: Dirty macros with define-syntax?

To: scsh-news@zurich.ai.mit.edu
Subject: Re: Dirty macros with define-syntax?
From: karttu@walrus.megabaud.fi (Antti Karttunen)
Date: 6 Mar 2002 16:00:00 +0200
Organization: Megabaud Oy, Helsinki
In article <f73a7491.0203051113.3e315074@posting.google.com>,
RT Happe <rthappe@web.de> wrote:
>Michael Sperber [Mr.  Preprocessor] writes:
>> >>>>> "Brian" == Brian D Carlstrom <bdc@zurich.ai.mit.edu> writes:
>
>> Brian> define-syntax does not have to be used with syntax-rules. You can
>> Brian> provide a functon to do explicit s-exp based conversion.
>
>> There's documentation at
>> 
>> ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/prop/exrename.ps.gz
>> 
>> which applies to Scheme 48/scsh as well, with the exception of the
>> TRANSFORMER keywords, which is omitted here.
>
>There are three simple examples in scsh-0.6.1/scheme/misc/doodl.scm
>(the macros SETTER, DEFINE-SETTER, and BIND).  Basically, you rename
>rather than gensym, and compare names with the dedicated comparison
>function rather than with Scheme's EQ? or something.
>
>  (define-syntax foo
>    ;; transforms EXP --> EXP'
>    (lambda (exp rename eq?) ...))
>
>rthappe

Thanks everybody, now I have advanced a bit.

% scsh
Welcome to scsh 0.6.1 (Combinatorial Algorithms)
Type ,? for help.
> ,config ,load convform.scm
convform.scm
> ,open convform
Load structure convform (y/n)? y
[convform[((for-syntax #f))]
]
> (define-convform list 1 2 3)
(#{Name define-convform} list 1 2 3)
'(1 2 3)
> 

where the contents of convform.scm is:

(define-structure convform 
   (export (define-convform :syntax)) 
   (open scheme) 
   (for-syntax 
     (open scheme) 
     (begin 
          (define (define-convform-expander arg1 arg2 arg3) 
            (write arg1) (newline) ; What we have eaten?
            (cdr arg1) ; Just something now, and avoid infinite recursion. 
          ) 
     ) 
   ) 
   (begin (define-syntax define-convform define-convform-expander)) 
) 
 

Now, the define-structure stuff above is written solely by the
trial-and-error method, as I couldn't find any kind of documentation
about the (for-syntax <clause>) part of define-structure -form,
neither from www.google.com nor groups.google.com.


Next I would like to know whether a macro defined
in such way as a transformer function can define more
similar dirty macros by calling define-syntax
at its expansion time? And if so, should those invocations
(of macro define-convform) occur inside some of the
structure- (i.e. Scheme48/scsh-module) -definitions?

Or should I use guile-scsh instead, with presumably has
the guile's old-fashioned define-macros?



What I have in mind is a simple language for defining
sequential conversion scripts that execute a set
of predefined conversion forms, which have been defined
like:

(define-convform (FETCH_LATEST_FROM_XYZZY (O output))
   (run (lynx -source ,(assq XYZZY_URL *DEFS*)) (> ,output))
)


(define-convform (NUKE_CRS (I infile) (O outfile))
   (run (tr -d '\015') (< ,infile) (> ,outfile))
)


and that macro define-convform would do two things:

A) define a function

(define (NUKE_CRS_fun *STEP* *DEFS* *IO_BINDINGS* infile outfile)
  (let ((infile (assq infile *IO_BINDINGS*))
        (outfile (some_code_for_creating_new_temporary_file_name
                   outfile and_destructively_adding_it_to *IO_BINDINGS*))
       )
    (some_catcher_and_thrower_for_possibly_ruined_execution_here?
      (run (tr -d `\015`) (< ,infile) (> ,outfile))
    )
  )
)

B) define a macro called NUKE_CRS

which would expand something like (NUKE_CRS (I file1) (O file2))
to call: (NUKE_CRS_fun *STEP* *DEFS* *IO_BINDINGS* 'file1 'file2)
and these all invocations would occur
inside some begin-form or such with *DEFS* and *IO_BINDINGS*
bound as appropriate association-lists.

So the resulting "scripts" look a bit like definitions of
a Prolog-clause with many subclauses, with their instantiated (here input
files marked with (I ...)) and uninstantiated (here output
files marked with (O ...)) variables, and the *IO_BINDINGS*
list keeping track which filenames are already instantiated
to temporary file names like tmp20020306.1, tmp20020306.2, etc.
But it also feels a bit like an assembly program
  MOV #item,@dest
  ADD (R1)+,@dest
etc.
with similar breakpoint and single-step capabilities
allowed as the most debuggers have.

But this syntax is still in development. Now somebody
asks why I don't use the pipe-notation of scsh,
for which the reason is that there are also conversion forms
which have more than one input and output, and I especially
need the single-stepping facility for debugging and all
the intermediate results of conversions saved (as files)
in case something goes bad in the later steps.




Yours,

Antti Karttunen

<Prev in Thread] Current Thread [Next in Thread>