[Hugs-users] Undocumented featurette: Postfix operators

Anthony Clayden anthony_clayden at clear.net.nz
Sun Sep 30 06:17:59 UTC 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/hugs-users/attachments/20180930/396a3eee/attachment.html>


More information about the Hugs-Users mailing list