Re: Why can't defstructs be redefined?

Alan Ruttenberg <[email protected]> Wed, 13 Jul 2022 23:42:14 -0400
Newsgroups gmane.editors.j.devel
Message-ID <CAFKQJ8msxhRensp9_XZZcRLWsDTiCrFoQC1aVFxb+Ob8Z+qz6w@mail.gmail.com>
--00000000000097509e05e3bbb145
Content-Type: text/plain; charset="UTF-8"

On Wed, Jul 13, 2022 at 10:53 PM Ville Voutilainen <
[email protected]> wrote:

> On Thu, 14 Jul 2022 at 05:33, Alan Ruttenberg <[email protected]>
> wrote:
> >
> > Comments inline
> > Seems to work, as long as you don't allow the source transform.
> Currently I'm experimenting with a new option to defstruct. So
> >
> > I compile a file with
> > (defstruct (test2 (:optimize nil)) a b)
>
> I'm thinking I'm stupid, since I'm not sure what you mean by "source
> transform",


ABCL has a mechanism for source transformations, which effectively inline
some code instead of calling a function. You can get a source
transformation with
sys:function-info. For example:

(sys::function-info my-accessor)
((:source-transform . #<anonymous-function
abcl_b09e0b54_a7c4_4513_86e6_fd9effb1fb25 {27C03870}>))

These are applied in the compiler and aren't affected, currently, by the
(declare (optimize ..)) settings. They are used for defstruct in
define-reader and define-writer.

The implementation I discuss is here:
https://github.com/alanruttenberg/abcl/commit/a9c5541d372012d24c0daa704a22fc637398e086

but I can with fair confidence say that if
> you can ensure that all defstructs use the option you add as a
> custom-thingy, then the rest of the concerns I spoke of
> are moot. :P You're basically saying "this is not a regular defstruct,
> it's a special one, don't do the usual stuff, and then
> I can make it mean whatever I want it to mean, side-stepping the
> problems". If that works for your use cases, sure,
> seems plausible in general. Or have I misunderstood your approach?
>

Sort of. There's no usual stuff around redefining defstructs, so it's more
of an added feature. It is implementation dependent, but then so is JSS
which I understand many people find useful.  I'm hoping it would be useful
for anyone who needs to develop defstructs during initial code writing and
testing. It doesn't change any behavior if you don't use the option, so
it's safe. It's not a perfect solution, but the perfect is the enemy of the
good. I will keep thinking about whether there's a way to remove the
potential gotchas.

FWIW, my use case is developing some code using lisp-binary
<https://github.com/j3pic/lisp-binary>(super useful for parsing binary
files). It generates defstructs with some extra code around them.
But since I don't know it well enough I'm still making mistakes and needing
to redefine things. Before this change the only option was to restart lisp
if I made a mistake, which gets old quickly.  If I had my druthers,
lisp-binary would have generated classes instead of defstructs, since there
are no problems with redefinitions there. I may try to modify lisp-binary
to do that at some point...

There's a more conservative approach which fits better with "regular"
defstruct usage, which is to allow adding slots to defstruct, but not
reordering or deleting. Even that can't be done now. I started to implement
that, which wouldn't need as much care to use it. But it turned out that
just wasn't enough for the development I was doing now. Not having even
that capability has burned me in the past.

YMMV.

Alan

--00000000000097509e05e3bbb145
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">On Wed, Jul 13, 2022 at 10:53 PM Vill=
e Voutilainen &lt;<a href=3D"mailto:[email protected]">ville.vout=
[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote"=
 style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);p=
adding-left:1ex">On Thu, 14 Jul 2022 at 05:33, Alan Ruttenberg &lt;<a href=
=3D"mailto:[email protected]" target=3D"_blank">alanruttenberg@gmail=
.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Comments inline<br>
&gt; Seems to work, as long as you don&#39;t allow the source transform. Cu=
rrently I&#39;m experimenting with a new option to defstruct. So<br>
&gt;<br>
&gt; I compile a file with<br>
&gt; (defstruct (test2 (:optimize nil)) a b)<br>
<br>
I&#39;m thinking I&#39;m stupid, since I&#39;m not sure what you mean by &q=
uot;source<br>
transform&quot;,</blockquote><div><br></div><div>ABCL has a mechanism for s=
ource transformations, which effectively inline some code instead of callin=
g a function. You can get a source transformation with</div><div>sys:functi=
on-info. For example:<br></div><div><br></div><div><span style=3D"font-fami=
ly:monospace">(sys::function-info my-accessor)<br>((:source-transform . #&l=
t;anonymous-function abcl_b09e0b54_a7c4_4513_86e6_fd9effb1fb25 {27C03870}&g=
t;))</span></div><div>=C2=A0</div><div>These are applied in the compiler an=
d aren&#39;t affected, currently, by the (declare (optimize ..)) settings. =
They are used for defstruct in define-reader and define-writer. <br></div><=
div><br></div><div>The implementation I discuss is here:</div><div><a href=
=3D"https://github.com/alanruttenberg/abcl/commit/a9c5541d372012d24c0daa704=
a22fc637398e086">https://github.com/alanruttenberg/abcl/commit/a9c5541d3720=
12d24c0daa704a22fc637398e086</a></div><div><br></div><blockquote class=3D"g=
mail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204=
,204,204);padding-left:1ex"> but I can with fair confidence say that if<br>
you can ensure that all defstructs use the option you add as a<br>
custom-thingy, then the rest of the concerns I spoke of<br>
are moot. :P You&#39;re basically saying &quot;this is not a regular defstr=
uct,<br>
it&#39;s a special one, don&#39;t do the usual stuff, and then<br>
I can make it mean whatever I want it to mean, side-stepping the<br>
problems&quot;. If that works for your use cases, sure,<br>
seems plausible in general. Or have I misunderstood your approach?<br></blo=
ckquote><div><br></div><div>Sort of. There&#39;s no usual stuff around rede=
fining defstructs, so it&#39;s more of an added feature. It is implementati=
on dependent, but then so is JSS which I understand many people find useful=
.=C2=A0 I&#39;m hoping it would be useful for anyone who needs to develop d=
efstructs during initial code writing and testing. It doesn&#39;t change an=
y behavior if you don&#39;t use the option, so it&#39;s safe. It&#39;s not =
a perfect solution, but the perfect is the enemy of the good. I will keep t=
hinking about whether there&#39;s a way to remove the potential gotchas.<br=
></div><div><br></div><div>FWIW, my use case is developing some code using =
<a href=3D"https://github.com/j3pic/lisp-binary">lisp-binary </a>(super use=
ful for parsing binary files). It generates defstructs with some extra code=
 around them. <br></div><div>But since I don&#39;t know it well enough I&#3=
9;m still making mistakes and needing to redefine things. Before this chang=
e the only option was to restart lisp if I made a mistake, which gets old q=
uickly.=C2=A0 If I had my druthers, lisp-binary would have generated classe=
s instead of defstructs, since there are no problems with redefinitions the=
re. I may try to modify lisp-binary to do that at some point... <br></div><=
div><br></div><div>There&#39;s a more conservative approach which fits bett=
er with &quot;regular&quot; defstruct usage, which is to allow adding slots=
 to defstruct, but not reordering or deleting. Even that can&#39;t be done =
now. I started to implement that, which wouldn&#39;t need as much care to u=
se it. But it turned out that just wasn&#39;t enough for the development I =
was doing now. Not having even that capability has burned me in the past.<b=
r></div><div><br></div><div>YMMV.<br></div><div><br></div><div>Alan</div><d=
iv><br></div></div></div>

--00000000000097509e05e3bbb145--