Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Are macros relevant in lazy functional languages?

7 views
Skip to first unread message

zhaoway

unread,
Jan 23, 2003, 11:17:20 AM1/23/03
to
Sometime ago, a C++ programmer asks me what is that macros can do
while inline functions can't. My answer is like that, but it is not
really relevant to him as a C++ programmer. Anyway.

My answer is that, to put it simply, if you don't have a, say, switch
statment in your language, then only macros can give it to you from
more primitive if statement, while inline functions can't do that for
you.

To put it in the context of Scheme, a function call is something like
(f a b c), if do is implemented as a function call based on other
primitives, you have to call it as (do '((v i s)) '(t r) 'e1 'e2),
ie., the quote is needed which really is not pretty. But if do is
implemented as a macro, then you don't need the quote and can write in
a more natural way.

First, is the above of my understanding correct?

Second, if the above is correct, then, back to the subject of this
post, is macro relevant for a lazy functional language?

Thanks for your time and patience to read this! Please clarify this
issue for me! Thank you!

Thant Tessman

unread,
Jan 23, 2003, 12:03:01 PM1/23/03
to
zhaoway wrote:

> Sometime ago, a C++ programmer asks me what is that macros can do
> while inline functions can't.

The hot topic in the C++ community right now is using templates to do
"metaprogramming"--that is, tricking the template instantiation
mechanism into generating computer code which is subsequently compiled.
Scheme and lisp macros are about metaprogramming, only it's way beyond
what can be done in C++. Your analysis is correct, but it's only the
beginning.

-thant

Joe Marshall

unread,
Jan 23, 2003, 2:28:51 PM1/23/03
to
z...@netspeed-tech.com (zhaoway) writes:

> To put it in the context of Scheme, a function call is something like
> (f a b c), if do is implemented as a function call based on other
> primitives, you have to call it as (do '((v i s)) '(t r) 'e1 'e2),
> ie., the quote is needed which really is not pretty. But if do is
> implemented as a macro, then you don't need the quote and can write in
> a more natural way.
>
> First, is the above of my understanding correct?

Yes.

> Second, if the above is correct, then, back to the subject of this
> post, is macro relevant for a lazy functional language?

Yes. Consider a test harness macro that not only runs the code, but
also prints a string to a log file that describes the code being run.

harsh

unread,
Jan 23, 2003, 2:56:21 PM1/23/03
to

R there ny,,....
i m a newbie and have no idea bout that
links or library names required...

thanks for ny help


--
=============================================================================
.--.
|o_o |
|:_/ | choose a job you love,and you will never
// \ \ have to work a day in your life
(|harsh|)
/'\_ _/`\
\___)=(___/

=============================================================================

zhaoway

unread,
Jan 23, 2003, 6:15:23 PM1/23/03
to
Joe Marshall <j...@ccs.neu.edu> wrote in message news:<fzrjwt...@ccs.neu.edu>...

> Yes. Consider a test harness macro that not only runs the code, but
> also prints a string to a log file that describes the code being run.

That's right! This is just another cute application of macros.

Thanks for the clarification!

Jeffrey Siegal

unread,
Jan 23, 2003, 8:13:24 PM1/23/03
to
Thant Tessman wrote:
> Scheme and lisp macros are about metaprogramming, only it's way beyond
> what can be done in C++.

Yes and no. Constant expressions in C++ template expressions get
evaluated at compile time, which gives you the ability to implement
numerical computations at compile time. Scheme doesn't have a concept
of constant expressions, so this can't be done without extreme effort.

Thant Tessman

unread,
Jan 23, 2003, 8:56:41 PM1/23/03
to

Not sure I understand. When I build a Scheme macro, I have the full
power of the language available. Macros can use values produced before
the macro itself is evaluated. How is this inferior to C++'s
template-langauge constant expressions?

If C++'s macros have any advantage over Scheme's, it's derived from
their tie-in with C++'s type system.

-thant

Thant Tessman

unread,
Jan 23, 2003, 8:58:03 PM1/23/03
to
Thant Tessman wrote:

[...]

> If C++'s macros have any advantage over Scheme's, it's derived from
> their tie-in with C++'s type system.

Of course that should be "...C++'s templates...", not "...C++'s macros...".

