From: Karol Skocik
Subject: is this possible with readtable?
Date: 
Message-ID: <1023530c-6601-42ab-babd-4b3998b38a59@x38g2000yqj.googlegroups.com>
Hi,
  is it possible to use a custom readtable (for example one which
reads [1 2 3] as '(1 2 3)) only in scope of macro expansion? I think
not because at the same time the readtable is set, the rest of the
code which should respect the newly set readtable is already read.

Something like:

(let ((*readtable* custom-readtable))
  (unwind-protect
    (expressions with possible [] lists)
   (setf *readtable* original-readtable))))

 But I had to ask whether there is some way, except eval which should
do that I think....

Karol

From: Kaz Kylheku
Subject: Re: is this possible with readtable?
Date: 
Message-ID: <20081124160352.106@gmail.com>
On 2008-11-24, Karol Skocik <············@gmail.com> wrote:
> Hi,
>   is it possible to use a custom readtable (for example one which
> reads [1 2 3] as '(1 2 3)) only in scope of macro expansion?

No.

> I think
> not because at the same time the readtable is set, the rest of the
> code which should respect the newly set readtable is already read.
>
> Something like:

On a different note, your UNWIND-PROTECT and SETF in your example below
are unnecessary.

The LET binding of a special variable is already unwind-safe.

So your (fantasy) code reduces to:

(let ((*readtable* custom-readtable))
  (expressions with possible [] lists))

> (let ((*readtable* custom-readtable))
>   (unwind-protect
>     (expressions with possible [] lists)
>    (setf *readtable* original-readtable))))

This an illogical mixing of levels, like wanting run-time to change something
at compile time.

It's like one of the ``impossible object'' type optical illusions, in which
some structural connection seems to make sense locally, but is actually a
dimensionally inappropriate span between the something in distant background
and the near foreground, which requires us to believe that the two are at the
same depth.

The entire (let ...) expression above is completely read (background) before
any of its evaluation semantics (foreground) come into play, at which time all
issues of reading are long settled.

You want the foreground to feed back into the background, like the falling
water in M.C. Escher's famous waterfall lithograph, which jumps from the
background into the foreground without changing perspective depth!

>  But I had to ask whether there is some way, except eval which should
> do that I think....

Even EVAL won't do it if the expression with [] syntax is actually embedded in
an expression which is to be read using a read table that doesn't have support
for [] syntax.

Lisp EVAL is not like the Bourne Shell, Perl or Ruby eval; it works with
structure, not with source characters.

You could do it using READ-FROM-STRING:

 (let ((*readtable* custom-readtable))
   (eval (read-from-string "[expression with square brackets]")))

READ-FROM-STRING is the tunnel which drills back into the background.

