unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Unquote syntax
@ 2014-01-01  3:28 Mike Gran
  2014-01-01 12:28 ` Panicz Maciej Godek
  2014-01-01 17:08 ` Ian Price
  0 siblings, 2 replies; 3+ messages in thread
From: Mike Gran @ 2014-01-01  3:28 UTC (permalink / raw)
  To: Guile User



Hi-

With Guile 2.0.9, I'm struggling with unquote syntax.

First, assume that there is already a defined function named 'func'.

I expect the following to be equal, and they are.


  (cons 'sym func)

  (quasiquote (sym . (unquote func)))  `(sym . ,func)

Guile writes them both as 


 (sym . #<procedure func (x)>)

But, the following is also equivalent, which is a surprise to me.

  (quasiquote (sym unquote func))
  `(sym unquote func)

Is this a valid use of unquote?

I get that (cons 'a (list 'b 'c)) == (list 'a 'b 'c), but, I'm not sure
if unquote should be valid in that sort of construction.


Thanks,


-Mike Gran



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Unquote syntax
  2014-01-01  3:28 Unquote syntax Mike Gran
@ 2014-01-01 12:28 ` Panicz Maciej Godek
  2014-01-01 17:08 ` Ian Price
  1 sibling, 0 replies; 3+ messages in thread
From: Panicz Maciej Godek @ 2014-01-01 12:28 UTC (permalink / raw)
  To: Mike Gran; +Cc: Guile User

Hi,

[...]
> I expect the following to be equal, and they are.
>
>   (cons 'sym func)
>
>   (quasiquote (sym . (unquote func)))  `(sym . ,func)
>
> Guile writes them both as
>
>  (sym . #<procedure func (x)>)
>
> But, the following is also equivalent, which is a surprise to me.
>
>   (quasiquote (sym unquote func))
>   `(sym unquote func)
>
> Is this a valid use of unquote?

It has to be. Because a list consists of car+cdr pairs,
the notations  (a . (b . (c . ()))) and (a b c) are equivalent
and thus indistinguishable to the scheme reader:

(equal?
 (with-input-from-string "(a . (b . (c . ())))" read)
 (with-input-from-string "(a b c)" read))
; ===> #t

> I get that (cons 'a (list 'b 'c)) == (list 'a 'b 'c), but, I'm not sure
> if unquote should be valid in that sort of construction.

The only alternative is that you wouldn't be able to write
`(a . ,b), because -- since `x is transformed by reader
to (quasiquote x), and ,x -- to (unquote x), the reader
sees this as:
(quasiquote (a . ,b))
== (quasiquote (a . (unquote b)))
== (quasiquote (a unquote b))

But I think that from the perspective of the implementation
of the "quasiquote" macro this is still consistent.
Let's consider a simplified version of the macro -- call it
semiquote -- that doesn't allow nesting of semiquotes,
supports only lists, and doesn't support unquote-splicing:

(define-syntax semiquote
  (syntax-rules (unquote)
    ((_ (unquote form))
     form)
    ((_ (head . tail))
     (cons (semiquote head) (semiquote tail)))
    ((_ atom)
     'atom)))

As you can see, the second case invokes semiquote
on the tail of the list. So if it happens to be
(unquote form), it will be considered the first case
in the next iteration.

Note however, that if you write (a b unquote c d),
the unquote won't be interpreted, because there's
only a rule for (_ (unquote form)), and not for
(_ (unquote form . rest)).

Having said that, I'd suggest reconsidering the idea
(which might be a little OT), that if a function call
is an improper list, like
(+ a b . c),

then it could be interpreted by the evaluator as
(apply + a b c)

(now it just raises a syntax error).

Best regards,
M.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Unquote syntax
  2014-01-01  3:28 Unquote syntax Mike Gran
  2014-01-01 12:28 ` Panicz Maciej Godek
@ 2014-01-01 17:08 ` Ian Price
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Price @ 2014-01-01 17:08 UTC (permalink / raw)
  To: Mike Gran; +Cc: Guile User

Mike Gran <spk121@yahoo.com> writes:

>   (quasiquote (sym unquote func))
>   `(sym unquote func)
>
> Is this a valid use of unquote?
>
> I get that (cons 'a (list 'b 'c)) == (list 'a 'b 'c), but, I'm not sure
> if unquote should be valid in that sort of construction.

As has already been pointed out, since ,foo is literally the same as
(unquote foo), (a . (unquote foo)) = (a unquote foo) has to be literally
the same as (a . ,foo). This is actually a source of one of my favourite
Scheme implementation bugs (favourite since it is common, subtle, but harmless)

Try a few scheme implementations and see how many error on

`#(1 2 3 unquote foo)

because of implementing vector quasiquotation under the hood as list
quasiquotation.

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-01-01 17:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-01  3:28 Unquote syntax Mike Gran
2014-01-01 12:28 ` Panicz Maciej Godek
2014-01-01 17:08 ` Ian Price

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).