[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elementary question about backquote in T.



I'm sure you'll get a lot of responses to your question, but anyway...

    
    I hope it is okay to send a T question to this list, since I am not aware
    of a separate list for T.
    
There are two:  T-USERS and T-DISCUSSION.  There was once a reason for having
separate lists but in practice everyone now reads both and there doesn't seem
to be any real difference in the type or level of questions.

    If we define:
    
    	(define-macro (bar x) `(list ,(car x)))
    
    then (macro-expand '(bar '(1 2)) *scratch-env*) ==> (list quote). That is, an
    attempt to evaluate (bar '(1 2)) will give an error to the effect that
    quote is unbound.
    
    This appears to me rather confusing. A macro is ideally a rewrite rule.
    So I expect (bar '(1 2)) to be rewritten to the form (list u) where
    u = (car '(1 2)) i.e. (list u) must be rewritten to (list 1).
    This expectation is supported by the following observations:
    
           >  (set x '(1 2))
    	  ;; x is bound to '(1 2)
    
           > `(list ,(car x))
    
    	  ==> (list 1)
    
    What am I missing? By the way, Franzlisp backquote behaves similarly.
    
    Thanks for any comments.
    
    -- Sanjai
    ---------

The single-quote character is a read macro that wraps a QUOTE around its
argument.  In T terms, that means the following:
  
    > (stream-read-table (standard-input))
    #{Read-table 53 *STANDARD-READ-TABLE*}
    > (read-table-entry (stream-read-table (standard-input)) #\')
    #{Procedure 54 READ-QUOTATION}

and, from TSYS/READ.T:

    (DEFINE (READ-QUOTATION STREAM CH RT)
       (IGNORE CH)
       (LIST *QUOTE* (READ-OBJECT-REFUSING-EOF STREAM RT)))

Thus '(1 2) gets read in as (QUOTE (1 2)), and (bar '(1 2)) expands to
(LIST u) where u is the CAR of (QUOTE (1 2)), i.e., the symbol QUOTE.
In other words, (bar '(1 2)) expands to (list QUOTE) as you noted.

Macros are rewrite rules, but on list structures (like "(QUOTE (1 2))"), not
on ASCII text (like "'(1 2)").  In your transcript, x gets bound, not to
"'(1 2)" as you say, but to a list structure represented as (1 2).  The CAR
of this structure is the atom 1, and thus you get (list 1), i.e., the list
(1) as your result.

Hope this helps... Ashwin.


-------