Re: Evaluating functor application only once

Matthew Fluet <[email protected]> Sat, 21 Dec 2019 16:02:31 -0500
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAMrhFL4wK5TNvF=DJRv6ZXdNGuU96+cdh8ndfKq+Jp-dTRNeUQ@mail.gmail.com>
--===============6717897284288745124==
Content-Type: multipart/alternative; boundary="00000000000094312f059a3d1d12"

--00000000000094312f059a3d1d12
Content-Type: text/plain; charset="UTF-8"

On Sat, Dec 21, 2019 at 1:15 PM Phil Clayton via MLton-user <
[email protected]> wrote:

> On 04/04/19 11:15, Matthew Fluet wrote:
> > On Thu, Apr 4, 2019 at 3:18 AM Phil Clayton <[email protected]
> > <mailto:[email protected]>> wrote:
> >
> >     In the absence of applicative functors, I am trying to ensure that
> >     certain functors are applied only once.  (Without going into too much
> >     detail, the issue is that a library that needs e.g.
> >        structure GUInt8Array = CArray(GUInt8)
> >     can't know whether another library has already introduced the same
> >     structure.)
> >
> >     My approach is to have the functor application in its own SML file:
> >
> >        <Name>.sml
> >          structure <Name> = F(X)
> >
> >     With Poly/ML, which doesn't support MLB files, I can have a
> >     variant of
> >     "use" that only processes a file <Name>.sml if there is no existing
> >     structure <Name>.
> >
> >     With MLB files, I plan to also introduce <Name>.mlb (which
> >     contains only
> >     <Name>.sml) and refer to that, so that it won't have any effect if
> >     another library has already evaluated <Name>.mlb. What I am doing is
> >     inherently complier-specific, so I wondered - is there is any other
> >     MLton-specific way of achieving the same thing, e.g. MLB extension?
> >
> >
> > No.  Your plan to use <Name>.mlb would be my suggestion; the semantics
> > of MLB files to evaluate a <file>.mlb exactly once is precisely to
> > support this style of "one definition".
> This is not working out as simply as I had hoped: <Name>.mlb can't
> contain just <Name>.sml, it must also contain the dependencies of
> <Name>.sml.  To avoid evaluating dependencies more than once, it is my
> understanding that they need their own MLB files and we end up in a
> situation where every SML file needs its own MLB file, which is pretty
> ugly.  Or have I misunderstood the semantics of MLB files?
>
>
No, you aren't necessarily misunderstanding the semantics of MLB files.
Every MLB file must be closed (self-contained).  So, if you trying to model
very fine grained dependencies, then yes, you may end up with a MLB file
per SML file.  The expectation is that one would have an MLB file per
"library" (a group of related modules).


> It's unfortunate that MLB files don't support conditional compilation
> like CM files.  Achieving 'one definition' would have been simple if we
> could write something like
>
>    if not (defined (structure S))
>    then
>      <basdec>
>    end
>

I don't understand how this would help.  CM files are also required to be
closed (self-contained).  And, even if MLB files did support this kind of
conditional compilation, unless each module that might need to define the
structure S also exported the structure S, then later modules would not
necessarily "see" that the structure S is defined.

I do agree that SML (and CM and MLB) files deviate somewhat from other
languages in their treatment of "import"-like declarations.  I do find that
students sometimes find SML source code confusing, because when they
encounter a structure/identifier that isn't defined in the file, they
cannot just jump to the beginning of the file and look at the various
"import" declarations to determine where the structure/identifier might be
coming from.  Instead, they need to jump to the CM/MLB file that contains
the source SML file and then piece together the imports from the CM/MLB
file.

I have thought that perhaps we should have an extended MLB format that
allows the inclusion of SML code after an import prelude and before an
export postlude; then at least the SML and MLB code would be in the same
file.  One of my past students tried out a simple surface-level
pre-processor to give this kind of behavior; see
https://github.com/myegorov/transmler.

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

<div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"fon=
t-family:courier new,monospace;font-size:large"><span style=3D"font-family:=
Arial,Helvetica,sans-serif;font-size:small">On Sat, Dec 21, 2019 at 1:15 PM=
 Phil Clayton via MLton-user &lt;<a href=3D"mailto:[email protected]=
orge.net">[email protected]</a>&gt; wrote:</span><br></div><=
/div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"=
margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-lef=
t:1ex">On 04/04/19 11:15, Matthew Fluet wrote:<br>
&gt; On Thu, Apr 4, 2019 at 3:18 AM Phil Clayton &lt;<a href=3D"mailto:phil=
[email protected]" target=3D"_blank">[email protected]</a> <br>
&gt; &lt;mailto:<a href=3D"mailto:[email protected]" target=3D"_blank=
">[email protected]</a>&gt;&gt; wrote:<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0In the absence of applicative functors, I am trying=
 to ensure that<br>