-thant

Jeffrey Siegal

unread,
Jan 23, 2003, 9:00:30 PM1/23/03
to
Thant Tessman wrote:
> Not sure I understand. When I build a Scheme macro, I have the full
> power of the language available.

What sort of "Scheme macro" are you referring to? R5RS macros certainly
don't have this. Some Scheme implementations have a low level macro
system with more power (at least in a practical if not theoretical sense).

Darius

unread,
Jan 24, 2003, 1:54:14 AM1/24/03
to
z...@netspeed-tech.com (zhaoway) wrote in message news:<345c8989.03012...@posting.google.com>...

One word (a really long word ;):
http://research.microsoft.com/~simonpj/papers/meta-haskell

Though I'd say it's slightly less necessary in a lazy functional
language (don't need to worry about eager evaluation of 1/0, e.g. if
can be a function instead of a special form).

Thant Tessman

unread,
Jan 24, 2003, 8:31:20 AM1/24/03
to

Is this your way of starting a conversation about how the current
standard leaves something to be desired?

My experience actually predates the R5RS when technically there was no
standard macro system. I used, among other things, syntax-case. If I
remember correctly, it didn't have the limitation you allude to despite
being high level. I must sadly admit, however, that I've completely
forgotten how to use it.

-thant

Jeffrey Mark Siskind

unread,
Jan 24, 2003, 9:57:32 AM1/24/03
to
Several years back, Richard Mann used FFIGEN to parse the header
files for Xlib and OpenGL and write a set of signature files in
S-expression format. He then wrote a collection of macros for a
number of Scheme and Common Lisp implementations that
automatically generate the FFI declarations for Xlib and OpenGL in
those implementations. This provides a cross-platform compatible
API for Xlib and OpenGL. I believe he did this for GCL, AllegroCL,
Scheme->C, Stalin, and perhaps others. It should be straightforward
to adapt this to any other Common Lisp or Scheme implementation
that provides an FFI that supports passing and return scalar
objects.

http://tapir-server.uwaterloo.ca/~mannr/software.html

zhaoway

unread,
Jan 24, 2003, 1:55:30 PM1/24/03
to
Joe Marshall <j...@ccs.neu.edu> wrote in message news:<fzrjwt...@ccs.neu.edu>...
> Yes. Consider a test harness macro that not only runs the code, but
> also prints a string to a log file that describes the code being run.

I experimented for awhile and found the above, or somthing similiar,
is not easy to achieve. Could you be kind enough to provide me an
example, or a pointer to some example implementation of the above, or
similiar functions?

Thank you!

Jeffrey Siegal

unread,
Jan 24, 2003, 2:22:55 PM1/24/03
to
Thant Tessman wrote:
> Jeffrey Siegal wrote:
>
>> Thant Tessman wrote:
>>
>>> Not sure I understand. When I build a Scheme macro, I have the full
>>> power of the language available.
>>
>>
>>
>> What sort of "Scheme macro" are you referring to? R5RS macros
>> certainly don't have this. Some Scheme implementations have a low
>> level macro system with more power (at least in a practical if not
>> theoretical sense).
>
>
> Is this your way of starting a conversation about how the current
> standard leaves something to be desired?

Possibly. I'm just pointing out that R5RS macros don't have the fully
power of the language available. What's more, they don't have a way to
do compile-time arithmetic on ordinary constants the way C++ templates do.

Al Petrofsky's suggestion to break up the digits (thereby making
compile-time constants recognizable by a macro) is clever, but not
nearly as convenient (or in some sense powerful) as being able to
operate on ordinary constants.

> My experience actually predates the R5RS when technically there was no
> standard macro system. I used, among other things, syntax-case. If I
> remember correctly, it didn't have the limitation you allude to despite
> being high level. I must sadly admit, however, that I've completely
> forgotten how to use it.

Syntax-case is sometimes described as low-level, at least in contast to
R5RS. Of cousre, syntax-case also supports "high level" pattern-based
rules.


Radey Shouman

unread,
Jan 24, 2003, 3:26:27 PM1/24/03
to
z...@netspeed-tech.com (zhaoway) writes:

From SCM/syntest1.scm, a file for testing the syntax-rules implementation:


