Lisp macros

Whoops, this belongs on ruby-talk... Sorry.

···

---------- Forwarded message ----------
From: Joe Van Dyk <joevandyk@gmail.com>
Date: Sep 26, 2005 6:20 PM
Subject: Lisp macros
To: rails@lists.rubyonrails.org

So, I'm diving into a little bit of Lisp, using
http://www.gigamonkeys.com/book/ as a starting point.

Lisp's big selling point is macros, right? I'm only up to chapter 3,
but from that (maybe very basic?) display of macros, it looks like
something that can be done in Ruby.

<excerpt>
(defun make-comparison-expr (field value)
  `(equal (getf cd ,field) ,value))

(defun make-comparisons-list (fields)
  (loop while fields
     collecting (make-comparison-expr (pop fields) (pop fields))))

(defmacro where (&rest clauses)
  `#'(lambda (cd) (and ,@(make-comparisons-list clauses))))

CL-USER> (macroexpand-1 '(where :title "Give Us a Break" :ripped t))
#'(LAMBDA (CD)
    (AND (EQUAL (GETF CD :TITLE) "Give Us a Break")
         (EQUAL (GETF CD :RIPPED) T)))
T

You could use eval + procs to do that, right?

Joe

Joe Van Dyk wrote:

Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on ruby-core about adding true Lisp-style macros to Ruby. My understanding is that while Ruby can do assorted flips and twists, it cannot do quite the same sort of things as found in Lisp (and I hope a true Lisper here can elaborate on this); I also believe that Matz is less than enamored with idea of adding this to Ruby (and I hope a true Matz can elaborate on this, too).

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

The big thing Lisp has that Ruby can't do is code-as-data. I wish I could provide a good example of how that might be used practically, but it's been quite a while since I touched lisp.

Devin

Joe Van Dyk wrote:

···

Whoops, this belongs on ruby-talk... Sorry.

---------- Forwarded message ----------
From: Joe Van Dyk <joevandyk@gmail.com>
Date: Sep 26, 2005 6:20 PM
Subject: Lisp macros
To: rails@lists.rubyonrails.org

So, I'm diving into a little bit of Lisp, using
Practical Common Lisp as a starting point.

Lisp's big selling point is macros, right? I'm only up to chapter 3,
but from that (maybe very basic?) display of macros, it looks like
something that can be done in Ruby.

<excerpt>
(defun make-comparison-expr (field value)
`(equal (getf cd ,field) ,value))

(defun make-comparisons-list (fields)
(loop while fields
    collecting (make-comparison-expr (pop fields) (pop fields))))

(defmacro where (&rest clauses)
`#'(lambda (cd) (and ,@(make-comparisons-list clauses))))

CL-USER> (macroexpand-1 '(where :title "Give Us a Break" :ripped t))
#'(LAMBDA (CD)
   (AND (EQUAL (GETF CD :TITLE) "Give Us a Break")
        (EQUAL (GETF CD :RIPPED) T)))
T

You could use eval + procs to do that, right?

Joe

Almost here with this http://rubyforge.org/projects/parsetree/ Just need to
able able to modify it and feed it back.

This http://boo.codehaus.org/Syntactic+Macros is also interesting reading.

Lyndon

···

On 9/27/05, James Britt <james_b@neurogami.com> wrote:

Joe Van Dyk wrote:
> Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the same
sort of things as found in Lisp (and I hope a true Lisper here can
elaborate on this); I also believe that Matz is less than enamored with
idea of adding this to Ruby (and I hope a true Matz can elaborate on
this, too).

I'm just repeating what I've heard numerous times, but apparently, you
can only get Lisp macros if the language has lots of silly irritating
parenthesis. And since Ruby has syntax, it wouldn't be possible.

Or something. Don't pay attention to me. I have no clue what i'm
talking about. :frowning:

···

On 9/26/05, James Britt <james_b@neurogami.com> wrote:

Joe Van Dyk wrote:
> Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the same
sort of things as found in Lisp (and I hope a true Lisper here can
elaborate on this); I also believe that Matz is less than enamored with
idea of adding this to Ruby (and I hope a true Matz can elaborate on
this, too).

If anyone wants to chime in with an example of what code-as-data is or
how it could be used, I'd appreciate it!

···

On 9/26/05, Devin Mullins <twifkak@comcast.net> wrote:

The big thing Lisp has that Ruby can't do is code-as-data. I wish I
could provide a good example of how that might be used practically, but
it's been quite a while since I touched lisp.

James Britt wrote:

Joe Van Dyk wrote:

Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on
ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the
same sort of things as found in Lisp (and I hope a true Lisper here
can elaborate on this); I also believe that Matz is less than enamored
with idea of adding this to Ruby (and I hope a true Matz can elaborate
on this, too).

I'm neither a true Matz nor a true Lisper. The basic facility Lisp
macros offer over what is available in Ruby is that they do *not*
evaluate their arguments. Where this is not an issue, you can get very
far with what is there in Ruby.

Michael

···

--
Michael Schuerig The more it stays the same,
mailto:michael@schuerig.de The less it changes!
Michael Schürig | Sentenced to making sense --Spinal Tap, The Majesty of Rock

Matz has said (at the Lightweight Language Conf) that Ruby will never
have macros because they are too easily abused, by the average person,
to mutate the language.

Yes, Ruby has continuations and one can abuse them, but you have to
be really smart. The average person won't even use them, let alone
abuse them. :slight_smile:

···

On 9/26/05, James Britt <james_b@neurogami.com> wrote:

Joe Van Dyk wrote:

Anyway, there have been sporadic discussions both here and on ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the same
sort of things as found in Lisp (and I hope a true Lisper here can
elaborate on this); I also believe that Matz is less than enamored with
idea of adding this to Ruby (and I hope a true Matz can elaborate on
this, too).

--
Jim Freeze

Devin Mullins wrote:

The big thing Lisp has that Ruby can't do is code-as-data. I wish I could provide a good example of how that might be used practically, but it's been quite a while since I touched lisp.

Devin

Well, nobody's provided an example. The best I can do is provide more explanation. Since code is just (blah blah (blah blah)) -- that is, just a list -- and Lisp has the capability to delay evaluation (I think with an '), you can pass code into another function and have that function actually manipulate the internals of your code, and build some new code out of it. You could, maybe, write something that stripped all the print statements out at runtime... Crappy example...

Help! Is there a Lisper in the house?

Devin

Joe Van Dyk wrote:

The big thing Lisp has that Ruby can't do is code-as-data. I wish I
could provide a good example of how that might be used practically, but
it's been quite a while since I touched lisp.
   

If anyone wants to chime in with an example of what code-as-data is or
how it could be used, I'd appreciate it!

--- foo.properties ---
foo_name = 'MyFoo',
foo_description = 'A code-as-data foo'
foo_question = 'Why is this useful?'
foo_answer = "Ah, well, sometimes it is"

--- foo_properties_loader.rb ---
puts File.read('foo.properties')
puts

load 'foo.properties'
puts foo_question
puts foo_answer

:slight_smile:

Alex

···

On 9/26/05, Devin Mullins <twifkak@comcast.net> wrote:

Thank you!

Does Lisp have any other advantages over Ruby that I should be looking
for? When would I want to use Lisp over Ruby?

···

On 9/26/05, Michael Schuerig <michael@schuerig.de> wrote:

James Britt wrote:

> Joe Van Dyk wrote:
>> Whoops, this belongs on ruby-talk... Sorry.
>
> I trust the accidental exposure to Lisp wasn't too traumatizing.
>
> Anyway, there have been sporadic discussions both here and on
> ruby-core
> about adding true Lisp-style macros to Ruby. My understanding is that
> while Ruby can do assorted flips and twists, it cannot do quite the
> same sort of things as found in Lisp (and I hope a true Lisper here
> can elaborate on this); I also believe that Matz is less than enamored
> with idea of adding this to Ruby (and I hope a true Matz can elaborate
> on this, too).

I'm neither a true Matz nor a true Lisper. The basic facility Lisp
macros offer over what is available in Ruby is that they do *not*
evaluate their arguments. Where this is not an issue, you can get very
far with what is there in Ruby.

> Joe Van Dyk wrote:
>
> Anyway, there have been sporadic discussions both here and on ruby-core
> about adding true Lisp-style macros to Ruby. My understanding is that
> while Ruby can do assorted flips and twists, it cannot do quite the same
> sort of things as found in Lisp (and I hope a true Lisper here can
> elaborate on this); I also believe that Matz is less than enamored with
> idea of adding this to Ruby (and I hope a true Matz can elaborate on
> this, too).

Matz has said (at the Lightweight Language Conf) that Ruby will never
have macros because they are too easily abused, by the average person,
to mutate the language.

Was he joking?

···

On 9/26/05, Jim Freeze <jim@freeze.org> wrote:

On 9/26/05, James Britt <james_b@neurogami.com> wrote:

Yes, Ruby has continuations and one can abuse them, but you have to
be really smart. The average person won't even use them, let alone
abuse them. :slight_smile:

Joe Van Dyk ha scritto:

···

On 9/26/05, James Britt <james_b@neurogami.com> wrote:

Joe Van Dyk wrote:

Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the same
sort of things as found in Lisp (and I hope a true Lisper here can
elaborate on this); I also believe that Matz is less than enamored with
idea of adding this to Ruby (and I hope a true Matz can elaborate on
this, too).

I'm just repeating what I've heard numerous times, but apparently, you
can only get Lisp macros if the language has lots of silly irritating
parenthesis. And since Ruby has syntax, it wouldn't be possible.

well you can get lisp macros only in a s-exp based language, but there are other mcro systems :slight_smile:
Take a look at Dylan (close relative of lisp with more syntax), Nemerle and Logix. All succed to put a powerful macro system in a syntax which does not need prefix based everything :slight_smile:

No, all you need is a way to manipulate the abstract syntax tree from
code (or, indeed, the bytecode representation from code, for languages
that work that way). Lisp gets it for free because s expressions *are*
an abstract syntax tree.

martin

···

Joe Van Dyk <joevandyk@gmail.com> wrote:

I'm just repeating what I've heard numerous times, but apparently, you
can only get Lisp macros if the language has lots of silly irritating
parenthesis. And since Ruby has syntax, it wouldn't be possible.

Or something. Don't pay attention to me. I have no clue what i'm
talking about. :frowning:

This is a cop-out. Every time a new language comes around and
doesn't support some feature, the reason is always "too easily
abused". Macros in lisp are about abstraction. Like every other
abstraction - interfaces, classes, pointers, etc, they can be abused
horribly. They can also be a powerful tool.
  If I recall my CS 101, computer programming is built on the pillars
of algorithms and data structures. Lisp and its variants are neat
because they represent the code as a native data structure. With that
in place it becomes easy to create algorithms to manipulate the data
structure, changing the syntax as you write, and the code as it runs.
If you're familiar with the more complex metaprogramming techniques
C++ provides with templates, you'll start to get the idea of what lisp
macros offer you. Macros are less limiting than C++ templates though,
and more naturally represented.

  Consider an array in Ruby whose first element is a method call:
a = [method, param1, param2]
eval "${a[0]} ${a[1]} ${a[2]}"
If param1 becomes an array, that is a nested call:
a = [method, [method2, x, y], param2]

In Lisp, if 'method' is a macro:
a = [macro, [method2, x, y], param2]
then macro gets 'expanded' at compile time WITHOUT evaluating the parameters:
a = [method [do-crazy-thing param2 [do-cool-thing [method2, x, y] ] ]
[ [param2 method2, x, y] ] ]
; Did I match all my braces?

I really like Lisp. :slight_smile:

-Ben

···

On 9/27/05, Jim Freeze <jim@freeze.org> wrote:

Matz has said (at the Lightweight Language Conf) that Ruby will never
have macros because they are too easily abused, by the average person,
to mutate the language.

Part of the attraction of Lisp macros is that they allow you to keep
the core language very small, and add the features you want with
macros. For example, as long as you have the lambda operator, you can
define things like "while" and "let" as macros, not keywords. This is
in fact how many Lisp implementations work, because it keeps the
compiler/interpreter simple.

Another use for macros is to perform some of your computation at
compile time, saving you from ever needing to perform it again at
runtime.

In the general case, macros can be used to rewrite whole programs.
For example, even though Lisp doesn't natively support continuations,
you can build them with macros that rewrite your program into an
explicit continuation-passing style.

Paul Graham's "On Lisp" is available online and contains several
chapters about macros. It gives many more examples:

http://www.paulgraham.com/onlisptext.html

regards,
Ed

···

On Tue, Sep 27, 2005 at 09:21:07PM +0900, Devin Mullins wrote:

Crappy example...

Help! Is there a Lisper in the house?

"Lyndon Samson" <lyndon.samson@gmail.com> wrote in message

Almost here with this http://rubyforge.org/projects/parsetree/ Just need
to able able to modify it and feed it back.

Will parsetree (and Rubyinline) run on Windows, and is a precompiled version
available?

code as data is what makes lisp macros so powerful.

In a c macro you have the power of a little macro language to manipulate
strings, in a lisp macro you have the power of lisp language to manipulate
lisp data structures. I'll elaborate.

When the compiler sees a call to a macro it passes all of the arguments to
the macro, before evaluating.
so in a call like

(dotimes (i 10)
(do-some-crazy-thing)
(print i))

the macro dotimes is passed the list (i 10) and the list
((do-some-crazy-thing) (print i))
the macro can then operate on them as data, using the full power of lisp. At
the end of all this, the macro returns another piece of data, a list, which
is then interpreted as a lisp form, code, and evaluated.

If lisp code wasn't the same as lisp data, macros would be a lot less
powerful as you would have to have two languages, one for manipulating lisp
data, and another for manipulating lisp code (this is what you get in c).

Hope this helps. Sorry the example isn't very enlightening, it's difficult
to illustrate the power of macros in (< 500 words) as they're very
complicated. If you'd like to know more* ... learn lisp :slight_smile:

Now I will stop with the lisp advocacy.

*on lisp by Paul Graham is the bible on macros, but it's a little hard for
someone who's new to lisp, try practical common lisp by Peter Siebel, (both
are available free online).

