Re: Are there real use cases with the Java access modes?

Alex Otenko via Concurrency-interest <[email protected]> Mon, 26 Jul 2021 08:06:45 +0100
Newsgroups gmane.comp.java.jsr.166-concurrency
Message-ID <CANkgWKg6WmjG6_M=X1FC79u8qJSiULQ_X=9GV=uTeDhQKvfXmw@mail.gmail.com>
--===============1911684259682918387==
Content-Type: multipart/alternative; boundary="0000000000001ae8ef05c8016622"

--0000000000001ae8ef05c8016622
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

"Don't write the code like that" is what the others said about
while(!done), so maybe you can see the point they are making.

As for this example - please take it as an example of code that may not
behave as written.

As to why it is written like that - well, it is derived from a more
elaborate mutually recursive case (with a bug).

It is perfectly normal to state "zero is even", "x+1 is even, if x is odd",
and "x+1 is odd, if x is even". This is a recursive definition that is
derived from a recursive definition of natural numbers. It is not complete,
but you can't tell if you don't have the compiler that will tell you that.

So you have:

char odd(int x){return even(x-1);}
char even(int x){return !x || odd(x-1);}

Inline even into odd, do tail call optimization, and you end up with a loop
like that (ok, x--, not x++). Both of these steps is what modern clang, gcc
and llvm do.

Alex

On Mon, 26 Jul 2021, 05:46 Gregg Wonderly, <[email protected]> wrote:

>
>
> > On Jul 24, 2021, at 12:18 AM, Alex Otenko <[email protected]>
> wrote:
> >
> > Yes. It depends on what you think an empty loop is.
> >
> > char odd(int x){
> >   for(;;){
> >     x++;
> >     if(!x) return 1;
> >     x++;
> >   }
> > }
> >
> > Oops, this can tell you that 2 is an odd number. But you can prove x
> never becomes 0 for even inputs. How's that for the least astonishment?
>
> I don=E2=80=99t see how this function tests anything about odd.  It shoul=
d use
> (x&1) should it not?   I don=E2=80=99t understand what this function is s=
upposed to
> do.  Why is there a loop?
>
> char odd(int x) { return !(x&1); }
>
> Gregg Wonderly
>
>
>

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

<div dir=3D"auto">&quot;Don&#39;t write the code like that&quot; is what th=
e others said about while(!done), so maybe you can see the point they are m=
aking.<div dir=3D"auto"><br></div><div dir=3D"auto">As for this example - p=
lease take it as an example of code that may not behave as written.</div><d=
iv dir=3D"auto"><br></div><div dir=3D"auto">As to why it is written like th=
at - well, it is derived from a more elaborate mutually recursive case (wit=
h a bug).</div><div dir=3D"auto"><br></div><div dir=3D"auto">It is perfectl=
y normal to state &quot;zero is even&quot;, &quot;x+1 is even, if x is odd&=
quot;, and &quot;x+1 is odd, if x is even&quot;. This is a recursive defini=
tion that is derived from a recursive definition of natural numbers. It is =
not complete, but you can&#39;t tell if you don&#39;t have the compiler tha=
t will tell you that.</div><div dir=3D"auto"><br></div><div dir=3D"auto">So=
 you have:</div><div dir=3D"auto"><br></div><div dir=3D"auto">char odd(int =
x){return even(x-1);}</div><div dir=3D"auto">char even(int x){return !x || =
odd(x-1);}</div><div dir=3D"auto"><br></div><div dir=3D"auto">Inline even i=
nto odd, do tail call optimization, and you end up with a loop like that (o=
k, x--, not x++). Both of these steps is what modern clang, gcc and llvm do=
.</div><div dir=3D"auto"><br></div><div dir=3D"auto">Alex</div></div><br><d=
iv class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, 26 J=
ul 2021, 05:46 Gregg Wonderly, &lt;<a href=3D"mailto:[email protected]">gergg@c=
ox.net</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
&gt; On Jul 24, 2021, at 12:18 AM, Alex Otenko &lt;<a href=3D"mailto:oleksa=
[email protected]" target=3D"_blank" rel=3D"noreferrer">oleksandr.otenko=
@gmail.com</a>&gt; wrote:<br>
&gt; <br>
&gt; Yes. It depends on what you think an empty loop is.<br>
&gt; <br>
&gt; char odd(int x){<br>
&gt;=C2=A0 =C2=A0for(;;){<br>
&gt;=C2=A0 =C2=A0 =C2=A0x++;<br>
&gt;=C2=A0 =C2=A0 =C2=A0if(!x) return 1;<br>
&gt;=C2=A0 =C2=A0 =C2=A0x++;<br>
&gt;=C2=A0 =C2=A0}<br>
&gt; }<br>
&gt; <br>
&gt; Oops, this can tell you that 2 is an odd number. But you can prove x n=
ever becomes 0 for even inputs. How&#39;s that for the least astonishment? =
<br>
<br>
I don=E2=80=99t see how this function tests anything about odd.=C2=A0 It sh=
ould use (x&amp;1) should it not?=C2=A0 =C2=A0I don=E2=80=99t understand wh=
at this function is supposed to do.=C2=A0 Why is there a loop?<br>
<br>
char odd(int x) { return !(x&amp;1); }<br>
<br>
Gregg Wonderly<br>
<br>
<br>
</blockquote></div>

--0000000000001ae8ef05c8016622--

--===============1911684259682918387==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KQ29uY3VycmVu
Y3ktaW50ZXJlc3QgbWFpbGluZyBsaXN0CkNvbmN1cnJlbmN5LWludGVyZXN0QGNzLm9zd2Vnby5l
ZHUKaHR0cDovL2NzLm9zd2Vnby5lZHUvbWFpbG1hbi9saXN0aW5mby9jb25jdXJyZW5jeS1pbnRl
cmVzdAo=

--===============1911684259682918387==--