(define synerrs '())

(define-syntax test
(syntax-rules ()
((test ?exp ?ans)
(begin
(display '?exp)
(display " ==> ")
(let* ((x ?exp))
(display x)
(newline)
(or (equal? x ?ans)
(begin
(set! synerrs
(cons (list x ?ans '?exp) synerrs))
(display "ERROR: expected ")
(display ?ans)
(newline))))))
((test ?exp0 ?exp1 ?exp2 ...)
(begin (display '?exp0)
(newline)
?exp0 (test ?exp1 ?exp2 ...)))))

(test (let ((x 'outer))
(let-syntax ((m (syntax-rules () ((m) x))))
(let ((x 'inner))
(m))))
'outer)

(test (let-syntax ((when (syntax-rules
()
((when ?test ?stmt1 ?stmt2 ...)
(if ?test (begin ?stmt1 ?stmt2 ...))))))
(let ((if #t))
(when if (set! if 'now))
if))
'now)

;;more tests elided, at the bottom of the file we do something with synerrs.

David Rush

unread,
Jan 25, 2003, 7:12:20 AM1/25/03
to
Jeffrey Siegal <j...@quiotix.com> writes:
> Possibly. I'm just pointing out that R5RS macros don't have the
> fully power of the language available. What's more, they don't have a
> way to do compile-time arithmetic on ordinary constants the way C++
> templates do.

Just as an item of contentious curiousity, how many Scheme compilers
*don't* do constant folding & propagation for simple arithmetic? I'd
really like to know so I can avoid such surprisingly stupid compilers.

david rush
--
The Revolution has begun...Comrade Shivers's helicopter will land on
College Green at 3.14pm. Deployment of the Underground begins soon
thereafter. Lambda bandanas will be available...Please remain calm
and nobody will get skewered on parentheses. -- sk (on c.l.s)

Anton van Straaten

unread,
Jan 25, 2003, 8:55:53 AM1/25/03
to
> X11 libraries for Scheme
> R there ny,,....

You could try Xlibscm: http://www.swiss.ai.mit.edu/~jaffer/Xlibscm_1.html

If you simply want to be able to develop GUI applications that run under X,
I'd recommend using a higher-level Scheme API, and avoid coding directly to
the X Window API. PLT Scheme (http://www.plt-scheme.org/) provides such an
API, in the form MrEd (http://www.plt-scheme.org/software/mred/). The
resulting applications run under Unix/X, Windows, and Mac.

Anton

bri...@aracnet.com

unread,
Jan 25, 2003, 11:24:27 AM1/25/03
to

Hey I know about that.

I picked up the ffigen distribution and worked with it until I had
something which could generate ffi's automagically. Works very nicely.

However it requires modifying and compiling lcc which is not
particularly hard, but somewhat a pain. So to make it more widely
useable I have decided to work with swig. I use the xml output option
from swig and the scheme sxpathlib to parse the xml.

The big advantages of using swig & xml:

1 you can write your ffi generation code in scheme (as it should be)

2. it's free (as in speech)

3. It supports c++. Many gui's use c++ so it was important for an
ffi generator to be able to generate c++ glue.

The c-portion of this effort is very useful and I can currently
generate a _complete_ ffi for glut. You have to do the #define
conversion manually, but that's trivial and it takes 5 min with emacs.

I'm working to get basic functionality finished for the c++ portion -
stay tuned.

Brian


>>>>> "Jeffrey" == Jeffrey Mark Siskind <qo...@purdue.edu> writes:

Jeffrey> Several years back, Richard Mann used FFIGEN to parse the
Jeffrey> header files for Xlib and OpenGL and write a set of
Jeffrey> signature files in S-expression format. He then wrote a
Jeffrey> collection of macros for a number of Scheme and Common Lisp
Jeffrey> implementations that automatically generate the FFI
Jeffrey> declarations for Xlib and OpenGL in those
Jeffrey> implementations. This provides a cross-platform compatible
Jeffrey> API for Xlib and OpenGL. I believe he did this for GCL,
Jeffrey> AllegroCL,


Scheme-> C, Stalin, and perhaps others. It should be straightforward

Jeffrey> to adapt this to any other Common Lisp or Scheme
Jeffrey> implementation that provides an FFI that supports passing
Jeffrey> and return scalar objects.

Jeffrey> http://tapir-server.uwaterloo.ca/~mannr/software.html

--

"There is no right place for the dead to live."

-- Kai, last of the Brunnen-G

Rob Browning

unread,
Feb 6, 2003, 9:56:01 PM2/6/03
to
bri...@aracnet.com writes:

> However it requires modifying and compiling lcc which is not
> particularly hard, but somewhat a pain. So to make it more widely
> useable I have decided to work with swig. I use the xml output option
> from swig and the scheme sxpathlib to parse the xml.
>
> The big advantages of using swig & xml:

Does swig actually have a full C parser now? It seems like a proper
(perhaps gcc compatible) C parser that could spit out sexp definitions
and prototypes from the contents of C headers might be fairly useful.
Though xml might be functionally equivalent, if a bit more awkward.
i.e. given pcre.h you might get

(c-define "PCRE_CASELESS" 1)
(c-define "PCRE_MULTILINE" 2)
...
(c-func "pcre_compile" (ptr pcre) ((const (ptr char)) int ...))

etc.

--
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4

Eric Marsden

unread,
Feb 7, 2003, 6:54:12 AM2/7/03
to
>>>>> "rb" == Rob Browning <r...@defaultvalue.org> writes:

rb> It seems like a proper (perhaps gcc compatible) C parser that
rb> could spit out sexp definitions and prototypes from the contents
rb> of C headers might be fairly useful.

the OpenMCL developers have patched gcc to output sexp descriptions of
C headers. See

<URL:http://openmcl.clozure.com/Doc/interface-translation.html>

--
Eric Marsden <URL:http://www.laas.fr/~emarsden/>

bri...@aracnet.com

unread,
Feb 8, 2003, 8:15:27 PM2/8/03
to
>>>>> "Rob" == Rob Browning <r...@defaultvalue.org> writes:

Rob> Does swig actually have a full C parser now? It seems like a
Rob> proper (perhaps gcc compatible) C parser that could spit out
Rob> sexp definitions and prototypes from the contents of C headers
Rob> might be fairly useful. Though xml might be functionally
Rob> equivalent, if a bit more awkward. i.e. given pcre.h you might
Rob> get

Don't think it does have a full parser, but I've been running it
through the paces on GLUT and Xlib and so far so good.

Brian

bri...@aracnet.com

unread,
Feb 8, 2003, 8:16:59 PM2/8/03
to
>>>>> "ecm" == Eric Marsden <emar...@laas.fr> writes:

>>>>> "rb" == Rob Browning <r...@defaultvalue.org> writes:
rb> It seems like a proper (perhaps gcc compatible) C parser that
rb> could spit out sexp definitions and prototypes from the contents
rb> of C headers might be fairly useful.

ecm> the OpenMCL developers have patched gcc to output sexp
ecm> descriptions of C headers. See

ecm>
ecm> <URL:http://openmcl.clozure.com/Doc/interface-translation.html>

This is very cool. Although I have to admit that the thought of
having to build a patched version of gcc gives me a headache before I
even start.

Brian

felix

unread,
Feb 9, 2003, 8:55:04 AM2/9/03
to
bri...@aracnet.com wrote:
>>>>>>"ecm" == Eric Marsden <emar...@laas.fr> writes:
>>>>>
>
>>>>>>"rb" == Rob Browning <r...@defaultvalue.org> writes:
>>>>>
> rb> It seems like a proper (perhaps gcc compatible) C parser that
> rb> could spit out sexp definitions and prototypes from the contents
> rb> of C headers might be fairly useful.
>
> ecm> the OpenMCL developers have patched gcc to output sexp
> ecm> descriptions of C headers. See
>
> ecm>
> ecm> <URL:http://openmcl.clozure.com/Doc/interface-translation.html>
>
> This is very cool. Although I have to admit that the thought of
> having to build a patched version of gcc gives me a headache before I
> even start.
>

You might also be interested in GCCXML. It parses C++ and outputs
XML, and looks *very* promising. You can download a binary that
makes it quite uncomplicated to install and use.

See: www.gccxml.org


cheers,
felix


0 new messages