···

On 9/27/05, Devin Mullins <twifkak@comcast.net> wrote:

Devin Mullins wrote:

> The big thing Lisp has that Ruby can't do is code-as-data. I wish I
> could provide a good example of how that might be used practically,
> but it's been quite a while since I touched lisp.
>
> Devin

Well, nobody's provided an example. The best I can do is provide more
explanation. Since code is just (blah blah (blah blah)) -- that is, just
a list -- and Lisp has the capability to delay evaluation (I think with
an '), you can pass code into another function and have that function
actually manipulate the internals of your code, and build some new code
out of it. You could, maybe, write something that stripped all the print
statements out at runtime... Crappy example...

Help! Is there a Lisper in the house?

Devin

In article <a39f6ad0050926185249a89232@mail.gmail.com>,

···

Lyndon Samson <lyndon.samson@gmail.com> wrote:

------=_Part_8710_13814260.1127785967504
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 9/27/05, James Britt <james_b@neurogami.com> wrote:

Joe Van Dyk wrote:
> Whoops, this belongs on ruby-talk... Sorry.

I trust the accidental exposure to Lisp wasn't too traumatizing.

Anyway, there have been sporadic discussions both here and on ruby-core
about adding true Lisp-style macros to Ruby. My understanding is that
while Ruby can do assorted flips and twists, it cannot do quite the same
sort of things as found in Lisp (and I hope a true Lisper here can
elaborate on this); I also believe that Matz is less than enamored with
idea of adding this to Ruby (and I hope a true Matz can elaborate on
this, too).

Almost here with this http://rubyforge.org/projects/parsetree/ Just need to
able able to modify it and feed it back.

This http://boo.codehaus.org/Syntactic+Macros is also interesting reading.

Then this was this post by George Moschovitis about a year ago where he
implmented a simple macro system (more of a preprocessor, really):
http://rubyurl.com/555

Phil

Thank you!

Does Lisp have any other advantages over Ruby that I should be looking
for? When would I want to use Lisp over Ruby?

When you want to write your own language, Lisp is very useful, I'm sure that you can do this quite well with ruby (but I'm no ruby expert), but Lisp is teh poster child for domain specific languages.

Kev