Subject: Re: bug#62287: Ungexp inside vector problem

From: Josselin Poiret <[email-masked]>
Date: Mon, 20 Mar 2023 11:45:11 +0100

Hi Andrew,

Andrew Tropin <[email-masked]> writes:

> I would expect two last expressions return the same result, but the
> former one doesn't do ungexp:
>
> --8<---------------cut here---------------start------------->8---
> (define a '(3 4))
>
> (define b `#(1 2 ,a))
>
> (eval-with-store
>  #~(list '(1 2 #$a))) ;; => ((1 2 (3 4)))
>
> (eval-with-store
>  #~(list #(1 2 #$a))) ;; => (#(1 2 (ungexp a)))
>
> (eval-with-store
>  #~(list #$b)) ;; => (#(1 2 4))
> --8<---------------cut here---------------end--------------->8---
>
> Am I doing/expecting something wrong or there is a bug here?

It's more related to how the guile reader works, and this is such a
corner case that I don't know whether we should fix.  Basically,
anything starting with # is a reader extension, and the next character
identifies which extension it is.  #( is the reader extension for
vectors, #~ for gexp and #$ for ungexp.

To simplify, whenever you use #~, guile will insert (gexp ...) instead,
which is a hygienic macro (not just a procedure!), that will look for
ungexps inside the expression.  That traversal is only made on cons
cells though, so it doesn't try to go through any piece of syntax that
is not a cons cell!  Since #( doesn't expand to a (vector ...)
cons-cell, the subexpression gets ignored for traversal.

This is in contrast to another reader extension, #' (for syntax), which
does expand to (syntax ...), and is thus further traversed!

You can find how both of these reader extensions operate in
<libguile/read.c>.

I guess the immediate fix is to use (vector ...) rather than #(...).  We
could also add code to the gexp traversal to also traverse vectors, but
I am not even sure if they go through the gexp->sexp dance unharmed, and
we also should in principle do that for everything that can get into a
gexp, not just vectors (eg. bytevectors).

Best,
-- 
Josselin Poiret