[plt-scheme] Mutable and immutable pairs in PLT Scheme

From: Thomas Chust (chust at web.de)
Date: Tue Nov 25 18:09:19 EST 2008


the discussion about R6RS support in PLT Scheme brought the topic of
immutable pairs in PLT's default Scheme dialect to my attention again.

Basically, I think having immutable pairs as the default is a good
thing, but I start wondering why mutable and immutable pairs are
completely disjunct types. Other datatypes, like strings, vectors and
hash tables, that also exist in mutable and immutable variants in PLT
Scheme, are not disjunct, but the pairs, maybe the most prominent
datatype(s) of Scheme, behave differently. Is there any clever rationale
behind this?

I would actually have expected that the following conditions all hold

  (pair? (mcons 1 2))
  (pair? (cons 1 2))

  (not (immutable? (mcons 1 2)))
  (immutable? (cons 1 2))

However, mutable pairs are not pairs, as far as pair?, car and cdr are
concerned and immutable pairs are not immutable, as far as immutable? is
concerned. This seems totally counterintuitive to me!

So unless there is a deeper reason for the disjunctness of the mutable
and immutable pair types, I suggest that the type of mutable pairs
should be made a subtype of the type of pairs, and consequently car and
cdr could be used to access mutable pairs just like immutable ones but
set-car! and set-cdr! could only be used on mutable ones. mpair? or
immutable? could be used to distinguish the types.

As far as I can see, my proposal would not introduce problems in most
existing code, the exception being code that explicitly relies on
immutable pairs being declared mutable by immutable? or code that uses
exception handling to catch expected parameter type errors -- both
things that I would consider bad programming practice. The proposal
would however greatly improve compatibility between libraries written in
R6RS and those written in PLT Scheme, since one could always work with
pairs returned from R6RS on the PLT Scheme side without problems and, in
many cases one could also pass PLT Scheme's immutable pairs to R6RS code
without causing trouble. And last but not least, I think this behaviour
would just feel more natural.

What do you think about it?


Posted on the users mailing list.