&gt;=C2=A0 =C2=A0 =C2=A0certain functors are applied only once.=C2=A0 (With=
out going into too much<br>
&gt;=C2=A0 =C2=A0 =C2=A0detail, the issue is that a library that needs e.g.=
<br>
&gt;=C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0structure GUInt8Array =3D CArray(GUInt=
8)<br>
&gt;=C2=A0 =C2=A0 =C2=A0can&#39;t know whether another library has already =
introduced the same<br>
&gt;=C2=A0 =C2=A0 =C2=A0structure.)<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0My approach is to have the functor application in i=
ts own SML file:<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0&lt;Name&gt;.sml<br>
&gt;=C2=A0 =C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0structure &lt;Name&gt; =3D F(X)=
<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0With Poly/ML, which doesn&#39;t support MLB files, =
I can have a<br>
&gt;=C2=A0 =C2=A0 =C2=A0variant of<br>
&gt;=C2=A0 =C2=A0 =C2=A0&quot;use&quot; that only processes a file &lt;Name=
&gt;.sml if there is no existing<br>
&gt;=C2=A0 =C2=A0 =C2=A0structure &lt;Name&gt;.<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0With MLB files, I plan to also introduce &lt;Name&g=
t;.mlb (which<br>
&gt;=C2=A0 =C2=A0 =C2=A0contains only<br>
&gt;=C2=A0 =C2=A0 =C2=A0&lt;Name&gt;.sml) and refer to that, so that it won=
&#39;t have any effect if<br>
&gt;=C2=A0 =C2=A0 =C2=A0another library has already evaluated &lt;Name&gt;.=
mlb. What I am doing is<br>
&gt;=C2=A0 =C2=A0 =C2=A0inherently complier-specific, so I wondered - is th=
ere is any other<br>
&gt;=C2=A0 =C2=A0 =C2=A0MLton-specific way of achieving the same thing, e.g=
. MLB extension?<br>
&gt;<br>
&gt;<br>
&gt; No.=C2=A0 Your plan to use &lt;Name&gt;.mlb would be my suggestion; th=
e semantics <br>
&gt; of MLB files to evaluate a &lt;file&gt;.mlb exactly once is precisely =
to <br>
&gt; support this style of &quot;one definition&quot;.<br>
This is not working out as simply as I had hoped: &lt;Name&gt;.mlb can&#39;=
t <br>
contain just &lt;Name&gt;.sml, it must also contain the dependencies of <br=
>
&lt;Name&gt;.sml.=C2=A0 To avoid evaluating dependencies more than once, it=
 is my <br>
understanding that they need their own MLB files and we end up in a <br>
situation where every SML file needs its own MLB file, which is pretty <br>
ugly.=C2=A0 Or have I misunderstood the semantics of MLB files?<br>
<br></blockquote><div><br></div><div><div class=3D"gmail_default" style=3D"=
font-family:&quot;courier new&quot;,monospace;font-size:large">No, you aren=
&#39;t necessarily misunderstanding the semantics of MLB files.=C2=A0 Every=
 MLB file must be closed (self-contained).=C2=A0 So, if you trying to model=
 very fine grained dependencies, then yes, you may end up with a MLB file p=
er SML file.=C2=A0 The expectation is that one would have an MLB file per &=
quot;library&quot; (a group of related modules).</div></div><div>=C2=A0</di=
v><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;borde=
r-left:1px solid rgb(204,204,204);padding-left:1ex">It&#39;s unfortunate th=
at MLB files don&#39;t support conditional compilation <br>
like CM files.=C2=A0 Achieving &#39;one definition&#39; would have been sim=
ple if we <br>
could write something like<br>
<br>
=C2=A0=C2=A0 if not (defined (structure S))<br>
=C2=A0=C2=A0 then<br>
=C2=A0=C2=A0=C2=A0=C2=A0 &lt;basdec&gt;<br>
=C2=A0=C2=A0 end<br></blockquote><div><br></div><div class=3D"gmail_default=
" style=3D"font-family:&quot;courier new&quot;,monospace;font-size:large">I=
 don&#39;t understand how this would help.=C2=A0 CM files are also required=
 to be closed (self-contained).=C2=A0 And, even if MLB files did support th=
is kind of conditional compilation, unless each module that might need to d=
efine the structure S also exported the structure S, then later modules wou=
ld not necessarily &quot;see&quot; that the structure S is defined.</div><d=
iv class=3D"gmail_default" style=3D"font-family:&quot;courier new&quot;,mon=
ospace;font-size:large"><br></div><div class=3D"gmail_default" style=3D"fon=
t-family:&quot;courier new&quot;,monospace;font-size:large">I do agree that=
 SML (and CM and MLB) files deviate somewhat from other languages in their =
treatment of &quot;import&quot;-like declarations.=C2=A0 I do find that stu=
dents sometimes find SML source code confusing, because when they encounter=
 a structure/identifier that isn&#39;t defined in the file, they cannot jus=
t jump to the beginning of the file and look at the various &quot;import&qu=
ot; declarations to determine where the structure/identifier might be comin=
g from.=C2=A0 Instead, they need to jump to the CM/MLB file that contains t=
he source SML file and then piece together the imports from the CM/MLB file=
.</div><div class=3D"gmail_default" style=3D"font-family:&quot;courier new&=
quot;,monospace;font-size:large"><br></div><div class=3D"gmail_default" sty=
le=3D"font-family:&quot;courier new&quot;,monospace;font-size:large">I have=
 thought that perhaps we should have an extended MLB format that allows the=
 inclusion of SML code after an import prelude and before an export postlud=
e; then at least the SML and MLB code would be in the same file.=C2=A0 One =
of my past students tried out a simple surface-level pre-processor to give =
this kind of behavior; see <a href=3D"https://github.com/myegorov/transmler=
">https://github.com/myegorov/transmler</a>.</div><div class=3D"gmail_defau=
lt" style=3D"font-family:&quot;courier new&quot;,monospace;font-size:large"=
><br></div></div></div>

--00000000000094312f059a3d1d12--


--===============6717897284288745124==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============6717897284288745124==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user

--===============6717897284288745124==--