unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#32841: assoc-set fails with dot notation association list
@ 2018-09-25 20:33 Hood, Christopher L.
  2018-09-25 22:04 ` John Cowan
  2018-09-27  3:02 ` Mark H Weaver
  0 siblings, 2 replies; 4+ messages in thread
From: Hood, Christopher L. @ 2018-09-25 20:33 UTC (permalink / raw)
  To: 32841

[-- Attachment #1: Type: text/plain, Size: 978 bytes --]

This fails with code pulled straight out of the Guile manual example (section 6.6.20.6).

(define capitals '(("New York" . "Albany")
                   ("Oregon"   . "Salem")
                   ("Florida"  . "Miami")))

Okay, that works define to define alist capitals. Now let's try another part of the example:


(set! capitals

      (assoc-set! capitals "Florida" "Tallahassee"))

This yields the error:

ERROR: In procedure assoc-set!:
In procedure set-cdr!: Wrong type argument in position 1 (expecting mutable pair): ("Florida" . "Miami")

I've experienced this behavior with builds of both Guile 2.2.2 and Guile 2.2.4.

I'll note that if you define the alist so its initial contents are defined using a quasiquote and the cons form instead of dot notation, this error is not reached. I'm not sure if the error is valid or not, but in any case, the code that produces is listed as an valid example in the manual, so that doesn't seem right.

chris

[-- Attachment #2: Type: text/html, Size: 4266 bytes --]

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

* bug#32841: assoc-set fails with dot notation association list
  2018-09-25 20:33 bug#32841: assoc-set fails with dot notation association list Hood, Christopher L.
@ 2018-09-25 22:04 ` John Cowan
  2018-09-27  3:02 ` Mark H Weaver
  1 sibling, 0 replies; 4+ messages in thread
From: John Cowan @ 2018-09-25 22:04 UTC (permalink / raw)
  To: Christopher.Hood; +Cc: 32841

[-- Attachment #1: Type: text/plain, Size: 1367 bytes --]

The error is valid, because you are attempting to mutate a program
literal.  Scheme makes that an error, and Guile enforces it (though many
Schemes do not).

On Tue, Sep 25, 2018 at 4:41 PM Hood, Christopher L. <
Christopher.Hood@gtri.gatech.edu> wrote:

> This fails with code pulled straight out of the Guile manual example
> (section 6.6.20.6).
>
>
>
> (define capitals '(("New York" . "Albany")
>
>                    ("Oregon"   . "Salem")
>
>                    ("Florida"  . "Miami")))
>
>
>
> Okay, that works define to define alist capitals. Now let’s try another
> part of the example:
>
>
>
> (set! capitals
>
>       (assoc-set! capitals "Florida" "Tallahassee"))
>
>
>
> This yields the error:
>
>
>
> ERROR: In procedure assoc-set!:
>
> In procedure set-cdr!: Wrong type argument in position 1 (expecting
> mutable pair): ("Florida" . "Miami")
>
>
>
> I’ve experienced this behavior with builds of both Guile 2.2.2 and Guile
> 2.2.4.
>
>
>
> I’ll note that if you define the alist so its initial contents are defined
> using a quasiquote and the cons form instead of dot notation, this error is
> not reached. I’m not sure if the error is valid or not, but in any case,
> the code that produces is listed as an valid example in the manual, so that
> doesn’t seem right.
>
>
>
> chris
>

[-- Attachment #2: Type: text/html, Size: 2803 bytes --]

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

* bug#32841: assoc-set fails with dot notation association list
  2018-09-25 20:33 bug#32841: assoc-set fails with dot notation association list Hood, Christopher L.
  2018-09-25 22:04 ` John Cowan
@ 2018-09-27  3:02 ` Mark H Weaver
  2018-09-27 12:09   ` John Cowan
  1 sibling, 1 reply; 4+ messages in thread
From: Mark H Weaver @ 2018-09-27  3:02 UTC (permalink / raw)
  To: Hood, Christopher L.; +Cc: 32841

Hi Christopher,

"Hood, Christopher L." <Christopher.Hood@gtri.gatech.edu> writes:
> This fails with code pulled straight out of the Guile manual example
> (section 6.6.20.6).

Indeed, the example code in the manual is bad.  Thanks for bringing this
to our attention.

> (define capitals '(("New York" . "Albany")
>                    ("Oregon"   . "Salem")
>                    ("Florida"  . "Miami")))

As John correctly pointed out, it's an error to mutate a program
literal.  It's analogous to trying to modify a string literal in C.
'assoc-set!' mutates the existing list structure in some cases, and so
it cannot be used on literals such as the one above.

To initialize an alist that will be mutated, you must instead do
something like this:

  (define capitals (list (cons "New York" "Albany")
                         (cons "Oregon"   "Salem")
                         (cons "Florida"  "Miami")))

> I’ll note that if you define the alist so its initial contents are
> defined using a quasiquote and the cons form instead of dot notation,
> this error is not reached.

Yes, that accomplishes essentially the same thing, although note that
quasiquote makes an effort to use program literals for parts of the
resulting list structure, e.g.:

  `(,a ,b c d)

expands to:

  (cons a (cons b '(c d)))

which means that the first two cons cells can be mutated, but the last
two are part of an immutable program literal.

> I’m not sure if the error is valid or not, but in any case, the code
> that produces is listed as an valid example in the manual, so that
> doesn’t seem right.

Indeed, the manual needs to be fixed.  Sorry for the confusion, and
thanks again for the bug report.

     Mark





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

* bug#32841: assoc-set fails with dot notation association list
  2018-09-27  3:02 ` Mark H Weaver
@ 2018-09-27 12:09   ` John Cowan
  0 siblings, 0 replies; 4+ messages in thread
From: John Cowan @ 2018-09-27 12:09 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 32841, Christopher.Hood

[-- Attachment #1: Type: text/plain, Size: 627 bytes --]

On Wed, Sep 26, 2018 at 11:03 PM Mark H Weaver <mhw@netris.org> wrote:


> To initialize an alist that will be mutated, you must instead do
> something like this:
>
>   (define capitals (list (cons "New York" "Albany")
>                          (cons "Oregon"   "Salem")
>                          (cons "Florida"  "Miami")))
>

More simply, you can wrap the literal in a call to copy-tree.  This returns
a deep copy of the literal that is fully mutable.

-- 
John Cowan          http://vrici.lojban.org/~cowan        cowan@ccil.org
Here lies the Christian, judge, and poet Peter,
Who broke the laws of God and man and metre.

[-- Attachment #2: Type: text/html, Size: 1234 bytes --]

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

end of thread, other threads:[~2018-09-27 12:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-25 20:33 bug#32841: assoc-set fails with dot notation association list Hood, Christopher L.
2018-09-25 22:04 ` John Cowan
2018-09-27  3:02 ` Mark H Weaver
2018-09-27 12:09   ` John Cowan

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).