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!
> 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
> 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.
thanks for ny help
--
=============================================================================
.--.
|o_o |
|:_/ | choose a job you love,and you will never
// \ \ have to work a day in your life
(|harsh|)
/'\_ _/`\
\___)=(___/
=============================================================================
That's right! This is just another cute application of macros.
Thanks for the clarification!
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.
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
[...]
> 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
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).
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).
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
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!
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.
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.
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)
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
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
> 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
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/>
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
>>>>> "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
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