[Haskell-cafe] binary operator modifiers

Tim Newsham newsham at lava.net
Mon Oct 29 03:36:18 EDT 2007


I would love to have the ability to define binary operator modifiers.
For example:

   f \overline{op} g       = liftM2 op f g
   f \overleftarrow{op} n  = liftM2 op f (return n)
   n \overrightarrow{op} g = liftM2 op (return n) f
   \widehat{f} x           = liftM f x

so that for example you could define functions like:

    (*3) \overline{+} (/2)

and

    3 \overrightarrow{+} \widehat{read} getContents

Obviously you could write this out the long way:

    liftM2 (3+) $ liftM read getContents

or go through the trouble of defining a bunch of binops

    f <+> g = liftM2 (+) f g
    n +>  g = return n <+> g
    f <+  n = f <+> return n
    read' = liftM read

    (*3) <+> (/2)
    3 +> read' getContents

but doing this for more than one or two operators gets tedious
quickly...

Is there any way in Haskell to modify binops in this way and still
be able to use them infix?  If not, has anyone considered supporting
strange syntaxes like this?

Tim Newsham
http://www.thenewsh.com/~newsham/


More information about the Haskell-Cafe mailing list