But by doing this, you lose the possibility of lexical reference from the
expressions being read:

 (let ((x 42))
   (eval '(+ x x)))

EVAL won't see the binding of X which assigns it a value of 42, unless
X is a dynamic rather than lexical variable.
From: budden
Subject: Re: is this possible with readtable?
Date: 
Message-ID: <68a33e05-5318-4348-aa2d-9961270b19f4@l42g2000yqe.googlegroups.com>
Hi!
  There are macro-characters for that.
You might do (I give the idea only)

  (let ((*readtable* (my-readtable))) (read stream))

in a macro-character expander. And then use

·@(this form read with your readtable)
From: Pascal J. Bourguignon
Subject: Re: is this possible with readtable?
Date: 
Message-ID: <87prkkodyg.fsf@informatimago.com>
Karol Skocik <············@gmail.com> writes:

> Hi,
>   is it possible to use a custom readtable (for example one which
> reads [1 2 3] as '(1 2 3)) only in scope of macro expansion? I think
> not because at the same time the readtable is set, the rest of the
> code which should respect the newly set readtable is already read.

You're right, it's not possible.  That's because the form is first read
in whole, then passed to EVAL, then macroexpanded, then the result is
passed to EVAL again.

> Something like:
>
> (let ((*readtable* custom-readtable))
>   (unwind-protect
>     (expressions with possible [] lists)
>    (setf *readtable* original-readtable))))
>
>  But I had to ask whether there is some way, except eval which should
> do that I think....

You'd better ask why you would want such a thing!?!


What would you gain in writting:

   (my-macro [1 2 3])  

instead of:

   (my-macro (1 2 3))
? 



-- 
__Pascal Bourguignon__
http://www.informatimago.com
From: Karol Skocik
Subject: Re: is this possible with readtable?
Date: 
Message-ID: <3ffcdab1-9b4c-4649-9157-5e67ddd9ae8d@e12g2000yqm.googlegroups.com>
On Nov 25, 1:18 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Karol Skocik <············@gmail.com> writes:
> > Hi,
> >   is it possible to use a custom readtable (for example one which
> > reads [1 2 3] as '(1 2 3)) only in scope of macro expansion? I think
> > not because at the same time the readtable is set, the rest of the
> > code which should respect the newly set readtable is already read.
>
> You're right, it's not possible.  That's because the form is first read
> in whole, then passed to EVAL, then macroexpanded, then the result is
> passed to EVAL again.
>
> > Something like:
>
> > (let ((*readtable* custom-readtable))
> >   (unwind-protect
> >     (expressions with possible [] lists)
> >    (setf *readtable* original-readtable))))
>
> >  But I had to ask whether there is some way, except eval which should
> > do that I think....
>
> You'd better ask why you would want such a thing!?!
>
> What would you gain in writting:
>
>    (my-macro [1 2 3])  
>
> instead of:
>
>    (my-macro (1 2 3))
> ?
>
> --
> __Pascal Bourguignon__http://www.informatimago.com

I want to get back to my library I was working before (http://common-
lisp.net/project/patg/) and fix some things I am not happy with. One
of the new design goals is to be the pattern-graph and search graph as
close syntactically as possible, and since the only thing they differ
are pattern attributes, I wanted to separate this  distinction to []
list. And since I didn't want to pollute the syntax in the whole file
or package but just in the scope of pattern-graph definition, I wanted
such readtable in scope of the pattern-graph parsing function.
So now, I will probably help myself with symbol-macrolet...

Regards,
  Karol
From: Pascal J. Bourguignon
Subject: Re: is this possible with readtable?
Date: 
Message-ID: <87ej10o75d.fsf@informatimago.com>
Karol Skocik <············@gmail.com> writes:

> On Nov 25, 1:18�am, ····@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Karol Skocik <············@gmail.com> writes:
>> > Hi,
>> > � is it possible to use a custom readtable (for example one which
>> > reads [1 2 3] as '(1 2 3)) only in scope of macro expansion? I think
>> > not because at the same time the readtable is set, the rest of the
>> > code which should respect the newly set readtable is already read.
>>
>> You're right, it's not possible. �That's because the form is first read
>> in whole, then passed to EVAL, then macroexpanded, then the result is
>> passed to EVAL again.
>>
>> > Something like:
>>
>> > (let ((*readtable* custom-readtable))
>> > � (unwind-protect
>> > � � (expressions with possible [] lists)
>> > � �(setf *readtable* original-readtable))))
>>
>> > �But I had to ask whether there is some way, except eval which should
>> > do that I think....
>>
>> You'd better ask why you would want such a thing!?!
>>
>> What would you gain in writting:
>>
>> � �(my-macro [1 2 3]) �
>>
>> instead of:
>>
>> � �(my-macro (1 2 3))
>> ?
>>
>> --
>> __Pascal Bourguignon__http://www.informatimago.com
>
> I want to get back to my library I was working before (http://common-
> lisp.net/project/patg/) and fix some things I am not happy with. One
> of the new design goals is to be the pattern-graph and search graph as
> close syntactically as possible, and since the only thing they differ
> are pattern attributes, I wanted to separate this  distinction to []
> list. And since I didn't want to pollute the syntax in the whole file
> or package but just in the scope of pattern-graph definition, I wanted
> such readtable in scope of the pattern-graph parsing function.
> So now, I will probably help myself with symbol-macrolet...

You can also parse it yourself.

(defun parse-bracketed-list (tokens)
  (labels ((parse-item ()
             (if (eql '\[ (car tokens))
                 (prog2 (pop tokens)
                        (parse-delimited-list '\])
                   (pop tokens))
                 (pop tokens)))
           (parse-delimited-list (delim)
             (loop
                :while (and tokens (not (eql (car tokens) delim)))
                :collect (parse-item))))
    (values (loop
               :while (and tokens (not (eql '\] (car tokens))))
               :collect (parse-item))
            tokens)))
(defmacro m (&rest args)
  `',(parse-bracketed-list args))

(m [ 1 2 3 ] [ 4 [ 5 6 ] ])
--> ((1 2 3) (4 (5 6)))


-- 
__Pascal Bourguignon__
http://www.informatimago.com