Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Explanation of macros; Haskell macros

Reply
Thread Tools

Explanation of macros; Haskell macros

 
 
Robert Klemme
Guest
Posts: n/a
 
      10-27-2003

"Lex Spoon" <> schrieb im Newsbeitrag
news:...
> This thread has seemed to miss the biggest advantage of macros. Most
> examples so far are cases where the macro buys you faster code. This
> is not extremely exciting, IMHO, because a better compiler can often
> accomplish the things that are described. If you care that much about
> performance then surely you want to be using a good optimizing
> compiler, and a good compiler will surely know about, e.g., for loops
> that go across constant ranges. I tend to think of macros as bad
> style for this kind of thing, unless it's very clearly a performance
> problem you are having. Macros often look like functions, but they
> don't act like them, and it's very easy to introduce bugs with them.
>
> A much bigger advantage of macros is that you can literally extend
> the language. For example, it is nice to be able to type something
> like this:
>
> (regex (| h H) "ello, " (| w W) orld (? "!"))
>
>
> To allow this in the nicest way requires using a macro for 'regex.
> Without macros, you can still do it:
>
> (regex '(| h H) "ello, " (| w W) orld (? "!"))
>
> but now this guy is going to get compiled at runtime, and you get into
> the issue of trying to save the compiled regex somewhere ahead of
> time. And while regexes themselves compile pretty quickly, suppose
> it's something more like:
>
> (parser
> (grammer
> ;;... a BNF grammer ...
>
>
>
> I also loved the example of both printing a statement and executing
> it, which is extremely useful for assertion checking. You can't
> really do this properly without macros.


Just for the sake of my understanding: could one summarize this as "macros
make control of *when* something is evaluated easier"? I mean, marco
arguments are not evaluated before the macro "call" as opposed to function
arguments. With that, you can decide inside the macro, how you treat
those arguments and if you evaluate them at all. Thx!

Regards

robert

 
Reply With Quote
 
 
 
 
Alain Picard
Guest
Posts: n/a
 
      10-27-2003
"Robert Klemme" <> writes:

> Just for the sake of my understanding: could one summarize this as "macros
> make control of *when* something is evaluated easier"?


More precisely: "macros make control of when, if, and how often,
something is evaluated _possible_."

Functions (your only other option) give you no choice.

But, yeah, you've got it.

--
It would be difficult to construe Larry Wall, in article
this as a feature. <>
 
Reply With Quote
 
 
 
 
Lex Spoon
Guest
Posts: n/a
 
      10-28-2003
"Robert Klemme" <> writes:
> Just for the sake of my understanding: could one summarize this as "macros
> make control of *when* something is evaluated easier"? I mean, marco
> arguments are not evaluated before the macro "call" as opposed to function
> arguments. With that, you can decide inside the macro, how you treat
> those arguments and if you evaluate them at all. Thx!



Yes, that's one big advantage. If you are willing to do everything at
runtime, then HOF plus a nice general syntax for literals
(e.g. s-expressions) give you the same thing.


-Lex
 
Reply With Quote
 
Joachim Durchholz
Guest
Posts: n/a
 
      10-30-2003
Alain Picard wrote:

> "Robert Klemme" <> writes:
>
>>Just for the sake of my understanding: could one summarize this as "macros
>>make control of *when* something is evaluated easier"?

>
> More precisely: "macros make control of when, if, and how often,
> something is evaluated _possible_."
>
> Functions (your only other option) give you no choice.


This statement is wrong if left in full generality: higher-order
functions can control quite precisely what gets evaluated when. If the
language offers both lazy and strict parameters, evaluation time control
extends to the parameter expressions as well.
It's a different kind of control than macros, of course. (It would be
interesting to see how things would map from macros to functions and
vice versa.)

Regards,
Jo

 
Reply With Quote
 
Alain Picard
Guest
Posts: n/a
 
      10-30-2003
Joachim Durchholz <> writes:

> Alain Picard wrote:
>
>> More precisely: "macros make control of when, if, and how often,
>> something is evaluated _possible_." Functions
>> (your only other option) give you no choice.

>
> This statement is wrong if left in full generality: higher-order
> functions can control quite precisely what gets evaluated when. If the
> language offers both lazy and strict parameters, evaluation time
> control extends to the parameter expressions as well.


My apologies to the functional guys. I was responding from a lisp
newsgroup: so let me amend the above to end with "IN LISP".
[We do not have lazy evaluation by default.]


 
Reply With Quote
 
Joachim Durchholz
Guest
Posts: n/a
 
      10-30-2003
Alain Picard wrote:
> Joachim Durchholz <> writes:
>>Alain Picard wrote:
>>>More precisely: "macros make control of when, if, and how often,
>>> something is evaluated _possible_." Functions
>>>(your only other option) give you no choice.

>>
>>This statement is wrong if left in full generality: higher-order
>>functions can control quite precisely what gets evaluated when. If the
>>language offers both lazy and strict parameters, evaluation time
>>control extends to the parameter expressions as well.

>
> My apologies to the functional guys. I was responding from a lisp
> newsgroup:


Ah, the joys of inadvertent crossposting

> so let me amend the above to end with "IN LISP".


IIRC even Lisp allows you to keep expressions unevaluated via quoting.
So macros wouldn't be the only way to control evaluation: quote the
expression and have the callee evaluate it at a convenient time.

> [We do not have lazy evaluation by default.]


The default evaluation strategy doesn't change too much; all you need is
some way of deferred evaluation. It need not even be laziness, having a
meta level (such as quoting) or access to the compiler can be used.

As I said, it's still different from macros.

Regards,
Jo

 
Reply With Quote
 
Joachim Durchholz
Guest
Posts: n/a
 
      10-30-2003
Roman Belenov wrote:
> Joachim Durchholz <> writes:
>
>>IIRC even Lisp allows you to keep expressions unevaluated via
>>quoting. So macros wouldn't be the only way to control evaluation:
>>quote the expression and have the callee evaluate it at a convenient
>>time.

>
>
> The problem is that quoted form is just a piece of data from
> compiler's point of view, so it is kept as is and has to be interpeted
> in runtime (while code generated by macros is usually compiled
> normally);


If that's the case, it's probably more due to lack of demand than due to
serious technical issues.

> besides, such forms can not access lexical variables in the
> enclosing context (otherwise runtime environment would have to support
> access to lexicals by name, which has performance implications and is
> not necessary for normal compiled code). So, theoretically, you can
> use quoting to control the order of evaluation, but practically it
> will lead to very inefficient [code]


Agreed.

> and [quoting will lead to] less intuitive code.


Efficiency issues aside: how are macros more intuitive than quoting?

Regards,
Jo

 
Reply With Quote
 
Roman Belenov
Guest
Posts: n/a
 
      10-30-2003
Joachim Durchholz <> writes:

> IIRC even Lisp allows you to keep expressions unevaluated via
> quoting. So macros wouldn't be the only way to control evaluation:
> quote the expression and have the callee evaluate it at a convenient
> time.


The problem is that quoted form is just a piece of data from
compiler's point of view, so it is kept as is and has to be interpeted
in runtime (while code generated by macros is usually compiled
normally); besides, such forms can not access lexical variables in the
enclosing context (otherwise runtime environment would have to support
access to lexicals by name, which has performance implications and is
not necessary for normal compiled code). So, theoretically, you can
use quoting to control the order of evaluation, but practically it
will lead to very inefficient and less intuitive code.

--
With regards, Roman.

Standard disclaimer: I work for them, but I don't speak for them.
 
Reply With Quote
 
Peter Seibel
Guest
Posts: n/a
 
      10-30-2003
Joachim Durchholz <> writes:

> Efficiency issues aside: how are macros more intuitive than quoting?


Because they let you write what you intuitively expect (assuming your
intuitions are tuned a particular way, I suppose). In Common Lisp,
WHEN is a macro that lets you write this:

(when (> x y) (print x))

If you tried to write it as a function using quoting to prevent
evaluation you might try this:

(when (> x y) '(print x))

which won't work in general because of the iniability for the code
inside WHEN to evaluate the data '(print x) in the lexical environment
where it appears.

Or you could do this:

(when (> x y) #'(lambda () (print x)))

which could work but seems a bit convoluted (i.e. unintuitive)
compared to the macro version.

-Peter

--
Peter Seibel

Lisp is the red pill. -- John Fraser, comp.lang.lisp
 
Reply With Quote
 
Joachim Durchholz
Guest
Posts: n/a
 
      10-30-2003
Peter Seibel wrote:
> Joachim Durchholz <> writes:
>
>
>>Efficiency issues aside: how are macros more intuitive than quoting?

>
> Or you could do this:
>
> (when (> x y) #'(lambda () (print x)))
>
> which could work but seems a bit convoluted (i.e. unintuitive)
> compared to the macro version.


Um, right, but that's just a question of having the right syntactic sugar.

Regards,
Jo

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
playing with berp: python3 to haskell compiler in haskell Alia Khouri Python 0 10-31-2010 08:16 AM
Hubris connects Ruby to Haskell, will there be such a connection between Python and Haskell? Casey Hawthorne Python 3 02-17-2010 10:31 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57