Re: LISP module from CPAN

2007-05-10 Thread Adrian Howard


On 9 May 2007, at 21:37, David Nicol wrote:


so if there were lisp-like macros available in perl, my sugar example
might be something like the following?

macro hashkeyaccessor(fieldname) {   sub fieldname { $_[0]- 
{fieldname}}  };

hashkeyaccessor $_ for qw/FIELD NAMES GO HERE .../;

and I would save the trickiness of getting the interpolation level
right.  That is
imagining a CPP macro...  the LISP idea on macros is that the template
would be created at the optree level rather than the source level,  
but its still

a template and replacement kind of deal.  What am I missing?


You're missing that you can execute code at compile time that  
determines what code you generate.


I'd recommend http://www.gigamonkeys.com/book/ as a reasonable into  
to Lisp for those who want to play. Macro chapter is http:// 
www.gigamonkeys.com/book/macros-defining-your-own.html.


Some devils brew of source filters and eval is the closest you'll get  
in Perl 5 Perl 6 with its explicit lexer/parser should give you  
the full power of Lisp style macros.


Adrian (recovering Smug Lisp Weenie :-)


Re: LISP module from CPAN

2007-05-10 Thread Ovid
--- David Nicol [EMAIL PROTECTED] wrote:

 Ever since reading Hackers and Painters
 http://www.amazon.com/exec/obidos/ISBN=0596006624/tipjartransactioA
 I've been defending Perl is a LISP!  (which is actually quite easy,
 because
 nobody knows what I'm talking about)

And for those who have not read the brilliant rebuttal to the Hackers
and Painters article:

  http://www.idlewords.com/2005/04/dabblers_and_blowhards.htm

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/


towards TERN::macro (was Re: LISP module from CPAN)

2007-05-10 Thread David Nicol

On 5/10/07, Adrian Howard [EMAIL PROTECTED] wrote:


On 9 May 2007, at 21:37, David Nicol wrote:
 [proposed macro syntax example:]
 macro hashkeyaccessor(fieldname) {   sub fieldname { $_[0]-
 {fieldname}}  };
 hashkeyaccessor $_ for qw/FIELD NAMES GO HERE .../;

 [summary of benefits of approach]  What am I missing?

You're missing that you can execute code at compile time that
determines what code you generate.


I disagree, I think I included that, and you didn't see that I had.

so to be fully lispy the extension would need to support

defmacro keylookup(key) { $_[0]-{key}}
defmacro hashkeyaccessor(fieldname) {   sub fieldname {
(keylookup fieldname) }  };
(hashkeyaccessor $_) for qw/FIELD NAMES GO HERE .../;

as well as




www.gigamonkeys.com/book/macros-defining-your-own.html

Some devils brew of source filters and eval is the closest you'll get
in Perl 5 Perl 6 with its explicit lexer/parser should give you
the full power of Lisp style macros.


the debate response to that is, show me something besides
syntax succinctness you can do with a LISP macro that you cannot
do with such a code-generated string eval


Adrian (recovering Smug Lisp Weenie :-)


That's fun link, the story of Mac the programmer, who eventually retires.  The
article contains this:

* When you write macros, you're writing programs that will be
* used by the compiler to generate the code that will then be
* compiled. Only after all the macros have been fully expanded
* and the resulting code compiled can the program actually be run.
* The time when macros run is called macro expansion time; this
* is distinct from runtime, when regular code, including the code
*  generated by macros, runs.

this separation of times sounds exactly the same as putting
an eval of generated source in a BEGIN block.  Adding a source
filter to the brew has to potential to allow for succinctness and
clarity that is absent from the generated source situation due
to the trickiness of escaping sygills appropriately  (easy to get
wrong, but you only have to get it once right, then it stays
working)

Later in the chapter, gigamonkeys admits that LISP macros also
suffer from the fact the human brain was not originially designed for
programming computers:
* In other words, you'll write a macro-writing macro: a
* macro that generates code that generates code. While
* complex macro-writing macros can be a bit confusing
* until you get used to keeping the various levels of code
* clear in your mind

LISP feature:  macros that produce macros.
Perl equivalent: evalling generated source to produce functions to
eval generated source.
Can do it, yes.  And it is a slightly bigger PITA than using LISP syntax.

If I find the time to put what I learned about source filters while
exploring writing a tail recursion optimizer (final result:  there
are too many edge cases: if you really want tail recursion
optimization, learn about Cgoto subname and use it) into
making my own macro syntax filter which will work as
exampled above, it will work as follows:

(1) remove pods and comments and abstract out quoting constructs;
(2) identify defmacro constructions, whatever they look like, convert
them to coderefs that take parameters and return the body as
evallable strings;
(3) identify expressions including defined macros, and run those
expressions immediately
(4) expand macros, even embedded in expressions, in results from (3)
(5) allow for macros declared in macro expansions to join the list
of declared macros from that point on
(6) allow for lexical scoping of macros
(7) allow for predeclaration of macros (? is this useful)
(8,...) extend macro language


What's already on CPAN:
http://search.cpan.org/~segv/Macro-0.2/Macro.pod
Essentially what I'm describing, but with a clunky Parse::RecDescent
syntax that may be more complex than doing it yourself; and with
a bugs/todo list at the bottom that restates various points made
above -- I will  start with Macro-02 instead of from scratch if I do this

http://search.cpan.org/~autrijus/Filter-Macro-0.11/lib/Filter/Macro.pm
misleadingly named, Filter::Macro appears to be Pollute::Persistent
plus hints accomplished through source filtering.

TERN (The Extensible Rewriting Notation) mailing list,
http://lists.nongnu.org/mailman/listinfo/TERN-discuss

The Take away David Nicol's internet access committee
mailing list, (...)

--
practical solutions to systemic problems