Undocumented featurette: Postfix operators

Anthony Clayden <[email protected]> Sun, 30 Sep 2018 19:17:59 +1300
Newsgroups gmane.comp.lang.haskell.hugs.user
Message-ID <CAM7nRYQ=wU8wzNm1v8auS12FkuE+njmwsAR-0oAqtt+td0ngLg@mail.gmail.com>
--===============6002406240689284051==
Content-Type: multipart/alternative; boundary="000000000000280e04057710a77d"

--000000000000280e04057710a77d
Content-Type: text/plain; charset="UTF-8"

There's an extension to GHC: 'Postfix Operators' .
https://downloads.haskell.org/~ghc/8.6.1/docs/html/users_guide/glasgow_exts.html#postfix-operators

Turns out Hugs can do that too (in Hugs mode).

> (!) :: Num a => a -> a     -- note defined as monadic

> (!) 0 = 1                          -- lhs of equation must use prefix form

> (!) n = n *  ((n - 1) !)       -- but rhs can use postfix

So (3 !) returns 6, etc. The parens are needed so that it's parsed as a
section.

I also took a leaf out of Oleg's book and built a variadic postfix operator
http://okmij.org/ftp/Haskell/polyvariadic.html

That is, (3 ~- 7) is parsed as 'subtract 3 from 7'. (3 ~-) is parsed as
postfix negate 3 aka 'subtract 3 from zero'. It kinda worked, but the terms
need a lot of explicit signatures and/or tricky type casting.


AntC

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

<div dir=3D"auto">There&#39;s an extension to GHC: &#39;Postfix Operators&#=
39; .<div><a href=3D"https://downloads.haskell.org/~ghc/8.6.1/docs/html/use=
rs_guide/glasgow_exts.html#postfix-operators">https://downloads.haskell.org=
/~ghc/8.6.1/docs/html/users_guide/glasgow_exts.html#postfix-operators</a></=
div><div dir=3D"auto"><br></div><div dir=3D"auto">Turns out Hugs can do tha=
t too (in Hugs mode).</div><div dir=3D"auto"><br></div><div dir=3D"auto">&g=
t;=C2=A0<span style=3D"font-size:12pt;font-family:Helvetica">(!) :: Num a =
=3D&gt; a -&gt; a </span><span style=3D"font-size:12pt;font-family:Helvetic=
a">=C2=A0 =C2=A0=C2=A0</span><span style=3D"font-size:12pt;font-family:Helv=
etica">-- note defined as monadic</span></div>
<p style=3D"margin:0px;font-size:12px;line-height:normal;font-family:Helvet=
ica"><span style=3D"font-size:12pt">&gt; (!) 0 =3D 1 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- lhs=
 of equation must use prefix form</span><br></p>
<p style=3D"margin:0px;font-size:12px;line-height:normal;font-family:Helvet=
ica"><span style=3D"font-size:12pt">&gt; (!) n =3D n *=C2=A0 ((n - 1) !) =
=C2=A0 =C2=A0 =C2=A0 -- but rhs can use postfix</span></p><div dir=3D"auto"=
><span style=3D"font-size:12pt"><br></span></div><div dir=3D"auto"><span st=
yle=3D"font-size:12pt">So (3 !) returns 6, etc. The parens are needed so th=
at it&#39;s parsed as a section.</span></div><div dir=3D"auto"><span style=
=3D"font-size:12pt"><br></span></div><div dir=3D"auto"><span style=3D"font-=
size:12pt">I also took a leaf out of Oleg&#39;s book and built a variadic p=
ostfix operator</span></div><div dir=3D"auto"><span style=3D"font-size:12pt=
"><div><a href=3D"http://okmij.org/ftp/Haskell/polyvariadic.html">http://ok=
mij.org/ftp/Haskell/polyvariadic.html</a></div><br></span></div><div dir=3D=
"auto"><span style=3D"font-size:12pt">That is, (3 ~- 7) is parsed as &#39;s=
ubtract 3 from 7&#39;. (3 ~-) is parsed as postfix negate 3 aka &#39;subtra=
ct 3 from zero&#39;. It kinda worked, but the terms need a lot of explicit =
signatures and/or tricky type casting.</span></div><div dir=3D"auto"><span =
style=3D"font-size:12pt"><br></span></div><div dir=3D"auto"><span style=3D"=
font-size:12pt"><br></span></div><div dir=3D"auto"><span style=3D"font-size=
:12pt">AntC</span></div></div>

--000000000000280e04057710a77d--

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

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KSHVncy1Vc2Vy
cyBtYWlsaW5nIGxpc3QKSHVncy1Vc2Vyc0BoYXNrZWxsLm9yZwpodHRwOi8vbWFpbC5oYXNrZWxs
Lm9yZy9jZ2ktYmluL21haWxtYW4vbGlzdGluZm8vaHVncy11c2Vycwo=

--===============6002406240689284051==--