~ Swansong [was: TypeCastery]

Anthony Clayden <[email protected]> Tue, 9 Oct 2018 23:57:36 +1300
Newsgroups gmane.comp.lang.haskell.hugs.user
Message-ID <CAM7nRYTPXwXyTowygzrMi8rejMh2UxKE6e+p=OGR_DjxWcveAA@mail.gmail.com>
--===============6629337142488773205==
Content-Type: multipart/alternative; boundary="000000000000baeef00577c99b3d"

--000000000000baeef00577c99b3d
Content-Type: text/plain; charset="UTF-8"

(More from the annals of oh-so-easy in yacc.)

On Thu, 4 Oct 2018 at 12:45 AM, Anthony Clayden <
[email protected]> wrote:

> A typical consequence of combining FunDeps + Overlapping instances is that
> you have to make the result parameter more general than it needs be, then
> use a TypeCast constraint to improve it.
>
> ... TypeCast, which does use FunDeps to mutually improve/unify its two
> parameters. Nowadays in GHC you'd use the (~) constraint.
>

I got so fed up writing out TypeCast constraints; and I'm so used to
writing infix (~) in GHC, I implemented (~) in Hugs. This is hard-coded
syntax in constraints; I've not implemented type operators. (Mark you, it's
also hard-coded in GHC, because (~) is a reserved symbol.)

This needed only changes to the yacc syntax. From that I insisted that "~"
is a legitimate name for a class. There is a downside that equality
constraints are printed with the '~' prefix -- because all class names are
prefix.

To be clear: this is not as powerful or well-integrated as (~) in GHC. You
still need at the term level to explicitly cast.

Then I got so fed up writing out explicit typeCast calls ..., I picked up
the postfix operators idea
https://mail.haskell.org/pipermail/hugs-users/2018-September/000909.html
and invented a postfix operator (~::) to do the job.
Trailing double-colon says I'm doing something typeful; tilde connects it
to the equality constraint.

Then here's the classic, compiled in Hugs, also exhibiting the FunDeps +
Overlaps combo

class TypeEq t t' r  | t t' -> r  where
  typeEq :: t -> t' -> r

instance TypeEq t t TTrue  where
  typeEq _ _ = TTrue

instance (TFalse ~ f) => TypeEq t t' f  where
  typeEq _ _ = (TFalse ~::)      -- without explicit cast, complains type
is not general enough


AntC

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

<div dir=3D"auto">(More from the annals of oh-so-easy in yacc.)</div><div d=
ir=3D"auto"><br></div><div>On Thu, 4 Oct 2018 at 12:45 AM, Anthony Clayden =
&lt;<a href=3D"mailto:[email protected]">[email protected]=
et.nz</a>&gt; wrote:<br></div><div><div class=3D"gmail_quote"><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;=
padding-left:1ex"><div dir=3D"auto">A typical consequence of combining FunD=
eps + Overlapping instances is that you have to make the result parameter m=
ore general than it needs be, then use a TypeCast constraint to improve it.=
</div><div dir=3D"auto"><br></div><div dir=3D"auto">... TypeCast, which doe=
s use FunDeps to mutually improve/unify its two parameters. Nowadays in GHC=
 you&#39;d use the (~) constraint.</div></blockquote><div dir=3D"auto"><br>=
</div><div dir=3D"auto">I got so fed up writing out TypeCast constraints; a=
nd I&#39;m so used to writing infix (~) in GHC, I implemented (~) in Hugs. =
This is hard-coded syntax in constraints; I&#39;ve not implemented type ope=
rators. (Mark you, it&#39;s also hard-coded in GHC, because (~) is a reserv=
ed symbol.)</div><div dir=3D"auto"><br></div><div dir=3D"auto">This needed =
only changes to the yacc syntax. From that I insisted that &quot;~&quot; is=
 a legitimate name for a class. There is a downside that equality constrain=
ts are printed with the &#39;~&#39; prefix -- because all class names are p=
refix.</div><div dir=3D"auto"><br></div><div dir=3D"auto">To be clear: this=
 is not as powerful or well-integrated as (~) in GHC. You still need at the=
 term level to explicitly cast.</div><div dir=3D"auto"><br></div><div dir=
=3D"auto">Then I got so fed up writing out explicit typeCast calls ..., I p=
icked up the postfix operators idea</div><div dir=3D"auto"><div><a href=3D"=
https://mail.haskell.org/pipermail/hugs-users/2018-September/000909.html">h=
ttps://mail.haskell.org/pipermail/hugs-users/2018-September/000909.html</a>=
</div>and invented a postfix operator (~::) to do the job.</div><div dir=3D=
"auto">Trailing double-colon says I&#39;m doing something typeful; tilde co=
nnects it to the equality constraint.</div><div dir=3D"auto"><br></div><div=
 dir=3D"auto">Then here&#39;s the classic, compiled in Hugs, also exhibitin=
g the FunDeps + Overlaps combo</div><div dir=3D"auto"><br></div><div dir=3D=
"auto">class TypeEq t t&#39; r =C2=A0| t t&#39; -&gt; r =C2=A0where</div><d=
iv dir=3D"auto">=C2=A0 typeEq :: t -&gt; t&#39; -&gt; r</div><div dir=3D"au=
to"><br></div><div dir=3D"auto">instance TypeEq t t TTrue =C2=A0where</div>=
<div dir=3D"auto">=C2=A0 typeEq _ _ =3D TTrue</div><div dir=3D"auto"><br></=
div><div dir=3D"auto">instance (TFalse ~ f) =3D&gt; TypeEq t t&#39; f =C2=
=A0where</div><div dir=3D"auto">=C2=A0 typeEq _ _ =3D (TFalse ~::) =C2=A0 =
=C2=A0 =C2=A0-- without explicit cast, complains type is not general enough=
</div><div dir=3D"auto"><br></div><div dir=3D"auto"><br></div><div dir=3D"a=
uto">AntC</div></div></div>

--000000000000baeef00577c99b3d--

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

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KSHVncy1Vc2Vy
cyBtYWlsaW5nIGxpc3QKSHVncy1Vc2Vyc0BoYXNrZWxsLm9yZwpodHRwOi8vbWFpbC5oYXNrZWxs
Lm9yZy9jZ2ktYmluL21haWxtYW4vbGlzdGluZm8vaHVncy11c2Vycwo=

--===============6629337142488773205==--